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
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 $
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
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>
97 #include <sys/sockio.h>
98 #include <sys/socket.h>
99 #include <sys/syslog.h>
100 #include <sys/libkern.h>
101 #include <sys/queue.h>
104 #include <net/if_dl.h>
105 #include <net/if_types.h>
106 #include <net/if_media.h>
108 #include <net/ethernet.h>
110 #include "opt_inet.h"
111 #include "opt_inet6.h"
113 #include <netinet/in.h>
115 #include <netinet/in_systm.h>
116 #include <netinet/ip.h>
120 #include <netinet/ip6.h>
123 #include "ng_message.h"
124 #include "netgraph.h"
125 #include "ng_parse.h"
129 * We need a way to stash a pointer to our netgraph node in the
130 * ifnet structure so that receive handling works. As far as I can
131 * tell, although there is an AF_NETGRAPH address family, it's only
132 * used to identify sockaddr_ng structures: there is no netgraph address
133 * family domain. This means the AF_NETGRAPH entry in ifp->if_afdata
134 * should be unused, so we can use to hold our node context.
136 #define IFP2NG(ifp) ((ifp)->if_afdata[AF_NETGRAPH])
139 * Current fast etherchannel implementations use either 2 or 4
140 * ports, so for now we limit the maximum bundle size to 4 interfaces.
142 #define FEC_BUNDLESIZ 4
144 struct ng_fec_portlist {
145 struct ifnet *fec_if;
146 void (*fec_if_input) (struct ifnet *,
148 const struct pktinfo *,
152 struct ether_addr fec_mac;
153 SLIST_HEAD(__mclhd, ng_fec_mc) fec_mc_head;
154 TAILQ_ENTRY(ng_fec_portlist) fec_list;
158 struct ifmultiaddr *mc_ifma;
159 SLIST_ENTRY(ng_fec_mc) mc_entries;
162 struct ng_fec_bundle {
163 TAILQ_HEAD(,ng_fec_portlist) ng_fec_ports;
166 int (*fec_if_output) (struct ifnet *,
172 #define FEC_BTYPE_MAC 0x01
173 #define FEC_BTYPE_INET 0x02
174 #define FEC_BTYPE_INET6 0x03
176 /* Node private data */
177 struct ng_fec_private {
179 struct ifmedia ifmedia;
181 int if_error; /* XXX */
182 int unit; /* Interface unit number */
183 node_p node; /* Our netgraph node */
184 struct ng_fec_bundle fec_bundle;/* Aggregate bundle */
185 struct callout_handle fec_ch; /* callout handle for ticker */
187 typedef struct ng_fec_private *priv_p;
189 /* Interface methods */
190 static void ng_fec_input(struct ifnet *, struct mbuf *,
191 const struct pktinfo *, int);
192 static void ng_fec_start(struct ifnet *ifp, struct ifaltq_subque *);
193 static int ng_fec_choose_port(struct ng_fec_bundle *b,
194 struct mbuf *m, struct ifnet **ifp);
195 static int ng_fec_setport(struct ifnet *ifp, u_long cmd, caddr_t data);
196 static void ng_fec_init(void *arg);
197 static void ng_fec_stop(struct ifnet *ifp);
198 static int ng_fec_ifmedia_upd(struct ifnet *ifp);
199 static void ng_fec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
200 static int ng_fec_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
201 static int ng_fec_output(struct ifnet *ifp, struct mbuf *m0,
202 struct sockaddr *dst, struct rtentry *rt0);
203 static void ng_fec_tick(void *arg);
204 static int ng_fec_addport(struct ng_fec_private *priv, char *iface);
205 static int ng_fec_delport(struct ng_fec_private *priv, char *iface);
206 static int ng_fec_ether_cmdmulti(struct ifnet *trifp, struct ng_fec_portlist *p, int set);
209 static void ng_fec_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
212 /* Netgraph methods */
213 static int ng_fec_mod_event(module_t, int, void *);
214 static ng_constructor_t ng_fec_constructor;
215 static ng_rcvmsg_t ng_fec_rcvmsg;
216 static ng_shutdown_t ng_fec_shutdown;
218 /* List of commands and how to convert arguments to/from ASCII */
219 static const struct ng_cmdlist ng_fec_cmds[] = {
224 &ng_parse_string_type,
231 &ng_parse_string_type,
236 NGM_FEC_SET_MODE_MAC,
243 NGM_FEC_SET_MODE_INET,
251 /* Node type descriptor */
252 static struct ng_type typestruct = {
253 .version = NG_ABI_VERSION,
254 .name = NG_FEC_NODE_TYPE,
255 .mod_event = ng_fec_mod_event,
256 .constructor = ng_fec_constructor,
257 .rcvmsg = ng_fec_rcvmsg,
258 .shutdown = ng_fec_shutdown,
259 .cmdlist = ng_fec_cmds,
261 NETGRAPH_INIT(fec, &typestruct);
263 /* We keep a bitmap indicating which unit numbers are free.
264 One means the unit number is free, zero means it's taken. */
265 static int *ng_fec_units = NULL;
266 static int ng_fec_units_len = 0;
267 static int ng_units_in_use = 0;
269 #define UNITS_BITSPERWORD (sizeof(*ng_fec_units) * NBBY)
271 static struct mtx ng_fec_mtx;
274 * Find the first free unit number for a new interface.
275 * Increase the size of the unit bitmap as necessary.
278 ng_fec_get_unit(int *unit)
282 mtx_lock(&ng_fec_mtx);
283 for (index = 0; index < ng_fec_units_len
284 && ng_fec_units[index] == 0; index++);
285 if (index == ng_fec_units_len) { /* extend array */
286 int i, *newarray, newlen;
288 newlen = (2 * ng_fec_units_len) + 4;
289 newarray = kmalloc(newlen * sizeof(*ng_fec_units),
290 M_NETGRAPH, M_WAITOK | M_NULLOK);
291 if (newarray == NULL) {
292 mtx_unlock(&ng_fec_mtx);
295 bcopy(ng_fec_units, newarray,
296 ng_fec_units_len * sizeof(*ng_fec_units));
297 for (i = ng_fec_units_len; i < newlen; i++)
299 if (ng_fec_units != NULL)
300 kfree(ng_fec_units, M_NETGRAPH);
301 ng_fec_units = newarray;
302 ng_fec_units_len = newlen;
304 bit = ffs(ng_fec_units[index]) - 1;
305 KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
306 ("%s: word=%d bit=%d", __func__, ng_fec_units[index], bit));
307 ng_fec_units[index] &= ~(1 << bit);
308 *unit = (index * UNITS_BITSPERWORD) + bit;
310 mtx_unlock(&ng_fec_mtx);
315 * Free a no longer needed unit number.
318 ng_fec_free_unit(int unit)
322 index = unit / UNITS_BITSPERWORD;
323 bit = unit % UNITS_BITSPERWORD;
324 mtx_lock(&ng_fec_mtx);
325 KASSERT(index < ng_fec_units_len,
326 ("%s: unit=%d len=%d", __func__, unit, ng_fec_units_len));
327 KASSERT((ng_fec_units[index] & (1 << bit)) == 0,
328 ("%s: unit=%d is free", __func__, unit));
329 ng_fec_units[index] |= (1 << bit);
331 * XXX We could think about reducing the size of ng_fec_units[]
332 * XXX here if the last portion is all ones
333 * XXX At least free it if no more units
334 * Needed if we are to eventually be able to unload.
337 if (ng_units_in_use == 0) { /* XXX make SMP safe */
338 kfree(ng_fec_units, M_NETGRAPH);
339 ng_fec_units_len = 0;
342 mtx_unlock(&ng_fec_mtx);
345 /************************************************************************
347 ************************************************************************/
350 ng_fec_addport(struct ng_fec_private *priv, char *iface)
352 struct ng_fec_bundle *b;
353 struct ifnet *ifp, *bifp;
354 struct ng_fec_portlist *p, *new;
356 if (priv == NULL || iface == NULL)
359 b = &priv->fec_bundle;
362 /* Only allow reconfiguration if not running. */
363 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
364 kprintf("fec%d: can't add new iface; bundle is running\n",
369 /* Find the interface */
370 bifp = ifunit(iface);
372 kprintf("fec%d: tried to add iface %s, which "
373 "doesn't seem to exist\n", priv->unit, iface);
377 /* See if we have room in the bundle */
378 if (b->fec_ifcnt == FEC_BUNDLESIZ) {
379 kprintf("fec%d: can't add new iface; bundle is full\n",
384 /* See if the interface is already in the bundle */
385 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
386 if (p->fec_if == bifp) {
387 kprintf("fec%d: iface %s is already in this "
388 "bundle\n", priv->unit, iface);
394 * All interfaces must use the same output vector. Once the
395 * user attaches an interface of one type, make all subsequent
396 * interfaces have the same output vector.
398 if (b->fec_if_output != NULL) {
399 if (b->fec_if_output != bifp->if_output) {
400 kprintf("fec%d: iface %s is not the same type "
401 "as the other interface(s) already in "
402 "the bundle\n", priv->unit, iface);
407 /* Allocate new list entry. */
408 new = kmalloc(sizeof(struct ng_fec_portlist), M_NETGRAPH,
409 M_WAITOK | M_NULLOK);
413 IF_AFDATA_LOCK(bifp);
414 IFP2NG(bifp) = priv->node;
415 IF_AFDATA_UNLOCK(bifp);
418 * If this is the first interface added to the bundle,
419 * use its MAC address for the virtual interface (and,
420 * by extension, all the other ports in the bundle).
422 if (b->fec_ifcnt == 0)
423 if_setlladdr(ifp, IF_LLADDR(bifp), ETHER_ADDR_LEN);
425 b->fec_btype = FEC_BTYPE_MAC;
426 new->fec_idx = b->fec_ifcnt;
429 /* Initialise the list of multicast addresses that we own. */
430 SLIST_INIT(&new->fec_mc_head);
432 /* Save the real MAC address. */
433 bcopy(IF_LLADDR(bifp),
434 (char *)&new->fec_mac, ETHER_ADDR_LEN);
436 /* Set up phony MAC address. */
437 if_setlladdr(bifp, IF_LLADDR(ifp), ETHER_ADDR_LEN);
439 /* Save original input vector */
440 new->fec_if_input = bifp->if_input;
442 /* Override it with our own */
443 bifp->if_input = ng_fec_input;
445 /* Save output vector too. */
446 if (b->fec_if_output == NULL)
447 b->fec_if_output = bifp->if_output;
449 /* Add to the queue */
451 new->fec_ifstat = -1;
452 TAILQ_INSERT_TAIL(&b->ng_fec_ports, new, fec_list);
454 /* Add multicast addresses to this port. */
455 ng_fec_ether_cmdmulti(ifp, new, 1);
461 ng_fec_delport(struct ng_fec_private *priv, char *iface)
463 struct ng_fec_bundle *b;
464 struct ifnet *ifp, *bifp;
465 struct ng_fec_portlist *p;
467 if (priv == NULL || iface == NULL)
470 b = &priv->fec_bundle;
473 /* Only allow reconfiguration if not running. */
474 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
475 kprintf("fec%d: can't remove iface; bundle is running\n",
480 /* Find the interface */
481 bifp = ifunit(iface);
483 kprintf("fec%d: tried to remove iface %s, which "
484 "doesn't seem to exist\n", priv->unit, iface);
488 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
489 if (p->fec_if == bifp)
494 kprintf("fec%d: tried to remove iface %s which "
495 "is not in our bundle\n", priv->unit, iface);
500 bifp->if_flags &= ~IFF_UP;
501 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
503 /* Restore MAC address. */
504 if_setlladdr(bifp, (u_char *)&p->fec_mac, ETHER_ADDR_LEN);
506 /* Restore input vector */
507 bifp->if_input = p->fec_if_input;
509 /* Remove our node context pointer. */
510 IF_AFDATA_LOCK(bifp);
512 IF_AFDATA_UNLOCK(bifp);
515 TAILQ_REMOVE(&b->ng_fec_ports, p, fec_list);
516 kfree(p, M_NETGRAPH);
519 if (b->fec_ifcnt == 0)
520 b->fec_if_output = NULL;
526 ng_fec_ether_cmdmulti(struct ifnet *trifp, struct ng_fec_portlist *p, int set)
528 struct ifnet *ifp = p->fec_if;
529 struct ng_fec_mc *mc;
530 struct ifmultiaddr *ifma, *rifma = NULL;
531 struct sockaddr_dl sdl;
534 bzero((char *)&sdl, sizeof(sdl));
535 sdl.sdl_len = sizeof(sdl);
536 sdl.sdl_family = AF_LINK;
537 sdl.sdl_type = IFT_ETHER;
538 sdl.sdl_alen = ETHER_ADDR_LEN;
539 sdl.sdl_index = ifp->if_index;
542 TAILQ_FOREACH(ifma, &trifp->if_multiaddrs, ifma_link) {
543 if (ifma->ifma_addr->sa_family != AF_LINK)
545 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
546 LLADDR(&sdl), ETHER_ADDR_LEN);
548 error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
551 mc = kmalloc(sizeof(struct ng_fec_mc), M_DEVBUF, M_WAITOK | M_NULLOK);
555 SLIST_INSERT_HEAD(&p->fec_mc_head, mc, mc_entries);
558 while ((mc = SLIST_FIRST(&p->fec_mc_head)) != NULL) {
559 SLIST_REMOVE(&p->fec_mc_head, mc, ng_fec_mc, mc_entries);
560 if_delmulti_ifma(mc->mc_ifma);
568 ng_fec_ether_setmulti(struct ifnet *ifp)
570 struct ng_fec_private *priv;
571 struct ng_fec_bundle *b;
572 struct ng_fec_portlist *p;
574 priv = ifp->if_softc;
575 b = &priv->fec_bundle;
577 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
578 /* First, remove any existing filter entries. */
579 ng_fec_ether_cmdmulti(ifp, p, 0);
580 /* copy all addresses from the fec interface to the port */
581 ng_fec_ether_cmdmulti(ifp, p, 1);
587 * Pass an ioctl command down to all the underyling interfaces in a
588 * bundle. Used for setting flags.
592 ng_fec_setport(struct ifnet *ifp, u_long command, caddr_t data)
594 struct ng_fec_private *priv;
595 struct ng_fec_bundle *b;
597 struct ng_fec_portlist *p;
599 priv = ifp->if_softc;
600 b = &priv->fec_bundle;
602 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
605 (*oifp->if_ioctl)(oifp, command, data);
612 ng_fec_init(void *arg)
614 struct ng_fec_private *priv;
615 struct ng_fec_bundle *b;
616 struct ifnet *ifp, *bifp;
617 struct ng_fec_portlist *p;
621 b = &priv->fec_bundle;
623 if (b->fec_ifcnt != 2 && b->fec_ifcnt != FEC_BUNDLESIZ) {
624 kprintf("fec%d: invalid bundle "
625 "size: %d\n", priv->unit,
632 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
634 bifp->if_flags |= IFF_UP;
635 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
636 /* mark iface as up and let the monitor check it */
640 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
641 ifp->if_drv_flags |= IFF_DRV_RUNNING;
643 priv->fec_ch = timeout(ng_fec_tick, priv, hz);
649 ng_fec_stop(struct ifnet *ifp)
651 struct ng_fec_private *priv;
652 struct ng_fec_bundle *b;
654 struct ng_fec_portlist *p;
656 priv = ifp->if_softc;
657 b = &priv->fec_bundle;
659 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
661 bifp->if_flags &= ~IFF_UP;
662 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
665 untimeout(ng_fec_tick, priv, priv->fec_ch);
667 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
673 ng_fec_tick(void *arg)
675 struct ng_fec_private *priv;
676 struct ng_fec_bundle *b;
677 struct ifmediareq ifmr;
679 struct ng_fec_portlist *p;
683 b = &priv->fec_bundle;
685 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
686 bzero((char *)&ifmr, sizeof(ifmr));
688 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
690 kprintf("fec%d: failed to check status "
691 "of link %s\n", priv->unit, ifp->if_xname);
695 if (ifmr.ifm_status & IFM_AVALID) {
696 if (ifmr.ifm_status & IFM_ACTIVE) {
697 if (p->fec_ifstat == -1 ||
698 p->fec_ifstat == 0) {
700 kprintf("fec%d: port %s in bundle "
701 "is up\n", priv->unit,
705 if (p->fec_ifstat == -1 ||
706 p->fec_ifstat == 1) {
708 kprintf("fec%d: port %s in bundle "
709 "is down\n", priv->unit,
717 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
718 priv->fec_ch = timeout(ng_fec_tick, priv, hz);
724 ng_fec_ifmedia_upd(struct ifnet *ifp)
729 static void ng_fec_ifmedia_sts(struct ifnet *ifp,
730 struct ifmediareq *ifmr)
732 struct ng_fec_private *priv;
733 struct ng_fec_bundle *b;
734 struct ng_fec_portlist *p;
736 priv = ifp->if_softc;
737 b = &priv->fec_bundle;
739 ifmr->ifm_status = IFM_AVALID;
740 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
741 if (p->fec_ifstat == 1) {
742 ifmr->ifm_status |= IFM_ACTIVE;
751 * Process an ioctl for the virtual interface
754 ng_fec_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
756 struct ifreq *const ifr = (struct ifreq *) data;
758 struct ng_fec_private *priv;
759 struct ng_fec_bundle *b;
761 priv = ifp->if_softc;
762 b = &priv->fec_bundle;
765 ng_fec_print_ioctl(ifp, command, data);
770 /* These two are mostly handled at a higher layer */
773 error = ether_ioctl(ifp, command, data);
777 if (ifr->ifr_mtu >= NG_FEC_MTU_MIN &&
778 ifr->ifr_mtu <= NG_FEC_MTU_MAX) {
779 struct ng_fec_portlist *p;
782 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
784 error = (*bifp->if_ioctl)(bifp, SIOCSIFMTU,
790 ifp->if_mtu = ifr->ifr_mtu;
798 * If the interface is marked up and stopped, then start it.
799 * If it is marked down and running, then stop it.
801 if (ifr->ifr_flags & IFF_UP) {
802 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
804 if (b->fec_ifcnt != 2 &&
805 b->fec_ifcnt != FEC_BUNDLESIZ) {
806 kprintf("fec%d: invalid bundle "
807 "size: %d\n", priv->unit,
815 * Bubble down changes in promisc mode to
816 * underlying interfaces.
818 if ((ifp->if_flags & IFF_PROMISC) !=
819 (priv->if_flags & IFF_PROMISC)) {
820 ng_fec_setport(ifp, command, data);
821 priv->if_flags = ifp->if_flags;
824 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
831 ng_fec_ether_setmulti(ifp);
836 error = ifmedia_ioctl(ifp, ifr, &priv->ifmedia, command);
838 /* Stuff that's not supported */
852 * This routine spies on mbufs received by underlying network device
853 * drivers. When we add an interface to our bundle, we override its
854 * if_input routine with a pointer to ng_fec_input(). This means we
855 * get to look at all the device's packets before sending them to the
856 * real ether_input() for processing by the stack. Once we verify the
857 * packet comes from an interface that's been aggregated into
858 * our bundle, we fix up the rcvif pointer and increment our
859 * packet counters so that it looks like the frames are actually
863 ng_fec_input(struct ifnet *ifp, struct mbuf *m0)
865 struct ng_node *node;
866 struct ng_fec_private *priv;
867 struct ng_fec_bundle *b;
869 struct ng_fec_portlist *p;
872 if (ifp == NULL || m0 == NULL)
877 /* Sanity check part II */
881 priv = NG_NODE_PRIVATE(node);
882 b = &priv->fec_bundle;
885 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
886 if (p->fec_if == m0->m_pkthdr.rcvif)
890 /* Wasn't meant for us; leave this frame alone. */
895 * Check for a BPF tap on the underlying interface. This
896 * is mainly a debugging aid: it allows tcpdump-ing of an
897 * individual interface in a bundle to work, which it
898 * otherwise would not. BPF tapping of our own aggregate
899 * interface will occur once we call ether_input().
901 BPF_MTAP(m0->m_pkthdr.rcvif, m0);
903 /* Convince the system that this is our frame. */
904 m0->m_pkthdr.rcvif = bifp;
907 * Count bytes on an individual interface in a bundle.
908 * The bytes will also be added to the aggregate interface
909 * once we call ether_input().
911 ifp->if_ibytes += m0->m_pkthdr.len;
914 bifp->if_input(bifp, m0, NULL, -1);
920 * Take a quick peek at the packet and see if it's ok for us to use
921 * the inet or inet6 hash methods on it, if they're enabled. We do
922 * this by setting flags in the mbuf header. Once we've made up our
923 * mind what to do, we pass the frame to output vector for further
928 ng_fec_output(struct ifnet *ifp, struct mbuf *m,
929 struct sockaddr *dst, struct rtentry *rt0)
931 const priv_p priv = (priv_p) ifp->if_softc;
932 struct ng_fec_bundle *b;
935 /* Check interface flags */
936 if (!((ifp->if_flags & IFF_UP) &&
937 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
942 b = &priv->fec_bundle;
944 switch (b->fec_btype) {
946 m->m_flags |= M_FEC_MAC;
951 * We can't use the INET address port selection
952 * scheme if this isn't an INET packet.
954 if (dst->sa_family == AF_INET)
955 m->m_flags |= M_FEC_INET;
957 else if (dst->sa_family == AF_INET6)
958 m->m_flags |= M_FEC_INET6;
962 if_printf(ifp, "can't do inet aggregation of non "
965 m->m_flags |= M_FEC_MAC;
970 if_printf(ifp, "bogus hash type: %d\n",
978 * Pass the frame to the output vector for all the protocol
979 * handling. This will put the ethernet header on the packet
983 error = (*b->fec_if_output)(ifp, m, dst, rt0);
984 if (priv->if_error && !error)
985 error = priv->if_error;
991 * Apply a hash to the source and destination addresses in the packet
992 * in order to select an interface. Also check link status and handle
993 * dead links accordingly.
997 ng_fec_choose_port(struct ng_fec_bundle *b,
998 struct mbuf *m, struct ifnet **ifp)
1000 struct ether_header *eh;
1005 struct ip6_hdr *ip6;
1009 struct ng_fec_portlist *p;
1013 * If there are only two ports, mask off all but the
1014 * last bit for XORing. If there are 4, mask off all
1015 * but the last 2 bits.
1017 mask = b->fec_ifcnt == 2 ? 0x1 : 0x3;
1018 eh = mtod(m, struct ether_header *);
1020 ip = (struct ip *)(mtod(m, char *) +
1021 sizeof(struct ether_header));
1023 ip6 = (struct ip6_hdr *)(mtod(m, char *) +
1024 sizeof(struct ether_header));
1029 * The fg_fec_output() routine is supposed to leave a
1030 * flag for us in the mbuf that tells us what hash to
1031 * use, but sometimes a new mbuf is prepended to the
1032 * chain, so we have to search every mbuf in the chain
1033 * to find the flags.
1037 if (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6))
1044 switch (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6)) {
1046 port = (eh->ether_dhost[5] ^
1047 eh->ether_shost[5]) & mask;
1051 port = (ntohl(ip->ip_dst.s_addr) ^
1052 ntohl(ip->ip_src.s_addr)) & mask;
1056 port = (ip6->ip6_dst.s6_addr[15] ^
1057 ip6->ip6_dst.s6_addr[15]) & mask;
1066 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
1067 if (port == p->fec_idx)
1072 * Now that we've chosen a port, make sure it's
1073 * alive. If it's not alive, cycle through the bundle
1074 * looking for a port that is alive. If we don't find
1075 * any, return an error.
1077 if (p->fec_ifstat != 1) {
1078 struct ng_fec_portlist *n = NULL;
1080 n = TAILQ_NEXT(p, fec_list);
1082 n = TAILQ_FIRST(&b->ng_fec_ports);
1084 if (n->fec_ifstat == 1)
1086 n = TAILQ_NEXT(n, fec_list);
1088 n = TAILQ_FIRST(&b->ng_fec_ports);
1101 * Now that the packet has been run through ether_output(), yank it
1102 * off our own send queue and stick it on the queue for the appropriate
1103 * underlying physical interface. Note that if the interface's send
1104 * queue is full, we save an error status in our private netgraph
1105 * space which will eventually be handed up to ng_fec_output(), which
1106 * will return it to the rest of the IP stack. We need to do this
1107 * in order to duplicate the effect of ether_output() returning ENOBUFS
1108 * when it detects that an interface's send queue is full. There's no
1109 * other way to signal the error status from here since the if_start()
1110 * routine is spec'ed to return void.
1112 * Once the frame is queued, we call ether_output_frame() to initiate
1116 ng_fec_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1118 struct ng_fec_private *priv;
1119 struct ng_fec_bundle *b;
1120 struct ifnet *oifp = NULL;
1124 priv = ifp->if_softc;
1125 b = &priv->fec_bundle;
1127 m0 = ifq_dequeue(&ifp->if_snd);
1133 /* Queue up packet on the proper port. */
1134 error = ng_fec_choose_port(b, m0, &oifp);
1138 priv->if_error = ENOBUFS;
1143 ifnet_deserialize_tx(ifp, ifsq);
1144 error = ifq_dispatch(oifp, m0, NULL);
1145 ifnet_serialize_tx(ifp, ifsq);
1147 priv->if_error = error;
1152 * Display an ioctl to the virtual interface
1156 ng_fec_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
1160 switch (command & IOC_DIRMASK) {
1176 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
1181 IOCPARM_LEN(command));
1185 /************************************************************************
1187 ************************************************************************/
1190 * Constructor for a node
1193 ng_fec_constructor(node_p node)
1195 char ifname[NG_FEC_FEC_NAME_MAX + 1];
1198 const uint8_t eaddr[ETHER_ADDR_LEN] = {0, 0, 0, 0, 0, 0};
1199 struct ng_fec_bundle *b;
1202 /* Allocate node and interface private structures */
1203 priv = kmalloc(sizeof(*priv), M_NETGRAPH,
1204 M_WAITOK | M_NULLOK | M_ZERO);
1208 ifp = priv->ifp = if_alloc(IFT_ETHER);
1210 kfree(priv, M_NETGRAPH);
1213 b = &priv->fec_bundle;
1215 /* Link them together */
1216 ifp->if_softc = priv;
1218 /* Get an interface unit number */
1219 if ((error = ng_fec_get_unit(&priv->unit)) != 0) {
1221 kfree(priv, M_NETGRAPH);
1225 /* Link together node and private info */
1226 NG_NODE_SET_PRIVATE(node, priv);
1229 /* Initialize interface structure */
1230 if_initname(ifp, NG_FEC_FEC_NAME, priv->unit);
1231 ifp->if_start = ng_fec_start;
1232 ifp->if_ioctl = ng_fec_ioctl;
1233 ifp->if_init = ng_fec_init;
1234 ifp->if_watchdog = NULL;
1235 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
1236 ifp->if_mtu = NG_FEC_MTU_DEFAULT;
1237 ifp->if_flags = (IFF_SIMPLEX|IFF_BROADCAST|IFF_MULTICAST);
1238 ifp->if_addrlen = 0; /* XXX */
1239 ifp->if_hdrlen = 0; /* XXX */
1240 ifp->if_baudrate = 100000000; /* XXX */
1241 TAILQ_INIT(&ifp->if_addrhead); /* XXX useless - done in if_attach */
1243 /* Give this node the same name as the interface (if possible) */
1244 bzero(ifname, sizeof(ifname));
1245 strlcpy(ifname, ifp->if_xname, sizeof(ifname));
1246 if (ng_name_node(node, ifname) != 0)
1247 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
1249 /* Attach the interface */
1250 ether_ifattach(ifp, eaddr);
1251 callout_handle_init(&priv->fec_ch);
1253 /* Override output method with our own */
1254 ifp->if_output = ng_fec_output;
1256 TAILQ_INIT(&b->ng_fec_ports);
1259 ifmedia_init(&priv->ifmedia, 0,
1260 ng_fec_ifmedia_upd, ng_fec_ifmedia_sts);
1261 ifmedia_add(&priv->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
1262 ifmedia_set(&priv->ifmedia, IFM_ETHER|IFM_NONE);
1269 * Receive a control message
1272 ng_fec_rcvmsg(node_p node, item_p item, hook_p lasthook)
1274 const priv_p priv = NG_NODE_PRIVATE(node);
1275 struct ng_fec_bundle *b;
1276 struct ng_mesg *resp = NULL;
1277 struct ng_mesg *msg;
1281 NGI_GET_MSG(item, msg);
1282 b = &priv->fec_bundle;
1284 switch (msg->header.typecookie) {
1285 case NGM_FEC_COOKIE:
1286 switch (msg->header.cmd) {
1287 case NGM_FEC_ADD_IFACE:
1289 error = ng_fec_addport(priv, ifname);
1291 case NGM_FEC_DEL_IFACE:
1293 error = ng_fec_delport(priv, ifname);
1295 case NGM_FEC_SET_MODE_MAC:
1296 b->fec_btype = FEC_BTYPE_MAC;
1299 case NGM_FEC_SET_MODE_INET:
1300 b->fec_btype = FEC_BTYPE_INET;
1303 case NGM_FEC_SET_MODE_INET6:
1304 b->fec_btype = FEC_BTYPE_INET6;
1317 NG_RESPOND_MSG(error, node, item, resp);
1323 * Shutdown and remove the node and its associated interface.
1326 ng_fec_shutdown(node_p node)
1328 const priv_p priv = NG_NODE_PRIVATE(node);
1329 struct ng_fec_bundle *b;
1330 struct ng_fec_portlist *p;
1332 b = &priv->fec_bundle;
1333 ng_fec_stop(priv->ifp);
1335 while (!TAILQ_EMPTY(&b->ng_fec_ports)) {
1336 p = TAILQ_FIRST(&b->ng_fec_ports);
1337 ng_fec_ether_cmdmulti(priv->ifp, p, 0);
1338 ng_fec_delport(priv, p->fec_if->if_xname);
1341 ether_ifdetach(priv->ifp);
1342 if_free_type(priv->ifp, IFT_ETHER);
1343 ifmedia_removeall(&priv->ifmedia);
1344 ng_fec_free_unit(priv->unit);
1345 kfree(priv, M_NETGRAPH);
1346 NG_NODE_SET_PRIVATE(node, NULL);
1347 NG_NODE_UNREF(node);
1352 * Handle loading and unloading for this node type.
1355 ng_fec_mod_event(module_t mod, int event, void *data)
1361 mtx_init(&ng_fec_mtx, "ng_fec", NULL, MTX_DEF);
1364 mtx_destroy(&ng_fec_mtx);