Home | History | Annotate | Line # | Download | only in agr
ieee8023ad_lacp_select.c revision 1.1
      1 /*	$NetBSD: ieee8023ad_lacp_select.c,v 1.1 2005/03/18 11:11:50 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c)2005 YAMAMOTO Takashi,
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: ieee8023ad_lacp_select.c,v 1.1 2005/03/18 11:11:50 yamt Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/mbuf.h>
     34 #include <sys/systm.h>
     35 
     36 #include <net/if.h>
     37 #include <net/if_ether.h>
     38 
     39 #include <net/agr/if_agrvar_impl.h>
     40 #include <net/agr/ieee8023_slowprotocols.h>
     41 #include <net/agr/ieee8023_tlv.h>
     42 #include <net/agr/ieee8023ad_lacp.h>
     43 #include <net/agr/ieee8023ad_lacp_impl.h>
     44 #include <net/agr/ieee8023ad_impl.h>
     45 #include <net/agr/ieee8023ad_lacp_debug.h>
     46 
     47 /* selection logic */
     48 
     49 static void lacp_fill_aggregator_id(struct lacp_aggregator *,
     50     const struct lacp_port *);
     51 static void lacp_fill_aggregator_id_peer(struct lacp_peerinfo *,
     52     const struct lacp_peerinfo *);
     53 static boolean_t lacp_aggregator_is_compatible(const struct lacp_aggregator *,
     54     const struct lacp_port *);
     55 static boolean_t lacp_peerinfo_is_compatible(const struct lacp_peerinfo *,
     56     const struct lacp_peerinfo *);
     57 
     58 static struct lacp_aggregator *lacp_aggregator_get(struct lacp_softc *,
     59     struct lacp_port *);
     60 static void lacp_aggregator_addref(struct lacp_softc *,
     61     struct lacp_aggregator *);
     62 static void lacp_aggregator_delref(struct lacp_softc *,
     63     struct lacp_aggregator *);
     64 
     65 static void
     66 lacp_aggregator_addref(struct lacp_softc *lsc, struct lacp_aggregator *la)
     67 {
     68 #if defined(LACP_DEBUG)
     69 	char buf[LACP_LAGIDSTR_MAX+1];
     70 #endif
     71 
     72 	LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
     73 	    __func__,
     74 	    lacp_format_lagid(&la->la_actor, &la->la_partner,
     75 	    buf, sizeof(buf)),
     76 	    la->la_refcnt, la->la_refcnt + 1));
     77 
     78 	KASSERT(la->la_refcnt > 0);
     79 	la->la_refcnt++;
     80 	KASSERT(la->la_refcnt > la->la_nports);
     81 }
     82 
     83 static void
     84 lacp_aggregator_delref(struct lacp_softc *lsc, struct lacp_aggregator *la)
     85 {
     86 #if defined(LACP_DEBUG)
     87 	char buf[LACP_LAGIDSTR_MAX+1];
     88 #endif
     89 
     90 	LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
     91 	    __func__,
     92 	    lacp_format_lagid(&la->la_actor, &la->la_partner,
     93 	    buf, sizeof(buf)),
     94 	    la->la_refcnt, la->la_refcnt - 1));
     95 
     96 	KASSERT(la->la_refcnt > la->la_nports);
     97 	la->la_refcnt--;
     98 	if (la->la_refcnt > 0) {
     99 		return;
    100 	}
    101 
    102 	KASSERT(la->la_refcnt == 0);
    103 	KASSERT(lsc->lsc_active_aggregator != la);
    104 
    105 	TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
    106 
    107 	free(la, M_DEVBUF);
    108 }
    109 
    110 /*
    111  * lacp_aggregator_get: allocate an aggregator.
    112  */
    113 
    114 static struct lacp_aggregator *
    115 lacp_aggregator_get(struct lacp_softc *lsc, struct lacp_port *lp)
    116 {
    117 	struct lacp_aggregator *la;
    118 
    119 	la = malloc(sizeof(*la), M_DEVBUF, M_NOWAIT);
    120 	if (la) {
    121 		la->la_refcnt = 1;
    122 		la->la_nports = 0;
    123 		TAILQ_INIT(&la->la_ports);
    124 		la->la_pending = 0;
    125 		TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
    126 	}
    127 
    128 	return la;
    129 }
    130 
    131 /*
    132  * lacp_fill_aggregator_id: setup a newly allocated aggregator from a port.
    133  */
    134 
    135 static void
    136 lacp_fill_aggregator_id(struct lacp_aggregator *la, const struct lacp_port *lp)
    137 {
    138 
    139 	lacp_fill_aggregator_id_peer(&la->la_partner, &lp->lp_partner);
    140 	lacp_fill_aggregator_id_peer(&la->la_actor, &lp->lp_actor);
    141 
    142 	la->la_actor.lip_state = lp->lp_state & LACP_STATE_AGGREGATION;
    143 }
    144 
    145 static void
    146 lacp_fill_aggregator_id_peer(struct lacp_peerinfo *lpi_aggr,
    147     const struct lacp_peerinfo *lpi_port)
    148 {
    149 
    150 	memset(lpi_aggr, 0, sizeof(*lpi_aggr));
    151 	lpi_aggr->lip_systemid = lpi_port->lip_systemid;
    152 	lpi_aggr->lip_key = lpi_port->lip_key;
    153 }
    154 
    155 /*
    156  * lacp_aggregator_is_compatible: check if a port can join to an aggregator.
    157  */
    158 
    159 static boolean_t
    160 lacp_aggregator_is_compatible(const struct lacp_aggregator *la,
    161     const struct lacp_port *lp)
    162 {
    163 
    164 	if (!(lp->lp_state & LACP_STATE_AGGREGATION) ||
    165 	    !(lp->lp_partner.lip_state & LACP_STATE_AGGREGATION)) {
    166 		return FALSE;
    167 	}
    168 
    169 	if (!(la->la_actor.lip_state & LACP_STATE_AGGREGATION)) {
    170 		return FALSE;
    171 	}
    172 
    173 	if (!lacp_peerinfo_is_compatible(&la->la_partner, &lp->lp_partner)) {
    174 		return FALSE;
    175 	}
    176 
    177 	if (!lacp_peerinfo_is_compatible(&la->la_actor, &lp->lp_actor)) {
    178 		return FALSE;
    179 	}
    180 
    181 	return TRUE;
    182 }
    183 
    184 static boolean_t
    185 lacp_peerinfo_is_compatible(const struct lacp_peerinfo *a,
    186     const struct lacp_peerinfo *b)
    187 {
    188 
    189 	if (memcmp(&a->lip_systemid, &b->lip_systemid,
    190 	    sizeof(a->lip_systemid))) {
    191 		return FALSE;
    192 	}
    193 
    194 	if (memcmp(&a->lip_key, &b->lip_key, sizeof(a->lip_key))) {
    195 		return FALSE;
    196 	}
    197 
    198 	return TRUE;
    199 }
    200 
    201 /*
    202  * lacp_select: select an aggregator.  create one if necessary.
    203  */
    204 
    205 void
    206 lacp_select(struct lacp_port *lp)
    207 {
    208 	struct lacp_softc *lsc = LACP_SOFTC(AGR_SC_FROM_PORT(lp->lp_agrport));
    209 	struct lacp_aggregator *la;
    210 #if defined(LACP_DEBUG)
    211 	char buf[LACP_LAGIDSTR_MAX+1];
    212 #endif
    213 
    214 	if (lp->lp_aggregator) {
    215 		return;
    216 	}
    217 
    218 	KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE));
    219 
    220 	LACP_DPRINTF((lp, "port lagid=%s\n",
    221 	    lacp_format_lagid(&lp->lp_actor, &lp->lp_partner,
    222 	    buf, sizeof(buf))));
    223 
    224 	TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
    225 		if (lacp_aggregator_is_compatible(la, lp)) {
    226 			break;
    227 		}
    228 	}
    229 
    230 	if (la == NULL) {
    231 		la = lacp_aggregator_get(lsc, lp);
    232 		if (la == NULL) {
    233 			LACP_DPRINTF((lp, "aggregator creation failed\n"));
    234 
    235 			/*
    236 			 * will retry on the next tick.
    237 			 */
    238 
    239 			return;
    240 		}
    241 		lacp_fill_aggregator_id(la, lp);
    242 		LACP_DPRINTF((lp, "aggregator created\n"));
    243 	} else {
    244 		LACP_DPRINTF((lp, "compatible aggregator found\n"));
    245 		lacp_aggregator_addref(lsc, la);
    246 	}
    247 
    248 	LACP_DPRINTF((lp, "aggregator lagid=%s\n",
    249 	    lacp_format_lagid(&la->la_actor, &la->la_partner,
    250 	    buf, sizeof(buf))));
    251 
    252 	lp->lp_aggregator = la;
    253 	lp->lp_selected = LACP_SELECTED;
    254 }
    255 
    256 /*
    257  * lacp_unselect: finish unselect/detach process.
    258  */
    259 
    260 void
    261 lacp_unselect(struct lacp_port *lp)
    262 {
    263 	struct lacp_softc *lsc = LACP_SOFTC(AGR_SC_FROM_PORT(lp->lp_agrport));
    264 	struct lacp_aggregator *la = lp->lp_aggregator;
    265 
    266 	KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE));
    267 
    268 	if (la == NULL) {
    269 		return;
    270 	}
    271 
    272 	lp->lp_aggregator = NULL;
    273 	lacp_aggregator_delref(lsc, la);
    274 }
    275