if_laggproto.c revision 1.4 1 1.4 yamaguch /* $NetBSD: if_laggproto.c,v 1.4 2022/03/31 03:05:41 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.4 yamaguch __KERNEL_RCSID(0, "$NetBSD: if_laggproto.c,v 1.4 2022/03/31 03:05:41 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.1 yamaguch };
61 1.1 yamaguch
62 1.1 yamaguch /*
63 1.1 yamaguch * Locking notes:
64 1.1 yamaguch * - Items of struct lagg_proto_softc is protected by
65 1.1 yamaguch * psc_lock (an adaptive mutex)
66 1.1 yamaguch * - psc_ports is protected by pserialize (psc_psz)
67 1.1 yamaguch * - Updates of psc_ports is serialized by sc_lock in
68 1.1 yamaguch * struct lagg_softc
69 1.1 yamaguch * - Other locking notes are described in if_laggproto.h
70 1.1 yamaguch */
71 1.1 yamaguch
72 1.1 yamaguch struct lagg_failover {
73 1.1 yamaguch bool fo_rx_all;
74 1.1 yamaguch };
75 1.1 yamaguch
76 1.1 yamaguch struct lagg_portmap {
77 1.1 yamaguch struct lagg_port *pm_ports[LAGG_MAX_PORTS];
78 1.1 yamaguch size_t pm_nports;
79 1.1 yamaguch };
80 1.1 yamaguch
81 1.1 yamaguch struct lagg_portmaps {
82 1.1 yamaguch struct lagg_portmap maps_pmap[2];
83 1.1 yamaguch size_t maps_activepmap;
84 1.1 yamaguch };
85 1.1 yamaguch
86 1.1 yamaguch struct lagg_lb {
87 1.1 yamaguch struct lagg_portmaps lb_pmaps;
88 1.1 yamaguch };
89 1.1 yamaguch
90 1.1 yamaguch struct lagg_proto_port {
91 1.1 yamaguch struct pslist_entry lpp_entry;
92 1.1 yamaguch struct lagg_port *lpp_laggport;
93 1.1 yamaguch bool lpp_active;
94 1.1 yamaguch };
95 1.1 yamaguch
96 1.1 yamaguch #define LAGG_PROTO_LOCK(_psc) mutex_enter(&(_psc)->psc_lock)
97 1.1 yamaguch #define LAGG_PROTO_UNLOCK(_psc) mutex_exit(&(_psc)->psc_lock)
98 1.1 yamaguch #define LAGG_PROTO_LOCKED(_psc) mutex_owned(&(_psc)->psc_lock)
99 1.1 yamaguch
100 1.1 yamaguch static struct lagg_proto_softc *
101 1.1 yamaguch lagg_proto_alloc(lagg_proto, struct lagg_softc *);
102 1.1 yamaguch static void lagg_proto_free(struct lagg_proto_softc *);
103 1.1 yamaguch static void lagg_proto_insert_port(struct lagg_proto_softc *,
104 1.1 yamaguch struct lagg_proto_port *);
105 1.1 yamaguch static void lagg_proto_remove_port(struct lagg_proto_softc *,
106 1.1 yamaguch struct lagg_proto_port *);
107 1.1 yamaguch static struct lagg_port *
108 1.1 yamaguch lagg_link_active(struct lagg_proto_softc *psc,
109 1.1 yamaguch struct lagg_proto_port *, struct psref *);
110 1.1 yamaguch
111 1.1 yamaguch static inline struct lagg_portmap *
112 1.1 yamaguch lagg_portmap_active(struct lagg_portmaps *maps)
113 1.1 yamaguch {
114 1.1 yamaguch size_t i;
115 1.1 yamaguch
116 1.1 yamaguch i = atomic_load_consume(&maps->maps_activepmap);
117 1.1 yamaguch
118 1.1 yamaguch return &maps->maps_pmap[i];
119 1.1 yamaguch }
120 1.1 yamaguch
121 1.1 yamaguch static inline struct lagg_portmap *
122 1.1 yamaguch lagg_portmap_next(struct lagg_portmaps *maps)
123 1.1 yamaguch {
124 1.1 yamaguch size_t i;
125 1.1 yamaguch
126 1.1 yamaguch i = atomic_load_consume(&maps->maps_activepmap);
127 1.1 yamaguch i &= 0x1;
128 1.1 yamaguch i ^= 0x1;
129 1.1 yamaguch
130 1.1 yamaguch return &maps->maps_pmap[i];
131 1.1 yamaguch }
132 1.1 yamaguch
133 1.1 yamaguch static inline void
134 1.1 yamaguch lagg_portmap_switch(struct lagg_portmaps *maps)
135 1.1 yamaguch {
136 1.1 yamaguch size_t i;
137 1.1 yamaguch
138 1.1 yamaguch i = atomic_load_consume(&maps->maps_activepmap);
139 1.1 yamaguch i &= 0x1;
140 1.1 yamaguch i ^= 0x1;
141 1.1 yamaguch
142 1.1 yamaguch atomic_store_release(&maps->maps_activepmap, i);
143 1.1 yamaguch }
144 1.1 yamaguch
145 1.1 yamaguch static struct lagg_proto_softc *
146 1.1 yamaguch lagg_proto_alloc(lagg_proto pr, struct lagg_softc *sc)
147 1.1 yamaguch {
148 1.1 yamaguch struct lagg_proto_softc *psc;
149 1.1 yamaguch size_t ctxsiz;
150 1.1 yamaguch
151 1.1 yamaguch switch (pr) {
152 1.1 yamaguch case LAGG_PROTO_FAILOVER:
153 1.1 yamaguch ctxsiz = sizeof(struct lagg_failover);
154 1.1 yamaguch break;
155 1.1 yamaguch case LAGG_PROTO_LOADBALANCE:
156 1.1 yamaguch ctxsiz = sizeof(struct lagg_lb);
157 1.1 yamaguch break;
158 1.1 yamaguch default:
159 1.1 yamaguch ctxsiz = 0;
160 1.1 yamaguch }
161 1.1 yamaguch
162 1.1 yamaguch psc = kmem_zalloc(sizeof(*psc), KM_NOSLEEP);
163 1.1 yamaguch if (psc == NULL)
164 1.1 yamaguch return NULL;
165 1.1 yamaguch
166 1.1 yamaguch if (ctxsiz > 0) {
167 1.1 yamaguch psc->psc_ctx = kmem_zalloc(ctxsiz, KM_NOSLEEP);
168 1.1 yamaguch if (psc->psc_ctx == NULL) {
169 1.1 yamaguch kmem_free(psc, sizeof(*psc));
170 1.1 yamaguch return NULL;
171 1.1 yamaguch }
172 1.1 yamaguch
173 1.1 yamaguch psc->psc_ctxsiz = ctxsiz;
174 1.1 yamaguch }
175 1.1 yamaguch
176 1.1 yamaguch PSLIST_INIT(&psc->psc_ports);
177 1.1 yamaguch psc->psc_psz = pserialize_create();
178 1.1 yamaguch mutex_init(&psc->psc_lock, MUTEX_DEFAULT, IPL_SOFTNET);
179 1.1 yamaguch psc->psc_softc = sc;
180 1.1 yamaguch
181 1.1 yamaguch return psc;
182 1.1 yamaguch }
183 1.1 yamaguch
184 1.1 yamaguch static void
185 1.1 yamaguch lagg_proto_free(struct lagg_proto_softc *psc)
186 1.1 yamaguch {
187 1.1 yamaguch
188 1.1 yamaguch pserialize_destroy(psc->psc_psz);
189 1.1 yamaguch mutex_destroy(&psc->psc_lock);
190 1.1 yamaguch
191 1.1 yamaguch if (psc->psc_ctxsiz > 0)
192 1.1 yamaguch kmem_free(psc->psc_ctx, psc->psc_ctxsiz);
193 1.1 yamaguch
194 1.1 yamaguch kmem_free(psc, sizeof(*psc));
195 1.1 yamaguch }
196 1.1 yamaguch
197 1.1 yamaguch static struct lagg_port *
198 1.1 yamaguch lagg_link_active(struct lagg_proto_softc *psc,
199 1.1 yamaguch struct lagg_proto_port *pport, struct psref *psref)
200 1.1 yamaguch {
201 1.1 yamaguch struct lagg_port *lp;
202 1.1 yamaguch int s;
203 1.1 yamaguch
204 1.1 yamaguch lp = NULL;
205 1.1 yamaguch s = pserialize_read_enter();
206 1.1 yamaguch
207 1.1 yamaguch for (;pport != NULL;
208 1.1 yamaguch pport = PSLIST_READER_NEXT(pport,
209 1.1 yamaguch struct lagg_proto_port, lpp_entry)) {
210 1.1 yamaguch if (atomic_load_relaxed(&pport->lpp_active)) {
211 1.1 yamaguch lp = pport->lpp_laggport;
212 1.1 yamaguch goto done;
213 1.1 yamaguch }
214 1.1 yamaguch }
215 1.1 yamaguch
216 1.1 yamaguch PSLIST_READER_FOREACH(pport, &psc->psc_ports,
217 1.1 yamaguch struct lagg_proto_port, lpp_entry) {
218 1.1 yamaguch if (atomic_load_relaxed(&pport->lpp_active)) {
219 1.1 yamaguch lp = pport->lpp_laggport;
220 1.1 yamaguch break;
221 1.1 yamaguch }
222 1.1 yamaguch }
223 1.1 yamaguch done:
224 1.1 yamaguch if (lp != NULL)
225 1.1 yamaguch lagg_port_getref(lp, psref);
226 1.1 yamaguch pserialize_read_exit(s);
227 1.1 yamaguch
228 1.1 yamaguch return lp;
229 1.1 yamaguch }
230 1.1 yamaguch
231 1.1 yamaguch int
232 1.1 yamaguch lagg_common_allocport(struct lagg_proto_softc *psc, struct lagg_port *lp)
233 1.1 yamaguch {
234 1.1 yamaguch struct lagg_proto_port *pport;
235 1.1 yamaguch
236 1.1 yamaguch KASSERT(LAGG_LOCKED(psc->psc_softc));
237 1.1 yamaguch
238 1.1 yamaguch pport = kmem_zalloc(sizeof(*pport), KM_NOSLEEP);
239 1.1 yamaguch if (pport == NULL)
240 1.1 yamaguch return ENOMEM;
241 1.1 yamaguch
242 1.1 yamaguch PSLIST_ENTRY_INIT(pport, lpp_entry);
243 1.1 yamaguch pport->lpp_laggport = lp;
244 1.1 yamaguch lp->lp_proto_ctx = (void *)pport;
245 1.1 yamaguch return 0;
246 1.1 yamaguch }
247 1.1 yamaguch
248 1.1 yamaguch void
249 1.1 yamaguch lagg_common_freeport(struct lagg_proto_softc *psc, struct lagg_port *lp)
250 1.1 yamaguch {
251 1.1 yamaguch struct lagg_proto_port *pport;
252 1.1 yamaguch
253 1.1 yamaguch pport = lp->lp_proto_ctx;
254 1.1 yamaguch lp->lp_proto_ctx = NULL;
255 1.1 yamaguch
256 1.1 yamaguch kmem_free(pport, sizeof(*pport));
257 1.1 yamaguch }
258 1.1 yamaguch
259 1.1 yamaguch static void
260 1.1 yamaguch lagg_proto_insert_port(struct lagg_proto_softc *psc,
261 1.1 yamaguch struct lagg_proto_port *pport)
262 1.1 yamaguch {
263 1.1 yamaguch struct lagg_proto_port *pport0;
264 1.1 yamaguch struct lagg_port *lp, *lp0;
265 1.1 yamaguch bool insert_after;
266 1.1 yamaguch
267 1.1 yamaguch insert_after = false;
268 1.1 yamaguch lp = pport->lpp_laggport;
269 1.1 yamaguch
270 1.1 yamaguch LAGG_PROTO_LOCK(psc);
271 1.1 yamaguch PSLIST_WRITER_FOREACH(pport0, &psc->psc_ports,
272 1.1 yamaguch struct lagg_proto_port, lpp_entry) {
273 1.1 yamaguch lp0 = pport0->lpp_laggport;
274 1.1 yamaguch if (lp0->lp_prio > lp->lp_prio)
275 1.1 yamaguch break;
276 1.1 yamaguch
277 1.1 yamaguch if (PSLIST_WRITER_NEXT(pport0,
278 1.1 yamaguch struct lagg_proto_port, lpp_entry) == NULL) {
279 1.1 yamaguch insert_after = true;
280 1.1 yamaguch break;
281 1.1 yamaguch }
282 1.1 yamaguch }
283 1.1 yamaguch
284 1.1 yamaguch if (pport0 == NULL) {
285 1.1 yamaguch PSLIST_WRITER_INSERT_HEAD(&psc->psc_ports, pport,
286 1.1 yamaguch lpp_entry);
287 1.1 yamaguch } else if (insert_after) {
288 1.1 yamaguch PSLIST_WRITER_INSERT_AFTER(pport0, pport, lpp_entry);
289 1.1 yamaguch } else {
290 1.1 yamaguch PSLIST_WRITER_INSERT_BEFORE(pport0, pport, lpp_entry);
291 1.1 yamaguch }
292 1.1 yamaguch LAGG_PROTO_UNLOCK(psc);
293 1.1 yamaguch }
294 1.1 yamaguch
295 1.1 yamaguch static void
296 1.1 yamaguch lagg_proto_remove_port(struct lagg_proto_softc *psc,
297 1.1 yamaguch struct lagg_proto_port *pport)
298 1.1 yamaguch {
299 1.1 yamaguch
300 1.1 yamaguch LAGG_PROTO_LOCK(psc);
301 1.1 yamaguch PSLIST_WRITER_REMOVE(pport, lpp_entry);
302 1.1 yamaguch pserialize_perform(psc->psc_psz);
303 1.1 yamaguch LAGG_PROTO_UNLOCK(psc);
304 1.1 yamaguch }
305 1.1 yamaguch
306 1.1 yamaguch void
307 1.1 yamaguch lagg_common_startport(struct lagg_proto_softc *psc, struct lagg_port *lp)
308 1.1 yamaguch {
309 1.1 yamaguch struct lagg_proto_port *pport;
310 1.1 yamaguch
311 1.1 yamaguch pport = lp->lp_proto_ctx;
312 1.1 yamaguch lagg_proto_insert_port(psc, pport);
313 1.1 yamaguch
314 1.1 yamaguch lagg_common_linkstate(psc, lp);
315 1.1 yamaguch }
316 1.1 yamaguch
317 1.1 yamaguch void
318 1.1 yamaguch lagg_common_stopport(struct lagg_proto_softc *psc, struct lagg_port *lp)
319 1.1 yamaguch {
320 1.1 yamaguch struct lagg_proto_port *pport;
321 1.1 yamaguch struct ifnet *ifp;
322 1.1 yamaguch
323 1.1 yamaguch pport = lp->lp_proto_ctx;
324 1.1 yamaguch lagg_proto_remove_port(psc, pport);
325 1.1 yamaguch
326 1.1 yamaguch if (pport->lpp_active) {
327 1.3 yamaguch KASSERT(psc->psc_nactports > 0);
328 1.3 yamaguch psc->psc_nactports--;
329 1.1 yamaguch
330 1.1 yamaguch if (psc->psc_nactports == 0) {
331 1.1 yamaguch ifp = &psc->psc_softc->sc_if;
332 1.1 yamaguch if_link_state_change(ifp, LINK_STATE_DOWN);
333 1.1 yamaguch }
334 1.1 yamaguch
335 1.1 yamaguch pport->lpp_active = false;
336 1.1 yamaguch }
337 1.1 yamaguch }
338 1.1 yamaguch
339 1.1 yamaguch void
340 1.1 yamaguch lagg_common_linkstate(struct lagg_proto_softc *psc, struct lagg_port *lp)
341 1.1 yamaguch {
342 1.1 yamaguch struct lagg_proto_port *pport;
343 1.1 yamaguch struct ifnet *ifp;
344 1.1 yamaguch bool is_active;
345 1.1 yamaguch
346 1.1 yamaguch pport = lp->lp_proto_ctx;
347 1.1 yamaguch is_active = lagg_portactive(lp);
348 1.1 yamaguch
349 1.1 yamaguch if (pport->lpp_active == is_active)
350 1.1 yamaguch return;
351 1.1 yamaguch
352 1.1 yamaguch ifp = &psc->psc_softc->sc_if;
353 1.1 yamaguch if (is_active) {
354 1.1 yamaguch psc->psc_nactports++;
355 1.1 yamaguch if (psc->psc_nactports == 1)
356 1.1 yamaguch if_link_state_change(ifp, LINK_STATE_UP);
357 1.1 yamaguch } else {
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 if_link_state_change(ifp, LINK_STATE_DOWN);
363 1.1 yamaguch }
364 1.1 yamaguch
365 1.1 yamaguch atomic_store_relaxed(&pport->lpp_active, is_active);
366 1.1 yamaguch }
367 1.1 yamaguch
368 1.1 yamaguch void
369 1.1 yamaguch lagg_common_detach(struct lagg_proto_softc *psc)
370 1.1 yamaguch {
371 1.1 yamaguch
372 1.1 yamaguch lagg_proto_free(psc);
373 1.1 yamaguch }
374 1.1 yamaguch
375 1.1 yamaguch int
376 1.1 yamaguch lagg_none_attach(struct lagg_softc *sc, struct lagg_proto_softc **pscp)
377 1.1 yamaguch {
378 1.1 yamaguch
379 1.1 yamaguch *pscp = NULL;
380 1.1 yamaguch return 0;
381 1.1 yamaguch }
382 1.1 yamaguch
383 1.1 yamaguch int
384 1.1 yamaguch lagg_none_up(struct lagg_proto_softc *psc __unused)
385 1.1 yamaguch {
386 1.1 yamaguch
387 1.1 yamaguch return EBUSY;
388 1.1 yamaguch }
389 1.1 yamaguch
390 1.1 yamaguch int
391 1.1 yamaguch lagg_fail_attach(struct lagg_softc *sc, struct lagg_proto_softc **xpsc)
392 1.1 yamaguch {
393 1.1 yamaguch struct lagg_proto_softc *psc;
394 1.1 yamaguch struct lagg_failover *fovr;
395 1.1 yamaguch
396 1.1 yamaguch psc = lagg_proto_alloc(LAGG_PROTO_FAILOVER, sc);
397 1.1 yamaguch if (psc == NULL)
398 1.1 yamaguch return ENOMEM;
399 1.1 yamaguch
400 1.1 yamaguch fovr = psc->psc_ctx;
401 1.1 yamaguch fovr->fo_rx_all = true;
402 1.1 yamaguch
403 1.1 yamaguch *xpsc = psc;
404 1.1 yamaguch return 0;
405 1.1 yamaguch }
406 1.1 yamaguch
407 1.1 yamaguch int
408 1.1 yamaguch lagg_fail_transmit(struct lagg_proto_softc *psc, struct mbuf *m)
409 1.1 yamaguch {
410 1.1 yamaguch struct ifnet *ifp;
411 1.1 yamaguch struct lagg_port *lp;
412 1.1 yamaguch struct psref psref;
413 1.1 yamaguch
414 1.1 yamaguch lp = lagg_link_active(psc, NULL, &psref);
415 1.1 yamaguch if (lp == NULL) {
416 1.1 yamaguch ifp = &psc->psc_softc->sc_if;
417 1.1 yamaguch if_statinc(ifp, if_oerrors);
418 1.1 yamaguch m_freem(m);
419 1.1 yamaguch return ENOENT;
420 1.1 yamaguch }
421 1.1 yamaguch
422 1.1 yamaguch lagg_enqueue(psc->psc_softc, lp, m);
423 1.1 yamaguch lagg_port_putref(lp, &psref);
424 1.1 yamaguch return 0;
425 1.1 yamaguch }
426 1.1 yamaguch
427 1.1 yamaguch struct mbuf *
428 1.1 yamaguch lagg_fail_input(struct lagg_proto_softc *psc, struct lagg_port *lp,
429 1.1 yamaguch struct mbuf *m)
430 1.1 yamaguch {
431 1.1 yamaguch struct lagg_failover *fovr;
432 1.1 yamaguch struct lagg_port *lp0;
433 1.1 yamaguch struct ifnet *ifp;
434 1.1 yamaguch struct psref psref;
435 1.1 yamaguch
436 1.1 yamaguch fovr = psc->psc_ctx;
437 1.1 yamaguch if (atomic_load_relaxed(&fovr->fo_rx_all))
438 1.1 yamaguch return m;
439 1.1 yamaguch
440 1.1 yamaguch lp0 = lagg_link_active(psc, NULL, &psref);
441 1.1 yamaguch if (lp0 == NULL) {
442 1.1 yamaguch goto drop;
443 1.1 yamaguch }
444 1.1 yamaguch
445 1.1 yamaguch if (lp0 != lp) {
446 1.1 yamaguch lagg_port_putref(lp0, &psref);
447 1.1 yamaguch goto drop;
448 1.1 yamaguch }
449 1.1 yamaguch
450 1.1 yamaguch lagg_port_putref(lp0, &psref);
451 1.1 yamaguch
452 1.1 yamaguch return m;
453 1.1 yamaguch drop:
454 1.1 yamaguch ifp = &psc->psc_softc->sc_if;
455 1.1 yamaguch if_statinc(ifp, if_ierrors);
456 1.1 yamaguch m_freem(m);
457 1.1 yamaguch return NULL;
458 1.1 yamaguch }
459 1.1 yamaguch
460 1.1 yamaguch void
461 1.1 yamaguch lagg_fail_portstat(struct lagg_proto_softc *psc, struct lagg_port *lp,
462 1.1 yamaguch struct laggreqport *resp)
463 1.1 yamaguch {
464 1.1 yamaguch struct lagg_failover *fovr;
465 1.1 yamaguch struct lagg_proto_port *pport;
466 1.1 yamaguch struct lagg_port *lp0;
467 1.1 yamaguch struct psref psref;
468 1.1 yamaguch
469 1.1 yamaguch fovr = psc->psc_ctx;
470 1.1 yamaguch pport = lp->lp_proto_ctx;
471 1.1 yamaguch
472 1.1 yamaguch if (pport->lpp_active) {
473 1.1 yamaguch lp0 = lagg_link_active(psc, NULL, &psref);
474 1.1 yamaguch if (lp0 == lp) {
475 1.1 yamaguch SET(resp->rp_flags,
476 1.4 yamaguch (LAGG_PORT_ACTIVE |
477 1.4 yamaguch LAGG_PORT_COLLECTING |
478 1.4 yamaguch LAGG_PORT_DISTRIBUTING));
479 1.4 yamaguch } else {
480 1.4 yamaguch if (fovr->fo_rx_all) {
481 1.4 yamaguch SET(resp->rp_flags,
482 1.4 yamaguch LAGG_PORT_COLLECTING);
483 1.4 yamaguch }
484 1.1 yamaguch }
485 1.4 yamaguch
486 1.1 yamaguch if (lp0 != NULL)
487 1.1 yamaguch lagg_port_putref(lp0, &psref);
488 1.1 yamaguch }
489 1.1 yamaguch }
490 1.1 yamaguch
491 1.1 yamaguch int
492 1.1 yamaguch lagg_fail_ioctl(struct lagg_proto_softc *psc, struct laggreqproto *lreq)
493 1.1 yamaguch {
494 1.1 yamaguch struct lagg_failover *fovr;
495 1.1 yamaguch struct laggreq_fail *rpfail;
496 1.1 yamaguch int error;
497 1.1 yamaguch bool set;
498 1.1 yamaguch
499 1.1 yamaguch error = 0;
500 1.1 yamaguch fovr = psc->psc_ctx;
501 1.1 yamaguch rpfail = &lreq->rp_fail;
502 1.1 yamaguch
503 1.1 yamaguch switch (rpfail->command) {
504 1.1 yamaguch case LAGGIOC_FAILSETFLAGS:
505 1.1 yamaguch case LAGGIOC_FAILCLRFLAGS:
506 1.1 yamaguch set = (rpfail->command == LAGGIOC_FAILSETFLAGS) ?
507 1.1 yamaguch true : false;
508 1.1 yamaguch
509 1.1 yamaguch if (ISSET(rpfail->flags, LAGGREQFAIL_RXALL))
510 1.1 yamaguch fovr->fo_rx_all = set;
511 1.1 yamaguch break;
512 1.1 yamaguch default:
513 1.1 yamaguch error = ENOTTY;
514 1.1 yamaguch break;
515 1.1 yamaguch }
516 1.1 yamaguch
517 1.1 yamaguch return error;
518 1.1 yamaguch }
519 1.1 yamaguch
520 1.1 yamaguch int
521 1.1 yamaguch lagg_lb_attach(struct lagg_softc *sc, struct lagg_proto_softc **xpsc)
522 1.1 yamaguch {
523 1.1 yamaguch struct lagg_proto_softc *psc;
524 1.1 yamaguch struct lagg_lb *lb;
525 1.1 yamaguch
526 1.1 yamaguch psc = lagg_proto_alloc(LAGG_PROTO_LOADBALANCE, sc);
527 1.1 yamaguch if (psc == NULL)
528 1.1 yamaguch return ENOMEM;
529 1.1 yamaguch
530 1.1 yamaguch lb = psc->psc_ctx;
531 1.1 yamaguch lb->lb_pmaps.maps_activepmap = 0;
532 1.1 yamaguch
533 1.1 yamaguch *xpsc = psc;
534 1.1 yamaguch return 0;
535 1.1 yamaguch }
536 1.1 yamaguch
537 1.1 yamaguch void
538 1.1 yamaguch lagg_lb_startport(struct lagg_proto_softc *psc, struct lagg_port *lp)
539 1.1 yamaguch {
540 1.1 yamaguch struct lagg_lb *lb;
541 1.1 yamaguch struct lagg_portmap *pm_act, *pm_next;
542 1.1 yamaguch size_t n;
543 1.1 yamaguch
544 1.1 yamaguch lb = psc->psc_ctx;
545 1.1 yamaguch lagg_common_startport(psc, lp);
546 1.1 yamaguch
547 1.1 yamaguch LAGG_PROTO_LOCK(psc);
548 1.1 yamaguch pm_act = lagg_portmap_active(&lb->lb_pmaps);
549 1.1 yamaguch pm_next = lagg_portmap_next(&lb->lb_pmaps);
550 1.1 yamaguch
551 1.1 yamaguch *pm_next = *pm_act;
552 1.1 yamaguch
553 1.1 yamaguch n = pm_next->pm_nports;
554 1.1 yamaguch pm_next->pm_ports[n] = lp;
555 1.1 yamaguch
556 1.1 yamaguch n++;
557 1.1 yamaguch pm_next->pm_nports = n;
558 1.1 yamaguch
559 1.1 yamaguch lagg_portmap_switch(&lb->lb_pmaps);
560 1.1 yamaguch pserialize_perform(psc->psc_psz);
561 1.1 yamaguch LAGG_PROTO_UNLOCK(psc);
562 1.1 yamaguch }
563 1.1 yamaguch
564 1.1 yamaguch void
565 1.1 yamaguch lagg_lb_stopport(struct lagg_proto_softc *psc, struct lagg_port *lp)
566 1.1 yamaguch {
567 1.1 yamaguch struct lagg_lb *lb;
568 1.1 yamaguch struct lagg_portmap *pm_act, *pm_next;
569 1.1 yamaguch size_t i, n;
570 1.1 yamaguch
571 1.1 yamaguch lb = psc->psc_ctx;
572 1.1 yamaguch
573 1.1 yamaguch LAGG_PROTO_LOCK(psc);
574 1.1 yamaguch pm_act = lagg_portmap_active(&lb->lb_pmaps);
575 1.1 yamaguch pm_next = lagg_portmap_next(&lb->lb_pmaps);
576 1.1 yamaguch n = 0;
577 1.1 yamaguch
578 1.1 yamaguch for (i = 0; i < pm_act->pm_nports; i++) {
579 1.1 yamaguch if (pm_act->pm_ports[i] == lp)
580 1.1 yamaguch continue;
581 1.1 yamaguch
582 1.1 yamaguch pm_next->pm_ports[n] = pm_act->pm_ports[i];
583 1.1 yamaguch n++;
584 1.1 yamaguch }
585 1.1 yamaguch
586 1.1 yamaguch lagg_portmap_switch(&lb->lb_pmaps);
587 1.1 yamaguch pserialize_perform(psc->psc_psz);
588 1.1 yamaguch LAGG_PROTO_UNLOCK(psc);
589 1.1 yamaguch
590 1.1 yamaguch lagg_common_stopport(psc, lp);
591 1.1 yamaguch }
592 1.1 yamaguch
593 1.1 yamaguch int
594 1.1 yamaguch lagg_lb_transmit(struct lagg_proto_softc *psc, struct mbuf *m)
595 1.1 yamaguch {
596 1.1 yamaguch struct lagg_lb *lb;
597 1.1 yamaguch struct lagg_portmap *pm;
598 1.1 yamaguch struct lagg_port *lp, *lp0;
599 1.1 yamaguch struct ifnet *ifp;
600 1.1 yamaguch struct psref psref;
601 1.1 yamaguch uint32_t hash;
602 1.1 yamaguch int s;
603 1.1 yamaguch
604 1.1 yamaguch lb = psc->psc_ctx;
605 1.1 yamaguch hash = lagg_hashmbuf(psc->psc_softc, m);
606 1.1 yamaguch
607 1.1 yamaguch s = pserialize_read_enter();
608 1.1 yamaguch
609 1.1 yamaguch pm = lagg_portmap_active(&lb->lb_pmaps);
610 1.1 yamaguch hash %= pm->pm_nports;
611 1.1 yamaguch lp0 = pm->pm_ports[hash];
612 1.1 yamaguch lp = lagg_link_active(psc, lp0->lp_proto_ctx, &psref);
613 1.1 yamaguch
614 1.1 yamaguch pserialize_read_exit(s);
615 1.1 yamaguch
616 1.1 yamaguch if (__predict_false(lp == NULL)) {
617 1.1 yamaguch ifp = &psc->psc_softc->sc_if;
618 1.1 yamaguch if_statinc(ifp, if_oerrors);
619 1.1 yamaguch m_freem(m);
620 1.1 yamaguch return ENOENT;
621 1.1 yamaguch }
622 1.1 yamaguch
623 1.1 yamaguch lagg_enqueue(psc->psc_softc, lp, m);
624 1.1 yamaguch lagg_port_putref(lp, &psref);
625 1.1 yamaguch
626 1.1 yamaguch return 0;
627 1.1 yamaguch }
628 1.1 yamaguch
629 1.1 yamaguch struct mbuf *
630 1.1 yamaguch lagg_lb_input(struct lagg_proto_softc *psc __unused,
631 1.1 yamaguch struct lagg_port *lp __unused, struct mbuf *m)
632 1.1 yamaguch {
633 1.1 yamaguch
634 1.1 yamaguch return m;
635 1.1 yamaguch }
636 1.1 yamaguch
637 1.1 yamaguch void
638 1.1 yamaguch lagg_lb_portstat(struct lagg_proto_softc *psc, struct lagg_port *lp,
639 1.1 yamaguch struct laggreqport *resp)
640 1.1 yamaguch {
641 1.1 yamaguch struct lagg_proto_port *pport;
642 1.1 yamaguch
643 1.1 yamaguch pport = lp->lp_proto_ctx;
644 1.1 yamaguch
645 1.1 yamaguch if (pport->lpp_active) {
646 1.1 yamaguch SET(resp->rp_flags, LAGG_PORT_ACTIVE |
647 1.1 yamaguch LAGG_PORT_COLLECTING | LAGG_PORT_DISTRIBUTING);
648 1.1 yamaguch }
649 1.1 yamaguch }
650