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