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