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