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>
101 #include <sys/thread2.h>
104 #include <net/if_types.h>
105 #include <net/if_arp.h>
106 #include <net/if_dl.h>
107 #include <net/if_media.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>
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 fec_timeout; /* callout for ticker */
166 int (*real_if_output)(struct ifnet *, struct mbuf *,
167 struct sockaddr *, struct rtentry *);
169 typedef struct ng_fec_private *priv_p;
171 /* Interface methods */
172 static void ng_fec_input(struct ifnet *, struct mbuf **);
173 static void ng_fec_start(struct ifnet *ifp);
174 static int ng_fec_choose_port(struct ng_fec_bundle *b,
175 struct mbuf *m, struct ifnet **ifp);
176 static int ng_fec_setport(struct ifnet *ifp, u_long cmd, caddr_t data);
177 static void ng_fec_init(void *arg);
178 static void ng_fec_stop(struct ifnet *ifp);
179 static int ng_fec_ifmedia_upd(struct ifnet *ifp);
180 static void ng_fec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
181 static int ng_fec_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,
183 static int ng_fec_output(struct ifnet *ifp, struct mbuf *m0,
184 struct sockaddr *dst, struct rtentry *rt0);
185 static void ng_fec_tick(void *arg);
186 static int ng_fec_addport(struct ng_fec_private *priv, char *iface);
187 static int ng_fec_delport(struct ng_fec_private *priv, char *iface);
190 static void ng_fec_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
193 /* Netgraph methods */
194 static ng_constructor_t ng_fec_constructor;
195 static ng_rcvmsg_t ng_fec_rcvmsg;
196 static ng_shutdown_t ng_fec_rmnode;
198 /* List of commands and how to convert arguments to/from ASCII */
199 static const struct ng_cmdlist ng_fec_cmds[] = {
204 &ng_parse_string_type,
211 &ng_parse_string_type,
216 NGM_FEC_SET_MODE_MAC,
223 NGM_FEC_SET_MODE_INET,
231 /* Node type descriptor */
232 static struct ng_type typestruct = {
247 NETGRAPH_INIT(fec, &typestruct);
249 /* We keep a bitmap indicating which unit numbers are free.
250 One means the unit number is free, zero means it's taken. */
251 static int *ng_fec_units = NULL;
252 static int ng_fec_units_len = 0;
253 static int ng_units_in_use = 0;
255 #define UNITS_BITSPERWORD (sizeof(*ng_fec_units) * NBBY)
258 * Find the first free unit number for a new interface.
259 * Increase the size of the unit bitmap as necessary.
261 static __inline__ int
262 ng_fec_get_unit(int *unit)
266 for (index = 0; index < ng_fec_units_len
267 && ng_fec_units[index] == 0; index++);
268 if (index == ng_fec_units_len) { /* extend array */
269 int i, *newarray, newlen;
271 newlen = (2 * ng_fec_units_len) + 4;
272 newarray = kmalloc(newlen * sizeof(*ng_fec_units),
273 M_NETGRAPH, M_NOWAIT);
274 if (newarray == NULL)
276 bcopy(ng_fec_units, newarray,
277 ng_fec_units_len * sizeof(*ng_fec_units));
278 for (i = ng_fec_units_len; i < newlen; i++)
280 if (ng_fec_units != NULL)
281 kfree(ng_fec_units, M_NETGRAPH);
282 ng_fec_units = newarray;
283 ng_fec_units_len = newlen;
285 bit = ffs(ng_fec_units[index]) - 1;
286 KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
287 ("%s: word=%d bit=%d", __func__, ng_fec_units[index], bit));
288 ng_fec_units[index] &= ~(1 << bit);
289 *unit = (index * UNITS_BITSPERWORD) + bit;
295 * Free a no longer needed unit number.
297 static __inline__ void
298 ng_fec_free_unit(int unit)
302 index = unit / UNITS_BITSPERWORD;
303 bit = unit % UNITS_BITSPERWORD;
304 KASSERT(index < ng_fec_units_len,
305 ("%s: unit=%d len=%d", __func__, unit, ng_fec_units_len));
306 KASSERT((ng_fec_units[index] & (1 << bit)) == 0,
307 ("%s: unit=%d is free", __func__, unit));
308 ng_fec_units[index] |= (1 << bit);
310 * XXX We could think about reducing the size of ng_fec_units[]
311 * XXX here if the last portion is all ones
312 * XXX At least free it if no more units.
313 * Needed if we are eventually be able to unload.
316 if (ng_units_in_use == 0) { /* XXX make SMP safe */
317 kfree(ng_fec_units, M_NETGRAPH);
318 ng_fec_units_len = 0;
323 /************************************************************************
325 ************************************************************************/
328 ng_fec_addport(struct ng_fec_private *priv, char *iface)
330 struct ng_fec_bundle *b;
331 struct ifnet *ifp, *bifp;
333 struct sockaddr_dl *sdl;
334 struct ng_fec_portlist *p, *new;
336 if (priv == NULL || iface == NULL)
339 b = &priv->fec_bundle;
340 ifp = &priv->arpcom.ac_if;
342 /* Find the interface */
343 bifp = ifunit(iface);
345 kprintf("fec%d: tried to add iface %s, which "
346 "doesn't seem to exist\n", priv->unit, iface);
350 /* See if we have room in the bundle */
351 if (b->fec_ifcnt == FEC_BUNDLESIZ) {
352 kprintf("fec%d: can't add new iface; bundle is full\n",
357 /* See if the interface is already in the bundle */
358 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
359 if (p->fec_if == bifp) {
360 kprintf("fec%d: iface %s is already in this "
361 "bundle\n", priv->unit, iface);
366 /* Allocate new list entry. */
367 new = kmalloc(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 sdl = IF_LLSOCKADDR(ifp);
381 bcopy((char *)ac->ac_enaddr,
382 priv->arpcom.ac_enaddr, ETHER_ADDR_LEN);
383 bcopy((char *)ac->ac_enaddr,
384 LLADDR(sdl), ETHER_ADDR_LEN);
387 b->fec_btype = FEC_BTYPE_MAC;
388 new->fec_idx = b->fec_ifcnt;
391 /* Save the real MAC address. */
392 bcopy((char *)ac->ac_enaddr,
393 (char *)&new->fec_mac, ETHER_ADDR_LEN);
395 /* Set up phony MAC address. */
396 sdl = IF_LLSOCKADDR(bifp);
397 bcopy(priv->arpcom.ac_enaddr, ac->ac_enaddr, ETHER_ADDR_LEN);
398 bcopy(priv->arpcom.ac_enaddr, LLADDR(sdl), ETHER_ADDR_LEN);
400 /* Add to the queue */
402 TAILQ_INSERT_TAIL(&b->ng_fec_ports, new, fec_list);
408 ng_fec_delport(struct ng_fec_private *priv, char *iface)
410 struct ng_fec_bundle *b;
411 struct ifnet *ifp, *bifp;
413 struct sockaddr_dl *sdl;
414 struct ng_fec_portlist *p;
416 if (priv == NULL || iface == NULL)
419 b = &priv->fec_bundle;
420 ifp = &priv->arpcom.ac_if;
422 /* Find the interface */
423 bifp = ifunit(iface);
425 kprintf("fec%d: tried to remove iface %s, which "
426 "doesn't seem to exist\n", priv->unit, iface);
430 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
431 if (p->fec_if == bifp)
436 kprintf("fec%d: tried to remove iface %s which "
437 "is not in our bundle\n", priv->unit, iface);
442 bifp->if_flags &= ~IFF_UP;
443 bifp->if_ioctl(bifp, SIOCSIFFLAGS, NULL, NULL);
445 /* Restore MAC address. */
446 ac = (struct arpcom *)bifp;
447 sdl = IF_LLSOCKADDR(bifp);
448 bcopy((char *)&p->fec_mac, ac->ac_enaddr, ETHER_ADDR_LEN);
449 bcopy((char *)&p->fec_mac, LLADDR(sdl), ETHER_ADDR_LEN);
452 TAILQ_REMOVE(&b->ng_fec_ports, p, fec_list);
453 kfree(p, M_NETGRAPH);
460 * Pass an ioctl command down to all the underyling interfaces in a
461 * bundle. Used for setting multicast filters and flags.
464 ng_fec_setport(struct ifnet *ifp, u_long command, caddr_t data)
466 struct ng_fec_private *priv;
467 struct ng_fec_bundle *b;
469 struct ng_fec_portlist *p;
471 priv = ifp->if_softc;
472 b = &priv->fec_bundle;
474 ifnet_deserialize_all(ifp); /* XXX */
475 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
478 ifnet_serialize_all(oifp);
479 oifp->if_ioctl(oifp, command, data, NULL);
480 ifnet_deserialize_all(oifp);
483 ifnet_serialize_all(ifp);
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 kprintf("fec%d: invalid bundle "
502 "size: %d\n", priv->unit,
509 ifnet_deserialize_all(ifp); /* XXX */
510 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
512 ifnet_serialize_all(bifp);
513 bifp->if_flags |= IFF_UP;
514 bifp->if_ioctl(bifp, SIOCSIFFLAGS, NULL, NULL);
515 /* mark iface as up and let the monitor check it */
517 ifnet_deserialize_all(bifp);
519 ifnet_serialize_all(ifp);
521 callout_reset(&priv->fec_timeout, hz, ng_fec_tick, priv);
525 ng_fec_stop(struct ifnet *ifp)
527 struct ng_fec_private *priv;
528 struct ng_fec_bundle *b;
530 struct ng_fec_portlist *p;
532 priv = ifp->if_softc;
533 b = &priv->fec_bundle;
535 ifnet_deserialize_all(ifp); /* XXX */
536 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
538 ifnet_serialize_all(bifp);
539 bifp->if_flags &= ~IFF_UP;
540 bifp->if_ioctl(bifp, SIOCSIFFLAGS, NULL, NULL);
541 ifnet_deserialize_all(bifp);
543 ifnet_serialize_all(ifp);
545 callout_stop(&priv->fec_timeout);
549 ng_fec_tick(void *arg)
551 struct ng_fec_private *priv;
552 struct ng_fec_bundle *b;
553 struct ifmediareq ifmr;
555 struct ng_fec_portlist *p;
559 b = &priv->fec_bundle;
562 * Note: serializer for parent interface not held on entry, and
563 * cannot be held during the loop to avoid a deadlock.
565 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
566 bzero((char *)&ifmr, sizeof(ifmr));
568 ifnet_serialize_all(ifp);
569 error = ifp->if_ioctl(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr, NULL);
571 kprintf("fec%d: failed to check status "
572 "of link %s\n", priv->unit, ifp->if_xname);
573 ifnet_deserialize_all(ifp);
577 if (ifmr.ifm_status & IFM_AVALID &&
578 IFM_TYPE(ifmr.ifm_active) == IFM_ETHER) {
579 if (ifmr.ifm_status & IFM_ACTIVE) {
580 if (p->fec_ifstat == -1 ||
581 p->fec_ifstat == 0) {
583 kprintf("fec%d: port %s in bundle "
584 "is up\n", priv->unit,
588 if (p->fec_ifstat == -1 ||
589 p->fec_ifstat == 1) {
591 kprintf("fec%d: port %s in bundle "
592 "is down\n", priv->unit,
597 ifnet_deserialize_all(ifp);
600 ifp = &priv->arpcom.ac_if;
601 if (ifp->if_flags & IFF_RUNNING)
602 callout_reset(&priv->fec_timeout, hz, ng_fec_tick, priv);
606 ng_fec_ifmedia_upd(struct ifnet *ifp)
612 ng_fec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
614 struct ng_fec_private *priv;
615 struct ng_fec_bundle *b;
616 struct ng_fec_portlist *p;
618 priv = ifp->if_softc;
619 b = &priv->fec_bundle;
621 ifmr->ifm_status = IFM_AVALID;
622 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
624 ifmr->ifm_status |= IFM_ACTIVE;
631 * Process an ioctl for the virtual interface
634 ng_fec_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
636 struct ifreq *const ifr = (struct ifreq *) data;
638 struct ng_fec_private *priv;
639 struct ng_fec_bundle *b;
641 priv = ifp->if_softc;
642 b = &priv->fec_bundle;
645 ng_fec_print_ioctl(ifp, command, data);
650 /* These two are mostly handled at a higher layer */
654 error = ether_ioctl(ifp, command, data);
660 * If the interface is marked up and stopped, then start it.
661 * If it is marked down and running, then stop it.
663 if (ifr->ifr_flags & IFF_UP) {
664 if (!(ifp->if_flags & IFF_RUNNING)) {
666 if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
667 kprintf("fec%d: invalid bundle "
668 "size: %d\n", priv->unit,
673 ifp->if_flags &= ~(IFF_OACTIVE);
674 ifp->if_flags |= IFF_RUNNING;
678 * Bubble down changes in promisc mode to
679 * underlying interfaces.
681 if ((ifp->if_flags & IFF_PROMISC) !=
682 (priv->if_flags & IFF_PROMISC)) {
683 ng_fec_setport(ifp, command, data);
684 priv->if_flags = ifp->if_flags;
687 if (ifp->if_flags & IFF_RUNNING)
688 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
695 ng_fec_setport(ifp, command, data);
700 error = ifmedia_ioctl(ifp, ifr, &priv->ifmedia, command);
702 /* Stuff that's not supported */
716 * This routine spies on mbufs passing through ether_input(). If
717 * they come from one of the interfaces that are aggregated into
718 * our bundle, we fix up the ifnet pointer and increment our
719 * packet counters so that it looks like the frames are actually
723 ng_fec_input(struct ifnet *ifp, struct mbuf **m0)
725 struct ng_node *node;
726 struct ng_fec_private *priv;
727 struct ng_fec_bundle *b;
730 struct ng_fec_portlist *p;
733 if (ifp == NULL || m0 == NULL)
738 /* Sanity check part II */
742 priv = node->private;
743 b = &priv->fec_bundle;
744 bifp = &priv->arpcom.ac_if;
747 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
748 if (p->fec_if == m->m_pkthdr.rcvif)
752 /* Wasn't meant for us; leave this frame alone. */
756 /* Pretend this is our frame. */
757 m->m_pkthdr.rcvif = bifp;
759 bifp->if_ibytes += m->m_pkthdr.len;
762 bpf_mtap(bifp->if_bpf, m);
766 * Take a quick peek at the packet and see if it's ok for us to use
767 * the inet or inet6 hash methods on it, if they're enabled. We do
768 * this by setting flags in the mbuf header. Once we've made up our
769 * mind what to do, we pass the frame to ether_output() for further
774 ng_fec_output_serialized(struct ifnet *ifp, struct mbuf *m,
775 struct sockaddr *dst, struct rtentry *rt0)
777 const priv_p priv = (priv_p) ifp->if_softc;
778 struct ng_fec_bundle *b;
781 /* Check interface flags */
782 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
787 b = &priv->fec_bundle;
789 switch (b->fec_btype) {
791 m->m_flags |= M_FEC_MAC;
796 * We can't use the INET address port selection
797 * scheme if this isn't an INET packet.
799 if (dst->sa_family == AF_INET)
800 m->m_flags |= M_FEC_INET;
802 else if (dst->sa_family == AF_INET6)
803 m->m_flags |= M_FEC_INET6;
807 kprintf("%s: can't do inet aggregation of non "
808 "inet packet\n", ifp->if_xname);
810 m->m_flags |= M_FEC_MAC;
815 kprintf("%s: bogus hash type: %d\n", ifp->if_xname,
823 * Pass the frame to ether_output() for all the protocol
824 * handling. This will put the ethernet header on the packet
828 error = priv->real_if_output(ifp, m, dst, rt0);
829 if (priv->if_error && !error)
830 error = priv->if_error;
836 ng_fec_output(struct ifnet *ifp, struct mbuf *m,
837 struct sockaddr *dst, struct rtentry *rt0)
841 ifnet_serialize_tx(ifp);
842 error = ng_fec_output_serialized(ifp, m, dst, rt0);
843 ifnet_deserialize_tx(ifp);
849 * Apply a hash to the source and destination addresses in the packet
850 * in order to select an interface. Also check link status and handle
851 * dead links accordingly.
855 ng_fec_choose_port(struct ng_fec_bundle *b,
856 struct mbuf *m, struct ifnet **ifp)
858 struct ether_header *eh;
867 struct ng_fec_portlist *p;
871 * If there are only two ports, mask off all but the
872 * last bit for XORing. If there are 4, mask off all
873 * but the last 2 bits.
875 mask = b->fec_ifcnt == 2 ? 0x1 : 0x3;
876 eh = mtod(m, struct ether_header *);
878 ip = (struct ip *)(mtod(m, char *) +
879 sizeof(struct ether_header));
881 ip6 = (struct ip6_hdr *)(mtod(m, char *) +
882 sizeof(struct ether_header));
887 * The fg_fec_output() routine is supposed to leave a
888 * flag for us in the mbuf that tells us what hash to
889 * use, but sometimes a new mbuf is prepended to the
890 * chain, so we have to search every mbuf in the chain
895 if (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6))
902 switch (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6)) {
904 port = (eh->ether_dhost[5] ^
905 eh->ether_shost[5]) & mask;
909 port = (ntohl(ip->ip_dst.s_addr) ^
910 ntohl(ip->ip_src.s_addr)) & mask;
914 port = (ip6->ip6_dst.s6_addr[15] ^
915 ip6->ip6_dst.s6_addr[15]) & mask;
924 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
925 if (port == p->fec_idx)
930 * Now that we've chosen a port, make sure it's
931 * alive. If it's not alive, cycle through the bundle
932 * looking for a port that is alive. If we don't find
933 * any, return an error.
935 if (p->fec_ifstat != 1) {
936 struct ng_fec_portlist *n = NULL;
938 n = TAILQ_NEXT(p, fec_list);
940 n = TAILQ_FIRST(&b->ng_fec_ports);
942 if (n->fec_ifstat == 1)
944 n = TAILQ_NEXT(n, fec_list);
946 n = TAILQ_FIRST(&b->ng_fec_ports);
959 * Now that the packet has been run through ether_output(), yank it
960 * off our own send queue and stick it on the queue for the appropriate
961 * underlying physical interface. Note that if the interface's send
962 * queue is full, we save an error status in our private netgraph
963 * space which will eventually be handed up to ng_fec_output(), which
964 * will return it to the rest of the IP stack. We need to do this
965 * in order to duplicate the effect of ether_output() returning ENOBUFS
966 * when it detects that an interface's send queue is full. There's no
967 * other way to signal the error status from here since the if_start()
968 * routine is spec'ed to return void.
970 * Once the frame is queued, we call ether_output_frame() to initiate
974 ng_fec_start(struct ifnet *ifp)
976 struct ng_fec_private *priv;
977 struct ng_fec_bundle *b;
978 struct ifnet *oifp = NULL;
982 priv = ifp->if_softc;
983 b = &priv->fec_bundle;
985 IF_DEQUEUE(&ifp->if_snd, m0);
991 /* Queue up packet on the proper port. */
992 error = ng_fec_choose_port(b, m0, &oifp);
996 priv->if_error = ENOBUFS;
1002 * Release current iface's serializer to avoid possible dead lock
1004 priv->if_error = ether_output_frame(oifp, m0);
1009 * Display an ioctl to the virtual interface
1013 ng_fec_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
1017 switch (command & IOC_DIRMASK) {
1033 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
1038 IOCPARM_LEN(command));
1042 /************************************************************************
1044 ************************************************************************/
1047 * Constructor for a node
1050 ng_fec_constructor(node_p *nodep)
1052 char ifname[NG_FEC_FEC_NAME_MAX + 1];
1056 struct ng_fec_bundle *b;
1059 /* Allocate node and interface private structures */
1060 priv = kmalloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
1064 ifp = &priv->arpcom.ac_if;
1065 b = &priv->fec_bundle;
1067 /* Link them together */
1068 ifp->if_softc = priv;
1070 /* Get an interface unit number */
1071 if ((error = ng_fec_get_unit(&priv->unit)) != 0) {
1072 kfree(ifp, M_NETGRAPH);
1073 kfree(priv, M_NETGRAPH);
1077 /* Call generic node constructor */
1078 if ((error = ng_make_node_common(&typestruct, nodep)) != 0) {
1079 ng_fec_free_unit(priv->unit);
1080 kfree(ifp, M_NETGRAPH);
1081 kfree(priv, M_NETGRAPH);
1086 /* Link together node and private info */
1087 node->private = priv;
1089 priv->arpcom.ac_netgraph = node;
1091 /* Initialize interface structure */
1092 if_initname(ifp, NG_FEC_FEC_NAME, priv->unit);
1093 ifp->if_start = ng_fec_start;
1094 ifp->if_ioctl = ng_fec_ioctl;
1095 ifp->if_init = ng_fec_init;
1096 ifp->if_watchdog = NULL;
1097 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
1098 ifp->if_mtu = NG_FEC_MTU_DEFAULT;
1099 ifp->if_flags = (IFF_SIMPLEX|IFF_BROADCAST|IFF_MULTICAST);
1100 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
1101 ifp->if_addrlen = 0; /* XXX */
1102 ifp->if_hdrlen = 0; /* XXX */
1103 ifp->if_baudrate = 100000000; /* XXX */
1105 /* Give this node the same name as the interface (if possible) */
1106 bzero(ifname, sizeof(ifname));
1107 strlcpy(ifname, ifp->if_xname, sizeof(ifname));
1108 if (ng_name_node(node, ifname) != 0)
1109 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
1111 /* Grab hold of the ether_input pipe. */
1112 if (ng_ether_input_p == NULL)
1113 ng_ether_input_p = ng_fec_input;
1115 /* Attach the interface */
1116 ether_ifattach(ifp, priv->arpcom.ac_enaddr, NULL);
1117 priv->real_if_output = ifp->if_output;
1118 ifp->if_output = ng_fec_output;
1119 callout_init(&priv->fec_timeout);
1121 TAILQ_INIT(&b->ng_fec_ports);
1124 ifmedia_init(&priv->ifmedia, 0,
1125 ng_fec_ifmedia_upd, ng_fec_ifmedia_sts);
1126 ifmedia_add(&priv->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
1127 ifmedia_set(&priv->ifmedia, IFM_ETHER|IFM_NONE);
1134 * Receive a control message
1137 ng_fec_rcvmsg(node_p node, struct ng_mesg *msg,
1138 const char *retaddr, struct ng_mesg **rptr)
1140 const priv_p priv = node->private;
1141 struct ng_fec_bundle *b;
1142 struct ng_mesg *resp = NULL;
1146 b = &priv->fec_bundle;
1148 switch (msg->header.typecookie) {
1149 case NGM_FEC_COOKIE:
1150 switch (msg->header.cmd) {
1151 case NGM_FEC_ADD_IFACE:
1153 error = ng_fec_addport(priv, ifname);
1155 case NGM_FEC_DEL_IFACE:
1157 error = ng_fec_delport(priv, ifname);
1159 case NGM_FEC_SET_MODE_MAC:
1160 b->fec_btype = FEC_BTYPE_MAC;
1163 case NGM_FEC_SET_MODE_INET:
1164 b->fec_btype = FEC_BTYPE_INET;
1167 case NGM_FEC_SET_MODE_INET6:
1168 b->fec_btype = FEC_BTYPE_INET6;
1184 kfree(resp, M_NETGRAPH);
1185 kfree(msg, M_NETGRAPH);
1190 * Shutdown and remove the node and its associated interface.
1193 ng_fec_rmnode(node_p node)
1195 const priv_p priv = node->private;
1196 struct ng_fec_bundle *b;
1197 struct ng_fec_portlist *p;
1198 char ifname[IFNAMSIZ];
1200 b = &priv->fec_bundle;
1201 ng_fec_stop(&priv->arpcom.ac_if);
1203 while (!TAILQ_EMPTY(&b->ng_fec_ports)) {
1204 p = TAILQ_FIRST(&b->ng_fec_ports);
1205 ksprintf(ifname, "%s",
1206 p->fec_if->if_xname); /* XXX: strings */
1207 ng_fec_delport(priv, ifname);
1212 if (ng_ether_input_p != NULL)
1213 ng_ether_input_p = NULL;
1214 ether_ifdetach(&priv->arpcom.ac_if);
1215 ifmedia_removeall(&priv->ifmedia);
1216 ng_fec_free_unit(priv->unit);
1217 kfree(priv, M_NETGRAPH);
1218 node->private = NULL;