2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* Based on sample implementation in 802.1D-1998. Above copyright and license
18 * applies to all modifications. */
23 #include <sys/types.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
28 #include "byte-order.h"
35 VLOG_DEFINE_THIS_MODULE(stp);
37 #define STP_PROTOCOL_ID 0x0000
38 #define STP_PROTOCOL_VERSION 0x00
39 #define STP_TYPE_CONFIG 0x00
40 #define STP_TYPE_TCN 0x80
42 struct stp_bpdu_header {
43 ovs_be16 protocol_id; /* STP_PROTOCOL_ID. */
44 uint8_t protocol_version; /* STP_PROTOCOL_VERSION. */
45 uint8_t bpdu_type; /* One of STP_TYPE_*. */
46 } __attribute__((packed));
47 BUILD_ASSERT_DECL(sizeof(struct stp_bpdu_header) == 4);
49 enum stp_config_bpdu_flags {
50 STP_CONFIG_TOPOLOGY_CHANGE_ACK = 0x80,
51 STP_CONFIG_TOPOLOGY_CHANGE = 0x01
54 struct stp_config_bpdu {
55 struct stp_bpdu_header header; /* Type STP_TYPE_CONFIG. */
56 uint8_t flags; /* STP_CONFIG_* flags. */
57 ovs_be64 root_id; /* 8.5.1.1: Bridge believed to be root. */
58 ovs_be32 root_path_cost; /* 8.5.1.2: Cost of path to root. */
59 ovs_be64 bridge_id; /* 8.5.1.3: ID of transmitting bridge. */
60 ovs_be16 port_id; /* 8.5.1.4: Port transmitting the BPDU. */
61 ovs_be16 message_age; /* 8.5.1.5: Age of BPDU at tx time. */
62 ovs_be16 max_age; /* 8.5.1.6: Timeout for received data. */
63 ovs_be16 hello_time; /* 8.5.1.7: Time between BPDU generation. */
64 ovs_be16 forward_delay; /* 8.5.1.8: State progression delay. */
65 } __attribute__((packed));
66 BUILD_ASSERT_DECL(sizeof(struct stp_config_bpdu) == 35);
69 struct stp_bpdu_header header; /* Type STP_TYPE_TCN. */
70 } __attribute__((packed));
71 BUILD_ASSERT_DECL(sizeof(struct stp_tcn_bpdu) == 4);
74 bool active; /* Timer in use? */
75 int value; /* Current value of timer, counting up. */
80 void *aux; /* Auxiliary data the user may retrieve. */
81 int port_id; /* 8.5.5.1: Unique port identifier. */
82 enum stp_state state; /* 8.5.5.2: Current state. */
83 int path_cost; /* 8.5.5.3: Cost of tx/rx on this port. */
84 stp_identifier designated_root; /* 8.5.5.4. */
85 int designated_cost; /* 8.5.5.5: Path cost to root on port. */
86 stp_identifier designated_bridge; /* 8.5.5.6. */
87 int designated_port; /* 8.5.5.7: Port to send config msgs on. */
88 bool topology_change_ack; /* 8.5.5.8: Flag for next config BPDU. */
89 bool config_pending; /* 8.5.5.9: Send BPDU when hold expires? */
90 bool change_detection_enabled; /* 8.5.5.10: Detect topology changes? */
92 struct stp_timer message_age_timer; /* 8.5.6.1: Age of received info. */
93 struct stp_timer forward_delay_timer; /* 8.5.6.2: State change timer. */
94 struct stp_timer hold_timer; /* 8.5.6.3: BPDU rate limit timer. */
96 int tx_count; /* Number of BPDUs transmitted. */
97 int rx_count; /* Number of valid BPDUs received. */
98 int error_count; /* Number of bad BPDUs received. */
104 struct list node; /* Node in all_stps list. */
106 /* Static bridge data. */
107 char *name; /* Human-readable name for log messages. */
108 stp_identifier bridge_id; /* 8.5.3.7: This bridge. */
109 int max_age; /* 8.5.3.4: Time to drop received data. */
110 int hello_time; /* 8.5.3.5: Time between sending BPDUs. */
111 int forward_delay; /* 8.5.3.6: Delay between state changes. */
112 int bridge_max_age; /* 8.5.3.8: max_age when we're root. */
113 int bridge_hello_time; /* 8.5.3.9: hello_time as root. */
114 int bridge_forward_delay; /* 8.5.3.10: forward_delay as root. */
115 int rq_max_age; /* User-requested max age, in ms. */
116 int rq_hello_time; /* User-requested hello time, in ms. */
117 int rq_forward_delay; /* User-requested forward delay, in ms. */
118 int elapsed_remainder; /* Left-over msecs from last stp_tick(). */
120 /* Dynamic bridge data. */
121 stp_identifier designated_root; /* 8.5.3.1: Bridge believed to be root. */
122 unsigned int root_path_cost; /* 8.5.3.2: Cost of path to root. */
123 struct stp_port *root_port; /* 8.5.3.3: Lowest cost port to root. */
124 bool topology_change_detected; /* 8.5.3.11: Detected a topology change? */
125 bool topology_change; /* 8.5.3.12: Received topology change? */
128 struct stp_timer hello_timer; /* 8.5.4.1: Hello timer. */
129 struct stp_timer tcn_timer; /* 8.5.4.2: Topology change timer. */
130 struct stp_timer topology_change_timer; /* 8.5.4.3. */
133 struct stp_port ports[STP_MAX_PORTS];
135 /* Interface to client. */
136 bool fdb_needs_flush; /* MAC learning tables needs flushing. */
137 struct stp_port *first_changed_port;
138 void (*send_bpdu)(struct ofpbuf *bpdu, int port_no, void *aux);
142 static struct list all_stps = LIST_INITIALIZER(&all_stps);
144 #define FOR_EACH_ENABLED_PORT(PORT, STP) \
145 for ((PORT) = stp_next_enabled_port((STP), (STP)->ports); \
147 (PORT) = stp_next_enabled_port((STP), (PORT) + 1))
148 static struct stp_port *
149 stp_next_enabled_port(const struct stp *stp, const struct stp_port *port)
151 for (; port < &stp->ports[ARRAY_SIZE(stp->ports)]; port++) {
152 if (port->state != STP_DISABLED) {
153 return CONST_CAST(struct stp_port *, port);
159 #define MESSAGE_AGE_INCREMENT 1
161 static void stp_transmit_config(struct stp_port *);
162 static bool stp_supersedes_port_info(const struct stp_port *,
163 const struct stp_config_bpdu *);
164 static void stp_record_config_information(struct stp_port *,
165 const struct stp_config_bpdu *);
166 static void stp_record_config_timeout_values(struct stp *,
167 const struct stp_config_bpdu *);
168 static bool stp_is_designated_port(const struct stp_port *);
169 static void stp_config_bpdu_generation(struct stp *);
170 static void stp_transmit_tcn(struct stp *);
171 static void stp_configuration_update(struct stp *);
172 static bool stp_supersedes_root(const struct stp_port *root,
173 const struct stp_port *);
174 static void stp_root_selection(struct stp *);
175 static void stp_designated_port_selection(struct stp *);
176 static void stp_become_designated_port(struct stp_port *);
177 static void stp_port_state_selection(struct stp *);
178 static void stp_make_forwarding(struct stp_port *);
179 static void stp_make_blocking(struct stp_port *);
180 static void stp_set_port_state(struct stp_port *, enum stp_state);
181 static void stp_topology_change_detection(struct stp *);
182 static void stp_topology_change_acknowledged(struct stp *);
183 static void stp_acknowledge_topology_change(struct stp_port *);
184 static void stp_received_config_bpdu(struct stp *, struct stp_port *,
185 const struct stp_config_bpdu *);
186 static void stp_received_tcn_bpdu(struct stp *, struct stp_port *);
187 static void stp_hello_timer_expiry(struct stp *);
188 static void stp_message_age_timer_expiry(struct stp_port *);
189 static bool stp_is_designated_for_some_port(const struct stp *);
190 static void stp_forward_delay_timer_expiry(struct stp_port *);
191 static void stp_tcn_timer_expiry(struct stp *);
192 static void stp_topology_change_timer_expiry(struct stp *);
193 static void stp_hold_timer_expiry(struct stp_port *);
194 static void stp_initialize_port(struct stp_port *, enum stp_state);
195 static void stp_become_root_bridge(struct stp *);
196 static void stp_update_bridge_timers(struct stp *);
198 static int clamp(int x, int min, int max);
199 static int ms_to_timer(int ms);
200 static int timer_to_ms(int timer);
201 static void stp_start_timer(struct stp_timer *, int value);
202 static void stp_stop_timer(struct stp_timer *);
203 static bool stp_timer_expired(struct stp_timer *, int elapsed, int timeout);
205 static void stp_send_bpdu(struct stp_port *, const void *, size_t);
206 static void stp_unixctl_tcn(struct unixctl_conn *, int argc,
207 const char *argv[], void *aux);
212 unixctl_command_register("stp/tcn", "[bridge]", 0, 1, stp_unixctl_tcn,
216 /* Creates and returns a new STP instance that initially has no ports enabled.
218 * 'bridge_id' should be a 48-bit MAC address as returned by
219 * eth_addr_to_uint64(). 'bridge_id' may also have a priority value in its top
220 * 16 bits; if those bits are set to 0, STP_DEFAULT_BRIDGE_PRIORITY is used.
221 * (This priority may be changed with stp_set_bridge_priority().)
223 * When the bridge needs to send out a BPDU, it calls 'send_bpdu'. This
224 * callback may be called from stp_tick() or stp_received_bpdu(). The
225 * arguments to 'send_bpdu' are an STP BPDU encapsulated in 'bpdu',
226 * the spanning tree port number 'port_no' that should transmit the
227 * packet, and auxiliary data to be passed to the callback in 'aux'.
230 stp_create(const char *name, stp_identifier bridge_id,
231 void (*send_bpdu)(struct ofpbuf *bpdu, int port_no, void *aux),
237 stp = xzalloc(sizeof *stp);
238 stp->name = xstrdup(name);
239 stp->bridge_id = bridge_id;
240 if (!(stp->bridge_id >> 48)) {
241 stp->bridge_id |= (uint64_t) STP_DEFAULT_BRIDGE_PRIORITY << 48;
244 stp->rq_max_age = STP_DEFAULT_MAX_AGE;
245 stp->rq_hello_time = STP_DEFAULT_HELLO_TIME;
246 stp->rq_forward_delay = STP_DEFAULT_FWD_DELAY;
247 stp_update_bridge_timers(stp);
248 stp->max_age = stp->bridge_max_age;
249 stp->hello_time = stp->bridge_hello_time;
250 stp->forward_delay = stp->bridge_forward_delay;
252 stp->designated_root = stp->bridge_id;
253 stp->root_path_cost = 0;
254 stp->root_port = NULL;
255 stp->topology_change_detected = false;
256 stp->topology_change = false;
258 stp_stop_timer(&stp->tcn_timer);
259 stp_stop_timer(&stp->topology_change_timer);
260 stp_start_timer(&stp->hello_timer, 0);
262 stp->send_bpdu = send_bpdu;
265 stp->first_changed_port = &stp->ports[ARRAY_SIZE(stp->ports)];
266 for (p = stp->ports; p < &stp->ports[ARRAY_SIZE(stp->ports)]; p++) {
268 p->port_id = (stp_port_no(p) + 1) | (STP_DEFAULT_PORT_PRIORITY << 8);
269 p->path_cost = 19; /* Recommended default for 100 Mb/s link. */
270 stp_initialize_port(p, STP_DISABLED);
272 list_push_back(&all_stps, &stp->node);
276 /* Destroys 'stp'. */
278 stp_destroy(struct stp *stp)
281 list_remove(&stp->node);
287 /* Runs 'stp' given that 'ms' milliseconds have passed. */
289 stp_tick(struct stp *stp, int ms)
294 /* Convert 'ms' to STP timer ticks. Preserve any leftover milliseconds
295 * from previous stp_tick() calls so that we don't lose STP ticks when we
296 * are called too frequently. */
297 ms = clamp(ms, 0, INT_MAX - 1000) + stp->elapsed_remainder;
298 elapsed = ms_to_timer(ms);
299 stp->elapsed_remainder = ms - timer_to_ms(elapsed);
304 if (stp_timer_expired(&stp->hello_timer, elapsed, stp->hello_time)) {
305 stp_hello_timer_expiry(stp);
307 if (stp_timer_expired(&stp->tcn_timer, elapsed, stp->bridge_hello_time)) {
308 stp_tcn_timer_expiry(stp);
310 if (stp_timer_expired(&stp->topology_change_timer, elapsed,
311 stp->max_age + stp->forward_delay)) {
312 stp_topology_change_timer_expiry(stp);
314 FOR_EACH_ENABLED_PORT (p, stp) {
315 if (stp_timer_expired(&p->message_age_timer, elapsed, stp->max_age)) {
316 stp_message_age_timer_expiry(p);
319 FOR_EACH_ENABLED_PORT (p, stp) {
320 if (stp_timer_expired(&p->forward_delay_timer, elapsed,
321 stp->forward_delay)) {
322 stp_forward_delay_timer_expiry(p);
324 if (stp_timer_expired(&p->hold_timer, elapsed, ms_to_timer(1000))) {
325 stp_hold_timer_expiry(p);
331 set_bridge_id(struct stp *stp, stp_identifier new_bridge_id)
333 if (new_bridge_id != stp->bridge_id) {
337 root = stp_is_root_bridge(stp);
338 FOR_EACH_ENABLED_PORT (p, stp) {
339 if (stp_is_designated_port(p)) {
340 p->designated_bridge = new_bridge_id;
343 stp->bridge_id = new_bridge_id;
344 stp_configuration_update(stp);
345 stp_port_state_selection(stp);
346 if (stp_is_root_bridge(stp) && !root) {
347 stp_become_root_bridge(stp);
353 stp_set_bridge_id(struct stp *stp, stp_identifier bridge_id)
355 const uint64_t mac_bits = (UINT64_C(1) << 48) - 1;
356 const uint64_t pri_bits = ~mac_bits;
357 set_bridge_id(stp, (stp->bridge_id & pri_bits) | (bridge_id & mac_bits));
361 stp_set_bridge_priority(struct stp *stp, uint16_t new_priority)
363 const uint64_t mac_bits = (UINT64_C(1) << 48) - 1;
364 set_bridge_id(stp, ((stp->bridge_id & mac_bits)
365 | ((uint64_t) new_priority << 48)));
368 /* Sets the desired hello time for 'stp' to 'ms', in milliseconds. The actual
369 * hello time is clamped to the range of 1 to 10 seconds and subject to the
370 * relationship (bridge_max_age >= 2 * (bridge_hello_time + 1 s)). The bridge
371 * hello time is only used when 'stp' is the root bridge. */
373 stp_set_hello_time(struct stp *stp, int ms)
375 stp->rq_hello_time = ms;
376 stp_update_bridge_timers(stp);
379 /* Sets the desired max age for 'stp' to 'ms', in milliseconds. The actual max
380 * age is clamped to the range of 6 to 40 seconds and subject to the
381 * relationships (2 * (bridge_forward_delay - 1 s) >= bridge_max_age) and
382 * (bridge_max_age >= 2 * (bridge_hello_time + 1 s)). The bridge max age is
383 * only used when 'stp' is the root bridge. */
385 stp_set_max_age(struct stp *stp, int ms)
387 stp->rq_max_age = ms;
388 stp_update_bridge_timers(stp);
391 /* Sets the desired forward delay for 'stp' to 'ms', in milliseconds. The
392 * actual forward delay is clamped to the range of 4 to 30 seconds and subject
393 * to the relationship (2 * (bridge_forward_delay - 1 s) >= bridge_max_age).
394 * The bridge forward delay is only used when 'stp' is the root bridge. */
396 stp_set_forward_delay(struct stp *stp, int ms)
398 stp->rq_forward_delay = ms;
399 stp_update_bridge_timers(stp);
402 /* Returns the name given to 'stp' in the call to stp_create(). */
404 stp_get_name(const struct stp *stp)
409 /* Returns the bridge ID for 'stp'. */
411 stp_get_bridge_id(const struct stp *stp)
413 return stp->bridge_id;
416 /* Returns the bridge ID of the bridge currently believed to be the root. */
418 stp_get_designated_root(const struct stp *stp)
420 return stp->designated_root;
423 /* Returns true if 'stp' believes itself to the be root of the spanning tree,
424 * false otherwise. */
426 stp_is_root_bridge(const struct stp *stp)
428 return stp->bridge_id == stp->designated_root;
431 /* Returns the cost of the path from 'stp' to the root of the spanning tree. */
433 stp_get_root_path_cost(const struct stp *stp)
435 return stp->root_path_cost;
438 /* Returns the bridge hello time, in ms. The returned value is not necessarily
439 * the value passed to stp_set_hello_time(): it is clamped to the valid range
440 * and quantized to the STP timer resolution. */
442 stp_get_hello_time(const struct stp *stp)
444 return timer_to_ms(stp->bridge_hello_time);
447 /* Returns the bridge max age, in ms. The returned value is not necessarily
448 * the value passed to stp_set_max_age(): it is clamped to the valid range,
449 * quantized to the STP timer resolution, and adjusted to match the constraints
450 * due to the hello time. */
452 stp_get_max_age(const struct stp *stp)
454 return timer_to_ms(stp->bridge_max_age);
457 /* Returns the bridge forward delay, in ms. The returned value is not
458 * necessarily the value passed to stp_set_forward_delay(): it is clamped to
459 * the valid range, quantized to the STP timer resolution, and adjusted to
460 * match the constraints due to the forward delay. */
462 stp_get_forward_delay(const struct stp *stp)
464 return timer_to_ms(stp->bridge_forward_delay);
467 /* Returns true if something has happened to 'stp' which necessitates flushing
468 * the client's MAC learning table. Calling this function resets 'stp' so that
469 * future calls will return false until flushing is required again. */
471 stp_check_and_reset_fdb_flush(struct stp *stp)
473 bool needs_flush = stp->fdb_needs_flush;
474 stp->fdb_needs_flush = false;
478 /* Returns the port in 'stp' with index 'port_no', which must be between 0 and
481 stp_get_port(struct stp *stp, int port_no)
483 ovs_assert(port_no >= 0 && port_no < ARRAY_SIZE(stp->ports));
484 return &stp->ports[port_no];
487 /* Returns the port connecting 'stp' to the root bridge, or a null pointer if
488 * there is no such port. */
490 stp_get_root_port(struct stp *stp)
492 return stp->root_port;
495 /* Finds a port whose state has changed. If successful, stores the port whose
496 * state changed in '*portp' and returns true. If no port has changed, stores
497 * NULL in '*portp' and returns false. */
499 stp_get_changed_port(struct stp *stp, struct stp_port **portp)
501 struct stp_port *end = &stp->ports[ARRAY_SIZE(stp->ports)];
504 for (p = stp->first_changed_port; p < end; p++) {
505 if (p->state_changed) {
506 p->state_changed = false;
507 stp->first_changed_port = p + 1;
512 stp->first_changed_port = end;
517 /* Returns the name for the given 'state' (for use in debugging and log
520 stp_state_name(enum stp_state state)
538 /* Returns true if 'state' is one in which packets received on a port should
539 * be forwarded, false otherwise.
541 * Returns true if 'state' is STP_DISABLED, since presumably in that case the
542 * port should still work, just not have STP applied to it. */
544 stp_forward_in_state(enum stp_state state)
546 return (state & (STP_DISABLED | STP_FORWARDING)) != 0;
549 /* Returns true if 'state' is one in which MAC learning should be done on
550 * packets received on a port, false otherwise.
552 * Returns true if 'state' is STP_DISABLED, since presumably in that case the
553 * port should still work, just not have STP applied to it. */
555 stp_learn_in_state(enum stp_state state)
557 return (state & (STP_DISABLED | STP_LEARNING | STP_FORWARDING)) != 0;
560 /* Returns the name for the given 'role' (for use in debugging and log
563 stp_role_name(enum stp_role role)
568 case STP_ROLE_DESIGNATED:
570 case STP_ROLE_ALTERNATE:
572 case STP_ROLE_DISABLED:
579 /* Notifies the STP entity that bridge protocol data unit 'bpdu', which is
580 * 'bpdu_size' bytes in length, was received on port 'p'.
582 * This function may call the 'send_bpdu' function provided to stp_create(). */
584 stp_received_bpdu(struct stp_port *p, const void *bpdu, size_t bpdu_size)
586 struct stp *stp = p->stp;
587 const struct stp_bpdu_header *header;
589 if (p->state == STP_DISABLED) {
593 if (bpdu_size < sizeof(struct stp_bpdu_header)) {
594 VLOG_WARN("%s: received runt %zu-byte BPDU", stp->name, bpdu_size);
600 if (header->protocol_id != htons(STP_PROTOCOL_ID)) {
601 VLOG_WARN("%s: received BPDU with unexpected protocol ID %"PRIu16,
602 stp->name, ntohs(header->protocol_id));
606 if (header->protocol_version != STP_PROTOCOL_VERSION) {
607 VLOG_DBG("%s: received BPDU with unexpected protocol version %"PRIu8,
608 stp->name, header->protocol_version);
611 switch (header->bpdu_type) {
612 case STP_TYPE_CONFIG:
613 if (bpdu_size < sizeof(struct stp_config_bpdu)) {
614 VLOG_WARN("%s: received config BPDU with invalid size %zu",
615 stp->name, bpdu_size);
619 stp_received_config_bpdu(stp, p, bpdu);
623 if (bpdu_size != sizeof(struct stp_tcn_bpdu)) {
624 VLOG_WARN("%s: received TCN BPDU with invalid size %zu",
625 stp->name, bpdu_size);
629 stp_received_tcn_bpdu(stp, p);
633 VLOG_WARN("%s: received BPDU of unexpected type %"PRIu8,
634 stp->name, header->bpdu_type);
641 /* Returns the STP entity in which 'p' is nested. */
643 stp_port_get_stp(struct stp_port *p)
648 /* Sets the 'aux' member of 'p'.
650 * The 'aux' member will be reset to NULL when stp_port_disable() is
651 * called or stp_port_enable() is called when the port is in a Disabled
654 stp_port_set_aux(struct stp_port *p, void *aux)
659 /* Returns the 'aux' member of 'p'. */
661 stp_port_get_aux(struct stp_port *p)
666 /* Returns the index of port 'p' within its bridge. */
668 stp_port_no(const struct stp_port *p)
670 struct stp *stp = p->stp;
671 ovs_assert(p >= stp->ports && p < &stp->ports[ARRAY_SIZE(stp->ports)]);
672 return p - stp->ports;
675 /* Returns the port ID for 'p'. */
677 stp_port_get_id(const struct stp_port *p)
682 /* Returns the state of port 'p'. */
684 stp_port_get_state(const struct stp_port *p)
689 /* Returns the role of port 'p'. */
691 stp_port_get_role(const struct stp_port *p)
693 struct stp_port *root_port = stp_get_root_port(p->stp);
695 if (root_port && root_port->port_id == p->port_id) {
696 return STP_ROLE_ROOT;
697 } else if (stp_is_designated_port(p)) {
698 return STP_ROLE_DESIGNATED;
699 } else if (p->state == STP_DISABLED) {
700 return STP_ROLE_DISABLED;
702 return STP_ROLE_ALTERNATE;
706 /* Retrieves BPDU transmit and receive counts for 'p'. */
707 void stp_port_get_counts(const struct stp_port *p,
708 int *tx_count, int *rx_count, int *error_count)
710 *tx_count = p->tx_count;
711 *rx_count = p->rx_count;
712 *error_count = p->error_count;
715 /* Disables STP on port 'p'. */
717 stp_port_disable(struct stp_port *p)
719 struct stp *stp = p->stp;
720 if (p->state != STP_DISABLED) {
721 bool root = stp_is_root_bridge(stp);
722 stp_become_designated_port(p);
723 stp_set_port_state(p, STP_DISABLED);
724 p->topology_change_ack = false;
725 p->config_pending = false;
726 stp_stop_timer(&p->message_age_timer);
727 stp_stop_timer(&p->forward_delay_timer);
728 stp_configuration_update(stp);
729 stp_port_state_selection(stp);
730 if (stp_is_root_bridge(stp) && !root) {
731 stp_become_root_bridge(stp);
737 /* Enables STP on port 'p'. The port will initially be in "blocking" state. */
739 stp_port_enable(struct stp_port *p)
741 if (p->state == STP_DISABLED) {
742 stp_initialize_port(p, STP_BLOCKING);
743 stp_port_state_selection(p->stp);
747 /* Sets the priority of port 'p' to 'new_priority'. Lower numerical values
748 * are interpreted as higher priorities. */
750 stp_port_set_priority(struct stp_port *p, uint8_t new_priority)
752 uint16_t new_port_id = (p->port_id & 0xff) | (new_priority << 8);
753 if (p->port_id != new_port_id) {
754 struct stp *stp = p->stp;
755 if (stp_is_designated_port(p)) {
756 p->designated_port = new_port_id;
758 p->port_id = new_port_id;
759 if (stp->bridge_id == p->designated_bridge
760 && p->port_id < p->designated_port) {
761 stp_become_designated_port(p);
762 stp_port_state_selection(stp);
767 /* Convert 'speed' (measured in Mb/s) into the path cost. */
769 stp_convert_speed_to_cost(unsigned int speed)
771 return speed >= 10000 ? 2 /* 10 Gb/s. */
772 : speed >= 1000 ? 4 /* 1 Gb/s. */
773 : speed >= 100 ? 19 /* 100 Mb/s. */
774 : speed >= 16 ? 62 /* 16 Mb/s. */
775 : speed >= 10 ? 100 /* 10 Mb/s. */
776 : speed >= 4 ? 250 /* 4 Mb/s. */
777 : 19; /* 100 Mb/s (guess). */
780 /* Sets the path cost of port 'p' to 'path_cost'. Lower values are generally
781 * used to indicate faster links. Use stp_port_set_speed() to automatically
782 * generate a default path cost from a link speed. */
784 stp_port_set_path_cost(struct stp_port *p, uint16_t path_cost)
786 if (p->path_cost != path_cost) {
787 struct stp *stp = p->stp;
788 p->path_cost = path_cost;
789 stp_configuration_update(stp);
790 stp_port_state_selection(stp);
794 /* Sets the path cost of port 'p' based on 'speed' (measured in Mb/s). */
796 stp_port_set_speed(struct stp_port *p, unsigned int speed)
798 stp_port_set_path_cost(p, stp_convert_speed_to_cost(speed));
801 /* Enables topology change detection on port 'p'. */
803 stp_port_enable_change_detection(struct stp_port *p)
805 p->change_detection_enabled = true;
808 /* Disables topology change detection on port 'p'. */
810 stp_port_disable_change_detection(struct stp_port *p)
812 p->change_detection_enabled = false;
816 stp_transmit_config(struct stp_port *p)
818 struct stp *stp = p->stp;
819 bool root = stp_is_root_bridge(stp);
820 if (!root && !stp->root_port) {
823 if (p->hold_timer.active) {
824 p->config_pending = true;
826 struct stp_config_bpdu config;
827 memset(&config, 0, sizeof config);
828 config.header.protocol_id = htons(STP_PROTOCOL_ID);
829 config.header.protocol_version = STP_PROTOCOL_VERSION;
830 config.header.bpdu_type = STP_TYPE_CONFIG;
832 if (p->topology_change_ack) {
833 config.flags |= STP_CONFIG_TOPOLOGY_CHANGE_ACK;
835 if (stp->topology_change) {
836 config.flags |= STP_CONFIG_TOPOLOGY_CHANGE;
838 config.root_id = htonll(stp->designated_root);
839 config.root_path_cost = htonl(stp->root_path_cost);
840 config.bridge_id = htonll(stp->bridge_id);
841 config.port_id = htons(p->port_id);
843 config.message_age = htons(0);
845 config.message_age = htons(stp->root_port->message_age_timer.value
846 + MESSAGE_AGE_INCREMENT);
848 config.max_age = htons(stp->max_age);
849 config.hello_time = htons(stp->hello_time);
850 config.forward_delay = htons(stp->forward_delay);
851 if (ntohs(config.message_age) < stp->max_age) {
852 p->topology_change_ack = false;
853 p->config_pending = false;
854 stp_send_bpdu(p, &config, sizeof config);
855 stp_start_timer(&p->hold_timer, 0);
861 stp_supersedes_port_info(const struct stp_port *p,
862 const struct stp_config_bpdu *config)
864 if (ntohll(config->root_id) != p->designated_root) {
865 return ntohll(config->root_id) < p->designated_root;
866 } else if (ntohl(config->root_path_cost) != p->designated_cost) {
867 return ntohl(config->root_path_cost) < p->designated_cost;
868 } else if (ntohll(config->bridge_id) != p->designated_bridge) {
869 return ntohll(config->bridge_id) < p->designated_bridge;
871 return (ntohll(config->bridge_id) != p->stp->bridge_id
872 || ntohs(config->port_id) <= p->designated_port);
877 stp_record_config_information(struct stp_port *p,
878 const struct stp_config_bpdu *config)
880 p->designated_root = ntohll(config->root_id);
881 p->designated_cost = ntohl(config->root_path_cost);
882 p->designated_bridge = ntohll(config->bridge_id);
883 p->designated_port = ntohs(config->port_id);
884 stp_start_timer(&p->message_age_timer, ntohs(config->message_age));
888 stp_record_config_timeout_values(struct stp *stp,
889 const struct stp_config_bpdu *config)
891 stp->max_age = ntohs(config->max_age);
892 stp->hello_time = ntohs(config->hello_time);
893 stp->forward_delay = ntohs(config->forward_delay);
894 stp->topology_change = config->flags & STP_CONFIG_TOPOLOGY_CHANGE;
898 stp_is_designated_port(const struct stp_port *p)
900 return (p->designated_bridge == p->stp->bridge_id
901 && p->designated_port == p->port_id);
905 stp_config_bpdu_generation(struct stp *stp)
909 FOR_EACH_ENABLED_PORT (p, stp) {
910 if (stp_is_designated_port(p)) {
911 stp_transmit_config(p);
917 stp_transmit_tcn(struct stp *stp)
919 struct stp_port *p = stp->root_port;
920 struct stp_tcn_bpdu tcn_bpdu;
924 tcn_bpdu.header.protocol_id = htons(STP_PROTOCOL_ID);
925 tcn_bpdu.header.protocol_version = STP_PROTOCOL_VERSION;
926 tcn_bpdu.header.bpdu_type = STP_TYPE_TCN;
927 stp_send_bpdu(p, &tcn_bpdu, sizeof tcn_bpdu);
931 stp_configuration_update(struct stp *stp)
933 stp_root_selection(stp);
934 stp_designated_port_selection(stp);
938 stp_supersedes_root(const struct stp_port *root, const struct stp_port *p)
940 int p_cost = p->designated_cost + p->path_cost;
941 int root_cost = root->designated_cost + root->path_cost;
943 if (p->designated_root != root->designated_root) {
944 return p->designated_root < root->designated_root;
945 } else if (p_cost != root_cost) {
946 return p_cost < root_cost;
947 } else if (p->designated_bridge != root->designated_bridge) {
948 return p->designated_bridge < root->designated_bridge;
949 } else if (p->designated_port != root->designated_port) {
950 return p->designated_port < root->designated_port;
952 return p->port_id < root->port_id;
957 stp_root_selection(struct stp *stp)
959 struct stp_port *p, *root;
962 FOR_EACH_ENABLED_PORT (p, stp) {
963 if (stp_is_designated_port(p)
964 || p->designated_root >= stp->bridge_id) {
967 if (root && !stp_supersedes_root(root, p)) {
972 stp->root_port = root;
974 stp->designated_root = stp->bridge_id;
975 stp->root_path_cost = 0;
977 stp->designated_root = root->designated_root;
978 stp->root_path_cost = root->designated_cost + root->path_cost;
983 stp_designated_port_selection(struct stp *stp)
987 FOR_EACH_ENABLED_PORT (p, stp) {
988 if (stp_is_designated_port(p)
989 || p->designated_root != stp->designated_root
990 || stp->root_path_cost < p->designated_cost
991 || (stp->root_path_cost == p->designated_cost
992 && (stp->bridge_id < p->designated_bridge
993 || (stp->bridge_id == p->designated_bridge
994 && p->port_id <= p->designated_port))))
996 stp_become_designated_port(p);
1002 stp_become_designated_port(struct stp_port *p)
1004 struct stp *stp = p->stp;
1005 p->designated_root = stp->designated_root;
1006 p->designated_cost = stp->root_path_cost;
1007 p->designated_bridge = stp->bridge_id;
1008 p->designated_port = p->port_id;
1012 stp_port_state_selection(struct stp *stp)
1016 FOR_EACH_ENABLED_PORT (p, stp) {
1017 if (p == stp->root_port) {
1018 p->config_pending = false;
1019 p->topology_change_ack = false;
1020 stp_make_forwarding(p);
1021 } else if (stp_is_designated_port(p)) {
1022 stp_stop_timer(&p->message_age_timer);
1023 stp_make_forwarding(p);
1025 p->config_pending = false;
1026 p->topology_change_ack = false;
1027 stp_make_blocking(p);
1033 stp_make_forwarding(struct stp_port *p)
1035 if (p->state == STP_BLOCKING) {
1036 stp_set_port_state(p, STP_LISTENING);
1037 stp_start_timer(&p->forward_delay_timer, 0);
1042 stp_make_blocking(struct stp_port *p)
1044 if (!(p->state & (STP_DISABLED | STP_BLOCKING))) {
1045 if (p->state & (STP_FORWARDING | STP_LEARNING)) {
1046 if (p->change_detection_enabled) {
1047 stp_topology_change_detection(p->stp);
1050 stp_set_port_state(p, STP_BLOCKING);
1051 stp_stop_timer(&p->forward_delay_timer);
1056 stp_set_port_state(struct stp_port *p, enum stp_state state)
1058 if (state != p->state && !p->state_changed) {
1059 p->state_changed = true;
1060 if (p < p->stp->first_changed_port) {
1061 p->stp->first_changed_port = p;
1068 stp_topology_change_detection(struct stp *stp)
1070 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1072 if (stp_is_root_bridge(stp)) {
1073 stp->topology_change = true;
1074 stp_start_timer(&stp->topology_change_timer, 0);
1075 } else if (!stp->topology_change_detected) {
1076 stp_transmit_tcn(stp);
1077 stp_start_timer(&stp->tcn_timer, 0);
1079 stp->fdb_needs_flush = true;
1080 stp->topology_change_detected = true;
1081 VLOG_INFO_RL(&rl, "%s: detected topology change.", stp->name);
1085 stp_topology_change_acknowledged(struct stp *stp)
1087 stp->topology_change_detected = false;
1088 stp_stop_timer(&stp->tcn_timer);
1092 stp_acknowledge_topology_change(struct stp_port *p)
1094 p->topology_change_ack = true;
1095 stp_transmit_config(p);
1099 stp_received_config_bpdu(struct stp *stp, struct stp_port *p,
1100 const struct stp_config_bpdu *config)
1102 if (ntohs(config->message_age) >= ntohs(config->max_age)) {
1103 VLOG_WARN("%s: received config BPDU with message age (%u) greater "
1104 "than max age (%u)",
1106 ntohs(config->message_age), ntohs(config->max_age));
1109 if (p->state != STP_DISABLED) {
1110 bool root = stp_is_root_bridge(stp);
1111 if (stp_supersedes_port_info(p, config)) {
1112 stp_record_config_information(p, config);
1113 stp_configuration_update(stp);
1114 stp_port_state_selection(stp);
1115 if (!stp_is_root_bridge(stp) && root) {
1116 stp_stop_timer(&stp->hello_timer);
1117 if (stp->topology_change_detected) {
1118 stp_stop_timer(&stp->topology_change_timer);
1119 stp_transmit_tcn(stp);
1120 stp_start_timer(&stp->tcn_timer, 0);
1123 if (p == stp->root_port) {
1124 stp_record_config_timeout_values(stp, config);
1125 stp_config_bpdu_generation(stp);
1126 if (config->flags & STP_CONFIG_TOPOLOGY_CHANGE_ACK) {
1127 stp_topology_change_acknowledged(stp);
1129 if (config->flags & STP_CONFIG_TOPOLOGY_CHANGE) {
1130 stp->fdb_needs_flush = true;
1133 } else if (stp_is_designated_port(p)) {
1134 stp_transmit_config(p);
1140 stp_received_tcn_bpdu(struct stp *stp, struct stp_port *p)
1142 if (p->state != STP_DISABLED) {
1143 if (stp_is_designated_port(p)) {
1144 stp_topology_change_detection(stp);
1145 stp_acknowledge_topology_change(p);
1151 stp_hello_timer_expiry(struct stp *stp)
1153 stp_config_bpdu_generation(stp);
1154 stp_start_timer(&stp->hello_timer, 0);
1158 stp_message_age_timer_expiry(struct stp_port *p)
1160 struct stp *stp = p->stp;
1161 bool root = stp_is_root_bridge(stp);
1162 stp_become_designated_port(p);
1163 stp_configuration_update(stp);
1164 stp_port_state_selection(stp);
1165 if (stp_is_root_bridge(stp) && !root) {
1166 stp->max_age = stp->bridge_max_age;
1167 stp->hello_time = stp->bridge_hello_time;
1168 stp->forward_delay = stp->bridge_forward_delay;
1169 stp_topology_change_detection(stp);
1170 stp_stop_timer(&stp->tcn_timer);
1171 stp_config_bpdu_generation(stp);
1172 stp_start_timer(&stp->hello_timer, 0);
1177 stp_is_designated_for_some_port(const struct stp *stp)
1179 const struct stp_port *p;
1181 FOR_EACH_ENABLED_PORT (p, stp) {
1182 if (p->designated_bridge == stp->bridge_id) {
1190 stp_forward_delay_timer_expiry(struct stp_port *p)
1192 if (p->state == STP_LISTENING) {
1193 stp_set_port_state(p, STP_LEARNING);
1194 stp_start_timer(&p->forward_delay_timer, 0);
1195 } else if (p->state == STP_LEARNING) {
1196 stp_set_port_state(p, STP_FORWARDING);
1197 if (stp_is_designated_for_some_port(p->stp)) {
1198 if (p->change_detection_enabled) {
1199 stp_topology_change_detection(p->stp);
1206 stp_tcn_timer_expiry(struct stp *stp)
1208 stp_transmit_tcn(stp);
1209 stp_start_timer(&stp->tcn_timer, 0);
1213 stp_topology_change_timer_expiry(struct stp *stp)
1215 stp->topology_change_detected = false;
1216 stp->topology_change = false;
1220 stp_hold_timer_expiry(struct stp_port *p)
1222 if (p->config_pending) {
1223 stp_transmit_config(p);
1228 stp_initialize_port(struct stp_port *p, enum stp_state state)
1230 ovs_assert(state & (STP_DISABLED | STP_BLOCKING));
1231 stp_become_designated_port(p);
1232 stp_set_port_state(p, state);
1233 p->topology_change_ack = false;
1234 p->config_pending = false;
1235 p->change_detection_enabled = true;
1237 stp_stop_timer(&p->message_age_timer);
1238 stp_stop_timer(&p->forward_delay_timer);
1239 stp_stop_timer(&p->hold_timer);
1240 p->tx_count = p->rx_count = p->error_count = 0;
1244 stp_become_root_bridge(struct stp *stp)
1246 stp->max_age = stp->bridge_max_age;
1247 stp->hello_time = stp->bridge_hello_time;
1248 stp->forward_delay = stp->bridge_forward_delay;
1249 stp_topology_change_detection(stp);
1250 stp_stop_timer(&stp->tcn_timer);
1251 stp_config_bpdu_generation(stp);
1252 stp_start_timer(&stp->hello_timer, 0);
1256 stp_start_timer(struct stp_timer *timer, int value)
1258 timer->value = value;
1259 timer->active = true;
1263 stp_stop_timer(struct stp_timer *timer)
1265 timer->active = false;
1269 stp_timer_expired(struct stp_timer *timer, int elapsed, int timeout)
1271 if (timer->active) {
1272 timer->value += elapsed;
1273 if (timer->value >= timeout) {
1274 timer->active = false;
1281 /* Returns the number of whole STP timer ticks in 'ms' milliseconds. There
1282 * are 256 STP timer ticks per second. */
1286 return ms * 0x100 / 1000;
1289 /* Returns the number of whole milliseconds in 'timer' STP timer ticks. There
1290 * are 256 STP timer ticks per second. */
1292 timer_to_ms(int timer)
1294 return timer * 1000 / 0x100;
1298 clamp(int x, int min, int max)
1300 return x < min ? min : x > max ? max : x;
1304 stp_update_bridge_timers(struct stp *stp)
1308 ht = clamp(stp->rq_hello_time, 1000, 10000);
1309 ma = clamp(stp->rq_max_age, MAX(2 * (ht + 1000), 6000), 40000);
1310 fd = clamp(stp->rq_forward_delay, ma / 2 + 1000, 30000);
1312 stp->bridge_hello_time = ms_to_timer(ht);
1313 stp->bridge_max_age = ms_to_timer(ma);
1314 stp->bridge_forward_delay = ms_to_timer(fd);
1316 if (stp_is_root_bridge(stp)) {
1317 stp->max_age = stp->bridge_max_age;
1318 stp->hello_time = stp->bridge_hello_time;
1319 stp->forward_delay = stp->bridge_forward_delay;
1324 stp_send_bpdu(struct stp_port *p, const void *bpdu, size_t bpdu_size)
1326 struct eth_header *eth;
1327 struct llc_header *llc;
1331 pkt = ofpbuf_new(ETH_HEADER_LEN + LLC_HEADER_LEN + bpdu_size);
1332 pkt->l2 = eth = ofpbuf_put_zeros(pkt, sizeof *eth);
1333 llc = ofpbuf_put_zeros(pkt, sizeof *llc);
1334 pkt->l3 = ofpbuf_put(pkt, bpdu, bpdu_size);
1337 memcpy(eth->eth_dst, eth_addr_stp, ETH_ADDR_LEN);
1338 /* p->stp->send_bpdu() must fill in source address. */
1339 eth->eth_type = htons(pkt->size - ETH_HEADER_LEN);
1342 llc->llc_dsap = STP_LLC_DSAP;
1343 llc->llc_ssap = STP_LLC_SSAP;
1344 llc->llc_cntl = STP_LLC_CNTL;
1346 p->stp->send_bpdu(pkt, stp_port_no(p), p->stp->aux);
1353 stp_find(const char *name)
1357 LIST_FOR_EACH (stp, node, &all_stps) {
1358 if (!strcmp(stp->name, name)) {
1366 stp_unixctl_tcn(struct unixctl_conn *conn, int argc,
1367 const char *argv[], void *aux OVS_UNUSED)
1370 struct stp *stp = stp_find(argv[1]);
1373 unixctl_command_reply_error(conn, "no such stp object");
1376 stp_topology_change_detection(stp);
1380 LIST_FOR_EACH (stp, node, &all_stps) {
1381 stp_topology_change_detection(stp);
1385 unixctl_command_reply(conn, "OK");