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