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