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