2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2016 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"
29 #include "connectivity.h"
30 #include "openvswitch/ofpbuf.h"
31 #include "ovs-atomic.h"
32 #include "dp-packet.h"
37 #include "openvswitch/vlog.h"
39 VLOG_DEFINE_THIS_MODULE(stp
);
41 static struct vlog_rate_limit stp_rl
= VLOG_RATE_LIMIT_INIT(60, 60);
43 #define STP_PROTOCOL_ID 0x0000
44 #define STP_PROTOCOL_VERSION 0x00
45 #define STP_TYPE_CONFIG 0x00
46 #define STP_TYPE_TCN 0x80
49 struct stp_bpdu_header
{
50 ovs_be16 protocol_id
; /* STP_PROTOCOL_ID. */
51 uint8_t protocol_version
; /* STP_PROTOCOL_VERSION. */
52 uint8_t bpdu_type
; /* One of STP_TYPE_*. */
54 BUILD_ASSERT_DECL(sizeof(struct stp_bpdu_header
) == 4);
56 enum stp_config_bpdu_flags
{
57 STP_CONFIG_TOPOLOGY_CHANGE_ACK
= 0x80,
58 STP_CONFIG_TOPOLOGY_CHANGE
= 0x01
62 struct stp_config_bpdu
{
63 struct stp_bpdu_header header
; /* Type STP_TYPE_CONFIG. */
64 uint8_t flags
; /* STP_CONFIG_* flags. */
65 ovs_be64 root_id
; /* 8.5.1.1: Bridge believed to be root. */
66 ovs_be32 root_path_cost
; /* 8.5.1.2: Cost of path to root. */
67 ovs_be64 bridge_id
; /* 8.5.1.3: ID of transmitting bridge. */
68 ovs_be16 port_id
; /* 8.5.1.4: Port transmitting the BPDU. */
69 ovs_be16 message_age
; /* 8.5.1.5: Age of BPDU at tx time. */
70 ovs_be16 max_age
; /* 8.5.1.6: Timeout for received data. */
71 ovs_be16 hello_time
; /* 8.5.1.7: Time between BPDU generation. */
72 ovs_be16 forward_delay
; /* 8.5.1.8: State progression delay. */
74 BUILD_ASSERT_DECL(sizeof(struct stp_config_bpdu
) == 35);
78 struct stp_bpdu_header header
; /* Type STP_TYPE_TCN. */
80 BUILD_ASSERT_DECL(sizeof(struct stp_tcn_bpdu
) == 4);
83 bool active
; /* Timer in use? */
84 int value
; /* Current value of timer, counting up. */
89 char *port_name
; /* Human-readable name for log messages. */
90 void *aux
; /* Auxiliary data the user may retrieve. */
91 int port_id
; /* 8.5.5.1: Unique port identifier. */
92 enum stp_state state
; /* 8.5.5.2: Current state. */
93 int path_cost
; /* 8.5.5.3: Cost of tx/rx on this port. */
94 stp_identifier designated_root
; /* 8.5.5.4. */
95 int designated_cost
; /* 8.5.5.5: Path cost to root on port. */
96 stp_identifier designated_bridge
; /* 8.5.5.6. */
97 int designated_port
; /* 8.5.5.7: Port to send config msgs on. */
98 bool topology_change_ack
; /* 8.5.5.8: Flag for next config BPDU. */
99 bool config_pending
; /* 8.5.5.9: Send BPDU when hold expires? */
100 bool change_detection_enabled
; /* 8.5.5.10: Detect topology changes? */
102 struct stp_timer message_age_timer
; /* 8.5.6.1: Age of received info. */
103 struct stp_timer forward_delay_timer
; /* 8.5.6.2: State change timer. */
104 struct stp_timer hold_timer
; /* 8.5.6.3: BPDU rate limit timer. */
106 int tx_count
; /* Number of BPDUs transmitted. */
107 int rx_count
; /* Number of valid BPDUs received. */
108 int error_count
; /* Number of bad BPDUs received. */
114 struct ovs_list node
; /* Node in all_stps list. */
116 /* Static bridge data. */
117 char *name
; /* Human-readable name for log messages. */
118 stp_identifier bridge_id
; /* 8.5.3.7: This bridge. */
119 int max_age
; /* 8.5.3.4: Time to drop received data. */
120 int hello_time
; /* 8.5.3.5: Time between sending BPDUs. */
121 int forward_delay
; /* 8.5.3.6: Delay between state changes. */
122 int bridge_max_age
; /* 8.5.3.8: max_age when we're root. */
123 int bridge_hello_time
; /* 8.5.3.9: hello_time as root. */
124 int bridge_forward_delay
; /* 8.5.3.10: forward_delay as root. */
125 int rq_max_age
; /* User-requested max age, in ms. */
126 int rq_hello_time
; /* User-requested hello time, in ms. */
127 int rq_forward_delay
; /* User-requested forward delay, in ms. */
128 int elapsed_remainder
; /* Left-over msecs from last stp_tick(). */
130 /* Dynamic bridge data. */
131 stp_identifier designated_root
; /* 8.5.3.1: Bridge believed to be root. */
132 unsigned int root_path_cost
; /* 8.5.3.2: Cost of path to root. */
133 struct stp_port
*root_port
; /* 8.5.3.3: Lowest cost port to root. */
134 bool topology_change_detected
; /* 8.5.3.11: Detected a topology change? */
135 bool topology_change
; /* 8.5.3.12: Received topology change? */
138 struct stp_timer hello_timer
; /* 8.5.4.1: Hello timer. */
139 struct stp_timer tcn_timer
; /* 8.5.4.2: Topology change timer. */
140 struct stp_timer topology_change_timer
; /* 8.5.4.3. */
143 struct stp_port ports
[STP_MAX_PORTS
];
145 /* Interface to client. */
146 bool fdb_needs_flush
; /* MAC learning tables needs flushing. */
147 struct stp_port
*first_changed_port
;
148 void (*send_bpdu
)(struct dp_packet
*bpdu
, int port_no
, void *aux
);
151 struct ovs_refcount ref_cnt
;
154 static struct ovs_mutex mutex
;
155 static struct ovs_list all_stps__
= OVS_LIST_INITIALIZER(&all_stps__
);
156 static struct ovs_list
*const all_stps
OVS_GUARDED_BY(mutex
) = &all_stps__
;
158 #define FOR_EACH_ENABLED_PORT(PORT, STP) \
159 for ((PORT) = stp_next_enabled_port((STP), (STP)->ports); \
161 (PORT) = stp_next_enabled_port((STP), (PORT) + 1))
162 static struct stp_port
*
163 stp_next_enabled_port(const struct stp
*stp
, const struct stp_port
*port
)
166 for (; port
< &stp
->ports
[ARRAY_SIZE(stp
->ports
)]; port
++) {
167 if (port
->state
!= STP_DISABLED
) {
168 return CONST_CAST(struct stp_port
*, port
);
174 #define MESSAGE_AGE_INCREMENT 1
176 static void stp_transmit_config(struct stp_port
*) OVS_REQUIRES(mutex
);
177 static bool stp_supersedes_port_info(const struct stp_port
*,
178 const struct stp_config_bpdu
*)
180 static void stp_record_config_information(struct stp_port
*,
181 const struct stp_config_bpdu
*)
183 static void stp_record_config_timeout_values(struct stp
*,
184 const struct stp_config_bpdu
*)
186 static bool stp_is_designated_port(const struct stp_port
*)
188 static void stp_config_bpdu_generation(struct stp
*) OVS_REQUIRES(mutex
);
189 static void stp_transmit_tcn(struct stp
*) OVS_REQUIRES(mutex
);
190 static void stp_configuration_update(struct stp
*) OVS_REQUIRES(mutex
);
191 static bool stp_supersedes_root(const struct stp_port
*root
,
192 const struct stp_port
*) OVS_REQUIRES(mutex
);
193 static void stp_root_selection(struct stp
*) OVS_REQUIRES(mutex
);
194 static void stp_designated_port_selection(struct stp
*) OVS_REQUIRES(mutex
);
195 static void stp_become_designated_port(struct stp_port
*)
197 static void stp_port_state_selection(struct stp
*) OVS_REQUIRES(mutex
);
198 static void stp_make_forwarding(struct stp_port
*) OVS_REQUIRES(mutex
);
199 static void stp_make_blocking(struct stp_port
*) OVS_REQUIRES(mutex
);
200 static void stp_set_port_state(struct stp_port
*, enum stp_state
)
202 static void stp_topology_change_detection(struct stp
*) OVS_REQUIRES(mutex
);
203 static void stp_topology_change_acknowledged(struct stp
*)
205 static void stp_acknowledge_topology_change(struct stp_port
*)
207 static void stp_received_config_bpdu(struct stp
*, struct stp_port
*,
208 const struct stp_config_bpdu
*)
210 static void stp_received_tcn_bpdu(struct stp
*, struct stp_port
*)
212 static void stp_hello_timer_expiry(struct stp
*) OVS_REQUIRES(mutex
);
213 static void stp_message_age_timer_expiry(struct stp_port
*)
215 static bool stp_is_designated_for_some_port(const struct stp
*)
217 static void stp_forward_delay_timer_expiry(struct stp_port
*)
219 static void stp_tcn_timer_expiry(struct stp
*) OVS_REQUIRES(mutex
);
220 static void stp_topology_change_timer_expiry(struct stp
*)
222 static void stp_hold_timer_expiry(struct stp_port
*) OVS_REQUIRES(mutex
);
223 static void stp_initialize_port(struct stp_port
*, enum stp_state
)
225 static void stp_become_root_bridge(struct stp
*) OVS_REQUIRES(mutex
);
226 static void stp_update_bridge_timers(struct stp
*) OVS_REQUIRES(mutex
);
228 static int clamp(int x
, int min
, int max
);
229 static int ms_to_timer(int ms
);
230 static int timer_to_ms(int timer
);
231 static void stp_start_timer(struct stp_timer
*, int value
);
232 static void stp_stop_timer(struct stp_timer
*);
233 static bool stp_timer_expired(struct stp_timer
*, int elapsed
, int timeout
);
235 static void stp_send_bpdu(struct stp_port
*, const void *, size_t)
237 static void stp_unixctl_tcn(struct unixctl_conn
*, int argc
,
238 const char *argv
[], void *aux
);
243 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
245 if (ovsthread_once_start(&once
)) {
246 /* We need a recursive mutex because stp_send_bpdu() could loop back
247 * into the stp module through a patch port. This happens
248 * intentionally as part of the unit tests. Ideally we'd ditch
249 * the call back function, but for now this is what we have. */
250 ovs_mutex_init_recursive(&mutex
);
252 unixctl_command_register("stp/tcn", "[bridge]", 0, 1, stp_unixctl_tcn
,
254 ovsthread_once_done(&once
);
258 /* Creates and returns a new STP instance that initially has no ports enabled.
260 * 'bridge_id' should be a 48-bit MAC address as returned by
261 * eth_addr_to_uint64(). 'bridge_id' may also have a priority value in its top
262 * 16 bits; if those bits are set to 0, STP_DEFAULT_BRIDGE_PRIORITY is used.
263 * (This priority may be changed with stp_set_bridge_priority().)
265 * When the bridge needs to send out a BPDU, it calls 'send_bpdu'. This
266 * callback may be called from stp_tick() or stp_received_bpdu(). The
267 * arguments to 'send_bpdu' are an STP BPDU encapsulated in 'bpdu',
268 * the spanning tree port number 'port_no' that should transmit the
269 * packet, and auxiliary data to be passed to the callback in 'aux'.
272 stp_create(const char *name
, stp_identifier bridge_id
,
273 void (*send_bpdu
)(struct dp_packet
*bpdu
, int port_no
, void *aux
),
281 ovs_mutex_lock(&mutex
);
282 stp
= xzalloc(sizeof *stp
);
283 stp
->name
= xstrdup(name
);
284 stp
->bridge_id
= bridge_id
;
285 if (!(stp
->bridge_id
>> 48)) {
286 stp
->bridge_id
|= (uint64_t) STP_DEFAULT_BRIDGE_PRIORITY
<< 48;
289 stp
->rq_max_age
= STP_DEFAULT_MAX_AGE
;
290 stp
->rq_hello_time
= STP_DEFAULT_HELLO_TIME
;
291 stp
->rq_forward_delay
= STP_DEFAULT_FWD_DELAY
;
292 stp_update_bridge_timers(stp
);
293 stp
->max_age
= stp
->bridge_max_age
;
294 stp
->hello_time
= stp
->bridge_hello_time
;
295 stp
->forward_delay
= stp
->bridge_forward_delay
;
297 stp
->designated_root
= stp
->bridge_id
;
298 stp
->root_path_cost
= 0;
299 stp
->root_port
= NULL
;
300 stp
->topology_change_detected
= false;
301 stp
->topology_change
= false;
303 stp_stop_timer(&stp
->tcn_timer
);
304 stp_stop_timer(&stp
->topology_change_timer
);
305 stp_start_timer(&stp
->hello_timer
, 0);
307 stp
->send_bpdu
= send_bpdu
;
310 stp
->first_changed_port
= &stp
->ports
[ARRAY_SIZE(stp
->ports
)];
311 for (p
= stp
->ports
; p
< &stp
->ports
[ARRAY_SIZE(stp
->ports
)]; p
++) {
313 p
->port_id
= (stp_port_no(p
) + 1) | (STP_DEFAULT_PORT_PRIORITY
<< 8);
314 p
->path_cost
= 19; /* Recommended default for 100 Mb/s link. */
315 stp_initialize_port(p
, STP_DISABLED
);
317 ovs_refcount_init(&stp
->ref_cnt
);
319 ovs_list_push_back(all_stps
, &stp
->node
);
320 ovs_mutex_unlock(&mutex
);
325 stp_ref(const struct stp
*stp_
)
327 struct stp
*stp
= CONST_CAST(struct stp
*, stp_
);
329 ovs_refcount_ref(&stp
->ref_cnt
);
334 /* Destroys 'stp'. */
336 stp_unref(struct stp
*stp
)
338 if (stp
&& ovs_refcount_unref_relaxed(&stp
->ref_cnt
) == 1) {
341 ovs_mutex_lock(&mutex
);
342 ovs_list_remove(&stp
->node
);
343 ovs_mutex_unlock(&mutex
);
346 for (i
= 0; i
< STP_MAX_PORTS
; i
++) {
347 free(stp
->ports
[i
].port_name
);
353 /* Runs 'stp' given that 'ms' milliseconds have passed. */
355 stp_tick(struct stp
*stp
, int ms
)
360 ovs_mutex_lock(&mutex
);
361 /* Convert 'ms' to STP timer ticks. Preserve any leftover milliseconds
362 * from previous stp_tick() calls so that we don't lose STP ticks when we
363 * are called too frequently. */
364 ms
= clamp(ms
, 0, INT_MAX
- 1000) + stp
->elapsed_remainder
;
365 elapsed
= ms_to_timer(ms
);
366 stp
->elapsed_remainder
= ms
- timer_to_ms(elapsed
);
371 if (stp_timer_expired(&stp
->hello_timer
, elapsed
, stp
->hello_time
)) {
372 stp_hello_timer_expiry(stp
);
374 if (stp_timer_expired(&stp
->tcn_timer
, elapsed
, stp
->bridge_hello_time
)) {
375 stp_tcn_timer_expiry(stp
);
377 if (stp_timer_expired(&stp
->topology_change_timer
, elapsed
,
378 stp
->max_age
+ stp
->forward_delay
)) {
379 stp_topology_change_timer_expiry(stp
);
381 FOR_EACH_ENABLED_PORT (p
, stp
) {
382 if (stp_timer_expired(&p
->message_age_timer
, elapsed
, stp
->max_age
)) {
383 stp_message_age_timer_expiry(p
);
386 FOR_EACH_ENABLED_PORT (p
, stp
) {
387 if (stp_timer_expired(&p
->forward_delay_timer
, elapsed
,
388 stp
->forward_delay
)) {
389 stp_forward_delay_timer_expiry(p
);
391 if (stp_timer_expired(&p
->hold_timer
, elapsed
, ms_to_timer(1000))) {
392 stp_hold_timer_expiry(p
);
397 ovs_mutex_unlock(&mutex
);
401 set_bridge_id(struct stp
*stp
, stp_identifier new_bridge_id
)
404 if (new_bridge_id
!= stp
->bridge_id
) {
408 root
= stp_is_root_bridge(stp
);
409 FOR_EACH_ENABLED_PORT (p
, stp
) {
410 if (stp_is_designated_port(p
)) {
411 p
->designated_bridge
= new_bridge_id
;
414 stp
->bridge_id
= new_bridge_id
;
415 stp_configuration_update(stp
);
416 stp_port_state_selection(stp
);
417 if (stp_is_root_bridge(stp
) && !root
) {
418 stp_become_root_bridge(stp
);
424 stp_set_bridge_id(struct stp
*stp
, stp_identifier bridge_id
)
426 const uint64_t mac_bits
= (UINT64_C(1) << 48) - 1;
427 const uint64_t pri_bits
= ~mac_bits
;
428 ovs_mutex_lock(&mutex
);
429 set_bridge_id(stp
, (stp
->bridge_id
& pri_bits
) | (bridge_id
& mac_bits
));
430 ovs_mutex_unlock(&mutex
);
434 stp_set_bridge_priority(struct stp
*stp
, uint16_t new_priority
)
436 const uint64_t mac_bits
= (UINT64_C(1) << 48) - 1;
437 ovs_mutex_lock(&mutex
);
438 set_bridge_id(stp
, ((stp
->bridge_id
& mac_bits
)
439 | ((uint64_t) new_priority
<< 48)));
440 ovs_mutex_unlock(&mutex
);
443 /* Sets the desired hello time for 'stp' to 'ms', in milliseconds. The actual
444 * hello time is clamped to the range of 1 to 10 seconds and subject to the
445 * relationship (bridge_max_age >= 2 * (bridge_hello_time + 1 s)). The bridge
446 * hello time is only used when 'stp' is the root bridge. */
448 stp_set_hello_time(struct stp
*stp
, int ms
)
450 ovs_mutex_lock(&mutex
);
451 stp
->rq_hello_time
= ms
;
452 stp_update_bridge_timers(stp
);
453 ovs_mutex_unlock(&mutex
);
456 /* Sets the desired max age for 'stp' to 'ms', in milliseconds. The actual max
457 * age is clamped to the range of 6 to 40 seconds and subject to the
458 * relationships (2 * (bridge_forward_delay - 1 s) >= bridge_max_age) and
459 * (bridge_max_age >= 2 * (bridge_hello_time + 1 s)). The bridge max age is
460 * only used when 'stp' is the root bridge. */
462 stp_set_max_age(struct stp
*stp
, int ms
)
464 ovs_mutex_lock(&mutex
);
465 stp
->rq_max_age
= ms
;
466 stp_update_bridge_timers(stp
);
467 ovs_mutex_unlock(&mutex
);
470 /* Sets the desired forward delay for 'stp' to 'ms', in milliseconds. The
471 * actual forward delay is clamped to the range of 4 to 30 seconds and subject
472 * to the relationship (2 * (bridge_forward_delay - 1 s) >= bridge_max_age).
473 * The bridge forward delay is only used when 'stp' is the root bridge. */
475 stp_set_forward_delay(struct stp
*stp
, int ms
)
477 ovs_mutex_lock(&mutex
);
478 stp
->rq_forward_delay
= ms
;
479 stp_update_bridge_timers(stp
);
480 ovs_mutex_unlock(&mutex
);
483 /* Returns the name given to 'stp' in the call to stp_create(). */
485 stp_get_name(const struct stp
*stp
)
489 ovs_mutex_lock(&mutex
);
491 ovs_mutex_unlock(&mutex
);
495 /* Returns the bridge ID for 'stp'. */
497 stp_get_bridge_id(const struct stp
*stp
)
499 stp_identifier bridge_id
;
501 ovs_mutex_lock(&mutex
);
502 bridge_id
= stp
->bridge_id
;
503 ovs_mutex_unlock(&mutex
);
507 /* Returns the bridge ID of the bridge currently believed to be the root. */
509 stp_get_designated_root(const struct stp
*stp
)
511 stp_identifier designated_root
;
513 ovs_mutex_lock(&mutex
);
514 designated_root
= stp
->designated_root
;
515 ovs_mutex_unlock(&mutex
);
516 return designated_root
;
519 /* Returns true if 'stp' believes itself to the be root of the spanning tree,
520 * false otherwise. */
522 stp_is_root_bridge(const struct stp
*stp
)
526 ovs_mutex_lock(&mutex
);
527 is_root
= stp
->bridge_id
== stp
->designated_root
;
528 ovs_mutex_unlock(&mutex
);
532 /* Returns the cost of the path from 'stp' to the root of the spanning tree. */
534 stp_get_root_path_cost(const struct stp
*stp
)
538 ovs_mutex_lock(&mutex
);
539 cost
= stp
->root_path_cost
;
540 ovs_mutex_unlock(&mutex
);
544 /* Returns the bridge hello time, in ms. The returned value is not necessarily
545 * the value passed to stp_set_hello_time(): it is clamped to the valid range
546 * and quantized to the STP timer resolution. */
548 stp_get_hello_time(const struct stp
*stp
)
552 ovs_mutex_lock(&mutex
);
553 time
= timer_to_ms(stp
->bridge_hello_time
);
554 ovs_mutex_unlock(&mutex
);
558 /* Returns the bridge max age, in ms. The returned value is not necessarily
559 * the value passed to stp_set_max_age(): it is clamped to the valid range,
560 * quantized to the STP timer resolution, and adjusted to match the constraints
561 * due to the hello time. */
563 stp_get_max_age(const struct stp
*stp
)
567 ovs_mutex_lock(&mutex
);
568 time
= timer_to_ms(stp
->bridge_max_age
);
569 ovs_mutex_unlock(&mutex
);
573 /* Returns the bridge forward delay, in ms. The returned value is not
574 * necessarily the value passed to stp_set_forward_delay(): it is clamped to
575 * the valid range, quantized to the STP timer resolution, and adjusted to
576 * match the constraints due to the forward delay. */
578 stp_get_forward_delay(const struct stp
*stp
)
582 ovs_mutex_lock(&mutex
);
583 time
= timer_to_ms(stp
->bridge_forward_delay
);
584 ovs_mutex_unlock(&mutex
);
588 /* Returns true if something has happened to 'stp' which necessitates flushing
589 * the client's MAC learning table. Calling this function resets 'stp' so that
590 * future calls will return false until flushing is required again. */
592 stp_check_and_reset_fdb_flush(struct stp
*stp
)
596 ovs_mutex_lock(&mutex
);
597 needs_flush
= stp
->fdb_needs_flush
;
598 stp
->fdb_needs_flush
= false;
599 ovs_mutex_unlock(&mutex
);
603 /* Returns the port in 'stp' with index 'port_no', which must be between 0 and
606 stp_get_port(struct stp
*stp
, int port_no
)
608 struct stp_port
*port
;
610 ovs_mutex_lock(&mutex
);
611 ovs_assert(port_no
>= 0 && port_no
< ARRAY_SIZE(stp
->ports
));
612 port
= &stp
->ports
[port_no
];
613 ovs_mutex_unlock(&mutex
);
617 /* Returns the port connecting 'stp' to the root bridge, or a null pointer if
618 * there is no such port. */
620 stp_get_root_port(struct stp
*stp
)
622 struct stp_port
*port
;
624 ovs_mutex_lock(&mutex
);
625 port
= stp
->root_port
;
626 ovs_mutex_unlock(&mutex
);
630 /* Finds a port whose state has changed. If successful, stores the port whose
631 * state changed in '*portp' and returns true. If no port has changed, stores
632 * NULL in '*portp' and returns false. */
634 stp_get_changed_port(struct stp
*stp
, struct stp_port
**portp
)
636 struct stp_port
*end
, *p
;
637 bool changed
= false;
639 ovs_mutex_lock(&mutex
);
640 end
= &stp
->ports
[ARRAY_SIZE(stp
->ports
)];
641 for (p
= stp
->first_changed_port
; p
< end
; p
++) {
642 if (p
->state_changed
) {
643 p
->state_changed
= false;
644 stp
->first_changed_port
= p
+ 1;
650 stp
->first_changed_port
= end
;
654 ovs_mutex_unlock(&mutex
);
658 /* Returns the name for the given 'state' (for use in debugging and log
661 stp_state_name(enum stp_state state
)
679 /* Returns true if 'state' is one in which packets received on a port should
680 * be forwarded, false otherwise.
683 stp_forward_in_state(enum stp_state state
)
685 return (state
& STP_FORWARDING
) != 0;
688 /* Returns true if 'state' is one in which MAC learning should be done on
689 * packets received on a port, false otherwise.
692 stp_learn_in_state(enum stp_state state
)
694 return (state
& (STP_LEARNING
| STP_FORWARDING
)) != 0;
697 /* Returns true if 'state' is one in which bpdus should be forwarded on a
698 * port, false otherwise.
700 * Returns true if 'state' is STP_DISABLED, since in that case the port does
701 * not generate the bpdu and should just forward it (e.g. patch port on pif
704 stp_should_forward_bpdu(enum stp_state state
)
707 ( STP_DISABLED
| STP_LISTENING
| STP_LEARNING
708 | STP_FORWARDING
)) != 0;
711 /* Returns the name for the given 'role' (for use in debugging and log
714 stp_role_name(enum stp_role role
)
719 case STP_ROLE_DESIGNATED
:
721 case STP_ROLE_ALTERNATE
:
723 case STP_ROLE_DISABLED
:
730 /* Notifies the STP entity that bridge protocol data unit 'bpdu', which is
731 * 'bpdu_size' bytes in length, was received on port 'p'.
733 * This function may call the 'send_bpdu' function provided to stp_create(). */
735 stp_received_bpdu(struct stp_port
*p
, const void *bpdu
, size_t bpdu_size
)
737 struct stp
*stp
= p
->stp
;
738 const struct stp_bpdu_header
*header
;
740 ovs_mutex_lock(&mutex
);
741 if (p
->state
== STP_DISABLED
) {
745 if (bpdu_size
< sizeof(struct stp_bpdu_header
)) {
746 VLOG_WARN("%s: received runt %"PRIuSIZE
"-byte BPDU", stp
->name
, bpdu_size
);
752 if (header
->protocol_id
!= htons(STP_PROTOCOL_ID
)) {
753 VLOG_WARN("%s: received BPDU with unexpected protocol ID %"PRIu16
,
754 stp
->name
, ntohs(header
->protocol_id
));
758 if (header
->protocol_version
!= STP_PROTOCOL_VERSION
) {
759 VLOG_DBG("%s: received BPDU with unexpected protocol version %"PRIu8
,
760 stp
->name
, header
->protocol_version
);
763 switch (header
->bpdu_type
) {
764 case STP_TYPE_CONFIG
:
765 if (bpdu_size
< sizeof(struct stp_config_bpdu
)) {
766 VLOG_WARN("%s: received config BPDU with invalid size %"PRIuSIZE
,
767 stp
->name
, bpdu_size
);
771 stp_received_config_bpdu(stp
, p
, bpdu
);
775 if (bpdu_size
!= sizeof(struct stp_tcn_bpdu
)) {
776 VLOG_WARN("%s: received TCN BPDU with invalid size %"PRIuSIZE
,
777 stp
->name
, bpdu_size
);
781 stp_received_tcn_bpdu(stp
, p
);
785 VLOG_WARN("%s: received BPDU of unexpected type %"PRIu8
,
786 stp
->name
, header
->bpdu_type
);
793 ovs_mutex_unlock(&mutex
);
796 /* Returns the STP entity in which 'p' is nested. */
798 stp_port_get_stp(struct stp_port
*p
)
802 ovs_mutex_lock(&mutex
);
804 ovs_mutex_unlock(&mutex
);
809 stp_port_set_name(struct stp_port
*p
, const char *name
)
813 ovs_mutex_lock(&mutex
);
815 p
->port_name
= xstrdup(name
);
817 ovs_mutex_unlock(&mutex
);
820 /* Sets the 'aux' member of 'p'.
822 * The 'aux' member will be reset to NULL when stp_port_disable() is
823 * called or stp_port_enable() is called when the port is in a Disabled
826 stp_port_set_aux(struct stp_port
*p
, void *aux
)
828 ovs_mutex_lock(&mutex
);
830 ovs_mutex_unlock(&mutex
);
833 /* Returns the 'aux' member of 'p'. */
835 stp_port_get_aux(struct stp_port
*p
)
839 ovs_mutex_lock(&mutex
);
841 ovs_mutex_unlock(&mutex
);
845 /* Returns the index of port 'p' within its bridge. */
847 stp_port_no(const struct stp_port
*p
)
852 ovs_mutex_lock(&mutex
);
854 ovs_assert(p
>= stp
->ports
&& p
< &stp
->ports
[ARRAY_SIZE(stp
->ports
)]);
855 index
= p
- p
->stp
->ports
;
856 ovs_mutex_unlock(&mutex
);
860 /* Returns the port ID for 'p'. */
862 stp_port_get_id(const struct stp_port
*p
)
866 ovs_mutex_lock(&mutex
);
867 port_id
= p
->port_id
;
868 ovs_mutex_unlock(&mutex
);
872 /* Returns the state of port 'p'. */
874 stp_port_get_state(const struct stp_port
*p
)
876 enum stp_state state
;
878 ovs_mutex_lock(&mutex
);
880 ovs_mutex_unlock(&mutex
);
884 /* Returns the role of port 'p'. */
886 stp_port_get_role(const struct stp_port
*p
)
888 struct stp_port
*root_port
;
891 ovs_mutex_lock(&mutex
);
892 root_port
= p
->stp
->root_port
;
893 if (root_port
&& root_port
->port_id
== p
->port_id
) {
894 role
= STP_ROLE_ROOT
;
895 } else if (stp_is_designated_port(p
)) {
896 role
= STP_ROLE_DESIGNATED
;
897 } else if (p
->state
== STP_DISABLED
) {
898 role
= STP_ROLE_DISABLED
;
900 role
= STP_ROLE_ALTERNATE
;
902 ovs_mutex_unlock(&mutex
);
906 /* Retrieves BPDU transmit and receive counts for 'p'. */
908 stp_port_get_counts(const struct stp_port
*p
,
909 int *tx_count
, int *rx_count
, int *error_count
)
911 ovs_mutex_lock(&mutex
);
912 *tx_count
= p
->tx_count
;
913 *rx_count
= p
->rx_count
;
914 *error_count
= p
->error_count
;
915 ovs_mutex_unlock(&mutex
);
918 /* Disables STP on port 'p'. */
920 stp_port_disable(struct stp_port
*p
)
924 ovs_mutex_lock(&mutex
);
926 if (p
->state
!= STP_DISABLED
) {
927 bool root
= stp_is_root_bridge(stp
);
928 stp_become_designated_port(p
);
929 stp_set_port_state(p
, STP_DISABLED
);
930 p
->topology_change_ack
= false;
931 p
->config_pending
= false;
932 stp_stop_timer(&p
->message_age_timer
);
933 stp_stop_timer(&p
->forward_delay_timer
);
934 stp_configuration_update(stp
);
935 stp_port_state_selection(stp
);
936 if (stp_is_root_bridge(stp
) && !root
) {
937 stp_become_root_bridge(stp
);
941 ovs_mutex_unlock(&mutex
);
944 /* Enables STP on port 'p'. The port will initially be in "blocking" state. */
946 stp_port_enable(struct stp_port
*p
)
948 ovs_mutex_lock(&mutex
);
949 if (p
->state
== STP_DISABLED
) {
950 stp_initialize_port(p
, STP_BLOCKING
);
951 stp_port_state_selection(p
->stp
);
953 ovs_mutex_unlock(&mutex
);
956 /* Sets the priority of port 'p' to 'new_priority'. Lower numerical values
957 * are interpreted as higher priorities. */
959 stp_port_set_priority(struct stp_port
*p
, uint8_t new_priority
)
961 uint16_t new_port_id
;
963 ovs_mutex_lock(&mutex
);
964 new_port_id
= (p
->port_id
& 0xff) | (new_priority
<< 8);
965 if (p
->port_id
!= new_port_id
) {
966 struct stp
*stp
= p
->stp
;
967 if (stp_is_designated_port(p
)) {
968 p
->designated_port
= new_port_id
;
970 p
->port_id
= new_port_id
;
971 if (stp
->bridge_id
== p
->designated_bridge
972 && p
->port_id
< p
->designated_port
) {
973 stp_become_designated_port(p
);
974 stp_port_state_selection(stp
);
977 ovs_mutex_unlock(&mutex
);
980 /* Convert 'speed' (measured in Mb/s) into the path cost. */
982 stp_convert_speed_to_cost(unsigned int speed
)
986 ovs_mutex_lock(&mutex
);
987 ret
= speed
>= 10000 ? 2 /* 10 Gb/s. */
988 : speed
>= 1000 ? 4 /* 1 Gb/s. */
989 : speed
>= 100 ? 19 /* 100 Mb/s. */
990 : speed
>= 16 ? 62 /* 16 Mb/s. */
991 : speed
>= 10 ? 100 /* 10 Mb/s. */
992 : speed
>= 4 ? 250 /* 4 Mb/s. */
993 : 19; /* 100 Mb/s (guess). */
994 ovs_mutex_unlock(&mutex
);
998 /* Sets the path cost of port 'p' to 'path_cost'. Lower values are generally
999 * used to indicate faster links. Use stp_port_set_speed() to automatically
1000 * generate a default path cost from a link speed. */
1002 stp_port_set_path_cost(struct stp_port
*p
, uint16_t path_cost
)
1004 ovs_mutex_lock(&mutex
);
1005 if (p
->path_cost
!= path_cost
) {
1006 struct stp
*stp
= p
->stp
;
1007 p
->path_cost
= path_cost
;
1008 stp_configuration_update(stp
);
1009 stp_port_state_selection(stp
);
1011 ovs_mutex_unlock(&mutex
);
1014 /* Sets the path cost of port 'p' based on 'speed' (measured in Mb/s). */
1016 stp_port_set_speed(struct stp_port
*p
, unsigned int speed
)
1018 stp_port_set_path_cost(p
, stp_convert_speed_to_cost(speed
));
1021 /* Enables topology change detection on port 'p'. */
1023 stp_port_enable_change_detection(struct stp_port
*p
)
1025 p
->change_detection_enabled
= true;
1028 /* Disables topology change detection on port 'p'. */
1030 stp_port_disable_change_detection(struct stp_port
*p
)
1032 p
->change_detection_enabled
= false;
1036 stp_transmit_config(struct stp_port
*p
) OVS_REQUIRES(mutex
)
1038 struct stp
*stp
= p
->stp
;
1039 bool root
= stp_is_root_bridge(stp
);
1040 if (!root
&& !stp
->root_port
) {
1043 if (p
->hold_timer
.active
) {
1044 VLOG_DBG_RL(&stp_rl
, "bridge: %s, port: %s, transmit config bpdu pending",
1045 stp
->name
, p
->port_name
);
1046 p
->config_pending
= true;
1048 struct stp_config_bpdu config
;
1049 memset(&config
, 0, sizeof config
);
1050 config
.header
.protocol_id
= htons(STP_PROTOCOL_ID
);
1051 config
.header
.protocol_version
= STP_PROTOCOL_VERSION
;
1052 config
.header
.bpdu_type
= STP_TYPE_CONFIG
;
1054 if (p
->topology_change_ack
) {
1055 config
.flags
|= STP_CONFIG_TOPOLOGY_CHANGE_ACK
;
1057 if (stp
->topology_change
) {
1058 config
.flags
|= STP_CONFIG_TOPOLOGY_CHANGE
;
1060 config
.root_id
= htonll(stp
->designated_root
);
1061 config
.root_path_cost
= htonl(stp
->root_path_cost
);
1062 config
.bridge_id
= htonll(stp
->bridge_id
);
1063 config
.port_id
= htons(p
->port_id
);
1065 config
.message_age
= htons(0);
1067 config
.message_age
= htons(stp
->root_port
->message_age_timer
.value
1068 + MESSAGE_AGE_INCREMENT
);
1070 config
.max_age
= htons(stp
->max_age
);
1071 config
.hello_time
= htons(stp
->hello_time
);
1072 config
.forward_delay
= htons(stp
->forward_delay
);
1073 if (ntohs(config
.message_age
) < stp
->max_age
) {
1074 p
->topology_change_ack
= false;
1075 p
->config_pending
= false;
1076 VLOG_DBG_RL(&stp_rl
, "bridge: %s, port: %s, transmit config bpdu",
1077 stp
->name
, p
->port_name
);
1078 stp_send_bpdu(p
, &config
, sizeof config
);
1079 stp_start_timer(&p
->hold_timer
, 0);
1085 stp_supersedes_port_info(const struct stp_port
*p
,
1086 const struct stp_config_bpdu
*config
)
1089 if (ntohll(config
->root_id
) != p
->designated_root
) {
1090 return ntohll(config
->root_id
) < p
->designated_root
;
1091 } else if (ntohl(config
->root_path_cost
) != p
->designated_cost
) {
1092 return ntohl(config
->root_path_cost
) < p
->designated_cost
;
1093 } else if (ntohll(config
->bridge_id
) != p
->designated_bridge
) {
1094 return ntohll(config
->bridge_id
) < p
->designated_bridge
;
1096 return (ntohll(config
->bridge_id
) != p
->stp
->bridge_id
1097 || ntohs(config
->port_id
) <= p
->designated_port
);
1102 stp_record_config_information(struct stp_port
*p
,
1103 const struct stp_config_bpdu
*config
)
1106 p
->designated_root
= ntohll(config
->root_id
);
1107 p
->designated_cost
= ntohl(config
->root_path_cost
);
1108 p
->designated_bridge
= ntohll(config
->bridge_id
);
1109 p
->designated_port
= ntohs(config
->port_id
);
1110 stp_start_timer(&p
->message_age_timer
, ntohs(config
->message_age
));
1114 stp_record_config_timeout_values(struct stp
*stp
,
1115 const struct stp_config_bpdu
*config
)
1118 stp
->max_age
= ntohs(config
->max_age
);
1119 stp
->hello_time
= ntohs(config
->hello_time
);
1120 stp
->forward_delay
= ntohs(config
->forward_delay
);
1121 stp
->topology_change
= config
->flags
& STP_CONFIG_TOPOLOGY_CHANGE
;
1125 stp_is_designated_port(const struct stp_port
*p
) OVS_REQUIRES(mutex
)
1127 return (p
->designated_bridge
== p
->stp
->bridge_id
1128 && p
->designated_port
== p
->port_id
);
1132 stp_config_bpdu_generation(struct stp
*stp
) OVS_REQUIRES(mutex
)
1136 FOR_EACH_ENABLED_PORT (p
, stp
) {
1137 if (stp_is_designated_port(p
)) {
1138 stp_transmit_config(p
);
1144 stp_transmit_tcn(struct stp
*stp
) OVS_REQUIRES(mutex
)
1146 struct stp_port
*p
= stp
->root_port
;
1147 struct stp_tcn_bpdu tcn_bpdu
;
1152 VLOG_DBG_RL(&stp_rl
, "bridge: %s, root port: %s, transmit tcn", stp
->name
,
1154 tcn_bpdu
.header
.protocol_id
= htons(STP_PROTOCOL_ID
);
1155 tcn_bpdu
.header
.protocol_version
= STP_PROTOCOL_VERSION
;
1156 tcn_bpdu
.header
.bpdu_type
= STP_TYPE_TCN
;
1157 stp_send_bpdu(p
, &tcn_bpdu
, sizeof tcn_bpdu
);
1161 stp_configuration_update(struct stp
*stp
) OVS_REQUIRES(mutex
)
1163 stp_root_selection(stp
);
1164 stp_designated_port_selection(stp
);
1165 seq_change(connectivity_seq_get());
1169 stp_supersedes_root(const struct stp_port
*root
, const struct stp_port
*p
)
1172 int p_cost
= p
->designated_cost
+ p
->path_cost
;
1173 int root_cost
= root
->designated_cost
+ root
->path_cost
;
1175 if (p
->designated_root
!= root
->designated_root
) {
1176 return p
->designated_root
< root
->designated_root
;
1177 } else if (p_cost
!= root_cost
) {
1178 return p_cost
< root_cost
;
1179 } else if (p
->designated_bridge
!= root
->designated_bridge
) {
1180 return p
->designated_bridge
< root
->designated_bridge
;
1181 } else if (p
->designated_port
!= root
->designated_port
) {
1182 return p
->designated_port
< root
->designated_port
;
1184 return p
->port_id
< root
->port_id
;
1189 stp_root_selection(struct stp
*stp
) OVS_REQUIRES(mutex
)
1191 struct stp_port
*p
, *root
;
1194 FOR_EACH_ENABLED_PORT (p
, stp
) {
1195 if (stp_is_designated_port(p
)
1196 || p
->designated_root
>= stp
->bridge_id
) {
1199 if (root
&& !stp_supersedes_root(root
, p
)) {
1204 stp
->root_port
= root
;
1206 stp
->designated_root
= stp
->bridge_id
;
1207 stp
->root_path_cost
= 0;
1209 stp
->designated_root
= root
->designated_root
;
1210 stp
->root_path_cost
= root
->designated_cost
+ root
->path_cost
;
1215 stp_designated_port_selection(struct stp
*stp
) OVS_REQUIRES(mutex
)
1219 FOR_EACH_ENABLED_PORT (p
, stp
) {
1220 if (stp_is_designated_port(p
)
1221 || p
->designated_root
!= stp
->designated_root
1222 || stp
->root_path_cost
< p
->designated_cost
1223 || (stp
->root_path_cost
== p
->designated_cost
1224 && (stp
->bridge_id
< p
->designated_bridge
1225 || (stp
->bridge_id
== p
->designated_bridge
1226 && p
->port_id
<= p
->designated_port
))))
1228 stp_become_designated_port(p
);
1234 stp_become_designated_port(struct stp_port
*p
) OVS_REQUIRES(mutex
)
1236 struct stp
*stp
= p
->stp
;
1237 p
->designated_root
= stp
->designated_root
;
1238 p
->designated_cost
= stp
->root_path_cost
;
1239 p
->designated_bridge
= stp
->bridge_id
;
1240 p
->designated_port
= p
->port_id
;
1244 stp_port_state_selection(struct stp
*stp
) OVS_REQUIRES(mutex
)
1248 FOR_EACH_ENABLED_PORT (p
, stp
) {
1249 if (p
== stp
->root_port
) {
1250 p
->config_pending
= false;
1251 p
->topology_change_ack
= false;
1252 stp_make_forwarding(p
);
1253 } else if (stp_is_designated_port(p
)) {
1254 stp_stop_timer(&p
->message_age_timer
);
1255 stp_make_forwarding(p
);
1257 p
->config_pending
= false;
1258 p
->topology_change_ack
= false;
1259 stp_make_blocking(p
);
1265 stp_make_forwarding(struct stp_port
*p
) OVS_REQUIRES(mutex
)
1267 if (p
->state
== STP_BLOCKING
) {
1268 stp_set_port_state(p
, STP_LISTENING
);
1269 stp_start_timer(&p
->forward_delay_timer
, 0);
1274 stp_make_blocking(struct stp_port
*p
) OVS_REQUIRES(mutex
)
1276 if (!(p
->state
& (STP_DISABLED
| STP_BLOCKING
))) {
1277 if (p
->state
& (STP_FORWARDING
| STP_LEARNING
)) {
1278 if (p
->change_detection_enabled
) {
1279 stp_topology_change_detection(p
->stp
);
1282 stp_set_port_state(p
, STP_BLOCKING
);
1283 stp_stop_timer(&p
->forward_delay_timer
);
1288 stp_set_port_state(struct stp_port
*p
, enum stp_state state
)
1291 if (state
!= p
->state
&& !p
->state_changed
) {
1292 p
->state_changed
= true;
1293 if (p
< p
->stp
->first_changed_port
) {
1294 p
->stp
->first_changed_port
= p
;
1296 seq_change(connectivity_seq_get());
1302 stp_topology_change_detection(struct stp
*stp
) OVS_REQUIRES(mutex
)
1304 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1306 if (stp_is_root_bridge(stp
)) {
1307 stp
->topology_change
= true;
1308 stp_start_timer(&stp
->topology_change_timer
, 0);
1309 } else if (!stp
->topology_change_detected
) {
1310 stp_transmit_tcn(stp
);
1311 stp_start_timer(&stp
->tcn_timer
, 0);
1313 stp
->fdb_needs_flush
= true;
1314 stp
->topology_change_detected
= true;
1315 seq_change(connectivity_seq_get());
1316 VLOG_INFO_RL(&rl
, "%s: detected topology change.", stp
->name
);
1320 stp_topology_change_acknowledged(struct stp
*stp
) OVS_REQUIRES(mutex
)
1322 stp
->topology_change_detected
= false;
1323 stp_stop_timer(&stp
->tcn_timer
);
1327 stp_acknowledge_topology_change(struct stp_port
*p
) OVS_REQUIRES(mutex
)
1329 p
->topology_change_ack
= true;
1330 stp_transmit_config(p
);
1334 stp_received_config_bpdu(struct stp
*stp
, struct stp_port
*p
,
1335 const struct stp_config_bpdu
*config
)
1338 if (ntohs(config
->message_age
) >= ntohs(config
->max_age
)) {
1339 VLOG_WARN("%s: received config BPDU with message age (%u) greater "
1340 "than max age (%u)",
1342 ntohs(config
->message_age
), ntohs(config
->max_age
));
1345 if (p
->state
!= STP_DISABLED
) {
1346 bool root
= stp_is_root_bridge(stp
);
1347 if (stp_supersedes_port_info(p
, config
)) {
1348 stp_record_config_information(p
, config
);
1349 stp_configuration_update(stp
);
1350 stp_port_state_selection(stp
);
1351 if (!stp_is_root_bridge(stp
) && root
) {
1352 stp_stop_timer(&stp
->hello_timer
);
1353 if (stp
->topology_change_detected
) {
1354 stp_stop_timer(&stp
->topology_change_timer
);
1355 stp_transmit_tcn(stp
);
1356 stp_start_timer(&stp
->tcn_timer
, 0);
1359 if (p
== stp
->root_port
) {
1360 stp_record_config_timeout_values(stp
, config
);
1361 stp_config_bpdu_generation(stp
);
1362 if (config
->flags
& STP_CONFIG_TOPOLOGY_CHANGE_ACK
) {
1363 stp_topology_change_acknowledged(stp
);
1365 if (config
->flags
& STP_CONFIG_TOPOLOGY_CHANGE
) {
1366 stp
->fdb_needs_flush
= true;
1369 } else if (stp_is_designated_port(p
)) {
1370 stp_transmit_config(p
);
1376 stp_received_tcn_bpdu(struct stp
*stp
, struct stp_port
*p
)
1379 if (p
->state
!= STP_DISABLED
) {
1380 if (stp_is_designated_port(p
)) {
1381 stp_topology_change_detection(stp
);
1382 stp_acknowledge_topology_change(p
);
1388 stp_hello_timer_expiry(struct stp
*stp
) OVS_REQUIRES(mutex
)
1390 stp_config_bpdu_generation(stp
);
1391 stp_start_timer(&stp
->hello_timer
, 0);
1395 stp_message_age_timer_expiry(struct stp_port
*p
) OVS_REQUIRES(mutex
)
1397 struct stp
*stp
= p
->stp
;
1398 bool root
= stp_is_root_bridge(stp
);
1400 VLOG_DBG_RL(&stp_rl
, "bridge: %s, port: %s, message age timer expired",
1401 stp
->name
, p
->port_name
);
1402 stp_become_designated_port(p
);
1403 stp_configuration_update(stp
);
1404 stp_port_state_selection(stp
);
1405 if (stp_is_root_bridge(stp
) && !root
) {
1406 stp
->max_age
= stp
->bridge_max_age
;
1407 stp
->hello_time
= stp
->bridge_hello_time
;
1408 stp
->forward_delay
= stp
->bridge_forward_delay
;
1409 stp_topology_change_detection(stp
);
1410 stp_stop_timer(&stp
->tcn_timer
);
1411 stp_config_bpdu_generation(stp
);
1412 stp_start_timer(&stp
->hello_timer
, 0);
1417 stp_is_designated_for_some_port(const struct stp
*stp
) OVS_REQUIRES(mutex
)
1419 const struct stp_port
*p
;
1421 FOR_EACH_ENABLED_PORT (p
, stp
) {
1422 if (p
->designated_bridge
== stp
->bridge_id
) {
1430 stp_forward_delay_timer_expiry(struct stp_port
*p
) OVS_REQUIRES(mutex
)
1432 if (p
->state
== STP_LISTENING
) {
1433 stp_set_port_state(p
, STP_LEARNING
);
1434 stp_start_timer(&p
->forward_delay_timer
, 0);
1435 } else if (p
->state
== STP_LEARNING
) {
1436 stp_set_port_state(p
, STP_FORWARDING
);
1437 if (stp_is_designated_for_some_port(p
->stp
)) {
1438 if (p
->change_detection_enabled
) {
1439 stp_topology_change_detection(p
->stp
);
1446 stp_tcn_timer_expiry(struct stp
*stp
) OVS_REQUIRES(mutex
)
1448 stp_transmit_tcn(stp
);
1449 stp_start_timer(&stp
->tcn_timer
, 0);
1453 stp_topology_change_timer_expiry(struct stp
*stp
) OVS_REQUIRES(mutex
)
1455 stp
->topology_change_detected
= false;
1456 stp
->topology_change
= false;
1460 stp_hold_timer_expiry(struct stp_port
*p
) OVS_REQUIRES(mutex
)
1462 if (p
->config_pending
) {
1463 stp_transmit_config(p
);
1468 stp_initialize_port(struct stp_port
*p
, enum stp_state state
)
1471 ovs_assert(state
& (STP_DISABLED
| STP_BLOCKING
));
1472 stp_become_designated_port(p
);
1474 if (!p
->state
&& state
== STP_DISABLED
) {
1475 p
->state
= state
; /* Do not trigger state change when initializing. */
1477 stp_set_port_state(p
, state
);
1479 p
->topology_change_ack
= false;
1480 p
->config_pending
= false;
1481 p
->change_detection_enabled
= true;
1483 stp_stop_timer(&p
->message_age_timer
);
1484 stp_stop_timer(&p
->forward_delay_timer
);
1485 stp_stop_timer(&p
->hold_timer
);
1486 p
->tx_count
= p
->rx_count
= p
->error_count
= 0;
1490 stp_become_root_bridge(struct stp
*stp
) OVS_REQUIRES(mutex
)
1492 stp
->max_age
= stp
->bridge_max_age
;
1493 stp
->hello_time
= stp
->bridge_hello_time
;
1494 stp
->forward_delay
= stp
->bridge_forward_delay
;
1495 stp_topology_change_detection(stp
);
1496 stp_stop_timer(&stp
->tcn_timer
);
1497 stp_config_bpdu_generation(stp
);
1498 stp_start_timer(&stp
->hello_timer
, 0);
1502 stp_start_timer(struct stp_timer
*timer
, int value
) OVS_REQUIRES(mutex
)
1504 timer
->value
= value
;
1505 timer
->active
= true;
1509 stp_stop_timer(struct stp_timer
*timer
) OVS_REQUIRES(mutex
)
1511 timer
->active
= false;
1515 stp_timer_expired(struct stp_timer
*timer
, int elapsed
, int timeout
)
1518 if (timer
->active
) {
1519 timer
->value
+= elapsed
;
1520 if (timer
->value
>= timeout
) {
1521 timer
->active
= false;
1528 /* Returns the number of whole STP timer ticks in 'ms' milliseconds. There
1529 * are 256 STP timer ticks per second. */
1533 return ms
* 0x100 / 1000;
1536 /* Returns the number of whole milliseconds in 'timer' STP timer ticks. There
1537 * are 256 STP timer ticks per second. */
1539 timer_to_ms(int timer
)
1541 return timer
* 1000 / 0x100;
1545 clamp(int x
, int min
, int max
)
1547 return x
< min
? min
: x
> max
? max
: x
;
1551 stp_update_bridge_timers(struct stp
*stp
) OVS_REQUIRES(mutex
)
1555 ht
= clamp(stp
->rq_hello_time
, 1000, 10000);
1556 ma
= clamp(stp
->rq_max_age
, MAX(2 * (ht
+ 1000), 6000), 40000);
1557 fd
= clamp(stp
->rq_forward_delay
, ma
/ 2 + 1000, 30000);
1559 stp
->bridge_hello_time
= ms_to_timer(ht
);
1560 stp
->bridge_max_age
= ms_to_timer(ma
);
1561 stp
->bridge_forward_delay
= ms_to_timer(fd
);
1563 if (stp_is_root_bridge(stp
)) {
1564 stp
->max_age
= stp
->bridge_max_age
;
1565 stp
->hello_time
= stp
->bridge_hello_time
;
1566 stp
->forward_delay
= stp
->bridge_forward_delay
;
1571 stp_send_bpdu(struct stp_port
*p
, const void *bpdu
, size_t bpdu_size
)
1574 struct eth_header
*eth
;
1575 struct llc_header
*llc
;
1576 struct dp_packet
*pkt
;
1579 pkt
= dp_packet_new(ETH_HEADER_LEN
+ LLC_HEADER_LEN
+ bpdu_size
);
1580 eth
= dp_packet_put_zeros(pkt
, sizeof *eth
);
1581 llc
= dp_packet_put_zeros(pkt
, sizeof *llc
);
1582 dp_packet_reset_offsets(pkt
);
1583 dp_packet_set_l3(pkt
, dp_packet_put(pkt
, bpdu
, bpdu_size
));
1586 eth
->eth_dst
= eth_addr_stp
;
1587 /* p->stp->send_bpdu() must fill in source address. */
1588 eth
->eth_type
= htons(dp_packet_size(pkt
) - ETH_HEADER_LEN
);
1591 llc
->llc_dsap
= STP_LLC_DSAP
;
1592 llc
->llc_ssap
= STP_LLC_SSAP
;
1593 llc
->llc_cntl
= STP_LLC_CNTL
;
1595 p
->stp
->send_bpdu(pkt
, stp_port_no(p
), p
->stp
->aux
);
1602 stp_find(const char *name
) OVS_REQUIRES(mutex
)
1606 LIST_FOR_EACH (stp
, node
, all_stps
) {
1607 if (!strcmp(stp
->name
, name
)) {
1615 stp_unixctl_tcn(struct unixctl_conn
*conn
, int argc
,
1616 const char *argv
[], void *aux OVS_UNUSED
)
1618 ovs_mutex_lock(&mutex
);
1620 struct stp
*stp
= stp_find(argv
[1]);
1623 unixctl_command_reply_error(conn
, "no such stp object");
1626 stp_topology_change_detection(stp
);
1630 LIST_FOR_EACH (stp
, node
, all_stps
) {
1631 stp_topology_change_detection(stp
);
1635 unixctl_command_reply(conn
, "OK");
1638 ovs_mutex_unlock(&mutex
);