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