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