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