if_laggproto.c revision 1.8 1 1.8 yamaguch /* $NetBSD: if_laggproto.c,v 1.8 2023/11/28 05:28:37 yamaguchi Exp $ */
2 1.1 yamaguch
3 1.1 yamaguch /*-
4 1.1 yamaguch * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5 1.1 yamaguch *
6 1.1 yamaguch * Copyright (c)2021 Internet Initiative Japan, Inc.
7 1.1 yamaguch * All rights reserved.
8 1.1 yamaguch *
9 1.1 yamaguch * Redistribution and use in source and binary forms, with or without
10 1.1 yamaguch * modification, are permitted provided that the following conditions
11 1.1 yamaguch * are met:
12 1.1 yamaguch * 1. Redistributions of source code must retain the above copyright
13 1.1 yamaguch * notice, this list of conditions and the following disclaimer.
14 1.1 yamaguch * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 yamaguch * notice, this list of conditions and the following disclaimer in the
16 1.1 yamaguch * documentation and/or other materials provided with the distribution.
17 1.1 yamaguch *
18 1.1 yamaguch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 1.1 yamaguch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 1.1 yamaguch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.1 yamaguch * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.1 yamaguch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 yamaguch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 1.1 yamaguch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 yamaguch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 yamaguch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 yamaguch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 yamaguch * SUCH DAMAGE.
29 1.1 yamaguch */
30 1.1 yamaguch
31 1.1 yamaguch #include <sys/cdefs.h>
32 1.8 yamaguch __KERNEL_RCSID(0, "$NetBSD: if_laggproto.c,v 1.8 2023/11/28 05:28:37 yamaguchi Exp $");
33 1.1 yamaguch
34 1.1 yamaguch #include <sys/param.h>
35 1.1 yamaguch #include <sys/types.h>
36 1.1 yamaguch
37 1.1 yamaguch #include <sys/evcnt.h>
38 1.1 yamaguch #include <sys/kmem.h>
39 1.1 yamaguch #include <sys/mbuf.h>
40 1.1 yamaguch #include <sys/mutex.h>
41 1.1 yamaguch #include <sys/pslist.h>
42 1.1 yamaguch #include <sys/syslog.h>
43 1.1 yamaguch #include <sys/workqueue.h>
44 1.1 yamaguch
45 1.1 yamaguch #include <net/if.h>
46 1.1 yamaguch #include <net/if_ether.h>
47 1.1 yamaguch #include <net/if_media.h>
48 1.1 yamaguch
49 1.1 yamaguch #include <net/lagg/if_lagg.h>
50 1.1 yamaguch #include <net/lagg/if_laggproto.h>
51 1.1 yamaguch
52 1.1 yamaguch struct lagg_proto_softc {
53 1.1 yamaguch struct lagg_softc *psc_softc;
54 1.1 yamaguch struct pslist_head psc_ports;
55 1.1 yamaguch kmutex_t psc_lock;
56 1.1 yamaguch pserialize_t psc_psz;
57 1.1 yamaguch size_t psc_ctxsiz;
58 1.1 yamaguch void *psc_ctx;
59 1.1 yamaguch size_t psc_nactports;
60 1.7 yamaguch struct workqueue *psc_workq;
61 1.7 yamaguch struct lagg_work psc_work_linkspeed;
62 1.1 yamaguch };
63 1.1 yamaguch
64 1.1 yamaguch /*
65 1.1 yamaguch * Locking notes:
66 1.1 yamaguch * - Items of struct lagg_proto_softc is protected by
67 1.1 yamaguch * psc_lock (an adaptive mutex)
68 1.1 yamaguch * - psc_ports is protected by pserialize (psc_psz)
69 1.1 yamaguch * - Updates of psc_ports is serialized by sc_lock in
70 1.1 yamaguch * struct lagg_softc
71 1.1 yamaguch * - Other locking notes are described in if_laggproto.h
72 1.1 yamaguch */
73 1.1 yamaguch
74 1.1 yamaguch struct lagg_failover {
75 1.1 yamaguch bool fo_rx_all;
76 1.1 yamaguch };
77 1.1 yamaguch
78 1.1 yamaguch struct lagg_portmap {
79 1.1 yamaguch struct lagg_port *pm_ports[LAGG_MAX_PORTS];
80 1.1 yamaguch size_t pm_nports;
81 1.1 yamaguch };
82 1.1 yamaguch
83 1.1 yamaguch struct lagg_portmaps {
84 1.1 yamaguch struct lagg_portmap maps_pmap[2];
85 1.1 yamaguch size_t maps_activepmap;
86 1.1 yamaguch };
87 1.1 yamaguch
88 1.1 yamaguch struct lagg_lb {
89 1.1 yamaguch struct lagg_portmaps lb_pmaps;
90 1.1 yamaguch };
91 1.1 yamaguch
92 1.1 yamaguch struct lagg_proto_port {
93 1.1 yamaguch struct pslist_entry lpp_entry;
94 1.1 yamaguch struct lagg_port *lpp_laggport;
95 1.7 yamaguch uint64_t lpp_linkspeed;
96 1.1 yamaguch bool lpp_active;
97 1.7 yamaguch bool lpp_running;
98 1.1 yamaguch };
99 1.1 yamaguch
100 1.1 yamaguch #define LAGG_PROTO_LOCK(_psc) mutex_enter(&(_psc)->psc_lock)
101 1.1 yamaguch #define LAGG_PROTO_UNLOCK(_psc) mutex_exit(&(_psc)->psc_lock)
102 1.1 yamaguch #define LAGG_PROTO_LOCKED(_psc) mutex_owned(&(_psc)->psc_lock)
103 1.1 yamaguch
104 1.1 yamaguch static struct lagg_proto_softc *
105 1.1 yamaguch lagg_proto_alloc(lagg_proto, struct lagg_softc *);
106 1.1 yamaguch static void lagg_proto_free(struct lagg_proto_softc *);
107 1.1 yamaguch static void lagg_proto_insert_port(struct lagg_proto_softc *,
108 1.1 yamaguch struct lagg_proto_port *);
109 1.1 yamaguch static void lagg_proto_remove_port(struct lagg_proto_softc *,
110 1.1 yamaguch struct lagg_proto_port *);
111 1.1 yamaguch static struct lagg_port *
112 1.1 yamaguch lagg_link_active(struct lagg_proto_softc *psc,
113 1.1 yamaguch struct lagg_proto_port *, struct psref *);
114 1.7 yamaguch static void lagg_fail_linkspeed_work(struct lagg_work *, void *);
115 1.7 yamaguch static void lagg_lb_linkspeed_work(struct lagg_work*,
116 1.7 yamaguch void *);
117 1.8 yamaguch static void lagg_common_linkstate(struct lagg_proto_softc *,
118 1.8 yamaguch struct lagg_port *);
119 1.1 yamaguch
120 1.1 yamaguch static inline struct lagg_portmap *
121 1.1 yamaguch lagg_portmap_active(struct lagg_portmaps *maps)
122 1.1 yamaguch {
123 1.1 yamaguch size_t i;
124 1.1 yamaguch
125 1.1 yamaguch i = atomic_load_consume(&maps->maps_activepmap);
126 1.1 yamaguch
127 1.1 yamaguch return &maps->maps_pmap[i];
128 1.1 yamaguch }
129 1.1 yamaguch
130 1.1 yamaguch static inline struct lagg_portmap *
131 1.1 yamaguch lagg_portmap_next(struct lagg_portmaps *maps)
132 1.1 yamaguch {
133 1.1 yamaguch size_t i;
134 1.1 yamaguch
135 1.1 yamaguch i = atomic_load_consume(&maps->maps_activepmap);
136 1.1 yamaguch i &= 0x1;
137 1.1 yamaguch i ^= 0x1;
138 1.1 yamaguch
139 1.1 yamaguch return &maps->maps_pmap[i];
140 1.1 yamaguch }
141 1.1 yamaguch
142 1.1 yamaguch static inline void
143 1.1 yamaguch lagg_portmap_switch(struct lagg_portmaps *maps)
144 1.1 yamaguch {
145 1.1 yamaguch size_t i;
146 1.1 yamaguch
147 1.1 yamaguch i = atomic_load_consume(&maps->maps_activepmap);
148 1.1 yamaguch i &= 0x1;
149 1.1 yamaguch i ^= 0x1;
150 1.1 yamaguch
151 1.1 yamaguch atomic_store_release(&maps->maps_activepmap, i);
152 1.1 yamaguch }
153 1.1 yamaguch
154 1.1 yamaguch static struct lagg_proto_softc *
155 1.1 yamaguch lagg_proto_alloc(lagg_proto pr, struct lagg_softc *sc)
156 1.1 yamaguch {
157 1.1 yamaguch struct lagg_proto_softc *psc;
158 1.7 yamaguch char xnamebuf[MAXCOMLEN];
159 1.1 yamaguch size_t ctxsiz;
160 1.1 yamaguch
161 1.1 yamaguch switch (pr) {
162 1.1 yamaguch case LAGG_PROTO_FAILOVER:
163 1.1 yamaguch ctxsiz = sizeof(struct lagg_failover);
164 1.1 yamaguch break;
165 1.1 yamaguch case LAGG_PROTO_LOADBALANCE:
166 1.1 yamaguch ctxsiz = sizeof(struct lagg_lb);
167 1.1 yamaguch break;
168 1.1 yamaguch default:
169 1.1 yamaguch ctxsiz = 0;
170 1.1 yamaguch }
171 1.1 yamaguch
172 1.1 yamaguch psc = kmem_zalloc(sizeof(*psc), KM_NOSLEEP);
173 1.1 yamaguch if (psc == NULL)
174 1.1 yamaguch return NULL;
175 1.1 yamaguch
176 1.7 yamaguch psc->psc_workq = lagg_workq_create(xnamebuf,
177 1.7 yamaguch PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
178 1.7 yamaguch if (psc->psc_workq == NULL) {
179 1.7 yamaguch LAGG_LOG(sc, LOG_ERR, "workqueue create failed\n");
180 1.7 yamaguch kmem_free(psc, sizeof(*psc));
181 1.7 yamaguch return NULL;
182 1.7 yamaguch }
183 1.7 yamaguch
184 1.1 yamaguch if (ctxsiz > 0) {
185 1.1 yamaguch psc->psc_ctx = kmem_zalloc(ctxsiz, KM_NOSLEEP);
186 1.1 yamaguch if (psc->psc_ctx == NULL) {
187 1.7 yamaguch lagg_workq_destroy(psc->psc_workq);
188 1.1 yamaguch kmem_free(psc, sizeof(*psc));
189 1.1 yamaguch return NULL;
190 1.1 yamaguch }
191 1.1 yamaguch
192 1.1 yamaguch psc->psc_ctxsiz = ctxsiz;
193 1.1 yamaguch }
194 1.1 yamaguch
195 1.1 yamaguch PSLIST_INIT(&psc->psc_ports);
196 1.1 yamaguch psc->psc_psz = pserialize_create();
197 1.1 yamaguch mutex_init(&psc->psc_lock, MUTEX_DEFAULT, IPL_SOFTNET);
198 1.1 yamaguch psc->psc_softc = sc;
199 1.1 yamaguch
200 1.1 yamaguch return psc;
201 1.1 yamaguch }
202 1.1 yamaguch
203 1.1 yamaguch static void
204 1.1 yamaguch lagg_proto_free(struct lagg_proto_softc *psc)
205 1.1 yamaguch {
206 1.1 yamaguch
207 1.7 yamaguch lagg_workq_wait(psc->psc_workq, &psc->psc_work_linkspeed);
208 1.1 yamaguch pserialize_destroy(psc->psc_psz);
209 1.1 yamaguch mutex_destroy(&psc->psc_lock);
210 1.7 yamaguch lagg_workq_destroy(psc->psc_workq);
211 1.1 yamaguch
212 1.1 yamaguch if (psc->psc_ctxsiz > 0)
213 1.1 yamaguch kmem_free(psc->psc_ctx, psc->psc_ctxsiz);
214 1.1 yamaguch
215 1.1 yamaguch kmem_free(psc, sizeof(*psc));
216 1.1 yamaguch }
217 1.1 yamaguch
218 1.1 yamaguch static struct lagg_port *
219 1.1 yamaguch lagg_link_active(struct lagg_proto_softc *psc,
220 1.1 yamaguch struct lagg_proto_port *pport, struct psref *psref)
221 1.1 yamaguch {
222 1.1 yamaguch struct lagg_port *lp;
223 1.1 yamaguch int s;
224 1.1 yamaguch
225 1.1 yamaguch lp = NULL;
226 1.1 yamaguch s = pserialize_read_enter();
227 1.1 yamaguch
228 1.1 yamaguch for (;pport != NULL;
229 1.1 yamaguch pport = PSLIST_READER_NEXT(pport,
230 1.1 yamaguch struct lagg_proto_port, lpp_entry)) {
231 1.1 yamaguch if (atomic_load_relaxed(&pport->lpp_active)) {
232 1.1 yamaguch lp = pport->lpp_laggport;
233 1.1 yamaguch goto done;
234 1.1 yamaguch }
235 1.1 yamaguch }
236 1.1 yamaguch
237 1.1 yamaguch PSLIST_READER_FOREACH(pport, &psc->psc_ports,
238 1.1 yamaguch struct lagg_proto_port, lpp_entry) {
239 1.1 yamaguch if (atomic_load_relaxed(&pport->lpp_active)) {
240 1.1 yamaguch lp = pport->lpp_laggport;
241 1.1 yamaguch break;
242 1.1 yamaguch }
243 1.1 yamaguch }
244 1.1 yamaguch done:
245 1.1 yamaguch if (lp != NULL)
246 1.1 yamaguch lagg_port_getref(lp, psref);
247 1.1 yamaguch pserialize_read_exit(s);
248 1.1 yamaguch
249 1.1 yamaguch return lp;
250 1.1 yamaguch }
251 1.1 yamaguch
252 1.1 yamaguch int
253 1.1 yamaguch lagg_common_allocport(struct lagg_proto_softc *psc, struct lagg_port *lp)
254 1.1 yamaguch {
255 1.1 yamaguch struct lagg_proto_port *pport;
256 1.1 yamaguch
257 1.1 yamaguch KASSERT(LAGG_LOCKED(psc->psc_softc));
258 1.1 yamaguch
259 1.1 yamaguch pport = kmem_zalloc(sizeof(*pport), KM_NOSLEEP);
260 1.1 yamaguch if (pport == NULL)
261 1.1 yamaguch return ENOMEM;
262 1.1 yamaguch
263 1.1 yamaguch PSLIST_ENTRY_INIT(pport, lpp_entry);
264 1.1 yamaguch pport->lpp_laggport = lp;
265 1.1 yamaguch lp->lp_proto_ctx = (void *)pport;
266 1.1 yamaguch return 0;
267 1.1 yamaguch }
268 1.1 yamaguch
269 1.1 yamaguch void
270 1.1 yamaguch lagg_common_freeport(struct lagg_proto_softc *psc, struct lagg_port *lp)
271 1.1 yamaguch {
272 1.1 yamaguch struct lagg_proto_port *pport;
273 1.1 yamaguch
274 1.1 yamaguch pport = lp->lp_proto_ctx;
275 1.7 yamaguch KASSERT(!pport->lpp_running);
276 1.1 yamaguch lp->lp_proto_ctx = NULL;
277 1.1 yamaguch
278 1.1 yamaguch kmem_free(pport, sizeof(*pport));
279 1.1 yamaguch }
280 1.1 yamaguch
281 1.1 yamaguch static void
282 1.1 yamaguch lagg_proto_insert_port(struct lagg_proto_softc *psc,
283 1.1 yamaguch struct lagg_proto_port *pport)
284 1.1 yamaguch {
285 1.1 yamaguch struct lagg_proto_port *pport0;
286 1.1 yamaguch struct lagg_port *lp, *lp0;
287 1.1 yamaguch bool insert_after;
288 1.1 yamaguch
289 1.1 yamaguch insert_after = false;
290 1.1 yamaguch lp = pport->lpp_laggport;
291 1.1 yamaguch
292 1.1 yamaguch LAGG_PROTO_LOCK(psc);
293 1.1 yamaguch PSLIST_WRITER_FOREACH(pport0, &psc->psc_ports,
294 1.1 yamaguch struct lagg_proto_port, lpp_entry) {
295 1.1 yamaguch lp0 = pport0->lpp_laggport;
296 1.1 yamaguch if (lp0->lp_prio > lp->lp_prio)
297 1.1 yamaguch break;
298 1.1 yamaguch
299 1.1 yamaguch if (PSLIST_WRITER_NEXT(pport0,
300 1.1 yamaguch struct lagg_proto_port, lpp_entry) == NULL) {
301 1.1 yamaguch insert_after = true;
302 1.1 yamaguch break;
303 1.1 yamaguch }
304 1.1 yamaguch }
305 1.1 yamaguch
306 1.1 yamaguch if (pport0 == NULL) {
307 1.1 yamaguch PSLIST_WRITER_INSERT_HEAD(&psc->psc_ports, pport,
308 1.1 yamaguch lpp_entry);
309 1.1 yamaguch } else if (insert_after) {
310 1.1 yamaguch PSLIST_WRITER_INSERT_AFTER(pport0, pport, lpp_entry);
311 1.1 yamaguch } else {
312 1.1 yamaguch PSLIST_WRITER_INSERT_BEFORE(pport0, pport, lpp_entry);
313 1.1 yamaguch }
314 1.1 yamaguch LAGG_PROTO_UNLOCK(psc);
315 1.1 yamaguch }
316 1.1 yamaguch
317 1.1 yamaguch static void
318 1.1 yamaguch lagg_proto_remove_port(struct lagg_proto_softc *psc,
319 1.1 yamaguch struct lagg_proto_port *pport)
320 1.1 yamaguch {
321 1.1 yamaguch
322 1.1 yamaguch LAGG_PROTO_LOCK(psc);
323 1.1 yamaguch PSLIST_WRITER_REMOVE(pport, lpp_entry);
324 1.1 yamaguch pserialize_perform(psc->psc_psz);
325 1.1 yamaguch LAGG_PROTO_UNLOCK(psc);
326 1.1 yamaguch }
327 1.1 yamaguch
328 1.1 yamaguch void
329 1.1 yamaguch lagg_common_startport(struct lagg_proto_softc *psc, struct lagg_port *lp)
330 1.1 yamaguch {
331 1.1 yamaguch struct lagg_proto_port *pport;
332 1.1 yamaguch
333 1.1 yamaguch pport = lp->lp_proto_ctx;
334 1.1 yamaguch lagg_proto_insert_port(psc, pport);
335 1.1 yamaguch
336 1.7 yamaguch LAGG_PROTO_LOCK(psc);
337 1.7 yamaguch pport->lpp_running = true;
338 1.7 yamaguch LAGG_PROTO_UNLOCK(psc);
339 1.7 yamaguch
340 1.1 yamaguch lagg_common_linkstate(psc, lp);
341 1.1 yamaguch }
342 1.1 yamaguch
343 1.1 yamaguch void
344 1.1 yamaguch lagg_common_stopport(struct lagg_proto_softc *psc, struct lagg_port *lp)
345 1.1 yamaguch {
346 1.1 yamaguch struct lagg_proto_port *pport;
347 1.1 yamaguch struct ifnet *ifp;
348 1.1 yamaguch
349 1.1 yamaguch pport = lp->lp_proto_ctx;
350 1.7 yamaguch
351 1.7 yamaguch LAGG_PROTO_LOCK(psc);
352 1.7 yamaguch pport->lpp_running = false;
353 1.7 yamaguch LAGG_PROTO_UNLOCK(psc);
354 1.7 yamaguch
355 1.1 yamaguch lagg_proto_remove_port(psc, pport);
356 1.1 yamaguch
357 1.1 yamaguch if (pport->lpp_active) {
358 1.3 yamaguch KASSERT(psc->psc_nactports > 0);
359 1.3 yamaguch psc->psc_nactports--;
360 1.1 yamaguch
361 1.1 yamaguch if (psc->psc_nactports == 0) {
362 1.1 yamaguch ifp = &psc->psc_softc->sc_if;
363 1.1 yamaguch if_link_state_change(ifp, LINK_STATE_DOWN);
364 1.1 yamaguch }
365 1.1 yamaguch
366 1.1 yamaguch pport->lpp_active = false;
367 1.1 yamaguch }
368 1.1 yamaguch }
369 1.8 yamaguch static void
370 1.8 yamaguch lagg_common_linkstate(struct lagg_proto_softc *psc, struct lagg_port *lp)
371 1.8 yamaguch {
372 1.8 yamaguch
373 1.8 yamaguch IFNET_ASSERT_UNLOCKED(lp->lp_ifp);
374 1.8 yamaguch
375 1.8 yamaguch IFNET_LOCK(lp->lp_ifp);
376 1.8 yamaguch lagg_common_linkstate_ifnet_locked(psc, lp);
377 1.8 yamaguch IFNET_UNLOCK(lp->lp_ifp);
378 1.8 yamaguch }
379 1.1 yamaguch
380 1.1 yamaguch void
381 1.8 yamaguch lagg_common_linkstate_ifnet_locked(struct lagg_proto_softc *psc, struct lagg_port *lp)
382 1.1 yamaguch {
383 1.1 yamaguch struct lagg_proto_port *pport;
384 1.7 yamaguch struct ifnet *ifp, *ifp_port;
385 1.7 yamaguch struct ifmediareq ifmr;
386 1.7 yamaguch uint64_t linkspeed;
387 1.1 yamaguch bool is_active;
388 1.7 yamaguch int error;
389 1.1 yamaguch
390 1.1 yamaguch pport = lp->lp_proto_ctx;
391 1.1 yamaguch is_active = lagg_portactive(lp);
392 1.7 yamaguch ifp_port = lp->lp_ifp;
393 1.1 yamaguch
394 1.8 yamaguch KASSERT(IFNET_LOCKED(ifp_port));
395 1.8 yamaguch
396 1.7 yamaguch LAGG_PROTO_LOCK(psc);
397 1.7 yamaguch if (!pport->lpp_running ||
398 1.7 yamaguch pport->lpp_active == is_active) {
399 1.7 yamaguch LAGG_PROTO_UNLOCK(psc);
400 1.1 yamaguch return;
401 1.7 yamaguch }
402 1.1 yamaguch
403 1.1 yamaguch ifp = &psc->psc_softc->sc_if;
404 1.7 yamaguch pport->lpp_active = is_active;
405 1.7 yamaguch
406 1.1 yamaguch if (is_active) {
407 1.1 yamaguch psc->psc_nactports++;
408 1.1 yamaguch if (psc->psc_nactports == 1)
409 1.1 yamaguch if_link_state_change(ifp, LINK_STATE_UP);
410 1.1 yamaguch } else {
411 1.3 yamaguch KASSERT(psc->psc_nactports > 0);
412 1.3 yamaguch psc->psc_nactports--;
413 1.1 yamaguch
414 1.1 yamaguch if (psc->psc_nactports == 0)
415 1.1 yamaguch if_link_state_change(ifp, LINK_STATE_DOWN);
416 1.1 yamaguch }
417 1.7 yamaguch LAGG_PROTO_UNLOCK(psc);
418 1.7 yamaguch
419 1.7 yamaguch memset(&ifmr, 0, sizeof(ifmr));
420 1.7 yamaguch error = if_ioctl(ifp_port, SIOCGIFMEDIA, (void *)&ifmr);
421 1.7 yamaguch if (error == 0) {
422 1.7 yamaguch linkspeed = ifmedia_baudrate(ifmr.ifm_active);
423 1.7 yamaguch } else {
424 1.7 yamaguch linkspeed = 0;
425 1.7 yamaguch }
426 1.1 yamaguch
427 1.7 yamaguch LAGG_PROTO_LOCK(psc);
428 1.7 yamaguch pport->lpp_linkspeed = linkspeed;
429 1.7 yamaguch LAGG_PROTO_UNLOCK(psc);
430 1.7 yamaguch lagg_workq_add(psc->psc_workq, &psc->psc_work_linkspeed);
431 1.1 yamaguch }
432 1.1 yamaguch
433 1.1 yamaguch void
434 1.1 yamaguch lagg_common_detach(struct lagg_proto_softc *psc)
435 1.1 yamaguch {
436 1.1 yamaguch
437 1.1 yamaguch lagg_proto_free(psc);
438 1.1 yamaguch }
439 1.1 yamaguch
440 1.1 yamaguch int
441 1.1 yamaguch lagg_none_attach(struct lagg_softc *sc, struct lagg_proto_softc **pscp)
442 1.1 yamaguch {
443 1.1 yamaguch
444 1.1 yamaguch *pscp = NULL;
445 1.1 yamaguch return 0;
446 1.1 yamaguch }
447 1.1 yamaguch
448 1.1 yamaguch int
449 1.1 yamaguch lagg_fail_attach(struct lagg_softc *sc, struct lagg_proto_softc **xpsc)
450 1.1 yamaguch {
451 1.1 yamaguch struct lagg_proto_softc *psc;
452 1.1 yamaguch struct lagg_failover *fovr;
453 1.1 yamaguch
454 1.1 yamaguch psc = lagg_proto_alloc(LAGG_PROTO_FAILOVER, sc);
455 1.1 yamaguch if (psc == NULL)
456 1.1 yamaguch return ENOMEM;
457 1.1 yamaguch
458 1.1 yamaguch fovr = psc->psc_ctx;
459 1.1 yamaguch fovr->fo_rx_all = true;
460 1.7 yamaguch lagg_work_set(&psc->psc_work_linkspeed,
461 1.7 yamaguch lagg_fail_linkspeed_work, psc);
462 1.1 yamaguch
463 1.1 yamaguch *xpsc = psc;
464 1.1 yamaguch return 0;
465 1.1 yamaguch }
466 1.1 yamaguch
467 1.1 yamaguch int
468 1.1 yamaguch lagg_fail_transmit(struct lagg_proto_softc *psc, struct mbuf *m)
469 1.1 yamaguch {
470 1.1 yamaguch struct ifnet *ifp;
471 1.1 yamaguch struct lagg_port *lp;
472 1.1 yamaguch struct psref psref;
473 1.1 yamaguch
474 1.1 yamaguch lp = lagg_link_active(psc, NULL, &psref);
475 1.1 yamaguch if (lp == NULL) {
476 1.1 yamaguch ifp = &psc->psc_softc->sc_if;
477 1.1 yamaguch if_statinc(ifp, if_oerrors);
478 1.1 yamaguch m_freem(m);
479 1.1 yamaguch return ENOENT;
480 1.1 yamaguch }
481 1.1 yamaguch
482 1.6 yamaguch lagg_output(psc->psc_softc, lp, m);
483 1.1 yamaguch lagg_port_putref(lp, &psref);
484 1.1 yamaguch return 0;
485 1.1 yamaguch }
486 1.1 yamaguch
487 1.1 yamaguch struct mbuf *
488 1.1 yamaguch lagg_fail_input(struct lagg_proto_softc *psc, struct lagg_port *lp,
489 1.1 yamaguch struct mbuf *m)
490 1.1 yamaguch {
491 1.1 yamaguch struct lagg_failover *fovr;
492 1.1 yamaguch struct lagg_port *lp0;
493 1.1 yamaguch struct ifnet *ifp;
494 1.1 yamaguch struct psref psref;
495 1.1 yamaguch
496 1.1 yamaguch fovr = psc->psc_ctx;
497 1.1 yamaguch if (atomic_load_relaxed(&fovr->fo_rx_all))
498 1.1 yamaguch return m;
499 1.1 yamaguch
500 1.1 yamaguch lp0 = lagg_link_active(psc, NULL, &psref);
501 1.1 yamaguch if (lp0 == NULL) {
502 1.1 yamaguch goto drop;
503 1.1 yamaguch }
504 1.1 yamaguch
505 1.1 yamaguch if (lp0 != lp) {
506 1.1 yamaguch lagg_port_putref(lp0, &psref);
507 1.1 yamaguch goto drop;
508 1.1 yamaguch }
509 1.1 yamaguch
510 1.1 yamaguch lagg_port_putref(lp0, &psref);
511 1.1 yamaguch
512 1.1 yamaguch return m;
513 1.1 yamaguch drop:
514 1.1 yamaguch ifp = &psc->psc_softc->sc_if;
515 1.1 yamaguch if_statinc(ifp, if_ierrors);
516 1.1 yamaguch m_freem(m);
517 1.1 yamaguch return NULL;
518 1.1 yamaguch }
519 1.1 yamaguch
520 1.1 yamaguch void
521 1.1 yamaguch lagg_fail_portstat(struct lagg_proto_softc *psc, struct lagg_port *lp,
522 1.1 yamaguch struct laggreqport *resp)
523 1.1 yamaguch {
524 1.1 yamaguch struct lagg_failover *fovr;
525 1.1 yamaguch struct lagg_proto_port *pport;
526 1.1 yamaguch struct lagg_port *lp0;
527 1.1 yamaguch struct psref psref;
528 1.1 yamaguch
529 1.1 yamaguch fovr = psc->psc_ctx;
530 1.1 yamaguch pport = lp->lp_proto_ctx;
531 1.1 yamaguch
532 1.1 yamaguch if (pport->lpp_active) {
533 1.1 yamaguch lp0 = lagg_link_active(psc, NULL, &psref);
534 1.1 yamaguch if (lp0 == lp) {
535 1.1 yamaguch SET(resp->rp_flags,
536 1.4 yamaguch (LAGG_PORT_ACTIVE |
537 1.4 yamaguch LAGG_PORT_COLLECTING |
538 1.4 yamaguch LAGG_PORT_DISTRIBUTING));
539 1.4 yamaguch } else {
540 1.4 yamaguch if (fovr->fo_rx_all) {
541 1.4 yamaguch SET(resp->rp_flags,
542 1.4 yamaguch LAGG_PORT_COLLECTING);
543 1.4 yamaguch }
544 1.1 yamaguch }
545 1.4 yamaguch
546 1.1 yamaguch if (lp0 != NULL)
547 1.1 yamaguch lagg_port_putref(lp0, &psref);
548 1.1 yamaguch }
549 1.1 yamaguch }
550 1.1 yamaguch
551 1.1 yamaguch int
552 1.1 yamaguch lagg_fail_ioctl(struct lagg_proto_softc *psc, struct laggreqproto *lreq)
553 1.1 yamaguch {
554 1.1 yamaguch struct lagg_failover *fovr;
555 1.1 yamaguch struct laggreq_fail *rpfail;
556 1.1 yamaguch int error;
557 1.1 yamaguch bool set;
558 1.1 yamaguch
559 1.1 yamaguch error = 0;
560 1.1 yamaguch fovr = psc->psc_ctx;
561 1.1 yamaguch rpfail = &lreq->rp_fail;
562 1.1 yamaguch
563 1.1 yamaguch switch (rpfail->command) {
564 1.1 yamaguch case LAGGIOC_FAILSETFLAGS:
565 1.1 yamaguch case LAGGIOC_FAILCLRFLAGS:
566 1.1 yamaguch set = (rpfail->command == LAGGIOC_FAILSETFLAGS) ?
567 1.1 yamaguch true : false;
568 1.1 yamaguch
569 1.1 yamaguch if (ISSET(rpfail->flags, LAGGREQFAIL_RXALL))
570 1.1 yamaguch fovr->fo_rx_all = set;
571 1.1 yamaguch break;
572 1.1 yamaguch default:
573 1.1 yamaguch error = ENOTTY;
574 1.1 yamaguch break;
575 1.1 yamaguch }
576 1.1 yamaguch
577 1.1 yamaguch return error;
578 1.1 yamaguch }
579 1.1 yamaguch
580 1.7 yamaguch void
581 1.7 yamaguch lagg_fail_linkspeed_work(struct lagg_work *_lw __unused, void *xpsc)
582 1.7 yamaguch {
583 1.7 yamaguch struct lagg_proto_softc *psc = xpsc;
584 1.7 yamaguch struct lagg_proto_port *pport;
585 1.7 yamaguch struct lagg_port *lp;
586 1.7 yamaguch struct psref psref;
587 1.7 yamaguch uint64_t linkspeed;
588 1.7 yamaguch
589 1.7 yamaguch kpreempt_disable();
590 1.7 yamaguch lp = lagg_link_active(psc, NULL, &psref);
591 1.7 yamaguch if (lp != NULL) {
592 1.7 yamaguch pport = lp->lp_proto_ctx;
593 1.7 yamaguch LAGG_PROTO_LOCK(psc);
594 1.7 yamaguch linkspeed = pport->lpp_linkspeed;
595 1.7 yamaguch LAGG_PROTO_UNLOCK(psc);
596 1.7 yamaguch lagg_port_putref(lp, &psref);
597 1.7 yamaguch } else {
598 1.7 yamaguch linkspeed = 0;
599 1.7 yamaguch }
600 1.7 yamaguch kpreempt_enable();
601 1.7 yamaguch
602 1.7 yamaguch LAGG_LOCK(psc->psc_softc);
603 1.7 yamaguch lagg_set_linkspeed(psc->psc_softc, linkspeed);
604 1.7 yamaguch LAGG_UNLOCK(psc->psc_softc);
605 1.7 yamaguch }
606 1.7 yamaguch
607 1.1 yamaguch int
608 1.1 yamaguch lagg_lb_attach(struct lagg_softc *sc, struct lagg_proto_softc **xpsc)
609 1.1 yamaguch {
610 1.1 yamaguch struct lagg_proto_softc *psc;
611 1.1 yamaguch struct lagg_lb *lb;
612 1.1 yamaguch
613 1.1 yamaguch psc = lagg_proto_alloc(LAGG_PROTO_LOADBALANCE, sc);
614 1.1 yamaguch if (psc == NULL)
615 1.1 yamaguch return ENOMEM;
616 1.1 yamaguch
617 1.1 yamaguch lb = psc->psc_ctx;
618 1.1 yamaguch lb->lb_pmaps.maps_activepmap = 0;
619 1.7 yamaguch lagg_work_set(&psc->psc_work_linkspeed,
620 1.7 yamaguch lagg_lb_linkspeed_work, psc);
621 1.1 yamaguch
622 1.1 yamaguch *xpsc = psc;
623 1.1 yamaguch return 0;
624 1.1 yamaguch }
625 1.1 yamaguch
626 1.1 yamaguch void
627 1.1 yamaguch lagg_lb_startport(struct lagg_proto_softc *psc, struct lagg_port *lp)
628 1.1 yamaguch {
629 1.1 yamaguch struct lagg_lb *lb;
630 1.1 yamaguch struct lagg_portmap *pm_act, *pm_next;
631 1.1 yamaguch size_t n;
632 1.1 yamaguch
633 1.1 yamaguch lb = psc->psc_ctx;
634 1.1 yamaguch lagg_common_startport(psc, lp);
635 1.1 yamaguch
636 1.1 yamaguch LAGG_PROTO_LOCK(psc);
637 1.1 yamaguch pm_act = lagg_portmap_active(&lb->lb_pmaps);
638 1.1 yamaguch pm_next = lagg_portmap_next(&lb->lb_pmaps);
639 1.1 yamaguch
640 1.1 yamaguch *pm_next = *pm_act;
641 1.1 yamaguch
642 1.1 yamaguch n = pm_next->pm_nports;
643 1.1 yamaguch pm_next->pm_ports[n] = lp;
644 1.1 yamaguch
645 1.1 yamaguch n++;
646 1.1 yamaguch pm_next->pm_nports = n;
647 1.1 yamaguch
648 1.1 yamaguch lagg_portmap_switch(&lb->lb_pmaps);
649 1.1 yamaguch pserialize_perform(psc->psc_psz);
650 1.1 yamaguch LAGG_PROTO_UNLOCK(psc);
651 1.1 yamaguch }
652 1.1 yamaguch
653 1.1 yamaguch void
654 1.1 yamaguch lagg_lb_stopport(struct lagg_proto_softc *psc, struct lagg_port *lp)
655 1.1 yamaguch {
656 1.1 yamaguch struct lagg_lb *lb;
657 1.1 yamaguch struct lagg_portmap *pm_act, *pm_next;
658 1.1 yamaguch size_t i, n;
659 1.1 yamaguch
660 1.1 yamaguch lb = psc->psc_ctx;
661 1.1 yamaguch
662 1.1 yamaguch LAGG_PROTO_LOCK(psc);
663 1.1 yamaguch pm_act = lagg_portmap_active(&lb->lb_pmaps);
664 1.1 yamaguch pm_next = lagg_portmap_next(&lb->lb_pmaps);
665 1.1 yamaguch n = 0;
666 1.1 yamaguch
667 1.1 yamaguch for (i = 0; i < pm_act->pm_nports; i++) {
668 1.1 yamaguch if (pm_act->pm_ports[i] == lp)
669 1.1 yamaguch continue;
670 1.1 yamaguch
671 1.1 yamaguch pm_next->pm_ports[n] = pm_act->pm_ports[i];
672 1.1 yamaguch n++;
673 1.1 yamaguch }
674 1.1 yamaguch
675 1.1 yamaguch lagg_portmap_switch(&lb->lb_pmaps);
676 1.1 yamaguch pserialize_perform(psc->psc_psz);
677 1.1 yamaguch LAGG_PROTO_UNLOCK(psc);
678 1.1 yamaguch
679 1.1 yamaguch lagg_common_stopport(psc, lp);
680 1.1 yamaguch }
681 1.1 yamaguch
682 1.1 yamaguch int
683 1.1 yamaguch lagg_lb_transmit(struct lagg_proto_softc *psc, struct mbuf *m)
684 1.1 yamaguch {
685 1.1 yamaguch struct lagg_lb *lb;
686 1.1 yamaguch struct lagg_portmap *pm;
687 1.1 yamaguch struct lagg_port *lp, *lp0;
688 1.1 yamaguch struct ifnet *ifp;
689 1.1 yamaguch struct psref psref;
690 1.1 yamaguch uint32_t hash;
691 1.1 yamaguch int s;
692 1.1 yamaguch
693 1.1 yamaguch lb = psc->psc_ctx;
694 1.1 yamaguch hash = lagg_hashmbuf(psc->psc_softc, m);
695 1.1 yamaguch
696 1.1 yamaguch s = pserialize_read_enter();
697 1.1 yamaguch
698 1.1 yamaguch pm = lagg_portmap_active(&lb->lb_pmaps);
699 1.1 yamaguch hash %= pm->pm_nports;
700 1.1 yamaguch lp0 = pm->pm_ports[hash];
701 1.1 yamaguch lp = lagg_link_active(psc, lp0->lp_proto_ctx, &psref);
702 1.1 yamaguch
703 1.1 yamaguch pserialize_read_exit(s);
704 1.1 yamaguch
705 1.1 yamaguch if (__predict_false(lp == NULL)) {
706 1.1 yamaguch ifp = &psc->psc_softc->sc_if;
707 1.1 yamaguch if_statinc(ifp, if_oerrors);
708 1.1 yamaguch m_freem(m);
709 1.1 yamaguch return ENOENT;
710 1.1 yamaguch }
711 1.1 yamaguch
712 1.6 yamaguch lagg_output(psc->psc_softc, lp, m);
713 1.1 yamaguch lagg_port_putref(lp, &psref);
714 1.1 yamaguch
715 1.1 yamaguch return 0;
716 1.1 yamaguch }
717 1.1 yamaguch
718 1.1 yamaguch struct mbuf *
719 1.1 yamaguch lagg_lb_input(struct lagg_proto_softc *psc __unused,
720 1.1 yamaguch struct lagg_port *lp __unused, struct mbuf *m)
721 1.1 yamaguch {
722 1.1 yamaguch
723 1.1 yamaguch return m;
724 1.1 yamaguch }
725 1.1 yamaguch
726 1.1 yamaguch void
727 1.1 yamaguch lagg_lb_portstat(struct lagg_proto_softc *psc, struct lagg_port *lp,
728 1.1 yamaguch struct laggreqport *resp)
729 1.1 yamaguch {
730 1.1 yamaguch struct lagg_proto_port *pport;
731 1.1 yamaguch
732 1.1 yamaguch pport = lp->lp_proto_ctx;
733 1.1 yamaguch
734 1.1 yamaguch if (pport->lpp_active) {
735 1.1 yamaguch SET(resp->rp_flags, LAGG_PORT_ACTIVE |
736 1.1 yamaguch LAGG_PORT_COLLECTING | LAGG_PORT_DISTRIBUTING);
737 1.1 yamaguch }
738 1.1 yamaguch }
739 1.7 yamaguch
740 1.7 yamaguch static void
741 1.7 yamaguch lagg_lb_linkspeed_work(struct lagg_work *_lw __unused, void *xpsc)
742 1.7 yamaguch {
743 1.7 yamaguch struct lagg_proto_softc *psc = xpsc;
744 1.7 yamaguch struct lagg_proto_port *pport;
745 1.7 yamaguch uint64_t linkspeed, l;
746 1.7 yamaguch int s;
747 1.7 yamaguch
748 1.7 yamaguch linkspeed = 0;
749 1.7 yamaguch
750 1.7 yamaguch s = pserialize_read_enter();
751 1.7 yamaguch PSLIST_READER_FOREACH(pport, &psc->psc_ports,
752 1.7 yamaguch struct lagg_proto_port, lpp_entry) {
753 1.7 yamaguch if (pport->lpp_active) {
754 1.7 yamaguch LAGG_PROTO_LOCK(psc);
755 1.7 yamaguch l = pport->lpp_linkspeed;
756 1.7 yamaguch LAGG_PROTO_UNLOCK(psc);
757 1.7 yamaguch linkspeed = MAX(linkspeed, l);
758 1.7 yamaguch }
759 1.7 yamaguch }
760 1.7 yamaguch pserialize_read_exit(s);
761 1.7 yamaguch
762 1.7 yamaguch LAGG_LOCK(psc->psc_softc);
763 1.7 yamaguch lagg_set_linkspeed(psc->psc_softc, linkspeed);
764 1.7 yamaguch LAGG_UNLOCK(psc->psc_softc);
765 1.7 yamaguch }
766