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.
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>
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 <netgraph/ng_message.h>
124 #include <netgraph/netgraph.h>
125 #include <netgraph/ng_parse.h>
126 #include <netgraph/ng_fec.h>
128 #define IFP2NG(ifp) ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph)
129 #define FEC_INC(x, y) (x) = (x + 1) % y
132 * Current fast etherchannel implementations use either 2 or 4
133 * ports, so for now we limit the maximum bundle size to 4 interfaces.
135 #define FEC_BUNDLESIZ 4
137 struct ng_fec_portlist {
138 struct ifnet *fec_if;
139 void (*fec_if_input) (struct ifnet *, struct mbuf *);
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 static void ng_fec_start(struct ifnet *ifp);
172 static int ng_fec_choose_port(struct ng_fec_bundle *b,
173 struct mbuf *m, struct ifnet **ifp);
174 static int ng_fec_setport(struct ifnet *ifp, u_long cmd, caddr_t data);
175 static void ng_fec_init(void *arg);
176 static void ng_fec_stop(struct ifnet *ifp);
177 static int ng_fec_ifmedia_upd(struct ifnet *ifp);
178 static void ng_fec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
179 static int ng_fec_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
180 static int ng_fec_output(struct ifnet *ifp, struct mbuf *m0,
181 struct sockaddr *dst, struct rtentry *rt0);
182 static void ng_fec_tick(void *arg);
183 static int ng_fec_addport(struct ng_fec_private *priv, char *iface);
184 static int ng_fec_delport(struct ng_fec_private *priv, char *iface);
187 static void ng_fec_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
190 /* Netgraph methods */
191 static ng_constructor_t ng_fec_constructor;
192 static ng_rcvmsg_t ng_fec_rcvmsg;
193 static ng_shutdown_t ng_fec_shutdown;
195 /* List of commands and how to convert arguments to/from ASCII */
196 static const struct ng_cmdlist ng_fec_cmds[] = {
201 &ng_parse_string_type,
208 &ng_parse_string_type,
213 NGM_FEC_SET_MODE_MAC,
220 NGM_FEC_SET_MODE_INET,
228 /* Node type descriptor */
229 static struct ng_type typestruct = {
243 NETGRAPH_INIT(fec, &typestruct);
245 /* We keep a bitmap indicating which unit numbers are free.
246 One means the unit number is free, zero means it's taken. */
247 static int *ng_fec_units = NULL;
248 static int ng_fec_units_len = 0;
249 static int ng_units_in_use = 0;
251 #define UNITS_BITSPERWORD (sizeof(*ng_fec_units) * NBBY)
254 * Find the first free unit number for a new interface.
255 * Increase the size of the unit bitmap as necessary.
257 static __inline__ int
258 ng_fec_get_unit(int *unit)
262 for (index = 0; index < ng_fec_units_len
263 && ng_fec_units[index] == 0; index++);
264 if (index == ng_fec_units_len) { /* extend array */
265 int i, *newarray, newlen;
267 newlen = (2 * ng_fec_units_len) + 4;
268 MALLOC(newarray, int *, newlen * sizeof(*ng_fec_units),
269 M_NETGRAPH, M_NOWAIT);
270 if (newarray == NULL)
272 bcopy(ng_fec_units, newarray,
273 ng_fec_units_len * sizeof(*ng_fec_units));
274 for (i = ng_fec_units_len; i < newlen; i++)
276 if (ng_fec_units != NULL)
277 FREE(ng_fec_units, M_NETGRAPH);
278 ng_fec_units = newarray;
279 ng_fec_units_len = newlen;
281 bit = ffs(ng_fec_units[index]) - 1;
282 KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
283 ("%s: word=%d bit=%d", __FUNCTION__, ng_fec_units[index], bit));
284 ng_fec_units[index] &= ~(1 << bit);
285 *unit = (index * UNITS_BITSPERWORD) + bit;
291 * Free a no longer needed unit number.
293 static __inline__ void
294 ng_fec_free_unit(int unit)
298 index = unit / UNITS_BITSPERWORD;
299 bit = unit % UNITS_BITSPERWORD;
300 KASSERT(index < ng_fec_units_len,
301 ("%s: unit=%d len=%d", __FUNCTION__, unit, ng_fec_units_len));
302 KASSERT((ng_fec_units[index] & (1 << bit)) == 0,
303 ("%s: unit=%d is free", __FUNCTION__, unit));
304 ng_fec_units[index] |= (1 << bit);
306 * XXX We could think about reducing the size of ng_fec_units[]
307 * XXX here if the last portion is all ones
308 * XXX At least free it if no more units
309 * Needed if we are to eventually be able to unload.
312 if (ng_units_in_use == 0) { /* XXX make SMP safe */
313 FREE(ng_fec_units, M_NETGRAPH);
314 ng_fec_units_len = 0;
319 /************************************************************************
321 ************************************************************************/
324 ng_fec_addport(struct ng_fec_private *priv, char *iface)
326 struct ng_fec_bundle *b;
327 struct ifnet *ifp, *bifp;
330 struct sockaddr_dl *sdl;
331 struct ng_fec_portlist *p, *new;
333 if (priv == NULL || iface == NULL)
336 b = &priv->fec_bundle;
337 ifp = &priv->arpcom.ac_if;
339 /* Find the interface */
340 bifp = ifunit(iface);
342 printf("fec%d: tried to add iface %s, which "
343 "doesn't seem to exist\n", priv->unit, iface);
347 /* See if we have room in the bundle */
348 if (b->fec_ifcnt == FEC_BUNDLESIZ) {
349 printf("fec%d: can't add new iface; bundle is full\n",
354 /* See if the interface is already in the bundle */
355 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
356 if (p->fec_if == bifp) {
357 printf("fec%d: iface %s is already in this "
358 "bundle\n", priv->unit, iface);
363 /* Allocate new list entry. */
364 MALLOC(new, struct ng_fec_portlist *,
365 sizeof(struct ng_fec_portlist), M_NETGRAPH, M_NOWAIT);
369 ac = (struct arpcom *)bifp;
370 ac->ac_netgraph = priv->node;
373 * If this is the first interface added to the bundle,
374 * use its MAC address for the virtual interface (and,
375 * by extension, all the other ports in the bundle).
377 if (b->fec_ifcnt == 0) {
378 ifa = TAILQ_FIRST(&ifp->if_addrhead);
379 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
380 bcopy((char *)ac->ac_enaddr,
381 priv->arpcom.ac_enaddr, ETHER_ADDR_LEN);
382 bcopy((char *)ac->ac_enaddr,
383 LLADDR(sdl), ETHER_ADDR_LEN);
386 b->fec_btype = FEC_BTYPE_MAC;
387 new->fec_idx = b->fec_ifcnt;
390 /* Save the real MAC address. */
391 bcopy((char *)ac->ac_enaddr,
392 (char *)&new->fec_mac, ETHER_ADDR_LEN);
394 /* Set up phony MAC address. */
395 ifa = TAILQ_FIRST(&bifp->if_addrhead);
396 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
397 bcopy(priv->arpcom.ac_enaddr, ac->ac_enaddr, ETHER_ADDR_LEN);
398 bcopy(priv->arpcom.ac_enaddr, LLADDR(sdl), ETHER_ADDR_LEN);
400 /* Save original input vector */
401 new->fec_if_input = bifp->if_input;
403 /* Override it with our own */
404 bifp->if_input = ng_fec_input;
406 /* Add to the queue */
408 TAILQ_INSERT_TAIL(&b->ng_fec_ports, new, fec_list);
414 ng_fec_delport(struct ng_fec_private *priv, char *iface)
416 struct ng_fec_bundle *b;
417 struct ifnet *ifp, *bifp;
420 struct sockaddr_dl *sdl;
421 struct ng_fec_portlist *p;
423 if (priv == NULL || iface == NULL)
426 b = &priv->fec_bundle;
427 ifp = &priv->arpcom.ac_if;
429 /* Find the interface */
430 bifp = ifunit(iface);
432 printf("fec%d: tried to remove iface %s, which "
433 "doesn't seem to exist\n", priv->unit, iface);
437 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
438 if (p->fec_if == bifp)
443 printf("fec%d: tried to remove iface %s which "
444 "is not in our bundle\n", priv->unit, iface);
449 bifp->if_flags &= ~IFF_UP;
450 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
452 /* Restore MAC address. */
453 ac = (struct arpcom *)bifp;
454 ifa = TAILQ_FIRST(&bifp->if_addrhead);
455 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
456 bcopy((char *)&p->fec_mac, ac->ac_enaddr, ETHER_ADDR_LEN);
457 bcopy((char *)&p->fec_mac, LLADDR(sdl), ETHER_ADDR_LEN);
459 /* Restore input vector */
460 bifp->if_input = p->fec_if_input;
463 TAILQ_REMOVE(&b->ng_fec_ports, p, fec_list);
471 * Pass an ioctl command down to all the underyling interfaces in a
472 * bundle. Used for setting multicast filters and flags.
476 ng_fec_setport(struct ifnet *ifp, u_long command, caddr_t data)
478 struct ng_fec_private *priv;
479 struct ng_fec_bundle *b;
481 struct ng_fec_portlist *p;
483 priv = ifp->if_softc;
484 b = &priv->fec_bundle;
486 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
489 (*oifp->if_ioctl)(oifp, command, data);
496 ng_fec_init(void *arg)
498 struct ng_fec_private *priv;
499 struct ng_fec_bundle *b;
500 struct ifnet *ifp, *bifp;
501 struct ng_fec_portlist *p;
504 priv = ifp->if_softc;
505 b = &priv->fec_bundle;
507 if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
508 printf("fec%d: invalid bundle "
509 "size: %d\n", priv->unit,
516 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
518 bifp->if_flags |= IFF_UP;
519 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
520 /* mark iface as up and let the monitor check it */
524 priv->fec_ch = timeout(ng_fec_tick, priv, hz);
530 ng_fec_stop(struct ifnet *ifp)
532 struct ng_fec_private *priv;
533 struct ng_fec_bundle *b;
535 struct ng_fec_portlist *p;
537 priv = ifp->if_softc;
538 b = &priv->fec_bundle;
540 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
542 bifp->if_flags &= ~IFF_UP;
543 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
546 untimeout(ng_fec_tick, priv, priv->fec_ch);
552 ng_fec_tick(void *arg)
554 struct ng_fec_private *priv;
555 struct ng_fec_bundle *b;
556 struct ifmediareq ifmr;
558 struct ng_fec_portlist *p;
562 b = &priv->fec_bundle;
564 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
565 bzero((char *)&ifmr, sizeof(ifmr));
567 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
569 printf("fec%d: failed to check status "
570 "of link %s%d\n", priv->unit, ifp->if_name,
575 if (ifmr.ifm_status & IFM_AVALID &&
576 IFM_TYPE(ifmr.ifm_active) == IFM_ETHER) {
577 if (ifmr.ifm_status & IFM_ACTIVE) {
578 if (p->fec_ifstat == -1 ||
579 p->fec_ifstat == 0) {
581 printf("fec%d: port %s%d in bundle "
582 "is up\n", priv->unit,
583 ifp->if_name, ifp->if_unit);
586 if (p->fec_ifstat == -1 ||
587 p->fec_ifstat == 1) {
589 printf("fec%d: port %s%d in bundle "
590 "is down\n", priv->unit,
591 ifp->if_name, ifp->if_unit);
597 ifp = &priv->arpcom.ac_if;
598 if (ifp->if_flags & IFF_RUNNING)
599 priv->fec_ch = timeout(ng_fec_tick, priv, hz);
605 ng_fec_ifmedia_upd(struct ifnet *ifp)
610 static void ng_fec_ifmedia_sts(struct ifnet *ifp,
611 struct ifmediareq *ifmr)
613 struct ng_fec_private *priv;
614 struct ng_fec_bundle *b;
615 struct ng_fec_portlist *p;
617 priv = ifp->if_softc;
618 b = &priv->fec_bundle;
620 ifmr->ifm_status = IFM_AVALID;
621 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
623 ifmr->ifm_status |= IFM_ACTIVE;
632 * Process an ioctl for the virtual interface
635 ng_fec_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
637 struct ifreq *const ifr = (struct ifreq *) data;
639 struct ng_fec_private *priv;
640 struct ng_fec_bundle *b;
642 priv = ifp->if_softc;
643 b = &priv->fec_bundle;
646 ng_fec_print_ioctl(ifp, command, data);
651 /* These two are mostly handled at a higher layer */
655 error = ether_ioctl(ifp, command, data);
661 * If the interface is marked up and stopped, then start it.
662 * If it is marked down and running, then stop it.
664 if (ifr->ifr_flags & IFF_UP) {
665 if (!(ifp->if_flags & IFF_RUNNING)) {
667 if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
668 printf("fec%d: invalid bundle "
669 "size: %d\n", priv->unit,
674 ifp->if_flags &= ~(IFF_OACTIVE);
675 ifp->if_flags |= IFF_RUNNING;
679 * Bubble down changes in promisc mode to
680 * underlying interfaces.
682 if ((ifp->if_flags & IFF_PROMISC) !=
683 (priv->if_flags & IFF_PROMISC)) {
684 ng_fec_setport(ifp, command, data);
685 priv->if_flags = ifp->if_flags;
688 if (ifp->if_flags & IFF_RUNNING)
689 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
696 ng_fec_setport(ifp, command, data);
701 error = ifmedia_ioctl(ifp, ifr, &priv->ifmedia, command);
703 /* Stuff that's not supported */
717 * This routine spies on mbufs received by underlying network device
718 * drivers. When we add an interface to our bundle, we override its
719 * if_input routine with a pointer to ng_fec_input(). This means we
720 * get to look at all the device's packets before sending them to the
721 * real ether_input() for processing by the stack. Once we verify the
722 * packet comes from an interface that's been aggregated into
723 * our bundle, we fix up the rcvif pointer and increment our
724 * packet counters so that it looks like the frames are actually
728 ng_fec_input(struct ifnet *ifp, struct mbuf *m0)
730 struct ng_node *node;
731 struct ng_fec_private *priv;
732 struct ng_fec_bundle *b;
734 struct ng_fec_portlist *p;
737 if (ifp == NULL || m0 == NULL)
742 /* Sanity check part II */
746 priv = NG_NODE_PRIVATE(node);
747 b = &priv->fec_bundle;
748 bifp = &priv->arpcom.ac_if;
750 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
751 if (p->fec_if == m0->m_pkthdr.rcvif)
755 /* Wasn't meant for us; leave this frame alone. */
760 * Check for a BPF tap on the underlying interface. This
761 * is mainly a debugging aid: it allows tcpdump-ing of an
762 * individual interface in a bundle to work, which it
763 * otherwise would not. BPF tapping of our own aggregate
764 * interface will occur once we call ether_input().
766 BPF_MTAP(m0->m_pkthdr.rcvif, m0);
768 /* Convince the system that this is our frame. */
769 m0->m_pkthdr.rcvif = bifp;
771 bifp->if_ibytes += m0->m_pkthdr.len + sizeof(struct ether_header);
773 (*bifp->if_input)(bifp, m0);
779 * Take a quick peek at the packet and see if it's ok for us to use
780 * the inet or inet6 hash methods on it, if they're enabled. We do
781 * this by setting flags in the mbuf header. Once we've made up our
782 * mind what to do, we pass the frame to ether_output() for further
787 ng_fec_output(struct ifnet *ifp, struct mbuf *m,
788 struct sockaddr *dst, struct rtentry *rt0)
790 const priv_p priv = (priv_p) ifp->if_softc;
791 struct ng_fec_bundle *b;
794 /* Check interface flags */
795 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
800 b = &priv->fec_bundle;
802 switch (b->fec_btype) {
804 m->m_flags |= M_FEC_MAC;
809 * We can't use the INET address port selection
810 * scheme if this isn't an INET packet.
812 if (dst->sa_family == AF_INET)
813 m->m_flags |= M_FEC_INET;
815 else if (dst->sa_family == AF_INET6)
816 m->m_flags |= M_FEC_INET6;
820 printf("fec%d: can't do inet aggregation of non "
821 "inet packet\n", ifp->if_unit);
823 m->m_flags |= M_FEC_MAC;
828 printf("fec%d: bogus hash type: %d\n", ifp->if_unit,
836 * Pass the frame to ether_output() for all the protocol
837 * handling. This will put the ethernet header on the packet
841 error = ether_output(ifp, m, dst, rt0);
842 if (priv->if_error && !error)
843 error = priv->if_error;
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;
1001 priv->if_error = ether_output_frame(oifp, m0);
1008 * Display an ioctl to the virtual interface
1012 ng_fec_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
1016 switch (command & IOC_DIRMASK) {
1032 log(LOG_DEBUG, "%s%d: %s('%c', %d, char[%d])\n",
1033 ifp->if_name, ifp->if_unit,
1037 IOCPARM_LEN(command));
1041 /************************************************************************
1043 ************************************************************************/
1046 * Constructor for a node
1049 ng_fec_constructor(node_p node)
1051 char ifname[NG_FEC_FEC_NAME_MAX + 1];
1054 struct ng_fec_bundle *b;
1057 /* Allocate node and interface private structures */
1058 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT);
1061 bzero(priv, sizeof(*priv));
1063 ifp = &priv->arpcom.ac_if;
1064 b = &priv->fec_bundle;
1066 /* Link them together */
1067 ifp->if_softc = priv;
1069 /* Get an interface unit number */
1070 if ((error = ng_fec_get_unit(&priv->unit)) != 0) {
1071 FREE(ifp, M_NETGRAPH);
1072 FREE(priv, M_NETGRAPH);
1076 /* Link together node and private info */
1077 NG_NODE_SET_PRIVATE(node, priv);
1079 priv->arpcom.ac_netgraph = node;
1081 /* Initialize interface structure */
1082 ifp->if_name = NG_FEC_FEC_NAME;
1083 ifp->if_unit = priv->unit;
1084 ifp->if_start = ng_fec_start;
1085 ifp->if_ioctl = ng_fec_ioctl;
1086 ifp->if_init = ng_fec_init;
1087 ifp->if_watchdog = NULL;
1088 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
1089 ifp->if_mtu = NG_FEC_MTU_DEFAULT;
1090 ifp->if_flags = (IFF_SIMPLEX|IFF_BROADCAST|IFF_MULTICAST);
1091 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
1092 ifp->if_addrlen = 0; /* XXX */
1093 ifp->if_hdrlen = 0; /* XXX */
1094 ifp->if_baudrate = 100000000; /* XXX */
1095 TAILQ_INIT(&ifp->if_addrhead);
1097 /* Give this node the same name as the interface (if possible) */
1098 bzero(ifname, sizeof(ifname));
1099 snprintf(ifname, sizeof(ifname), "%s%d", ifp->if_name, ifp->if_unit);
1100 if (ng_name_node(node, ifname) != 0)
1101 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
1103 /* Attach the interface */
1104 ether_ifattach(ifp, priv->arpcom.ac_enaddr);
1105 callout_handle_init(&priv->fec_ch);
1107 /* Override output method with our own */
1108 ifp->if_output = ng_fec_output;
1110 TAILQ_INIT(&b->ng_fec_ports);
1113 ifmedia_init(&priv->ifmedia, 0,
1114 ng_fec_ifmedia_upd, ng_fec_ifmedia_sts);
1115 ifmedia_add(&priv->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
1116 ifmedia_set(&priv->ifmedia, IFM_ETHER|IFM_NONE);
1123 * Receive a control message
1126 ng_fec_rcvmsg(node_p node, item_p item, hook_p lasthook)
1128 const priv_p priv = NG_NODE_PRIVATE(node);
1129 struct ng_fec_bundle *b;
1130 struct ng_mesg *resp = NULL;
1131 struct ng_mesg *msg;
1135 NGI_GET_MSG(item, msg);
1136 b = &priv->fec_bundle;
1138 switch (msg->header.typecookie) {
1139 case NGM_FEC_COOKIE:
1140 switch (msg->header.cmd) {
1141 case NGM_FEC_ADD_IFACE:
1143 error = ng_fec_addport(priv, ifname);
1145 case NGM_FEC_DEL_IFACE:
1147 error = ng_fec_delport(priv, ifname);
1149 case NGM_FEC_SET_MODE_MAC:
1150 b->fec_btype = FEC_BTYPE_MAC;
1153 case NGM_FEC_SET_MODE_INET:
1154 b->fec_btype = FEC_BTYPE_INET;
1157 case NGM_FEC_SET_MODE_INET6:
1158 b->fec_btype = FEC_BTYPE_INET6;
1171 NG_RESPOND_MSG(error, node, item, resp);
1177 * Shutdown and remove the node and its associated interface.
1180 ng_fec_shutdown(node_p node)
1182 const priv_p priv = NG_NODE_PRIVATE(node);
1183 struct ng_fec_bundle *b;
1184 struct ng_fec_portlist *p;
1185 char ifname[IFNAMSIZ];
1187 b = &priv->fec_bundle;
1188 ng_fec_stop(&priv->arpcom.ac_if);
1190 while (!TAILQ_EMPTY(&b->ng_fec_ports)) {
1191 p = TAILQ_FIRST(&b->ng_fec_ports);
1192 sprintf(ifname, "%s%d",
1194 p->fec_if->if_unit);
1195 ng_fec_delport(priv, ifname);
1198 ether_ifdetach(&priv->arpcom.ac_if);
1199 ifmedia_removeall(&priv->ifmedia);
1200 ng_fec_free_unit(priv->unit);
1201 FREE(priv, M_NETGRAPH);
1202 NG_NODE_SET_PRIVATE(node, NULL);
1203 NG_NODE_UNREF(node);