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