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