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