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