2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* Based on sample implementation in 802.1D-1998. Above copyright and license
18 * applies to all modifications. */
23 #include <sys/types.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
28 #include "byte-order.h"
35 VLOG_DEFINE_THIS_MODULE(stp
);
37 #define STP_PROTOCOL_ID 0x0000
38 #define STP_PROTOCOL_VERSION 0x00
39 #define STP_TYPE_CONFIG 0x00
40 #define STP_TYPE_TCN 0x80
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_*. */
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
56 struct stp_config_bpdu
{
57 struct stp_bpdu_header header
; /* Type STP_TYPE_CONFIG. */
58 uint8_t flags
; /* STP_CONFIG_* flags. */
59 ovs_be64 root_id
; /* 8.5.1.1: Bridge believed to be root. */
60 ovs_be32 root_path_cost
; /* 8.5.1.2: Cost of path to root. */
61 ovs_be64 bridge_id
; /* 8.5.1.3: ID of transmitting bridge. */
62 ovs_be16 port_id
; /* 8.5.1.4: Port transmitting the BPDU. */
63 ovs_be16 message_age
; /* 8.5.1.5: Age of BPDU at tx time. */
64 ovs_be16 max_age
; /* 8.5.1.6: Timeout for received data. */
65 ovs_be16 hello_time
; /* 8.5.1.7: Time between BPDU generation. */
66 ovs_be16 forward_delay
; /* 8.5.1.8: State progression delay. */
68 BUILD_ASSERT_DECL(sizeof(struct stp_config_bpdu
) == 35);
72 struct stp_bpdu_header header
; /* Type STP_TYPE_TCN. */
74 BUILD_ASSERT_DECL(sizeof(struct stp_tcn_bpdu
) == 4);
77 bool active
; /* Timer in use? */
78 int value
; /* Current value of timer, counting up. */
83 void *aux
; /* Auxiliary data the user may retrieve. */
84 int port_id
; /* 8.5.5.1: Unique port identifier. */
85 enum stp_state state
; /* 8.5.5.2: Current state. */
86 int path_cost
; /* 8.5.5.3: Cost of tx/rx on this port. */
87 stp_identifier designated_root
; /* 8.5.5.4. */
88 int designated_cost
; /* 8.5.5.5: Path cost to root on port. */
89 stp_identifier designated_bridge
; /* 8.5.5.6. */
90 int designated_port
; /* 8.5.5.7: Port to send config msgs on. */
91 bool topology_change_ack
; /* 8.5.5.8: Flag for next config BPDU. */
92 bool config_pending
; /* 8.5.5.9: Send BPDU when hold expires? */
93 bool change_detection_enabled
; /* 8.5.5.10: Detect topology changes? */
95 struct stp_timer message_age_timer
; /* 8.5.6.1: Age of received info. */
96 struct stp_timer forward_delay_timer
; /* 8.5.6.2: State change timer. */
97 struct stp_timer hold_timer
; /* 8.5.6.3: BPDU rate limit timer. */
99 int tx_count
; /* Number of BPDUs transmitted. */
100 int rx_count
; /* Number of valid BPDUs received. */
101 int error_count
; /* Number of bad BPDUs received. */
107 struct list node
; /* Node in all_stps list. */
109 /* Static bridge data. */
110 char *name
; /* Human-readable name for log messages. */
111 stp_identifier bridge_id
; /* 8.5.3.7: This bridge. */
112 int max_age
; /* 8.5.3.4: Time to drop received data. */
113 int hello_time
; /* 8.5.3.5: Time between sending BPDUs. */
114 int forward_delay
; /* 8.5.3.6: Delay between state changes. */
115 int bridge_max_age
; /* 8.5.3.8: max_age when we're root. */
116 int bridge_hello_time
; /* 8.5.3.9: hello_time as root. */
117 int bridge_forward_delay
; /* 8.5.3.10: forward_delay as root. */
118 int rq_max_age
; /* User-requested max age, in ms. */
119 int rq_hello_time
; /* User-requested hello time, in ms. */
120 int rq_forward_delay
; /* User-requested forward delay, in ms. */
121 int elapsed_remainder
; /* Left-over msecs from last stp_tick(). */
123 /* Dynamic bridge data. */
124 stp_identifier designated_root
; /* 8.5.3.1: Bridge believed to be root. */
125 unsigned int root_path_cost
; /* 8.5.3.2: Cost of path to root. */
126 struct stp_port
*root_port
; /* 8.5.3.3: Lowest cost port to root. */
127 bool topology_change_detected
; /* 8.5.3.11: Detected a topology change? */
128 bool topology_change
; /* 8.5.3.12: Received topology change? */
131 struct stp_timer hello_timer
; /* 8.5.4.1: Hello timer. */
132 struct stp_timer tcn_timer
; /* 8.5.4.2: Topology change timer. */
133 struct stp_timer topology_change_timer
; /* 8.5.4.3. */
136 struct stp_port ports
[STP_MAX_PORTS
];
138 /* Interface to client. */
139 bool fdb_needs_flush
; /* MAC learning tables needs flushing. */
140 struct stp_port
*first_changed_port
;
141 void (*send_bpdu
)(struct ofpbuf
*bpdu
, int port_no
, void *aux
);
145 static struct list all_stps
= LIST_INITIALIZER(&all_stps
);
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 CONST_CAST(struct stp_port
*, port
);
162 #define MESSAGE_AGE_INCREMENT 1
164 static void stp_transmit_config(struct stp_port
*);
165 static bool stp_supersedes_port_info(const struct stp_port
*,
166 const struct stp_config_bpdu
*);
167 static void stp_record_config_information(struct stp_port
*,
168 const struct stp_config_bpdu
*);
169 static void stp_record_config_timeout_values(struct stp
*,
170 const struct stp_config_bpdu
*);
171 static bool stp_is_designated_port(const struct stp_port
*);
172 static void stp_config_bpdu_generation(struct stp
*);
173 static void stp_transmit_tcn(struct stp
*);
174 static void stp_configuration_update(struct stp
*);
175 static bool stp_supersedes_root(const struct stp_port
*root
,
176 const struct stp_port
*);
177 static void stp_root_selection(struct stp
*);
178 static void stp_designated_port_selection(struct stp
*);
179 static void stp_become_designated_port(struct stp_port
*);
180 static void stp_port_state_selection(struct stp
*);
181 static void stp_make_forwarding(struct stp_port
*);
182 static void stp_make_blocking(struct stp_port
*);
183 static void stp_set_port_state(struct stp_port
*, enum stp_state
);
184 static void stp_topology_change_detection(struct stp
*);
185 static void stp_topology_change_acknowledged(struct stp
*);
186 static void stp_acknowledge_topology_change(struct stp_port
*);
187 static void stp_received_config_bpdu(struct stp
*, struct stp_port
*,
188 const struct stp_config_bpdu
*);
189 static void stp_received_tcn_bpdu(struct stp
*, struct stp_port
*);
190 static void stp_hello_timer_expiry(struct stp
*);
191 static void stp_message_age_timer_expiry(struct stp_port
*);
192 static bool stp_is_designated_for_some_port(const struct stp
*);
193 static void stp_forward_delay_timer_expiry(struct stp_port
*);
194 static void stp_tcn_timer_expiry(struct stp
*);
195 static void stp_topology_change_timer_expiry(struct stp
*);
196 static void stp_hold_timer_expiry(struct stp_port
*);
197 static void stp_initialize_port(struct stp_port
*, enum stp_state
);
198 static void stp_become_root_bridge(struct stp
*);
199 static void stp_update_bridge_timers(struct stp
*);
201 static int clamp(int x
, int min
, int max
);
202 static int ms_to_timer(int ms
);
203 static int timer_to_ms(int timer
);
204 static void stp_start_timer(struct stp_timer
*, int value
);
205 static void stp_stop_timer(struct stp_timer
*);
206 static bool stp_timer_expired(struct stp_timer
*, int elapsed
, int timeout
);
208 static void stp_send_bpdu(struct stp_port
*, const void *, size_t);
209 static void stp_unixctl_tcn(struct unixctl_conn
*, int argc
,
210 const char *argv
[], void *aux
);
215 unixctl_command_register("stp/tcn", "[bridge]", 0, 1, stp_unixctl_tcn
,
219 /* Creates and returns a new STP instance that initially has no ports enabled.
221 * 'bridge_id' should be a 48-bit MAC address as returned by
222 * eth_addr_to_uint64(). 'bridge_id' may also have a priority value in its top
223 * 16 bits; if those bits are set to 0, STP_DEFAULT_BRIDGE_PRIORITY is used.
224 * (This priority may be changed with stp_set_bridge_priority().)
226 * When the bridge needs to send out a BPDU, it calls 'send_bpdu'. This
227 * callback may be called from stp_tick() or stp_received_bpdu(). The
228 * arguments to 'send_bpdu' are an STP BPDU encapsulated in 'bpdu',
229 * the spanning tree port number 'port_no' that should transmit the
230 * packet, and auxiliary data to be passed to the callback in 'aux'.
233 stp_create(const char *name
, stp_identifier bridge_id
,
234 void (*send_bpdu
)(struct ofpbuf
*bpdu
, int port_no
, void *aux
),
240 stp
= xzalloc(sizeof *stp
);
241 stp
->name
= xstrdup(name
);
242 stp
->bridge_id
= bridge_id
;
243 if (!(stp
->bridge_id
>> 48)) {
244 stp
->bridge_id
|= (uint64_t) STP_DEFAULT_BRIDGE_PRIORITY
<< 48;
247 stp
->rq_max_age
= STP_DEFAULT_MAX_AGE
;
248 stp
->rq_hello_time
= STP_DEFAULT_HELLO_TIME
;
249 stp
->rq_forward_delay
= STP_DEFAULT_FWD_DELAY
;
250 stp_update_bridge_timers(stp
);
251 stp
->max_age
= stp
->bridge_max_age
;
252 stp
->hello_time
= stp
->bridge_hello_time
;
253 stp
->forward_delay
= stp
->bridge_forward_delay
;
255 stp
->designated_root
= stp
->bridge_id
;
256 stp
->root_path_cost
= 0;
257 stp
->root_port
= NULL
;
258 stp
->topology_change_detected
= false;
259 stp
->topology_change
= false;
261 stp_stop_timer(&stp
->tcn_timer
);
262 stp_stop_timer(&stp
->topology_change_timer
);
263 stp_start_timer(&stp
->hello_timer
, 0);
265 stp
->send_bpdu
= send_bpdu
;
268 stp
->first_changed_port
= &stp
->ports
[ARRAY_SIZE(stp
->ports
)];
269 for (p
= stp
->ports
; p
< &stp
->ports
[ARRAY_SIZE(stp
->ports
)]; p
++) {
271 p
->port_id
= (stp_port_no(p
) + 1) | (STP_DEFAULT_PORT_PRIORITY
<< 8);
272 p
->path_cost
= 19; /* Recommended default for 100 Mb/s link. */
273 stp_initialize_port(p
, STP_DISABLED
);
275 list_push_back(&all_stps
, &stp
->node
);
279 /* Destroys 'stp'. */
281 stp_destroy(struct stp
*stp
)
284 list_remove(&stp
->node
);
290 /* Runs 'stp' given that 'ms' milliseconds have passed. */
292 stp_tick(struct stp
*stp
, int ms
)
297 /* Convert 'ms' to STP timer ticks. Preserve any leftover milliseconds
298 * from previous stp_tick() calls so that we don't lose STP ticks when we
299 * are called too frequently. */
300 ms
= clamp(ms
, 0, INT_MAX
- 1000) + stp
->elapsed_remainder
;
301 elapsed
= ms_to_timer(ms
);
302 stp
->elapsed_remainder
= ms
- timer_to_ms(elapsed
);
307 if (stp_timer_expired(&stp
->hello_timer
, elapsed
, stp
->hello_time
)) {
308 stp_hello_timer_expiry(stp
);
310 if (stp_timer_expired(&stp
->tcn_timer
, elapsed
, stp
->bridge_hello_time
)) {
311 stp_tcn_timer_expiry(stp
);
313 if (stp_timer_expired(&stp
->topology_change_timer
, elapsed
,
314 stp
->max_age
+ stp
->forward_delay
)) {
315 stp_topology_change_timer_expiry(stp
);
317 FOR_EACH_ENABLED_PORT (p
, stp
) {
318 if (stp_timer_expired(&p
->message_age_timer
, elapsed
, stp
->max_age
)) {
319 stp_message_age_timer_expiry(p
);
322 FOR_EACH_ENABLED_PORT (p
, stp
) {
323 if (stp_timer_expired(&p
->forward_delay_timer
, elapsed
,
324 stp
->forward_delay
)) {
325 stp_forward_delay_timer_expiry(p
);
327 if (stp_timer_expired(&p
->hold_timer
, elapsed
, ms_to_timer(1000))) {
328 stp_hold_timer_expiry(p
);
334 set_bridge_id(struct stp
*stp
, stp_identifier new_bridge_id
)
336 if (new_bridge_id
!= stp
->bridge_id
) {
340 root
= stp_is_root_bridge(stp
);
341 FOR_EACH_ENABLED_PORT (p
, stp
) {
342 if (stp_is_designated_port(p
)) {
343 p
->designated_bridge
= new_bridge_id
;
346 stp
->bridge_id
= new_bridge_id
;
347 stp_configuration_update(stp
);
348 stp_port_state_selection(stp
);
349 if (stp_is_root_bridge(stp
) && !root
) {
350 stp_become_root_bridge(stp
);
356 stp_set_bridge_id(struct stp
*stp
, stp_identifier bridge_id
)
358 const uint64_t mac_bits
= (UINT64_C(1) << 48) - 1;
359 const uint64_t pri_bits
= ~mac_bits
;
360 set_bridge_id(stp
, (stp
->bridge_id
& pri_bits
) | (bridge_id
& mac_bits
));
364 stp_set_bridge_priority(struct stp
*stp
, uint16_t new_priority
)
366 const uint64_t mac_bits
= (UINT64_C(1) << 48) - 1;
367 set_bridge_id(stp
, ((stp
->bridge_id
& mac_bits
)
368 | ((uint64_t) new_priority
<< 48)));
371 /* Sets the desired hello time for 'stp' to 'ms', in milliseconds. The actual
372 * hello time is clamped to the range of 1 to 10 seconds and subject to the
373 * relationship (bridge_max_age >= 2 * (bridge_hello_time + 1 s)). The bridge
374 * hello time is only used when 'stp' is the root bridge. */
376 stp_set_hello_time(struct stp
*stp
, int ms
)
378 stp
->rq_hello_time
= ms
;
379 stp_update_bridge_timers(stp
);
382 /* Sets the desired max age for 'stp' to 'ms', in milliseconds. The actual max
383 * age is clamped to the range of 6 to 40 seconds and subject to the
384 * relationships (2 * (bridge_forward_delay - 1 s) >= bridge_max_age) and
385 * (bridge_max_age >= 2 * (bridge_hello_time + 1 s)). The bridge max age is
386 * only used when 'stp' is the root bridge. */
388 stp_set_max_age(struct stp
*stp
, int ms
)
390 stp
->rq_max_age
= ms
;
391 stp_update_bridge_timers(stp
);
394 /* Sets the desired forward delay for 'stp' to 'ms', in milliseconds. The
395 * actual forward delay is clamped to the range of 4 to 30 seconds and subject
396 * to the relationship (2 * (bridge_forward_delay - 1 s) >= bridge_max_age).
397 * The bridge forward delay is only used when 'stp' is the root bridge. */
399 stp_set_forward_delay(struct stp
*stp
, int ms
)
401 stp
->rq_forward_delay
= ms
;
402 stp_update_bridge_timers(stp
);
405 /* Returns the name given to 'stp' in the call to stp_create(). */
407 stp_get_name(const struct stp
*stp
)
412 /* Returns the bridge ID for 'stp'. */
414 stp_get_bridge_id(const struct stp
*stp
)
416 return stp
->bridge_id
;
419 /* Returns the bridge ID of the bridge currently believed to be the root. */
421 stp_get_designated_root(const struct stp
*stp
)
423 return stp
->designated_root
;
426 /* Returns true if 'stp' believes itself to the be root of the spanning tree,
427 * false otherwise. */
429 stp_is_root_bridge(const struct stp
*stp
)
431 return stp
->bridge_id
== stp
->designated_root
;
434 /* Returns the cost of the path from 'stp' to the root of the spanning tree. */
436 stp_get_root_path_cost(const struct stp
*stp
)
438 return stp
->root_path_cost
;
441 /* Returns the bridge hello time, in ms. The returned value is not necessarily
442 * the value passed to stp_set_hello_time(): it is clamped to the valid range
443 * and quantized to the STP timer resolution. */
445 stp_get_hello_time(const struct stp
*stp
)
447 return timer_to_ms(stp
->bridge_hello_time
);
450 /* Returns the bridge max age, in ms. The returned value is not necessarily
451 * the value passed to stp_set_max_age(): it is clamped to the valid range,
452 * quantized to the STP timer resolution, and adjusted to match the constraints
453 * due to the hello time. */
455 stp_get_max_age(const struct stp
*stp
)
457 return timer_to_ms(stp
->bridge_max_age
);
460 /* Returns the bridge forward delay, in ms. The returned value is not
461 * necessarily the value passed to stp_set_forward_delay(): it is clamped to
462 * the valid range, quantized to the STP timer resolution, and adjusted to
463 * match the constraints due to the forward delay. */
465 stp_get_forward_delay(const struct stp
*stp
)
467 return timer_to_ms(stp
->bridge_forward_delay
);
470 /* Returns true if something has happened to 'stp' which necessitates flushing
471 * the client's MAC learning table. Calling this function resets 'stp' so that
472 * future calls will return false until flushing is required again. */
474 stp_check_and_reset_fdb_flush(struct stp
*stp
)
476 bool needs_flush
= stp
->fdb_needs_flush
;
477 stp
->fdb_needs_flush
= false;
481 /* Returns the port in 'stp' with index 'port_no', which must be between 0 and
484 stp_get_port(struct stp
*stp
, int port_no
)
486 ovs_assert(port_no
>= 0 && port_no
< ARRAY_SIZE(stp
->ports
));
487 return &stp
->ports
[port_no
];
490 /* Returns the port connecting 'stp' to the root bridge, or a null pointer if
491 * there is no such port. */
493 stp_get_root_port(struct stp
*stp
)
495 return stp
->root_port
;
498 /* Finds a port whose state has changed. If successful, stores the port whose
499 * state changed in '*portp' and returns true. If no port has changed, stores
500 * NULL in '*portp' and returns false. */
502 stp_get_changed_port(struct stp
*stp
, struct stp_port
**portp
)
504 struct stp_port
*end
= &stp
->ports
[ARRAY_SIZE(stp
->ports
)];
507 for (p
= stp
->first_changed_port
; p
< end
; p
++) {
508 if (p
->state_changed
) {
509 p
->state_changed
= false;
510 stp
->first_changed_port
= p
+ 1;
515 stp
->first_changed_port
= end
;
520 /* Returns the name for the given 'state' (for use in debugging and log
523 stp_state_name(enum stp_state state
)
541 /* Returns true if 'state' is one in which packets received on a port should
542 * be forwarded, false otherwise.
544 * Returns true if 'state' is STP_DISABLED, since presumably in that case the
545 * port should still work, just not have STP applied to it. */
547 stp_forward_in_state(enum stp_state state
)
549 return (state
& (STP_DISABLED
| STP_FORWARDING
)) != 0;
552 /* Returns true if 'state' is one in which MAC learning should be done on
553 * packets received on a port, false otherwise.
555 * Returns true if 'state' is STP_DISABLED, since presumably in that case the
556 * port should still work, just not have STP applied to it. */
558 stp_learn_in_state(enum stp_state state
)
560 return (state
& (STP_DISABLED
| STP_LEARNING
| STP_FORWARDING
)) != 0;
563 /* Returns the name for the given 'role' (for use in debugging and log
566 stp_role_name(enum stp_role role
)
571 case STP_ROLE_DESIGNATED
:
573 case STP_ROLE_ALTERNATE
:
575 case STP_ROLE_DISABLED
:
582 /* Notifies the STP entity that bridge protocol data unit 'bpdu', which is
583 * 'bpdu_size' bytes in length, was received on port 'p'.
585 * This function may call the 'send_bpdu' function provided to stp_create(). */
587 stp_received_bpdu(struct stp_port
*p
, const void *bpdu
, size_t bpdu_size
)
589 struct stp
*stp
= p
->stp
;
590 const struct stp_bpdu_header
*header
;
592 if (p
->state
== STP_DISABLED
) {
596 if (bpdu_size
< sizeof(struct stp_bpdu_header
)) {
597 VLOG_WARN("%s: received runt %zu-byte BPDU", stp
->name
, bpdu_size
);
603 if (header
->protocol_id
!= htons(STP_PROTOCOL_ID
)) {
604 VLOG_WARN("%s: received BPDU with unexpected protocol ID %"PRIu16
,
605 stp
->name
, ntohs(header
->protocol_id
));
609 if (header
->protocol_version
!= STP_PROTOCOL_VERSION
) {
610 VLOG_DBG("%s: received BPDU with unexpected protocol version %"PRIu8
,
611 stp
->name
, header
->protocol_version
);
614 switch (header
->bpdu_type
) {
615 case STP_TYPE_CONFIG
:
616 if (bpdu_size
< sizeof(struct stp_config_bpdu
)) {
617 VLOG_WARN("%s: received config BPDU with invalid size %zu",
618 stp
->name
, bpdu_size
);
622 stp_received_config_bpdu(stp
, p
, bpdu
);
626 if (bpdu_size
!= sizeof(struct stp_tcn_bpdu
)) {
627 VLOG_WARN("%s: received TCN BPDU with invalid size %zu",
628 stp
->name
, bpdu_size
);
632 stp_received_tcn_bpdu(stp
, p
);
636 VLOG_WARN("%s: received BPDU of unexpected type %"PRIu8
,
637 stp
->name
, header
->bpdu_type
);
644 /* Returns the STP entity in which 'p' is nested. */
646 stp_port_get_stp(struct stp_port
*p
)
651 /* Sets the 'aux' member of 'p'.
653 * The 'aux' member will be reset to NULL when stp_port_disable() is
654 * called or stp_port_enable() is called when the port is in a Disabled
657 stp_port_set_aux(struct stp_port
*p
, void *aux
)
662 /* Returns the 'aux' member of 'p'. */
664 stp_port_get_aux(struct stp_port
*p
)
669 /* Returns the index of port 'p' within its bridge. */
671 stp_port_no(const struct stp_port
*p
)
673 struct stp
*stp
= p
->stp
;
674 ovs_assert(p
>= stp
->ports
&& p
< &stp
->ports
[ARRAY_SIZE(stp
->ports
)]);
675 return p
- stp
->ports
;
678 /* Returns the port ID for 'p'. */
680 stp_port_get_id(const struct stp_port
*p
)
685 /* Returns the state of port 'p'. */
687 stp_port_get_state(const struct stp_port
*p
)
692 /* Returns the role of port 'p'. */
694 stp_port_get_role(const struct stp_port
*p
)
696 struct stp_port
*root_port
= stp_get_root_port(p
->stp
);
698 if (root_port
&& root_port
->port_id
== p
->port_id
) {
699 return STP_ROLE_ROOT
;
700 } else if (stp_is_designated_port(p
)) {
701 return STP_ROLE_DESIGNATED
;
702 } else if (p
->state
== STP_DISABLED
) {
703 return STP_ROLE_DISABLED
;
705 return STP_ROLE_ALTERNATE
;
709 /* Retrieves BPDU transmit and receive counts for 'p'. */
710 void stp_port_get_counts(const struct stp_port
*p
,
711 int *tx_count
, int *rx_count
, int *error_count
)
713 *tx_count
= p
->tx_count
;
714 *rx_count
= p
->rx_count
;
715 *error_count
= p
->error_count
;
718 /* Disables STP on port 'p'. */
720 stp_port_disable(struct stp_port
*p
)
722 struct stp
*stp
= p
->stp
;
723 if (p
->state
!= STP_DISABLED
) {
724 bool root
= stp_is_root_bridge(stp
);
725 stp_become_designated_port(p
);
726 stp_set_port_state(p
, STP_DISABLED
);
727 p
->topology_change_ack
= false;
728 p
->config_pending
= false;
729 stp_stop_timer(&p
->message_age_timer
);
730 stp_stop_timer(&p
->forward_delay_timer
);
731 stp_configuration_update(stp
);
732 stp_port_state_selection(stp
);
733 if (stp_is_root_bridge(stp
) && !root
) {
734 stp_become_root_bridge(stp
);
740 /* Enables STP on port 'p'. The port will initially be in "blocking" state. */
742 stp_port_enable(struct stp_port
*p
)
744 if (p
->state
== STP_DISABLED
) {
745 stp_initialize_port(p
, STP_BLOCKING
);
746 stp_port_state_selection(p
->stp
);
750 /* Sets the priority of port 'p' to 'new_priority'. Lower numerical values
751 * are interpreted as higher priorities. */
753 stp_port_set_priority(struct stp_port
*p
, uint8_t new_priority
)
755 uint16_t new_port_id
= (p
->port_id
& 0xff) | (new_priority
<< 8);
756 if (p
->port_id
!= new_port_id
) {
757 struct stp
*stp
= p
->stp
;
758 if (stp_is_designated_port(p
)) {
759 p
->designated_port
= new_port_id
;
761 p
->port_id
= new_port_id
;
762 if (stp
->bridge_id
== p
->designated_bridge
763 && p
->port_id
< p
->designated_port
) {
764 stp_become_designated_port(p
);
765 stp_port_state_selection(stp
);
770 /* Convert 'speed' (measured in Mb/s) into the path cost. */
772 stp_convert_speed_to_cost(unsigned int speed
)
774 return speed
>= 10000 ? 2 /* 10 Gb/s. */
775 : speed
>= 1000 ? 4 /* 1 Gb/s. */
776 : speed
>= 100 ? 19 /* 100 Mb/s. */
777 : speed
>= 16 ? 62 /* 16 Mb/s. */
778 : speed
>= 10 ? 100 /* 10 Mb/s. */
779 : speed
>= 4 ? 250 /* 4 Mb/s. */
780 : 19; /* 100 Mb/s (guess). */
783 /* Sets the path cost of port 'p' to 'path_cost'. Lower values are generally
784 * used to indicate faster links. Use stp_port_set_speed() to automatically
785 * generate a default path cost from a link speed. */
787 stp_port_set_path_cost(struct stp_port
*p
, uint16_t path_cost
)
789 if (p
->path_cost
!= path_cost
) {
790 struct stp
*stp
= p
->stp
;
791 p
->path_cost
= path_cost
;
792 stp_configuration_update(stp
);
793 stp_port_state_selection(stp
);
797 /* Sets the path cost of port 'p' based on 'speed' (measured in Mb/s). */
799 stp_port_set_speed(struct stp_port
*p
, unsigned int speed
)
801 stp_port_set_path_cost(p
, stp_convert_speed_to_cost(speed
));
804 /* Enables topology change detection on port 'p'. */
806 stp_port_enable_change_detection(struct stp_port
*p
)
808 p
->change_detection_enabled
= true;
811 /* Disables topology change detection on port 'p'. */
813 stp_port_disable_change_detection(struct stp_port
*p
)
815 p
->change_detection_enabled
= false;
819 stp_transmit_config(struct stp_port
*p
)
821 struct stp
*stp
= p
->stp
;
822 bool root
= stp_is_root_bridge(stp
);
823 if (!root
&& !stp
->root_port
) {
826 if (p
->hold_timer
.active
) {
827 p
->config_pending
= true;
829 struct stp_config_bpdu config
;
830 memset(&config
, 0, sizeof config
);
831 config
.header
.protocol_id
= htons(STP_PROTOCOL_ID
);
832 config
.header
.protocol_version
= STP_PROTOCOL_VERSION
;
833 config
.header
.bpdu_type
= STP_TYPE_CONFIG
;
835 if (p
->topology_change_ack
) {
836 config
.flags
|= STP_CONFIG_TOPOLOGY_CHANGE_ACK
;
838 if (stp
->topology_change
) {
839 config
.flags
|= STP_CONFIG_TOPOLOGY_CHANGE
;
841 config
.root_id
= htonll(stp
->designated_root
);
842 config
.root_path_cost
= htonl(stp
->root_path_cost
);
843 config
.bridge_id
= htonll(stp
->bridge_id
);
844 config
.port_id
= htons(p
->port_id
);
846 config
.message_age
= htons(0);
848 config
.message_age
= htons(stp
->root_port
->message_age_timer
.value
849 + MESSAGE_AGE_INCREMENT
);
851 config
.max_age
= htons(stp
->max_age
);
852 config
.hello_time
= htons(stp
->hello_time
);
853 config
.forward_delay
= htons(stp
->forward_delay
);
854 if (ntohs(config
.message_age
) < stp
->max_age
) {
855 p
->topology_change_ack
= false;
856 p
->config_pending
= false;
857 stp_send_bpdu(p
, &config
, sizeof config
);
858 stp_start_timer(&p
->hold_timer
, 0);
864 stp_supersedes_port_info(const struct stp_port
*p
,
865 const struct stp_config_bpdu
*config
)
867 if (ntohll(config
->root_id
) != p
->designated_root
) {
868 return ntohll(config
->root_id
) < p
->designated_root
;
869 } else if (ntohl(config
->root_path_cost
) != p
->designated_cost
) {
870 return ntohl(config
->root_path_cost
) < p
->designated_cost
;
871 } else if (ntohll(config
->bridge_id
) != p
->designated_bridge
) {
872 return ntohll(config
->bridge_id
) < p
->designated_bridge
;
874 return (ntohll(config
->bridge_id
) != p
->stp
->bridge_id
875 || ntohs(config
->port_id
) <= p
->designated_port
);
880 stp_record_config_information(struct stp_port
*p
,
881 const struct stp_config_bpdu
*config
)
883 p
->designated_root
= ntohll(config
->root_id
);
884 p
->designated_cost
= ntohl(config
->root_path_cost
);
885 p
->designated_bridge
= ntohll(config
->bridge_id
);
886 p
->designated_port
= ntohs(config
->port_id
);
887 stp_start_timer(&p
->message_age_timer
, ntohs(config
->message_age
));
891 stp_record_config_timeout_values(struct stp
*stp
,
892 const struct stp_config_bpdu
*config
)
894 stp
->max_age
= ntohs(config
->max_age
);
895 stp
->hello_time
= ntohs(config
->hello_time
);
896 stp
->forward_delay
= ntohs(config
->forward_delay
);
897 stp
->topology_change
= config
->flags
& STP_CONFIG_TOPOLOGY_CHANGE
;
901 stp_is_designated_port(const struct stp_port
*p
)
903 return (p
->designated_bridge
== p
->stp
->bridge_id
904 && p
->designated_port
== p
->port_id
);
908 stp_config_bpdu_generation(struct stp
*stp
)
912 FOR_EACH_ENABLED_PORT (p
, stp
) {
913 if (stp_is_designated_port(p
)) {
914 stp_transmit_config(p
);
920 stp_transmit_tcn(struct stp
*stp
)
922 struct stp_port
*p
= stp
->root_port
;
923 struct stp_tcn_bpdu tcn_bpdu
;
927 tcn_bpdu
.header
.protocol_id
= htons(STP_PROTOCOL_ID
);
928 tcn_bpdu
.header
.protocol_version
= STP_PROTOCOL_VERSION
;
929 tcn_bpdu
.header
.bpdu_type
= STP_TYPE_TCN
;
930 stp_send_bpdu(p
, &tcn_bpdu
, sizeof tcn_bpdu
);
934 stp_configuration_update(struct stp
*stp
)
936 stp_root_selection(stp
);
937 stp_designated_port_selection(stp
);
941 stp_supersedes_root(const struct stp_port
*root
, const struct stp_port
*p
)
943 int p_cost
= p
->designated_cost
+ p
->path_cost
;
944 int root_cost
= root
->designated_cost
+ root
->path_cost
;
946 if (p
->designated_root
!= root
->designated_root
) {
947 return p
->designated_root
< root
->designated_root
;
948 } else if (p_cost
!= root_cost
) {
949 return p_cost
< root_cost
;
950 } else if (p
->designated_bridge
!= root
->designated_bridge
) {
951 return p
->designated_bridge
< root
->designated_bridge
;
952 } else if (p
->designated_port
!= root
->designated_port
) {
953 return p
->designated_port
< root
->designated_port
;
955 return p
->port_id
< root
->port_id
;
960 stp_root_selection(struct stp
*stp
)
962 struct stp_port
*p
, *root
;
965 FOR_EACH_ENABLED_PORT (p
, stp
) {
966 if (stp_is_designated_port(p
)
967 || p
->designated_root
>= stp
->bridge_id
) {
970 if (root
&& !stp_supersedes_root(root
, p
)) {
975 stp
->root_port
= root
;
977 stp
->designated_root
= stp
->bridge_id
;
978 stp
->root_path_cost
= 0;
980 stp
->designated_root
= root
->designated_root
;
981 stp
->root_path_cost
= root
->designated_cost
+ root
->path_cost
;
986 stp_designated_port_selection(struct stp
*stp
)
990 FOR_EACH_ENABLED_PORT (p
, stp
) {
991 if (stp_is_designated_port(p
)
992 || p
->designated_root
!= stp
->designated_root
993 || stp
->root_path_cost
< p
->designated_cost
994 || (stp
->root_path_cost
== p
->designated_cost
995 && (stp
->bridge_id
< p
->designated_bridge
996 || (stp
->bridge_id
== p
->designated_bridge
997 && p
->port_id
<= p
->designated_port
))))
999 stp_become_designated_port(p
);
1005 stp_become_designated_port(struct stp_port
*p
)
1007 struct stp
*stp
= p
->stp
;
1008 p
->designated_root
= stp
->designated_root
;
1009 p
->designated_cost
= stp
->root_path_cost
;
1010 p
->designated_bridge
= stp
->bridge_id
;
1011 p
->designated_port
= p
->port_id
;
1015 stp_port_state_selection(struct stp
*stp
)
1019 FOR_EACH_ENABLED_PORT (p
, stp
) {
1020 if (p
== stp
->root_port
) {
1021 p
->config_pending
= false;
1022 p
->topology_change_ack
= false;
1023 stp_make_forwarding(p
);
1024 } else if (stp_is_designated_port(p
)) {
1025 stp_stop_timer(&p
->message_age_timer
);
1026 stp_make_forwarding(p
);
1028 p
->config_pending
= false;
1029 p
->topology_change_ack
= false;
1030 stp_make_blocking(p
);
1036 stp_make_forwarding(struct stp_port
*p
)
1038 if (p
->state
== STP_BLOCKING
) {
1039 stp_set_port_state(p
, STP_LISTENING
);
1040 stp_start_timer(&p
->forward_delay_timer
, 0);
1045 stp_make_blocking(struct stp_port
*p
)
1047 if (!(p
->state
& (STP_DISABLED
| STP_BLOCKING
))) {
1048 if (p
->state
& (STP_FORWARDING
| STP_LEARNING
)) {
1049 if (p
->change_detection_enabled
) {
1050 stp_topology_change_detection(p
->stp
);
1053 stp_set_port_state(p
, STP_BLOCKING
);
1054 stp_stop_timer(&p
->forward_delay_timer
);
1059 stp_set_port_state(struct stp_port
*p
, enum stp_state state
)
1061 if (state
!= p
->state
&& !p
->state_changed
) {
1062 p
->state_changed
= true;
1063 if (p
< p
->stp
->first_changed_port
) {
1064 p
->stp
->first_changed_port
= p
;
1071 stp_topology_change_detection(struct stp
*stp
)
1073 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1075 if (stp_is_root_bridge(stp
)) {
1076 stp
->topology_change
= true;
1077 stp_start_timer(&stp
->topology_change_timer
, 0);
1078 } else if (!stp
->topology_change_detected
) {
1079 stp_transmit_tcn(stp
);
1080 stp_start_timer(&stp
->tcn_timer
, 0);
1082 stp
->fdb_needs_flush
= true;
1083 stp
->topology_change_detected
= true;
1084 VLOG_INFO_RL(&rl
, "%s: detected topology change.", stp
->name
);
1088 stp_topology_change_acknowledged(struct stp
*stp
)
1090 stp
->topology_change_detected
= false;
1091 stp_stop_timer(&stp
->tcn_timer
);
1095 stp_acknowledge_topology_change(struct stp_port
*p
)
1097 p
->topology_change_ack
= true;
1098 stp_transmit_config(p
);
1102 stp_received_config_bpdu(struct stp
*stp
, struct stp_port
*p
,
1103 const struct stp_config_bpdu
*config
)
1105 if (ntohs(config
->message_age
) >= ntohs(config
->max_age
)) {
1106 VLOG_WARN("%s: received config BPDU with message age (%u) greater "
1107 "than max age (%u)",
1109 ntohs(config
->message_age
), ntohs(config
->max_age
));
1112 if (p
->state
!= STP_DISABLED
) {
1113 bool root
= stp_is_root_bridge(stp
);
1114 if (stp_supersedes_port_info(p
, config
)) {
1115 stp_record_config_information(p
, config
);
1116 stp_configuration_update(stp
);
1117 stp_port_state_selection(stp
);
1118 if (!stp_is_root_bridge(stp
) && root
) {
1119 stp_stop_timer(&stp
->hello_timer
);
1120 if (stp
->topology_change_detected
) {
1121 stp_stop_timer(&stp
->topology_change_timer
);
1122 stp_transmit_tcn(stp
);
1123 stp_start_timer(&stp
->tcn_timer
, 0);
1126 if (p
== stp
->root_port
) {
1127 stp_record_config_timeout_values(stp
, config
);
1128 stp_config_bpdu_generation(stp
);
1129 if (config
->flags
& STP_CONFIG_TOPOLOGY_CHANGE_ACK
) {
1130 stp_topology_change_acknowledged(stp
);
1132 if (config
->flags
& STP_CONFIG_TOPOLOGY_CHANGE
) {
1133 stp
->fdb_needs_flush
= true;
1136 } else if (stp_is_designated_port(p
)) {
1137 stp_transmit_config(p
);
1143 stp_received_tcn_bpdu(struct stp
*stp
, struct stp_port
*p
)
1145 if (p
->state
!= STP_DISABLED
) {
1146 if (stp_is_designated_port(p
)) {
1147 stp_topology_change_detection(stp
);
1148 stp_acknowledge_topology_change(p
);
1154 stp_hello_timer_expiry(struct stp
*stp
)
1156 stp_config_bpdu_generation(stp
);
1157 stp_start_timer(&stp
->hello_timer
, 0);
1161 stp_message_age_timer_expiry(struct stp_port
*p
)
1163 struct stp
*stp
= p
->stp
;
1164 bool root
= stp_is_root_bridge(stp
);
1165 stp_become_designated_port(p
);
1166 stp_configuration_update(stp
);
1167 stp_port_state_selection(stp
);
1168 if (stp_is_root_bridge(stp
) && !root
) {
1169 stp
->max_age
= stp
->bridge_max_age
;
1170 stp
->hello_time
= stp
->bridge_hello_time
;
1171 stp
->forward_delay
= stp
->bridge_forward_delay
;
1172 stp_topology_change_detection(stp
);
1173 stp_stop_timer(&stp
->tcn_timer
);
1174 stp_config_bpdu_generation(stp
);
1175 stp_start_timer(&stp
->hello_timer
, 0);
1180 stp_is_designated_for_some_port(const struct stp
*stp
)
1182 const struct stp_port
*p
;
1184 FOR_EACH_ENABLED_PORT (p
, stp
) {
1185 if (p
->designated_bridge
== stp
->bridge_id
) {
1193 stp_forward_delay_timer_expiry(struct stp_port
*p
)
1195 if (p
->state
== STP_LISTENING
) {
1196 stp_set_port_state(p
, STP_LEARNING
);
1197 stp_start_timer(&p
->forward_delay_timer
, 0);
1198 } else if (p
->state
== STP_LEARNING
) {
1199 stp_set_port_state(p
, STP_FORWARDING
);
1200 if (stp_is_designated_for_some_port(p
->stp
)) {
1201 if (p
->change_detection_enabled
) {
1202 stp_topology_change_detection(p
->stp
);
1209 stp_tcn_timer_expiry(struct stp
*stp
)
1211 stp_transmit_tcn(stp
);
1212 stp_start_timer(&stp
->tcn_timer
, 0);
1216 stp_topology_change_timer_expiry(struct stp
*stp
)
1218 stp
->topology_change_detected
= false;
1219 stp
->topology_change
= false;
1223 stp_hold_timer_expiry(struct stp_port
*p
)
1225 if (p
->config_pending
) {
1226 stp_transmit_config(p
);
1231 stp_initialize_port(struct stp_port
*p
, enum stp_state state
)
1233 ovs_assert(state
& (STP_DISABLED
| STP_BLOCKING
));
1234 stp_become_designated_port(p
);
1235 stp_set_port_state(p
, state
);
1236 p
->topology_change_ack
= false;
1237 p
->config_pending
= false;
1238 p
->change_detection_enabled
= true;
1240 stp_stop_timer(&p
->message_age_timer
);
1241 stp_stop_timer(&p
->forward_delay_timer
);
1242 stp_stop_timer(&p
->hold_timer
);
1243 p
->tx_count
= p
->rx_count
= p
->error_count
= 0;
1247 stp_become_root_bridge(struct stp
*stp
)
1249 stp
->max_age
= stp
->bridge_max_age
;
1250 stp
->hello_time
= stp
->bridge_hello_time
;
1251 stp
->forward_delay
= stp
->bridge_forward_delay
;
1252 stp_topology_change_detection(stp
);
1253 stp_stop_timer(&stp
->tcn_timer
);
1254 stp_config_bpdu_generation(stp
);
1255 stp_start_timer(&stp
->hello_timer
, 0);
1259 stp_start_timer(struct stp_timer
*timer
, int value
)
1261 timer
->value
= value
;
1262 timer
->active
= true;
1266 stp_stop_timer(struct stp_timer
*timer
)
1268 timer
->active
= false;
1272 stp_timer_expired(struct stp_timer
*timer
, int elapsed
, int timeout
)
1274 if (timer
->active
) {
1275 timer
->value
+= elapsed
;
1276 if (timer
->value
>= timeout
) {
1277 timer
->active
= false;
1284 /* Returns the number of whole STP timer ticks in 'ms' milliseconds. There
1285 * are 256 STP timer ticks per second. */
1289 return ms
* 0x100 / 1000;
1292 /* Returns the number of whole milliseconds in 'timer' STP timer ticks. There
1293 * are 256 STP timer ticks per second. */
1295 timer_to_ms(int timer
)
1297 return timer
* 1000 / 0x100;
1301 clamp(int x
, int min
, int max
)
1303 return x
< min
? min
: x
> max
? max
: x
;
1307 stp_update_bridge_timers(struct stp
*stp
)
1311 ht
= clamp(stp
->rq_hello_time
, 1000, 10000);
1312 ma
= clamp(stp
->rq_max_age
, MAX(2 * (ht
+ 1000), 6000), 40000);
1313 fd
= clamp(stp
->rq_forward_delay
, ma
/ 2 + 1000, 30000);
1315 stp
->bridge_hello_time
= ms_to_timer(ht
);
1316 stp
->bridge_max_age
= ms_to_timer(ma
);
1317 stp
->bridge_forward_delay
= ms_to_timer(fd
);
1319 if (stp_is_root_bridge(stp
)) {
1320 stp
->max_age
= stp
->bridge_max_age
;
1321 stp
->hello_time
= stp
->bridge_hello_time
;
1322 stp
->forward_delay
= stp
->bridge_forward_delay
;
1327 stp_send_bpdu(struct stp_port
*p
, const void *bpdu
, size_t bpdu_size
)
1329 struct eth_header
*eth
;
1330 struct llc_header
*llc
;
1334 pkt
= ofpbuf_new(ETH_HEADER_LEN
+ LLC_HEADER_LEN
+ bpdu_size
);
1335 pkt
->l2
= eth
= ofpbuf_put_zeros(pkt
, sizeof *eth
);
1336 llc
= ofpbuf_put_zeros(pkt
, sizeof *llc
);
1337 pkt
->l3
= ofpbuf_put(pkt
, bpdu
, bpdu_size
);
1340 memcpy(eth
->eth_dst
, eth_addr_stp
, ETH_ADDR_LEN
);
1341 /* p->stp->send_bpdu() must fill in source address. */
1342 eth
->eth_type
= htons(pkt
->size
- ETH_HEADER_LEN
);
1345 llc
->llc_dsap
= STP_LLC_DSAP
;
1346 llc
->llc_ssap
= STP_LLC_SSAP
;
1347 llc
->llc_cntl
= STP_LLC_CNTL
;
1349 p
->stp
->send_bpdu(pkt
, stp_port_no(p
), p
->stp
->aux
);
1356 stp_find(const char *name
)
1360 LIST_FOR_EACH (stp
, node
, &all_stps
) {
1361 if (!strcmp(stp
->name
, name
)) {
1369 stp_unixctl_tcn(struct unixctl_conn
*conn
, int argc
,
1370 const char *argv
[], void *aux OVS_UNUSED
)
1373 struct stp
*stp
= stp_find(argv
[1]);
1376 unixctl_command_reply_error(conn
, "no such stp object");
1379 stp_topology_change_detection(stp
);
1383 LIST_FOR_EACH (stp
, node
, &all_stps
) {
1384 stp_topology_change_detection(stp
);
1388 unixctl_command_reply(conn
, "OK");