Index: sbin/ifconfig/ifbridge.c =================================================================== RCS file: /home/ncvs/src/sbin/ifconfig/ifbridge.c,v retrieving revision 1.3 diff -u -p -r1.3 ifbridge.c --- sbin/ifconfig/ifbridge.c 14 Dec 2005 02:52:12 -0000 1.3 +++ sbin/ifconfig/ifbridge.c 12 Oct 2006 10:46:13 -0000 @@ -61,6 +61,27 @@ static const char rcsid[] = #include "ifconfig.h" +static const char *stpstates[] = { + "disabled", + "listening", + "learning", + "forwarding", + "blocking", + "discarding" +}; +static const char *stpproto[] = { + "stp", + "-", + "rstp" +}; +static const char *stproles[] = { + "disabled", + "root", + "designated", + "alternate", + "backup" +}; + static int get_val(const char *cp, u_long *valp) { @@ -113,13 +134,6 @@ do_bridgeflag(int sock, const char *ifs, static void bridge_interfaces(int s, const char *prefix) { - static const char *stpstates[] = { - "disabled", - "listening", - "learning", - "forwarding", - "blocking", - }; struct ifbifconf bifc; struct ifbreq *req; char *inbuf = NULL, *ninbuf; @@ -159,9 +173,23 @@ bridge_interfaces(int s, const char *pre printf("port %u priority %u", req->ifbr_portno, req->ifbr_priority); printf(" path cost %u", req->ifbr_path_cost); + if (req->ifbr_proto < + sizeof(stpproto) / sizeof(stpproto[0])) + printf(" proto %s", stpproto[req->ifbr_proto]); + else + printf(" ", + req->ifbr_proto); + + printf("\n%s", pad); + if (req->ifbr_role < + sizeof(stproles) / sizeof(stproles[0])) + printf("role %s", stproles[req->ifbr_role]); + else + printf("", + req->ifbr_role); if (req->ifbr_state < sizeof(stpstates) / sizeof(stpstates[0])) - printf(" %s", stpstates[req->ifbr_state]); + printf(" state %s", stpstates[req->ifbr_state]); else printf(" ", req->ifbr_state); @@ -210,28 +238,22 @@ bridge_addresses(int s, const char *pref static void bridge_status(int s) { - struct ifbrparam param; + struct ifbropreq param; u_int16_t pri; - u_int8_t ht, fd, ma; + u_int8_t ht, fd, ma, hc, pro; - if (do_cmd(s, BRDGGPRI, ¶m, sizeof(param), 0) < 0) + if (do_cmd(s, BRDGPARAM, ¶m, sizeof(param), 0) < 0) return; - pri = param.ifbrp_prio; - - if (do_cmd(s, BRDGGHT, ¶m, sizeof(param), 0) < 0) - return; - ht = param.ifbrp_hellotime; - - if (do_cmd(s, BRDGGFD, ¶m, sizeof(param), 0) < 0) - return; - fd = param.ifbrp_fwddelay; - - if (do_cmd(s, BRDGGMA, ¶m, sizeof(param), 0) < 0) - return; - ma = param.ifbrp_maxage; - - printf("\tpriority %u hellotime %u fwddelay %u maxage %u\n", - pri, ht, fd, ma); + pri = param.ifbop_priority; + pro = param.ifbop_protocol; + ht = param.ifbop_hellotime; + fd = param.ifbop_fwddelay; + hc = param.ifbop_holdcount; + ma = param.ifbop_maxage; + + printf("\tpriority %u hellotime %u fwddelay %u" + " maxage %u hc %u proto %s\n", + pri, ht, fd, ma, hc, stpproto[pro]); bridge_interfaces(s, "\tmember: "); @@ -469,6 +491,38 @@ setbridge_priority(const char *arg, int } static void +setbridge_protocol(const char *arg, int d, int s, const struct afswtch *afp) +{ + struct ifbrparam param; + + if (strcasecmp(arg, "stp") == 0) { + param.ifbrp_proto = 0; + } else if (strcasecmp(arg, "rstp") == 0) { + param.ifbrp_proto = 2; + } else { + errx(1, "unknown stp protocol"); + } + + if (do_cmd(s, BRDGSPROTO, ¶m, sizeof(param), 1) < 0) + err(1, "BRDGSPROTO %s", arg); +} + +static void +setbridge_holdcount(const char *arg, int d, int s, const struct afswtch *afp) +{ + struct ifbrparam param; + u_long val; + + if (get_val(arg, &val) < 0 || (val & ~0xff) != 0) + errx(1, "invalid value: %s", arg); + + param.ifbrp_txhc = val & 0xff; + + if (do_cmd(s, BRDGSTXHC, ¶m, sizeof(param), 1) < 0) + err(1, "BRDGSTXHC %s", arg); +} + +static void setbridge_ifpriority(const char *ifn, const char *pri, int s, const struct afswtch *afp) { @@ -496,11 +550,11 @@ setbridge_ifpathcost(const char *ifn, co memset(&req, 0, sizeof(req)); - if (get_val(cost, &val) < 0 || (val & ~0xff) != 0) + if (get_val(cost, &val) < 0) errx(1, "invalid value: %s", cost); strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname)); - req.ifbr_path_cost = val & 0xffff; + req.ifbr_path_cost = val; if (do_cmd(s, BRDGSIFCOST, &req, sizeof(req), 1) < 0) err(1, "BRDGSIFCOST %s", cost); @@ -542,6 +596,8 @@ static struct cmd bridge_cmds[] = { DEF_CMD_ARG("fwddelay", setbridge_fwddelay), DEF_CMD_ARG("maxage", setbridge_maxage), DEF_CMD_ARG("priority", setbridge_priority), + DEF_CMD_ARG("proto", setbridge_protocol), + DEF_CMD_ARG("holdcount", setbridge_holdcount), DEF_CMD_ARG2("ifpriority", setbridge_ifpriority), DEF_CMD_ARG2("ifpathcost", setbridge_ifpathcost), DEF_CMD_ARG("timeout", setbridge_timeout), Index: sbin/ifconfig/ifconfig.8 =================================================================== RCS file: /home/ncvs/src/sbin/ifconfig/ifconfig.8,v retrieving revision 1.124 diff -u -p -r1.124 ifconfig.8 --- sbin/ifconfig/ifconfig.8 10 Oct 2006 09:44:08 -0000 1.124 +++ sbin/ifconfig/ifconfig.8 12 Oct 2006 10:46:33 -0000 @@ -1270,35 +1270,47 @@ This is the default for all interfaces a .It Cm maxage Ar seconds Set the time that a Spanning Tree protocol configuration is valid. The default is 20 seconds. -The minimum is 1 second and the maximum is 255 seconds. +The minimum is 6 seconds and the maximum is 40 seconds. .It Cm fwddelay Ar seconds Set the time that must pass before an interface begins forwarding packets when Spanning Tree is enabled. The default is 15 seconds. -The minimum is 1 second and the maximum is 255 seconds. +The minimum is 4 seconds and the maximum is 30 seconds. .It Cm hellotime Ar seconds Set the time between broadcasting of Spanning Tree protocol configuration messages. +The hello time may only be changed when operating in legacy stp mode. The default is 2 seconds. -The minimum is 1 second and the maximum is 255 seconds. +The minimum is 1 second and the maximum is 2 seconds. .It Cm priority Ar value Set the bridge priority for Spanning Tree. The default is 32768. -The minimum is 0 and the maximum is 65536. +The minimum is 0 and the maximum is 61440. +.It Cm protocol Ar value +Set the Spanning Tree protocol. +The default is rstp. +The available options are stp and rstp. +.It Cm holdcount Ar value +Set the transmit hold count for Spanning Tree. +This is the number of packets transmitted before being rate limited. +The default is 6. +The minimum is 1 and the maximum is 10. .It Cm ifpriority Ar interface Ar value Set the Spanning Tree priority of .Ar interface to .Ar value . The default is 128. -The minimum is 0 and the maximum is 255. +The minimum is 0 and the maximum is 240. .It Cm ifpathcost Ar interface Ar value Set the Spanning Tree path cost of .Ar interface to .Ar value . -The default is 55. -The minimum is 0 and the maximum is 65535. +The default is calculated from the link speed. +To change a previously selected path cost back to automatic, set the +cost to 0. +The minimum is 1 and the maximum is 200000000. .El .Pp The following parameters are specific to IP tunnel interfaces, Index: sys/net/bridgestp.c =================================================================== RCS file: /home/ncvs/src/sys/net/bridgestp.c,v retrieving revision 1.20 diff -u -p -r1.20 bridgestp.c --- sys/net/bridgestp.c 1 Oct 2006 03:48:32 -0000 1.20 +++ sys/net/bridgestp.c 12 Oct 2006 10:57:57 -0000 @@ -2,6 +2,7 @@ /* * Copyright (c) 2000 Jason L. Wright (jason@thought.net) + * Copyright (c) 2006 Andrew Thompson (thompsa@FreeBSD.org) * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,8 +31,7 @@ /* * Implementation of the spanning tree protocol as defined in - * ISO/IEC Final DIS 15802-3 (IEEE P802.1D/D17), May 25, 1998. - * (In English: IEEE 802.1D, Draft 17, 1998) + * ISO/IEC 802.1D-2004, June 9, 2004. */ #include @@ -62,276 +62,177 @@ __FBSDID("$FreeBSD: src/sys/net/bridgest #include #include +#define BRIDGESTP_DEBUG 1 + +#ifdef BRIDGESTP_DEBUG +#define DPRINTF(fmt, arg...) printf("bstp: " fmt, ##arg) +#else +#define DPRINTF(fmt, arg...) +#endif + +#define PV2ADDR(pv, eaddr) do { \ + eaddr[0] = pv >> 40; \ + eaddr[1] = pv >> 32; \ + eaddr[2] = pv >> 24; \ + eaddr[3] = pv >> 16; \ + eaddr[4] = pv >> 8; \ + eaddr[5] = pv >> 0; \ +} while (0) + +#define INFO_BETTER 1 +#define INFO_SAME 0 +#define INFO_WORSE -1 + const uint8_t bstp_etheraddr[] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 }; LIST_HEAD(, bstp_state) bstp_list; static struct mtx bstp_list_mtx; -static void bstp_initialize_port(struct bstp_state *, - struct bstp_port *); -static void bstp_ifupdstatus(struct bstp_state *, struct bstp_port *); -static void bstp_enable_port(struct bstp_state *, struct bstp_port *); -static void bstp_disable_port(struct bstp_state *, - struct bstp_port *); -#ifdef notused -static void bstp_enable_change_detection(struct bstp_port *); -static void bstp_disable_change_detection(struct bstp_port *); -#endif /* notused */ -static int bstp_root_bridge(struct bstp_state *bs); -static int bstp_supersedes_port_info(struct bstp_state *, - struct bstp_port *, struct bstp_config_unit *); -static int bstp_designated_port(struct bstp_state *, +static void bstp_transmit(struct bstp_state *, struct bstp_port *); -static int bstp_designated_for_some_port(struct bstp_state *); -static void bstp_transmit_config(struct bstp_state *, +static void bstp_transmit_bpdu(struct bstp_state *, struct bstp_port *); -static void bstp_transmit_tcn(struct bstp_state *); -static void bstp_received_config_bpdu(struct bstp_state *, - struct bstp_port *, struct bstp_config_unit *); -static void bstp_received_tcn_bpdu(struct bstp_state *, - struct bstp_port *, struct bstp_tcn_unit *); -static void bstp_record_config_information(struct bstp_state *, - struct bstp_port *, struct bstp_config_unit *); -static void bstp_record_config_timeout_values(struct bstp_state *, +static void bstp_transmit_tcn(struct bstp_state *, + struct bstp_port *); +static void bstp_decode_bpdu(struct bstp_port *, struct bstp_cbpdu *, struct bstp_config_unit *); -static void bstp_config_bpdu_generation(struct bstp_state *); -static void bstp_send_config_bpdu(struct bstp_state *, - struct bstp_port *, struct bstp_config_unit *); -static void bstp_configuration_update(struct bstp_state *); -static void bstp_root_selection(struct bstp_state *); -static void bstp_designated_port_selection(struct bstp_state *); -static void bstp_become_designated_port(struct bstp_state *, - struct bstp_port *); -static void bstp_port_state_selection(struct bstp_state *); -static void bstp_make_forwarding(struct bstp_state *, - struct bstp_port *); -static void bstp_make_blocking(struct bstp_state *, - struct bstp_port *); -static void bstp_set_port_state(struct bstp_port *, uint8_t); -static void bstp_state_change(void *, int); -static void bstp_update_forward_transitions(struct bstp_port *); -#ifdef notused -static void bstp_set_bridge_priority(struct bstp_state *, uint64_t); -static void bstp_set_port_priority(struct bstp_state *, - struct bstp_port *, uint16_t); -static void bstp_set_path_cost(struct bstp_state *, - struct bstp_port *, uint32_t); -#endif /* notused */ -static void bstp_topology_change_detection(struct bstp_state *); -static void bstp_topology_change_acknowledged(struct bstp_state *); -static void bstp_acknowledge_topology_change(struct bstp_state *, - struct bstp_port *); - +static void bstp_send_bpdu(struct bstp_state *, + struct bstp_port *, struct bstp_cbpdu *); static void bstp_enqueue(struct ifnet *, struct mbuf *); +static int bstp_pdu_flags(struct bstp_port *); +static void bstp_received_stp(struct bstp_state *, struct bstp_port *, + struct mbuf *, struct bstp_tbpdu *); +static void bstp_received_rstp(struct bstp_state *, + struct bstp_port *, struct mbuf *, struct bstp_tbpdu *); +static void bstp_received_tcn(struct bstp_state *, + struct bstp_port *, struct bstp_tcn_unit *); +static void bstp_received_bpdu(struct bstp_state *, + struct bstp_port *, struct bstp_config_unit *); +static int bstp_pdu_rcvtype(struct bstp_port *, struct bstp_config_unit *); +static int bstp_pdu_bettersame(struct bstp_port *, int); +static int bstp_info_cmp(struct bstp_pri_vector *, + struct bstp_pri_vector *); +static int bstp_info_superior(struct bstp_pri_vector *, + struct bstp_pri_vector *); +static void bstp_assign_roles(struct bstp_state *); +static void bstp_update_roles(struct bstp_state *, struct bstp_port *); +static void bstp_update_state(struct bstp_state *, struct bstp_port *); +static void bstp_update_tc(struct bstp_port *); +static void bstp_update_info(struct bstp_port *); +static void bstp_set_other_tcprop(struct bstp_port *); +static void bstp_set_all_reroot(struct bstp_state *); +static void bstp_set_all_sync(struct bstp_state *); +static void bstp_set_port_state(struct bstp_port *, int); +static void bstp_set_port_role(struct bstp_port *, int); +static void bstp_set_port_proto(struct bstp_port *, int); +static void bstp_set_port_tc(struct bstp_port *, int); +static void bstp_set_timer_tc(struct bstp_port *); +static void bstp_set_timer_msgage(struct bstp_port *); +static int bstp_rerooted(struct bstp_state *, struct bstp_port *); +static uint32_t bstp_calc_path_cost(struct bstp_port *); +static void bstp_notify_state(void *, int); +static void bstp_notify_rtage(void *, int); +static void bstp_ifupdstatus(struct bstp_state *, struct bstp_port *); +static void bstp_enable_port(struct bstp_state *, struct bstp_port *); +static void bstp_disable_port(struct bstp_state *, struct bstp_port *); static void bstp_tick(void *); static void bstp_timer_start(struct bstp_timer *, uint16_t); static void bstp_timer_stop(struct bstp_timer *); -static int bstp_timer_expired(struct bstp_timer *, uint16_t); - -static void bstp_hold_timer_expiry(struct bstp_state *, +static void bstp_timer_latch(struct bstp_timer *); +static int bstp_timer_expired(struct bstp_timer *); +static void bstp_hello_timer_expiry(struct bstp_state *, + struct bstp_port *); +static void bstp_message_age_expiry(struct bstp_state *, struct bstp_port *); -static void bstp_message_age_timer_expiry(struct bstp_state *, +static void bstp_migrate_delay_expiry(struct bstp_state *, struct bstp_port *); -static void bstp_forward_delay_timer_expiry(struct bstp_state *, +static void bstp_edge_delay_expiry(struct bstp_state *, struct bstp_port *); -static void bstp_topology_change_timer_expiry(struct bstp_state *); -static void bstp_tcn_timer_expiry(struct bstp_state *); -static void bstp_hello_timer_expiry(struct bstp_state *); static int bstp_addr_cmp(const uint8_t *, const uint8_t *); +static int bstp_same_bridgeid(uint64_t, uint64_t); +static void bstp_reinit(struct bstp_state *); +static void bstp_stop_locked(struct bstp_state *); static void -bstp_transmit_config(struct bstp_state *bs, struct bstp_port *bp) +bstp_transmit(struct bstp_state *bs, struct bstp_port *bp) { - BSTP_LOCK_ASSERT(bs); - - if (bp->bp_hold_timer.active) { - bp->bp_config_pending = 1; + /* + * a PDU can only be sent if we have tx quota left and the + * hello timer is running. + */ + if (bp->bp_hello_timer.active == 0) { + /* Test if it needs to be reset */ + bstp_hello_timer_expiry(bs, bp); return; } + if (bp->bp_txcount > bs->bs_txholdcount) + /* Ran out of karma */ + return; - bp->bp_config_bpdu.cu_message_type = BSTP_MSGTYPE_CFG; - bp->bp_config_bpdu.cu_rootid = bs->bs_designated_root; - bp->bp_config_bpdu.cu_root_path_cost = bs->bs_root_path_cost; - bp->bp_config_bpdu.cu_bridge_id = bs->bs_bridge_id; - bp->bp_config_bpdu.cu_port_id = bp->bp_port_id; - - if (bstp_root_bridge(bs)) - bp->bp_config_bpdu.cu_message_age = 0; - else - bp->bp_config_bpdu.cu_message_age = - bs->bs_root_port->bp_message_age_timer.value + - BSTP_MESSAGE_AGE_INCR; - - bp->bp_config_bpdu.cu_max_age = bs->bs_max_age; - bp->bp_config_bpdu.cu_hello_time = bs->bs_hello_time; - bp->bp_config_bpdu.cu_forward_delay = bs->bs_forward_delay; - bp->bp_config_bpdu.cu_topology_change_acknowledgment - = bp->bp_topology_change_acknowledge; - bp->bp_config_bpdu.cu_topology_change = bs->bs_topology_change; - - if (bp->bp_config_bpdu.cu_message_age < bs->bs_max_age) { - bp->bp_topology_change_acknowledge = 0; - bp->bp_config_pending = 0; - bstp_send_config_bpdu(bs, bp, &bp->bp_config_bpdu); - bstp_timer_start(&bp->bp_hold_timer, 0); + if (bp->bp_protover == BSTP_PROTO_RSTP) { + bstp_transmit_bpdu(bs, bp); + bp->bp_tc_ack = 0; + } else { /* STP */ + switch (bp->bp_role) { + case BSTP_ROLE_DESIGNATED: + bstp_transmit_bpdu(bs, bp); + bp->bp_tc_ack = 0; + break; + + case BSTP_ROLE_ROOT: + bstp_transmit_tcn(bs, bp); + break; + } } + bstp_timer_start(&bp->bp_hello_timer, bp->bp_desg_htime); + bp->bp_flags &= ~BSTP_PORT_NEWINFO; } static void -bstp_send_config_bpdu(struct bstp_state *bs, struct bstp_port *bp, - struct bstp_config_unit *cu) +bstp_transmit_bpdu(struct bstp_state *bs, struct bstp_port *bp) { - struct ifnet *ifp; - struct mbuf *m; - struct ether_header *eh; struct bstp_cbpdu bpdu; BSTP_LOCK_ASSERT(bs); - ifp = bp->bp_ifp; - - if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) - return; - - MGETHDR(m, M_DONTWAIT, MT_DATA); - if (m == NULL) - return; - - eh = mtod(m, struct ether_header *); - - m->m_pkthdr.rcvif = ifp; - m->m_pkthdr.len = sizeof(*eh) + sizeof(bpdu); - m->m_len = m->m_pkthdr.len; - - bpdu.cbu_ssap = bpdu.cbu_dsap = LLC_8021D_LSAP; - bpdu.cbu_ctl = LLC_UI; - bpdu.cbu_protoid = htons(0); - bpdu.cbu_protover = 0; - bpdu.cbu_bpdutype = cu->cu_message_type; - bpdu.cbu_flags = (cu->cu_topology_change ? BSTP_FLAG_TC : 0) | - (cu->cu_topology_change_acknowledgment ? BSTP_FLAG_TCA : 0); - - bpdu.cbu_rootpri = htons(cu->cu_rootid >> 48); - bpdu.cbu_rootaddr[0] = cu->cu_rootid >> 40; - bpdu.cbu_rootaddr[1] = cu->cu_rootid >> 32; - bpdu.cbu_rootaddr[2] = cu->cu_rootid >> 24; - bpdu.cbu_rootaddr[3] = cu->cu_rootid >> 16; - bpdu.cbu_rootaddr[4] = cu->cu_rootid >> 8; - bpdu.cbu_rootaddr[5] = cu->cu_rootid >> 0; - - bpdu.cbu_rootpathcost = htonl(cu->cu_root_path_cost); - - bpdu.cbu_bridgepri = htons(cu->cu_bridge_id >> 48); - bpdu.cbu_bridgeaddr[0] = cu->cu_bridge_id >> 40; - bpdu.cbu_bridgeaddr[1] = cu->cu_bridge_id >> 32; - bpdu.cbu_bridgeaddr[2] = cu->cu_bridge_id >> 24; - bpdu.cbu_bridgeaddr[3] = cu->cu_bridge_id >> 16; - bpdu.cbu_bridgeaddr[4] = cu->cu_bridge_id >> 8; - bpdu.cbu_bridgeaddr[5] = cu->cu_bridge_id >> 0; - - bpdu.cbu_portid = htons(cu->cu_port_id); - bpdu.cbu_messageage = htons(cu->cu_message_age); - bpdu.cbu_maxage = htons(cu->cu_max_age); - bpdu.cbu_hellotime = htons(cu->cu_hello_time); - bpdu.cbu_forwarddelay = htons(cu->cu_forward_delay); - - memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN); - memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN); - eh->ether_type = htons(sizeof(bpdu)); - - memcpy(mtod(m, caddr_t) + sizeof(*eh), &bpdu, sizeof(bpdu)); - - bstp_enqueue(ifp, m); -} - -static int -bstp_root_bridge(struct bstp_state *bs) -{ - return (bs->bs_designated_root == bs->bs_bridge_id); -} - -static int -bstp_supersedes_port_info(struct bstp_state *bs, struct bstp_port *bp, - struct bstp_config_unit *cu) -{ - if (cu->cu_rootid < bp->bp_designated_root) - return (1); - if (cu->cu_rootid > bp->bp_designated_root) - return (0); - - if (cu->cu_root_path_cost < bp->bp_designated_cost) - return (1); - if (cu->cu_root_path_cost > bp->bp_designated_cost) - return (0); - - if (cu->cu_bridge_id < bp->bp_designated_bridge) - return (1); - if (cu->cu_bridge_id > bp->bp_designated_bridge) - return (0); - - if (bs->bs_bridge_id != cu->cu_bridge_id) - return (1); - if (cu->cu_port_id <= bp->bp_designated_port) - return (1); - return (0); -} - -static void -bstp_record_config_information(struct bstp_state *bs, - struct bstp_port *bp, struct bstp_config_unit *cu) -{ - BSTP_LOCK_ASSERT(bs); + bpdu.cbu_rootpri = htons(bp->bp_desg_pv.pv_root_id >> 48); + PV2ADDR(bp->bp_desg_pv.pv_root_id, bpdu.cbu_rootaddr); - bp->bp_designated_root = cu->cu_rootid; - bp->bp_designated_cost = cu->cu_root_path_cost; - bp->bp_designated_bridge = cu->cu_bridge_id; - bp->bp_designated_port = cu->cu_port_id; - bstp_timer_start(&bp->bp_message_age_timer, cu->cu_message_age); -} + bpdu.cbu_rootpathcost = htonl(bp->bp_desg_pv.pv_cost); -static void -bstp_record_config_timeout_values(struct bstp_state *bs, - struct bstp_config_unit *config) -{ - BSTP_LOCK_ASSERT(bs); + bpdu.cbu_bridgepri = htons(bp->bp_desg_pv.pv_dbridge_id >> 48); + PV2ADDR(bp->bp_desg_pv.pv_dbridge_id, bpdu.cbu_bridgeaddr); - bs->bs_max_age = config->cu_max_age; - bs->bs_hello_time = config->cu_hello_time; - bs->bs_forward_delay = config->cu_forward_delay; - bs->bs_topology_change = config->cu_topology_change; -} + bpdu.cbu_portid = htons(bp->bp_port_id); + bpdu.cbu_messageage = htons(bp->bp_desg_msg_age); + bpdu.cbu_maxage = htons(bp->bp_desg_max_age); + bpdu.cbu_hellotime = htons(bp->bp_desg_htime); + bpdu.cbu_forwarddelay = htons(bp->bp_desg_fdelay); -static void -bstp_config_bpdu_generation(struct bstp_state *bs) -{ - struct bstp_port *bp; + bpdu.cbu_flags = bstp_pdu_flags(bp); - BSTP_LOCK_ASSERT(bs); + switch (bp->bp_protover) { + case BSTP_PROTO_STP: + bpdu.cbu_bpdutype = BSTP_MSGTYPE_CFG; + break; - LIST_FOREACH(bp, &bs->bs_bplist, bp_next) { - if (bstp_designated_port(bs, bp) && - (bp->bp_state != BSTP_IFSTATE_DISABLED)) - bstp_transmit_config(bs, bp); + case BSTP_PROTO_RSTP: + bpdu.cbu_bpdutype = BSTP_MSGTYPE_RSTP; + break; } -} -static int -bstp_designated_port(struct bstp_state *bs, struct bstp_port *bp) -{ - return ((bp->bp_designated_bridge == bs->bs_bridge_id) - && (bp->bp_designated_port == bp->bp_port_id)); + bstp_send_bpdu(bs, bp, &bpdu); } static void -bstp_transmit_tcn(struct bstp_state *bs) +bstp_transmit_tcn(struct bstp_state *bs, struct bstp_port *bp) { struct bstp_tbpdu bpdu; - struct bstp_port *bp = bs->bs_root_port; struct ifnet *ifp = bp->bp_ifp; struct ether_header *eh; struct mbuf *m; - BSTP_LOCK_ASSERT(bs); + KASSERT(bp == bs->bs_root_port, ("%s: bad root port\n", __func__)); if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) return; @@ -358,234 +259,205 @@ bstp_transmit_tcn(struct bstp_state *bs) memcpy(mtod(m, caddr_t) + sizeof(*eh), &bpdu, sizeof(bpdu)); + bp->bp_txcount++; bstp_enqueue(ifp, m); } static void -bstp_configuration_update(struct bstp_state *bs) -{ - BSTP_LOCK_ASSERT(bs); - - bstp_root_selection(bs); - bstp_designated_port_selection(bs); -} - -static void -bstp_root_selection(struct bstp_state *bs) +bstp_decode_bpdu(struct bstp_port *bp, struct bstp_cbpdu *cpdu, + struct bstp_config_unit *cu) { - struct bstp_port *root_port = NULL, *bp; - - BSTP_LOCK_ASSERT(bs); - - LIST_FOREACH(bp, &bs->bs_bplist, bp_next) { - if (bstp_designated_port(bs, bp)) - continue; - if (bp->bp_state == BSTP_IFSTATE_DISABLED) - continue; - if (bp->bp_designated_root >= bs->bs_bridge_id) - continue; - if (root_port == NULL) - goto set_port; - - if (bp->bp_designated_root < root_port->bp_designated_root) - goto set_port; - if (bp->bp_designated_root > root_port->bp_designated_root) - continue; + int flags; - if ((bp->bp_designated_cost + bp->bp_path_cost) < - (root_port->bp_designated_cost + root_port->bp_path_cost)) - goto set_port; - if ((bp->bp_designated_cost + bp->bp_path_cost) > - (root_port->bp_designated_cost + root_port->bp_path_cost)) - continue; - - if (bp->bp_designated_bridge < - root_port->bp_designated_bridge) - goto set_port; - if (bp->bp_designated_bridge > - root_port->bp_designated_bridge) - continue; - - if (bp->bp_designated_port < root_port->bp_designated_port) - goto set_port; - if (bp->bp_designated_port > root_port->bp_designated_port) - continue; + cu->cu_pv.pv_root_id = + (((uint64_t)ntohs(cpdu->cbu_rootpri)) << 48) | + (((uint64_t)cpdu->cbu_rootaddr[0]) << 40) | + (((uint64_t)cpdu->cbu_rootaddr[1]) << 32) | + (((uint64_t)cpdu->cbu_rootaddr[2]) << 24) | + (((uint64_t)cpdu->cbu_rootaddr[3]) << 16) | + (((uint64_t)cpdu->cbu_rootaddr[4]) << 8) | + (((uint64_t)cpdu->cbu_rootaddr[5]) << 0); + + cu->cu_pv.pv_dbridge_id = + (((uint64_t)ntohs(cpdu->cbu_bridgepri)) << 48) | + (((uint64_t)cpdu->cbu_bridgeaddr[0]) << 40) | + (((uint64_t)cpdu->cbu_bridgeaddr[1]) << 32) | + (((uint64_t)cpdu->cbu_bridgeaddr[2]) << 24) | + (((uint64_t)cpdu->cbu_bridgeaddr[3]) << 16) | + (((uint64_t)cpdu->cbu_bridgeaddr[4]) << 8) | + (((uint64_t)cpdu->cbu_bridgeaddr[5]) << 0); + + cu->cu_pv.pv_cost = ntohl(cpdu->cbu_rootpathcost); + cu->cu_message_age = ntohs(cpdu->cbu_messageage); + cu->cu_max_age = ntohs(cpdu->cbu_maxage); + cu->cu_hello_time = ntohs(cpdu->cbu_hellotime); + cu->cu_forward_delay = ntohs(cpdu->cbu_forwarddelay); + cu->cu_pv.pv_dport_id = ntohs(cpdu->cbu_portid); + cu->cu_pv.pv_port_id = bp->bp_port_id; + cu->cu_message_type = cpdu->cbu_bpdutype; + + /* Strip off unused flags in STP mode */ + flags = cpdu->cbu_flags; + switch (bp->bp_protover) { + case BSTP_PROTO_STP: + flags &= BSTP_PDU_STPMASK; + /* A STP BPDU explicitly conveys a Designated Port */ + cu->cu_role = BSTP_ROLE_DESIGNATED; + break; - if (bp->bp_port_id >= root_port->bp_port_id) - continue; -set_port: - root_port = bp; + case BSTP_PROTO_RSTP: + flags &= BSTP_PDU_RSTPMASK; + break; } - bs->bs_root_port = root_port; - if (root_port == NULL) { - bs->bs_designated_root = bs->bs_bridge_id; - bs->bs_root_path_cost = 0; - } else { - bs->bs_designated_root = root_port->bp_designated_root; - bs->bs_root_path_cost = root_port->bp_designated_cost + - root_port->bp_path_cost; + cu->cu_topology_change_ack = + (flags & BSTP_PDU_F_TCA) ? 1 : 0; + cu->cu_proposal = + (flags & BSTP_PDU_F_P) ? 1 : 0; + cu->cu_agree = + (flags & BSTP_PDU_F_A) ? 1 : 0; + cu->cu_learning = + (flags & BSTP_PDU_F_L) ? 1 : 0; + cu->cu_forwarding = + (flags & BSTP_PDU_F_F) ? 1 : 0; + cu->cu_topology_change = + (flags & BSTP_PDU_F_TC) ? 1 : 0; + + switch ((flags & BSTP_PDU_PRMASK) >> BSTP_PDU_PRSHIFT) { + case BSTP_PDU_F_ROOT: + cu->cu_role = BSTP_ROLE_ROOT; + break; + case BSTP_PDU_F_ALT: + cu->cu_role = BSTP_ROLE_ALTERNATE; + break; + case BSTP_PDU_F_DESG: + cu->cu_role = BSTP_ROLE_DESIGNATED; + break; } } static void -bstp_designated_port_selection(struct bstp_state *bs) +bstp_send_bpdu(struct bstp_state *bs, struct bstp_port *bp, + struct bstp_cbpdu *bpdu) { - struct bstp_port *bp; + struct ifnet *ifp; + struct mbuf *m; + struct ether_header *eh; BSTP_LOCK_ASSERT(bs); - LIST_FOREACH(bp, &bs->bs_bplist, bp_next) { - if (bstp_designated_port(bs, bp)) - goto designated; - if (bp->bp_designated_root != bs->bs_designated_root) - goto designated; - - if (bs->bs_root_path_cost < bp->bp_designated_cost) - goto designated; - if (bs->bs_root_path_cost > bp->bp_designated_cost) - continue; - - if (bs->bs_bridge_id < bp->bp_designated_bridge) - goto designated; - if (bs->bs_bridge_id > bp->bp_designated_bridge) - continue; + ifp = bp->bp_ifp; - if (bp->bp_port_id > bp->bp_designated_port) - continue; -designated: - bstp_become_designated_port(bs, bp); - } -} + if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) + return; -static void -bstp_become_designated_port(struct bstp_state *bs, struct bstp_port *bp) -{ - BSTP_LOCK_ASSERT(bs); + MGETHDR(m, M_DONTWAIT, MT_DATA); + if (m == NULL) + return; - bp->bp_designated_root = bs->bs_designated_root; - bp->bp_designated_cost = bs->bs_root_path_cost; - bp->bp_designated_bridge = bs->bs_bridge_id; - bp->bp_designated_port = bp->bp_port_id; -} + eh = mtod(m, struct ether_header *); -static void -bstp_port_state_selection(struct bstp_state *bs) -{ - struct bstp_port *bp; + bpdu->cbu_ssap = bpdu->cbu_dsap = LLC_8021D_LSAP; + bpdu->cbu_ctl = LLC_UI; + bpdu->cbu_protoid = htons(BSTP_PROTO_ID); - BSTP_LOCK_ASSERT(bs); + memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN); + memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN); - LIST_FOREACH(bp, &bs->bs_bplist, bp_next) { - if (bp == bs->bs_root_port) { - bp->bp_config_pending = 0; - bp->bp_topology_change_acknowledge = 0; - bstp_make_forwarding(bs, bp); - } else if (bstp_designated_port(bs, bp)) { - bstp_timer_stop(&bp->bp_message_age_timer); - bstp_make_forwarding(bs, bp); - } else { - bp->bp_config_pending = 0; - bp->bp_topology_change_acknowledge = 0; - bstp_make_blocking(bs, bp); - } - } -} + switch (bpdu->cbu_bpdutype) { + case BSTP_MSGTYPE_CFG: + bpdu->cbu_protover = BSTP_PROTO_STP; + m->m_pkthdr.len = sizeof(*eh) + BSTP_BPDU_STP_LEN; + eh->ether_type = htons(BSTP_BPDU_STP_LEN); + memcpy(mtod(m, caddr_t) + sizeof(*eh), bpdu, + BSTP_BPDU_STP_LEN); + break; -static void -bstp_make_forwarding(struct bstp_state *bs, struct bstp_port *bp) -{ - BSTP_LOCK_ASSERT(bs); + case BSTP_MSGTYPE_RSTP: + bpdu->cbu_protover = BSTP_PROTO_RSTP; + bpdu->cbu_versionlen = htons(0); + m->m_pkthdr.len = sizeof(*eh) + BSTP_BPDU_RSTP_LEN; + eh->ether_type = htons(BSTP_BPDU_RSTP_LEN); + memcpy(mtod(m, caddr_t) + sizeof(*eh), bpdu, + BSTP_BPDU_RSTP_LEN); + break; - if (bp->bp_state == BSTP_IFSTATE_BLOCKING) { - bstp_set_port_state(bp, BSTP_IFSTATE_LISTENING); - bstp_timer_start(&bp->bp_forward_delay_timer, 0); + default: + panic("not implemented"); } -} - -static void -bstp_make_blocking(struct bstp_state *bs, struct bstp_port *bp) -{ - BSTP_LOCK_ASSERT(bs); + m->m_pkthdr.rcvif = ifp; + m->m_len = m->m_pkthdr.len; - if ((bp->bp_state != BSTP_IFSTATE_DISABLED) && - (bp->bp_state != BSTP_IFSTATE_BLOCKING)) { - if ((bp->bp_state == BSTP_IFSTATE_FORWARDING) || - (bp->bp_state == BSTP_IFSTATE_LEARNING)) { - if (bp->bp_change_detection_enabled) { - bstp_topology_change_detection(bs); - } - } - bstp_set_port_state(bp, BSTP_IFSTATE_BLOCKING); - bstp_timer_stop(&bp->bp_forward_delay_timer); - } + bp->bp_txcount++; + bstp_enqueue(ifp, m); } static void -bstp_set_port_state(struct bstp_port *bp, uint8_t state) +bstp_enqueue(struct ifnet *dst_ifp, struct mbuf *m) { - struct bstp_state *bs = bp->bp_bs; + int err = 0; - bp->bp_state = state; + IFQ_ENQUEUE(&dst_ifp->if_snd, m, err); - /* notify the parent bridge */ - if (bs->bs_state_cb != NULL) - taskqueue_enqueue(taskqueue_swi, &bp->bp_statetask); + if ((dst_ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0) + (*dst_ifp->if_start)(dst_ifp); } -/* - * Notify the bridge that a port state has changed, we need to do this from a - * taskqueue to avoid a LOR. - */ -static void -bstp_state_change(void *arg, int pending) +static int +bstp_pdu_flags(struct bstp_port *bp) { - struct bstp_port *bp = (struct bstp_port *)arg; - struct bstp_state *bs = bp->bp_bs; + int flags = 0; - if (bp->bp_active == 1) - (*bs->bs_state_cb)(bp->bp_ifp, bp->bp_state); -} + if (bp->bp_proposing && bp->bp_state != BSTP_IFSTATE_FORWARDING) + flags |= BSTP_PDU_F_P; -static void -bstp_update_forward_transitions(struct bstp_port *bp) -{ - bp->bp_forward_transitions++; -} + if (bp->bp_agree) + flags |= BSTP_PDU_F_A; -static void -bstp_topology_change_detection(struct bstp_state *bs) -{ - BSTP_LOCK_ASSERT(bs); + if (bp->bp_tc_timer.active) + flags |= BSTP_PDU_F_TC; + + if (bp->bp_tc_ack) + flags |= BSTP_PDU_F_TCA; + + switch (bp->bp_state) { + case BSTP_IFSTATE_LEARNING: + flags |= BSTP_PDU_F_L; + break; - if (bstp_root_bridge(bs)) { - bs->bs_topology_change = 1; - bstp_timer_start(&bs->bs_topology_change_timer, 0); - } else if (!bs->bs_topology_change_detected) { - bstp_transmit_tcn(bs); - bstp_timer_start(&bs->bs_tcn_timer, 0); + case BSTP_IFSTATE_FORWARDING: + flags |= (BSTP_PDU_F_L | BSTP_PDU_F_F); + break; } - bs->bs_topology_change_detected = 1; - getmicrotime(&bs->bs_last_tc_time); -} -static void -bstp_topology_change_acknowledged(struct bstp_state *bs) -{ - BSTP_LOCK_ASSERT(bs); + switch (bp->bp_role) { + case BSTP_ROLE_ROOT: + flags |= + (BSTP_PDU_F_ROOT << BSTP_PDU_PRSHIFT); + break; - bs->bs_topology_change_detected = 0; - bstp_timer_stop(&bs->bs_tcn_timer); -} + case BSTP_ROLE_ALTERNATE: + case BSTP_ROLE_BACKUP: /* fall through */ + flags |= + (BSTP_PDU_F_ALT << BSTP_PDU_PRSHIFT); + break; -static void -bstp_acknowledge_topology_change(struct bstp_state *bs, - struct bstp_port *bp) -{ - BSTP_LOCK_ASSERT(bs); + case BSTP_ROLE_DESIGNATED: + flags |= + (BSTP_PDU_F_DESG << BSTP_PDU_PRSHIFT); + break; + } - bp->bp_topology_change_acknowledge = 1; - bstp_transmit_config(bs, bp); + /* Strip off unused flags in either mode */ + switch (bp->bp_protover) { + case BSTP_PROTO_STP: + flags &= BSTP_PDU_STPMASK; + break; + case BSTP_PROTO_RSTP: + flags &= BSTP_PDU_RSTPMASK; + break; + } + return (flags); } struct mbuf * @@ -594,9 +466,6 @@ bstp_input(struct bstp_port *bp, struct struct bstp_state *bs = bp->bp_bs; struct ether_header *eh; struct bstp_tbpdu tpdu; - struct bstp_cbpdu cpdu; - struct bstp_config_unit cu; - struct bstp_tcn_unit tu; uint16_t len; if (bp->bp_active == 0) { @@ -622,59 +491,39 @@ bstp_input(struct bstp_port *bp, struct memcpy(&tpdu, mtod(m, caddr_t), sizeof(tpdu)); + /* basic packet checks */ if (tpdu.tbu_dsap != LLC_8021D_LSAP || tpdu.tbu_ssap != LLC_8021D_LSAP || tpdu.tbu_ctl != LLC_UI) goto out; - if (tpdu.tbu_protoid != 0 || tpdu.tbu_protover != 0) + if (tpdu.tbu_protoid != BSTP_PROTO_ID) goto out; - switch (tpdu.tbu_bpdutype) { - case BSTP_MSGTYPE_TCN: - tu.tu_message_type = tpdu.tbu_bpdutype; - bstp_received_tcn_bpdu(bs, bp, &tu); - break; - case BSTP_MSGTYPE_CFG: - if (m->m_len < sizeof(cpdu) && - (m = m_pullup(m, sizeof(cpdu))) == NULL) + if (tpdu.tbu_protover != bp->bp_protover) { + /* + * Wait for the migration delay timer to expire before changing + * protocol version to avoid flip-flops. + */ + if (bp->bp_flags & BSTP_PORT_CANMIGRATE) + bstp_set_port_proto(bp, tpdu.tbu_protover); + else goto out; - memcpy(&cpdu, mtod(m, caddr_t), sizeof(cpdu)); - - cu.cu_rootid = - (((uint64_t)ntohs(cpdu.cbu_rootpri)) << 48) | - (((uint64_t)cpdu.cbu_rootaddr[0]) << 40) | - (((uint64_t)cpdu.cbu_rootaddr[1]) << 32) | - (((uint64_t)cpdu.cbu_rootaddr[2]) << 24) | - (((uint64_t)cpdu.cbu_rootaddr[3]) << 16) | - (((uint64_t)cpdu.cbu_rootaddr[4]) << 8) | - (((uint64_t)cpdu.cbu_rootaddr[5]) << 0); - - cu.cu_bridge_id = - (((uint64_t)ntohs(cpdu.cbu_bridgepri)) << 48) | - (((uint64_t)cpdu.cbu_bridgeaddr[0]) << 40) | - (((uint64_t)cpdu.cbu_bridgeaddr[1]) << 32) | - (((uint64_t)cpdu.cbu_bridgeaddr[2]) << 24) | - (((uint64_t)cpdu.cbu_bridgeaddr[3]) << 16) | - (((uint64_t)cpdu.cbu_bridgeaddr[4]) << 8) | - (((uint64_t)cpdu.cbu_bridgeaddr[5]) << 0); - - cu.cu_root_path_cost = ntohl(cpdu.cbu_rootpathcost); - cu.cu_message_age = ntohs(cpdu.cbu_messageage); - cu.cu_max_age = ntohs(cpdu.cbu_maxage); - cu.cu_hello_time = ntohs(cpdu.cbu_hellotime); - cu.cu_forward_delay = ntohs(cpdu.cbu_forwarddelay); - cu.cu_port_id = ntohs(cpdu.cbu_portid); - cu.cu_message_type = cpdu.cbu_bpdutype; - cu.cu_topology_change_acknowledgment = - (cpdu.cbu_flags & BSTP_FLAG_TCA) ? 1 : 0; - cu.cu_topology_change = - (cpdu.cbu_flags & BSTP_FLAG_TC) ? 1 : 0; - bstp_received_config_bpdu(bs, bp, &cu); - break; - default: - goto out; } + /* Clear operedge upon receiving a PDU on the port */ + bp->bp_operedge = 0; + bstp_timer_start(&bp->bp_edge_delay_timer, + BSTP_DEFAULT_MIGRATE_DELAY); + + switch (tpdu.tbu_protover) { + case BSTP_PROTO_STP: + bstp_received_stp(bs, bp, m, &tpdu); + break; + + case BSTP_PROTO_RSTP: + bstp_received_rstp(bs, bp, m, &tpdu); + break; + } out: BSTP_UNLOCK(bs); if (m) @@ -683,141 +532,1359 @@ out: } static void -bstp_received_config_bpdu(struct bstp_state *bs, struct bstp_port *bp, - struct bstp_config_unit *cu) +bstp_received_stp(struct bstp_state *bs, struct bstp_port *bp, + struct mbuf *m, struct bstp_tbpdu *tpdu) +{ + struct bstp_cbpdu cpdu; + struct bstp_config_unit *cu = &bp->bp_msg_cu; + struct bstp_tcn_unit tu; + + switch (tpdu->tbu_bpdutype) { + case BSTP_MSGTYPE_TCN: + tu.tu_message_type = tpdu->tbu_bpdutype; + bstp_received_tcn(bs, bp, &tu); + break; + case BSTP_MSGTYPE_CFG: + if (m->m_len < BSTP_BPDU_STP_LEN && + (m = m_pullup(m, BSTP_BPDU_STP_LEN)) == NULL) + return; + memcpy(&cpdu, mtod(m, caddr_t), BSTP_BPDU_STP_LEN); + + bstp_decode_bpdu(bp, &cpdu, cu); + bstp_received_bpdu(bs, bp, cu); + break; + } +} + +static void +bstp_received_rstp(struct bstp_state *bs, struct bstp_port *bp, + struct mbuf *m, struct bstp_tbpdu *tpdu) +{ + struct bstp_cbpdu cpdu; + struct bstp_config_unit *cu = &bp->bp_msg_cu; + + if (tpdu->tbu_bpdutype != BSTP_MSGTYPE_RSTP) { + m_freem(m); + return; + } + + if (m->m_len < BSTP_BPDU_RSTP_LEN && + (m = m_pullup(m, BSTP_BPDU_RSTP_LEN)) == NULL) + return; + memcpy(&cpdu, mtod(m, caddr_t), BSTP_BPDU_RSTP_LEN); + + bstp_decode_bpdu(bp, &cpdu, cu); + bstp_received_bpdu(bs, bp, cu); +} + +static void +bstp_received_tcn(struct bstp_state *bs, struct bstp_port *bp, + struct bstp_tcn_unit *tcn) +{ + bp->bp_rcvdtcn = 1; + bstp_update_tc(bp); +} + +static void +bstp_received_bpdu(struct bstp_state *bs, struct bstp_port *bp, + struct bstp_config_unit *cu) +{ + int type; + + BSTP_LOCK_ASSERT(bs); + + /* We need to have transitioned to INFO_MINE before proceeding */ + switch (bp->bp_infois) { + case BSTP_INFO_DISABLED: + case BSTP_INFO_AGED: + return; + } + + type = bstp_pdu_rcvtype(bp, cu); + + switch (type) { + case BSTP_PDU_SUPERIOR: + bs->bs_allsynced = 0; + bp->bp_agreed = 0; + bp->bp_proposing = 0; + + if (cu->cu_proposal && cu->cu_forwarding == 0) + bp->bp_proposed = 1; + if (cu->cu_topology_change) + bp->bp_rcvdtc = 1; + if (cu->cu_topology_change_ack) + bp->bp_rcvdtca = 1; + + if (bp->bp_agree && + !bstp_pdu_bettersame(bp, BSTP_INFO_RECIEVED)) + bp->bp_agree = 0; + + /* copy the received priority and timers to the port */ + bp->bp_port_pv = cu->cu_pv; + bp->bp_port_msg_age = cu->cu_message_age; + bp->bp_port_max_age = cu->cu_max_age; + bp->bp_port_fdelay = cu->cu_forward_delay; + bp->bp_port_htime = + (cu->cu_hello_time > BSTP_MIN_HELLO_TIME ? + cu->cu_hello_time : BSTP_MIN_HELLO_TIME); + + /* set expiry for the new info */ + bstp_set_timer_msgage(bp); + + bp->bp_infois = BSTP_INFO_RECIEVED; + bstp_assign_roles(bs); + break; + + case BSTP_PDU_REPEATED: + if (cu->cu_proposal && cu->cu_forwarding == 0) + bp->bp_proposed = 1; + if (cu->cu_topology_change) + bp->bp_rcvdtc = 1; + if (cu->cu_topology_change_ack) + bp->bp_rcvdtca = 1; + + /* rearm the age timer */ + bstp_set_timer_msgage(bp); + break; + + case BSTP_PDU_INFERIOR: + if (cu->cu_learning) { + bp->bp_agreed = 1; + bp->bp_proposing = 0; + } + break; + + case BSTP_PDU_INFERIORALT: + /* + * only point to point links are allowed fast + * transitions to forwarding. + */ + if (cu->cu_agree && bp->bp_p2p_link) { + bp->bp_agreed = 1; + bp->bp_proposing = 0; + } else + bp->bp_agreed = 0; + + if (cu->cu_topology_change) + bp->bp_rcvdtc = 1; + if (cu->cu_topology_change_ack) + bp->bp_rcvdtca = 1; + break; + + case BSTP_PDU_OTHER: + return; /* do nothing */ + } + /* update the state machines with the new data */ + bstp_update_state(bs, bp); +} + +static int +bstp_pdu_rcvtype(struct bstp_port *bp, struct bstp_config_unit *cu) +{ + int type; + + /* default return type */ + type = BSTP_PDU_OTHER; + + switch (cu->cu_role) { + case BSTP_ROLE_DESIGNATED: + if (bstp_info_superior(&bp->bp_port_pv, &cu->cu_pv)) + /* bpdu priority is superior */ + type = BSTP_PDU_SUPERIOR; + else if (bstp_info_cmp(&bp->bp_port_pv, &cu->cu_pv) == + INFO_SAME) { + if (bp->bp_port_msg_age != cu->cu_message_age || + bp->bp_port_max_age != cu->cu_max_age || + bp->bp_port_fdelay != cu->cu_forward_delay || + bp->bp_port_htime != cu->cu_hello_time) + /* bpdu priority is equal and timers differ */ + type = BSTP_PDU_SUPERIOR; + else + /* bpdu is equal */ + type = BSTP_PDU_REPEATED; + } else + /* bpdu priority is worse */ + type = BSTP_PDU_INFERIOR; + + break; + + case BSTP_ROLE_ROOT: + case BSTP_ROLE_ALTERNATE: + case BSTP_ROLE_BACKUP: + if (bstp_info_cmp(&bp->bp_port_pv, &cu->cu_pv) <= INFO_SAME) + /* + * not a designated port and priority is the same or + * worse + */ + type = BSTP_PDU_INFERIORALT; + break; + } + + return (type); +} + +static int +bstp_pdu_bettersame(struct bstp_port *bp, int newinfo) +{ + if (newinfo == BSTP_INFO_RECIEVED && + bp->bp_infois == BSTP_INFO_RECIEVED && + bstp_info_cmp(&bp->bp_port_pv, &bp->bp_msg_cu.cu_pv) >= INFO_SAME) { + return (1); + } + + if (newinfo == BSTP_INFO_MINE && + bp->bp_infois == BSTP_INFO_MINE && + bstp_info_cmp(&bp->bp_port_pv, &bp->bp_desg_pv) >= INFO_SAME) { + return (1); + } + + return (0); +} + +static int +bstp_info_cmp(struct bstp_pri_vector *pv, + struct bstp_pri_vector *cpv) +{ + if (cpv->pv_root_id < pv->pv_root_id) + return (INFO_BETTER); + if (cpv->pv_root_id > pv->pv_root_id) + return (INFO_WORSE); + + if (cpv->pv_cost < pv->pv_cost) + return (INFO_BETTER); + if (cpv->pv_cost > pv->pv_cost) + return (INFO_WORSE); + + if (cpv->pv_dbridge_id < pv->pv_dbridge_id) + return (INFO_BETTER); + if (cpv->pv_dbridge_id > pv->pv_dbridge_id) + return (INFO_WORSE); + + if (cpv->pv_dport_id < pv->pv_dport_id) + return (INFO_BETTER); + if (cpv->pv_dport_id > pv->pv_dport_id) + return (INFO_WORSE); + + return (INFO_SAME); +} + +/* + * This message priority vector is superior to the port priority vector and + * will replace it if, and only if, the message priority vector is better than + * the port priority vector, or the message has been transmitted from the same + * designated bridge and designated port as the port priority vector. + */ +static int +bstp_info_superior(struct bstp_pri_vector *pv, + struct bstp_pri_vector *cpv) +{ + if (bstp_info_cmp(pv, cpv) == INFO_BETTER || + (bstp_same_bridgeid(pv->pv_dbridge_id, cpv->pv_dbridge_id) && + (cpv->pv_dport_id & 0xfff) == (pv->pv_dport_id & 0xfff))) + return (1); + return (0); +} + +static void +bstp_assign_roles(struct bstp_state *bs) +{ + struct bstp_port *bp, *rbp = NULL; + struct bstp_pri_vector pv; + + /* default to our priority vector */ + bs->bs_root_pv = bs->bs_bridge_pv; + bs->bs_root_msg_age = 0; + bs->bs_root_max_age = bs->bs_bridge_max_age; + bs->bs_root_fdelay = bs->bs_bridge_fdelay; + bs->bs_root_htime = bs->bs_bridge_htime; + bs->bs_root_port = NULL; + + /* check if any recieved info supersedes us */ + LIST_FOREACH(bp, &bs->bs_bplist, bp_next) { + if (bp->bp_infois != BSTP_INFO_RECIEVED) + continue; + + pv = bp->bp_port_pv; + pv.pv_cost += bp->bp_path_cost; + + /* The root priority vector is the best of the set comprising + * the bridge priority vector plus all root path priority + * vectors whose bridge address is not equal to us. + */ + if (bstp_same_bridgeid(pv.pv_dbridge_id, + bs->bs_bridge_pv.pv_dbridge_id) == 0 && + bstp_info_cmp(&bs->bs_root_pv, &pv) == INFO_BETTER) { + /* the port vector replaces the root */ + bs->bs_root_pv = pv; + bs->bs_root_msg_age = bp->bp_port_msg_age + + BSTP_MESSAGE_AGE_INCR; + bs->bs_root_max_age = bp->bp_port_max_age; + bs->bs_root_fdelay = bp->bp_port_fdelay; + bs->bs_root_htime = bp->bp_port_htime; + rbp = bp; + } + } + + LIST_FOREACH(bp, &bs->bs_bplist, bp_next) { + /* calculate the port designated vector */ + bp->bp_desg_pv.pv_root_id = bs->bs_root_pv.pv_root_id; + bp->bp_desg_pv.pv_cost = bs->bs_root_pv.pv_cost; + bp->bp_desg_pv.pv_dbridge_id = bs->bs_bridge_pv.pv_dbridge_id; + bp->bp_desg_pv.pv_dport_id = bp->bp_port_id; + bp->bp_desg_pv.pv_port_id = bp->bp_port_id; + + /* calculate designated times */ + bp->bp_desg_msg_age = bs->bs_root_msg_age; + bp->bp_desg_max_age = bs->bs_root_max_age; + bp->bp_desg_fdelay = bs->bs_root_fdelay; + bp->bp_desg_htime = bs->bs_bridge_htime; + + + switch (bp->bp_infois) { + case BSTP_INFO_DISABLED: + bstp_set_port_role(bp, BSTP_ROLE_DISABLED); + break; + + case BSTP_INFO_AGED: + bstp_set_port_role(bp, BSTP_ROLE_DESIGNATED); + bstp_update_info(bp); + break; + + case BSTP_INFO_MINE: + bstp_set_port_role(bp, BSTP_ROLE_DESIGNATED); + /* update the port info if stale */ + if (bstp_info_cmp(&bp->bp_port_pv, + &bp->bp_desg_pv) != INFO_SAME || + (rbp != NULL && + (bp->bp_port_msg_age != rbp->bp_port_msg_age || + bp->bp_port_max_age != rbp->bp_port_max_age || + bp->bp_port_fdelay != rbp->bp_port_fdelay || + bp->bp_port_htime != rbp->bp_port_htime))) + bstp_update_info(bp); + break; + + case BSTP_INFO_RECIEVED: + if (bp == rbp) { + /* + * root priority is derived from this + * port, make it the root port. + */ + bstp_set_port_role(bp, BSTP_ROLE_ROOT); + bs->bs_root_port = bp; + } else if (bstp_info_cmp(&bp->bp_port_pv, + &bp->bp_desg_pv) == INFO_BETTER) { + /* + * the port priority is lower than the root + * port. + */ + bstp_set_port_role(bp, BSTP_ROLE_DESIGNATED); + bstp_update_info(bp); + } else { + if (bstp_same_bridgeid( + bp->bp_port_pv.pv_dbridge_id, + bs->bs_bridge_pv.pv_dbridge_id)) { + /* + * the designated bridge refers to + * another port on this bridge. + */ + bstp_set_port_role(bp, + BSTP_ROLE_BACKUP); + } else { + /* + * the port is an inferior path to the + * root bridge. + */ + bstp_set_port_role(bp, + BSTP_ROLE_ALTERNATE); + } + } + break; + } + } +} + +static void +bstp_update_state(struct bstp_state *bs, struct bstp_port *bp) +{ + struct bstp_port *bp2; + int synced; + + BSTP_LOCK_ASSERT(bs); + + /* check if all the ports have syncronised again */ + if (!bs->bs_allsynced) { + synced = 1; + LIST_FOREACH(bp2, &bs->bs_bplist, bp_next) { + if (!(bp->bp_synced || + bp->bp_role == BSTP_ROLE_ROOT)) { + synced = 0; + break; + } + } + bs->bs_allsynced = synced; + } + + bstp_update_roles(bs, bp); + bstp_update_tc(bp); +} + +static void +bstp_update_roles(struct bstp_state *bs, struct bstp_port *bp) +{ + switch (bp->bp_role) { + case BSTP_ROLE_DISABLED: + /* Clear any flags if set */ + if (bp->bp_sync || !bp->bp_synced || bp->bp_reroot) { + bp->bp_sync = 0; + bp->bp_synced = 1; + bp->bp_reroot = 0; + } + break; + + case BSTP_ROLE_ALTERNATE: + case BSTP_ROLE_BACKUP: + if ((bs->bs_allsynced && !bp->bp_agree) || + (bp->bp_proposed && bp->bp_agree)) { + bp->bp_proposed = 0; + bp->bp_agree = 1; + bp->bp_flags |= BSTP_PORT_NEWINFO; + DPRINTF("%s -> ALTERNATE_AGREED\n", + bp->bp_ifp->if_xname); + } + + if (bp->bp_proposed && !bp->bp_agree) { + bstp_set_all_sync(bs); + bp->bp_proposed = 0; + DPRINTF("%s -> ALTERNATE_PROPOSED\n", + bp->bp_ifp->if_xname); + } + + /* Clear any flags if set */ + if (bp->bp_sync || !bp->bp_synced || bp->bp_reroot) { + bp->bp_sync = 0; + bp->bp_synced = 1; + bp->bp_reroot = 0; + DPRINTF("%s -> ALTERNATE_PORT\n", bp->bp_ifp->if_xname); + } + break; + + case BSTP_ROLE_ROOT: + if (bp->bp_state != BSTP_IFSTATE_FORWARDING && !bp->bp_reroot) { + bstp_set_all_reroot(bs); + DPRINTF("%s -> ROOT_REROOT\n", bp->bp_ifp->if_xname); + } + + if ((bs->bs_allsynced && !bp->bp_agree) || + (bp->bp_proposed && bp->bp_agree)) { + bp->bp_proposed = 0; + bp->bp_sync = 0; + bp->bp_agree = 1; + bp->bp_flags |= BSTP_PORT_NEWINFO; + DPRINTF("%s -> ROOT_AGREED\n", bp->bp_ifp->if_xname); + } + + if (bp->bp_proposed && !bp->bp_agree) { + bstp_set_all_sync(bs); + bp->bp_proposed = 0; + DPRINTF("%s -> ROOT_PROPOSED\n", bp->bp_ifp->if_xname); + } + + if (bp->bp_forward_delay_timer.active == 0 || + (bstp_rerooted(bs, bp) && + bp->bp_recent_backup_timer.active == 0 && + bp->bp_protover == BSTP_PROTO_RSTP)) { + switch (bp->bp_state) { + case BSTP_IFSTATE_DISCARDING: + bstp_set_port_state(bp, BSTP_IFSTATE_LEARNING); + break; + case BSTP_IFSTATE_LEARNING: + bstp_set_port_state(bp, + BSTP_IFSTATE_FORWARDING); + break; + } + } + + if (bp->bp_state == BSTP_IFSTATE_FORWARDING && bp->bp_reroot) { + bp->bp_reroot = 0; + DPRINTF("%s -> ROOT_REROOTED\n", bp->bp_ifp->if_xname); + } + break; + + case BSTP_ROLE_DESIGNATED: + if (bp->bp_recent_root_timer.active == 0 && bp->bp_reroot) { + bp->bp_reroot = 0; + DPRINTF("%s -> DESIGNATED_RETIRED\n", + bp->bp_ifp->if_xname); + } + + if ((bp->bp_state == BSTP_IFSTATE_DISCARDING && + !bp->bp_synced) || (bp->bp_agreed && !bp->bp_synced) || + (bp->bp_operedge && !bp->bp_synced) || + (bp->bp_sync && bp->bp_synced)) { + bstp_timer_stop(&bp->bp_recent_root_timer); + bp->bp_synced = 1; + bp->bp_sync = 0; + DPRINTF("%s -> DESIGNATED_SYNCED\n", + bp->bp_ifp->if_xname); + } + + if (bp->bp_state != BSTP_IFSTATE_FORWARDING && + !bp->bp_agreed && !bp->bp_proposing && + !bp->bp_operedge) { + bp->bp_proposing = 1; + bp->bp_flags |= BSTP_PORT_NEWINFO; + bstp_timer_start(&bp->bp_edge_delay_timer, + (bp->bp_p2p_link ? BSTP_DEFAULT_MIGRATE_DELAY : + bp->bp_desg_max_age)); + DPRINTF("%s -> DESIGNATED_PROPOSE\n", + bp->bp_ifp->if_xname); + } + + if ((bp->bp_forward_delay_timer.active == 0 || bp->bp_agreed || + bp->bp_operedge) && + (bp->bp_recent_root_timer.active == 0 || !bp->bp_reroot) && + !bp->bp_sync) { + switch (bp->bp_state) { + case BSTP_IFSTATE_DISCARDING: + bstp_set_port_state(bp, BSTP_IFSTATE_LEARNING); + break; + case BSTP_IFSTATE_LEARNING: + bstp_set_port_state(bp, BSTP_IFSTATE_FORWARDING); + bp->bp_agreed = bp->bp_protover; + break; + } + } + + if (((bp->bp_sync && !bp->bp_synced) || + (bp->bp_reroot && bp->bp_recent_root_timer.active) || + (bp->bp_flags & BSTP_PORT_DISPUTED)) && !bp->bp_operedge && + bp->bp_state != BSTP_IFSTATE_DISCARDING) { + bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING); + bp->bp_flags &= ~BSTP_PORT_DISPUTED; + bstp_timer_start(&bp->bp_forward_delay_timer, + bp->bp_protover == BSTP_PROTO_RSTP ? + bp->bp_desg_htime : bp->bp_desg_fdelay); + DPRINTF("%s -> DESIGNATED_DISCARD\n", + bp->bp_ifp->if_xname); + } + break; + } + + if (bp->bp_flags & BSTP_PORT_NEWINFO) + bstp_transmit(bs, bp); +} + +static void +bstp_update_tc(struct bstp_port *bp) +{ + switch (bp->bp_tcstate) { + case BSTP_TCSTATE_ACTIVE: + if ((bp->bp_role != BSTP_ROLE_DESIGNATED && + bp->bp_role != BSTP_ROLE_ROOT) || bp->bp_operedge) + bstp_set_port_tc(bp, BSTP_TCSTATE_LEARNING); + + if (bp->bp_rcvdtcn) + bstp_set_port_tc(bp, BSTP_TCSTATE_TCN); + if (bp->bp_rcvdtc) + bstp_set_port_tc(bp, BSTP_TCSTATE_TC); + + if (bp->bp_tc_prop && !bp->bp_operedge) + bstp_set_port_tc(bp, BSTP_TCSTATE_PROPAG); + + if (bp->bp_rcvdtca) + bstp_set_port_tc(bp, BSTP_TCSTATE_ACK); + break; + + case BSTP_TCSTATE_INACTIVE: + if ((bp->bp_state == BSTP_IFSTATE_LEARNING || + bp->bp_state == BSTP_IFSTATE_FORWARDING) && + bp->bp_fdbflush == 0) + bstp_set_port_tc(bp, BSTP_TCSTATE_LEARNING); + break; + + case BSTP_TCSTATE_LEARNING: + if (bp->bp_rcvdtc || bp->bp_rcvdtcn || bp->bp_rcvdtca || + bp->bp_tc_prop) + bstp_set_port_tc(bp, BSTP_TCSTATE_LEARNING); + else if (bp->bp_role != BSTP_ROLE_DESIGNATED && + bp->bp_role != BSTP_ROLE_ROOT && + bp->bp_state == BSTP_IFSTATE_DISCARDING) + bstp_set_port_tc(bp, BSTP_TCSTATE_INACTIVE); + + if ((bp->bp_role == BSTP_ROLE_DESIGNATED || + bp->bp_role == BSTP_ROLE_ROOT) && + bp->bp_state == BSTP_IFSTATE_FORWARDING && + !bp->bp_operedge) + bstp_set_port_tc(bp, BSTP_TCSTATE_DETECTED); + break; + + /* these are transient states and go straight back to ACTIVE */ + case BSTP_TCSTATE_DETECTED: + case BSTP_TCSTATE_TCN: + case BSTP_TCSTATE_TC: + case BSTP_TCSTATE_PROPAG: + case BSTP_TCSTATE_ACK: + DPRINTF("Invalid TC state for %s\n", + bp->bp_ifp->if_xname); + break; + } + +} + +static void +bstp_update_info(struct bstp_port *bp) +{ + struct bstp_state *bs = bp->bp_bs; + + bp->bp_proposing = 0; + bp->bp_proposed = 0; + + if (bp->bp_agreed && !bstp_pdu_bettersame(bp, BSTP_INFO_MINE)) + bp->bp_agreed = 0; + + if (bp->bp_synced && !bp->bp_agreed) { + bp->bp_synced = 0; + bs->bs_allsynced = 0; + } + + /* copy the designated pv to the port */ + bp->bp_port_pv = bp->bp_desg_pv; + bp->bp_port_msg_age = bp->bp_desg_msg_age; + bp->bp_port_max_age = bp->bp_desg_max_age; + bp->bp_port_fdelay = bp->bp_desg_fdelay; + bp->bp_port_htime = bp->bp_desg_htime; + bp->bp_infois = BSTP_INFO_MINE; + + bp->bp_flags |= BSTP_PORT_NEWINFO; + bstp_transmit(bs, bp); +} + +/* set tcprop on every port other than the caller */ +static void +bstp_set_other_tcprop(struct bstp_port *bp) +{ + struct bstp_state *bs = bp->bp_bs; + struct bstp_port *bp2; + + BSTP_LOCK_ASSERT(bs); + + LIST_FOREACH(bp2, &bs->bs_bplist, bp_next) { + if (bp2 == bp) + continue; + bp->bp_tc_prop = 1; + } +} + +static void +bstp_set_all_reroot(struct bstp_state *bs) +{ + struct bstp_port *bp; + + BSTP_LOCK_ASSERT(bs); + + LIST_FOREACH(bp, &bs->bs_bplist, bp_next) + bp->bp_reroot = 1; +} + +static void +bstp_set_all_sync(struct bstp_state *bs) +{ + struct bstp_port *bp; + + BSTP_LOCK_ASSERT(bs); + + LIST_FOREACH(bp, &bs->bs_bplist, bp_next) { + bp->bp_sync = 1; + bp->bp_synced = 0; /* Not explicit in spec */ + } + + bs->bs_allsynced = 0; +} + +static void +bstp_set_port_state(struct bstp_port *bp, int state) +{ + if (bp->bp_state == state) + return; + + bp->bp_state = state; + + switch (bp->bp_state) { + case BSTP_IFSTATE_DISCARDING: + DPRINTF("state changed to DISCARDING on %s\n", + bp->bp_ifp->if_xname); + break; + + case BSTP_IFSTATE_LEARNING: + DPRINTF("state changed to LEARNING on %s\n", + bp->bp_ifp->if_xname); + + bstp_timer_start(&bp->bp_forward_delay_timer, + bp->bp_protover == BSTP_PROTO_RSTP ? + bp->bp_desg_htime : bp->bp_desg_fdelay); + break; + + case BSTP_IFSTATE_FORWARDING: + DPRINTF("state changed to FORWARDING on %s\n", + bp->bp_ifp->if_xname); + + bstp_timer_stop(&bp->bp_forward_delay_timer); + /* Record that we enabled forwarding */ + bp->bp_forward_transitions++; + break; + } + + /* notify the parent bridge */ + taskqueue_enqueue(taskqueue_swi, &bp->bp_statetask); +} + +static void +bstp_set_port_role(struct bstp_port *bp, int role) +{ + struct bstp_state *bs = bp->bp_bs; + + if (bp->bp_role == role) + return; + + /* perform pre-change tasks */ + switch (bp->bp_role) { + case BSTP_ROLE_DISABLED: + bstp_timer_start(&bp->bp_forward_delay_timer, + bp->bp_desg_max_age); + break; + + case BSTP_ROLE_BACKUP: + bstp_timer_start(&bp->bp_recent_backup_timer, + bp->bp_desg_htime * 2); + /* fall through */ + case BSTP_ROLE_ALTERNATE: + bstp_timer_start(&bp->bp_forward_delay_timer, + bp->bp_desg_fdelay); + bp->bp_sync = 0; + bp->bp_synced = 1; + bp->bp_reroot = 0; + break; + + case BSTP_ROLE_ROOT: + bstp_timer_start(&bp->bp_recent_root_timer, + BSTP_DEFAULT_FORWARD_DELAY); + break; + } + + bp->bp_role = role; + /* clear values not carried between roles */ + bp->bp_proposing = 0; + bs->bs_allsynced = 0; + + /* initialise the new role */ + switch (bp->bp_role) { + case BSTP_ROLE_DISABLED: + case BSTP_ROLE_ALTERNATE: + case BSTP_ROLE_BACKUP: + DPRINTF("%s role -> ALT/BACK/DISABLED\n", + bp->bp_ifp->if_xname); + bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING); + bstp_timer_stop(&bp->bp_recent_root_timer); + bstp_timer_latch(&bp->bp_forward_delay_timer); + bp->bp_sync = 0; + bp->bp_synced = 1; + bp->bp_reroot = 0; + break; + + case BSTP_ROLE_ROOT: + DPRINTF("%s role -> ROOT\n", + bp->bp_ifp->if_xname); + bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING); + bstp_timer_latch(&bp->bp_recent_root_timer); + bp->bp_proposing = 0; + break; + + case BSTP_ROLE_DESIGNATED: + DPRINTF("%s role -> DESIGNATED\n", + bp->bp_ifp->if_xname); + bstp_timer_start(&bp->bp_hello_timer, + bp->bp_desg_htime); + bp->bp_agree = 0; + break; + } + + /* let the TC state know that the role changed */ + bstp_update_tc(bp); +} + +static void +bstp_set_port_proto(struct bstp_port *bp, int proto) +{ + struct bstp_state *bs = bp->bp_bs; + + /* supported protocol versions */ + switch (proto) { + case BSTP_PROTO_STP: + /* we can downgrade protocols only */ + bstp_timer_stop(&bp->bp_migrate_delay_timer); + /* clear unsupported features */ + bp->bp_operedge = 0; + break; + + case BSTP_PROTO_RSTP: + bstp_timer_start(&bp->bp_migrate_delay_timer, + bs->bs_migration_delay); + break; + + default: + DPRINTF("Unsupported STP version %d\n", proto); + return; + } + + bp->bp_protover = proto; + bp->bp_flags &= ~BSTP_PORT_CANMIGRATE; +} + +static void +bstp_set_port_tc(struct bstp_port *bp, int state) +{ + struct bstp_state *bs = bp->bp_bs; + + bp->bp_tcstate = state; + + /* initialise the new state */ + switch (bp->bp_tcstate) { + case BSTP_TCSTATE_ACTIVE: + DPRINTF("%s -> TC_ACTIVE\n", bp->bp_ifp->if_xname); + /* nothing to do */ + break; + + case BSTP_TCSTATE_INACTIVE: + bstp_timer_stop(&bp->bp_tc_timer); + /* flush routes on the parent bridge */ + bp->bp_fdbflush = 1; + taskqueue_enqueue(taskqueue_swi, &bp->bp_rtagetask); + bp->bp_tc_ack = 0; + DPRINTF("%s -> TC_INACTIVE\n", bp->bp_ifp->if_xname); + break; + + case BSTP_TCSTATE_LEARNING: + bp->bp_rcvdtc = 0; + bp->bp_rcvdtcn = 0; + bp->bp_rcvdtca = 0; + bp->bp_tc_prop = 0; + DPRINTF("%s -> TC_LEARNING\n", bp->bp_ifp->if_xname); + break; + + case BSTP_TCSTATE_DETECTED: + bstp_set_timer_tc(bp); + bstp_set_other_tcprop(bp); + /* send out notification */ + bp->bp_flags |= BSTP_PORT_NEWINFO; + bstp_transmit(bs, bp); + getmicrotime(&bs->bs_last_tc_time); + DPRINTF("%s -> TC_DETECTED\n", bp->bp_ifp->if_xname); + bp->bp_tcstate = BSTP_TCSTATE_ACTIVE; /* UCT */ + break; + + case BSTP_TCSTATE_TCN: + bstp_set_timer_tc(bp); + DPRINTF("%s -> TC_TCN\n", bp->bp_ifp->if_xname); + /* fall through */ + case BSTP_TCSTATE_TC: + bp->bp_rcvdtc = 0; + bp->bp_rcvdtcn = 0; + if (bp->bp_role == BSTP_ROLE_DESIGNATED) + bp->bp_tc_ack = 1; + + bstp_set_other_tcprop(bp); + DPRINTF("%s -> TC_TC\n", bp->bp_ifp->if_xname); + bp->bp_tcstate = BSTP_TCSTATE_ACTIVE; /* UCT */ + break; + + case BSTP_TCSTATE_PROPAG: + /* flush routes on the parent bridge */ + bp->bp_fdbflush = 1; + taskqueue_enqueue(taskqueue_swi, &bp->bp_rtagetask); + bp->bp_tc_prop = 0; + bstp_set_timer_tc(bp); + DPRINTF("%s -> TC_PROPAG\n", bp->bp_ifp->if_xname); + bp->bp_tcstate = BSTP_TCSTATE_ACTIVE; /* UCT */ + break; + + case BSTP_TCSTATE_ACK: + bstp_timer_stop(&bp->bp_tc_timer); + bp->bp_rcvdtca = 0; + DPRINTF("%s -> TC_ACK\n", bp->bp_ifp->if_xname); + bp->bp_tcstate = BSTP_TCSTATE_ACTIVE; /* UCT */ + break; + } +} + +static void +bstp_set_timer_tc(struct bstp_port *bp) +{ + struct bstp_state *bs = bp->bp_bs; + + if (bp->bp_tc_timer.active) + return; + + switch (bp->bp_protover) { + case BSTP_PROTO_RSTP: + bstp_timer_start(&bp->bp_tc_timer, + bp->bp_desg_htime + BSTP_TICK_VAL); + bp->bp_flags |= BSTP_PORT_NEWINFO; + break; + + case BSTP_PROTO_STP: + bstp_timer_start(&bp->bp_tc_timer, + bs->bs_root_max_age + bs->bs_root_fdelay); + break; + } +} + +static void +bstp_set_timer_msgage(struct bstp_port *bp) +{ + if (bp->bp_port_msg_age + BSTP_MESSAGE_AGE_INCR <= + bp->bp_port_max_age) { + bstp_timer_start(&bp->bp_message_age_timer, + bp->bp_port_htime * 3); + } else + /* expires immediately */ + bstp_timer_start(&bp->bp_message_age_timer, 0); +} + +static int +bstp_rerooted(struct bstp_state *bs, struct bstp_port *bp) +{ + struct bstp_port *bp2; + int rr_set = 0; + + LIST_FOREACH(bp2, &bs->bs_bplist, bp_next) { + if (bp2 == bp) + continue; + if (bp2->bp_recent_root_timer.active) { + rr_set = 1; + break; + } + } + return (!rr_set); +} + +int +bstp_set_htime(struct bstp_state *bs, int t) +{ + /* convert seconds to ticks */ + t *= BSTP_TICK_VAL; + + /* value can only be changed in leagacy stp mode */ + if (bs->bs_protover != BSTP_PROTO_STP) + return (EINVAL); + + if (t < BSTP_MIN_HELLO_TIME || t > BSTP_MAX_HELLO_TIME) + return (EINVAL); + + BSTP_LOCK(bs); + bs->bs_bridge_htime = t; + bstp_reinit(bs); + BSTP_UNLOCK(bs); + return (0); +} + +int +bstp_set_fdelay(struct bstp_state *bs, int t) +{ + /* convert seconds to ticks */ + t *= BSTP_TICK_VAL; + + if (t < BSTP_MIN_FORWARD_DELAY || t > BSTP_MAX_FORWARD_DELAY) + return (EINVAL); + + BSTP_LOCK(bs); + bs->bs_bridge_fdelay = t; + bstp_reinit(bs); + BSTP_UNLOCK(bs); + return (0); +} + +int +bstp_set_maxage(struct bstp_state *bs, int t) +{ + /* convert seconds to ticks */ + t *= BSTP_TICK_VAL; + + if (t < BSTP_MIN_MAX_AGE || t > BSTP_MAX_MAX_AGE) + return (EINVAL); + + BSTP_LOCK(bs); + bs->bs_bridge_max_age = t; + bstp_reinit(bs); + BSTP_UNLOCK(bs); + return (0); +} + +int +bstp_set_holdcount(struct bstp_state *bs, int count) +{ + struct bstp_port *bp; + + if (count < BSTP_MIN_HOLD_COUNT || + count > BSTP_MAX_HOLD_COUNT) + return (EINVAL); + + BSTP_LOCK(bs); + bs->bs_txholdcount = count; + LIST_FOREACH(bp, &bs->bs_bplist, bp_next) + bp->bp_txcount = 0; + BSTP_UNLOCK(bs); + return (0); +} + +int +bstp_set_protocol(struct bstp_state *bs, int proto) +{ + struct bstp_port *bp; + + switch (proto) { + /* Supported protocol versions */ + case BSTP_PROTO_STP: + case BSTP_PROTO_RSTP: + break; + + default: + return (EINVAL); + } + + BSTP_LOCK(bs); + bs->bs_protover = proto; + bs->bs_bridge_htime = BSTP_DEFAULT_HELLO_TIME; + LIST_FOREACH(bp, &bs->bs_bplist, bp_next) { + /* reinit state */ + bp->bp_infois = BSTP_INFO_DISABLED; + bp->bp_txcount = 0; + bstp_set_port_proto(bp, bs->bs_protover); + bstp_set_port_role(bp, BSTP_ROLE_DISABLED); + bstp_set_port_tc(bp, BSTP_TCSTATE_INACTIVE); + bstp_timer_stop(&bp->bp_recent_backup_timer); + } + bstp_reinit(bs); + BSTP_UNLOCK(bs); + return (0); +} + +int +bstp_set_priority(struct bstp_state *bs, int pri) +{ + if (pri < 0 || pri > BSTP_MAX_PRIORITY) + return (EINVAL); + + /* Limit to steps of 4096 */ + pri -= pri % 4096; + + BSTP_LOCK(bs); + bs->bs_bridge_priority = pri; + bstp_reinit(bs); + BSTP_UNLOCK(bs); + return (0); +} + +int +bstp_set_port_priority(struct bstp_port *bp, int pri) +{ + struct bstp_state *bs = bp->bp_bs; + + if (pri < 0 || pri > BSTP_MAX_PORT_PRIORITY) + return (EINVAL); + + /* Limit to steps of 16 */ + pri -= pri % 16; + + BSTP_LOCK(bs); + bp->bp_priority = pri; + bstp_reinit(bs); + BSTP_UNLOCK(bs); + return (0); +} + +int +bstp_set_path_cost(struct bstp_port *bp, uint32_t path_cost) +{ + struct bstp_state *bs = bp->bp_bs; + + if (path_cost > BSTP_MAX_PATH_COST) + return (EINVAL); + + BSTP_LOCK(bs); + + if (path_cost == 0) { /* use auto */ + bp->bp_flags &= ~BSTP_PORT_ADMCOST; + bp->bp_path_cost = bstp_calc_path_cost(bp); + } else { + bp->bp_path_cost = path_cost; + bp->bp_flags |= BSTP_PORT_ADMCOST; + } + bstp_reinit(bs); + BSTP_UNLOCK(bs); + return (0); +} + +/* + * Calculate the path cost according to the link speed. + */ +static uint32_t +bstp_calc_path_cost(struct bstp_port *bp) +{ + struct ifnet *ifp = bp->bp_ifp; + uint32_t path_cost; + + /* If the priority has been manually set then retain the value */ + if (bp->bp_flags & BSTP_PORT_ADMCOST) + return bp->bp_path_cost; + + if (ifp->if_baudrate < 1000) + return (BSTP_DEFAULT_PATH_COST); + + /* formula from section 17.14, IEEE Std 802.1D-2004 */ + path_cost = 20000000000 / (ifp->if_baudrate / 1000); + + if (path_cost > BSTP_MAX_PATH_COST) + path_cost = BSTP_MAX_PATH_COST; + + /* STP compat mode only uses 16 bits of the 32 */ + if (bp->bp_protover == BSTP_PROTO_STP && path_cost > 65535) + path_cost = 65535; + + return (path_cost); +} + +/* + * Notify the bridge that a port state has changed, we need to do this from a + * taskqueue to avoid a LOR. + */ +static void +bstp_notify_state(void *arg, int pending) +{ + struct bstp_port *bp = (struct bstp_port *)arg; + struct bstp_state *bs = bp->bp_bs; + + if (bp->bp_active == 1 && bs->bs_state_cb != NULL) + (*bs->bs_state_cb)(bp->bp_ifp, bp->bp_state); +} + +/* + * Flush the routes on the bridge port, we need to do this from a + * taskqueue to avoid a LOR. + */ +static void +bstp_notify_rtage(void *arg, int pending) +{ + struct bstp_port *bp = (struct bstp_port *)arg; + struct bstp_state *bs = bp->bp_bs; + int age = 0; + + BSTP_LOCK(bs); + switch (bp->bp_protover) { + case BSTP_PROTO_STP: + /* convert to seconds */ + age = bp->bp_desg_fdelay / BSTP_TICK_VAL; + break; + + case BSTP_PROTO_RSTP: + age = 0; + break; + } + BSTP_UNLOCK(bs); + + if (bp->bp_active == 1 && bs->bs_rtage_cb != NULL) + (*bs->bs_rtage_cb)(bp->bp_ifp, age); + + /* flush is complete */ + BSTP_LOCK(bs); + bp->bp_fdbflush = 0; + BSTP_UNLOCK(bs); +} + +void +bstp_linkstate(struct ifnet *ifp, int state) +{ + struct bstp_state *bs; + struct bstp_port *bp; + + /* search for the stp port */ + mtx_lock(&bstp_list_mtx); + LIST_FOREACH(bs, &bstp_list, bs_list) { + BSTP_LOCK(bs); + LIST_FOREACH(bp, &bs->bs_bplist, bp_next) { + if (bp->bp_ifp == ifp) { + bstp_ifupdstatus(bs, bp); + /* it only exists once so return */ + BSTP_UNLOCK(bs); + mtx_unlock(&bstp_list_mtx); + return; + } + } + BSTP_UNLOCK(bs); + } + mtx_unlock(&bstp_list_mtx); +} + +static void +bstp_ifupdstatus(struct bstp_state *bs, struct bstp_port *bp) +{ + struct ifnet *ifp = bp->bp_ifp; + struct ifmediareq ifmr; + int error = 0; + + BSTP_LOCK_ASSERT(bs); + + bzero((char *)&ifmr, sizeof(ifmr)); + error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr); + + if ((error == 0) && (ifp->if_flags & IFF_UP)) { + if (ifmr.ifm_status & IFM_ACTIVE) { + /* A full-duplex link is assumed to be point to point */ + bp->bp_p2p_link = ifmr.ifm_active & IFM_FDX ? 1 : 0; + + if (bp->bp_role == BSTP_ROLE_DISABLED) + bstp_enable_port(bs, bp); + } else { + if (bp->bp_role != BSTP_ROLE_DISABLED) + bstp_disable_port(bs, bp); + } + return; + } + + if (bp->bp_infois != BSTP_INFO_DISABLED) + bstp_disable_port(bs, bp); +} + +static void +bstp_enable_port(struct bstp_state *bs, struct bstp_port *bp) +{ + bp->bp_infois = BSTP_INFO_AGED; + bstp_assign_roles(bs); +} + +static void +bstp_disable_port(struct bstp_state *bs, struct bstp_port *bp) +{ + bp->bp_infois = BSTP_INFO_DISABLED; + bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING); + bstp_assign_roles(bs); +} + +static void +bstp_tick(void *arg) { - int root; + struct bstp_state *bs = arg; + struct bstp_port *bp; BSTP_LOCK_ASSERT(bs); - root = bstp_root_bridge(bs); + /* slow timer to catch missed link events */ + if (bstp_timer_expired(&bs->bs_link_timer)) { + LIST_FOREACH(bp, &bs->bs_bplist, bp_next) + bstp_ifupdstatus(bs, bp); + bstp_timer_start(&bs->bs_link_timer, BSTP_LINK_TIMER); + } - if (bp->bp_state != BSTP_IFSTATE_DISABLED) { - if (bstp_supersedes_port_info(bs, bp, cu)) { - bstp_record_config_information(bs, bp, cu); - bstp_configuration_update(bs); - bstp_port_state_selection(bs); - - if ((bstp_root_bridge(bs) == 0) && root) { - bstp_timer_stop(&bs->bs_hello_timer); - - if (bs->bs_topology_change_detected) { - bstp_timer_stop( - &bs->bs_topology_change_timer); - bstp_transmit_tcn(bs); - bstp_timer_start(&bs->bs_tcn_timer, 0); - } - } + LIST_FOREACH(bp, &bs->bs_bplist, bp_next) { + /* no events need to happen for these */ + bstp_timer_expired(&bp->bp_tc_timer); + bstp_timer_expired(&bp->bp_recent_root_timer); + bstp_timer_expired(&bp->bp_forward_delay_timer); + bstp_timer_expired(&bp->bp_recent_backup_timer); - if (bp == bs->bs_root_port) { - bstp_record_config_timeout_values(bs, cu); - bstp_config_bpdu_generation(bs); + if (bstp_timer_expired(&bp->bp_hello_timer)) + bstp_hello_timer_expiry(bs, bp); - if (cu->cu_topology_change_acknowledgment) - bstp_topology_change_acknowledged(bs); - } - } else if (bstp_designated_port(bs, bp)) - bstp_transmit_config(bs, bp); - } -} + if (bstp_timer_expired(&bp->bp_message_age_timer)) + bstp_message_age_expiry(bs, bp); -static void -bstp_received_tcn_bpdu(struct bstp_state *bs, struct bstp_port *bp, - struct bstp_tcn_unit *tcn) -{ - if (bp->bp_state != BSTP_IFSTATE_DISABLED && - bstp_designated_port(bs, bp)) { - bstp_topology_change_detection(bs); - bstp_acknowledge_topology_change(bs, bp); + if (bstp_timer_expired(&bp->bp_migrate_delay_timer)) + bstp_migrate_delay_expiry(bs, bp); + + if (bstp_timer_expired(&bp->bp_edge_delay_timer)) + bstp_edge_delay_expiry(bs, bp); + + /* update the various state machines for the port */ + bstp_update_state(bs, bp); + + if (bp->bp_txcount > 0) + bp->bp_txcount--; } + + callout_reset(&bs->bs_bstpcallout, hz, bstp_tick, bs); } static void -bstp_hello_timer_expiry(struct bstp_state *bs) +bstp_timer_start(struct bstp_timer *t, uint16_t v) { - bstp_config_bpdu_generation(bs); - bstp_timer_start(&bs->bs_hello_timer, 0); + t->value = v; + t->active = 1; + t->latched = 0; } static void -bstp_message_age_timer_expiry(struct bstp_state *bs, - struct bstp_port *bp) +bstp_timer_stop(struct bstp_timer *t) { - int root; - - BSTP_LOCK_ASSERT(bs); - - root = bstp_root_bridge(bs); - bstp_become_designated_port(bs, bp); - bstp_configuration_update(bs); - bstp_port_state_selection(bs); - - if ((bstp_root_bridge(bs)) && (root == 0)) { - bs->bs_max_age = bs->bs_bridge_max_age; - bs->bs_hello_time = bs->bs_bridge_hello_time; - bs->bs_forward_delay = bs->bs_bridge_forward_delay; - - bstp_topology_change_detection(bs); - bstp_timer_stop(&bs->bs_tcn_timer); - bstp_config_bpdu_generation(bs); - bstp_timer_start(&bs->bs_hello_timer, 0); - } + t->value = 0; + t->active = 0; + t->latched = 0; } static void -bstp_forward_delay_timer_expiry(struct bstp_state *bs, - struct bstp_port *bp) -{ - if (bp->bp_state == BSTP_IFSTATE_LISTENING) { - bstp_set_port_state(bp, BSTP_IFSTATE_LEARNING); - bstp_timer_start(&bp->bp_forward_delay_timer, 0); - } else if (bp->bp_state == BSTP_IFSTATE_LEARNING) { - bstp_set_port_state(bp, BSTP_IFSTATE_FORWARDING); - bstp_update_forward_transitions(bp); - if (bstp_designated_for_some_port(bs) && - bp->bp_change_detection_enabled) - bstp_topology_change_detection(bs); - } +bstp_timer_latch(struct bstp_timer *t) +{ + t->latched = 1; + t->active = 1; } static int -bstp_designated_for_some_port(struct bstp_state *bs) +bstp_timer_expired(struct bstp_timer *t) { - - struct bstp_port *bp; - - BSTP_LOCK_ASSERT(bs); - - LIST_FOREACH(bp, &bs->bs_bplist, bp_next) { - if (bp->bp_designated_bridge == bs->bs_bridge_id) - return (1); + if (t->active == 0 || t->latched) + return (0); + t->value -= BSTP_TICK_VAL; + if (t->value <= 0) { + bstp_timer_stop(t); + return (1); } return (0); + } static void -bstp_tcn_timer_expiry(struct bstp_state *bs) +bstp_hello_timer_expiry(struct bstp_state *bs, struct bstp_port *bp) { - BSTP_LOCK_ASSERT(bs); - - bstp_transmit_tcn(bs); - bstp_timer_start(&bs->bs_tcn_timer, 0); + if ((bp->bp_flags & BSTP_PORT_NEWINFO) || + bp->bp_role == BSTP_ROLE_DESIGNATED || + (bp->bp_role == BSTP_ROLE_ROOT && + bp->bp_tc_timer.active == 1)) { + bstp_timer_start(&bp->bp_hello_timer, bp->bp_desg_htime); + bp->bp_flags |= BSTP_PORT_NEWINFO; + bstp_transmit(bs, bp); + } } static void -bstp_topology_change_timer_expiry(struct bstp_state *bs) +bstp_message_age_expiry(struct bstp_state *bs, struct bstp_port *bp) { - BSTP_LOCK_ASSERT(bs); + if (bp->bp_infois == BSTP_INFO_RECIEVED) { + bp->bp_infois = BSTP_INFO_AGED; + bstp_assign_roles(bs); + DPRINTF("aged info on %s\n", bp->bp_ifp->if_xname); + } +} - bs->bs_topology_change_detected = 0; - bs->bs_topology_change = 0; +static void +bstp_migrate_delay_expiry(struct bstp_state *bs, struct bstp_port *bp) +{ + bp->bp_flags |= BSTP_PORT_CANMIGRATE; } static void -bstp_hold_timer_expiry(struct bstp_state *bs, struct bstp_port *bp) +bstp_edge_delay_expiry(struct bstp_state *bs, struct bstp_port *bp) { - if (bp->bp_config_pending) - bstp_transmit_config(bs, bp); + if (bp->bp_protover == BSTP_PROTO_RSTP && bp->bp_proposing && + bp->bp_role == BSTP_ROLE_DESIGNATED) + bp->bp_operedge = 1; } static int @@ -832,18 +1899,36 @@ bstp_addr_cmp(const uint8_t *a, const ui return (d); } +/* + * compare the bridge address component of the bridgeid + */ +static int +bstp_same_bridgeid(uint64_t id1, uint64_t id2) +{ + u_char addr1[ETHER_ADDR_LEN]; + u_char addr2[ETHER_ADDR_LEN]; + + PV2ADDR(id1, addr1); + PV2ADDR(id2, addr2); + + if (bstp_addr_cmp(addr1, addr2) == 0) { + return (1); + } + return (0); +} + void bstp_reinit(struct bstp_state *bs) { struct bstp_port *bp, *mbp; u_char *e_addr; - BSTP_LOCK(bs); + BSTP_LOCK_ASSERT(bs); mbp = NULL; LIST_FOREACH(bp, &bs->bs_bplist, bp_next) { bp->bp_port_id = (bp->bp_priority << 8) | - (bp->bp_ifp->if_index & 0xff); + (bp->bp_ifp->if_index & 0xfff); if (mbp == NULL) { mbp = bp; @@ -856,13 +1941,12 @@ bstp_reinit(struct bstp_state *bs) } } if (mbp == NULL) { - BSTP_UNLOCK(bs); - bstp_stop(bs); + bstp_stop_locked(bs); return; } e_addr = IF_LLADDR(mbp->bp_ifp); - bs->bs_bridge_id = + bs->bs_bridge_pv.pv_dbridge_id = (((uint64_t)bs->bs_bridge_priority) << 48) | (((uint64_t)e_addr[0]) << 40) | (((uint64_t)e_addr[1]) << 32) | @@ -871,17 +1955,10 @@ bstp_reinit(struct bstp_state *bs) (((uint64_t)e_addr[4]) << 8) | (((uint64_t)e_addr[5])); - bs->bs_designated_root = bs->bs_bridge_id; - bs->bs_root_path_cost = 0; - bs->bs_root_port = NULL; - - bs->bs_max_age = bs->bs_bridge_max_age; - bs->bs_hello_time = bs->bs_bridge_hello_time; - bs->bs_forward_delay = bs->bs_bridge_forward_delay; - bs->bs_topology_change_detected = 0; - bs->bs_topology_change = 0; - bstp_timer_stop(&bs->bs_tcn_timer); - bstp_timer_stop(&bs->bs_topology_change_timer); + bs->bs_bridge_pv.pv_root_id = bs->bs_bridge_pv.pv_dbridge_id; + bs->bs_bridge_pv.pv_cost = 0; + bs->bs_bridge_pv.pv_dport_id = 0; + bs->bs_bridge_pv.pv_port_id = 0; if (callout_pending(&bs->bs_bstpcallout) == 0) callout_reset(&bs->bs_bstpcallout, hz, @@ -891,11 +1968,8 @@ bstp_reinit(struct bstp_state *bs) bstp_ifupdstatus(bs, bp); getmicrotime(&bs->bs_last_tc_time); - bstp_port_state_selection(bs); - bstp_config_bpdu_generation(bs); - bstp_timer_start(&bs->bs_hello_timer, 0); - bstp_timer_start(&bs->bs_link_timer, 0); - BSTP_UNLOCK(bs); + bstp_assign_roles(bs); + bstp_timer_start(&bs->bs_link_timer, BSTP_LINK_TIMER); } static int @@ -927,18 +2001,25 @@ DECLARE_MODULE(bridgestp, bstp_mod, SI_S MODULE_VERSION(bridgestp, 1); void -bstp_attach(struct bstp_state *bs, bstp_state_cb_t state_callback) +bstp_attach(struct bstp_state *bs, bstp_state_cb_t state_callback, + bstp_rtage_cb_t rtage_callback) { BSTP_LOCK_INIT(bs); callout_init_mtx(&bs->bs_bstpcallout, &bs->bs_mtx, 0); LIST_INIT(&bs->bs_bplist); bs->bs_bridge_max_age = BSTP_DEFAULT_MAX_AGE; - bs->bs_bridge_hello_time = BSTP_DEFAULT_HELLO_TIME; - bs->bs_bridge_forward_delay = BSTP_DEFAULT_FORWARD_DELAY; + bs->bs_bridge_htime = BSTP_DEFAULT_HELLO_TIME; + bs->bs_bridge_fdelay = BSTP_DEFAULT_FORWARD_DELAY; bs->bs_bridge_priority = BSTP_DEFAULT_BRIDGE_PRIORITY; bs->bs_hold_time = BSTP_DEFAULT_HOLD_TIME; + bs->bs_migration_delay = BSTP_DEFAULT_MIGRATE_DELAY; + bs->bs_txholdcount = BSTP_DEFAULT_HOLD_COUNT; + bs->bs_protover = BSTP_PROTO_RSTP; bs->bs_state_cb = state_callback; + bs->bs_rtage_cb = rtage_callback; + + getmicrotime(&bs->bs_last_tc_time); mtx_lock(&bstp_list_mtx); LIST_INSERT_HEAD(&bstp_list, bs, bs_list); @@ -959,294 +2040,32 @@ bstp_detach(struct bstp_state *bs) void bstp_init(struct bstp_state *bs) { + BSTP_LOCK(bs); callout_reset(&bs->bs_bstpcallout, hz, bstp_tick, bs); bstp_reinit(bs); + BSTP_UNLOCK(bs); } void bstp_stop(struct bstp_state *bs) { - struct bstp_port *bp; - BSTP_LOCK(bs); - - LIST_FOREACH(bp, &bs->bs_bplist, bp_next) { - bstp_set_port_state(bp, BSTP_IFSTATE_DISABLED); - bstp_timer_stop(&bp->bp_hold_timer); - bstp_timer_stop(&bp->bp_message_age_timer); - bstp_timer_stop(&bp->bp_forward_delay_timer); - } - - callout_drain(&bs->bs_bstpcallout); - callout_stop(&bs->bs_bstpcallout); - - bstp_timer_stop(&bs->bs_topology_change_timer); - bstp_timer_stop(&bs->bs_tcn_timer); - bstp_timer_stop(&bs->bs_hello_timer); - + bstp_stop_locked(bs); BSTP_UNLOCK(bs); } static void -bstp_initialize_port(struct bstp_state *bs, struct bstp_port *bp) -{ - BSTP_LOCK_ASSERT(bs); - - bstp_become_designated_port(bs, bp); - bstp_set_port_state(bp, BSTP_IFSTATE_BLOCKING); - bp->bp_topology_change_acknowledge = 0; - bp->bp_config_pending = 0; - bp->bp_change_detection_enabled = 1; - bstp_timer_stop(&bp->bp_message_age_timer); - bstp_timer_stop(&bp->bp_forward_delay_timer); - bstp_timer_stop(&bp->bp_hold_timer); -} - -static void -bstp_enable_port(struct bstp_state *bs, struct bstp_port *bp) -{ - bstp_initialize_port(bs, bp); - bstp_port_state_selection(bs); -} - -static void -bstp_disable_port(struct bstp_state *bs, struct bstp_port *bp) -{ - int root; - - BSTP_LOCK_ASSERT(bs); - - root = bstp_root_bridge(bs); - bstp_become_designated_port(bs, bp); - bstp_set_port_state(bp, BSTP_IFSTATE_DISABLED); - bp->bp_topology_change_acknowledge = 0; - bp->bp_config_pending = 0; - bstp_timer_stop(&bp->bp_message_age_timer); - bstp_timer_stop(&bp->bp_forward_delay_timer); - bstp_configuration_update(bs); - bstp_port_state_selection(bs); - - if (bstp_root_bridge(bs) && (root == 0)) { - bs->bs_max_age = bs->bs_bridge_max_age; - bs->bs_hello_time = bs->bs_bridge_hello_time; - bs->bs_forward_delay = bs->bs_bridge_forward_delay; - - bstp_topology_change_detection(bs); - bstp_timer_stop(&bs->bs_tcn_timer); - bstp_config_bpdu_generation(bs); - bstp_timer_start(&bs->bs_hello_timer, 0); - } -} - -#ifdef notused -static void -bstp_set_bridge_priority(struct bstp_state *bs, uint64_t new_bridge_id) -{ - struct bstp_port *bp; - int root; - - BSTP_LOCK_ASSERT(bs); - - root = bstp_root_bridge(bs); - - LIST_FOREACH(bp, &bs->bs_bplist, bp_next) { - if (bstp_designated_port(bs, bp)) - bp->bp_designated_bridge = new_bridge_id; - } - - bs->bs_bridge_id = new_bridge_id; - - bstp_configuration_update(bs); - bstp_port_state_selection(bs); - - if (bstp_root_bridge(bs) && (root == 0)) { - bs->bs_max_age = bs->bs_bridge_max_age; - bs->bs_hello_time = bs->bs_bridge_hello_time; - bs->bs_forward_delay = bs->bs_bridge_forward_delay; - - bstp_topology_change_detection(bs); - bstp_timer_stop(&bs->bs_tcn_timer); - bstp_config_bpdu_generation(bs); - bstp_timer_start(&bs->bs_hello_timer, 0); - } -} - -static void -bstp_set_port_priority(struct bstp_state *bs, struct bstp_port *bp, - uint16_t new_port_id) -{ - if (bstp_designated_port(bs, bp)) - bp->bp_designated_port = new_port_id; - - bp->bp_port_id = new_port_id; - - if ((bs->bs_bridge_id == bp->bp_designated_bridge) && - (bp->bp_port_id < bp->bp_designated_port)) { - bstp_become_designated_port(bs, bp); - bstp_port_state_selection(bs); - } -} - -static void -bstp_set_path_cost(struct bstp_state *bs, struct bstp_port *bp, - uint32_t path_cost) -{ - bp->bp_path_cost = path_cost; - bstp_configuration_update(bs); - bstp_port_state_selection(bs); -} - -static void -bstp_enable_change_detection(struct bstp_port *bp) -{ - bp->bp_change_detection_enabled = 1; -} - -static void -bstp_disable_change_detection(struct bstp_port *bp) -{ - bp->bp_change_detection_enabled = 0; -} -#endif /* notused */ - -static void -bstp_enqueue(struct ifnet *dst_ifp, struct mbuf *m) -{ - int err = 0; - - IFQ_ENQUEUE(&dst_ifp->if_snd, m, err); - - if ((dst_ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0) - (*dst_ifp->if_start)(dst_ifp); -} - -void -bstp_linkstate(struct ifnet *ifp, int state) -{ - struct bstp_state *bs; - struct bstp_port *bp; - - /* - * It would be nice if the ifnet had a pointer to the bstp_port so we - * didnt need to search for it, but that may be an overkill. In reality - * this is fast and doesnt get called often. - */ - mtx_lock(&bstp_list_mtx); - LIST_FOREACH(bs, &bstp_list, bs_list) { - BSTP_LOCK(bs); - LIST_FOREACH(bp, &bs->bs_bplist, bp_next) { - if (bp->bp_ifp == ifp) { - bstp_ifupdstatus(bs, bp); - /* it only exists once so return */ - BSTP_UNLOCK(bs); - mtx_unlock(&bstp_list_mtx); - return; - } - } - BSTP_UNLOCK(bs); - } - mtx_unlock(&bstp_list_mtx); -} - -static void -bstp_ifupdstatus(struct bstp_state *bs, struct bstp_port *bp) -{ - struct ifnet *ifp = bp->bp_ifp; - struct ifmediareq ifmr; - int error = 0; - - BSTP_LOCK_ASSERT(bs); - - bzero((char *)&ifmr, sizeof(ifmr)); - error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr); - - if ((error == 0) && (ifp->if_flags & IFF_UP)) { - if (ifmr.ifm_status & IFM_ACTIVE) { - if (bp->bp_state == BSTP_IFSTATE_DISABLED) - bstp_enable_port(bs, bp); - - } else { - if (bp->bp_state != BSTP_IFSTATE_DISABLED) - bstp_disable_port(bs, bp); - } - return; - } - - if (bp->bp_state != BSTP_IFSTATE_DISABLED) - bstp_disable_port(bs, bp); -} - -static void -bstp_tick(void *arg) +bstp_stop_locked(struct bstp_state *bs) { - struct bstp_state *bs = arg; struct bstp_port *bp; BSTP_LOCK_ASSERT(bs); - /* slow timer to catch missed link events */ - if (bstp_timer_expired(&bs->bs_link_timer, BSTP_LINK_TIMER)) { - LIST_FOREACH(bp, &bs->bs_bplist, bp_next) { - bstp_ifupdstatus(bs, bp); - } - bstp_timer_start(&bs->bs_link_timer, 0); - } - - if (bstp_timer_expired(&bs->bs_hello_timer, bs->bs_hello_time)) - bstp_hello_timer_expiry(bs); - - if (bstp_timer_expired(&bs->bs_tcn_timer, bs->bs_bridge_hello_time)) - bstp_tcn_timer_expiry(bs); - - if (bstp_timer_expired(&bs->bs_topology_change_timer, - bs->bs_topology_change_time)) - bstp_topology_change_timer_expiry(bs); - - LIST_FOREACH(bp, &bs->bs_bplist, bp_next) { - if (bstp_timer_expired(&bp->bp_message_age_timer, - bs->bs_max_age)) - bstp_message_age_timer_expiry(bs, bp); - } - - LIST_FOREACH(bp, &bs->bs_bplist, bp_next) { - if (bstp_timer_expired(&bp->bp_forward_delay_timer, - bs->bs_forward_delay)) - bstp_forward_delay_timer_expiry(bs, bp); - - if (bstp_timer_expired(&bp->bp_hold_timer, - bs->bs_hold_time)) - bstp_hold_timer_expiry(bs, bp); - } - - callout_reset(&bs->bs_bstpcallout, hz, bstp_tick, bs); -} - -static void -bstp_timer_start(struct bstp_timer *t, uint16_t v) -{ - t->value = v; - t->active = 1; -} - -static void -bstp_timer_stop(struct bstp_timer *t) -{ - t->value = 0; - t->active = 0; -} - -static int -bstp_timer_expired(struct bstp_timer *t, uint16_t v) -{ - if (t->active == 0) - return (0); - t->value += BSTP_TICK_VAL; - if (t->value >= v) { - bstp_timer_stop(t); - return (1); - } - return (0); + LIST_FOREACH(bp, &bs->bs_bplist, bp_next) + bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING); + callout_drain(&bs->bs_bstpcallout); + callout_stop(&bs->bs_bstpcallout); } int @@ -1262,18 +2081,31 @@ bstp_add(struct bstp_state *bs, struct b return (EINVAL); } + bzero(bp, sizeof(struct bstp_port)); + BSTP_LOCK(bs); bp->bp_ifp = ifp; bp->bp_bs = bs; - bp->bp_active = 1; bp->bp_priority = BSTP_DEFAULT_PORT_PRIORITY; - bp->bp_path_cost = BSTP_DEFAULT_PATH_COST; + bp->bp_txcount = 0; + TASK_INIT(&bp->bp_statetask, 0, bstp_notify_state, bp); + TASK_INIT(&bp->bp_rtagetask, 0, bstp_notify_rtage, bp); + + /* Init state */ + bp->bp_infois = BSTP_INFO_DISABLED; + bstp_set_port_state(bp, BSTP_IFSTATE_DISCARDING); + bstp_set_port_proto(bp, bs->bs_protover); + bstp_set_port_role(bp, BSTP_ROLE_DISABLED); + bstp_set_port_tc(bp, BSTP_TCSTATE_INACTIVE); + bp->bp_path_cost = bstp_calc_path_cost(bp); LIST_INSERT_HEAD(&bs->bs_bplist, bp, bp_next); - TASK_INIT(&bp->bp_statetask, 0, bstp_state_change, bp); - BSTP_UNLOCK(bs); - bstp_reinit(bs); + bp->bp_active = 1; + bp->bp_flags |= BSTP_PORT_NEWINFO; + bstp_reinit(bs); + bstp_update_roles(bs, bp); + BSTP_UNLOCK(bs); return (0); } @@ -1285,14 +2117,11 @@ bstp_delete(struct bstp_port *bp) KASSERT(bp->bp_active == 1, ("not a bstp member")); BSTP_LOCK(bs); - if (bp->bp_state != BSTP_IFSTATE_DISABLED) - bstp_disable_port(bs, bp); LIST_REMOVE(bp, bp_next); - BSTP_UNLOCK(bs); bp->bp_bs = NULL; bp->bp_active = 0; - bstp_reinit(bs); + BSTP_UNLOCK(bs); } /* @@ -1303,4 +2132,5 @@ bstp_drain(struct bstp_port *bp) { KASSERT(bp->bp_active == 0, ("port is still attached")); taskqueue_drain(taskqueue_swi, &bp->bp_statetask); + taskqueue_drain(taskqueue_swi, &bp->bp_rtagetask); } Index: sys/net/bridgestp.h =================================================================== RCS file: /home/ncvs/src/sys/net/bridgestp.h,v retrieving revision 1.4 diff -u -p -r1.4 bridgestp.h --- sys/net/bridgestp.h 2 Aug 2006 02:47:27 -0000 1.4 +++ sys/net/bridgestp.h 12 Oct 2006 10:51:44 -0000 @@ -83,9 +83,55 @@ #define BSTP_IFSTATE_LEARNING 2 #define BSTP_IFSTATE_FORWARDING 3 #define BSTP_IFSTATE_BLOCKING 4 +#define BSTP_IFSTATE_DISCARDING 5 + +#define BSTP_TCSTATE_ACTIVE 1 +#define BSTP_TCSTATE_DETECTED 2 +#define BSTP_TCSTATE_INACTIVE 3 +#define BSTP_TCSTATE_LEARNING 4 +#define BSTP_TCSTATE_PROPAG 5 +#define BSTP_TCSTATE_ACK 6 +#define BSTP_TCSTATE_TC 7 +#define BSTP_TCSTATE_TCN 8 + +#define BSTP_ROLE_DISABLED 0 +#define BSTP_ROLE_ROOT 1 +#define BSTP_ROLE_DESIGNATED 2 +#define BSTP_ROLE_ALTERNATE 3 +#define BSTP_ROLE_BACKUP 4 #ifdef _KERNEL +/* STP port flags */ +#define BSTP_PORT_CANMIGRATE 0x0001 +#define BSTP_PORT_NEWINFO 0x0002 +#define BSTP_PORT_DISPUTED 0x0004 +#define BSTP_PORT_ADMCOST 0x0008 + +/* BPDU priority */ +#define BSTP_PDU_SUPERIOR 1 +#define BSTP_PDU_REPEATED 2 +#define BSTP_PDU_INFERIOR 3 +#define BSTP_PDU_INFERIORALT 4 +#define BSTP_PDU_OTHER 5 + +/* BPDU flags */ +#define BSTP_PDU_PRMASK 0x0c /* Port Role */ +#define BSTP_PDU_PRSHIFT 2 /* Port Role offset */ +#define BSTP_PDU_F_UNKN 0x00 /* Unknown port (00) */ +#define BSTP_PDU_F_ALT 0x01 /* Alt/Backup port (01) */ +#define BSTP_PDU_F_ROOT 0x02 /* Root port (10) */ +#define BSTP_PDU_F_DESG 0x03 /* Designated port (11) */ + +#define BSTP_PDU_STPMASK 0x81 /* strip unused STP flags */ +#define BSTP_PDU_RSTPMASK 0x7f /* strip unused RSTP flags */ +#define BSTP_PDU_F_TC 0x01 /* Topology change */ +#define BSTP_PDU_F_P 0x02 /* Proposal flag */ +#define BSTP_PDU_F_L 0x10 /* Learning flag */ +#define BSTP_PDU_F_F 0x20 /* Forwarding flag */ +#define BSTP_PDU_F_A 0x40 /* Agreement flag */ +#define BSTP_PDU_F_TCA 0x80 /* Topology change ack */ + /* * Spanning tree defaults. */ @@ -93,26 +139,48 @@ #define BSTP_DEFAULT_HELLO_TIME (2 * 256) #define BSTP_DEFAULT_FORWARD_DELAY (15 * 256) #define BSTP_DEFAULT_HOLD_TIME (1 * 256) +#define BSTP_DEFAULT_MIGRATE_DELAY (3 * 256) +#define BSTP_DEFAULT_HOLD_COUNT 6 #define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000 #define BSTP_DEFAULT_PORT_PRIORITY 0x80 #define BSTP_DEFAULT_PATH_COST 55 +#define BSTP_MIN_HELLO_TIME (1 * 256) +#define BSTP_MIN_MAX_AGE (6 * 256) +#define BSTP_MIN_FORWARD_DELAY (4 * 256) +#define BSTP_MIN_HOLD_COUNT 1 +#define BSTP_MAX_HELLO_TIME (2 * 256) +#define BSTP_MAX_MAX_AGE (40 * 256) +#define BSTP_MAX_FORWARD_DELAY (30 * 256) +#define BSTP_MAX_HOLD_COUNT 10 +#define BSTP_MAX_PRIORITY 61440 +#define BSTP_MAX_PORT_PRIORITY 240 +#define BSTP_MAX_PATH_COST 200000000 /* BPDU message types */ #define BSTP_MSGTYPE_CFG 0x00 /* Configuration */ +#define BSTP_MSGTYPE_RSTP 0x02 /* Rapid STP */ #define BSTP_MSGTYPE_TCN 0x80 /* Topology chg notification */ -/* BPDU flags */ -#define BSTP_FLAG_TC 0x01 /* Topology change */ -#define BSTP_FLAG_TCA 0x80 /* Topology change ack */ +/* Protocol versions */ +#define BSTP_PROTO_ID 0x00 +#define BSTP_PROTO_STP 0x00 +#define BSTP_PROTO_RSTP 0x02 + +#define BSTP_INFO_RECIEVED 1 +#define BSTP_INFO_MINE 2 +#define BSTP_INFO_AGED 3 +#define BSTP_INFO_DISABLED 4 + #define BSTP_MESSAGE_AGE_INCR (1 * 256) /* in 256ths of a second */ #define BSTP_TICK_VAL (1 * 256) /* in 256ths of a second */ -#define BSTP_LINK_TIMER (BSTP_TICK_VAL * 30) +#define BSTP_LINK_TIMER (BSTP_TICK_VAL * 15) /* * * Driver callbacks for STP state changes * */ typedef void (*bstp_state_cb_t)(struct ifnet *, int); +typedef void (*bstp_rtage_cb_t)(struct ifnet *, int); /* * Because BPDU's do not make nicely aligned structures, two different @@ -145,7 +213,10 @@ struct bstp_cbpdu { uint16_t cbu_maxage; /* maximum age */ uint16_t cbu_hellotime; /* hello time */ uint16_t cbu_forwarddelay; /* forwarding delay */ + uint8_t cbu_versionlen; /* version 1 length */ } __attribute__((__packed__)); +#define BSTP_BPDU_STP_LEN (3 + 35) /* LLC + STP pdu */ +#define BSTP_BPDU_RSTP_LEN (3 + 36) /* LLC + RSTP pdu */ /* topology change notification bridge protocol data unit */ struct bstp_tbpdu { @@ -161,53 +232,91 @@ struct bstp_tbpdu { * Timekeeping structure used in spanning tree code. */ struct bstp_timer { - uint16_t active; - uint16_t value; + int active; + int latched; + int value; +}; + +struct bstp_pri_vector { + uint64_t pv_root_id; + uint32_t pv_cost; + uint64_t pv_dbridge_id; + uint16_t pv_dport_id; + uint16_t pv_port_id; }; struct bstp_config_unit { - uint64_t cu_rootid; - uint64_t cu_bridge_id; - uint32_t cu_root_path_cost; + struct bstp_pri_vector cu_pv; uint16_t cu_message_age; uint16_t cu_max_age; - uint16_t cu_hello_time; uint16_t cu_forward_delay; - uint16_t cu_port_id; + uint16_t cu_hello_time; uint8_t cu_message_type; - uint8_t cu_topology_change_acknowledgment; + uint8_t cu_topology_change_ack; uint8_t cu_topology_change; + uint8_t cu_proposal; + uint8_t cu_agree; + uint8_t cu_learning; + uint8_t cu_forwarding; + uint8_t cu_role; }; struct bstp_tcn_unit { uint8_t tu_message_type; }; -/* - * Bridge interface list entry. - */ struct bstp_port { LIST_ENTRY(bstp_port) bp_next; struct ifnet *bp_ifp; /* parent if */ struct bstp_state *bp_bs; - int bp_active; - uint64_t bp_designated_root; - uint64_t bp_designated_bridge; + uint8_t bp_active; + uint8_t bp_protover; + uint32_t bp_flags; uint32_t bp_path_cost; - uint32_t bp_designated_cost; - struct bstp_timer bp_hold_timer; - struct bstp_timer bp_message_age_timer; + uint16_t bp_port_msg_age; + uint16_t bp_port_max_age; + uint16_t bp_port_fdelay; + uint16_t bp_port_htime; + uint16_t bp_desg_msg_age; + uint16_t bp_desg_max_age; + uint16_t bp_desg_fdelay; + uint16_t bp_desg_htime; + struct bstp_timer bp_edge_delay_timer; struct bstp_timer bp_forward_delay_timer; - struct bstp_config_unit bp_config_bpdu; + struct bstp_timer bp_hello_timer; + struct bstp_timer bp_message_age_timer; + struct bstp_timer bp_migrate_delay_timer; + struct bstp_timer bp_recent_backup_timer; + struct bstp_timer bp_recent_root_timer; + struct bstp_timer bp_tc_timer; + struct bstp_config_unit bp_msg_cu; + struct bstp_pri_vector bp_desg_pv; + struct bstp_pri_vector bp_port_pv; uint16_t bp_port_id; - uint16_t bp_designated_port; uint8_t bp_state; - uint8_t bp_topology_change_acknowledge; - uint8_t bp_config_pending; - uint8_t bp_change_detection_enabled; + uint8_t bp_tcstate; + uint8_t bp_role; + uint8_t bp_infois; + uint8_t bp_tc_ack; + uint8_t bp_tc_prop; + uint8_t bp_fdbflush; uint8_t bp_priority; + uint8_t bp_p2p_link; + uint8_t bp_agree; + uint8_t bp_agreed; + uint8_t bp_sync; + uint8_t bp_synced; + uint8_t bp_proposing; + uint8_t bp_proposed; + uint8_t bp_operedge; + uint8_t bp_reroot; + uint8_t bp_rcvdtc; + uint8_t bp_rcvdtca; + uint8_t bp_rcvdtcn; uint32_t bp_forward_transitions; + uint8_t bp_txcount; struct task bp_statetask; + struct task bp_rtagetask; }; /* @@ -216,51 +325,58 @@ struct bstp_port { struct bstp_state { LIST_ENTRY(bstp_state) bs_list; struct mtx bs_mtx; - uint64_t bs_designated_root; - uint64_t bs_bridge_id; + struct bstp_pri_vector bs_bridge_pv; + struct bstp_pri_vector bs_root_pv; struct bstp_port *bs_root_port; - uint32_t bs_root_path_cost; - uint16_t bs_max_age; - uint16_t bs_hello_time; - uint16_t bs_forward_delay; + uint8_t bs_protover; + uint16_t bs_migration_delay; + uint16_t bs_edge_delay; uint16_t bs_bridge_max_age; - uint16_t bs_bridge_hello_time; - uint16_t bs_bridge_forward_delay; - uint16_t bs_topology_change_time; + uint16_t bs_bridge_fdelay; + uint16_t bs_bridge_htime; + uint16_t bs_root_msg_age; + uint16_t bs_root_max_age; + uint16_t bs_root_fdelay; + uint16_t bs_root_htime; uint16_t bs_hold_time; uint16_t bs_bridge_priority; - uint8_t bs_topology_change_detected; - uint8_t bs_topology_change; - struct bstp_timer bs_hello_timer; - struct bstp_timer bs_topology_change_timer; - struct bstp_timer bs_tcn_timer; + uint8_t bs_txholdcount; + uint8_t bs_allsynced; struct callout bs_bstpcallout; /* STP callout */ struct bstp_timer bs_link_timer; struct timeval bs_last_tc_time; LIST_HEAD(, bstp_port) bs_bplist; bstp_state_cb_t bs_state_cb; + bstp_rtage_cb_t bs_rtage_cb; }; -#define BSTP_LOCK_INIT(_bs) mtx_init(&(_bs)->bs_mtx, "bstp", \ +#define BSTP_LOCK_INIT(_bs) mtx_init(&(_bs)->bs_mtx, "bstp", \ NULL, MTX_DEF) -#define BSTP_LOCK_DESTROY(_bs) mtx_destroy(&(_bs)->bs_mtx) -#define BSTP_LOCK(_bs) mtx_lock(&(_bs)->bs_mtx) -#define BSTP_UNLOCK(_bs) mtx_unlock(&(_bs)->bs_mtx) -#define BSTP_LOCK_ASSERT(_bs) mtx_assert(&(_bs)->bs_mtx, MA_OWNED) +#define BSTP_LOCK_DESTROY(_bs) mtx_destroy(&(_bs)->bs_mtx) +#define BSTP_LOCK(_bs) mtx_lock(&(_bs)->bs_mtx) +#define BSTP_UNLOCK(_bs) mtx_unlock(&(_bs)->bs_mtx) +#define BSTP_LOCK_ASSERT(_bs) mtx_assert(&(_bs)->bs_mtx, MA_OWNED) extern const uint8_t bstp_etheraddr[]; extern void (*bstp_linkstate_p)(struct ifnet *ifp, int state); -void bstp_attach(struct bstp_state *, bstp_state_cb_t); +void bstp_attach(struct bstp_state *, bstp_state_cb_t, bstp_rtage_cb_t); void bstp_detach(struct bstp_state *); void bstp_init(struct bstp_state *); -void bstp_reinit(struct bstp_state *); void bstp_stop(struct bstp_state *); int bstp_add(struct bstp_state *, struct bstp_port *, struct ifnet *); void bstp_delete(struct bstp_port *); void bstp_drain(struct bstp_port *); void bstp_linkstate(struct ifnet *, int); +int bstp_set_htime(struct bstp_state *, int); +int bstp_set_fdelay(struct bstp_state *, int); +int bstp_set_maxage(struct bstp_state *, int); +int bstp_set_holdcount(struct bstp_state *, int); +int bstp_set_protocol(struct bstp_state *, int); +int bstp_set_priority(struct bstp_state *, int); +int bstp_set_port_priority(struct bstp_port *, int); +int bstp_set_path_cost(struct bstp_port *, uint32_t); struct mbuf *bstp_input(struct bstp_port *, struct ifnet *, struct mbuf *); #endif /* _KERNEL */ Index: sys/net/if_bridge.c =================================================================== RCS file: /home/ncvs/src/sys/net/if_bridge.c,v retrieving revision 1.82 diff -u -p -r1.82 if_bridge.c --- sys/net/if_bridge.c 9 Oct 2006 00:49:57 -0000 1.82 +++ sys/net/if_bridge.c 12 Oct 2006 10:52:27 -0000 @@ -266,6 +266,7 @@ static int bridge_rtnode_insert(struct b struct bridge_rtnode *); static void bridge_rtnode_destroy(struct bridge_softc *, struct bridge_rtnode *); +static void bridge_rtable_expire(struct ifnet *, int); static void bridge_state_change(struct ifnet *, int); static struct bridge_iflist *bridge_lookup_member(struct bridge_softc *, @@ -305,6 +306,8 @@ static int bridge_ioctl_delspan(struct b static int bridge_ioctl_gbparam(struct bridge_softc *, void *); static int bridge_ioctl_grte(struct bridge_softc *, void *); static int bridge_ioctl_gifsstp(struct bridge_softc *, void *); +static int bridge_ioctl_sproto(struct bridge_softc *, void *); +static int bridge_ioctl_stxhc(struct bridge_softc *, void *); static int bridge_pfil(struct mbuf **, struct ifnet *, struct ifnet *, int); static int bridge_ip_checkbasic(struct mbuf **mp); @@ -418,6 +421,12 @@ const struct bridge_control bridge_contr { bridge_ioctl_gifsstp, sizeof(struct ifbpstpconf), BC_F_COPYOUT }, + + { bridge_ioctl_sproto, sizeof(struct ifbrparam), + BC_F_COPYIN|BC_F_SUSER }, + + { bridge_ioctl_stxhc, sizeof(struct ifbrparam), + BC_F_COPYIN|BC_F_SUSER }, }; const int bridge_control_table_size = sizeof(bridge_control_table) / sizeof(bridge_control_table[0]); @@ -531,7 +540,6 @@ bridge_clone_create(struct if_clone *ifc sc->sc_brtmax = BRIDGE_RTABLE_MAX; sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT; - getmicrotime(&(sc->sc_stp.bs_last_tc_time)); /* Initialize our routing table. */ bridge_rtable_init(sc); @@ -574,7 +582,7 @@ bridge_clone_create(struct if_clone *ifc mtx_unlock(&bridge_list_mtx); } - bstp_attach(&sc->sc_stp, bridge_state_change); + bstp_attach(&sc->sc_stp, bridge_state_change, bridge_rtable_expire); ether_ifattach(ifp, eaddr); /* Now undo some of the damage... */ ifp->if_baudrate = 0; @@ -997,7 +1005,10 @@ bridge_ioctl_gifflags(struct bridge_soft req->ifbr_state = bif->bif_stp.bp_state; req->ifbr_priority = bif->bif_stp.bp_priority; req->ifbr_path_cost = bif->bif_stp.bp_path_cost; - req->ifbr_portno = bif->bif_ifp->if_index & 0xff; + req->ifbr_portno = bif->bif_ifp->if_index & 0xfff; + req->ifbr_proto = bif->bif_stp.bp_protover; + req->ifbr_role = bif->bif_stp.bp_role; + req->ifbr_stpflags = bif->bif_stp.bp_flags; return (0); } @@ -1087,7 +1098,10 @@ bridge_ioctl_gifs(struct bridge_softc *s breq.ifbr_state = bif->bif_stp.bp_state; breq.ifbr_priority = bif->bif_stp.bp_priority; breq.ifbr_path_cost = bif->bif_stp.bp_path_cost; - breq.ifbr_portno = bif->bif_ifp->if_index & 0xff; + breq.ifbr_portno = bif->bif_ifp->if_index & 0xfff; + breq.ifbr_proto = bif->bif_stp.bp_protover; + breq.ifbr_role = bif->bif_stp.bp_role; + breq.ifbr_stpflags = bif->bif_stp.bp_flags; error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq)); if (error) break; @@ -1101,10 +1115,7 @@ bridge_ioctl_gifs(struct bridge_softc *s strlcpy(breq.ifbr_ifsname, bif->bif_ifp->if_xname, sizeof(breq.ifbr_ifsname)); breq.ifbr_ifsflags = bif->bif_flags; - breq.ifbr_state = bif->bif_stp.bp_state; - breq.ifbr_priority = bif->bif_stp.bp_priority; - breq.ifbr_path_cost = bif->bif_stp.bp_path_cost; - breq.ifbr_portno = bif->bif_ifp->if_index & 0xff; + breq.ifbr_portno = bif->bif_ifp->if_index & 0xfff; error = copyout(&breq, bifc->ifbic_req + count, sizeof(breq)); if (error) break; @@ -1219,12 +1230,10 @@ static int bridge_ioctl_spri(struct bridge_softc *sc, void *arg) { struct ifbrparam *param = arg; - struct bstp_state *bs = &sc->sc_stp; - - bs->bs_bridge_priority = param->ifbrp_prio; - bstp_reinit(bs); + int error; - return (0); + error = bstp_set_priority(&sc->sc_stp, param->ifbrp_prio); + return (error); } static int @@ -1233,7 +1242,7 @@ bridge_ioctl_ght(struct bridge_softc *sc struct ifbrparam *param = arg; struct bstp_state *bs = &sc->sc_stp; - param->ifbrp_hellotime = bs->bs_bridge_hello_time >> 8; + param->ifbrp_hellotime = bs->bs_bridge_htime >> 8; return (0); } @@ -1241,14 +1250,10 @@ static int bridge_ioctl_sht(struct bridge_softc *sc, void *arg) { struct ifbrparam *param = arg; - struct bstp_state *bs = &sc->sc_stp; - - if (param->ifbrp_hellotime == 0) - return (EINVAL); - bs->bs_bridge_hello_time = param->ifbrp_hellotime << 8; - bstp_reinit(bs); + int error; - return (0); + error = bstp_set_htime(&sc->sc_stp, param->ifbrp_hellotime); + return (error); } static int @@ -1257,7 +1262,7 @@ bridge_ioctl_gfd(struct bridge_softc *sc struct ifbrparam *param = arg; struct bstp_state *bs = &sc->sc_stp; - param->ifbrp_fwddelay = bs->bs_bridge_forward_delay >> 8; + param->ifbrp_fwddelay = bs->bs_bridge_fdelay >> 8; return (0); } @@ -1265,14 +1270,10 @@ static int bridge_ioctl_sfd(struct bridge_softc *sc, void *arg) { struct ifbrparam *param = arg; - struct bstp_state *bs = &sc->sc_stp; - - if (param->ifbrp_fwddelay == 0) - return (EINVAL); - bs->bs_bridge_forward_delay = param->ifbrp_fwddelay << 8; - bstp_reinit(bs); + int error; - return (0); + error = bstp_set_fdelay(&sc->sc_stp, param->ifbrp_fwddelay); + return (error); } static int @@ -1289,14 +1290,10 @@ static int bridge_ioctl_sma(struct bridge_softc *sc, void *arg) { struct ifbrparam *param = arg; - struct bstp_state *bs = &sc->sc_stp; - - if (param->ifbrp_maxage == 0) - return (EINVAL); - bs->bs_bridge_max_age = param->ifbrp_maxage << 8; - bstp_reinit(bs); + int error; - return (0); + error = bstp_set_maxage(&sc->sc_stp, param->ifbrp_maxage); + return (error); } static int @@ -1304,15 +1301,14 @@ bridge_ioctl_sifprio(struct bridge_softc { struct ifbreq *req = arg; struct bridge_iflist *bif; + int error; bif = bridge_lookup_member(sc, req->ifbr_ifsname); if (bif == NULL) return (ENOENT); - bif->bif_stp.bp_priority = req->ifbr_priority; - bstp_reinit(&sc->sc_stp); - - return (0); + error = bstp_set_port_priority(&bif->bif_stp, req->ifbr_priority); + return (error); } static int @@ -1320,15 +1316,14 @@ bridge_ioctl_sifcost(struct bridge_softc { struct ifbreq *req = arg; struct bridge_iflist *bif; + int error; bif = bridge_lookup_member(sc, req->ifbr_ifsname); if (bif == NULL) return (ENOENT); - bif->bif_stp.bp_path_cost = req->ifbr_path_cost; - bstp_reinit(&sc->sc_stp); - - return (0); + error = bstp_set_path_cost(&bif->bif_stp, req->ifbr_path_cost); + return (error); } static int @@ -1397,22 +1392,26 @@ static int bridge_ioctl_gbparam(struct bridge_softc *sc, void *arg) { struct ifbropreq *req = arg; + struct bstp_state *bs = &sc->sc_stp; struct bstp_port *root_port; - req->ifbop_maxage = sc->sc_stp.bs_max_age; - req->ifbop_hellotime = sc->sc_stp.bs_hello_time; - req->ifbop_fwddelay = sc->sc_stp.bs_forward_delay; + req->ifbop_maxage = bs->bs_bridge_max_age >> 8; + req->ifbop_hellotime = bs->bs_bridge_htime >> 8; + req->ifbop_fwddelay = bs->bs_bridge_fdelay >> 8; - root_port = sc->sc_stp.bs_root_port; + root_port = bs->bs_root_port; if (root_port == NULL) req->ifbop_root_port = 0; else req->ifbop_root_port = root_port->bp_ifp->if_index; - req->ifbop_root_path_cost = sc->sc_stp.bs_root_path_cost; - req->ifbop_designated_root = sc->sc_stp.bs_designated_root; - req->ifbop_last_tc_time.tv_sec = sc->sc_stp.bs_last_tc_time.tv_sec; - req->ifbop_last_tc_time.tv_usec = sc->sc_stp.bs_last_tc_time.tv_usec; + req->ifbop_holdcount = bs->bs_txholdcount; + req->ifbop_priority = bs->bs_bridge_priority; + req->ifbop_protocol = bs->bs_protover; + req->ifbop_root_path_cost = bs->bs_root_pv.pv_cost; + req->ifbop_designated_root = bs->bs_root_pv.pv_root_id; + req->ifbop_last_tc_time.tv_sec = bs->bs_last_tc_time.tv_sec; + req->ifbop_last_tc_time.tv_usec = bs->bs_last_tc_time.tv_usec; return (0); } @@ -1455,12 +1454,12 @@ bridge_ioctl_gifsstp(struct bridge_softc if ((bif->bif_flags & IFBIF_STP) == 0) continue; - bpreq.ifbp_portno = bif->bif_ifp->if_index & 0xff; + bpreq.ifbp_portno = bif->bif_ifp->if_index & 0xfff; bpreq.ifbp_fwd_trans = bif->bif_stp.bp_forward_transitions; - bpreq.ifbp_design_cost = bif->bif_stp.bp_designated_cost; - bpreq.ifbp_design_port = bif->bif_stp.bp_designated_port; - bpreq.ifbp_design_bridge = bif->bif_stp.bp_designated_bridge; - bpreq.ifbp_design_root = bif->bif_stp.bp_designated_root; + bpreq.ifbp_design_cost = bif->bif_stp.bp_desg_pv.pv_cost; + bpreq.ifbp_design_port = bif->bif_stp.bp_desg_pv.pv_port_id; + bpreq.ifbp_design_bridge = bif->bif_stp.bp_desg_pv.pv_dbridge_id; + bpreq.ifbp_design_root = bif->bif_stp.bp_desg_pv.pv_root_id; error = copyout(&bpreq, bifstp->ifbpstp_req + count, sizeof(bpreq)); @@ -1475,6 +1474,26 @@ bridge_ioctl_gifsstp(struct bridge_softc return (error); } +static int +bridge_ioctl_sproto(struct bridge_softc *sc, void *arg) +{ + struct ifbrparam *param = arg; + int error; + + error = bstp_set_protocol(&sc->sc_stp, param->ifbrp_proto); + return (error); +} + +static int +bridge_ioctl_stxhc(struct bridge_softc *sc, void *arg) +{ + struct ifbrparam *param = arg; + int error; + + error = bstp_set_holdcount(&sc->sc_stp, param->ifbrp_txhc); + return (error); +} + /* * bridge_ifdetach: * @@ -1716,15 +1735,9 @@ bridge_output(struct ifnet *ifp, struct * tree, make sure the port is in a state that * allows forwarding. */ - if (dst_if != ifp && - (bif->bif_flags & IFBIF_STP) != 0) { - switch (bif->bif_stp.bp_state) { - case BSTP_IFSTATE_BLOCKING: - case BSTP_IFSTATE_LISTENING: - case BSTP_IFSTATE_DISABLED: - continue; - } - } + if (dst_if != ifp && (bif->bif_flags & IFBIF_STP) && + bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) + continue; if (LIST_NEXT(bif, bif_next) == NULL) { used = 1; @@ -1834,15 +1847,11 @@ bridge_forward(struct bridge_softc *sc, return; } - if (bif->bif_flags & IFBIF_STP) { - switch (bif->bif_stp.bp_state) { - case BSTP_IFSTATE_BLOCKING: - case BSTP_IFSTATE_LISTENING: - case BSTP_IFSTATE_DISABLED: - BRIDGE_UNLOCK(sc); - m_freem(m); - return; - } + if ((bif->bif_flags & IFBIF_STP) && + bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) { + BRIDGE_UNLOCK(sc); + m_freem(m); + return; } eh = mtod(m, struct ether_header *); @@ -1941,14 +1950,11 @@ bridge_forward(struct bridge_softc *sc, return; } - if (bif->bif_flags & IFBIF_STP) { - switch (bif->bif_stp.bp_state) { - case BSTP_IFSTATE_DISABLED: - case BSTP_IFSTATE_BLOCKING: - BRIDGE_UNLOCK(sc); - m_freem(m); - return; - } + if ((bif->bif_flags & IFBIF_STP) && + bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) { + BRIDGE_UNLOCK(sc); + m_freem(m); + return; } BRIDGE_UNLOCK(sc); @@ -2040,14 +2046,10 @@ bridge_input(struct ifnet *ifp, struct m } } - if (bif->bif_flags & IFBIF_STP) { - switch (bif->bif_stp.bp_state) { - case BSTP_IFSTATE_BLOCKING: - case BSTP_IFSTATE_LISTENING: - case BSTP_IFSTATE_DISABLED: - BRIDGE_UNLOCK(sc); - return (m); - } + if ((bif->bif_flags & IFBIF_STP) && + bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) { + BRIDGE_UNLOCK(sc); + return (m); } if (bcmp(etherbroadcastaddr, eh->ether_dhost, @@ -2093,14 +2095,10 @@ bridge_input(struct ifnet *ifp, struct m return (m); } - if (bif->bif_flags & IFBIF_STP) { - switch (bif->bif_stp.bp_state) { - case BSTP_IFSTATE_BLOCKING: - case BSTP_IFSTATE_LISTENING: - case BSTP_IFSTATE_DISABLED: - BRIDGE_UNLOCK(sc); - return (m); - } + if ((bif->bif_flags & IFBIF_STP) && + bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) { + BRIDGE_UNLOCK(sc); + return (m); } /* @@ -2186,13 +2184,9 @@ bridge_broadcast(struct bridge_softc *sc if (dst_if == src_if) continue; - if (bif->bif_flags & IFBIF_STP) { - switch (bif->bif_stp.bp_state) { - case BSTP_IFSTATE_BLOCKING: - case BSTP_IFSTATE_DISABLED: - continue; - } - } + if ((bif->bif_flags & IFBIF_STP) && + bif->bif_stp.bp_state == BSTP_IFSTATE_DISCARDING) + continue; if ((bif->bif_flags & IFBIF_DISCOVER) == 0 && (m->m_flags & (M_BCAST|M_MCAST)) == 0) @@ -2653,6 +2647,37 @@ bridge_rtnode_destroy(struct bridge_soft } /* + * bridge_rtable_expire: + * + * Set the expiry time for all routes on an interface. + */ +static void +bridge_rtable_expire(struct ifnet *ifp, int age) +{ + struct bridge_softc *sc = ifp->if_bridge; + struct bridge_rtnode *brt; + + BRIDGE_LOCK(sc); + + /* + * If the age is zero then flush, otherwise set all the expiry times to + * age for the interface + */ + if (age == 0) + bridge_rtdelete(sc, ifp, IFBF_FLUSHDYN); + else { + LIST_FOREACH(brt, &sc->sc_rtlist, brt_list) { + /* Cap the expiry time to 'age' */ + if (brt->brt_ifp == ifp && + brt->brt_expire > time_uptime + age && + (brt->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) + brt->brt_expire = time_uptime + age; + } + } + BRIDGE_UNLOCK(sc); +} + +/* * bridge_state_change: * * Callback from the bridgestp code when a port changes states. @@ -2667,20 +2692,12 @@ bridge_state_change(struct ifnet *ifp, i "learning", "forwarding", "blocking", + "discarding" }; if (log_stp) log(LOG_NOTICE, "%s: state changed to %s on %s\n", sc->sc_ifp->if_xname, stpstates[state], ifp->if_xname); - - /* if the port is blocking then remove any routes to it */ - switch (state) { - case BSTP_IFSTATE_DISABLED: - case BSTP_IFSTATE_BLOCKING: - BRIDGE_LOCK(sc); - bridge_rtdelete(sc, ifp, IFBF_FLUSHDYN); - BRIDGE_UNLOCK(sc); - } } /* Index: sys/net/if_bridgevar.h =================================================================== RCS file: /home/ncvs/src/sys/net/if_bridgevar.h,v retrieving revision 1.15 diff -u -p -r1.15 if_bridgevar.h --- sys/net/if_bridgevar.h 31 Jul 2006 20:24:46 -0000 1.15 +++ sys/net/if_bridgevar.h 12 Oct 2006 10:51:44 -0000 @@ -112,6 +112,8 @@ #define BRDGGRTE 26 /* get cache drops (ifbrparam) */ #define BRDGGIFSSTP 27 /* get member STP params list * (ifbpstpconf) */ +#define BRDGSPROTO 28 /* set protocol (ifbrparam) */ +#define BRDGSTXHC 29 /* set tx hold count (ifbrparam) */ /* * Generic bridge control request. @@ -119,10 +121,13 @@ struct ifbreq { char ifbr_ifsname[IFNAMSIZ]; /* member if name */ uint32_t ifbr_ifsflags; /* member if flags */ - uint8_t ifbr_state; /* member if STP state */ - uint8_t ifbr_priority; /* member if STP priority */ - uint8_t ifbr_path_cost; /* member if STP cost */ + uint32_t ifbr_stpflags; /* member if STP flags */ + uint32_t ifbr_path_cost; /* member if STP cost */ uint8_t ifbr_portno; /* member if port number */ + uint8_t ifbr_priority; /* member if STP priority */ + uint8_t ifbr_proto; /* member if STP protocol */ + uint8_t ifbr_role; /* member if STP state */ + uint8_t ifbr_state; /* member if STP state */ }; /* BRDGGIFFLAGS, BRDGSIFFLAGS */ @@ -192,6 +197,8 @@ struct ifbrparam { #define ifbrp_csize ifbrp_ifbrpu.ifbrpu_int32 /* cache size */ #define ifbrp_ctime ifbrp_ifbrpu.ifbrpu_int32 /* cache time (sec) */ #define ifbrp_prio ifbrp_ifbrpu.ifbrpu_int16 /* bridge priority */ +#define ifbrp_proto ifbrp_ifbrpu.ifbrpu_int8 /* bridge protocol */ +#define ifbrp_txhc ifbrp_ifbrpu.ifbrpu_int8 /* bridge protocol */ #define ifbrp_hellotime ifbrp_ifbrpu.ifbrpu_int8 /* hello time (sec) */ #define ifbrp_fwddelay ifbrp_ifbrpu.ifbrpu_int8 /* fwd time (sec) */ #define ifbrp_maxage ifbrp_ifbrpu.ifbrpu_int8 /* max age (sec) */ @@ -201,9 +208,12 @@ struct ifbrparam { * Bridge current operational parameters structure. */ struct ifbropreq { + uint8_t ifbop_holdcount; uint8_t ifbop_maxage; uint8_t ifbop_hellotime; uint8_t ifbop_fwddelay; + uint8_t ifbop_protocol; + uint16_t ifbop_priority; uint16_t ifbop_root_port; uint32_t ifbop_root_path_cost; uint64_t ifbop_designated_root;