X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fstp.c;h=ee5c8bcde84527c4509d46231915e56d272eeb8a;hb=e0edde6fee279cdbbf3c179f5f50adaf0c7c7f1e;hp=5d35156acb7ccef49e2be0f097fbc1a691ff4f3a;hpb=7f3adc00f8d9cc0e035cd58817a28301e62eaec8;p=sliver-openvswitch.git diff --git a/lib/stp.c b/lib/stp.c index 5d35156ac..ee5c8bcde 100644 --- a/lib/stp.c +++ b/lib/stp.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2009, 2010 Nicira Networks. + * Copyright (c) 2008, 2009, 2010, 2011 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,8 @@ /* Based on sample implementation in 802.1D-1998. Above copyright and license * applies to all modifications. */ +#include + #include "stp.h" #include #include @@ -24,17 +26,14 @@ #include #include #include +#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 @@ -42,7 +41,7 @@ const uint8_t stp_eth_addr[ETH_ADDR_LEN] #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)); @@ -56,14 +55,14 @@ enum stp_config_bpdu_flags { 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); @@ -79,6 +78,7 @@ struct stp_timer { 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. */ @@ -94,10 +94,16 @@ struct stp_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. */ @@ -128,11 +134,14 @@ struct stp { 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); \ @@ -189,13 +198,21 @@ static void stp_update_bridge_timers(struct stp *); 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. * @@ -206,7 +223,9 @@ static void stp_send_bpdu(struct stp_port *, const void *, size_t); * * 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, @@ -216,16 +235,16 @@ stp_create(const char *name, stp_identifier bridge_id, struct stp *stp; struct stp_port *p; - stp = xcalloc(1, sizeof *stp); + stp = xzalloc(sizeof *stp); stp->name = xstrdup(name); stp->bridge_id = bridge_id; if (!(stp->bridge_id >> 48)) { 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; @@ -251,6 +270,7 @@ stp_create(const char *name, stp_identifier bridge_id, 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; } @@ -258,7 +278,11 @@ stp_create(const char *name, stp_identifier bridge_id, 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. */ @@ -273,7 +297,7 @@ stp_tick(struct stp *stp, int ms) * 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; } @@ -441,6 +465,17 @@ stp_get_forward_delay(const struct stp *stp) 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 * @@ -523,6 +558,25 @@ stp_learn_in_state(enum stp_state state) 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'. * @@ -539,6 +593,7 @@ stp_received_bpdu(struct stp_port *p, const void *bpdu, size_t bpdu_size) if (bpdu_size < sizeof(struct stp_bpdu_header)) { VLOG_WARN("%s: received runt %zu-byte BPDU", stp->name, bpdu_size); + p->error_count++; return; } @@ -546,6 +601,7 @@ stp_received_bpdu(struct stp_port *p, const void *bpdu, size_t bpdu_size) 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) { @@ -558,6 +614,7 @@ stp_received_bpdu(struct stp_port *p, const void *bpdu, size_t bpdu_size) 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); @@ -567,6 +624,7 @@ stp_received_bpdu(struct stp_port *p, const void *bpdu, size_t bpdu_size) 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); @@ -575,8 +633,10 @@ stp_received_bpdu(struct stp_port *p, const void *bpdu, size_t bpdu_size) 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. */ @@ -586,6 +646,24 @@ stp_port_get_stp(struct stp_port *p) 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) @@ -595,6 +673,13 @@ stp_port_no(const struct stp_port *p) 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) @@ -602,6 +687,32 @@ 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) @@ -620,6 +731,7 @@ stp_port_disable(struct stp_port *p) if (stp_is_root_bridge(stp) && !root) { stp_become_root_bridge(stp); } + p->aux = NULL; } } @@ -653,6 +765,19 @@ stp_port_set_priority(struct stp_port *p, uint8_t new_priority) } } +/* 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. */ @@ -671,13 +796,7 @@ stp_port_set_path_cost(struct stp_port *p, uint16_t path_cost) 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'. */ @@ -712,10 +831,10 @@ stp_transmit_config(struct stp_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); @@ -773,7 +892,7 @@ stp_record_config_timeout_values(struct stp *stp, 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 @@ -949,6 +1068,8 @@ stp_set_port_state(struct stp_port *p, enum stp_state state) 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); @@ -956,7 +1077,9 @@ stp_topology_change_detection(struct stp *stp) 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 @@ -973,7 +1096,7 @@ stp_acknowledge_topology_change(struct stp_port *p) stp_transmit_config(p); } -void +static void stp_received_config_bpdu(struct stp *stp, struct stp_port *p, const struct stp_config_bpdu *config) { @@ -1001,9 +1124,12 @@ stp_received_config_bpdu(struct stp *stp, struct stp_port *p, 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); @@ -1011,7 +1137,7 @@ stp_received_config_bpdu(struct stp *stp, struct stp_port *p, } } -void +static void stp_received_tcn_bpdu(struct stp *stp, struct stp_port *p) { if (p->state != STP_DISABLED) { @@ -1108,9 +1234,11 @@ stp_initialize_port(struct stp_port *p, enum stp_state 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 @@ -1159,14 +1287,6 @@ ms_to_timer(int ms) 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 @@ -1215,7 +1335,7 @@ stp_send_bpdu(struct stp_port *p, const void *bpdu, size_t bpdu_size) 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); @@ -1225,4 +1345,43 @@ stp_send_bpdu(struct stp_port *p, const void *bpdu, size_t bpdu_size) llc->llc_cntl = STP_LLC_CNTL; p->stp->send_bpdu(pkt, stp_port_no(p), p->stp->aux); + p->tx_count++; +} + +/* 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"); }