2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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>
29 #include "byte-order.h"
36 VLOG_DEFINE_THIS_MODULE(stp);
38 #define STP_PROTOCOL_ID 0x0000
39 #define STP_PROTOCOL_VERSION 0x00
40 #define STP_TYPE_CONFIG 0x00
41 #define STP_TYPE_TCN 0x80
43 struct stp_bpdu_header {
44 ovs_be16 protocol_id; /* STP_PROTOCOL_ID. */
45 uint8_t protocol_version; /* STP_PROTOCOL_VERSION. */
46 uint8_t bpdu_type; /* One of STP_TYPE_*. */
47 } __attribute__((packed));
48 BUILD_ASSERT_DECL(sizeof(struct stp_bpdu_header) == 4);
50 enum stp_config_bpdu_flags {
51 STP_CONFIG_TOPOLOGY_CHANGE_ACK = 0x80,
52 STP_CONFIG_TOPOLOGY_CHANGE = 0x01
55 struct stp_config_bpdu {
56 struct stp_bpdu_header header; /* Type STP_TYPE_CONFIG. */
57 uint8_t flags; /* STP_CONFIG_* flags. */
58 ovs_be64 root_id; /* 8.5.1.1: Bridge believed to be root. */
59 ovs_be32 root_path_cost; /* 8.5.1.2: Cost of path to root. */
60 ovs_be64 bridge_id; /* 8.5.1.3: ID of transmitting bridge. */
61 ovs_be16 port_id; /* 8.5.1.4: Port transmitting the BPDU. */
62 ovs_be16 message_age; /* 8.5.1.5: Age of BPDU at tx time. */
63 ovs_be16 max_age; /* 8.5.1.6: Timeout for received data. */
64 ovs_be16 hello_time; /* 8.5.1.7: Time between BPDU generation. */
65 ovs_be16 forward_delay; /* 8.5.1.8: State progression delay. */
66 } __attribute__((packed));
67 BUILD_ASSERT_DECL(sizeof(struct stp_config_bpdu) == 35);
70 struct stp_bpdu_header header; /* Type STP_TYPE_TCN. */
71 } __attribute__((packed));
72 BUILD_ASSERT_DECL(sizeof(struct stp_tcn_bpdu) == 4);
75 bool active; /* Timer in use? */
76 int value; /* Current value of timer, counting up. */
81 void *aux; /* Auxiliary data the user may retrieve. */
82 int port_id; /* 8.5.5.1: Unique port identifier. */
83 enum stp_state state; /* 8.5.5.2: Current state. */
84 int path_cost; /* 8.5.5.3: Cost of tx/rx on this port. */
85 stp_identifier designated_root; /* 8.5.5.4. */
86 int designated_cost; /* 8.5.5.5: Path cost to root on port. */
87 stp_identifier designated_bridge; /* 8.5.5.6. */
88 int designated_port; /* 8.5.5.7: Port to send config msgs on. */
89 bool topology_change_ack; /* 8.5.5.8: Flag for next config BPDU. */
90 bool config_pending; /* 8.5.5.9: Send BPDU when hold expires? */
91 bool change_detection_enabled; /* 8.5.5.10: Detect topology changes? */
93 struct stp_timer message_age_timer; /* 8.5.6.1: Age of received info. */
94 struct stp_timer forward_delay_timer; /* 8.5.6.2: State change timer. */
95 struct stp_timer hold_timer; /* 8.5.6.3: BPDU rate limit timer. */
97 int tx_count; /* Number of BPDUs transmitted. */
98 int rx_count; /* Number of valid BPDUs received. */
99 int error_count; /* Number of bad BPDUs received. */
105 struct list node; /* Node in all_stps list. */
107 /* Static bridge data. */
108 char *name; /* Human-readable name for log messages. */
109 stp_identifier bridge_id; /* 8.5.3.7: This bridge. */
110 int max_age; /* 8.5.3.4: Time to drop received data. */
111 int hello_time; /* 8.5.3.5: Time between sending BPDUs. */
112 int forward_delay; /* 8.5.3.6: Delay between state changes. */
113 int bridge_max_age; /* 8.5.3.8: max_age when we're root. */
114 int bridge_hello_time; /* 8.5.3.9: hello_time as root. */
115 int bridge_forward_delay; /* 8.5.3.10: forward_delay as root. */
116 int rq_max_age; /* User-requested max age, in ms. */
117 int rq_hello_time; /* User-requested hello time, in ms. */
118 int rq_forward_delay; /* User-requested forward delay, in ms. */
119 int elapsed_remainder; /* Left-over msecs from last stp_tick(). */
121 /* Dynamic bridge data. */
122 stp_identifier designated_root; /* 8.5.3.1: Bridge believed to be root. */
123 unsigned int root_path_cost; /* 8.5.3.2: Cost of path to root. */
124 struct stp_port *root_port; /* 8.5.3.3: Lowest cost port to root. */
125 bool topology_change_detected; /* 8.5.3.11: Detected a topology change? */
126 bool topology_change; /* 8.5.3.12: Received topology change? */
129 struct stp_timer hello_timer; /* 8.5.4.1: Hello timer. */
130 struct stp_timer tcn_timer; /* 8.5.4.2: Topology change timer. */
131 struct stp_timer topology_change_timer; /* 8.5.4.3. */
134 struct stp_port ports[STP_MAX_PORTS];
136 /* Interface to client. */
137 bool fdb_needs_flush; /* MAC learning tables needs flushing. */
138 struct stp_port *first_changed_port;
139 void (*send_bpdu)(struct ofpbuf *bpdu, int port_no, void *aux);
143 static struct list all_stps = LIST_INITIALIZER(&all_stps);
145 #define FOR_EACH_ENABLED_PORT(PORT, STP) \
146 for ((PORT) = stp_next_enabled_port((STP), (STP)->ports); \
148 (PORT) = stp_next_enabled_port((STP), (PORT) + 1))
149 static struct stp_port *
150 stp_next_enabled_port(const struct stp *stp, const struct stp_port *port)
152 for (; port < &stp->ports[ARRAY_SIZE(stp->ports)]; port++) {
153 if (port->state != STP_DISABLED) {
154 return (struct stp_port *) port;
160 #define MESSAGE_AGE_INCREMENT 1
162 static void stp_transmit_config(struct stp_port *);
163 static bool stp_supersedes_port_info(const struct stp_port *,
164 const struct stp_config_bpdu *);
165 static void stp_record_config_information(struct stp_port *,
166 const struct stp_config_bpdu *);
167 static void stp_record_config_timeout_values(struct stp *,
168 const struct stp_config_bpdu *);
169 static bool stp_is_designated_port(const struct stp_port *);
170 static void stp_config_bpdu_generation(struct stp *);
171 static void stp_transmit_tcn(struct stp *);
172 static void stp_configuration_update(struct stp *);
173 static bool stp_supersedes_root(const struct stp_port *root,
174 const struct stp_port *);
175 static void stp_root_selection(struct stp *);
176 static void stp_designated_port_selection(struct stp *);
177 static void stp_become_designated_port(struct stp_port *);
178 static void stp_port_state_selection(struct stp *);
179 static void stp_make_forwarding(struct stp_port *);
180 static void stp_make_blocking(struct stp_port *);
181 static void stp_set_port_state(struct stp_port *, enum stp_state);
182 static void stp_topology_change_detection(struct stp *);
183 static void stp_topology_change_acknowledged(struct stp *);
184 static void stp_acknowledge_topology_change(struct stp_port *);
185 static void stp_received_config_bpdu(struct stp *, struct stp_port *,
186 const struct stp_config_bpdu *);
187 static void stp_received_tcn_bpdu(struct stp *, struct stp_port *);
188 static void stp_hello_timer_expiry(struct stp *);
189 static void stp_message_age_timer_expiry(struct stp_port *);
190 static bool stp_is_designated_for_some_port(const struct stp *);
191 static void stp_forward_delay_timer_expiry(struct stp_port *);
192 static void stp_tcn_timer_expiry(struct stp *);
193 static void stp_topology_change_timer_expiry(struct stp *);
194 static void stp_hold_timer_expiry(struct stp_port *);
195 static void stp_initialize_port(struct stp_port *, enum stp_state);
196 static void stp_become_root_bridge(struct stp *);
197 static void stp_update_bridge_timers(struct stp *);
199 static int clamp(int x, int min, int max);
200 static int ms_to_timer(int ms);
201 static int timer_to_ms(int timer);
202 static void stp_start_timer(struct stp_timer *, int value);
203 static void stp_stop_timer(struct stp_timer *);
204 static bool stp_timer_expired(struct stp_timer *, int elapsed, int timeout);
206 static void stp_send_bpdu(struct stp_port *, const void *, size_t);
207 static void stp_unixctl_tcn(struct unixctl_conn *, int argc,
208 const char *argv[], void *aux);
213 unixctl_command_register("stp/tcn", "[bridge]", 0, 1, stp_unixctl_tcn,
217 /* Creates and returns a new STP instance that initially has no ports enabled.
219 * 'bridge_id' should be a 48-bit MAC address as returned by
220 * eth_addr_to_uint64(). 'bridge_id' may also have a priority value in its top
221 * 16 bits; if those bits are set to 0, STP_DEFAULT_BRIDGE_PRIORITY is used.
222 * (This priority may be changed with stp_set_bridge_priority().)
224 * When the bridge needs to send out a BPDU, it calls 'send_bpdu'. This
225 * callback may be called from stp_tick() or stp_received_bpdu(). The
226 * arguments to 'send_bpdu' are an STP BPDU encapsulated in 'bpdu',
227 * the spanning tree port number 'port_no' that should transmit the
228 * packet, and auxiliary data to be passed to the callback in 'aux'.
231 stp_create(const char *name, stp_identifier bridge_id,
232 void (*send_bpdu)(struct ofpbuf *bpdu, int port_no, void *aux),
238 stp = xzalloc(sizeof *stp);
239 stp->name = xstrdup(name);
240 stp->bridge_id = bridge_id;
241 if (!(stp->bridge_id >> 48)) {
242 stp->bridge_id |= (uint64_t) STP_DEFAULT_BRIDGE_PRIORITY << 48;
245 stp->rq_max_age = STP_DEFAULT_MAX_AGE;
246 stp->rq_hello_time = STP_DEFAULT_HELLO_TIME;
247 stp->rq_forward_delay = STP_DEFAULT_FWD_DELAY;
248 stp_update_bridge_timers(stp);
249 stp->max_age = stp->bridge_max_age;
250 stp->hello_time = stp->bridge_hello_time;
251 stp->forward_delay = stp->bridge_forward_delay;
253 stp->designated_root = stp->bridge_id;
254 stp->root_path_cost = 0;
255 stp->root_port = NULL;
256 stp->topology_change_detected = false;
257 stp->topology_change = false;
259 stp_stop_timer(&stp->tcn_timer);
260 stp_stop_timer(&stp->topology_change_timer);
261 stp_start_timer(&stp->hello_timer, 0);
263 stp->send_bpdu = send_bpdu;
266 stp->first_changed_port = &stp->ports[ARRAY_SIZE(stp->ports)];
267 for (p = stp->ports; p < &stp->ports[ARRAY_SIZE(stp->ports)]; p++) {
269 p->port_id = (stp_port_no(p) + 1) | (STP_DEFAULT_PORT_PRIORITY << 8);
270 p->path_cost = 19; /* Recommended default for 100 Mb/s link. */
271 stp_initialize_port(p, STP_DISABLED);
273 list_push_back(&all_stps, &stp->node);
277 /* Destroys 'stp'. */
279 stp_destroy(struct stp *stp)
282 list_remove(&stp->node);
288 /* Runs 'stp' given that 'ms' milliseconds have passed. */
290 stp_tick(struct stp *stp, int ms)
295 /* Convert 'ms' to STP timer ticks. Preserve any leftover milliseconds
296 * from previous stp_tick() calls so that we don't lose STP ticks when we
297 * are called too frequently. */
298 ms = clamp(ms, 0, INT_MAX - 1000) + stp->elapsed_remainder;
299 elapsed = ms_to_timer(ms);
300 stp->elapsed_remainder = ms - timer_to_ms(elapsed);
305 if (stp_timer_expired(&stp->hello_timer, elapsed, stp->hello_time)) {
306 stp_hello_timer_expiry(stp);
308 if (stp_timer_expired(&stp->tcn_timer, elapsed, stp->bridge_hello_time)) {
309 stp_tcn_timer_expiry(stp);
311 if (stp_timer_expired(&stp->topology_change_timer, elapsed,
312 stp->max_age + stp->forward_delay)) {
313 stp_topology_change_timer_expiry(stp);
315 FOR_EACH_ENABLED_PORT (p, stp) {
316 if (stp_timer_expired(&p->message_age_timer, elapsed, stp->max_age)) {
317 stp_message_age_timer_expiry(p);
320 FOR_EACH_ENABLED_PORT (p, stp) {
321 if (stp_timer_expired(&p->forward_delay_timer, elapsed,
322 stp->forward_delay)) {
323 stp_forward_delay_timer_expiry(p);
325 if (stp_timer_expired(&p->hold_timer, elapsed, ms_to_timer(1000))) {
326 stp_hold_timer_expiry(p);
332 set_bridge_id(struct stp *stp, stp_identifier new_bridge_id)
334 if (new_bridge_id != stp->bridge_id) {
338 root = stp_is_root_bridge(stp);
339 FOR_EACH_ENABLED_PORT (p, stp) {
340 if (stp_is_designated_port(p)) {
341 p->designated_bridge = new_bridge_id;
344 stp->bridge_id = new_bridge_id;
345 stp_configuration_update(stp);
346 stp_port_state_selection(stp);
347 if (stp_is_root_bridge(stp) && !root) {
348 stp_become_root_bridge(stp);
354 stp_set_bridge_id(struct stp *stp, stp_identifier bridge_id)
356 const uint64_t mac_bits = (UINT64_C(1) << 48) - 1;
357 const uint64_t pri_bits = ~mac_bits;
358 set_bridge_id(stp, (stp->bridge_id & pri_bits) | (bridge_id & mac_bits));
362 stp_set_bridge_priority(struct stp *stp, uint16_t new_priority)
364 const uint64_t mac_bits = (UINT64_C(1) << 48) - 1;
365 set_bridge_id(stp, ((stp->bridge_id & mac_bits)
366 | ((uint64_t) new_priority << 48)));
369 /* Sets the desired hello time for 'stp' to 'ms', in milliseconds. The actual
370 * hello time is clamped to the range of 1 to 10 seconds and subject to the
371 * relationship (bridge_max_age >= 2 * (bridge_hello_time + 1 s)). The bridge
372 * hello time is only used when 'stp' is the root bridge. */
374 stp_set_hello_time(struct stp *stp, int ms)
376 stp->rq_hello_time = ms;
377 stp_update_bridge_timers(stp);
380 /* Sets the desired max age for 'stp' to 'ms', in milliseconds. The actual max
381 * age is clamped to the range of 6 to 40 seconds and subject to the
382 * relationships (2 * (bridge_forward_delay - 1 s) >= bridge_max_age) and
383 * (bridge_max_age >= 2 * (bridge_hello_time + 1 s)). The bridge max age is
384 * only used when 'stp' is the root bridge. */
386 stp_set_max_age(struct stp *stp, int ms)
388 stp->rq_max_age = ms;
389 stp_update_bridge_timers(stp);
392 /* Sets the desired forward delay for 'stp' to 'ms', in milliseconds. The
393 * actual forward delay is clamped to the range of 4 to 30 seconds and subject
394 * to the relationship (2 * (bridge_forward_delay - 1 s) >= bridge_max_age).
395 * The bridge forward delay is only used when 'stp' is the root bridge. */
397 stp_set_forward_delay(struct stp *stp, int ms)
399 stp->rq_forward_delay = ms;
400 stp_update_bridge_timers(stp);
403 /* Returns the name given to 'stp' in the call to stp_create(). */
405 stp_get_name(const struct stp *stp)
410 /* Returns the bridge ID for 'stp'. */
412 stp_get_bridge_id(const struct stp *stp)
414 return stp->bridge_id;
417 /* Returns the bridge ID of the bridge currently believed to be the root. */
419 stp_get_designated_root(const struct stp *stp)
421 return stp->designated_root;
424 /* Returns true if 'stp' believes itself to the be root of the spanning tree,
425 * false otherwise. */
427 stp_is_root_bridge(const struct stp *stp)
429 return stp->bridge_id == stp->designated_root;
432 /* Returns the cost of the path from 'stp' to the root of the spanning tree. */
434 stp_get_root_path_cost(const struct stp *stp)
436 return stp->root_path_cost;
439 /* Returns the bridge hello time, in ms. The returned value is not necessarily
440 * the value passed to stp_set_hello_time(): it is clamped to the valid range
441 * and quantized to the STP timer resolution. */
443 stp_get_hello_time(const struct stp *stp)
445 return timer_to_ms(stp->bridge_hello_time);
448 /* Returns the bridge max age, in ms. The returned value is not necessarily
449 * the value passed to stp_set_max_age(): it is clamped to the valid range,
450 * quantized to the STP timer resolution, and adjusted to match the constraints
451 * due to the hello time. */
453 stp_get_max_age(const struct stp *stp)
455 return timer_to_ms(stp->bridge_max_age);
458 /* Returns the bridge forward delay, in ms. The returned value is not
459 * necessarily the value passed to stp_set_forward_delay(): it is clamped to
460 * the valid range, quantized to the STP timer resolution, and adjusted to
461 * match the constraints due to the forward delay. */
463 stp_get_forward_delay(const struct stp *stp)
465 return timer_to_ms(stp->bridge_forward_delay);
468 /* Returns true if something has happened to 'stp' which necessitates flushing
469 * the client's MAC learning table. Calling this function resets 'stp' so that
470 * future calls will return false until flushing is required again. */
472 stp_check_and_reset_fdb_flush(struct stp *stp)
474 bool needs_flush = stp->fdb_needs_flush;
475 stp->fdb_needs_flush = false;
479 /* Returns the port in 'stp' with index 'port_no', which must be between 0 and
482 stp_get_port(struct stp *stp, int port_no)
484 assert(port_no >= 0 && port_no < ARRAY_SIZE(stp->ports));
485 return &stp->ports[port_no];
488 /* Returns the port connecting 'stp' to the root bridge, or a null pointer if
489 * there is no such port. */
491 stp_get_root_port(struct stp *stp)
493 return stp->root_port;
496 /* Finds a port whose state has changed. If successful, stores the port whose
497 * state changed in '*portp' and returns true. If no port has changed, stores
498 * NULL in '*portp' and returns false. */
500 stp_get_changed_port(struct stp *stp, struct stp_port **portp)
502 struct stp_port *end = &stp->ports[ARRAY_SIZE(stp->ports)];
505 for (p = stp->first_changed_port; p < end; p++) {
506 if (p->state_changed) {
507 p->state_changed = false;
508 stp->first_changed_port = p + 1;
513 stp->first_changed_port = end;
518 /* Returns the name for the given 'state' (for use in debugging and log
521 stp_state_name(enum stp_state state)
539 /* Returns true if 'state' is one in which packets received on a port should
540 * be forwarded, false otherwise.
542 * Returns true if 'state' is STP_DISABLED, since presumably in that case the
543 * port should still work, just not have STP applied to it. */
545 stp_forward_in_state(enum stp_state state)
547 return (state & (STP_DISABLED | STP_FORWARDING)) != 0;
550 /* Returns true if 'state' is one in which MAC learning should be done on
551 * packets received on a port, false otherwise.
553 * Returns true if 'state' is STP_DISABLED, since presumably in that case the
554 * port should still work, just not have STP applied to it. */
556 stp_learn_in_state(enum stp_state state)
558 return (state & (STP_DISABLED | STP_LEARNING | STP_FORWARDING)) != 0;
561 /* Returns the name for the given 'role' (for use in debugging and log
564 stp_role_name(enum stp_role role)
569 case STP_ROLE_DESIGNATED:
571 case STP_ROLE_ALTERNATE:
573 case STP_ROLE_DISABLED:
580 /* Notifies the STP entity that bridge protocol data unit 'bpdu', which is
581 * 'bpdu_size' bytes in length, was received on port 'p'.
583 * This function may call the 'send_bpdu' function provided to stp_create(). */
585 stp_received_bpdu(struct stp_port *p, const void *bpdu, size_t bpdu_size)
587 struct stp *stp = p->stp;
588 const struct stp_bpdu_header *header;
590 if (p->state == STP_DISABLED) {
594 if (bpdu_size < sizeof(struct stp_bpdu_header)) {
595 VLOG_WARN("%s: received runt %zu-byte BPDU", stp->name, bpdu_size);
601 if (header->protocol_id != htons(STP_PROTOCOL_ID)) {
602 VLOG_WARN("%s: received BPDU with unexpected protocol ID %"PRIu16,
603 stp->name, ntohs(header->protocol_id));
607 if (header->protocol_version != STP_PROTOCOL_VERSION) {
608 VLOG_DBG("%s: received BPDU with unexpected protocol version %"PRIu8,
609 stp->name, header->protocol_version);
612 switch (header->bpdu_type) {
613 case STP_TYPE_CONFIG:
614 if (bpdu_size < sizeof(struct stp_config_bpdu)) {
615 VLOG_WARN("%s: received config BPDU with invalid size %zu",
616 stp->name, bpdu_size);
620 stp_received_config_bpdu(stp, p, bpdu);
624 if (bpdu_size != sizeof(struct stp_tcn_bpdu)) {
625 VLOG_WARN("%s: received TCN BPDU with invalid size %zu",
626 stp->name, bpdu_size);
630 stp_received_tcn_bpdu(stp, p);
634 VLOG_WARN("%s: received BPDU of unexpected type %"PRIu8,
635 stp->name, header->bpdu_type);
642 /* Returns the STP entity in which 'p' is nested. */
644 stp_port_get_stp(struct stp_port *p)
649 /* Sets the 'aux' member of 'p'.
651 * The 'aux' member will be reset to NULL when stp_port_disable() is
652 * called or stp_port_enable() is called when the port is in a Disabled
655 stp_port_set_aux(struct stp_port *p, void *aux)
660 /* Returns the 'aux' member of 'p'. */
662 stp_port_get_aux(struct stp_port *p)
667 /* Returns the index of port 'p' within its bridge. */
669 stp_port_no(const struct stp_port *p)
671 struct stp *stp = p->stp;
672 assert(p >= stp->ports && p < &stp->ports[ARRAY_SIZE(stp->ports)]);
673 return p - stp->ports;
676 /* Returns the port ID for 'p'. */
678 stp_port_get_id(const struct stp_port *p)
683 /* Returns the state of port 'p'. */
685 stp_port_get_state(const struct stp_port *p)
690 /* Returns the role of port 'p'. */
692 stp_port_get_role(const struct stp_port *p)
694 struct stp_port *root_port = stp_get_root_port(p->stp);
696 if (root_port && root_port->port_id == p->port_id) {
697 return STP_ROLE_ROOT;
698 } else if (stp_is_designated_port(p)) {
699 return STP_ROLE_DESIGNATED;
700 } else if (p->state == STP_DISABLED) {
701 return STP_ROLE_DISABLED;
703 return STP_ROLE_ALTERNATE;
707 /* Retrieves BPDU transmit and receive counts for 'p'. */
708 void stp_port_get_counts(const struct stp_port *p,
709 int *tx_count, int *rx_count, int *error_count)
711 *tx_count = p->tx_count;
712 *rx_count = p->rx_count;
713 *error_count = p->error_count;
716 /* Disables STP on port 'p'. */
718 stp_port_disable(struct stp_port *p)
720 struct stp *stp = p->stp;
721 if (p->state != STP_DISABLED) {
722 bool root = stp_is_root_bridge(stp);
723 stp_become_designated_port(p);
724 stp_set_port_state(p, STP_DISABLED);
725 p->topology_change_ack = false;
726 p->config_pending = false;
727 stp_stop_timer(&p->message_age_timer);
728 stp_stop_timer(&p->forward_delay_timer);
729 stp_configuration_update(stp);
730 stp_port_state_selection(stp);
731 if (stp_is_root_bridge(stp) && !root) {
732 stp_become_root_bridge(stp);
738 /* Enables STP on port 'p'. The port will initially be in "blocking" state. */
740 stp_port_enable(struct stp_port *p)
742 if (p->state == STP_DISABLED) {
743 stp_initialize_port(p, STP_BLOCKING);
744 stp_port_state_selection(p->stp);
748 /* Sets the priority of port 'p' to 'new_priority'. Lower numerical values
749 * are interpreted as higher priorities. */
751 stp_port_set_priority(struct stp_port *p, uint8_t new_priority)
753 uint16_t new_port_id = (p->port_id & 0xff) | (new_priority << 8);
754 if (p->port_id != new_port_id) {
755 struct stp *stp = p->stp;
756 if (stp_is_designated_port(p)) {
757 p->designated_port = new_port_id;
759 p->port_id = new_port_id;
760 if (stp->bridge_id == p->designated_bridge
761 && p->port_id < p->designated_port) {
762 stp_become_designated_port(p);
763 stp_port_state_selection(stp);
768 /* Convert 'speed' (measured in Mb/s) into the path cost. */
770 stp_convert_speed_to_cost(unsigned int speed)
772 return speed >= 10000 ? 2 /* 10 Gb/s. */
773 : speed >= 1000 ? 4 /* 1 Gb/s. */
774 : speed >= 100 ? 19 /* 100 Mb/s. */
775 : speed >= 16 ? 62 /* 16 Mb/s. */
776 : speed >= 10 ? 100 /* 10 Mb/s. */
777 : speed >= 4 ? 250 /* 4 Mb/s. */
778 : 19; /* 100 Mb/s (guess). */
781 /* Sets the path cost of port 'p' to 'path_cost'. Lower values are generally
782 * used to indicate faster links. Use stp_port_set_speed() to automatically
783 * generate a default path cost from a link speed. */
785 stp_port_set_path_cost(struct stp_port *p, uint16_t path_cost)
787 if (p->path_cost != path_cost) {
788 struct stp *stp = p->stp;
789 p->path_cost = path_cost;
790 stp_configuration_update(stp);
791 stp_port_state_selection(stp);
795 /* Sets the path cost of port 'p' based on 'speed' (measured in Mb/s). */
797 stp_port_set_speed(struct stp_port *p, unsigned int speed)
799 stp_port_set_path_cost(p, stp_convert_speed_to_cost(speed));
802 /* Enables topology change detection on port 'p'. */
804 stp_port_enable_change_detection(struct stp_port *p)
806 p->change_detection_enabled = true;
809 /* Disables topology change detection on port 'p'. */
811 stp_port_disable_change_detection(struct stp_port *p)
813 p->change_detection_enabled = false;
817 stp_transmit_config(struct stp_port *p)
819 struct stp *stp = p->stp;
820 bool root = stp_is_root_bridge(stp);
821 if (!root && !stp->root_port) {
824 if (p->hold_timer.active) {
825 p->config_pending = true;
827 struct stp_config_bpdu config;
828 memset(&config, 0, sizeof config);
829 config.header.protocol_id = htons(STP_PROTOCOL_ID);
830 config.header.protocol_version = STP_PROTOCOL_VERSION;
831 config.header.bpdu_type = STP_TYPE_CONFIG;
833 if (p->topology_change_ack) {
834 config.flags |= STP_CONFIG_TOPOLOGY_CHANGE_ACK;
836 if (stp->topology_change) {
837 config.flags |= STP_CONFIG_TOPOLOGY_CHANGE;
839 config.root_id = htonll(stp->designated_root);
840 config.root_path_cost = htonl(stp->root_path_cost);
841 config.bridge_id = htonll(stp->bridge_id);
842 config.port_id = htons(p->port_id);
844 config.message_age = htons(0);
846 config.message_age = htons(stp->root_port->message_age_timer.value
847 + MESSAGE_AGE_INCREMENT);
849 config.max_age = htons(stp->max_age);
850 config.hello_time = htons(stp->hello_time);
851 config.forward_delay = htons(stp->forward_delay);
852 if (ntohs(config.message_age) < stp->max_age) {
853 p->topology_change_ack = false;
854 p->config_pending = false;
855 stp_send_bpdu(p, &config, sizeof config);
856 stp_start_timer(&p->hold_timer, 0);
862 stp_supersedes_port_info(const struct stp_port *p,
863 const struct stp_config_bpdu *config)
865 if (ntohll(config->root_id) != p->designated_root) {
866 return ntohll(config->root_id) < p->designated_root;
867 } else if (ntohl(config->root_path_cost) != p->designated_cost) {
868 return ntohl(config->root_path_cost) < p->designated_cost;
869 } else if (ntohll(config->bridge_id) != p->designated_bridge) {
870 return ntohll(config->bridge_id) < p->designated_bridge;
872 return (ntohll(config->bridge_id) != p->stp->bridge_id
873 || ntohs(config->port_id) <= p->designated_port);
878 stp_record_config_information(struct stp_port *p,
879 const struct stp_config_bpdu *config)
881 p->designated_root = ntohll(config->root_id);
882 p->designated_cost = ntohl(config->root_path_cost);
883 p->designated_bridge = ntohll(config->bridge_id);
884 p->designated_port = ntohs(config->port_id);
885 stp_start_timer(&p->message_age_timer, ntohs(config->message_age));
889 stp_record_config_timeout_values(struct stp *stp,
890 const struct stp_config_bpdu *config)
892 stp->max_age = ntohs(config->max_age);
893 stp->hello_time = ntohs(config->hello_time);
894 stp->forward_delay = ntohs(config->forward_delay);
895 stp->topology_change = config->flags & STP_CONFIG_TOPOLOGY_CHANGE;
899 stp_is_designated_port(const struct stp_port *p)
901 return (p->designated_bridge == p->stp->bridge_id
902 && p->designated_port == p->port_id);
906 stp_config_bpdu_generation(struct stp *stp)
910 FOR_EACH_ENABLED_PORT (p, stp) {
911 if (stp_is_designated_port(p)) {
912 stp_transmit_config(p);
918 stp_transmit_tcn(struct stp *stp)
920 struct stp_port *p = stp->root_port;
921 struct stp_tcn_bpdu tcn_bpdu;
925 tcn_bpdu.header.protocol_id = htons(STP_PROTOCOL_ID);
926 tcn_bpdu.header.protocol_version = STP_PROTOCOL_VERSION;
927 tcn_bpdu.header.bpdu_type = STP_TYPE_TCN;
928 stp_send_bpdu(p, &tcn_bpdu, sizeof tcn_bpdu);
932 stp_configuration_update(struct stp *stp)
934 stp_root_selection(stp);
935 stp_designated_port_selection(stp);
939 stp_supersedes_root(const struct stp_port *root, const struct stp_port *p)
941 int p_cost = p->designated_cost + p->path_cost;
942 int root_cost = root->designated_cost + root->path_cost;
944 if (p->designated_root != root->designated_root) {
945 return p->designated_root < root->designated_root;
946 } else if (p_cost != root_cost) {
947 return p_cost < root_cost;
948 } else if (p->designated_bridge != root->designated_bridge) {
949 return p->designated_bridge < root->designated_bridge;
950 } else if (p->designated_port != root->designated_port) {
951 return p->designated_port < root->designated_port;
953 return p->port_id < root->port_id;
958 stp_root_selection(struct stp *stp)
960 struct stp_port *p, *root;
963 FOR_EACH_ENABLED_PORT (p, stp) {
964 if (stp_is_designated_port(p)
965 || p->designated_root >= stp->bridge_id) {
968 if (root && !stp_supersedes_root(root, p)) {
973 stp->root_port = root;
975 stp->designated_root = stp->bridge_id;
976 stp->root_path_cost = 0;
978 stp->designated_root = root->designated_root;
979 stp->root_path_cost = root->designated_cost + root->path_cost;
984 stp_designated_port_selection(struct stp *stp)
988 FOR_EACH_ENABLED_PORT (p, stp) {
989 if (stp_is_designated_port(p)
990 || p->designated_root != stp->designated_root
991 || stp->root_path_cost < p->designated_cost
992 || (stp->root_path_cost == p->designated_cost
993 && (stp->bridge_id < p->designated_bridge
994 || (stp->bridge_id == p->designated_bridge
995 && p->port_id <= p->designated_port))))
997 stp_become_designated_port(p);
1003 stp_become_designated_port(struct stp_port *p)
1005 struct stp *stp = p->stp;
1006 p->designated_root = stp->designated_root;
1007 p->designated_cost = stp->root_path_cost;
1008 p->designated_bridge = stp->bridge_id;
1009 p->designated_port = p->port_id;
1013 stp_port_state_selection(struct stp *stp)
1017 FOR_EACH_ENABLED_PORT (p, stp) {
1018 if (p == stp->root_port) {
1019 p->config_pending = false;
1020 p->topology_change_ack = false;
1021 stp_make_forwarding(p);
1022 } else if (stp_is_designated_port(p)) {
1023 stp_stop_timer(&p->message_age_timer);
1024 stp_make_forwarding(p);
1026 p->config_pending = false;
1027 p->topology_change_ack = false;
1028 stp_make_blocking(p);
1034 stp_make_forwarding(struct stp_port *p)
1036 if (p->state == STP_BLOCKING) {
1037 stp_set_port_state(p, STP_LISTENING);
1038 stp_start_timer(&p->forward_delay_timer, 0);
1043 stp_make_blocking(struct stp_port *p)
1045 if (!(p->state & (STP_DISABLED | STP_BLOCKING))) {
1046 if (p->state & (STP_FORWARDING | STP_LEARNING)) {
1047 if (p->change_detection_enabled) {
1048 stp_topology_change_detection(p->stp);
1051 stp_set_port_state(p, STP_BLOCKING);
1052 stp_stop_timer(&p->forward_delay_timer);
1057 stp_set_port_state(struct stp_port *p, enum stp_state state)
1059 if (state != p->state && !p->state_changed) {
1060 p->state_changed = true;
1061 if (p < p->stp->first_changed_port) {
1062 p->stp->first_changed_port = p;
1069 stp_topology_change_detection(struct stp *stp)
1071 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1073 if (stp_is_root_bridge(stp)) {
1074 stp->topology_change = true;
1075 stp_start_timer(&stp->topology_change_timer, 0);
1076 } else if (!stp->topology_change_detected) {
1077 stp_transmit_tcn(stp);
1078 stp_start_timer(&stp->tcn_timer, 0);
1080 stp->fdb_needs_flush = true;
1081 stp->topology_change_detected = true;
1082 VLOG_INFO_RL(&rl, "%s: detected topology change.", stp->name);
1086 stp_topology_change_acknowledged(struct stp *stp)
1088 stp->topology_change_detected = false;
1089 stp_stop_timer(&stp->tcn_timer);
1093 stp_acknowledge_topology_change(struct stp_port *p)
1095 p->topology_change_ack = true;
1096 stp_transmit_config(p);
1100 stp_received_config_bpdu(struct stp *stp, struct stp_port *p,
1101 const struct stp_config_bpdu *config)
1103 if (ntohs(config->message_age) >= ntohs(config->max_age)) {
1104 VLOG_WARN("%s: received config BPDU with message age (%u) greater "
1105 "than max age (%u)",
1107 ntohs(config->message_age), ntohs(config->max_age));
1110 if (p->state != STP_DISABLED) {
1111 bool root = stp_is_root_bridge(stp);
1112 if (stp_supersedes_port_info(p, config)) {
1113 stp_record_config_information(p, config);
1114 stp_configuration_update(stp);
1115 stp_port_state_selection(stp);
1116 if (!stp_is_root_bridge(stp) && root) {
1117 stp_stop_timer(&stp->hello_timer);
1118 if (stp->topology_change_detected) {
1119 stp_stop_timer(&stp->topology_change_timer);
1120 stp_transmit_tcn(stp);
1121 stp_start_timer(&stp->tcn_timer, 0);
1124 if (p == stp->root_port) {
1125 stp_record_config_timeout_values(stp, config);
1126 stp_config_bpdu_generation(stp);
1127 if (config->flags & STP_CONFIG_TOPOLOGY_CHANGE_ACK) {
1128 stp_topology_change_acknowledged(stp);
1130 if (config->flags & STP_CONFIG_TOPOLOGY_CHANGE) {
1131 stp->fdb_needs_flush = true;
1134 } else if (stp_is_designated_port(p)) {
1135 stp_transmit_config(p);
1141 stp_received_tcn_bpdu(struct stp *stp, struct stp_port *p)
1143 if (p->state != STP_DISABLED) {
1144 if (stp_is_designated_port(p)) {
1145 stp_topology_change_detection(stp);
1146 stp_acknowledge_topology_change(p);
1152 stp_hello_timer_expiry(struct stp *stp)
1154 stp_config_bpdu_generation(stp);
1155 stp_start_timer(&stp->hello_timer, 0);
1159 stp_message_age_timer_expiry(struct stp_port *p)
1161 struct stp *stp = p->stp;
1162 bool root = stp_is_root_bridge(stp);
1163 stp_become_designated_port(p);
1164 stp_configuration_update(stp);
1165 stp_port_state_selection(stp);
1166 if (stp_is_root_bridge(stp) && !root) {
1167 stp->max_age = stp->bridge_max_age;
1168 stp->hello_time = stp->bridge_hello_time;
1169 stp->forward_delay = stp->bridge_forward_delay;
1170 stp_topology_change_detection(stp);
1171 stp_stop_timer(&stp->tcn_timer);
1172 stp_config_bpdu_generation(stp);
1173 stp_start_timer(&stp->hello_timer, 0);
1178 stp_is_designated_for_some_port(const struct stp *stp)
1180 const struct stp_port *p;
1182 FOR_EACH_ENABLED_PORT (p, stp) {
1183 if (p->designated_bridge == stp->bridge_id) {
1191 stp_forward_delay_timer_expiry(struct stp_port *p)
1193 if (p->state == STP_LISTENING) {
1194 stp_set_port_state(p, STP_LEARNING);
1195 stp_start_timer(&p->forward_delay_timer, 0);
1196 } else if (p->state == STP_LEARNING) {
1197 stp_set_port_state(p, STP_FORWARDING);
1198 if (stp_is_designated_for_some_port(p->stp)) {
1199 if (p->change_detection_enabled) {
1200 stp_topology_change_detection(p->stp);
1207 stp_tcn_timer_expiry(struct stp *stp)
1209 stp_transmit_tcn(stp);
1210 stp_start_timer(&stp->tcn_timer, 0);
1214 stp_topology_change_timer_expiry(struct stp *stp)
1216 stp->topology_change_detected = false;
1217 stp->topology_change = false;
1221 stp_hold_timer_expiry(struct stp_port *p)
1223 if (p->config_pending) {
1224 stp_transmit_config(p);
1229 stp_initialize_port(struct stp_port *p, enum stp_state state)
1231 assert(state & (STP_DISABLED | STP_BLOCKING));
1232 stp_become_designated_port(p);
1233 stp_set_port_state(p, state);
1234 p->topology_change_ack = false;
1235 p->config_pending = false;
1236 p->change_detection_enabled = true;
1238 stp_stop_timer(&p->message_age_timer);
1239 stp_stop_timer(&p->forward_delay_timer);
1240 stp_stop_timer(&p->hold_timer);
1241 p->tx_count = p->rx_count = p->error_count = 0;
1245 stp_become_root_bridge(struct stp *stp)
1247 stp->max_age = stp->bridge_max_age;
1248 stp->hello_time = stp->bridge_hello_time;
1249 stp->forward_delay = stp->bridge_forward_delay;
1250 stp_topology_change_detection(stp);
1251 stp_stop_timer(&stp->tcn_timer);
1252 stp_config_bpdu_generation(stp);
1253 stp_start_timer(&stp->hello_timer, 0);
1257 stp_start_timer(struct stp_timer *timer, int value)
1259 timer->value = value;
1260 timer->active = true;
1264 stp_stop_timer(struct stp_timer *timer)
1266 timer->active = false;
1270 stp_timer_expired(struct stp_timer *timer, int elapsed, int timeout)
1272 if (timer->active) {
1273 timer->value += elapsed;
1274 if (timer->value >= timeout) {
1275 timer->active = false;
1282 /* Returns the number of whole STP timer ticks in 'ms' milliseconds. There
1283 * are 256 STP timer ticks per second. */
1287 return ms * 0x100 / 1000;
1290 /* Returns the number of whole milliseconds in 'timer' STP timer ticks. There
1291 * are 256 STP timer ticks per second. */
1293 timer_to_ms(int timer)
1295 return timer * 1000 / 0x100;
1299 clamp(int x, int min, int max)
1301 return x < min ? min : x > max ? max : x;
1305 stp_update_bridge_timers(struct stp *stp)
1309 ht = clamp(stp->rq_hello_time, 1000, 10000);
1310 ma = clamp(stp->rq_max_age, MAX(2 * (ht + 1000), 6000), 40000);
1311 fd = clamp(stp->rq_forward_delay, ma / 2 + 1000, 30000);
1313 stp->bridge_hello_time = ms_to_timer(ht);
1314 stp->bridge_max_age = ms_to_timer(ma);
1315 stp->bridge_forward_delay = ms_to_timer(fd);
1317 if (stp_is_root_bridge(stp)) {
1318 stp->max_age = stp->bridge_max_age;
1319 stp->hello_time = stp->bridge_hello_time;
1320 stp->forward_delay = stp->bridge_forward_delay;
1325 stp_send_bpdu(struct stp_port *p, const void *bpdu, size_t bpdu_size)
1327 struct eth_header *eth;
1328 struct llc_header *llc;
1332 pkt = ofpbuf_new(ETH_HEADER_LEN + LLC_HEADER_LEN + bpdu_size);
1333 pkt->l2 = eth = ofpbuf_put_zeros(pkt, sizeof *eth);
1334 llc = ofpbuf_put_zeros(pkt, sizeof *llc);
1335 pkt->l3 = ofpbuf_put(pkt, bpdu, bpdu_size);
1338 memcpy(eth->eth_dst, eth_addr_stp, ETH_ADDR_LEN);
1339 /* p->stp->send_bpdu() must fill in source address. */
1340 eth->eth_type = htons(pkt->size - ETH_HEADER_LEN);
1343 llc->llc_dsap = STP_LLC_DSAP;
1344 llc->llc_ssap = STP_LLC_SSAP;
1345 llc->llc_cntl = STP_LLC_CNTL;
1347 p->stp->send_bpdu(pkt, stp_port_no(p), p->stp->aux);
1354 stp_find(const char *name)
1358 LIST_FOR_EACH (stp, node, &all_stps) {
1359 if (!strcmp(stp->name, name)) {
1367 stp_unixctl_tcn(struct unixctl_conn *conn, int argc,
1368 const char *argv[], void *aux OVS_UNUSED)
1371 struct stp *stp = stp_find(argv[1]);
1374 unixctl_command_reply(conn, 501, "no such stp object");
1377 stp_topology_change_detection(stp);
1381 LIST_FOR_EACH (stp, node, &all_stps) {
1382 stp_topology_change_detection(stp);
1386 unixctl_command_reply(conn, 200, "OK");