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