if_lagg_lacp.c revision 1.3 1 /* $NetBSD: if_lagg_lacp.c,v 1.3 2021/06/30 06:39:47 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.3 2021/06/30 06:39:47 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/lagg/if_lagg.h>
56 #include <net/lagg/if_laggproto.h>
57 #include <net/lagg/if_lagg_lacp.h>
58
59 #define LACP_SYSTEMIDSTR_LEN 32
60
61 enum {
62 LACP_TIMER_CURRENT_WHILE = 0,
63 LACP_TIMER_PERIODIC,
64 LACP_TIMER_WAIT_WHILE,
65 LACP_NTIMER
66 };
67
68 enum {
69 LACP_PTIMER_DISTRIBUTING = 0,
70 LACP_NPTIMER
71 };
72
73 enum lacp_selected {
74 LACP_UNSELECTED,
75 LACP_STANDBY,
76 LACP_SELECTED,
77 };
78
79 enum lacp_mux_state {
80 LACP_MUX_DETACHED,
81 LACP_MUX_WAITING,
82 LACP_MUX_ATTACHED,
83 LACP_MUX_COLLECTING,
84 LACP_MUX_DISTRIBUTING,
85 LACP_MUX_INIT,
86 };
87
88 struct lacp_aggregator_systemid {
89 uint16_t sid_prio;
90 uint16_t sid_key;
91 uint8_t sid_mac[LACP_MAC_LEN];
92 };
93
94 struct lacp_aggregator {
95 TAILQ_ENTRY(lacp_aggregator)
96 la_q;
97 LIST_HEAD(, lacp_port)
98 la_ports;
99 ssize_t la_attached_port;
100
101 struct lacp_aggregator_systemid
102 la_sid;
103 };
104
105 struct lacp_portinfo {
106 uint8_t lpi_state;
107 uint16_t lpi_portno;
108 #define LACP_PORTNO_NONE 0
109 uint16_t lpi_portprio;
110 };
111
112 struct lacp_port {
113 struct lagg_port *lp_laggport;
114 int lp_timer[LACP_NTIMER];
115 uint32_t lp_marker_xid;
116 enum lacp_selected lp_selected;
117 enum lacp_mux_state lp_mux_state;
118
119 struct lacp_portinfo lp_actor;
120 struct lacp_portinfo lp_partner;
121 struct lacp_aggregator *lp_aggregator;
122 struct lacp_aggregator_systemid
123 lp_aggregator_sidbuf;
124 uint32_t lp_media;
125 int lp_pending;
126 LIST_ENTRY(lacp_port) lp_entry_la;
127 struct timeval lp_last_lacpdu;
128 int lp_lacpdu_sent;
129 bool lp_collector;
130
131 unsigned int lp_flags;
132 #define LACP_PORT_NTT __BIT(0)
133 #define LACP_PORT_MARK __BIT(1)
134
135 struct lagg_work lp_work_smtx;
136 struct lagg_work lp_work_marker;
137 };
138
139 struct lacp_portmap {
140 size_t pm_count;
141 struct lagg_port *pm_ports[LACP_MAX_PORTS];
142 };
143
144 struct lacp_softc {
145 struct lagg_softc *lsc_softc;
146 kmutex_t lsc_lock;
147 pserialize_t lsc_psz;
148 bool lsc_running;
149 bool lsc_suppress_distributing;
150 int lsc_timer[LACP_NPTIMER];
151 uint8_t lsc_system_mac[LACP_MAC_LEN];
152 uint16_t lsc_system_prio;
153 uint16_t lsc_key;
154 size_t lsc_max_ports;
155 size_t lsc_activemap;
156 struct lacp_portmap lsc_portmaps[2]; /* active & idle */
157 struct lacp_aggregator *lsc_aggregator;
158 TAILQ_HEAD(, lacp_aggregator)
159 lsc_aggregators;
160 struct workqueue *lsc_workq;
161 struct lagg_work lsc_work_tick;
162 callout_t lsc_tick;
163
164 char lsc_evgroup[32];
165 struct evcnt lsc_mgethdr_failed;
166 struct evcnt lsc_mpullup_failed;
167 struct evcnt lsc_badlacpdu;
168 struct evcnt lsc_badmarkerdu;
169
170 bool lsc_optimistic;
171 bool lsc_stop_lacpdu;
172 bool lsc_dump_du;
173 bool lsc_multi_linkspeed;
174 };
175
176 /*
177 * Locking notes:
178 * - Items in struct lacp_softc are protected by
179 * lsc_lock (an adaptive mutex)
180 * - lsc_activemap is protected by pserialize (lsc_psz)
181 * - Items of struct lagg_port in lsc_portmaps are protected by
182 * protected by both pserialize (lsc_psz) and psref (lp_psref)
183 * - Updates for lsc_activemap and lsc_portmaps is serialized by
184 * sc_lock in struct lagg_softc
185 * - Other locking notes are described in if_laggproto.h
186 */
187
188 static void lacp_dprintf(const struct lacp_softc *,
189 const struct lacp_port *, const char *, ...)
190 __attribute__((__format__(__printf__, 3, 4)));
191
192 #ifdef LACP_DEBUG
193 #define LACP_DPRINTF(a) do { lacp_dprintf a; } while (/*CONSTCOND*/ 0)
194 #define LACP_PEERINFO_IDSTR(_pi, _b, _bs) \
195 lacp_peerinfo_idstr(_pi, _b, _bs)
196 #define LACP_STATE_STR(_s, _b, _bs) lacp_state_str(_s, _b, _bs)
197 #define LACP_AGGREGATOR_STR(_a, _b, _bs) \
198 lacp_aggregator_str(_a, _b, _bs)
199 #define __LACPDEBUGUSED
200 #else
201 #define LACP_DPRINTF(a) __nothing
202 #define LACP_PEERINFO_IDSTR(_pi, _b, _bs) __nothing
203 #define LACP_STATE_STR(_s, _b, _bs) __nothing
204 #define LACP_AGGREGATOR_STR(_a, _b, _bs) __nothing
205 #define __LACPDEBUGUSED __unused
206 #endif
207
208 #define LACP_LOCK(_sc) mutex_enter(&(_sc)->lsc_lock)
209 #define LACP_UNLOCK(_sc) mutex_exit(&(_sc)->lsc_lock)
210 #define LACP_LOCKED(_sc) mutex_owned(&(_sc)->lsc_lock)
211 #define LACP_LINKSTATE(_sc, _lp) \
212 lacp_linkstate((struct lagg_proto_softc *)(_sc), (_lp))
213 #define LACP_TIMER_ARM(_lacpp, _timer, _val) \
214 (_lacpp)->lp_timer[(_timer)] = (_val)
215 #define LACP_TIMER_DISARM(_lacpp, _timer) \
216 LACP_TIMER_ARM((_lacpp), (_timer), 0)
217 #define LACP_TIMER_ISARMED(_lacpp, _timer) \
218 ((_lacpp)->lp_timer[(_timer)] > 0)
219 #define LACP_PTIMER_ARM(_sc, _timer, _val) \
220 (_sc)->lsc_timer[(_timer)] = (_val)
221 #define LACP_PTIMER_DISARM(_sc, _timer) \
222 LACP_PTIMER_ARM((_sc), (_timer), 0)
223 #define LACP_PTIMER_ISARMED(_sc, _timer) \
224 ((_sc)->lsc_timer[(_timer)] > 0)
225 #define LACP_STATE_EQ(_s1, _s2, _mask) (!ISSET((_s1) ^ (_s2), (_mask)))
226 #define LACP_PORT_XNAME(_lacpp) (_lacpp != NULL) ? \
227 (_lacpp)->lp_laggport->lp_ifp->if_xname : "(unknown)"
228 #define LACP_ISDUMPING(_sc) (_sc)->lsc_dump_du
229 #define LACP_PORTMAP_ACTIVE(_sc) \
230 atomic_load_consume(&(_sc)->lsc_activemap)
231 #define LACP_PORTMAP_NEXT(_sc) \
232 (((LACP_PORTMAP_ACTIVE((_sc))) ^ 0x01) &0x01)
233 #define LACP_SYS_PRI(_la) ntohs((_la)->la_sid.sid_prio)
234 #define LACP_TLV_PARSE(_du, _st, _name, _tlvlist) \
235 tlv_parse(&(_du)->_name, \
236 sizeof(_st) - offsetof(_st, _name), \
237 (_tlvlist))
238
239 static void lacp_tick(void *);
240 static void lacp_tick_work(struct lagg_work *, void *);
241 static uint32_t lacp_ifmedia2lacpmedia(u_int);
242 static void lacp_port_disable(struct lacp_softc *, struct lacp_port *);
243 static void lacp_port_enable(struct lacp_softc *, struct lacp_port *);
244 static void lacp_peerinfo_actor(struct lacp_softc *, struct lacp_port *,
245 struct lacpdu_peerinfo *);
246 static void lacp_peerinfo_partner(struct lacp_port *,
247 struct lacpdu_peerinfo *);
248 static struct lagg_port *
249 lacp_select_tx_port(struct lacp_softc *, struct mbuf *,
250 struct psref *);
251 static void lacp_suppress_distributing(struct lacp_softc *);
252 static void lacp_distributing_timer(struct lacp_softc *);
253
254 static void lacp_select(struct lacp_softc *, struct lacp_port *);
255 static void lacp_unselect(struct lacp_softc *, struct lacp_port *);
256 static void lacp_selected_update(struct lacp_softc *,
257 struct lacp_aggregator *);
258 static void lacp_sm_port_init(struct lacp_softc *,
259 struct lacp_port *, struct lagg_port *);
260 static int lacp_set_mux(struct lacp_softc *,
261 struct lacp_port *, enum lacp_mux_state);
262 static void lacp_sm_mux(struct lacp_softc *, struct lacp_port *);
263 static void lacp_sm_mux_timer(struct lacp_softc *, struct lacp_port *);
264 static void lacp_sm_rx(struct lacp_softc *, struct lacp_port *,
265 struct lacpdu_peerinfo *, struct lacpdu_peerinfo *);
266 static void lacp_sm_rx_set_expired(struct lacp_port *);
267 static void lacp_sm_rx_timer(struct lacp_softc *, struct lacp_port *);
268 static void lacp_sm_rx_record_default(struct lacp_softc *,
269 struct lacp_port *);
270
271 static void lacp_sm_tx(struct lacp_softc *, struct lacp_port *);
272 static void lacp_sm_tx_work(struct lagg_work *, void *);
273 static void lacp_sm_ptx_timer(struct lacp_softc *, struct lacp_port *);
274 static void lacp_sm_ptx_schedule(struct lacp_port *);
275 static void lacp_sm_ptx_update_timeout(struct lacp_port *, uint8_t);
276
277 static void lacp_marker_work(struct lagg_work *, void *);
278 static void lacp_dump_lacpdutlv(const struct lacpdu_peerinfo *,
279 const struct lacpdu_peerinfo *,
280 const struct lacpdu_collectorinfo *);
281 static void lacp_dump_markertlv(const struct markerdu_info *,
282 const struct markerdu_info *);
283
284 typedef void (*lacp_timer_func_t)(struct lacp_softc *, struct lacp_port *);
285 static const lacp_timer_func_t lacp_timer_funcs[] = {
286 [LACP_TIMER_CURRENT_WHILE] = lacp_sm_rx_timer,
287 [LACP_TIMER_PERIODIC] = lacp_sm_ptx_timer,
288 [LACP_TIMER_WAIT_WHILE] = lacp_sm_mux_timer,
289 };
290 typedef void (*lacp_prototimer_func_t)(struct lacp_softc *);
291 static const lacp_prototimer_func_t lacp_ptimer_funcs[] = {
292 [LACP_PTIMER_DISTRIBUTING] = lacp_distributing_timer,
293 };
294
295 static void
296 lacp_dprintf(const struct lacp_softc *lsc, const struct lacp_port *lacpp,
297 const char *fmt, ...)
298 {
299 struct lagg_softc *sc;
300 va_list va;
301
302 if (lsc != NULL && lsc->lsc_softc != NULL) {
303 sc = lsc->lsc_softc;
304 printf("%s", sc->sc_if.if_xname);
305 } else {
306 printf("lacp");
307 }
308
309 if (lacpp != NULL)
310 printf("(%s)", LACP_PORT_XNAME(lacpp));
311
312 printf(": ");
313
314 va_start(va, fmt);
315 vprintf(fmt, va);
316 va_end(va);
317 }
318
319 static inline void
320 lacp_evcnt_attach(struct lacp_softc *lsc,
321 struct evcnt *ev, const char *name)
322 {
323
324 evcnt_attach_dynamic(ev, EVCNT_TYPE_MISC, NULL,
325 lsc->lsc_evgroup, name);
326 }
327
328 static inline bool
329 lacp_iscollecting(struct lacp_port *lacpp)
330 {
331
332 return atomic_load_relaxed(&lacpp->lp_collector);
333 }
334
335 static inline bool
336 lacp_isdistributing(struct lacp_port *lacpp)
337 {
338
339 return ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_DISTRIBUTING);
340 }
341
342 static inline bool
343 lacp_isactive(struct lacp_softc *lsc, struct lacp_port *lacpp)
344 {
345
346 if (lacpp->lp_selected != LACP_SELECTED)
347 return false;
348
349 if (lacpp->lp_aggregator == NULL)
350 return false;
351
352 if (lacpp->lp_aggregator != lsc->lsc_aggregator)
353 return false;
354
355 return true;
356 }
357
358 static inline void
359 lacp_mcastaddr(struct ifreq *ifr, const char *if_xname)
360 {
361 static const uint8_t addr[ETHER_ADDR_LEN] =
362 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
363
364 memset(ifr, 0, sizeof(*ifr));
365
366 strlcpy(ifr->ifr_name, if_xname,
367 sizeof(ifr->ifr_name));
368 ifr->ifr_addr.sa_len = sizeof(ifr->ifr_addr);
369 ifr->ifr_addr.sa_family = AF_UNSPEC;
370
371 KASSERT(sizeof(ifr->ifr_addr) >= sizeof(addr));
372 memcpy(&ifr->ifr_addr.sa_data, addr, sizeof(addr));
373 }
374
375 static inline u_int
376 lacp_portmap_linkstate(struct lacp_portmap *pm)
377 {
378
379 if (pm->pm_count == 0)
380 return LINK_STATE_DOWN;
381
382 return LINK_STATE_UP;
383 }
384
385 static inline struct lacp_port *
386 lacp_port_priority_max(struct lacp_port *a, struct lacp_port *b)
387 {
388 uint16_t pri_a, pri_b;
389
390 pri_a = ntohs(a->lp_actor.lpi_portprio);
391 pri_b = ntohs(b->lp_actor.lpi_portprio);
392
393 if (pri_a < pri_b)
394 return a;
395 if (pri_b < pri_a)
396 return b;
397
398 pri_a = ntohs(a->lp_partner.lpi_portprio);
399 pri_b = ntohs(b->lp_partner.lpi_portprio);
400
401 if (pri_a < pri_b)
402 return a;
403 if (pri_b < pri_a)
404 return b;
405
406 if (a->lp_media > b->lp_media)
407 return a;
408 if (b->lp_media > a->lp_media)
409 return b;
410
411 return a;
412 }
413
414 static void
415 tlv_parse(void *vp, size_t len, struct tlv *list)
416 {
417 struct tlvhdr *th;
418 uint8_t *p;
419 size_t l, i;
420
421 th = (struct tlvhdr *)vp;
422 p = (uint8_t *)vp;
423
424 for (l = 0; l < len; l += th->tlv_length) {
425 th = (struct tlvhdr *)(p + l);
426
427 if (th->tlv_type == TLV_TYPE_TERMINATE)
428 break;
429
430 for (i = 0; list[i].tlv_t != TLV_TYPE_TERMINATE; i++) {
431 if (th->tlv_type != list[i].tlv_t)
432 continue;
433
434 if (th->tlv_length - sizeof(*th) != list[i].tlv_l)
435 break;
436
437 if (list[i].tlv_v == NULL) {
438 list[i].tlv_v =
439 (void *)((uint8_t *)th + sizeof(*th));
440 }
441
442 break;
443 }
444 }
445 }
446
447 int
448 lacp_attach(struct lagg_softc *sc, struct lagg_proto_softc **lscp)
449 {
450 struct lacp_softc *lsc;
451 char xnamebuf[MAXCOMLEN];
452 int error;
453
454 KASSERT(LAGG_LOCKED(sc));
455
456 lsc = kmem_zalloc(sizeof(*lsc), KM_NOSLEEP);
457 if (lsc == NULL)
458 return ENOMEM;
459
460 mutex_init(&lsc->lsc_lock, MUTEX_DEFAULT, IPL_SOFTNET);
461 lsc->lsc_softc = sc;
462 lsc->lsc_key = htons(if_get_index(&sc->sc_if));
463 lsc->lsc_system_prio = htons(LACP_SYSTEM_PRIO);
464 lsc->lsc_running = false;
465 lsc->lsc_max_ports = LACP_MAX_PORTS;
466 lsc->lsc_multi_linkspeed = true;
467 TAILQ_INIT(&lsc->lsc_aggregators);
468
469 lagg_work_set(&lsc->lsc_work_tick, lacp_tick_work, lsc);
470 snprintf(xnamebuf, sizeof(xnamebuf), "%s.lacp",
471 sc->sc_if.if_xname);
472 lsc->lsc_workq = lagg_workq_create(xnamebuf,
473 PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
474 if (lsc->lsc_workq == NULL) {
475 lagg_log(sc, LOG_ERR, "workqueue create failed\n");
476 error = ENOMEM;
477 goto destroy_lock;
478 }
479
480 lsc->lsc_psz = pserialize_create();
481
482 callout_init(&lsc->lsc_tick, CALLOUT_MPSAFE);
483 callout_setfunc(&lsc->lsc_tick, lacp_tick, lsc);
484
485 snprintf(lsc->lsc_evgroup, sizeof(lsc->lsc_evgroup),
486 "%s-lacp", sc->sc_if.if_xname);
487 lacp_evcnt_attach(lsc, &lsc->lsc_mgethdr_failed, "MGETHDR failed");
488 lacp_evcnt_attach(lsc, &lsc->lsc_mpullup_failed, "m_pullup failed");
489 lacp_evcnt_attach(lsc, &lsc->lsc_badlacpdu, "Bad LACPDU recieved");
490 lacp_evcnt_attach(lsc, &lsc->lsc_badmarkerdu, "Bad MarkerDU recieved");
491
492 if_link_state_change(&sc->sc_if, LINK_STATE_DOWN);
493
494 *lscp = (struct lagg_proto_softc *)lsc;
495 return 0;
496
497 destroy_lock:
498 mutex_destroy(&lsc->lsc_lock);
499
500 return error;
501 }
502
503 void
504 lacp_detach(struct lagg_proto_softc *xlsc)
505 {
506 struct lacp_softc *lsc = (struct lacp_softc *)xlsc;
507 struct lagg_softc *sc __diagused = lsc->lsc_softc;
508
509 KASSERT(LAGG_LOCKED(lsc->lsc_softc));
510 KASSERT(TAILQ_EMPTY(&lsc->lsc_aggregators));
511 KASSERT(SIMPLEQ_EMPTY(&sc->sc_ports));
512
513 lacp_down(xlsc);
514
515 evcnt_detach(&lsc->lsc_mgethdr_failed);
516 evcnt_detach(&lsc->lsc_mpullup_failed);
517 evcnt_detach(&lsc->lsc_badlacpdu);
518 evcnt_detach(&lsc->lsc_badmarkerdu);
519 lagg_workq_destroy(lsc->lsc_workq);
520 pserialize_destroy(lsc->lsc_psz);
521 mutex_destroy(&lsc->lsc_lock);
522 kmem_free(lsc, sizeof(*lsc));
523 }
524
525 int
526 lacp_up(struct lagg_proto_softc *xlsc)
527 {
528 struct lagg_softc *sc;
529 struct lagg_port *lp;
530 struct lacp_softc *lsc;
531
532 lsc = (struct lacp_softc *)xlsc;
533 sc = lsc->lsc_softc;
534
535 KASSERT(LAGG_LOCKED(sc));
536
537 LACP_LOCK(lsc);
538 lsc->lsc_running = true;
539 callout_schedule(&lsc->lsc_tick, hz);
540 LACP_UNLOCK(lsc);
541
542 LAGG_PORTS_FOREACH(sc, lp) {
543 lacp_linkstate(xlsc, lp);
544 }
545
546 LACP_DPRINTF((lsc, NULL, "lacp start\n"));
547
548 return 0;
549 }
550
551 static void
552 lacp_down_locked(struct lacp_softc *lsc)
553 {
554 struct lagg_softc *sc;
555 struct lagg_port *lp;
556
557 sc = lsc->lsc_softc;
558
559 KASSERT(LAGG_LOCKED(sc));
560 KASSERT(LACP_LOCKED(lsc));
561
562 lsc->lsc_running = false;
563 callout_halt(&lsc->lsc_tick, &lsc->lsc_lock);
564
565 LAGG_PORTS_FOREACH(sc, lp) {
566 lacp_port_disable(lsc, lp->lp_proto_ctx);
567 }
568
569 LACP_DPRINTF((lsc, NULL, "lacp stopped\n"));
570 }
571
572 void
573 lacp_down(struct lagg_proto_softc *xlsc)
574 {
575 struct lacp_softc *lsc;
576
577 lsc = (struct lacp_softc *)xlsc;
578
579 KASSERT(LAGG_LOCKED(lsc->lsc_softc));
580
581 LACP_LOCK(lsc);
582 lacp_down_locked(lsc);
583 LACP_UNLOCK(lsc);
584 }
585
586 int
587 lacp_transmit(struct lagg_proto_softc *xlsc, struct mbuf *m)
588 {
589 struct lacp_softc *lsc;
590 struct lagg_port *lp;
591 struct ifnet *ifp;
592 struct psref psref;
593
594 lsc = (struct lacp_softc *)xlsc;
595
596 if (__predict_false(lsc->lsc_suppress_distributing)) {
597 LACP_DPRINTF((lsc, NULL, "waiting transit\n"));
598 m_freem(m);
599 return ENOBUFS;
600 }
601
602 lp = lacp_select_tx_port(lsc, m, &psref);
603 if (__predict_false(lp == NULL)) {
604 LACP_DPRINTF((lsc, NULL, "no distributing port\n"));
605 ifp = &lsc->lsc_softc->sc_if;
606 if_statinc(ifp, if_oerrors);
607 m_freem(m);
608 return ENOENT;
609 }
610
611 lagg_enqueue(lsc->lsc_softc, lp, m);
612 lagg_port_putref(lp, &psref);
613
614 return 0;
615 }
616
617 int
618 lacp_allocport(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
619 {
620 struct lagg_softc *sc;
621 struct lacp_softc *lsc;
622 struct lacp_port *lacpp;
623 struct ifreq ifr;
624 int error;
625
626 lsc = (struct lacp_softc *)xlsc;
627 sc = lsc->lsc_softc;
628
629 KASSERT(LAGG_LOCKED(sc));
630
631 IFNET_LOCK(lp->lp_ifp);
632 lacp_mcastaddr(&ifr, lp->lp_ifp->if_xname);
633 error = lp->lp_ioctl(lp->lp_ifp, SIOCADDMULTI, (void *)&ifr);
634 IFNET_UNLOCK(lp->lp_ifp);
635
636 if (error != 0) {
637 lagg_log(sc, LOG_ERR, "SIOCADDMULTI failed on %s\n",
638 lp->lp_ifp->if_xname);
639 return error;
640 }
641
642 lacpp = kmem_zalloc(sizeof(*lacpp), KM_NOSLEEP);
643 if (lacpp == NULL)
644 return ENOMEM;
645
646 lagg_work_set(&lacpp->lp_work_smtx, lacp_sm_tx_work, lsc);
647 lagg_work_set(&lacpp->lp_work_marker, lacp_marker_work, lsc);
648
649 LACP_LOCK(lsc);
650 if (LAGG_PORTS_EMPTY(sc)) {
651 memcpy(lsc->lsc_system_mac, LAGG_CLLADDR(sc),
652 sizeof(lsc->lsc_system_mac));
653 }
654 lacp_sm_port_init(lsc, lacpp, lp);
655 LACP_UNLOCK(lsc);
656
657 lp->lp_proto_ctx = (void *)lacpp;
658 lp->lp_prio = ntohs(lacpp->lp_actor.lpi_portprio);
659 LACP_LINKSTATE(lsc, lp);
660
661 return 0;
662 }
663
664 void
665 lacp_startport(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
666 {
667 struct lacp_port *lacpp;
668 uint16_t prio;
669
670 lacpp = lp->lp_proto_ctx;
671
672 prio = (uint16_t)MIN(lp->lp_prio, UINT16_MAX);
673 lacpp->lp_actor.lpi_portprio = htons(prio);
674
675 LACP_LINKSTATE((struct lacp_softc *)xlsc, lp);
676 }
677
678 void
679 lacp_stopport(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
680 {
681 struct lacp_softc *lsc;
682 struct lacp_port *lacpp;
683 int i;
684
685 lsc = (struct lacp_softc *)xlsc;
686 lacpp = lp->lp_proto_ctx;
687
688 KASSERT(LAGG_LOCKED(lsc->lsc_softc));
689
690 LACP_LOCK(lsc);
691 for (i = 0; i < LACP_NTIMER; i++) {
692 LACP_TIMER_DISARM(lacpp, i);
693 }
694
695 lacp_port_disable(lsc, lacpp);
696 LACP_UNLOCK(lsc);
697 }
698
699 void
700 lacp_freeport(struct lagg_proto_softc *xlsc, struct lagg_port *lp)
701 {
702 struct lacp_softc *lsc;
703 struct lacp_port *lacpp, *lacpp0;
704 struct lagg_softc *sc;
705 struct lagg_port *lp0;
706 struct ifreq ifr;
707
708 lsc = (struct lacp_softc *)xlsc;
709 sc = lsc->lsc_softc;
710 lacpp = lp->lp_proto_ctx;
711
712 KASSERT(LAGG_LOCKED(sc));
713
714 lagg_workq_wait(lsc->lsc_workq, &lacpp->lp_work_smtx);
715 lagg_workq_wait(lsc->lsc_workq, &lacpp->lp_work_marker);
716
717 lacp_mcastaddr(&ifr, LACP_PORT_XNAME(lacpp));
718
719 IFNET_LOCK(lp->lp_ifp);
720 (void)lp->lp_ioctl(lp->lp_ifp, SIOCDELMULTI, (void *)&ifr);
721 IFNET_UNLOCK(lp->lp_ifp);
722
723 LACP_LOCK(lsc);
724 if (LAGG_PORTS_EMPTY(sc)) {
725 memset(lsc->lsc_system_mac, 0,
726 sizeof(lsc->lsc_system_mac));
727 } else if (memcmp(LAGG_CLLADDR(sc), lsc->lsc_system_mac,
728 sizeof(lsc->lsc_system_mac)) != 0) {
729 memcpy(lsc->lsc_system_mac, LAGG_CLLADDR(sc),
730 sizeof(lsc->lsc_system_mac));
731 LAGG_PORTS_FOREACH(sc, lp0) {
732 lacpp0 = lp0->lp_proto_ctx;
733 lacpp0->lp_selected = LACP_UNSELECTED;
734 /* reset state of the port at lacp_set_mux() */
735 }
736 }
737 LACP_UNLOCK(lsc);
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_ATTACHED:
2125 lacp_port_attached(lsc, lacpp);
2126 lacp_disable_collecting(lacpp);
2127 lacp_sm_assert_ntt(lacpp);
2128 break;
2129 case LACP_MUX_COLLECTING:
2130 lacp_enable_collecting(lacpp);
2131 lacp_disable_distributing(lsc, lacpp);
2132 lacp_sm_assert_ntt(lacpp);
2133 break;
2134 case LACP_MUX_DISTRIBUTING:
2135 lacp_enable_distributing(lsc, lacpp);
2136 break;
2137 case LACP_MUX_INIT:
2138 default:
2139 panic("%s: unknown state", ifp->if_xname);
2140 }
2141
2142 LACP_DPRINTF((lsc, lacpp, "mux_state %d -> %d\n",
2143 lacpp->lp_mux_state, new_state));
2144
2145 lacpp->lp_mux_state = new_state;
2146 return 0;
2147 }
2148
2149 static void
2150 lacp_sm_mux(struct lacp_softc *lsc, struct lacp_port *lacpp)
2151 {
2152 struct lacp_aggregator *la __diagused;
2153 enum lacp_mux_state next_state;
2154 enum lacp_selected selected;
2155 bool p_sync, p_collecting;
2156
2157 p_sync = ISSET(lacpp->lp_partner.lpi_state, LACP_STATE_SYNC);
2158 p_collecting = ISSET(lacpp->lp_partner.lpi_state,
2159 LACP_STATE_COLLECTING);
2160
2161 do {
2162 next_state = lacpp->lp_mux_state;
2163 la = lacpp->lp_aggregator;
2164 selected = lacpp->lp_selected;
2165 KASSERT(la != NULL ||
2166 lacpp->lp_mux_state == LACP_MUX_DETACHED);
2167
2168 switch (lacpp->lp_mux_state) {
2169 case LACP_MUX_DETACHED:
2170 if (selected != LACP_UNSELECTED)
2171 next_state = LACP_MUX_WAITING;
2172 break;
2173 case LACP_MUX_WAITING:
2174 KASSERTMSG((lacpp->lp_pending > 0 ||
2175 !LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE)),
2176 "lp_pending=%d, timer=%d", lacpp->lp_pending,
2177 !LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE));
2178
2179 if (selected == LACP_SELECTED &&
2180 lacpp->lp_pending == 0) {
2181 next_state = LACP_MUX_ATTACHED;
2182 } else if (selected == LACP_UNSELECTED) {
2183 next_state = LACP_MUX_DETACHED;
2184 }
2185 break;
2186 case LACP_MUX_ATTACHED:
2187 if (selected != LACP_SELECTED) {
2188 next_state = LACP_MUX_DETACHED;
2189 } else if (lacp_isactive(lsc, lacpp) && p_sync) {
2190 next_state = LACP_MUX_COLLECTING;
2191 }
2192 break;
2193 case LACP_MUX_COLLECTING:
2194 if (selected != LACP_SELECTED ||
2195 !lacp_isactive(lsc, lacpp)
2196 || !p_sync) {
2197 next_state = LACP_MUX_ATTACHED;
2198 } else if (p_collecting) {
2199 next_state = LACP_MUX_DISTRIBUTING;
2200 }
2201 break;
2202 case LACP_MUX_DISTRIBUTING:
2203 if (selected != LACP_SELECTED ||
2204 !lacp_isactive(lsc, lacpp)
2205 || !p_sync || !p_collecting) {
2206 next_state = LACP_MUX_COLLECTING;
2207 LACP_DPRINTF((lsc, lacpp,
2208 "Interface stopped DISTRIBUTING,"
2209 " possible flapping\n"));
2210 }
2211 break;
2212 case LACP_MUX_INIT:
2213 default:
2214 panic("%s: unknown state",
2215 lsc->lsc_softc->sc_if.if_xname);
2216 }
2217 } while (lacp_set_mux(lsc, lacpp, next_state) == 0);
2218 }
2219
2220 static bool
2221 lacp_aggregator_is_match(struct lacp_aggregator_systemid *a,
2222 struct lacp_aggregator_systemid *b)
2223 {
2224
2225 if (a->sid_prio != b->sid_prio)
2226 return false;
2227
2228 if (a->sid_key != b->sid_key)
2229 return false;
2230
2231 if (memcmp(a->sid_mac, b->sid_mac, sizeof(a->sid_mac)) != 0)
2232 return false;
2233
2234 return true;
2235 }
2236
2237 static void
2238 lacp_selected_update(struct lacp_softc *lsc, struct lacp_aggregator *la)
2239 {
2240 struct lacp_port *lacpp;
2241 size_t nselected;
2242 uint32_t media;
2243
2244 KASSERT(LACP_LOCKED(lsc));
2245
2246 lacpp = LIST_FIRST(&la->la_ports);
2247 if (lacpp == NULL)
2248 return;
2249
2250 media = lacpp->lp_media;
2251 nselected = 0;
2252 LIST_FOREACH(lacpp, &la->la_ports, lp_entry_la) {
2253 if (nselected >= lsc->lsc_max_ports ||
2254 (!lsc->lsc_multi_linkspeed && media != lacpp->lp_media)) {
2255 if (lacpp->lp_selected == LACP_SELECTED)
2256 lacpp->lp_selected = LACP_STANDBY;
2257 continue;
2258 }
2259
2260 switch (lacpp->lp_selected) {
2261 case LACP_STANDBY:
2262 lacpp->lp_selected = LACP_SELECTED;
2263 /* fall through */
2264 case LACP_SELECTED:
2265 nselected++;
2266 break;
2267 default:
2268 /* do nothing */
2269 break;
2270 }
2271 }
2272 }
2273
2274 static void
2275 lacp_select(struct lacp_softc *lsc, struct lacp_port *lacpp)
2276 {
2277 struct lacp_aggregator *la;
2278 struct lacp_aggregator_systemid *sid;
2279 struct lacp_port *lacpp0;
2280 char buf[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
2281 bool insert_after;
2282
2283 if (lacpp->lp_aggregator != NULL)
2284 return;
2285
2286 /* If we haven't heard from our peer, skip this step. */
2287 if (ISSET(lacpp->lp_actor.lpi_state, LACP_STATE_DEFAULTED))
2288 return
2289
2290 KASSERT(!LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE));
2291
2292 sid = &lacpp->lp_aggregator_sidbuf;
2293
2294 TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
2295 if (lacp_aggregator_is_match(&la->la_sid, sid))
2296 break;
2297 }
2298
2299 if (la == NULL) {
2300 la = kmem_zalloc(sizeof(*la), KM_NOSLEEP);
2301 if (la == NULL) {
2302 LACP_DPRINTF((lsc, lacpp,
2303 "couldn't allocate aggregator\n"));
2304 /* will retry the next tick. */
2305 return;
2306 }
2307 LIST_INIT(&la->la_ports);
2308
2309 la->la_sid = *sid;
2310 TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
2311 LACP_DPRINTF((lsc, lacpp, "a new aggregator created\n"));
2312 } else {
2313 LACP_DPRINTF((lsc, lacpp, "aggregator found\n"));
2314 }
2315
2316 KASSERT(la != NULL);
2317 LACP_AGGREGATOR_STR(la, buf, sizeof(buf));
2318 LACP_DPRINTF((lsc, lacpp, "aggregator lagid=%s\n", buf));
2319
2320 lacpp->lp_aggregator = la;
2321 lacpp->lp_selected = LACP_STANDBY;
2322
2323 insert_after = false;
2324
2325 LIST_FOREACH(lacpp0, &la->la_ports, lp_entry_la) {
2326 if (lacp_port_priority_max(lacpp0, lacpp) == lacpp)
2327 break;
2328
2329 if (LIST_NEXT(lacpp0, lp_entry_la) == NULL) {
2330 insert_after = true;
2331 break;
2332 }
2333 }
2334
2335 if (lacpp0 == NULL) {
2336 LIST_INSERT_HEAD(&la->la_ports, lacpp, lp_entry_la);
2337 } else if (insert_after) {
2338 LIST_INSERT_AFTER(lacpp0, lacpp, lp_entry_la);
2339 } else {
2340 LIST_INSERT_BEFORE(lacpp0, lacpp, lp_entry_la);
2341 }
2342
2343 lacp_selected_update(lsc, la);
2344 }
2345
2346 static void
2347 lacp_unselect(struct lacp_softc *lsc, struct lacp_port *lacpp)
2348 {
2349 struct lacp_aggregator *la;
2350 char buf[LACP_SYSTEMIDSTR_LEN] __LACPDEBUGUSED;
2351 bool remove_actaggr;
2352
2353 KASSERT(LACP_LOCKED(lsc));
2354 KASSERT(!LACP_TIMER_ISARMED(lacpp, LACP_TIMER_WAIT_WHILE));
2355
2356 la = lacpp->lp_aggregator;
2357 lacpp->lp_selected = LACP_UNSELECTED;
2358
2359 if (la == NULL)
2360 return;
2361
2362 KASSERT(!LIST_EMPTY(&la->la_ports));
2363
2364 LACP_AGGREGATOR_STR(la, buf, sizeof(buf));
2365 LACP_DPRINTF((lsc, lacpp, "unselect aggregator lagid=%s\n", buf));
2366
2367 LIST_REMOVE(lacpp, lp_entry_la);
2368 lacpp->lp_aggregator = NULL;
2369
2370 if (LIST_EMPTY(&la->la_ports)) {
2371 remove_actaggr = false;
2372
2373 if (la == lsc->lsc_aggregator) {
2374 LACP_DPRINTF((lsc, NULL, "remove active aggregator\n"));
2375 lsc->lsc_aggregator = NULL;
2376 remove_actaggr = true;
2377 }
2378
2379 TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
2380 kmem_free(la, sizeof(*la));
2381
2382 if (remove_actaggr) {
2383 lacp_select_active_aggregator(lsc);
2384 }
2385 } else {
2386 lacp_selected_update(lsc, la);
2387 }
2388 }
2389
2390 static void
2391 lacp_suppress_distributing(struct lacp_softc *lsc)
2392 {
2393 struct lacp_aggregator *la;
2394 struct lacp_port *lacpp;
2395
2396 KASSERT(LACP_LOCKED(lsc));
2397
2398 la = lsc->lsc_aggregator;
2399
2400 LIST_FOREACH(lacpp, &la->la_ports, lp_entry_la) {
2401 if (ISSET(lacpp->lp_actor.lpi_state,
2402 LACP_STATE_DISTRIBUTING)) {
2403 lagg_workq_add(lsc->lsc_workq,
2404 &lacpp->lp_work_marker);
2405 }
2406 }
2407
2408 LACP_PTIMER_ARM(lsc, LACP_PTIMER_DISTRIBUTING,
2409 LACP_TRANSIT_DELAY);
2410 }
2411
2412 static void
2413 lacp_distributing_timer(struct lacp_softc *lsc)
2414 {
2415
2416 KASSERT(LACP_LOCKED(lsc));
2417
2418 if (lsc->lsc_suppress_distributing) {
2419 LACP_DPRINTF((lsc, NULL,
2420 "disable suppress distributing\n"));
2421 lsc->lsc_suppress_distributing = false;
2422 }
2423 }
2424
2425 static struct mbuf *
2426 lacp_markerdu_mbuf(struct lacp_softc *lsc, struct lacp_port *lacpp)
2427 {
2428 struct ifnet *ifp_port;
2429 struct mbuf *m;
2430 struct markerdu *mdu;
2431 struct markerdu_info *mi;
2432
2433 KASSERT(LACP_LOCKED(lsc));
2434
2435 ifp_port = lacpp->lp_laggport->lp_ifp;
2436
2437 MGETHDR(m, M_DONTWAIT, MT_DATA);
2438 if (m == NULL) {
2439 lsc->lsc_mgethdr_failed.ev_count++;
2440 return NULL;
2441 }
2442
2443 m->m_pkthdr.len = m->m_len = sizeof(*mdu);
2444
2445 mdu = mtod(m, struct markerdu *);
2446
2447 memset(mdu, 0, sizeof(*mdu));
2448
2449 m->m_flags |= M_MCAST;
2450 memcpy(mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
2451 ETHER_ADDR_LEN);
2452 memcpy(mdu->mdu_eh.ether_shost, CLLADDR(ifp_port->if_sadl),
2453 ETHER_ADDR_LEN);
2454 mdu->mdu_eh.ether_type = ntohs(ETHERTYPE_SLOWPROTOCOLS);
2455 mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER;
2456 mdu->mdu_sph.sph_version = 1;
2457
2458 mi = &mdu->mdu_info;
2459 tlv_set(&mdu->mdu_tlv_info, MARKER_TYPE_INFO,
2460 sizeof(*mi));
2461 mi->mi_rq_port = lacpp->lp_actor.lpi_portno;
2462 mi->mi_rq_xid = htonl(lacpp->lp_marker_xid);
2463 memcpy(mi->mi_rq_system, lsc->lsc_system_mac, LACP_MAC_LEN);
2464
2465 mdu->mdu_tlv_term.tlv_type = MARKER_TYPE_TERMINATE;
2466 mdu->mdu_tlv_term.tlv_length = 0;
2467
2468 return m;
2469 }
2470
2471 static void
2472 lacp_marker_work(struct lagg_work *lw, void *xlsc)
2473 {
2474 struct lacp_softc *lsc;
2475 struct lacp_port *lacpp;
2476 struct lagg_port *lp;
2477 struct markerdu *mdu;
2478 struct mbuf *m;
2479 struct psref psref;
2480 int bound;
2481
2482 lsc = xlsc;
2483 lacpp = container_of(lw, struct lacp_port, lp_work_marker);
2484
2485 LACP_LOCK(lsc);
2486 lacpp->lp_marker_xid++;
2487 m = lacp_markerdu_mbuf(lsc, lacpp);
2488 if (m == NULL) {
2489 LACP_UNLOCK(lsc);
2490 return;
2491 }
2492 SET(lacpp->lp_flags, LACP_PORT_MARK);
2493 lsc->lsc_suppress_distributing = true;
2494 lp = lacpp->lp_laggport;
2495 bound = curlwp_bind();
2496 lagg_port_getref(lp, &psref);
2497 LACP_UNLOCK(lsc);
2498
2499 if (LACP_ISDUMPING(lsc)) {
2500 lacp_dprintf(lsc, lacpp, "markerdu transmit\n");
2501 mdu = mtod(m, struct markerdu *);
2502 lacp_dump_markertlv(&mdu->mdu_info, NULL);
2503 }
2504
2505 lagg_port_xmit(lp, m);
2506 lagg_port_putref(lp, &psref);
2507 curlwp_bindx(bound);
2508 }
2509
2510 static void
2511 lacp_dump_lacpdutlv(const struct lacpdu_peerinfo *pi_actor,
2512 const struct lacpdu_peerinfo *pi_partner,
2513 const struct lacpdu_collectorinfo *lci)
2514 {
2515 char str[LACP_STATESTR_LEN];
2516
2517 if (pi_actor != NULL) {
2518 lacp_peerinfo_idstr(pi_actor, str, sizeof(str));
2519 printf("actor=%s\n", str);
2520 lacp_state_str(pi_actor->lpi_state,
2521 str, sizeof(str));
2522 printf("actor.state=%s portno=%d portprio=0x%04x\n",
2523 str,
2524 ntohs(pi_actor->lpi_port_no),
2525 ntohs(pi_actor->lpi_port_prio));;
2526 } else {
2527 printf("no actor info\n");
2528 }
2529
2530 if (pi_partner != NULL) {
2531 lacp_peerinfo_idstr(pi_partner, str, sizeof(str));
2532 printf("partner=%s\n", str);
2533 lacp_state_str(pi_partner->lpi_state,
2534 str, sizeof(str));
2535 printf("partner.state=%s portno=%d portprio=0x%04x\n",
2536 str,
2537 ntohs(pi_partner->lpi_port_no),
2538 ntohs(pi_partner->lpi_port_prio));
2539 } else {
2540 printf("no partner info\n");
2541 }
2542
2543 if (lci != NULL) {
2544 printf("maxdelay=%d\n", ntohs(lci->lci_maxdelay));
2545 } else {
2546 printf("no collector info\n");
2547 }
2548 }
2549
2550 static void
2551 lacp_dump_markertlv(const struct markerdu_info *mi_info,
2552 const struct markerdu_info *mi_res)
2553 {
2554
2555 if (mi_info != NULL) {
2556 printf("marker info: port=%d, sys=%s, id=%u\n",
2557 ntohs(mi_info->mi_rq_port),
2558 ether_sprintf(mi_info->mi_rq_system),
2559 ntohl(mi_info->mi_rq_xid));
2560 }
2561
2562 if (mi_res != NULL) {
2563 printf("marker resp: port=%d, sys=%s, id=%u\n",
2564 ntohs(mi_res->mi_rq_port),
2565 ether_sprintf(mi_res->mi_rq_system),
2566 ntohl(mi_res->mi_rq_xid));
2567
2568 }
2569 }
2570
2571 static uint32_t
2572 lacp_ifmedia2lacpmedia(u_int ifmedia)
2573 {
2574 uint32_t rv;
2575
2576 switch (IFM_SUBTYPE(ifmedia)) {
2577 case IFM_10_T:
2578 case IFM_10_2:
2579 case IFM_10_5:
2580 case IFM_10_STP:
2581 case IFM_10_FL:
2582 rv = LACP_LINKSPEED_10;
2583 break;
2584 case IFM_100_TX:
2585 case IFM_100_FX:
2586 case IFM_100_T4:
2587 case IFM_100_VG:
2588 case IFM_100_T2:
2589 rv = LACP_LINKSPEED_100;
2590 break;
2591 case IFM_1000_SX:
2592 case IFM_1000_LX:
2593 case IFM_1000_CX:
2594 case IFM_1000_T:
2595 case IFM_1000_BX10:
2596 case IFM_1000_KX:
2597 rv = LACP_LINKSPEED_1000;
2598 break;
2599 case IFM_2500_SX:
2600 case IFM_2500_KX:
2601 rv = LACP_LINKSPEED_2500;
2602 break;
2603 case IFM_5000_T:
2604 rv = LACP_LINKSPEED_5000;
2605 break;
2606 case IFM_10G_LR:
2607 case IFM_10G_SR:
2608 case IFM_10G_CX4:
2609 case IFM_10G_TWINAX:
2610 case IFM_10G_TWINAX_LONG:
2611 case IFM_10G_LRM:
2612 case IFM_10G_T:
2613 rv = LACP_LINKSPEED_10G;
2614 break;
2615 default:
2616 rv = LACP_LINKSPEED_UNKNOWN;
2617 }
2618
2619 if (IFM_TYPE(ifmedia) == IFM_ETHER)
2620 SET(rv, LACP_MEDIA_ETHER);
2621 if ((ifmedia & IFM_FDX) != 0)
2622 SET(rv, LACP_MEDIA_FDX);
2623
2624 return rv;
2625 }
2626