Vendor import of netgraph from FreeBSD-current 20080626
[dragonfly.git] / sys / netgraph7 / ng_fec.c
blob0b5bdbb7afb34a620f76c9510a3e74fe3613c659
1 /*
2 * ng_fec.c
3 */
5 /*-
6 * Copyright (c) 2001 Berkeley Software Design, Inc.
7 * Copyright (c) 2000, 2001
8 * Bill Paul <wpaul@osd.bsdi.com>. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Bill Paul.
21 * 4. Neither the name of the author nor the names of any co-contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35 * THE POSSIBILITY OF SUCH DAMAGE.
37 * $FreeBSD: src/sys/netgraph/ng_fec.c,v 1.30 2007/05/18 15:05:49 dwmalone Exp $
39 /*-
40 * Copyright (c) 1996-1999 Whistle Communications, Inc.
41 * All rights reserved.
43 * Subject to the following obligations and disclaimer of warranty, use and
44 * redistribution of this software, in source or object code forms, with or
45 * without modifications are expressly permitted by Whistle Communications;
46 * provided, however, that:
47 * 1. Any and all reproductions of the source or object code must include the
48 * copyright notice above and the following disclaimer of warranties; and
49 * 2. No rights are granted, in any manner or form, to use Whistle
50 * Communications, Inc. trademarks, including the mark "WHISTLE
51 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
52 * such appears in the above copyright notice or in the software.
54 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
55 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
56 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
57 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
58 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
59 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
60 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
61 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
62 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
63 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
64 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
65 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
66 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
67 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
68 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
69 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
70 * OF SUCH DAMAGE.
72 * Author: Archie Cobbs <archie@freebsd.org>
74 * $Whistle: ng_fec.c,v 1.33 1999/11/01 09:24:51 julian Exp $
78 * This module implements ethernet channel bonding using the Cisco
79 * Fast EtherChannel mechanism. Two or four ports may be combined
80 * into a single aggregate interface.
82 * Interfaces are named fec0, fec1, etc. New nodes take the
83 * first available interface name.
85 * This node also includes Berkeley packet filter support.
87 * Note that this node doesn't need to connect to any other
88 * netgraph nodes in order to do its work.
91 #include <sys/param.h>
92 #include <sys/systm.h>
93 #include <sys/errno.h>
94 #include <sys/kernel.h>
95 #include <sys/malloc.h>
96 #include <sys/mbuf.h>
97 #include <sys/errno.h>
98 #include <sys/sockio.h>
99 #include <sys/socket.h>
100 #include <sys/syslog.h>
101 #include <sys/libkern.h>
102 #include <sys/queue.h>
104 #include <net/if.h>
105 #include <net/if_dl.h>
106 #include <net/if_types.h>
107 #include <net/if_media.h>
108 #include <net/bpf.h>
109 #include <net/ethernet.h>
111 #include "opt_inet.h"
112 #include "opt_inet6.h"
114 #include <netinet/in.h>
115 #ifdef INET
116 #include <netinet/in_systm.h>
117 #include <netinet/ip.h>
118 #endif
120 #ifdef INET6
121 #include <netinet/ip6.h>
122 #endif
124 #include <netgraph/ng_message.h>
125 #include <netgraph/netgraph.h>
126 #include <netgraph/ng_parse.h>
127 #include <netgraph/ng_fec.h>
130 * We need a way to stash a pointer to our netgraph node in the
131 * ifnet structure so that receive handling works. As far as I can
132 * tell, although there is an AF_NETGRAPH address family, it's only
133 * used to identify sockaddr_ng structures: there is no netgraph address
134 * family domain. This means the AF_NETGRAPH entry in ifp->if_afdata
135 * should be unused, so we can use to hold our node context.
137 #define IFP2NG(ifp) ((ifp)->if_afdata[AF_NETGRAPH])
140 * Current fast etherchannel implementations use either 2 or 4
141 * ports, so for now we limit the maximum bundle size to 4 interfaces.
143 #define FEC_BUNDLESIZ 4
145 struct ng_fec_portlist {
146 struct ifnet *fec_if;
147 void (*fec_if_input) (struct ifnet *,
148 struct mbuf *);
149 int fec_idx;
150 int fec_ifstat;
151 struct ether_addr fec_mac;
152 SLIST_HEAD(__mclhd, ng_fec_mc) fec_mc_head;
153 TAILQ_ENTRY(ng_fec_portlist) fec_list;
156 struct ng_fec_mc {
157 struct ifmultiaddr *mc_ifma;
158 SLIST_ENTRY(ng_fec_mc) mc_entries;
161 struct ng_fec_bundle {
162 TAILQ_HEAD(,ng_fec_portlist) ng_fec_ports;
163 int fec_ifcnt;
164 int fec_btype;
165 int (*fec_if_output) (struct ifnet *,
166 struct mbuf *,
167 struct sockaddr *,
168 struct rtentry *);
171 #define FEC_BTYPE_MAC 0x01
172 #define FEC_BTYPE_INET 0x02
173 #define FEC_BTYPE_INET6 0x03
175 /* Node private data */
176 struct ng_fec_private {
177 struct ifnet *ifp;
178 struct ifmedia ifmedia;
179 int if_flags;
180 int if_error; /* XXX */
181 int unit; /* Interface unit number */
182 node_p node; /* Our netgraph node */
183 struct ng_fec_bundle fec_bundle;/* Aggregate bundle */
184 struct callout_handle fec_ch; /* callout handle for ticker */
186 typedef struct ng_fec_private *priv_p;
188 /* Interface methods */
189 static void ng_fec_input(struct ifnet *, struct mbuf *);
190 static void ng_fec_start(struct ifnet *ifp);
191 static int ng_fec_choose_port(struct ng_fec_bundle *b,
192 struct mbuf *m, struct ifnet **ifp);
193 static int ng_fec_setport(struct ifnet *ifp, u_long cmd, caddr_t data);
194 static void ng_fec_init(void *arg);
195 static void ng_fec_stop(struct ifnet *ifp);
196 static int ng_fec_ifmedia_upd(struct ifnet *ifp);
197 static void ng_fec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
198 static int ng_fec_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
199 static int ng_fec_output(struct ifnet *ifp, struct mbuf *m0,
200 struct sockaddr *dst, struct rtentry *rt0);
201 static void ng_fec_tick(void *arg);
202 static int ng_fec_addport(struct ng_fec_private *priv, char *iface);
203 static int ng_fec_delport(struct ng_fec_private *priv, char *iface);
204 static int ng_fec_ether_cmdmulti(struct ifnet *trifp, struct ng_fec_portlist *p, int set);
206 #ifdef DEBUG
207 static void ng_fec_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
208 #endif
210 /* Netgraph methods */
211 static int ng_fec_mod_event(module_t, int, void *);
212 static ng_constructor_t ng_fec_constructor;
213 static ng_rcvmsg_t ng_fec_rcvmsg;
214 static ng_shutdown_t ng_fec_shutdown;
216 /* List of commands and how to convert arguments to/from ASCII */
217 static const struct ng_cmdlist ng_fec_cmds[] = {
219 NGM_FEC_COOKIE,
220 NGM_FEC_ADD_IFACE,
221 "add_iface",
222 &ng_parse_string_type,
223 NULL,
226 NGM_FEC_COOKIE,
227 NGM_FEC_DEL_IFACE,
228 "del_iface",
229 &ng_parse_string_type,
230 NULL,
233 NGM_FEC_COOKIE,
234 NGM_FEC_SET_MODE_MAC,
235 "set_mode_mac",
236 NULL,
237 NULL,
240 NGM_FEC_COOKIE,
241 NGM_FEC_SET_MODE_INET,
242 "set_mode_inet",
243 NULL,
244 NULL,
246 { 0 }
249 /* Node type descriptor */
250 static struct ng_type typestruct = {
251 .version = NG_ABI_VERSION,
252 .name = NG_FEC_NODE_TYPE,
253 .mod_event = ng_fec_mod_event,
254 .constructor = ng_fec_constructor,
255 .rcvmsg = ng_fec_rcvmsg,
256 .shutdown = ng_fec_shutdown,
257 .cmdlist = ng_fec_cmds,
259 NETGRAPH_INIT(fec, &typestruct);
261 /* We keep a bitmap indicating which unit numbers are free.
262 One means the unit number is free, zero means it's taken. */
263 static int *ng_fec_units = NULL;
264 static int ng_fec_units_len = 0;
265 static int ng_units_in_use = 0;
267 #define UNITS_BITSPERWORD (sizeof(*ng_fec_units) * NBBY)
269 static struct mtx ng_fec_mtx;
272 * Find the first free unit number for a new interface.
273 * Increase the size of the unit bitmap as necessary.
275 static __inline int
276 ng_fec_get_unit(int *unit)
278 int index, bit;
280 mtx_lock(&ng_fec_mtx);
281 for (index = 0; index < ng_fec_units_len
282 && ng_fec_units[index] == 0; index++);
283 if (index == ng_fec_units_len) { /* extend array */
284 int i, *newarray, newlen;
286 newlen = (2 * ng_fec_units_len) + 4;
287 MALLOC(newarray, int *, newlen * sizeof(*ng_fec_units),
288 M_NETGRAPH, M_NOWAIT);
289 if (newarray == NULL) {
290 mtx_unlock(&ng_fec_mtx);
291 return (ENOMEM);
293 bcopy(ng_fec_units, newarray,
294 ng_fec_units_len * sizeof(*ng_fec_units));
295 for (i = ng_fec_units_len; i < newlen; i++)
296 newarray[i] = ~0;
297 if (ng_fec_units != NULL)
298 FREE(ng_fec_units, M_NETGRAPH);
299 ng_fec_units = newarray;
300 ng_fec_units_len = newlen;
302 bit = ffs(ng_fec_units[index]) - 1;
303 KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
304 ("%s: word=%d bit=%d", __func__, ng_fec_units[index], bit));
305 ng_fec_units[index] &= ~(1 << bit);
306 *unit = (index * UNITS_BITSPERWORD) + bit;
307 ng_units_in_use++;
308 mtx_unlock(&ng_fec_mtx);
309 return (0);
313 * Free a no longer needed unit number.
315 static __inline void
316 ng_fec_free_unit(int unit)
318 int index, bit;
320 index = unit / UNITS_BITSPERWORD;
321 bit = unit % UNITS_BITSPERWORD;
322 mtx_lock(&ng_fec_mtx);
323 KASSERT(index < ng_fec_units_len,
324 ("%s: unit=%d len=%d", __func__, unit, ng_fec_units_len));
325 KASSERT((ng_fec_units[index] & (1 << bit)) == 0,
326 ("%s: unit=%d is free", __func__, unit));
327 ng_fec_units[index] |= (1 << bit);
329 * XXX We could think about reducing the size of ng_fec_units[]
330 * XXX here if the last portion is all ones
331 * XXX At least free it if no more units
332 * Needed if we are to eventually be able to unload.
334 ng_units_in_use--;
335 if (ng_units_in_use == 0) { /* XXX make SMP safe */
336 FREE(ng_fec_units, M_NETGRAPH);
337 ng_fec_units_len = 0;
338 ng_fec_units = NULL;
340 mtx_unlock(&ng_fec_mtx);
343 /************************************************************************
344 INTERFACE STUFF
345 ************************************************************************/
347 static int
348 ng_fec_addport(struct ng_fec_private *priv, char *iface)
350 struct ng_fec_bundle *b;
351 struct ifnet *ifp, *bifp;
352 struct ng_fec_portlist *p, *new;
354 if (priv == NULL || iface == NULL)
355 return(EINVAL);
357 b = &priv->fec_bundle;
358 ifp = priv->ifp;
360 /* Only allow reconfiguration if not running. */
361 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
362 printf("fec%d: can't add new iface; bundle is running\n",
363 priv->unit);
364 return (EINVAL);
367 /* Find the interface */
368 bifp = ifunit(iface);
369 if (bifp == NULL) {
370 printf("fec%d: tried to add iface %s, which "
371 "doesn't seem to exist\n", priv->unit, iface);
372 return(ENOENT);
375 /* See if we have room in the bundle */
376 if (b->fec_ifcnt == FEC_BUNDLESIZ) {
377 printf("fec%d: can't add new iface; bundle is full\n",
378 priv->unit);
379 return(ENOSPC);
382 /* See if the interface is already in the bundle */
383 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
384 if (p->fec_if == bifp) {
385 printf("fec%d: iface %s is already in this "
386 "bundle\n", priv->unit, iface);
387 return(EINVAL);
392 * All interfaces must use the same output vector. Once the
393 * user attaches an interface of one type, make all subsequent
394 * interfaces have the same output vector.
396 if (b->fec_if_output != NULL) {
397 if (b->fec_if_output != bifp->if_output) {
398 printf("fec%d: iface %s is not the same type "
399 "as the other interface(s) already in "
400 "the bundle\n", priv->unit, iface);
401 return(EINVAL);
405 /* Allocate new list entry. */
406 MALLOC(new, struct ng_fec_portlist *,
407 sizeof(struct ng_fec_portlist), M_NETGRAPH, M_NOWAIT);
408 if (new == NULL)
409 return(ENOMEM);
411 IF_AFDATA_LOCK(bifp);
412 IFP2NG(bifp) = priv->node;
413 IF_AFDATA_UNLOCK(bifp);
416 * If this is the first interface added to the bundle,
417 * use its MAC address for the virtual interface (and,
418 * by extension, all the other ports in the bundle).
420 if (b->fec_ifcnt == 0)
421 if_setlladdr(ifp, IF_LLADDR(bifp), ETHER_ADDR_LEN);
423 b->fec_btype = FEC_BTYPE_MAC;
424 new->fec_idx = b->fec_ifcnt;
425 b->fec_ifcnt++;
427 /* Initialise the list of multicast addresses that we own. */
428 SLIST_INIT(&new->fec_mc_head);
430 /* Save the real MAC address. */
431 bcopy(IF_LLADDR(bifp),
432 (char *)&new->fec_mac, ETHER_ADDR_LEN);
434 /* Set up phony MAC address. */
435 if_setlladdr(bifp, IF_LLADDR(ifp), ETHER_ADDR_LEN);
437 /* Save original input vector */
438 new->fec_if_input = bifp->if_input;
440 /* Override it with our own */
441 bifp->if_input = ng_fec_input;
443 /* Save output vector too. */
444 if (b->fec_if_output == NULL)
445 b->fec_if_output = bifp->if_output;
447 /* Add to the queue */
448 new->fec_if = bifp;
449 new->fec_ifstat = -1;
450 TAILQ_INSERT_TAIL(&b->ng_fec_ports, new, fec_list);
452 /* Add multicast addresses to this port. */
453 ng_fec_ether_cmdmulti(ifp, new, 1);
455 return(0);
458 static int
459 ng_fec_delport(struct ng_fec_private *priv, char *iface)
461 struct ng_fec_bundle *b;
462 struct ifnet *ifp, *bifp;
463 struct ng_fec_portlist *p;
465 if (priv == NULL || iface == NULL)
466 return(EINVAL);
468 b = &priv->fec_bundle;
469 ifp = priv->ifp;
471 /* Only allow reconfiguration if not running. */
472 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
473 printf("fec%d: can't remove iface; bundle is running\n",
474 priv->unit);
475 return (EINVAL);
478 /* Find the interface */
479 bifp = ifunit(iface);
480 if (bifp == NULL) {
481 printf("fec%d: tried to remove iface %s, which "
482 "doesn't seem to exist\n", priv->unit, iface);
483 return(ENOENT);
486 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
487 if (p->fec_if == bifp)
488 break;
491 if (p == NULL) {
492 printf("fec%d: tried to remove iface %s which "
493 "is not in our bundle\n", priv->unit, iface);
494 return(EINVAL);
497 /* Stop interface */
498 bifp->if_flags &= ~IFF_UP;
499 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
501 /* Restore MAC address. */
502 if_setlladdr(bifp, (u_char *)&p->fec_mac, ETHER_ADDR_LEN);
504 /* Restore input vector */
505 bifp->if_input = p->fec_if_input;
507 /* Remove our node context pointer. */
508 IF_AFDATA_LOCK(bifp);
509 IFP2NG(bifp) = NULL;
510 IF_AFDATA_UNLOCK(bifp);
512 /* Delete port */
513 TAILQ_REMOVE(&b->ng_fec_ports, p, fec_list);
514 FREE(p, M_NETGRAPH);
515 b->fec_ifcnt--;
517 if (b->fec_ifcnt == 0)
518 b->fec_if_output = NULL;
520 return(0);
523 static int
524 ng_fec_ether_cmdmulti(struct ifnet *trifp, struct ng_fec_portlist *p, int set)
526 struct ifnet *ifp = p->fec_if;
527 struct ng_fec_mc *mc;
528 struct ifmultiaddr *ifma, *rifma = NULL;
529 struct sockaddr_dl sdl;
530 int error;
532 bzero((char *)&sdl, sizeof(sdl));
533 sdl.sdl_len = sizeof(sdl);
534 sdl.sdl_family = AF_LINK;
535 sdl.sdl_type = IFT_ETHER;
536 sdl.sdl_alen = ETHER_ADDR_LEN;
537 sdl.sdl_index = ifp->if_index;
539 if (set) {
540 TAILQ_FOREACH(ifma, &trifp->if_multiaddrs, ifma_link) {
541 if (ifma->ifma_addr->sa_family != AF_LINK)
542 continue;
543 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
544 LLADDR(&sdl), ETHER_ADDR_LEN);
546 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
547 if (error)
548 return (error);
549 mc = malloc(sizeof(struct ng_fec_mc), M_DEVBUF, M_NOWAIT);
550 if (mc == NULL)
551 return (ENOMEM);
552 mc->mc_ifma = rifma;
553 SLIST_INSERT_HEAD(&p->fec_mc_head, mc, mc_entries);
555 } else {
556 while ((mc = SLIST_FIRST(&p->fec_mc_head)) != NULL) {
557 SLIST_REMOVE(&p->fec_mc_head, mc, ng_fec_mc, mc_entries);
558 if_delmulti_ifma(mc->mc_ifma);
559 free(mc, M_DEVBUF);
562 return (0);
565 static int
566 ng_fec_ether_setmulti(struct ifnet *ifp)
568 struct ng_fec_private *priv;
569 struct ng_fec_bundle *b;
570 struct ng_fec_portlist *p;
572 priv = ifp->if_softc;
573 b = &priv->fec_bundle;
575 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
576 /* First, remove any existing filter entries. */
577 ng_fec_ether_cmdmulti(ifp, p, 0);
578 /* copy all addresses from the fec interface to the port */
579 ng_fec_ether_cmdmulti(ifp, p, 1);
581 return (0);
585 * Pass an ioctl command down to all the underyling interfaces in a
586 * bundle. Used for setting flags.
589 static int
590 ng_fec_setport(struct ifnet *ifp, u_long command, caddr_t data)
592 struct ng_fec_private *priv;
593 struct ng_fec_bundle *b;
594 struct ifnet *oifp;
595 struct ng_fec_portlist *p;
597 priv = ifp->if_softc;
598 b = &priv->fec_bundle;
600 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
601 oifp = p->fec_if;
602 if (oifp != NULL)
603 (*oifp->if_ioctl)(oifp, command, data);
606 return(0);
609 static void
610 ng_fec_init(void *arg)
612 struct ng_fec_private *priv;
613 struct ng_fec_bundle *b;
614 struct ifnet *ifp, *bifp;
615 struct ng_fec_portlist *p;
617 priv = arg;
618 ifp = priv->ifp;
619 b = &priv->fec_bundle;
621 if (b->fec_ifcnt != 2 && b->fec_ifcnt != FEC_BUNDLESIZ) {
622 printf("fec%d: invalid bundle "
623 "size: %d\n", priv->unit,
624 b->fec_ifcnt);
625 return;
628 ng_fec_stop(ifp);
630 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
631 bifp = p->fec_if;
632 bifp->if_flags |= IFF_UP;
633 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
634 /* mark iface as up and let the monitor check it */
635 p->fec_ifstat = -1;
638 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
639 ifp->if_drv_flags |= IFF_DRV_RUNNING;
641 priv->fec_ch = timeout(ng_fec_tick, priv, hz);
643 return;
646 static void
647 ng_fec_stop(struct ifnet *ifp)
649 struct ng_fec_private *priv;
650 struct ng_fec_bundle *b;
651 struct ifnet *bifp;
652 struct ng_fec_portlist *p;
654 priv = ifp->if_softc;
655 b = &priv->fec_bundle;
657 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
658 bifp = p->fec_if;
659 bifp->if_flags &= ~IFF_UP;
660 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
663 untimeout(ng_fec_tick, priv, priv->fec_ch);
665 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
667 return;
670 static void
671 ng_fec_tick(void *arg)
673 struct ng_fec_private *priv;
674 struct ng_fec_bundle *b;
675 struct ifmediareq ifmr;
676 struct ifnet *ifp;
677 struct ng_fec_portlist *p;
678 int error = 0;
680 priv = arg;
681 b = &priv->fec_bundle;
683 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
684 bzero((char *)&ifmr, sizeof(ifmr));
685 ifp = p->fec_if;
686 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
687 if (error) {
688 printf("fec%d: failed to check status "
689 "of link %s\n", priv->unit, ifp->if_xname);
690 continue;
693 if (ifmr.ifm_status & IFM_AVALID) {
694 if (ifmr.ifm_status & IFM_ACTIVE) {
695 if (p->fec_ifstat == -1 ||
696 p->fec_ifstat == 0) {
697 p->fec_ifstat = 1;
698 printf("fec%d: port %s in bundle "
699 "is up\n", priv->unit,
700 ifp->if_xname);
702 } else {
703 if (p->fec_ifstat == -1 ||
704 p->fec_ifstat == 1) {
705 p->fec_ifstat = 0;
706 printf("fec%d: port %s in bundle "
707 "is down\n", priv->unit,
708 ifp->if_xname);
714 ifp = priv->ifp;
715 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
716 priv->fec_ch = timeout(ng_fec_tick, priv, hz);
718 return;
721 static int
722 ng_fec_ifmedia_upd(struct ifnet *ifp)
724 return(0);
727 static void ng_fec_ifmedia_sts(struct ifnet *ifp,
728 struct ifmediareq *ifmr)
730 struct ng_fec_private *priv;
731 struct ng_fec_bundle *b;
732 struct ng_fec_portlist *p;
734 priv = ifp->if_softc;
735 b = &priv->fec_bundle;
737 ifmr->ifm_status = IFM_AVALID;
738 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
739 if (p->fec_ifstat == 1) {
740 ifmr->ifm_status |= IFM_ACTIVE;
741 break;
745 return;
749 * Process an ioctl for the virtual interface
751 static int
752 ng_fec_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
754 struct ifreq *const ifr = (struct ifreq *) data;
755 int s, error = 0;
756 struct ng_fec_private *priv;
757 struct ng_fec_bundle *b;
759 priv = ifp->if_softc;
760 b = &priv->fec_bundle;
762 #ifdef DEBUG
763 ng_fec_print_ioctl(ifp, command, data);
764 #endif
765 s = splimp();
766 switch (command) {
768 /* These two are mostly handled at a higher layer */
769 case SIOCSIFADDR:
770 case SIOCGIFADDR:
771 error = ether_ioctl(ifp, command, data);
772 break;
774 case SIOCSIFMTU:
775 if (ifr->ifr_mtu >= NG_FEC_MTU_MIN &&
776 ifr->ifr_mtu <= NG_FEC_MTU_MAX) {
777 struct ng_fec_portlist *p;
778 struct ifnet *bifp;
780 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
781 bifp = p->fec_if;
782 error = (*bifp->if_ioctl)(bifp, SIOCSIFMTU,
783 data);
784 if (error != 0)
785 break;
787 if (error == 0)
788 ifp->if_mtu = ifr->ifr_mtu;
789 } else
790 error = EINVAL;
791 break;
793 /* Set flags */
794 case SIOCSIFFLAGS:
796 * If the interface is marked up and stopped, then start it.
797 * If it is marked down and running, then stop it.
799 if (ifr->ifr_flags & IFF_UP) {
800 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
801 /* Sanity. */
802 if (b->fec_ifcnt != 2 &&
803 b->fec_ifcnt != FEC_BUNDLESIZ) {
804 printf("fec%d: invalid bundle "
805 "size: %d\n", priv->unit,
806 b->fec_ifcnt);
807 error = EINVAL;
808 break;
810 ng_fec_init(priv);
813 * Bubble down changes in promisc mode to
814 * underlying interfaces.
816 if ((ifp->if_flags & IFF_PROMISC) !=
817 (priv->if_flags & IFF_PROMISC)) {
818 ng_fec_setport(ifp, command, data);
819 priv->if_flags = ifp->if_flags;
821 } else {
822 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
823 ng_fec_stop(ifp);
825 break;
827 case SIOCADDMULTI:
828 case SIOCDELMULTI:
829 ng_fec_ether_setmulti(ifp);
830 error = 0;
831 break;
832 case SIOCGIFMEDIA:
833 case SIOCSIFMEDIA:
834 error = ifmedia_ioctl(ifp, ifr, &priv->ifmedia, command);
835 break;
836 /* Stuff that's not supported */
837 case SIOCSIFPHYS:
838 error = EOPNOTSUPP;
839 break;
841 default:
842 error = EINVAL;
843 break;
845 (void) splx(s);
846 return (error);
850 * This routine spies on mbufs received by underlying network device
851 * drivers. When we add an interface to our bundle, we override its
852 * if_input routine with a pointer to ng_fec_input(). This means we
853 * get to look at all the device's packets before sending them to the
854 * real ether_input() for processing by the stack. Once we verify the
855 * packet comes from an interface that's been aggregated into
856 * our bundle, we fix up the rcvif pointer and increment our
857 * packet counters so that it looks like the frames are actually
858 * coming from us.
860 static void
861 ng_fec_input(struct ifnet *ifp, struct mbuf *m0)
863 struct ng_node *node;
864 struct ng_fec_private *priv;
865 struct ng_fec_bundle *b;
866 struct ifnet *bifp;
867 struct ng_fec_portlist *p;
869 /* Sanity check */
870 if (ifp == NULL || m0 == NULL)
871 return;
873 node = IFP2NG(ifp);
875 /* Sanity check part II */
876 if (node == NULL)
877 return;
879 priv = NG_NODE_PRIVATE(node);
880 b = &priv->fec_bundle;
881 bifp = priv->ifp;
883 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
884 if (p->fec_if == m0->m_pkthdr.rcvif)
885 break;
888 /* Wasn't meant for us; leave this frame alone. */
889 if (p == NULL)
890 return;
893 * Check for a BPF tap on the underlying interface. This
894 * is mainly a debugging aid: it allows tcpdump-ing of an
895 * individual interface in a bundle to work, which it
896 * otherwise would not. BPF tapping of our own aggregate
897 * interface will occur once we call ether_input().
899 BPF_MTAP(m0->m_pkthdr.rcvif, m0);
901 /* Convince the system that this is our frame. */
902 m0->m_pkthdr.rcvif = bifp;
905 * Count bytes on an individual interface in a bundle.
906 * The bytes will also be added to the aggregate interface
907 * once we call ether_input().
909 ifp->if_ibytes += m0->m_pkthdr.len;
911 bifp->if_ipackets++;
912 (*bifp->if_input)(bifp, m0);
914 return;
918 * Take a quick peek at the packet and see if it's ok for us to use
919 * the inet or inet6 hash methods on it, if they're enabled. We do
920 * this by setting flags in the mbuf header. Once we've made up our
921 * mind what to do, we pass the frame to output vector for further
922 * processing.
925 static int
926 ng_fec_output(struct ifnet *ifp, struct mbuf *m,
927 struct sockaddr *dst, struct rtentry *rt0)
929 const priv_p priv = (priv_p) ifp->if_softc;
930 struct ng_fec_bundle *b;
931 int error;
933 /* Check interface flags */
934 if (!((ifp->if_flags & IFF_UP) &&
935 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
936 m_freem(m);
937 return (ENETDOWN);
940 b = &priv->fec_bundle;
942 switch (b->fec_btype) {
943 case FEC_BTYPE_MAC:
944 m->m_flags |= M_FEC_MAC;
945 break;
946 #ifdef INET
947 case FEC_BTYPE_INET:
949 * We can't use the INET address port selection
950 * scheme if this isn't an INET packet.
952 if (dst->sa_family == AF_INET)
953 m->m_flags |= M_FEC_INET;
954 #ifdef INET6
955 else if (dst->sa_family == AF_INET6)
956 m->m_flags |= M_FEC_INET6;
957 #endif
958 else {
959 #ifdef DEBUG
960 if_printf(ifp, "can't do inet aggregation of non "
961 "inet packet\n");
962 #endif
963 m->m_flags |= M_FEC_MAC;
965 break;
966 #endif
967 default:
968 if_printf(ifp, "bogus hash type: %d\n",
969 b->fec_btype);
970 m_freem(m);
971 return(EINVAL);
972 break;
976 * Pass the frame to the output vector for all the protocol
977 * handling. This will put the ethernet header on the packet
978 * for us.
980 priv->if_error = 0;
981 error = (*b->fec_if_output)(ifp, m, dst, rt0);
982 if (priv->if_error && !error)
983 error = priv->if_error;
985 return(error);
989 * Apply a hash to the source and destination addresses in the packet
990 * in order to select an interface. Also check link status and handle
991 * dead links accordingly.
994 static int
995 ng_fec_choose_port(struct ng_fec_bundle *b,
996 struct mbuf *m, struct ifnet **ifp)
998 struct ether_header *eh;
999 struct mbuf *m0;
1000 #ifdef INET
1001 struct ip *ip;
1002 #ifdef INET6
1003 struct ip6_hdr *ip6;
1004 #endif
1005 #endif
1007 struct ng_fec_portlist *p;
1008 int port = 0, mask;
1011 * If there are only two ports, mask off all but the
1012 * last bit for XORing. If there are 4, mask off all
1013 * but the last 2 bits.
1015 mask = b->fec_ifcnt == 2 ? 0x1 : 0x3;
1016 eh = mtod(m, struct ether_header *);
1017 #ifdef INET
1018 ip = (struct ip *)(mtod(m, char *) +
1019 sizeof(struct ether_header));
1020 #ifdef INET6
1021 ip6 = (struct ip6_hdr *)(mtod(m, char *) +
1022 sizeof(struct ether_header));
1023 #endif
1024 #endif
1027 * The fg_fec_output() routine is supposed to leave a
1028 * flag for us in the mbuf that tells us what hash to
1029 * use, but sometimes a new mbuf is prepended to the
1030 * chain, so we have to search every mbuf in the chain
1031 * to find the flags.
1033 m0 = m;
1034 while (m0) {
1035 if (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6))
1036 break;
1037 m0 = m0->m_next;
1039 if (m0 == NULL)
1040 return(EINVAL);
1042 switch (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6)) {
1043 case M_FEC_MAC:
1044 port = (eh->ether_dhost[5] ^
1045 eh->ether_shost[5]) & mask;
1046 break;
1047 #ifdef INET
1048 case M_FEC_INET:
1049 port = (ntohl(ip->ip_dst.s_addr) ^
1050 ntohl(ip->ip_src.s_addr)) & mask;
1051 break;
1052 #ifdef INET6
1053 case M_FEC_INET6:
1054 port = (ip6->ip6_dst.s6_addr[15] ^
1055 ip6->ip6_dst.s6_addr[15]) & mask;
1056 break;
1057 #endif
1058 #endif
1059 default:
1060 return(EINVAL);
1061 break;
1064 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
1065 if (port == p->fec_idx)
1066 break;
1070 * Now that we've chosen a port, make sure it's
1071 * alive. If it's not alive, cycle through the bundle
1072 * looking for a port that is alive. If we don't find
1073 * any, return an error.
1075 if (p->fec_ifstat != 1) {
1076 struct ng_fec_portlist *n = NULL;
1078 n = TAILQ_NEXT(p, fec_list);
1079 if (n == NULL)
1080 n = TAILQ_FIRST(&b->ng_fec_ports);
1081 while (n != p) {
1082 if (n->fec_ifstat == 1)
1083 break;
1084 n = TAILQ_NEXT(n, fec_list);
1085 if (n == NULL)
1086 n = TAILQ_FIRST(&b->ng_fec_ports);
1088 if (n == p)
1089 return(EAGAIN);
1090 p = n;
1093 *ifp = p->fec_if;
1095 return(0);
1099 * Now that the packet has been run through ether_output(), yank it
1100 * off our own send queue and stick it on the queue for the appropriate
1101 * underlying physical interface. Note that if the interface's send
1102 * queue is full, we save an error status in our private netgraph
1103 * space which will eventually be handed up to ng_fec_output(), which
1104 * will return it to the rest of the IP stack. We need to do this
1105 * in order to duplicate the effect of ether_output() returning ENOBUFS
1106 * when it detects that an interface's send queue is full. There's no
1107 * other way to signal the error status from here since the if_start()
1108 * routine is spec'ed to return void.
1110 * Once the frame is queued, we call ether_output_frame() to initiate
1111 * transmission.
1113 static void
1114 ng_fec_start(struct ifnet *ifp)
1116 struct ng_fec_private *priv;
1117 struct ng_fec_bundle *b;
1118 struct ifnet *oifp = NULL;
1119 struct mbuf *m0;
1120 int error;
1122 priv = ifp->if_softc;
1123 b = &priv->fec_bundle;
1125 IF_DEQUEUE(&ifp->if_snd, m0);
1126 if (m0 == NULL)
1127 return;
1129 BPF_MTAP(ifp, m0);
1131 /* Queue up packet on the proper port. */
1132 error = ng_fec_choose_port(b, m0, &oifp);
1133 if (error) {
1134 ifp->if_ierrors++;
1135 m_freem(m0);
1136 priv->if_error = ENOBUFS;
1137 return;
1139 ifp->if_opackets++;
1141 priv->if_error = IF_HANDOFF(&oifp->if_snd, m0, oifp) ? 0 : ENOBUFS;
1143 return;
1146 #ifdef DEBUG
1148 * Display an ioctl to the virtual interface
1151 static void
1152 ng_fec_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
1154 char *str;
1156 switch (command & IOC_DIRMASK) {
1157 case IOC_VOID:
1158 str = "IO";
1159 break;
1160 case IOC_OUT:
1161 str = "IOR";
1162 break;
1163 case IOC_IN:
1164 str = "IOW";
1165 break;
1166 case IOC_INOUT:
1167 str = "IORW";
1168 break;
1169 default:
1170 str = "IO??";
1172 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
1173 ifp->if_xname,
1174 str,
1175 IOCGROUP(command),
1176 command & 0xff,
1177 IOCPARM_LEN(command));
1179 #endif /* DEBUG */
1181 /************************************************************************
1182 NETGRAPH NODE STUFF
1183 ************************************************************************/
1186 * Constructor for a node
1188 static int
1189 ng_fec_constructor(node_p node)
1191 char ifname[NG_FEC_FEC_NAME_MAX + 1];
1192 struct ifnet *ifp;
1193 priv_p priv;
1194 const uint8_t eaddr[ETHER_ADDR_LEN] = {0, 0, 0, 0, 0, 0};
1195 struct ng_fec_bundle *b;
1196 int error = 0;
1198 /* Allocate node and interface private structures */
1199 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
1200 if (priv == NULL)
1201 return (ENOMEM);
1203 ifp = priv->ifp = if_alloc(IFT_ETHER);
1204 if (ifp == NULL) {
1205 FREE(priv, M_NETGRAPH);
1206 return (ENOSPC);
1208 b = &priv->fec_bundle;
1210 /* Link them together */
1211 ifp->if_softc = priv;
1213 /* Get an interface unit number */
1214 if ((error = ng_fec_get_unit(&priv->unit)) != 0) {
1215 if_free(ifp);
1216 FREE(priv, M_NETGRAPH);
1217 return (error);
1220 /* Link together node and private info */
1221 NG_NODE_SET_PRIVATE(node, priv);
1222 priv->node = node;
1224 /* Initialize interface structure */
1225 if_initname(ifp, NG_FEC_FEC_NAME, priv->unit);
1226 ifp->if_start = ng_fec_start;
1227 ifp->if_ioctl = ng_fec_ioctl;
1228 ifp->if_init = ng_fec_init;
1229 ifp->if_watchdog = NULL;
1230 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
1231 ifp->if_mtu = NG_FEC_MTU_DEFAULT;
1232 ifp->if_flags = (IFF_SIMPLEX|IFF_BROADCAST|IFF_MULTICAST);
1233 ifp->if_addrlen = 0; /* XXX */
1234 ifp->if_hdrlen = 0; /* XXX */
1235 ifp->if_baudrate = 100000000; /* XXX */
1236 TAILQ_INIT(&ifp->if_addrhead); /* XXX useless - done in if_attach */
1238 /* Give this node the same name as the interface (if possible) */
1239 bzero(ifname, sizeof(ifname));
1240 strlcpy(ifname, ifp->if_xname, sizeof(ifname));
1241 if (ng_name_node(node, ifname) != 0)
1242 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
1244 /* Attach the interface */
1245 ether_ifattach(ifp, eaddr);
1246 callout_handle_init(&priv->fec_ch);
1248 /* Override output method with our own */
1249 ifp->if_output = ng_fec_output;
1251 TAILQ_INIT(&b->ng_fec_ports);
1252 b->fec_ifcnt = 0;
1254 ifmedia_init(&priv->ifmedia, 0,
1255 ng_fec_ifmedia_upd, ng_fec_ifmedia_sts);
1256 ifmedia_add(&priv->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
1257 ifmedia_set(&priv->ifmedia, IFM_ETHER|IFM_NONE);
1259 /* Done */
1260 return (0);
1264 * Receive a control message
1266 static int
1267 ng_fec_rcvmsg(node_p node, item_p item, hook_p lasthook)
1269 const priv_p priv = NG_NODE_PRIVATE(node);
1270 struct ng_fec_bundle *b;
1271 struct ng_mesg *resp = NULL;
1272 struct ng_mesg *msg;
1273 char *ifname;
1274 int error = 0;
1276 NGI_GET_MSG(item, msg);
1277 b = &priv->fec_bundle;
1279 switch (msg->header.typecookie) {
1280 case NGM_FEC_COOKIE:
1281 switch (msg->header.cmd) {
1282 case NGM_FEC_ADD_IFACE:
1283 ifname = msg->data;
1284 error = ng_fec_addport(priv, ifname);
1285 break;
1286 case NGM_FEC_DEL_IFACE:
1287 ifname = msg->data;
1288 error = ng_fec_delport(priv, ifname);
1289 break;
1290 case NGM_FEC_SET_MODE_MAC:
1291 b->fec_btype = FEC_BTYPE_MAC;
1292 break;
1293 #ifdef INET
1294 case NGM_FEC_SET_MODE_INET:
1295 b->fec_btype = FEC_BTYPE_INET;
1296 break;
1297 #ifdef INET6
1298 case NGM_FEC_SET_MODE_INET6:
1299 b->fec_btype = FEC_BTYPE_INET6;
1300 break;
1301 #endif
1302 #endif
1303 default:
1304 error = EINVAL;
1305 break;
1307 break;
1308 default:
1309 error = EINVAL;
1310 break;
1312 NG_RESPOND_MSG(error, node, item, resp);
1313 NG_FREE_MSG(msg);
1314 return (error);
1318 * Shutdown and remove the node and its associated interface.
1320 static int
1321 ng_fec_shutdown(node_p node)
1323 const priv_p priv = NG_NODE_PRIVATE(node);
1324 struct ng_fec_bundle *b;
1325 struct ng_fec_portlist *p;
1327 b = &priv->fec_bundle;
1328 ng_fec_stop(priv->ifp);
1330 while (!TAILQ_EMPTY(&b->ng_fec_ports)) {
1331 p = TAILQ_FIRST(&b->ng_fec_ports);
1332 ng_fec_ether_cmdmulti(priv->ifp, p, 0);
1333 ng_fec_delport(priv, p->fec_if->if_xname);
1336 ether_ifdetach(priv->ifp);
1337 if_free_type(priv->ifp, IFT_ETHER);
1338 ifmedia_removeall(&priv->ifmedia);
1339 ng_fec_free_unit(priv->unit);
1340 FREE(priv, M_NETGRAPH);
1341 NG_NODE_SET_PRIVATE(node, NULL);
1342 NG_NODE_UNREF(node);
1343 return (0);
1347 * Handle loading and unloading for this node type.
1349 static int
1350 ng_fec_mod_event(module_t mod, int event, void *data)
1352 int error = 0;
1354 switch (event) {
1355 case MOD_LOAD:
1356 mtx_init(&ng_fec_mtx, "ng_fec", NULL, MTX_DEF);
1357 break;
1358 case MOD_UNLOAD:
1359 mtx_destroy(&ng_fec_mtx);
1360 break;
1361 default:
1362 error = EOPNOTSUPP;
1363 break;
1365 return (error);