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