1 /* $NetBSD: ieee8023ad_lacp.c,v 1.3 2005/12/11 12:24:54 christos Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-2-Clause
5 *
6 * Copyright (c)2005 YAMAMOTO Takashi,
7 * Copyright (c)2008 Andrew Thompson <thompsa@FreeBSD.org>
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #include "opt_kern_tls.h"
34 #include "opt_ratelimit.h"
35
36 #include <sys/param.h>
37 #include <sys/callout.h>
38 #include <sys/eventhandler.h>
39 #include <sys/mbuf.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h> /* hz */
43 #include <sys/socket.h> /* for net/if.h */
44 #include <sys/sockio.h>
45 #include <sys/stdarg.h>
46 #include <sys/sysctl.h>
47 #include <sys/lock.h>
48 #include <sys/rwlock.h>
49 #include <sys/taskqueue.h>
50 #include <sys/time.h>
51
52 #include <net/if.h>
53 #include <net/if_var.h>
54 #include <net/if_private.h>
55 #include <net/if_dl.h>
56 #include <net/ethernet.h>
57 #include <net/infiniband.h>
58 #include <net/if_media.h>
59 #include <net/if_types.h>
60
61 #include <net/if_lagg.h>
62 #include <net/ieee8023ad_lacp.h>
63
64 /*
65 * actor system priority and port priority.
66 * XXX should be configurable.
67 */
68
69 #define LACP_SYSTEM_PRIO 0x8000
70 #define LACP_PORT_PRIO 0x8000
71
72 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
73 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
74
75 static const struct tlv_template lacp_info_tlv_template[] = {
76 { LACP_TYPE_ACTORINFO,
77 sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
78 { LACP_TYPE_PARTNERINFO,
79 sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
80 { LACP_TYPE_COLLECTORINFO,
81 sizeof(struct tlvhdr) + sizeof(struct lacp_collectorinfo) },
82 { 0, 0 },
83 };
84
85 static const struct tlv_template marker_info_tlv_template[] = {
86 { MARKER_TYPE_INFO,
87 sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
88 { 0, 0 },
89 };
90
91 static const struct tlv_template marker_response_tlv_template[] = {
92 { MARKER_TYPE_RESPONSE,
93 sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
94 { 0, 0 },
95 };
96
97 typedef void (*lacp_timer_func_t)(struct lacp_port *);
98
99 static void lacp_fill_actorinfo(struct lacp_port *, struct lacp_peerinfo *);
100 static void lacp_fill_markerinfo(struct lacp_port *,
101 struct lacp_markerinfo *);
102
103 static uint64_t lacp_aggregator_bandwidth(struct lacp_aggregator *);
104 static void lacp_suppress_distributing(struct lacp_softc *,
105 struct lacp_aggregator *);
106 static void lacp_transit_expire(void *);
107 static void lacp_update_portmap(struct lacp_softc *);
108 static void lacp_select_active_aggregator(struct lacp_softc *);
109 static uint16_t lacp_compose_key(struct lacp_port *);
110 static int tlv_check(const void *, size_t, const struct tlvhdr *,
111 const struct tlv_template *, boolean_t);
112 static void lacp_tick(void *);
113
114 static void lacp_fill_aggregator_id(struct lacp_aggregator *,
115 const struct lacp_port *);
116 static void lacp_fill_aggregator_id_peer(struct lacp_peerinfo *,
117 const struct lacp_peerinfo *);
118 static bool lacp_aggregator_is_compatible(const struct lacp_aggregator *,
119 const struct lacp_port *);
120 static bool lacp_peerinfo_is_compatible(const struct lacp_peerinfo *,
121 const struct lacp_peerinfo *);
122
123 static struct lacp_aggregator *lacp_aggregator_get(struct lacp_softc *,
124 struct lacp_port *);
125 static void lacp_aggregator_addref(struct lacp_softc *,
126 struct lacp_aggregator *);
127 static void lacp_aggregator_delref(struct lacp_softc *,
128 struct lacp_aggregator *);
129
130 /* receive machine */
131
132 static int lacp_pdu_input(struct lacp_port *, struct mbuf *);
133 static int lacp_marker_input(struct lacp_port *, struct mbuf *);
134 static void lacp_sm_rx(struct lacp_port *, const struct lacpdu *);
135 static void lacp_sm_rx_timer(struct lacp_port *);
136 static void lacp_sm_rx_set_expired(struct lacp_port *);
137 static void lacp_sm_rx_update_ntt(struct lacp_port *,
138 const struct lacpdu *);
139 static void lacp_sm_rx_record_pdu(struct lacp_port *,
140 const struct lacpdu *);
141 static void lacp_sm_rx_update_selected(struct lacp_port *,
142 const struct lacpdu *);
143 static void lacp_sm_rx_record_default(struct lacp_port *);
144 static void lacp_sm_rx_update_default_selected(struct lacp_port *);
145 static void lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *,
146 const struct lacp_peerinfo *);
147
148 /* mux machine */
149
150 static void lacp_sm_mux(struct lacp_port *);
151 static void lacp_set_mux(struct lacp_port *, enum lacp_mux_state);
152 static void lacp_sm_mux_timer(struct lacp_port *);
153
154 /* periodic transmit machine */
155
156 static void lacp_sm_ptx_update_timeout(struct lacp_port *, uint8_t);
157 static void lacp_sm_ptx_tx_schedule(struct lacp_port *);
158 static void lacp_sm_ptx_timer(struct lacp_port *);
159
160 /* transmit machine */
161
162 static void lacp_sm_tx(struct lacp_port *);
163 static void lacp_sm_assert_ntt(struct lacp_port *);
164
165 static void lacp_run_timers(struct lacp_port *);
166 static int lacp_compare_peerinfo(const struct lacp_peerinfo *,
167 const struct lacp_peerinfo *);
168 static int lacp_compare_systemid(const struct lacp_systemid *,
169 const struct lacp_systemid *);
170 static void lacp_port_enable(struct lacp_port *);
171 static void lacp_port_disable(struct lacp_port *);
172 static void lacp_select(struct lacp_port *);
173 static void lacp_unselect(struct lacp_port *);
174 static void lacp_disable_collecting(struct lacp_port *);
175 static void lacp_enable_collecting(struct lacp_port *);
176 static void lacp_disable_distributing(struct lacp_port *);
177 static void lacp_enable_distributing(struct lacp_port *);
178 static int lacp_xmit_lacpdu(struct lacp_port *);
179 static int lacp_xmit_marker(struct lacp_port *);
180
181 /* Debugging */
182
183 static void lacp_dump_lacpdu(const struct lacpdu *);
184 static const char *lacp_format_partner(const struct lacp_peerinfo *, char *,
185 size_t);
186 static const char *lacp_format_lagid(const struct lacp_peerinfo *,
187 const struct lacp_peerinfo *, char *, size_t);
188 static const char *lacp_format_lagid_aggregator(const struct lacp_aggregator *,
189 char *, size_t);
190 static const char *lacp_format_state(uint8_t, char *, size_t);
191 static const char *lacp_format_mac(const uint8_t *, char *, size_t);
192 static const char *lacp_format_systemid(const struct lacp_systemid *, char *,
193 size_t);
194 static const char *lacp_format_portid(const struct lacp_portid *, char *,
195 size_t);
196 static void lacp_dprintf(const struct lacp_port *, const char *, ...)
197 __attribute__((__format__(__printf__, 2, 3)));
198
199 VNET_DEFINE_STATIC(int, lacp_debug);
200 #define V_lacp_debug VNET(lacp_debug)
201 SYSCTL_NODE(_net_link_lagg, OID_AUTO, lacp, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
202 "ieee802.3ad");
203 SYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, debug, CTLFLAG_RWTUN | CTLFLAG_VNET,
204 &VNET_NAME(lacp_debug), 0, "Enable LACP debug logging (1=debug, 2=trace)");
205
206 VNET_DEFINE_STATIC(int, lacp_default_strict_mode) = 1;
207 SYSCTL_INT(_net_link_lagg_lacp, OID_AUTO, default_strict_mode,
208 CTLFLAG_RWTUN | CTLFLAG_VNET, &VNET_NAME(lacp_default_strict_mode), 0,
209 "LACP strict protocol compliance default");
210 #define LACP_DPRINTF(a) if (V_lacp_debug & 0x01) { lacp_dprintf a ; }
211 #define LACP_TRACE(a) if (V_lacp_debug & 0x02) { lacp_dprintf(a,"%s\n",__func__); }
212 #define LACP_TPRINTF(a) if (V_lacp_debug & 0x04) { lacp_dprintf a ; }
213
214 /*
215 * partner administration variables.
216 * XXX should be configurable.
217 */
218
219 static const struct lacp_peerinfo lacp_partner_admin_optimistic = {
220 .lip_systemid = { .lsi_prio = 0xffff },
221 .lip_portid = { .lpi_prio = 0xffff },
222 .lip_state = LACP_STATE_SYNC | LACP_STATE_AGGREGATION |
223 LACP_STATE_COLLECTING | LACP_STATE_DISTRIBUTING,
224 };
225
226 static const struct lacp_peerinfo lacp_partner_admin_strict = {
227 .lip_systemid = { .lsi_prio = 0xffff },
228 .lip_portid = { .lpi_prio = 0xffff },
229 .lip_state = 0,
230 };
231
232 static const lacp_timer_func_t lacp_timer_funcs[LACP_NTIMER] = {
233 [LACP_TIMER_CURRENT_WHILE] = lacp_sm_rx_timer,
234 [LACP_TIMER_PERIODIC] = lacp_sm_ptx_timer,
235 [LACP_TIMER_WAIT_WHILE] = lacp_sm_mux_timer,
236 };
237
238 struct mbuf *
lacp_input(struct lagg_port * lgp,struct mbuf * m)239 lacp_input(struct lagg_port *lgp, struct mbuf *m)
240 {
241 struct lacp_port *lp = LACP_PORT(lgp);
242 uint8_t subtype;
243
244 if (m->m_pkthdr.len < sizeof(struct ether_header) + sizeof(subtype)) {
245 m_freem(m);
246 return (NULL);
247 }
248
249 m_copydata(m, sizeof(struct ether_header), sizeof(subtype), &subtype);
250 switch (subtype) {
251 case SLOWPROTOCOLS_SUBTYPE_LACP:
252 lacp_pdu_input(lp, m);
253 return (NULL);
254
255 case SLOWPROTOCOLS_SUBTYPE_MARKER:
256 lacp_marker_input(lp, m);
257 return (NULL);
258 }
259
260 /* Not a subtype we are interested in */
261 return (m);
262 }
263
264 /*
265 * lacp_pdu_input: process lacpdu
266 */
267 static int
lacp_pdu_input(struct lacp_port * lp,struct mbuf * m)268 lacp_pdu_input(struct lacp_port *lp, struct mbuf *m)
269 {
270 struct lacp_softc *lsc = lp->lp_lsc;
271 struct lacpdu *du;
272 int error = 0;
273
274 if (m->m_pkthdr.len != sizeof(*du)) {
275 goto bad;
276 }
277
278 if ((m->m_flags & M_MCAST) == 0) {
279 goto bad;
280 }
281
282 if (m->m_len < sizeof(*du)) {
283 m = m_pullup(m, sizeof(*du));
284 if (m == NULL) {
285 return (ENOMEM);
286 }
287 }
288
289 du = mtod(m, struct lacpdu *);
290
291 if (memcmp(&du->ldu_eh.ether_dhost,
292 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
293 goto bad;
294 }
295
296 /*
297 * ignore the version for compatibility with
298 * the future protocol revisions.
299 */
300 #if 0
301 if (du->ldu_sph.sph_version != 1) {
302 goto bad;
303 }
304 #endif
305
306 /*
307 * ignore tlv types for compatibility with
308 * the future protocol revisions.
309 */
310 if (tlv_check(du, sizeof(*du), &du->ldu_tlv_actor,
311 lacp_info_tlv_template, FALSE)) {
312 goto bad;
313 }
314
315 if (V_lacp_debug > 0) {
316 lacp_dprintf(lp, "lacpdu receive\n");
317 lacp_dump_lacpdu(du);
318 }
319
320 if ((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_rx_test) {
321 LACP_TPRINTF((lp, "Dropping RX PDU\n"));
322 goto bad;
323 }
324
325 LACP_LOCK(lsc);
326 lacp_sm_rx(lp, du);
327 LACP_UNLOCK(lsc);
328
329 m_freem(m);
330 return (error);
331
332 bad:
333 m_freem(m);
334 return (EINVAL);
335 }
336
337 static void
lacp_fill_actorinfo(struct lacp_port * lp,struct lacp_peerinfo * info)338 lacp_fill_actorinfo(struct lacp_port *lp, struct lacp_peerinfo *info)
339 {
340 struct lagg_port *lgp = lp->lp_lagg;
341 struct lagg_softc *sc = lgp->lp_softc;
342
343 info->lip_systemid.lsi_prio = htons(LACP_SYSTEM_PRIO);
344 memcpy(&info->lip_systemid.lsi_mac,
345 IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN);
346 info->lip_portid.lpi_prio = htons(LACP_PORT_PRIO);
347 info->lip_portid.lpi_portno = htons(lp->lp_ifp->if_index);
348 info->lip_state = lp->lp_state;
349 }
350
351 static void
lacp_fill_markerinfo(struct lacp_port * lp,struct lacp_markerinfo * info)352 lacp_fill_markerinfo(struct lacp_port *lp, struct lacp_markerinfo *info)
353 {
354 struct ifnet *ifp = lp->lp_ifp;
355
356 /* Fill in the port index and system id (encoded as the MAC) */
357 info->mi_rq_port = htons(ifp->if_index);
358 memcpy(&info->mi_rq_system, lp->lp_systemid.lsi_mac, ETHER_ADDR_LEN);
359 info->mi_rq_xid = htonl(0);
360 }
361
362 static int
lacp_xmit_lacpdu(struct lacp_port * lp)363 lacp_xmit_lacpdu(struct lacp_port *lp)
364 {
365 struct lagg_port *lgp = lp->lp_lagg;
366 struct mbuf *m;
367 struct lacpdu *du;
368 int error;
369
370 LACP_LOCK_ASSERT(lp->lp_lsc);
371
372 m = m_gethdr(M_NOWAIT, MT_DATA);
373 if (m == NULL) {
374 return (ENOMEM);
375 }
376 m->m_len = m->m_pkthdr.len = sizeof(*du);
377
378 du = mtod(m, struct lacpdu *);
379 memset(du, 0, sizeof(*du));
380
381 memcpy(&du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
382 ETHER_ADDR_LEN);
383 memcpy(&du->ldu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
384 du->ldu_eh.ether_type = htons(ETHERTYPE_SLOW);
385
386 du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP;
387 du->ldu_sph.sph_version = 1;
388
389 TLV_SET(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO, sizeof(du->ldu_actor));
390 du->ldu_actor = lp->lp_actor;
391
392 TLV_SET(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO,
393 sizeof(du->ldu_partner));
394 du->ldu_partner = lp->lp_partner;
395
396 TLV_SET(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO,
397 sizeof(du->ldu_collector));
398 du->ldu_collector.lci_maxdelay = 0;
399
400 if (V_lacp_debug > 0) {
401 lacp_dprintf(lp, "lacpdu transmit\n");
402 lacp_dump_lacpdu(du);
403 }
404
405 m->m_flags |= M_MCAST;
406
407 /*
408 * XXX should use higher priority queue.
409 * otherwise network congestion can break aggregation.
410 */
411
412 error = lagg_enqueue(lp->lp_ifp, m);
413 return (error);
414 }
415
416 static int
lacp_xmit_marker(struct lacp_port * lp)417 lacp_xmit_marker(struct lacp_port *lp)
418 {
419 struct lagg_port *lgp = lp->lp_lagg;
420 struct mbuf *m;
421 struct markerdu *mdu;
422 int error;
423
424 LACP_LOCK_ASSERT(lp->lp_lsc);
425
426 m = m_gethdr(M_NOWAIT, MT_DATA);
427 if (m == NULL) {
428 return (ENOMEM);
429 }
430 m->m_len = m->m_pkthdr.len = sizeof(*mdu);
431
432 mdu = mtod(m, struct markerdu *);
433 memset(mdu, 0, sizeof(*mdu));
434
435 memcpy(&mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
436 ETHER_ADDR_LEN);
437 memcpy(&mdu->mdu_eh.ether_shost, lgp->lp_lladdr, ETHER_ADDR_LEN);
438 mdu->mdu_eh.ether_type = htons(ETHERTYPE_SLOW);
439
440 mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER;
441 mdu->mdu_sph.sph_version = 1;
442
443 /* Bump the transaction id and copy over the marker info */
444 lp->lp_marker.mi_rq_xid = htonl(ntohl(lp->lp_marker.mi_rq_xid) + 1);
445 TLV_SET(&mdu->mdu_tlv, MARKER_TYPE_INFO, sizeof(mdu->mdu_info));
446 mdu->mdu_info = lp->lp_marker;
447
448 LACP_DPRINTF((lp, "marker transmit, port=%u, sys=%6D, id=%u\n",
449 ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system, ":",
450 ntohl(mdu->mdu_info.mi_rq_xid)));
451
452 m->m_flags |= M_MCAST;
453 error = lagg_enqueue(lp->lp_ifp, m);
454 return (error);
455 }
456
457 void
lacp_linkstate(struct lagg_port * lgp)458 lacp_linkstate(struct lagg_port *lgp)
459 {
460 struct lacp_port *lp = LACP_PORT(lgp);
461 struct lacp_softc *lsc = lp->lp_lsc;
462 struct ifnet *ifp = lgp->lp_ifp;
463 struct ifmediareq ifmr;
464 int error = 0;
465 u_int media;
466 uint8_t old_state;
467 uint16_t old_key;
468
469 bzero((char *)&ifmr, sizeof(ifmr));
470 error = (*ifp->if_ioctl)(ifp, SIOCGIFXMEDIA, (caddr_t)&ifmr);
471 if (error != 0) {
472 bzero((char *)&ifmr, sizeof(ifmr));
473 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
474 }
475 if (error != 0)
476 return;
477
478 LACP_LOCK(lsc);
479 media = ifmr.ifm_active;
480 LACP_DPRINTF((lp, "media changed 0x%x -> 0x%x, ether = %d, fdx = %d, "
481 "link = %d\n", lp->lp_media, media, IFM_TYPE(media) == IFM_ETHER,
482 (media & IFM_FDX) != 0, ifp->if_link_state == LINK_STATE_UP));
483 old_state = lp->lp_state;
484 old_key = lp->lp_key;
485
486 lp->lp_media = media;
487 /*
488 * If the port is not an active full duplex Ethernet link then it can
489 * not be aggregated.
490 */
491 if (IFM_TYPE(media) != IFM_ETHER || (media & IFM_FDX) == 0 ||
492 ifp->if_link_state != LINK_STATE_UP) {
493 lacp_port_disable(lp);
494 } else {
495 lacp_port_enable(lp);
496 }
497 lp->lp_key = lacp_compose_key(lp);
498
499 if (old_state != lp->lp_state || old_key != lp->lp_key) {
500 LACP_DPRINTF((lp, "-> UNSELECTED\n"));
501 lp->lp_selected = LACP_UNSELECTED;
502 }
503 LACP_UNLOCK(lsc);
504 }
505
506 static void
lacp_tick(void * arg)507 lacp_tick(void *arg)
508 {
509 struct lacp_softc *lsc = arg;
510 struct lacp_port *lp;
511
512 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
513 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0)
514 continue;
515
516 CURVNET_SET(lp->lp_ifp->if_vnet);
517 lacp_run_timers(lp);
518
519 lacp_select(lp);
520 lacp_sm_mux(lp);
521 lacp_sm_tx(lp);
522 lacp_sm_ptx_tx_schedule(lp);
523 CURVNET_RESTORE();
524 }
525 callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
526 }
527
528 int
lacp_port_create(struct lagg_port * lgp)529 lacp_port_create(struct lagg_port *lgp)
530 {
531 struct lagg_softc *sc = lgp->lp_softc;
532 struct lacp_softc *lsc = LACP_SOFTC(sc);
533 struct lacp_port *lp;
534 struct ifnet *ifp = lgp->lp_ifp;
535 struct sockaddr_dl sdl;
536 struct ifmultiaddr *rifma = NULL;
537 int error;
538
539 link_init_sdl(ifp, (struct sockaddr *)&sdl, IFT_ETHER);
540 sdl.sdl_alen = ETHER_ADDR_LEN;
541
542 bcopy(ðermulticastaddr_slowprotocols,
543 LLADDR(&sdl), ETHER_ADDR_LEN);
544 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
545 if (error) {
546 printf("%s: ADDMULTI failed on %s\n", __func__,
547 lgp->lp_ifp->if_xname);
548 return (error);
549 }
550
551 lp = malloc(sizeof(struct lacp_port),
552 M_DEVBUF, M_NOWAIT|M_ZERO);
553 if (lp == NULL)
554 return (ENOMEM);
555
556 LACP_LOCK(lsc);
557 lgp->lp_psc = lp;
558 lp->lp_ifp = ifp;
559 lp->lp_lagg = lgp;
560 lp->lp_lsc = lsc;
561 lp->lp_ifma = rifma;
562
563 LIST_INSERT_HEAD(&lsc->lsc_ports, lp, lp_next);
564
565 lacp_fill_actorinfo(lp, &lp->lp_actor);
566 lacp_fill_markerinfo(lp, &lp->lp_marker);
567 lp->lp_state = LACP_STATE_ACTIVITY;
568 lp->lp_aggregator = NULL;
569 lacp_sm_rx_set_expired(lp);
570 LACP_UNLOCK(lsc);
571 lacp_linkstate(lgp);
572
573 return (0);
574 }
575
576 void
lacp_port_destroy(struct lagg_port * lgp)577 lacp_port_destroy(struct lagg_port *lgp)
578 {
579 struct lacp_port *lp = LACP_PORT(lgp);
580 struct lacp_softc *lsc = lp->lp_lsc;
581 int i;
582
583 LACP_LOCK(lsc);
584 for (i = 0; i < LACP_NTIMER; i++) {
585 LACP_TIMER_DISARM(lp, i);
586 }
587
588 lacp_disable_collecting(lp);
589 lacp_disable_distributing(lp);
590 lacp_unselect(lp);
591
592 LIST_REMOVE(lp, lp_next);
593 LACP_UNLOCK(lsc);
594
595 /* The address may have already been removed by if_purgemaddrs() */
596 if (!lgp->lp_detaching)
597 if_delmulti_ifma(lp->lp_ifma);
598
599 free(lp, M_DEVBUF);
600 }
601
602 void
lacp_req(struct lagg_softc * sc,void * data)603 lacp_req(struct lagg_softc *sc, void *data)
604 {
605 struct lacp_opreq *req = (struct lacp_opreq *)data;
606 struct lacp_softc *lsc = LACP_SOFTC(sc);
607 struct lacp_aggregator *la;
608
609 bzero(req, sizeof(struct lacp_opreq));
610
611 /*
612 * If the LACP softc is NULL, return with the opreq structure full of
613 * zeros. It is normal for the softc to be NULL while the lagg is
614 * being destroyed.
615 */
616 if (NULL == lsc)
617 return;
618
619 la = lsc->lsc_active_aggregator;
620 LACP_LOCK(lsc);
621 if (la != NULL) {
622 req->actor_prio = ntohs(la->la_actor.lip_systemid.lsi_prio);
623 memcpy(&req->actor_mac, &la->la_actor.lip_systemid.lsi_mac,
624 ETHER_ADDR_LEN);
625 req->actor_key = ntohs(la->la_actor.lip_key);
626 req->actor_portprio = ntohs(la->la_actor.lip_portid.lpi_prio);
627 req->actor_portno = ntohs(la->la_actor.lip_portid.lpi_portno);
628 req->actor_state = la->la_actor.lip_state;
629
630 req->partner_prio = ntohs(la->la_partner.lip_systemid.lsi_prio);
631 memcpy(&req->partner_mac, &la->la_partner.lip_systemid.lsi_mac,
632 ETHER_ADDR_LEN);
633 req->partner_key = ntohs(la->la_partner.lip_key);
634 req->partner_portprio = ntohs(la->la_partner.lip_portid.lpi_prio);
635 req->partner_portno = ntohs(la->la_partner.lip_portid.lpi_portno);
636 req->partner_state = la->la_partner.lip_state;
637 }
638 LACP_UNLOCK(lsc);
639 }
640
641 void
lacp_portreq(struct lagg_port * lgp,void * data)642 lacp_portreq(struct lagg_port *lgp, void *data)
643 {
644 struct lacp_opreq *req = (struct lacp_opreq *)data;
645 struct lacp_port *lp = LACP_PORT(lgp);
646 struct lacp_softc *lsc = lp->lp_lsc;
647
648 LACP_LOCK(lsc);
649 req->actor_prio = ntohs(lp->lp_actor.lip_systemid.lsi_prio);
650 memcpy(&req->actor_mac, &lp->lp_actor.lip_systemid.lsi_mac,
651 ETHER_ADDR_LEN);
652 req->actor_key = ntohs(lp->lp_actor.lip_key);
653 req->actor_portprio = ntohs(lp->lp_actor.lip_portid.lpi_prio);
654 req->actor_portno = ntohs(lp->lp_actor.lip_portid.lpi_portno);
655 req->actor_state = lp->lp_actor.lip_state;
656
657 req->partner_prio = ntohs(lp->lp_partner.lip_systemid.lsi_prio);
658 memcpy(&req->partner_mac, &lp->lp_partner.lip_systemid.lsi_mac,
659 ETHER_ADDR_LEN);
660 req->partner_key = ntohs(lp->lp_partner.lip_key);
661 req->partner_portprio = ntohs(lp->lp_partner.lip_portid.lpi_prio);
662 req->partner_portno = ntohs(lp->lp_partner.lip_portid.lpi_portno);
663 req->partner_state = lp->lp_partner.lip_state;
664 LACP_UNLOCK(lsc);
665 }
666
667 static void
lacp_disable_collecting(struct lacp_port * lp)668 lacp_disable_collecting(struct lacp_port *lp)
669 {
670 LACP_DPRINTF((lp, "collecting disabled\n"));
671 lp->lp_state &= ~LACP_STATE_COLLECTING;
672 }
673
674 static void
lacp_enable_collecting(struct lacp_port * lp)675 lacp_enable_collecting(struct lacp_port *lp)
676 {
677 LACP_DPRINTF((lp, "collecting enabled\n"));
678 lp->lp_state |= LACP_STATE_COLLECTING;
679 }
680
681 static void
lacp_disable_distributing(struct lacp_port * lp)682 lacp_disable_distributing(struct lacp_port *lp)
683 {
684 struct lacp_aggregator *la = lp->lp_aggregator;
685 struct lacp_aggregator *la_active;
686 struct lacp_softc *lsc = lp->lp_lsc;
687 struct lagg_softc *sc = lsc->lsc_softc;
688 char buf[LACP_LAGIDSTR_MAX+1];
689
690 LACP_LOCK_ASSERT(lsc);
691
692 if (la == NULL || (lp->lp_state & LACP_STATE_DISTRIBUTING) == 0) {
693 return;
694 }
695
696 KASSERT(!TAILQ_EMPTY(&la->la_ports), ("no aggregator ports"));
697 KASSERT(la->la_nports > 0, ("nports invalid (%d)", la->la_nports));
698 KASSERT(la->la_refcnt >= la->la_nports, ("aggregator refcnt invalid"));
699
700 LACP_DPRINTF((lp, "disable distributing on aggregator %s, "
701 "nports %d -> %d\n",
702 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
703 la->la_nports, la->la_nports - 1));
704
705 TAILQ_REMOVE(&la->la_ports, lp, lp_dist_q);
706 la->la_nports--;
707
708 if (lsc->lsc_active_aggregator == la) {
709 lacp_suppress_distributing(lsc, la);
710 lacp_select_active_aggregator(lsc);
711 /* regenerate the port map, the active aggregator has changed */
712 lacp_update_portmap(lsc);
713 }
714
715 lp->lp_state &= ~LACP_STATE_DISTRIBUTING;
716 la_active = lsc->lsc_active_aggregator;
717 sc->sc_active = la_active != NULL ? la_active->la_nports : 0;
718 if_link_state_change(sc->sc_ifp,
719 sc->sc_active ? LINK_STATE_UP : LINK_STATE_DOWN);
720 }
721
722 static void
lacp_enable_distributing(struct lacp_port * lp)723 lacp_enable_distributing(struct lacp_port *lp)
724 {
725 struct lacp_aggregator *la = lp->lp_aggregator;
726 struct lacp_aggregator *la_active;
727 struct lacp_softc *lsc = lp->lp_lsc;
728 struct lagg_softc *sc = lsc->lsc_softc;
729 char buf[LACP_LAGIDSTR_MAX+1];
730
731 LACP_LOCK_ASSERT(lsc);
732
733 if ((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0) {
734 return;
735 }
736
737 LACP_DPRINTF((lp, "enable distributing on aggregator %s, "
738 "nports %d -> %d\n",
739 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
740 la->la_nports, la->la_nports + 1));
741
742 KASSERT(la->la_refcnt > la->la_nports, ("aggregator refcnt invalid"));
743 TAILQ_INSERT_HEAD(&la->la_ports, lp, lp_dist_q);
744 la->la_nports++;
745
746 lp->lp_state |= LACP_STATE_DISTRIBUTING;
747
748 if (lsc->lsc_active_aggregator == la) {
749 lacp_suppress_distributing(lsc, la);
750 lacp_update_portmap(lsc);
751 } else
752 /* try to become the active aggregator */
753 lacp_select_active_aggregator(lsc);
754
755 la_active = lsc->lsc_active_aggregator;
756 sc->sc_active = la_active != NULL ? la_active->la_nports : 0;
757 if_link_state_change(sc->sc_ifp,
758 sc->sc_active ? LINK_STATE_UP : LINK_STATE_DOWN);
759 }
760
761 static void
lacp_transit_expire(void * vp)762 lacp_transit_expire(void *vp)
763 {
764 struct lacp_softc *lsc = vp;
765
766 LACP_LOCK_ASSERT(lsc);
767
768 CURVNET_SET(lsc->lsc_softc->sc_ifp->if_vnet);
769 LACP_TRACE(NULL);
770 CURVNET_RESTORE();
771
772 lsc->lsc_suppress_distributing = FALSE;
773 }
774
775 void
lacp_attach(struct lagg_softc * sc)776 lacp_attach(struct lagg_softc *sc)
777 {
778 struct lacp_softc *lsc;
779
780 lsc = malloc(sizeof(struct lacp_softc), M_DEVBUF, M_WAITOK | M_ZERO);
781
782 sc->sc_psc = lsc;
783 lsc->lsc_softc = sc;
784
785 lsc->lsc_hashkey = m_ether_tcpip_hash_init();
786 lsc->lsc_active_aggregator = NULL;
787 lsc->lsc_strict_mode = VNET(lacp_default_strict_mode);
788 LACP_LOCK_INIT(lsc);
789 TAILQ_INIT(&lsc->lsc_aggregators);
790 LIST_INIT(&lsc->lsc_ports);
791
792 callout_init_mtx(&lsc->lsc_transit_callout, &lsc->lsc_mtx, 0);
793 callout_init_mtx(&lsc->lsc_callout, &lsc->lsc_mtx, 0);
794
795 /* if the lagg is already up then do the same */
796 if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
797 lacp_init(sc);
798 }
799
800 void
lacp_detach(void * psc)801 lacp_detach(void *psc)
802 {
803 struct lacp_softc *lsc = (struct lacp_softc *)psc;
804
805 KASSERT(TAILQ_EMPTY(&lsc->lsc_aggregators),
806 ("aggregators still active"));
807 KASSERT(lsc->lsc_active_aggregator == NULL,
808 ("aggregator still attached"));
809
810 callout_drain(&lsc->lsc_transit_callout);
811 callout_drain(&lsc->lsc_callout);
812
813 LACP_LOCK_DESTROY(lsc);
814 free(lsc, M_DEVBUF);
815 }
816
817 void
lacp_init(struct lagg_softc * sc)818 lacp_init(struct lagg_softc *sc)
819 {
820 struct lacp_softc *lsc = LACP_SOFTC(sc);
821
822 LACP_LOCK(lsc);
823 callout_reset(&lsc->lsc_callout, hz, lacp_tick, lsc);
824 LACP_UNLOCK(lsc);
825 }
826
827 void
lacp_stop(struct lagg_softc * sc)828 lacp_stop(struct lagg_softc *sc)
829 {
830 struct lacp_softc *lsc = LACP_SOFTC(sc);
831
832 LACP_LOCK(lsc);
833 callout_stop(&lsc->lsc_transit_callout);
834 callout_stop(&lsc->lsc_callout);
835 LACP_UNLOCK(lsc);
836 }
837
838 struct lagg_port *
lacp_select_tx_port_by_hash(struct lagg_softc * sc,uint32_t hash,uint8_t numa_domain,int * err)839 lacp_select_tx_port_by_hash(struct lagg_softc *sc, uint32_t hash,
840 uint8_t numa_domain, int *err)
841 {
842 struct lacp_softc *lsc = LACP_SOFTC(sc);
843 struct lacp_portmap *pm;
844 struct lacp_port *lp;
845 struct lacp_port **map;
846 int count;
847
848 if (__predict_false(lsc->lsc_suppress_distributing)) {
849 LACP_DPRINTF((NULL, "%s: waiting transit\n", __func__));
850 *err = ENOBUFS;
851 return (NULL);
852 }
853
854 pm = &lsc->lsc_pmap[lsc->lsc_activemap];
855 if (pm->pm_count == 0) {
856 LACP_DPRINTF((NULL, "%s: no active aggregator\n", __func__));
857 *err = ENETDOWN;
858 return (NULL);
859 }
860
861 #ifdef NUMA
862 if ((sc->sc_opts & LAGG_OPT_USE_NUMA) &&
863 pm->pm_num_dom > 1 && numa_domain < MAXMEMDOM) {
864 count = pm->pm_numa[numa_domain].count;
865 if (count > 0) {
866 map = pm->pm_numa[numa_domain].map;
867 } else {
868 /* No ports on this domain; use global hash. */
869 map = pm->pm_map;
870 count = pm->pm_count;
871 }
872 } else
873 #endif
874 {
875 map = pm->pm_map;
876 count = pm->pm_count;
877 }
878
879 hash %= count;
880 lp = map[hash];
881
882 return (lp->lp_lagg);
883 }
884
885 struct lagg_port *
lacp_select_tx_port(struct lagg_softc * sc,struct mbuf * m,int * err)886 lacp_select_tx_port(struct lagg_softc *sc, struct mbuf *m, int *err)
887 {
888 struct lacp_softc *lsc = LACP_SOFTC(sc);
889 uint32_t hash;
890 uint8_t numa_domain;
891
892 if ((sc->sc_opts & LAGG_OPT_USE_FLOWID) &&
893 M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
894 hash = m->m_pkthdr.flowid >> sc->flowid_shift;
895 else
896 hash = m_ether_tcpip_hash(sc->sc_flags, m, lsc->lsc_hashkey);
897
898 numa_domain = m->m_pkthdr.numa_domain;
899 return (lacp_select_tx_port_by_hash(sc, hash, numa_domain, err));
900 }
901
902 /*
903 * lacp_suppress_distributing: drop transmit packets for a while
904 * to preserve packet ordering.
905 */
906
907 static void
lacp_suppress_distributing(struct lacp_softc * lsc,struct lacp_aggregator * la)908 lacp_suppress_distributing(struct lacp_softc *lsc, struct lacp_aggregator *la)
909 {
910 struct lacp_port *lp;
911
912 if (lsc->lsc_active_aggregator != la) {
913 return;
914 }
915
916 LACP_TRACE(NULL);
917
918 lsc->lsc_suppress_distributing = TRUE;
919
920 /* send a marker frame down each port to verify the queues are empty */
921 LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
922 lp->lp_flags |= LACP_PORT_MARK;
923 if (lacp_xmit_marker(lp) != 0)
924 lp->lp_flags &= ~LACP_PORT_MARK;
925 }
926
927 /* set a timeout for the marker frames */
928 callout_reset(&lsc->lsc_transit_callout,
929 LACP_TRANSIT_DELAY * hz / 1000, lacp_transit_expire, lsc);
930 }
931
932 static int
lacp_compare_peerinfo(const struct lacp_peerinfo * a,const struct lacp_peerinfo * b)933 lacp_compare_peerinfo(const struct lacp_peerinfo *a,
934 const struct lacp_peerinfo *b)
935 {
936 return (memcmp(a, b, offsetof(struct lacp_peerinfo, lip_state)));
937 }
938
939 static int
lacp_compare_systemid(const struct lacp_systemid * a,const struct lacp_systemid * b)940 lacp_compare_systemid(const struct lacp_systemid *a,
941 const struct lacp_systemid *b)
942 {
943 return (memcmp(a, b, sizeof(*a)));
944 }
945
946 #if 0 /* unused */
947 static int
948 lacp_compare_portid(const struct lacp_portid *a,
949 const struct lacp_portid *b)
950 {
951 return (memcmp(a, b, sizeof(*a)));
952 }
953 #endif
954
955 static uint64_t
lacp_aggregator_bandwidth(struct lacp_aggregator * la)956 lacp_aggregator_bandwidth(struct lacp_aggregator *la)
957 {
958 struct lacp_port *lp;
959 uint64_t speed;
960
961 lp = TAILQ_FIRST(&la->la_ports);
962 if (lp == NULL) {
963 return (0);
964 }
965
966 speed = ifmedia_baudrate(lp->lp_media);
967 speed *= la->la_nports;
968 if (speed == 0) {
969 LACP_DPRINTF((lp, "speed 0? media=0x%x nports=%d\n",
970 lp->lp_media, la->la_nports));
971 }
972
973 return (speed);
974 }
975
976 /*
977 * lacp_select_active_aggregator: select an aggregator to be used to transmit
978 * packets from lagg(4) interface.
979 */
980
981 static void
lacp_select_active_aggregator(struct lacp_softc * lsc)982 lacp_select_active_aggregator(struct lacp_softc *lsc)
983 {
984 struct lacp_aggregator *la;
985 struct lacp_aggregator *best_la = NULL;
986 uint64_t best_speed = 0;
987 char buf[LACP_LAGIDSTR_MAX+1];
988
989 LACP_TRACE(NULL);
990
991 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
992 uint64_t speed;
993
994 if (la->la_nports == 0) {
995 continue;
996 }
997
998 speed = lacp_aggregator_bandwidth(la);
999 LACP_DPRINTF((NULL, "%s, speed=%jd, nports=%d\n",
1000 lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
1001 speed, la->la_nports));
1002
1003 /*
1004 * This aggregator is chosen if the partner has a better
1005 * system priority or, the total aggregated speed is higher
1006 * or, it is already the chosen aggregator
1007 */
1008 if ((best_la != NULL && LACP_SYS_PRI(la->la_partner) <
1009 LACP_SYS_PRI(best_la->la_partner)) ||
1010 speed > best_speed ||
1011 (speed == best_speed &&
1012 la == lsc->lsc_active_aggregator)) {
1013 best_la = la;
1014 best_speed = speed;
1015 }
1016 }
1017
1018 KASSERT(best_la == NULL || best_la->la_nports > 0,
1019 ("invalid aggregator refcnt"));
1020 KASSERT(best_la == NULL || !TAILQ_EMPTY(&best_la->la_ports),
1021 ("invalid aggregator list"));
1022
1023 if (lsc->lsc_active_aggregator != best_la) {
1024 LACP_DPRINTF((NULL, "active aggregator changed\n"));
1025 LACP_DPRINTF((NULL, "old %s\n",
1026 lacp_format_lagid_aggregator(lsc->lsc_active_aggregator,
1027 buf, sizeof(buf))));
1028 } else {
1029 LACP_DPRINTF((NULL, "active aggregator not changed\n"));
1030 }
1031 LACP_DPRINTF((NULL, "new %s\n",
1032 lacp_format_lagid_aggregator(best_la, buf, sizeof(buf))));
1033
1034 if (lsc->lsc_active_aggregator != best_la) {
1035 lsc->lsc_active_aggregator = best_la;
1036 lacp_update_portmap(lsc);
1037 if (best_la) {
1038 lacp_suppress_distributing(lsc, best_la);
1039 }
1040 }
1041 }
1042
1043 static int
lacp_pm_compare(const void * p1,const void * p2)1044 lacp_pm_compare(const void *p1, const void *p2)
1045 {
1046 struct lacp_port *const *a = p1;
1047 struct lacp_port *const *b = p2;
1048 int left, right;
1049
1050 left = (*a)->lp_ifp->if_index;
1051 right = (*b)->lp_ifp->if_index;
1052 return ((left > right) - (left < right));
1053 }
1054
1055 /*
1056 * Updated the inactive portmap array with the new list of ports and
1057 * make it live.
1058 */
1059 static void
lacp_update_portmap(struct lacp_softc * lsc)1060 lacp_update_portmap(struct lacp_softc *lsc)
1061 {
1062 struct lagg_softc *sc = lsc->lsc_softc;
1063 struct lacp_aggregator *la;
1064 struct lacp_portmap *p;
1065 struct lacp_port *lp;
1066 uint64_t speed;
1067 u_int newmap;
1068 int i;
1069 #ifdef NUMA
1070 int count;
1071 uint8_t domain;
1072 #endif
1073
1074 newmap = lsc->lsc_activemap == 0 ? 1 : 0;
1075 p = &lsc->lsc_pmap[newmap];
1076 la = lsc->lsc_active_aggregator;
1077 speed = 0;
1078 bzero(p, sizeof(struct lacp_portmap));
1079
1080 if (la != NULL && la->la_nports > 0) {
1081 p->pm_count = la->la_nports;
1082 i = 0;
1083 TAILQ_FOREACH(lp, &la->la_ports, lp_dist_q) {
1084 p->pm_map[i++] = lp;
1085 #ifdef NUMA
1086 domain = lp->lp_ifp->if_numa_domain;
1087 if (domain >= MAXMEMDOM)
1088 continue;
1089 count = p->pm_numa[domain].count;
1090 p->pm_numa[domain].map[count] = lp;
1091 p->pm_numa[domain].count++;
1092 #endif
1093 }
1094 KASSERT(i == p->pm_count, ("Invalid port count"));
1095
1096 #ifdef NUMA
1097 for (i = 0; i < MAXMEMDOM; i++) {
1098 if (p->pm_numa[i].count != 0) {
1099 p->pm_num_dom++;
1100 if (p->pm_numa[i].count > 1) {
1101 qsort(&p->pm_numa[i].map[0],
1102 p->pm_numa[i].count,
1103 sizeof(p->pm_numa[i].map[0]),
1104 lacp_pm_compare);
1105 }
1106 }
1107 }
1108 #endif
1109 speed = lacp_aggregator_bandwidth(la);
1110 }
1111
1112 if (p->pm_count > 1) {
1113 qsort(&p->pm_map[0], p->pm_count,
1114 sizeof(p->pm_map[0]), lacp_pm_compare);
1115 }
1116 sc->sc_ifp->if_baudrate = speed;
1117 EVENTHANDLER_INVOKE(ifnet_event, sc->sc_ifp,
1118 IFNET_EVENT_UPDATE_BAUDRATE);
1119
1120 /* switch the active portmap over */
1121 atomic_store_rel_int(&lsc->lsc_activemap, newmap);
1122 LACP_DPRINTF((NULL, "Set table %d with %d ports\n",
1123 lsc->lsc_activemap,
1124 lsc->lsc_pmap[lsc->lsc_activemap].pm_count));
1125 }
1126
1127 static uint16_t
lacp_compose_key(struct lacp_port * lp)1128 lacp_compose_key(struct lacp_port *lp)
1129 {
1130 struct lagg_port *lgp = lp->lp_lagg;
1131 struct lagg_softc *sc = lgp->lp_softc;
1132 u_int media = lp->lp_media;
1133 uint16_t key;
1134
1135 if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0) {
1136 /*
1137 * non-aggregatable links should have unique keys.
1138 *
1139 * XXX this isn't really unique as if_index is 16 bit.
1140 */
1141
1142 /* bit 0..14: (some bits of) if_index of this port */
1143 key = lp->lp_ifp->if_index;
1144 /* bit 15: 1 */
1145 key |= 0x8000;
1146 } else {
1147 u_int subtype = IFM_SUBTYPE(media);
1148
1149 KASSERT(IFM_TYPE(media) == IFM_ETHER, ("invalid media type"));
1150 KASSERT((media & IFM_FDX) != 0, ("aggregating HDX interface"));
1151
1152 /* bit 0..4: IFM_SUBTYPE modulo speed */
1153 switch (subtype) {
1154 case IFM_10_T:
1155 case IFM_10_2:
1156 case IFM_10_5:
1157 case IFM_10_STP:
1158 case IFM_10_FL:
1159 key = IFM_10_T;
1160 break;
1161 case IFM_100_TX:
1162 case IFM_100_FX:
1163 case IFM_100_T4:
1164 case IFM_100_VG:
1165 case IFM_100_T2:
1166 case IFM_100_T:
1167 case IFM_100_SGMII:
1168 case IFM_100_BX:
1169 key = IFM_100_TX;
1170 break;
1171 case IFM_1000_SX:
1172 case IFM_1000_LX:
1173 case IFM_1000_CX:
1174 case IFM_1000_T:
1175 case IFM_1000_KX:
1176 case IFM_1000_SGMII:
1177 case IFM_1000_CX_SGMII:
1178 case IFM_1000_BX:
1179 key = IFM_1000_SX;
1180 break;
1181 case IFM_10G_LR:
1182 case IFM_10G_SR:
1183 case IFM_10G_CX4:
1184 case IFM_10G_TWINAX:
1185 case IFM_10G_TWINAX_LONG:
1186 case IFM_10G_LRM:
1187 case IFM_10G_T:
1188 case IFM_10G_KX4:
1189 case IFM_10G_KR:
1190 case IFM_10G_CR1:
1191 case IFM_10G_ER:
1192 case IFM_10G_SFI:
1193 case IFM_10G_AOC:
1194 key = IFM_10G_LR;
1195 break;
1196 case IFM_20G_KR2:
1197 key = IFM_20G_KR2;
1198 break;
1199 case IFM_2500_KX:
1200 case IFM_2500_T:
1201 case IFM_2500_X:
1202 key = IFM_2500_KX;
1203 break;
1204 case IFM_5000_T:
1205 case IFM_5000_KR:
1206 case IFM_5000_KR_S:
1207 case IFM_5000_KR1:
1208 key = IFM_5000_T;
1209 break;
1210 case IFM_50G_PCIE:
1211 case IFM_50G_CR2:
1212 case IFM_50G_KR2:
1213 case IFM_50G_KR4:
1214 case IFM_50G_SR2:
1215 case IFM_50G_LR2:
1216 case IFM_50G_LAUI2_AC:
1217 case IFM_50G_LAUI2:
1218 case IFM_50G_AUI2_AC:
1219 case IFM_50G_AUI2:
1220 case IFM_50G_CP:
1221 case IFM_50G_SR:
1222 case IFM_50G_LR:
1223 case IFM_50G_FR:
1224 case IFM_50G_KR_PAM4:
1225 case IFM_50G_AUI1_AC:
1226 case IFM_50G_AUI1:
1227 key = IFM_50G_PCIE;
1228 break;
1229 case IFM_56G_R4:
1230 key = IFM_56G_R4;
1231 break;
1232 case IFM_25G_PCIE:
1233 case IFM_25G_CR:
1234 case IFM_25G_KR:
1235 case IFM_25G_SR:
1236 case IFM_25G_LR:
1237 case IFM_25G_ACC:
1238 case IFM_25G_AOC:
1239 case IFM_25G_T:
1240 case IFM_25G_CR_S:
1241 case IFM_25G_CR1:
1242 case IFM_25G_KR_S:
1243 case IFM_25G_AUI:
1244 case IFM_25G_KR1:
1245 key = IFM_25G_PCIE;
1246 break;
1247 case IFM_40G_CR4:
1248 case IFM_40G_SR4:
1249 case IFM_40G_LR4:
1250 case IFM_40G_LM4:
1251 case IFM_40G_XLPPI:
1252 case IFM_40G_KR4:
1253 case IFM_40G_XLAUI:
1254 case IFM_40G_XLAUI_AC:
1255 case IFM_40G_ER4:
1256 key = IFM_40G_CR4;
1257 break;
1258 case IFM_100G_CR4:
1259 case IFM_100G_SR4:
1260 case IFM_100G_KR4:
1261 case IFM_100G_LR4:
1262 case IFM_100G_CAUI4_AC:
1263 case IFM_100G_CAUI4:
1264 case IFM_100G_AUI4_AC:
1265 case IFM_100G_AUI4:
1266 case IFM_100G_CR_PAM4:
1267 case IFM_100G_KR_PAM4:
1268 case IFM_100G_CP2:
1269 case IFM_100G_SR2:
1270 case IFM_100G_DR:
1271 case IFM_100G_KR2_PAM4:
1272 case IFM_100G_CAUI2_AC:
1273 case IFM_100G_CAUI2:
1274 case IFM_100G_AUI2_AC:
1275 case IFM_100G_AUI2:
1276 key = IFM_100G_CR4;
1277 break;
1278 case IFM_200G_CR4_PAM4:
1279 case IFM_200G_SR4:
1280 case IFM_200G_FR4:
1281 case IFM_200G_LR4:
1282 case IFM_200G_DR4:
1283 case IFM_200G_KR4_PAM4:
1284 case IFM_200G_AUI4_AC:
1285 case IFM_200G_AUI4:
1286 case IFM_200G_AUI8_AC:
1287 case IFM_200G_AUI8:
1288 key = IFM_200G_CR4_PAM4;
1289 break;
1290 case IFM_400G_FR8:
1291 case IFM_400G_LR8:
1292 case IFM_400G_DR4:
1293 case IFM_400G_AUI8_AC:
1294 case IFM_400G_AUI8:
1295 case IFM_400G_SR8:
1296 case IFM_400G_CR8:
1297 key = IFM_400G_FR8;
1298 break;
1299 default:
1300 key = subtype;
1301 break;
1302 }
1303 /* bit 5..14: (some bits of) if_index of lagg device */
1304 key |= 0x7fe0 & ((sc->sc_ifp->if_index) << 5);
1305 /* bit 15: 0 */
1306 }
1307 return (htons(key));
1308 }
1309
1310 static void
lacp_aggregator_addref(struct lacp_softc * lsc,struct lacp_aggregator * la)1311 lacp_aggregator_addref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1312 {
1313 char buf[LACP_LAGIDSTR_MAX+1];
1314
1315 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1316 __func__,
1317 lacp_format_lagid(&la->la_actor, &la->la_partner,
1318 buf, sizeof(buf)),
1319 la->la_refcnt, la->la_refcnt + 1));
1320
1321 KASSERT(la->la_refcnt > 0, ("refcount <= 0"));
1322 la->la_refcnt++;
1323 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcount"));
1324 }
1325
1326 static void
lacp_aggregator_delref(struct lacp_softc * lsc,struct lacp_aggregator * la)1327 lacp_aggregator_delref(struct lacp_softc *lsc, struct lacp_aggregator *la)
1328 {
1329 char buf[LACP_LAGIDSTR_MAX+1];
1330
1331 LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
1332 __func__,
1333 lacp_format_lagid(&la->la_actor, &la->la_partner,
1334 buf, sizeof(buf)),
1335 la->la_refcnt, la->la_refcnt - 1));
1336
1337 KASSERT(la->la_refcnt > la->la_nports, ("invalid refcnt"));
1338 la->la_refcnt--;
1339 if (la->la_refcnt > 0) {
1340 return;
1341 }
1342
1343 KASSERT(la->la_refcnt == 0, ("refcount not zero"));
1344 KASSERT(lsc->lsc_active_aggregator != la, ("aggregator active"));
1345
1346 TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
1347
1348 free(la, M_DEVBUF);
1349 }
1350
1351 /*
1352 * lacp_aggregator_get: allocate an aggregator.
1353 */
1354
1355 static struct lacp_aggregator *
lacp_aggregator_get(struct lacp_softc * lsc,struct lacp_port * lp)1356 lacp_aggregator_get(struct lacp_softc *lsc, struct lacp_port *lp)
1357 {
1358 struct lacp_aggregator *la;
1359
1360 la = malloc(sizeof(*la), M_DEVBUF, M_NOWAIT);
1361 if (la) {
1362 la->la_refcnt = 1;
1363 la->la_nports = 0;
1364 TAILQ_INIT(&la->la_ports);
1365 la->la_pending = 0;
1366 TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
1367 }
1368
1369 return (la);
1370 }
1371
1372 /*
1373 * lacp_fill_aggregator_id: setup a newly allocated aggregator from a port.
1374 */
1375
1376 static void
lacp_fill_aggregator_id(struct lacp_aggregator * la,const struct lacp_port * lp)1377 lacp_fill_aggregator_id(struct lacp_aggregator *la, const struct lacp_port *lp)
1378 {
1379 lacp_fill_aggregator_id_peer(&la->la_partner, &lp->lp_partner);
1380 lacp_fill_aggregator_id_peer(&la->la_actor, &lp->lp_actor);
1381
1382 la->la_actor.lip_state = lp->lp_state & LACP_STATE_AGGREGATION;
1383 }
1384
1385 static void
lacp_fill_aggregator_id_peer(struct lacp_peerinfo * lpi_aggr,const struct lacp_peerinfo * lpi_port)1386 lacp_fill_aggregator_id_peer(struct lacp_peerinfo *lpi_aggr,
1387 const struct lacp_peerinfo *lpi_port)
1388 {
1389 memset(lpi_aggr, 0, sizeof(*lpi_aggr));
1390 lpi_aggr->lip_systemid = lpi_port->lip_systemid;
1391 lpi_aggr->lip_key = lpi_port->lip_key;
1392 }
1393
1394 /*
1395 * lacp_aggregator_is_compatible: check if a port can join to an aggregator.
1396 */
1397
1398 static bool
lacp_aggregator_is_compatible(const struct lacp_aggregator * la,const struct lacp_port * lp)1399 lacp_aggregator_is_compatible(const struct lacp_aggregator *la,
1400 const struct lacp_port *lp)
1401 {
1402 if (!(lp->lp_state & LACP_STATE_AGGREGATION) ||
1403 !(lp->lp_partner.lip_state & LACP_STATE_AGGREGATION)) {
1404 return (false);
1405 }
1406
1407 if (!(la->la_actor.lip_state & LACP_STATE_AGGREGATION))
1408 return (false);
1409
1410 if (!lacp_peerinfo_is_compatible(&la->la_partner, &lp->lp_partner))
1411 return (false);
1412
1413 if (!lacp_peerinfo_is_compatible(&la->la_actor, &lp->lp_actor))
1414 return (false);
1415
1416 return (true);
1417 }
1418
1419 static bool
lacp_peerinfo_is_compatible(const struct lacp_peerinfo * a,const struct lacp_peerinfo * b)1420 lacp_peerinfo_is_compatible(const struct lacp_peerinfo *a,
1421 const struct lacp_peerinfo *b)
1422 {
1423 if (memcmp(&a->lip_systemid, &b->lip_systemid,
1424 sizeof(a->lip_systemid)) != 0) {
1425 return (false);
1426 }
1427
1428 if (memcmp(&a->lip_key, &b->lip_key, sizeof(a->lip_key)) != 0)
1429 return (false);
1430
1431 return (true);
1432 }
1433
1434 static void
lacp_port_enable(struct lacp_port * lp)1435 lacp_port_enable(struct lacp_port *lp)
1436 {
1437 lp->lp_state |= LACP_STATE_AGGREGATION;
1438 }
1439
1440 static void
lacp_port_disable(struct lacp_port * lp)1441 lacp_port_disable(struct lacp_port *lp)
1442 {
1443 lacp_set_mux(lp, LACP_MUX_DETACHED);
1444
1445 lp->lp_state &= ~LACP_STATE_AGGREGATION;
1446 lp->lp_selected = LACP_UNSELECTED;
1447 lacp_sm_rx_record_default(lp);
1448 lp->lp_partner.lip_state &= ~LACP_STATE_AGGREGATION;
1449 lp->lp_state &= ~LACP_STATE_EXPIRED;
1450 }
1451
1452 /*
1453 * lacp_select: select an aggregator. create one if necessary.
1454 */
1455 static void
lacp_select(struct lacp_port * lp)1456 lacp_select(struct lacp_port *lp)
1457 {
1458 struct lacp_softc *lsc = lp->lp_lsc;
1459 struct lacp_aggregator *la;
1460 char buf[LACP_LAGIDSTR_MAX+1];
1461
1462 if (lp->lp_aggregator) {
1463 return;
1464 }
1465
1466 /* If we haven't heard from our peer, skip this step. */
1467 if (lp->lp_state & LACP_STATE_DEFAULTED)
1468 return;
1469
1470 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1471 ("timer_wait_while still active"));
1472
1473 LACP_DPRINTF((lp, "port lagid=%s\n",
1474 lacp_format_lagid(&lp->lp_actor, &lp->lp_partner,
1475 buf, sizeof(buf))));
1476
1477 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
1478 if (lacp_aggregator_is_compatible(la, lp)) {
1479 break;
1480 }
1481 }
1482
1483 if (la == NULL) {
1484 la = lacp_aggregator_get(lsc, lp);
1485 if (la == NULL) {
1486 LACP_DPRINTF((lp, "aggregator creation failed\n"));
1487
1488 /*
1489 * will retry on the next tick.
1490 */
1491
1492 return;
1493 }
1494 lacp_fill_aggregator_id(la, lp);
1495 LACP_DPRINTF((lp, "aggregator created\n"));
1496 } else {
1497 LACP_DPRINTF((lp, "compatible aggregator found\n"));
1498 if (la->la_refcnt == LACP_MAX_PORTS)
1499 return;
1500 lacp_aggregator_addref(lsc, la);
1501 }
1502
1503 LACP_DPRINTF((lp, "aggregator lagid=%s\n",
1504 lacp_format_lagid(&la->la_actor, &la->la_partner,
1505 buf, sizeof(buf))));
1506
1507 lp->lp_aggregator = la;
1508 lp->lp_selected = LACP_SELECTED;
1509 }
1510
1511 /*
1512 * lacp_unselect: finish unselect/detach process.
1513 */
1514
1515 static void
lacp_unselect(struct lacp_port * lp)1516 lacp_unselect(struct lacp_port *lp)
1517 {
1518 struct lacp_softc *lsc = lp->lp_lsc;
1519 struct lacp_aggregator *la = lp->lp_aggregator;
1520
1521 KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1522 ("timer_wait_while still active"));
1523
1524 if (la == NULL) {
1525 return;
1526 }
1527
1528 lp->lp_aggregator = NULL;
1529 lacp_aggregator_delref(lsc, la);
1530 }
1531
1532 /* mux machine */
1533
1534 static void
lacp_sm_mux(struct lacp_port * lp)1535 lacp_sm_mux(struct lacp_port *lp)
1536 {
1537 struct lagg_port *lgp = lp->lp_lagg;
1538 struct lagg_softc *sc = lgp->lp_softc;
1539 enum lacp_mux_state new_state;
1540 boolean_t p_sync =
1541 (lp->lp_partner.lip_state & LACP_STATE_SYNC) != 0;
1542 boolean_t p_collecting =
1543 (lp->lp_partner.lip_state & LACP_STATE_COLLECTING) != 0;
1544 enum lacp_selected selected = lp->lp_selected;
1545 struct lacp_aggregator *la;
1546
1547 if (V_lacp_debug > 1)
1548 lacp_dprintf(lp, "%s: state= 0x%x, selected= 0x%x, "
1549 "p_sync= 0x%x, p_collecting= 0x%x\n", __func__,
1550 lp->lp_mux_state, selected, p_sync, p_collecting);
1551
1552 re_eval:
1553 la = lp->lp_aggregator;
1554 KASSERT(lp->lp_mux_state == LACP_MUX_DETACHED || la != NULL,
1555 ("MUX not detached"));
1556 new_state = lp->lp_mux_state;
1557 switch (lp->lp_mux_state) {
1558 case LACP_MUX_DETACHED:
1559 if (selected != LACP_UNSELECTED) {
1560 new_state = LACP_MUX_WAITING;
1561 }
1562 break;
1563 case LACP_MUX_WAITING:
1564 KASSERT(la->la_pending > 0 ||
1565 !LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE),
1566 ("timer_wait_while still active"));
1567 if (selected == LACP_SELECTED && la->la_pending == 0) {
1568 new_state = LACP_MUX_ATTACHED;
1569 } else if (selected == LACP_UNSELECTED) {
1570 new_state = LACP_MUX_DETACHED;
1571 }
1572 break;
1573 case LACP_MUX_ATTACHED:
1574 if (selected == LACP_SELECTED && p_sync) {
1575 new_state = LACP_MUX_COLLECTING;
1576 } else if (selected != LACP_SELECTED) {
1577 new_state = LACP_MUX_DETACHED;
1578 }
1579 break;
1580 case LACP_MUX_COLLECTING:
1581 if (selected == LACP_SELECTED && p_sync && p_collecting) {
1582 new_state = LACP_MUX_DISTRIBUTING;
1583 } else if (selected != LACP_SELECTED || !p_sync) {
1584 new_state = LACP_MUX_ATTACHED;
1585 }
1586 break;
1587 case LACP_MUX_DISTRIBUTING:
1588 if (selected != LACP_SELECTED || !p_sync || !p_collecting) {
1589 new_state = LACP_MUX_COLLECTING;
1590 lacp_dprintf(lp, "Interface stopped DISTRIBUTING, possible flapping\n");
1591 sc->sc_flapping++;
1592 }
1593 break;
1594 default:
1595 panic("%s: unknown state", __func__);
1596 }
1597
1598 if (lp->lp_mux_state == new_state) {
1599 return;
1600 }
1601
1602 lacp_set_mux(lp, new_state);
1603 goto re_eval;
1604 }
1605
1606 static void
lacp_set_mux(struct lacp_port * lp,enum lacp_mux_state new_state)1607 lacp_set_mux(struct lacp_port *lp, enum lacp_mux_state new_state)
1608 {
1609 struct lacp_aggregator *la = lp->lp_aggregator;
1610
1611 if (lp->lp_mux_state == new_state) {
1612 return;
1613 }
1614
1615 switch (new_state) {
1616 case LACP_MUX_DETACHED:
1617 lp->lp_state &= ~LACP_STATE_SYNC;
1618 lacp_disable_distributing(lp);
1619 lacp_disable_collecting(lp);
1620 lacp_sm_assert_ntt(lp);
1621 /* cancel timer */
1622 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE)) {
1623 KASSERT(la->la_pending > 0,
1624 ("timer_wait_while not active"));
1625 la->la_pending--;
1626 }
1627 LACP_TIMER_DISARM(lp, LACP_TIMER_WAIT_WHILE);
1628 lacp_unselect(lp);
1629 break;
1630 case LACP_MUX_WAITING:
1631 LACP_TIMER_ARM(lp, LACP_TIMER_WAIT_WHILE,
1632 LACP_AGGREGATE_WAIT_TIME);
1633 la->la_pending++;
1634 break;
1635 case LACP_MUX_ATTACHED:
1636 lp->lp_state |= LACP_STATE_SYNC;
1637 lacp_disable_collecting(lp);
1638 lacp_sm_assert_ntt(lp);
1639 break;
1640 case LACP_MUX_COLLECTING:
1641 lacp_enable_collecting(lp);
1642 lacp_disable_distributing(lp);
1643 lacp_sm_assert_ntt(lp);
1644 break;
1645 case LACP_MUX_DISTRIBUTING:
1646 lacp_enable_distributing(lp);
1647 break;
1648 default:
1649 panic("%s: unknown state", __func__);
1650 }
1651
1652 LACP_DPRINTF((lp, "mux_state %d -> %d\n", lp->lp_mux_state, new_state));
1653
1654 lp->lp_mux_state = new_state;
1655 }
1656
1657 static void
lacp_sm_mux_timer(struct lacp_port * lp)1658 lacp_sm_mux_timer(struct lacp_port *lp)
1659 {
1660 struct lacp_aggregator *la = lp->lp_aggregator;
1661 char buf[LACP_LAGIDSTR_MAX+1];
1662
1663 KASSERT(la->la_pending > 0, ("no pending event"));
1664
1665 LACP_DPRINTF((lp, "%s: aggregator %s, pending %d -> %d\n", __func__,
1666 lacp_format_lagid(&la->la_actor, &la->la_partner,
1667 buf, sizeof(buf)),
1668 la->la_pending, la->la_pending - 1));
1669
1670 la->la_pending--;
1671 }
1672
1673 /* periodic transmit machine */
1674
1675 static void
lacp_sm_ptx_update_timeout(struct lacp_port * lp,uint8_t oldpstate)1676 lacp_sm_ptx_update_timeout(struct lacp_port *lp, uint8_t oldpstate)
1677 {
1678 if (LACP_STATE_EQ(oldpstate, lp->lp_partner.lip_state,
1679 LACP_STATE_TIMEOUT)) {
1680 return;
1681 }
1682
1683 LACP_DPRINTF((lp, "partner timeout changed\n"));
1684
1685 /*
1686 * FAST_PERIODIC -> SLOW_PERIODIC
1687 * or
1688 * SLOW_PERIODIC (-> PERIODIC_TX) -> FAST_PERIODIC
1689 *
1690 * let lacp_sm_ptx_tx_schedule to update timeout.
1691 */
1692
1693 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1694
1695 /*
1696 * if timeout has been shortened, assert NTT.
1697 */
1698
1699 if ((lp->lp_partner.lip_state & LACP_STATE_TIMEOUT)) {
1700 lacp_sm_assert_ntt(lp);
1701 }
1702 }
1703
1704 static void
lacp_sm_ptx_tx_schedule(struct lacp_port * lp)1705 lacp_sm_ptx_tx_schedule(struct lacp_port *lp)
1706 {
1707 int timeout;
1708
1709 if (!(lp->lp_state & LACP_STATE_ACTIVITY) &&
1710 !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY)) {
1711 /*
1712 * NO_PERIODIC
1713 */
1714
1715 LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
1716 return;
1717 }
1718
1719 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_PERIODIC)) {
1720 return;
1721 }
1722
1723 timeout = (lp->lp_partner.lip_state & LACP_STATE_TIMEOUT) ?
1724 LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME;
1725
1726 LACP_TIMER_ARM(lp, LACP_TIMER_PERIODIC, timeout);
1727 }
1728
1729 static void
lacp_sm_ptx_timer(struct lacp_port * lp)1730 lacp_sm_ptx_timer(struct lacp_port *lp)
1731 {
1732 lacp_sm_assert_ntt(lp);
1733 }
1734
1735 static void
lacp_sm_rx(struct lacp_port * lp,const struct lacpdu * du)1736 lacp_sm_rx(struct lacp_port *lp, const struct lacpdu *du)
1737 {
1738 int timeout;
1739
1740 /*
1741 * check LACP_DISABLED first
1742 */
1743
1744 if (!(lp->lp_state & LACP_STATE_AGGREGATION)) {
1745 return;
1746 }
1747
1748 /*
1749 * check loopback condition.
1750 */
1751
1752 if (!lacp_compare_systemid(&du->ldu_actor.lip_systemid,
1753 &lp->lp_actor.lip_systemid)) {
1754 return;
1755 }
1756
1757 /*
1758 * EXPIRED, DEFAULTED, CURRENT -> CURRENT
1759 */
1760
1761 microuptime(&lp->lp_last_lacpdu_rx);
1762 lacp_sm_rx_update_selected(lp, du);
1763 lacp_sm_rx_update_ntt(lp, du);
1764 lacp_sm_rx_record_pdu(lp, du);
1765
1766 timeout = (lp->lp_state & LACP_STATE_TIMEOUT) ?
1767 LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME;
1768 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, timeout);
1769
1770 lp->lp_state &= ~LACP_STATE_EXPIRED;
1771
1772 /*
1773 * kick transmit machine without waiting the next tick.
1774 */
1775
1776 lacp_sm_tx(lp);
1777 }
1778
1779 static void
lacp_sm_rx_set_expired(struct lacp_port * lp)1780 lacp_sm_rx_set_expired(struct lacp_port *lp)
1781 {
1782 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1783 lp->lp_partner.lip_state |= LACP_STATE_TIMEOUT;
1784 LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, LACP_SHORT_TIMEOUT_TIME);
1785 lp->lp_state |= LACP_STATE_EXPIRED;
1786 }
1787
1788 static void
lacp_sm_rx_timer(struct lacp_port * lp)1789 lacp_sm_rx_timer(struct lacp_port *lp)
1790 {
1791 if ((lp->lp_state & LACP_STATE_EXPIRED) == 0) {
1792 /* CURRENT -> EXPIRED */
1793 LACP_DPRINTF((lp, "%s: CURRENT -> EXPIRED\n", __func__));
1794 lacp_sm_rx_set_expired(lp);
1795 } else {
1796 /* EXPIRED -> DEFAULTED */
1797 LACP_DPRINTF((lp, "%s: EXPIRED -> DEFAULTED\n", __func__));
1798 lacp_sm_rx_update_default_selected(lp);
1799 lacp_sm_rx_record_default(lp);
1800 lp->lp_state &= ~LACP_STATE_EXPIRED;
1801 }
1802 }
1803
1804 static void
lacp_sm_rx_record_pdu(struct lacp_port * lp,const struct lacpdu * du)1805 lacp_sm_rx_record_pdu(struct lacp_port *lp, const struct lacpdu *du)
1806 {
1807 boolean_t active;
1808 uint8_t oldpstate;
1809 char buf[LACP_STATESTR_MAX+1];
1810
1811 LACP_TRACE(lp);
1812
1813 oldpstate = lp->lp_partner.lip_state;
1814
1815 active = (du->ldu_actor.lip_state & LACP_STATE_ACTIVITY)
1816 || ((lp->lp_state & LACP_STATE_ACTIVITY) &&
1817 (du->ldu_partner.lip_state & LACP_STATE_ACTIVITY));
1818
1819 lp->lp_partner = du->ldu_actor;
1820 if (active &&
1821 ((LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1822 LACP_STATE_AGGREGATION) &&
1823 !lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner))
1824 || (du->ldu_partner.lip_state & LACP_STATE_AGGREGATION) == 0)) {
1825 /*
1826 * XXX Maintain legacy behavior of leaving the
1827 * LACP_STATE_SYNC bit unchanged from the partner's
1828 * advertisement if lsc_strict_mode is false.
1829 * TODO: We should re-examine the concept of the "strict mode"
1830 * to ensure it makes sense to maintain a non-strict mode.
1831 */
1832 if (lp->lp_lsc->lsc_strict_mode)
1833 lp->lp_partner.lip_state |= LACP_STATE_SYNC;
1834 } else {
1835 lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
1836 }
1837
1838 lp->lp_state &= ~LACP_STATE_DEFAULTED;
1839
1840 if (oldpstate != lp->lp_partner.lip_state) {
1841 LACP_DPRINTF((lp, "old pstate %s\n",
1842 lacp_format_state(oldpstate, buf, sizeof(buf))));
1843 LACP_DPRINTF((lp, "new pstate %s\n",
1844 lacp_format_state(lp->lp_partner.lip_state, buf,
1845 sizeof(buf))));
1846 }
1847
1848 lacp_sm_ptx_update_timeout(lp, oldpstate);
1849 }
1850
1851 static void
lacp_sm_rx_update_ntt(struct lacp_port * lp,const struct lacpdu * du)1852 lacp_sm_rx_update_ntt(struct lacp_port *lp, const struct lacpdu *du)
1853 {
1854
1855 LACP_TRACE(lp);
1856
1857 if (lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner) ||
1858 !LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
1859 LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) {
1860 LACP_DPRINTF((lp, "%s: assert ntt\n", __func__));
1861 lacp_sm_assert_ntt(lp);
1862 }
1863 }
1864
1865 static void
lacp_sm_rx_record_default(struct lacp_port * lp)1866 lacp_sm_rx_record_default(struct lacp_port *lp)
1867 {
1868 uint8_t oldpstate;
1869
1870 LACP_TRACE(lp);
1871
1872 oldpstate = lp->lp_partner.lip_state;
1873 if (lp->lp_lsc->lsc_strict_mode)
1874 lp->lp_partner = lacp_partner_admin_strict;
1875 else
1876 lp->lp_partner = lacp_partner_admin_optimistic;
1877 lp->lp_state |= LACP_STATE_DEFAULTED;
1878 lacp_sm_ptx_update_timeout(lp, oldpstate);
1879 }
1880
1881 static void
lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port * lp,const struct lacp_peerinfo * info)1882 lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *lp,
1883 const struct lacp_peerinfo *info)
1884 {
1885
1886 LACP_TRACE(lp);
1887
1888 if (lacp_compare_peerinfo(&lp->lp_partner, info) ||
1889 !LACP_STATE_EQ(lp->lp_partner.lip_state, info->lip_state,
1890 LACP_STATE_AGGREGATION)) {
1891 lp->lp_selected = LACP_UNSELECTED;
1892 /* mux machine will clean up lp->lp_aggregator */
1893 }
1894 }
1895
1896 static void
lacp_sm_rx_update_selected(struct lacp_port * lp,const struct lacpdu * du)1897 lacp_sm_rx_update_selected(struct lacp_port *lp, const struct lacpdu *du)
1898 {
1899
1900 LACP_TRACE(lp);
1901
1902 lacp_sm_rx_update_selected_from_peerinfo(lp, &du->ldu_actor);
1903 }
1904
1905 static void
lacp_sm_rx_update_default_selected(struct lacp_port * lp)1906 lacp_sm_rx_update_default_selected(struct lacp_port *lp)
1907 {
1908
1909 LACP_TRACE(lp);
1910
1911 if (lp->lp_lsc->lsc_strict_mode)
1912 lacp_sm_rx_update_selected_from_peerinfo(lp,
1913 &lacp_partner_admin_strict);
1914 else
1915 lacp_sm_rx_update_selected_from_peerinfo(lp,
1916 &lacp_partner_admin_optimistic);
1917 }
1918
1919 /* transmit machine */
1920
1921 static void
lacp_sm_tx(struct lacp_port * lp)1922 lacp_sm_tx(struct lacp_port *lp)
1923 {
1924 int error = 0;
1925
1926 if (!(lp->lp_state & LACP_STATE_AGGREGATION)
1927 #if 1
1928 || (!(lp->lp_state & LACP_STATE_ACTIVITY)
1929 && !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY))
1930 #endif
1931 ) {
1932 lp->lp_flags &= ~LACP_PORT_NTT;
1933 }
1934
1935 if (!(lp->lp_flags & LACP_PORT_NTT)) {
1936 return;
1937 }
1938
1939 /* Rate limit to 3 PDUs per LACP_FAST_PERIODIC_TIME */
1940 if (ppsratecheck(&lp->lp_last_lacpdu, &lp->lp_lacpdu_sent,
1941 (3 / LACP_FAST_PERIODIC_TIME)) == 0) {
1942 LACP_DPRINTF((lp, "rate limited pdu\n"));
1943 return;
1944 }
1945
1946 if (((1 << lp->lp_ifp->if_dunit) & lp->lp_lsc->lsc_debug.lsc_tx_test) == 0) {
1947 error = lacp_xmit_lacpdu(lp);
1948 } else {
1949 LACP_TPRINTF((lp, "Dropping TX PDU\n"));
1950 }
1951
1952 if (error == 0) {
1953 lp->lp_flags &= ~LACP_PORT_NTT;
1954 } else {
1955 LACP_DPRINTF((lp, "lacpdu transmit failure, error %d\n",
1956 error));
1957 }
1958 }
1959
1960 static void
lacp_sm_assert_ntt(struct lacp_port * lp)1961 lacp_sm_assert_ntt(struct lacp_port *lp)
1962 {
1963
1964 lp->lp_flags |= LACP_PORT_NTT;
1965 }
1966
1967 static void
lacp_run_timers(struct lacp_port * lp)1968 lacp_run_timers(struct lacp_port *lp)
1969 {
1970 int i;
1971 struct timeval time_diff;
1972
1973 for (i = 0; i < LACP_NTIMER; i++) {
1974 KASSERT(lp->lp_timer[i] >= 0,
1975 ("invalid timer value %d", lp->lp_timer[i]));
1976 if (lp->lp_timer[i] == 0) {
1977 continue;
1978 } else {
1979 if (i == LACP_TIMER_CURRENT_WHILE) {
1980 microuptime(&time_diff);
1981 timevalsub(&time_diff, &lp->lp_last_lacpdu_rx);
1982 if (time_diff.tv_sec) {
1983 /* At least one sec has elapsed since last LACP packet. */
1984 --lp->lp_timer[i];
1985 }
1986 } else {
1987 --lp->lp_timer[i];
1988 }
1989
1990 if ((lp->lp_timer[i] <= 0) && (lacp_timer_funcs[i])) {
1991 (*lacp_timer_funcs[i])(lp);
1992 }
1993 }
1994 }
1995 }
1996
1997 int
lacp_marker_input(struct lacp_port * lp,struct mbuf * m)1998 lacp_marker_input(struct lacp_port *lp, struct mbuf *m)
1999 {
2000 struct lacp_softc *lsc = lp->lp_lsc;
2001 struct lagg_port *lgp = lp->lp_lagg;
2002 struct lacp_port *lp2;
2003 struct markerdu *mdu;
2004 int error = 0;
2005 int pending = 0;
2006
2007 if (m->m_pkthdr.len != sizeof(*mdu)) {
2008 goto bad;
2009 }
2010
2011 if ((m->m_flags & M_MCAST) == 0) {
2012 goto bad;
2013 }
2014
2015 if (m->m_len < sizeof(*mdu)) {
2016 m = m_pullup(m, sizeof(*mdu));
2017 if (m == NULL) {
2018 return (ENOMEM);
2019 }
2020 }
2021
2022 mdu = mtod(m, struct markerdu *);
2023
2024 if (memcmp(&mdu->mdu_eh.ether_dhost,
2025 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN)) {
2026 goto bad;
2027 }
2028
2029 if (mdu->mdu_sph.sph_version != 1) {
2030 goto bad;
2031 }
2032
2033 switch (mdu->mdu_tlv.tlv_type) {
2034 case MARKER_TYPE_INFO:
2035 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
2036 marker_info_tlv_template, TRUE)) {
2037 goto bad;
2038 }
2039 mdu->mdu_tlv.tlv_type = MARKER_TYPE_RESPONSE;
2040 memcpy(&mdu->mdu_eh.ether_dhost,
2041 ðermulticastaddr_slowprotocols, ETHER_ADDR_LEN);
2042 memcpy(&mdu->mdu_eh.ether_shost,
2043 lgp->lp_lladdr, ETHER_ADDR_LEN);
2044 error = lagg_enqueue(lp->lp_ifp, m);
2045 break;
2046
2047 case MARKER_TYPE_RESPONSE:
2048 if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
2049 marker_response_tlv_template, TRUE)) {
2050 goto bad;
2051 }
2052 LACP_DPRINTF((lp, "marker response, port=%u, sys=%6D, id=%u\n",
2053 ntohs(mdu->mdu_info.mi_rq_port), mdu->mdu_info.mi_rq_system,
2054 ":", ntohl(mdu->mdu_info.mi_rq_xid)));
2055
2056 /* Verify that it is the last marker we sent out */
2057 if (memcmp(&mdu->mdu_info, &lp->lp_marker,
2058 sizeof(struct lacp_markerinfo)))
2059 goto bad;
2060
2061 LACP_LOCK(lsc);
2062 lp->lp_flags &= ~LACP_PORT_MARK;
2063
2064 if (lsc->lsc_suppress_distributing) {
2065 /* Check if any ports are waiting for a response */
2066 LIST_FOREACH(lp2, &lsc->lsc_ports, lp_next) {
2067 if (lp2->lp_flags & LACP_PORT_MARK) {
2068 pending = 1;
2069 break;
2070 }
2071 }
2072
2073 if (pending == 0) {
2074 /* All interface queues are clear */
2075 LACP_DPRINTF((NULL, "queue flush complete\n"));
2076 lsc->lsc_suppress_distributing = FALSE;
2077 }
2078 }
2079 LACP_UNLOCK(lsc);
2080 m_freem(m);
2081 break;
2082
2083 default:
2084 goto bad;
2085 }
2086
2087 return (error);
2088
2089 bad:
2090 LACP_DPRINTF((lp, "bad marker frame\n"));
2091 m_freem(m);
2092 return (EINVAL);
2093 }
2094
2095 static int
tlv_check(const void * p,size_t size,const struct tlvhdr * tlv,const struct tlv_template * tmpl,boolean_t check_type)2096 tlv_check(const void *p, size_t size, const struct tlvhdr *tlv,
2097 const struct tlv_template *tmpl, boolean_t check_type)
2098 {
2099 while (/* CONSTCOND */ 1) {
2100 if ((const char *)tlv - (const char *)p + sizeof(*tlv) > size) {
2101 return (EINVAL);
2102 }
2103 if ((check_type && tlv->tlv_type != tmpl->tmpl_type) ||
2104 tlv->tlv_length != tmpl->tmpl_length) {
2105 return (EINVAL);
2106 }
2107 if (tmpl->tmpl_type == 0) {
2108 break;
2109 }
2110 tlv = (const struct tlvhdr *)
2111 ((const char *)tlv + tlv->tlv_length);
2112 tmpl++;
2113 }
2114
2115 return (0);
2116 }
2117
2118 /* Debugging */
2119 const char *
lacp_format_mac(const uint8_t * mac,char * buf,size_t buflen)2120 lacp_format_mac(const uint8_t *mac, char *buf, size_t buflen)
2121 {
2122 snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X",
2123 (int)mac[0],
2124 (int)mac[1],
2125 (int)mac[2],
2126 (int)mac[3],
2127 (int)mac[4],
2128 (int)mac[5]);
2129
2130 return (buf);
2131 }
2132
2133 const char *
lacp_format_systemid(const struct lacp_systemid * sysid,char * buf,size_t buflen)2134 lacp_format_systemid(const struct lacp_systemid *sysid,
2135 char *buf, size_t buflen)
2136 {
2137 char macbuf[LACP_MACSTR_MAX+1];
2138
2139 snprintf(buf, buflen, "%04X,%s",
2140 ntohs(sysid->lsi_prio),
2141 lacp_format_mac(sysid->lsi_mac, macbuf, sizeof(macbuf)));
2142
2143 return (buf);
2144 }
2145
2146 const char *
lacp_format_portid(const struct lacp_portid * portid,char * buf,size_t buflen)2147 lacp_format_portid(const struct lacp_portid *portid, char *buf, size_t buflen)
2148 {
2149 snprintf(buf, buflen, "%04X,%04X",
2150 ntohs(portid->lpi_prio),
2151 ntohs(portid->lpi_portno));
2152
2153 return (buf);
2154 }
2155
2156 const char *
lacp_format_partner(const struct lacp_peerinfo * peer,char * buf,size_t buflen)2157 lacp_format_partner(const struct lacp_peerinfo *peer, char *buf, size_t buflen)
2158 {
2159 char sysid[LACP_SYSTEMIDSTR_MAX+1];
2160 char portid[LACP_PORTIDSTR_MAX+1];
2161
2162 snprintf(buf, buflen, "(%s,%04X,%s)",
2163 lacp_format_systemid(&peer->lip_systemid, sysid, sizeof(sysid)),
2164 ntohs(peer->lip_key),
2165 lacp_format_portid(&peer->lip_portid, portid, sizeof(portid)));
2166
2167 return (buf);
2168 }
2169
2170 const char *
lacp_format_lagid(const struct lacp_peerinfo * a,const struct lacp_peerinfo * b,char * buf,size_t buflen)2171 lacp_format_lagid(const struct lacp_peerinfo *a,
2172 const struct lacp_peerinfo *b, char *buf, size_t buflen)
2173 {
2174 char astr[LACP_PARTNERSTR_MAX+1];
2175 char bstr[LACP_PARTNERSTR_MAX+1];
2176
2177 #if 0
2178 /*
2179 * there's a convention to display small numbered peer
2180 * in the left.
2181 */
2182
2183 if (lacp_compare_peerinfo(a, b) > 0) {
2184 const struct lacp_peerinfo *t;
2185
2186 t = a;
2187 a = b;
2188 b = t;
2189 }
2190 #endif
2191
2192 snprintf(buf, buflen, "[%s,%s]",
2193 lacp_format_partner(a, astr, sizeof(astr)),
2194 lacp_format_partner(b, bstr, sizeof(bstr)));
2195
2196 return (buf);
2197 }
2198
2199 const char *
lacp_format_lagid_aggregator(const struct lacp_aggregator * la,char * buf,size_t buflen)2200 lacp_format_lagid_aggregator(const struct lacp_aggregator *la,
2201 char *buf, size_t buflen)
2202 {
2203 if (la == NULL) {
2204 return ("(none)");
2205 }
2206
2207 return (lacp_format_lagid(&la->la_actor, &la->la_partner, buf, buflen));
2208 }
2209
2210 const char *
lacp_format_state(uint8_t state,char * buf,size_t buflen)2211 lacp_format_state(uint8_t state, char *buf, size_t buflen)
2212 {
2213 snprintf(buf, buflen, "%b", state, LACP_STATE_BITS);
2214 return (buf);
2215 }
2216
2217 static void
lacp_dump_lacpdu(const struct lacpdu * du)2218 lacp_dump_lacpdu(const struct lacpdu *du)
2219 {
2220 char buf[LACP_PARTNERSTR_MAX+1];
2221 char buf2[LACP_STATESTR_MAX+1];
2222
2223 printf("actor=%s\n",
2224 lacp_format_partner(&du->ldu_actor, buf, sizeof(buf)));
2225 printf("actor.state=%s\n",
2226 lacp_format_state(du->ldu_actor.lip_state, buf2, sizeof(buf2)));
2227 printf("partner=%s\n",
2228 lacp_format_partner(&du->ldu_partner, buf, sizeof(buf)));
2229 printf("partner.state=%s\n",
2230 lacp_format_state(du->ldu_partner.lip_state, buf2, sizeof(buf2)));
2231
2232 printf("maxdelay=%d\n", ntohs(du->ldu_collector.lci_maxdelay));
2233 }
2234
2235 static void
lacp_dprintf(const struct lacp_port * lp,const char * fmt,...)2236 lacp_dprintf(const struct lacp_port *lp, const char *fmt, ...)
2237 {
2238 va_list va;
2239
2240 if (lp) {
2241 printf("%s: ", lp->lp_ifp->if_xname);
2242 }
2243
2244 va_start(va, fmt);
2245 vprintf(fmt, va);
2246 va_end(va);
2247 }
2248