X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fstp.c;h=94b9a4b5b993d17c5f0d1a8519f4262b011ceaa9;hb=23edef9e61fd8c89db4a238c5526b8f44e938aaf;hp=38885c07f666e472ce4691988863e7df7879e991;hpb=5f55c39b21e69025045437ffbd3bb98fe6ce2e89;p=sliver-openvswitch.git diff --git a/lib/stp.c b/lib/stp.c index 38885c07f..94b9a4b5b 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 Networks. * * 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,13 @@ #include #include #include +#include "byte-order.h" #include "ofpbuf.h" #include "packets.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 +40,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 +54,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 +77,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. */ @@ -206,7 +205,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, @@ -223,9 +224,9 @@ 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; @@ -526,6 +527,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'. * @@ -589,6 +609,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) @@ -598,6 +636,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) @@ -605,6 +650,23 @@ 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; + } +} + /* Disables STP on port 'p'. */ void stp_port_disable(struct stp_port *p) @@ -623,6 +685,7 @@ stp_port_disable(struct stp_port *p) if (stp_is_root_bridge(stp) && !root) { stp_become_root_bridge(stp); } + p->aux = NULL; } } @@ -656,6 +719,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. */ @@ -674,13 +750,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'. */ @@ -715,10 +785,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); @@ -776,7 +846,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 @@ -976,7 +1046,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) { @@ -1004,7 +1074,7 @@ 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); } } @@ -1014,7 +1084,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) { @@ -1111,6 +1181,7 @@ 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); @@ -1218,7 +1289,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);