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