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