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