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