]>
Commit | Line | Data |
---|---|---|
829a7d02 | 1 | /* |
8917f72c | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc. |
829a7d02 JP |
3 | * |
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: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
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. | |
15 | */ | |
16 | ||
17 | /* Based on sample implementation in 802.1D-1998. Above copyright and license | |
18 | * applies to all modifications. */ | |
19 | ||
20 | #include <config.h> | |
21 | ||
22 | #include "stp.h" | |
23 | #include <sys/types.h> | |
24 | #include <netinet/in.h> | |
25 | #include <arpa/inet.h> | |
829a7d02 JP |
26 | #include <inttypes.h> |
27 | #include <stdlib.h> | |
28 | #include "byte-order.h" | |
f23d157c | 29 | #include "connectivity.h" |
829a7d02 JP |
30 | #include "ofpbuf.h" |
31 | #include "packets.h" | |
f23d157c | 32 | #include "seq.h" |
fe4a02e4 | 33 | #include "unixctl.h" |
829a7d02 JP |
34 | #include "util.h" |
35 | #include "vlog.h" | |
36 | ||
37 | VLOG_DEFINE_THIS_MODULE(stp); | |
38 | ||
11306274 AW |
39 | static struct vlog_rate_limit stp_rl = VLOG_RATE_LIMIT_INIT(60, 60); |
40 | ||
829a7d02 JP |
41 | #define STP_PROTOCOL_ID 0x0000 |
42 | #define STP_PROTOCOL_VERSION 0x00 | |
43 | #define STP_TYPE_CONFIG 0x00 | |
44 | #define STP_TYPE_TCN 0x80 | |
45 | ||
13b6bae6 | 46 | OVS_PACKED( |
829a7d02 JP |
47 | struct stp_bpdu_header { |
48 | ovs_be16 protocol_id; /* STP_PROTOCOL_ID. */ | |
49 | uint8_t protocol_version; /* STP_PROTOCOL_VERSION. */ | |
50 | uint8_t bpdu_type; /* One of STP_TYPE_*. */ | |
13b6bae6 | 51 | }); |
829a7d02 JP |
52 | BUILD_ASSERT_DECL(sizeof(struct stp_bpdu_header) == 4); |
53 | ||
54 | enum stp_config_bpdu_flags { | |
55 | STP_CONFIG_TOPOLOGY_CHANGE_ACK = 0x80, | |
56 | STP_CONFIG_TOPOLOGY_CHANGE = 0x01 | |
57 | }; | |
58 | ||
13b6bae6 | 59 | OVS_PACKED( |
829a7d02 JP |
60 | struct stp_config_bpdu { |
61 | struct stp_bpdu_header header; /* Type STP_TYPE_CONFIG. */ | |
62 | uint8_t flags; /* STP_CONFIG_* flags. */ | |
63 | ovs_be64 root_id; /* 8.5.1.1: Bridge believed to be root. */ | |
64 | ovs_be32 root_path_cost; /* 8.5.1.2: Cost of path to root. */ | |
65 | ovs_be64 bridge_id; /* 8.5.1.3: ID of transmitting bridge. */ | |
66 | ovs_be16 port_id; /* 8.5.1.4: Port transmitting the BPDU. */ | |
67 | ovs_be16 message_age; /* 8.5.1.5: Age of BPDU at tx time. */ | |
68 | ovs_be16 max_age; /* 8.5.1.6: Timeout for received data. */ | |
69 | ovs_be16 hello_time; /* 8.5.1.7: Time between BPDU generation. */ | |
70 | ovs_be16 forward_delay; /* 8.5.1.8: State progression delay. */ | |
13b6bae6 | 71 | }); |
829a7d02 JP |
72 | BUILD_ASSERT_DECL(sizeof(struct stp_config_bpdu) == 35); |
73 | ||
13b6bae6 | 74 | OVS_PACKED( |
829a7d02 JP |
75 | struct stp_tcn_bpdu { |
76 | struct stp_bpdu_header header; /* Type STP_TYPE_TCN. */ | |
13b6bae6 | 77 | }); |
829a7d02 JP |
78 | BUILD_ASSERT_DECL(sizeof(struct stp_tcn_bpdu) == 4); |
79 | ||
80 | struct stp_timer { | |
81 | bool active; /* Timer in use? */ | |
82 | int value; /* Current value of timer, counting up. */ | |
83 | }; | |
84 | ||
85 | struct stp_port { | |
86 | struct stp *stp; | |
11306274 | 87 | char *port_name; /* Human-readable name for log messages. */ |
3310b570 | 88 | void *aux; /* Auxiliary data the user may retrieve. */ |
829a7d02 JP |
89 | int port_id; /* 8.5.5.1: Unique port identifier. */ |
90 | enum stp_state state; /* 8.5.5.2: Current state. */ | |
91 | int path_cost; /* 8.5.5.3: Cost of tx/rx on this port. */ | |
92 | stp_identifier designated_root; /* 8.5.5.4. */ | |
93 | int designated_cost; /* 8.5.5.5: Path cost to root on port. */ | |
94 | stp_identifier designated_bridge; /* 8.5.5.6. */ | |
95 | int designated_port; /* 8.5.5.7: Port to send config msgs on. */ | |
96 | bool topology_change_ack; /* 8.5.5.8: Flag for next config BPDU. */ | |
97 | bool config_pending; /* 8.5.5.9: Send BPDU when hold expires? */ | |
98 | bool change_detection_enabled; /* 8.5.5.10: Detect topology changes? */ | |
99 | ||
100 | struct stp_timer message_age_timer; /* 8.5.6.1: Age of received info. */ | |
101 | struct stp_timer forward_delay_timer; /* 8.5.6.2: State change timer. */ | |
102 | struct stp_timer hold_timer; /* 8.5.6.3: BPDU rate limit timer. */ | |
103 | ||
80740385 JP |
104 | int tx_count; /* Number of BPDUs transmitted. */ |
105 | int rx_count; /* Number of valid BPDUs received. */ | |
106 | int error_count; /* Number of bad BPDUs received. */ | |
107 | ||
829a7d02 JP |
108 | bool state_changed; |
109 | }; | |
110 | ||
111 | struct stp { | |
ca6ba700 | 112 | struct ovs_list node; /* Node in all_stps list. */ |
fe4a02e4 | 113 | |
829a7d02 JP |
114 | /* Static bridge data. */ |
115 | char *name; /* Human-readable name for log messages. */ | |
116 | stp_identifier bridge_id; /* 8.5.3.7: This bridge. */ | |
117 | int max_age; /* 8.5.3.4: Time to drop received data. */ | |
118 | int hello_time; /* 8.5.3.5: Time between sending BPDUs. */ | |
119 | int forward_delay; /* 8.5.3.6: Delay between state changes. */ | |
120 | int bridge_max_age; /* 8.5.3.8: max_age when we're root. */ | |
121 | int bridge_hello_time; /* 8.5.3.9: hello_time as root. */ | |
122 | int bridge_forward_delay; /* 8.5.3.10: forward_delay as root. */ | |
123 | int rq_max_age; /* User-requested max age, in ms. */ | |
124 | int rq_hello_time; /* User-requested hello time, in ms. */ | |
125 | int rq_forward_delay; /* User-requested forward delay, in ms. */ | |
126 | int elapsed_remainder; /* Left-over msecs from last stp_tick(). */ | |
127 | ||
128 | /* Dynamic bridge data. */ | |
129 | stp_identifier designated_root; /* 8.5.3.1: Bridge believed to be root. */ | |
130 | unsigned int root_path_cost; /* 8.5.3.2: Cost of path to root. */ | |
131 | struct stp_port *root_port; /* 8.5.3.3: Lowest cost port to root. */ | |
132 | bool topology_change_detected; /* 8.5.3.11: Detected a topology change? */ | |
133 | bool topology_change; /* 8.5.3.12: Received topology change? */ | |
134 | ||
135 | /* Bridge timers. */ | |
136 | struct stp_timer hello_timer; /* 8.5.4.1: Hello timer. */ | |
137 | struct stp_timer tcn_timer; /* 8.5.4.2: Topology change timer. */ | |
138 | struct stp_timer topology_change_timer; /* 8.5.4.3. */ | |
139 | ||
140 | /* Ports. */ | |
141 | struct stp_port ports[STP_MAX_PORTS]; | |
142 | ||
143 | /* Interface to client. */ | |
6ae50723 | 144 | bool fdb_needs_flush; /* MAC learning tables needs flushing. */ |
829a7d02 JP |
145 | struct stp_port *first_changed_port; |
146 | void (*send_bpdu)(struct ofpbuf *bpdu, int port_no, void *aux); | |
147 | void *aux; | |
bd54dbcd | 148 | |
37bec3d3 | 149 | struct ovs_refcount ref_cnt; |
829a7d02 JP |
150 | }; |
151 | ||
bd54dbcd | 152 | static struct ovs_mutex mutex; |
55951e15 | 153 | static struct ovs_list all_stps__ = OVS_LIST_INITIALIZER(&all_stps__); |
ca6ba700 | 154 | static struct ovs_list *const all_stps OVS_GUARDED_BY(mutex) = &all_stps__; |
fe4a02e4 | 155 | |
829a7d02 JP |
156 | #define FOR_EACH_ENABLED_PORT(PORT, STP) \ |
157 | for ((PORT) = stp_next_enabled_port((STP), (STP)->ports); \ | |
158 | (PORT); \ | |
159 | (PORT) = stp_next_enabled_port((STP), (PORT) + 1)) | |
160 | static struct stp_port * | |
161 | stp_next_enabled_port(const struct stp *stp, const struct stp_port *port) | |
bd3950dd | 162 | OVS_REQUIRES(mutex) |
829a7d02 JP |
163 | { |
164 | for (; port < &stp->ports[ARRAY_SIZE(stp->ports)]; port++) { | |
165 | if (port->state != STP_DISABLED) { | |
ebc56baa | 166 | return CONST_CAST(struct stp_port *, port); |
829a7d02 JP |
167 | } |
168 | } | |
169 | return NULL; | |
170 | } | |
171 | ||
172 | #define MESSAGE_AGE_INCREMENT 1 | |
173 | ||
bd3950dd | 174 | static void stp_transmit_config(struct stp_port *) OVS_REQUIRES(mutex); |
829a7d02 | 175 | static bool stp_supersedes_port_info(const struct stp_port *, |
bd54dbcd | 176 | const struct stp_config_bpdu *) |
bd3950dd | 177 | OVS_REQUIRES(mutex); |
829a7d02 | 178 | static void stp_record_config_information(struct stp_port *, |
bd54dbcd | 179 | const struct stp_config_bpdu *) |
bd3950dd | 180 | OVS_REQUIRES(mutex); |
829a7d02 | 181 | static void stp_record_config_timeout_values(struct stp *, |
bd54dbcd | 182 | const struct stp_config_bpdu *) |
bd3950dd | 183 | OVS_REQUIRES(mutex); |
bd54dbcd | 184 | static bool stp_is_designated_port(const struct stp_port *) |
bd3950dd AW |
185 | OVS_REQUIRES(mutex); |
186 | static void stp_config_bpdu_generation(struct stp *) OVS_REQUIRES(mutex); | |
187 | static void stp_transmit_tcn(struct stp *) OVS_REQUIRES(mutex); | |
188 | static void stp_configuration_update(struct stp *) OVS_REQUIRES(mutex); | |
829a7d02 | 189 | static bool stp_supersedes_root(const struct stp_port *root, |
bd3950dd AW |
190 | const struct stp_port *) OVS_REQUIRES(mutex); |
191 | static void stp_root_selection(struct stp *) OVS_REQUIRES(mutex); | |
192 | static void stp_designated_port_selection(struct stp *) OVS_REQUIRES(mutex); | |
bd54dbcd | 193 | static void stp_become_designated_port(struct stp_port *) |
bd3950dd AW |
194 | OVS_REQUIRES(mutex); |
195 | static void stp_port_state_selection(struct stp *) OVS_REQUIRES(mutex); | |
196 | static void stp_make_forwarding(struct stp_port *) OVS_REQUIRES(mutex); | |
197 | static void stp_make_blocking(struct stp_port *) OVS_REQUIRES(mutex); | |
bd54dbcd | 198 | static void stp_set_port_state(struct stp_port *, enum stp_state) |
bd3950dd AW |
199 | OVS_REQUIRES(mutex); |
200 | static void stp_topology_change_detection(struct stp *) OVS_REQUIRES(mutex); | |
bd54dbcd | 201 | static void stp_topology_change_acknowledged(struct stp *) |
bd3950dd | 202 | OVS_REQUIRES(mutex); |
bd54dbcd | 203 | static void stp_acknowledge_topology_change(struct stp_port *) |
bd3950dd | 204 | OVS_REQUIRES(mutex); |
829a7d02 | 205 | static void stp_received_config_bpdu(struct stp *, struct stp_port *, |
bd54dbcd | 206 | const struct stp_config_bpdu *) |
bd3950dd | 207 | OVS_REQUIRES(mutex); |
bd54dbcd | 208 | static void stp_received_tcn_bpdu(struct stp *, struct stp_port *) |
bd3950dd AW |
209 | OVS_REQUIRES(mutex); |
210 | static void stp_hello_timer_expiry(struct stp *) OVS_REQUIRES(mutex); | |
bd54dbcd | 211 | static void stp_message_age_timer_expiry(struct stp_port *) |
bd3950dd | 212 | OVS_REQUIRES(mutex); |
bd54dbcd | 213 | static bool stp_is_designated_for_some_port(const struct stp *) |
bd3950dd | 214 | OVS_REQUIRES(mutex); |
bd54dbcd | 215 | static void stp_forward_delay_timer_expiry(struct stp_port *) |
bd3950dd AW |
216 | OVS_REQUIRES(mutex); |
217 | static void stp_tcn_timer_expiry(struct stp *) OVS_REQUIRES(mutex); | |
bd54dbcd | 218 | static void stp_topology_change_timer_expiry(struct stp *) |
bd3950dd AW |
219 | OVS_REQUIRES(mutex); |
220 | static void stp_hold_timer_expiry(struct stp_port *) OVS_REQUIRES(mutex); | |
bd54dbcd | 221 | static void stp_initialize_port(struct stp_port *, enum stp_state) |
bd3950dd AW |
222 | OVS_REQUIRES(mutex); |
223 | static void stp_become_root_bridge(struct stp *) OVS_REQUIRES(mutex); | |
224 | static void stp_update_bridge_timers(struct stp *) OVS_REQUIRES(mutex); | |
829a7d02 JP |
225 | |
226 | static int clamp(int x, int min, int max); | |
227 | static int ms_to_timer(int ms); | |
829a7d02 JP |
228 | static int timer_to_ms(int timer); |
229 | static void stp_start_timer(struct stp_timer *, int value); | |
230 | static void stp_stop_timer(struct stp_timer *); | |
231 | static bool stp_timer_expired(struct stp_timer *, int elapsed, int timeout); | |
232 | ||
bd54dbcd | 233 | static void stp_send_bpdu(struct stp_port *, const void *, size_t) |
bd3950dd | 234 | OVS_REQUIRES(mutex); |
fe4a02e4 EJ |
235 | static void stp_unixctl_tcn(struct unixctl_conn *, int argc, |
236 | const char *argv[], void *aux); | |
237 | ||
238 | void | |
239 | stp_init(void) | |
240 | { | |
241 | unixctl_command_register("stp/tcn", "[bridge]", 0, 1, stp_unixctl_tcn, | |
242 | NULL); | |
243 | } | |
829a7d02 JP |
244 | |
245 | /* Creates and returns a new STP instance that initially has no ports enabled. | |
246 | * | |
247 | * 'bridge_id' should be a 48-bit MAC address as returned by | |
248 | * eth_addr_to_uint64(). 'bridge_id' may also have a priority value in its top | |
249 | * 16 bits; if those bits are set to 0, STP_DEFAULT_BRIDGE_PRIORITY is used. | |
250 | * (This priority may be changed with stp_set_bridge_priority().) | |
251 | * | |
252 | * When the bridge needs to send out a BPDU, it calls 'send_bpdu'. This | |
253 | * callback may be called from stp_tick() or stp_received_bpdu(). The | |
254 | * arguments to 'send_bpdu' are an STP BPDU encapsulated in 'bpdu', | |
255 | * the spanning tree port number 'port_no' that should transmit the | |
256 | * packet, and auxiliary data to be passed to the callback in 'aux'. | |
257 | */ | |
258 | struct stp * | |
259 | stp_create(const char *name, stp_identifier bridge_id, | |
260 | void (*send_bpdu)(struct ofpbuf *bpdu, int port_no, void *aux), | |
261 | void *aux) | |
262 | { | |
bd54dbcd | 263 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; |
829a7d02 JP |
264 | struct stp *stp; |
265 | struct stp_port *p; | |
266 | ||
bd54dbcd EJ |
267 | if (ovsthread_once_start(&once)) { |
268 | /* We need a recursive mutex because stp_send_bpdu() could loop back | |
269 | * into the stp module through a patch port. This happens | |
270 | * intentionally as part of the unit tests. Ideally we'd ditch | |
271 | * the call back function, but for now this is what we have. */ | |
834d6caf | 272 | ovs_mutex_init_recursive(&mutex); |
bd54dbcd EJ |
273 | ovsthread_once_done(&once); |
274 | } | |
275 | ||
276 | ovs_mutex_lock(&mutex); | |
829a7d02 JP |
277 | stp = xzalloc(sizeof *stp); |
278 | stp->name = xstrdup(name); | |
279 | stp->bridge_id = bridge_id; | |
280 | if (!(stp->bridge_id >> 48)) { | |
281 | stp->bridge_id |= (uint64_t) STP_DEFAULT_BRIDGE_PRIORITY << 48; | |
282 | } | |
283 | ||
3310b570 JP |
284 | stp->rq_max_age = STP_DEFAULT_MAX_AGE; |
285 | stp->rq_hello_time = STP_DEFAULT_HELLO_TIME; | |
286 | stp->rq_forward_delay = STP_DEFAULT_FWD_DELAY; | |
829a7d02 JP |
287 | stp_update_bridge_timers(stp); |
288 | stp->max_age = stp->bridge_max_age; | |
289 | stp->hello_time = stp->bridge_hello_time; | |
290 | stp->forward_delay = stp->bridge_forward_delay; | |
291 | ||
292 | stp->designated_root = stp->bridge_id; | |
293 | stp->root_path_cost = 0; | |
294 | stp->root_port = NULL; | |
295 | stp->topology_change_detected = false; | |
296 | stp->topology_change = false; | |
297 | ||
298 | stp_stop_timer(&stp->tcn_timer); | |
299 | stp_stop_timer(&stp->topology_change_timer); | |
300 | stp_start_timer(&stp->hello_timer, 0); | |
301 | ||
302 | stp->send_bpdu = send_bpdu; | |
303 | stp->aux = aux; | |
304 | ||
305 | stp->first_changed_port = &stp->ports[ARRAY_SIZE(stp->ports)]; | |
306 | for (p = stp->ports; p < &stp->ports[ARRAY_SIZE(stp->ports)]; p++) { | |
307 | p->stp = stp; | |
308 | p->port_id = (stp_port_no(p) + 1) | (STP_DEFAULT_PORT_PRIORITY << 8); | |
309 | p->path_cost = 19; /* Recommended default for 100 Mb/s link. */ | |
310 | stp_initialize_port(p, STP_DISABLED); | |
311 | } | |
37bec3d3 | 312 | ovs_refcount_init(&stp->ref_cnt); |
bd54dbcd EJ |
313 | |
314 | list_push_back(all_stps, &stp->node); | |
315 | ovs_mutex_unlock(&mutex); | |
316 | return stp; | |
317 | } | |
318 | ||
319 | struct stp * | |
320 | stp_ref(const struct stp *stp_) | |
321 | { | |
322 | struct stp *stp = CONST_CAST(struct stp *, stp_); | |
323 | if (stp) { | |
37bec3d3 | 324 | ovs_refcount_ref(&stp->ref_cnt); |
bd54dbcd | 325 | } |
829a7d02 JP |
326 | return stp; |
327 | } | |
328 | ||
329 | /* Destroys 'stp'. */ | |
330 | void | |
bd54dbcd | 331 | stp_unref(struct stp *stp) |
829a7d02 | 332 | { |
24f83812 | 333 | if (stp && ovs_refcount_unref_relaxed(&stp->ref_cnt) == 1) { |
11306274 AW |
334 | size_t i; |
335 | ||
bd54dbcd | 336 | ovs_mutex_lock(&mutex); |
fe4a02e4 | 337 | list_remove(&stp->node); |
bd54dbcd | 338 | ovs_mutex_unlock(&mutex); |
829a7d02 | 339 | free(stp->name); |
11306274 AW |
340 | |
341 | for (i = 0; i < STP_MAX_PORTS; i++) { | |
342 | free(stp->ports[i].port_name); | |
343 | } | |
829a7d02 JP |
344 | free(stp); |
345 | } | |
346 | } | |
347 | ||
348 | /* Runs 'stp' given that 'ms' milliseconds have passed. */ | |
349 | void | |
350 | stp_tick(struct stp *stp, int ms) | |
351 | { | |
352 | struct stp_port *p; | |
353 | int elapsed; | |
354 | ||
bd54dbcd | 355 | ovs_mutex_lock(&mutex); |
829a7d02 JP |
356 | /* Convert 'ms' to STP timer ticks. Preserve any leftover milliseconds |
357 | * from previous stp_tick() calls so that we don't lose STP ticks when we | |
358 | * are called too frequently. */ | |
359 | ms = clamp(ms, 0, INT_MAX - 1000) + stp->elapsed_remainder; | |
360 | elapsed = ms_to_timer(ms); | |
ba25773f | 361 | stp->elapsed_remainder = ms - timer_to_ms(elapsed); |
829a7d02 | 362 | if (!elapsed) { |
bd54dbcd | 363 | goto out; |
829a7d02 JP |
364 | } |
365 | ||
366 | if (stp_timer_expired(&stp->hello_timer, elapsed, stp->hello_time)) { | |
367 | stp_hello_timer_expiry(stp); | |
368 | } | |
369 | if (stp_timer_expired(&stp->tcn_timer, elapsed, stp->bridge_hello_time)) { | |
370 | stp_tcn_timer_expiry(stp); | |
371 | } | |
372 | if (stp_timer_expired(&stp->topology_change_timer, elapsed, | |
373 | stp->max_age + stp->forward_delay)) { | |
374 | stp_topology_change_timer_expiry(stp); | |
375 | } | |
376 | FOR_EACH_ENABLED_PORT (p, stp) { | |
377 | if (stp_timer_expired(&p->message_age_timer, elapsed, stp->max_age)) { | |
378 | stp_message_age_timer_expiry(p); | |
379 | } | |
380 | } | |
381 | FOR_EACH_ENABLED_PORT (p, stp) { | |
382 | if (stp_timer_expired(&p->forward_delay_timer, elapsed, | |
383 | stp->forward_delay)) { | |
384 | stp_forward_delay_timer_expiry(p); | |
385 | } | |
386 | if (stp_timer_expired(&p->hold_timer, elapsed, ms_to_timer(1000))) { | |
387 | stp_hold_timer_expiry(p); | |
388 | } | |
389 | } | |
bd54dbcd EJ |
390 | |
391 | out: | |
392 | ovs_mutex_unlock(&mutex); | |
829a7d02 JP |
393 | } |
394 | ||
395 | static void | |
396 | set_bridge_id(struct stp *stp, stp_identifier new_bridge_id) | |
bd3950dd | 397 | OVS_REQUIRES(mutex) |
829a7d02 JP |
398 | { |
399 | if (new_bridge_id != stp->bridge_id) { | |
400 | bool root; | |
401 | struct stp_port *p; | |
402 | ||
403 | root = stp_is_root_bridge(stp); | |
404 | FOR_EACH_ENABLED_PORT (p, stp) { | |
405 | if (stp_is_designated_port(p)) { | |
406 | p->designated_bridge = new_bridge_id; | |
407 | } | |
408 | } | |
409 | stp->bridge_id = new_bridge_id; | |
410 | stp_configuration_update(stp); | |
411 | stp_port_state_selection(stp); | |
412 | if (stp_is_root_bridge(stp) && !root) { | |
413 | stp_become_root_bridge(stp); | |
414 | } | |
415 | } | |
416 | } | |
417 | ||
418 | void | |
419 | stp_set_bridge_id(struct stp *stp, stp_identifier bridge_id) | |
420 | { | |
421 | const uint64_t mac_bits = (UINT64_C(1) << 48) - 1; | |
422 | const uint64_t pri_bits = ~mac_bits; | |
bd54dbcd | 423 | ovs_mutex_lock(&mutex); |
829a7d02 | 424 | set_bridge_id(stp, (stp->bridge_id & pri_bits) | (bridge_id & mac_bits)); |
bd54dbcd | 425 | ovs_mutex_unlock(&mutex); |
829a7d02 JP |
426 | } |
427 | ||
428 | void | |
429 | stp_set_bridge_priority(struct stp *stp, uint16_t new_priority) | |
430 | { | |
431 | const uint64_t mac_bits = (UINT64_C(1) << 48) - 1; | |
bd54dbcd | 432 | ovs_mutex_lock(&mutex); |
829a7d02 JP |
433 | set_bridge_id(stp, ((stp->bridge_id & mac_bits) |
434 | | ((uint64_t) new_priority << 48))); | |
bd54dbcd | 435 | ovs_mutex_unlock(&mutex); |
829a7d02 JP |
436 | } |
437 | ||
438 | /* Sets the desired hello time for 'stp' to 'ms', in milliseconds. The actual | |
439 | * hello time is clamped to the range of 1 to 10 seconds and subject to the | |
440 | * relationship (bridge_max_age >= 2 * (bridge_hello_time + 1 s)). The bridge | |
441 | * hello time is only used when 'stp' is the root bridge. */ | |
442 | void | |
443 | stp_set_hello_time(struct stp *stp, int ms) | |
444 | { | |
bd54dbcd | 445 | ovs_mutex_lock(&mutex); |
829a7d02 JP |
446 | stp->rq_hello_time = ms; |
447 | stp_update_bridge_timers(stp); | |
bd54dbcd | 448 | ovs_mutex_unlock(&mutex); |
829a7d02 JP |
449 | } |
450 | ||
451 | /* Sets the desired max age for 'stp' to 'ms', in milliseconds. The actual max | |
452 | * age is clamped to the range of 6 to 40 seconds and subject to the | |
453 | * relationships (2 * (bridge_forward_delay - 1 s) >= bridge_max_age) and | |
454 | * (bridge_max_age >= 2 * (bridge_hello_time + 1 s)). The bridge max age is | |
455 | * only used when 'stp' is the root bridge. */ | |
456 | void | |
457 | stp_set_max_age(struct stp *stp, int ms) | |
458 | { | |
bd54dbcd | 459 | ovs_mutex_lock(&mutex); |
829a7d02 JP |
460 | stp->rq_max_age = ms; |
461 | stp_update_bridge_timers(stp); | |
bd54dbcd | 462 | ovs_mutex_unlock(&mutex); |
829a7d02 JP |
463 | } |
464 | ||
465 | /* Sets the desired forward delay for 'stp' to 'ms', in milliseconds. The | |
466 | * actual forward delay is clamped to the range of 4 to 30 seconds and subject | |
467 | * to the relationship (2 * (bridge_forward_delay - 1 s) >= bridge_max_age). | |
468 | * The bridge forward delay is only used when 'stp' is the root bridge. */ | |
469 | void | |
470 | stp_set_forward_delay(struct stp *stp, int ms) | |
471 | { | |
bd54dbcd | 472 | ovs_mutex_lock(&mutex); |
829a7d02 JP |
473 | stp->rq_forward_delay = ms; |
474 | stp_update_bridge_timers(stp); | |
bd54dbcd | 475 | ovs_mutex_unlock(&mutex); |
829a7d02 JP |
476 | } |
477 | ||
478 | /* Returns the name given to 'stp' in the call to stp_create(). */ | |
479 | const char * | |
480 | stp_get_name(const struct stp *stp) | |
481 | { | |
bd54dbcd EJ |
482 | char *name; |
483 | ||
484 | ovs_mutex_lock(&mutex); | |
485 | name = stp->name; | |
486 | ovs_mutex_unlock(&mutex); | |
487 | return name; | |
829a7d02 JP |
488 | } |
489 | ||
490 | /* Returns the bridge ID for 'stp'. */ | |
491 | stp_identifier | |
492 | stp_get_bridge_id(const struct stp *stp) | |
493 | { | |
bd54dbcd EJ |
494 | stp_identifier bridge_id; |
495 | ||
496 | ovs_mutex_lock(&mutex); | |
497 | bridge_id = stp->bridge_id; | |
498 | ovs_mutex_unlock(&mutex); | |
499 | return bridge_id; | |
829a7d02 JP |
500 | } |
501 | ||
502 | /* Returns the bridge ID of the bridge currently believed to be the root. */ | |
503 | stp_identifier | |
504 | stp_get_designated_root(const struct stp *stp) | |
505 | { | |
bd54dbcd EJ |
506 | stp_identifier designated_root; |
507 | ||
508 | ovs_mutex_lock(&mutex); | |
509 | designated_root = stp->designated_root; | |
510 | ovs_mutex_unlock(&mutex); | |
511 | return designated_root; | |
829a7d02 JP |
512 | } |
513 | ||
514 | /* Returns true if 'stp' believes itself to the be root of the spanning tree, | |
515 | * false otherwise. */ | |
516 | bool | |
517 | stp_is_root_bridge(const struct stp *stp) | |
518 | { | |
bd54dbcd EJ |
519 | bool is_root; |
520 | ||
521 | ovs_mutex_lock(&mutex); | |
522 | is_root = stp->bridge_id == stp->designated_root; | |
523 | ovs_mutex_unlock(&mutex); | |
524 | return is_root; | |
829a7d02 JP |
525 | } |
526 | ||
527 | /* Returns the cost of the path from 'stp' to the root of the spanning tree. */ | |
528 | int | |
529 | stp_get_root_path_cost(const struct stp *stp) | |
530 | { | |
bd54dbcd EJ |
531 | int cost; |
532 | ||
533 | ovs_mutex_lock(&mutex); | |
534 | cost = stp->root_path_cost; | |
535 | ovs_mutex_unlock(&mutex); | |
536 | return cost; | |
829a7d02 JP |
537 | } |
538 | ||
539 | /* Returns the bridge hello time, in ms. The returned value is not necessarily | |
540 | * the value passed to stp_set_hello_time(): it is clamped to the valid range | |
541 | * and quantized to the STP timer resolution. */ | |
542 | int | |
543 | stp_get_hello_time(const struct stp *stp) | |
544 | { | |
bd54dbcd EJ |
545 | int time; |
546 | ||
547 | ovs_mutex_lock(&mutex); | |
548 | time = timer_to_ms(stp->bridge_hello_time); | |
549 | ovs_mutex_unlock(&mutex); | |
550 | return time; | |
829a7d02 JP |
551 | } |
552 | ||
553 | /* Returns the bridge max age, in ms. The returned value is not necessarily | |
554 | * the value passed to stp_set_max_age(): it is clamped to the valid range, | |
555 | * quantized to the STP timer resolution, and adjusted to match the constraints | |
556 | * due to the hello time. */ | |
557 | int | |
558 | stp_get_max_age(const struct stp *stp) | |
559 | { | |
bd54dbcd EJ |
560 | int time; |
561 | ||
562 | ovs_mutex_lock(&mutex); | |
563 | time = timer_to_ms(stp->bridge_max_age); | |
564 | ovs_mutex_unlock(&mutex); | |
565 | return time; | |
829a7d02 JP |
566 | } |
567 | ||
568 | /* Returns the bridge forward delay, in ms. The returned value is not | |
569 | * necessarily the value passed to stp_set_forward_delay(): it is clamped to | |
570 | * the valid range, quantized to the STP timer resolution, and adjusted to | |
571 | * match the constraints due to the forward delay. */ | |
572 | int | |
573 | stp_get_forward_delay(const struct stp *stp) | |
574 | { | |
bd54dbcd EJ |
575 | int time; |
576 | ||
577 | ovs_mutex_lock(&mutex); | |
578 | time = timer_to_ms(stp->bridge_forward_delay); | |
579 | ovs_mutex_unlock(&mutex); | |
580 | return time; | |
829a7d02 JP |
581 | } |
582 | ||
6ae50723 EJ |
583 | /* Returns true if something has happened to 'stp' which necessitates flushing |
584 | * the client's MAC learning table. Calling this function resets 'stp' so that | |
585 | * future calls will return false until flushing is required again. */ | |
586 | bool | |
587 | stp_check_and_reset_fdb_flush(struct stp *stp) | |
588 | { | |
bd54dbcd EJ |
589 | bool needs_flush; |
590 | ||
591 | ovs_mutex_lock(&mutex); | |
592 | needs_flush = stp->fdb_needs_flush; | |
6ae50723 | 593 | stp->fdb_needs_flush = false; |
bd54dbcd | 594 | ovs_mutex_unlock(&mutex); |
6ae50723 EJ |
595 | return needs_flush; |
596 | } | |
597 | ||
829a7d02 JP |
598 | /* Returns the port in 'stp' with index 'port_no', which must be between 0 and |
599 | * STP_MAX_PORTS. */ | |
600 | struct stp_port * | |
601 | stp_get_port(struct stp *stp, int port_no) | |
602 | { | |
bd54dbcd EJ |
603 | struct stp_port *port; |
604 | ||
605 | ovs_mutex_lock(&mutex); | |
cb22974d | 606 | ovs_assert(port_no >= 0 && port_no < ARRAY_SIZE(stp->ports)); |
bd54dbcd EJ |
607 | port = &stp->ports[port_no]; |
608 | ovs_mutex_unlock(&mutex); | |
609 | return port; | |
829a7d02 JP |
610 | } |
611 | ||
612 | /* Returns the port connecting 'stp' to the root bridge, or a null pointer if | |
613 | * there is no such port. */ | |
614 | struct stp_port * | |
615 | stp_get_root_port(struct stp *stp) | |
616 | { | |
bd54dbcd EJ |
617 | struct stp_port *port; |
618 | ||
619 | ovs_mutex_lock(&mutex); | |
620 | port = stp->root_port; | |
621 | ovs_mutex_unlock(&mutex); | |
622 | return port; | |
829a7d02 JP |
623 | } |
624 | ||
625 | /* Finds a port whose state has changed. If successful, stores the port whose | |
626 | * state changed in '*portp' and returns true. If no port has changed, stores | |
627 | * NULL in '*portp' and returns false. */ | |
628 | bool | |
629 | stp_get_changed_port(struct stp *stp, struct stp_port **portp) | |
630 | { | |
bd54dbcd EJ |
631 | struct stp_port *end, *p; |
632 | bool changed = false; | |
829a7d02 | 633 | |
bd54dbcd EJ |
634 | ovs_mutex_lock(&mutex); |
635 | end = &stp->ports[ARRAY_SIZE(stp->ports)]; | |
829a7d02 JP |
636 | for (p = stp->first_changed_port; p < end; p++) { |
637 | if (p->state_changed) { | |
638 | p->state_changed = false; | |
639 | stp->first_changed_port = p + 1; | |
640 | *portp = p; | |
bd54dbcd EJ |
641 | changed = true; |
642 | goto out; | |
829a7d02 JP |
643 | } |
644 | } | |
645 | stp->first_changed_port = end; | |
646 | *portp = NULL; | |
bd54dbcd EJ |
647 | |
648 | out: | |
649 | ovs_mutex_unlock(&mutex); | |
650 | return changed; | |
829a7d02 JP |
651 | } |
652 | ||
653 | /* Returns the name for the given 'state' (for use in debugging and log | |
654 | * messages). */ | |
655 | const char * | |
656 | stp_state_name(enum stp_state state) | |
657 | { | |
658 | switch (state) { | |
659 | case STP_DISABLED: | |
660 | return "disabled"; | |
661 | case STP_LISTENING: | |
662 | return "listening"; | |
663 | case STP_LEARNING: | |
664 | return "learning"; | |
665 | case STP_FORWARDING: | |
666 | return "forwarding"; | |
667 | case STP_BLOCKING: | |
668 | return "blocking"; | |
669 | default: | |
428b2edd | 670 | OVS_NOT_REACHED(); |
829a7d02 JP |
671 | } |
672 | } | |
673 | ||
674 | /* Returns true if 'state' is one in which packets received on a port should | |
675 | * be forwarded, false otherwise. | |
4b5f1996 | 676 | */ |
829a7d02 JP |
677 | bool |
678 | stp_forward_in_state(enum stp_state state) | |
679 | { | |
4b5f1996 | 680 | return (state & STP_FORWARDING) != 0; |
829a7d02 JP |
681 | } |
682 | ||
683 | /* Returns true if 'state' is one in which MAC learning should be done on | |
684 | * packets received on a port, false otherwise. | |
4b5f1996 | 685 | */ |
829a7d02 JP |
686 | bool |
687 | stp_learn_in_state(enum stp_state state) | |
688 | { | |
4b5f1996 | 689 | return (state & (STP_LEARNING | STP_FORWARDING)) != 0; |
829a7d02 JP |
690 | } |
691 | ||
bacdb85a AW |
692 | /* Returns true if 'state' is one in which bpdus should be forwarded on a |
693 | * port, false otherwise. | |
694 | * | |
695 | * Returns true if 'state' is STP_DISABLED, since in that case the port does | |
696 | * not generate the bpdu and should just forward it (e.g. patch port on pif | |
697 | * bridge). */ | |
0d1cee12 | 698 | bool |
bacdb85a | 699 | stp_should_forward_bpdu(enum stp_state state) |
0d1cee12 K |
700 | { |
701 | return (state & | |
bacdb85a AW |
702 | ( STP_DISABLED | STP_LISTENING | STP_LEARNING |
703 | | STP_FORWARDING)) != 0; | |
0d1cee12 K |
704 | } |
705 | ||
3310b570 JP |
706 | /* Returns the name for the given 'role' (for use in debugging and log |
707 | * messages). */ | |
708 | const char * | |
709 | stp_role_name(enum stp_role role) | |
710 | { | |
711 | switch (role) { | |
712 | case STP_ROLE_ROOT: | |
713 | return "root"; | |
714 | case STP_ROLE_DESIGNATED: | |
715 | return "designated"; | |
716 | case STP_ROLE_ALTERNATE: | |
717 | return "alternate"; | |
718 | case STP_ROLE_DISABLED: | |
719 | return "disabled"; | |
720 | default: | |
428b2edd | 721 | OVS_NOT_REACHED(); |
3310b570 JP |
722 | } |
723 | } | |
724 | ||
829a7d02 JP |
725 | /* Notifies the STP entity that bridge protocol data unit 'bpdu', which is |
726 | * 'bpdu_size' bytes in length, was received on port 'p'. | |
727 | * | |
728 | * This function may call the 'send_bpdu' function provided to stp_create(). */ | |
729 | void | |
730 | stp_received_bpdu(struct stp_port *p, const void *bpdu, size_t bpdu_size) | |
731 | { | |
732 | struct stp *stp = p->stp; | |
733 | const struct stp_bpdu_header *header; | |
734 | ||
bd54dbcd | 735 | ovs_mutex_lock(&mutex); |
829a7d02 | 736 | if (p->state == STP_DISABLED) { |
bd54dbcd | 737 | goto out; |
829a7d02 JP |
738 | } |
739 | ||
740 | if (bpdu_size < sizeof(struct stp_bpdu_header)) { | |
34582733 | 741 | VLOG_WARN("%s: received runt %"PRIuSIZE"-byte BPDU", stp->name, bpdu_size); |
80740385 | 742 | p->error_count++; |
bd54dbcd | 743 | goto out; |
829a7d02 JP |
744 | } |
745 | ||
746 | header = bpdu; | |
747 | if (header->protocol_id != htons(STP_PROTOCOL_ID)) { | |
748 | VLOG_WARN("%s: received BPDU with unexpected protocol ID %"PRIu16, | |
749 | stp->name, ntohs(header->protocol_id)); | |
80740385 | 750 | p->error_count++; |
bd54dbcd | 751 | goto out; |
829a7d02 JP |
752 | } |
753 | if (header->protocol_version != STP_PROTOCOL_VERSION) { | |
754 | VLOG_DBG("%s: received BPDU with unexpected protocol version %"PRIu8, | |
755 | stp->name, header->protocol_version); | |
756 | } | |
757 | ||
758 | switch (header->bpdu_type) { | |
759 | case STP_TYPE_CONFIG: | |
760 | if (bpdu_size < sizeof(struct stp_config_bpdu)) { | |
34582733 | 761 | VLOG_WARN("%s: received config BPDU with invalid size %"PRIuSIZE, |
829a7d02 | 762 | stp->name, bpdu_size); |
80740385 | 763 | p->error_count++; |
bd54dbcd | 764 | goto out; |
829a7d02 JP |
765 | } |
766 | stp_received_config_bpdu(stp, p, bpdu); | |
767 | break; | |
768 | ||
769 | case STP_TYPE_TCN: | |
770 | if (bpdu_size != sizeof(struct stp_tcn_bpdu)) { | |
34582733 | 771 | VLOG_WARN("%s: received TCN BPDU with invalid size %"PRIuSIZE, |
829a7d02 | 772 | stp->name, bpdu_size); |
80740385 | 773 | p->error_count++; |
bd54dbcd | 774 | goto out; |
829a7d02 JP |
775 | } |
776 | stp_received_tcn_bpdu(stp, p); | |
777 | break; | |
778 | ||
779 | default: | |
780 | VLOG_WARN("%s: received BPDU of unexpected type %"PRIu8, | |
781 | stp->name, header->bpdu_type); | |
80740385 | 782 | p->error_count++; |
bd54dbcd | 783 | goto out; |
829a7d02 | 784 | } |
80740385 | 785 | p->rx_count++; |
bd54dbcd EJ |
786 | |
787 | out: | |
788 | ovs_mutex_unlock(&mutex); | |
829a7d02 JP |
789 | } |
790 | ||
791 | /* Returns the STP entity in which 'p' is nested. */ | |
792 | struct stp * | |
793 | stp_port_get_stp(struct stp_port *p) | |
794 | { | |
bd54dbcd EJ |
795 | struct stp *stp; |
796 | ||
797 | ovs_mutex_lock(&mutex); | |
798 | stp = p->stp; | |
799 | ovs_mutex_unlock(&mutex); | |
800 | return stp; | |
829a7d02 JP |
801 | } |
802 | ||
11306274 AW |
803 | void |
804 | stp_port_set_name(struct stp_port *p, const char *name) | |
805 | { | |
806 | char *old; | |
807 | ||
808 | ovs_mutex_lock(&mutex); | |
809 | old = p->port_name; | |
810 | p->port_name = xstrdup(name); | |
811 | free(old); | |
812 | ovs_mutex_unlock(&mutex); | |
813 | } | |
814 | ||
3310b570 JP |
815 | /* Sets the 'aux' member of 'p'. |
816 | * | |
817 | * The 'aux' member will be reset to NULL when stp_port_disable() is | |
818 | * called or stp_port_enable() is called when the port is in a Disabled | |
819 | * state. */ | |
820 | void | |
821 | stp_port_set_aux(struct stp_port *p, void *aux) | |
822 | { | |
bd54dbcd | 823 | ovs_mutex_lock(&mutex); |
3310b570 | 824 | p->aux = aux; |
bd54dbcd | 825 | ovs_mutex_unlock(&mutex); |
3310b570 JP |
826 | } |
827 | ||
828 | /* Returns the 'aux' member of 'p'. */ | |
829 | void * | |
830 | stp_port_get_aux(struct stp_port *p) | |
831 | { | |
bd54dbcd EJ |
832 | void *aux; |
833 | ||
834 | ovs_mutex_lock(&mutex); | |
835 | aux = p->aux; | |
836 | ovs_mutex_unlock(&mutex); | |
837 | return aux; | |
3310b570 JP |
838 | } |
839 | ||
829a7d02 JP |
840 | /* Returns the index of port 'p' within its bridge. */ |
841 | int | |
842 | stp_port_no(const struct stp_port *p) | |
843 | { | |
bd54dbcd EJ |
844 | struct stp *stp; |
845 | int index; | |
846 | ||
847 | ovs_mutex_lock(&mutex); | |
848 | stp = p->stp; | |
cb22974d | 849 | ovs_assert(p >= stp->ports && p < &stp->ports[ARRAY_SIZE(stp->ports)]); |
bd54dbcd EJ |
850 | index = p - p->stp->ports; |
851 | ovs_mutex_unlock(&mutex); | |
852 | return index; | |
829a7d02 JP |
853 | } |
854 | ||
3310b570 JP |
855 | /* Returns the port ID for 'p'. */ |
856 | int | |
857 | stp_port_get_id(const struct stp_port *p) | |
858 | { | |
bd54dbcd EJ |
859 | int port_id; |
860 | ||
861 | ovs_mutex_lock(&mutex); | |
862 | port_id = p->port_id; | |
863 | ovs_mutex_unlock(&mutex); | |
864 | return port_id; | |
3310b570 JP |
865 | } |
866 | ||
829a7d02 JP |
867 | /* Returns the state of port 'p'. */ |
868 | enum stp_state | |
869 | stp_port_get_state(const struct stp_port *p) | |
870 | { | |
bd54dbcd EJ |
871 | enum stp_state state; |
872 | ||
873 | ovs_mutex_lock(&mutex); | |
874 | state = p->state; | |
875 | ovs_mutex_unlock(&mutex); | |
876 | return state; | |
829a7d02 JP |
877 | } |
878 | ||
3310b570 JP |
879 | /* Returns the role of port 'p'. */ |
880 | enum stp_role | |
881 | stp_port_get_role(const struct stp_port *p) | |
882 | { | |
bd54dbcd EJ |
883 | struct stp_port *root_port; |
884 | enum stp_role role; | |
3310b570 | 885 | |
bd54dbcd EJ |
886 | ovs_mutex_lock(&mutex); |
887 | root_port = p->stp->root_port; | |
3310b570 | 888 | if (root_port && root_port->port_id == p->port_id) { |
bd54dbcd | 889 | role = STP_ROLE_ROOT; |
3310b570 | 890 | } else if (stp_is_designated_port(p)) { |
bd54dbcd | 891 | role = STP_ROLE_DESIGNATED; |
3310b570 | 892 | } else if (p->state == STP_DISABLED) { |
bd54dbcd | 893 | role = STP_ROLE_DISABLED; |
3310b570 | 894 | } else { |
bd54dbcd | 895 | role = STP_ROLE_ALTERNATE; |
3310b570 | 896 | } |
bd54dbcd EJ |
897 | ovs_mutex_unlock(&mutex); |
898 | return role; | |
3310b570 JP |
899 | } |
900 | ||
80740385 | 901 | /* Retrieves BPDU transmit and receive counts for 'p'. */ |
bd54dbcd EJ |
902 | void |
903 | stp_port_get_counts(const struct stp_port *p, | |
904 | int *tx_count, int *rx_count, int *error_count) | |
80740385 | 905 | { |
bd54dbcd | 906 | ovs_mutex_lock(&mutex); |
80740385 JP |
907 | *tx_count = p->tx_count; |
908 | *rx_count = p->rx_count; | |
909 | *error_count = p->error_count; | |
bd54dbcd | 910 | ovs_mutex_unlock(&mutex); |
80740385 JP |
911 | } |
912 | ||
829a7d02 JP |
913 | /* Disables STP on port 'p'. */ |
914 | void | |
915 | stp_port_disable(struct stp_port *p) | |
916 | { | |
bd54dbcd EJ |
917 | struct stp *stp; |
918 | ||
919 | ovs_mutex_lock(&mutex); | |
920 | stp = p->stp; | |
829a7d02 JP |
921 | if (p->state != STP_DISABLED) { |
922 | bool root = stp_is_root_bridge(stp); | |
923 | stp_become_designated_port(p); | |
924 | stp_set_port_state(p, STP_DISABLED); | |
925 | p->topology_change_ack = false; | |
926 | p->config_pending = false; | |
927 | stp_stop_timer(&p->message_age_timer); | |
928 | stp_stop_timer(&p->forward_delay_timer); | |
929 | stp_configuration_update(stp); | |
930 | stp_port_state_selection(stp); | |
931 | if (stp_is_root_bridge(stp) && !root) { | |
932 | stp_become_root_bridge(stp); | |
933 | } | |
3310b570 | 934 | p->aux = NULL; |
829a7d02 | 935 | } |
bd54dbcd | 936 | ovs_mutex_unlock(&mutex); |
829a7d02 JP |
937 | } |
938 | ||
939 | /* Enables STP on port 'p'. The port will initially be in "blocking" state. */ | |
940 | void | |
941 | stp_port_enable(struct stp_port *p) | |
942 | { | |
bd54dbcd | 943 | ovs_mutex_lock(&mutex); |
829a7d02 JP |
944 | if (p->state == STP_DISABLED) { |
945 | stp_initialize_port(p, STP_BLOCKING); | |
946 | stp_port_state_selection(p->stp); | |
947 | } | |
bd54dbcd | 948 | ovs_mutex_unlock(&mutex); |
829a7d02 JP |
949 | } |
950 | ||
951 | /* Sets the priority of port 'p' to 'new_priority'. Lower numerical values | |
952 | * are interpreted as higher priorities. */ | |
953 | void | |
954 | stp_port_set_priority(struct stp_port *p, uint8_t new_priority) | |
955 | { | |
bd54dbcd EJ |
956 | uint16_t new_port_id; |
957 | ||
958 | ovs_mutex_lock(&mutex); | |
959 | new_port_id = (p->port_id & 0xff) | (new_priority << 8); | |
829a7d02 JP |
960 | if (p->port_id != new_port_id) { |
961 | struct stp *stp = p->stp; | |
962 | if (stp_is_designated_port(p)) { | |
963 | p->designated_port = new_port_id; | |
964 | } | |
965 | p->port_id = new_port_id; | |
966 | if (stp->bridge_id == p->designated_bridge | |
967 | && p->port_id < p->designated_port) { | |
968 | stp_become_designated_port(p); | |
969 | stp_port_state_selection(stp); | |
970 | } | |
971 | } | |
bd54dbcd | 972 | ovs_mutex_unlock(&mutex); |
829a7d02 JP |
973 | } |
974 | ||
3310b570 JP |
975 | /* Convert 'speed' (measured in Mb/s) into the path cost. */ |
976 | uint16_t | |
977 | stp_convert_speed_to_cost(unsigned int speed) | |
978 | { | |
bd54dbcd EJ |
979 | uint16_t ret; |
980 | ||
981 | ovs_mutex_lock(&mutex); | |
982 | ret = speed >= 10000 ? 2 /* 10 Gb/s. */ | |
983 | : speed >= 1000 ? 4 /* 1 Gb/s. */ | |
984 | : speed >= 100 ? 19 /* 100 Mb/s. */ | |
985 | : speed >= 16 ? 62 /* 16 Mb/s. */ | |
986 | : speed >= 10 ? 100 /* 10 Mb/s. */ | |
987 | : speed >= 4 ? 250 /* 4 Mb/s. */ | |
988 | : 19; /* 100 Mb/s (guess). */ | |
989 | ovs_mutex_unlock(&mutex); | |
990 | return ret; | |
3310b570 JP |
991 | } |
992 | ||
829a7d02 JP |
993 | /* Sets the path cost of port 'p' to 'path_cost'. Lower values are generally |
994 | * used to indicate faster links. Use stp_port_set_speed() to automatically | |
995 | * generate a default path cost from a link speed. */ | |
996 | void | |
997 | stp_port_set_path_cost(struct stp_port *p, uint16_t path_cost) | |
998 | { | |
bd54dbcd | 999 | ovs_mutex_lock(&mutex); |
829a7d02 JP |
1000 | if (p->path_cost != path_cost) { |
1001 | struct stp *stp = p->stp; | |
1002 | p->path_cost = path_cost; | |
1003 | stp_configuration_update(stp); | |
1004 | stp_port_state_selection(stp); | |
1005 | } | |
bd54dbcd | 1006 | ovs_mutex_unlock(&mutex); |
829a7d02 JP |
1007 | } |
1008 | ||
1009 | /* Sets the path cost of port 'p' based on 'speed' (measured in Mb/s). */ | |
1010 | void | |
1011 | stp_port_set_speed(struct stp_port *p, unsigned int speed) | |
1012 | { | |
3310b570 | 1013 | stp_port_set_path_cost(p, stp_convert_speed_to_cost(speed)); |
829a7d02 JP |
1014 | } |
1015 | ||
1016 | /* Enables topology change detection on port 'p'. */ | |
1017 | void | |
1018 | stp_port_enable_change_detection(struct stp_port *p) | |
1019 | { | |
1020 | p->change_detection_enabled = true; | |
1021 | } | |
1022 | ||
1023 | /* Disables topology change detection on port 'p'. */ | |
1024 | void | |
1025 | stp_port_disable_change_detection(struct stp_port *p) | |
1026 | { | |
1027 | p->change_detection_enabled = false; | |
1028 | } | |
1029 | \f | |
1030 | static void | |
bd3950dd | 1031 | stp_transmit_config(struct stp_port *p) OVS_REQUIRES(mutex) |
829a7d02 JP |
1032 | { |
1033 | struct stp *stp = p->stp; | |
1034 | bool root = stp_is_root_bridge(stp); | |
1035 | if (!root && !stp->root_port) { | |
1036 | return; | |
1037 | } | |
1038 | if (p->hold_timer.active) { | |
11306274 AW |
1039 | VLOG_DBG_RL(&stp_rl, "bridge: %s, port: %s, transmit config bpdu pending", |
1040 | stp->name, p->port_name); | |
829a7d02 JP |
1041 | p->config_pending = true; |
1042 | } else { | |
1043 | struct stp_config_bpdu config; | |
1044 | memset(&config, 0, sizeof config); | |
1045 | config.header.protocol_id = htons(STP_PROTOCOL_ID); | |
1046 | config.header.protocol_version = STP_PROTOCOL_VERSION; | |
1047 | config.header.bpdu_type = STP_TYPE_CONFIG; | |
1048 | config.flags = 0; | |
1049 | if (p->topology_change_ack) { | |
3310b570 | 1050 | config.flags |= STP_CONFIG_TOPOLOGY_CHANGE_ACK; |
829a7d02 JP |
1051 | } |
1052 | if (stp->topology_change) { | |
3310b570 | 1053 | config.flags |= STP_CONFIG_TOPOLOGY_CHANGE; |
829a7d02 JP |
1054 | } |
1055 | config.root_id = htonll(stp->designated_root); | |
1056 | config.root_path_cost = htonl(stp->root_path_cost); | |
1057 | config.bridge_id = htonll(stp->bridge_id); | |
1058 | config.port_id = htons(p->port_id); | |
1059 | if (root) { | |
1060 | config.message_age = htons(0); | |
1061 | } else { | |
1062 | config.message_age = htons(stp->root_port->message_age_timer.value | |
1063 | + MESSAGE_AGE_INCREMENT); | |
1064 | } | |
1065 | config.max_age = htons(stp->max_age); | |
1066 | config.hello_time = htons(stp->hello_time); | |
1067 | config.forward_delay = htons(stp->forward_delay); | |
1068 | if (ntohs(config.message_age) < stp->max_age) { | |
1069 | p->topology_change_ack = false; | |
1070 | p->config_pending = false; | |
11306274 AW |
1071 | VLOG_DBG_RL(&stp_rl, "bridge: %s, port: %s, transmit config bpdu", |
1072 | stp->name, p->port_name); | |
829a7d02 JP |
1073 | stp_send_bpdu(p, &config, sizeof config); |
1074 | stp_start_timer(&p->hold_timer, 0); | |
1075 | } | |
1076 | } | |
1077 | } | |
1078 | ||
1079 | static bool | |
1080 | stp_supersedes_port_info(const struct stp_port *p, | |
1081 | const struct stp_config_bpdu *config) | |
bd3950dd | 1082 | OVS_REQUIRES(mutex) |
829a7d02 JP |
1083 | { |
1084 | if (ntohll(config->root_id) != p->designated_root) { | |
1085 | return ntohll(config->root_id) < p->designated_root; | |
1086 | } else if (ntohl(config->root_path_cost) != p->designated_cost) { | |
1087 | return ntohl(config->root_path_cost) < p->designated_cost; | |
1088 | } else if (ntohll(config->bridge_id) != p->designated_bridge) { | |
1089 | return ntohll(config->bridge_id) < p->designated_bridge; | |
1090 | } else { | |
1091 | return (ntohll(config->bridge_id) != p->stp->bridge_id | |
1092 | || ntohs(config->port_id) <= p->designated_port); | |
1093 | } | |
1094 | } | |
1095 | ||
1096 | static void | |
1097 | stp_record_config_information(struct stp_port *p, | |
1098 | const struct stp_config_bpdu *config) | |
bd3950dd | 1099 | OVS_REQUIRES(mutex) |
829a7d02 JP |
1100 | { |
1101 | p->designated_root = ntohll(config->root_id); | |
1102 | p->designated_cost = ntohl(config->root_path_cost); | |
1103 | p->designated_bridge = ntohll(config->bridge_id); | |
1104 | p->designated_port = ntohs(config->port_id); | |
1105 | stp_start_timer(&p->message_age_timer, ntohs(config->message_age)); | |
1106 | } | |
1107 | ||
1108 | static void | |
1109 | stp_record_config_timeout_values(struct stp *stp, | |
1110 | const struct stp_config_bpdu *config) | |
bd3950dd | 1111 | OVS_REQUIRES(mutex) |
829a7d02 JP |
1112 | { |
1113 | stp->max_age = ntohs(config->max_age); | |
1114 | stp->hello_time = ntohs(config->hello_time); | |
1115 | stp->forward_delay = ntohs(config->forward_delay); | |
3310b570 | 1116 | stp->topology_change = config->flags & STP_CONFIG_TOPOLOGY_CHANGE; |
829a7d02 JP |
1117 | } |
1118 | ||
1119 | static bool | |
bd3950dd | 1120 | stp_is_designated_port(const struct stp_port *p) OVS_REQUIRES(mutex) |
829a7d02 JP |
1121 | { |
1122 | return (p->designated_bridge == p->stp->bridge_id | |
1123 | && p->designated_port == p->port_id); | |
1124 | } | |
1125 | ||
1126 | static void | |
bd3950dd | 1127 | stp_config_bpdu_generation(struct stp *stp) OVS_REQUIRES(mutex) |
829a7d02 JP |
1128 | { |
1129 | struct stp_port *p; | |
1130 | ||
1131 | FOR_EACH_ENABLED_PORT (p, stp) { | |
1132 | if (stp_is_designated_port(p)) { | |
1133 | stp_transmit_config(p); | |
1134 | } | |
1135 | } | |
1136 | } | |
1137 | ||
1138 | static void | |
bd3950dd | 1139 | stp_transmit_tcn(struct stp *stp) OVS_REQUIRES(mutex) |
829a7d02 JP |
1140 | { |
1141 | struct stp_port *p = stp->root_port; | |
1142 | struct stp_tcn_bpdu tcn_bpdu; | |
11306274 | 1143 | |
829a7d02 JP |
1144 | if (!p) { |
1145 | return; | |
1146 | } | |
11306274 AW |
1147 | VLOG_DBG_RL(&stp_rl, "bridge: %s, root port: %s, transmit tcn", stp->name, |
1148 | p->port_name); | |
829a7d02 JP |
1149 | tcn_bpdu.header.protocol_id = htons(STP_PROTOCOL_ID); |
1150 | tcn_bpdu.header.protocol_version = STP_PROTOCOL_VERSION; | |
1151 | tcn_bpdu.header.bpdu_type = STP_TYPE_TCN; | |
1152 | stp_send_bpdu(p, &tcn_bpdu, sizeof tcn_bpdu); | |
1153 | } | |
1154 | ||
1155 | static void | |
bd3950dd | 1156 | stp_configuration_update(struct stp *stp) OVS_REQUIRES(mutex) |
829a7d02 JP |
1157 | { |
1158 | stp_root_selection(stp); | |
1159 | stp_designated_port_selection(stp); | |
f23d157c | 1160 | seq_change(connectivity_seq_get()); |
829a7d02 JP |
1161 | } |
1162 | ||
1163 | static bool | |
1164 | stp_supersedes_root(const struct stp_port *root, const struct stp_port *p) | |
bd3950dd | 1165 | OVS_REQUIRES(mutex) |
829a7d02 JP |
1166 | { |
1167 | int p_cost = p->designated_cost + p->path_cost; | |
1168 | int root_cost = root->designated_cost + root->path_cost; | |
1169 | ||
1170 | if (p->designated_root != root->designated_root) { | |
1171 | return p->designated_root < root->designated_root; | |
1172 | } else if (p_cost != root_cost) { | |
1173 | return p_cost < root_cost; | |
1174 | } else if (p->designated_bridge != root->designated_bridge) { | |
1175 | return p->designated_bridge < root->designated_bridge; | |
1176 | } else if (p->designated_port != root->designated_port) { | |
1177 | return p->designated_port < root->designated_port; | |
1178 | } else { | |
1179 | return p->port_id < root->port_id; | |
1180 | } | |
1181 | } | |
1182 | ||
1183 | static void | |
bd3950dd | 1184 | stp_root_selection(struct stp *stp) OVS_REQUIRES(mutex) |
829a7d02 JP |
1185 | { |
1186 | struct stp_port *p, *root; | |
1187 | ||
1188 | root = NULL; | |
1189 | FOR_EACH_ENABLED_PORT (p, stp) { | |
1190 | if (stp_is_designated_port(p) | |
1191 | || p->designated_root >= stp->bridge_id) { | |
1192 | continue; | |
1193 | } | |
1194 | if (root && !stp_supersedes_root(root, p)) { | |
1195 | continue; | |
1196 | } | |
1197 | root = p; | |
1198 | } | |
1199 | stp->root_port = root; | |
1200 | if (!root) { | |
1201 | stp->designated_root = stp->bridge_id; | |
1202 | stp->root_path_cost = 0; | |
1203 | } else { | |
1204 | stp->designated_root = root->designated_root; | |
1205 | stp->root_path_cost = root->designated_cost + root->path_cost; | |
1206 | } | |
1207 | } | |
1208 | ||
1209 | static void | |
bd3950dd | 1210 | stp_designated_port_selection(struct stp *stp) OVS_REQUIRES(mutex) |
829a7d02 JP |
1211 | { |
1212 | struct stp_port *p; | |
1213 | ||
1214 | FOR_EACH_ENABLED_PORT (p, stp) { | |
1215 | if (stp_is_designated_port(p) | |
1216 | || p->designated_root != stp->designated_root | |
1217 | || stp->root_path_cost < p->designated_cost | |
1218 | || (stp->root_path_cost == p->designated_cost | |
1219 | && (stp->bridge_id < p->designated_bridge | |
1220 | || (stp->bridge_id == p->designated_bridge | |
1221 | && p->port_id <= p->designated_port)))) | |
1222 | { | |
1223 | stp_become_designated_port(p); | |
1224 | } | |
1225 | } | |
1226 | } | |
1227 | ||
1228 | static void | |
bd3950dd | 1229 | stp_become_designated_port(struct stp_port *p) OVS_REQUIRES(mutex) |
829a7d02 JP |
1230 | { |
1231 | struct stp *stp = p->stp; | |
1232 | p->designated_root = stp->designated_root; | |
1233 | p->designated_cost = stp->root_path_cost; | |
1234 | p->designated_bridge = stp->bridge_id; | |
1235 | p->designated_port = p->port_id; | |
1236 | } | |
1237 | ||
1238 | static void | |
bd3950dd | 1239 | stp_port_state_selection(struct stp *stp) OVS_REQUIRES(mutex) |
829a7d02 JP |
1240 | { |
1241 | struct stp_port *p; | |
1242 | ||
1243 | FOR_EACH_ENABLED_PORT (p, stp) { | |
1244 | if (p == stp->root_port) { | |
1245 | p->config_pending = false; | |
1246 | p->topology_change_ack = false; | |
1247 | stp_make_forwarding(p); | |
1248 | } else if (stp_is_designated_port(p)) { | |
1249 | stp_stop_timer(&p->message_age_timer); | |
1250 | stp_make_forwarding(p); | |
1251 | } else { | |
1252 | p->config_pending = false; | |
1253 | p->topology_change_ack = false; | |
1254 | stp_make_blocking(p); | |
1255 | } | |
1256 | } | |
1257 | } | |
1258 | ||
1259 | static void | |
bd3950dd | 1260 | stp_make_forwarding(struct stp_port *p) OVS_REQUIRES(mutex) |
829a7d02 JP |
1261 | { |
1262 | if (p->state == STP_BLOCKING) { | |
1263 | stp_set_port_state(p, STP_LISTENING); | |
1264 | stp_start_timer(&p->forward_delay_timer, 0); | |
1265 | } | |
1266 | } | |
1267 | ||
1268 | static void | |
bd3950dd | 1269 | stp_make_blocking(struct stp_port *p) OVS_REQUIRES(mutex) |
829a7d02 JP |
1270 | { |
1271 | if (!(p->state & (STP_DISABLED | STP_BLOCKING))) { | |
1272 | if (p->state & (STP_FORWARDING | STP_LEARNING)) { | |
1273 | if (p->change_detection_enabled) { | |
1274 | stp_topology_change_detection(p->stp); | |
1275 | } | |
1276 | } | |
1277 | stp_set_port_state(p, STP_BLOCKING); | |
1278 | stp_stop_timer(&p->forward_delay_timer); | |
1279 | } | |
1280 | } | |
1281 | ||
1282 | static void | |
1283 | stp_set_port_state(struct stp_port *p, enum stp_state state) | |
bd3950dd | 1284 | OVS_REQUIRES(mutex) |
829a7d02 JP |
1285 | { |
1286 | if (state != p->state && !p->state_changed) { | |
1287 | p->state_changed = true; | |
1288 | if (p < p->stp->first_changed_port) { | |
1289 | p->stp->first_changed_port = p; | |
1290 | } | |
f23d157c | 1291 | seq_change(connectivity_seq_get()); |
829a7d02 JP |
1292 | } |
1293 | p->state = state; | |
1294 | } | |
1295 | ||
1296 | static void | |
bd3950dd | 1297 | stp_topology_change_detection(struct stp *stp) OVS_REQUIRES(mutex) |
829a7d02 | 1298 | { |
634408e0 EJ |
1299 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
1300 | ||
829a7d02 JP |
1301 | if (stp_is_root_bridge(stp)) { |
1302 | stp->topology_change = true; | |
1303 | stp_start_timer(&stp->topology_change_timer, 0); | |
1304 | } else if (!stp->topology_change_detected) { | |
1305 | stp_transmit_tcn(stp); | |
1306 | stp_start_timer(&stp->tcn_timer, 0); | |
1307 | } | |
6ae50723 | 1308 | stp->fdb_needs_flush = true; |
829a7d02 | 1309 | stp->topology_change_detected = true; |
f23d157c | 1310 | seq_change(connectivity_seq_get()); |
634408e0 | 1311 | VLOG_INFO_RL(&rl, "%s: detected topology change.", stp->name); |
829a7d02 JP |
1312 | } |
1313 | ||
1314 | static void | |
bd3950dd | 1315 | stp_topology_change_acknowledged(struct stp *stp) OVS_REQUIRES(mutex) |
829a7d02 JP |
1316 | { |
1317 | stp->topology_change_detected = false; | |
1318 | stp_stop_timer(&stp->tcn_timer); | |
1319 | } | |
1320 | ||
1321 | static void | |
bd3950dd | 1322 | stp_acknowledge_topology_change(struct stp_port *p) OVS_REQUIRES(mutex) |
829a7d02 JP |
1323 | { |
1324 | p->topology_change_ack = true; | |
1325 | stp_transmit_config(p); | |
1326 | } | |
1327 | ||
1328 | static void | |
1329 | stp_received_config_bpdu(struct stp *stp, struct stp_port *p, | |
1330 | const struct stp_config_bpdu *config) | |
bd3950dd | 1331 | OVS_REQUIRES(mutex) |
829a7d02 JP |
1332 | { |
1333 | if (ntohs(config->message_age) >= ntohs(config->max_age)) { | |
1334 | VLOG_WARN("%s: received config BPDU with message age (%u) greater " | |
1335 | "than max age (%u)", | |
1336 | stp->name, | |
1337 | ntohs(config->message_age), ntohs(config->max_age)); | |
1338 | return; | |
1339 | } | |
1340 | if (p->state != STP_DISABLED) { | |
1341 | bool root = stp_is_root_bridge(stp); | |
1342 | if (stp_supersedes_port_info(p, config)) { | |
1343 | stp_record_config_information(p, config); | |
1344 | stp_configuration_update(stp); | |
1345 | stp_port_state_selection(stp); | |
1346 | if (!stp_is_root_bridge(stp) && root) { | |
1347 | stp_stop_timer(&stp->hello_timer); | |
1348 | if (stp->topology_change_detected) { | |
1349 | stp_stop_timer(&stp->topology_change_timer); | |
1350 | stp_transmit_tcn(stp); | |
1351 | stp_start_timer(&stp->tcn_timer, 0); | |
1352 | } | |
1353 | } | |
1354 | if (p == stp->root_port) { | |
1355 | stp_record_config_timeout_values(stp, config); | |
1356 | stp_config_bpdu_generation(stp); | |
3310b570 | 1357 | if (config->flags & STP_CONFIG_TOPOLOGY_CHANGE_ACK) { |
829a7d02 JP |
1358 | stp_topology_change_acknowledged(stp); |
1359 | } | |
6ae50723 EJ |
1360 | if (config->flags & STP_CONFIG_TOPOLOGY_CHANGE) { |
1361 | stp->fdb_needs_flush = true; | |
1362 | } | |
829a7d02 JP |
1363 | } |
1364 | } else if (stp_is_designated_port(p)) { | |
1365 | stp_transmit_config(p); | |
1366 | } | |
1367 | } | |
1368 | } | |
1369 | ||
1370 | static void | |
1371 | stp_received_tcn_bpdu(struct stp *stp, struct stp_port *p) | |
bd3950dd | 1372 | OVS_REQUIRES(mutex) |
829a7d02 JP |
1373 | { |
1374 | if (p->state != STP_DISABLED) { | |
1375 | if (stp_is_designated_port(p)) { | |
1376 | stp_topology_change_detection(stp); | |
1377 | stp_acknowledge_topology_change(p); | |
1378 | } | |
1379 | } | |
1380 | } | |
1381 | ||
1382 | static void | |
bd3950dd | 1383 | stp_hello_timer_expiry(struct stp *stp) OVS_REQUIRES(mutex) |
829a7d02 JP |
1384 | { |
1385 | stp_config_bpdu_generation(stp); | |
1386 | stp_start_timer(&stp->hello_timer, 0); | |
1387 | } | |
1388 | ||
1389 | static void | |
bd3950dd | 1390 | stp_message_age_timer_expiry(struct stp_port *p) OVS_REQUIRES(mutex) |
829a7d02 JP |
1391 | { |
1392 | struct stp *stp = p->stp; | |
1393 | bool root = stp_is_root_bridge(stp); | |
11306274 AW |
1394 | |
1395 | VLOG_DBG_RL(&stp_rl, "bridge: %s, port: %s, message age timer expired", | |
1396 | stp->name, p->port_name); | |
829a7d02 JP |
1397 | stp_become_designated_port(p); |
1398 | stp_configuration_update(stp); | |
1399 | stp_port_state_selection(stp); | |
1400 | if (stp_is_root_bridge(stp) && !root) { | |
1401 | stp->max_age = stp->bridge_max_age; | |
1402 | stp->hello_time = stp->bridge_hello_time; | |
1403 | stp->forward_delay = stp->bridge_forward_delay; | |
1404 | stp_topology_change_detection(stp); | |
1405 | stp_stop_timer(&stp->tcn_timer); | |
1406 | stp_config_bpdu_generation(stp); | |
1407 | stp_start_timer(&stp->hello_timer, 0); | |
1408 | } | |
1409 | } | |
1410 | ||
1411 | static bool | |
bd3950dd | 1412 | stp_is_designated_for_some_port(const struct stp *stp) OVS_REQUIRES(mutex) |
829a7d02 JP |
1413 | { |
1414 | const struct stp_port *p; | |
1415 | ||
1416 | FOR_EACH_ENABLED_PORT (p, stp) { | |
1417 | if (p->designated_bridge == stp->bridge_id) { | |
1418 | return true; | |
1419 | } | |
1420 | } | |
1421 | return false; | |
1422 | } | |
1423 | ||
1424 | static void | |
bd3950dd | 1425 | stp_forward_delay_timer_expiry(struct stp_port *p) OVS_REQUIRES(mutex) |
829a7d02 JP |
1426 | { |
1427 | if (p->state == STP_LISTENING) { | |
1428 | stp_set_port_state(p, STP_LEARNING); | |
1429 | stp_start_timer(&p->forward_delay_timer, 0); | |
1430 | } else if (p->state == STP_LEARNING) { | |
1431 | stp_set_port_state(p, STP_FORWARDING); | |
1432 | if (stp_is_designated_for_some_port(p->stp)) { | |
1433 | if (p->change_detection_enabled) { | |
1434 | stp_topology_change_detection(p->stp); | |
1435 | } | |
1436 | } | |
1437 | } | |
1438 | } | |
1439 | ||
1440 | static void | |
bd3950dd | 1441 | stp_tcn_timer_expiry(struct stp *stp) OVS_REQUIRES(mutex) |
829a7d02 JP |
1442 | { |
1443 | stp_transmit_tcn(stp); | |
1444 | stp_start_timer(&stp->tcn_timer, 0); | |
1445 | } | |
1446 | ||
1447 | static void | |
bd3950dd | 1448 | stp_topology_change_timer_expiry(struct stp *stp) OVS_REQUIRES(mutex) |
829a7d02 JP |
1449 | { |
1450 | stp->topology_change_detected = false; | |
1451 | stp->topology_change = false; | |
1452 | } | |
1453 | ||
1454 | static void | |
bd3950dd | 1455 | stp_hold_timer_expiry(struct stp_port *p) OVS_REQUIRES(mutex) |
829a7d02 JP |
1456 | { |
1457 | if (p->config_pending) { | |
1458 | stp_transmit_config(p); | |
1459 | } | |
1460 | } | |
1461 | ||
1462 | static void | |
1463 | stp_initialize_port(struct stp_port *p, enum stp_state state) | |
bd3950dd | 1464 | OVS_REQUIRES(mutex) |
829a7d02 | 1465 | { |
cb22974d | 1466 | ovs_assert(state & (STP_DISABLED | STP_BLOCKING)); |
829a7d02 | 1467 | stp_become_designated_port(p); |
fb20b0bf JR |
1468 | |
1469 | if (!p->state && state == STP_DISABLED) { | |
1470 | p->state = state; /* Do not trigger state change when initializing. */ | |
1471 | } else { | |
1472 | stp_set_port_state(p, state); | |
1473 | } | |
829a7d02 JP |
1474 | p->topology_change_ack = false; |
1475 | p->config_pending = false; | |
1476 | p->change_detection_enabled = true; | |
3310b570 | 1477 | p->aux = NULL; |
829a7d02 JP |
1478 | stp_stop_timer(&p->message_age_timer); |
1479 | stp_stop_timer(&p->forward_delay_timer); | |
1480 | stp_stop_timer(&p->hold_timer); | |
80740385 | 1481 | p->tx_count = p->rx_count = p->error_count = 0; |
829a7d02 JP |
1482 | } |
1483 | ||
1484 | static void | |
bd3950dd | 1485 | stp_become_root_bridge(struct stp *stp) OVS_REQUIRES(mutex) |
829a7d02 JP |
1486 | { |
1487 | stp->max_age = stp->bridge_max_age; | |
1488 | stp->hello_time = stp->bridge_hello_time; | |
1489 | stp->forward_delay = stp->bridge_forward_delay; | |
1490 | stp_topology_change_detection(stp); | |
1491 | stp_stop_timer(&stp->tcn_timer); | |
1492 | stp_config_bpdu_generation(stp); | |
1493 | stp_start_timer(&stp->hello_timer, 0); | |
1494 | } | |
1495 | ||
1496 | static void | |
bd3950dd | 1497 | stp_start_timer(struct stp_timer *timer, int value) OVS_REQUIRES(mutex) |
829a7d02 JP |
1498 | { |
1499 | timer->value = value; | |
1500 | timer->active = true; | |
1501 | } | |
1502 | ||
1503 | static void | |
bd3950dd | 1504 | stp_stop_timer(struct stp_timer *timer) OVS_REQUIRES(mutex) |
829a7d02 JP |
1505 | { |
1506 | timer->active = false; | |
1507 | } | |
1508 | ||
1509 | static bool | |
1510 | stp_timer_expired(struct stp_timer *timer, int elapsed, int timeout) | |
bd3950dd | 1511 | OVS_REQUIRES(mutex) |
829a7d02 JP |
1512 | { |
1513 | if (timer->active) { | |
1514 | timer->value += elapsed; | |
1515 | if (timer->value >= timeout) { | |
1516 | timer->active = false; | |
1517 | return true; | |
1518 | } | |
1519 | } | |
1520 | return false; | |
1521 | } | |
1522 | ||
1523 | /* Returns the number of whole STP timer ticks in 'ms' milliseconds. There | |
1524 | * are 256 STP timer ticks per second. */ | |
1525 | static int | |
1526 | ms_to_timer(int ms) | |
1527 | { | |
1528 | return ms * 0x100 / 1000; | |
1529 | } | |
1530 | ||
829a7d02 JP |
1531 | /* Returns the number of whole milliseconds in 'timer' STP timer ticks. There |
1532 | * are 256 STP timer ticks per second. */ | |
1533 | static int | |
1534 | timer_to_ms(int timer) | |
1535 | { | |
1536 | return timer * 1000 / 0x100; | |
1537 | } | |
1538 | ||
1539 | static int | |
1540 | clamp(int x, int min, int max) | |
1541 | { | |
1542 | return x < min ? min : x > max ? max : x; | |
1543 | } | |
1544 | ||
1545 | static void | |
bd3950dd | 1546 | stp_update_bridge_timers(struct stp *stp) OVS_REQUIRES(mutex) |
829a7d02 JP |
1547 | { |
1548 | int ht, ma, fd; | |
1549 | ||
1550 | ht = clamp(stp->rq_hello_time, 1000, 10000); | |
1551 | ma = clamp(stp->rq_max_age, MAX(2 * (ht + 1000), 6000), 40000); | |
1552 | fd = clamp(stp->rq_forward_delay, ma / 2 + 1000, 30000); | |
1553 | ||
1554 | stp->bridge_hello_time = ms_to_timer(ht); | |
1555 | stp->bridge_max_age = ms_to_timer(ma); | |
1556 | stp->bridge_forward_delay = ms_to_timer(fd); | |
1557 | ||
1558 | if (stp_is_root_bridge(stp)) { | |
1559 | stp->max_age = stp->bridge_max_age; | |
1560 | stp->hello_time = stp->bridge_hello_time; | |
1561 | stp->forward_delay = stp->bridge_forward_delay; | |
1562 | } | |
1563 | } | |
1564 | ||
1565 | static void | |
1566 | stp_send_bpdu(struct stp_port *p, const void *bpdu, size_t bpdu_size) | |
bd3950dd | 1567 | OVS_REQUIRES(mutex) |
829a7d02 JP |
1568 | { |
1569 | struct eth_header *eth; | |
1570 | struct llc_header *llc; | |
1571 | struct ofpbuf *pkt; | |
1572 | ||
1573 | /* Skeleton. */ | |
1574 | pkt = ofpbuf_new(ETH_HEADER_LEN + LLC_HEADER_LEN + bpdu_size); | |
cf3b7538 | 1575 | eth = ofpbuf_put_zeros(pkt, sizeof *eth); |
829a7d02 | 1576 | llc = ofpbuf_put_zeros(pkt, sizeof *llc); |
cf3b7538 | 1577 | ofpbuf_set_frame(pkt, eth); |
437d0d22 | 1578 | ofpbuf_set_l3(pkt, ofpbuf_put(pkt, bpdu, bpdu_size)); |
829a7d02 JP |
1579 | |
1580 | /* 802.2 header. */ | |
1581 | memcpy(eth->eth_dst, eth_addr_stp, ETH_ADDR_LEN); | |
1582 | /* p->stp->send_bpdu() must fill in source address. */ | |
1f317cb5 | 1583 | eth->eth_type = htons(ofpbuf_size(pkt) - ETH_HEADER_LEN); |
829a7d02 JP |
1584 | |
1585 | /* LLC header. */ | |
1586 | llc->llc_dsap = STP_LLC_DSAP; | |
1587 | llc->llc_ssap = STP_LLC_SSAP; | |
1588 | llc->llc_cntl = STP_LLC_CNTL; | |
1589 | ||
1590 | p->stp->send_bpdu(pkt, stp_port_no(p), p->stp->aux); | |
80740385 | 1591 | p->tx_count++; |
829a7d02 | 1592 | } |
fe4a02e4 EJ |
1593 | \f |
1594 | /* Unixctl. */ | |
1595 | ||
1596 | static struct stp * | |
bd3950dd | 1597 | stp_find(const char *name) OVS_REQUIRES(mutex) |
fe4a02e4 EJ |
1598 | { |
1599 | struct stp *stp; | |
1600 | ||
bd54dbcd | 1601 | LIST_FOR_EACH (stp, node, all_stps) { |
fe4a02e4 EJ |
1602 | if (!strcmp(stp->name, name)) { |
1603 | return stp; | |
1604 | } | |
1605 | } | |
1606 | return NULL; | |
1607 | } | |
1608 | ||
1609 | static void | |
1610 | stp_unixctl_tcn(struct unixctl_conn *conn, int argc, | |
1611 | const char *argv[], void *aux OVS_UNUSED) | |
1612 | { | |
bd54dbcd | 1613 | ovs_mutex_lock(&mutex); |
fe4a02e4 EJ |
1614 | if (argc > 1) { |
1615 | struct stp *stp = stp_find(argv[1]); | |
1616 | ||
1617 | if (!stp) { | |
bde9f75d | 1618 | unixctl_command_reply_error(conn, "no such stp object"); |
bd54dbcd | 1619 | goto out; |
fe4a02e4 EJ |
1620 | } |
1621 | stp_topology_change_detection(stp); | |
1622 | } else { | |
1623 | struct stp *stp; | |
1624 | ||
bd54dbcd | 1625 | LIST_FOR_EACH (stp, node, all_stps) { |
fe4a02e4 EJ |
1626 | stp_topology_change_detection(stp); |
1627 | } | |
1628 | } | |
1629 | ||
bde9f75d | 1630 | unixctl_command_reply(conn, "OK"); |
bd54dbcd EJ |
1631 | |
1632 | out: | |
1633 | ovs_mutex_unlock(&mutex); | |
fe4a02e4 | 1634 | } |