if_lagg_lacp.c revision 1.35 1 /* $NetBSD: if_lagg_lacp.c,v 1.35 2024/04/04 08:54:52 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.35 2024/04/04 08:54:52 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 KASSERT(LACP_LOCKED(lsc));
1500
1501 for (i = 0; i < LACP_NTIMER; i++) {
1502 KASSERT(lacpp->lp_timer[i] >= 0);
1503
1504 if (lacpp->lp_timer[i] == 0)
1505 continue;
1506 if (--lacpp->lp_timer[i] > 0)
1507 continue;
1508
1509 KASSERT(lacp_timer_funcs[i] != NULL);
1510 lacp_timer_funcs[i](lsc, lacpp);
1511 }
1512 }
1513
1514 static void
1515 lacp_run_prototimers(struct lacp_softc *lsc)
1516 {
1517 size_t i;
1518
1519 for (i = 0; i < LACP_NPTIMER; i++) {
1520 KASSERT(lsc->lsc_timer[i] >= 0);
1521
1522 if (lsc->lsc_timer[i] == 0)
1523 continue;
1524 if (--lsc->lsc_timer[i] > 0)
1525 continue;
1526
1527 KASSERT(lacp_ptimer_funcs[i] != NULL);
1528 lacp_ptimer_funcs[i](lsc);
1529 }
1530 }
1531
1532 static void
1533 lacp_tick_work(struct lagg_work *lw __unused, void *xlsc)
1534 {
1535 struct lacp_softc *lsc;
1536 struct lacp_port *lacpp;
1537 struct lagg_softc *sc;
1538 struct lagg_port *lp;
1539
1540 lsc = xlsc;
1541 sc = lsc->lsc_softc;
1542
1543 LACP_LOCK(lsc);
1544 lacp_run_prototimers(lsc);
1545 LACP_UNLOCK(lsc);
1546
1547 LAGG_LOCK(sc);
1548 LACP_LOCK(lsc);
1549 LAGG_PORTS_FOREACH(sc, lp) {
1550 lacpp = lp->lp_proto_ctx;
1551 if (!ISSET(lacpp->lp_actor.lpi_state,
1552 LACP_STATE_AGGREGATION)) {
1553 continue;
1554 }
1555
1556 lacp_run_timers(lsc, lacpp);
1557 lacp_select(lsc, lacpp);
1558 lacp_sm_mux(lsc, lacpp);
1559 lacp_sm_tx(lsc, lacpp);
1560 lacp_sm_ptx_schedule(lacpp);
1561 }
1562
1563 LACP_UNLOCK(lsc);
1564 LAGG_UNLOCK(sc);
1565 }
1566
1567 static void
1568 lacp_systemid_str(char *buf, size_t buflen,
1569 uint16_t prio, const uint8_t *mac, uint16_t key)
1570 {
1571
1572 snprintf(buf, buflen,
1573 "%04X,"
1574 "%02X-%02X-%02X-%02X-%02X-%02X,"
1575 "%04X",
1576 (unsigned int)ntohs(prio),
1577 (int)mac[0], (int)mac[1], (int)mac[2],
1578 (int)mac[3], (int)mac[4], (int)mac[5],
1579 (unsigned int)htons(key));
1580
1581 }
1582
1583 __LACPDEBUGUSED static void
1584 lacp_aggregator_str(struct lacp_aggregator *la, char *buf, size_t buflen)
1585 {
1586
1587 lacp_systemid_str(buf, buflen, la->la_sid.sid_prio,
1588 la->la_sid.sid_mac, la->la_sid.sid_key);
1589 }
1590
1591 static void
1592 lacp_peerinfo_idstr(const struct lacpdu_peerinfo *pi,
1593 char *buf, size_t buflen)
1594 {
1595
1596 lacp_systemid_str(buf, buflen, pi->lpi_system_prio,
1597 pi->lpi_system_mac, pi->lpi_key);
1598 }
1599
1600 static void
1601 lacp_state_str(uint8_t state, char *buf, size_t buflen)
1602 {
1603
1604 snprintb(buf, buflen, LACP_STATE_BITS, state);
1605 }
1606
1607 static struct lagg_port *
1608 lacp_select_tx_port(struct lacp_softc *lsc, struct mbuf *m,
1609 struct psref *psref)
1610 {
1611 struct lacp_portmap *pm;
1612 struct lagg_port *lp;
1613 uint32_t hash;
1614 size_t act;
1615 int s;
1616
1617 hash = lagg_hashmbuf(lsc->lsc_softc, m);
1618
1619 s = pserialize_read_enter();
1620 act = LACP_PORTMAP_ACTIVE(lsc);
1621 pm = &lsc->lsc_portmaps[act];
1622
1623 if (pm->pm_count == 0) {
1624 pserialize_read_exit(s);
1625 return NULL;
1626 }
1627
1628 hash %= pm->pm_count;
1629 lp = pm->pm_ports[hash];
1630 lagg_port_getref(lp, psref);
1631
1632 pserialize_read_exit(s);
1633
1634 return lp;
1635 }
1636
1637 static void
1638 lacp_peerinfo_actor(struct lacp_softc *lsc, struct lacp_port *lacpp,
1639 struct lacpdu_peerinfo *dst)
1640 {
1641
1642 memcpy(dst->lpi_system_mac, lsc->lsc_system_mac, LACP_MAC_LEN);
1643 dst->lpi_system_prio = lsc->lsc_system_prio;
1644 dst->lpi_key = lsc->lsc_key;
1645 dst->lpi_port_no = lacpp->lp_actor.lpi_portno;
1646 dst->lpi_port_prio = lacpp->lp_actor.lpi_portprio;
1647 dst->lpi_state = lacpp->lp_actor.lpi_state;
1648 }
1649
1650 static void
1651 lacp_peerinfo_partner(struct lacp_port *lacpp, struct lacpdu_peerinfo *dst)
1652 {
1653 struct lacp_aggregator *la;
1654
1655 la = lacpp->lp_aggregator;
1656
1657 if (la != NULL) {
1658 memcpy(dst->lpi_system_mac, la->la_sid.sid_mac, LACP_MAC_LEN);
1659 dst->lpi_system_prio = la->la_sid.sid_prio;
1660 dst->lpi_key = la->la_sid.sid_key;
1661 } else {
1662 memset(dst->lpi_system_mac, 0, LACP_MAC_LEN);
1663 dst->lpi_system_prio = 0;
1664 dst->lpi_key = 0;
1665 }
1666 dst->lpi_port_no = lacpp->lp_partner.lpi_portno;
1667 dst->lpi_port_prio = lacpp->lp_partner.lpi_portprio;
1668 dst->lpi_state = lacpp->lp_partner.lpi_state;
1669 }
1670
1671 static int
1672 lacp_compare_peerinfo(struct lacpdu_peerinfo *a, struct lacpdu_peerinfo *b)
1673 {
1674
1675 return memcmp(a, b, offsetof(struct lacpdu_peerinfo, lpi_state));
1676 }
1677
1678 static void
1679 lacp_sm_rx_record_default(struct lacp_softc *lsc, struct lacp_port *lacpp)
1680 {
1681 uint8_t oldpstate;
1682 struct lacp_portinfo *pi;
1683 char buf[LACP_STATESTR_LEN] __LACPDEBUGUSED;
1684
1685 pi = &lacpp->lp_partner;
1686
1687 oldpstate = pi->lpi_state;
1688 pi->lpi_portno = htons(LACP_PORTNO_NONE);
1689 pi->lpi_portprio = htons(0xffff);
1690
1691 if (lsc->lsc_optimistic)
1692 pi->lpi_state = LACP_PARTNER_ADMIN_OPTIMISTIC;
1693 else
1694 pi->lpi_state = LACP_PARTNER_ADMIN_STRICT;
1695
1696 SET(lacpp->lp_actor.lpi_state, LACP_STATE_DEFAULTED);
1697
1698 if (oldpstate != pi->lpi_state) {
1699 LACP_STATE_STR(oldpstate, buf, sizeof(buf));
1700 LACP_DPRINTF((lsc, lacpp, "oldpstate %s\n", buf));
1701
1702 LACP_STATE_STR(pi->lpi_state, buf, sizeof(buf));
1703 LACP_DPRINTF((lsc, lacpp, "newpstate %s\n", buf));
1704 }
1705
1706 lacp_sm_ptx_update_timeout(lacpp, oldpstate);
1707 }
1708
1709 static inline bool
1710 lacp_port_is_synced(struct lacp_softc *lsc, struct lacp_port *lacpp,
1711 struct lacpdu_peerinfo *my_pi, struct lacpdu_peerinfo *peer_pi)
1712 {
1713 struct lacpdu_peerinfo actor;
1714
1715 if (!ISSET(peer_pi->lpi_state, LACP_STATE_ACTIVITY) &&
1716 (!ISSET(my_pi->lpi_state, LACP_STATE_ACTIVITY) ||
1717 !ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_ACTIVITY)))
1718 return false;
1719
1720 if (!ISSET(peer_pi->lpi_state, LACP_STATE_AGGREGATION))
1721 return false;
1722
1723 lacp_peerinfo_actor(lsc, lacpp, &actor);
1724 if (lacp_compare_peerinfo(&actor, my_pi) != 0)
1725 return false;
1726
1727 if (!LACP_STATE_EQ(actor.lpi_state, my_pi->lpi_state,
1728 LACP_STATE_AGGREGATION)) {
1729 return false;
1730 }
1731
1732 return true;
1733 }
1734
1735 static void
1736 lacp_sm_rx_record_peerinfo(struct lacp_softc *lsc, struct lacp_port *lacpp,
1737 struct lacpdu_peerinfo *my_pi, struct lacpdu_peerinfo *peer_pi)
1738 {
1739 char buf[LACP_STATESTR_LEN] __LACPDEBUGUSED;
1740 uint8_t oldpstate;
1741 struct lacp_portinfo *pi;
1742 struct lacp_aggregator_systemid *sid;
1743
1744 pi = &lacpp->lp_partner;
1745 sid = &lacpp->lp_aggregator_sidbuf;
1746
1747 oldpstate = lacpp->lp_partner.lpi_state;
1748
1749 sid->sid_prio = peer_pi->lpi_system_prio;
1750 sid->sid_key = peer_pi->lpi_key;
1751 memcpy(sid->sid_mac, peer_pi->lpi_system_mac,
1752 sizeof(sid->sid_mac));
1753
1754 pi->lpi_portno = peer_pi->lpi_port_no;
1755 pi->lpi_portprio = peer_pi->lpi_port_prio;
1756 pi->lpi_state = peer_pi->lpi_state;
1757
1758 if (lacp_port_is_synced(lsc, lacpp, my_pi, peer_pi)) {
1759 if (lsc->lsc_optimistic)
1760 SET(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
1761 } else {
1762 CLR(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
1763 }
1764
1765 CLR(lacpp->lp_actor.lpi_state, LACP_STATE_DEFAULTED);
1766
1767 if (oldpstate != lacpp->lp_partner.lpi_state) {
1768 LACP_STATE_STR(oldpstate, buf, sizeof(buf));
1769 LACP_DPRINTF((lsc, lacpp, "oldpstate %s\n", buf));
1770
1771 LACP_STATE_STR(lacpp->lp_partner.lpi_state,
1772 buf, sizeof(buf));
1773 LACP_DPRINTF((lsc, lacpp, "newpstate %s\n", buf));
1774 }
1775
1776 lacp_sm_ptx_update_timeout(lacpp, oldpstate);
1777 }
1778
1779 static void
1780 lacp_sm_rx_set_expired(struct lacp_port *lacpp)
1781 {
1782 uint8_t oldpstate;
1783
1784 oldpstate = lacpp->lp_partner.lpi_state;
1785
1786 CLR(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
1787 SET(lacpp->lp_partner.lpi_state, LACP_STATE_TIMEOUT);
1788 LACP_TIMER_ARM(lacpp, LACP_TIMER_CURRENT_WHILE,
1789 LACP_SHORT_TIMEOUT_TIME);
1790 SET(lacpp->lp_actor.lpi_state, LACP_STATE_EXPIRED);
1791
1792 lacp_sm_ptx_update_timeout(lacpp, oldpstate);
1793 }
1794
1795 static void
1796 lacp_sm_port_init(struct lacp_softc *lsc, struct lacp_port *lacpp,
1797 struct lagg_port *lp)
1798 {
1799
1800 KASSERT(LACP_LOCKED(lsc));
1801
1802 lacpp->lp_laggport = lp;
1803 lacpp->lp_actor.lpi_state = LACP_STATE_ACTIVITY;
1804 lacpp->lp_actor.lpi_portno = htons(if_get_index(lp->lp_ifp));
1805 lacpp->lp_actor.lpi_portprio = htons(LACP_PORT_PRIO);
1806 lacpp->lp_partner.lpi_state = LACP_STATE_TIMEOUT;
1807 lacpp->lp_aggregator = NULL;
1808 lacpp->lp_marker_xid = 0;
1809 lacpp->lp_mux_state = LACP_MUX_INIT;
1810
1811 lacp_set_mux(lsc, lacpp, LACP_MUX_DETACHED);
1812 lacp_sm_rx_record_default(lsc, lacpp);
1813 }
1814
1815 static void
1816 lacp_port_disable(struct lacp_softc *lsc, struct lacp_port *lacpp)
1817 {
1818
1819 KASSERT(LACP_LOCKED(lsc));
1820
1821 if (ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_AGGREGATION))
1822 LACP_DPRINTF((lsc, lacpp, "enable -> disable\n"));
1823
1824 lacp_set_mux(lsc, lacpp, LACP_MUX_DETACHED);
1825 lacp_sm_rx_record_default(lsc, lacpp);
1826 CLR(lacpp->lp_actor.lpi_state,
1827 LACP_STATE_AGGREGATION | LACP_STATE_EXPIRED);
1828 CLR(lacpp->lp_partner.lpi_state, LACP_STATE_AGGREGATION);
1829 }
1830
1831 static void
1832 lacp_port_enable(struct lacp_softc *lsc __LACPDEBUGUSED,
1833 struct lacp_port *lacpp)
1834 {
1835
1836 KASSERT(LACP_LOCKED(lsc));
1837
1838 if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_AGGREGATION))
1839 LACP_DPRINTF((lsc, lacpp, "disable -> enable\n"));
1840
1841 SET(lacpp->lp_actor.lpi_state, LACP_STATE_AGGREGATION);
1842 lacp_sm_rx_set_expired(lacpp);
1843 }
1844
1845 static void
1846 lacp_sm_rx_timer(struct lacp_softc *lsc, struct lacp_port *lacpp)
1847 {
1848
1849 KASSERT(LACP_LOCKED(lsc));
1850
1851 if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_EXPIRED)) {
1852 /* CURRENT -> EXPIRED */
1853 LACP_DPRINTF((lsc, lacpp, "CURRENT -> EXPIRED\n"));
1854 lacp_sm_rx_set_expired(lacpp);
1855 } else {
1856 LACP_DPRINTF((lsc, lacpp, "EXPIRED -> DEFAULTED\n"));
1857 lacp_set_mux(lsc, lacpp, LACP_MUX_DETACHED);
1858 lacp_sm_rx_record_default(lsc, lacpp);
1859 }
1860 }
1861
1862 static void
1863 lacp_sm_ptx_timer(struct lacp_softc *lsc __unused, struct lacp_port *lacpp)
1864 {
1865
1866 KASSERT(LACP_LOCKED(lsc));
1867 lacp_sm_assert_ntt(lacpp);
1868 }
1869
1870 static void
1871 lacp_sm_ptx_schedule(struct lacp_port *lacpp)
1872 {
1873 int timeout;
1874
1875 /* no periodic */
1876 if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_ACTIVITY) &&
1877 !ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_ACTIVITY)) {
1878 LACP_TIMER_DISARM(lacpp, LACP_TIMER_PERIODIC);
1879 return;
1880 }
1881
1882 if (LACP_TIMER_ISARMED(lacpp, LACP_TIMER_PERIODIC))
1883 return;
1884
1885 timeout = ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_TIMEOUT) ?
1886 LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME;
1887
1888 LACP_TIMER_ARM(lacpp, LACP_TIMER_PERIODIC, timeout);
1889 }
1890
1891 static void
1892 lacp_sm_ptx_update_timeout(struct lacp_port *lacpp, uint8_t oldpstate)
1893 {
1894
1895 if (LACP_STATE_EQ(oldpstate, lacpp->lp_partner.lpi_state,
1896 LACP_STATE_TIMEOUT))
1897 return;
1898
1899 LACP_DPRINTF((NULL, lacpp, "partner timeout changed\n"));
1900
1901 LACP_TIMER_DISARM(lacpp, LACP_TIMER_PERIODIC);
1902
1903 /* if timeout has been shorted, assert NTT */
1904 if (ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_TIMEOUT))
1905 lacp_sm_assert_ntt(lacpp);
1906 }
1907
1908 static void
1909 lacp_sm_mux_timer(struct lacp_softc *lsc __LACPDEBUGUSED,
1910 struct lacp_port *lacpp)
1911 {
1912 char buf[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
1913
1914 KASSERT(LACP_LOCKED(lsc));
1915 KASSERT(lacpp->lp_pending > 0);
1916
1917 LACP_AGGREGATOR_STR(lacpp->lp_aggregator, buf, sizeof(buf));
1918 LACP_DPRINTF((lsc, lacpp, "aggregator %s, pending %d -> %d\n",
1919 buf, lacpp->lp_pending, lacpp->lp_pending -1));
1920
1921 lacpp->lp_pending--;
1922 }
1923
1924 static void
1925 lacp_sm_rx_update_selected(struct lacp_softc *lsc, struct lacp_port *lacpp,
1926 struct lacpdu_peerinfo *peer_pi)
1927 {
1928 struct lacpdu_peerinfo partner;
1929 char str0[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
1930 char str1[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
1931
1932 if (lacpp->lp_aggregator == NULL)
1933 return;
1934
1935 lacp_peerinfo_partner(lacpp, &partner);
1936 if (lacp_compare_peerinfo(peer_pi, &partner) != 0) {
1937 LACP_PEERINFO_IDSTR(&partner, str0, sizeof(str0));
1938 LACP_PEERINFO_IDSTR(peer_pi, str1, sizeof(str1));
1939 LACP_DPRINTF((lsc, lacpp,
1940 "different peerinfo, %s vs %s\n", str0, str1));
1941 goto do_unselect;
1942 }
1943
1944 if (!LACP_STATE_EQ(lacpp->lp_partner.lpi_state,
1945 peer_pi->lpi_state, LACP_STATE_AGGREGATION)) {
1946 LACP_DPRINTF((lsc, lacpp,
1947 "STATE_AGGREGATION changed %d -> %d\n",
1948 ISSET(lacpp->lp_partner.lpi_state,
1949 LACP_STATE_AGGREGATION) != 0,
1950 ISSET(peer_pi->lpi_state, LACP_STATE_AGGREGATION) != 0));
1951 goto do_unselect;
1952 }
1953
1954 return;
1955
1956 do_unselect:
1957 lacpp->lp_selected = LACP_UNSELECTED;
1958 /* lacpp->lp_aggregator will be released at lacp_set_mux() */
1959 }
1960
1961 static void
1962 lacp_sm_rx_update_ntt(struct lacp_softc *lsc, struct lacp_port *lacpp,
1963 struct lacpdu_peerinfo *my_pi)
1964 {
1965 struct lacpdu_peerinfo actor;
1966
1967 lacp_peerinfo_actor(lsc, lacpp, &actor);
1968
1969 if (lacp_compare_peerinfo(&actor, my_pi) != 0 ||
1970 !LACP_STATE_EQ(lacpp->lp_actor.lpi_state, my_pi->lpi_state,
1971 LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) {
1972 LACP_DPRINTF((lsc, lacpp, "assert ntt\n"));
1973 lacp_sm_assert_ntt(lacpp);
1974 }
1975 }
1976
1977 static void
1978 lacp_sm_rx(struct lacp_softc *lsc, struct lacp_port *lacpp,
1979 struct lacpdu_peerinfo *my_pi, struct lacpdu_peerinfo *peer_pi)
1980 {
1981 int timeout;
1982
1983 KASSERT(LACP_LOCKED(lsc));
1984
1985 /* check LACP disabled first */
1986 if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_AGGREGATION))
1987 return;
1988
1989 /* check loopback condition */
1990 if (memcmp(lsc->lsc_system_mac, peer_pi->lpi_system_mac,
1991 LACP_MAC_LEN) == 0 &&
1992 lsc->lsc_system_prio == peer_pi->lpi_system_prio)
1993 return;
1994
1995 lacp_sm_rx_update_selected(lsc, lacpp, peer_pi);
1996 lacp_sm_rx_update_ntt(lsc, lacpp, my_pi);
1997 lacp_sm_rx_record_peerinfo(lsc, lacpp, my_pi, peer_pi);
1998
1999 timeout = ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_TIMEOUT) ?
2000 LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME;
2001 LACP_TIMER_ARM(lacpp, LACP_TIMER_CURRENT_WHILE, timeout);
2002
2003 CLR(lacpp->lp_actor.lpi_state, LACP_STATE_EXPIRED);
2004
2005 /* kick transmit machine without timeout. */
2006 lacp_sm_tx(lsc, lacpp);
2007 }
2008
2009 static void
2010 lacp_disable_collecting(struct lacp_port *lacpp)
2011 {
2012
2013 LACP_DPRINTF((NULL, lacpp, "collecting disabled\n"));
2014 CLR(lacpp->lp_actor.lpi_state, LACP_STATE_COLLECTING);
2015 atomic_store_relaxed(&lacpp->lp_collector, false);
2016 }
2017
2018 static void
2019 lacp_enable_collecting(struct lacp_port *lacpp)
2020 {
2021 LACP_DPRINTF((NULL, lacpp, "collecting enabled\n"));
2022 SET(lacpp->lp_actor.lpi_state, LACP_STATE_COLLECTING);
2023 atomic_store_relaxed(&lacpp->lp_collector, true);
2024 }
2025
2026 static void
2027 lacp_update_portmap(struct lacp_softc *lsc)
2028 {
2029 struct lagg_softc *sc;
2030 struct lacp_aggregator *la;
2031 struct lacp_portmap *pm_act, *pm_next;
2032 struct lacp_port *lacpp;
2033 size_t pmap, n;
2034 u_int link;
2035
2036 KASSERT(LACP_LOCKED(lsc));
2037
2038 la = lsc->lsc_aggregator;
2039
2040 pmap = LACP_PORTMAP_ACTIVE(lsc);
2041 pm_act = &lsc->lsc_portmaps[pmap];
2042
2043 pmap = LACP_PORTMAP_NEXT(lsc);
2044 pm_next = &lsc->lsc_portmaps[pmap];
2045
2046 n = 0;
2047 if (la != NULL) {
2048 LIST_FOREACH(lacpp, &la->la_ports, lp_entry_la) {
2049 if (!ISSET(lacpp->lp_actor.lpi_state,
2050 LACP_STATE_DISTRIBUTING)) {
2051 continue;
2052 }
2053
2054 pm_next->pm_ports[n] = lacpp->lp_laggport;
2055 n++;
2056
2057 if (n >= LACP_MAX_PORTS)
2058 break;
2059 }
2060 }
2061 pm_next->pm_count = n;
2062
2063 atomic_store_release(&lsc->lsc_activemap, pmap);
2064 pserialize_perform(lsc->lsc_psz);
2065
2066 LACP_DPRINTF((lsc, NULL, "portmap count updated (%zu -> %zu)\n",
2067 pm_act->pm_count, pm_next->pm_count));
2068
2069 link = lacp_portmap_linkstate(pm_next);
2070 if (link != lacp_portmap_linkstate(pm_act)) {
2071 sc = lsc->lsc_softc;
2072 if_link_state_change(&sc->sc_if, link);
2073 }
2074
2075 lagg_workq_add(lsc->lsc_workq, &lsc->lsc_work_linkspeed);
2076
2077 /* cleanup */
2078 pm_act->pm_count = 0;
2079 memset(pm_act->pm_ports, 0, sizeof(pm_act->pm_ports));
2080 }
2081
2082 static void
2083 lacp_disable_distributing(struct lacp_softc *lsc, struct lacp_port *lacpp)
2084 {
2085 struct lacp_portmap *pm;
2086 bool do_update;
2087 size_t act, i;
2088 int s;
2089
2090 KASSERT(LACP_LOCKED(lsc));
2091
2092 if (ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_DISTRIBUTING)) {
2093 LAGG_LOG(lsc->lsc_softc, LOG_INFO,
2094 "disable distributing on %s\n", LACP_PORT_XNAME(lacpp));
2095 CLR(lacpp->lp_actor.lpi_state, LACP_STATE_DISTRIBUTING);
2096 }
2097
2098 s = pserialize_read_enter();
2099 act = LACP_PORTMAP_ACTIVE(lsc);
2100 pm = &lsc->lsc_portmaps[act];
2101
2102 do_update = false;
2103 for (i = 0; i < pm->pm_count; i++) {
2104 if (pm->pm_ports[i] == lacpp->lp_laggport) {
2105 do_update = true;
2106 break;
2107 }
2108 }
2109 pserialize_read_exit(s);
2110
2111 if (do_update)
2112 lacp_update_portmap(lsc);
2113 }
2114
2115 static void
2116 lacp_enable_distributing(struct lacp_softc *lsc, struct lacp_port *lacpp)
2117 {
2118
2119 KASSERT(LACP_LOCKED(lsc));
2120
2121 KASSERT(lacp_isactive(lsc, lacpp));
2122
2123 LAGG_LOG(lsc->lsc_softc, LOG_INFO,
2124 "enable distributing on %s\n", LACP_PORT_XNAME(lacpp));
2125 SET(lacpp->lp_actor.lpi_state, LACP_STATE_DISTRIBUTING);
2126 lacp_suppress_distributing(lsc);
2127 lacp_update_portmap(lsc);
2128 }
2129
2130 static void
2131 lacp_select_active_aggregator(struct lacp_softc *lsc)
2132 {
2133 struct lacp_aggregator *la, *best_la;
2134 char str[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
2135
2136 KASSERT(LACP_LOCKED(lsc));
2137
2138 la = lsc->lsc_aggregator;
2139 if (la != NULL && la->la_attached_port > 0) {
2140 best_la = la;
2141 } else {
2142 best_la = NULL;
2143 }
2144
2145 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
2146 if (la->la_attached_port <= 0)
2147 continue;
2148
2149 if (best_la == NULL ||
2150 LACP_SYS_PRI(la) < LACP_SYS_PRI(best_la))
2151 best_la = la;
2152 }
2153
2154 if (best_la != lsc->lsc_aggregator) {
2155 LACP_DPRINTF((lsc, NULL, "active aggregator changed\n"));
2156
2157 if (lsc->lsc_aggregator != NULL) {
2158 LACP_AGGREGATOR_STR(lsc->lsc_aggregator,
2159 str, sizeof(str));
2160 } else {
2161 snprintf(str, sizeof(str), "(null)");
2162 }
2163 LACP_DPRINTF((lsc, NULL, "old aggregator=%s\n", str));
2164
2165 if (best_la != NULL) {
2166 LACP_AGGREGATOR_STR(best_la, str, sizeof(str));
2167 } else {
2168 snprintf(str, sizeof(str), "(null)");
2169 }
2170 LACP_DPRINTF((lsc, NULL, "new aggregator=%s\n", str));
2171
2172 lsc->lsc_aggregator = best_la;
2173 }
2174 }
2175
2176 static void
2177 lacp_port_attached(struct lacp_softc *lsc, struct lacp_port *lacpp)
2178 {
2179 struct lacp_aggregator *la;
2180
2181 KASSERT(LACP_LOCKED(lsc));
2182
2183 if (ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_SYNC))
2184 return;
2185
2186 la = lacpp->lp_aggregator;
2187 KASSERT(la != NULL);
2188 KASSERT(la->la_attached_port >= 0);
2189
2190 SET(lacpp->lp_actor.lpi_state, LACP_STATE_SYNC);
2191 la->la_attached_port++;
2192 lacp_select_active_aggregator(lsc);
2193 }
2194
2195 static void
2196 lacp_port_detached(struct lacp_softc *lsc, struct lacp_port *lacpp)
2197 {
2198 struct lacp_aggregator *la;
2199
2200 KASSERT(LACP_LOCKED(lsc));
2201
2202 if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_SYNC))
2203 return;
2204
2205 la = lacpp->lp_aggregator;
2206 KASSERT(la != NULL);
2207 KASSERT(la->la_attached_port > 0);
2208
2209 CLR(lacpp->lp_actor.lpi_state, LACP_STATE_SYNC);
2210 la->la_attached_port--;
2211 lacp_select_active_aggregator(lsc);
2212 }
2213
2214 static int
2215 lacp_set_mux(struct lacp_softc *lsc, struct lacp_port *lacpp,
2216 enum lacp_mux_state new_state)
2217 {
2218 struct lagg_softc *sc;
2219 struct ifnet *ifp;
2220
2221 KASSERT(LACP_LOCKED(lsc));
2222
2223 sc = lacpp->lp_laggport->lp_softc;
2224 ifp = &sc->sc_if;
2225
2226 if (lacpp->lp_mux_state == new_state)
2227 return -1;
2228
2229 switch (new_state) {
2230 case LACP_MUX_DETACHED:
2231 lacp_port_detached(lsc, lacpp);
2232 lacp_disable_distributing(lsc, lacpp);
2233 lacp_disable_collecting(lacpp);
2234 lacp_sm_assert_ntt(lacpp);
2235 /* cancel timer */
2236 if (LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE)) {
2237 KASSERT(lacpp->lp_pending > 0);
2238 lacpp->lp_pending--;
2239 LACP_TIMER_DISARM(lacpp, LACP_TIMER_WAIT_WHILE);
2240 }
2241 lacp_unselect(lsc, lacpp);
2242 break;
2243 case LACP_MUX_WAITING:
2244 LACP_TIMER_ARM(lacpp, LACP_TIMER_WAIT_WHILE,
2245 LACP_AGGREGATE_WAIT_TIME);
2246 lacpp->lp_pending++;
2247 break;
2248 case LACP_MUX_STANDBY:
2249 #ifdef LACP_STANDBY_SYNCED
2250 lacp_port_attached(lsc, lacpp);
2251 lacp_disable_collecting(lacpp);
2252 lacp_sm_assert_ntt(lacpp);
2253 #endif
2254 break;
2255 case LACP_MUX_ATTACHED:
2256 lacp_port_attached(lsc, lacpp);
2257 lacp_disable_collecting(lacpp);
2258 lacp_sm_assert_ntt(lacpp);
2259 break;
2260 case LACP_MUX_COLLECTING:
2261 lacp_enable_collecting(lacpp);
2262 lacp_disable_distributing(lsc, lacpp);
2263 lacp_sm_assert_ntt(lacpp);
2264 break;
2265 case LACP_MUX_DISTRIBUTING:
2266 lacp_enable_distributing(lsc, lacpp);
2267 break;
2268 case LACP_MUX_INIT:
2269 default:
2270 panic("%s: unknown state", ifp->if_xname);
2271 }
2272
2273 LACP_DPRINTF((lsc, lacpp, "mux_state %d -> %d\n",
2274 lacpp->lp_mux_state, new_state));
2275
2276 lacpp->lp_mux_state = new_state;
2277 return 0;
2278 }
2279
2280 static void
2281 lacp_sm_mux(struct lacp_softc *lsc, struct lacp_port *lacpp)
2282 {
2283 struct lacp_aggregator *la __diagused;
2284 enum lacp_mux_state next_state;
2285 enum lacp_selected selected;
2286 bool p_sync, p_collecting;
2287
2288 p_sync = ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
2289 p_collecting = ISSET(lacpp->lp_partner.lpi_state,
2290 LACP_STATE_COLLECTING);
2291
2292 do {
2293 next_state = lacpp->lp_mux_state;
2294 la = lacpp->lp_aggregator;
2295 selected = lacpp->lp_selected;
2296 KASSERT(la != NULL ||
2297 lacpp->lp_mux_state == LACP_MUX_DETACHED);
2298
2299 switch (lacpp->lp_mux_state) {
2300 case LACP_MUX_DETACHED:
2301 if (selected != LACP_UNSELECTED)
2302 next_state = LACP_MUX_WAITING;
2303 break;
2304 case LACP_MUX_WAITING:
2305 KASSERTMSG((lacpp->lp_pending > 0 ||
2306 !LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE)),
2307 "lp_pending=%d, timer=%d", lacpp->lp_pending,
2308 !LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE));
2309
2310 if (selected == LACP_UNSELECTED) {
2311 next_state = LACP_MUX_DETACHED;
2312 } else if (lacpp->lp_pending == 0) {
2313 if (selected == LACP_SELECTED) {
2314 next_state = LACP_MUX_ATTACHED;
2315 } else if (selected == LACP_STANDBY) {
2316 next_state = LACP_MUX_STANDBY;
2317 } else {
2318 next_state = LACP_MUX_DETACHED;
2319 }
2320 }
2321 break;
2322 case LACP_MUX_STANDBY:
2323 if (selected == LACP_SELECTED) {
2324 next_state = LACP_MUX_ATTACHED;
2325 } else if (selected != LACP_STANDBY) {
2326 next_state = LACP_MUX_DETACHED;
2327 }
2328 break;
2329 case LACP_MUX_ATTACHED:
2330 if (selected != LACP_SELECTED) {
2331 if (selected == LACP_STANDBY)
2332 LAGG_LOG(lsc->lsc_softc, LOG_INFO,
2333 "detaching %s\n",
2334 LACP_PORT_XNAME(lacpp));
2335 next_state = LACP_MUX_DETACHED;
2336 } else if (lacp_isactive(lsc, lacpp) && p_sync) {
2337 next_state = LACP_MUX_COLLECTING;
2338 }
2339 break;
2340 case LACP_MUX_COLLECTING:
2341 if (selected != LACP_SELECTED ||
2342 !lacp_isactive(lsc, lacpp)
2343 || !p_sync) {
2344 next_state = LACP_MUX_ATTACHED;
2345 } else if (p_collecting) {
2346 next_state = LACP_MUX_DISTRIBUTING;
2347 }
2348 break;
2349 case LACP_MUX_DISTRIBUTING:
2350 if (selected != LACP_SELECTED ||
2351 !lacp_isactive(lsc, lacpp)
2352 || !p_sync || !p_collecting) {
2353 next_state = LACP_MUX_COLLECTING;
2354 LACP_DPRINTF((lsc, lacpp,
2355 "Interface stopped DISTRIBUTING,"
2356 " possible flapping\n"));
2357 }
2358 break;
2359 case LACP_MUX_INIT:
2360 default:
2361 panic("%s: unknown state",
2362 lsc->lsc_softc->sc_if.if_xname);
2363 }
2364 } while (lacp_set_mux(lsc, lacpp, next_state) == 0);
2365 }
2366
2367 static bool
2368 lacp_aggregator_is_match(struct lacp_aggregator_systemid *a,
2369 struct lacp_aggregator_systemid *b)
2370 {
2371
2372 if (a->sid_prio != b->sid_prio)
2373 return false;
2374
2375 if (a->sid_key != b->sid_key)
2376 return false;
2377
2378 if (memcmp(a->sid_mac, b->sid_mac, sizeof(a->sid_mac)) != 0)
2379 return false;
2380
2381 return true;
2382 }
2383
2384 static void
2385 lacp_selected_update(struct lacp_softc *lsc, struct lacp_aggregator *la)
2386 {
2387 struct lacp_port *lacpp;
2388 enum lacp_selected next_selected;
2389 const char *msg;
2390 size_t nselected;
2391 uint64_t linkspeed;
2392
2393 KASSERT(LACP_LOCKED(lsc));
2394
2395 lacpp = LIST_FIRST(&la->la_ports);
2396 if (lacpp == NULL)
2397 return;
2398
2399 linkspeed = lacpp->lp_linkspeed;
2400 nselected = 0;
2401 LIST_FOREACH(lacpp, &la->la_ports, lp_entry_la) {
2402 if (lacpp->lp_selected == LACP_UNSELECTED)
2403 continue;
2404
2405 next_selected = LACP_SELECTED;
2406 msg = " is selected";
2407
2408 if (nselected >= lsc->lsc_max_ports) {
2409 next_selected = LACP_STANDBY;
2410 msg = " is standby because of too many active ports";
2411 }
2412
2413 if (!lsc->lsc_multi_linkspeed &&
2414 linkspeed != lacpp->lp_linkspeed) {
2415 next_selected = LACP_STANDBY;
2416 msg = " is standby because of link speed mismatch";
2417 }
2418
2419 if (lacpp->lp_selected != next_selected) {
2420 lacpp->lp_selected = next_selected;
2421 LAGG_LOG(lsc->lsc_softc, LOG_INFO,
2422 "%s%s\n", LACP_PORT_XNAME(lacpp), msg);
2423 }
2424
2425 if (lacpp->lp_selected == LACP_SELECTED)
2426 nselected++;
2427 }
2428 }
2429
2430 static void
2431 lacp_select(struct lacp_softc *lsc, struct lacp_port *lacpp)
2432 {
2433 struct lacp_aggregator *la;
2434 struct lacp_aggregator_systemid *sid;
2435 struct lacp_port *lacpp0;
2436 char buf[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
2437
2438 if (lacpp->lp_aggregator != NULL)
2439 return;
2440
2441 /* If we haven't heard from our peer, skip this step. */
2442 if (ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_DEFAULTED))
2443 return
2444
2445 KASSERT(!LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE));
2446
2447 sid = &lacpp->lp_aggregator_sidbuf;
2448
2449 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
2450 if (lacp_aggregator_is_match(&la->la_sid, sid))
2451 break;
2452 }
2453
2454 if (la == NULL) {
2455 la = kmem_zalloc(sizeof(*la), KM_NOSLEEP);
2456 if (la == NULL) {
2457 LACP_DPRINTF((lsc, lacpp,
2458 "couldn't allocate aggregator\n"));
2459 /* will retry the next tick. */
2460 return;
2461 }
2462 LIST_INIT(&la->la_ports);
2463
2464 la->la_sid = *sid;
2465 TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
2466 LACP_DPRINTF((lsc, lacpp, "a new aggregator created\n"));
2467 } else {
2468 LACP_DPRINTF((lsc, lacpp, "aggregator found\n"));
2469 }
2470
2471 KASSERT(la != NULL);
2472 LACP_AGGREGATOR_STR(la, buf, sizeof(buf));
2473 LACP_DPRINTF((lsc, lacpp, "aggregator lagid=%s\n", buf));
2474
2475 lacpp->lp_aggregator = la;
2476 lacpp->lp_selected = LACP_READY;
2477
2478 LIST_FOREACH(lacpp0, &la->la_ports, lp_entry_la) {
2479 if (lacp_port_priority_max(lacpp0, lacpp) == lacpp) {
2480 LIST_INSERT_BEFORE(lacpp0, lacpp, lp_entry_la);
2481 break;
2482 }
2483
2484 if (LIST_NEXT(lacpp0, lp_entry_la) == NULL) {
2485 LIST_INSERT_AFTER(lacpp0, lacpp, lp_entry_la);
2486 break;
2487 }
2488 }
2489
2490 if (lacpp0 == NULL)
2491 LIST_INSERT_HEAD(&la->la_ports, lacpp, lp_entry_la);
2492
2493 lacp_selected_update(lsc, la);
2494 }
2495
2496 static void
2497 lacp_unselect(struct lacp_softc *lsc, struct lacp_port *lacpp)
2498 {
2499 struct lacp_aggregator *la;
2500 char buf[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
2501 bool remove_actaggr;
2502
2503 KASSERT(LACP_LOCKED(lsc));
2504 KASSERT(!LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE));
2505
2506 la = lacpp->lp_aggregator;
2507 lacpp->lp_selected = LACP_UNSELECTED;
2508
2509 if (la == NULL)
2510 return;
2511
2512 KASSERT(!LIST_EMPTY(&la->la_ports));
2513
2514 LACP_AGGREGATOR_STR(la, buf, sizeof(buf));
2515 LACP_DPRINTF((lsc, lacpp, "unselect aggregator lagid=%s\n", buf));
2516
2517 LIST_REMOVE(lacpp, lp_entry_la);
2518 lacpp->lp_aggregator = NULL;
2519
2520 if (LIST_EMPTY(&la->la_ports)) {
2521 remove_actaggr = false;
2522
2523 if (la == lsc->lsc_aggregator) {
2524 LACP_DPRINTF((lsc, NULL, "remove active aggregator\n"));
2525 lsc->lsc_aggregator = NULL;
2526 remove_actaggr = true;
2527 }
2528
2529 TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
2530 kmem_free(la, sizeof(*la));
2531
2532 if (remove_actaggr)
2533 lacp_select_active_aggregator(lsc);
2534 } else {
2535 lacp_selected_update(lsc, la);
2536 }
2537 }
2538
2539 static void
2540 lacp_suppress_distributing(struct lacp_softc *lsc)
2541 {
2542 struct lacp_aggregator *la;
2543 struct lacp_port *lacpp;
2544
2545 KASSERT(LACP_LOCKED(lsc));
2546
2547 la = lsc->lsc_aggregator;
2548
2549 LIST_FOREACH(lacpp, &la->la_ports, lp_entry_la) {
2550 if (ISSET(lacpp->lp_actor.lpi_state,
2551 LACP_STATE_DISTRIBUTING)) {
2552 lagg_workq_add(lsc->lsc_workq,
2553 &lacpp->lp_work_marker);
2554 }
2555 }
2556
2557 LACP_PTIMER_ARM(lsc, LACP_PTIMER_DISTRIBUTING,
2558 LACP_TRANSIT_DELAY);
2559 }
2560
2561 static void
2562 lacp_distributing_timer(struct lacp_softc *lsc)
2563 {
2564
2565 KASSERT(LACP_LOCKED(lsc));
2566
2567 if (lsc->lsc_suppress_distributing) {
2568 LACP_DPRINTF((lsc, NULL,
2569 "disable suppress distributing\n"));
2570 lsc->lsc_suppress_distributing = false;
2571 }
2572 }
2573
2574 static struct mbuf *
2575 lacp_markerdu_mbuf(struct lacp_softc *lsc, struct lacp_port *lacpp)
2576 {
2577 struct ifnet *ifp_port;
2578 struct mbuf *m;
2579 struct markerdu *mdu;
2580 struct markerdu_info *mi;
2581
2582 KASSERT(LACP_LOCKED(lsc));
2583
2584 ifp_port = lacpp->lp_laggport->lp_ifp;
2585
2586 MGETHDR(m, M_DONTWAIT, MT_DATA);
2587 if (m == NULL) {
2588 lsc->lsc_mgethdr_failed.ev_count++;
2589 return NULL;
2590 }
2591
2592 m->m_pkthdr.len = m->m_len = sizeof(*mdu);
2593 m_reset_rcvif(m);
2594
2595 mdu = mtod(m, struct markerdu *);
2596
2597 memset(mdu, 0, sizeof(*mdu));
2598
2599 m->m_flags |= M_MCAST;
2600 memcpy(mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
2601 ETHER_ADDR_LEN);
2602 memcpy(mdu->mdu_eh.ether_shost, CLLADDR(ifp_port->if_sadl),
2603 ETHER_ADDR_LEN);
2604 mdu->mdu_eh.ether_type = ntohs(ETHERTYPE_SLOWPROTOCOLS);
2605 mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER;
2606 mdu->mdu_sph.sph_version = 1;
2607
2608 mi = &mdu->mdu_info;
2609 tlv_set(&mdu->mdu_tlv_info, MARKER_TYPE_INFO,
2610 sizeof(*mi));
2611 mi->mi_rq_port = lacpp->lp_actor.lpi_portno;
2612 mi->mi_rq_xid = htonl(lacpp->lp_marker_xid);
2613 memcpy(mi->mi_rq_system, lsc->lsc_system_mac, LACP_MAC_LEN);
2614
2615 mdu->mdu_tlv_term.tlv_type = MARKER_TYPE_TERMINATE;
2616 mdu->mdu_tlv_term.tlv_length = 0;
2617
2618 return m;
2619 }
2620
2621 static void
2622 lacp_marker_work(struct lagg_work *lw, void *xlsc)
2623 {
2624 struct lacp_softc *lsc;
2625 struct lacp_port *lacpp;
2626 struct lagg_port *lp;
2627 struct markerdu *mdu;
2628 struct mbuf *m;
2629 struct psref psref;
2630 int bound;
2631
2632 lsc = xlsc;
2633 lacpp = container_of(lw, struct lacp_port, lp_work_marker);
2634
2635 LACP_LOCK(lsc);
2636 lacpp->lp_marker_xid++;
2637 m = lacp_markerdu_mbuf(lsc, lacpp);
2638 if (m == NULL) {
2639 LACP_UNLOCK(lsc);
2640 return;
2641 }
2642 SET(lacpp->lp_flags, LACP_PORT_MARK);
2643 lsc->lsc_suppress_distributing = true;
2644 lp = lacpp->lp_laggport;
2645 bound = curlwp_bind();
2646 lagg_port_getref(lp, &psref);
2647 LACP_UNLOCK(lsc);
2648
2649 if (LACP_ISDUMPING(lsc)) {
2650 lacp_dprintf(lsc, lacpp, "markerdu transmit\n");
2651 mdu = mtod(m, struct markerdu *);
2652 lacp_dump_markertlv(&mdu->mdu_info, NULL);
2653 }
2654
2655 lagg_port_xmit(lp, m);
2656 lagg_port_putref(lp, &psref);
2657 curlwp_bindx(bound);
2658 }
2659
2660 static void
2661 lacp_dump_lacpdutlv(const struct lacpdu_peerinfo *pi_actor,
2662 const struct lacpdu_peerinfo *pi_partner,
2663 const struct lacpdu_collectorinfo *lci)
2664 {
2665 char str[LACP_STATESTR_LEN];
2666
2667 if (pi_actor != NULL) {
2668 lacp_peerinfo_idstr(pi_actor, str, sizeof(str));
2669 printf("actor=%s\n", str);
2670 lacp_state_str(pi_actor->lpi_state,
2671 str, sizeof(str));
2672 printf("actor.state=%s portno=%d portprio=0x%04x\n",
2673 str,
2674 ntohs(pi_actor->lpi_port_no),
2675 ntohs(pi_actor->lpi_port_prio));
2676 } else {
2677 printf("no actor info\n");
2678 }
2679
2680 if (pi_partner != NULL) {
2681 lacp_peerinfo_idstr(pi_partner, str, sizeof(str));
2682 printf("partner=%s\n", str);
2683 lacp_state_str(pi_partner->lpi_state,
2684 str, sizeof(str));
2685 printf("partner.state=%s portno=%d portprio=0x%04x\n",
2686 str,
2687 ntohs(pi_partner->lpi_port_no),
2688 ntohs(pi_partner->lpi_port_prio));
2689 } else {
2690 printf("no partner info\n");
2691 }
2692
2693 if (lci != NULL) {
2694 printf("maxdelay=%d\n", ntohs(lci->lci_maxdelay));
2695 } else {
2696 printf("no collector info\n");
2697 }
2698 }
2699
2700 static void
2701 lacp_dump_markertlv(const struct markerdu_info *mi_info,
2702 const struct markerdu_info *mi_res)
2703 {
2704
2705 if (mi_info != NULL) {
2706 printf("marker info: port=%d, sys=%s, id=%u\n",
2707 ntohs(mi_info->mi_rq_port),
2708 ether_sprintf(mi_info->mi_rq_system),
2709 ntohl(mi_info->mi_rq_xid));
2710 }
2711
2712 if (mi_res != NULL) {
2713 printf("marker resp: port=%d, sys=%s, id=%u\n",
2714 ntohs(mi_res->mi_rq_port),
2715 ether_sprintf(mi_res->mi_rq_system),
2716 ntohl(mi_res->mi_rq_xid));
2717 }
2718 }
2719
2720 static void
2721 lacp_linkstate(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
2722 {
2723
2724 IFNET_ASSERT_UNLOCKED(lp->lp_ifp);
2725
2726 IFNET_LOCK(lp->lp_ifp);
2727 lacp_linkstate_ifnet_locked(xlsc, lp);
2728 IFNET_UNLOCK(lp->lp_ifp);
2729 }
2730
2731 static void
2732 lacp_linkspeed_work(struct lagg_work *lw __unused, void *xlsc)
2733 {
2734 struct lacp_softc *lsc = (struct lacp_softc *)xlsc;
2735 struct lagg_softc *sc = lsc->lsc_softc;
2736 struct lacp_portmap *pm;
2737 struct lagg_port *lp;
2738 struct lacp_port *lacpp;
2739 uint64_t linkspeed;
2740 size_t act, i;
2741
2742 linkspeed = 0;
2743
2744 LACP_LOCK(lsc);
2745 act = LACP_PORTMAP_ACTIVE(lsc);
2746 pm = &lsc->lsc_portmaps[act];
2747 for (i = 0; i < pm->pm_count; i++) {
2748 lp = pm->pm_ports[i];
2749 lacpp = lp->lp_proto_ctx;
2750 linkspeed = MAX(linkspeed, lacpp->lp_linkspeed);
2751 }
2752 LACP_UNLOCK(lsc);
2753
2754 LAGG_LOCK(sc);
2755 lagg_set_linkspeed(sc, linkspeed);
2756 LAGG_UNLOCK(sc);
2757 }
2758