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