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