For SNAT, don't store the pre-fragment L2 header before actions are applied.
[sliver-openvswitch.git] / lib / stp.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  *
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  *
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 /* Based on sample implementation in 802.1D-1998.  Above copyright and license
35  * applies to all modifications. */
36
37 #include "stp.h"
38 #include <arpa/inet.h>
39 #include <assert.h>
40 #include <inttypes.h>
41 #include <stdlib.h>
42 #include "packets.h"
43 #include "util.h"
44 #include "xtoxll.h"
45
46 #include "vlog.h"
47 #define THIS_MODULE VLM_stp
48
49 /* Ethernet address used as the destination for STP frames. */
50 const uint8_t stp_eth_addr[ETH_ADDR_LEN]
51 = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x01 };
52
53 #define STP_PROTOCOL_ID 0x0000
54 #define STP_PROTOCOL_VERSION 0x00
55 #define STP_TYPE_CONFIG 0x00
56 #define STP_TYPE_TCN 0x80
57
58 struct stp_bpdu_header {
59     uint16_t protocol_id;       /* STP_PROTOCOL_ID. */
60     uint8_t protocol_version;   /* STP_PROTOCOL_VERSION. */
61     uint8_t bpdu_type;          /* One of STP_TYPE_*. */
62 } __attribute__((packed));
63 BUILD_ASSERT_DECL(sizeof(struct stp_bpdu_header) == 4);
64
65 enum stp_config_bpdu_flags {
66     STP_CONFIG_TOPOLOGY_CHANGE_ACK = 0x80,
67     STP_CONFIG_TOPOLOGY_CHANGE = 0x01
68 };
69
70 struct stp_config_bpdu {
71     struct stp_bpdu_header header; /* Type STP_TYPE_CONFIG. */
72     uint8_t flags;                 /* STP_CONFIG_* flags. */
73     uint64_t root_id;              /* 8.5.1.1: Bridge believed to be root. */
74     uint32_t root_path_cost;       /* 8.5.1.2: Cost of path to root. */
75     uint64_t bridge_id;            /* 8.5.1.3: ID of transmitting bridge. */
76     uint16_t port_id;              /* 8.5.1.4: Port transmitting the BPDU. */
77     uint16_t message_age;          /* 8.5.1.5: Age of BPDU at tx time. */
78     uint16_t max_age;              /* 8.5.1.6: Timeout for received data. */
79     uint16_t hello_time;           /* 8.5.1.7: Time between BPDU generation. */
80     uint16_t forward_delay;        /* 8.5.1.8: State progression delay. */
81 } __attribute__((packed));
82 BUILD_ASSERT_DECL(sizeof(struct stp_config_bpdu) == 35);
83
84 struct stp_tcn_bpdu {
85     struct stp_bpdu_header header; /* Type STP_TYPE_TCN. */
86 } __attribute__((packed));
87 BUILD_ASSERT_DECL(sizeof(struct stp_tcn_bpdu) == 4);
88
89 struct stp_timer {
90     bool active;                 /* Timer in use? */
91     int value;                   /* Current value of timer, counting up. */
92 };
93
94 struct stp_port {
95     struct stp *stp;
96     int port_id;                    /* 8.5.5.1: Unique port identifier. */
97     enum stp_state state;           /* 8.5.5.2: Current state. */
98     int path_cost;                  /* 8.5.5.3: Cost of tx/rx on this port. */
99     stp_identifier designated_root; /* 8.5.5.4. */
100     int designated_cost;            /* 8.5.5.5: Path cost to root on port. */
101     stp_identifier designated_bridge; /* 8.5.5.6. */
102     int designated_port;            /* 8.5.5.7: Port to send config msgs on. */
103     bool topology_change_ack;       /* 8.5.5.8: Flag for next config BPDU. */
104     bool config_pending;            /* 8.5.5.9: Send BPDU when hold expires? */
105     bool change_detection_enabled;  /* 8.5.5.10: Detect topology changes? */
106
107     struct stp_timer message_age_timer; /* 8.5.6.1: Age of received info. */
108     struct stp_timer forward_delay_timer; /* 8.5.6.2: State change timer. */
109     struct stp_timer hold_timer;        /* 8.5.6.3: BPDU rate limit timer. */
110
111     bool state_changed;
112 };
113
114 struct stp {
115     /* Static bridge data. */
116     char *name;                     /* Human-readable name for log messages. */
117     stp_identifier bridge_id;       /* 8.5.3.7: This bridge. */
118     int max_age;                    /* 8.5.3.4: Time to drop received data. */
119     int hello_time;                 /* 8.5.3.5: Time between sending BPDUs. */
120     int forward_delay;              /* 8.5.3.6: Delay between state changes. */
121     int bridge_max_age;             /* 8.5.3.8: max_age when we're root. */
122     int bridge_hello_time;          /* 8.5.3.9: hello_time as root. */
123     int bridge_forward_delay;       /* 8.5.3.10: forward_delay as root. */
124
125     /* Dynamic bridge data. */
126     stp_identifier designated_root; /* 8.5.3.1: Bridge believed to be root. */
127     unsigned int root_path_cost;    /* 8.5.3.2: Cost of path to root. */
128     struct stp_port *root_port;     /* 8.5.3.3: Lowest cost port to root. */
129     bool topology_change_detected;  /* 8.5.3.11: Detected a topology change? */
130     bool topology_change;           /* 8.5.3.12: Received topology change? */
131
132     /* Bridge timers. */
133     struct stp_timer hello_timer;   /* 8.5.4.1: Hello timer. */
134     struct stp_timer tcn_timer;     /* 8.5.4.2: Topology change timer. */
135     struct stp_timer topology_change_timer; /* 8.5.4.3. */
136
137     /* Ports. */
138     struct stp_port ports[STP_MAX_PORTS];
139
140     /* Interface to client. */
141     struct stp_port *first_changed_port;
142     void (*send_bpdu)(const void *bpdu, size_t bpdu_size,
143                       int port_no, void *aux);
144     void *aux;
145 };
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 (struct stp_port *) port;
157         }
158     }
159     return NULL;
160 }
161
162 #define SECONDS_TO_TIMER(SECS) ((SECS) * 0x100)
163
164 #define MESSAGE_AGE_INCREMENT 1
165
166 static void stp_transmit_config(struct stp_port *);
167 static bool stp_supersedes_port_info(const struct stp_port *,
168                                      const struct stp_config_bpdu *);
169 static void stp_record_config_information(struct stp_port *,
170                                           const struct stp_config_bpdu *);
171 static void stp_record_config_timeout_values(struct stp *,
172                                              const struct stp_config_bpdu  *);
173 static bool stp_is_designated_port(const struct stp_port *);
174 static void stp_config_bpdu_generation(struct stp *);
175 static void stp_transmit_tcn(struct stp *);
176 static void stp_configuration_update(struct stp *);
177 static bool stp_supersedes_root(const struct stp_port *root,
178                                 const struct stp_port *);
179 static void stp_root_selection(struct stp *);
180 static void stp_designated_port_selection(struct stp *);
181 static void stp_become_designated_port(struct stp_port *);
182 static void stp_port_state_selection(struct stp *);
183 static void stp_make_forwarding(struct stp_port *);
184 static void stp_make_blocking(struct stp_port *);
185 static void stp_set_port_state(struct stp_port *, enum stp_state);
186 static void stp_topology_change_detection(struct stp *);
187 static void stp_topology_change_acknowledged(struct stp *);
188 static void stp_acknowledge_topology_change(struct stp_port *);
189 static void stp_received_config_bpdu(struct stp *, struct stp_port *,
190                                      const struct stp_config_bpdu *);
191 static void stp_received_tcn_bpdu(struct stp *, struct stp_port *,
192                                   const struct stp_tcn_bpdu *);
193 static void stp_hello_timer_expiry(struct stp *);
194 static void stp_message_age_timer_expiry(struct stp_port *);
195 static bool stp_is_designated_for_some_port(const struct stp *);
196 static void stp_forward_delay_timer_expiry(struct stp_port *);
197 static void stp_tcn_timer_expiry(struct stp *);
198 static void stp_topology_change_timer_expiry(struct stp *);
199 static void stp_hold_timer_expiry(struct stp_port *);
200 static void stp_initialize_port(struct stp_port *, enum stp_state);
201 static void stp_become_root_bridge(struct stp *stp);
202
203 static void stp_start_timer(struct stp_timer *, int value);
204 static void stp_stop_timer(struct stp_timer *);
205 static bool stp_timer_expired(struct stp_timer *, int elapsed, int timeout);
206
207 /* Creates and returns a new STP instance that initially has no port enabled.
208  *
209  * 'bridge_id' should be a 48-bit MAC address as returned by
210  * eth_addr_to_uint64().  'bridge_id' may also have a priority value in its top
211  * 16 bits; if those bits are set to 0, the default bridge priority of 32768 is
212  * used.  (This priority may be changed with stp_set_bridge_priority().)
213  *
214  * When the bridge needs to send out a BPDU, it calls 'send_bpdu', passing
215  * 'aux' as auxiliary data.  This callback may be called from stp_tick() or
216  * stp_received_bpdu().
217  */
218 struct stp *
219 stp_create(const char *name, stp_identifier bridge_id,
220            void (*send_bpdu)(const void *bpdu, size_t bpdu_size,
221                              int port_no, void *aux),
222            void *aux)
223 {
224     struct stp *stp;
225     struct stp_port *p;
226
227     stp = xcalloc(1, sizeof *stp);
228     stp->name = xstrdup(name);
229     stp->bridge_id = bridge_id;
230     if (!(stp->bridge_id >> 48)) {
231         stp->bridge_id |= UINT64_C(32768) << 48;
232     }
233     stp->max_age = SECONDS_TO_TIMER(6);
234     stp->hello_time = SECONDS_TO_TIMER(2);
235     stp->forward_delay = SECONDS_TO_TIMER(4);
236     stp->bridge_max_age = stp->max_age;
237     stp->bridge_hello_time = stp->hello_time;
238     stp->bridge_forward_delay = stp->forward_delay;
239
240     /* Verify constraints stated by 802.1D. */
241     assert(2 * (stp->forward_delay - SECONDS_TO_TIMER(1)) >= stp->max_age);
242     assert(stp->max_age >= 2 * (stp->hello_time + SECONDS_TO_TIMER(1)));
243
244     stp->designated_root = stp->bridge_id;
245     stp->root_path_cost = 0;
246     stp->root_port = NULL;
247     stp->topology_change_detected = false;
248     stp->topology_change = false;
249
250     stp_stop_timer(&stp->tcn_timer);
251     stp_stop_timer(&stp->topology_change_timer);
252     stp_start_timer(&stp->hello_timer, 0);
253
254     stp->send_bpdu = send_bpdu;
255     stp->aux = aux;
256
257     stp->first_changed_port = &stp->ports[ARRAY_SIZE(stp->ports)];
258     for (p = stp->ports; p < &stp->ports[ARRAY_SIZE(stp->ports)]; p++) {
259         p->stp = stp;
260         p->port_id = (stp_port_no(p) + 1) | (128 << 8);
261         p->path_cost = 19;      /* Recommended default for 100 Mb/s link. */
262         stp_initialize_port(p, STP_DISABLED);
263     }
264     return stp;
265 }
266
267 /* Destroys 'stp'. */
268 void
269 stp_destroy(struct stp *stp)
270 {
271     free(stp);
272 }
273
274 void
275 stp_tick(struct stp *stp, int elapsed)
276 {
277     struct stp_port *p;
278
279     if (stp_timer_expired(&stp->hello_timer, elapsed, stp->hello_time)) {
280         stp_hello_timer_expiry(stp);
281     }
282     if (stp_timer_expired(&stp->tcn_timer, elapsed, stp->bridge_hello_time)) {
283         stp_tcn_timer_expiry(stp);
284     }
285     if (stp_timer_expired(&stp->topology_change_timer, elapsed,
286                           stp->max_age + stp->forward_delay)) {
287         stp_topology_change_timer_expiry(stp);
288     }
289     FOR_EACH_ENABLED_PORT (p, stp) {
290         if (stp_timer_expired(&p->message_age_timer, elapsed, stp->max_age)) {
291             stp_message_age_timer_expiry(p);
292         }
293     }
294     FOR_EACH_ENABLED_PORT (p, stp) {
295         if (stp_timer_expired(&p->forward_delay_timer, elapsed,
296                               stp->forward_delay)) {
297             stp_forward_delay_timer_expiry(p);
298         }
299         if (stp_timer_expired(&p->hold_timer, elapsed, SECONDS_TO_TIMER(1))) {
300             stp_hold_timer_expiry(p);
301         }
302     }
303 }
304
305 static void
306 set_bridge_id(struct stp *stp, stp_identifier new_bridge_id)
307 {
308     if (new_bridge_id != stp->bridge_id) {
309         bool root;
310         struct stp_port *p;
311
312         root = stp_is_root_bridge(stp);
313         FOR_EACH_ENABLED_PORT (p, stp) {
314             if (stp_is_designated_port(p)) {
315                 p->designated_bridge = new_bridge_id;
316             }
317         }
318         stp->bridge_id = new_bridge_id;
319         stp_configuration_update(stp);
320         stp_port_state_selection(stp);
321         if (stp_is_root_bridge(stp) && !root) {
322             stp_become_root_bridge(stp);
323         }
324     }
325 }
326
327 void
328 stp_set_bridge_id(struct stp *stp, stp_identifier bridge_id)
329 {
330     const uint64_t mac_bits = (UINT64_C(1) << 48) - 1;
331     const uint64_t pri_bits = ~mac_bits;
332     set_bridge_id(stp, (stp->bridge_id & pri_bits) | (bridge_id & mac_bits));
333 }
334
335 void
336 stp_set_bridge_priority(struct stp *stp, uint16_t new_priority)
337 {
338     const uint64_t mac_bits = (UINT64_C(1) << 48) - 1;
339     set_bridge_id(stp, ((stp->bridge_id & mac_bits)
340                         | ((uint64_t) new_priority << 48)));
341 }
342
343 /* Returns the name given to 'stp' in the call to stp_create(). */
344 const char *
345 stp_get_name(const struct stp *stp)
346 {
347     return stp->name;
348 }
349
350 /* Returns the bridge ID for 'stp'. */
351 stp_identifier
352 stp_get_bridge_id(const struct stp *stp)
353 {
354     return stp->bridge_id;
355 }
356
357 /* Returns the bridge ID of the bridge currently believed to be the root. */
358 stp_identifier
359 stp_get_designated_root(const struct stp *stp)
360 {
361     return stp->designated_root;
362 }
363
364 /* Returns true if 'stp' believes itself to the be root of the spanning tree,
365  * false otherwise. */
366 bool
367 stp_is_root_bridge(const struct stp *stp)
368 {
369     return stp->bridge_id == stp->designated_root;
370 }
371
372 /* Returns the cost of the path from 'stp' to the root of the spanning tree. */
373 int
374 stp_get_root_path_cost(const struct stp *stp)
375 {
376     return stp->root_path_cost;
377 }
378
379 /* Returns the port in 'stp' with index 'port_no', which must be between 0 and
380  * STP_MAX_PORTS. */
381 struct stp_port *
382 stp_get_port(struct stp *stp, int port_no)
383 {
384     assert(port_no >= 0 && port_no < ARRAY_SIZE(stp->ports));
385     return &stp->ports[port_no];
386 }
387
388 /* Returns the port connecting 'stp' to the root bridge, or a null pointer if
389  * there is no such port. */
390 struct stp_port *
391 stp_get_root_port(struct stp *stp)
392 {
393     return stp->root_port;
394 }
395
396 /* Finds a port whose state has changed.  If successful, stores the port whose
397  * state changed in '*portp' and returns true.  If no port has changed, stores
398  * NULL in '*portp' and returns false. */
399 bool
400 stp_get_changed_port(struct stp *stp, struct stp_port **portp)
401 {
402     struct stp_port *end = &stp->ports[ARRAY_SIZE(stp->ports)];
403     struct stp_port *p;
404
405     for (p = stp->first_changed_port; p < end; p++) {
406         if (p->state_changed) {
407             p->state_changed = false;
408             stp->first_changed_port = p + 1;
409             *portp = p;
410             return true;
411         }
412     }
413     stp->first_changed_port = end;
414     *portp = NULL;
415     return false;
416 }
417
418 /* Returns the name for the given 'state' (for use in debugging and log
419  * messages). */
420 const char *
421 stp_state_name(enum stp_state state)
422 {
423     switch (state) {
424     case STP_DISABLED:
425         return "disabled";
426     case STP_LISTENING:
427         return "listening";
428     case STP_LEARNING:
429         return "learning";
430     case STP_FORWARDING:
431         return "forwarding";
432     case STP_BLOCKING:
433         return "blocking";
434     default:
435         NOT_REACHED();
436     }
437 }
438
439 /* Returns true if 'state' is one in which packets received on a port should
440  * be forwarded, false otherwise. */
441 bool
442 stp_forward_in_state(enum stp_state state)
443 {
444     return state == STP_FORWARDING;
445 }
446
447 /* Returns true if 'state' is one in which MAC learning should be done on
448  * packets received on a port, false otherwise. */
449 bool
450 stp_learn_in_state(enum stp_state state)
451 {
452     return state & (STP_LEARNING | STP_FORWARDING);
453 }
454
455 /* Notifies the STP entity that bridge protocol data unit 'bpdu', which is
456  * 'bpdu_size' bytes in length, was received on port 'p'.
457  *
458  * This function may call the 'send_bpdu' function provided to stp_create(). */
459 void
460 stp_received_bpdu(struct stp_port *p, const void *bpdu, size_t bpdu_size)
461 {
462     struct stp *stp = p->stp;
463     const struct stp_bpdu_header *header;
464
465     if (p->state == STP_DISABLED) {
466         return;
467     }
468
469     if (bpdu_size < sizeof(struct stp_bpdu_header)) {
470         VLOG_WARN("%s: received runt %zu-byte BPDU", stp->name, bpdu_size);
471         return;
472     }
473
474     header = bpdu;
475     if (header->protocol_id != htons(STP_PROTOCOL_ID)) {
476         VLOG_WARN("%s: received BPDU with unexpected protocol ID %"PRIu16,
477                   stp->name, ntohs(header->protocol_id));
478         return;
479     }
480     if (header->protocol_version != STP_PROTOCOL_VERSION) {
481         VLOG_DBG("%s: received BPDU with unexpected protocol version %"PRIu8,
482                  stp->name, header->protocol_version);
483     }
484
485     switch (header->bpdu_type) {
486     case STP_TYPE_CONFIG:
487         if (bpdu_size < sizeof(struct stp_config_bpdu)) {
488             VLOG_WARN("%s: received config BPDU with invalid size %zu",
489                       stp->name, bpdu_size);
490             return;
491         }
492         stp_received_config_bpdu(stp, p, bpdu);
493         break;
494
495     case STP_TYPE_TCN:
496         if (bpdu_size != sizeof(struct stp_tcn_bpdu)) {
497             VLOG_WARN("%s: received TCN BPDU with invalid size %zu",
498                       stp->name, bpdu_size);
499             return;
500         }
501         stp_received_tcn_bpdu(stp, p, bpdu);
502         break;
503
504     default:
505         VLOG_WARN("%s: received BPDU of unexpected type %"PRIu8,
506                   stp->name, header->bpdu_type);
507         return;
508     }
509 }
510
511 /* Returns the STP entity in which 'p' is nested. */
512 struct stp *
513 stp_port_get_stp(struct stp_port *p)
514 {
515     return p->stp;
516 }
517
518 /* Returns the index of port 'p' within its bridge. */
519 int
520 stp_port_no(const struct stp_port *p)
521 {
522     struct stp *stp = p->stp;
523     assert(p >= stp->ports && p < &stp->ports[ARRAY_SIZE(stp->ports)]);
524     return p - stp->ports;
525 }
526
527 /* Returns the state of port 'p'. */
528 enum stp_state
529 stp_port_get_state(const struct stp_port *p)
530 {
531     return p->state;
532 }
533
534 /* Disables STP on port 'p'. */
535 void
536 stp_port_disable(struct stp_port *p)
537 {
538     struct stp *stp = p->stp;
539     if (p->state != STP_DISABLED) {
540         bool root = stp_is_root_bridge(stp);
541         stp_become_designated_port(p);
542         stp_set_port_state(p, STP_DISABLED);
543         p->topology_change_ack = false;
544         p->config_pending = false;
545         stp_stop_timer(&p->message_age_timer);
546         stp_stop_timer(&p->forward_delay_timer);
547         stp_configuration_update(stp);
548         stp_port_state_selection(stp);
549         if (stp_is_root_bridge(stp) && !root) {
550             stp_become_root_bridge(stp);
551         }
552     }
553 }
554
555 /* Enables STP on port 'p'.  The port will initially be in "blocking" state. */
556 void
557 stp_port_enable(struct stp_port *p)
558 {
559     if (p->state == STP_DISABLED) {
560         stp_initialize_port(p, STP_BLOCKING);
561         stp_port_state_selection(p->stp);
562     }
563 }
564
565 /* Sets the priority of port 'p' to 'new_priority'.  Lower numerical values
566  * are interpreted as higher priorities. */
567 void
568 stp_port_set_priority(struct stp_port *p, uint8_t new_priority)
569 {
570     uint16_t new_port_id = (p->port_id & 0xff) | (new_priority << 8);
571     if (p->port_id != new_port_id) {
572         struct stp *stp = p->stp;
573         if (stp_is_designated_port(p)) {
574             p->designated_port = new_port_id;
575         }
576         p->port_id = new_port_id;
577         if (stp->bridge_id == p->designated_bridge
578             && p->port_id < p->designated_port) {
579             stp_become_designated_port(p);
580             stp_port_state_selection(stp);
581         }
582     }
583 }
584
585 /* Sets the path cost of port 'p' to 'path_cost'.  Lower values are generally
586  * used to indicate faster links.  Use stp_port_set_speed() to automatically
587  * generate a default path cost from a link speed. */
588 void
589 stp_port_set_path_cost(struct stp_port *p, unsigned int path_cost)
590 {
591     if (p->path_cost != path_cost) {
592         struct stp *stp = p->stp;
593         p->path_cost = path_cost;
594         stp_configuration_update(stp);
595         stp_port_state_selection(stp);
596     }
597 }
598
599 /* Sets the path cost of port 'p' based on 'speed' (measured in Mb/s). */
600 void
601 stp_port_set_speed(struct stp_port *p, unsigned int speed)
602 {
603     stp_port_set_path_cost(p, (speed >= 10000 ? 2  /* 10 Gb/s. */
604                                : speed >= 1000 ? 4 /* 1 Gb/s. */
605                                : speed >= 100 ? 19 /* 100 Mb/s. */
606                                : speed >= 16 ? 62  /* 16 Mb/s. */
607                                : speed >= 10 ? 100 /* 10 Mb/s. */
608                                : speed >= 4 ? 250  /* 4 Mb/s. */
609                                : 19));             /* 100 Mb/s (guess). */
610 }
611
612 /* Enables topology change detection on port 'p'. */
613 void
614 stp_port_enable_change_detection(struct stp_port *p)
615 {
616     p->change_detection_enabled = true;
617 }
618
619 /* Disables topology change detection on port 'p'. */
620 void
621 stp_port_disable_change_detection(struct stp_port *p)
622 {
623     p->change_detection_enabled = false;
624 }
625 \f
626 static void
627 stp_transmit_config(struct stp_port *p)
628 {
629     struct stp *stp = p->stp;
630     bool root = stp_is_root_bridge(stp);
631     if (!root && !stp->root_port) {
632         return;
633     }
634     if (p->hold_timer.active) {
635         p->config_pending = true;
636     } else {
637         struct stp_config_bpdu config;
638         memset(&config, 0, sizeof config);
639         config.header.protocol_id = htons(STP_PROTOCOL_ID);
640         config.header.protocol_version = STP_PROTOCOL_VERSION;
641         config.header.bpdu_type = STP_TYPE_CONFIG;
642         config.flags = 0;
643         if (p->topology_change_ack) {
644             config.flags |= htons(STP_CONFIG_TOPOLOGY_CHANGE_ACK);
645         }
646         if (stp->topology_change) {
647             config.flags |= htons(STP_CONFIG_TOPOLOGY_CHANGE);
648         }
649         config.root_id = htonll(stp->designated_root);
650         config.root_path_cost = htonl(stp->root_path_cost);
651         config.bridge_id = htonll(stp->bridge_id);
652         config.port_id = htons(p->port_id);
653         if (root) {
654             config.message_age = htons(0);
655         } else {
656             config.message_age = htons(stp->root_port->message_age_timer.value
657                                        + MESSAGE_AGE_INCREMENT);
658         }
659         config.max_age = htons(stp->max_age);
660         config.hello_time = htons(stp->hello_time);
661         config.forward_delay = htons(stp->forward_delay);
662         if (ntohs(config.message_age) < stp->max_age) {
663             p->topology_change_ack = false;
664             p->config_pending = false;
665             stp->send_bpdu(&config, sizeof config, stp_port_no(p), stp->aux);
666             stp_start_timer(&p->hold_timer, 0);
667         }
668     }
669 }
670
671 static bool
672 stp_supersedes_port_info(const struct stp_port *p,
673                          const struct stp_config_bpdu *config)
674 {
675     if (ntohll(config->root_id) != p->designated_root) {
676         return ntohll(config->root_id) < p->designated_root;
677     } else if (ntohl(config->root_path_cost) != p->designated_cost) {
678         return ntohl(config->root_path_cost) < p->designated_cost;
679     } else if (ntohll(config->bridge_id) != p->designated_bridge) {
680         return ntohll(config->bridge_id) < p->designated_bridge;
681     } else {
682         return (ntohll(config->bridge_id) != p->stp->bridge_id
683                 || ntohs(config->port_id) <= p->designated_port);
684     }
685 }
686
687 static void
688 stp_record_config_information(struct stp_port *p,
689                               const struct stp_config_bpdu *config)
690 {
691     p->designated_root = ntohll(config->root_id);
692     p->designated_cost = ntohl(config->root_path_cost);
693     p->designated_bridge = ntohll(config->bridge_id);
694     p->designated_port = ntohs(config->port_id);
695     stp_start_timer(&p->message_age_timer, ntohs(config->message_age));
696 }
697
698 static void
699 stp_record_config_timeout_values(struct stp *stp,
700                                  const struct stp_config_bpdu  *config)
701 {
702     stp->max_age = ntohs(config->max_age);
703     stp->hello_time = ntohs(config->hello_time);
704     stp->forward_delay = ntohs(config->forward_delay);
705     stp->topology_change = config->flags & htons(STP_CONFIG_TOPOLOGY_CHANGE);
706 }
707
708 static bool
709 stp_is_designated_port(const struct stp_port *p)
710 {
711     return (p->designated_bridge == p->stp->bridge_id
712             && p->designated_port == p->port_id);
713 }
714
715 static void
716 stp_config_bpdu_generation(struct stp *stp)
717 {
718     struct stp_port *p;
719
720     FOR_EACH_ENABLED_PORT (p, stp) {
721         if (stp_is_designated_port(p)) {
722             stp_transmit_config(p);
723         }
724     }
725 }
726
727 static void
728 stp_transmit_tcn(struct stp *stp)
729 {
730     struct stp_port *p = stp->root_port;
731     struct stp_tcn_bpdu tcn_bpdu;
732     if (!p) {
733         return;
734     }
735     tcn_bpdu.header.protocol_id = htons(STP_PROTOCOL_ID);
736     tcn_bpdu.header.protocol_version = STP_PROTOCOL_VERSION;
737     tcn_bpdu.header.bpdu_type = STP_TYPE_TCN;
738     stp->send_bpdu(&tcn_bpdu, sizeof tcn_bpdu, stp_port_no(p), stp->aux);
739 }
740
741 static void
742 stp_configuration_update(struct stp *stp)
743 {
744     stp_root_selection(stp);
745     stp_designated_port_selection(stp);
746 }
747
748 static bool
749 stp_supersedes_root(const struct stp_port *root, const struct stp_port *p)
750 {
751     int p_cost = p->designated_cost + p->path_cost;
752     int root_cost = root->designated_cost + root->path_cost;
753
754     if (p->designated_root != root->designated_root) {
755         return p->designated_root < root->designated_root;
756     } else if (p_cost != root_cost) {
757         return p_cost < root_cost;
758     } else if (p->designated_bridge != root->designated_bridge) {
759         return p->designated_bridge < root->designated_bridge;
760     } else if (p->designated_port != root->designated_port) {
761         return p->designated_port < root->designated_port;
762     } else {
763         return p->port_id < root->port_id;
764     }
765 }
766
767 static void
768 stp_root_selection(struct stp *stp)
769 {
770     struct stp_port *p, *root;
771
772     root = NULL;
773     FOR_EACH_ENABLED_PORT (p, stp) {
774         if (stp_is_designated_port(p)
775             || p->designated_root >= stp->bridge_id) {
776             continue;
777         }
778         if (root && !stp_supersedes_root(root, p)) {
779             continue;
780         }
781         root = p;
782     }
783     stp->root_port = root;
784     if (!root) {
785         stp->designated_root = stp->bridge_id;
786         stp->root_path_cost = 0;
787     } else {
788         stp->designated_root = root->designated_root;
789         stp->root_path_cost = root->designated_cost + root->path_cost;
790     }
791 }
792
793 static void
794 stp_designated_port_selection(struct stp *stp)
795 {
796     struct stp_port *p;
797
798     FOR_EACH_ENABLED_PORT (p, stp) {
799         if (stp_is_designated_port(p)
800             || p->designated_root != stp->designated_root
801             || stp->root_path_cost < p->designated_cost
802             || (stp->root_path_cost == p->designated_cost
803                 && (stp->bridge_id < p->designated_bridge
804                     || (stp->bridge_id == p->designated_bridge
805                         && p->port_id <= p->designated_port))))
806         {
807             stp_become_designated_port(p);
808         }
809     }
810 }
811
812 static void
813 stp_become_designated_port(struct stp_port *p)
814 {
815     struct stp *stp = p->stp;
816     p->designated_root = stp->designated_root;
817     p->designated_cost = stp->root_path_cost;
818     p->designated_bridge = stp->bridge_id;
819     p->designated_port = p->port_id;
820 }
821
822 static void
823 stp_port_state_selection(struct stp *stp)
824 {
825     struct stp_port *p;
826
827     FOR_EACH_ENABLED_PORT (p, stp) {
828         if (p == stp->root_port) {
829             p->config_pending = false;
830             p->topology_change_ack = false;
831             stp_make_forwarding(p);
832         } else if (stp_is_designated_port(p)) {
833             stp_stop_timer(&p->message_age_timer);
834             stp_make_forwarding(p);
835         } else {
836             p->config_pending = false;
837             p->topology_change_ack = false;
838             stp_make_blocking(p);
839         }
840     }
841 }
842
843 static void
844 stp_make_forwarding(struct stp_port *p)
845 {
846     if (p->state == STP_BLOCKING) {
847         stp_set_port_state(p, STP_LISTENING);
848         stp_start_timer(&p->forward_delay_timer, 0);
849     }
850 }
851
852 static void
853 stp_make_blocking(struct stp_port *p)
854 {
855     if (!(p->state & (STP_DISABLED | STP_BLOCKING))) {
856         if (p->state & (STP_FORWARDING | STP_LEARNING)) {
857             if (p->change_detection_enabled) {
858                 stp_topology_change_detection(p->stp);
859             }
860         }
861         stp_set_port_state(p, STP_BLOCKING);
862         stp_stop_timer(&p->forward_delay_timer);
863     }
864 }
865
866 static void
867 stp_set_port_state(struct stp_port *p, enum stp_state state)
868 {
869     if (state != p->state && !p->state_changed) {
870         p->state_changed = true;
871         if (p < p->stp->first_changed_port) {
872             p->stp->first_changed_port = p;
873         }
874     }
875     p->state = state;
876 }
877
878 static void
879 stp_topology_change_detection(struct stp *stp)
880 {
881     if (stp_is_root_bridge(stp)) {
882         stp->topology_change = true;
883         stp_start_timer(&stp->topology_change_timer, 0);
884     } else if (!stp->topology_change_detected) {
885         stp_transmit_tcn(stp);
886         stp_start_timer(&stp->tcn_timer, 0);
887     }
888     stp->topology_change_detected = true;
889 }
890
891 static void
892 stp_topology_change_acknowledged(struct stp *stp)
893 {
894     stp->topology_change_detected = false;
895     stp_stop_timer(&stp->tcn_timer);
896 }
897
898 static void
899 stp_acknowledge_topology_change(struct stp_port *p)
900 {
901     p->topology_change_ack = true;
902     stp_transmit_config(p);
903 }
904
905 void
906 stp_received_config_bpdu(struct stp *stp, struct stp_port *p,
907                          const struct stp_config_bpdu *config)
908 {
909     if (ntohs(config->message_age) >= ntohs(config->max_age)) {
910         VLOG_WARN("%s: received config BPDU with message age (%u) greater "
911                   "than max age (%u)",
912                   stp->name,
913                   ntohs(config->message_age), ntohs(config->max_age));
914         return;
915     }
916     if (p->state != STP_DISABLED) {
917         bool root = stp_is_root_bridge(stp);
918         if (stp_supersedes_port_info(p, config)) {
919             stp_record_config_information(p, config);
920             stp_configuration_update(stp);
921             stp_port_state_selection(stp);
922             if (!stp_is_root_bridge(stp) && root) {
923                 stp_stop_timer(&stp->hello_timer);
924                 if (stp->topology_change_detected) {
925                     stp_stop_timer(&stp->topology_change_timer);
926                     stp_transmit_tcn(stp);
927                     stp_start_timer(&stp->tcn_timer, 0);
928                 }
929             }
930             if (p == stp->root_port) {
931                 stp_record_config_timeout_values(stp, config);
932                 stp_config_bpdu_generation(stp);
933                 if (config->flags & htons(STP_CONFIG_TOPOLOGY_CHANGE_ACK)) {
934                     stp_topology_change_acknowledged(stp);
935                 }
936             }
937         } else if (stp_is_designated_port(p)) {
938             stp_transmit_config(p);
939         }
940     }
941 }
942
943 void
944 stp_received_tcn_bpdu(struct stp *stp, struct stp_port *p,
945                       const struct stp_tcn_bpdu *tcn)
946 {
947     if (p->state != STP_DISABLED) {
948         if (stp_is_designated_port(p)) {
949             stp_topology_change_detection(stp);
950             stp_acknowledge_topology_change(p);
951         }
952     }
953 }
954
955 static void
956 stp_hello_timer_expiry(struct stp *stp)
957 {
958     stp_config_bpdu_generation(stp);
959     stp_start_timer(&stp->hello_timer, 0);
960 }
961
962 static void
963 stp_message_age_timer_expiry(struct stp_port *p)
964 {
965     struct stp *stp = p->stp;
966     bool root = stp_is_root_bridge(stp);
967     stp_become_designated_port(p);
968     stp_configuration_update(stp);
969     stp_port_state_selection(stp);
970     if (stp_is_root_bridge(stp) && !root) {
971         stp->max_age = stp->bridge_max_age;
972         stp->hello_time = stp->bridge_hello_time;
973         stp->forward_delay = stp->bridge_forward_delay;
974         stp_topology_change_detection(stp);
975         stp_stop_timer(&stp->tcn_timer);
976         stp_config_bpdu_generation(stp);
977         stp_start_timer(&stp->hello_timer, 0);
978     }
979 }
980
981 static bool
982 stp_is_designated_for_some_port(const struct stp *stp)
983 {
984     const struct stp_port *p;
985
986     FOR_EACH_ENABLED_PORT (p, stp) {
987         if (p->designated_bridge == stp->bridge_id) {
988             return true;
989         }
990     }
991     return false;
992 }
993
994 static void
995 stp_forward_delay_timer_expiry(struct stp_port *p)
996 {
997     if (p->state == STP_LISTENING) {
998         stp_set_port_state(p, STP_LEARNING);
999         stp_start_timer(&p->forward_delay_timer, 0);
1000     } else if (p->state == STP_LEARNING) {
1001         stp_set_port_state(p, STP_FORWARDING);
1002         if (stp_is_designated_for_some_port(p->stp)) {
1003             if (p->change_detection_enabled) {
1004                 stp_topology_change_detection(p->stp);
1005             }
1006         }
1007     }
1008 }
1009
1010 static void
1011 stp_tcn_timer_expiry(struct stp *stp)
1012 {
1013     stp_transmit_tcn(stp);
1014     stp_start_timer(&stp->tcn_timer, 0);
1015 }
1016
1017 static void
1018 stp_topology_change_timer_expiry(struct stp *stp)
1019 {
1020     stp->topology_change_detected = false;
1021     stp->topology_change = false;
1022 }
1023
1024 static void
1025 stp_hold_timer_expiry(struct stp_port *p)
1026 {
1027     if (p->config_pending) {
1028         stp_transmit_config(p);
1029     }
1030 }
1031
1032 static void
1033 stp_initialize_port(struct stp_port *p, enum stp_state state)
1034 {
1035     assert(state & (STP_DISABLED | STP_BLOCKING));
1036     stp_become_designated_port(p);
1037     stp_set_port_state(p, state);
1038     p->topology_change_ack = false;
1039     p->config_pending = false;
1040     p->change_detection_enabled = true;
1041     stp_stop_timer(&p->message_age_timer);
1042     stp_stop_timer(&p->forward_delay_timer);
1043     stp_stop_timer(&p->hold_timer);
1044 }
1045
1046 static void
1047 stp_become_root_bridge(struct stp *stp)
1048 {
1049     stp->max_age = stp->bridge_max_age;
1050     stp->hello_time = stp->bridge_hello_time;
1051     stp->forward_delay = stp->bridge_forward_delay;
1052     stp_topology_change_detection(stp);
1053     stp_stop_timer(&stp->tcn_timer);
1054     stp_config_bpdu_generation(stp);
1055     stp_start_timer(&stp->hello_timer, 0);
1056 }
1057
1058 static void
1059 stp_start_timer(struct stp_timer *timer, int value)
1060 {
1061     timer->value = value;
1062     timer->active = true;
1063 }
1064
1065 static void
1066 stp_stop_timer(struct stp_timer *timer)
1067 {
1068     timer->active = false;
1069 }
1070
1071 static bool
1072 stp_timer_expired(struct stp_timer *timer, int elapsed, int timeout)
1073 {
1074     if (timer->active) {
1075         timer->value += elapsed;
1076         if (timer->value >= timeout) {
1077             timer->active = false;
1078             return true;
1079         }
1080     }
1081     return false;
1082 }
1083