2 * Copyright (c) 2008, 2009, 2010 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. */
21 #include <arpa/inet.h>
31 #define THIS_MODULE VLM_stp
33 /* Ethernet address used as the destination for STP frames. */
34 const uint8_t stp_eth_addr[ETH_ADDR_LEN]
35 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x01 };
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 uint16_t 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 uint64_t root_id; /* 8.5.1.1: Bridge believed to be root. */
58 uint32_t root_path_cost; /* 8.5.1.2: Cost of path to root. */
59 uint64_t bridge_id; /* 8.5.1.3: ID of transmitting bridge. */
60 uint16_t port_id; /* 8.5.1.4: Port transmitting the BPDU. */
61 uint16_t message_age; /* 8.5.1.5: Age of BPDU at tx time. */
62 uint16_t max_age; /* 8.5.1.6: Timeout for received data. */
63 uint16_t hello_time; /* 8.5.1.7: Time between BPDU generation. */
64 uint16_t 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 int port_id; /* 8.5.5.1: Unique port identifier. */
81 enum stp_state state; /* 8.5.5.2: Current state. */
82 int path_cost; /* 8.5.5.3: Cost of tx/rx on this port. */
83 stp_identifier designated_root; /* 8.5.5.4. */
84 int designated_cost; /* 8.5.5.5: Path cost to root on port. */
85 stp_identifier designated_bridge; /* 8.5.5.6. */
86 int designated_port; /* 8.5.5.7: Port to send config msgs on. */
87 bool topology_change_ack; /* 8.5.5.8: Flag for next config BPDU. */
88 bool config_pending; /* 8.5.5.9: Send BPDU when hold expires? */
89 bool change_detection_enabled; /* 8.5.5.10: Detect topology changes? */
91 struct stp_timer message_age_timer; /* 8.5.6.1: Age of received info. */
92 struct stp_timer forward_delay_timer; /* 8.5.6.2: State change timer. */
93 struct stp_timer hold_timer; /* 8.5.6.3: BPDU rate limit timer. */
99 /* Static bridge data. */
100 char *name; /* Human-readable name for log messages. */
101 stp_identifier bridge_id; /* 8.5.3.7: This bridge. */
102 int max_age; /* 8.5.3.4: Time to drop received data. */
103 int hello_time; /* 8.5.3.5: Time between sending BPDUs. */
104 int forward_delay; /* 8.5.3.6: Delay between state changes. */
105 int bridge_max_age; /* 8.5.3.8: max_age when we're root. */
106 int bridge_hello_time; /* 8.5.3.9: hello_time as root. */
107 int bridge_forward_delay; /* 8.5.3.10: forward_delay as root. */
108 int rq_max_age; /* User-requested max age, in ms. */
109 int rq_hello_time; /* User-requested hello time, in ms. */
110 int rq_forward_delay; /* User-requested forward delay, in ms. */
111 int elapsed_remainder; /* Left-over msecs from last stp_tick(). */
113 /* Dynamic bridge data. */
114 stp_identifier designated_root; /* 8.5.3.1: Bridge believed to be root. */
115 unsigned int root_path_cost; /* 8.5.3.2: Cost of path to root. */
116 struct stp_port *root_port; /* 8.5.3.3: Lowest cost port to root. */
117 bool topology_change_detected; /* 8.5.3.11: Detected a topology change? */
118 bool topology_change; /* 8.5.3.12: Received topology change? */
121 struct stp_timer hello_timer; /* 8.5.4.1: Hello timer. */
122 struct stp_timer tcn_timer; /* 8.5.4.2: Topology change timer. */
123 struct stp_timer topology_change_timer; /* 8.5.4.3. */
126 struct stp_port ports[STP_MAX_PORTS];
128 /* Interface to client. */
129 struct stp_port *first_changed_port;
130 void (*send_bpdu)(struct ofpbuf *bpdu, int port_no, void *aux);
134 #define FOR_EACH_ENABLED_PORT(PORT, STP) \
135 for ((PORT) = stp_next_enabled_port((STP), (STP)->ports); \
137 (PORT) = stp_next_enabled_port((STP), (PORT) + 1))
138 static struct stp_port *
139 stp_next_enabled_port(const struct stp *stp, const struct stp_port *port)
141 for (; port < &stp->ports[ARRAY_SIZE(stp->ports)]; port++) {
142 if (port->state != STP_DISABLED) {
143 return (struct stp_port *) port;
149 #define MESSAGE_AGE_INCREMENT 1
151 static void stp_transmit_config(struct stp_port *);
152 static bool stp_supersedes_port_info(const struct stp_port *,
153 const struct stp_config_bpdu *);
154 static void stp_record_config_information(struct stp_port *,
155 const struct stp_config_bpdu *);
156 static void stp_record_config_timeout_values(struct stp *,
157 const struct stp_config_bpdu *);
158 static bool stp_is_designated_port(const struct stp_port *);
159 static void stp_config_bpdu_generation(struct stp *);
160 static void stp_transmit_tcn(struct stp *);
161 static void stp_configuration_update(struct stp *);
162 static bool stp_supersedes_root(const struct stp_port *root,
163 const struct stp_port *);
164 static void stp_root_selection(struct stp *);
165 static void stp_designated_port_selection(struct stp *);
166 static void stp_become_designated_port(struct stp_port *);
167 static void stp_port_state_selection(struct stp *);
168 static void stp_make_forwarding(struct stp_port *);
169 static void stp_make_blocking(struct stp_port *);
170 static void stp_set_port_state(struct stp_port *, enum stp_state);
171 static void stp_topology_change_detection(struct stp *);
172 static void stp_topology_change_acknowledged(struct stp *);
173 static void stp_acknowledge_topology_change(struct stp_port *);
174 static void stp_received_config_bpdu(struct stp *, struct stp_port *,
175 const struct stp_config_bpdu *);
176 static void stp_received_tcn_bpdu(struct stp *, struct stp_port *);
177 static void stp_hello_timer_expiry(struct stp *);
178 static void stp_message_age_timer_expiry(struct stp_port *);
179 static bool stp_is_designated_for_some_port(const struct stp *);
180 static void stp_forward_delay_timer_expiry(struct stp_port *);
181 static void stp_tcn_timer_expiry(struct stp *);
182 static void stp_topology_change_timer_expiry(struct stp *);
183 static void stp_hold_timer_expiry(struct stp_port *);
184 static void stp_initialize_port(struct stp_port *, enum stp_state);
185 static void stp_become_root_bridge(struct stp *);
186 static void stp_update_bridge_timers(struct stp *);
188 static int clamp(int x, int min, int max);
189 static int ms_to_timer(int ms);
190 static int ms_to_timer_remainder(int ms);
191 static int timer_to_ms(int timer);
192 static void stp_start_timer(struct stp_timer *, int value);
193 static void stp_stop_timer(struct stp_timer *);
194 static bool stp_timer_expired(struct stp_timer *, int elapsed, int timeout);
196 static void stp_send_bpdu(struct stp_port *, const void *, size_t);
198 /* Creates and returns a new STP instance that initially has no ports enabled.
200 * 'bridge_id' should be a 48-bit MAC address as returned by
201 * eth_addr_to_uint64(). 'bridge_id' may also have a priority value in its top
202 * 16 bits; if those bits are set to 0, STP_DEFAULT_BRIDGE_PRIORITY is used.
203 * (This priority may be changed with stp_set_bridge_priority().)
205 * When the bridge needs to send out a BPDU, it calls 'send_bpdu'. This
206 * callback may be called from stp_tick() or stp_received_bpdu(). The
207 * arguments to 'send_bpdu' are an STP BPDU encapsulated in
210 stp_create(const char *name, stp_identifier bridge_id,
211 void (*send_bpdu)(struct ofpbuf *bpdu, int port_no, void *aux),
217 stp = xzalloc(sizeof *stp);
218 stp->name = xstrdup(name);
219 stp->bridge_id = bridge_id;
220 if (!(stp->bridge_id >> 48)) {
221 stp->bridge_id |= (uint64_t) STP_DEFAULT_BRIDGE_PRIORITY << 48;
224 stp->rq_max_age = 6000;
225 stp->rq_hello_time = 2000;
226 stp->rq_forward_delay = 4000;
227 stp_update_bridge_timers(stp);
228 stp->max_age = stp->bridge_max_age;
229 stp->hello_time = stp->bridge_hello_time;
230 stp->forward_delay = stp->bridge_forward_delay;
232 stp->designated_root = stp->bridge_id;
233 stp->root_path_cost = 0;
234 stp->root_port = NULL;
235 stp->topology_change_detected = false;
236 stp->topology_change = false;
238 stp_stop_timer(&stp->tcn_timer);
239 stp_stop_timer(&stp->topology_change_timer);
240 stp_start_timer(&stp->hello_timer, 0);
242 stp->send_bpdu = send_bpdu;
245 stp->first_changed_port = &stp->ports[ARRAY_SIZE(stp->ports)];
246 for (p = stp->ports; p < &stp->ports[ARRAY_SIZE(stp->ports)]; p++) {
248 p->port_id = (stp_port_no(p) + 1) | (STP_DEFAULT_PORT_PRIORITY << 8);
249 p->path_cost = 19; /* Recommended default for 100 Mb/s link. */
250 stp_initialize_port(p, STP_DISABLED);
255 /* Destroys 'stp'. */
257 stp_destroy(struct stp *stp)
265 /* Runs 'stp' given that 'ms' milliseconds have passed. */
267 stp_tick(struct stp *stp, int ms)
272 /* Convert 'ms' to STP timer ticks. Preserve any leftover milliseconds
273 * from previous stp_tick() calls so that we don't lose STP ticks when we
274 * are called too frequently. */
275 ms = clamp(ms, 0, INT_MAX - 1000) + stp->elapsed_remainder;
276 elapsed = ms_to_timer(ms);
277 stp->elapsed_remainder = ms_to_timer_remainder(ms);
282 if (stp_timer_expired(&stp->hello_timer, elapsed, stp->hello_time)) {
283 stp_hello_timer_expiry(stp);
285 if (stp_timer_expired(&stp->tcn_timer, elapsed, stp->bridge_hello_time)) {
286 stp_tcn_timer_expiry(stp);
288 if (stp_timer_expired(&stp->topology_change_timer, elapsed,
289 stp->max_age + stp->forward_delay)) {
290 stp_topology_change_timer_expiry(stp);
292 FOR_EACH_ENABLED_PORT (p, stp) {
293 if (stp_timer_expired(&p->message_age_timer, elapsed, stp->max_age)) {
294 stp_message_age_timer_expiry(p);
297 FOR_EACH_ENABLED_PORT (p, stp) {
298 if (stp_timer_expired(&p->forward_delay_timer, elapsed,
299 stp->forward_delay)) {
300 stp_forward_delay_timer_expiry(p);
302 if (stp_timer_expired(&p->hold_timer, elapsed, ms_to_timer(1000))) {
303 stp_hold_timer_expiry(p);
309 set_bridge_id(struct stp *stp, stp_identifier new_bridge_id)
311 if (new_bridge_id != stp->bridge_id) {
315 root = stp_is_root_bridge(stp);
316 FOR_EACH_ENABLED_PORT (p, stp) {
317 if (stp_is_designated_port(p)) {
318 p->designated_bridge = new_bridge_id;
321 stp->bridge_id = new_bridge_id;
322 stp_configuration_update(stp);
323 stp_port_state_selection(stp);
324 if (stp_is_root_bridge(stp) && !root) {
325 stp_become_root_bridge(stp);
331 stp_set_bridge_id(struct stp *stp, stp_identifier bridge_id)
333 const uint64_t mac_bits = (UINT64_C(1) << 48) - 1;
334 const uint64_t pri_bits = ~mac_bits;
335 set_bridge_id(stp, (stp->bridge_id & pri_bits) | (bridge_id & mac_bits));
339 stp_set_bridge_priority(struct stp *stp, uint16_t new_priority)
341 const uint64_t mac_bits = (UINT64_C(1) << 48) - 1;
342 set_bridge_id(stp, ((stp->bridge_id & mac_bits)
343 | ((uint64_t) new_priority << 48)));
346 /* Sets the desired hello time for 'stp' to 'ms', in milliseconds. The actual
347 * hello time is clamped to the range of 1 to 10 seconds and subject to the
348 * relationship (bridge_max_age >= 2 * (bridge_hello_time + 1 s)). The bridge
349 * hello time is only used when 'stp' is the root bridge. */
351 stp_set_hello_time(struct stp *stp, int ms)
353 stp->rq_hello_time = ms;
354 stp_update_bridge_timers(stp);
357 /* Sets the desired max age for 'stp' to 'ms', in milliseconds. The actual max
358 * age is clamped to the range of 6 to 40 seconds and subject to the
359 * relationships (2 * (bridge_forward_delay - 1 s) >= bridge_max_age) and
360 * (bridge_max_age >= 2 * (bridge_hello_time + 1 s)). The bridge max age is
361 * only used when 'stp' is the root bridge. */
363 stp_set_max_age(struct stp *stp, int ms)
365 stp->rq_max_age = ms;
366 stp_update_bridge_timers(stp);
369 /* Sets the desired forward delay for 'stp' to 'ms', in milliseconds. The
370 * actual forward delay is clamped to the range of 4 to 30 seconds and subject
371 * to the relationship (2 * (bridge_forward_delay - 1 s) >= bridge_max_age).
372 * The bridge forward delay is only used when 'stp' is the root bridge. */
374 stp_set_forward_delay(struct stp *stp, int ms)
376 stp->rq_forward_delay = ms;
377 stp_update_bridge_timers(stp);
380 /* Returns the name given to 'stp' in the call to stp_create(). */
382 stp_get_name(const struct stp *stp)
387 /* Returns the bridge ID for 'stp'. */
389 stp_get_bridge_id(const struct stp *stp)
391 return stp->bridge_id;
394 /* Returns the bridge ID of the bridge currently believed to be the root. */
396 stp_get_designated_root(const struct stp *stp)
398 return stp->designated_root;
401 /* Returns true if 'stp' believes itself to the be root of the spanning tree,
402 * false otherwise. */
404 stp_is_root_bridge(const struct stp *stp)
406 return stp->bridge_id == stp->designated_root;
409 /* Returns the cost of the path from 'stp' to the root of the spanning tree. */
411 stp_get_root_path_cost(const struct stp *stp)
413 return stp->root_path_cost;
416 /* Returns the bridge hello time, in ms. The returned value is not necessarily
417 * the value passed to stp_set_hello_time(): it is clamped to the valid range
418 * and quantized to the STP timer resolution. */
420 stp_get_hello_time(const struct stp *stp)
422 return timer_to_ms(stp->bridge_hello_time);
425 /* Returns the bridge max age, in ms. The returned value is not necessarily
426 * the value passed to stp_set_max_age(): it is clamped to the valid range,
427 * quantized to the STP timer resolution, and adjusted to match the constraints
428 * due to the hello time. */
430 stp_get_max_age(const struct stp *stp)
432 return timer_to_ms(stp->bridge_max_age);
435 /* Returns the bridge forward delay, in ms. The returned value is not
436 * necessarily the value passed to stp_set_forward_delay(): it is clamped to
437 * the valid range, quantized to the STP timer resolution, and adjusted to
438 * match the constraints due to the forward delay. */
440 stp_get_forward_delay(const struct stp *stp)
442 return timer_to_ms(stp->bridge_forward_delay);
445 /* Returns the port in 'stp' with index 'port_no', which must be between 0 and
448 stp_get_port(struct stp *stp, int port_no)
450 assert(port_no >= 0 && port_no < ARRAY_SIZE(stp->ports));
451 return &stp->ports[port_no];
454 /* Returns the port connecting 'stp' to the root bridge, or a null pointer if
455 * there is no such port. */
457 stp_get_root_port(struct stp *stp)
459 return stp->root_port;
462 /* Finds a port whose state has changed. If successful, stores the port whose
463 * state changed in '*portp' and returns true. If no port has changed, stores
464 * NULL in '*portp' and returns false. */
466 stp_get_changed_port(struct stp *stp, struct stp_port **portp)
468 struct stp_port *end = &stp->ports[ARRAY_SIZE(stp->ports)];
471 for (p = stp->first_changed_port; p < end; p++) {
472 if (p->state_changed) {
473 p->state_changed = false;
474 stp->first_changed_port = p + 1;
479 stp->first_changed_port = end;
484 /* Returns the name for the given 'state' (for use in debugging and log
487 stp_state_name(enum stp_state state)
505 /* Returns true if 'state' is one in which packets received on a port should
506 * be forwarded, false otherwise.
508 * Returns true if 'state' is STP_DISABLED, since presumably in that case the
509 * port should still work, just not have STP applied to it. */
511 stp_forward_in_state(enum stp_state state)
513 return (state & (STP_DISABLED | STP_FORWARDING)) != 0;
516 /* Returns true if 'state' is one in which MAC learning should be done on
517 * packets received on a port, false otherwise.
519 * Returns true if 'state' is STP_DISABLED, since presumably in that case the
520 * port should still work, just not have STP applied to it. */
522 stp_learn_in_state(enum stp_state state)
524 return (state & (STP_DISABLED | STP_LEARNING | STP_FORWARDING)) != 0;
527 /* Notifies the STP entity that bridge protocol data unit 'bpdu', which is
528 * 'bpdu_size' bytes in length, was received on port 'p'.
530 * This function may call the 'send_bpdu' function provided to stp_create(). */
532 stp_received_bpdu(struct stp_port *p, const void *bpdu, size_t bpdu_size)
534 struct stp *stp = p->stp;
535 const struct stp_bpdu_header *header;
537 if (p->state == STP_DISABLED) {
541 if (bpdu_size < sizeof(struct stp_bpdu_header)) {
542 VLOG_WARN("%s: received runt %zu-byte BPDU", stp->name, bpdu_size);
547 if (header->protocol_id != htons(STP_PROTOCOL_ID)) {
548 VLOG_WARN("%s: received BPDU with unexpected protocol ID %"PRIu16,
549 stp->name, ntohs(header->protocol_id));
552 if (header->protocol_version != STP_PROTOCOL_VERSION) {
553 VLOG_DBG("%s: received BPDU with unexpected protocol version %"PRIu8,
554 stp->name, header->protocol_version);
557 switch (header->bpdu_type) {
558 case STP_TYPE_CONFIG:
559 if (bpdu_size < sizeof(struct stp_config_bpdu)) {
560 VLOG_WARN("%s: received config BPDU with invalid size %zu",
561 stp->name, bpdu_size);
564 stp_received_config_bpdu(stp, p, bpdu);
568 if (bpdu_size != sizeof(struct stp_tcn_bpdu)) {
569 VLOG_WARN("%s: received TCN BPDU with invalid size %zu",
570 stp->name, bpdu_size);
573 stp_received_tcn_bpdu(stp, p);
577 VLOG_WARN("%s: received BPDU of unexpected type %"PRIu8,
578 stp->name, header->bpdu_type);
583 /* Returns the STP entity in which 'p' is nested. */
585 stp_port_get_stp(struct stp_port *p)
590 /* Returns the index of port 'p' within its bridge. */
592 stp_port_no(const struct stp_port *p)
594 struct stp *stp = p->stp;
595 assert(p >= stp->ports && p < &stp->ports[ARRAY_SIZE(stp->ports)]);
596 return p - stp->ports;
599 /* Returns the state of port 'p'. */
601 stp_port_get_state(const struct stp_port *p)
606 /* Disables STP on port 'p'. */
608 stp_port_disable(struct stp_port *p)
610 struct stp *stp = p->stp;
611 if (p->state != STP_DISABLED) {
612 bool root = stp_is_root_bridge(stp);
613 stp_become_designated_port(p);
614 stp_set_port_state(p, STP_DISABLED);
615 p->topology_change_ack = false;
616 p->config_pending = false;
617 stp_stop_timer(&p->message_age_timer);
618 stp_stop_timer(&p->forward_delay_timer);
619 stp_configuration_update(stp);
620 stp_port_state_selection(stp);
621 if (stp_is_root_bridge(stp) && !root) {
622 stp_become_root_bridge(stp);
627 /* Enables STP on port 'p'. The port will initially be in "blocking" state. */
629 stp_port_enable(struct stp_port *p)
631 if (p->state == STP_DISABLED) {
632 stp_initialize_port(p, STP_BLOCKING);
633 stp_port_state_selection(p->stp);
637 /* Sets the priority of port 'p' to 'new_priority'. Lower numerical values
638 * are interpreted as higher priorities. */
640 stp_port_set_priority(struct stp_port *p, uint8_t new_priority)
642 uint16_t new_port_id = (p->port_id & 0xff) | (new_priority << 8);
643 if (p->port_id != new_port_id) {
644 struct stp *stp = p->stp;
645 if (stp_is_designated_port(p)) {
646 p->designated_port = new_port_id;
648 p->port_id = new_port_id;
649 if (stp->bridge_id == p->designated_bridge
650 && p->port_id < p->designated_port) {
651 stp_become_designated_port(p);
652 stp_port_state_selection(stp);
657 /* Sets the path cost of port 'p' to 'path_cost'. Lower values are generally
658 * used to indicate faster links. Use stp_port_set_speed() to automatically
659 * generate a default path cost from a link speed. */
661 stp_port_set_path_cost(struct stp_port *p, uint16_t path_cost)
663 if (p->path_cost != path_cost) {
664 struct stp *stp = p->stp;
665 p->path_cost = path_cost;
666 stp_configuration_update(stp);
667 stp_port_state_selection(stp);
671 /* Sets the path cost of port 'p' based on 'speed' (measured in Mb/s). */
673 stp_port_set_speed(struct stp_port *p, unsigned int speed)
675 stp_port_set_path_cost(p, (speed >= 10000 ? 2 /* 10 Gb/s. */
676 : speed >= 1000 ? 4 /* 1 Gb/s. */
677 : speed >= 100 ? 19 /* 100 Mb/s. */
678 : speed >= 16 ? 62 /* 16 Mb/s. */
679 : speed >= 10 ? 100 /* 10 Mb/s. */
680 : speed >= 4 ? 250 /* 4 Mb/s. */
681 : 19)); /* 100 Mb/s (guess). */
684 /* Enables topology change detection on port 'p'. */
686 stp_port_enable_change_detection(struct stp_port *p)
688 p->change_detection_enabled = true;
691 /* Disables topology change detection on port 'p'. */
693 stp_port_disable_change_detection(struct stp_port *p)
695 p->change_detection_enabled = false;
699 stp_transmit_config(struct stp_port *p)
701 struct stp *stp = p->stp;
702 bool root = stp_is_root_bridge(stp);
703 if (!root && !stp->root_port) {
706 if (p->hold_timer.active) {
707 p->config_pending = true;
709 struct stp_config_bpdu config;
710 memset(&config, 0, sizeof config);
711 config.header.protocol_id = htons(STP_PROTOCOL_ID);
712 config.header.protocol_version = STP_PROTOCOL_VERSION;
713 config.header.bpdu_type = STP_TYPE_CONFIG;
715 if (p->topology_change_ack) {
716 config.flags |= htons(STP_CONFIG_TOPOLOGY_CHANGE_ACK);
718 if (stp->topology_change) {
719 config.flags |= htons(STP_CONFIG_TOPOLOGY_CHANGE);
721 config.root_id = htonll(stp->designated_root);
722 config.root_path_cost = htonl(stp->root_path_cost);
723 config.bridge_id = htonll(stp->bridge_id);
724 config.port_id = htons(p->port_id);
726 config.message_age = htons(0);
728 config.message_age = htons(stp->root_port->message_age_timer.value
729 + MESSAGE_AGE_INCREMENT);
731 config.max_age = htons(stp->max_age);
732 config.hello_time = htons(stp->hello_time);
733 config.forward_delay = htons(stp->forward_delay);
734 if (ntohs(config.message_age) < stp->max_age) {
735 p->topology_change_ack = false;
736 p->config_pending = false;
737 stp_send_bpdu(p, &config, sizeof config);
738 stp_start_timer(&p->hold_timer, 0);
744 stp_supersedes_port_info(const struct stp_port *p,
745 const struct stp_config_bpdu *config)
747 if (ntohll(config->root_id) != p->designated_root) {
748 return ntohll(config->root_id) < p->designated_root;
749 } else if (ntohl(config->root_path_cost) != p->designated_cost) {
750 return ntohl(config->root_path_cost) < p->designated_cost;
751 } else if (ntohll(config->bridge_id) != p->designated_bridge) {
752 return ntohll(config->bridge_id) < p->designated_bridge;
754 return (ntohll(config->bridge_id) != p->stp->bridge_id
755 || ntohs(config->port_id) <= p->designated_port);
760 stp_record_config_information(struct stp_port *p,
761 const struct stp_config_bpdu *config)
763 p->designated_root = ntohll(config->root_id);
764 p->designated_cost = ntohl(config->root_path_cost);
765 p->designated_bridge = ntohll(config->bridge_id);
766 p->designated_port = ntohs(config->port_id);
767 stp_start_timer(&p->message_age_timer, ntohs(config->message_age));
771 stp_record_config_timeout_values(struct stp *stp,
772 const struct stp_config_bpdu *config)
774 stp->max_age = ntohs(config->max_age);
775 stp->hello_time = ntohs(config->hello_time);
776 stp->forward_delay = ntohs(config->forward_delay);
777 stp->topology_change = config->flags & htons(STP_CONFIG_TOPOLOGY_CHANGE);
781 stp_is_designated_port(const struct stp_port *p)
783 return (p->designated_bridge == p->stp->bridge_id
784 && p->designated_port == p->port_id);
788 stp_config_bpdu_generation(struct stp *stp)
792 FOR_EACH_ENABLED_PORT (p, stp) {
793 if (stp_is_designated_port(p)) {
794 stp_transmit_config(p);
800 stp_transmit_tcn(struct stp *stp)
802 struct stp_port *p = stp->root_port;
803 struct stp_tcn_bpdu tcn_bpdu;
807 tcn_bpdu.header.protocol_id = htons(STP_PROTOCOL_ID);
808 tcn_bpdu.header.protocol_version = STP_PROTOCOL_VERSION;
809 tcn_bpdu.header.bpdu_type = STP_TYPE_TCN;
810 stp_send_bpdu(p, &tcn_bpdu, sizeof tcn_bpdu);
814 stp_configuration_update(struct stp *stp)
816 stp_root_selection(stp);
817 stp_designated_port_selection(stp);
821 stp_supersedes_root(const struct stp_port *root, const struct stp_port *p)
823 int p_cost = p->designated_cost + p->path_cost;
824 int root_cost = root->designated_cost + root->path_cost;
826 if (p->designated_root != root->designated_root) {
827 return p->designated_root < root->designated_root;
828 } else if (p_cost != root_cost) {
829 return p_cost < root_cost;
830 } else if (p->designated_bridge != root->designated_bridge) {
831 return p->designated_bridge < root->designated_bridge;
832 } else if (p->designated_port != root->designated_port) {
833 return p->designated_port < root->designated_port;
835 return p->port_id < root->port_id;
840 stp_root_selection(struct stp *stp)
842 struct stp_port *p, *root;
845 FOR_EACH_ENABLED_PORT (p, stp) {
846 if (stp_is_designated_port(p)
847 || p->designated_root >= stp->bridge_id) {
850 if (root && !stp_supersedes_root(root, p)) {
855 stp->root_port = root;
857 stp->designated_root = stp->bridge_id;
858 stp->root_path_cost = 0;
860 stp->designated_root = root->designated_root;
861 stp->root_path_cost = root->designated_cost + root->path_cost;
866 stp_designated_port_selection(struct stp *stp)
870 FOR_EACH_ENABLED_PORT (p, stp) {
871 if (stp_is_designated_port(p)
872 || p->designated_root != stp->designated_root
873 || stp->root_path_cost < p->designated_cost
874 || (stp->root_path_cost == p->designated_cost
875 && (stp->bridge_id < p->designated_bridge
876 || (stp->bridge_id == p->designated_bridge
877 && p->port_id <= p->designated_port))))
879 stp_become_designated_port(p);
885 stp_become_designated_port(struct stp_port *p)
887 struct stp *stp = p->stp;
888 p->designated_root = stp->designated_root;
889 p->designated_cost = stp->root_path_cost;
890 p->designated_bridge = stp->bridge_id;
891 p->designated_port = p->port_id;
895 stp_port_state_selection(struct stp *stp)
899 FOR_EACH_ENABLED_PORT (p, stp) {
900 if (p == stp->root_port) {
901 p->config_pending = false;
902 p->topology_change_ack = false;
903 stp_make_forwarding(p);
904 } else if (stp_is_designated_port(p)) {
905 stp_stop_timer(&p->message_age_timer);
906 stp_make_forwarding(p);
908 p->config_pending = false;
909 p->topology_change_ack = false;
910 stp_make_blocking(p);
916 stp_make_forwarding(struct stp_port *p)
918 if (p->state == STP_BLOCKING) {
919 stp_set_port_state(p, STP_LISTENING);
920 stp_start_timer(&p->forward_delay_timer, 0);
925 stp_make_blocking(struct stp_port *p)
927 if (!(p->state & (STP_DISABLED | STP_BLOCKING))) {
928 if (p->state & (STP_FORWARDING | STP_LEARNING)) {
929 if (p->change_detection_enabled) {
930 stp_topology_change_detection(p->stp);
933 stp_set_port_state(p, STP_BLOCKING);
934 stp_stop_timer(&p->forward_delay_timer);
939 stp_set_port_state(struct stp_port *p, enum stp_state state)
941 if (state != p->state && !p->state_changed) {
942 p->state_changed = true;
943 if (p < p->stp->first_changed_port) {
944 p->stp->first_changed_port = p;
951 stp_topology_change_detection(struct stp *stp)
953 if (stp_is_root_bridge(stp)) {
954 stp->topology_change = true;
955 stp_start_timer(&stp->topology_change_timer, 0);
956 } else if (!stp->topology_change_detected) {
957 stp_transmit_tcn(stp);
958 stp_start_timer(&stp->tcn_timer, 0);
960 stp->topology_change_detected = true;
964 stp_topology_change_acknowledged(struct stp *stp)
966 stp->topology_change_detected = false;
967 stp_stop_timer(&stp->tcn_timer);
971 stp_acknowledge_topology_change(struct stp_port *p)
973 p->topology_change_ack = true;
974 stp_transmit_config(p);
978 stp_received_config_bpdu(struct stp *stp, struct stp_port *p,
979 const struct stp_config_bpdu *config)
981 if (ntohs(config->message_age) >= ntohs(config->max_age)) {
982 VLOG_WARN("%s: received config BPDU with message age (%u) greater "
985 ntohs(config->message_age), ntohs(config->max_age));
988 if (p->state != STP_DISABLED) {
989 bool root = stp_is_root_bridge(stp);
990 if (stp_supersedes_port_info(p, config)) {
991 stp_record_config_information(p, config);
992 stp_configuration_update(stp);
993 stp_port_state_selection(stp);
994 if (!stp_is_root_bridge(stp) && root) {
995 stp_stop_timer(&stp->hello_timer);
996 if (stp->topology_change_detected) {
997 stp_stop_timer(&stp->topology_change_timer);
998 stp_transmit_tcn(stp);
999 stp_start_timer(&stp->tcn_timer, 0);
1002 if (p == stp->root_port) {
1003 stp_record_config_timeout_values(stp, config);
1004 stp_config_bpdu_generation(stp);
1005 if (config->flags & htons(STP_CONFIG_TOPOLOGY_CHANGE_ACK)) {
1006 stp_topology_change_acknowledged(stp);
1009 } else if (stp_is_designated_port(p)) {
1010 stp_transmit_config(p);
1016 stp_received_tcn_bpdu(struct stp *stp, struct stp_port *p)
1018 if (p->state != STP_DISABLED) {
1019 if (stp_is_designated_port(p)) {
1020 stp_topology_change_detection(stp);
1021 stp_acknowledge_topology_change(p);
1027 stp_hello_timer_expiry(struct stp *stp)
1029 stp_config_bpdu_generation(stp);
1030 stp_start_timer(&stp->hello_timer, 0);
1034 stp_message_age_timer_expiry(struct stp_port *p)
1036 struct stp *stp = p->stp;
1037 bool root = stp_is_root_bridge(stp);
1038 stp_become_designated_port(p);
1039 stp_configuration_update(stp);
1040 stp_port_state_selection(stp);
1041 if (stp_is_root_bridge(stp) && !root) {
1042 stp->max_age = stp->bridge_max_age;
1043 stp->hello_time = stp->bridge_hello_time;
1044 stp->forward_delay = stp->bridge_forward_delay;
1045 stp_topology_change_detection(stp);
1046 stp_stop_timer(&stp->tcn_timer);
1047 stp_config_bpdu_generation(stp);
1048 stp_start_timer(&stp->hello_timer, 0);
1053 stp_is_designated_for_some_port(const struct stp *stp)
1055 const struct stp_port *p;
1057 FOR_EACH_ENABLED_PORT (p, stp) {
1058 if (p->designated_bridge == stp->bridge_id) {
1066 stp_forward_delay_timer_expiry(struct stp_port *p)
1068 if (p->state == STP_LISTENING) {
1069 stp_set_port_state(p, STP_LEARNING);
1070 stp_start_timer(&p->forward_delay_timer, 0);
1071 } else if (p->state == STP_LEARNING) {
1072 stp_set_port_state(p, STP_FORWARDING);
1073 if (stp_is_designated_for_some_port(p->stp)) {
1074 if (p->change_detection_enabled) {
1075 stp_topology_change_detection(p->stp);
1082 stp_tcn_timer_expiry(struct stp *stp)
1084 stp_transmit_tcn(stp);
1085 stp_start_timer(&stp->tcn_timer, 0);
1089 stp_topology_change_timer_expiry(struct stp *stp)
1091 stp->topology_change_detected = false;
1092 stp->topology_change = false;
1096 stp_hold_timer_expiry(struct stp_port *p)
1098 if (p->config_pending) {
1099 stp_transmit_config(p);
1104 stp_initialize_port(struct stp_port *p, enum stp_state state)
1106 assert(state & (STP_DISABLED | STP_BLOCKING));
1107 stp_become_designated_port(p);
1108 stp_set_port_state(p, state);
1109 p->topology_change_ack = false;
1110 p->config_pending = false;
1111 p->change_detection_enabled = true;
1112 stp_stop_timer(&p->message_age_timer);
1113 stp_stop_timer(&p->forward_delay_timer);
1114 stp_stop_timer(&p->hold_timer);
1118 stp_become_root_bridge(struct stp *stp)
1120 stp->max_age = stp->bridge_max_age;
1121 stp->hello_time = stp->bridge_hello_time;
1122 stp->forward_delay = stp->bridge_forward_delay;
1123 stp_topology_change_detection(stp);
1124 stp_stop_timer(&stp->tcn_timer);
1125 stp_config_bpdu_generation(stp);
1126 stp_start_timer(&stp->hello_timer, 0);
1130 stp_start_timer(struct stp_timer *timer, int value)
1132 timer->value = value;
1133 timer->active = true;
1137 stp_stop_timer(struct stp_timer *timer)
1139 timer->active = false;
1143 stp_timer_expired(struct stp_timer *timer, int elapsed, int timeout)
1145 if (timer->active) {
1146 timer->value += elapsed;
1147 if (timer->value >= timeout) {
1148 timer->active = false;
1155 /* Returns the number of whole STP timer ticks in 'ms' milliseconds. There
1156 * are 256 STP timer ticks per second. */
1160 return ms * 0x100 / 1000;
1163 /* Returns the number of leftover milliseconds when 'ms' is converted to STP
1166 ms_to_timer_remainder(int ms)
1168 return ms * 0x100 % 1000;
1171 /* Returns the number of whole milliseconds in 'timer' STP timer ticks. There
1172 * are 256 STP timer ticks per second. */
1174 timer_to_ms(int timer)
1176 return timer * 1000 / 0x100;
1180 clamp(int x, int min, int max)
1182 return x < min ? min : x > max ? max : x;
1186 stp_update_bridge_timers(struct stp *stp)
1190 ht = clamp(stp->rq_hello_time, 1000, 10000);
1191 ma = clamp(stp->rq_max_age, MAX(2 * (ht + 1000), 6000), 40000);
1192 fd = clamp(stp->rq_forward_delay, ma / 2 + 1000, 30000);
1194 stp->bridge_hello_time = ms_to_timer(ht);
1195 stp->bridge_max_age = ms_to_timer(ma);
1196 stp->bridge_forward_delay = ms_to_timer(fd);
1198 if (stp_is_root_bridge(stp)) {
1199 stp->max_age = stp->bridge_max_age;
1200 stp->hello_time = stp->bridge_hello_time;
1201 stp->forward_delay = stp->bridge_forward_delay;
1206 stp_send_bpdu(struct stp_port *p, const void *bpdu, size_t bpdu_size)
1208 struct eth_header *eth;
1209 struct llc_header *llc;
1213 pkt = ofpbuf_new(ETH_HEADER_LEN + LLC_HEADER_LEN + bpdu_size);
1214 pkt->l2 = eth = ofpbuf_put_zeros(pkt, sizeof *eth);
1215 llc = ofpbuf_put_zeros(pkt, sizeof *llc);
1216 pkt->l3 = ofpbuf_put(pkt, bpdu, bpdu_size);
1219 memcpy(eth->eth_dst, stp_eth_addr, ETH_ADDR_LEN);
1220 /* p->stp->send_bpdu() must fill in source address. */
1221 eth->eth_type = htons(pkt->size - ETH_HEADER_LEN);
1224 llc->llc_dsap = STP_LLC_DSAP;
1225 llc->llc_ssap = STP_LLC_SSAP;
1226 llc->llc_cntl = STP_LLC_CNTL;
1228 p->stp->send_bpdu(pkt, stp_port_no(p), p->stp->aux);