bridgestp.h revision 164653
1/* $NetBSD: if_bridgevar.h,v 1.4 2003/07/08 07:13:50 itojun Exp $ */ 2 3/* 4 * Copyright 2001 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38/* 39 * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net) 40 * All rights reserved. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. All advertising materials mentioning features or use of this software 51 * must display the following acknowledgement: 52 * This product includes software developed by Jason L. Wright 53 * 4. The name of the author may not be used to endorse or promote products 54 * derived from this software without specific prior written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 57 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 58 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 59 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 60 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 61 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 62 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 64 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 65 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 66 * POSSIBILITY OF SUCH DAMAGE. 67 * 68 * OpenBSD: if_bridge.h,v 1.14 2001/03/22 03:48:29 jason Exp 69 * 70 * $FreeBSD: head/sys/net/bridgestp.h 164653 2006-11-27 00:35:40Z thompsa $ 71 */ 72 73/* 74 * Data structure and control definitions for STP interfaces. 75 */ 76 77#include <sys/callout.h> 78#include <sys/queue.h> 79 80/* STP port states */ 81#define BSTP_IFSTATE_DISABLED 0 82#define BSTP_IFSTATE_LISTENING 1 83#define BSTP_IFSTATE_LEARNING 2 84#define BSTP_IFSTATE_FORWARDING 3 85#define BSTP_IFSTATE_BLOCKING 4 86#define BSTP_IFSTATE_DISCARDING 5 87 88#define BSTP_TCSTATE_ACTIVE 1 89#define BSTP_TCSTATE_DETECTED 2 90#define BSTP_TCSTATE_INACTIVE 3 91#define BSTP_TCSTATE_LEARNING 4 92#define BSTP_TCSTATE_PROPAG 5 93#define BSTP_TCSTATE_ACK 6 94#define BSTP_TCSTATE_TC 7 95#define BSTP_TCSTATE_TCN 8 96 97#define BSTP_ROLE_DISABLED 0 98#define BSTP_ROLE_ROOT 1 99#define BSTP_ROLE_DESIGNATED 2 100#define BSTP_ROLE_ALTERNATE 3 101#define BSTP_ROLE_BACKUP 4 102 103#ifdef _KERNEL 104 105/* STP port flags */ 106#define BSTP_PORT_CANMIGRATE 0x0001 107#define BSTP_PORT_NEWINFO 0x0002 108#define BSTP_PORT_DISPUTED 0x0004 109#define BSTP_PORT_ADMCOST 0x0008 110#define BSTP_PORT_AUTOEDGE 0x0010 111#define BSTP_PORT_AUTOP2P 0x0020 112 113/* BPDU priority */ 114#define BSTP_PDU_SUPERIOR 1 115#define BSTP_PDU_REPEATED 2 116#define BSTP_PDU_INFERIOR 3 117#define BSTP_PDU_INFERIORALT 4 118#define BSTP_PDU_OTHER 5 119 120/* BPDU flags */ 121#define BSTP_PDU_PRMASK 0x0c /* Port Role */ 122#define BSTP_PDU_PRSHIFT 2 /* Port Role offset */ 123#define BSTP_PDU_F_UNKN 0x00 /* Unknown port (00) */ 124#define BSTP_PDU_F_ALT 0x01 /* Alt/Backup port (01) */ 125#define BSTP_PDU_F_ROOT 0x02 /* Root port (10) */ 126#define BSTP_PDU_F_DESG 0x03 /* Designated port (11) */ 127 128#define BSTP_PDU_STPMASK 0x81 /* strip unused STP flags */ 129#define BSTP_PDU_RSTPMASK 0x7f /* strip unused RSTP flags */ 130#define BSTP_PDU_F_TC 0x01 /* Topology change */ 131#define BSTP_PDU_F_P 0x02 /* Proposal flag */ 132#define BSTP_PDU_F_L 0x10 /* Learning flag */ 133#define BSTP_PDU_F_F 0x20 /* Forwarding flag */ 134#define BSTP_PDU_F_A 0x40 /* Agreement flag */ 135#define BSTP_PDU_F_TCA 0x80 /* Topology change ack */ 136 137/* 138 * Spanning tree defaults. 139 */ 140#define BSTP_DEFAULT_MAX_AGE (20 * 256) 141#define BSTP_DEFAULT_HELLO_TIME (2 * 256) 142#define BSTP_DEFAULT_FORWARD_DELAY (15 * 256) 143#define BSTP_DEFAULT_HOLD_TIME (1 * 256) 144#define BSTP_DEFAULT_MIGRATE_DELAY (3 * 256) 145#define BSTP_DEFAULT_HOLD_COUNT 6 146#define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000 147#define BSTP_DEFAULT_PORT_PRIORITY 0x80 148#define BSTP_DEFAULT_PATH_COST 55 149#define BSTP_MIN_HELLO_TIME (1 * 256) 150#define BSTP_MIN_MAX_AGE (6 * 256) 151#define BSTP_MIN_FORWARD_DELAY (4 * 256) 152#define BSTP_MIN_HOLD_COUNT 1 153#define BSTP_MAX_HELLO_TIME (2 * 256) 154#define BSTP_MAX_MAX_AGE (40 * 256) 155#define BSTP_MAX_FORWARD_DELAY (30 * 256) 156#define BSTP_MAX_HOLD_COUNT 10 157#define BSTP_MAX_PRIORITY 61440 158#define BSTP_MAX_PORT_PRIORITY 240 159#define BSTP_MAX_PATH_COST 200000000 160 161/* BPDU message types */ 162#define BSTP_MSGTYPE_CFG 0x00 /* Configuration */ 163#define BSTP_MSGTYPE_RSTP 0x02 /* Rapid STP */ 164#define BSTP_MSGTYPE_TCN 0x80 /* Topology chg notification */ 165 166/* Protocol versions */ 167#define BSTP_PROTO_ID 0x00 168#define BSTP_PROTO_STP 0x00 169#define BSTP_PROTO_RSTP 0x02 170#define BSTP_PROTO_MAX BSTP_PROTO_RSTP 171 172#define BSTP_INFO_RECIEVED 1 173#define BSTP_INFO_MINE 2 174#define BSTP_INFO_AGED 3 175#define BSTP_INFO_DISABLED 4 176 177 178#define BSTP_MESSAGE_AGE_INCR (1 * 256) /* in 256ths of a second */ 179#define BSTP_TICK_VAL (1 * 256) /* in 256ths of a second */ 180#define BSTP_LINK_TIMER (BSTP_TICK_VAL * 15) 181 182/* 183 * Driver callbacks for STP state changes 184 */ 185typedef void (*bstp_state_cb_t)(struct ifnet *, int); 186typedef void (*bstp_rtage_cb_t)(struct ifnet *, int); 187 188/* 189 * Because BPDU's do not make nicely aligned structures, two different 190 * declarations are used: bstp_?bpdu (wire representation, packed) and 191 * bstp_*_unit (internal, nicely aligned version). 192 */ 193 194/* configuration bridge protocol data unit */ 195struct bstp_cbpdu { 196 uint8_t cbu_dsap; /* LLC: destination sap */ 197 uint8_t cbu_ssap; /* LLC: source sap */ 198 uint8_t cbu_ctl; /* LLC: control */ 199 uint16_t cbu_protoid; /* protocol id */ 200 uint8_t cbu_protover; /* protocol version */ 201 uint8_t cbu_bpdutype; /* message type */ 202 uint8_t cbu_flags; /* flags (below) */ 203 204 /* root id */ 205 uint16_t cbu_rootpri; /* root priority */ 206 uint8_t cbu_rootaddr[6]; /* root address */ 207 208 uint32_t cbu_rootpathcost; /* root path cost */ 209 210 /* bridge id */ 211 uint16_t cbu_bridgepri; /* bridge priority */ 212 uint8_t cbu_bridgeaddr[6]; /* bridge address */ 213 214 uint16_t cbu_portid; /* port id */ 215 uint16_t cbu_messageage; /* current message age */ 216 uint16_t cbu_maxage; /* maximum age */ 217 uint16_t cbu_hellotime; /* hello time */ 218 uint16_t cbu_forwarddelay; /* forwarding delay */ 219 uint8_t cbu_versionlen; /* version 1 length */ 220} __attribute__((__packed__)); 221#define BSTP_BPDU_STP_LEN (3 + 35) /* LLC + STP pdu */ 222#define BSTP_BPDU_RSTP_LEN (3 + 36) /* LLC + RSTP pdu */ 223 224/* topology change notification bridge protocol data unit */ 225struct bstp_tbpdu { 226 uint8_t tbu_dsap; /* LLC: destination sap */ 227 uint8_t tbu_ssap; /* LLC: source sap */ 228 uint8_t tbu_ctl; /* LLC: control */ 229 uint16_t tbu_protoid; /* protocol id */ 230 uint8_t tbu_protover; /* protocol version */ 231 uint8_t tbu_bpdutype; /* message type */ 232} __attribute__((__packed__)); 233 234/* 235 * Timekeeping structure used in spanning tree code. 236 */ 237struct bstp_timer { 238 int active; 239 int latched; 240 int value; 241}; 242 243struct bstp_pri_vector { 244 uint64_t pv_root_id; 245 uint32_t pv_cost; 246 uint64_t pv_dbridge_id; 247 uint16_t pv_dport_id; 248 uint16_t pv_port_id; 249}; 250 251struct bstp_config_unit { 252 struct bstp_pri_vector cu_pv; 253 uint16_t cu_message_age; 254 uint16_t cu_max_age; 255 uint16_t cu_forward_delay; 256 uint16_t cu_hello_time; 257 uint8_t cu_message_type; 258 uint8_t cu_topology_change_ack; 259 uint8_t cu_topology_change; 260 uint8_t cu_proposal; 261 uint8_t cu_agree; 262 uint8_t cu_learning; 263 uint8_t cu_forwarding; 264 uint8_t cu_role; 265}; 266 267struct bstp_tcn_unit { 268 uint8_t tu_message_type; 269}; 270 271struct bstp_port { 272 LIST_ENTRY(bstp_port) bp_next; 273 struct ifnet *bp_ifp; /* parent if */ 274 struct bstp_state *bp_bs; 275 uint8_t bp_active; 276 uint8_t bp_protover; 277 uint32_t bp_flags; 278 uint32_t bp_path_cost; 279 uint16_t bp_port_msg_age; 280 uint16_t bp_port_max_age; 281 uint16_t bp_port_fdelay; 282 uint16_t bp_port_htime; 283 uint16_t bp_desg_msg_age; 284 uint16_t bp_desg_max_age; 285 uint16_t bp_desg_fdelay; 286 uint16_t bp_desg_htime; 287 struct bstp_timer bp_edge_delay_timer; 288 struct bstp_timer bp_forward_delay_timer; 289 struct bstp_timer bp_hello_timer; 290 struct bstp_timer bp_message_age_timer; 291 struct bstp_timer bp_migrate_delay_timer; 292 struct bstp_timer bp_recent_backup_timer; 293 struct bstp_timer bp_recent_root_timer; 294 struct bstp_timer bp_tc_timer; 295 struct bstp_config_unit bp_msg_cu; 296 struct bstp_pri_vector bp_desg_pv; 297 struct bstp_pri_vector bp_port_pv; 298 uint16_t bp_port_id; 299 uint8_t bp_state; 300 uint8_t bp_tcstate; 301 uint8_t bp_role; 302 uint8_t bp_infois; 303 uint8_t bp_tc_ack; 304 uint8_t bp_tc_prop; 305 uint8_t bp_fdbflush; 306 uint8_t bp_priority; 307 uint8_t bp_p2p_link; 308 uint8_t bp_agree; 309 uint8_t bp_agreed; 310 uint8_t bp_sync; 311 uint8_t bp_synced; 312 uint8_t bp_proposing; 313 uint8_t bp_proposed; 314 uint8_t bp_operedge; 315 uint8_t bp_reroot; 316 uint8_t bp_rcvdtc; 317 uint8_t bp_rcvdtca; 318 uint8_t bp_rcvdtcn; 319 uint32_t bp_forward_transitions; 320 uint8_t bp_txcount; 321 struct task bp_statetask; 322 struct task bp_rtagetask; 323}; 324 325/* 326 * Software state for each bridge STP. 327 */ 328struct bstp_state { 329 LIST_ENTRY(bstp_state) bs_list; 330 uint8_t bs_running; 331 struct mtx bs_mtx; 332 struct bstp_pri_vector bs_bridge_pv; 333 struct bstp_pri_vector bs_root_pv; 334 struct bstp_port *bs_root_port; 335 uint8_t bs_protover; 336 uint16_t bs_migration_delay; 337 uint16_t bs_edge_delay; 338 uint16_t bs_bridge_max_age; 339 uint16_t bs_bridge_fdelay; 340 uint16_t bs_bridge_htime; 341 uint16_t bs_root_msg_age; 342 uint16_t bs_root_max_age; 343 uint16_t bs_root_fdelay; 344 uint16_t bs_root_htime; 345 uint16_t bs_hold_time; 346 uint16_t bs_bridge_priority; 347 uint8_t bs_txholdcount; 348 uint8_t bs_allsynced; 349 struct callout bs_bstpcallout; /* STP callout */ 350 struct bstp_timer bs_link_timer; 351 struct timeval bs_last_tc_time; 352 LIST_HEAD(, bstp_port) bs_bplist; 353 bstp_state_cb_t bs_state_cb; 354 bstp_rtage_cb_t bs_rtage_cb; 355}; 356 357#define BSTP_LOCK_INIT(_bs) mtx_init(&(_bs)->bs_mtx, "bstp", NULL, MTX_DEF) 358#define BSTP_LOCK_DESTROY(_bs) mtx_destroy(&(_bs)->bs_mtx) 359#define BSTP_LOCK(_bs) mtx_lock(&(_bs)->bs_mtx) 360#define BSTP_UNLOCK(_bs) mtx_unlock(&(_bs)->bs_mtx) 361#define BSTP_LOCK_ASSERT(_bs) mtx_assert(&(_bs)->bs_mtx, MA_OWNED) 362 363extern const uint8_t bstp_etheraddr[]; 364 365extern void (*bstp_linkstate_p)(struct ifnet *ifp, int state); 366 367void bstp_attach(struct bstp_state *, bstp_state_cb_t, bstp_rtage_cb_t); 368void bstp_detach(struct bstp_state *); 369void bstp_init(struct bstp_state *); 370void bstp_stop(struct bstp_state *); 371int bstp_create(struct bstp_state *, struct bstp_port *, struct ifnet *); 372int bstp_enable(struct bstp_port *); 373void bstp_disable(struct bstp_port *); 374void bstp_destroy(struct bstp_port *); 375void bstp_linkstate(struct ifnet *, int); 376int bstp_set_htime(struct bstp_state *, int); 377int bstp_set_fdelay(struct bstp_state *, int); 378int bstp_set_maxage(struct bstp_state *, int); 379int bstp_set_holdcount(struct bstp_state *, int); 380int bstp_set_protocol(struct bstp_state *, int); 381int bstp_set_priority(struct bstp_state *, int); 382int bstp_set_port_priority(struct bstp_port *, int); 383int bstp_set_path_cost(struct bstp_port *, uint32_t); 384int bstp_set_edge(struct bstp_port *, int); 385int bstp_set_autoedge(struct bstp_port *, int); 386int bstp_set_p2p(struct bstp_port *, int); 387int bstp_set_autop2p(struct bstp_port *, int); 388struct mbuf *bstp_input(struct bstp_port *, struct ifnet *, struct mbuf *); 389 390#endif /* _KERNEL */ 391