]> git.proxmox.com Git - ovs.git/blame - lib/stp.c
stp: Change the api for next patch.
[ovs.git] / lib / stp.c
CommitLineData
829a7d02 1/*
49558bf6 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2016 Nicira, Inc.
829a7d02
JP
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/* Based on sample implementation in 802.1D-1998. Above copyright and license
18 * applies to all modifications. */
19
20#include <config.h>
21
22#include "stp.h"
23#include <sys/types.h>
24#include <netinet/in.h>
25#include <arpa/inet.h>
829a7d02
JP
26#include <inttypes.h>
27#include <stdlib.h>
28#include "byte-order.h"
f23d157c 29#include "connectivity.h"
64c96779 30#include "openvswitch/ofpbuf.h"
e6211adc 31#include "ovs-atomic.h"
cf62fa4c 32#include "dp-packet.h"
829a7d02 33#include "packets.h"
f23d157c 34#include "seq.h"
fe4a02e4 35#include "unixctl.h"
829a7d02 36#include "util.h"
e6211adc 37#include "openvswitch/vlog.h"
829a7d02
JP
38
39VLOG_DEFINE_THIS_MODULE(stp);
40
11306274
AW
41static struct vlog_rate_limit stp_rl = VLOG_RATE_LIMIT_INIT(60, 60);
42
829a7d02
JP
43#define STP_PROTOCOL_ID 0x0000
44#define STP_PROTOCOL_VERSION 0x00
45#define STP_TYPE_CONFIG 0x00
46#define STP_TYPE_TCN 0x80
47
13b6bae6 48OVS_PACKED(
829a7d02
JP
49struct stp_bpdu_header {
50 ovs_be16 protocol_id; /* STP_PROTOCOL_ID. */
51 uint8_t protocol_version; /* STP_PROTOCOL_VERSION. */
52 uint8_t bpdu_type; /* One of STP_TYPE_*. */
13b6bae6 53});
829a7d02
JP
54BUILD_ASSERT_DECL(sizeof(struct stp_bpdu_header) == 4);
55
56enum stp_config_bpdu_flags {
57 STP_CONFIG_TOPOLOGY_CHANGE_ACK = 0x80,
58 STP_CONFIG_TOPOLOGY_CHANGE = 0x01
59};
60
13b6bae6 61OVS_PACKED(
829a7d02
JP
62struct stp_config_bpdu {
63 struct stp_bpdu_header header; /* Type STP_TYPE_CONFIG. */
64 uint8_t flags; /* STP_CONFIG_* flags. */
65 ovs_be64 root_id; /* 8.5.1.1: Bridge believed to be root. */
66 ovs_be32 root_path_cost; /* 8.5.1.2: Cost of path to root. */
67 ovs_be64 bridge_id; /* 8.5.1.3: ID of transmitting bridge. */
68 ovs_be16 port_id; /* 8.5.1.4: Port transmitting the BPDU. */
69 ovs_be16 message_age; /* 8.5.1.5: Age of BPDU at tx time. */
70 ovs_be16 max_age; /* 8.5.1.6: Timeout for received data. */
71 ovs_be16 hello_time; /* 8.5.1.7: Time between BPDU generation. */
72 ovs_be16 forward_delay; /* 8.5.1.8: State progression delay. */
13b6bae6 73});
829a7d02
JP
74BUILD_ASSERT_DECL(sizeof(struct stp_config_bpdu) == 35);
75
13b6bae6 76OVS_PACKED(
829a7d02
JP
77struct stp_tcn_bpdu {
78 struct stp_bpdu_header header; /* Type STP_TYPE_TCN. */
13b6bae6 79});
829a7d02
JP
80BUILD_ASSERT_DECL(sizeof(struct stp_tcn_bpdu) == 4);
81
82struct stp_timer {
83 bool active; /* Timer in use? */
84 int value; /* Current value of timer, counting up. */
85};
86
87struct stp_port {
88 struct stp *stp;
11306274 89 char *port_name; /* Human-readable name for log messages. */
3310b570 90 void *aux; /* Auxiliary data the user may retrieve. */
829a7d02
JP
91 int port_id; /* 8.5.5.1: Unique port identifier. */
92 enum stp_state state; /* 8.5.5.2: Current state. */
93 int path_cost; /* 8.5.5.3: Cost of tx/rx on this port. */
94 stp_identifier designated_root; /* 8.5.5.4. */
95 int designated_cost; /* 8.5.5.5: Path cost to root on port. */
96 stp_identifier designated_bridge; /* 8.5.5.6. */
97 int designated_port; /* 8.5.5.7: Port to send config msgs on. */
98 bool topology_change_ack; /* 8.5.5.8: Flag for next config BPDU. */
99 bool config_pending; /* 8.5.5.9: Send BPDU when hold expires? */
100 bool change_detection_enabled; /* 8.5.5.10: Detect topology changes? */
101
102 struct stp_timer message_age_timer; /* 8.5.6.1: Age of received info. */
103 struct stp_timer forward_delay_timer; /* 8.5.6.2: State change timer. */
104 struct stp_timer hold_timer; /* 8.5.6.3: BPDU rate limit timer. */
105
80740385
JP
106 int tx_count; /* Number of BPDUs transmitted. */
107 int rx_count; /* Number of valid BPDUs received. */
108 int error_count; /* Number of bad BPDUs received. */
109
829a7d02
JP
110 bool state_changed;
111};
112
113struct stp {
ca6ba700 114 struct ovs_list node; /* Node in all_stps list. */
fe4a02e4 115
829a7d02
JP
116 /* Static bridge data. */
117 char *name; /* Human-readable name for log messages. */
118 stp_identifier bridge_id; /* 8.5.3.7: This bridge. */
119 int max_age; /* 8.5.3.4: Time to drop received data. */
120 int hello_time; /* 8.5.3.5: Time between sending BPDUs. */
121 int forward_delay; /* 8.5.3.6: Delay between state changes. */
122 int bridge_max_age; /* 8.5.3.8: max_age when we're root. */
123 int bridge_hello_time; /* 8.5.3.9: hello_time as root. */
124 int bridge_forward_delay; /* 8.5.3.10: forward_delay as root. */
125 int rq_max_age; /* User-requested max age, in ms. */
126 int rq_hello_time; /* User-requested hello time, in ms. */
127 int rq_forward_delay; /* User-requested forward delay, in ms. */
128 int elapsed_remainder; /* Left-over msecs from last stp_tick(). */
129
130 /* Dynamic bridge data. */
131 stp_identifier designated_root; /* 8.5.3.1: Bridge believed to be root. */
132 unsigned int root_path_cost; /* 8.5.3.2: Cost of path to root. */
133 struct stp_port *root_port; /* 8.5.3.3: Lowest cost port to root. */
134 bool topology_change_detected; /* 8.5.3.11: Detected a topology change? */
135 bool topology_change; /* 8.5.3.12: Received topology change? */
136
137 /* Bridge timers. */
138 struct stp_timer hello_timer; /* 8.5.4.1: Hello timer. */
139 struct stp_timer tcn_timer; /* 8.5.4.2: Topology change timer. */
140 struct stp_timer topology_change_timer; /* 8.5.4.3. */
141
142 /* Ports. */
143 struct stp_port ports[STP_MAX_PORTS];
144
145 /* Interface to client. */
6ae50723 146 bool fdb_needs_flush; /* MAC learning tables needs flushing. */
829a7d02 147 struct stp_port *first_changed_port;
cf62fa4c 148 void (*send_bpdu)(struct dp_packet *bpdu, int port_no, void *aux);
829a7d02 149 void *aux;
bd54dbcd 150
37bec3d3 151 struct ovs_refcount ref_cnt;
829a7d02
JP
152};
153
bd54dbcd 154static struct ovs_mutex mutex;
55951e15 155static struct ovs_list all_stps__ = OVS_LIST_INITIALIZER(&all_stps__);
ca6ba700 156static struct ovs_list *const all_stps OVS_GUARDED_BY(mutex) = &all_stps__;
fe4a02e4 157
829a7d02
JP
158#define FOR_EACH_ENABLED_PORT(PORT, STP) \
159 for ((PORT) = stp_next_enabled_port((STP), (STP)->ports); \
160 (PORT); \
161 (PORT) = stp_next_enabled_port((STP), (PORT) + 1))
162static struct stp_port *
163stp_next_enabled_port(const struct stp *stp, const struct stp_port *port)
bd3950dd 164 OVS_REQUIRES(mutex)
829a7d02
JP
165{
166 for (; port < &stp->ports[ARRAY_SIZE(stp->ports)]; port++) {
167 if (port->state != STP_DISABLED) {
ebc56baa 168 return CONST_CAST(struct stp_port *, port);
829a7d02
JP
169 }
170 }
171 return NULL;
172}
173
174#define MESSAGE_AGE_INCREMENT 1
175
bd3950dd 176static void stp_transmit_config(struct stp_port *) OVS_REQUIRES(mutex);
829a7d02 177static bool stp_supersedes_port_info(const struct stp_port *,
bd54dbcd 178 const struct stp_config_bpdu *)
bd3950dd 179 OVS_REQUIRES(mutex);
829a7d02 180static void stp_record_config_information(struct stp_port *,
bd54dbcd 181 const struct stp_config_bpdu *)
bd3950dd 182 OVS_REQUIRES(mutex);
829a7d02 183static void stp_record_config_timeout_values(struct stp *,
bd54dbcd 184 const struct stp_config_bpdu *)
bd3950dd 185 OVS_REQUIRES(mutex);
bd54dbcd 186static bool stp_is_designated_port(const struct stp_port *)
bd3950dd
AW
187 OVS_REQUIRES(mutex);
188static void stp_config_bpdu_generation(struct stp *) OVS_REQUIRES(mutex);
189static void stp_transmit_tcn(struct stp *) OVS_REQUIRES(mutex);
190static void stp_configuration_update(struct stp *) OVS_REQUIRES(mutex);
829a7d02 191static bool stp_supersedes_root(const struct stp_port *root,
bd3950dd
AW
192 const struct stp_port *) OVS_REQUIRES(mutex);
193static void stp_root_selection(struct stp *) OVS_REQUIRES(mutex);
194static void stp_designated_port_selection(struct stp *) OVS_REQUIRES(mutex);
bd54dbcd 195static void stp_become_designated_port(struct stp_port *)
bd3950dd
AW
196 OVS_REQUIRES(mutex);
197static void stp_port_state_selection(struct stp *) OVS_REQUIRES(mutex);
198static void stp_make_forwarding(struct stp_port *) OVS_REQUIRES(mutex);
199static void stp_make_blocking(struct stp_port *) OVS_REQUIRES(mutex);
bd54dbcd 200static void stp_set_port_state(struct stp_port *, enum stp_state)
bd3950dd
AW
201 OVS_REQUIRES(mutex);
202static void stp_topology_change_detection(struct stp *) OVS_REQUIRES(mutex);
bd54dbcd 203static void stp_topology_change_acknowledged(struct stp *)
bd3950dd 204 OVS_REQUIRES(mutex);
bd54dbcd 205static void stp_acknowledge_topology_change(struct stp_port *)
bd3950dd 206 OVS_REQUIRES(mutex);
829a7d02 207static void stp_received_config_bpdu(struct stp *, struct stp_port *,
bd54dbcd 208 const struct stp_config_bpdu *)
bd3950dd 209 OVS_REQUIRES(mutex);
bd54dbcd 210static void stp_received_tcn_bpdu(struct stp *, struct stp_port *)
bd3950dd
AW
211 OVS_REQUIRES(mutex);
212static void stp_hello_timer_expiry(struct stp *) OVS_REQUIRES(mutex);
bd54dbcd 213static void stp_message_age_timer_expiry(struct stp_port *)
bd3950dd 214 OVS_REQUIRES(mutex);
bd54dbcd 215static bool stp_is_designated_for_some_port(const struct stp *)
bd3950dd 216 OVS_REQUIRES(mutex);
bd54dbcd 217static void stp_forward_delay_timer_expiry(struct stp_port *)
bd3950dd
AW
218 OVS_REQUIRES(mutex);
219static void stp_tcn_timer_expiry(struct stp *) OVS_REQUIRES(mutex);
bd54dbcd 220static void stp_topology_change_timer_expiry(struct stp *)
bd3950dd
AW
221 OVS_REQUIRES(mutex);
222static void stp_hold_timer_expiry(struct stp_port *) OVS_REQUIRES(mutex);
bd54dbcd 223static void stp_initialize_port(struct stp_port *, enum stp_state)
bd3950dd
AW
224 OVS_REQUIRES(mutex);
225static void stp_become_root_bridge(struct stp *) OVS_REQUIRES(mutex);
226static void stp_update_bridge_timers(struct stp *) OVS_REQUIRES(mutex);
829a7d02
JP
227
228static int clamp(int x, int min, int max);
229static int ms_to_timer(int ms);
829a7d02
JP
230static int timer_to_ms(int timer);
231static void stp_start_timer(struct stp_timer *, int value);
232static void stp_stop_timer(struct stp_timer *);
233static bool stp_timer_expired(struct stp_timer *, int elapsed, int timeout);
234
bd54dbcd 235static void stp_send_bpdu(struct stp_port *, const void *, size_t)
bd3950dd 236 OVS_REQUIRES(mutex);
fe4a02e4
EJ
237static void stp_unixctl_tcn(struct unixctl_conn *, int argc,
238 const char *argv[], void *aux);
239
240void
241stp_init(void)
242{
49558bf6
BP
243 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
244
245 if (ovsthread_once_start(&once)) {
246 /* We need a recursive mutex because stp_send_bpdu() could loop back
247 * into the stp module through a patch port. This happens
248 * intentionally as part of the unit tests. Ideally we'd ditch
249 * the call back function, but for now this is what we have. */
250 ovs_mutex_init_recursive(&mutex);
251
252 unixctl_command_register("stp/tcn", "[bridge]", 0, 1, stp_unixctl_tcn,
253 NULL);
254 ovsthread_once_done(&once);
255 }
fe4a02e4 256}
829a7d02
JP
257
258/* Creates and returns a new STP instance that initially has no ports enabled.
259 *
260 * 'bridge_id' should be a 48-bit MAC address as returned by
261 * eth_addr_to_uint64(). 'bridge_id' may also have a priority value in its top
262 * 16 bits; if those bits are set to 0, STP_DEFAULT_BRIDGE_PRIORITY is used.
263 * (This priority may be changed with stp_set_bridge_priority().)
264 *
265 * When the bridge needs to send out a BPDU, it calls 'send_bpdu'. This
266 * callback may be called from stp_tick() or stp_received_bpdu(). The
267 * arguments to 'send_bpdu' are an STP BPDU encapsulated in 'bpdu',
268 * the spanning tree port number 'port_no' that should transmit the
269 * packet, and auxiliary data to be passed to the callback in 'aux'.
270 */
271struct stp *
272stp_create(const char *name, stp_identifier bridge_id,
cf62fa4c 273 void (*send_bpdu)(struct dp_packet *bpdu, int port_no, void *aux),
829a7d02
JP
274 void *aux)
275{
276 struct stp *stp;
277 struct stp_port *p;
278
49558bf6 279 stp_init();
bd54dbcd
EJ
280
281 ovs_mutex_lock(&mutex);
829a7d02
JP
282 stp = xzalloc(sizeof *stp);
283 stp->name = xstrdup(name);
284 stp->bridge_id = bridge_id;
285 if (!(stp->bridge_id >> 48)) {
286 stp->bridge_id |= (uint64_t) STP_DEFAULT_BRIDGE_PRIORITY << 48;
287 }
288
3310b570
JP
289 stp->rq_max_age = STP_DEFAULT_MAX_AGE;
290 stp->rq_hello_time = STP_DEFAULT_HELLO_TIME;
291 stp->rq_forward_delay = STP_DEFAULT_FWD_DELAY;
829a7d02
JP
292 stp_update_bridge_timers(stp);
293 stp->max_age = stp->bridge_max_age;
294 stp->hello_time = stp->bridge_hello_time;
295 stp->forward_delay = stp->bridge_forward_delay;
296
297 stp->designated_root = stp->bridge_id;
298 stp->root_path_cost = 0;
299 stp->root_port = NULL;
300 stp->topology_change_detected = false;
301 stp->topology_change = false;
302
303 stp_stop_timer(&stp->tcn_timer);
304 stp_stop_timer(&stp->topology_change_timer);
ef0bd569 305 stp_start_timer(&stp->hello_timer, stp->hello_time);
829a7d02
JP
306
307 stp->send_bpdu = send_bpdu;
308 stp->aux = aux;
309
310 stp->first_changed_port = &stp->ports[ARRAY_SIZE(stp->ports)];
311 for (p = stp->ports; p < &stp->ports[ARRAY_SIZE(stp->ports)]; p++) {
312 p->stp = stp;
313 p->port_id = (stp_port_no(p) + 1) | (STP_DEFAULT_PORT_PRIORITY << 8);
314 p->path_cost = 19; /* Recommended default for 100 Mb/s link. */
315 stp_initialize_port(p, STP_DISABLED);
316 }
37bec3d3 317 ovs_refcount_init(&stp->ref_cnt);
bd54dbcd 318
417e7e66 319 ovs_list_push_back(all_stps, &stp->node);
bd54dbcd
EJ
320 ovs_mutex_unlock(&mutex);
321 return stp;
322}
323
324struct stp *
325stp_ref(const struct stp *stp_)
326{
327 struct stp *stp = CONST_CAST(struct stp *, stp_);
328 if (stp) {
37bec3d3 329 ovs_refcount_ref(&stp->ref_cnt);
bd54dbcd 330 }
829a7d02
JP
331 return stp;
332}
333
334/* Destroys 'stp'. */
335void
bd54dbcd 336stp_unref(struct stp *stp)
829a7d02 337{
24f83812 338 if (stp && ovs_refcount_unref_relaxed(&stp->ref_cnt) == 1) {
11306274
AW
339 size_t i;
340
bd54dbcd 341 ovs_mutex_lock(&mutex);
417e7e66 342 ovs_list_remove(&stp->node);
bd54dbcd 343 ovs_mutex_unlock(&mutex);
829a7d02 344 free(stp->name);
11306274
AW
345
346 for (i = 0; i < STP_MAX_PORTS; i++) {
347 free(stp->ports[i].port_name);
348 }
829a7d02
JP
349 free(stp);
350 }
351}
352
353/* Runs 'stp' given that 'ms' milliseconds have passed. */
354void
355stp_tick(struct stp *stp, int ms)
356{
357 struct stp_port *p;
358 int elapsed;
359
bd54dbcd 360 ovs_mutex_lock(&mutex);
829a7d02
JP
361 /* Convert 'ms' to STP timer ticks. Preserve any leftover milliseconds
362 * from previous stp_tick() calls so that we don't lose STP ticks when we
363 * are called too frequently. */
364 ms = clamp(ms, 0, INT_MAX - 1000) + stp->elapsed_remainder;
365 elapsed = ms_to_timer(ms);
ba25773f 366 stp->elapsed_remainder = ms - timer_to_ms(elapsed);
829a7d02 367 if (!elapsed) {
bd54dbcd 368 goto out;
829a7d02
JP
369 }
370
371 if (stp_timer_expired(&stp->hello_timer, elapsed, stp->hello_time)) {
372 stp_hello_timer_expiry(stp);
373 }
374 if (stp_timer_expired(&stp->tcn_timer, elapsed, stp->bridge_hello_time)) {
375 stp_tcn_timer_expiry(stp);
376 }
377 if (stp_timer_expired(&stp->topology_change_timer, elapsed,
378 stp->max_age + stp->forward_delay)) {
379 stp_topology_change_timer_expiry(stp);
380 }
381 FOR_EACH_ENABLED_PORT (p, stp) {
382 if (stp_timer_expired(&p->message_age_timer, elapsed, stp->max_age)) {
383 stp_message_age_timer_expiry(p);
384 }
385 }
386 FOR_EACH_ENABLED_PORT (p, stp) {
387 if (stp_timer_expired(&p->forward_delay_timer, elapsed,
388 stp->forward_delay)) {
389 stp_forward_delay_timer_expiry(p);
390 }
391 if (stp_timer_expired(&p->hold_timer, elapsed, ms_to_timer(1000))) {
392 stp_hold_timer_expiry(p);
393 }
394 }
bd54dbcd
EJ
395
396out:
397 ovs_mutex_unlock(&mutex);
829a7d02
JP
398}
399
400static void
401set_bridge_id(struct stp *stp, stp_identifier new_bridge_id)
bd3950dd 402 OVS_REQUIRES(mutex)
829a7d02
JP
403{
404 if (new_bridge_id != stp->bridge_id) {
405 bool root;
406 struct stp_port *p;
407
408 root = stp_is_root_bridge(stp);
409 FOR_EACH_ENABLED_PORT (p, stp) {
410 if (stp_is_designated_port(p)) {
411 p->designated_bridge = new_bridge_id;
412 }
413 }
414 stp->bridge_id = new_bridge_id;
415 stp_configuration_update(stp);
416 stp_port_state_selection(stp);
417 if (stp_is_root_bridge(stp) && !root) {
418 stp_become_root_bridge(stp);
419 }
420 }
421}
422
423void
424stp_set_bridge_id(struct stp *stp, stp_identifier bridge_id)
425{
426 const uint64_t mac_bits = (UINT64_C(1) << 48) - 1;
427 const uint64_t pri_bits = ~mac_bits;
bd54dbcd 428 ovs_mutex_lock(&mutex);
829a7d02 429 set_bridge_id(stp, (stp->bridge_id & pri_bits) | (bridge_id & mac_bits));
bd54dbcd 430 ovs_mutex_unlock(&mutex);
829a7d02
JP
431}
432
433void
434stp_set_bridge_priority(struct stp *stp, uint16_t new_priority)
435{
436 const uint64_t mac_bits = (UINT64_C(1) << 48) - 1;
bd54dbcd 437 ovs_mutex_lock(&mutex);
829a7d02
JP
438 set_bridge_id(stp, ((stp->bridge_id & mac_bits)
439 | ((uint64_t) new_priority << 48)));
bd54dbcd 440 ovs_mutex_unlock(&mutex);
829a7d02
JP
441}
442
443/* Sets the desired hello time for 'stp' to 'ms', in milliseconds. The actual
444 * hello time is clamped to the range of 1 to 10 seconds and subject to the
445 * relationship (bridge_max_age >= 2 * (bridge_hello_time + 1 s)). The bridge
446 * hello time is only used when 'stp' is the root bridge. */
447void
448stp_set_hello_time(struct stp *stp, int ms)
449{
bd54dbcd 450 ovs_mutex_lock(&mutex);
829a7d02
JP
451 stp->rq_hello_time = ms;
452 stp_update_bridge_timers(stp);
bd54dbcd 453 ovs_mutex_unlock(&mutex);
829a7d02
JP
454}
455
456/* Sets the desired max age for 'stp' to 'ms', in milliseconds. The actual max
457 * age is clamped to the range of 6 to 40 seconds and subject to the
458 * relationships (2 * (bridge_forward_delay - 1 s) >= bridge_max_age) and
459 * (bridge_max_age >= 2 * (bridge_hello_time + 1 s)). The bridge max age is
460 * only used when 'stp' is the root bridge. */
461void
462stp_set_max_age(struct stp *stp, int ms)
463{
bd54dbcd 464 ovs_mutex_lock(&mutex);
829a7d02
JP
465 stp->rq_max_age = ms;
466 stp_update_bridge_timers(stp);
bd54dbcd 467 ovs_mutex_unlock(&mutex);
829a7d02
JP
468}
469
470/* Sets the desired forward delay for 'stp' to 'ms', in milliseconds. The
471 * actual forward delay is clamped to the range of 4 to 30 seconds and subject
472 * to the relationship (2 * (bridge_forward_delay - 1 s) >= bridge_max_age).
473 * The bridge forward delay is only used when 'stp' is the root bridge. */
474void
475stp_set_forward_delay(struct stp *stp, int ms)
476{
bd54dbcd 477 ovs_mutex_lock(&mutex);
829a7d02
JP
478 stp->rq_forward_delay = ms;
479 stp_update_bridge_timers(stp);
bd54dbcd 480 ovs_mutex_unlock(&mutex);
829a7d02
JP
481}
482
483/* Returns the name given to 'stp' in the call to stp_create(). */
484const char *
485stp_get_name(const struct stp *stp)
486{
bd54dbcd
EJ
487 char *name;
488
489 ovs_mutex_lock(&mutex);
490 name = stp->name;
491 ovs_mutex_unlock(&mutex);
492 return name;
829a7d02
JP
493}
494
495/* Returns the bridge ID for 'stp'. */
496stp_identifier
497stp_get_bridge_id(const struct stp *stp)
498{
bd54dbcd
EJ
499 stp_identifier bridge_id;
500
501 ovs_mutex_lock(&mutex);
502 bridge_id = stp->bridge_id;
503 ovs_mutex_unlock(&mutex);
504 return bridge_id;
829a7d02
JP
505}
506
507/* Returns the bridge ID of the bridge currently believed to be the root. */
508stp_identifier
509stp_get_designated_root(const struct stp *stp)
510{
bd54dbcd
EJ
511 stp_identifier designated_root;
512
513 ovs_mutex_lock(&mutex);
514 designated_root = stp->designated_root;
515 ovs_mutex_unlock(&mutex);
516 return designated_root;
829a7d02
JP
517}
518
519/* Returns true if 'stp' believes itself to the be root of the spanning tree,
520 * false otherwise. */
521bool
522stp_is_root_bridge(const struct stp *stp)
523{
bd54dbcd
EJ
524 bool is_root;
525
526 ovs_mutex_lock(&mutex);
527 is_root = stp->bridge_id == stp->designated_root;
528 ovs_mutex_unlock(&mutex);
529 return is_root;
829a7d02
JP
530}
531
532/* Returns the cost of the path from 'stp' to the root of the spanning tree. */
533int
534stp_get_root_path_cost(const struct stp *stp)
535{
bd54dbcd
EJ
536 int cost;
537
538 ovs_mutex_lock(&mutex);
539 cost = stp->root_path_cost;
540 ovs_mutex_unlock(&mutex);
541 return cost;
829a7d02
JP
542}
543
544/* Returns the bridge hello time, in ms. The returned value is not necessarily
545 * the value passed to stp_set_hello_time(): it is clamped to the valid range
546 * and quantized to the STP timer resolution. */
547int
548stp_get_hello_time(const struct stp *stp)
549{
bd54dbcd
EJ
550 int time;
551
552 ovs_mutex_lock(&mutex);
553 time = timer_to_ms(stp->bridge_hello_time);
554 ovs_mutex_unlock(&mutex);
555 return time;
829a7d02
JP
556}
557
558/* Returns the bridge max age, in ms. The returned value is not necessarily
559 * the value passed to stp_set_max_age(): it is clamped to the valid range,
560 * quantized to the STP timer resolution, and adjusted to match the constraints
561 * due to the hello time. */
562int
563stp_get_max_age(const struct stp *stp)
564{
bd54dbcd
EJ
565 int time;
566
567 ovs_mutex_lock(&mutex);
568 time = timer_to_ms(stp->bridge_max_age);
569 ovs_mutex_unlock(&mutex);
570 return time;
829a7d02
JP
571}
572
573/* Returns the bridge forward delay, in ms. The returned value is not
574 * necessarily the value passed to stp_set_forward_delay(): it is clamped to
575 * the valid range, quantized to the STP timer resolution, and adjusted to
576 * match the constraints due to the forward delay. */
577int
578stp_get_forward_delay(const struct stp *stp)
579{
bd54dbcd
EJ
580 int time;
581
582 ovs_mutex_lock(&mutex);
583 time = timer_to_ms(stp->bridge_forward_delay);
584 ovs_mutex_unlock(&mutex);
585 return time;
829a7d02
JP
586}
587
6ae50723
EJ
588/* Returns true if something has happened to 'stp' which necessitates flushing
589 * the client's MAC learning table. Calling this function resets 'stp' so that
590 * future calls will return false until flushing is required again. */
591bool
592stp_check_and_reset_fdb_flush(struct stp *stp)
593{
bd54dbcd
EJ
594 bool needs_flush;
595
596 ovs_mutex_lock(&mutex);
597 needs_flush = stp->fdb_needs_flush;
6ae50723 598 stp->fdb_needs_flush = false;
bd54dbcd 599 ovs_mutex_unlock(&mutex);
6ae50723
EJ
600 return needs_flush;
601}
602
829a7d02
JP
603/* Returns the port in 'stp' with index 'port_no', which must be between 0 and
604 * STP_MAX_PORTS. */
605struct stp_port *
606stp_get_port(struct stp *stp, int port_no)
607{
bd54dbcd
EJ
608 struct stp_port *port;
609
610 ovs_mutex_lock(&mutex);
cb22974d 611 ovs_assert(port_no >= 0 && port_no < ARRAY_SIZE(stp->ports));
bd54dbcd
EJ
612 port = &stp->ports[port_no];
613 ovs_mutex_unlock(&mutex);
614 return port;
829a7d02
JP
615}
616
617/* Returns the port connecting 'stp' to the root bridge, or a null pointer if
618 * there is no such port. */
619struct stp_port *
620stp_get_root_port(struct stp *stp)
621{
bd54dbcd
EJ
622 struct stp_port *port;
623
624 ovs_mutex_lock(&mutex);
625 port = stp->root_port;
626 ovs_mutex_unlock(&mutex);
627 return port;
829a7d02
JP
628}
629
630/* Finds a port whose state has changed. If successful, stores the port whose
631 * state changed in '*portp' and returns true. If no port has changed, stores
632 * NULL in '*portp' and returns false. */
633bool
634stp_get_changed_port(struct stp *stp, struct stp_port **portp)
635{
bd54dbcd
EJ
636 struct stp_port *end, *p;
637 bool changed = false;
829a7d02 638
bd54dbcd
EJ
639 ovs_mutex_lock(&mutex);
640 end = &stp->ports[ARRAY_SIZE(stp->ports)];
829a7d02
JP
641 for (p = stp->first_changed_port; p < end; p++) {
642 if (p->state_changed) {
643 p->state_changed = false;
644 stp->first_changed_port = p + 1;
645 *portp = p;
bd54dbcd
EJ
646 changed = true;
647 goto out;
829a7d02
JP
648 }
649 }
650 stp->first_changed_port = end;
651 *portp = NULL;
bd54dbcd
EJ
652
653out:
654 ovs_mutex_unlock(&mutex);
655 return changed;
829a7d02
JP
656}
657
658/* Returns the name for the given 'state' (for use in debugging and log
659 * messages). */
660const char *
661stp_state_name(enum stp_state state)
662{
663 switch (state) {
664 case STP_DISABLED:
665 return "disabled";
666 case STP_LISTENING:
667 return "listening";
668 case STP_LEARNING:
669 return "learning";
670 case STP_FORWARDING:
671 return "forwarding";
672 case STP_BLOCKING:
673 return "blocking";
674 default:
428b2edd 675 OVS_NOT_REACHED();
829a7d02
JP
676 }
677}
678
679/* Returns true if 'state' is one in which packets received on a port should
680 * be forwarded, false otherwise.
4b5f1996 681 */
829a7d02
JP
682bool
683stp_forward_in_state(enum stp_state state)
684{
4b5f1996 685 return (state & STP_FORWARDING) != 0;
829a7d02
JP
686}
687
688/* Returns true if 'state' is one in which MAC learning should be done on
689 * packets received on a port, false otherwise.
4b5f1996 690 */
829a7d02
JP
691bool
692stp_learn_in_state(enum stp_state state)
693{
4b5f1996 694 return (state & (STP_LEARNING | STP_FORWARDING)) != 0;
829a7d02
JP
695}
696
bacdb85a
AW
697/* Returns true if 'state' is one in which bpdus should be forwarded on a
698 * port, false otherwise.
699 *
700 * Returns true if 'state' is STP_DISABLED, since in that case the port does
701 * not generate the bpdu and should just forward it (e.g. patch port on pif
702 * bridge). */
0d1cee12 703bool
bacdb85a 704stp_should_forward_bpdu(enum stp_state state)
0d1cee12
K
705{
706 return (state &
bacdb85a
AW
707 ( STP_DISABLED | STP_LISTENING | STP_LEARNING
708 | STP_FORWARDING)) != 0;
0d1cee12
K
709}
710
3310b570
JP
711/* Returns the name for the given 'role' (for use in debugging and log
712 * messages). */
713const char *
714stp_role_name(enum stp_role role)
715{
716 switch (role) {
717 case STP_ROLE_ROOT:
718 return "root";
719 case STP_ROLE_DESIGNATED:
720 return "designated";
721 case STP_ROLE_ALTERNATE:
722 return "alternate";
723 case STP_ROLE_DISABLED:
724 return "disabled";
725 default:
428b2edd 726 OVS_NOT_REACHED();
3310b570
JP
727 }
728}
729
829a7d02
JP
730/* Notifies the STP entity that bridge protocol data unit 'bpdu', which is
731 * 'bpdu_size' bytes in length, was received on port 'p'.
732 *
733 * This function may call the 'send_bpdu' function provided to stp_create(). */
734void
735stp_received_bpdu(struct stp_port *p, const void *bpdu, size_t bpdu_size)
736{
737 struct stp *stp = p->stp;
738 const struct stp_bpdu_header *header;
739
bd54dbcd 740 ovs_mutex_lock(&mutex);
829a7d02 741 if (p->state == STP_DISABLED) {
bd54dbcd 742 goto out;
829a7d02
JP
743 }
744
745 if (bpdu_size < sizeof(struct stp_bpdu_header)) {
34582733 746 VLOG_WARN("%s: received runt %"PRIuSIZE"-byte BPDU", stp->name, bpdu_size);
80740385 747 p->error_count++;
bd54dbcd 748 goto out;
829a7d02
JP
749 }
750
751 header = bpdu;
752 if (header->protocol_id != htons(STP_PROTOCOL_ID)) {
753 VLOG_WARN("%s: received BPDU with unexpected protocol ID %"PRIu16,
754 stp->name, ntohs(header->protocol_id));
80740385 755 p->error_count++;
bd54dbcd 756 goto out;
829a7d02
JP
757 }
758 if (header->protocol_version != STP_PROTOCOL_VERSION) {
759 VLOG_DBG("%s: received BPDU with unexpected protocol version %"PRIu8,
760 stp->name, header->protocol_version);
761 }
762
763 switch (header->bpdu_type) {
764 case STP_TYPE_CONFIG:
765 if (bpdu_size < sizeof(struct stp_config_bpdu)) {
34582733 766 VLOG_WARN("%s: received config BPDU with invalid size %"PRIuSIZE,
829a7d02 767 stp->name, bpdu_size);
80740385 768 p->error_count++;
bd54dbcd 769 goto out;
829a7d02
JP
770 }
771 stp_received_config_bpdu(stp, p, bpdu);
772 break;
773
774 case STP_TYPE_TCN:
775 if (bpdu_size != sizeof(struct stp_tcn_bpdu)) {
34582733 776 VLOG_WARN("%s: received TCN BPDU with invalid size %"PRIuSIZE,
829a7d02 777 stp->name, bpdu_size);
80740385 778 p->error_count++;
bd54dbcd 779 goto out;
829a7d02
JP
780 }
781 stp_received_tcn_bpdu(stp, p);
782 break;
783
784 default:
785 VLOG_WARN("%s: received BPDU of unexpected type %"PRIu8,
786 stp->name, header->bpdu_type);
80740385 787 p->error_count++;
bd54dbcd 788 goto out;
829a7d02 789 }
80740385 790 p->rx_count++;
bd54dbcd
EJ
791
792out:
793 ovs_mutex_unlock(&mutex);
829a7d02
JP
794}
795
796/* Returns the STP entity in which 'p' is nested. */
797struct stp *
798stp_port_get_stp(struct stp_port *p)
799{
bd54dbcd
EJ
800 struct stp *stp;
801
802 ovs_mutex_lock(&mutex);
803 stp = p->stp;
804 ovs_mutex_unlock(&mutex);
805 return stp;
829a7d02
JP
806}
807
11306274
AW
808void
809stp_port_set_name(struct stp_port *p, const char *name)
810{
811 char *old;
812
813 ovs_mutex_lock(&mutex);
814 old = p->port_name;
815 p->port_name = xstrdup(name);
816 free(old);
817 ovs_mutex_unlock(&mutex);
818}
819
3310b570
JP
820/* Sets the 'aux' member of 'p'.
821 *
822 * The 'aux' member will be reset to NULL when stp_port_disable() is
823 * called or stp_port_enable() is called when the port is in a Disabled
824 * state. */
825void
826stp_port_set_aux(struct stp_port *p, void *aux)
827{
bd54dbcd 828 ovs_mutex_lock(&mutex);
3310b570 829 p->aux = aux;
bd54dbcd 830 ovs_mutex_unlock(&mutex);
3310b570
JP
831}
832
833/* Returns the 'aux' member of 'p'. */
834void *
835stp_port_get_aux(struct stp_port *p)
836{
bd54dbcd
EJ
837 void *aux;
838
839 ovs_mutex_lock(&mutex);
840 aux = p->aux;
841 ovs_mutex_unlock(&mutex);
842 return aux;
3310b570
JP
843}
844
829a7d02
JP
845/* Returns the index of port 'p' within its bridge. */
846int
847stp_port_no(const struct stp_port *p)
848{
bd54dbcd
EJ
849 struct stp *stp;
850 int index;
851
852 ovs_mutex_lock(&mutex);
853 stp = p->stp;
cb22974d 854 ovs_assert(p >= stp->ports && p < &stp->ports[ARRAY_SIZE(stp->ports)]);
bd54dbcd
EJ
855 index = p - p->stp->ports;
856 ovs_mutex_unlock(&mutex);
857 return index;
829a7d02
JP
858}
859
860/* Returns the state of port 'p'. */
861enum stp_state
862stp_port_get_state(const struct stp_port *p)
863{
bd54dbcd
EJ
864 enum stp_state state;
865
866 ovs_mutex_lock(&mutex);
867 state = p->state;
868 ovs_mutex_unlock(&mutex);
869 return state;
829a7d02
JP
870}
871
3310b570 872/* Returns the role of port 'p'. */
926c9a4a 873static enum stp_role
874stp_port_get_role(const struct stp_port *p) OVS_REQUIRES(mutex)
3310b570 875{
bd54dbcd
EJ
876 struct stp_port *root_port;
877 enum stp_role role;
3310b570 878
bd54dbcd 879 root_port = p->stp->root_port;
3310b570 880 if (root_port && root_port->port_id == p->port_id) {
bd54dbcd 881 role = STP_ROLE_ROOT;
3310b570 882 } else if (stp_is_designated_port(p)) {
bd54dbcd 883 role = STP_ROLE_DESIGNATED;
3310b570 884 } else if (p->state == STP_DISABLED) {
bd54dbcd 885 role = STP_ROLE_DISABLED;
3310b570 886 } else {
bd54dbcd 887 role = STP_ROLE_ALTERNATE;
3310b570 888 }
bd54dbcd 889 return role;
3310b570
JP
890}
891
80740385 892/* Retrieves BPDU transmit and receive counts for 'p'. */
bd54dbcd
EJ
893void
894stp_port_get_counts(const struct stp_port *p,
895 int *tx_count, int *rx_count, int *error_count)
80740385 896{
bd54dbcd 897 ovs_mutex_lock(&mutex);
80740385
JP
898 *tx_count = p->tx_count;
899 *rx_count = p->rx_count;
900 *error_count = p->error_count;
bd54dbcd 901 ovs_mutex_unlock(&mutex);
80740385
JP
902}
903
926c9a4a 904void
905stp_port_get_status(const struct stp_port *p,
906 int *port_id, enum stp_state *state, enum stp_role *role)
907{
908 ovs_mutex_lock(&mutex);
909 *port_id = p->port_id;
910 *state = p->state;
911 *role = stp_port_get_role(p);
912 ovs_mutex_unlock(&mutex);
913}
914
829a7d02
JP
915/* Disables STP on port 'p'. */
916void
917stp_port_disable(struct stp_port *p)
918{
bd54dbcd
EJ
919 struct stp *stp;
920
921 ovs_mutex_lock(&mutex);
922 stp = p->stp;
829a7d02
JP
923 if (p->state != STP_DISABLED) {
924 bool root = stp_is_root_bridge(stp);
925 stp_become_designated_port(p);
926 stp_set_port_state(p, STP_DISABLED);
927 p->topology_change_ack = false;
928 p->config_pending = false;
929 stp_stop_timer(&p->message_age_timer);
930 stp_stop_timer(&p->forward_delay_timer);
931 stp_configuration_update(stp);
932 stp_port_state_selection(stp);
933 if (stp_is_root_bridge(stp) && !root) {
934 stp_become_root_bridge(stp);
935 }
3310b570 936 p->aux = NULL;
829a7d02 937 }
bd54dbcd 938 ovs_mutex_unlock(&mutex);
829a7d02
JP
939}
940
941/* Enables STP on port 'p'. The port will initially be in "blocking" state. */
942void
943stp_port_enable(struct stp_port *p)
944{
bd54dbcd 945 ovs_mutex_lock(&mutex);
829a7d02
JP
946 if (p->state == STP_DISABLED) {
947 stp_initialize_port(p, STP_BLOCKING);
948 stp_port_state_selection(p->stp);
949 }
bd54dbcd 950 ovs_mutex_unlock(&mutex);
829a7d02
JP
951}
952
953/* Sets the priority of port 'p' to 'new_priority'. Lower numerical values
954 * are interpreted as higher priorities. */
955void
956stp_port_set_priority(struct stp_port *p, uint8_t new_priority)
957{
bd54dbcd
EJ
958 uint16_t new_port_id;
959
960 ovs_mutex_lock(&mutex);
961 new_port_id = (p->port_id & 0xff) | (new_priority << 8);
829a7d02
JP
962 if (p->port_id != new_port_id) {
963 struct stp *stp = p->stp;
964 if (stp_is_designated_port(p)) {
965 p->designated_port = new_port_id;
966 }
967 p->port_id = new_port_id;
968 if (stp->bridge_id == p->designated_bridge
969 && p->port_id < p->designated_port) {
970 stp_become_designated_port(p);
971 stp_port_state_selection(stp);
972 }
973 }
bd54dbcd 974 ovs_mutex_unlock(&mutex);
829a7d02
JP
975}
976
3310b570
JP
977/* Convert 'speed' (measured in Mb/s) into the path cost. */
978uint16_t
979stp_convert_speed_to_cost(unsigned int speed)
980{
bd54dbcd
EJ
981 uint16_t ret;
982
983 ovs_mutex_lock(&mutex);
984 ret = speed >= 10000 ? 2 /* 10 Gb/s. */
985 : speed >= 1000 ? 4 /* 1 Gb/s. */
986 : speed >= 100 ? 19 /* 100 Mb/s. */
987 : speed >= 16 ? 62 /* 16 Mb/s. */
988 : speed >= 10 ? 100 /* 10 Mb/s. */
989 : speed >= 4 ? 250 /* 4 Mb/s. */
990 : 19; /* 100 Mb/s (guess). */
991 ovs_mutex_unlock(&mutex);
992 return ret;
3310b570
JP
993}
994
829a7d02
JP
995/* Sets the path cost of port 'p' to 'path_cost'. Lower values are generally
996 * used to indicate faster links. Use stp_port_set_speed() to automatically
997 * generate a default path cost from a link speed. */
998void
999stp_port_set_path_cost(struct stp_port *p, uint16_t path_cost)
1000{
bd54dbcd 1001 ovs_mutex_lock(&mutex);
829a7d02
JP
1002 if (p->path_cost != path_cost) {
1003 struct stp *stp = p->stp;
1004 p->path_cost = path_cost;
1005 stp_configuration_update(stp);
1006 stp_port_state_selection(stp);
1007 }
bd54dbcd 1008 ovs_mutex_unlock(&mutex);
829a7d02
JP
1009}
1010
1011/* Sets the path cost of port 'p' based on 'speed' (measured in Mb/s). */
1012void
1013stp_port_set_speed(struct stp_port *p, unsigned int speed)
1014{
3310b570 1015 stp_port_set_path_cost(p, stp_convert_speed_to_cost(speed));
829a7d02
JP
1016}
1017
1018/* Enables topology change detection on port 'p'. */
1019void
1020stp_port_enable_change_detection(struct stp_port *p)
1021{
1022 p->change_detection_enabled = true;
1023}
1024
1025/* Disables topology change detection on port 'p'. */
1026void
1027stp_port_disable_change_detection(struct stp_port *p)
1028{
1029 p->change_detection_enabled = false;
1030}
1031\f
1032static void
bd3950dd 1033stp_transmit_config(struct stp_port *p) OVS_REQUIRES(mutex)
829a7d02
JP
1034{
1035 struct stp *stp = p->stp;
1036 bool root = stp_is_root_bridge(stp);
1037 if (!root && !stp->root_port) {
1038 return;
1039 }
1040 if (p->hold_timer.active) {
11306274
AW
1041 VLOG_DBG_RL(&stp_rl, "bridge: %s, port: %s, transmit config bpdu pending",
1042 stp->name, p->port_name);
829a7d02
JP
1043 p->config_pending = true;
1044 } else {
1045 struct stp_config_bpdu config;
1046 memset(&config, 0, sizeof config);
1047 config.header.protocol_id = htons(STP_PROTOCOL_ID);
1048 config.header.protocol_version = STP_PROTOCOL_VERSION;
1049 config.header.bpdu_type = STP_TYPE_CONFIG;
1050 config.flags = 0;
1051 if (p->topology_change_ack) {
3310b570 1052 config.flags |= STP_CONFIG_TOPOLOGY_CHANGE_ACK;
829a7d02
JP
1053 }
1054 if (stp->topology_change) {
3310b570 1055 config.flags |= STP_CONFIG_TOPOLOGY_CHANGE;
829a7d02
JP
1056 }
1057 config.root_id = htonll(stp->designated_root);
1058 config.root_path_cost = htonl(stp->root_path_cost);
1059 config.bridge_id = htonll(stp->bridge_id);
1060 config.port_id = htons(p->port_id);
1061 if (root) {
1062 config.message_age = htons(0);
1063 } else {
1064 config.message_age = htons(stp->root_port->message_age_timer.value
1065 + MESSAGE_AGE_INCREMENT);
1066 }
1067 config.max_age = htons(stp->max_age);
1068 config.hello_time = htons(stp->hello_time);
1069 config.forward_delay = htons(stp->forward_delay);
1070 if (ntohs(config.message_age) < stp->max_age) {
1071 p->topology_change_ack = false;
1072 p->config_pending = false;
11306274
AW
1073 VLOG_DBG_RL(&stp_rl, "bridge: %s, port: %s, transmit config bpdu",
1074 stp->name, p->port_name);
829a7d02
JP
1075 stp_send_bpdu(p, &config, sizeof config);
1076 stp_start_timer(&p->hold_timer, 0);
1077 }
1078 }
1079}
1080
1081static bool
1082stp_supersedes_port_info(const struct stp_port *p,
1083 const struct stp_config_bpdu *config)
bd3950dd 1084 OVS_REQUIRES(mutex)
829a7d02
JP
1085{
1086 if (ntohll(config->root_id) != p->designated_root) {
1087 return ntohll(config->root_id) < p->designated_root;
1088 } else if (ntohl(config->root_path_cost) != p->designated_cost) {
1089 return ntohl(config->root_path_cost) < p->designated_cost;
1090 } else if (ntohll(config->bridge_id) != p->designated_bridge) {
1091 return ntohll(config->bridge_id) < p->designated_bridge;
1092 } else {
1093 return (ntohll(config->bridge_id) != p->stp->bridge_id
1094 || ntohs(config->port_id) <= p->designated_port);
1095 }
1096}
1097
1098static void
1099stp_record_config_information(struct stp_port *p,
1100 const struct stp_config_bpdu *config)
bd3950dd 1101 OVS_REQUIRES(mutex)
829a7d02
JP
1102{
1103 p->designated_root = ntohll(config->root_id);
1104 p->designated_cost = ntohl(config->root_path_cost);
1105 p->designated_bridge = ntohll(config->bridge_id);
1106 p->designated_port = ntohs(config->port_id);
1107 stp_start_timer(&p->message_age_timer, ntohs(config->message_age));
1108}
1109
1110static void
1111stp_record_config_timeout_values(struct stp *stp,
1112 const struct stp_config_bpdu *config)
bd3950dd 1113 OVS_REQUIRES(mutex)
829a7d02
JP
1114{
1115 stp->max_age = ntohs(config->max_age);
1116 stp->hello_time = ntohs(config->hello_time);
1117 stp->forward_delay = ntohs(config->forward_delay);
3310b570 1118 stp->topology_change = config->flags & STP_CONFIG_TOPOLOGY_CHANGE;
829a7d02
JP
1119}
1120
1121static bool
bd3950dd 1122stp_is_designated_port(const struct stp_port *p) OVS_REQUIRES(mutex)
829a7d02
JP
1123{
1124 return (p->designated_bridge == p->stp->bridge_id
1125 && p->designated_port == p->port_id);
1126}
1127
1128static void
bd3950dd 1129stp_config_bpdu_generation(struct stp *stp) OVS_REQUIRES(mutex)
829a7d02
JP
1130{
1131 struct stp_port *p;
1132
1133 FOR_EACH_ENABLED_PORT (p, stp) {
1134 if (stp_is_designated_port(p)) {
1135 stp_transmit_config(p);
1136 }
1137 }
1138}
1139
1140static void
bd3950dd 1141stp_transmit_tcn(struct stp *stp) OVS_REQUIRES(mutex)
829a7d02
JP
1142{
1143 struct stp_port *p = stp->root_port;
1144 struct stp_tcn_bpdu tcn_bpdu;
11306274 1145
829a7d02
JP
1146 if (!p) {
1147 return;
1148 }
11306274
AW
1149 VLOG_DBG_RL(&stp_rl, "bridge: %s, root port: %s, transmit tcn", stp->name,
1150 p->port_name);
829a7d02
JP
1151 tcn_bpdu.header.protocol_id = htons(STP_PROTOCOL_ID);
1152 tcn_bpdu.header.protocol_version = STP_PROTOCOL_VERSION;
1153 tcn_bpdu.header.bpdu_type = STP_TYPE_TCN;
1154 stp_send_bpdu(p, &tcn_bpdu, sizeof tcn_bpdu);
1155}
1156
1157static void
bd3950dd 1158stp_configuration_update(struct stp *stp) OVS_REQUIRES(mutex)
829a7d02
JP
1159{
1160 stp_root_selection(stp);
1161 stp_designated_port_selection(stp);
f23d157c 1162 seq_change(connectivity_seq_get());
829a7d02
JP
1163}
1164
1165static bool
1166stp_supersedes_root(const struct stp_port *root, const struct stp_port *p)
bd3950dd 1167 OVS_REQUIRES(mutex)
829a7d02
JP
1168{
1169 int p_cost = p->designated_cost + p->path_cost;
1170 int root_cost = root->designated_cost + root->path_cost;
1171
1172 if (p->designated_root != root->designated_root) {
1173 return p->designated_root < root->designated_root;
1174 } else if (p_cost != root_cost) {
1175 return p_cost < root_cost;
1176 } else if (p->designated_bridge != root->designated_bridge) {
1177 return p->designated_bridge < root->designated_bridge;
1178 } else if (p->designated_port != root->designated_port) {
1179 return p->designated_port < root->designated_port;
1180 } else {
1181 return p->port_id < root->port_id;
1182 }
1183}
1184
1185static void
bd3950dd 1186stp_root_selection(struct stp *stp) OVS_REQUIRES(mutex)
829a7d02
JP
1187{
1188 struct stp_port *p, *root;
1189
1190 root = NULL;
1191 FOR_EACH_ENABLED_PORT (p, stp) {
1192 if (stp_is_designated_port(p)
1193 || p->designated_root >= stp->bridge_id) {
1194 continue;
1195 }
1196 if (root && !stp_supersedes_root(root, p)) {
1197 continue;
1198 }
1199 root = p;
1200 }
1201 stp->root_port = root;
1202 if (!root) {
1203 stp->designated_root = stp->bridge_id;
1204 stp->root_path_cost = 0;
1205 } else {
1206 stp->designated_root = root->designated_root;
1207 stp->root_path_cost = root->designated_cost + root->path_cost;
1208 }
1209}
1210
1211static void
bd3950dd 1212stp_designated_port_selection(struct stp *stp) OVS_REQUIRES(mutex)
829a7d02
JP
1213{
1214 struct stp_port *p;
1215
1216 FOR_EACH_ENABLED_PORT (p, stp) {
1217 if (stp_is_designated_port(p)
1218 || p->designated_root != stp->designated_root
1219 || stp->root_path_cost < p->designated_cost
1220 || (stp->root_path_cost == p->designated_cost
1221 && (stp->bridge_id < p->designated_bridge
1222 || (stp->bridge_id == p->designated_bridge
1223 && p->port_id <= p->designated_port))))
1224 {
1225 stp_become_designated_port(p);
1226 }
1227 }
1228}
1229
1230static void
bd3950dd 1231stp_become_designated_port(struct stp_port *p) OVS_REQUIRES(mutex)
829a7d02
JP
1232{
1233 struct stp *stp = p->stp;
1234 p->designated_root = stp->designated_root;
1235 p->designated_cost = stp->root_path_cost;
1236 p->designated_bridge = stp->bridge_id;
1237 p->designated_port = p->port_id;
1238}
1239
1240static void
bd3950dd 1241stp_port_state_selection(struct stp *stp) OVS_REQUIRES(mutex)
829a7d02
JP
1242{
1243 struct stp_port *p;
1244
1245 FOR_EACH_ENABLED_PORT (p, stp) {
1246 if (p == stp->root_port) {
1247 p->config_pending = false;
1248 p->topology_change_ack = false;
1249 stp_make_forwarding(p);
1250 } else if (stp_is_designated_port(p)) {
1251 stp_stop_timer(&p->message_age_timer);
1252 stp_make_forwarding(p);
1253 } else {
1254 p->config_pending = false;
1255 p->topology_change_ack = false;
1256 stp_make_blocking(p);
1257 }
1258 }
1259}
1260
1261static void
bd3950dd 1262stp_make_forwarding(struct stp_port *p) OVS_REQUIRES(mutex)
829a7d02
JP
1263{
1264 if (p->state == STP_BLOCKING) {
1265 stp_set_port_state(p, STP_LISTENING);
1266 stp_start_timer(&p->forward_delay_timer, 0);
1267 }
1268}
1269
1270static void
bd3950dd 1271stp_make_blocking(struct stp_port *p) OVS_REQUIRES(mutex)
829a7d02
JP
1272{
1273 if (!(p->state & (STP_DISABLED | STP_BLOCKING))) {
1274 if (p->state & (STP_FORWARDING | STP_LEARNING)) {
1275 if (p->change_detection_enabled) {
1276 stp_topology_change_detection(p->stp);
1277 }
1278 }
1279 stp_set_port_state(p, STP_BLOCKING);
1280 stp_stop_timer(&p->forward_delay_timer);
1281 }
1282}
1283
1284static void
1285stp_set_port_state(struct stp_port *p, enum stp_state state)
bd3950dd 1286 OVS_REQUIRES(mutex)
829a7d02
JP
1287{
1288 if (state != p->state && !p->state_changed) {
1289 p->state_changed = true;
1290 if (p < p->stp->first_changed_port) {
1291 p->stp->first_changed_port = p;
1292 }
f23d157c 1293 seq_change(connectivity_seq_get());
829a7d02
JP
1294 }
1295 p->state = state;
1296}
1297
1298static void
bd3950dd 1299stp_topology_change_detection(struct stp *stp) OVS_REQUIRES(mutex)
829a7d02 1300{
634408e0
EJ
1301 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1302
829a7d02
JP
1303 if (stp_is_root_bridge(stp)) {
1304 stp->topology_change = true;
1305 stp_start_timer(&stp->topology_change_timer, 0);
1306 } else if (!stp->topology_change_detected) {
1307 stp_transmit_tcn(stp);
1308 stp_start_timer(&stp->tcn_timer, 0);
1309 }
6ae50723 1310 stp->fdb_needs_flush = true;
829a7d02 1311 stp->topology_change_detected = true;
f23d157c 1312 seq_change(connectivity_seq_get());
634408e0 1313 VLOG_INFO_RL(&rl, "%s: detected topology change.", stp->name);
829a7d02
JP
1314}
1315
1316static void
bd3950dd 1317stp_topology_change_acknowledged(struct stp *stp) OVS_REQUIRES(mutex)
829a7d02
JP
1318{
1319 stp->topology_change_detected = false;
1320 stp_stop_timer(&stp->tcn_timer);
1321}
1322
1323static void
bd3950dd 1324stp_acknowledge_topology_change(struct stp_port *p) OVS_REQUIRES(mutex)
829a7d02
JP
1325{
1326 p->topology_change_ack = true;
1327 stp_transmit_config(p);
1328}
1329
1330static void
1331stp_received_config_bpdu(struct stp *stp, struct stp_port *p,
1332 const struct stp_config_bpdu *config)
bd3950dd 1333 OVS_REQUIRES(mutex)
829a7d02
JP
1334{
1335 if (ntohs(config->message_age) >= ntohs(config->max_age)) {
1336 VLOG_WARN("%s: received config BPDU with message age (%u) greater "
1337 "than max age (%u)",
1338 stp->name,
1339 ntohs(config->message_age), ntohs(config->max_age));
1340 return;
1341 }
1342 if (p->state != STP_DISABLED) {
1343 bool root = stp_is_root_bridge(stp);
1344 if (stp_supersedes_port_info(p, config)) {
1345 stp_record_config_information(p, config);
1346 stp_configuration_update(stp);
1347 stp_port_state_selection(stp);
1348 if (!stp_is_root_bridge(stp) && root) {
1349 stp_stop_timer(&stp->hello_timer);
1350 if (stp->topology_change_detected) {
1351 stp_stop_timer(&stp->topology_change_timer);
1352 stp_transmit_tcn(stp);
1353 stp_start_timer(&stp->tcn_timer, 0);
1354 }
1355 }
1356 if (p == stp->root_port) {
1357 stp_record_config_timeout_values(stp, config);
1358 stp_config_bpdu_generation(stp);
3310b570 1359 if (config->flags & STP_CONFIG_TOPOLOGY_CHANGE_ACK) {
829a7d02
JP
1360 stp_topology_change_acknowledged(stp);
1361 }
6ae50723
EJ
1362 if (config->flags & STP_CONFIG_TOPOLOGY_CHANGE) {
1363 stp->fdb_needs_flush = true;
1364 }
829a7d02
JP
1365 }
1366 } else if (stp_is_designated_port(p)) {
1367 stp_transmit_config(p);
1368 }
1369 }
1370}
1371
1372static void
1373stp_received_tcn_bpdu(struct stp *stp, struct stp_port *p)
bd3950dd 1374 OVS_REQUIRES(mutex)
829a7d02
JP
1375{
1376 if (p->state != STP_DISABLED) {
1377 if (stp_is_designated_port(p)) {
1378 stp_topology_change_detection(stp);
1379 stp_acknowledge_topology_change(p);
1380 }
1381 }
1382}
1383
1384static void
bd3950dd 1385stp_hello_timer_expiry(struct stp *stp) OVS_REQUIRES(mutex)
829a7d02
JP
1386{
1387 stp_config_bpdu_generation(stp);
1388 stp_start_timer(&stp->hello_timer, 0);
1389}
1390
1391static void
bd3950dd 1392stp_message_age_timer_expiry(struct stp_port *p) OVS_REQUIRES(mutex)
829a7d02
JP
1393{
1394 struct stp *stp = p->stp;
1395 bool root = stp_is_root_bridge(stp);
11306274
AW
1396
1397 VLOG_DBG_RL(&stp_rl, "bridge: %s, port: %s, message age timer expired",
1398 stp->name, p->port_name);
829a7d02
JP
1399 stp_become_designated_port(p);
1400 stp_configuration_update(stp);
1401 stp_port_state_selection(stp);
1402 if (stp_is_root_bridge(stp) && !root) {
1403 stp->max_age = stp->bridge_max_age;
1404 stp->hello_time = stp->bridge_hello_time;
1405 stp->forward_delay = stp->bridge_forward_delay;
1406 stp_topology_change_detection(stp);
1407 stp_stop_timer(&stp->tcn_timer);
1408 stp_config_bpdu_generation(stp);
1409 stp_start_timer(&stp->hello_timer, 0);
1410 }
1411}
1412
1413static bool
bd3950dd 1414stp_is_designated_for_some_port(const struct stp *stp) OVS_REQUIRES(mutex)
829a7d02
JP
1415{
1416 const struct stp_port *p;
1417
1418 FOR_EACH_ENABLED_PORT (p, stp) {
1419 if (p->designated_bridge == stp->bridge_id) {
1420 return true;
1421 }
1422 }
1423 return false;
1424}
1425
1426static void
bd3950dd 1427stp_forward_delay_timer_expiry(struct stp_port *p) OVS_REQUIRES(mutex)
829a7d02
JP
1428{
1429 if (p->state == STP_LISTENING) {
1430 stp_set_port_state(p, STP_LEARNING);
1431 stp_start_timer(&p->forward_delay_timer, 0);
1432 } else if (p->state == STP_LEARNING) {
1433 stp_set_port_state(p, STP_FORWARDING);
1434 if (stp_is_designated_for_some_port(p->stp)) {
1435 if (p->change_detection_enabled) {
1436 stp_topology_change_detection(p->stp);
1437 }
1438 }
1439 }
1440}
1441
1442static void
bd3950dd 1443stp_tcn_timer_expiry(struct stp *stp) OVS_REQUIRES(mutex)
829a7d02
JP
1444{
1445 stp_transmit_tcn(stp);
1446 stp_start_timer(&stp->tcn_timer, 0);
1447}
1448
1449static void
bd3950dd 1450stp_topology_change_timer_expiry(struct stp *stp) OVS_REQUIRES(mutex)
829a7d02
JP
1451{
1452 stp->topology_change_detected = false;
1453 stp->topology_change = false;
1454}
1455
1456static void
bd3950dd 1457stp_hold_timer_expiry(struct stp_port *p) OVS_REQUIRES(mutex)
829a7d02
JP
1458{
1459 if (p->config_pending) {
1460 stp_transmit_config(p);
1461 }
1462}
1463
1464static void
1465stp_initialize_port(struct stp_port *p, enum stp_state state)
bd3950dd 1466 OVS_REQUIRES(mutex)
829a7d02 1467{
cb22974d 1468 ovs_assert(state & (STP_DISABLED | STP_BLOCKING));
829a7d02 1469 stp_become_designated_port(p);
fb20b0bf
JR
1470
1471 if (!p->state && state == STP_DISABLED) {
1472 p->state = state; /* Do not trigger state change when initializing. */
1473 } else {
1474 stp_set_port_state(p, state);
1475 }
829a7d02
JP
1476 p->topology_change_ack = false;
1477 p->config_pending = false;
1478 p->change_detection_enabled = true;
3310b570 1479 p->aux = NULL;
829a7d02
JP
1480 stp_stop_timer(&p->message_age_timer);
1481 stp_stop_timer(&p->forward_delay_timer);
1482 stp_stop_timer(&p->hold_timer);
80740385 1483 p->tx_count = p->rx_count = p->error_count = 0;
829a7d02
JP
1484}
1485
1486static void
bd3950dd 1487stp_become_root_bridge(struct stp *stp) OVS_REQUIRES(mutex)
829a7d02
JP
1488{
1489 stp->max_age = stp->bridge_max_age;
1490 stp->hello_time = stp->bridge_hello_time;
1491 stp->forward_delay = stp->bridge_forward_delay;
1492 stp_topology_change_detection(stp);
1493 stp_stop_timer(&stp->tcn_timer);
1494 stp_config_bpdu_generation(stp);
1495 stp_start_timer(&stp->hello_timer, 0);
1496}
1497
1498static void
bd3950dd 1499stp_start_timer(struct stp_timer *timer, int value) OVS_REQUIRES(mutex)
829a7d02
JP
1500{
1501 timer->value = value;
1502 timer->active = true;
1503}
1504
1505static void
bd3950dd 1506stp_stop_timer(struct stp_timer *timer) OVS_REQUIRES(mutex)
829a7d02
JP
1507{
1508 timer->active = false;
1509}
1510
1511static bool
1512stp_timer_expired(struct stp_timer *timer, int elapsed, int timeout)
bd3950dd 1513 OVS_REQUIRES(mutex)
829a7d02
JP
1514{
1515 if (timer->active) {
1516 timer->value += elapsed;
1517 if (timer->value >= timeout) {
1518 timer->active = false;
1519 return true;
1520 }
1521 }
1522 return false;
1523}
1524
1525/* Returns the number of whole STP timer ticks in 'ms' milliseconds. There
1526 * are 256 STP timer ticks per second. */
1527static int
1528ms_to_timer(int ms)
1529{
1530 return ms * 0x100 / 1000;
1531}
1532
829a7d02
JP
1533/* Returns the number of whole milliseconds in 'timer' STP timer ticks. There
1534 * are 256 STP timer ticks per second. */
1535static int
1536timer_to_ms(int timer)
1537{
1538 return timer * 1000 / 0x100;
1539}
1540
1541static int
1542clamp(int x, int min, int max)
1543{
1544 return x < min ? min : x > max ? max : x;
1545}
1546
1547static void
bd3950dd 1548stp_update_bridge_timers(struct stp *stp) OVS_REQUIRES(mutex)
829a7d02
JP
1549{
1550 int ht, ma, fd;
1551
1552 ht = clamp(stp->rq_hello_time, 1000, 10000);
1553 ma = clamp(stp->rq_max_age, MAX(2 * (ht + 1000), 6000), 40000);
1554 fd = clamp(stp->rq_forward_delay, ma / 2 + 1000, 30000);
1555
1556 stp->bridge_hello_time = ms_to_timer(ht);
1557 stp->bridge_max_age = ms_to_timer(ma);
1558 stp->bridge_forward_delay = ms_to_timer(fd);
1559
1560 if (stp_is_root_bridge(stp)) {
1561 stp->max_age = stp->bridge_max_age;
1562 stp->hello_time = stp->bridge_hello_time;
1563 stp->forward_delay = stp->bridge_forward_delay;
1564 }
1565}
1566
1567static void
1568stp_send_bpdu(struct stp_port *p, const void *bpdu, size_t bpdu_size)
bd3950dd 1569 OVS_REQUIRES(mutex)
829a7d02
JP
1570{
1571 struct eth_header *eth;
1572 struct llc_header *llc;
cf62fa4c 1573 struct dp_packet *pkt;
829a7d02
JP
1574
1575 /* Skeleton. */
cf62fa4c
PS
1576 pkt = dp_packet_new(ETH_HEADER_LEN + LLC_HEADER_LEN + bpdu_size);
1577 eth = dp_packet_put_zeros(pkt, sizeof *eth);
1578 llc = dp_packet_put_zeros(pkt, sizeof *llc);
82eb5b0a 1579 dp_packet_reset_offsets(pkt);
cf62fa4c 1580 dp_packet_set_l3(pkt, dp_packet_put(pkt, bpdu, bpdu_size));
829a7d02
JP
1581
1582 /* 802.2 header. */
74ff3298 1583 eth->eth_dst = eth_addr_stp;
829a7d02 1584 /* p->stp->send_bpdu() must fill in source address. */
cf62fa4c 1585 eth->eth_type = htons(dp_packet_size(pkt) - ETH_HEADER_LEN);
829a7d02
JP
1586
1587 /* LLC header. */
1588 llc->llc_dsap = STP_LLC_DSAP;
1589 llc->llc_ssap = STP_LLC_SSAP;
1590 llc->llc_cntl = STP_LLC_CNTL;
1591
1592 p->stp->send_bpdu(pkt, stp_port_no(p), p->stp->aux);
80740385 1593 p->tx_count++;
829a7d02 1594}
fe4a02e4
EJ
1595\f
1596/* Unixctl. */
1597
1598static struct stp *
bd3950dd 1599stp_find(const char *name) OVS_REQUIRES(mutex)
fe4a02e4
EJ
1600{
1601 struct stp *stp;
1602
bd54dbcd 1603 LIST_FOR_EACH (stp, node, all_stps) {
fe4a02e4
EJ
1604 if (!strcmp(stp->name, name)) {
1605 return stp;
1606 }
1607 }
1608 return NULL;
1609}
1610
1611static void
1612stp_unixctl_tcn(struct unixctl_conn *conn, int argc,
1613 const char *argv[], void *aux OVS_UNUSED)
1614{
bd54dbcd 1615 ovs_mutex_lock(&mutex);
fe4a02e4
EJ
1616 if (argc > 1) {
1617 struct stp *stp = stp_find(argv[1]);
1618
1619 if (!stp) {
bde9f75d 1620 unixctl_command_reply_error(conn, "no such stp object");
bd54dbcd 1621 goto out;
fe4a02e4
EJ
1622 }
1623 stp_topology_change_detection(stp);
1624 } else {
1625 struct stp *stp;
1626
bd54dbcd 1627 LIST_FOR_EACH (stp, node, all_stps) {
fe4a02e4
EJ
1628 stp_topology_change_detection(stp);
1629 }
1630 }
1631
bde9f75d 1632 unixctl_command_reply(conn, "OK");
bd54dbcd
EJ
1633
1634out:
1635 ovs_mutex_unlock(&mutex);
fe4a02e4 1636}