if_lagg_lacp.c revision 1.41 1 /* $NetBSD: if_lagg_lacp.c,v 1.41 2024/04/05 06:19:28 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.41 2024/04/05 06:19:28 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 lagg_workq_wait(lsc->lsc_workq, &lsc->lsc_work_tick);
548 evcnt_detach(&lsc->lsc_mgethdr_failed);
549 evcnt_detach(&lsc->lsc_mpullup_failed);
550 evcnt_detach(&lsc->lsc_badlacpdu);
551 evcnt_detach(&lsc->lsc_badmarkerdu);
552 evcnt_detach(&lsc->lsc_norcvif);
553 evcnt_detach(&lsc->lsc_nolaggport);
554 evcnt_detach(&lsc->lsc_duq_nospc);
555 lagg_workq_destroy(lsc->lsc_workq);
556 pserialize_destroy(lsc->lsc_psz);
557 mutex_destroy(&lsc->lsc_lock);
558 pcq_destroy(lsc->lsc_du_q);
559 kmem_free(lsc, sizeof(*lsc));
560 }
561
562 int
563 lacp_up(struct lagg_proto_softc *xlsc)
564 {
565 struct lagg_softc *sc;
566 struct lagg_port *lp;
567 struct lacp_softc *lsc;
568
569 lsc = (struct lacp_softc *)xlsc;
570 sc = lsc->lsc_softc;
571
572 KASSERT(LAGG_LOCKED(sc));
573
574 LACP_LOCK(lsc);
575 if (memcmp(lsc->lsc_system_mac, LAGG_CLLADDR(sc),
576 sizeof(lsc->lsc_system_mac)) != 0) {
577 memcpy(lsc->lsc_system_mac, LAGG_CLLADDR(sc),
578 sizeof(lsc->lsc_system_mac));
579 }
580 lsc->lsc_running = true;
581 callout_schedule(&lsc->lsc_tick, hz);
582 LACP_UNLOCK(lsc);
583
584 LAGG_PORTS_FOREACH(sc, lp) {
585 lacp_linkstate(xlsc, lp);
586 }
587
588 LACP_DPRINTF((lsc, NULL, "lacp start\n"));
589
590 return 0;
591 }
592
593 static void
594 lacp_down_locked(struct lacp_softc *lsc)
595 {
596 struct lagg_softc *sc;
597 struct lagg_port *lp;
598
599 sc = lsc->lsc_softc;
600
601 KASSERT(LAGG_LOCKED(sc));
602 KASSERT(LACP_LOCKED(lsc));
603
604 lsc->lsc_running = false;
605 callout_halt(&lsc->lsc_tick, &lsc->lsc_lock);
606
607 LAGG_PORTS_FOREACH(sc, lp) {
608 lacp_port_disable(lsc, lp->lp_proto_ctx);
609 }
610
611 memset(lsc->lsc_system_mac, 0,
612 sizeof(lsc->lsc_system_mac));
613
614 LACP_DPRINTF((lsc, NULL, "lacp stopped\n"));
615 }
616
617 void
618 lacp_down(struct lagg_proto_softc *xlsc)
619 {
620 struct lacp_softc *lsc;
621
622 lsc = (struct lacp_softc *)xlsc;
623
624 KASSERT(LAGG_LOCKED(lsc->lsc_softc));
625
626 LACP_LOCK(lsc);
627 lacp_down_locked(lsc);
628 LACP_UNLOCK(lsc);
629 }
630
631 int
632 lacp_transmit(struct lagg_proto_softc *xlsc, struct mbuf *m)
633 {
634 struct lacp_softc *lsc;
635 struct lagg_port *lp;
636 struct ifnet *ifp;
637 struct psref psref;
638
639 lsc = (struct lacp_softc *)xlsc;
640
641 if (__predict_false(lsc->lsc_suppress_distributing)) {
642 LACP_DPRINTF((lsc, NULL, "waiting transit\n"));
643 m_freem(m);
644 return EBUSY;
645 }
646
647 lp = lacp_select_tx_port(lsc, m, &psref);
648 if (__predict_false(lp == NULL)) {
649 LACP_DPRINTF((lsc, NULL, "no distributing port\n"));
650 ifp = &lsc->lsc_softc->sc_if;
651 if_statinc(ifp, if_oerrors);
652 m_freem(m);
653 return ENOENT;
654 }
655
656 lagg_output(lsc->lsc_softc, lp, m);
657 lagg_port_putref(lp, &psref);
658
659 return 0;
660 }
661
662 int
663 lacp_allocport(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
664 {
665 struct lagg_softc *sc;
666 struct lacp_softc *lsc;
667 struct lacp_port *lacpp;
668 struct ifreq ifr;
669 bool added_multi;
670 int error;
671
672 lsc = (struct lacp_softc *)xlsc;
673 sc = lsc->lsc_softc;
674
675 KASSERT(LAGG_LOCKED(sc));
676 KASSERT(IFNET_LOCKED(lp->lp_ifp));
677
678 lacpp = kmem_zalloc(sizeof(*lacpp), KM_NOSLEEP);
679 if (lacpp == NULL)
680 return ENOMEM;
681
682 lacp_mcastaddr(&ifr, lp->lp_ifp->if_xname);
683 error = lp->lp_ioctl(lp->lp_ifp, SIOCADDMULTI, (void *)&ifr);
684
685 switch (error) {
686 case 0:
687 added_multi = true;
688 break;
689 case EAFNOSUPPORT:
690 added_multi = false;
691 break;
692 default:
693 LAGG_LOG(sc, LOG_ERR, "SIOCADDMULTI failed on %s\n",
694 lp->lp_ifp->if_xname);
695 kmem_free(lacpp, sizeof(*lacpp));
696 return error;
697 }
698
699 lacpp->lp_added_multi = added_multi;
700 lagg_work_set(&lacpp->lp_work_smtx, lacp_sm_tx_work, lsc);
701 lagg_work_set(&lacpp->lp_work_marker, lacp_marker_work, lsc);
702
703 LACP_LOCK(lsc);
704 lacp_sm_port_init(lsc, lacpp, lp);
705 LACP_UNLOCK(lsc);
706
707 lp->lp_proto_ctx = (void *)lacpp;
708 lp->lp_prio = ntohs(lacpp->lp_actor.lpi_portprio);
709
710 return 0;
711 }
712
713 void
714 lacp_startport(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
715 {
716 struct lacp_port *lacpp;
717 uint16_t prio;
718
719 lacpp = lp->lp_proto_ctx;
720
721 prio = (uint16_t)MIN(lp->lp_prio, UINT16_MAX);
722 lacpp->lp_actor.lpi_portprio = htons(prio);
723
724 lacp_linkstate(xlsc, lp);
725 }
726
727 void
728 lacp_stopport(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
729 {
730 struct lacp_softc *lsc;
731 struct lacp_port *lacpp;
732 int i;
733
734 lsc = (struct lacp_softc *)xlsc;
735 lacpp = lp->lp_proto_ctx;
736
737 KASSERT(LAGG_LOCKED(lsc->lsc_softc));
738
739 LACP_LOCK(lsc);
740 for (i = 0; i < LACP_NTIMER; i++) {
741 LACP_TIMER_DISARM(lacpp, i);
742 }
743
744 lacp_port_disable(lsc, lacpp);
745 LACP_UNLOCK(lsc);
746 }
747
748 void
749 lacp_freeport(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
750 {
751 struct lacp_softc *lsc;
752 struct lacp_port *lacpp;
753 struct ifreq ifr;
754
755 lsc = (struct lacp_softc *)xlsc;
756 lacpp = lp->lp_proto_ctx;
757
758 KASSERT(LAGG_LOCKED(lsc->lsc_softc));
759 KASSERT(IFNET_LOCKED(lp->lp_ifp));
760
761 lagg_workq_wait(lsc->lsc_workq, &lacpp->lp_work_smtx);
762 lagg_workq_wait(lsc->lsc_workq, &lacpp->lp_work_marker);
763
764 if (lacpp->lp_added_multi) {
765 lacp_mcastaddr(&ifr, LACP_PORT_XNAME(lacpp));
766
767 (void)lp->lp_ioctl(lp->lp_ifp, SIOCDELMULTI, (void *)&ifr);
768 }
769
770 lp->lp_proto_ctx = NULL;
771 kmem_free(lacpp, sizeof(*lacpp));
772 }
773
774 void
775 lacp_protostat(struct lagg_proto_softc *xlsc, struct laggreqproto *resp)
776 {
777 struct laggreq_lacp *rplacp;
778 struct lacp_softc *lsc;
779 struct lacp_aggregator *la;
780 struct lacp_aggregator_systemid *sid;
781
782 lsc = (struct lacp_softc *)xlsc;
783
784 LACP_LOCK(lsc);
785 la = lsc->lsc_aggregator;
786 rplacp = &resp->rp_lacp;
787
788 if (lsc->lsc_optimistic)
789 SET(rplacp->flags, LAGGREQLACP_OPTIMISTIC);
790 if (lsc->lsc_dump_du)
791 SET(rplacp->flags, LAGGREQLACP_DUMPDU);
792 if (lsc->lsc_stop_lacpdu)
793 SET(rplacp->flags, LAGGREQLACP_STOPDU);
794 if (lsc->lsc_multi_linkspeed)
795 SET(rplacp->flags, LAGGREQLACP_MULTILS);
796
797 rplacp->maxports = lsc->lsc_max_ports;
798 rplacp->actor_prio = ntohs(lsc->lsc_system_prio);
799 memcpy(rplacp->actor_mac, lsc->lsc_system_mac,
800 sizeof(rplacp->actor_mac));
801 rplacp->actor_key = ntohs(lsc->lsc_key);
802
803 if (la != NULL) {
804 sid = &la->la_sid;
805 rplacp->partner_prio = ntohs(sid->sid_prio);
806 memcpy(rplacp->partner_mac, sid->sid_mac,
807 sizeof(rplacp->partner_mac));
808 rplacp->partner_key = ntohs(sid->sid_key);
809 }
810 LACP_UNLOCK(lsc);
811 }
812
813 void
814 lacp_portstat(struct lagg_proto_softc *xlsc, struct lagg_port *lp,
815 struct laggreqport *resp)
816 {
817 struct laggreq_lacpport *llp;
818 struct lacp_softc *lsc;
819 struct lacp_port *lacpp;
820 struct lacp_aggregator *la;
821 struct lacp_aggregator_systemid *sid;
822
823 lsc = (struct lacp_softc *)xlsc;
824 lacpp = lp->lp_proto_ctx;
825 la = lacpp->lp_aggregator;
826 llp = &resp->rp_lacpport;
827
828 if (lacp_isactive(lsc, lacpp))
829 SET(resp->rp_flags, LAGG_PORT_ACTIVE);
830 if (lacp_iscollecting(lacpp))
831 SET(resp->rp_flags, LAGG_PORT_COLLECTING);
832 if (lacp_isdistributing(lacpp))
833 SET(resp->rp_flags, LAGG_PORT_DISTRIBUTING);
834 if (lacpp->lp_selected == LACP_STANDBY)
835 SET(resp->rp_flags, LAGG_PORT_STANDBY);
836
837 if (la != NULL) {
838 sid = &la->la_sid;
839 llp->partner_prio = ntohs(sid->sid_prio);
840 memcpy(llp->partner_mac, sid->sid_mac,
841 sizeof(llp->partner_mac));
842 llp->partner_key = ntohs(sid->sid_key);
843 }
844
845 llp->actor_portprio = ntohs(lacpp->lp_actor.lpi_portprio);
846 llp->actor_portno = ntohs(lacpp->lp_actor.lpi_portno);
847 llp->actor_state = lacpp->lp_actor.lpi_state;
848
849 llp->partner_portprio = ntohs(lacpp->lp_partner.lpi_portprio);
850 llp->partner_portno = ntohs(lacpp->lp_partner.lpi_portno);
851 llp->partner_state = lacpp->lp_partner.lpi_state;
852 }
853
854 void
855 lacp_linkstate_ifnet_locked(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
856 {
857 struct lacp_softc *lsc;
858 struct lacp_port *lacpp;
859 struct ifmediareq ifmr;
860 struct ifnet *ifp_port;
861 uint8_t old_state;
862 uint64_t old_linkspeed, new_linkspeed;
863 int error;
864
865 KASSERT(IFNET_LOCKED(lp->lp_ifp));
866
867 lsc = (struct lacp_softc *)xlsc;
868
869 ifp_port = lp->lp_ifp;
870 lacpp = lp->lp_proto_ctx;
871
872 memset(&ifmr, 0, sizeof(ifmr));
873 ifmr.ifm_count = 0;
874 error = if_ioctl(ifp_port, SIOCGIFMEDIA, (void *)&ifmr);
875 if (error == 0) {
876 new_linkspeed = ifmedia_baudrate(ifmr.ifm_active);
877 #ifdef LACP_NOFDX
878 /*
879 * some driver that has no media, e.g. vioif(4),
880 * returns (IFM_ETHER | IFM_AUTO)
881 */
882 if ((ifmr.ifm_active & ~(IFM_ETHER | IFM_AUTO)) == 0)
883 ifmr.ifm_active |= IFM_FDX;
884 #endif
885 } else if (error == ENOTTY) {
886 ifmr.ifm_active = IFM_FDX | IF_Gbps(0);
887 new_linkspeed = 0;
888 } else {
889 LACP_DPRINTF((lsc, lacpp,
890 "SIOCGIFMEDIA failed (%d)\n", error));
891 return;
892 }
893
894 LACP_LOCK(lsc);
895 if (lsc->lsc_running) {
896 old_linkspeed = lacpp->lp_linkspeed;
897 old_state = lacpp->lp_actor.lpi_state;
898
899 if (new_linkspeed != old_linkspeed) {
900 LACP_DPRINTF((lsc, lacpp,
901 "linkspeed changed %"PRIu64" -> %"PRIu64"\n",
902 old_linkspeed, new_linkspeed));
903 lacpp->lp_linkspeed = new_linkspeed;
904 }
905
906 if (ISSET(ifmr.ifm_active, IFM_FDX) &&
907 ISSET(ifp_port->if_flags, IFF_RUNNING) &&
908 ifp_port->if_link_state != LINK_STATE_DOWN) {
909 lacp_port_enable(lsc, lacpp);
910 } else {
911 LACP_DPRINTF((lsc, lacpp,
912 "FDX=%d, RUNNING=%d, link=%d\n",
913 ISSET(ifmr.ifm_active, IFM_FDX) != 0,
914 ISSET(ifp_port->if_flags, IFF_RUNNING) != 0,
915 ifp_port->if_link_state != LINK_STATE_DOWN));
916 lacp_port_disable(lsc, lacpp);
917 }
918
919 if (old_state != lacpp->lp_actor.lpi_state ||
920 old_linkspeed != new_linkspeed) {
921 LACP_DPRINTF((lsc, lacpp,
922 "state changed to UNSELECTED\n"));
923 lacpp->lp_selected = LACP_UNSELECTED;
924 }
925 } else {
926 LACP_DPRINTF((lsc, lacpp,
927 "LACP is inactive, skip linkstate\n"));
928 }
929 LACP_UNLOCK(lsc);
930 }
931
932 int
933 lacp_ioctl(struct lagg_proto_softc *xlsc, struct laggreqproto *lreq)
934 {
935 struct lacp_softc *lsc;
936 struct laggreq_lacp *rplacp;
937 struct lacp_aggregator *la;
938 int error;
939 size_t maxports;
940 bool set;
941
942 lsc = (struct lacp_softc *)xlsc;
943 rplacp = &lreq->rp_lacp;
944 error = 0;
945
946 switch (rplacp->command) {
947 case LAGGIOC_LACPSETFLAGS:
948 case LAGGIOC_LACPCLRFLAGS:
949 set = (rplacp->command == LAGGIOC_LACPSETFLAGS) ?
950 true : false;
951
952 LACP_LOCK(lsc);
953
954 if (ISSET(rplacp->flags, LAGGREQLACP_OPTIMISTIC))
955 lsc->lsc_optimistic = set;
956 if (ISSET(rplacp->flags, LAGGREQLACP_DUMPDU))
957 lsc->lsc_dump_du = set;
958 if (ISSET(rplacp->flags, LAGGREQLACP_STOPDU))
959 lsc->lsc_stop_lacpdu = set;
960
961 if (ISSET(rplacp->flags, LAGGREQLACP_MULTILS) &&
962 lsc->lsc_multi_linkspeed != set) {
963 lsc->lsc_multi_linkspeed = set;
964 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
965 lacp_selected_update(lsc, la);
966 }
967 }
968
969 LACP_UNLOCK(lsc);
970 break;
971 case LAGGIOC_LACPSETMAXPORTS:
972 case LAGGIOC_LACPCLRMAXPORTS:
973 maxports = (rplacp->command == LAGGIOC_LACPSETMAXPORTS) ?
974 rplacp->maxports : LACP_MAX_PORTS;
975 if (0 == maxports || LACP_MAX_PORTS < maxports) {
976 error = ERANGE;
977 break;
978 }
979
980 LACP_LOCK(lsc);
981 if (lsc->lsc_max_ports != maxports) {
982 lsc->lsc_max_ports = maxports;
983 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
984 lacp_selected_update(lsc, la);
985 }
986 }
987 LACP_UNLOCK(lsc);
988 break;
989 default:
990 error = ENOTTY;
991 }
992
993 return error;
994 }
995
996 static int
997 lacp_pdu_input(struct lacp_softc *lsc, struct lacp_port *lacpp, struct mbuf *m)
998 {
999 enum {
1000 LACP_TLV_ACTOR = 0,
1001 LACP_TLV_PARTNER,
1002 LACP_TLV_COLLECTOR,
1003 LACP_TLV_TERM,
1004 LACP_TLV_NUM
1005 };
1006
1007 struct lacpdu *du;
1008 struct lacpdu_peerinfo *pi_actor, *pi_partner;
1009 struct lacpdu_collectorinfo *lci;
1010 struct tlv tlvlist_lacp[LACP_TLV_NUM] = {
1011 [LACP_TLV_ACTOR] = {
1012 .tlv_t = LACP_TYPE_ACTORINFO,
1013 .tlv_l = sizeof(*pi_actor)},
1014 [LACP_TLV_PARTNER] = {
1015 .tlv_t = LACP_TYPE_PARTNERINFO,
1016 .tlv_l = sizeof(*pi_partner)},
1017 [LACP_TLV_COLLECTOR] = {
1018 .tlv_t = LACP_TYPE_COLLECTORINFO,
1019 .tlv_l = sizeof(*lci)},
1020 [LACP_TLV_TERM] = {
1021 .tlv_t = TLV_TYPE_TERMINATE,
1022 .tlv_l = 0},
1023 };
1024
1025 if (m->m_pkthdr.len != sizeof(*du))
1026 goto bad;
1027
1028 if (m->m_len < (int)sizeof(*du)) {
1029 m = m_pullup(m, sizeof(*du));
1030 if (m == NULL) {
1031 lsc->lsc_mpullup_failed.ev_count++;
1032 return ENOMEM;
1033 }
1034 }
1035
1036 du = mtod(m, struct lacpdu *);
1037
1038 if (memcmp(&du->ldu_eh.ether_dhost,
1039 ethermulticastaddr_slowprotocols, ETHER_ADDR_LEN) != 0)
1040 goto bad;
1041
1042 LACP_TLV_PARSE(du, struct lacpdu, ldu_tlv_actor,
1043 tlvlist_lacp);
1044
1045 pi_actor = tlvlist_lacp[LACP_TLV_ACTOR].tlv_v;
1046 pi_partner = tlvlist_lacp[LACP_TLV_PARTNER].tlv_v;
1047 lci = tlvlist_lacp[LACP_TLV_COLLECTOR].tlv_v;
1048
1049 if (pi_actor == NULL || pi_partner == NULL)
1050 goto bad;
1051
1052 if (LACP_ISDUMPING(lsc)) {
1053 lacp_dprintf(lsc, lacpp, "lacpdu received\n");
1054 lacp_dump_lacpdutlv(pi_actor, pi_partner, lci);
1055 }
1056
1057 LACP_LOCK(lsc);
1058 lacp_sm_rx(lsc, lacpp, pi_partner, pi_actor);
1059 LACP_UNLOCK(lsc);
1060
1061 m_freem(m);
1062 return 0;
1063 bad:
1064 lsc->lsc_badlacpdu.ev_count++;
1065 m_freem(m);
1066 return EINVAL;
1067 }
1068
1069 static int
1070 marker_cmp(struct markerdu_info *mi,
1071 struct lacp_softc *lsc, struct lacp_port *lacpp)
1072 {
1073
1074 KASSERT(LACP_LOCKED(lsc));
1075
1076 if (mi->mi_rq_port != lacpp->lp_actor.lpi_portno)
1077 return -1;
1078
1079 if (ntohl(mi->mi_rq_xid) != lacpp->lp_marker_xid)
1080 return -1;
1081
1082 return memcmp(mi->mi_rq_system, lsc->lsc_system_mac,
1083 LACP_MAC_LEN);
1084 }
1085
1086 static void
1087 lacp_marker_reply(struct lacp_softc *lsc, struct lacp_port *lacpp,
1088 struct mbuf *m_info)
1089 {
1090 struct lagg_port *lp;
1091 struct markerdu *mdu;
1092 struct ifnet *ifp_port;
1093 struct psref psref;
1094
1095 LACP_LOCK(lsc);
1096 lp = lacpp->lp_laggport;
1097 lagg_port_getref(lp, &psref);
1098 LACP_UNLOCK(lsc);
1099
1100 ifp_port = lp->lp_ifp;
1101 mdu = mtod(m_info, struct markerdu *);
1102
1103 mdu->mdu_tlv_info.tlv_type = MARKER_TYPE_RESPONSE;
1104 /* ether_dhost is already ethermulticastaddr_slowprotocols */
1105 m_info->m_flags |= M_MCAST;
1106 memcpy(mdu->mdu_eh.ether_shost,
1107 CLLADDR(ifp_port->if_sadl), ETHER_ADDR_LEN);
1108
1109 if (LACP_ISDUMPING(lsc)) {
1110 lacp_dprintf(lsc, lacpp, "markerdu reply\n");
1111 lacp_dump_markertlv(NULL, &mdu->mdu_info);
1112 }
1113
1114 lagg_port_xmit(lp, m_info);
1115 lagg_port_putref(lp, &psref);
1116 }
1117
1118 static int
1119 lacp_marker_recv_response(struct lacp_softc *lsc, struct lacp_port *lacpp,
1120 struct markerdu_info *mi_res)
1121 {
1122 struct lagg_softc *sc;
1123 struct lagg_port *lp0;
1124 struct lacp_port *lacpp0;
1125 bool pending;
1126
1127 sc = lsc->lsc_softc;
1128
1129 LACP_LOCK(lsc);
1130 if (marker_cmp(mi_res, lsc, lacpp) != 0) {
1131 LACP_UNLOCK(lsc);
1132 return -1;
1133 }
1134 CLR(lacpp->lp_flags, LACP_PORT_MARK);
1135 LACP_UNLOCK(lsc);
1136
1137 LAGG_LOCK(sc);
1138 LACP_LOCK(lsc);
1139
1140 if (lsc->lsc_suppress_distributing) {
1141 pending = false;
1142 LAGG_PORTS_FOREACH(sc, lp0) {
1143 lacpp0 = lp0->lp_proto_ctx;
1144 if (ISSET(lacpp0->lp_flags, LACP_PORT_MARK)) {
1145 pending = true;
1146 break;
1147 }
1148 }
1149
1150 if (!pending) {
1151 LACP_DPRINTF((lsc, NULL, "queue flush complete\n"));
1152 LACP_PTIMER_DISARM(lsc, LACP_PTIMER_DISTRIBUTING);
1153 lsc->lsc_suppress_distributing = false;
1154 }
1155 }
1156
1157 LACP_UNLOCK(lsc);
1158 LAGG_UNLOCK(sc);
1159
1160 return 0;
1161 }
1162
1163 static int
1164 lacp_marker_input(struct lacp_softc *lsc, struct lacp_port *lacpp,
1165 struct mbuf *m)
1166 {
1167 enum {
1168 MARKER_TLV_INFO = 0,
1169 MARKER_TLV_RESPONSE,
1170 MARKER_TLV_TERM,
1171 MARKER_TLV_NUM
1172 };
1173
1174 struct markerdu *mdu;
1175 struct markerdu_info *mi_info, *mi_res;
1176 int error;
1177 struct tlv tlvlist_marker[MARKER_TLV_NUM] = {
1178 [MARKER_TLV_INFO] = {
1179 .tlv_t = MARKER_TYPE_INFO,
1180 .tlv_l = sizeof(*mi_info)},
1181 [MARKER_TLV_RESPONSE] = {
1182 .tlv_t = MARKER_TYPE_RESPONSE,
1183 .tlv_l = sizeof(*mi_res)},
1184 [MARKER_TLV_TERM] = {
1185 .tlv_t = TLV_TYPE_TERMINATE,
1186 .tlv_l = 0},
1187 };
1188
1189 if (m->m_pkthdr.len != sizeof(*mdu))
1190 goto bad;
1191
1192 if (m->m_len < (int)sizeof(*mdu)) {
1193 m = m_pullup(m, sizeof(*mdu));
1194 if (m == NULL) {
1195 lsc->lsc_mpullup_failed.ev_count++;
1196 return ENOMEM;
1197 }
1198 }
1199
1200 mdu = mtod(m, struct markerdu *);
1201
1202 if (memcmp(mdu->mdu_eh.ether_dhost,
1203 ethermulticastaddr_slowprotocols, ETHER_ADDR_LEN) != 0)
1204 goto bad;
1205
1206 LACP_TLV_PARSE(mdu, struct markerdu, mdu_tlv_info,
1207 tlvlist_marker);
1208
1209 mi_info = tlvlist_marker[MARKER_TLV_INFO].tlv_v;
1210 mi_res = tlvlist_marker[MARKER_TLV_RESPONSE].tlv_v;
1211
1212 if (LACP_ISDUMPING(lsc)) {
1213 lacp_dprintf(lsc, lacpp, "markerdu received\n");
1214 lacp_dump_markertlv(mi_info, mi_res);
1215 }
1216
1217 if (mi_info != NULL && mi_res == NULL) {
1218 lacp_marker_reply(lsc, lacpp, m);
1219 } else if (mi_info == NULL && mi_res != NULL) {
1220 error = lacp_marker_recv_response(lsc, lacpp,
1221 mi_res);
1222 if (error != 0) {
1223 goto bad;
1224 } else {
1225 m_freem(m);
1226 }
1227 } else {
1228 goto bad;
1229 }
1230
1231 return 0;
1232 bad:
1233 lsc->lsc_badmarkerdu.ev_count++;
1234 m_freem(m);
1235 return EINVAL;
1236 }
1237
1238 struct mbuf *
1239 lacp_input(struct lagg_proto_softc *xlsc, struct lagg_port *lp, struct mbuf *m)
1240 {
1241 struct ifnet *ifp;
1242 struct lacp_softc *lsc;
1243 struct lacp_port *lacpp;
1244 struct ether_header *eh;
1245 uint8_t subtype;
1246
1247 eh = mtod(m, struct ether_header *);
1248 lsc = (struct lacp_softc *)xlsc;
1249 ifp = &lsc->lsc_softc->sc_if;
1250 lacpp = lp->lp_proto_ctx;
1251
1252 if (!vlan_has_tag(m) &&
1253 eh->ether_type == htons(ETHERTYPE_SLOWPROTOCOLS)) {
1254 if (m->m_pkthdr.len < (int)(sizeof(*eh) + sizeof(subtype))) {
1255 m_freem(m);
1256 return NULL;
1257 }
1258
1259 m_copydata(m, sizeof(struct ether_header),
1260 sizeof(subtype), &subtype);
1261
1262 switch (subtype) {
1263 case SLOWPROTOCOLS_SUBTYPE_LACP:
1264 case SLOWPROTOCOLS_SUBTYPE_MARKER:
1265 if (pcq_put(lsc->lsc_du_q, (void *)m)) {
1266 lagg_workq_add(lsc->lsc_workq,
1267 &lsc->lsc_work_rcvdu);
1268 } else {
1269 m_freem(m);
1270 lsc->lsc_duq_nospc.ev_count++;
1271 }
1272 return NULL;
1273 }
1274 }
1275
1276 if (!lacp_iscollecting(lacpp) || !lacp_isactive(lsc, lacpp)) {
1277 if_statinc(ifp, if_ierrors);
1278 m_freem(m);
1279 return NULL;
1280 }
1281
1282 return m;
1283 }
1284
1285 static void
1286 lacp_rcvdu_work(struct lagg_work *lw __unused, void *xlsc)
1287 {
1288 struct lacp_softc *lsc = (struct lacp_softc *)xlsc;
1289 struct ifnet *ifp;
1290 struct psref psref_lp;
1291 struct lagg_port *lp;
1292 struct mbuf *m;
1293 uint8_t subtype;
1294 int bound, s0, s1;
1295
1296 bound = curlwp_bind();
1297
1298 for (;;) {
1299 m = pcq_get(lsc->lsc_du_q);
1300 if (m == NULL)
1301 break;
1302
1303 ifp = m_get_rcvif(m, &s0);
1304 if (ifp == NULL) {
1305 m_freem(m);
1306 lsc->lsc_norcvif.ev_count++;
1307 continue;
1308 }
1309
1310 s1 = pserialize_read_enter();
1311 lp = atomic_load_consume(&ifp->if_lagg);
1312 if (lp == NULL) {
1313 pserialize_read_exit(s1);
1314 m_put_rcvif(ifp, &s0);
1315 m_freem(m);
1316 lsc->lsc_norcvif.ev_count++;
1317 continue;
1318 }
1319
1320 lagg_port_getref(lp, &psref_lp);
1321 pserialize_read_exit(s1);
1322 m_put_rcvif(ifp, &s0);
1323
1324 m_copydata(m, sizeof(struct ether_header),
1325 sizeof(subtype), &subtype);
1326
1327 switch (subtype) {
1328 case SLOWPROTOCOLS_SUBTYPE_LACP:
1329 (void)lacp_pdu_input(lsc,
1330 lp->lp_proto_ctx, m);
1331 break;
1332 case SLOWPROTOCOLS_SUBTYPE_MARKER:
1333 (void)lacp_marker_input(lsc,
1334 lp->lp_proto_ctx, m);
1335 break;
1336 }
1337
1338 lagg_port_putref(lp, &psref_lp);
1339 }
1340
1341 curlwp_bindx(bound);
1342 }
1343
1344 static bool
1345 lacp_port_need_to_tell(struct lacp_port *lacpp)
1346 {
1347
1348 if (!ISSET(lacpp->lp_actor.lpi_state,
1349 LACP_STATE_AGGREGATION)) {
1350 return false;
1351 }
1352
1353 if (!ISSET(lacpp->lp_actor.lpi_state,
1354 LACP_STATE_ACTIVITY)
1355 && !ISSET(lacpp->lp_partner.lpi_state,
1356 LACP_STATE_ACTIVITY)) {
1357 return false;
1358 }
1359
1360 if (!ISSET(lacpp->lp_flags, LACP_PORT_NTT))
1361 return false;
1362
1363 if (ppsratecheck(&lacpp->lp_last_lacpdu, &lacpp->lp_lacpdu_sent,
1364 (LACP_SENDDU_PPS / LACP_FAST_PERIODIC_TIME)) == 0)
1365 return false;
1366
1367 return true;
1368 }
1369
1370 static void
1371 lacp_sm_assert_ntt(struct lacp_port *lacpp)
1372 {
1373
1374 SET(lacpp->lp_flags, LACP_PORT_NTT);
1375 }
1376
1377 static void
1378 lacp_sm_negate_ntt(struct lacp_port *lacpp)
1379 {
1380
1381 CLR(lacpp->lp_flags, LACP_PORT_NTT);
1382 }
1383
1384 static struct mbuf *
1385 lacp_lacpdu_mbuf(struct lacp_softc *lsc, struct lacp_port *lacpp)
1386 {
1387 struct ifnet *ifp_port;
1388 struct mbuf *m;
1389 struct lacpdu *du;
1390
1391 KASSERT(LACP_LOCKED(lsc));
1392
1393 ifp_port = lacpp->lp_laggport->lp_ifp;
1394
1395 MGETHDR(m, M_DONTWAIT, MT_DATA);
1396 if (m == NULL) {
1397 lsc->lsc_mgethdr_failed.ev_count++;
1398 return NULL;
1399 }
1400
1401 m->m_pkthdr.len = m->m_len = sizeof(*du);
1402 m_reset_rcvif(m);
1403
1404 du = mtod(m, struct lacpdu *);
1405 memset(du, 0, sizeof(*du));
1406
1407 m->m_flags |= M_MCAST;
1408 memcpy(du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
1409 ETHER_ADDR_LEN);
1410 memcpy(du->ldu_eh.ether_shost, CLLADDR(ifp_port->if_sadl),
1411 ETHER_ADDR_LEN);
1412 du->ldu_eh.ether_type = htons(ETHERTYPE_SLOWPROTOCOLS);
1413 du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP;
1414 du->ldu_sph.sph_version = 1;
1415
1416 tlv_set(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO,
1417 sizeof(du->ldu_actor));
1418 lacp_peerinfo_actor(lsc, lacpp, &du->ldu_actor);
1419
1420 tlv_set(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO,
1421 sizeof(du->ldu_partner));
1422 lacp_peerinfo_partner(lacpp, &du->ldu_partner);
1423
1424 tlv_set(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO,
1425 sizeof(du->ldu_collector));
1426 du->ldu_collector.lci_maxdelay = 0;
1427
1428 du->ldu_tlv_term.tlv_type = LACP_TYPE_TERMINATE;
1429 du->ldu_tlv_term.tlv_length = 0;
1430
1431 return m;
1432 }
1433
1434 static void
1435 lacp_sm_tx_work(struct lagg_work *lw, void *xlsc)
1436 {
1437 struct lacp_softc *lsc;
1438 struct lacp_port *lacpp;
1439 struct lagg_port *lp;
1440 struct lacpdu *du;
1441 struct mbuf *m;
1442 struct psref psref;
1443 int bound;
1444
1445 lsc = xlsc;
1446 lacpp = container_of(lw, struct lacp_port, lp_work_smtx);
1447
1448 LACP_LOCK(lsc);
1449
1450 if (lsc->lsc_stop_lacpdu) {
1451 LACP_UNLOCK(lsc);
1452 return;
1453 }
1454
1455 m = lacp_lacpdu_mbuf(lsc, lacpp);
1456 if (m == NULL) {
1457 LACP_UNLOCK(lsc);
1458 return;
1459 }
1460 lacp_sm_negate_ntt(lacpp);
1461 lp = lacpp->lp_laggport;
1462 bound = curlwp_bind();
1463 lagg_port_getref(lp, &psref);
1464 LACP_UNLOCK(lsc);
1465
1466 if (LACP_ISDUMPING(lsc)) {
1467 lacp_dprintf(lsc, lacpp, "lacpdu transmit\n");
1468 du = mtod(m, struct lacpdu *);
1469 lacp_dump_lacpdutlv(&du->ldu_actor,
1470 &du->ldu_partner, &du->ldu_collector);
1471 }
1472
1473 lagg_port_xmit(lp, m);
1474 lagg_port_putref(lp, &psref);
1475 curlwp_bindx(bound);
1476 }
1477
1478 static void
1479 lacp_sm_tx(struct lacp_softc *lsc, struct lacp_port *lacpp)
1480 {
1481
1482 if (!lacp_port_need_to_tell(lacpp))
1483 return;
1484
1485 lagg_workq_add(lsc->lsc_workq, &lacpp->lp_work_smtx);
1486 }
1487
1488 static void
1489 lacp_tick(void *xlsc)
1490 {
1491 struct lacp_softc *lsc;
1492
1493 lsc = xlsc;
1494
1495 LACP_LOCK(lsc);
1496
1497 if (!lsc->lsc_running) {
1498 LACP_UNLOCK(lsc);
1499 return;
1500 }
1501
1502 lagg_workq_add(lsc->lsc_workq, &lsc->lsc_work_tick);
1503 callout_schedule(&lsc->lsc_tick, hz);
1504
1505 LACP_UNLOCK(lsc);
1506 }
1507
1508 static void
1509 lacp_run_timers(struct lacp_softc *lsc, struct lacp_port *lacpp)
1510 {
1511 size_t i;
1512
1513 KASSERT(LACP_LOCKED(lsc));
1514
1515 for (i = 0; i < LACP_NTIMER; i++) {
1516 KASSERT(lacpp->lp_timer[i] >= 0);
1517
1518 if (lacpp->lp_timer[i] == 0)
1519 continue;
1520 if (--lacpp->lp_timer[i] > 0)
1521 continue;
1522
1523 KASSERT(lacp_timer_funcs[i] != NULL);
1524 lacp_timer_funcs[i](lsc, lacpp);
1525 }
1526 }
1527
1528 static void
1529 lacp_run_prototimers(struct lacp_softc *lsc)
1530 {
1531 size_t i;
1532
1533 for (i = 0; i < LACP_NPTIMER; i++) {
1534 KASSERT(lsc->lsc_timer[i] >= 0);
1535
1536 if (lsc->lsc_timer[i] == 0)
1537 continue;
1538 if (--lsc->lsc_timer[i] > 0)
1539 continue;
1540
1541 KASSERT(lacp_ptimer_funcs[i] != NULL);
1542 lacp_ptimer_funcs[i](lsc);
1543 }
1544 }
1545
1546 static void
1547 lacp_tick_work(struct lagg_work *lw __unused, void *xlsc)
1548 {
1549 struct lacp_softc *lsc;
1550 struct lacp_port *lacpp;
1551 struct lagg_softc *sc;
1552 struct lagg_port *lp;
1553
1554 lsc = xlsc;
1555 sc = lsc->lsc_softc;
1556
1557 LACP_LOCK(lsc);
1558 if (!lsc->lsc_running) {
1559 LACP_UNLOCK(lsc);
1560 return;
1561 }
1562
1563 lacp_run_prototimers(lsc);
1564 LACP_UNLOCK(lsc);
1565
1566 LAGG_LOCK(sc);
1567 LACP_LOCK(lsc);
1568 LAGG_PORTS_FOREACH(sc, lp) {
1569 lacpp = lp->lp_proto_ctx;
1570 if (!ISSET(lacpp->lp_actor.lpi_state,
1571 LACP_STATE_AGGREGATION)) {
1572 continue;
1573 }
1574
1575 lacp_run_timers(lsc, lacpp);
1576 lacp_select(lsc, lacpp);
1577 lacp_sm_mux(lsc, lacpp);
1578 lacp_sm_tx(lsc, lacpp);
1579 lacp_sm_ptx_schedule(lacpp);
1580 }
1581
1582 LACP_UNLOCK(lsc);
1583 LAGG_UNLOCK(sc);
1584 }
1585
1586 static void
1587 lacp_systemid_str(char *buf, size_t buflen,
1588 uint16_t prio, const uint8_t *mac, uint16_t key)
1589 {
1590
1591 snprintf(buf, buflen,
1592 "%04X,"
1593 "%02X-%02X-%02X-%02X-%02X-%02X,"
1594 "%04X",
1595 (unsigned int)ntohs(prio),
1596 (int)mac[0], (int)mac[1], (int)mac[2],
1597 (int)mac[3], (int)mac[4], (int)mac[5],
1598 (unsigned int)htons(key));
1599
1600 }
1601
1602 __LACPDEBUGUSED static void
1603 lacp_aggregator_str(struct lacp_aggregator *la, char *buf, size_t buflen)
1604 {
1605
1606 lacp_systemid_str(buf, buflen, la->la_sid.sid_prio,
1607 la->la_sid.sid_mac, la->la_sid.sid_key);
1608 }
1609
1610 static void
1611 lacp_peerinfo_idstr(const struct lacpdu_peerinfo *pi,
1612 char *buf, size_t buflen)
1613 {
1614
1615 lacp_systemid_str(buf, buflen, pi->lpi_system_prio,
1616 pi->lpi_system_mac, pi->lpi_key);
1617 }
1618
1619 static void
1620 lacp_state_str(uint8_t state, char *buf, size_t buflen)
1621 {
1622
1623 snprintb(buf, buflen, LACP_STATE_BITS, state);
1624 }
1625
1626 static struct lagg_port *
1627 lacp_select_tx_port(struct lacp_softc *lsc, struct mbuf *m,
1628 struct psref *psref)
1629 {
1630 struct lacp_portmap *pm;
1631 struct lagg_port *lp;
1632 uint32_t hash;
1633 size_t act;
1634 int s;
1635
1636 hash = lagg_hashmbuf(lsc->lsc_softc, m);
1637
1638 s = pserialize_read_enter();
1639 act = LACP_PORTMAP_ACTIVE(lsc);
1640 pm = &lsc->lsc_portmaps[act];
1641
1642 if (pm->pm_count == 0) {
1643 pserialize_read_exit(s);
1644 return NULL;
1645 }
1646
1647 hash %= pm->pm_count;
1648 lp = pm->pm_ports[hash];
1649 lagg_port_getref(lp, psref);
1650
1651 pserialize_read_exit(s);
1652
1653 return lp;
1654 }
1655
1656 static void
1657 lacp_peerinfo_actor(struct lacp_softc *lsc, struct lacp_port *lacpp,
1658 struct lacpdu_peerinfo *dst)
1659 {
1660
1661 memcpy(dst->lpi_system_mac, lsc->lsc_system_mac, LACP_MAC_LEN);
1662 dst->lpi_system_prio = lsc->lsc_system_prio;
1663 dst->lpi_key = lsc->lsc_key;
1664 dst->lpi_port_no = lacpp->lp_actor.lpi_portno;
1665 dst->lpi_port_prio = lacpp->lp_actor.lpi_portprio;
1666 dst->lpi_state = lacpp->lp_actor.lpi_state;
1667 }
1668
1669 static void
1670 lacp_peerinfo_partner(struct lacp_port *lacpp, struct lacpdu_peerinfo *dst)
1671 {
1672 struct lacp_aggregator *la;
1673
1674 la = lacpp->lp_aggregator;
1675
1676 if (la != NULL) {
1677 memcpy(dst->lpi_system_mac, la->la_sid.sid_mac, LACP_MAC_LEN);
1678 dst->lpi_system_prio = la->la_sid.sid_prio;
1679 dst->lpi_key = la->la_sid.sid_key;
1680 } else {
1681 memset(dst->lpi_system_mac, 0, LACP_MAC_LEN);
1682 dst->lpi_system_prio = 0;
1683 dst->lpi_key = 0;
1684 }
1685 dst->lpi_port_no = lacpp->lp_partner.lpi_portno;
1686 dst->lpi_port_prio = lacpp->lp_partner.lpi_portprio;
1687 dst->lpi_state = lacpp->lp_partner.lpi_state;
1688 }
1689
1690 static int
1691 lacp_compare_peerinfo(struct lacpdu_peerinfo *a, struct lacpdu_peerinfo *b)
1692 {
1693
1694 return memcmp(a, b, offsetof(struct lacpdu_peerinfo, lpi_state));
1695 }
1696
1697 static void
1698 lacp_sm_rx_record_default(struct lacp_softc *lsc, struct lacp_port *lacpp)
1699 {
1700 uint8_t oldpstate;
1701 struct lacp_portinfo *pi;
1702 char buf[LACP_STATESTR_LEN] __LACPDEBUGUSED;
1703
1704 pi = &lacpp->lp_partner;
1705
1706 oldpstate = pi->lpi_state;
1707 pi->lpi_portno = htons(LACP_PORTNO_NONE);
1708 pi->lpi_portprio = htons(0xffff);
1709
1710 if (lsc->lsc_optimistic)
1711 pi->lpi_state = LACP_PARTNER_ADMIN_OPTIMISTIC;
1712 else
1713 pi->lpi_state = LACP_PARTNER_ADMIN_STRICT;
1714
1715 SET(lacpp->lp_actor.lpi_state, LACP_STATE_DEFAULTED);
1716
1717 if (oldpstate != pi->lpi_state) {
1718 LACP_STATE_STR(oldpstate, buf, sizeof(buf));
1719 LACP_DPRINTF((lsc, lacpp, "oldpstate %s\n", buf));
1720
1721 LACP_STATE_STR(pi->lpi_state, buf, sizeof(buf));
1722 LACP_DPRINTF((lsc, lacpp, "newpstate %s\n", buf));
1723 }
1724
1725 lacp_sm_ptx_update_timeout(lacpp, oldpstate);
1726 }
1727
1728 static inline bool
1729 lacp_port_is_synced(struct lacp_softc *lsc, struct lacp_port *lacpp,
1730 struct lacpdu_peerinfo *my_pi, struct lacpdu_peerinfo *peer_pi)
1731 {
1732 struct lacpdu_peerinfo actor;
1733
1734 if (!ISSET(peer_pi->lpi_state, LACP_STATE_ACTIVITY) &&
1735 (!ISSET(my_pi->lpi_state, LACP_STATE_ACTIVITY) ||
1736 !ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_ACTIVITY)))
1737 return false;
1738
1739 if (!ISSET(peer_pi->lpi_state, LACP_STATE_AGGREGATION))
1740 return false;
1741
1742 lacp_peerinfo_actor(lsc, lacpp, &actor);
1743 if (lacp_compare_peerinfo(&actor, my_pi) != 0)
1744 return false;
1745
1746 if (!LACP_STATE_EQ(actor.lpi_state, my_pi->lpi_state,
1747 LACP_STATE_AGGREGATION)) {
1748 return false;
1749 }
1750
1751 return true;
1752 }
1753
1754 static void
1755 lacp_sm_rx_record_peerinfo(struct lacp_softc *lsc, struct lacp_port *lacpp,
1756 struct lacpdu_peerinfo *my_pi, struct lacpdu_peerinfo *peer_pi)
1757 {
1758 char buf[LACP_STATESTR_LEN] __LACPDEBUGUSED;
1759 uint8_t oldpstate;
1760 struct lacp_portinfo *pi;
1761 struct lacp_aggregator_systemid *sid;
1762
1763 pi = &lacpp->lp_partner;
1764 sid = &lacpp->lp_aggregator_sidbuf;
1765
1766 oldpstate = lacpp->lp_partner.lpi_state;
1767
1768 sid->sid_prio = peer_pi->lpi_system_prio;
1769 sid->sid_key = peer_pi->lpi_key;
1770 memcpy(sid->sid_mac, peer_pi->lpi_system_mac,
1771 sizeof(sid->sid_mac));
1772
1773 pi->lpi_portno = peer_pi->lpi_port_no;
1774 pi->lpi_portprio = peer_pi->lpi_port_prio;
1775 pi->lpi_state = peer_pi->lpi_state;
1776
1777 if (lacp_port_is_synced(lsc, lacpp, my_pi, peer_pi)) {
1778 if (lsc->lsc_optimistic)
1779 SET(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
1780 } else {
1781 CLR(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
1782 }
1783
1784 CLR(lacpp->lp_actor.lpi_state, LACP_STATE_DEFAULTED);
1785
1786 if (oldpstate != lacpp->lp_partner.lpi_state) {
1787 LACP_STATE_STR(oldpstate, buf, sizeof(buf));
1788 LACP_DPRINTF((lsc, lacpp, "oldpstate %s\n", buf));
1789
1790 LACP_STATE_STR(lacpp->lp_partner.lpi_state,
1791 buf, sizeof(buf));
1792 LACP_DPRINTF((lsc, lacpp, "newpstate %s\n", buf));
1793 }
1794
1795 lacp_sm_ptx_update_timeout(lacpp, oldpstate);
1796 }
1797
1798 static void
1799 lacp_sm_rx_set_expired(struct lacp_port *lacpp)
1800 {
1801 uint8_t oldpstate;
1802
1803 oldpstate = lacpp->lp_partner.lpi_state;
1804
1805 CLR(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
1806 SET(lacpp->lp_partner.lpi_state, LACP_STATE_TIMEOUT);
1807 LACP_TIMER_ARM(lacpp, LACP_TIMER_CURRENT_WHILE,
1808 LACP_SHORT_TIMEOUT_TIME);
1809 SET(lacpp->lp_actor.lpi_state, LACP_STATE_EXPIRED);
1810
1811 lacp_sm_ptx_update_timeout(lacpp, oldpstate);
1812 }
1813
1814 static void
1815 lacp_sm_port_init(struct lacp_softc *lsc, struct lacp_port *lacpp,
1816 struct lagg_port *lp)
1817 {
1818
1819 KASSERT(LACP_LOCKED(lsc));
1820
1821 lacpp->lp_laggport = lp;
1822 lacpp->lp_actor.lpi_state = LACP_STATE_ACTIVITY;
1823 lacpp->lp_actor.lpi_portno = htons(if_get_index(lp->lp_ifp));
1824 lacpp->lp_actor.lpi_portprio = htons(LACP_PORT_PRIO);
1825 lacpp->lp_partner.lpi_state = LACP_STATE_TIMEOUT;
1826 lacpp->lp_aggregator = NULL;
1827 lacpp->lp_marker_xid = 0;
1828 lacpp->lp_mux_state = LACP_MUX_INIT;
1829
1830 lacp_set_mux(lsc, lacpp, LACP_MUX_DETACHED);
1831 lacp_sm_rx_record_default(lsc, lacpp);
1832 }
1833
1834 static void
1835 lacp_port_disable(struct lacp_softc *lsc, struct lacp_port *lacpp)
1836 {
1837
1838 KASSERT(LACP_LOCKED(lsc));
1839
1840 if (ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_AGGREGATION))
1841 LACP_DPRINTF((lsc, lacpp, "enable -> disable\n"));
1842
1843 lacp_set_mux(lsc, lacpp, LACP_MUX_DETACHED);
1844 lacp_sm_rx_record_default(lsc, lacpp);
1845 CLR(lacpp->lp_actor.lpi_state,
1846 LACP_STATE_AGGREGATION | LACP_STATE_EXPIRED);
1847 CLR(lacpp->lp_partner.lpi_state, LACP_STATE_AGGREGATION);
1848 }
1849
1850 static void
1851 lacp_port_enable(struct lacp_softc *lsc __LACPDEBUGUSED,
1852 struct lacp_port *lacpp)
1853 {
1854
1855 KASSERT(LACP_LOCKED(lsc));
1856
1857 if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_AGGREGATION))
1858 LACP_DPRINTF((lsc, lacpp, "disable -> enable\n"));
1859
1860 SET(lacpp->lp_actor.lpi_state, LACP_STATE_AGGREGATION);
1861 lacp_sm_rx_set_expired(lacpp);
1862 }
1863
1864 static void
1865 lacp_sm_rx_timer(struct lacp_softc *lsc, struct lacp_port *lacpp)
1866 {
1867
1868 KASSERT(LACP_LOCKED(lsc));
1869
1870 if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_EXPIRED)) {
1871 /* CURRENT -> EXPIRED */
1872 LACP_DPRINTF((lsc, lacpp, "CURRENT -> EXPIRED\n"));
1873 lacp_sm_rx_set_expired(lacpp);
1874 } else {
1875 LACP_DPRINTF((lsc, lacpp, "EXPIRED -> DEFAULTED\n"));
1876 lacp_set_mux(lsc, lacpp, LACP_MUX_DETACHED);
1877 lacp_sm_rx_record_default(lsc, lacpp);
1878 }
1879 }
1880
1881 static void
1882 lacp_sm_ptx_timer(struct lacp_softc *lsc __unused, struct lacp_port *lacpp)
1883 {
1884
1885 KASSERT(LACP_LOCKED(lsc));
1886 lacp_sm_assert_ntt(lacpp);
1887 }
1888
1889 static void
1890 lacp_sm_ptx_schedule(struct lacp_port *lacpp)
1891 {
1892 int timeout;
1893
1894 /* no periodic */
1895 if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_ACTIVITY) &&
1896 !ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_ACTIVITY)) {
1897 LACP_TIMER_DISARM(lacpp, LACP_TIMER_PERIODIC);
1898 return;
1899 }
1900
1901 if (LACP_TIMER_ISARMED(lacpp, LACP_TIMER_PERIODIC))
1902 return;
1903
1904 timeout = ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_TIMEOUT) ?
1905 LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME;
1906
1907 LACP_TIMER_ARM(lacpp, LACP_TIMER_PERIODIC, timeout);
1908 }
1909
1910 static void
1911 lacp_sm_ptx_update_timeout(struct lacp_port *lacpp, uint8_t oldpstate)
1912 {
1913
1914 if (LACP_STATE_EQ(oldpstate, lacpp->lp_partner.lpi_state,
1915 LACP_STATE_TIMEOUT))
1916 return;
1917
1918 LACP_DPRINTF((NULL, lacpp, "partner timeout changed\n"));
1919
1920 LACP_TIMER_DISARM(lacpp, LACP_TIMER_PERIODIC);
1921
1922 /* if timeout has been shorted, assert NTT */
1923 if (ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_TIMEOUT))
1924 lacp_sm_assert_ntt(lacpp);
1925 }
1926
1927 static void
1928 lacp_sm_mux_timer(struct lacp_softc *lsc __LACPDEBUGUSED,
1929 struct lacp_port *lacpp)
1930 {
1931 char buf[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
1932
1933 KASSERT(LACP_LOCKED(lsc));
1934 KASSERT(lacpp->lp_pending > 0);
1935
1936 LACP_AGGREGATOR_STR(lacpp->lp_aggregator, buf, sizeof(buf));
1937 LACP_DPRINTF((lsc, lacpp, "aggregator %s, pending %d -> %d\n",
1938 buf, lacpp->lp_pending, lacpp->lp_pending -1));
1939
1940 lacpp->lp_pending--;
1941 }
1942
1943 static void
1944 lacp_sm_rx_update_selected(struct lacp_softc *lsc, struct lacp_port *lacpp,
1945 struct lacpdu_peerinfo *peer_pi)
1946 {
1947 struct lacpdu_peerinfo partner;
1948 char str0[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
1949 char str1[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
1950
1951 if (lacpp->lp_aggregator == NULL)
1952 return;
1953
1954 lacp_peerinfo_partner(lacpp, &partner);
1955 if (lacp_compare_peerinfo(peer_pi, &partner) != 0) {
1956 LACP_PEERINFO_IDSTR(&partner, str0, sizeof(str0));
1957 LACP_PEERINFO_IDSTR(peer_pi, str1, sizeof(str1));
1958 LACP_DPRINTF((lsc, lacpp,
1959 "different peerinfo, %s vs %s\n", str0, str1));
1960 goto do_unselect;
1961 }
1962
1963 if (!LACP_STATE_EQ(lacpp->lp_partner.lpi_state,
1964 peer_pi->lpi_state, LACP_STATE_AGGREGATION)) {
1965 LACP_DPRINTF((lsc, lacpp,
1966 "STATE_AGGREGATION changed %d -> %d\n",
1967 ISSET(lacpp->lp_partner.lpi_state,
1968 LACP_STATE_AGGREGATION) != 0,
1969 ISSET(peer_pi->lpi_state, LACP_STATE_AGGREGATION) != 0));
1970 goto do_unselect;
1971 }
1972
1973 return;
1974
1975 do_unselect:
1976 lacpp->lp_selected = LACP_UNSELECTED;
1977 /* lacpp->lp_aggregator will be released at lacp_set_mux() */
1978 }
1979
1980 static void
1981 lacp_sm_rx_update_ntt(struct lacp_softc *lsc, struct lacp_port *lacpp,
1982 struct lacpdu_peerinfo *my_pi)
1983 {
1984 struct lacpdu_peerinfo actor;
1985
1986 lacp_peerinfo_actor(lsc, lacpp, &actor);
1987
1988 if (lacp_compare_peerinfo(&actor, my_pi) != 0 ||
1989 !LACP_STATE_EQ(lacpp->lp_actor.lpi_state, my_pi->lpi_state,
1990 LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) {
1991 LACP_DPRINTF((lsc, lacpp, "assert ntt\n"));
1992 lacp_sm_assert_ntt(lacpp);
1993 }
1994 }
1995
1996 static void
1997 lacp_sm_rx(struct lacp_softc *lsc, struct lacp_port *lacpp,
1998 struct lacpdu_peerinfo *my_pi, struct lacpdu_peerinfo *peer_pi)
1999 {
2000 int timeout;
2001
2002 KASSERT(LACP_LOCKED(lsc));
2003
2004 /* check LACP disabled first */
2005 if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_AGGREGATION))
2006 return;
2007
2008 /* check loopback condition */
2009 if (memcmp(lsc->lsc_system_mac, peer_pi->lpi_system_mac,
2010 LACP_MAC_LEN) == 0 &&
2011 lsc->lsc_system_prio == peer_pi->lpi_system_prio)
2012 return;
2013
2014 lacp_sm_rx_update_selected(lsc, lacpp, peer_pi);
2015 lacp_sm_rx_update_ntt(lsc, lacpp, my_pi);
2016 lacp_sm_rx_record_peerinfo(lsc, lacpp, my_pi, peer_pi);
2017
2018 timeout = ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_TIMEOUT) ?
2019 LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME;
2020 LACP_TIMER_ARM(lacpp, LACP_TIMER_CURRENT_WHILE, timeout);
2021
2022 CLR(lacpp->lp_actor.lpi_state, LACP_STATE_EXPIRED);
2023
2024 /* kick transmit machine without timeout. */
2025 lacp_sm_tx(lsc, lacpp);
2026 }
2027
2028 static void
2029 lacp_disable_collecting(struct lacp_port *lacpp)
2030 {
2031
2032 LACP_DPRINTF((NULL, lacpp, "collecting disabled\n"));
2033 CLR(lacpp->lp_actor.lpi_state, LACP_STATE_COLLECTING);
2034 atomic_store_relaxed(&lacpp->lp_collector, false);
2035 }
2036
2037 static void
2038 lacp_enable_collecting(struct lacp_port *lacpp)
2039 {
2040 LACP_DPRINTF((NULL, lacpp, "collecting enabled\n"));
2041 SET(lacpp->lp_actor.lpi_state, LACP_STATE_COLLECTING);
2042 atomic_store_relaxed(&lacpp->lp_collector, true);
2043 }
2044
2045 static void
2046 lacp_update_portmap(struct lacp_softc *lsc)
2047 {
2048 struct lagg_softc *sc;
2049 struct lacp_aggregator *la;
2050 struct lacp_portmap *pm_act, *pm_next;
2051 struct lacp_port *lacpp;
2052 size_t pmap, n;
2053 u_int link;
2054
2055 KASSERT(LACP_LOCKED(lsc));
2056
2057 la = lsc->lsc_aggregator;
2058
2059 pmap = LACP_PORTMAP_ACTIVE(lsc);
2060 pm_act = &lsc->lsc_portmaps[pmap];
2061
2062 pmap = LACP_PORTMAP_NEXT(lsc);
2063 pm_next = &lsc->lsc_portmaps[pmap];
2064
2065 n = 0;
2066 if (la != NULL) {
2067 LIST_FOREACH(lacpp, &la->la_ports, lp_entry_la) {
2068 if (!ISSET(lacpp->lp_actor.lpi_state,
2069 LACP_STATE_DISTRIBUTING)) {
2070 continue;
2071 }
2072
2073 pm_next->pm_ports[n] = lacpp->lp_laggport;
2074 n++;
2075
2076 if (n >= LACP_MAX_PORTS)
2077 break;
2078 }
2079 }
2080 pm_next->pm_count = n;
2081
2082 atomic_store_release(&lsc->lsc_activemap, pmap);
2083 pserialize_perform(lsc->lsc_psz);
2084
2085 LACP_DPRINTF((lsc, NULL, "portmap count updated (%zu -> %zu)\n",
2086 pm_act->pm_count, pm_next->pm_count));
2087
2088 link = lacp_portmap_linkstate(pm_next);
2089 if (link != lacp_portmap_linkstate(pm_act)) {
2090 sc = lsc->lsc_softc;
2091 if_link_state_change(&sc->sc_if, link);
2092 }
2093
2094 lagg_workq_add(lsc->lsc_workq, &lsc->lsc_work_linkspeed);
2095
2096 /* cleanup */
2097 pm_act->pm_count = 0;
2098 memset(pm_act->pm_ports, 0, sizeof(pm_act->pm_ports));
2099 }
2100
2101 static void
2102 lacp_disable_distributing(struct lacp_softc *lsc, struct lacp_port *lacpp)
2103 {
2104 struct lacp_portmap *pm;
2105 bool do_update;
2106 size_t act, i;
2107 int s;
2108
2109 KASSERT(LACP_LOCKED(lsc));
2110
2111 if (ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_DISTRIBUTING)) {
2112 LAGG_LOG(lsc->lsc_softc, LOG_INFO,
2113 "disable distributing on %s\n", LACP_PORT_XNAME(lacpp));
2114 CLR(lacpp->lp_actor.lpi_state, LACP_STATE_DISTRIBUTING);
2115 }
2116
2117 s = pserialize_read_enter();
2118 act = LACP_PORTMAP_ACTIVE(lsc);
2119 pm = &lsc->lsc_portmaps[act];
2120
2121 do_update = false;
2122 for (i = 0; i < pm->pm_count; i++) {
2123 if (pm->pm_ports[i] == lacpp->lp_laggport) {
2124 do_update = true;
2125 break;
2126 }
2127 }
2128 pserialize_read_exit(s);
2129
2130 if (do_update)
2131 lacp_update_portmap(lsc);
2132 }
2133
2134 static void
2135 lacp_enable_distributing(struct lacp_softc *lsc, struct lacp_port *lacpp)
2136 {
2137
2138 KASSERT(LACP_LOCKED(lsc));
2139
2140 KASSERT(lacp_isactive(lsc, lacpp));
2141
2142 LAGG_LOG(lsc->lsc_softc, LOG_INFO,
2143 "enable distributing on %s\n", LACP_PORT_XNAME(lacpp));
2144 SET(lacpp->lp_actor.lpi_state, LACP_STATE_DISTRIBUTING);
2145 lacp_suppress_distributing(lsc);
2146 lacp_update_portmap(lsc);
2147 }
2148
2149 static void
2150 lacp_select_active_aggregator(struct lacp_softc *lsc)
2151 {
2152 struct lacp_aggregator *la, *best_la;
2153 char str[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
2154
2155 KASSERT(LACP_LOCKED(lsc));
2156
2157 la = lsc->lsc_aggregator;
2158 if (la != NULL && la->la_attached_port > 0) {
2159 best_la = la;
2160 } else {
2161 best_la = NULL;
2162 }
2163
2164 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
2165 if (la->la_attached_port <= 0)
2166 continue;
2167
2168 if (best_la == NULL ||
2169 LACP_SYS_PRI(la) < LACP_SYS_PRI(best_la))
2170 best_la = la;
2171 }
2172
2173 if (best_la != lsc->lsc_aggregator) {
2174 LACP_DPRINTF((lsc, NULL, "active aggregator changed\n"));
2175
2176 if (lsc->lsc_aggregator != NULL) {
2177 LACP_AGGREGATOR_STR(lsc->lsc_aggregator,
2178 str, sizeof(str));
2179 } else {
2180 snprintf(str, sizeof(str), "(null)");
2181 }
2182 LACP_DPRINTF((lsc, NULL, "old aggregator=%s\n", str));
2183
2184 if (best_la != NULL) {
2185 LACP_AGGREGATOR_STR(best_la, str, sizeof(str));
2186 } else {
2187 snprintf(str, sizeof(str), "(null)");
2188 }
2189 LACP_DPRINTF((lsc, NULL, "new aggregator=%s\n", str));
2190
2191 lsc->lsc_aggregator = best_la;
2192 }
2193 }
2194
2195 static void
2196 lacp_port_attached(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 SET(lacpp->lp_actor.lpi_state, LACP_STATE_SYNC);
2210 la->la_attached_port++;
2211 lacp_select_active_aggregator(lsc);
2212 }
2213
2214 static void
2215 lacp_port_detached(struct lacp_softc *lsc, struct lacp_port *lacpp)
2216 {
2217 struct lacp_aggregator *la;
2218
2219 KASSERT(LACP_LOCKED(lsc));
2220
2221 if (!ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_SYNC))
2222 return;
2223
2224 la = lacpp->lp_aggregator;
2225 KASSERT(la != NULL);
2226 KASSERT(la->la_attached_port > 0);
2227
2228 CLR(lacpp->lp_actor.lpi_state, LACP_STATE_SYNC);
2229 la->la_attached_port--;
2230 lacp_select_active_aggregator(lsc);
2231 }
2232
2233 static int
2234 lacp_set_mux(struct lacp_softc *lsc, struct lacp_port *lacpp,
2235 enum lacp_mux_state new_state)
2236 {
2237 struct lagg_softc *sc;
2238 struct ifnet *ifp;
2239
2240 KASSERT(LACP_LOCKED(lsc));
2241
2242 sc = lacpp->lp_laggport->lp_softc;
2243 ifp = &sc->sc_if;
2244
2245 if (lacpp->lp_mux_state == new_state)
2246 return -1;
2247
2248 switch (new_state) {
2249 case LACP_MUX_DETACHED:
2250 lacp_port_detached(lsc, lacpp);
2251 lacp_disable_distributing(lsc, lacpp);
2252 lacp_disable_collecting(lacpp);
2253 lacp_sm_assert_ntt(lacpp);
2254 /* cancel timer */
2255 if (LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE)) {
2256 KASSERT(lacpp->lp_pending > 0);
2257 lacpp->lp_pending--;
2258 LACP_TIMER_DISARM(lacpp, LACP_TIMER_WAIT_WHILE);
2259 }
2260 lacp_unselect(lsc, lacpp);
2261 break;
2262 case LACP_MUX_WAITING:
2263 LACP_TIMER_ARM(lacpp, LACP_TIMER_WAIT_WHILE,
2264 LACP_AGGREGATE_WAIT_TIME);
2265 lacpp->lp_pending++;
2266 break;
2267 case LACP_MUX_STANDBY:
2268 #ifdef LACP_STANDBY_SYNCED
2269 lacp_port_attached(lsc, lacpp);
2270 lacp_disable_collecting(lacpp);
2271 lacp_sm_assert_ntt(lacpp);
2272 #endif
2273 break;
2274 case LACP_MUX_ATTACHED:
2275 lacp_port_attached(lsc, lacpp);
2276 lacp_disable_collecting(lacpp);
2277 lacp_sm_assert_ntt(lacpp);
2278 break;
2279 case LACP_MUX_COLLECTING:
2280 lacp_enable_collecting(lacpp);
2281 lacp_disable_distributing(lsc, lacpp);
2282 lacp_sm_assert_ntt(lacpp);
2283 break;
2284 case LACP_MUX_DISTRIBUTING:
2285 lacp_enable_distributing(lsc, lacpp);
2286 break;
2287 case LACP_MUX_INIT:
2288 default:
2289 panic("%s: unknown state", ifp->if_xname);
2290 }
2291
2292 LACP_DPRINTF((lsc, lacpp, "mux_state %d -> %d\n",
2293 lacpp->lp_mux_state, new_state));
2294
2295 lacpp->lp_mux_state = new_state;
2296 return 0;
2297 }
2298
2299 static void
2300 lacp_sm_mux(struct lacp_softc *lsc, struct lacp_port *lacpp)
2301 {
2302 struct lacp_aggregator *la __diagused;
2303 enum lacp_mux_state next_state;
2304 enum lacp_selected selected;
2305 bool p_sync, p_collecting;
2306
2307 p_sync = ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
2308 p_collecting = ISSET(lacpp->lp_partner.lpi_state,
2309 LACP_STATE_COLLECTING);
2310
2311 do {
2312 next_state = lacpp->lp_mux_state;
2313 la = lacpp->lp_aggregator;
2314 selected = lacpp->lp_selected;
2315 KASSERT(la != NULL ||
2316 lacpp->lp_mux_state == LACP_MUX_DETACHED);
2317
2318 switch (lacpp->lp_mux_state) {
2319 case LACP_MUX_DETACHED:
2320 if (selected != LACP_UNSELECTED)
2321 next_state = LACP_MUX_WAITING;
2322 break;
2323 case LACP_MUX_WAITING:
2324 KASSERTMSG((lacpp->lp_pending > 0 ||
2325 !LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE)),
2326 "lp_pending=%d, timer=%d", lacpp->lp_pending,
2327 !LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE));
2328
2329 if (selected == LACP_UNSELECTED) {
2330 next_state = LACP_MUX_DETACHED;
2331 } else if (lacpp->lp_pending == 0) {
2332 if (selected == LACP_SELECTED) {
2333 next_state = LACP_MUX_ATTACHED;
2334 } else if (selected == LACP_STANDBY) {
2335 next_state = LACP_MUX_STANDBY;
2336 } else {
2337 next_state = LACP_MUX_DETACHED;
2338 }
2339 }
2340 break;
2341 case LACP_MUX_STANDBY:
2342 if (selected == LACP_SELECTED) {
2343 next_state = LACP_MUX_ATTACHED;
2344 } else if (selected != LACP_STANDBY) {
2345 next_state = LACP_MUX_DETACHED;
2346 }
2347 break;
2348 case LACP_MUX_ATTACHED:
2349 if (selected != LACP_SELECTED) {
2350 if (selected == LACP_STANDBY)
2351 LAGG_LOG(lsc->lsc_softc, LOG_INFO,
2352 "detaching %s\n",
2353 LACP_PORT_XNAME(lacpp));
2354 next_state = LACP_MUX_DETACHED;
2355 } else if (lacp_isactive(lsc, lacpp) && p_sync) {
2356 next_state = LACP_MUX_COLLECTING;
2357 }
2358 break;
2359 case LACP_MUX_COLLECTING:
2360 if (selected != LACP_SELECTED ||
2361 !lacp_isactive(lsc, lacpp)
2362 || !p_sync) {
2363 next_state = LACP_MUX_ATTACHED;
2364 } else if (p_collecting) {
2365 next_state = LACP_MUX_DISTRIBUTING;
2366 }
2367 break;
2368 case LACP_MUX_DISTRIBUTING:
2369 if (selected != LACP_SELECTED ||
2370 !lacp_isactive(lsc, lacpp)
2371 || !p_sync || !p_collecting) {
2372 next_state = LACP_MUX_COLLECTING;
2373 LACP_DPRINTF((lsc, lacpp,
2374 "Interface stopped DISTRIBUTING,"
2375 " possible flapping\n"));
2376 }
2377 break;
2378 case LACP_MUX_INIT:
2379 default:
2380 panic("%s: unknown state",
2381 lsc->lsc_softc->sc_if.if_xname);
2382 }
2383 } while (lacp_set_mux(lsc, lacpp, next_state) == 0);
2384 }
2385
2386 static bool
2387 lacp_aggregator_is_match(struct lacp_aggregator_systemid *a,
2388 struct lacp_aggregator_systemid *b)
2389 {
2390
2391 if (a->sid_prio != b->sid_prio)
2392 return false;
2393
2394 if (a->sid_key != b->sid_key)
2395 return false;
2396
2397 if (memcmp(a->sid_mac, b->sid_mac, sizeof(a->sid_mac)) != 0)
2398 return false;
2399
2400 return true;
2401 }
2402
2403 static void
2404 lacp_selected_update(struct lacp_softc *lsc, struct lacp_aggregator *la)
2405 {
2406 struct lacp_port *lacpp;
2407 enum lacp_selected next_selected;
2408 const char *msg;
2409 size_t nselected;
2410 uint64_t linkspeed;
2411
2412 KASSERT(LACP_LOCKED(lsc));
2413
2414 lacpp = LIST_FIRST(&la->la_ports);
2415 if (lacpp == NULL)
2416 return;
2417
2418 linkspeed = lacpp->lp_linkspeed;
2419 nselected = 0;
2420 LIST_FOREACH(lacpp, &la->la_ports, lp_entry_la) {
2421 if (lacpp->lp_selected == LACP_UNSELECTED)
2422 continue;
2423
2424 next_selected = LACP_SELECTED;
2425 msg = " is selected";
2426
2427 if (nselected >= lsc->lsc_max_ports) {
2428 next_selected = LACP_STANDBY;
2429 msg = " is standby because of too many active ports";
2430 }
2431
2432 if (!lsc->lsc_multi_linkspeed &&
2433 linkspeed != lacpp->lp_linkspeed) {
2434 next_selected = LACP_STANDBY;
2435 msg = " is standby because of link speed mismatch";
2436 }
2437
2438 if (lacpp->lp_selected != next_selected) {
2439 lacpp->lp_selected = next_selected;
2440 LAGG_LOG(lsc->lsc_softc, LOG_INFO,
2441 "%s%s\n", LACP_PORT_XNAME(lacpp), msg);
2442 }
2443
2444 if (lacpp->lp_selected == LACP_SELECTED)
2445 nselected++;
2446 }
2447 }
2448
2449 static void
2450 lacp_select(struct lacp_softc *lsc, struct lacp_port *lacpp)
2451 {
2452 struct lacp_aggregator *la;
2453 struct lacp_aggregator_systemid *sid;
2454 struct lacp_port *lacpp0;
2455 char buf[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
2456
2457 if (lacpp->lp_aggregator != NULL)
2458 return;
2459
2460 /* If we haven't heard from our peer, skip this step. */
2461 if (ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_DEFAULTED))
2462 return
2463
2464 KASSERT(!LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE));
2465
2466 sid = &lacpp->lp_aggregator_sidbuf;
2467
2468 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
2469 if (lacp_aggregator_is_match(&la->la_sid, sid))
2470 break;
2471 }
2472
2473 if (la == NULL) {
2474 la = kmem_zalloc(sizeof(*la), KM_NOSLEEP);
2475 if (la == NULL) {
2476 LACP_DPRINTF((lsc, lacpp,
2477 "couldn't allocate aggregator\n"));
2478 /* will retry the next tick. */
2479 return;
2480 }
2481 LIST_INIT(&la->la_ports);
2482
2483 la->la_sid = *sid;
2484 TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
2485 LACP_DPRINTF((lsc, lacpp, "a new aggregator created\n"));
2486 } else {
2487 LACP_DPRINTF((lsc, lacpp, "aggregator found\n"));
2488 }
2489
2490 KASSERT(la != NULL);
2491 LACP_AGGREGATOR_STR(la, buf, sizeof(buf));
2492 LACP_DPRINTF((lsc, lacpp, "aggregator lagid=%s\n", buf));
2493
2494 lacpp->lp_aggregator = la;
2495 lacpp->lp_selected = LACP_READY;
2496
2497 LIST_FOREACH(lacpp0, &la->la_ports, lp_entry_la) {
2498 if (lacp_port_priority_max(lacpp0, lacpp) == lacpp) {
2499 LIST_INSERT_BEFORE(lacpp0, lacpp, lp_entry_la);
2500 break;
2501 }
2502
2503 if (LIST_NEXT(lacpp0, lp_entry_la) == NULL) {
2504 LIST_INSERT_AFTER(lacpp0, lacpp, lp_entry_la);
2505 break;
2506 }
2507 }
2508
2509 if (lacpp0 == NULL)
2510 LIST_INSERT_HEAD(&la->la_ports, lacpp, lp_entry_la);
2511
2512 lacp_selected_update(lsc, la);
2513 }
2514
2515 static void
2516 lacp_unselect(struct lacp_softc *lsc, struct lacp_port *lacpp)
2517 {
2518 struct lacp_aggregator *la;
2519 char buf[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
2520 bool remove_actaggr;
2521
2522 KASSERT(LACP_LOCKED(lsc));
2523 KASSERT(!LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE));
2524
2525 la = lacpp->lp_aggregator;
2526 lacpp->lp_selected = LACP_UNSELECTED;
2527
2528 if (la == NULL)
2529 return;
2530
2531 KASSERT(!LIST_EMPTY(&la->la_ports));
2532
2533 LACP_AGGREGATOR_STR(la, buf, sizeof(buf));
2534 LACP_DPRINTF((lsc, lacpp, "unselect aggregator lagid=%s\n", buf));
2535
2536 LIST_REMOVE(lacpp, lp_entry_la);
2537 lacpp->lp_aggregator = NULL;
2538
2539 if (LIST_EMPTY(&la->la_ports)) {
2540 remove_actaggr = false;
2541
2542 if (la == lsc->lsc_aggregator) {
2543 LACP_DPRINTF((lsc, NULL, "remove active aggregator\n"));
2544 lsc->lsc_aggregator = NULL;
2545 remove_actaggr = true;
2546 }
2547
2548 TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
2549 kmem_free(la, sizeof(*la));
2550
2551 if (remove_actaggr)
2552 lacp_select_active_aggregator(lsc);
2553 } else {
2554 lacp_selected_update(lsc, la);
2555 }
2556 }
2557
2558 static void
2559 lacp_suppress_distributing(struct lacp_softc *lsc)
2560 {
2561 struct lacp_aggregator *la;
2562 struct lacp_port *lacpp;
2563
2564 KASSERT(LACP_LOCKED(lsc));
2565
2566 la = lsc->lsc_aggregator;
2567
2568 LIST_FOREACH(lacpp, &la->la_ports, lp_entry_la) {
2569 if (ISSET(lacpp->lp_actor.lpi_state,
2570 LACP_STATE_DISTRIBUTING)) {
2571 lagg_workq_add(lsc->lsc_workq,
2572 &lacpp->lp_work_marker);
2573 }
2574 }
2575
2576 LACP_PTIMER_ARM(lsc, LACP_PTIMER_DISTRIBUTING,
2577 LACP_TRANSIT_DELAY);
2578 }
2579
2580 static void
2581 lacp_distributing_timer(struct lacp_softc *lsc)
2582 {
2583
2584 KASSERT(LACP_LOCKED(lsc));
2585
2586 if (lsc->lsc_suppress_distributing) {
2587 LACP_DPRINTF((lsc, NULL,
2588 "disable suppress distributing\n"));
2589 lsc->lsc_suppress_distributing = false;
2590 }
2591 }
2592
2593 static struct mbuf *
2594 lacp_markerdu_mbuf(struct lacp_softc *lsc, struct lacp_port *lacpp)
2595 {
2596 struct ifnet *ifp_port;
2597 struct mbuf *m;
2598 struct markerdu *mdu;
2599 struct markerdu_info *mi;
2600
2601 KASSERT(LACP_LOCKED(lsc));
2602
2603 ifp_port = lacpp->lp_laggport->lp_ifp;
2604
2605 MGETHDR(m, M_DONTWAIT, MT_DATA);
2606 if (m == NULL) {
2607 lsc->lsc_mgethdr_failed.ev_count++;
2608 return NULL;
2609 }
2610
2611 m->m_pkthdr.len = m->m_len = sizeof(*mdu);
2612 m_reset_rcvif(m);
2613
2614 mdu = mtod(m, struct markerdu *);
2615
2616 memset(mdu, 0, sizeof(*mdu));
2617
2618 m->m_flags |= M_MCAST;
2619 memcpy(mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
2620 ETHER_ADDR_LEN);
2621 memcpy(mdu->mdu_eh.ether_shost, CLLADDR(ifp_port->if_sadl),
2622 ETHER_ADDR_LEN);
2623 mdu->mdu_eh.ether_type = ntohs(ETHERTYPE_SLOWPROTOCOLS);
2624 mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER;
2625 mdu->mdu_sph.sph_version = 1;
2626
2627 mi = &mdu->mdu_info;
2628 tlv_set(&mdu->mdu_tlv_info, MARKER_TYPE_INFO,
2629 sizeof(*mi));
2630 mi->mi_rq_port = lacpp->lp_actor.lpi_portno;
2631 mi->mi_rq_xid = htonl(lacpp->lp_marker_xid);
2632 memcpy(mi->mi_rq_system, lsc->lsc_system_mac, LACP_MAC_LEN);
2633
2634 mdu->mdu_tlv_term.tlv_type = MARKER_TYPE_TERMINATE;
2635 mdu->mdu_tlv_term.tlv_length = 0;
2636
2637 return m;
2638 }
2639
2640 static void
2641 lacp_marker_work(struct lagg_work *lw, void *xlsc)
2642 {
2643 struct lacp_softc *lsc;
2644 struct lacp_port *lacpp;
2645 struct lagg_port *lp;
2646 struct markerdu *mdu;
2647 struct mbuf *m;
2648 struct psref psref;
2649 int bound;
2650
2651 lsc = xlsc;
2652 lacpp = container_of(lw, struct lacp_port, lp_work_marker);
2653
2654 LACP_LOCK(lsc);
2655 lacpp->lp_marker_xid++;
2656 m = lacp_markerdu_mbuf(lsc, lacpp);
2657 if (m == NULL) {
2658 LACP_UNLOCK(lsc);
2659 return;
2660 }
2661 SET(lacpp->lp_flags, LACP_PORT_MARK);
2662 lsc->lsc_suppress_distributing = true;
2663 lp = lacpp->lp_laggport;
2664 bound = curlwp_bind();
2665 lagg_port_getref(lp, &psref);
2666 LACP_UNLOCK(lsc);
2667
2668 if (LACP_ISDUMPING(lsc)) {
2669 lacp_dprintf(lsc, lacpp, "markerdu transmit\n");
2670 mdu = mtod(m, struct markerdu *);
2671 lacp_dump_markertlv(&mdu->mdu_info, NULL);
2672 }
2673
2674 lagg_port_xmit(lp, m);
2675 lagg_port_putref(lp, &psref);
2676 curlwp_bindx(bound);
2677 }
2678
2679 static void
2680 lacp_dump_lacpdutlv(const struct lacpdu_peerinfo *pi_actor,
2681 const struct lacpdu_peerinfo *pi_partner,
2682 const struct lacpdu_collectorinfo *lci)
2683 {
2684 char str[LACP_STATESTR_LEN];
2685
2686 if (pi_actor != NULL) {
2687 lacp_peerinfo_idstr(pi_actor, str, sizeof(str));
2688 printf("actor=%s\n", str);
2689 lacp_state_str(pi_actor->lpi_state,
2690 str, sizeof(str));
2691 printf("actor.state=%s portno=%d portprio=0x%04x\n",
2692 str,
2693 ntohs(pi_actor->lpi_port_no),
2694 ntohs(pi_actor->lpi_port_prio));
2695 } else {
2696 printf("no actor info\n");
2697 }
2698
2699 if (pi_partner != NULL) {
2700 lacp_peerinfo_idstr(pi_partner, str, sizeof(str));
2701 printf("partner=%s\n", str);
2702 lacp_state_str(pi_partner->lpi_state,
2703 str, sizeof(str));
2704 printf("partner.state=%s portno=%d portprio=0x%04x\n",
2705 str,
2706 ntohs(pi_partner->lpi_port_no),
2707 ntohs(pi_partner->lpi_port_prio));
2708 } else {
2709 printf("no partner info\n");
2710 }
2711
2712 if (lci != NULL) {
2713 printf("maxdelay=%d\n", ntohs(lci->lci_maxdelay));
2714 } else {
2715 printf("no collector info\n");
2716 }
2717 }
2718
2719 static void
2720 lacp_dump_markertlv(const struct markerdu_info *mi_info,
2721 const struct markerdu_info *mi_res)
2722 {
2723
2724 if (mi_info != NULL) {
2725 printf("marker info: port=%d, sys=%s, id=%u\n",
2726 ntohs(mi_info->mi_rq_port),
2727 ether_sprintf(mi_info->mi_rq_system),
2728 ntohl(mi_info->mi_rq_xid));
2729 }
2730
2731 if (mi_res != NULL) {
2732 printf("marker resp: port=%d, sys=%s, id=%u\n",
2733 ntohs(mi_res->mi_rq_port),
2734 ether_sprintf(mi_res->mi_rq_system),
2735 ntohl(mi_res->mi_rq_xid));
2736 }
2737 }
2738
2739 /*
2740 * lacp_linkstate:
2741 * callback on link state changed.
2742 * enable, disable or reset LACP processing on the physical port.
2743 */
2744
2745 static void
2746 lacp_linkstate(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
2747 {
2748
2749 IFNET_ASSERT_UNLOCKED(lp->lp_ifp);
2750
2751 IFNET_LOCK(lp->lp_ifp);
2752 lacp_linkstate_ifnet_locked(xlsc, lp);
2753 IFNET_UNLOCK(lp->lp_ifp);
2754 }
2755
2756 static void
2757 lacp_linkspeed_work(struct lagg_work *lw __unused, void *xlsc)
2758 {
2759 struct lacp_softc *lsc = (struct lacp_softc *)xlsc;
2760 struct lagg_softc *sc = lsc->lsc_softc;
2761 struct lacp_portmap *pm;
2762 struct lagg_port *lp;
2763 struct lacp_port *lacpp;
2764 uint64_t linkspeed;
2765 size_t act, i;
2766
2767 linkspeed = 0;
2768
2769 LACP_LOCK(lsc);
2770 act = LACP_PORTMAP_ACTIVE(lsc);
2771 pm = &lsc->lsc_portmaps[act];
2772 for (i = 0; i < pm->pm_count; i++) {
2773 lp = pm->pm_ports[i];
2774 lacpp = lp->lp_proto_ctx;
2775 linkspeed = MAX(linkspeed, lacpp->lp_linkspeed);
2776 }
2777 LACP_UNLOCK(lsc);
2778
2779 LAGG_LOCK(sc);
2780 lagg_set_linkspeed(sc, linkspeed);
2781 LAGG_UNLOCK(sc);
2782 }
2783