ieee8023ad_lacp.c revision 1.1 1 1.1 yamt /* $NetBSD: ieee8023ad_lacp.c,v 1.1 2005/03/18 11:11:50 yamt 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.1 yamt __KERNEL_RCSID(0, "$NetBSD: ieee8023ad_lacp.c,v 1.1 2005/03/18 11:11:50 yamt Exp $");
31 1.1 yamt
32 1.1 yamt #include <sys/param.h>
33 1.1 yamt #include <sys/mbuf.h>
34 1.1 yamt #include <sys/systm.h>
35 1.1 yamt #include <sys/malloc.h>
36 1.1 yamt #include <sys/kernel.h> /* hz */
37 1.1 yamt
38 1.1 yamt #include <net/if.h>
39 1.1 yamt #include <net/if_dl.h>
40 1.1 yamt #include <net/if_ether.h>
41 1.1 yamt #include <net/if_media.h>
42 1.1 yamt
43 1.1 yamt #include <net/agr/if_agrvar_impl.h>
44 1.1 yamt #include <net/agr/if_agrsubr.h>
45 1.1 yamt #include <net/agr/ieee8023_slowprotocols.h>
46 1.1 yamt #include <net/agr/ieee8023_tlv.h>
47 1.1 yamt #include <net/agr/ieee8023ad.h>
48 1.1 yamt #include <net/agr/ieee8023ad_lacp.h>
49 1.1 yamt #include <net/agr/ieee8023ad_lacp_impl.h>
50 1.1 yamt #include <net/agr/ieee8023ad_impl.h>
51 1.1 yamt #include <net/agr/ieee8023ad_lacp_sm.h>
52 1.1 yamt #include <net/agr/ieee8023ad_lacp_debug.h>
53 1.1 yamt
54 1.1 yamt static void lacp_fill_actorinfo(struct agr_port *, struct lacp_peerinfo *);
55 1.1 yamt
56 1.1 yamt static uint64_t lacp_aggregator_bandwidth(struct lacp_aggregator *);
57 1.1 yamt static void lacp_suppress_distributing(struct lacp_softc *,
58 1.1 yamt struct lacp_aggregator *);
59 1.1 yamt static void lacp_transit_expire(void *);
60 1.1 yamt static void lacp_select_active_aggregator(struct lacp_softc *);
61 1.1 yamt static uint16_t lacp_compose_key(struct lacp_port *);
62 1.1 yamt
63 1.1 yamt /*
64 1.1 yamt * actor system priority and port priority.
65 1.1 yamt * XXX should be configurable.
66 1.1 yamt */
67 1.1 yamt
68 1.1 yamt #define LACP_SYSTEM_PRIO 0x8000
69 1.1 yamt #define LACP_PORT_PRIO 0x8000
70 1.1 yamt
71 1.1 yamt static const struct tlv_template lacp_info_tlv_template[] = {
72 1.1 yamt { LACP_TYPE_ACTORINFO,
73 1.1 yamt sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
74 1.1 yamt { LACP_TYPE_PARTNERINFO,
75 1.1 yamt sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
76 1.1 yamt { LACP_TYPE_COLLECTORINFO,
77 1.1 yamt sizeof(struct tlvhdr) + sizeof(struct lacp_collectorinfo) },
78 1.1 yamt { 0, 0 },
79 1.1 yamt };
80 1.1 yamt
81 1.1 yamt /*
82 1.1 yamt * ieee8023ad_lacp_input: process lacpdu
83 1.1 yamt *
84 1.1 yamt * => called from ether_input. (ie. at IPL_NET)
85 1.1 yamt *
86 1.1 yamt * XXX is it better to defer processing to lower IPL?
87 1.1 yamt * XXX anyway input rate should be very low...
88 1.1 yamt */
89 1.1 yamt
90 1.1 yamt int
91 1.1 yamt ieee8023ad_lacp_input(struct ifnet *ifp, struct mbuf *m)
92 1.1 yamt {
93 1.1 yamt struct lacpdu *du;
94 1.1 yamt struct agr_softc *sc;
95 1.1 yamt struct agr_port *port;
96 1.1 yamt struct lacp_port *lp;
97 1.1 yamt int error = 0;
98 1.1 yamt int s;
99 1.1 yamt
100 1.1 yamt port = ifp->if_agrprivate; /* XXX race with agr_remport. */
101 1.1 yamt if (__predict_false(port->port_flags & AGRPORT_DETACHING)) {
102 1.1 yamt goto bad;
103 1.1 yamt }
104 1.1 yamt sc = AGR_SC_FROM_PORT(port);
105 1.1 yamt KASSERT(port);
106 1.1 yamt
107 1.1 yamt if (m->m_pkthdr.len != sizeof(*du)) {
108 1.1 yamt goto bad;
109 1.1 yamt }
110 1.1 yamt
111 1.1 yamt if ((m->m_flags & M_MCAST) == 0) {
112 1.1 yamt goto bad;
113 1.1 yamt }
114 1.1 yamt
115 1.1 yamt if (m->m_len < sizeof(*du)) {
116 1.1 yamt m = m_pullup(m, sizeof(*du));
117 1.1 yamt if (m == NULL) {
118 1.1 yamt return ENOMEM;
119 1.1 yamt }
120 1.1 yamt }
121 1.1 yamt
122 1.1 yamt du = mtod(m, struct lacpdu *);
123 1.1 yamt
124 1.1 yamt if (memcmp(&du->ldu_eh.ether_dhost,
125 1.1 yamt ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
126 1.1 yamt goto bad;
127 1.1 yamt }
128 1.1 yamt
129 1.1 yamt KASSERT(du->ldu_sph.sph_subtype == SLOWPROTOCOLS_SUBTYPE_LACP);
130 1.1 yamt
131 1.1 yamt /*
132 1.1 yamt * ignore the version for compatibility with
133 1.1 yamt * the future protocol revisions.
134 1.1 yamt */
135 1.1 yamt
136 1.1 yamt #if 0
137 1.1 yamt if (du->ldu_sph.sph_version != 1) {
138 1.1 yamt goto bad;
139 1.1 yamt }
140 1.1 yamt #endif
141 1.1 yamt
142 1.1 yamt /*
143 1.1 yamt * ignore tlv types for compatibility with
144 1.1 yamt * the future protocol revisions.
145 1.1 yamt */
146 1.1 yamt
147 1.1 yamt if (tlv_check(du, sizeof(*du), &du->ldu_tlv_actor,
148 1.1 yamt lacp_info_tlv_template, FALSE)) {
149 1.1 yamt goto bad;
150 1.1 yamt }
151 1.1 yamt
152 1.1 yamt s = AGR_LOCK(sc);
153 1.1 yamt lp = LACP_PORT(port);
154 1.1 yamt
155 1.1 yamt #if defined(LACP_DEBUG)
156 1.1 yamt if (lacpdebug) {
157 1.1 yamt LACP_DPRINTF((lp, "lacpdu receive\n"));
158 1.1 yamt lacp_dump_lacpdu(du);
159 1.1 yamt }
160 1.1 yamt #endif /* defined(LACP_DEBUG) */
161 1.1 yamt lacp_sm_rx(lp, du);
162 1.1 yamt
163 1.1 yamt AGR_UNLOCK(sc, s);
164 1.1 yamt
165 1.1 yamt m_freem(m);
166 1.1 yamt
167 1.1 yamt return error;
168 1.1 yamt
169 1.1 yamt bad:
170 1.1 yamt m_freem(m);
171 1.1 yamt return EINVAL;
172 1.1 yamt }
173 1.1 yamt
174 1.1 yamt static void
175 1.1 yamt lacp_fill_actorinfo(struct agr_port *port, struct lacp_peerinfo *info)
176 1.1 yamt {
177 1.1 yamt struct lacp_port *lp = LACP_PORT(port);
178 1.1 yamt
179 1.1 yamt info->lip_systemid.lsi_prio = htobe16(LACP_SYSTEM_PRIO);
180 1.1 yamt memcpy(&info->lip_systemid.lsi_mac,
181 1.1 yamt LLADDR(port->port_ifp->if_sadl), ETHER_ADDR_LEN);
182 1.1 yamt info->lip_portid.lpi_prio = htobe16(LACP_PORT_PRIO);
183 1.1 yamt info->lip_portid.lpi_portno = htobe16(port->port_ifp->if_index);
184 1.1 yamt info->lip_state = lp->lp_state;
185 1.1 yamt }
186 1.1 yamt
187 1.1 yamt int
188 1.1 yamt lacp_xmit_lacpdu(struct lacp_port *lp)
189 1.1 yamt {
190 1.1 yamt struct agr_port *port = lp->lp_agrport;
191 1.1 yamt struct mbuf *m;
192 1.1 yamt struct lacpdu *du;
193 1.1 yamt int error;
194 1.1 yamt
195 1.1 yamt KDASSERT(MHLEN >= sizeof(*du));
196 1.1 yamt
197 1.1 yamt m = m_gethdr(M_DONTWAIT, MT_DATA);
198 1.1 yamt if (m == NULL) {
199 1.1 yamt return ENOMEM;
200 1.1 yamt }
201 1.1 yamt m->m_len = m->m_pkthdr.len = sizeof(*du);
202 1.1 yamt
203 1.1 yamt du = mtod(m, struct lacpdu *);
204 1.1 yamt memset(du, 0, sizeof(*du));
205 1.1 yamt
206 1.1 yamt memcpy(&du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
207 1.1 yamt ETHER_ADDR_LEN);
208 1.1 yamt memcpy(&du->ldu_eh.ether_shost, &port->port_origlladdr, ETHER_ADDR_LEN);
209 1.1 yamt du->ldu_eh.ether_type = htobe16(ETHERTYPE_SLOWPROTOCOLS);
210 1.1 yamt
211 1.1 yamt du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP;
212 1.1 yamt du->ldu_sph.sph_version = 1;
213 1.1 yamt
214 1.1 yamt TLV_SET(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO, sizeof(du->ldu_actor));
215 1.1 yamt du->ldu_actor = lp->lp_actor;
216 1.1 yamt
217 1.1 yamt TLV_SET(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO,
218 1.1 yamt sizeof(du->ldu_partner));
219 1.1 yamt du->ldu_partner = lp->lp_partner;
220 1.1 yamt
221 1.1 yamt TLV_SET(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO,
222 1.1 yamt sizeof(du->ldu_collector));
223 1.1 yamt du->ldu_collector.lci_maxdelay = 0;
224 1.1 yamt
225 1.1 yamt #if defined(LACP_DEBUG)
226 1.1 yamt if (lacpdebug) {
227 1.1 yamt LACP_DPRINTF((lp, "lacpdu transmit\n"));
228 1.1 yamt lacp_dump_lacpdu(du);
229 1.1 yamt }
230 1.1 yamt #endif /* defined(LACP_DEBUG) */
231 1.1 yamt
232 1.1 yamt m->m_flags |= M_MCAST;
233 1.1 yamt
234 1.1 yamt /*
235 1.1 yamt * XXX should use higher priority queue.
236 1.1 yamt * otherwise network congestion can break aggregation.
237 1.1 yamt */
238 1.1 yamt
239 1.1 yamt error = agr_xmit_frame(port->port_ifp, m);
240 1.1 yamt return error;
241 1.1 yamt }
242 1.1 yamt
243 1.1 yamt void
244 1.1 yamt ieee8023ad_lacp_portstate(struct agr_port *port)
245 1.1 yamt {
246 1.1 yamt struct lacp_port *lp = LACP_PORT(port);
247 1.1 yamt u_int media = port->port_media;
248 1.1 yamt uint8_t old_state;
249 1.1 yamt uint16_t old_key;
250 1.1 yamt
251 1.1 yamt AGR_ASSERT_LOCKED(AGR_SC_FROM_PORT(port));
252 1.1 yamt
253 1.1 yamt LACP_DPRINTF((lp, "media changed 0x%x -> 0x%x\n", lp->lp_media, media));
254 1.1 yamt
255 1.1 yamt old_state = lp->lp_state;
256 1.1 yamt old_key = lp->lp_key;
257 1.1 yamt
258 1.1 yamt lp->lp_media = media;
259 1.1 yamt if ((media & IFM_HDX) != 0) {
260 1.1 yamt lp->lp_state &= ~LACP_STATE_AGGREGATION;
261 1.1 yamt } else {
262 1.1 yamt lp->lp_state |= LACP_STATE_AGGREGATION;
263 1.1 yamt }
264 1.1 yamt lp->lp_key = lacp_compose_key(lp);
265 1.1 yamt
266 1.1 yamt if (old_state != lp->lp_state || old_key != lp->lp_key) {
267 1.1 yamt LACP_DPRINTF((lp, "-> UNSELECTED\n"));
268 1.1 yamt lp->lp_selected = LACP_UNSELECTED;
269 1.1 yamt }
270 1.1 yamt }
271 1.1 yamt
272 1.1 yamt void
273 1.1 yamt ieee8023ad_lacp_porttick(struct agr_softc *sc, struct agr_port *port)
274 1.1 yamt {
275 1.1 yamt struct lacp_port *lp = LACP_PORT(port);
276 1.1 yamt
277 1.1 yamt AGR_ASSERT_LOCKED(sc);
278 1.1 yamt
279 1.1 yamt lacp_run_timers(lp);
280 1.1 yamt
281 1.1 yamt lacp_select(lp);
282 1.1 yamt lacp_sm_mux(lp);
283 1.1 yamt lacp_sm_tx(lp);
284 1.1 yamt lacp_sm_ptx_tx_schedule(lp);
285 1.1 yamt }
286 1.1 yamt
287 1.1 yamt void
288 1.1 yamt lacp_portinit(struct agr_port *port)
289 1.1 yamt {
290 1.1 yamt struct lacp_port *lp = LACP_PORT(port);
291 1.1 yamt boolean_t active = TRUE; /* XXX should be configurable */
292 1.1 yamt boolean_t fast = FALSE; /* XXX should be configurable */
293 1.1 yamt
294 1.1 yamt lp->lp_agrport = port;
295 1.1 yamt lacp_fill_actorinfo(port, &lp->lp_actor);
296 1.1 yamt lp->lp_state =
297 1.1 yamt (active ? LACP_STATE_ACTIVITY : 0) |
298 1.1 yamt (fast ? LACP_STATE_TIMEOUT : 0);
299 1.1 yamt lp->lp_aggregator = NULL;
300 1.1 yamt lp->lp_media = port->port_media; /* XXX */
301 1.1 yamt lp->lp_key = lacp_compose_key(lp);
302 1.1 yamt lacp_sm_rx_set_expired(lp);
303 1.1 yamt }
304 1.1 yamt
305 1.1 yamt void
306 1.1 yamt lacp_portfini(struct agr_port *port)
307 1.1 yamt {
308 1.1 yamt struct lacp_port *lp = LACP_PORT(port);
309 1.1 yamt struct lacp_aggregator *la = lp->lp_aggregator;
310 1.1 yamt int i;
311 1.1 yamt
312 1.1 yamt LACP_DPRINTF((lp, "portfini\n"));
313 1.1 yamt
314 1.1 yamt for (i = 0; i < LACP_NTIMER; i++) {
315 1.1 yamt LACP_TIMER_DISARM(lp, i);
316 1.1 yamt }
317 1.1 yamt
318 1.1 yamt if (la == NULL) {
319 1.1 yamt return;
320 1.1 yamt }
321 1.1 yamt
322 1.1 yamt lacp_disable_distributing(lp);
323 1.1 yamt lacp_unselect(lp);
324 1.1 yamt }
325 1.1 yamt
326 1.1 yamt /* -------------------- */
327 1.1 yamt void
328 1.1 yamt lacp_disable_collecting(struct lacp_port *lp)
329 1.1 yamt {
330 1.1 yamt struct agr_port *port = lp->lp_agrport;
331 1.1 yamt
332 1.1 yamt lp->lp_state &= ~LACP_STATE_COLLECTING;
333 1.1 yamt port->port_flags &= ~AGRPORT_COLLECTING;
334 1.1 yamt }
335 1.1 yamt
336 1.1 yamt void
337 1.1 yamt lacp_enable_collecting(struct lacp_port *lp)
338 1.1 yamt {
339 1.1 yamt struct agr_port *port = lp->lp_agrport;
340 1.1 yamt
341 1.1 yamt lp->lp_state |= LACP_STATE_COLLECTING;
342 1.1 yamt port->port_flags |= AGRPORT_COLLECTING;
343 1.1 yamt }
344 1.1 yamt
345 1.1 yamt void
346 1.1 yamt lacp_disable_distributing(struct lacp_port *lp)
347 1.1 yamt {
348 1.1 yamt struct agr_port *port = lp->lp_agrport;
349 1.1 yamt struct lacp_aggregator *la = lp->lp_aggregator;
350 1.1 yamt struct lacp_softc *lsc = LACP_SOFTC(AGR_SC_FROM_PORT(port));
351 1.1 yamt #if defined(LACP_DEBUG)
352 1.1 yamt char buf[LACP_LAGIDSTR_MAX+1];
353 1.1 yamt #endif /* defined(LACP_DEBUG) */
354 1.1 yamt
355 1.1 yamt if ((lp->lp_state & LACP_STATE_DISTRIBUTING) == 0) {
356 1.1 yamt return;
357 1.1 yamt }
358 1.1 yamt
359 1.1 yamt KASSERT(la);
360 1.1 yamt KASSERT(!TAILQ_EMPTY(&la->la_ports));
361 1.1 yamt KASSERT(la->la_nports > 0);
362 1.1 yamt KASSERT(la->la_refcnt >= la->la_nports);
363 1.1 yamt
364 1.1 yamt LACP_DPRINTF((lp, "disable distributing on aggregator %s, "
365 1.1 yamt "nports %d -> %d\n",
366 1.1 yamt lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
367 1.1 yamt la->la_nports, la->la_nports - 1));
368 1.1 yamt
369 1.1 yamt TAILQ_REMOVE(&la->la_ports, lp, lp_dist_q);
370 1.1 yamt la->la_nports--;
371 1.1 yamt
372 1.1 yamt lacp_suppress_distributing(lsc, la);
373 1.1 yamt
374 1.1 yamt lp->lp_state &= ~LACP_STATE_DISTRIBUTING;
375 1.1 yamt port->port_flags &= ~AGRPORT_DISTRIBUTING;
376 1.1 yamt
377 1.1 yamt if (lsc->lsc_active_aggregator == la) {
378 1.1 yamt lacp_select_active_aggregator(lsc);
379 1.1 yamt }
380 1.1 yamt }
381 1.1 yamt
382 1.1 yamt void
383 1.1 yamt lacp_enable_distributing(struct lacp_port *lp)
384 1.1 yamt {
385 1.1 yamt struct agr_port *port = lp->lp_agrport;
386 1.1 yamt struct lacp_aggregator *la = lp->lp_aggregator;
387 1.1 yamt struct lacp_softc *lsc = LACP_SOFTC(AGR_SC_FROM_PORT(port));
388 1.1 yamt #if defined(LACP_DEBUG)
389 1.1 yamt char buf[LACP_LAGIDSTR_MAX+1];
390 1.1 yamt #endif /* defined(LACP_DEBUG) */
391 1.1 yamt
392 1.1 yamt if ((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0) {
393 1.1 yamt return;
394 1.1 yamt }
395 1.1 yamt
396 1.1 yamt KASSERT(la);
397 1.1 yamt
398 1.1 yamt LACP_DPRINTF((lp, "enable distributing on aggregator %s, "
399 1.1 yamt "nports %d -> %d\n",
400 1.1 yamt lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
401 1.1 yamt la->la_nports, la->la_nports + 1));
402 1.1 yamt
403 1.1 yamt KASSERT(la->la_refcnt > la->la_nports);
404 1.1 yamt TAILQ_INSERT_HEAD(&la->la_ports, lp, lp_dist_q);
405 1.1 yamt la->la_nports++;
406 1.1 yamt
407 1.1 yamt lacp_suppress_distributing(lsc, la);
408 1.1 yamt
409 1.1 yamt lp->lp_state |= LACP_STATE_DISTRIBUTING;
410 1.1 yamt port->port_flags |= AGRPORT_DISTRIBUTING;
411 1.1 yamt
412 1.1 yamt if (lsc->lsc_active_aggregator != la) {
413 1.1 yamt lacp_select_active_aggregator(lsc);
414 1.1 yamt }
415 1.1 yamt }
416 1.1 yamt
417 1.1 yamt static void
418 1.1 yamt lacp_transit_expire(void *vp)
419 1.1 yamt {
420 1.1 yamt struct agr_softc *sc = vp;
421 1.1 yamt struct lacp_softc *lsc = LACP_SOFTC(sc);
422 1.1 yamt int s;
423 1.1 yamt
424 1.1 yamt s = AGR_LOCK(sc);
425 1.1 yamt LACP_DPRINTF((NULL, "%s\n", __func__));
426 1.1 yamt lsc->lsc_suppress_distributing = FALSE;
427 1.1 yamt AGR_UNLOCK(sc, s);
428 1.1 yamt }
429 1.1 yamt
430 1.1 yamt /* -------------------- */
431 1.1 yamt /* XXX */
432 1.1 yamt void
433 1.1 yamt ieee8023ad_portinit(struct agr_port *port)
434 1.1 yamt {
435 1.1 yamt struct ieee8023ad_port *iport = IEEE8023AD_PORT(port);
436 1.1 yamt
437 1.1 yamt memset(iport, 0, sizeof(iport));
438 1.1 yamt
439 1.1 yamt lacp_portinit(port);
440 1.1 yamt }
441 1.1 yamt
442 1.1 yamt void
443 1.1 yamt ieee8023ad_portfini(struct agr_port *port)
444 1.1 yamt {
445 1.1 yamt struct agr_softc *sc = AGR_SC_FROM_PORT(port);
446 1.1 yamt int s;
447 1.1 yamt
448 1.1 yamt s = AGR_LOCK(sc);
449 1.1 yamt
450 1.1 yamt lacp_portfini(port);
451 1.1 yamt
452 1.1 yamt AGR_UNLOCK(sc, s);
453 1.1 yamt }
454 1.1 yamt
455 1.1 yamt void
456 1.1 yamt ieee8023ad_ctor(struct agr_softc *sc)
457 1.1 yamt {
458 1.1 yamt struct ieee8023ad_softc *isc = IEEE8023AD_SOFTC(sc);
459 1.1 yamt struct lacp_softc *lsc = &isc->isc_lacpsc;
460 1.1 yamt
461 1.1 yamt lsc->lsc_active_aggregator = NULL;
462 1.1 yamt TAILQ_INIT(&lsc->lsc_aggregators);
463 1.1 yamt callout_init(&lsc->lsc_transit_callout);
464 1.1 yamt callout_setfunc(&lsc->lsc_transit_callout, lacp_transit_expire, sc);
465 1.1 yamt }
466 1.1 yamt
467 1.1 yamt void
468 1.1 yamt ieee8023ad_dtor(struct agr_softc *sc)
469 1.1 yamt {
470 1.1 yamt struct ieee8023ad_softc *isc = IEEE8023AD_SOFTC(sc);
471 1.1 yamt struct lacp_softc *lsc = &isc->isc_lacpsc;
472 1.1 yamt
473 1.1 yamt LACP_DPRINTF((NULL, "%s\n", __func__));
474 1.1 yamt
475 1.1 yamt callout_stop(&lsc->lsc_transit_callout);
476 1.1 yamt KASSERT(TAILQ_EMPTY(&lsc->lsc_aggregators));
477 1.1 yamt KASSERT(lsc->lsc_active_aggregator == NULL);
478 1.1 yamt }
479 1.1 yamt
480 1.1 yamt /* -------------------- */
481 1.1 yamt
482 1.1 yamt struct agr_port *
483 1.1 yamt ieee8023ad_select_tx_port(struct agr_softc *sc, struct mbuf *m)
484 1.1 yamt {
485 1.1 yamt const struct lacp_softc *lsc = LACP_SOFTC(sc);
486 1.1 yamt const struct lacp_aggregator *la;
487 1.1 yamt const struct lacp_port *lp;
488 1.1 yamt uint32_t hash;
489 1.1 yamt int nports;
490 1.1 yamt
491 1.1 yamt if (__predict_false(lsc->lsc_suppress_distributing &&
492 1.1 yamt !AGR_ROUNDROBIN(sc))) {
493 1.1 yamt LACP_DPRINTF((NULL, "%s: waiting transit\n", __func__));
494 1.1 yamt sc->sc_if.if_collisions++; /* XXX abuse */
495 1.1 yamt return NULL;
496 1.1 yamt }
497 1.1 yamt
498 1.1 yamt la = lsc->lsc_active_aggregator;
499 1.1 yamt if (__predict_false(la == NULL)) {
500 1.1 yamt LACP_DPRINTF((NULL, "%s: no active aggregator\n", __func__));
501 1.1 yamt return NULL;
502 1.1 yamt }
503 1.1 yamt
504 1.1 yamt nports = la->la_nports;
505 1.1 yamt KASSERT(nports > 0);
506 1.1 yamt
507 1.1 yamt if (AGR_ROUNDROBIN(sc)) {
508 1.1 yamt /* packet ordering rule violation */
509 1.1 yamt hash = sc->sc_rr_counter++;
510 1.1 yamt } else {
511 1.1 yamt hash = (*sc->sc_iftop->iftop_hashmbuf)(sc, m);
512 1.1 yamt }
513 1.1 yamt hash %= nports;
514 1.1 yamt lp = TAILQ_FIRST(&la->la_ports);
515 1.1 yamt KASSERT(lp != NULL);
516 1.1 yamt while (hash--) {
517 1.1 yamt lp = TAILQ_NEXT(lp, lp_dist_q);
518 1.1 yamt KASSERT(lp != NULL);
519 1.1 yamt }
520 1.1 yamt
521 1.1 yamt KASSERT((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0);
522 1.1 yamt
523 1.1 yamt return lp->lp_agrport;
524 1.1 yamt }
525 1.1 yamt
526 1.1 yamt /*
527 1.1 yamt * lacp_suppress_distributing: drop transmit packets for a while
528 1.1 yamt * to preserve packet ordering.
529 1.1 yamt */
530 1.1 yamt
531 1.1 yamt static void
532 1.1 yamt lacp_suppress_distributing(struct lacp_softc *lsc, struct lacp_aggregator *la)
533 1.1 yamt {
534 1.1 yamt
535 1.1 yamt if (lsc->lsc_active_aggregator != la) {
536 1.1 yamt return;
537 1.1 yamt }
538 1.1 yamt
539 1.1 yamt LACP_DPRINTF((NULL, "%s\n", __func__));
540 1.1 yamt lsc->lsc_suppress_distributing = TRUE;
541 1.1 yamt /* XXX should consider collector max delay */
542 1.1 yamt callout_schedule(&lsc->lsc_transit_callout,
543 1.1 yamt LACP_TRANSIT_DELAY * hz / 1000);
544 1.1 yamt }
545 1.1 yamt
546 1.1 yamt /* -------------------- */
547 1.1 yamt
548 1.1 yamt int
549 1.1 yamt lacp_compare_peerinfo(const struct lacp_peerinfo *a,
550 1.1 yamt const struct lacp_peerinfo *b)
551 1.1 yamt {
552 1.1 yamt
553 1.1 yamt return memcmp(a, b, offsetof(struct lacp_peerinfo, lip_state));
554 1.1 yamt }
555 1.1 yamt
556 1.1 yamt int
557 1.1 yamt lacp_compare_systemid(const struct lacp_systemid *a,
558 1.1 yamt const struct lacp_systemid *b)
559 1.1 yamt {
560 1.1 yamt
561 1.1 yamt return memcmp(a, b, sizeof(*a));
562 1.1 yamt }
563 1.1 yamt
564 1.1 yamt int
565 1.1 yamt lacp_compare_portid(const struct lacp_portid *a,
566 1.1 yamt const struct lacp_portid *b)
567 1.1 yamt {
568 1.1 yamt
569 1.1 yamt return memcmp(a, b, sizeof(*a));
570 1.1 yamt }
571 1.1 yamt
572 1.1 yamt /* -------------------- */
573 1.1 yamt
574 1.1 yamt static uint64_t
575 1.1 yamt lacp_aggregator_bandwidth(struct lacp_aggregator *la)
576 1.1 yamt {
577 1.1 yamt struct lacp_port *lp;
578 1.1 yamt uint64_t speed;
579 1.1 yamt
580 1.1 yamt lp = TAILQ_FIRST(&la->la_ports);
581 1.1 yamt if (lp == NULL) {
582 1.1 yamt return 0;
583 1.1 yamt }
584 1.1 yamt
585 1.1 yamt speed = ifmedia_baudrate(lp->lp_media);
586 1.1 yamt speed *= la->la_nports;
587 1.1 yamt if (speed == 0) {
588 1.1 yamt LACP_DPRINTF((lp, "speed 0? media=0x%x nports=%d\n",
589 1.1 yamt lp->lp_media, la->la_nports));
590 1.1 yamt }
591 1.1 yamt
592 1.1 yamt return speed;
593 1.1 yamt }
594 1.1 yamt
595 1.1 yamt /*
596 1.1 yamt * lacp_select_active_aggregator: select an aggregator to be used to transmit
597 1.1 yamt * packets from agr(4) interface.
598 1.1 yamt */
599 1.1 yamt
600 1.1 yamt static void
601 1.1 yamt lacp_select_active_aggregator(struct lacp_softc *lsc)
602 1.1 yamt {
603 1.1 yamt struct lacp_aggregator *la;
604 1.1 yamt struct lacp_aggregator *best_la = NULL;
605 1.1 yamt uint64_t best_speed = 0;
606 1.1 yamt #if defined(LACP_DEBUG)
607 1.1 yamt char buf[LACP_LAGIDSTR_MAX+1];
608 1.1 yamt #endif /* defined(LACP_DEBUG) */
609 1.1 yamt
610 1.1 yamt LACP_DPRINTF((NULL, "%s:\n", __func__));
611 1.1 yamt
612 1.1 yamt TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
613 1.1 yamt uint64_t speed;
614 1.1 yamt
615 1.1 yamt if (la->la_nports == 0) {
616 1.1 yamt continue;
617 1.1 yamt }
618 1.1 yamt
619 1.1 yamt speed = lacp_aggregator_bandwidth(la);
620 1.1 yamt LACP_DPRINTF((NULL, "%s, speed=%" PRIu64 ", nports=%d\n",
621 1.1 yamt lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
622 1.1 yamt speed, la->la_nports));
623 1.1 yamt if (speed > best_speed ||
624 1.1 yamt (speed == best_speed &&
625 1.1 yamt la == lsc->lsc_active_aggregator)) {
626 1.1 yamt best_la = la;
627 1.1 yamt best_speed = speed;
628 1.1 yamt }
629 1.1 yamt }
630 1.1 yamt
631 1.1 yamt KASSERT(best_la == NULL || best_la->la_nports > 0);
632 1.1 yamt KASSERT(best_la == NULL || !TAILQ_EMPTY(&best_la->la_ports));
633 1.1 yamt
634 1.1 yamt #if defined(LACP_DEBUG)
635 1.1 yamt if (lsc->lsc_active_aggregator != best_la) {
636 1.1 yamt LACP_DPRINTF((NULL, "active aggregator changed\n"));
637 1.1 yamt LACP_DPRINTF((NULL, "old %s\n",
638 1.1 yamt lacp_format_lagid_aggregator(lsc->lsc_active_aggregator,
639 1.1 yamt buf, sizeof(buf))));
640 1.1 yamt } else {
641 1.1 yamt LACP_DPRINTF((NULL, "active aggregator not changed\n"));
642 1.1 yamt }
643 1.1 yamt LACP_DPRINTF((NULL, "new %s\n",
644 1.1 yamt lacp_format_lagid_aggregator(best_la, buf, sizeof(buf))));
645 1.1 yamt #endif /* defined(LACP_DEBUG) */
646 1.1 yamt
647 1.1 yamt if (lsc->lsc_active_aggregator != best_la) {
648 1.1 yamt lsc->lsc_active_aggregator = best_la;
649 1.1 yamt if (best_la) {
650 1.1 yamt lacp_suppress_distributing(lsc, best_la);
651 1.1 yamt }
652 1.1 yamt }
653 1.1 yamt }
654 1.1 yamt
655 1.1 yamt uint16_t
656 1.1 yamt lacp_compose_key(struct lacp_port *lp)
657 1.1 yamt {
658 1.1 yamt u_int media = lp->lp_media;
659 1.1 yamt uint16_t key;
660 1.1 yamt
661 1.1 yamt KASSERT(IFM_TYPE(media) == IFM_ETHER);
662 1.1 yamt
663 1.1 yamt if (!(lp->lp_state & LACP_STATE_AGGREGATION)) {
664 1.1 yamt
665 1.1 yamt /*
666 1.1 yamt * non-aggregatable links should have unique keys.
667 1.1 yamt *
668 1.1 yamt * XXX this isn't really unique as if_index is 16 bit.
669 1.1 yamt */
670 1.1 yamt
671 1.1 yamt /* bit 0..14: (some bits of) if_index of this port */
672 1.1 yamt key = lp->lp_agrport->port_ifp->if_index;
673 1.1 yamt /* bit 15: 1 */
674 1.1 yamt key |= 0x8000;
675 1.1 yamt } else {
676 1.1 yamt u_int subtype = IFM_SUBTYPE(media);
677 1.1 yamt
678 1.1 yamt KASSERT((media & IFM_HDX) == 0); /* should be handled above */
679 1.1 yamt KASSERT((subtype & 0x1f) == subtype);
680 1.1 yamt
681 1.1 yamt /* bit 0..4: IFM_SUBTYPE */
682 1.1 yamt key = subtype;
683 1.1 yamt /* bit 5..14: (some bits of) if_index of agr device */
684 1.1 yamt key |= 0x7fe0 & ((lp->lp_agrport->port_agrifp->if_index) << 5);
685 1.1 yamt /* bit 15: 0 */
686 1.1 yamt }
687 1.1 yamt
688 1.1 yamt return htobe16(key);
689 1.1 yamt }
690