4 * Copyright (c) 2001 Berkeley Software Design, Inc.
5 * Copyright (c) 2000, 2001
6 * Bill Paul <wpaul@osd.bsdi.com>. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Bill Paul.
19 * 4. Neither the name of the author nor the names of any co-contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 * THE POSSIBILITY OF SUCH DAMAGE.
35 * $FreeBSD: src/sys/netgraph/ng_fec.c,v 1.1.2.1 2002/11/01 21:39:31 julian Exp $
38 * Copyright (c) 1996-1999 Whistle Communications, Inc.
39 * All rights reserved.
41 * Subject to the following obligations and disclaimer of warranty, use and
42 * redistribution of this software, in source or object code forms, with or
43 * without modifications are expressly permitted by Whistle Communications;
44 * provided, however, that:
45 * 1. Any and all reproductions of the source or object code must include the
46 * copyright notice above and the following disclaimer of warranties; and
47 * 2. No rights are granted, in any manner or form, to use Whistle
48 * Communications, Inc. trademarks, including the mark "WHISTLE
49 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
50 * such appears in the above copyright notice or in the software.
52 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
53 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
54 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
55 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
56 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
57 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
58 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
59 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
60 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
61 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
62 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
63 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
64 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
65 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
66 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
67 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
70 * Author: Archie Cobbs <archie@freebsd.org>
72 * $Whistle: ng_fec.c,v 1.33 1999/11/01 09:24:51 julian Exp $
76 * This module implements ethernet channel bonding using the Cisco
77 * Fast EtherChannel mechanism. Two or four ports may be combined
78 * into a single aggregate interface.
80 * Interfaces are named fec0, fec1, etc. New nodes take the
81 * first available interface name.
83 * This node also includes Berkeley packet filter support.
85 * Note that this node doesn't need to connect to any other
86 * netgraph nodes in order to do its work.
89 #include <sys/param.h>
90 #include <sys/systm.h>
91 #include <sys/errno.h>
92 #include <sys/kernel.h>
93 #include <sys/malloc.h>
95 #include <sys/errno.h>
96 #include <sys/sockio.h>
97 #include <sys/socket.h>
98 #include <sys/syslog.h>
99 #include <sys/libkern.h>
100 #include <sys/queue.h>
103 #include <net/if_types.h>
104 #include <net/if_arp.h>
105 #include <net/if_dl.h>
106 #include <net/if_media.h>
107 #include <net/intrq.h>
109 #include <net/ethernet.h>
111 #include "opt_inet.h"
112 #include "opt_inet6.h"
114 #include <netinet/in.h>
116 #include <netinet/in_systm.h>
117 #include <netinet/ip.h>
121 #include <netinet/ip6.h>
124 #include <netgraph/ng_message.h>
125 #include <netgraph/netgraph.h>
126 #include <netgraph/ng_parse.h>
127 #include <netgraph/ng_fec.h>
129 #define IFP2NG(ifp) ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph)
130 #define FEC_INC(x, y) (x) = (x + 1) % y
133 * Current fast etherchannel implementations use either 2 or 4
134 * ports, so for now we limit the maximum bundle size to 4 interfaces.
136 #define FEC_BUNDLESIZ 4
138 struct ng_fec_portlist {
139 struct ifnet *fec_if;
142 struct ether_addr fec_mac;
143 TAILQ_ENTRY(ng_fec_portlist) fec_list;
146 struct ng_fec_bundle {
147 TAILQ_HEAD(,ng_fec_portlist) ng_fec_ports;
152 #define FEC_BTYPE_MAC 0x01
153 #define FEC_BTYPE_INET 0x02
154 #define FEC_BTYPE_INET6 0x03
156 /* Node private data */
157 struct ng_fec_private {
158 struct arpcom arpcom;
159 struct ifmedia ifmedia;
161 int if_error; /* XXX */
162 int unit; /* Interface unit number */
163 node_p node; /* Our netgraph node */
164 struct ng_fec_bundle fec_bundle;/* Aggregate bundle */
165 struct callout_handle fec_ch; /* callout handle for ticker */
167 typedef struct ng_fec_private *priv_p;
169 /* Interface methods */
170 static void ng_fec_input(struct ifnet *, struct mbuf **,
171 struct ether_header *);
172 static void ng_fec_start(struct ifnet *ifp);
173 static int ng_fec_choose_port(struct ng_fec_bundle *b,
174 struct mbuf *m, struct ifnet **ifp);
175 static int ng_fec_setport(struct ifnet *ifp, u_long cmd, caddr_t data);
176 static void ng_fec_init(void *arg);
177 static void ng_fec_stop(struct ifnet *ifp);
178 static int ng_fec_ifmedia_upd(struct ifnet *ifp);
179 static void ng_fec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
180 static int ng_fec_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
181 static int ng_fec_output(struct ifnet *ifp, struct mbuf *m0,
182 struct sockaddr *dst, struct rtentry *rt0);
183 static void ng_fec_tick(void *arg);
184 static int ng_fec_addport(struct ng_fec_private *priv, char *iface);
185 static int ng_fec_delport(struct ng_fec_private *priv, char *iface);
188 static void ng_fec_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
191 /* Netgraph methods */
192 static ng_constructor_t ng_fec_constructor;
193 static ng_rcvmsg_t ng_fec_rcvmsg;
194 static ng_shutdown_t ng_fec_rmnode;
196 /* List of commands and how to convert arguments to/from ASCII */
197 static const struct ng_cmdlist ng_fec_cmds[] = {
202 &ng_parse_string_type,
209 &ng_parse_string_type,
214 NGM_FEC_SET_MODE_MAC,
221 NGM_FEC_SET_MODE_INET,
229 /* Node type descriptor */
230 static struct ng_type typestruct = {
245 NETGRAPH_INIT(fec, &typestruct);
247 /* We keep a bitmap indicating which unit numbers are free.
248 One means the unit number is free, zero means it's taken. */
249 static int *ng_fec_units = NULL;
250 static int ng_fec_units_len = 0;
251 static int ng_units_in_use = 0;
253 #define UNITS_BITSPERWORD (sizeof(*ng_fec_units) * NBBY)
256 * Find the first free unit number for a new interface.
257 * Increase the size of the unit bitmap as necessary.
259 static __inline__ int
260 ng_fec_get_unit(int *unit)
264 for (index = 0; index < ng_fec_units_len
265 && ng_fec_units[index] == 0; index++);
266 if (index == ng_fec_units_len) { /* extend array */
267 int i, *newarray, newlen;
269 newlen = (2 * ng_fec_units_len) + 4;
270 MALLOC(newarray, int *, newlen * sizeof(*ng_fec_units),
271 M_NETGRAPH, M_NOWAIT);
272 if (newarray == NULL)
274 bcopy(ng_fec_units, newarray,
275 ng_fec_units_len * sizeof(*ng_fec_units));
276 for (i = ng_fec_units_len; i < newlen; i++)
278 if (ng_fec_units != NULL)
279 FREE(ng_fec_units, M_NETGRAPH);
280 ng_fec_units = newarray;
281 ng_fec_units_len = newlen;
283 bit = ffs(ng_fec_units[index]) - 1;
284 KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
285 ("%s: word=%d bit=%d", __FUNCTION__, ng_fec_units[index], bit));
286 ng_fec_units[index] &= ~(1 << bit);
287 *unit = (index * UNITS_BITSPERWORD) + bit;
293 * Free a no longer needed unit number.
295 static __inline__ void
296 ng_fec_free_unit(int unit)
300 index = unit / UNITS_BITSPERWORD;
301 bit = unit % UNITS_BITSPERWORD;
302 KASSERT(index < ng_fec_units_len,
303 ("%s: unit=%d len=%d", __FUNCTION__, unit, ng_fec_units_len));
304 KASSERT((ng_fec_units[index] & (1 << bit)) == 0,
305 ("%s: unit=%d is free", __FUNCTION__, unit));
306 ng_fec_units[index] |= (1 << bit);
308 * XXX We could think about reducing the size of ng_fec_units[]
309 * XXX here if the last portion is all ones
310 * XXX At least free it if no more units.
311 * Needed if we are eventually be able to unload.
314 if (ng_units_in_use == 0) { /* XXX make SMP safe */
315 FREE(ng_fec_units, M_NETGRAPH);
316 ng_fec_units_len = 0;
321 /************************************************************************
323 ************************************************************************/
326 ng_fec_addport(struct ng_fec_private *priv, char *iface)
328 struct ng_fec_bundle *b;
329 struct ifnet *ifp, *bifp;
332 struct sockaddr_dl *sdl;
333 struct ng_fec_portlist *p, *new;
335 if (priv == NULL || iface == NULL)
338 b = &priv->fec_bundle;
339 ifp = &priv->arpcom.ac_if;
341 /* Find the interface */
342 bifp = ifunit(iface);
344 printf("fec%d: tried to add iface %s, which "
345 "doesn't seem to exist\n", priv->unit, iface);
349 /* See if we have room in the bundle */
350 if (b->fec_ifcnt == FEC_BUNDLESIZ) {
351 printf("fec%d: can't add new iface; bundle is full\n",
356 /* See if the interface is already in the bundle */
357 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
358 if (p->fec_if == bifp) {
359 printf("fec%d: iface %s is already in this "
360 "bundle\n", priv->unit, iface);
365 /* Allocate new list entry. */
366 MALLOC(new, struct ng_fec_portlist *,
367 sizeof(struct ng_fec_portlist), M_NETGRAPH, M_NOWAIT);
371 ac = (struct arpcom *)bifp;
372 ac->ac_netgraph = priv->node;
375 * If this is the first interface added to the bundle,
376 * use its MAC address for the virtual interface (and,
377 * by extension, all the other ports in the bundle).
379 if (b->fec_ifcnt == 0) {
380 ifa = ifnet_addrs[ifp->if_index - 1];
381 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
382 bcopy((char *)ac->ac_enaddr,
383 priv->arpcom.ac_enaddr, ETHER_ADDR_LEN);
384 bcopy((char *)ac->ac_enaddr,
385 LLADDR(sdl), ETHER_ADDR_LEN);
388 b->fec_btype = FEC_BTYPE_MAC;
389 new->fec_idx = b->fec_ifcnt;
392 /* Save the real MAC address. */
393 bcopy((char *)ac->ac_enaddr,
394 (char *)&new->fec_mac, ETHER_ADDR_LEN);
396 /* Set up phony MAC address. */
397 ifa = ifnet_addrs[bifp->if_index - 1];
398 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
399 bcopy(priv->arpcom.ac_enaddr, ac->ac_enaddr, ETHER_ADDR_LEN);
400 bcopy(priv->arpcom.ac_enaddr, LLADDR(sdl), ETHER_ADDR_LEN);
402 /* Add to the queue */
404 TAILQ_INSERT_TAIL(&b->ng_fec_ports, new, fec_list);
410 ng_fec_delport(struct ng_fec_private *priv, char *iface)
412 struct ng_fec_bundle *b;
413 struct ifnet *ifp, *bifp;
416 struct sockaddr_dl *sdl;
417 struct ng_fec_portlist *p;
419 if (priv == NULL || iface == NULL)
422 b = &priv->fec_bundle;
423 ifp = &priv->arpcom.ac_if;
425 /* Find the interface */
426 bifp = ifunit(iface);
428 printf("fec%d: tried to remove iface %s, which "
429 "doesn't seem to exist\n", priv->unit, iface);
433 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
434 if (p->fec_if == bifp)
439 printf("fec%d: tried to remove iface %s which "
440 "is not in our bundle\n", priv->unit, iface);
445 bifp->if_flags &= ~IFF_UP;
446 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
448 /* Restore MAC address. */
449 ac = (struct arpcom *)bifp;
450 ifa = ifnet_addrs[bifp->if_index - 1];
451 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
452 bcopy((char *)&p->fec_mac, ac->ac_enaddr, ETHER_ADDR_LEN);
453 bcopy((char *)&p->fec_mac, LLADDR(sdl), ETHER_ADDR_LEN);
456 TAILQ_REMOVE(&b->ng_fec_ports, p, fec_list);
464 * Pass an ioctl command down to all the underyling interfaces in a
465 * bundle. Used for setting multicast filters and flags.
469 ng_fec_setport(struct ifnet *ifp, u_long command, caddr_t data)
471 struct ng_fec_private *priv;
472 struct ng_fec_bundle *b;
474 struct ng_fec_portlist *p;
476 priv = ifp->if_softc;
477 b = &priv->fec_bundle;
479 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
482 (*oifp->if_ioctl)(oifp, command, data);
489 ng_fec_init(void *arg)
491 struct ng_fec_private *priv;
492 struct ng_fec_bundle *b;
493 struct ifnet *ifp, *bifp;
494 struct ng_fec_portlist *p;
497 priv = ifp->if_softc;
498 b = &priv->fec_bundle;
500 if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
501 printf("fec%d: invalid bundle "
502 "size: %d\n", priv->unit,
509 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
511 bifp->if_flags |= IFF_UP;
512 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
513 /* mark iface as up and let the monitor check it */
517 priv->fec_ch = timeout(ng_fec_tick, priv, hz);
523 ng_fec_stop(struct ifnet *ifp)
525 struct ng_fec_private *priv;
526 struct ng_fec_bundle *b;
528 struct ng_fec_portlist *p;
530 priv = ifp->if_softc;
531 b = &priv->fec_bundle;
533 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
535 bifp->if_flags &= ~IFF_UP;
536 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
539 untimeout(ng_fec_tick, priv, priv->fec_ch);
545 ng_fec_tick(void *arg)
547 struct ng_fec_private *priv;
548 struct ng_fec_bundle *b;
549 struct ifmediareq ifmr;
551 struct ng_fec_portlist *p;
555 b = &priv->fec_bundle;
557 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
558 bzero((char *)&ifmr, sizeof(ifmr));
560 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
562 printf("fec%d: failed to check status "
563 "of link %s%d\n", priv->unit, ifp->if_name,
568 if (ifmr.ifm_status & IFM_AVALID &&
569 IFM_TYPE(ifmr.ifm_active) == IFM_ETHER) {
570 if (ifmr.ifm_status & IFM_ACTIVE) {
571 if (p->fec_ifstat == -1 ||
572 p->fec_ifstat == 0) {
574 printf("fec%d: port %s%d in bundle "
575 "is up\n", priv->unit,
576 ifp->if_name, ifp->if_unit);
579 if (p->fec_ifstat == -1 ||
580 p->fec_ifstat == 1) {
582 printf("fec%d: port %s%d in bundle "
583 "is down\n", priv->unit,
584 ifp->if_name, ifp->if_unit);
590 ifp = &priv->arpcom.ac_if;
591 if (ifp->if_flags & IFF_RUNNING)
592 priv->fec_ch = timeout(ng_fec_tick, priv, hz);
598 ng_fec_ifmedia_upd(struct ifnet *ifp)
603 static void ng_fec_ifmedia_sts(struct ifnet *ifp,
604 struct ifmediareq *ifmr)
606 struct ng_fec_private *priv;
607 struct ng_fec_bundle *b;
608 struct ng_fec_portlist *p;
610 priv = ifp->if_softc;
611 b = &priv->fec_bundle;
613 ifmr->ifm_status = IFM_AVALID;
614 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
616 ifmr->ifm_status |= IFM_ACTIVE;
625 * Process an ioctl for the virtual interface
628 ng_fec_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
630 struct ifreq *const ifr = (struct ifreq *) data;
632 struct ng_fec_private *priv;
633 struct ng_fec_bundle *b;
635 priv = ifp->if_softc;
636 b = &priv->fec_bundle;
639 ng_fec_print_ioctl(ifp, command, data);
644 /* These two are mostly handled at a higher layer */
648 error = ether_ioctl(ifp, command, data);
654 * If the interface is marked up and stopped, then start it.
655 * If it is marked down and running, then stop it.
657 if (ifr->ifr_flags & IFF_UP) {
658 if (!(ifp->if_flags & IFF_RUNNING)) {
660 if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
661 printf("fec%d: invalid bundle "
662 "size: %d\n", priv->unit,
667 ifp->if_flags &= ~(IFF_OACTIVE);
668 ifp->if_flags |= IFF_RUNNING;
672 * Bubble down changes in promisc mode to
673 * underlying interfaces.
675 if ((ifp->if_flags & IFF_PROMISC) !=
676 (priv->if_flags & IFF_PROMISC)) {
677 ng_fec_setport(ifp, command, data);
678 priv->if_flags = ifp->if_flags;
681 if (ifp->if_flags & IFF_RUNNING)
682 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
689 ng_fec_setport(ifp, command, data);
694 error = ifmedia_ioctl(ifp, ifr, &priv->ifmedia, command);
696 /* Stuff that's not supported */
710 * This routine spies on mbufs passing through ether_input(). If
711 * they come from one of the interfaces that are aggregated into
712 * our bundle, we fix up the ifnet pointer and increment our
713 * packet counters so that it looks like the frames are actually
717 ng_fec_input(struct ifnet *ifp, struct mbuf **m0,
718 struct ether_header *eh)
720 struct ng_node *node;
721 struct ng_fec_private *priv;
722 struct ng_fec_bundle *b;
725 struct ng_fec_portlist *p;
728 if (ifp == NULL || m0 == NULL || eh == NULL)
733 /* Sanity check part II */
737 priv = node->private;
738 b = &priv->fec_bundle;
739 bifp = &priv->arpcom.ac_if;
742 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
743 if (p->fec_if == m->m_pkthdr.rcvif)
747 /* Wasn't meant for us; leave this frame alone. */
751 /* Pretend this is our frame. */
752 m->m_pkthdr.rcvif = bifp;
754 bifp->if_ibytes += m->m_pkthdr.len + sizeof(struct ether_header);
756 /* Check for a BPF tap */
757 if (bifp->if_bpf != NULL) {
760 /* This kludge is OK; BPF treats the "mbuf" as read-only */
762 mh.mh_data = (char *)eh;
763 mh.mh_len = ETHER_HDR_LEN;
764 bpf_mtap(bifp, (struct mbuf *)&mh);
771 * Take a quick peek at the packet and see if it's ok for us to use
772 * the inet or inet6 hash methods on it, if they're enabled. We do
773 * this by setting flags in the mbuf header. Once we've made up our
774 * mind what to do, we pass the frame to ether_output() for further
779 ng_fec_output(struct ifnet *ifp, struct mbuf *m,
780 struct sockaddr *dst, struct rtentry *rt0)
782 const priv_p priv = (priv_p) ifp->if_softc;
783 struct ng_fec_bundle *b;
786 /* Check interface flags */
787 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
792 b = &priv->fec_bundle;
794 switch (b->fec_btype) {
796 m->m_flags |= M_FEC_MAC;
801 * We can't use the INET address port selection
802 * scheme if this isn't an INET packet.
804 if (dst->sa_family == AF_INET)
805 m->m_flags |= M_FEC_INET;
807 else if (dst->sa_family == AF_INET6)
808 m->m_flags |= M_FEC_INET6;
812 printf("fec%d: can't do inet aggregation of non "
813 "inet packet\n", ifp->if_unit);
815 m->m_flags |= M_FEC_MAC;
820 printf("fec%d: bogus hash type: %d\n", ifp->if_unit,
828 * Pass the frame to ether_output() for all the protocol
829 * handling. This will put the ethernet header on the packet
833 error = ether_output(ifp, m, dst, rt0);
834 if (priv->if_error && !error)
835 error = priv->if_error;
841 * Apply a hash to the source and destination addresses in the packet
842 * in order to select an interface. Also check link status and handle
843 * dead links accordingly.
847 ng_fec_choose_port(struct ng_fec_bundle *b,
848 struct mbuf *m, struct ifnet **ifp)
850 struct ether_header *eh;
859 struct ng_fec_portlist *p;
863 * If there are only two ports, mask off all but the
864 * last bit for XORing. If there are 4, mask off all
865 * but the last 2 bits.
867 mask = b->fec_ifcnt == 2 ? 0x1 : 0x3;
868 eh = mtod(m, struct ether_header *);
870 ip = (struct ip *)(mtod(m, char *) +
871 sizeof(struct ether_header));
873 ip6 = (struct ip6_hdr *)(mtod(m, char *) +
874 sizeof(struct ether_header));
879 * The fg_fec_output() routine is supposed to leave a
880 * flag for us in the mbuf that tells us what hash to
881 * use, but sometimes a new mbuf is prepended to the
882 * chain, so we have to search every mbuf in the chain
887 if (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6))
894 switch (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6)) {
896 port = (eh->ether_dhost[5] ^
897 eh->ether_shost[5]) & mask;
901 port = (ntohl(ip->ip_dst.s_addr) ^
902 ntohl(ip->ip_src.s_addr)) & mask;
906 port = (ip6->ip6_dst.s6_addr[15] ^
907 ip6->ip6_dst.s6_addr[15]) & mask;
916 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
917 if (port == p->fec_idx)
922 * Now that we've chosen a port, make sure it's
923 * alive. If it's not alive, cycle through the bundle
924 * looking for a port that is alive. If we don't find
925 * any, return an error.
927 if (p->fec_ifstat != 1) {
928 struct ng_fec_portlist *n = NULL;
930 n = TAILQ_NEXT(p, fec_list);
932 n = TAILQ_FIRST(&b->ng_fec_ports);
934 if (n->fec_ifstat == 1)
936 n = TAILQ_NEXT(n, fec_list);
938 n = TAILQ_FIRST(&b->ng_fec_ports);
951 * Now that the packet has been run through ether_output(), yank it
952 * off our own send queue and stick it on the queue for the appropriate
953 * underlying physical interface. Note that if the interface's send
954 * queue is full, we save an error status in our private netgraph
955 * space which will eventually be handed up to ng_fec_output(), which
956 * will return it to the rest of the IP stack. We need to do this
957 * in order to duplicate the effect of ether_output() returning ENOBUFS
958 * when it detects that an interface's send queue is full. There's no
959 * other way to signal the error status from here since the if_start()
960 * routine is spec'ed to return void.
962 * Once the frame is queued, we call ether_output_frame() to initiate
966 ng_fec_start(struct ifnet *ifp)
968 struct ng_fec_private *priv;
969 struct ng_fec_bundle *b;
970 struct ifnet *oifp = NULL;
974 priv = ifp->if_softc;
975 b = &priv->fec_bundle;
977 IF_DEQUEUE(&ifp->if_snd, m0);
984 /* Queue up packet on the proper port. */
985 error = ng_fec_choose_port(b, m0, &oifp);
989 priv->if_error = ENOBUFS;
994 priv->if_error = ether_output_frame(oifp, m0);
1000 * Display an ioctl to the virtual interface
1004 ng_fec_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
1008 switch (command & IOC_DIRMASK) {
1024 log(LOG_DEBUG, "%s%d: %s('%c', %d, char[%d])\n",
1025 ifp->if_name, ifp->if_unit,
1029 IOCPARM_LEN(command));
1033 /************************************************************************
1035 ************************************************************************/
1038 * Constructor for a node
1041 ng_fec_constructor(node_p *nodep)
1043 char ifname[NG_FEC_FEC_NAME_MAX + 1];
1047 struct ng_fec_bundle *b;
1050 /* Allocate node and interface private structures */
1051 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT);
1054 bzero(priv, sizeof(*priv));
1056 ifp = &priv->arpcom.ac_if;
1057 b = &priv->fec_bundle;
1059 /* Link them together */
1060 ifp->if_softc = priv;
1062 /* Get an interface unit number */
1063 if ((error = ng_fec_get_unit(&priv->unit)) != 0) {
1064 FREE(ifp, M_NETGRAPH);
1065 FREE(priv, M_NETGRAPH);
1069 /* Call generic node constructor */
1070 if ((error = ng_make_node_common(&typestruct, nodep)) != 0) {
1071 ng_fec_free_unit(priv->unit);
1072 FREE(ifp, M_NETGRAPH);
1073 FREE(priv, M_NETGRAPH);
1078 /* Link together node and private info */
1079 node->private = priv;
1081 priv->arpcom.ac_netgraph = node;
1083 /* Initialize interface structure */
1084 ifp->if_name = NG_FEC_FEC_NAME;
1085 ifp->if_unit = priv->unit;
1086 ifp->if_output = ng_fec_output;
1087 ifp->if_start = ng_fec_start;
1088 ifp->if_ioctl = ng_fec_ioctl;
1089 ifp->if_init = ng_fec_init;
1090 ifp->if_watchdog = NULL;
1091 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
1092 ifp->if_mtu = NG_FEC_MTU_DEFAULT;
1093 ifp->if_flags = (IFF_SIMPLEX|IFF_BROADCAST|IFF_MULTICAST);
1094 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
1095 ifp->if_addrlen = 0; /* XXX */
1096 ifp->if_hdrlen = 0; /* XXX */
1097 ifp->if_baudrate = 100000000; /* XXX */
1098 TAILQ_INIT(&ifp->if_addrhead);
1100 /* Give this node the same name as the interface (if possible) */
1101 bzero(ifname, sizeof(ifname));
1102 snprintf(ifname, sizeof(ifname), "%s%d", ifp->if_name, ifp->if_unit);
1103 if (ng_name_node(node, ifname) != 0)
1104 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
1106 /* Grab hold of the ether_input pipe. */
1107 if (ng_ether_input_p == NULL)
1108 ng_ether_input_p = ng_fec_input;
1110 /* Attach the interface */
1111 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
1112 callout_handle_init(&priv->fec_ch);
1114 TAILQ_INIT(&b->ng_fec_ports);
1117 ifmedia_init(&priv->ifmedia, 0,
1118 ng_fec_ifmedia_upd, ng_fec_ifmedia_sts);
1119 ifmedia_add(&priv->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
1120 ifmedia_set(&priv->ifmedia, IFM_ETHER|IFM_NONE);
1127 * Receive a control message
1130 ng_fec_rcvmsg(node_p node, struct ng_mesg *msg,
1131 const char *retaddr, struct ng_mesg **rptr)
1133 const priv_p priv = node->private;
1134 struct ng_fec_bundle *b;
1135 struct ng_mesg *resp = NULL;
1139 b = &priv->fec_bundle;
1141 switch (msg->header.typecookie) {
1142 case NGM_FEC_COOKIE:
1143 switch (msg->header.cmd) {
1144 case NGM_FEC_ADD_IFACE:
1146 error = ng_fec_addport(priv, ifname);
1148 case NGM_FEC_DEL_IFACE:
1150 error = ng_fec_delport(priv, ifname);
1152 case NGM_FEC_SET_MODE_MAC:
1153 b->fec_btype = FEC_BTYPE_MAC;
1156 case NGM_FEC_SET_MODE_INET:
1157 b->fec_btype = FEC_BTYPE_INET;
1160 case NGM_FEC_SET_MODE_INET6:
1161 b->fec_btype = FEC_BTYPE_INET6;
1177 FREE(resp, M_NETGRAPH);
1178 FREE(msg, M_NETGRAPH);
1183 * Shutdown and remove the node and its associated interface.
1186 ng_fec_rmnode(node_p node)
1188 const priv_p priv = node->private;
1189 struct ng_fec_bundle *b;
1190 struct ng_fec_portlist *p;
1191 char ifname[IFNAMSIZ];
1193 b = &priv->fec_bundle;
1194 ng_fec_stop(&priv->arpcom.ac_if);
1196 while (!TAILQ_EMPTY(&b->ng_fec_ports)) {
1197 p = TAILQ_FIRST(&b->ng_fec_ports);
1198 sprintf(ifname, "%s%d",
1200 p->fec_if->if_unit);
1201 ng_fec_delport(priv, ifname);
1206 if (ng_ether_input_p != NULL)
1207 ng_ether_input_p = NULL;
1208 ether_ifdetach(&priv->arpcom.ac_if, ETHER_BPF_SUPPORTED);
1209 ifmedia_removeall(&priv->ifmedia);
1210 ng_fec_free_unit(priv->unit);
1211 FREE(priv, M_NETGRAPH);
1212 node->private = NULL;