1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
34 /* Based on sample implementation in 802.1D-1998. Above copyright and license
35 * applies to all modifications. */
38 #include <arpa/inet.h>
47 #define THIS_MODULE VLM_stp
49 /* Ethernet address used as the destination for STP frames. */
50 const uint8_t stp_eth_addr[ETH_ADDR_LEN]
51 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x01 };
53 #define STP_PROTOCOL_ID 0x0000
54 #define STP_PROTOCOL_VERSION 0x00
55 #define STP_TYPE_CONFIG 0x00
56 #define STP_TYPE_TCN 0x80
58 struct stp_bpdu_header {
59 uint16_t protocol_id; /* STP_PROTOCOL_ID. */
60 uint8_t protocol_version; /* STP_PROTOCOL_VERSION. */
61 uint8_t bpdu_type; /* One of STP_TYPE_*. */
62 } __attribute__((packed));
63 BUILD_ASSERT_DECL(sizeof(struct stp_bpdu_header) == 4);
65 enum stp_config_bpdu_flags {
66 STP_CONFIG_TOPOLOGY_CHANGE_ACK = 0x80,
67 STP_CONFIG_TOPOLOGY_CHANGE = 0x01
70 struct stp_config_bpdu {
71 struct stp_bpdu_header header; /* Type STP_TYPE_CONFIG. */
72 uint8_t flags; /* STP_CONFIG_* flags. */
73 uint64_t root_id; /* 8.5.1.1: Bridge believed to be root. */
74 uint32_t root_path_cost; /* 8.5.1.2: Cost of path to root. */
75 uint64_t bridge_id; /* 8.5.1.3: ID of transmitting bridge. */
76 uint16_t port_id; /* 8.5.1.4: Port transmitting the BPDU. */
77 uint16_t message_age; /* 8.5.1.5: Age of BPDU at tx time. */
78 uint16_t max_age; /* 8.5.1.6: Timeout for received data. */
79 uint16_t hello_time; /* 8.5.1.7: Time between BPDU generation. */
80 uint16_t forward_delay; /* 8.5.1.8: State progression delay. */
81 } __attribute__((packed));
82 BUILD_ASSERT_DECL(sizeof(struct stp_config_bpdu) == 35);
85 struct stp_bpdu_header header; /* Type STP_TYPE_TCN. */
86 } __attribute__((packed));
87 BUILD_ASSERT_DECL(sizeof(struct stp_tcn_bpdu) == 4);
90 bool active; /* Timer in use? */
91 int value; /* Current value of timer, counting up. */
96 int port_id; /* 8.5.5.1: Unique port identifier. */
97 enum stp_state state; /* 8.5.5.2: Current state. */
98 int path_cost; /* 8.5.5.3: Cost of tx/rx on this port. */
99 stp_identifier designated_root; /* 8.5.5.4. */
100 int designated_cost; /* 8.5.5.5: Path cost to root on port. */
101 stp_identifier designated_bridge; /* 8.5.5.6. */
102 int designated_port; /* 8.5.5.7: Port to send config msgs on. */
103 bool topology_change_ack; /* 8.5.5.8: Flag for next config BPDU. */
104 bool config_pending; /* 8.5.5.9: Send BPDU when hold expires? */
105 bool change_detection_enabled; /* 8.5.5.10: Detect topology changes? */
107 struct stp_timer message_age_timer; /* 8.5.6.1: Age of received info. */
108 struct stp_timer forward_delay_timer; /* 8.5.6.2: State change timer. */
109 struct stp_timer hold_timer; /* 8.5.6.3: BPDU rate limit timer. */
115 /* Static bridge data. */
116 char *name; /* Human-readable name for log messages. */
117 stp_identifier bridge_id; /* 8.5.3.7: This bridge. */
118 int max_age; /* 8.5.3.4: Time to drop received data. */
119 int hello_time; /* 8.5.3.5: Time between sending BPDUs. */
120 int forward_delay; /* 8.5.3.6: Delay between state changes. */
121 int bridge_max_age; /* 8.5.3.8: max_age when we're root. */
122 int bridge_hello_time; /* 8.5.3.9: hello_time as root. */
123 int bridge_forward_delay; /* 8.5.3.10: forward_delay as root. */
125 /* Dynamic bridge data. */
126 stp_identifier designated_root; /* 8.5.3.1: Bridge believed to be root. */
127 unsigned int root_path_cost; /* 8.5.3.2: Cost of path to root. */
128 struct stp_port *root_port; /* 8.5.3.3: Lowest cost port to root. */
129 bool topology_change_detected; /* 8.5.3.11: Detected a topology change? */
130 bool topology_change; /* 8.5.3.12: Received topology change? */
133 struct stp_timer hello_timer; /* 8.5.4.1: Hello timer. */
134 struct stp_timer tcn_timer; /* 8.5.4.2: Topology change timer. */
135 struct stp_timer topology_change_timer; /* 8.5.4.3. */
138 struct stp_port ports[STP_MAX_PORTS];
140 /* Interface to client. */
141 struct stp_port *first_changed_port;
142 void (*send_bpdu)(const void *bpdu, size_t bpdu_size,
143 int port_no, void *aux);
147 #define FOR_EACH_ENABLED_PORT(PORT, STP) \
148 for ((PORT) = stp_next_enabled_port((STP), (STP)->ports); \
150 (PORT) = stp_next_enabled_port((STP), (PORT) + 1))
151 static struct stp_port *
152 stp_next_enabled_port(const struct stp *stp, const struct stp_port *port)
154 for (; port < &stp->ports[ARRAY_SIZE(stp->ports)]; port++) {
155 if (port->state != STP_DISABLED) {
156 return (struct stp_port *) port;
162 #define SECONDS_TO_TIMER(SECS) ((SECS) * 0x100)
164 #define MESSAGE_AGE_INCREMENT 1
166 static void stp_transmit_config(struct stp_port *);
167 static bool stp_supersedes_port_info(const struct stp_port *,
168 const struct stp_config_bpdu *);
169 static void stp_record_config_information(struct stp_port *,
170 const struct stp_config_bpdu *);
171 static void stp_record_config_timeout_values(struct stp *,
172 const struct stp_config_bpdu *);
173 static bool stp_is_designated_port(const struct stp_port *);
174 static void stp_config_bpdu_generation(struct stp *);
175 static void stp_transmit_tcn(struct stp *);
176 static void stp_configuration_update(struct stp *);
177 static bool stp_supersedes_root(const struct stp_port *root,
178 const struct stp_port *);
179 static void stp_root_selection(struct stp *);
180 static void stp_designated_port_selection(struct stp *);
181 static void stp_become_designated_port(struct stp_port *);
182 static void stp_port_state_selection(struct stp *);
183 static void stp_make_forwarding(struct stp_port *);
184 static void stp_make_blocking(struct stp_port *);
185 static void stp_set_port_state(struct stp_port *, enum stp_state);
186 static void stp_topology_change_detection(struct stp *);
187 static void stp_topology_change_acknowledged(struct stp *);
188 static void stp_acknowledge_topology_change(struct stp_port *);
189 static void stp_received_config_bpdu(struct stp *, struct stp_port *,
190 const struct stp_config_bpdu *);
191 static void stp_received_tcn_bpdu(struct stp *, struct stp_port *,
192 const struct stp_tcn_bpdu *);
193 static void stp_hello_timer_expiry(struct stp *);
194 static void stp_message_age_timer_expiry(struct stp_port *);
195 static bool stp_is_designated_for_some_port(const struct stp *);
196 static void stp_forward_delay_timer_expiry(struct stp_port *);
197 static void stp_tcn_timer_expiry(struct stp *);
198 static void stp_topology_change_timer_expiry(struct stp *);
199 static void stp_hold_timer_expiry(struct stp_port *);
200 static void stp_initialize_port(struct stp_port *, enum stp_state);
201 static void stp_become_root_bridge(struct stp *stp);
203 static void stp_start_timer(struct stp_timer *, int value);
204 static void stp_stop_timer(struct stp_timer *);
205 static bool stp_timer_expired(struct stp_timer *, int elapsed, int timeout);
207 /* Creates and returns a new STP instance that initially has no port enabled.
209 * 'bridge_id' should be a 48-bit MAC address as returned by
210 * eth_addr_to_uint64(). 'bridge_id' may also have a priority value in its top
211 * 16 bits; if those bits are set to 0, the default bridge priority of 32768 is
212 * used. (This priority may be changed with stp_set_bridge_priority().)
214 * When the bridge needs to send out a BPDU, it calls 'send_bpdu', passing
215 * 'aux' as auxiliary data. This callback may be called from stp_tick() or
216 * stp_received_bpdu().
219 stp_create(const char *name, stp_identifier bridge_id,
220 void (*send_bpdu)(const void *bpdu, size_t bpdu_size,
221 int port_no, void *aux),
227 stp = xcalloc(1, sizeof *stp);
228 stp->name = xstrdup(name);
229 stp->bridge_id = bridge_id;
230 if (!(stp->bridge_id >> 48)) {
231 stp->bridge_id |= UINT64_C(32768) << 48;
233 stp->max_age = SECONDS_TO_TIMER(6);
234 stp->hello_time = SECONDS_TO_TIMER(2);
235 stp->forward_delay = SECONDS_TO_TIMER(4);
236 stp->bridge_max_age = stp->max_age;
237 stp->bridge_hello_time = stp->hello_time;
238 stp->bridge_forward_delay = stp->forward_delay;
240 stp->designated_root = stp->bridge_id;
241 stp->root_path_cost = 0;
242 stp->root_port = NULL;
243 stp->topology_change_detected = false;
244 stp->topology_change = false;
246 stp_stop_timer(&stp->tcn_timer);
247 stp_stop_timer(&stp->topology_change_timer);
248 stp_start_timer(&stp->hello_timer, 0);
250 stp->send_bpdu = send_bpdu;
253 stp->first_changed_port = &stp->ports[ARRAY_SIZE(stp->ports)];
254 for (p = stp->ports; p < &stp->ports[ARRAY_SIZE(stp->ports)]; p++) {
256 p->port_id = (stp_port_no(p) + 1) | (128 << 8);
257 p->path_cost = 19; /* Recommended default for 100 Mb/s link. */
258 stp_initialize_port(p, STP_DISABLED);
263 /* Destroys 'stp'. */
265 stp_destroy(struct stp *stp)
271 stp_tick(struct stp *stp, int elapsed)
275 if (stp_timer_expired(&stp->hello_timer, elapsed, stp->hello_time)) {
276 stp_hello_timer_expiry(stp);
278 if (stp_timer_expired(&stp->tcn_timer, elapsed, stp->bridge_hello_time)) {
279 stp_tcn_timer_expiry(stp);
281 if (stp_timer_expired(&stp->topology_change_timer, elapsed,
282 stp->max_age + stp->forward_delay)) {
283 stp_topology_change_timer_expiry(stp);
285 FOR_EACH_ENABLED_PORT (p, stp) {
286 if (stp_timer_expired(&p->message_age_timer, elapsed, stp->max_age)) {
287 stp_message_age_timer_expiry(p);
290 FOR_EACH_ENABLED_PORT (p, stp) {
291 if (stp_timer_expired(&p->forward_delay_timer, elapsed,
292 stp->forward_delay)) {
293 stp_forward_delay_timer_expiry(p);
295 if (stp_timer_expired(&p->hold_timer, elapsed, SECONDS_TO_TIMER(1))) {
296 stp_hold_timer_expiry(p);
302 set_bridge_id(struct stp *stp, stp_identifier new_bridge_id)
304 if (new_bridge_id != stp->bridge_id) {
308 root = stp_is_root_bridge(stp);
309 FOR_EACH_ENABLED_PORT (p, stp) {
310 if (stp_is_designated_port(p)) {
311 p->designated_bridge = new_bridge_id;
314 stp->bridge_id = new_bridge_id;
315 stp_configuration_update(stp);
316 stp_port_state_selection(stp);
317 if (stp_is_root_bridge(stp) && !root) {
318 stp_become_root_bridge(stp);
324 stp_set_bridge_id(struct stp *stp, stp_identifier bridge_id)
326 const uint64_t mac_bits = (UINT64_C(1) << 48) - 1;
327 const uint64_t pri_bits = ~mac_bits;
328 set_bridge_id(stp, (stp->bridge_id & pri_bits) | (bridge_id & mac_bits));
332 stp_set_bridge_priority(struct stp *stp, uint16_t new_priority)
334 const uint64_t mac_bits = (UINT64_C(1) << 48) - 1;
335 set_bridge_id(stp, ((stp->bridge_id & mac_bits)
336 | ((uint64_t) new_priority << 48)));
339 /* Returns the name given to 'stp' in the call to stp_create(). */
341 stp_get_name(const struct stp *stp)
346 /* Returns the bridge ID for 'stp'. */
348 stp_get_bridge_id(const struct stp *stp)
350 return stp->bridge_id;
353 /* Returns the bridge ID of the bridge currently believed to be the root. */
355 stp_get_designated_root(const struct stp *stp)
357 return stp->designated_root;
360 /* Returns true if 'stp' believes itself to the be root of the spanning tree,
361 * false otherwise. */
363 stp_is_root_bridge(const struct stp *stp)
365 return stp->bridge_id == stp->designated_root;
368 /* Returns the cost of the path from 'stp' to the root of the spanning tree. */
370 stp_get_root_path_cost(const struct stp *stp)
372 return stp->root_path_cost;
375 /* Returns the port in 'stp' with index 'port_no', which must be between 0 and
378 stp_get_port(struct stp *stp, int port_no)
380 assert(port_no >= 0 && port_no < ARRAY_SIZE(stp->ports));
381 return &stp->ports[port_no];
384 /* Returns the port connecting 'stp' to the root bridge, or a null pointer if
385 * there is no such port. */
387 stp_get_root_port(struct stp *stp)
389 return stp->root_port;
392 /* Finds a port whose state has changed. If successful, stores the port whose
393 * state changed in '*portp' and returns true. If no port has changed, stores
394 * NULL in '*portp' and returns false. */
396 stp_get_changed_port(struct stp *stp, struct stp_port **portp)
398 struct stp_port *end = &stp->ports[ARRAY_SIZE(stp->ports)];
401 for (p = stp->first_changed_port; p < end; p++) {
402 if (p->state_changed) {
403 p->state_changed = false;
404 stp->first_changed_port = p + 1;
409 stp->first_changed_port = end;
414 /* Returns the name for the given 'state' (for use in debugging and log
417 stp_state_name(enum stp_state state)
435 /* Returns true if 'state' is one in which packets received on a port should
436 * be forwarded, false otherwise. */
438 stp_forward_in_state(enum stp_state state)
440 return state == STP_FORWARDING;
443 /* Returns true if 'state' is one in which MAC learning should be done on
444 * packets received on a port, false otherwise. */
446 stp_learn_in_state(enum stp_state state)
448 return state & (STP_LEARNING | STP_FORWARDING);
451 /* Notifies the STP entity that bridge protocol data unit 'bpdu', which is
452 * 'bpdu_size' bytes in length, was received on port 'p'.
454 * This function may call the 'send_bpdu' function provided to stp_create(). */
456 stp_received_bpdu(struct stp_port *p, const void *bpdu, size_t bpdu_size)
458 struct stp *stp = p->stp;
459 const struct stp_bpdu_header *header;
461 if (p->state == STP_DISABLED) {
465 if (bpdu_size < sizeof(struct stp_bpdu_header)) {
466 VLOG_WARN("%s: received runt %zu-byte BPDU", stp->name, bpdu_size);
471 if (header->protocol_id != htons(STP_PROTOCOL_ID)) {
472 VLOG_WARN("%s: received BPDU with unexpected protocol ID %"PRIu16,
473 stp->name, ntohs(header->protocol_id));
476 if (header->protocol_version != STP_PROTOCOL_VERSION) {
477 VLOG_DBG("%s: received BPDU with unexpected protocol version %"PRIu8,
478 stp->name, header->protocol_version);
481 switch (header->bpdu_type) {
482 case STP_TYPE_CONFIG:
483 if (bpdu_size < sizeof(struct stp_config_bpdu)) {
484 VLOG_WARN("%s: received config BPDU with invalid size %zu",
485 stp->name, bpdu_size);
488 stp_received_config_bpdu(stp, p, bpdu);
492 if (bpdu_size != sizeof(struct stp_tcn_bpdu)) {
493 VLOG_WARN("%s: received TCN BPDU with invalid size %zu",
494 stp->name, bpdu_size);
497 stp_received_tcn_bpdu(stp, p, bpdu);
501 VLOG_WARN("%s: received BPDU of unexpected type %"PRIu8,
502 stp->name, header->bpdu_type);
507 /* Returns the STP entity in which 'p' is nested. */
509 stp_port_get_stp(struct stp_port *p)
514 /* Returns the index of port 'p' within its bridge. */
516 stp_port_no(const struct stp_port *p)
518 struct stp *stp = p->stp;
519 assert(p >= stp->ports && p < &stp->ports[ARRAY_SIZE(stp->ports)]);
520 return p - stp->ports;
523 /* Returns the state of port 'p'. */
525 stp_port_get_state(const struct stp_port *p)
530 /* Disables STP on port 'p'. */
532 stp_port_disable(struct stp_port *p)
534 struct stp *stp = p->stp;
535 if (p->state != STP_DISABLED) {
536 bool root = stp_is_root_bridge(stp);
537 stp_become_designated_port(p);
538 stp_set_port_state(p, STP_DISABLED);
539 p->topology_change_ack = false;
540 p->config_pending = false;
541 stp_stop_timer(&p->message_age_timer);
542 stp_stop_timer(&p->forward_delay_timer);
543 stp_configuration_update(stp);
544 stp_port_state_selection(stp);
545 if (stp_is_root_bridge(stp) && !root) {
546 stp_become_root_bridge(stp);
551 /* Enables STP on port 'p'. The port will initially be in "blocking" state. */
553 stp_port_enable(struct stp_port *p)
555 if (p->state == STP_DISABLED) {
556 stp_initialize_port(p, STP_BLOCKING);
557 stp_port_state_selection(p->stp);
561 /* Sets the priority of port 'p' to 'new_priority'. Lower numerical values
562 * are interpreted as higher priorities. */
564 stp_port_set_priority(struct stp_port *p, uint8_t new_priority)
566 uint16_t new_port_id = (p->port_id & 0xff) | (new_priority << 8);
567 if (p->port_id != new_port_id) {
568 struct stp *stp = p->stp;
569 if (stp_is_designated_port(p)) {
570 p->designated_port = new_port_id;
572 p->port_id = new_port_id;
573 if (stp->bridge_id == p->designated_bridge
574 && p->port_id < p->designated_port) {
575 stp_become_designated_port(p);
576 stp_port_state_selection(stp);
581 /* Sets the path cost of port 'p' to 'path_cost'. Lower values are generally
582 * used to indicate faster links. Use stp_port_set_speed() to automatically
583 * generate a default path cost from a link speed. */
585 stp_port_set_path_cost(struct stp_port *p, unsigned int path_cost)
587 if (p->path_cost != path_cost) {
588 struct stp *stp = p->stp;
589 p->path_cost = path_cost;
590 stp_configuration_update(stp);
591 stp_port_state_selection(stp);
595 /* Sets the path cost of port 'p' based on 'speed' (measured in Mb/s). */
597 stp_port_set_speed(struct stp_port *p, unsigned int speed)
599 stp_port_set_path_cost(p, (speed >= 10000 ? 2 /* 10 Gb/s. */
600 : speed >= 1000 ? 4 /* 1 Gb/s. */
601 : speed >= 100 ? 19 /* 100 Mb/s. */
602 : speed >= 16 ? 62 /* 16 Mb/s. */
603 : speed >= 10 ? 100 /* 10 Mb/s. */
604 : speed >= 4 ? 250 /* 4 Mb/s. */
605 : 19)); /* 100 Mb/s (guess). */
608 /* Enables topology change detection on port 'p'. */
610 stp_port_enable_change_detection(struct stp_port *p)
612 p->change_detection_enabled = true;
615 /* Disables topology change detection on port 'p'. */
617 stp_port_disable_change_detection(struct stp_port *p)
619 p->change_detection_enabled = false;
623 stp_transmit_config(struct stp_port *p)
625 struct stp *stp = p->stp;
626 bool root = stp_is_root_bridge(stp);
627 if (!root && !stp->root_port) {
630 if (p->hold_timer.active) {
631 p->config_pending = true;
633 struct stp_config_bpdu config;
634 memset(&config, 0, sizeof config);
635 config.header.protocol_id = htons(STP_PROTOCOL_ID);
636 config.header.protocol_version = STP_PROTOCOL_VERSION;
637 config.header.bpdu_type = STP_TYPE_CONFIG;
639 if (p->topology_change_ack) {
640 config.flags |= htons(STP_CONFIG_TOPOLOGY_CHANGE_ACK);
642 if (stp->topology_change) {
643 config.flags |= htons(STP_CONFIG_TOPOLOGY_CHANGE);
645 config.root_id = htonll(stp->designated_root);
646 config.root_path_cost = htonl(stp->root_path_cost);
647 config.bridge_id = htonll(stp->bridge_id);
648 config.port_id = htons(p->port_id);
650 config.message_age = htons(0);
652 config.message_age = htons(stp->root_port->message_age_timer.value
653 + MESSAGE_AGE_INCREMENT);
655 config.max_age = htons(stp->max_age);
656 config.hello_time = htons(stp->hello_time);
657 config.forward_delay = htons(stp->forward_delay);
658 if (ntohs(config.message_age) < stp->max_age) {
659 p->topology_change_ack = false;
660 p->config_pending = false;
661 stp->send_bpdu(&config, sizeof config, stp_port_no(p), stp->aux);
662 stp_start_timer(&p->hold_timer, 0);
668 stp_supersedes_port_info(const struct stp_port *p,
669 const struct stp_config_bpdu *config)
671 if (ntohll(config->root_id) != p->designated_root) {
672 return ntohll(config->root_id) < p->designated_root;
673 } else if (ntohl(config->root_path_cost) != p->designated_cost) {
674 return ntohl(config->root_path_cost) < p->designated_cost;
675 } else if (ntohll(config->bridge_id) != p->designated_bridge) {
676 return ntohll(config->bridge_id) < p->designated_bridge;
678 return (ntohll(config->bridge_id) != p->stp->bridge_id
679 || ntohs(config->port_id) <= p->designated_port);
684 stp_record_config_information(struct stp_port *p,
685 const struct stp_config_bpdu *config)
687 p->designated_root = ntohll(config->root_id);
688 p->designated_cost = ntohl(config->root_path_cost);
689 p->designated_bridge = ntohll(config->bridge_id);
690 p->designated_port = ntohs(config->port_id);
691 stp_start_timer(&p->message_age_timer, ntohs(config->message_age));
695 stp_record_config_timeout_values(struct stp *stp,
696 const struct stp_config_bpdu *config)
698 stp->max_age = ntohs(config->max_age);
699 stp->hello_time = ntohs(config->hello_time);
700 stp->forward_delay = ntohs(config->forward_delay);
701 stp->topology_change = config->flags & htons(STP_CONFIG_TOPOLOGY_CHANGE);
705 stp_is_designated_port(const struct stp_port *p)
707 return (p->designated_bridge == p->stp->bridge_id
708 && p->designated_port == p->port_id);
712 stp_config_bpdu_generation(struct stp *stp)
716 FOR_EACH_ENABLED_PORT (p, stp) {
717 if (stp_is_designated_port(p)) {
718 stp_transmit_config(p);
724 stp_transmit_tcn(struct stp *stp)
726 struct stp_port *p = stp->root_port;
727 struct stp_tcn_bpdu tcn_bpdu;
731 tcn_bpdu.header.protocol_id = htons(STP_PROTOCOL_ID);
732 tcn_bpdu.header.protocol_version = STP_PROTOCOL_VERSION;
733 tcn_bpdu.header.bpdu_type = STP_TYPE_TCN;
734 stp->send_bpdu(&tcn_bpdu, sizeof tcn_bpdu, stp_port_no(p), stp->aux);
738 stp_configuration_update(struct stp *stp)
740 stp_root_selection(stp);
741 stp_designated_port_selection(stp);
745 stp_supersedes_root(const struct stp_port *root, const struct stp_port *p)
747 int p_cost = p->designated_cost + p->path_cost;
748 int root_cost = root->designated_cost + root->path_cost;
750 if (p->designated_root != root->designated_root) {
751 return p->designated_root < root->designated_root;
752 } else if (p_cost != root_cost) {
753 return p_cost < root_cost;
754 } else if (p->designated_bridge != root->designated_bridge) {
755 return p->designated_bridge < root->designated_bridge;
756 } else if (p->designated_port != root->designated_port) {
757 return p->designated_port < root->designated_port;
759 return p->port_id < root->port_id;
764 stp_root_selection(struct stp *stp)
766 struct stp_port *p, *root;
769 FOR_EACH_ENABLED_PORT (p, stp) {
770 if (stp_is_designated_port(p)
771 || p->designated_root >= stp->bridge_id) {
774 if (root && !stp_supersedes_root(root, p)) {
779 stp->root_port = root;
781 stp->designated_root = stp->bridge_id;
782 stp->root_path_cost = 0;
784 stp->designated_root = root->designated_root;
785 stp->root_path_cost = root->designated_cost + root->path_cost;
790 stp_designated_port_selection(struct stp *stp)
794 FOR_EACH_ENABLED_PORT (p, stp) {
795 if (stp_is_designated_port(p)
796 || p->designated_root != stp->designated_root
797 || stp->root_path_cost < p->designated_cost
798 || (stp->root_path_cost == p->designated_cost
799 && (stp->bridge_id < p->designated_bridge
800 || (stp->bridge_id == p->designated_bridge
801 && p->port_id <= p->designated_port))))
803 stp_become_designated_port(p);
809 stp_become_designated_port(struct stp_port *p)
811 struct stp *stp = p->stp;
812 p->designated_root = stp->designated_root;
813 p->designated_cost = stp->root_path_cost;
814 p->designated_bridge = stp->bridge_id;
815 p->designated_port = p->port_id;
819 stp_port_state_selection(struct stp *stp)
823 FOR_EACH_ENABLED_PORT (p, stp) {
824 if (p == stp->root_port) {
825 p->config_pending = false;
826 p->topology_change_ack = false;
827 stp_make_forwarding(p);
828 } else if (stp_is_designated_port(p)) {
829 stp_stop_timer(&p->message_age_timer);
830 stp_make_forwarding(p);
832 p->config_pending = false;
833 p->topology_change_ack = false;
834 stp_make_blocking(p);
840 stp_make_forwarding(struct stp_port *p)
842 if (p->state == STP_BLOCKING) {
843 stp_set_port_state(p, STP_LISTENING);
844 stp_start_timer(&p->forward_delay_timer, 0);
849 stp_make_blocking(struct stp_port *p)
851 if (!(p->state & (STP_DISABLED | STP_BLOCKING))) {
852 if (p->state & (STP_FORWARDING | STP_LEARNING)) {
853 if (p->change_detection_enabled) {
854 stp_topology_change_detection(p->stp);
857 stp_set_port_state(p, STP_BLOCKING);
858 stp_stop_timer(&p->forward_delay_timer);
863 stp_set_port_state(struct stp_port *p, enum stp_state state)
865 if (state != p->state && !p->state_changed) {
866 p->state_changed = true;
867 if (p < p->stp->first_changed_port) {
868 p->stp->first_changed_port = p;
875 stp_topology_change_detection(struct stp *stp)
877 if (stp_is_root_bridge(stp)) {
878 stp->topology_change = true;
879 stp_start_timer(&stp->topology_change_timer, 0);
880 } else if (!stp->topology_change_detected) {
881 stp_transmit_tcn(stp);
882 stp_start_timer(&stp->tcn_timer, 0);
884 stp->topology_change_detected = true;
888 stp_topology_change_acknowledged(struct stp *stp)
890 stp->topology_change_detected = false;
891 stp_stop_timer(&stp->tcn_timer);
895 stp_acknowledge_topology_change(struct stp_port *p)
897 p->topology_change_ack = true;
898 stp_transmit_config(p);
902 stp_received_config_bpdu(struct stp *stp, struct stp_port *p,
903 const struct stp_config_bpdu *config)
905 if (ntohs(config->message_age) >= ntohs(config->max_age)) {
906 VLOG_WARN("%s: received config BPDU with message age (%u) greater "
909 ntohs(config->message_age), ntohs(config->max_age));
912 if (p->state != STP_DISABLED) {
913 bool root = stp_is_root_bridge(stp);
914 if (stp_supersedes_port_info(p, config)) {
915 stp_record_config_information(p, config);
916 stp_configuration_update(stp);
917 stp_port_state_selection(stp);
918 if (!stp_is_root_bridge(stp) && root) {
919 stp_stop_timer(&stp->hello_timer);
920 if (stp->topology_change_detected) {
921 stp_stop_timer(&stp->topology_change_timer);
922 stp_transmit_tcn(stp);
923 stp_start_timer(&stp->tcn_timer, 0);
926 if (p == stp->root_port) {
927 stp_record_config_timeout_values(stp, config);
928 stp_config_bpdu_generation(stp);
929 if (config->flags & htons(STP_CONFIG_TOPOLOGY_CHANGE_ACK)) {
930 stp_topology_change_acknowledged(stp);
933 } else if (stp_is_designated_port(p)) {
934 stp_transmit_config(p);
940 stp_received_tcn_bpdu(struct stp *stp, struct stp_port *p,
941 const struct stp_tcn_bpdu *tcn)
943 if (p->state != STP_DISABLED) {
944 if (stp_is_designated_port(p)) {
945 stp_topology_change_detection(stp);
946 stp_acknowledge_topology_change(p);
952 stp_hello_timer_expiry(struct stp *stp)
954 stp_config_bpdu_generation(stp);
955 stp_start_timer(&stp->hello_timer, 0);
959 stp_message_age_timer_expiry(struct stp_port *p)
961 struct stp *stp = p->stp;
962 bool root = stp_is_root_bridge(stp);
963 stp_become_designated_port(p);
964 stp_configuration_update(stp);
965 stp_port_state_selection(stp);
966 if (stp_is_root_bridge(stp) && !root) {
967 stp->max_age = stp->bridge_max_age;
968 stp->hello_time = stp->bridge_hello_time;
969 stp->forward_delay = stp->bridge_forward_delay;
970 stp_topology_change_detection(stp);
971 stp_stop_timer(&stp->tcn_timer);
972 stp_config_bpdu_generation(stp);
973 stp_start_timer(&stp->hello_timer, 0);
978 stp_is_designated_for_some_port(const struct stp *stp)
980 const struct stp_port *p;
982 FOR_EACH_ENABLED_PORT (p, stp) {
983 if (p->designated_bridge == stp->bridge_id) {
991 stp_forward_delay_timer_expiry(struct stp_port *p)
993 if (p->state == STP_LISTENING) {
994 stp_set_port_state(p, STP_LEARNING);
995 stp_start_timer(&p->forward_delay_timer, 0);
996 } else if (p->state == STP_LEARNING) {
997 stp_set_port_state(p, STP_FORWARDING);
998 if (stp_is_designated_for_some_port(p->stp)) {
999 if (p->change_detection_enabled) {
1000 stp_topology_change_detection(p->stp);
1007 stp_tcn_timer_expiry(struct stp *stp)
1009 stp_transmit_tcn(stp);
1010 stp_start_timer(&stp->tcn_timer, 0);
1014 stp_topology_change_timer_expiry(struct stp *stp)
1016 stp->topology_change_detected = false;
1017 stp->topology_change = false;
1021 stp_hold_timer_expiry(struct stp_port *p)
1023 if (p->config_pending) {
1024 stp_transmit_config(p);
1029 stp_initialize_port(struct stp_port *p, enum stp_state state)
1031 assert(state & (STP_DISABLED | STP_BLOCKING));
1032 stp_become_designated_port(p);
1033 stp_set_port_state(p, state);
1034 p->topology_change_ack = false;
1035 p->config_pending = false;
1036 p->change_detection_enabled = true;
1037 stp_stop_timer(&p->message_age_timer);
1038 stp_stop_timer(&p->forward_delay_timer);
1039 stp_stop_timer(&p->hold_timer);
1043 stp_become_root_bridge(struct stp *stp)
1045 stp->max_age = stp->bridge_max_age;
1046 stp->hello_time = stp->bridge_hello_time;
1047 stp->forward_delay = stp->bridge_forward_delay;
1048 stp_topology_change_detection(stp);
1049 stp_stop_timer(&stp->tcn_timer);
1050 stp_config_bpdu_generation(stp);
1051 stp_start_timer(&stp->hello_timer, 0);
1055 stp_start_timer(struct stp_timer *timer, int value)
1057 timer->value = value;
1058 timer->active = true;
1062 stp_stop_timer(struct stp_timer *timer)
1064 timer->active = false;
1068 stp_timer_expired(struct stp_timer *timer, int elapsed, int timeout)
1070 if (timer->active) {
1071 timer->value += elapsed;
1072 if (timer->value >= timeout) {
1073 timer->active = false;