]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2011-2015 M3S, Srl - Italy | |
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 | /* | |
18 | * Rapid Spanning Tree Protocol (IEEE 802.1D-2004) public interface. | |
19 | * | |
20 | * Authors: | |
21 | * Martino Fornasa <mf@fornasa.it> | |
22 | * Daniele Venturino <daniele.venturino@m3s.it> | |
23 | * Carlo Andreotti <c.andreotti@m3s.it> | |
24 | * | |
25 | * References to IEEE 802.1D-2004 standard are enclosed in square brackets. | |
26 | * E.g. [17.3], [Table 17-1], etc. | |
27 | * | |
28 | */ | |
29 | ||
30 | #include <config.h> | |
31 | ||
32 | #include "rstp.h" | |
33 | #include "rstp-common.h" | |
34 | #include "rstp-state-machines.h" | |
35 | #include <arpa/inet.h> | |
36 | #include <inttypes.h> | |
37 | #include <netinet/in.h> | |
38 | #include <stdlib.h> | |
39 | #include <sys/types.h> | |
40 | #include "byte-order.h" | |
41 | #include "connectivity.h" | |
42 | #include "openvswitch/ofpbuf.h" | |
43 | #include "ofproto/ofproto.h" | |
44 | #include "dp-packet.h" | |
45 | #include "packets.h" | |
46 | #include "seq.h" | |
47 | #include "unixctl.h" | |
48 | #include "util.h" | |
49 | #include "openvswitch/vlog.h" | |
50 | ||
51 | VLOG_DEFINE_THIS_MODULE(rstp); | |
52 | ||
53 | struct ovs_mutex rstp_mutex = OVS_MUTEX_INITIALIZER; | |
54 | ||
55 | static struct ovs_list all_rstps__ = OVS_LIST_INITIALIZER(&all_rstps__); | |
56 | static struct ovs_list *const all_rstps OVS_GUARDED_BY(rstp_mutex) = &all_rstps__; | |
57 | ||
58 | /* Internal use only. */ | |
59 | static void rstp_set_bridge_address__(struct rstp *, rstp_identifier) | |
60 | OVS_REQUIRES(rstp_mutex); | |
61 | static void rstp_set_bridge_priority__(struct rstp *, int new_priority) | |
62 | OVS_REQUIRES(rstp_mutex); | |
63 | static void rstp_set_bridge_ageing_time__(struct rstp *, int new_ageing_time) | |
64 | OVS_REQUIRES(rstp_mutex); | |
65 | static void rstp_set_bridge_force_protocol_version__(struct rstp *, | |
66 | enum rstp_force_protocol_version) | |
67 | OVS_REQUIRES(rstp_mutex); | |
68 | static void rstp_set_bridge_hello_time__(struct rstp *) | |
69 | OVS_REQUIRES(rstp_mutex); | |
70 | static void rstp_set_bridge_max_age__(struct rstp *, int new_max_age) | |
71 | OVS_REQUIRES(rstp_mutex); | |
72 | static void rstp_set_bridge_forward_delay__(struct rstp *, int new_forward_delay) | |
73 | OVS_REQUIRES(rstp_mutex); | |
74 | static void rstp_set_bridge_transmit_hold_count__(struct rstp *, | |
75 | int new_transmit_hold_count) | |
76 | OVS_REQUIRES(rstp_mutex); | |
77 | static void rstp_set_bridge_migrate_time__(struct rstp *) | |
78 | OVS_REQUIRES(rstp_mutex); | |
79 | static void rstp_set_bridge_times__(struct rstp *, int new_forward_delay, | |
80 | int new_hello_time, int new_max_age, | |
81 | int new_message_age) | |
82 | OVS_REQUIRES(rstp_mutex); | |
83 | ||
84 | static struct rstp_port *rstp_get_port__(struct rstp *rstp, | |
85 | uint16_t port_number) | |
86 | OVS_REQUIRES(rstp_mutex); | |
87 | static void set_port_id__(struct rstp_port *) | |
88 | OVS_REQUIRES(rstp_mutex); | |
89 | static void update_port_enabled__(struct rstp_port *) | |
90 | OVS_REQUIRES(rstp_mutex); | |
91 | static void set_bridge_priority__(struct rstp *) | |
92 | OVS_REQUIRES(rstp_mutex); | |
93 | static void reinitialize_rstp__(struct rstp *) | |
94 | OVS_REQUIRES(rstp_mutex); | |
95 | static bool is_port_number_available__(struct rstp *, int, struct rstp_port *) | |
96 | OVS_REQUIRES(rstp_mutex); | |
97 | static uint16_t rstp_first_free_number__(struct rstp *, struct rstp_port *) | |
98 | OVS_REQUIRES(rstp_mutex); | |
99 | static void rstp_initialize_port_defaults__(struct rstp_port *) | |
100 | OVS_REQUIRES(rstp_mutex); | |
101 | static void rstp_port_set_priority__(struct rstp_port *, int priority) | |
102 | OVS_REQUIRES(rstp_mutex); | |
103 | static void rstp_port_set_port_number__(struct rstp_port *, | |
104 | uint16_t port_number) | |
105 | OVS_REQUIRES(rstp_mutex); | |
106 | static void rstp_port_set_path_cost__(struct rstp_port *, uint32_t path_cost) | |
107 | OVS_REQUIRES(rstp_mutex); | |
108 | static void rstp_port_set_administrative_bridge_port__(struct rstp_port *, | |
109 | uint8_t admin_port_state, | |
110 | bool initializing) | |
111 | OVS_REQUIRES(rstp_mutex); | |
112 | static void rstp_port_set_admin_edge__(struct rstp_port *, bool admin_edge) | |
113 | OVS_REQUIRES(rstp_mutex); | |
114 | static void rstp_port_set_auto_edge__(struct rstp_port *, bool auto_edge) | |
115 | OVS_REQUIRES(rstp_mutex); | |
116 | static void rstp_port_set_admin_point_to_point_mac__(struct rstp_port *, | |
117 | enum rstp_admin_point_to_point_mac_state admin_p2p_mac_state) | |
118 | OVS_REQUIRES(rstp_mutex); | |
119 | static void rstp_port_set_mcheck__(struct rstp_port *, bool mcheck) | |
120 | OVS_REQUIRES(rstp_mutex); | |
121 | static void reinitialize_port__(struct rstp_port *p) | |
122 | OVS_REQUIRES(rstp_mutex); | |
123 | ||
124 | const char * | |
125 | rstp_state_name(enum rstp_state state) | |
126 | { | |
127 | switch (state) { | |
128 | case RSTP_DISABLED: | |
129 | return "Disabled"; | |
130 | case RSTP_LEARNING: | |
131 | return "Learning"; | |
132 | case RSTP_FORWARDING: | |
133 | return "Forwarding"; | |
134 | case RSTP_DISCARDING: | |
135 | return "Discarding"; | |
136 | default: | |
137 | return "Unknown"; | |
138 | } | |
139 | } | |
140 | ||
141 | const char * | |
142 | rstp_port_role_name(enum rstp_port_role role) | |
143 | { | |
144 | switch (role) { | |
145 | case ROLE_ROOT: | |
146 | return "Root"; | |
147 | case ROLE_DESIGNATED: | |
148 | return "Designated"; | |
149 | case ROLE_ALTERNATE: | |
150 | return "Alternate"; | |
151 | case ROLE_BACKUP: | |
152 | return "Backup"; | |
153 | case ROLE_DISABLED: | |
154 | return "Disabled"; | |
155 | default: | |
156 | return "Unknown"; | |
157 | } | |
158 | } | |
159 | ||
160 | /* Caller has to hold a reference to prevent 'rstp' from being deleted | |
161 | * while taking a new reference. */ | |
162 | struct rstp * | |
163 | rstp_ref(struct rstp *rstp) | |
164 | OVS_EXCLUDED(rstp_mutex) | |
165 | { | |
166 | if (rstp) { | |
167 | ovs_refcount_ref(&rstp->ref_cnt); | |
168 | } | |
169 | return rstp; | |
170 | } | |
171 | ||
172 | /* Frees RSTP struct when reference count reaches zero. */ | |
173 | void | |
174 | rstp_unref(struct rstp *rstp) | |
175 | OVS_EXCLUDED(rstp_mutex) | |
176 | { | |
177 | if (rstp && ovs_refcount_unref_relaxed(&rstp->ref_cnt) == 1) { | |
178 | ovs_mutex_lock(&rstp_mutex); | |
179 | ||
180 | /* Each RSTP port points back to struct rstp without holding a | |
181 | * reference for that pointer. This is OK as we never move | |
182 | * ports from one bridge to another, and holders always | |
183 | * release their ports before releasing the bridge. This | |
184 | * means that there should be not ports at this time. */ | |
185 | ovs_assert(hmap_is_empty(&rstp->ports)); | |
186 | ||
187 | ovs_list_remove(&rstp->node); | |
188 | ovs_mutex_unlock(&rstp_mutex); | |
189 | hmap_destroy(&rstp->ports); | |
190 | free(rstp->name); | |
191 | free(rstp); | |
192 | } | |
193 | } | |
194 | ||
195 | /* Returns the port number. Mutex is needed to guard against | |
196 | * concurrent reinitialization (which can temporarily clear the | |
197 | * port_number). */ | |
198 | int | |
199 | rstp_port_get_number(const struct rstp_port *p) | |
200 | OVS_EXCLUDED(rstp_mutex) | |
201 | { | |
202 | int number; | |
203 | ||
204 | ovs_mutex_lock(&rstp_mutex); | |
205 | number = p->port_number; | |
206 | ovs_mutex_unlock(&rstp_mutex); | |
207 | ||
208 | return number; | |
209 | } | |
210 | ||
211 | static void rstp_unixctl_tcn(struct unixctl_conn *, int argc, | |
212 | const char *argv[], void *aux); | |
213 | ||
214 | /* Decrements the State Machines' timers. */ | |
215 | void | |
216 | rstp_tick_timers(struct rstp *rstp) | |
217 | OVS_EXCLUDED(rstp_mutex) | |
218 | { | |
219 | ovs_mutex_lock(&rstp_mutex); | |
220 | decrease_rstp_port_timers__(rstp); | |
221 | ovs_mutex_unlock(&rstp_mutex); | |
222 | } | |
223 | ||
224 | /* Processes an incoming BPDU. */ | |
225 | void | |
226 | rstp_port_received_bpdu(struct rstp_port *rp, const void *bpdu, | |
227 | size_t bpdu_size) | |
228 | OVS_EXCLUDED(rstp_mutex) | |
229 | { | |
230 | ovs_mutex_lock(&rstp_mutex); | |
231 | /* Only process packets on ports that have RSTP enabled. */ | |
232 | if (rp && rp->rstp_state != RSTP_DISABLED) { | |
233 | process_received_bpdu__(rp, bpdu, bpdu_size); | |
234 | } | |
235 | ovs_mutex_unlock(&rstp_mutex); | |
236 | } | |
237 | ||
238 | void | |
239 | rstp_init(void) | |
240 | OVS_EXCLUDED(rstp_mutex) | |
241 | { | |
242 | unixctl_command_register("rstp/tcn", "[bridge]", 0, 1, rstp_unixctl_tcn, | |
243 | NULL); | |
244 | } | |
245 | ||
246 | /* Creates and returns a new RSTP instance that initially has no ports. */ | |
247 | struct rstp * | |
248 | rstp_create(const char *name, rstp_identifier bridge_address, | |
249 | void (*send_bpdu)(struct dp_packet *bpdu, void *port_aux, | |
250 | void *rstp_aux), | |
251 | void *aux) | |
252 | OVS_EXCLUDED(rstp_mutex) | |
253 | { | |
254 | struct rstp *rstp; | |
255 | ||
256 | VLOG_DBG("Creating RSTP instance"); | |
257 | ||
258 | rstp = xzalloc(sizeof *rstp); | |
259 | rstp->name = xstrdup(name); | |
260 | ||
261 | /* Initialize the ports map before calling any setters, | |
262 | * so that the state machines will see an empty ports map. */ | |
263 | hmap_init(&rstp->ports); | |
264 | ||
265 | ovs_mutex_lock(&rstp_mutex); | |
266 | /* Set bridge address. */ | |
267 | rstp_set_bridge_address__(rstp, bridge_address); | |
268 | /* Set default parameters values. */ | |
269 | rstp_set_bridge_priority__(rstp, RSTP_DEFAULT_PRIORITY); | |
270 | rstp_set_bridge_ageing_time__(rstp, RSTP_DEFAULT_AGEING_TIME); | |
271 | rstp_set_bridge_force_protocol_version__(rstp, FPV_DEFAULT); | |
272 | rstp_set_bridge_forward_delay__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY); | |
273 | rstp_set_bridge_hello_time__(rstp); | |
274 | rstp_set_bridge_max_age__(rstp, RSTP_DEFAULT_BRIDGE_MAX_AGE); | |
275 | rstp_set_bridge_migrate_time__(rstp); | |
276 | rstp_set_bridge_transmit_hold_count__(rstp, | |
277 | RSTP_DEFAULT_TRANSMIT_HOLD_COUNT); | |
278 | rstp_set_bridge_times__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY, | |
279 | RSTP_BRIDGE_HELLO_TIME, | |
280 | RSTP_DEFAULT_BRIDGE_MAX_AGE, 0); | |
281 | rstp->send_bpdu = send_bpdu; | |
282 | rstp->aux = aux; | |
283 | rstp->changes = false; | |
284 | rstp->begin = true; | |
285 | rstp->old_root_aux = NULL; | |
286 | rstp->new_root_aux = NULL; | |
287 | ||
288 | ovs_refcount_init(&rstp->ref_cnt); | |
289 | ||
290 | ovs_list_push_back(all_rstps, &rstp->node); | |
291 | ovs_mutex_unlock(&rstp_mutex); | |
292 | ||
293 | VLOG_DBG("RSTP instance creation done"); | |
294 | return rstp; | |
295 | } | |
296 | ||
297 | /* Called by rstp_set_bridge_address() and rstp_set_bridge_priority(), | |
298 | * it updates the bridge priority vector according to the values passed by | |
299 | * those setters. | |
300 | */ | |
301 | static void | |
302 | set_bridge_priority__(struct rstp *rstp) | |
303 | OVS_REQUIRES(rstp_mutex) | |
304 | { | |
305 | struct rstp_port *p; | |
306 | ||
307 | rstp->bridge_priority.root_bridge_id = rstp->bridge_identifier; | |
308 | rstp->bridge_priority.designated_bridge_id = rstp->bridge_identifier; | |
309 | VLOG_DBG("%s: new bridge identifier: "RSTP_ID_FMT"", rstp->name, | |
310 | RSTP_ID_ARGS(rstp->bridge_identifier)); | |
311 | ||
312 | /* [17.13] When the bridge address changes, recalculates all priority | |
313 | * vectors. | |
314 | */ | |
315 | HMAP_FOR_EACH (p, node, &rstp->ports) { | |
316 | p->selected = false; | |
317 | p->reselect = true; | |
318 | } | |
319 | rstp->changes = true; | |
320 | updt_roles_tree__(rstp); | |
321 | } | |
322 | ||
323 | /* Sets the bridge address. */ | |
324 | static void | |
325 | rstp_set_bridge_address__(struct rstp *rstp, rstp_identifier bridge_address) | |
326 | OVS_REQUIRES(rstp_mutex) | |
327 | { | |
328 | VLOG_DBG("%s: set bridge address to: "RSTP_ID_FMT"", rstp->name, | |
329 | RSTP_ID_ARGS(bridge_address)); | |
330 | if (rstp->address != bridge_address) { | |
331 | rstp->address = bridge_address; | |
332 | rstp->bridge_identifier &= 0xffff000000000000ULL; | |
333 | rstp->bridge_identifier |= bridge_address; | |
334 | set_bridge_priority__(rstp); | |
335 | } | |
336 | } | |
337 | ||
338 | /* Sets the bridge address. */ | |
339 | void | |
340 | rstp_set_bridge_address(struct rstp *rstp, rstp_identifier bridge_address) | |
341 | OVS_EXCLUDED(rstp_mutex) | |
342 | { | |
343 | ovs_mutex_lock(&rstp_mutex); | |
344 | rstp_set_bridge_address__(rstp, bridge_address); | |
345 | ovs_mutex_unlock(&rstp_mutex); | |
346 | } | |
347 | ||
348 | const char * | |
349 | rstp_get_name(const struct rstp *rstp) | |
350 | OVS_EXCLUDED(rstp_mutex) | |
351 | { | |
352 | char *name; | |
353 | ||
354 | ovs_mutex_lock(&rstp_mutex); | |
355 | name = rstp->name; | |
356 | ovs_mutex_unlock(&rstp_mutex); | |
357 | return name; | |
358 | } | |
359 | ||
360 | rstp_identifier | |
361 | rstp_get_bridge_id(const struct rstp *rstp) | |
362 | OVS_EXCLUDED(rstp_mutex) | |
363 | { | |
364 | rstp_identifier bridge_id; | |
365 | ||
366 | ovs_mutex_lock(&rstp_mutex); | |
367 | bridge_id = rstp->bridge_identifier; | |
368 | ovs_mutex_unlock(&rstp_mutex); | |
369 | ||
370 | return bridge_id; | |
371 | } | |
372 | ||
373 | /* Sets the bridge priority. */ | |
374 | static void | |
375 | rstp_set_bridge_priority__(struct rstp *rstp, int new_priority) | |
376 | OVS_REQUIRES(rstp_mutex) | |
377 | { | |
378 | new_priority = ROUND_DOWN(new_priority, RSTP_PRIORITY_STEP); | |
379 | ||
380 | if (rstp->priority != new_priority | |
381 | && new_priority >= RSTP_MIN_PRIORITY | |
382 | && new_priority <= RSTP_MAX_PRIORITY) { | |
383 | VLOG_DBG("%s: set bridge priority to %d", rstp->name, new_priority); | |
384 | ||
385 | rstp->priority = new_priority; | |
386 | rstp->bridge_identifier &= 0x0000ffffffffffffULL; | |
387 | rstp->bridge_identifier |= (uint64_t)new_priority << 48; | |
388 | set_bridge_priority__(rstp); | |
389 | } | |
390 | } | |
391 | ||
392 | void | |
393 | rstp_set_bridge_priority(struct rstp *rstp, int new_priority) | |
394 | OVS_EXCLUDED(rstp_mutex) | |
395 | { | |
396 | ovs_mutex_lock(&rstp_mutex); | |
397 | rstp_set_bridge_priority__(rstp, new_priority); | |
398 | ovs_mutex_unlock(&rstp_mutex); | |
399 | } | |
400 | ||
401 | /* Sets the bridge ageing time. */ | |
402 | static void | |
403 | rstp_set_bridge_ageing_time__(struct rstp *rstp, int new_ageing_time) | |
404 | OVS_REQUIRES(rstp_mutex) | |
405 | { | |
406 | if (new_ageing_time >= RSTP_MIN_AGEING_TIME | |
407 | && new_ageing_time <= RSTP_MAX_AGEING_TIME) { | |
408 | VLOG_DBG("%s: set ageing time to %d", rstp->name, new_ageing_time); | |
409 | ||
410 | rstp->ageing_time = new_ageing_time; | |
411 | } | |
412 | } | |
413 | ||
414 | void | |
415 | rstp_set_bridge_ageing_time(struct rstp *rstp, int new_ageing_time) | |
416 | OVS_EXCLUDED(rstp_mutex) | |
417 | { | |
418 | ovs_mutex_lock(&rstp_mutex); | |
419 | rstp_set_bridge_ageing_time__(rstp, new_ageing_time); | |
420 | ovs_mutex_unlock(&rstp_mutex); | |
421 | } | |
422 | ||
423 | /* Reinitializes RSTP when switching from RSTP mode to STP mode | |
424 | * or vice versa. | |
425 | */ | |
426 | static void | |
427 | reinitialize_rstp__(struct rstp *rstp) | |
428 | OVS_REQUIRES(rstp_mutex) | |
429 | { | |
430 | struct rstp temp; | |
431 | static struct hmap ports; | |
432 | struct rstp_port *p; | |
433 | ||
434 | /* Copy rstp in temp */ | |
435 | temp = *rstp; | |
436 | ports = rstp->ports; | |
437 | ||
438 | /* stop and clear rstp */ | |
439 | memset(rstp, 0, sizeof(struct rstp)); | |
440 | ||
441 | /* Initialize rstp. */ | |
442 | rstp->name = temp.name; | |
443 | ||
444 | /* Initialize the ports hmap before calling any setters, | |
445 | * so that the state machines will see an empty ports list. */ | |
446 | hmap_init(&rstp->ports); | |
447 | ||
448 | /* Set bridge address. */ | |
449 | rstp_set_bridge_address__(rstp, temp.address); | |
450 | /* Set default parameters values. */ | |
451 | rstp_set_bridge_priority__(rstp, RSTP_DEFAULT_PRIORITY); | |
452 | rstp_set_bridge_ageing_time__(rstp, RSTP_DEFAULT_AGEING_TIME); | |
453 | rstp_set_bridge_forward_delay__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY); | |
454 | rstp_set_bridge_hello_time__(rstp); | |
455 | rstp_set_bridge_max_age__(rstp, RSTP_DEFAULT_BRIDGE_MAX_AGE); | |
456 | rstp_set_bridge_migrate_time__(rstp); | |
457 | rstp_set_bridge_transmit_hold_count__(rstp, | |
458 | RSTP_DEFAULT_TRANSMIT_HOLD_COUNT); | |
459 | rstp_set_bridge_times__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY, | |
460 | RSTP_BRIDGE_HELLO_TIME, | |
461 | RSTP_DEFAULT_BRIDGE_MAX_AGE, 0); | |
462 | ||
463 | rstp->send_bpdu = temp.send_bpdu; | |
464 | rstp->aux = temp.aux; | |
465 | rstp->node = temp.node; | |
466 | rstp->changes = false; | |
467 | rstp->begin = true; | |
468 | ||
469 | /* Restore ports. */ | |
470 | rstp->ports = ports; | |
471 | ||
472 | HMAP_FOR_EACH (p, node, &rstp->ports) { | |
473 | reinitialize_port__(p); | |
474 | } | |
475 | ||
476 | rstp->ref_cnt = temp.ref_cnt; | |
477 | } | |
478 | ||
479 | /* Sets the force protocol version parameter. */ | |
480 | static void | |
481 | rstp_set_bridge_force_protocol_version__(struct rstp *rstp, | |
482 | enum rstp_force_protocol_version new_force_protocol_version) | |
483 | OVS_REQUIRES(rstp_mutex) | |
484 | { | |
485 | if (new_force_protocol_version != rstp->force_protocol_version && | |
486 | (new_force_protocol_version == FPV_STP_COMPATIBILITY || | |
487 | new_force_protocol_version == FPV_DEFAULT)) { | |
488 | VLOG_DBG("%s: set bridge Force Protocol Version to %d", rstp->name, | |
489 | new_force_protocol_version); | |
490 | ||
491 | /* [17.13] The Spanning Tree Protocol Entity shall be reinitialized, | |
492 | * as specified by the assertion of BEGIN (17.18.1) in the state | |
493 | * machine specification. | |
494 | */ | |
495 | reinitialize_rstp__(rstp); | |
496 | rstp->force_protocol_version = new_force_protocol_version; | |
497 | if (rstp->force_protocol_version < 2) { | |
498 | rstp->stp_version = true; | |
499 | rstp->rstp_version = false; | |
500 | } else { | |
501 | rstp->stp_version = false; | |
502 | rstp->rstp_version = true; | |
503 | } | |
504 | rstp->changes = true; | |
505 | move_rstp__(rstp); | |
506 | } | |
507 | } | |
508 | ||
509 | void | |
510 | rstp_set_bridge_force_protocol_version(struct rstp *rstp, | |
511 | enum rstp_force_protocol_version new_force_protocol_version) | |
512 | OVS_EXCLUDED(rstp_mutex) | |
513 | { | |
514 | ovs_mutex_lock(&rstp_mutex); | |
515 | rstp_set_bridge_force_protocol_version__(rstp, new_force_protocol_version); | |
516 | ovs_mutex_unlock(&rstp_mutex); | |
517 | } | |
518 | ||
519 | /* Sets the bridge Hello Time parameter. */ | |
520 | static void | |
521 | rstp_set_bridge_hello_time__(struct rstp *rstp) | |
522 | OVS_REQUIRES(rstp_mutex) | |
523 | { | |
524 | VLOG_DBG("%s: set RSTP Hello Time to %d", rstp->name, | |
525 | RSTP_BRIDGE_HELLO_TIME); | |
526 | /* 2 is the only acceptable value. */ | |
527 | rstp->bridge_hello_time = RSTP_BRIDGE_HELLO_TIME; | |
528 | } | |
529 | ||
530 | /* Sets the bridge max age parameter. */ | |
531 | static void | |
532 | rstp_set_bridge_max_age__(struct rstp *rstp, int new_max_age) | |
533 | OVS_REQUIRES(rstp_mutex) | |
534 | { | |
535 | if (rstp->bridge_max_age != new_max_age | |
536 | && new_max_age >= RSTP_MIN_BRIDGE_MAX_AGE | |
537 | && new_max_age <= RSTP_MAX_BRIDGE_MAX_AGE) { | |
538 | /* [17.13] */ | |
539 | if ((2 * (rstp->bridge_forward_delay - 1) >= new_max_age) | |
540 | && (new_max_age >= 2 * rstp->bridge_hello_time)) { | |
541 | VLOG_DBG("%s: set RSTP bridge Max Age to %d", rstp->name, | |
542 | new_max_age); | |
543 | ||
544 | rstp->bridge_max_age = new_max_age; | |
545 | rstp->bridge_times.max_age = new_max_age; | |
546 | rstp->changes = true; | |
547 | updt_roles_tree__(rstp); | |
548 | } | |
549 | } | |
550 | } | |
551 | ||
552 | void | |
553 | rstp_set_bridge_max_age(struct rstp *rstp, int new_max_age) | |
554 | OVS_EXCLUDED(rstp_mutex) | |
555 | { | |
556 | ovs_mutex_lock(&rstp_mutex); | |
557 | rstp_set_bridge_max_age__(rstp, new_max_age); | |
558 | ovs_mutex_unlock(&rstp_mutex); | |
559 | } | |
560 | ||
561 | /* Sets the bridge forward delay parameter. */ | |
562 | static void | |
563 | rstp_set_bridge_forward_delay__(struct rstp *rstp, int new_forward_delay) | |
564 | OVS_REQUIRES(rstp_mutex) | |
565 | { | |
566 | if (rstp->bridge_forward_delay != new_forward_delay | |
567 | && new_forward_delay >= RSTP_MIN_BRIDGE_FORWARD_DELAY | |
568 | && new_forward_delay <= RSTP_MAX_BRIDGE_FORWARD_DELAY) { | |
569 | if (2 * (new_forward_delay - 1) >= rstp->bridge_max_age) { | |
570 | VLOG_DBG("%s: set RSTP Forward Delay to %d", rstp->name, | |
571 | new_forward_delay); | |
572 | rstp->bridge_forward_delay = new_forward_delay; | |
573 | rstp->bridge_times.forward_delay = new_forward_delay; | |
574 | rstp->changes = true; | |
575 | updt_roles_tree__(rstp); | |
576 | } | |
577 | } | |
578 | } | |
579 | ||
580 | void | |
581 | rstp_set_bridge_forward_delay(struct rstp *rstp, int new_forward_delay) | |
582 | OVS_EXCLUDED(rstp_mutex) | |
583 | { | |
584 | ovs_mutex_lock(&rstp_mutex); | |
585 | rstp_set_bridge_forward_delay__(rstp, new_forward_delay); | |
586 | ovs_mutex_unlock(&rstp_mutex); | |
587 | } | |
588 | ||
589 | /* Sets the bridge transmit hold count parameter. */ | |
590 | static void | |
591 | rstp_set_bridge_transmit_hold_count__(struct rstp *rstp, | |
592 | int new_transmit_hold_count) | |
593 | OVS_REQUIRES(rstp_mutex) | |
594 | { | |
595 | if (rstp->transmit_hold_count != new_transmit_hold_count | |
596 | && new_transmit_hold_count >= RSTP_MIN_TRANSMIT_HOLD_COUNT | |
597 | && new_transmit_hold_count <= RSTP_MAX_TRANSMIT_HOLD_COUNT) { | |
598 | struct rstp_port *p; | |
599 | ||
600 | VLOG_DBG("%s: set RSTP Transmit Hold Count to %d", rstp->name, | |
601 | new_transmit_hold_count); | |
602 | /* Resetting txCount on all ports [17.13]. */ | |
603 | ||
604 | rstp->transmit_hold_count = new_transmit_hold_count; | |
605 | HMAP_FOR_EACH (p, node, &rstp->ports) { | |
606 | p->tx_count = 0; | |
607 | } | |
608 | } | |
609 | } | |
610 | ||
611 | void | |
612 | rstp_set_bridge_transmit_hold_count(struct rstp *rstp, | |
613 | int new_transmit_hold_count) | |
614 | OVS_EXCLUDED(rstp_mutex) | |
615 | { | |
616 | ovs_mutex_lock(&rstp_mutex); | |
617 | rstp_set_bridge_transmit_hold_count__(rstp, new_transmit_hold_count); | |
618 | ovs_mutex_unlock(&rstp_mutex); | |
619 | } | |
620 | ||
621 | /* Sets the bridge migrate time parameter. */ | |
622 | static void | |
623 | rstp_set_bridge_migrate_time__(struct rstp *rstp) | |
624 | OVS_REQUIRES(rstp_mutex) | |
625 | { | |
626 | VLOG_DBG("%s: set RSTP Migrate Time to %d", rstp->name, | |
627 | RSTP_MIGRATE_TIME); | |
628 | /* 3 is the only acceptable value */ | |
629 | rstp->migrate_time = RSTP_MIGRATE_TIME; | |
630 | } | |
631 | ||
632 | /* Sets the bridge times. */ | |
633 | static void | |
634 | rstp_set_bridge_times__(struct rstp *rstp, int new_forward_delay, | |
635 | int new_hello_time, int new_max_age, | |
636 | int new_message_age) | |
637 | OVS_REQUIRES(rstp_mutex) | |
638 | { | |
639 | VLOG_DBG("%s: set RSTP times to (%d, %d, %d, %d)", rstp->name, | |
640 | new_forward_delay, new_hello_time, new_max_age, new_message_age); | |
641 | if (new_forward_delay >= RSTP_MIN_BRIDGE_FORWARD_DELAY | |
642 | && new_forward_delay <= RSTP_MAX_BRIDGE_FORWARD_DELAY) { | |
643 | rstp->bridge_times.forward_delay = new_forward_delay; | |
644 | } | |
645 | if (new_hello_time == RSTP_BRIDGE_HELLO_TIME) { | |
646 | rstp->bridge_times.hello_time = new_hello_time; | |
647 | } | |
648 | if (new_max_age >= RSTP_MIN_BRIDGE_MAX_AGE | |
649 | && new_max_age <= RSTP_MAX_BRIDGE_MAX_AGE) { | |
650 | rstp->bridge_times.max_age = new_max_age; | |
651 | } | |
652 | rstp->bridge_times.message_age = new_message_age; | |
653 | } | |
654 | ||
655 | /* Sets the port id, it is called by rstp_port_set_port_number__() or | |
656 | * rstp_port_set_priority__(). | |
657 | */ | |
658 | static void | |
659 | set_port_id__(struct rstp_port *p) | |
660 | OVS_REQUIRES(rstp_mutex) | |
661 | { | |
662 | struct rstp *rstp; | |
663 | ||
664 | rstp = p->rstp; | |
665 | /* [9.2.7] Port identifier. */ | |
666 | p->port_id = p->port_number | (p->priority << 8); | |
667 | VLOG_DBG("%s: new RSTP port id "RSTP_PORT_ID_FMT"", rstp->name, | |
668 | p->port_id); | |
669 | } | |
670 | ||
671 | /* Sets the port priority. */ | |
672 | static void | |
673 | rstp_port_set_priority__(struct rstp_port *port, int priority) | |
674 | OVS_REQUIRES(rstp_mutex) | |
675 | { | |
676 | if (port->priority != priority | |
677 | && priority >= RSTP_MIN_PORT_PRIORITY | |
678 | && priority <= RSTP_MAX_PORT_PRIORITY) { | |
679 | VLOG_DBG("%s, port %u: set RSTP port priority to %d", port->rstp->name, | |
680 | port->port_number, priority); | |
681 | ||
682 | priority -= priority % RSTP_STEP_PORT_PRIORITY; | |
683 | port->priority = priority; | |
684 | set_port_id__(port); | |
685 | port->selected = false; | |
686 | port->reselect = true; | |
687 | } | |
688 | } | |
689 | ||
690 | /* Checks if a port number is available. */ | |
691 | static bool | |
692 | is_port_number_available__(struct rstp *rstp, int n, struct rstp_port *port) | |
693 | OVS_REQUIRES(rstp_mutex) | |
694 | { | |
695 | if (n >= 1 && n <= RSTP_MAX_PORTS) { | |
696 | struct rstp_port *p = rstp_get_port__(rstp, n); | |
697 | ||
698 | return p == NULL || p == port; | |
699 | } | |
700 | return false; | |
701 | } | |
702 | ||
703 | static uint16_t | |
704 | rstp_first_free_number__(struct rstp *rstp, struct rstp_port *rstp_port) | |
705 | OVS_REQUIRES(rstp_mutex) | |
706 | { | |
707 | int free_number = 1; | |
708 | ||
709 | while (free_number <= RSTP_MAX_PORTS) { | |
710 | if (is_port_number_available__(rstp, free_number, rstp_port)) { | |
711 | return free_number; | |
712 | } | |
713 | free_number++; | |
714 | } | |
715 | VLOG_DBG("%s, No free port number available.", rstp->name); | |
716 | return 0; | |
717 | } | |
718 | ||
719 | /* Sets the port number. */ | |
720 | static void | |
721 | rstp_port_set_port_number__(struct rstp_port *port, uint16_t port_number) | |
722 | OVS_REQUIRES(rstp_mutex) | |
723 | { | |
724 | int old_port_number = port->port_number; | |
725 | ||
726 | /* If new_port_number is available, use it, otherwise use the first free | |
727 | * available port number. */ | |
728 | if (port->port_number != port_number || port_number == 0) { | |
729 | port->port_number = | |
730 | is_port_number_available__(port->rstp, port_number, port) | |
731 | ? port_number | |
732 | : rstp_first_free_number__(port->rstp, port); | |
733 | ||
734 | if (port->port_number != old_port_number) { | |
735 | set_port_id__(port); | |
736 | /* [17.13] is not clear. I suppose that a port number change | |
737 | * should trigger reselection like a port priority change. */ | |
738 | port->selected = false; | |
739 | port->reselect = true; | |
740 | ||
741 | /* Adjust the ports hmap. */ | |
742 | if (!hmap_node_is_null(&port->node)) { | |
743 | hmap_remove(&port->rstp->ports, &port->node); | |
744 | } | |
745 | hmap_insert(&port->rstp->ports, &port->node, | |
746 | hash_int(port->port_number, 0)); | |
747 | ||
748 | VLOG_DBG("%s: set new RSTP port number %d", port->rstp->name, | |
749 | port->port_number); | |
750 | } | |
751 | } | |
752 | } | |
753 | ||
754 | /* Converts the link speed to a port path cost [Table 17-3]. */ | |
755 | uint32_t | |
756 | rstp_convert_speed_to_cost(unsigned int speed) | |
757 | { | |
758 | uint32_t value; | |
759 | ||
760 | value = speed >= 10000000 ? 2 /* 10 Tb/s. */ | |
761 | : speed >= 1000000 ? 20 /* 1 Tb/s. */ | |
762 | : speed >= 100000 ? 200 /* 100 Gb/s. */ | |
763 | : speed >= 10000 ? 2000 /* 10 Gb/s. */ | |
764 | : speed >= 1000 ? 20000 /* 1 Gb/s. */ | |
765 | : speed >= 100 ? 200000 /* 100 Mb/s. */ | |
766 | : speed >= 10 ? 2000000 /* 10 Mb/s. */ | |
767 | : speed >= 1 ? 20000000 /* 1 Mb/s. */ | |
768 | : RSTP_DEFAULT_PORT_PATH_COST; /* 100 Mb/s. */ | |
769 | ||
770 | return value; | |
771 | } | |
772 | ||
773 | /* Sets the port path cost. */ | |
774 | static void | |
775 | rstp_port_set_path_cost__(struct rstp_port *port, uint32_t path_cost) | |
776 | OVS_REQUIRES(rstp_mutex) | |
777 | { | |
778 | if (port->port_path_cost != path_cost | |
779 | && path_cost >= RSTP_MIN_PORT_PATH_COST | |
780 | && path_cost <= RSTP_MAX_PORT_PATH_COST) { | |
781 | VLOG_DBG("%s, port %u, set RSTP port path cost to %d", | |
782 | port->rstp->name, port->port_number, path_cost); | |
783 | ||
784 | port->port_path_cost = path_cost; | |
785 | port->selected = false; | |
786 | port->reselect = true; | |
787 | } | |
788 | } | |
789 | ||
790 | /* Gets the root path cost. */ | |
791 | uint32_t | |
792 | rstp_get_root_path_cost(const struct rstp *rstp) | |
793 | OVS_EXCLUDED(rstp_mutex) | |
794 | { | |
795 | uint32_t cost; | |
796 | ||
797 | ovs_mutex_lock(&rstp_mutex); | |
798 | cost = rstp->root_priority.root_path_cost; | |
799 | ovs_mutex_unlock(&rstp_mutex); | |
800 | return cost; | |
801 | } | |
802 | ||
803 | /* Finds a port which needs to flush its own MAC learning table. A NULL | |
804 | * pointer is returned if no port needs to flush its MAC learning table. | |
805 | * '*port' needs to be NULL in the first call to start the iteration. If | |
806 | * '*port' is passed as non-NULL, it must be the value set by the last | |
807 | * invocation of this function. | |
808 | * | |
809 | * This function may only be called by the thread that creates and deletes | |
810 | * ports. Otherwise this function is not thread safe, as the returned | |
811 | * '*port' could become stale before it is used in the next invocation. */ | |
812 | void * | |
813 | rstp_check_and_reset_fdb_flush(struct rstp *rstp, struct rstp_port **port) | |
814 | OVS_EXCLUDED(rstp_mutex) | |
815 | { | |
816 | void *aux = NULL; | |
817 | ||
818 | ovs_mutex_lock(&rstp_mutex); | |
819 | if (*port == NULL) { | |
820 | struct rstp_port *p; | |
821 | ||
822 | HMAP_FOR_EACH (p, node, &rstp->ports) { | |
823 | if (p->fdb_flush) { | |
824 | aux = p->aux; | |
825 | *port = p; | |
826 | goto out; | |
827 | } | |
828 | } | |
829 | } else { /* continue */ | |
830 | struct rstp_port *p = *port; | |
831 | ||
832 | HMAP_FOR_EACH_CONTINUE (p, node, &rstp->ports) { | |
833 | if (p->fdb_flush) { | |
834 | aux = p->aux; | |
835 | *port = p; | |
836 | goto out; | |
837 | } | |
838 | } | |
839 | } | |
840 | /* No port needs flushing. */ | |
841 | *port = NULL; | |
842 | out: | |
843 | /* fdb_flush should be reset by the filtering database | |
844 | * once the entries are removed if rstp_version is TRUE, and | |
845 | * immediately if stp_version is TRUE.*/ | |
846 | if (*port != NULL) { | |
847 | (*port)->fdb_flush = false; | |
848 | } | |
849 | ovs_mutex_unlock(&rstp_mutex); | |
850 | ||
851 | return aux; | |
852 | } | |
853 | ||
854 | /* Finds a port whose state has changed, and returns the aux pointer set for | |
855 | * the port. A NULL pointer is returned when no changed port is found. On | |
856 | * return '*portp' contains the pointer to the rstp port that changed, or NULL | |
857 | * if no changed port can be found. | |
858 | * | |
859 | * If '*portp' is passed as non-NULL, it must be the value set by the last | |
860 | * invocation of this function. | |
861 | * | |
862 | * This function may only be called by the thread that creates and deletes | |
863 | * ports. Otherwise this function is not thread safe, as the returned | |
864 | * '*portp' could become stale before it is used in the next invocation. */ | |
865 | void * | |
866 | rstp_get_next_changed_port_aux(struct rstp *rstp, struct rstp_port **portp) | |
867 | { | |
868 | void *aux = NULL; | |
869 | ||
870 | ovs_mutex_lock(&rstp_mutex); | |
871 | if (*portp == NULL) { | |
872 | struct rstp_port *p; | |
873 | ||
874 | HMAP_FOR_EACH (p, node, &rstp->ports) { | |
875 | if (p->state_changed) { | |
876 | p->state_changed = false; | |
877 | aux = p->aux; | |
878 | *portp = p; | |
879 | goto out; | |
880 | } | |
881 | } | |
882 | } else { /* continue */ | |
883 | struct rstp_port *p = *portp; | |
884 | ||
885 | HMAP_FOR_EACH_CONTINUE (p, node, &rstp->ports) { | |
886 | if (p->state_changed) { | |
887 | p->state_changed = false; | |
888 | aux = p->aux; | |
889 | *portp = p; | |
890 | goto out; | |
891 | } | |
892 | } | |
893 | } | |
894 | /* No changed port found. */ | |
895 | *portp = NULL; | |
896 | out: | |
897 | ovs_mutex_unlock(&rstp_mutex); | |
898 | ||
899 | return aux; | |
900 | } | |
901 | ||
902 | bool | |
903 | rstp_shift_root_learned_address(struct rstp *rstp) | |
904 | { | |
905 | bool ret; | |
906 | ||
907 | ovs_mutex_lock(&rstp_mutex); | |
908 | ret = rstp->root_changed; | |
909 | ovs_mutex_unlock(&rstp_mutex); | |
910 | ||
911 | return ret; | |
912 | } | |
913 | ||
914 | void * | |
915 | rstp_get_old_root_aux(struct rstp *rstp) | |
916 | { | |
917 | void *aux; | |
918 | ||
919 | ovs_mutex_lock(&rstp_mutex); | |
920 | aux = rstp->old_root_aux; | |
921 | ovs_mutex_unlock(&rstp_mutex); | |
922 | ||
923 | return aux; | |
924 | } | |
925 | ||
926 | void * | |
927 | rstp_get_new_root_aux(struct rstp *rstp) | |
928 | { | |
929 | void *aux; | |
930 | ||
931 | ovs_mutex_lock(&rstp_mutex); | |
932 | aux = rstp->new_root_aux; | |
933 | ovs_mutex_unlock(&rstp_mutex); | |
934 | ||
935 | return aux; | |
936 | } | |
937 | ||
938 | void | |
939 | rstp_reset_root_changed(struct rstp *rstp) | |
940 | { | |
941 | ovs_mutex_lock(&rstp_mutex); | |
942 | rstp->root_changed = false; | |
943 | ovs_mutex_unlock(&rstp_mutex); | |
944 | } | |
945 | ||
946 | /* Returns the port in 'rstp' with number 'port_number'. | |
947 | * | |
948 | * XXX: May only be called while concurrent deletion of ports is excluded. */ | |
949 | static struct rstp_port * | |
950 | rstp_get_port__(struct rstp *rstp, uint16_t port_number) | |
951 | OVS_REQUIRES(rstp_mutex) | |
952 | { | |
953 | struct rstp_port *port; | |
954 | ||
955 | ovs_assert(rstp && port_number > 0 && port_number <= RSTP_MAX_PORTS); | |
956 | ||
957 | HMAP_FOR_EACH_WITH_HASH (port, node, hash_int(port_number, 0), | |
958 | &rstp->ports) { | |
959 | if (port->port_number == port_number) { | |
960 | return port; | |
961 | } | |
962 | } | |
963 | return NULL; | |
964 | } | |
965 | ||
966 | struct rstp_port * | |
967 | rstp_get_port(struct rstp *rstp, uint16_t port_number) | |
968 | OVS_EXCLUDED(rstp_mutex) | |
969 | { | |
970 | struct rstp_port *p; | |
971 | ||
972 | ovs_mutex_lock(&rstp_mutex); | |
973 | p = rstp_get_port__(rstp, port_number); | |
974 | ovs_mutex_unlock(&rstp_mutex); | |
975 | return p; | |
976 | } | |
977 | ||
978 | void * | |
979 | rstp_get_port_aux__(struct rstp *rstp, uint16_t port_number) | |
980 | OVS_REQUIRES(rstp_mutex) | |
981 | { | |
982 | struct rstp_port *p; | |
983 | p = rstp_get_port__(rstp, port_number); | |
984 | if (p) { | |
985 | return p->aux; | |
986 | } | |
987 | return NULL; | |
988 | } | |
989 | ||
990 | /* Updates the port_enabled parameter. */ | |
991 | static void | |
992 | update_port_enabled__(struct rstp_port *p) | |
993 | OVS_REQUIRES(rstp_mutex) | |
994 | { | |
995 | if (p->mac_operational && p->is_administrative_bridge_port | |
996 | == RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED) { | |
997 | p->port_enabled = true; | |
998 | } else { | |
999 | p->port_enabled = false; | |
1000 | } | |
1001 | } | |
1002 | ||
1003 | /* Sets the port MAC_Operational parameter [6.4.2]. */ | |
1004 | void | |
1005 | rstp_port_set_mac_operational(struct rstp_port *p, bool new_mac_operational) | |
1006 | OVS_EXCLUDED(rstp_mutex) | |
1007 | { | |
1008 | struct rstp *rstp; | |
1009 | ||
1010 | ovs_mutex_lock(&rstp_mutex); | |
1011 | rstp = p->rstp; | |
1012 | if (p->mac_operational != new_mac_operational) { | |
1013 | p->mac_operational = new_mac_operational; | |
1014 | update_port_enabled__(p); | |
1015 | rstp->changes = true; | |
1016 | move_rstp__(rstp); | |
1017 | } | |
1018 | ovs_mutex_unlock(&rstp_mutex); | |
1019 | } | |
1020 | ||
1021 | /* Sets the port Administrative Bridge Port parameter. */ | |
1022 | static void | |
1023 | rstp_port_set_administrative_bridge_port__(struct rstp_port *p, | |
1024 | uint8_t admin_port_state, | |
1025 | bool initializing) | |
1026 | OVS_REQUIRES(rstp_mutex) | |
1027 | { | |
1028 | VLOG_DBG("%s, port %u: set RSTP port admin-port-state to %d", | |
1029 | p->rstp->name, p->port_number, admin_port_state); | |
1030 | ||
1031 | if (p->is_administrative_bridge_port != admin_port_state | |
1032 | && (admin_port_state == RSTP_ADMIN_BRIDGE_PORT_STATE_DISABLED | |
1033 | || admin_port_state == RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED)) { | |
1034 | p->is_administrative_bridge_port = admin_port_state; | |
1035 | update_port_enabled__(p); | |
1036 | ||
1037 | if (!initializing) { | |
1038 | struct rstp *rstp = p->rstp; | |
1039 | ||
1040 | rstp->changes = true; | |
1041 | move_rstp__(rstp); | |
1042 | } | |
1043 | } | |
1044 | } | |
1045 | ||
1046 | /* Sets the port oper_point_to_point_mac parameter. */ | |
1047 | static void | |
1048 | rstp_port_set_oper_point_to_point_mac__(struct rstp_port *p, | |
1049 | uint8_t new_oper_p2p_mac) | |
1050 | OVS_REQUIRES(rstp_mutex) | |
1051 | { | |
1052 | if (p->oper_point_to_point_mac != new_oper_p2p_mac | |
1053 | && (new_oper_p2p_mac == RSTP_OPER_P2P_MAC_STATE_DISABLED | |
1054 | || new_oper_p2p_mac == RSTP_OPER_P2P_MAC_STATE_ENABLED)) { | |
1055 | ||
1056 | p->oper_point_to_point_mac = new_oper_p2p_mac; | |
1057 | update_port_enabled__(p); | |
1058 | } | |
1059 | } | |
1060 | ||
1061 | /* Initializes a port with the defaults values for its parameters. */ | |
1062 | static void | |
1063 | rstp_initialize_port_defaults__(struct rstp_port *p) | |
1064 | OVS_REQUIRES(rstp_mutex) | |
1065 | { | |
1066 | rstp_port_set_administrative_bridge_port__(p, | |
1067 | RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED, | |
1068 | true); | |
1069 | rstp_port_set_oper_point_to_point_mac__(p, | |
1070 | RSTP_OPER_P2P_MAC_STATE_ENABLED); | |
1071 | rstp_port_set_path_cost__(p, RSTP_DEFAULT_PORT_PATH_COST); | |
1072 | rstp_port_set_admin_edge__(p, false); | |
1073 | rstp_port_set_auto_edge__(p, true); | |
1074 | rstp_port_set_mcheck__(p, false); | |
1075 | ||
1076 | /* Initialize state machines. */ | |
1077 | p->port_receive_sm_state = PORT_RECEIVE_SM_INIT; | |
1078 | p->port_protocol_migration_sm_state = PORT_PROTOCOL_MIGRATION_SM_INIT; | |
1079 | p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_INIT; | |
1080 | p->port_transmit_sm_state = PORT_TRANSMIT_SM_INIT; | |
1081 | p->port_information_sm_state = PORT_INFORMATION_SM_INIT; | |
1082 | p->port_role_transition_sm_state = PORT_ROLE_TRANSITION_SM_INIT; | |
1083 | p->port_state_transition_sm_state = PORT_STATE_TRANSITION_SM_INIT; | |
1084 | p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_INIT; | |
1085 | p->uptime = 0; | |
1086 | ||
1087 | } | |
1088 | ||
1089 | static void | |
1090 | reinitialize_port__(struct rstp_port *p) | |
1091 | OVS_REQUIRES(rstp_mutex) | |
1092 | { | |
1093 | struct rstp_port temp_port; | |
1094 | struct rstp *rstp; | |
1095 | ||
1096 | rstp = p->rstp; | |
1097 | temp_port = *p; | |
1098 | memset(p, 0, sizeof(struct rstp_port)); | |
1099 | ||
1100 | p->ref_cnt = temp_port.ref_cnt; | |
1101 | p->rstp = rstp; | |
1102 | p->node = temp_port.node; | |
1103 | p->aux = temp_port.aux; | |
1104 | p->port_number = temp_port.port_number; | |
1105 | p->port_priority = temp_port.port_priority; | |
1106 | p->port_id = temp_port.port_id; | |
1107 | p->rstp_state = RSTP_DISCARDING; | |
1108 | ||
1109 | rstp_initialize_port_defaults__(p); | |
1110 | ||
1111 | VLOG_DBG("%s: RSTP port "RSTP_PORT_ID_FMT" reinitialized.", rstp->name, | |
1112 | p->port_id); | |
1113 | } | |
1114 | ||
1115 | void | |
1116 | reinitialize_port(struct rstp_port *p) | |
1117 | OVS_EXCLUDED(rstp_mutex) | |
1118 | { | |
1119 | ovs_mutex_lock(&rstp_mutex); | |
1120 | reinitialize_port__(p); | |
1121 | ovs_mutex_unlock(&rstp_mutex); | |
1122 | } | |
1123 | ||
1124 | /* Sets the port state. */ | |
1125 | void | |
1126 | rstp_port_set_state__(struct rstp_port *p, enum rstp_state state) | |
1127 | OVS_REQUIRES(rstp_mutex) | |
1128 | { | |
1129 | struct rstp *rstp; | |
1130 | ||
1131 | rstp = p->rstp; | |
1132 | VLOG_DBG("%s, port %u: set RSTP port state %s -> %s", rstp->name, | |
1133 | p->port_number, | |
1134 | rstp_state_name(p->rstp_state), rstp_state_name(state)); | |
1135 | ||
1136 | if (state != p->rstp_state && !p->state_changed) { | |
1137 | p->state_changed = true; | |
1138 | seq_change(connectivity_seq_get()); | |
1139 | } | |
1140 | p->rstp_state = state; | |
1141 | } | |
1142 | ||
1143 | void | |
1144 | rstp_port_set_state(struct rstp_port *p, enum rstp_state state) | |
1145 | OVS_EXCLUDED(rstp_mutex) | |
1146 | { | |
1147 | ovs_mutex_lock(&rstp_mutex); | |
1148 | rstp_port_set_state__(p, state); | |
1149 | ovs_mutex_unlock(&rstp_mutex); | |
1150 | } | |
1151 | ||
1152 | /* Adds a RSTP port. */ | |
1153 | struct rstp_port * | |
1154 | rstp_add_port(struct rstp *rstp) | |
1155 | OVS_EXCLUDED(rstp_mutex) | |
1156 | { | |
1157 | struct rstp_port *p = xzalloc(sizeof *p); | |
1158 | ||
1159 | ovs_refcount_init(&p->ref_cnt); | |
1160 | hmap_node_nullify(&p->node); | |
1161 | ||
1162 | ovs_mutex_lock(&rstp_mutex); | |
1163 | p->rstp = rstp; | |
1164 | rstp_port_set_priority__(p, RSTP_DEFAULT_PORT_PRIORITY); | |
1165 | rstp_port_set_port_number__(p, 0); | |
1166 | p->aux = NULL; | |
1167 | rstp_initialize_port_defaults__(p); | |
1168 | VLOG_DBG("%s: RSTP port "RSTP_PORT_ID_FMT" initialized.", rstp->name, | |
1169 | p->port_id); | |
1170 | ||
1171 | rstp_port_set_state__(p, RSTP_DISCARDING); | |
1172 | rstp->changes = true; | |
1173 | move_rstp__(rstp); | |
1174 | VLOG_DBG("%s: added port "RSTP_PORT_ID_FMT"", rstp->name, p->port_id); | |
1175 | ovs_mutex_unlock(&rstp_mutex); | |
1176 | return p; | |
1177 | } | |
1178 | ||
1179 | /* Caller has to hold a reference to prevent 'rstp_port' from being deleted | |
1180 | * while taking a new reference. */ | |
1181 | struct rstp_port * | |
1182 | rstp_port_ref(const struct rstp_port *rp_) | |
1183 | OVS_EXCLUDED(rstp_mutex) | |
1184 | { | |
1185 | struct rstp_port *rp = CONST_CAST(struct rstp_port *, rp_); | |
1186 | ||
1187 | if (rp) { | |
1188 | ovs_refcount_ref(&rp->ref_cnt); | |
1189 | } | |
1190 | return rp; | |
1191 | } | |
1192 | ||
1193 | /* Frees RSTP struct. This can be caller by any thread. */ | |
1194 | void | |
1195 | rstp_port_unref(struct rstp_port *rp) | |
1196 | OVS_EXCLUDED(rstp_mutex) | |
1197 | { | |
1198 | if (rp && ovs_refcount_unref_relaxed(&rp->ref_cnt) == 1) { | |
1199 | struct rstp *rstp; | |
1200 | ||
1201 | ovs_mutex_lock(&rstp_mutex); | |
1202 | rstp = rp->rstp; | |
1203 | rstp_port_set_state__(rp, RSTP_DISABLED); | |
1204 | hmap_remove(&rstp->ports, &rp->node); | |
1205 | VLOG_DBG("%s: removed port "RSTP_PORT_ID_FMT"", rstp->name, | |
1206 | rp->port_id); | |
1207 | ovs_mutex_unlock(&rstp_mutex); | |
1208 | free(rp); | |
1209 | } | |
1210 | } | |
1211 | ||
1212 | /* Sets the port Admin Edge parameter. */ | |
1213 | static void | |
1214 | rstp_port_set_admin_edge__(struct rstp_port *port, bool admin_edge) | |
1215 | OVS_REQUIRES(rstp_mutex) | |
1216 | { | |
1217 | if (port->admin_edge != admin_edge) { | |
1218 | VLOG_DBG("%s, port %u: set RSTP Admin Edge to %d", port->rstp->name, | |
1219 | port->port_number, admin_edge); | |
1220 | ||
1221 | port->admin_edge = admin_edge; | |
1222 | } | |
1223 | } | |
1224 | ||
1225 | /* Sets the port Auto Edge parameter. */ | |
1226 | static void | |
1227 | rstp_port_set_auto_edge__(struct rstp_port *port, bool auto_edge) | |
1228 | OVS_REQUIRES(rstp_mutex) | |
1229 | { | |
1230 | if (port->auto_edge != auto_edge) { | |
1231 | VLOG_DBG("%s, port %u: set RSTP Auto Edge to %d", port->rstp->name, | |
1232 | port->port_number, auto_edge); | |
1233 | ||
1234 | port->auto_edge = auto_edge; | |
1235 | } | |
1236 | } | |
1237 | ||
1238 | /* Sets the port admin_point_to_point_mac parameter. */ | |
1239 | static void rstp_port_set_admin_point_to_point_mac__(struct rstp_port *port, | |
1240 | enum rstp_admin_point_to_point_mac_state admin_p2p_mac_state) | |
1241 | OVS_REQUIRES(rstp_mutex) | |
1242 | { | |
1243 | VLOG_DBG("%s, port %u: set RSTP port admin-point-to-point-mac to %d", | |
1244 | port->rstp->name, port->port_number, admin_p2p_mac_state); | |
1245 | if (port->admin_point_to_point_mac != admin_p2p_mac_state) { | |
1246 | if (admin_p2p_mac_state == RSTP_ADMIN_P2P_MAC_FORCE_TRUE) { | |
1247 | port->admin_point_to_point_mac = admin_p2p_mac_state; | |
1248 | rstp_port_set_oper_point_to_point_mac__( | |
1249 | port, RSTP_OPER_P2P_MAC_STATE_ENABLED); | |
1250 | } else if (admin_p2p_mac_state == RSTP_ADMIN_P2P_MAC_FORCE_FALSE) { | |
1251 | port->admin_point_to_point_mac = admin_p2p_mac_state; | |
1252 | rstp_port_set_oper_point_to_point_mac__( | |
1253 | port, RSTP_OPER_P2P_MAC_STATE_DISABLED); | |
1254 | } else if (admin_p2p_mac_state == RSTP_ADMIN_P2P_MAC_AUTO) { | |
1255 | /* If adminPointToPointMAC is set to Auto, then the value of | |
1256 | * operPointToPointMAC is determined in accordance with the | |
1257 | * specific procedures defined for the MAC entity concerned, as | |
1258 | * defined in 6.5. If these procedures determine that the MAC | |
1259 | * entity is connected to a point-to-point LAN, then | |
1260 | * operPointToPointMAC is set TRUE; otherwise it is set FALSE. | |
1261 | * In the absence of a specific definition of how to determine | |
1262 | * whether the MAC is connected to a point-to-point LAN or not, | |
1263 | * the value of operPointToPointMAC shall be FALSE. */ | |
1264 | port->admin_point_to_point_mac = admin_p2p_mac_state; | |
1265 | rstp_port_set_oper_point_to_point_mac__( | |
1266 | port, RSTP_OPER_P2P_MAC_STATE_DISABLED); | |
1267 | } | |
1268 | } | |
1269 | } | |
1270 | ||
1271 | /* Sets the port mcheck parameter. | |
1272 | * [17.19.13] May be set by management to force the Port Protocol Migration | |
1273 | * state machine to transmit RST BPDUs for a MigrateTime (17.13.9) period, to | |
1274 | * test whether all STP Bridges (17.4) on the attached LAN have been removed | |
1275 | * and the Port can continue to transmit RSTP BPDUs. Setting mcheck has no | |
1276 | * effect if stpVersion (17.20.12) is TRUE, i.e., the Bridge is operating in | |
1277 | * STP Compatibility mode. | |
1278 | */ | |
1279 | static void | |
1280 | rstp_port_set_mcheck__(struct rstp_port *port, bool mcheck) | |
1281 | OVS_REQUIRES(rstp_mutex) | |
1282 | { | |
1283 | if (mcheck == true && port->rstp->force_protocol_version >= 2) { | |
1284 | port->mcheck = true; | |
1285 | ||
1286 | VLOG_DBG("%s, port %u: set RSTP mcheck to %d", port->rstp->name, | |
1287 | port->port_number, mcheck); | |
1288 | } | |
1289 | } | |
1290 | ||
1291 | /* Returns the designated bridge id. */ | |
1292 | rstp_identifier | |
1293 | rstp_get_designated_id(const struct rstp *rstp) | |
1294 | OVS_EXCLUDED(rstp_mutex) | |
1295 | { | |
1296 | rstp_identifier designated_id; | |
1297 | ||
1298 | ovs_mutex_lock(&rstp_mutex); | |
1299 | designated_id = rstp->root_priority.designated_bridge_id; | |
1300 | ovs_mutex_unlock(&rstp_mutex); | |
1301 | ||
1302 | return designated_id; | |
1303 | } | |
1304 | ||
1305 | /* Returns the root bridge id. */ | |
1306 | rstp_identifier | |
1307 | rstp_get_root_id(const struct rstp *rstp) | |
1308 | OVS_EXCLUDED(rstp_mutex) | |
1309 | { | |
1310 | rstp_identifier root_id; | |
1311 | ||
1312 | ovs_mutex_lock(&rstp_mutex); | |
1313 | root_id = rstp->root_priority.root_bridge_id; | |
1314 | ovs_mutex_unlock(&rstp_mutex); | |
1315 | ||
1316 | return root_id; | |
1317 | } | |
1318 | ||
1319 | /* Returns the designated port id. */ | |
1320 | uint16_t | |
1321 | rstp_get_designated_port_id(const struct rstp *rstp) | |
1322 | OVS_EXCLUDED(rstp_mutex) | |
1323 | { | |
1324 | uint16_t designated_port_id; | |
1325 | ||
1326 | ovs_mutex_lock(&rstp_mutex); | |
1327 | designated_port_id = rstp->root_priority.designated_port_id; | |
1328 | ovs_mutex_unlock(&rstp_mutex); | |
1329 | ||
1330 | return designated_port_id; | |
1331 | } | |
1332 | ||
1333 | /* Return the bridge port id. */ | |
1334 | uint16_t | |
1335 | rstp_get_bridge_port_id(const struct rstp *rstp) | |
1336 | OVS_EXCLUDED(rstp_mutex) | |
1337 | { | |
1338 | uint16_t bridge_port_id; | |
1339 | ||
1340 | ovs_mutex_lock(&rstp_mutex); | |
1341 | bridge_port_id = rstp->root_priority.bridge_port_id; | |
1342 | ovs_mutex_unlock(&rstp_mutex); | |
1343 | ||
1344 | return bridge_port_id; | |
1345 | } | |
1346 | ||
1347 | /* Returns true if the bridge believes to the be root of the spanning tree, | |
1348 | * false otherwise. | |
1349 | */ | |
1350 | bool | |
1351 | rstp_is_root_bridge(const struct rstp *rstp) | |
1352 | OVS_EXCLUDED(rstp_mutex) | |
1353 | { | |
1354 | bool is_root; | |
1355 | ||
1356 | ovs_mutex_lock(&rstp_mutex); | |
1357 | is_root = rstp->bridge_identifier == | |
1358 | rstp->root_priority.designated_bridge_id; | |
1359 | ovs_mutex_unlock(&rstp_mutex); | |
1360 | ||
1361 | return is_root; | |
1362 | } | |
1363 | ||
1364 | /* Returns the bridge ID of the bridge currently believed to be the root. */ | |
1365 | rstp_identifier | |
1366 | rstp_get_designated_root(const struct rstp *rstp) | |
1367 | OVS_EXCLUDED(rstp_mutex) | |
1368 | { | |
1369 | rstp_identifier designated_root; | |
1370 | ||
1371 | ovs_mutex_lock(&rstp_mutex); | |
1372 | designated_root = rstp->root_priority.designated_bridge_id; | |
1373 | ovs_mutex_unlock(&rstp_mutex); | |
1374 | ||
1375 | return designated_root; | |
1376 | } | |
1377 | ||
1378 | /* Returns the port connecting 'rstp' to the root bridge, or a null pointer if | |
1379 | * there is no such port. | |
1380 | */ | |
1381 | struct rstp_port * | |
1382 | rstp_get_root_port(struct rstp *rstp) | |
1383 | OVS_EXCLUDED(rstp_mutex) | |
1384 | { | |
1385 | struct rstp_port *p; | |
1386 | ||
1387 | ovs_mutex_lock(&rstp_mutex); | |
1388 | HMAP_FOR_EACH (p, node, &rstp->ports) { | |
1389 | if (p->port_id == rstp->root_port_id) { | |
1390 | ovs_mutex_unlock(&rstp_mutex); | |
1391 | return p; | |
1392 | } | |
1393 | } | |
1394 | ovs_mutex_unlock(&rstp_mutex); | |
1395 | return NULL; | |
1396 | } | |
1397 | ||
1398 | /* Returns the state of port 'p'. */ | |
1399 | enum rstp_state | |
1400 | rstp_port_get_state(const struct rstp_port *p) | |
1401 | OVS_EXCLUDED(rstp_mutex) | |
1402 | { | |
1403 | enum rstp_state state; | |
1404 | ||
1405 | ovs_mutex_lock(&rstp_mutex); | |
1406 | state = p->rstp_state; | |
1407 | ovs_mutex_unlock(&rstp_mutex); | |
1408 | ||
1409 | return state; | |
1410 | } | |
1411 | ||
1412 | /* Retrieves port status. */ | |
1413 | void | |
1414 | rstp_port_get_status(const struct rstp_port *p, uint16_t *id, | |
1415 | enum rstp_state *state, enum rstp_port_role *role, | |
1416 | rstp_identifier *designated_bridge_id, | |
1417 | uint16_t *designated_port_id, | |
1418 | uint32_t *designated_path_cost, int *tx_count, | |
1419 | int *rx_count, int *error_count, int *uptime) | |
1420 | OVS_EXCLUDED(rstp_mutex) | |
1421 | { | |
1422 | ovs_mutex_lock(&rstp_mutex); | |
1423 | *id = p->port_id; | |
1424 | *state = p->rstp_state; | |
1425 | *role = p->role; | |
1426 | ||
1427 | *designated_bridge_id = p->port_priority.designated_bridge_id; | |
1428 | *designated_port_id = p->port_priority.designated_port_id; | |
1429 | *designated_path_cost = p->port_priority.root_path_cost; | |
1430 | ||
1431 | *tx_count = p->tx_count; | |
1432 | *rx_count = p->rx_rstp_bpdu_cnt; | |
1433 | *error_count = p->error_count; | |
1434 | *uptime = p->uptime; | |
1435 | ovs_mutex_unlock(&rstp_mutex); | |
1436 | } | |
1437 | ||
1438 | void | |
1439 | rstp_port_set(struct rstp_port *port, uint16_t port_num, int priority, | |
1440 | uint32_t path_cost, bool is_admin_edge, bool is_auto_edge, | |
1441 | enum rstp_admin_point_to_point_mac_state admin_p2p_mac_state, | |
1442 | bool admin_port_state, bool do_mcheck, void *aux) | |
1443 | OVS_EXCLUDED(rstp_mutex) | |
1444 | { | |
1445 | ovs_mutex_lock(&rstp_mutex); | |
1446 | port->aux = aux; | |
1447 | rstp_port_set_priority__(port, priority); | |
1448 | rstp_port_set_port_number__(port, port_num); | |
1449 | rstp_port_set_path_cost__(port, path_cost); | |
1450 | rstp_port_set_admin_edge__(port, is_admin_edge); | |
1451 | rstp_port_set_auto_edge__(port, is_auto_edge); | |
1452 | rstp_port_set_admin_point_to_point_mac__(port, admin_p2p_mac_state); | |
1453 | rstp_port_set_administrative_bridge_port__(port, admin_port_state, false); | |
1454 | rstp_port_set_mcheck__(port, do_mcheck); | |
1455 | ovs_mutex_unlock(&rstp_mutex); | |
1456 | } | |
1457 | ||
1458 | /* Individual setters only used by test-rstp.c. */ | |
1459 | void | |
1460 | rstp_port_set_priority(struct rstp_port *port, int priority) | |
1461 | OVS_EXCLUDED(rstp_mutex) | |
1462 | { | |
1463 | ovs_mutex_lock(&rstp_mutex); | |
1464 | rstp_port_set_priority__(port, priority); | |
1465 | ovs_mutex_unlock(&rstp_mutex); | |
1466 | } | |
1467 | ||
1468 | void | |
1469 | rstp_port_set_path_cost(struct rstp_port *port, uint32_t path_cost) | |
1470 | OVS_EXCLUDED(rstp_mutex) | |
1471 | { | |
1472 | ovs_mutex_lock(&rstp_mutex); | |
1473 | rstp_port_set_path_cost__(port, path_cost); | |
1474 | ovs_mutex_unlock(&rstp_mutex); | |
1475 | } | |
1476 | ||
1477 | void | |
1478 | rstp_port_set_aux(struct rstp_port *port, void *aux) | |
1479 | OVS_EXCLUDED(rstp_mutex) | |
1480 | { | |
1481 | ovs_mutex_lock(&rstp_mutex); | |
1482 | port->aux = aux; | |
1483 | ovs_mutex_unlock(&rstp_mutex); | |
1484 | } | |
1485 | ||
1486 | /* Unixctl. */ | |
1487 | static struct rstp * | |
1488 | rstp_find(const char *name) | |
1489 | OVS_REQUIRES(rstp_mutex) | |
1490 | { | |
1491 | struct rstp *rstp; | |
1492 | ||
1493 | LIST_FOR_EACH (rstp, node, all_rstps) { | |
1494 | if (!strcmp(rstp->name, name)) { | |
1495 | return rstp; | |
1496 | } | |
1497 | } | |
1498 | return NULL; | |
1499 | } | |
1500 | ||
1501 | static void | |
1502 | rstp_unixctl_tcn(struct unixctl_conn *conn, int argc, | |
1503 | const char *argv[], void *aux OVS_UNUSED) | |
1504 | OVS_EXCLUDED(rstp_mutex) | |
1505 | { | |
1506 | ovs_mutex_lock(&rstp_mutex); | |
1507 | if (argc > 1) { | |
1508 | struct rstp *rstp = rstp_find(argv[1]); | |
1509 | if (!rstp) { | |
1510 | unixctl_command_reply_error(conn, "No such RSTP object"); | |
1511 | goto out; | |
1512 | } | |
1513 | rstp->changes = true; | |
1514 | move_rstp__(rstp); | |
1515 | } else { | |
1516 | struct rstp *rstp; | |
1517 | LIST_FOR_EACH (rstp, node, all_rstps) { | |
1518 | rstp->changes = true; | |
1519 | move_rstp__(rstp); | |
1520 | } | |
1521 | } | |
1522 | unixctl_command_reply(conn, "OK"); | |
1523 | ||
1524 | out: | |
1525 | ovs_mutex_unlock(&rstp_mutex); | |
1526 | } |