if_lagg_lacp.c revision 1.34 1 /* $NetBSD: if_lagg_lacp.c,v 1.34 2024/04/04 08:53:14 yamaguchi Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5 *
6 * Copyright (c)2005 YAMAMOTO Takashi,
7 * Copyright (c)2008 Andrew Thompson <thompsa (at) FreeBSD.org>
8 * Copyright (c)2021 Internet Initiative Japan, Inc.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: if_lagg_lacp.c,v 1.34 2024/04/04 08:53:14 yamaguchi Exp $");
35
36 #ifdef _KERNEL_OPT
37 #include "opt_lagg.h"
38 #endif
39
40 #include <sys/param.h>
41 #include <sys/types.h>
42
43 #include <sys/evcnt.h>
44 #include <sys/kmem.h>
45 #include <sys/pcq.h>
46 #include <sys/pslist.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
49 #include <sys/workqueue.h>
50
51 #include <net/if.h>
52 #include <net/if_dl.h>
53 #include <net/if_ether.h>
54 #include <net/if_media.h>
55
56 #include <net/ether_slowprotocols.h>
57
58 #include <net/lagg/if_lagg.h>
59 #include <net/lagg/if_laggproto.h>
60 #include <net/lagg/if_lagg_lacp.h>
61
62 #define LACP_SYSTEMIDSTR_LEN 32
63
64 enum {
65 LACP_TIMER_CURRENT_WHILE = 0,
66 LACP_TIMER_PERIODIC,
67 LACP_TIMER_WAIT_WHILE,
68 LACP_NTIMER
69 };
70
71 enum {
72 LACP_PTIMER_DISTRIBUTING = 0,
73 LACP_NPTIMER
74 };
75
76 enum lacp_selected {
77 LACP_UNSELECTED,
78 LACP_READY,
79 LACP_STANDBY,
80 LACP_SELECTED,
81 };
82
83 enum lacp_mux_state {
84 LACP_MUX_DETACHED,
85 LACP_MUX_WAITING,
86 LACP_MUX_STANDBY,
87 LACP_MUX_ATTACHED,
88 LACP_MUX_COLLECTING,
89 LACP_MUX_DISTRIBUTING,
90 LACP_MUX_INIT,
91 };
92
93 struct lacp_aggregator_systemid {
94 uint16_t sid_prio;
95 uint16_t sid_key;
96 uint8_t sid_mac[LACP_MAC_LEN];
97 };
98
99 struct lacp_aggregator {
100 TAILQ_ENTRY(lacp_aggregator)
101 la_q;
102 LIST_HEAD(, lacp_port)
103 la_ports;
104 ssize_t la_attached_port;
105
106 struct lacp_aggregator_systemid
107 la_sid;
108 };
109
110 struct lacp_portinfo {
111 uint8_t lpi_state;
112 uint16_t lpi_portno;
113 #define LACP_PORTNO_NONE 0
114 uint16_t lpi_portprio;
115 };
116
117 struct lacp_port {
118 struct lagg_port *lp_laggport;
119 bool lp_added_multi;
120 int lp_timer[LACP_NTIMER];
121 uint32_t lp_marker_xid;
122 enum lacp_selected lp_selected;
123 enum lacp_mux_state lp_mux_state;
124
125 struct lacp_portinfo lp_actor;
126 struct lacp_portinfo lp_partner;
127 struct lacp_aggregator *lp_aggregator;
128 struct lacp_aggregator_systemid
129 lp_aggregator_sidbuf;
130 uint64_t lp_linkspeed;
131 int lp_pending;
132 LIST_ENTRY(lacp_port) lp_entry_la;
133 struct timeval lp_last_lacpdu;
134 int lp_lacpdu_sent;
135 bool lp_collector;
136
137 unsigned int lp_flags;
138 #define LACP_PORT_NTT __BIT(0)
139 #define LACP_PORT_MARK __BIT(1)
140
141 struct lagg_work lp_work_smtx;
142 struct lagg_work lp_work_marker;
143 };
144
145 struct lacp_portmap {
146 size_t pm_count;
147 struct lagg_port *pm_ports[LACP_MAX_PORTS];
148 };
149
150 struct lacp_softc {
151 struct lagg_softc *lsc_softc;
152 kmutex_t lsc_lock;
153 pserialize_t lsc_psz;
154 bool lsc_running;
155 bool lsc_suppress_distributing;
156 int lsc_timer[LACP_NPTIMER];
157 uint8_t lsc_system_mac[LACP_MAC_LEN];
158 uint16_t lsc_system_prio;
159 uint16_t lsc_key;
160 size_t lsc_max_ports;
161 size_t lsc_activemap;
162 struct lacp_portmap lsc_portmaps[2]; /* active & idle */
163 struct lacp_aggregator *lsc_aggregator;
164 TAILQ_HEAD(, lacp_aggregator)
165 lsc_aggregators;
166 struct workqueue *lsc_workq;
167 struct lagg_work lsc_work_tick;
168 struct lagg_work lsc_work_rcvdu;
169 struct lagg_work lsc_work_linkspeed;
170 callout_t lsc_tick;
171 pcq_t *lsc_du_q;
172
173 char lsc_evgroup[32];
174 struct evcnt lsc_mgethdr_failed;
175 struct evcnt lsc_mpullup_failed;
176 struct evcnt lsc_badlacpdu;
177 struct evcnt lsc_badmarkerdu;
178 struct evcnt lsc_norcvif;
179 struct evcnt lsc_nolaggport;
180 struct evcnt lsc_duq_nospc;
181
182 bool lsc_optimistic;
183 bool lsc_stop_lacpdu;
184 bool lsc_dump_du;
185 bool lsc_multi_linkspeed;
186 };
187
188 /*
189 * Locking notes:
190 * - Items in struct lacp_softc are protected by
191 * lsc_lock (an adaptive mutex)
192 * - lsc_activemap is protected by pserialize (lsc_psz)
193 * - Items of struct lagg_port in lsc_portmaps are protected by
194 * protected by both pserialize (lsc_psz) and psref (lp_psref)
195 * - Updates for lsc_activemap and lsc_portmaps is serialized by
196 * sc_lock in struct lagg_softc
197 * - Other locking notes are described in if_laggproto.h
198 */
199
200 static void lacp_dprintf(const struct lacp_softc *,
201 const struct lacp_port *, const char *, ...)
202 __attribute__((__format__(__printf__, 3, 4)));
203
204 #ifdef LACP_DEBUG
205 #define LACP_DPRINTF(a) do { lacp_dprintf a; } while (/*CONSTCOND*/ 0)
206 #define LACP_PEERINFO_IDSTR(_pi, _b, _bs) \
207 lacp_peerinfo_idstr(_pi, _b, _bs)
208 #define LACP_STATE_STR(_s, _b, _bs) lacp_state_str(_s, _b, _bs)
209 #define LACP_AGGREGATOR_STR(_a, _b, _bs) \
210 lacp_aggregator_str(_a, _b, _bs)
211 #define __LACPDEBUGUSED
212 #else
213 #define LACP_DPRINTF(a) __nothing
214 #define LACP_PEERINFO_IDSTR(_pi, _b, _bs) __nothing
215 #define LACP_STATE_STR(_s, _b, _bs) __nothing
216 #define LACP_AGGREGATOR_STR(_a, _b, _bs) __nothing
217 #define __LACPDEBUGUSED __unused
218 #endif
219
220 #define LACP_LOCK(_sc) mutex_enter(&(_sc)->lsc_lock)
221 #define LACP_UNLOCK(_sc) mutex_exit(&(_sc)->lsc_lock)
222 #define LACP_LOCKED(_sc) mutex_owned(&(_sc)->lsc_lock)
223 #define LACP_TIMER_ARM(_lacpp, _timer, _val) \
224 (_lacpp)->lp_timer[(_timer)] = (_val)
225 #define LACP_TIMER_DISARM(_lacpp, _timer) \
226 LACP_TIMER_ARM((_lacpp), (_timer), 0)
227 #define LACP_TIMER_ISARMED(_lacpp, _timer) \
228 ((_lacpp)->lp_timer[(_timer)] > 0)
229 #define LACP_PTIMER_ARM(_sc, _timer, _val) \
230 (_sc)->lsc_timer[(_timer)] = (_val)
231 #define LACP_PTIMER_DISARM(_sc, _timer) \
232 LACP_PTIMER_ARM((_sc), (_timer), 0)
233 #define LACP_PTIMER_ISARMED(_sc, _timer) \
234 ((_sc)->lsc_timer[(_timer)] > 0)
235 #define LACP_STATE_EQ(_s1, _s2, _mask) (!ISSET((_s1) ^ (_s2), (_mask)))
236 #define LACP_PORT_XNAME(_lacpp) (_lacpp != NULL) ? \
237 (_lacpp)->lp_laggport->lp_ifp->if_xname : "(unknown)"
238 #define LACP_ISDUMPING(_sc) (_sc)->lsc_dump_du
239 #define LACP_PORTMAP_ACTIVE(_sc) \
240 atomic_load_consume(&(_sc)->lsc_activemap)
241 #define LACP_PORTMAP_NEXT(_sc) \
242 (((LACP_PORTMAP_ACTIVE((_sc))) ^ 0x01) &0x01)
243 #define LACP_SYS_PRI(_la) ntohs((_la)->la_sid.sid_prio)
244 #define LACP_TLV_PARSE(_du, _st, _name, _tlvlist) \
245 tlv_parse(&(_du)->_name, \
246 sizeof(_st) - offsetof(_st, _name), \
247 (_tlvlist))
248
249 static void lacp_tick(void *);
250 static void lacp_tick_work(struct lagg_work *, void *);
251 static void lacp_linkstate(struct lagg_proto_softc *, struct lagg_port *);
252 static void lacp_port_disable(struct lacp_softc *, struct lacp_port *);
253 static void lacp_port_enable(struct lacp_softc *, struct lacp_port *);
254 static void lacp_peerinfo_actor(struct lacp_softc *, struct lacp_port *,
255 struct lacpdu_peerinfo *);
256 static void lacp_peerinfo_partner(struct lacp_port *,
257 struct lacpdu_peerinfo *);
258 static struct lagg_port *
259 lacp_select_tx_port(struct lacp_softc *, struct mbuf *,
260 struct psref *);
261 static void lacp_suppress_distributing(struct lacp_softc *);
262 static void lacp_distributing_timer(struct lacp_softc *);
263
264 static void lacp_select(struct lacp_softc *, struct lacp_port *);
265 static void lacp_unselect(struct lacp_softc *, struct lacp_port *);
266 static void lacp_selected_update(struct lacp_softc *,
267 struct lacp_aggregator *);
268 static void lacp_sm_port_init(struct lacp_softc *,
269 struct lacp_port *, struct lagg_port *);
270 static int lacp_set_mux(struct lacp_softc *,
271 struct lacp_port *, enum lacp_mux_state);
272 static void lacp_sm_mux(struct lacp_softc *, struct lacp_port *);
273 static void lacp_sm_mux_timer(struct lacp_softc *, struct lacp_port *);
274 static void lacp_sm_rx(struct lacp_softc *, struct lacp_port *,
275 struct lacpdu_peerinfo *, struct lacpdu_peerinfo *);
276 static void lacp_sm_rx_set_expired(struct lacp_port *);
277 static void lacp_sm_rx_timer(struct lacp_softc *, struct lacp_port *);
278 static void lacp_sm_rx_record_default(struct lacp_softc *,
279 struct lacp_port *);
280
281 static void lacp_sm_tx(struct lacp_softc *, struct lacp_port *);
282 static void lacp_sm_tx_work(struct lagg_work *, void *);
283 static void lacp_sm_ptx_timer(struct lacp_softc *, struct lacp_port *);
284 static void lacp_sm_ptx_schedule(struct lacp_port *);
285 static void lacp_sm_ptx_update_timeout(struct lacp_port *, uint8_t);
286
287 static void lacp_rcvdu_work(struct lagg_work *, void *);
288 static void lacp_marker_work(struct lagg_work *, void *);
289 static void lacp_linkspeed_work(struct lagg_work *, void *);
290 static void lacp_dump_lacpdutlv(const struct lacpdu_peerinfo *,
291 const struct lacpdu_peerinfo *,
292 const struct lacpdu_collectorinfo *);
293 static void lacp_dump_markertlv(const struct markerdu_info *,
294 const struct markerdu_info *);
295
296 typedef void (*lacp_timer_func_t)(struct lacp_softc *, struct lacp_port *);
297 static const lacp_timer_func_t lacp_timer_funcs[] = {
298 [LACP_TIMER_CURRENT_WHILE] = lacp_sm_rx_timer,
299 [LACP_TIMER_PERIODIC] = lacp_sm_ptx_timer,
300 [LACP_TIMER_WAIT_WHILE] = lacp_sm_mux_timer,
301 };
302 typedef void (*lacp_prototimer_func_t)(struct lacp_softc *);
303 static const lacp_prototimer_func_t lacp_ptimer_funcs[] = {
304 [LACP_PTIMER_DISTRIBUTING] = lacp_distributing_timer,
305 };
306
307 static void
308 lacp_dprintf(const struct lacp_softc *lsc, const struct lacp_port *lacpp,
309 const char *fmt, ...)
310 {
311 struct lagg_softc *sc;
312 va_list va;
313
314 if (lsc != NULL && lsc->lsc_softc != NULL) {
315 sc = lsc->lsc_softc;
316 printf("%s", sc->sc_if.if_xname);
317 } else {
318 printf("lacp");
319 }
320
321 if (lacpp != NULL)
322 printf("(%s)", LACP_PORT_XNAME(lacpp));
323
324 printf(": ");
325
326 va_start(va, fmt);
327 vprintf(fmt, va);
328 va_end(va);
329 }
330
331 static inline void
332 lacp_evcnt_attach(struct lacp_softc *lsc,
333 struct evcnt *ev, const char *name)
334 {
335
336 evcnt_attach_dynamic(ev, EVCNT_TYPE_MISC, NULL,
337 lsc->lsc_evgroup, name);
338 }
339
340 static inline bool
341 lacp_iscollecting(struct lacp_port *lacpp)
342 {
343
344 return atomic_load_relaxed(&lacpp->lp_collector);
345 }
346
347 static inline bool
348 lacp_isdistributing(struct lacp_port *lacpp)
349 {
350
351 return ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_DISTRIBUTING);
352 }
353
354 static inline bool
355 lacp_isactive(struct lacp_softc *lsc, struct lacp_port *lacpp)
356 {
357
358 if (lacpp->lp_selected != LACP_SELECTED)
359 return false;
360
361 if (lacpp->lp_aggregator == NULL)
362 return false;
363
364 if (lacpp->lp_aggregator != lsc->lsc_aggregator)
365 return false;
366
367 return true;
368 }
369
370 static inline void
371 lacp_mcastaddr(struct ifreq *ifr, const char *if_xname)
372 {
373 static const uint8_t addr[ETHER_ADDR_LEN] =
374 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
375
376 memset(ifr, 0, sizeof(*ifr));
377
378 strlcpy(ifr->ifr_name, if_xname,
379 sizeof(ifr->ifr_name));
380 ifr->ifr_addr.sa_len = sizeof(ifr->ifr_addr);
381 ifr->ifr_addr.sa_family = AF_UNSPEC;
382
383 CTASSERT(sizeof(ifr->ifr_addr) >= sizeof(addr));
384 memcpy(&ifr->ifr_addr.sa_data, addr, sizeof(addr));
385 }
386
387 static inline u_int
388 lacp_portmap_linkstate(struct lacp_portmap *pm)
389 {
390
391 if (pm->pm_count == 0)
392 return LINK_STATE_DOWN;
393
394 return LINK_STATE_UP;
395 }
396
397 static inline struct lacp_port *
398 lacp_port_priority_max(struct lacp_port *a, struct lacp_port *b)
399 {
400 uint16_t pri_a, pri_b;
401
402 pri_a = ntohs(a->lp_actor.lpi_portprio);
403 pri_b = ntohs(b->lp_actor.lpi_portprio);
404
405 if (pri_a < pri_b)
406 return a;
407 if (pri_b < pri_a)
408 return b;
409
410 pri_a = ntohs(a->lp_partner.lpi_portprio);
411 pri_b = ntohs(b->lp_partner.lpi_portprio);
412
413 if (pri_a < pri_b)
414 return a;
415 if (pri_b < pri_a)
416 return b;
417
418 if (a->lp_linkspeed > b->lp_linkspeed)
419 return a;
420 if (b->lp_linkspeed > a->lp_linkspeed)
421 return b;
422
423 return a;
424 }
425
426 static void
427 tlv_parse(void *vp, size_t len, struct tlv *list)
428 {
429 struct tlvhdr *th;
430 uint8_t *p;
431 size_t l, i;
432
433 th = (struct tlvhdr *)vp;
434 p = (uint8_t *)vp;
435
436 for (l = 0; l < len; l += th->tlv_length) {
437 th = (struct tlvhdr *)(p + l);
438
439 if (th->tlv_type == TLV_TYPE_TERMINATE)
440 break;
441
442 if (th->tlv_length <= 0)
443 break;
444
445 for (i = 0; list[i].tlv_t != TLV_TYPE_TERMINATE; i++) {
446 if (th->tlv_type != list[i].tlv_t)
447 continue;
448
449 if (th->tlv_length - sizeof(*th) != list[i].tlv_l)
450 break;
451
452 if (list[i].tlv_v == NULL) {
453 list[i].tlv_v =
454 (void *)((uint8_t *)th + sizeof(*th));
455 }
456
457 break;
458 }
459 }
460 }
461
462 int
463 lacp_attach(struct lagg_softc *sc, struct lagg_proto_softc **lscp)
464 {
465 struct lacp_softc *lsc;
466 char xnamebuf[MAXCOMLEN];
467 int error;
468
469 KASSERT(LAGG_LOCKED(sc));
470
471 lsc = kmem_zalloc(sizeof(*lsc), KM_NOSLEEP);
472 if (lsc == NULL)
473 return ENOMEM;
474
475 lsc->lsc_du_q = pcq_create(LACP_RCVDU_LIMIT, KM_NOSLEEP);
476 if (lsc->lsc_du_q == NULL) {
477 error = ENOMEM;
478 goto free_lsc;
479 }
480
481 mutex_init(&lsc->lsc_lock, MUTEX_DEFAULT, IPL_SOFTNET);
482 lsc->lsc_softc = sc;
483 lsc->lsc_key = htons(if_get_index(&sc->sc_if));
484 lsc->lsc_system_prio = htons(LACP_SYSTEM_PRIO);
485 lsc->lsc_running = false;
486 lsc->lsc_max_ports = LACP_MAX_PORTS;
487 lsc->lsc_multi_linkspeed = true;
488 TAILQ_INIT(&lsc->lsc_aggregators);
489
490 lagg_work_set(&lsc->lsc_work_tick, lacp_tick_work, lsc);
491 lagg_work_set(&lsc->lsc_work_rcvdu, lacp_rcvdu_work, lsc);
492 lagg_work_set(&lsc->lsc_work_linkspeed,
493 lacp_linkspeed_work, lsc);
494
495 snprintf(xnamebuf, sizeof(xnamebuf), "%s.lacp",
496 sc->sc_if.if_xname);
497 lsc->lsc_workq = lagg_workq_create(xnamebuf,
498 PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
499 if (lsc->lsc_workq == NULL) {
500 LAGG_LOG(sc, LOG_ERR, "workqueue create failed\n");
501 error = ENOMEM;
502 goto destroy_lock;
503 }
504
505 lsc->lsc_psz = pserialize_create();
506
507 callout_init(&lsc->lsc_tick, CALLOUT_MPSAFE);
508 callout_setfunc(&lsc->lsc_tick, lacp_tick, lsc);
509
510 snprintf(lsc->lsc_evgroup, sizeof(lsc->lsc_evgroup),
511 "%s-lacp", sc->sc_if.if_xname);
512 lacp_evcnt_attach(lsc, &lsc->lsc_mgethdr_failed, "MGETHDR failed");
513 lacp_evcnt_attach(lsc, &lsc->lsc_mpullup_failed, "m_pullup failed");
514 lacp_evcnt_attach(lsc, &lsc->lsc_badlacpdu, "Bad LACPDU received");
515 lacp_evcnt_attach(lsc, &lsc->lsc_badmarkerdu, "Bad MarkerDU received");
516 lacp_evcnt_attach(lsc, &lsc->lsc_norcvif, "No received interface");
517 lacp_evcnt_attach(lsc, &lsc->lsc_nolaggport, "No lagg context");
518 lacp_evcnt_attach(lsc, &lsc->lsc_duq_nospc, "No space left on queues");
519
520 if_link_state_change(&sc->sc_if, LINK_STATE_DOWN);
521
522 *lscp = (struct lagg_proto_softc *)lsc;
523 return 0;
524 destroy_lock:
525 mutex_destroy(&lsc->lsc_lock);
526 pcq_destroy(lsc->lsc_du_q);
527 free_lsc:
528 kmem_free(lsc, sizeof(*lsc));
529
530 return error;
531 }
532
533 void
534 lacp_detach(struct lagg_proto_softc *xlsc)
535 {
536 struct lacp_softc *lsc = (struct lacp_softc *)xlsc;
537 struct lagg_softc *sc __diagused = lsc->lsc_softc;
538
539 KASSERT(TAILQ_EMPTY(&lsc->lsc_aggregators));
540 KASSERT(SIMPLEQ_EMPTY(&sc->sc_ports));
541
542 LAGG_LOCK(lsc->lsc_softc);
543 lacp_down(xlsc);
544 LAGG_UNLOCK(lsc->lsc_softc);
545
546 lagg_workq_wait(lsc->lsc_workq, &lsc->lsc_work_rcvdu);
547 evcnt_detach(&lsc->lsc_mgethdr_failed);
548 evcnt_detach(&lsc->lsc_mpullup_failed);
549 evcnt_detach(&lsc->lsc_badlacpdu);
550 evcnt_detach(&lsc->lsc_badmarkerdu);
551 evcnt_detach(&lsc->lsc_norcvif);
552 evcnt_detach(&lsc->lsc_nolaggport);
553 evcnt_detach(&lsc->lsc_duq_nospc);
554 lagg_workq_destroy(lsc->lsc_workq);
555 pserialize_destroy(lsc->lsc_psz);
556 mutex_destroy(&lsc->lsc_lock);
557 pcq_destroy(lsc->lsc_du_q);
558 kmem_free(lsc, sizeof(*lsc));
559 }
560
561 int
562 lacp_up(struct lagg_proto_softc *xlsc)
563 {
564 struct lagg_softc *sc;
565 struct lagg_port *lp;
566 struct lacp_softc *lsc;
567
568 lsc = (struct lacp_softc *)xlsc;
569 sc = lsc->lsc_softc;
570
571 KASSERT(LAGG_LOCKED(sc));
572
573 LACP_LOCK(lsc);
574 if (memcmp(lsc->lsc_system_mac, LAGG_CLLADDR(sc),
575 sizeof(lsc->lsc_system_mac)) != 0) {
576 memcpy(lsc->lsc_system_mac, LAGG_CLLADDR(sc),
577 sizeof(lsc->lsc_system_mac));
578 }
579 lsc->lsc_running = true;
580 callout_schedule(&lsc->lsc_tick, hz);
581 LACP_UNLOCK(lsc);
582
583 LAGG_PORTS_FOREACH(sc, lp) {
584 lacp_linkstate(xlsc, lp);
585 }
586
587 LACP_DPRINTF((lsc, NULL, "lacp start\n"));
588
589 return 0;
590 }
591
592 static void
593 lacp_down_locked(struct lacp_softc *lsc)
594 {
595 struct lagg_softc *sc;
596 struct lagg_port *lp;
597
598 sc = lsc->lsc_softc;
599
600 KASSERT(LAGG_LOCKED(sc));
601 KASSERT(LACP_LOCKED(lsc));
602
603 lsc->lsc_running = false;
604 callout_halt(&lsc->lsc_tick, &lsc->lsc_lock);
605
606 LAGG_PORTS_FOREACH(sc, lp) {
607 lacp_port_disable(lsc, lp->lp_proto_ctx);
608 }
609
610 memset(lsc->lsc_system_mac, 0,
611 sizeof(lsc->lsc_system_mac));
612
613 LACP_DPRINTF((lsc, NULL, "lacp stopped\n"));
614 }
615
616 void
617 lacp_down(struct lagg_proto_softc *xlsc)
618 {
619 struct lacp_softc *lsc;
620
621 lsc = (struct lacp_softc *)xlsc;
622
623 KASSERT(LAGG_LOCKED(lsc->lsc_softc));
624
625 LACP_LOCK(lsc);
626 lacp_down_locked(lsc);
627 LACP_UNLOCK(lsc);
628 }
629
630 int
631 lacp_transmit(struct lagg_proto_softc *xlsc, struct mbuf *m)
632 {
633 struct lacp_softc *lsc;
634 struct lagg_port *lp;
635 struct ifnet *ifp;
636 struct psref psref;
637
638 lsc = (struct lacp_softc *)xlsc;
639
640 if (__predict_false(lsc->lsc_suppress_distributing)) {
641 LACP_DPRINTF((lsc, NULL, "waiting transit\n"));
642 m_freem(m);
643 return EBUSY;
644 }
645
646 lp = lacp_select_tx_port(lsc, m, &psref);
647 if (__predict_false(lp == NULL)) {
648 LACP_DPRINTF((lsc, NULL, "no distributing port\n"));
649 ifp = &lsc->lsc_softc->sc_if;
650 if_statinc(ifp, if_oerrors);
651 m_freem(m);
652 return ENOENT;
653 }
654
655 lagg_output(lsc->lsc_softc, lp, m);
656 lagg_port_putref(lp, &psref);
657
658 return 0;
659 }
660
661 int
662 lacp_allocport(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
663 {
664 struct lagg_softc *sc;
665 struct lacp_softc *lsc;
666 struct lacp_port *lacpp;
667 struct ifreq ifr;
668 bool added_multi;
669 int error;
670
671 lsc = (struct lacp_softc *)xlsc;
672 sc = lsc->lsc_softc;
673
674 KASSERT(LAGG_LOCKED(sc));
675 KASSERT(IFNET_LOCKED(lp->lp_ifp));
676
677 lacp_mcastaddr(&ifr, lp->lp_ifp->if_xname);
678 error = lp->lp_ioctl(lp->lp_ifp, SIOCADDMULTI, (void *)&ifr);
679
680 switch (error) {
681 case 0:
682 added_multi = true;
683 break;
684 case EAFNOSUPPORT:
685 added_multi = false;
686 break;
687 default:
688 LAGG_LOG(sc, LOG_ERR, "SIOCADDMULTI failed on %s\n",
689 lp->lp_ifp->if_xname);
690 return error;
691 }
692
693 lacpp = kmem_zalloc(sizeof(*lacpp), KM_NOSLEEP);
694 if (lacpp == NULL)
695 return ENOMEM;
696
697 lacpp->lp_added_multi = added_multi;
698 lagg_work_set(&lacpp->lp_work_smtx, lacp_sm_tx_work, lsc);
699 lagg_work_set(&lacpp->lp_work_marker, lacp_marker_work, lsc);
700
701 LACP_LOCK(lsc);
702 lacp_sm_port_init(lsc, lacpp, lp);
703 LACP_UNLOCK(lsc);
704
705 lp->lp_proto_ctx = (void *)lacpp;
706 lp->lp_prio = ntohs(lacpp->lp_actor.lpi_portprio);
707
708 return 0;
709 }
710
711 void
712 lacp_startport(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
713 {
714 struct lacp_port *lacpp;
715 uint16_t prio;
716
717 lacpp = lp->lp_proto_ctx;
718
719 prio = (uint16_t)MIN(lp->lp_prio, UINT16_MAX);
720 lacpp->lp_actor.lpi_portprio = htons(prio);
721
722 lacp_linkstate(xlsc, lp);
723 }
724
725 void
726 lacp_stopport(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
727 {
728 struct lacp_softc *lsc;
729 struct lacp_port *lacpp;
730 int i;
731
732 lsc = (struct lacp_softc *)xlsc;
733 lacpp = lp->lp_proto_ctx;
734
735 KASSERT(LAGG_LOCKED(lsc->lsc_softc));
736
737 LACP_LOCK(lsc);
738 for (i = 0; i < LACP_NTIMER; i++) {
739 LACP_TIMER_DISARM(lacpp, i);
740 }
741
742 lacp_port_disable(lsc, lacpp);
743 LACP_UNLOCK(lsc);
744 }
745
746 void
747 lacp_freeport(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
748 {
749 struct lacp_softc *lsc;
750 struct lacp_port *lacpp;
751 struct ifreq ifr;
752
753 lsc = (struct lacp_softc *)xlsc;
754 lacpp = lp->lp_proto_ctx;
755
756 KASSERT(LAGG_LOCKED(lsc->lsc_softc));
757 KASSERT(IFNET_LOCKED(lp->lp_ifp));
758
759 lagg_workq_wait(lsc->lsc_workq, &lacpp->lp_work_smtx);
760 lagg_workq_wait(lsc->lsc_workq, &lacpp->lp_work_marker);
761
762 if (lacpp->lp_added_multi) {
763 lacp_mcastaddr(&ifr, LACP_PORT_XNAME(lacpp));
764
765 (void)lp->lp_ioctl(lp->lp_ifp, SIOCDELMULTI, (void *)&ifr);
766 }
767
768 lp->lp_proto_ctx = NULL;
769 kmem_free(lacpp, sizeof(*lacpp));
770 }
771
772 void
773 lacp_protostat(struct lagg_proto_softc *xlsc, struct laggreqproto *resp)
774 {
775 struct laggreq_lacp *rplacp;
776 struct lacp_softc *lsc;
777 struct lacp_aggregator *la;
778 struct lacp_aggregator_systemid *sid;
779
780 lsc = (struct lacp_softc *)xlsc;
781
782 LACP_LOCK(lsc);
783 la = lsc->lsc_aggregator;
784 rplacp = &resp->rp_lacp;
785
786 if (lsc->lsc_optimistic)
787 SET(rplacp->flags, LAGGREQLACP_OPTIMISTIC);
788 if (lsc->lsc_dump_du)
789 SET(rplacp->flags, LAGGREQLACP_DUMPDU);
790 if (lsc->lsc_stop_lacpdu)
791 SET(rplacp->flags, LAGGREQLACP_STOPDU);
792 if (lsc->lsc_multi_linkspeed)
793 SET(rplacp->flags, LAGGREQLACP_MULTILS);
794
795 rplacp->maxports = lsc->lsc_max_ports;
796 rplacp->actor_prio = ntohs(lsc->lsc_system_prio);
797 memcpy(rplacp->actor_mac, lsc->lsc_system_mac,
798 sizeof(rplacp->actor_mac));
799 rplacp->actor_key = ntohs(lsc->lsc_key);
800
801 if (la != NULL) {
802 sid = &la->la_sid;
803 rplacp->partner_prio = ntohs(sid->sid_prio);
804 memcpy(rplacp->partner_mac, sid->sid_mac,
805 sizeof(rplacp->partner_mac));
806 rplacp->partner_key = ntohs(sid->sid_key);
807 }
808 LACP_UNLOCK(lsc);
809 }
810
811 void
812 lacp_portstat(struct lagg_proto_softc *xlsc, struct lagg_port *lp,
813 struct laggreqport *resp)
814 {
815 struct laggreq_lacpport *llp;
816 struct lacp_softc *lsc;
817 struct lacp_port *lacpp;
818 struct lacp_aggregator *la;
819 struct lacp_aggregator_systemid *sid;
820
821 lsc = (struct lacp_softc *)xlsc;
822 lacpp = lp->lp_proto_ctx;
823 la = lacpp->lp_aggregator;
824 llp = &resp->rp_lacpport;
825
826 if (lacp_isactive(lsc, lacpp))
827 SET(resp->rp_flags, LAGG_PORT_ACTIVE);
828 if (lacp_iscollecting(lacpp))
829 SET(resp->rp_flags, LAGG_PORT_COLLECTING);
830 if (lacp_isdistributing(lacpp))
831 SET(resp->rp_flags, LAGG_PORT_DISTRIBUTING);
832 if (lacpp->lp_selected == LACP_STANDBY)
833 SET(resp->rp_flags, LAGG_PORT_STANDBY);
834
835 if (la != NULL) {
836 sid = &la->la_sid;
837 llp->partner_prio = ntohs(sid->sid_prio);
838 memcpy(llp->partner_mac, sid->sid_mac,
839 sizeof(llp->partner_mac));
840 llp->partner_key = ntohs(sid->sid_key);
841 }
842
843 llp->actor_portprio = ntohs(lacpp->lp_actor.lpi_portprio);
844 llp->actor_portno = ntohs(lacpp->lp_actor.lpi_portno);
845 llp->actor_state = lacpp->lp_actor.lpi_state;
846
847 llp->partner_portprio = ntohs(lacpp->lp_partner.lpi_portprio);
848 llp->partner_portno = ntohs(lacpp->lp_partner.lpi_portno);
849 llp->partner_state = lacpp->lp_partner.lpi_state;
850 }
851
852 void
853 lacp_linkstate_ifnet_locked(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
854 {
855 struct lacp_softc *lsc;
856 struct lacp_port *lacpp;
857 struct ifmediareq ifmr;
858 struct ifnet *ifp_port;
859 uint8_t old_state;
860 uint64_t old_linkspeed, new_linkspeed;
861 int error;
862
863 KASSERT(IFNET_LOCKED(lp->lp_ifp));
864
865 lsc = (struct lacp_softc *)xlsc;
866
867 ifp_port = lp->lp_ifp;
868 lacpp = lp->lp_proto_ctx;
869
870 memset(&ifmr, 0, sizeof(ifmr));
871 ifmr.ifm_count = 0;
872 error = if_ioctl(ifp_port, SIOCGIFMEDIA, (void *)&ifmr);
873 if (error == 0) {
874 new_linkspeed = ifmedia_baudrate(ifmr.ifm_active);
875 #ifdef LACP_NOFDX
876 /*
877 * some driver that has no media, e.g. vioif(4),
878 * returns (IFM_ETHER | IFM_AUTO)
879 */
880 if ((ifmr.ifm_active & ~(IFM_ETHER | IFM_AUTO)) == 0)
881 ifmr.ifm_active |= IFM_FDX;
882 #endif
883 } else if (error == ENOTTY) {
884 ifmr.ifm_active = IFM_FDX | IF_Gbps(0);
885 new_linkspeed = 0;
886 } else {
887 LACP_DPRINTF((lsc, lacpp,
888 "SIOCGIFMEDIA failed (%d)\n", error));
889 return;
890 }
891
892 LACP_LOCK(lsc);
893 if (lsc->lsc_running) {
894 old_linkspeed = lacpp->lp_linkspeed;
895 old_state = lacpp->lp_actor.lpi_state;
896
897 if (new_linkspeed != old_linkspeed) {
898 LACP_DPRINTF((lsc, lacpp,
899 "linkspeed changed %"PRIu64" -> %"PRIu64"\n",
900 old_linkspeed, new_linkspeed));
901 lacpp->lp_linkspeed = new_linkspeed;
902 }
903
904 if (ISSET(ifmr.ifm_active, IFM_FDX) &&
905 ISSET(ifp_port->if_flags, IFF_RUNNING) &&
906 ifp_port->if_link_state != LINK_STATE_DOWN) {
907 lacp_port_enable(lsc, lacpp);
908 } else {
909 LACP_DPRINTF((lsc, lacpp,
910 "FDX=%d, RUNNING=%d, link=%d\n",
911 ISSET(ifmr.ifm_active, IFM_FDX) != 0,
912 ISSET(ifp_port->if_flags, IFF_RUNNING) != 0,
913 ifp_port->if_link_state != LINK_STATE_DOWN));
914 lacp_port_disable(lsc, lacpp);
915 }
916
917 if (old_state != lacpp->lp_actor.lpi_state ||
918 old_linkspeed != new_linkspeed) {
919 LACP_DPRINTF((lsc, lacpp,
920 "state changed to UNSELECTED\n"));
921 lacpp->lp_selected = LACP_UNSELECTED;
922 }
923 } else {
924 LACP_DPRINTF((lsc, lacpp,
925 "LACP is inactive, skip linkstate\n"));
926 }
927 LACP_UNLOCK(lsc);
928 }
929
930 int
931 lacp_ioctl(struct lagg_proto_softc *xlsc, struct laggreqproto *lreq)
932 {
933 struct lacp_softc *lsc;
934 struct laggreq_lacp *rplacp;
935 struct lacp_aggregator *la;
936 int error;
937 size_t maxports;
938 bool set;
939
940 lsc = (struct lacp_softc *)xlsc;
941 rplacp = &lreq->rp_lacp;
942 error = 0;
943
944 switch (rplacp->command) {
945 case LAGGIOC_LACPSETFLAGS:
946 case LAGGIOC_LACPCLRFLAGS:
947 set = (rplacp->command == LAGGIOC_LACPSETFLAGS) ?
948 true : false;
949
950 LACP_LOCK(lsc);
951
952 if (ISSET(rplacp->flags, LAGGREQLACP_OPTIMISTIC))
953 lsc->lsc_optimistic = set;
954 if (ISSET(rplacp->flags, LAGGREQLACP_DUMPDU))
955 lsc->lsc_dump_du = set;
956 if (ISSET(rplacp->flags, LAGGREQLACP_STOPDU))
957 lsc->lsc_stop_lacpdu = set;
958
959 if (ISSET(rplacp->flags, LAGGREQLACP_MULTILS) &&
960 lsc->lsc_multi_linkspeed != set) {
961 lsc->lsc_multi_linkspeed = set;
962 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
963 lacp_selected_update(lsc, la);
964 }
965 }
966
967 LACP_UNLOCK(lsc);
968 break;
969 case LAGGIOC_LACPSETMAXPORTS:
970 case LAGGIOC_LACPCLRMAXPORTS:
971 maxports = (rplacp->command == LAGGIOC_LACPSETMAXPORTS) ?
972 rplacp->maxports : LACP_MAX_PORTS;
973 if (0 == maxports || LACP_MAX_PORTS < maxports) {
974 error = ERANGE;
975 break;
976 }
977
978 LACP_LOCK(lsc);
979 if (lsc->lsc_max_ports != maxports) {
980 lsc->lsc_max_ports = maxports;
981 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
982 lacp_selected_update(lsc, la);
983 }
984 }
985 LACP_UNLOCK(lsc);
986 break;
987 default:
988 error = ENOTTY;
989 }
990
991 return error;
992 }
993
994 static int
995 lacp_pdu_input(struct lacp_softc *lsc, struct lacp_port *lacpp, struct mbuf *m)
996 {
997 enum {
998 LACP_TLV_ACTOR = 0,
999 LACP_TLV_PARTNER,
1000 LACP_TLV_COLLECTOR,
1001 LACP_TLV_TERM,
1002 LACP_TLV_NUM
1003 };
1004
1005 struct lacpdu *du;
1006 struct lacpdu_peerinfo *pi_actor, *pi_partner;
1007 struct lacpdu_collectorinfo *lci;
1008 struct tlv tlvlist_lacp[LACP_TLV_NUM] = {
1009 [LACP_TLV_ACTOR] = {
1010 .tlv_t = LACP_TYPE_ACTORINFO,
1011 .tlv_l = sizeof(*pi_actor)},
1012 [LACP_TLV_PARTNER] = {
1013 .tlv_t = LACP_TYPE_PARTNERINFO,
1014 .tlv_l = sizeof(*pi_partner)},
1015 [LACP_TLV_COLLECTOR] = {
1016 .tlv_t = LACP_TYPE_COLLECTORINFO,
1017 .tlv_l = sizeof(*lci)},
1018 [LACP_TLV_TERM] = {
1019 .tlv_t = TLV_TYPE_TERMINATE,
1020 .tlv_l = 0},
1021 };
1022
1023 if (m->m_pkthdr.len != sizeof(*du))
1024 goto bad;
1025
1026 if (m->m_len < (int)sizeof(*du)) {
1027 m = m_pullup(m, sizeof(*du));
1028 if (m == NULL) {
1029 lsc->lsc_mpullup_failed.ev_count++;
1030 return ENOMEM;
1031 }
1032 }
1033
1034 du = mtod(m, struct lacpdu *);
1035
1036 if (memcmp(&du->ldu_eh.ether_dhost,
1037 ethermulticastaddr_slowprotocols, ETHER_ADDR_LEN) != 0)
1038 goto bad;
1039
1040 LACP_TLV_PARSE(du, struct lacpdu, ldu_tlv_actor,
1041 tlvlist_lacp);
1042
1043 pi_actor = tlvlist_lacp[LACP_TLV_ACTOR].tlv_v;
1044 pi_partner = tlvlist_lacp[LACP_TLV_PARTNER].tlv_v;
1045 lci = tlvlist_lacp[LACP_TLV_COLLECTOR].tlv_v;
1046
1047 if (pi_actor == NULL || pi_partner == NULL)
1048 goto bad;
1049
1050 if (LACP_ISDUMPING(lsc)) {
1051 lacp_dprintf(lsc, lacpp, "lacpdu received\n");
1052 lacp_dump_lacpdutlv(pi_actor, pi_partner, lci);
1053 }
1054
1055 LACP_LOCK(lsc);
1056 lacp_sm_rx(lsc, lacpp, pi_partner, pi_actor);
1057 LACP_UNLOCK(lsc);
1058
1059 m_freem(m);
1060 return 0;
1061 bad:
1062 lsc->lsc_badlacpdu.ev_count++;
1063 m_freem(m);
1064 return EINVAL;
1065 }
1066
1067 static int
1068 marker_cmp(struct markerdu_info *mi,
1069 struct lacp_softc *lsc, struct lacp_port *lacpp)
1070 {
1071
1072 KASSERT(LACP_LOCKED(lsc));
1073
1074 if (mi->mi_rq_port != lacpp->lp_actor.lpi_portno)
1075 return -1;
1076
1077 if (ntohl(mi->mi_rq_xid) != lacpp->lp_marker_xid)
1078 return -1;
1079
1080 return memcmp(mi->mi_rq_system, lsc->lsc_system_mac,
1081 LACP_MAC_LEN);
1082 }
1083
1084 static void
1085 lacp_marker_reply(struct lacp_softc *lsc, struct lacp_port *lacpp,
1086 struct mbuf *m_info)
1087 {
1088 struct lagg_port *lp;
1089 struct markerdu *mdu;
1090 struct ifnet *ifp_port;
1091 struct psref psref;
1092
1093 LACP_LOCK(lsc);
1094 lp = lacpp->lp_laggport;
1095 lagg_port_getref(lp, &psref);
1096 LACP_UNLOCK(lsc);
1097
1098 ifp_port = lp->lp_ifp;
1099 mdu = mtod(m_info, struct markerdu *);
1100
1101 mdu->mdu_tlv_info.tlv_type = MARKER_TYPE_RESPONSE;
1102 /* ether_dhost is already ethermulticastaddr_slowprotocols */
1103 m_info->m_flags |= M_MCAST;
1104 memcpy(mdu->mdu_eh.ether_shost,
1105 CLLADDR(ifp_port->if_sadl), ETHER_ADDR_LEN);
1106
1107 if (LACP_ISDUMPING(lsc)) {
1108 lacp_dprintf(lsc, lacpp, "markerdu reply\n");
1109 lacp_dump_markertlv(NULL, &mdu->mdu_info);
1110 }
1111
1112 lagg_port_xmit(lp, m_info);
1113 lagg_port_putref(lp, &psref);
1114 }
1115
1116 static int
1117 lacp_marker_recv_response(struct lacp_softc *lsc, struct lacp_port *lacpp,
1118 struct markerdu_info *mi_res)
1119 {
1120 struct lagg_softc *sc;
1121 struct lagg_port *lp0;
1122 struct lacp_port *lacpp0;
1123 bool pending;
1124
1125 sc = lsc->lsc_softc;
1126
1127 LACP_LOCK(lsc);
1128 if (marker_cmp(mi_res, lsc, lacpp) != 0) {
1129 LACP_UNLOCK(lsc);
1130 return -1;
1131 }
1132 CLR(lacpp->lp_flags, LACP_PORT_MARK);
1133 LACP_UNLOCK(lsc);
1134
1135 LAGG_LOCK(sc);
1136 LACP_LOCK(lsc);
1137
1138 if (lsc->lsc_suppress_distributing) {
1139 pending = false;
1140 LAGG_PORTS_FOREACH(sc, lp0) {
1141 lacpp0 = lp0->lp_proto_ctx;
1142 if (ISSET(lacpp0->lp_flags, LACP_PORT_MARK)) {
1143 pending = true;
1144 break;
1145 }
1146 }
1147
1148 if (!pending) {
1149 LACP_DPRINTF((lsc, NULL, "queue flush complete\n"));
1150 LACP_PTIMER_DISARM(lsc, LACP_PTIMER_DISTRIBUTING);
1151 lsc->lsc_suppress_distributing = false;
1152 }
1153 }
1154
1155 LACP_UNLOCK(lsc);
1156 LAGG_UNLOCK(sc);
1157
1158 return 0;
1159 }
1160
1161 static int
1162 lacp_marker_input(struct lacp_softc *lsc, struct lacp_port *lacpp,
1163 struct mbuf *m)
1164 {
1165 enum {
1166 MARKER_TLV_INFO = 0,
1167 MARKER_TLV_RESPONSE,
1168 MARKER_TLV_TERM,
1169 MARKER_TLV_NUM
1170 };
1171
1172 struct markerdu *mdu;
1173 struct markerdu_info *mi_info, *mi_res;
1174 int error;
1175 struct tlv tlvlist_marker[MARKER_TLV_NUM] = {
1176 [MARKER_TLV_INFO] = {
1177 .tlv_t = MARKER_TYPE_INFO,
1178 .tlv_l = sizeof(*mi_info)},
1179 [MARKER_TLV_RESPONSE] = {
1180 .tlv_t = MARKER_TYPE_RESPONSE,
1181 .tlv_l = sizeof(*mi_res)},
1182 [MARKER_TLV_TERM] = {
1183 .tlv_t = TLV_TYPE_TERMINATE,
1184 .tlv_l = 0},
1185 };
1186
1187 if (m->m_pkthdr.len != sizeof(*mdu))
1188 goto bad;
1189
1190 if (m->m_len < (int)sizeof(*mdu)) {
1191 m = m_pullup(m, sizeof(*mdu));
1192 if (m == NULL) {
1193 lsc->lsc_mpullup_failed.ev_count++;
1194 return ENOMEM;
1195 }
1196 }
1197
1198 mdu = mtod(m, struct markerdu *);
1199
1200 if (memcmp(mdu->mdu_eh.ether_dhost,
1201 ethermulticastaddr_slowprotocols, ETHER_ADDR_LEN) != 0)
1202 goto bad;
1203
1204 LACP_TLV_PARSE(mdu, struct markerdu, mdu_tlv_info,
1205 tlvlist_marker);
1206
1207 mi_info = tlvlist_marker[MARKER_TLV_INFO].tlv_v;
1208 mi_res = tlvlist_marker[MARKER_TLV_RESPONSE].tlv_v;
1209
1210 if (LACP_ISDUMPING(lsc)) {
1211 lacp_dprintf(lsc, lacpp, "markerdu received\n");
1212 lacp_dump_markertlv(mi_info, mi_res);
1213 }
1214
1215 if (mi_info != NULL && mi_res == NULL) {
1216 lacp_marker_reply(lsc, lacpp, m);
1217 } else if (mi_info == NULL && mi_res != NULL) {
1218 error = lacp_marker_recv_response(lsc, lacpp,
1219 mi_res);
1220 if (error != 0) {
1221 goto bad;
1222 } else {
1223 m_freem(m);
1224 }
1225 } else {
1226 goto bad;
1227 }
1228
1229 return 0;
1230 bad:
1231 lsc->lsc_badmarkerdu.ev_count++;
1232 m_freem(m);
1233 return EINVAL;
1234 }
1235
1236 struct mbuf *
1237 lacp_input(struct lagg_proto_softc *xlsc, struct lagg_port *lp, struct mbuf *m)
1238 {
1239 struct ifnet *ifp;
1240 struct lacp_softc *lsc;
1241 struct lacp_port *lacpp;
1242 struct ether_header *eh;
1243 uint8_t subtype;
1244
1245 eh = mtod(m, struct ether_header *);
1246 lsc = (struct lacp_softc *)xlsc;
1247 ifp = &lsc->lsc_softc->sc_if;
1248 lacpp = lp->lp_proto_ctx;
1249
1250 if (!vlan_has_tag(m) &&
1251 eh->ether_type == htons(ETHERTYPE_SLOWPROTOCOLS)) {
1252 if (m->m_pkthdr.len < (int)(sizeof(*eh) + sizeof(subtype))) {
1253 m_freem(m);
1254 return NULL;
1255 }
1256
1257 m_copydata(m, sizeof(struct ether_header),
1258 sizeof(subtype), &subtype);
1259
1260 switch (subtype) {
1261 case SLOWPROTOCOLS_SUBTYPE_LACP:
1262 case SLOWPROTOCOLS_SUBTYPE_MARKER:
1263 if (pcq_put(lsc->lsc_du_q, (void *)m)) {
1264 lagg_workq_add(lsc->lsc_workq,
1265 &lsc->lsc_work_rcvdu);
1266 } else {
1267 m_freem(m);
1268 lsc->lsc_duq_nospc.ev_count++;
1269 }
1270 return NULL;
1271 }
1272 }
1273
1274 if (!lacp_iscollecting(lacpp) || !lacp_isactive(lsc, lacpp)) {
1275 if_statinc(ifp, if_ierrors);
1276 m_freem(m);
1277 return NULL;
1278 }
1279
1280 return m;
1281 }
1282
1283 static void
1284 lacp_rcvdu_work(struct lagg_work *lw __unused, void *xlsc)
1285 {
1286 struct lacp_softc *lsc = (struct lacp_softc *)xlsc;
1287 struct ifnet *ifp;
1288 struct psref psref_lp;
1289 struct lagg_port *lp;
1290 struct mbuf *m;
1291 uint8_t subtype;
1292 int bound, s;
1293
1294 bound = curlwp_bind();
1295
1296 for (;;) {
1297 m = pcq_get(lsc->lsc_du_q);
1298 if (m == NULL)
1299 break;
1300
1301 ifp = m_get_rcvif(m, &s);
1302 if (ifp == NULL) {
1303 m_freem(m);
1304 lsc->lsc_norcvif.ev_count++;
1305 continue;
1306 }
1307
1308 lp = atomic_load_consume(&ifp->if_lagg);
1309 if (lp == NULL) {
1310 m_put_rcvif(ifp, &s);
1311 m_freem(m);
1312 lsc->lsc_norcvif.ev_count++;
1313 continue;
1314 }
1315
1316 lagg_port_getref(lp, &psref_lp);
1317 m_put_rcvif(ifp, &s);
1318
1319 m_copydata(m, sizeof(struct ether_header),
1320 sizeof(subtype), &subtype);
1321
1322 switch (subtype) {
1323 case SLOWPROTOCOLS_SUBTYPE_LACP:
1324 (void)lacp_pdu_input(lsc,
1325 lp->lp_proto_ctx, m);
1326 break;
1327 case SLOWPROTOCOLS_SUBTYPE_MARKER:
1328 (void)lacp_marker_input(lsc,
1329 lp->lp_proto_ctx, m);
1330 break;
1331 }
1332
1333 lagg_port_putref(lp, &psref_lp);
1334 }
1335
1336 curlwp_bindx(bound);
1337 }
1338
1339 static bool
1340 lacp_port_need_to_tell(struct lacp_port *lacpp)
1341 {
1342
1343 if (!ISSET(lacpp->lp_actor.lpi_state,
1344 LACP_STATE_AGGREGATION)) {
1345 return false;
1346 }
1347
1348 if (!ISSET(lacpp->lp_actor.lpi_state,
1349 LACP_STATE_ACTIVITY)
1350 && !ISSET(lacpp->lp_partner.lpi_state,
1351 LACP_STATE_ACTIVITY)) {
1352 return false;
1353 }
1354
1355 if (!ISSET(lacpp->lp_flags, LACP_PORT_NTT))
1356 return false;
1357
1358 if (ppsratecheck(&lacpp->lp_last_lacpdu, &lacpp->lp_lacpdu_sent,
1359 (LACP_SENDDU_PPS / LACP_FAST_PERIODIC_TIME)) == 0)
1360 return false;
1361
1362 return true;
1363 }
1364
1365 static void
1366 lacp_sm_assert_ntt(struct lacp_port *lacpp)
1367 {
1368
1369 SET(lacpp->lp_flags, LACP_PORT_NTT);
1370 }
1371
1372 static void
1373 lacp_sm_negate_ntt(struct lacp_port *lacpp)
1374 {
1375
1376 CLR(lacpp->lp_flags, LACP_PORT_NTT);
1377 }
1378
1379 static struct mbuf *
1380 lacp_lacpdu_mbuf(struct lacp_softc *lsc, struct lacp_port *lacpp)
1381 {
1382 struct ifnet *ifp_port;
1383 struct mbuf *m;
1384 struct lacpdu *du;
1385
1386 KASSERT(LACP_LOCKED(lsc));
1387
1388 ifp_port = lacpp->lp_laggport->lp_ifp;
1389
1390 MGETHDR(m, M_DONTWAIT, MT_DATA);
1391 if (m == NULL) {
1392 lsc->lsc_mgethdr_failed.ev_count++;
1393 return NULL;
1394 }
1395
1396 m->m_pkthdr.len = m->m_len = sizeof(*du);
1397 m_reset_rcvif(m);
1398
1399 du = mtod(m, struct lacpdu *);
1400 memset(du, 0, sizeof(*du));
1401
1402 m->m_flags |= M_MCAST;
1403 memcpy(du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
1404 ETHER_ADDR_LEN);
1405 memcpy(du->ldu_eh.ether_shost, CLLADDR(ifp_port->if_sadl),
1406 ETHER_ADDR_LEN);
1407 du->ldu_eh.ether_type = htons(ETHERTYPE_SLOWPROTOCOLS);
1408 du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP;
1409 du->ldu_sph.sph_version = 1;
1410
1411 tlv_set(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO,
1412 sizeof(du->ldu_actor));
1413 lacp_peerinfo_actor(lsc, lacpp, &du->ldu_actor);
1414
1415 tlv_set(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO,
1416 sizeof(du->ldu_partner));
1417 lacp_peerinfo_partner(lacpp, &du->ldu_partner);
1418
1419 tlv_set(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO,
1420 sizeof(du->ldu_collector));
1421 du->ldu_collector.lci_maxdelay = 0;
1422
1423 du->ldu_tlv_term.tlv_type = LACP_TYPE_TERMINATE;
1424 du->ldu_tlv_term.tlv_length = 0;
1425
1426 return m;
1427 }
1428
1429 static void
1430 lacp_sm_tx_work(struct lagg_work *lw, void *xlsc)
1431 {
1432 struct lacp_softc *lsc;
1433 struct lacp_port *lacpp;
1434 struct lagg_port *lp;
1435 struct lacpdu *du;
1436 struct mbuf *m;
1437 struct psref psref;
1438 int bound;
1439
1440 lsc = xlsc;
1441 lacpp = container_of(lw, struct lacp_port, lp_work_smtx);
1442
1443 if (lsc->lsc_stop_lacpdu)
1444 return;
1445
1446 LACP_LOCK(lsc);
1447 m = lacp_lacpdu_mbuf(lsc, lacpp);
1448 if (m == NULL) {
1449 LACP_UNLOCK(lsc);
1450 return;
1451 }
1452 lacp_sm_negate_ntt(lacpp);
1453 lp = lacpp->lp_laggport;
1454 bound = curlwp_bind();
1455 lagg_port_getref(lp, &psref);
1456 LACP_UNLOCK(lsc);
1457
1458 if (LACP_ISDUMPING(lsc)) {
1459 lacp_dprintf(lsc, lacpp, "lacpdu transmit\n");
1460 du = mtod(m, struct lacpdu *);
1461 lacp_dump_lacpdutlv(&du->ldu_actor,
1462 &du->ldu_partner, &du->ldu_collector);
1463 }
1464
1465 lagg_port_xmit(lp, m);
1466 lagg_port_putref(lp, &psref);
1467 curlwp_bindx(bound);
1468 }
1469
1470 static void
1471 lacp_sm_tx(struct lacp_softc *lsc, struct lacp_port *lacpp)
1472 {
1473
1474 if (!lacp_port_need_to_tell(lacpp))
1475 return;
1476
1477 lagg_workq_add(lsc->lsc_workq, &lacpp->lp_work_smtx);
1478 }
1479
1480 static void
1481 lacp_tick(void *xlsc)
1482 {
1483 struct lacp_softc *lsc;
1484
1485 lsc = xlsc;
1486
1487 lagg_workq_add(lsc->lsc_workq, &lsc->lsc_work_tick);
1488
1489 LACP_LOCK(lsc);
1490 callout_schedule(&lsc->lsc_tick, hz);
1491 LACP_UNLOCK(lsc);
1492 }
1493
1494 static void
1495 lacp_run_timers(struct lacp_softc *lsc, struct lacp_port *lacpp)
1496 {
1497 size_t i;
1498
1499 for (i = 0; i < LACP_NTIMER; i++) {
1500 KASSERT(lacpp->lp_timer[i] >= 0);
1501
1502 if (lacpp->lp_timer[i] == 0)
1503 continue;
1504 if (--lacpp->lp_timer[i] > 0)
1505 continue;
1506
1507 KASSERT(lacp_timer_funcs[i] != NULL);
1508 lacp_timer_funcs[i](lsc, lacpp);
1509 }
1510 }
1511
1512 static void
1513 lacp_run_prototimers(struct lacp_softc *lsc)
1514 {
1515 size_t i;
1516
1517 for (i = 0; i < LACP_NPTIMER; i++) {
1518 KASSERT(lsc->lsc_timer[i] >= 0);
1519
1520 if (lsc->lsc_timer[i] == 0)
1521 continue;
1522 if (--lsc->lsc_timer[i] > 0)
1523 continue;
1524
1525 KASSERT(lacp_ptimer_funcs[i] != NULL);
1526 lacp_ptimer_funcs[i](lsc);
1527 }
1528 }
1529
1530 static void
1531 lacp_tick_work(struct lagg_work *lw __unused, void *xlsc)
1532 {
1533 struct lacp_softc *lsc;
1534 struct lacp_port *lacpp;
1535 struct lagg_softc *sc;
1536 struct lagg_port *lp;
1537
1538 lsc = xlsc;
1539 sc = lsc->lsc_softc;
1540
1541 LACP_LOCK(lsc);
1542 lacp_run_prototimers(lsc);
1543 LACP_UNLOCK(lsc);
1544
1545 LAGG_LOCK(sc);
1546 LACP_LOCK(lsc);
1547 LAGG_PORTS_FOREACH(sc, lp) {
1548 lacpp = lp->lp_proto_ctx;
1549 if (!ISSET(lacpp->lp_actor.lpi_state,
1550 LACP_STATE_AGGREGATION)) {
1551 continue;
1552 }
1553
1554 lacp_run_timers(lsc, lacpp);
1555 lacp_select(lsc, lacpp);
1556 lacp_sm_mux(lsc, lacpp);
1557 lacp_sm_tx(lsc, lacpp);
1558 lacp_sm_ptx_schedule(lacpp);
1559 }
1560
1561 LACP_UNLOCK(lsc);
1562 LAGG_UNLOCK(sc);
1563 }
1564
1565 static void
1566 lacp_systemid_str(char *buf, size_t buflen,
1567 uint16_t prio, const uint8_t *mac, uint16_t key)
1568 {
1569
1570 snprintf(buf, buflen,
1571 "%04X,"
1572 "%02X-%02X-%02X-%02X-%02X-%02X,"
1573 "%04X",
1574 (unsigned int)ntohs(prio),
1575 (int)mac[0], (int)mac[1], (int)mac[2],
1576 (int)mac[3], (int)mac[4], (int)mac[5],
1577 (unsigned int)htons(key));
1578
1579 }
1580
1581 __LACPDEBUGUSED static void
1582 lacp_aggregator_str(struct lacp_aggregator *la, char *buf, size_t buflen)
1583 {
1584
1585 lacp_systemid_str(buf, buflen, la->la_sid.sid_prio,
1586 la->la_sid.sid_mac, la->la_sid.sid_key);
1587 }
1588
1589 static void
1590 lacp_peerinfo_idstr(const struct lacpdu_peerinfo *pi,
1591 char *buf, size_t buflen)
1592 {
1593
1594 lacp_systemid_str(buf, buflen, pi->lpi_system_prio,
1595 pi->lpi_system_mac, pi->lpi_key);
1596 }
1597
1598 static void
1599 lacp_state_str(uint8_t state, char *buf, size_t buflen)
1600 {
1601
1602 snprintb(buf, buflen, LACP_STATE_BITS, state);
1603 }
1604
1605 static struct lagg_port *
1606 lacp_select_tx_port(struct lacp_softc *lsc, struct mbuf *m,
1607 struct psref *psref)
1608 {
1609 struct lacp_portmap *pm;
1610 struct lagg_port *lp;
1611 uint32_t hash;
1612 size_t act;
1613 int s;
1614
1615 hash = lagg_hashmbuf(lsc->lsc_softc, m);
1616
1617 s = pserialize_read_enter();
1618 act = LACP_PORTMAP_ACTIVE(lsc);
1619 pm = &lsc->lsc_portmaps[act];
1620
1621 if (pm->pm_count == 0) {
1622 pserialize_read_exit(s);
1623 return NULL;
1624 }
1625
1626 hash %= pm->pm_count;
1627 lp = pm->pm_ports[hash];
1628 lagg_port_getref(lp, psref);
1629
1630 pserialize_read_exit(s);
1631
1632 return lp;
1633 }
1634
1635 static void
1636 lacp_peerinfo_actor(struct lacp_softc *lsc, struct lacp_port *lacpp,
1637 struct lacpdu_peerinfo *dst)
1638 {
1639
1640 memcpy(dst->lpi_system_mac, lsc->lsc_system_mac, LACP_MAC_LEN);
1641 dst->lpi_system_prio = lsc->lsc_system_prio;
1642 dst->lpi_key = lsc->lsc_key;
1643 dst->lpi_port_no = lacpp->lp_actor.lpi_portno;
1644 dst->lpi_port_prio = lacpp->lp_actor.lpi_portprio;
1645 dst->lpi_state = lacpp->lp_actor.lpi_state;
1646 }
1647
1648 static void
1649 lacp_peerinfo_partner(struct lacp_port *lacpp, struct lacpdu_peerinfo *dst)
1650 {
1651 struct lacp_aggregator *la;
1652
1653 la = lacpp->lp_aggregator;
1654
1655 if (la != NULL) {
1656 memcpy(dst->lpi_system_mac, la->la_sid.sid_mac, LACP_MAC_LEN);
1657 dst->lpi_system_prio = la->la_sid.sid_prio;
1658 dst->lpi_key = la->la_sid.sid_key;
1659 } else {
1660 memset(dst->lpi_system_mac, 0, LACP_MAC_LEN);
1661 dst->lpi_system_prio = 0;
1662 dst->lpi_key = 0;
1663 }
1664 dst->lpi_port_no = lacpp->lp_partner.lpi_portno;
1665 dst->lpi_port_prio = lacpp->lp_partner.lpi_portprio;
1666 dst->lpi_state = lacpp->lp_partner.lpi_state;
1667 }
1668
1669 static int
1670 lacp_compare_peerinfo(struct lacpdu_peerinfo *a, struct lacpdu_peerinfo *b)
1671 {
1672
1673 return memcmp(a, b, offsetof(struct lacpdu_peerinfo, lpi_state));
1674 }
1675
1676 static void
1677 lacp_sm_rx_record_default(struct lacp_softc *lsc, struct lacp_port *lacpp)
1678 {
1679 uint8_t oldpstate;
1680 struct lacp_portinfo *pi;
1681 char buf[LACP_STATESTR_LEN] __LACPDEBUGUSED;
1682
1683 pi = &lacpp->lp_partner;
1684
1685 oldpstate = pi->lpi_state;
1686 pi->lpi_portno = htons(LACP_PORTNO_NONE);
1687 pi->lpi_portprio = htons(0xffff);
1688
1689 if (lsc->lsc_optimistic)
1690 pi->lpi_state = LACP_PARTNER_ADMIN_OPTIMISTIC;
1691 else
1692 pi->lpi_state = LACP_PARTNER_ADMIN_STRICT;
1693
1694 SET(lacpp->lp_actor.lpi_state, LACP_STATE_DEFAULTED);
1695
1696 if (oldpstate != pi->lpi_state) {
1697 LACP_STATE_STR(oldpstate, buf, sizeof(buf));
1698 LACP_DPRINTF((lsc, lacpp, "oldpstate %s\n", buf));
1699
1700 LACP_STATE_STR(pi->lpi_state, buf, sizeof(buf));
1701 LACP_DPRINTF((lsc, lacpp, "newpstate %s\n", buf));
1702 }
1703
1704 lacp_sm_ptx_update_timeout(lacpp, oldpstate);
1705 }
1706
1707 static inline bool
1708 lacp_port_is_synced(struct lacp_softc *lsc, struct lacp_port *lacpp,
1709 struct lacpdu_peerinfo *my_pi, struct lacpdu_peerinfo *peer_pi)
1710 {
1711 struct lacpdu_peerinfo actor;
1712
1713 if (!ISSET(peer_pi->lpi_state, LACP_STATE_ACTIVITY) &&
1714 (!ISSET(my_pi->lpi_state, LACP_STATE_ACTIVITY) ||
1715 !ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_ACTIVITY)))
1716 return false;
1717
1718 if (!ISSET(peer_pi->lpi_state, LACP_STATE_AGGREGATION))
1719 return false;
1720
1721 lacp_peerinfo_actor(lsc, lacpp, &actor);
1722 if (lacp_compare_peerinfo(&actor, my_pi) != 0)
1723 return false;
1724
1725 if (!LACP_STATE_EQ(actor.lpi_state, my_pi->lpi_state,
1726 LACP_STATE_AGGREGATION)) {
1727 return false;
1728 }
1729
1730 return true;
1731 }
1732
1733 static void
1734 lacp_sm_rx_record_peerinfo(struct lacp_softc *lsc, struct lacp_port *lacpp,
1735 struct lacpdu_peerinfo *my_pi, struct lacpdu_peerinfo *peer_pi)
1736 {
1737 char buf[LACP_STATESTR_LEN] __LACPDEBUGUSED;
1738 uint8_t oldpstate;
1739 struct lacp_portinfo *pi;
1740 struct lacp_aggregator_systemid *sid;
1741
1742 pi = &lacpp->lp_partner;
1743 sid = &lacpp->lp_aggregator_sidbuf;
1744
1745 oldpstate = lacpp->lp_partner.lpi_state;
1746
1747 sid->sid_prio = peer_pi->lpi_system_prio;
1748 sid->sid_key = peer_pi->lpi_key;
1749 memcpy(sid->sid_mac, peer_pi->lpi_system_mac,
1750 sizeof(sid->sid_mac));
1751
1752 pi->lpi_portno = peer_pi->lpi_port_no;
1753 pi->lpi_portprio = peer_pi->lpi_port_prio;
1754 pi->lpi_state = peer_pi->lpi_state;
1755
1756 if (lacp_port_is_synced(lsc, lacpp, my_pi, peer_pi)) {
1757 if (lsc->lsc_optimistic)
1758 SET(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
1759 } else {
1760 CLR(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
1761 }
1762
1763 CLR(lacpp->lp_actor.lpi_state, LACP_STATE_DEFAULTED);
1764
1765 if (oldpstate != lacpp->lp_partner.lpi_state) {
1766 LACP_STATE_STR(oldpstate, buf, sizeof(buf));
1767 LACP_DPRINTF((lsc, lacpp, "oldpstate %s\n", buf));
1768
1769 LACP_STATE_STR(lacpp->lp_partner.lpi_state,
1770 buf, sizeof(buf));
1771 LACP_DPRINTF((lsc, lacpp, "newpstate %s\n", buf));
1772 }
1773
1774 lacp_sm_ptx_update_timeout(lacpp, oldpstate);
1775 }
1776
1777 static void
1778 lacp_sm_rx_set_expired(struct lacp_port *lacpp)
1779 {
1780 uint8_t oldpstate;
1781
1782 oldpstate = lacpp->lp_partner.lpi_state;
1783
1784 CLR(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
1785 SET(lacpp->lp_partner.lpi_state, LACP_STATE_TIMEOUT);
1786 LACP_TIMER_ARM(lacpp, LACP_TIMER_CURRENT_WHILE,
1787 LACP_SHORT_TIMEOUT_TIME);
1788 SET(lacpp->lp_actor.lpi_state, LACP_STATE_EXPIRED);
1789
1790 lacp_sm_ptx_update_timeout(lacpp, oldpstate);
1791 }
1792
1793 static void
1794 lacp_sm_port_init(struct lacp_softc *lsc, struct lacp_port *lacpp,
1795 struct lagg_port *lp)
1796 {
1797
1798 KASSERT(LACP_LOCKED(lsc));
1799
1800 lacpp->lp_laggport = lp;
1801 lacpp->lp_actor.lpi_state = LACP_STATE_ACTIVITY;
1802 lacpp->lp_actor.lpi_portno = htons(if_get_index(lp->lp_ifp));
1803 lacpp->lp_actor.lpi_portprio = htons(LACP_PORT_PRIO);
1804 lacpp->lp_partner.lpi_state = LACP_STATE_TIMEOUT;
1805 lacpp->lp_aggregator = NULL;
1806 lacpp->lp_marker_xid = 0;
1807 lacpp->lp_mux_state = LACP_MUX_INIT;
1808
1809 lacp_set_mux(lsc, lacpp, LACP_MUX_DETACHED);
1810 lacp_sm_rx_record_default(lsc, lacpp);
1811 }
1812
1813 static void
1814 lacp_port_disable(struct lacp_softc *lsc, struct lacp_port *lacpp)
1815 {
1816
1817 if (ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_AGGREGATION))
1818 LACP_DPRINTF((lsc, lacpp, "enable -> disable\n"));
1819
1820 lacp_set_mux(lsc, lacpp, LACP_MUX_DETACHED);
1821 lacp_sm_rx_record_default(lsc, lacpp);
1822 CLR(lacpp->lp_actor.lpi_state,
1823 LACP_STATE_AGGREGATION | LACP_STATE_EXPIRED);
1824 CLR(lacpp->lp_partner.lpi_state, LACP_STATE_AGGREGATION);
1825 }
1826
1827 static void
1828 lacp_port_enable(struct lacp_softc *lsc __LACPDEBUGUSED,
1829 struct lacp_port *lacpp)
1830 {
1831
1832 if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_AGGREGATION))
1833 LACP_DPRINTF((lsc, lacpp, "disable -> enable\n"));
1834
1835 SET(lacpp->lp_actor.lpi_state, LACP_STATE_AGGREGATION);
1836 lacp_sm_rx_set_expired(lacpp);
1837 }
1838
1839 static void
1840 lacp_sm_rx_timer(struct lacp_softc *lsc, struct lacp_port *lacpp)
1841 {
1842
1843 if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_EXPIRED)) {
1844 /* CURRENT -> EXPIRED */
1845 LACP_DPRINTF((lsc, lacpp, "CURRENT -> EXPIRED\n"));
1846 lacp_sm_rx_set_expired(lacpp);
1847 } else {
1848 LACP_DPRINTF((lsc, lacpp, "EXPIRED -> DEFAULTED\n"));
1849 lacp_set_mux(lsc, lacpp, LACP_MUX_DETACHED);
1850 lacp_sm_rx_record_default(lsc, lacpp);
1851 }
1852 }
1853
1854 static void
1855 lacp_sm_ptx_timer(struct lacp_softc *lsc __unused, struct lacp_port *lacpp)
1856 {
1857
1858 lacp_sm_assert_ntt(lacpp);
1859 }
1860
1861 static void
1862 lacp_sm_ptx_schedule(struct lacp_port *lacpp)
1863 {
1864 int timeout;
1865
1866 /* no periodic */
1867 if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_ACTIVITY) &&
1868 !ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_ACTIVITY)) {
1869 LACP_TIMER_DISARM(lacpp, LACP_TIMER_PERIODIC);
1870 return;
1871 }
1872
1873 if (LACP_TIMER_ISARMED(lacpp, LACP_TIMER_PERIODIC))
1874 return;
1875
1876 timeout = ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_TIMEOUT) ?
1877 LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME;
1878
1879 LACP_TIMER_ARM(lacpp, LACP_TIMER_PERIODIC, timeout);
1880 }
1881
1882 static void
1883 lacp_sm_ptx_update_timeout(struct lacp_port *lacpp, uint8_t oldpstate)
1884 {
1885
1886 if (LACP_STATE_EQ(oldpstate, lacpp->lp_partner.lpi_state,
1887 LACP_STATE_TIMEOUT))
1888 return;
1889
1890 LACP_DPRINTF((NULL, lacpp, "partner timeout changed\n"));
1891
1892 LACP_TIMER_DISARM(lacpp, LACP_TIMER_PERIODIC);
1893
1894 /* if timeout has been shorted, assert NTT */
1895 if (ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_TIMEOUT))
1896 lacp_sm_assert_ntt(lacpp);
1897 }
1898
1899 static void
1900 lacp_sm_mux_timer(struct lacp_softc *lsc __LACPDEBUGUSED,
1901 struct lacp_port *lacpp)
1902 {
1903 char buf[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
1904
1905 KASSERT(lacpp->lp_pending > 0);
1906
1907 LACP_AGGREGATOR_STR(lacpp->lp_aggregator, buf, sizeof(buf));
1908 LACP_DPRINTF((lsc, lacpp, "aggregator %s, pending %d -> %d\n",
1909 buf, lacpp->lp_pending, lacpp->lp_pending -1));
1910
1911 lacpp->lp_pending--;
1912 }
1913
1914 static void
1915 lacp_sm_rx_update_selected(struct lacp_softc *lsc, struct lacp_port *lacpp,
1916 struct lacpdu_peerinfo *peer_pi)
1917 {
1918 struct lacpdu_peerinfo partner;
1919 char str0[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
1920 char str1[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
1921
1922 if (lacpp->lp_aggregator == NULL)
1923 return;
1924
1925 lacp_peerinfo_partner(lacpp, &partner);
1926 if (lacp_compare_peerinfo(peer_pi, &partner) != 0) {
1927 LACP_PEERINFO_IDSTR(&partner, str0, sizeof(str0));
1928 LACP_PEERINFO_IDSTR(peer_pi, str1, sizeof(str1));
1929 LACP_DPRINTF((lsc, lacpp,
1930 "different peerinfo, %s vs %s\n", str0, str1));
1931 goto do_unselect;
1932 }
1933
1934 if (!LACP_STATE_EQ(lacpp->lp_partner.lpi_state,
1935 peer_pi->lpi_state, LACP_STATE_AGGREGATION)) {
1936 LACP_DPRINTF((lsc, lacpp,
1937 "STATE_AGGREGATION changed %d -> %d\n",
1938 ISSET(lacpp->lp_partner.lpi_state,
1939 LACP_STATE_AGGREGATION) != 0,
1940 ISSET(peer_pi->lpi_state, LACP_STATE_AGGREGATION) != 0));
1941 goto do_unselect;
1942 }
1943
1944 return;
1945
1946 do_unselect:
1947 lacpp->lp_selected = LACP_UNSELECTED;
1948 /* lacpp->lp_aggregator will be released at lacp_set_mux() */
1949 }
1950
1951 static void
1952 lacp_sm_rx_update_ntt(struct lacp_softc *lsc, struct lacp_port *lacpp,
1953 struct lacpdu_peerinfo *my_pi)
1954 {
1955 struct lacpdu_peerinfo actor;
1956
1957 lacp_peerinfo_actor(lsc, lacpp, &actor);
1958
1959 if (lacp_compare_peerinfo(&actor, my_pi) != 0 ||
1960 !LACP_STATE_EQ(lacpp->lp_actor.lpi_state, my_pi->lpi_state,
1961 LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) {
1962 LACP_DPRINTF((lsc, lacpp, "assert ntt\n"));
1963 lacp_sm_assert_ntt(lacpp);
1964 }
1965 }
1966
1967 static void
1968 lacp_sm_rx(struct lacp_softc *lsc, struct lacp_port *lacpp,
1969 struct lacpdu_peerinfo *my_pi, struct lacpdu_peerinfo *peer_pi)
1970 {
1971 int timeout;
1972
1973 KASSERT(LACP_LOCKED(lsc));
1974
1975 /* check LACP disabled first */
1976 if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_AGGREGATION))
1977 return;
1978
1979 /* check loopback condition */
1980 if (memcmp(lsc->lsc_system_mac, peer_pi->lpi_system_mac,
1981 LACP_MAC_LEN) == 0 &&
1982 lsc->lsc_system_prio == peer_pi->lpi_system_prio)
1983 return;
1984
1985 lacp_sm_rx_update_selected(lsc, lacpp, peer_pi);
1986 lacp_sm_rx_update_ntt(lsc, lacpp, my_pi);
1987 lacp_sm_rx_record_peerinfo(lsc, lacpp, my_pi, peer_pi);
1988
1989 timeout = ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_TIMEOUT) ?
1990 LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME;
1991 LACP_TIMER_ARM(lacpp, LACP_TIMER_CURRENT_WHILE, timeout);
1992
1993 CLR(lacpp->lp_actor.lpi_state, LACP_STATE_EXPIRED);
1994
1995 /* kick transmit machine without timeout. */
1996 lacp_sm_tx(lsc, lacpp);
1997 }
1998
1999 static void
2000 lacp_disable_collecting(struct lacp_port *lacpp)
2001 {
2002
2003 LACP_DPRINTF((NULL, lacpp, "collecting disabled\n"));
2004 CLR(lacpp->lp_actor.lpi_state, LACP_STATE_COLLECTING);
2005 atomic_store_relaxed(&lacpp->lp_collector, false);
2006 }
2007
2008 static void
2009 lacp_enable_collecting(struct lacp_port *lacpp)
2010 {
2011 LACP_DPRINTF((NULL, lacpp, "collecting enabled\n"));
2012 SET(lacpp->lp_actor.lpi_state, LACP_STATE_COLLECTING);
2013 atomic_store_relaxed(&lacpp->lp_collector, true);
2014 }
2015
2016 static void
2017 lacp_update_portmap(struct lacp_softc *lsc)
2018 {
2019 struct lagg_softc *sc;
2020 struct lacp_aggregator *la;
2021 struct lacp_portmap *pm_act, *pm_next;
2022 struct lacp_port *lacpp;
2023 size_t pmap, n;
2024 u_int link;
2025
2026 KASSERT(LACP_LOCKED(lsc));
2027
2028 la = lsc->lsc_aggregator;
2029
2030 pmap = LACP_PORTMAP_ACTIVE(lsc);
2031 pm_act = &lsc->lsc_portmaps[pmap];
2032
2033 pmap = LACP_PORTMAP_NEXT(lsc);
2034 pm_next = &lsc->lsc_portmaps[pmap];
2035
2036 n = 0;
2037 if (la != NULL) {
2038 LIST_FOREACH(lacpp, &la->la_ports, lp_entry_la) {
2039 if (!ISSET(lacpp->lp_actor.lpi_state,
2040 LACP_STATE_DISTRIBUTING)) {
2041 continue;
2042 }
2043
2044 pm_next->pm_ports[n] = lacpp->lp_laggport;
2045 n++;
2046
2047 if (n >= LACP_MAX_PORTS)
2048 break;
2049 }
2050 }
2051 pm_next->pm_count = n;
2052
2053 atomic_store_release(&lsc->lsc_activemap, pmap);
2054 pserialize_perform(lsc->lsc_psz);
2055
2056 LACP_DPRINTF((lsc, NULL, "portmap count updated (%zu -> %zu)\n",
2057 pm_act->pm_count, pm_next->pm_count));
2058
2059 link = lacp_portmap_linkstate(pm_next);
2060 if (link != lacp_portmap_linkstate(pm_act)) {
2061 sc = lsc->lsc_softc;
2062 if_link_state_change(&sc->sc_if, link);
2063 }
2064
2065 lagg_workq_add(lsc->lsc_workq, &lsc->lsc_work_linkspeed);
2066
2067 /* cleanup */
2068 pm_act->pm_count = 0;
2069 memset(pm_act->pm_ports, 0, sizeof(pm_act->pm_ports));
2070 }
2071
2072 static void
2073 lacp_disable_distributing(struct lacp_softc *lsc, struct lacp_port *lacpp)
2074 {
2075 struct lacp_portmap *pm;
2076 bool do_update;
2077 size_t act, i;
2078 int s;
2079
2080 KASSERT(LACP_LOCKED(lsc));
2081
2082 if (ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_DISTRIBUTING)) {
2083 LAGG_LOG(lsc->lsc_softc, LOG_INFO,
2084 "disable distributing on %s\n", LACP_PORT_XNAME(lacpp));
2085 CLR(lacpp->lp_actor.lpi_state, LACP_STATE_DISTRIBUTING);
2086 }
2087
2088 s = pserialize_read_enter();
2089 act = LACP_PORTMAP_ACTIVE(lsc);
2090 pm = &lsc->lsc_portmaps[act];
2091
2092 do_update = false;
2093 for (i = 0; i < pm->pm_count; i++) {
2094 if (pm->pm_ports[i] == lacpp->lp_laggport) {
2095 do_update = true;
2096 break;
2097 }
2098 }
2099 pserialize_read_exit(s);
2100
2101 if (do_update)
2102 lacp_update_portmap(lsc);
2103 }
2104
2105 static void
2106 lacp_enable_distributing(struct lacp_softc *lsc, struct lacp_port *lacpp)
2107 {
2108
2109 KASSERT(LACP_LOCKED(lsc));
2110
2111 KASSERT(lacp_isactive(lsc, lacpp));
2112
2113 LAGG_LOG(lsc->lsc_softc, LOG_INFO,
2114 "enable distributing on %s\n", LACP_PORT_XNAME(lacpp));
2115 SET(lacpp->lp_actor.lpi_state, LACP_STATE_DISTRIBUTING);
2116 lacp_suppress_distributing(lsc);
2117 lacp_update_portmap(lsc);
2118 }
2119
2120 static void
2121 lacp_select_active_aggregator(struct lacp_softc *lsc)
2122 {
2123 struct lacp_aggregator *la, *best_la;
2124 char str[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
2125
2126 KASSERT(LACP_LOCKED(lsc));
2127
2128 la = lsc->lsc_aggregator;
2129 if (la != NULL && la->la_attached_port > 0) {
2130 best_la = la;
2131 } else {
2132 best_la = NULL;
2133 }
2134
2135 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
2136 if (la->la_attached_port <= 0)
2137 continue;
2138
2139 if (best_la == NULL ||
2140 LACP_SYS_PRI(la) < LACP_SYS_PRI(best_la))
2141 best_la = la;
2142 }
2143
2144 if (best_la != lsc->lsc_aggregator) {
2145 LACP_DPRINTF((lsc, NULL, "active aggregator changed\n"));
2146
2147 if (lsc->lsc_aggregator != NULL) {
2148 LACP_AGGREGATOR_STR(lsc->lsc_aggregator,
2149 str, sizeof(str));
2150 } else {
2151 snprintf(str, sizeof(str), "(null)");
2152 }
2153 LACP_DPRINTF((lsc, NULL, "old aggregator=%s\n", str));
2154
2155 if (best_la != NULL) {
2156 LACP_AGGREGATOR_STR(best_la, str, sizeof(str));
2157 } else {
2158 snprintf(str, sizeof(str), "(null)");
2159 }
2160 LACP_DPRINTF((lsc, NULL, "new aggregator=%s\n", str));
2161
2162 lsc->lsc_aggregator = best_la;
2163 }
2164 }
2165
2166 static void
2167 lacp_port_attached(struct lacp_softc *lsc, struct lacp_port *lacpp)
2168 {
2169 struct lacp_aggregator *la;
2170
2171 KASSERT(LACP_LOCKED(lsc));
2172
2173 if (ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_SYNC))
2174 return;
2175
2176 la = lacpp->lp_aggregator;
2177 KASSERT(la != NULL);
2178 KASSERT(la->la_attached_port >= 0);
2179
2180 SET(lacpp->lp_actor.lpi_state, LACP_STATE_SYNC);
2181 la->la_attached_port++;
2182 lacp_select_active_aggregator(lsc);
2183 }
2184
2185 static void
2186 lacp_port_detached(struct lacp_softc *lsc, struct lacp_port *lacpp)
2187 {
2188 struct lacp_aggregator *la;
2189
2190 KASSERT(LACP_LOCKED(lsc));
2191
2192 if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_SYNC))
2193 return;
2194
2195 la = lacpp->lp_aggregator;
2196 KASSERT(la != NULL);
2197 KASSERT(la->la_attached_port > 0);
2198
2199 CLR(lacpp->lp_actor.lpi_state, LACP_STATE_SYNC);
2200 la->la_attached_port--;
2201 lacp_select_active_aggregator(lsc);
2202 }
2203
2204 static int
2205 lacp_set_mux(struct lacp_softc *lsc, struct lacp_port *lacpp,
2206 enum lacp_mux_state new_state)
2207 {
2208 struct lagg_softc *sc;
2209 struct ifnet *ifp;
2210
2211 KASSERT(LACP_LOCKED(lsc));
2212
2213 sc = lacpp->lp_laggport->lp_softc;
2214 ifp = &sc->sc_if;
2215
2216 if (lacpp->lp_mux_state == new_state)
2217 return -1;
2218
2219 switch (new_state) {
2220 case LACP_MUX_DETACHED:
2221 lacp_port_detached(lsc, lacpp);
2222 lacp_disable_distributing(lsc, lacpp);
2223 lacp_disable_collecting(lacpp);
2224 lacp_sm_assert_ntt(lacpp);
2225 /* cancel timer */
2226 if (LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE)) {
2227 KASSERT(lacpp->lp_pending > 0);
2228 lacpp->lp_pending--;
2229 LACP_TIMER_DISARM(lacpp, LACP_TIMER_WAIT_WHILE);
2230 }
2231 lacp_unselect(lsc, lacpp);
2232 break;
2233 case LACP_MUX_WAITING:
2234 LACP_TIMER_ARM(lacpp, LACP_TIMER_WAIT_WHILE,
2235 LACP_AGGREGATE_WAIT_TIME);
2236 lacpp->lp_pending++;
2237 break;
2238 case LACP_MUX_STANDBY:
2239 #ifdef LACP_STANDBY_SYNCED
2240 lacp_port_attached(lsc, lacpp);
2241 lacp_disable_collecting(lacpp);
2242 lacp_sm_assert_ntt(lacpp);
2243 #endif
2244 break;
2245 case LACP_MUX_ATTACHED:
2246 lacp_port_attached(lsc, lacpp);
2247 lacp_disable_collecting(lacpp);
2248 lacp_sm_assert_ntt(lacpp);
2249 break;
2250 case LACP_MUX_COLLECTING:
2251 lacp_enable_collecting(lacpp);
2252 lacp_disable_distributing(lsc, lacpp);
2253 lacp_sm_assert_ntt(lacpp);
2254 break;
2255 case LACP_MUX_DISTRIBUTING:
2256 lacp_enable_distributing(lsc, lacpp);
2257 break;
2258 case LACP_MUX_INIT:
2259 default:
2260 panic("%s: unknown state", ifp->if_xname);
2261 }
2262
2263 LACP_DPRINTF((lsc, lacpp, "mux_state %d -> %d\n",
2264 lacpp->lp_mux_state, new_state));
2265
2266 lacpp->lp_mux_state = new_state;
2267 return 0;
2268 }
2269
2270 static void
2271 lacp_sm_mux(struct lacp_softc *lsc, struct lacp_port *lacpp)
2272 {
2273 struct lacp_aggregator *la __diagused;
2274 enum lacp_mux_state next_state;
2275 enum lacp_selected selected;
2276 bool p_sync, p_collecting;
2277
2278 p_sync = ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
2279 p_collecting = ISSET(lacpp->lp_partner.lpi_state,
2280 LACP_STATE_COLLECTING);
2281
2282 do {
2283 next_state = lacpp->lp_mux_state;
2284 la = lacpp->lp_aggregator;
2285 selected = lacpp->lp_selected;
2286 KASSERT(la != NULL ||
2287 lacpp->lp_mux_state == LACP_MUX_DETACHED);
2288
2289 switch (lacpp->lp_mux_state) {
2290 case LACP_MUX_DETACHED:
2291 if (selected != LACP_UNSELECTED)
2292 next_state = LACP_MUX_WAITING;
2293 break;
2294 case LACP_MUX_WAITING:
2295 KASSERTMSG((lacpp->lp_pending > 0 ||
2296 !LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE)),
2297 "lp_pending=%d, timer=%d", lacpp->lp_pending,
2298 !LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE));
2299
2300 if (selected == LACP_UNSELECTED) {
2301 next_state = LACP_MUX_DETACHED;
2302 } else if (lacpp->lp_pending == 0) {
2303 if (selected == LACP_SELECTED) {
2304 next_state = LACP_MUX_ATTACHED;
2305 } else if (selected == LACP_STANDBY) {
2306 next_state = LACP_MUX_STANDBY;
2307 } else {
2308 next_state = LACP_MUX_DETACHED;
2309 }
2310 }
2311 break;
2312 case LACP_MUX_STANDBY:
2313 if (selected == LACP_SELECTED) {
2314 next_state = LACP_MUX_ATTACHED;
2315 } else if (selected != LACP_STANDBY) {
2316 next_state = LACP_MUX_DETACHED;
2317 }
2318 break;
2319 case LACP_MUX_ATTACHED:
2320 if (selected != LACP_SELECTED) {
2321 if (selected == LACP_STANDBY)
2322 LAGG_LOG(lsc->lsc_softc, LOG_INFO,
2323 "detaching %s\n",
2324 LACP_PORT_XNAME(lacpp));
2325 next_state = LACP_MUX_DETACHED;
2326 } else if (lacp_isactive(lsc, lacpp) && p_sync) {
2327 next_state = LACP_MUX_COLLECTING;
2328 }
2329 break;
2330 case LACP_MUX_COLLECTING:
2331 if (selected != LACP_SELECTED ||
2332 !lacp_isactive(lsc, lacpp)
2333 || !p_sync) {
2334 next_state = LACP_MUX_ATTACHED;
2335 } else if (p_collecting) {
2336 next_state = LACP_MUX_DISTRIBUTING;
2337 }
2338 break;
2339 case LACP_MUX_DISTRIBUTING:
2340 if (selected != LACP_SELECTED ||
2341 !lacp_isactive(lsc, lacpp)
2342 || !p_sync || !p_collecting) {
2343 next_state = LACP_MUX_COLLECTING;
2344 LACP_DPRINTF((lsc, lacpp,
2345 "Interface stopped DISTRIBUTING,"
2346 " possible flapping\n"));
2347 }
2348 break;
2349 case LACP_MUX_INIT:
2350 default:
2351 panic("%s: unknown state",
2352 lsc->lsc_softc->sc_if.if_xname);
2353 }
2354 } while (lacp_set_mux(lsc, lacpp, next_state) == 0);
2355 }
2356
2357 static bool
2358 lacp_aggregator_is_match(struct lacp_aggregator_systemid *a,
2359 struct lacp_aggregator_systemid *b)
2360 {
2361
2362 if (a->sid_prio != b->sid_prio)
2363 return false;
2364
2365 if (a->sid_key != b->sid_key)
2366 return false;
2367
2368 if (memcmp(a->sid_mac, b->sid_mac, sizeof(a->sid_mac)) != 0)
2369 return false;
2370
2371 return true;
2372 }
2373
2374 static void
2375 lacp_selected_update(struct lacp_softc *lsc, struct lacp_aggregator *la)
2376 {
2377 struct lacp_port *lacpp;
2378 enum lacp_selected next_selected;
2379 const char *msg;
2380 size_t nselected;
2381 uint64_t linkspeed;
2382
2383 KASSERT(LACP_LOCKED(lsc));
2384
2385 lacpp = LIST_FIRST(&la->la_ports);
2386 if (lacpp == NULL)
2387 return;
2388
2389 linkspeed = lacpp->lp_linkspeed;
2390 nselected = 0;
2391 LIST_FOREACH(lacpp, &la->la_ports, lp_entry_la) {
2392 if (lacpp->lp_selected == LACP_UNSELECTED)
2393 continue;
2394
2395 next_selected = LACP_SELECTED;
2396 msg = " is selected";
2397
2398 if (nselected >= lsc->lsc_max_ports) {
2399 next_selected = LACP_STANDBY;
2400 msg = " is standby because of too many active ports";
2401 }
2402
2403 if (!lsc->lsc_multi_linkspeed &&
2404 linkspeed != lacpp->lp_linkspeed) {
2405 next_selected = LACP_STANDBY;
2406 msg = " is standby because of link speed mismatch";
2407 }
2408
2409 if (lacpp->lp_selected != next_selected) {
2410 lacpp->lp_selected = next_selected;
2411 LAGG_LOG(lsc->lsc_softc, LOG_INFO,
2412 "%s%s\n", LACP_PORT_XNAME(lacpp), msg);
2413 }
2414
2415 if (lacpp->lp_selected == LACP_SELECTED)
2416 nselected++;
2417 }
2418 }
2419
2420 static void
2421 lacp_select(struct lacp_softc *lsc, struct lacp_port *lacpp)
2422 {
2423 struct lacp_aggregator *la;
2424 struct lacp_aggregator_systemid *sid;
2425 struct lacp_port *lacpp0;
2426 char buf[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
2427
2428 if (lacpp->lp_aggregator != NULL)
2429 return;
2430
2431 /* If we haven't heard from our peer, skip this step. */
2432 if (ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_DEFAULTED))
2433 return
2434
2435 KASSERT(!LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE));
2436
2437 sid = &lacpp->lp_aggregator_sidbuf;
2438
2439 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
2440 if (lacp_aggregator_is_match(&la->la_sid, sid))
2441 break;
2442 }
2443
2444 if (la == NULL) {
2445 la = kmem_zalloc(sizeof(*la), KM_NOSLEEP);
2446 if (la == NULL) {
2447 LACP_DPRINTF((lsc, lacpp,
2448 "couldn't allocate aggregator\n"));
2449 /* will retry the next tick. */
2450 return;
2451 }
2452 LIST_INIT(&la->la_ports);
2453
2454 la->la_sid = *sid;
2455 TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
2456 LACP_DPRINTF((lsc, lacpp, "a new aggregator created\n"));
2457 } else {
2458 LACP_DPRINTF((lsc, lacpp, "aggregator found\n"));
2459 }
2460
2461 KASSERT(la != NULL);
2462 LACP_AGGREGATOR_STR(la, buf, sizeof(buf));
2463 LACP_DPRINTF((lsc, lacpp, "aggregator lagid=%s\n", buf));
2464
2465 lacpp->lp_aggregator = la;
2466 lacpp->lp_selected = LACP_READY;
2467
2468 LIST_FOREACH(lacpp0, &la->la_ports, lp_entry_la) {
2469 if (lacp_port_priority_max(lacpp0, lacpp) == lacpp) {
2470 LIST_INSERT_BEFORE(lacpp0, lacpp, lp_entry_la);
2471 break;
2472 }
2473
2474 if (LIST_NEXT(lacpp0, lp_entry_la) == NULL) {
2475 LIST_INSERT_AFTER(lacpp0, lacpp, lp_entry_la);
2476 break;
2477 }
2478 }
2479
2480 if (lacpp0 == NULL)
2481 LIST_INSERT_HEAD(&la->la_ports, lacpp, lp_entry_la);
2482
2483 lacp_selected_update(lsc, la);
2484 }
2485
2486 static void
2487 lacp_unselect(struct lacp_softc *lsc, struct lacp_port *lacpp)
2488 {
2489 struct lacp_aggregator *la;
2490 char buf[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
2491 bool remove_actaggr;
2492
2493 KASSERT(LACP_LOCKED(lsc));
2494 KASSERT(!LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE));
2495
2496 la = lacpp->lp_aggregator;
2497 lacpp->lp_selected = LACP_UNSELECTED;
2498
2499 if (la == NULL)
2500 return;
2501
2502 KASSERT(!LIST_EMPTY(&la->la_ports));
2503
2504 LACP_AGGREGATOR_STR(la, buf, sizeof(buf));
2505 LACP_DPRINTF((lsc, lacpp, "unselect aggregator lagid=%s\n", buf));
2506
2507 LIST_REMOVE(lacpp, lp_entry_la);
2508 lacpp->lp_aggregator = NULL;
2509
2510 if (LIST_EMPTY(&la->la_ports)) {
2511 remove_actaggr = false;
2512
2513 if (la == lsc->lsc_aggregator) {
2514 LACP_DPRINTF((lsc, NULL, "remove active aggregator\n"));
2515 lsc->lsc_aggregator = NULL;
2516 remove_actaggr = true;
2517 }
2518
2519 TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
2520 kmem_free(la, sizeof(*la));
2521
2522 if (remove_actaggr)
2523 lacp_select_active_aggregator(lsc);
2524 } else {
2525 lacp_selected_update(lsc, la);
2526 }
2527 }
2528
2529 static void
2530 lacp_suppress_distributing(struct lacp_softc *lsc)
2531 {
2532 struct lacp_aggregator *la;
2533 struct lacp_port *lacpp;
2534
2535 KASSERT(LACP_LOCKED(lsc));
2536
2537 la = lsc->lsc_aggregator;
2538
2539 LIST_FOREACH(lacpp, &la->la_ports, lp_entry_la) {
2540 if (ISSET(lacpp->lp_actor.lpi_state,
2541 LACP_STATE_DISTRIBUTING)) {
2542 lagg_workq_add(lsc->lsc_workq,
2543 &lacpp->lp_work_marker);
2544 }
2545 }
2546
2547 LACP_PTIMER_ARM(lsc, LACP_PTIMER_DISTRIBUTING,
2548 LACP_TRANSIT_DELAY);
2549 }
2550
2551 static void
2552 lacp_distributing_timer(struct lacp_softc *lsc)
2553 {
2554
2555 KASSERT(LACP_LOCKED(lsc));
2556
2557 if (lsc->lsc_suppress_distributing) {
2558 LACP_DPRINTF((lsc, NULL,
2559 "disable suppress distributing\n"));
2560 lsc->lsc_suppress_distributing = false;
2561 }
2562 }
2563
2564 static struct mbuf *
2565 lacp_markerdu_mbuf(struct lacp_softc *lsc, struct lacp_port *lacpp)
2566 {
2567 struct ifnet *ifp_port;
2568 struct mbuf *m;
2569 struct markerdu *mdu;
2570 struct markerdu_info *mi;
2571
2572 KASSERT(LACP_LOCKED(lsc));
2573
2574 ifp_port = lacpp->lp_laggport->lp_ifp;
2575
2576 MGETHDR(m, M_DONTWAIT, MT_DATA);
2577 if (m == NULL) {
2578 lsc->lsc_mgethdr_failed.ev_count++;
2579 return NULL;
2580 }
2581
2582 m->m_pkthdr.len = m->m_len = sizeof(*mdu);
2583 m_reset_rcvif(m);
2584
2585 mdu = mtod(m, struct markerdu *);
2586
2587 memset(mdu, 0, sizeof(*mdu));
2588
2589 m->m_flags |= M_MCAST;
2590 memcpy(mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
2591 ETHER_ADDR_LEN);
2592 memcpy(mdu->mdu_eh.ether_shost, CLLADDR(ifp_port->if_sadl),
2593 ETHER_ADDR_LEN);
2594 mdu->mdu_eh.ether_type = ntohs(ETHERTYPE_SLOWPROTOCOLS);
2595 mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER;
2596 mdu->mdu_sph.sph_version = 1;
2597
2598 mi = &mdu->mdu_info;
2599 tlv_set(&mdu->mdu_tlv_info, MARKER_TYPE_INFO,
2600 sizeof(*mi));
2601 mi->mi_rq_port = lacpp->lp_actor.lpi_portno;
2602 mi->mi_rq_xid = htonl(lacpp->lp_marker_xid);
2603 memcpy(mi->mi_rq_system, lsc->lsc_system_mac, LACP_MAC_LEN);
2604
2605 mdu->mdu_tlv_term.tlv_type = MARKER_TYPE_TERMINATE;
2606 mdu->mdu_tlv_term.tlv_length = 0;
2607
2608 return m;
2609 }
2610
2611 static void
2612 lacp_marker_work(struct lagg_work *lw, void *xlsc)
2613 {
2614 struct lacp_softc *lsc;
2615 struct lacp_port *lacpp;
2616 struct lagg_port *lp;
2617 struct markerdu *mdu;
2618 struct mbuf *m;
2619 struct psref psref;
2620 int bound;
2621
2622 lsc = xlsc;
2623 lacpp = container_of(lw, struct lacp_port, lp_work_marker);
2624
2625 LACP_LOCK(lsc);
2626 lacpp->lp_marker_xid++;
2627 m = lacp_markerdu_mbuf(lsc, lacpp);
2628 if (m == NULL) {
2629 LACP_UNLOCK(lsc);
2630 return;
2631 }
2632 SET(lacpp->lp_flags, LACP_PORT_MARK);
2633 lsc->lsc_suppress_distributing = true;
2634 lp = lacpp->lp_laggport;
2635 bound = curlwp_bind();
2636 lagg_port_getref(lp, &psref);
2637 LACP_UNLOCK(lsc);
2638
2639 if (LACP_ISDUMPING(lsc)) {
2640 lacp_dprintf(lsc, lacpp, "markerdu transmit\n");
2641 mdu = mtod(m, struct markerdu *);
2642 lacp_dump_markertlv(&mdu->mdu_info, NULL);
2643 }
2644
2645 lagg_port_xmit(lp, m);
2646 lagg_port_putref(lp, &psref);
2647 curlwp_bindx(bound);
2648 }
2649
2650 static void
2651 lacp_dump_lacpdutlv(const struct lacpdu_peerinfo *pi_actor,
2652 const struct lacpdu_peerinfo *pi_partner,
2653 const struct lacpdu_collectorinfo *lci)
2654 {
2655 char str[LACP_STATESTR_LEN];
2656
2657 if (pi_actor != NULL) {
2658 lacp_peerinfo_idstr(pi_actor, str, sizeof(str));
2659 printf("actor=%s\n", str);
2660 lacp_state_str(pi_actor->lpi_state,
2661 str, sizeof(str));
2662 printf("actor.state=%s portno=%d portprio=0x%04x\n",
2663 str,
2664 ntohs(pi_actor->lpi_port_no),
2665 ntohs(pi_actor->lpi_port_prio));
2666 } else {
2667 printf("no actor info\n");
2668 }
2669
2670 if (pi_partner != NULL) {
2671 lacp_peerinfo_idstr(pi_partner, str, sizeof(str));
2672 printf("partner=%s\n", str);
2673 lacp_state_str(pi_partner->lpi_state,
2674 str, sizeof(str));
2675 printf("partner.state=%s portno=%d portprio=0x%04x\n",
2676 str,
2677 ntohs(pi_partner->lpi_port_no),
2678 ntohs(pi_partner->lpi_port_prio));
2679 } else {
2680 printf("no partner info\n");
2681 }
2682
2683 if (lci != NULL) {
2684 printf("maxdelay=%d\n", ntohs(lci->lci_maxdelay));
2685 } else {
2686 printf("no collector info\n");
2687 }
2688 }
2689
2690 static void
2691 lacp_dump_markertlv(const struct markerdu_info *mi_info,
2692 const struct markerdu_info *mi_res)
2693 {
2694
2695 if (mi_info != NULL) {
2696 printf("marker info: port=%d, sys=%s, id=%u\n",
2697 ntohs(mi_info->mi_rq_port),
2698 ether_sprintf(mi_info->mi_rq_system),
2699 ntohl(mi_info->mi_rq_xid));
2700 }
2701
2702 if (mi_res != NULL) {
2703 printf("marker resp: port=%d, sys=%s, id=%u\n",
2704 ntohs(mi_res->mi_rq_port),
2705 ether_sprintf(mi_res->mi_rq_system),
2706 ntohl(mi_res->mi_rq_xid));
2707 }
2708 }
2709
2710 static void
2711 lacp_linkstate(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
2712 {
2713
2714 IFNET_ASSERT_UNLOCKED(lp->lp_ifp);
2715
2716 IFNET_LOCK(lp->lp_ifp);
2717 lacp_linkstate_ifnet_locked(xlsc, lp);
2718 IFNET_UNLOCK(lp->lp_ifp);
2719 }
2720
2721 static void
2722 lacp_linkspeed_work(struct lagg_work *lw __unused, void *xlsc)
2723 {
2724 struct lacp_softc *lsc = (struct lacp_softc *)xlsc;
2725 struct lagg_softc *sc = lsc->lsc_softc;
2726 struct lacp_portmap *pm;
2727 struct lagg_port *lp;
2728 struct lacp_port *lacpp;
2729 uint64_t linkspeed;
2730 size_t act, i;
2731
2732 linkspeed = 0;
2733
2734 LACP_LOCK(lsc);
2735 act = LACP_PORTMAP_ACTIVE(lsc);
2736 pm = &lsc->lsc_portmaps[act];
2737 for (i = 0; i < pm->pm_count; i++) {
2738 lp = pm->pm_ports[i];
2739 lacpp = lp->lp_proto_ctx;
2740 linkspeed = MAX(linkspeed, lacpp->lp_linkspeed);
2741 }
2742 LACP_UNLOCK(lsc);
2743
2744 LAGG_LOCK(sc);
2745 lagg_set_linkspeed(sc, linkspeed);
2746 LAGG_UNLOCK(sc);
2747 }
2748