/*
- * Copyright (c) 2008, 2009 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
/* Based on sample implementation in 802.1D-1998. Above copyright and license
* applies to all modifications. */
+#include <config.h>
+
#include "stp.h"
+#include <sys/types.h>
+#include <netinet/in.h>
#include <arpa/inet.h>
-#include <assert.h>
#include <inttypes.h>
#include <stdlib.h>
+#include "byte-order.h"
#include "ofpbuf.h"
#include "packets.h"
+#include "unixctl.h"
#include "util.h"
-#include "xtoxll.h"
-
#include "vlog.h"
-#define THIS_MODULE VLM_stp
-/* Ethernet address used as the destination for STP frames. */
-const uint8_t stp_eth_addr[ETH_ADDR_LEN]
-= { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x01 };
+VLOG_DEFINE_THIS_MODULE(stp);
#define STP_PROTOCOL_ID 0x0000
#define STP_PROTOCOL_VERSION 0x00
#define STP_TYPE_TCN 0x80
struct stp_bpdu_header {
- uint16_t protocol_id; /* STP_PROTOCOL_ID. */
+ ovs_be16 protocol_id; /* STP_PROTOCOL_ID. */
uint8_t protocol_version; /* STP_PROTOCOL_VERSION. */
uint8_t bpdu_type; /* One of STP_TYPE_*. */
} __attribute__((packed));
struct stp_config_bpdu {
struct stp_bpdu_header header; /* Type STP_TYPE_CONFIG. */
uint8_t flags; /* STP_CONFIG_* flags. */
- uint64_t root_id; /* 8.5.1.1: Bridge believed to be root. */
- uint32_t root_path_cost; /* 8.5.1.2: Cost of path to root. */
- uint64_t bridge_id; /* 8.5.1.3: ID of transmitting bridge. */
- uint16_t port_id; /* 8.5.1.4: Port transmitting the BPDU. */
- uint16_t message_age; /* 8.5.1.5: Age of BPDU at tx time. */
- uint16_t max_age; /* 8.5.1.6: Timeout for received data. */
- uint16_t hello_time; /* 8.5.1.7: Time between BPDU generation. */
- uint16_t forward_delay; /* 8.5.1.8: State progression delay. */
+ ovs_be64 root_id; /* 8.5.1.1: Bridge believed to be root. */
+ ovs_be32 root_path_cost; /* 8.5.1.2: Cost of path to root. */
+ ovs_be64 bridge_id; /* 8.5.1.3: ID of transmitting bridge. */
+ ovs_be16 port_id; /* 8.5.1.4: Port transmitting the BPDU. */
+ ovs_be16 message_age; /* 8.5.1.5: Age of BPDU at tx time. */
+ ovs_be16 max_age; /* 8.5.1.6: Timeout for received data. */
+ ovs_be16 hello_time; /* 8.5.1.7: Time between BPDU generation. */
+ ovs_be16 forward_delay; /* 8.5.1.8: State progression delay. */
} __attribute__((packed));
BUILD_ASSERT_DECL(sizeof(struct stp_config_bpdu) == 35);
struct stp_port {
struct stp *stp;
+ void *aux; /* Auxiliary data the user may retrieve. */
int port_id; /* 8.5.5.1: Unique port identifier. */
enum stp_state state; /* 8.5.5.2: Current state. */
int path_cost; /* 8.5.5.3: Cost of tx/rx on this port. */
struct stp_timer forward_delay_timer; /* 8.5.6.2: State change timer. */
struct stp_timer hold_timer; /* 8.5.6.3: BPDU rate limit timer. */
+ int tx_count; /* Number of BPDUs transmitted. */
+ int rx_count; /* Number of valid BPDUs received. */
+ int error_count; /* Number of bad BPDUs received. */
+
bool state_changed;
};
struct stp {
+ struct list node; /* Node in all_stps list. */
+
/* Static bridge data. */
char *name; /* Human-readable name for log messages. */
stp_identifier bridge_id; /* 8.5.3.7: This bridge. */
struct stp_port ports[STP_MAX_PORTS];
/* Interface to client. */
+ bool fdb_needs_flush; /* MAC learning tables needs flushing. */
struct stp_port *first_changed_port;
void (*send_bpdu)(struct ofpbuf *bpdu, int port_no, void *aux);
void *aux;
};
+static struct list all_stps = LIST_INITIALIZER(&all_stps);
+
#define FOR_EACH_ENABLED_PORT(PORT, STP) \
for ((PORT) = stp_next_enabled_port((STP), (STP)->ports); \
(PORT); \
{
for (; port < &stp->ports[ARRAY_SIZE(stp->ports)]; port++) {
if (port->state != STP_DISABLED) {
- return (struct stp_port *) port;
+ return CONST_CAST(struct stp_port *, port);
}
}
return NULL;
static int clamp(int x, int min, int max);
static int ms_to_timer(int ms);
-static int ms_to_timer_remainder(int ms);
static int timer_to_ms(int timer);
static void stp_start_timer(struct stp_timer *, int value);
static void stp_stop_timer(struct stp_timer *);
static bool stp_timer_expired(struct stp_timer *, int elapsed, int timeout);
static void stp_send_bpdu(struct stp_port *, const void *, size_t);
+static void stp_unixctl_tcn(struct unixctl_conn *, int argc,
+ const char *argv[], void *aux);
+
+void
+stp_init(void)
+{
+ unixctl_command_register("stp/tcn", "[bridge]", 0, 1, stp_unixctl_tcn,
+ NULL);
+}
/* Creates and returns a new STP instance that initially has no ports enabled.
*
*
* When the bridge needs to send out a BPDU, it calls 'send_bpdu'. This
* callback may be called from stp_tick() or stp_received_bpdu(). The
- * arguments to 'send_bpdu' are an STP BPDU encapsulated in
+ * arguments to 'send_bpdu' are an STP BPDU encapsulated in 'bpdu',
+ * the spanning tree port number 'port_no' that should transmit the
+ * packet, and auxiliary data to be passed to the callback in 'aux'.
*/
struct stp *
stp_create(const char *name, stp_identifier bridge_id,
stp->bridge_id |= (uint64_t) STP_DEFAULT_BRIDGE_PRIORITY << 48;
}
- stp->rq_max_age = 6000;
- stp->rq_hello_time = 2000;
- stp->rq_forward_delay = 4000;
+ stp->rq_max_age = STP_DEFAULT_MAX_AGE;
+ stp->rq_hello_time = STP_DEFAULT_HELLO_TIME;
+ stp->rq_forward_delay = STP_DEFAULT_FWD_DELAY;
stp_update_bridge_timers(stp);
stp->max_age = stp->bridge_max_age;
stp->hello_time = stp->bridge_hello_time;
p->path_cost = 19; /* Recommended default for 100 Mb/s link. */
stp_initialize_port(p, STP_DISABLED);
}
+ list_push_back(&all_stps, &stp->node);
return stp;
}
void
stp_destroy(struct stp *stp)
{
- free(stp);
+ if (stp) {
+ list_remove(&stp->node);
+ free(stp->name);
+ free(stp);
+ }
}
/* Runs 'stp' given that 'ms' milliseconds have passed. */
* are called too frequently. */
ms = clamp(ms, 0, INT_MAX - 1000) + stp->elapsed_remainder;
elapsed = ms_to_timer(ms);
- stp->elapsed_remainder = ms_to_timer_remainder(ms);
+ stp->elapsed_remainder = ms - timer_to_ms(elapsed);
if (!elapsed) {
return;
}
return timer_to_ms(stp->bridge_forward_delay);
}
+/* Returns true if something has happened to 'stp' which necessitates flushing
+ * the client's MAC learning table. Calling this function resets 'stp' so that
+ * future calls will return false until flushing is required again. */
+bool
+stp_check_and_reset_fdb_flush(struct stp *stp)
+{
+ bool needs_flush = stp->fdb_needs_flush;
+ stp->fdb_needs_flush = false;
+ return needs_flush;
+}
+
/* Returns the port in 'stp' with index 'port_no', which must be between 0 and
* STP_MAX_PORTS. */
struct stp_port *
stp_get_port(struct stp *stp, int port_no)
{
- assert(port_no >= 0 && port_no < ARRAY_SIZE(stp->ports));
+ ovs_assert(port_no >= 0 && port_no < ARRAY_SIZE(stp->ports));
return &stp->ports[port_no];
}
return (state & (STP_DISABLED | STP_LEARNING | STP_FORWARDING)) != 0;
}
+/* Returns the name for the given 'role' (for use in debugging and log
+ * messages). */
+const char *
+stp_role_name(enum stp_role role)
+{
+ switch (role) {
+ case STP_ROLE_ROOT:
+ return "root";
+ case STP_ROLE_DESIGNATED:
+ return "designated";
+ case STP_ROLE_ALTERNATE:
+ return "alternate";
+ case STP_ROLE_DISABLED:
+ return "disabled";
+ default:
+ NOT_REACHED();
+ }
+}
+
/* Notifies the STP entity that bridge protocol data unit 'bpdu', which is
* 'bpdu_size' bytes in length, was received on port 'p'.
*
if (bpdu_size < sizeof(struct stp_bpdu_header)) {
VLOG_WARN("%s: received runt %zu-byte BPDU", stp->name, bpdu_size);
+ p->error_count++;
return;
}
if (header->protocol_id != htons(STP_PROTOCOL_ID)) {
VLOG_WARN("%s: received BPDU with unexpected protocol ID %"PRIu16,
stp->name, ntohs(header->protocol_id));
+ p->error_count++;
return;
}
if (header->protocol_version != STP_PROTOCOL_VERSION) {
if (bpdu_size < sizeof(struct stp_config_bpdu)) {
VLOG_WARN("%s: received config BPDU with invalid size %zu",
stp->name, bpdu_size);
+ p->error_count++;
return;
}
stp_received_config_bpdu(stp, p, bpdu);
if (bpdu_size != sizeof(struct stp_tcn_bpdu)) {
VLOG_WARN("%s: received TCN BPDU with invalid size %zu",
stp->name, bpdu_size);
+ p->error_count++;
return;
}
stp_received_tcn_bpdu(stp, p);
default:
VLOG_WARN("%s: received BPDU of unexpected type %"PRIu8,
stp->name, header->bpdu_type);
+ p->error_count++;
return;
}
+ p->rx_count++;
}
/* Returns the STP entity in which 'p' is nested. */
return p->stp;
}
+/* Sets the 'aux' member of 'p'.
+ *
+ * The 'aux' member will be reset to NULL when stp_port_disable() is
+ * called or stp_port_enable() is called when the port is in a Disabled
+ * state. */
+void
+stp_port_set_aux(struct stp_port *p, void *aux)
+{
+ p->aux = aux;
+}
+
+/* Returns the 'aux' member of 'p'. */
+void *
+stp_port_get_aux(struct stp_port *p)
+{
+ return p->aux;
+}
+
/* Returns the index of port 'p' within its bridge. */
int
stp_port_no(const struct stp_port *p)
{
struct stp *stp = p->stp;
- assert(p >= stp->ports && p < &stp->ports[ARRAY_SIZE(stp->ports)]);
+ ovs_assert(p >= stp->ports && p < &stp->ports[ARRAY_SIZE(stp->ports)]);
return p - stp->ports;
}
+/* Returns the port ID for 'p'. */
+int
+stp_port_get_id(const struct stp_port *p)
+{
+ return p->port_id;
+}
+
/* Returns the state of port 'p'. */
enum stp_state
stp_port_get_state(const struct stp_port *p)
return p->state;
}
+/* Returns the role of port 'p'. */
+enum stp_role
+stp_port_get_role(const struct stp_port *p)
+{
+ struct stp_port *root_port = stp_get_root_port(p->stp);
+
+ if (root_port && root_port->port_id == p->port_id) {
+ return STP_ROLE_ROOT;
+ } else if (stp_is_designated_port(p)) {
+ return STP_ROLE_DESIGNATED;
+ } else if (p->state == STP_DISABLED) {
+ return STP_ROLE_DISABLED;
+ } else {
+ return STP_ROLE_ALTERNATE;
+ }
+}
+
+/* Retrieves BPDU transmit and receive counts for 'p'. */
+void stp_port_get_counts(const struct stp_port *p,
+ int *tx_count, int *rx_count, int *error_count)
+{
+ *tx_count = p->tx_count;
+ *rx_count = p->rx_count;
+ *error_count = p->error_count;
+}
+
/* Disables STP on port 'p'. */
void
stp_port_disable(struct stp_port *p)
if (stp_is_root_bridge(stp) && !root) {
stp_become_root_bridge(stp);
}
+ p->aux = NULL;
}
}
}
}
+/* Convert 'speed' (measured in Mb/s) into the path cost. */
+uint16_t
+stp_convert_speed_to_cost(unsigned int speed)
+{
+ return speed >= 10000 ? 2 /* 10 Gb/s. */
+ : speed >= 1000 ? 4 /* 1 Gb/s. */
+ : speed >= 100 ? 19 /* 100 Mb/s. */
+ : speed >= 16 ? 62 /* 16 Mb/s. */
+ : speed >= 10 ? 100 /* 10 Mb/s. */
+ : speed >= 4 ? 250 /* 4 Mb/s. */
+ : 19; /* 100 Mb/s (guess). */
+}
+
/* Sets the path cost of port 'p' to 'path_cost'. Lower values are generally
* used to indicate faster links. Use stp_port_set_speed() to automatically
* generate a default path cost from a link speed. */
void
stp_port_set_speed(struct stp_port *p, unsigned int speed)
{
- stp_port_set_path_cost(p, (speed >= 10000 ? 2 /* 10 Gb/s. */
- : speed >= 1000 ? 4 /* 1 Gb/s. */
- : speed >= 100 ? 19 /* 100 Mb/s. */
- : speed >= 16 ? 62 /* 16 Mb/s. */
- : speed >= 10 ? 100 /* 10 Mb/s. */
- : speed >= 4 ? 250 /* 4 Mb/s. */
- : 19)); /* 100 Mb/s (guess). */
+ stp_port_set_path_cost(p, stp_convert_speed_to_cost(speed));
}
/* Enables topology change detection on port 'p'. */
config.header.bpdu_type = STP_TYPE_CONFIG;
config.flags = 0;
if (p->topology_change_ack) {
- config.flags |= htons(STP_CONFIG_TOPOLOGY_CHANGE_ACK);
+ config.flags |= STP_CONFIG_TOPOLOGY_CHANGE_ACK;
}
if (stp->topology_change) {
- config.flags |= htons(STP_CONFIG_TOPOLOGY_CHANGE);
+ config.flags |= STP_CONFIG_TOPOLOGY_CHANGE;
}
config.root_id = htonll(stp->designated_root);
config.root_path_cost = htonl(stp->root_path_cost);
stp->max_age = ntohs(config->max_age);
stp->hello_time = ntohs(config->hello_time);
stp->forward_delay = ntohs(config->forward_delay);
- stp->topology_change = config->flags & htons(STP_CONFIG_TOPOLOGY_CHANGE);
+ stp->topology_change = config->flags & STP_CONFIG_TOPOLOGY_CHANGE;
}
static bool
static void
stp_topology_change_detection(struct stp *stp)
{
+ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
+
if (stp_is_root_bridge(stp)) {
stp->topology_change = true;
stp_start_timer(&stp->topology_change_timer, 0);
stp_transmit_tcn(stp);
stp_start_timer(&stp->tcn_timer, 0);
}
+ stp->fdb_needs_flush = true;
stp->topology_change_detected = true;
+ VLOG_INFO_RL(&rl, "%s: detected topology change.", stp->name);
}
static void
stp_transmit_config(p);
}
-void
+static void
stp_received_config_bpdu(struct stp *stp, struct stp_port *p,
const struct stp_config_bpdu *config)
{
if (p == stp->root_port) {
stp_record_config_timeout_values(stp, config);
stp_config_bpdu_generation(stp);
- if (config->flags & htons(STP_CONFIG_TOPOLOGY_CHANGE_ACK)) {
+ if (config->flags & STP_CONFIG_TOPOLOGY_CHANGE_ACK) {
stp_topology_change_acknowledged(stp);
}
+ if (config->flags & STP_CONFIG_TOPOLOGY_CHANGE) {
+ stp->fdb_needs_flush = true;
+ }
}
} else if (stp_is_designated_port(p)) {
stp_transmit_config(p);
}
}
-void
+static void
stp_received_tcn_bpdu(struct stp *stp, struct stp_port *p)
{
if (p->state != STP_DISABLED) {
static void
stp_initialize_port(struct stp_port *p, enum stp_state state)
{
- assert(state & (STP_DISABLED | STP_BLOCKING));
+ ovs_assert(state & (STP_DISABLED | STP_BLOCKING));
stp_become_designated_port(p);
stp_set_port_state(p, state);
p->topology_change_ack = false;
p->config_pending = false;
p->change_detection_enabled = true;
+ p->aux = NULL;
stp_stop_timer(&p->message_age_timer);
stp_stop_timer(&p->forward_delay_timer);
stp_stop_timer(&p->hold_timer);
+ p->tx_count = p->rx_count = p->error_count = 0;
}
static void
return ms * 0x100 / 1000;
}
-/* Returns the number of leftover milliseconds when 'ms' is converted to STP
- * timer ticks. */
-static int
-ms_to_timer_remainder(int ms)
-{
- return ms * 0x100 % 1000;
-}
-
/* Returns the number of whole milliseconds in 'timer' STP timer ticks. There
* are 256 STP timer ticks per second. */
static int
pkt->l3 = ofpbuf_put(pkt, bpdu, bpdu_size);
/* 802.2 header. */
- memcpy(eth->eth_dst, stp_eth_addr, ETH_ADDR_LEN);
+ memcpy(eth->eth_dst, eth_addr_stp, ETH_ADDR_LEN);
/* p->stp->send_bpdu() must fill in source address. */
eth->eth_type = htons(pkt->size - ETH_HEADER_LEN);
llc->llc_cntl = STP_LLC_CNTL;
p->stp->send_bpdu(pkt, stp_port_no(p), p->stp->aux);
+ p->tx_count++;
+}
+\f
+/* Unixctl. */
+
+static struct stp *
+stp_find(const char *name)
+{
+ struct stp *stp;
+
+ LIST_FOR_EACH (stp, node, &all_stps) {
+ if (!strcmp(stp->name, name)) {
+ return stp;
+ }
+ }
+ return NULL;
+}
+
+static void
+stp_unixctl_tcn(struct unixctl_conn *conn, int argc,
+ const char *argv[], void *aux OVS_UNUSED)
+{
+ if (argc > 1) {
+ struct stp *stp = stp_find(argv[1]);
+
+ if (!stp) {
+ unixctl_command_reply_error(conn, "no such stp object");
+ return;
+ }
+ stp_topology_change_detection(stp);
+ } else {
+ struct stp *stp;
+
+ LIST_FOR_EACH (stp, node, &all_stps) {
+ stp_topology_change_detection(stp);
+ }
+ }
+
+ unixctl_command_reply(conn, "OK");
}