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 $
36 * $DragonFly: src/sys/netgraph/fec/ng_fec.c,v 1.7 2004/07/19 09:23:23 joerg Exp $
39 * Copyright (c) 1996-1999 Whistle Communications, Inc.
40 * All rights reserved.
42 * Subject to the following obligations and disclaimer of warranty, use and
43 * redistribution of this software, in source or object code forms, with or
44 * without modifications are expressly permitted by Whistle Communications;
45 * provided, however, that:
46 * 1. Any and all reproductions of the source or object code must include the
47 * copyright notice above and the following disclaimer of warranties; and
48 * 2. No rights are granted, in any manner or form, to use Whistle
49 * Communications, Inc. trademarks, including the mark "WHISTLE
50 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
51 * such appears in the above copyright notice or in the software.
53 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
54 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
55 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
56 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
57 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
58 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
59 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
60 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
61 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
62 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
63 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
64 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
65 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
66 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
68 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
71 * Author: Archie Cobbs <archie@freebsd.org>
73 * $Whistle: ng_fec.c,v 1.33 1999/11/01 09:24:51 julian Exp $
77 * This module implements ethernet channel bonding using the Cisco
78 * Fast EtherChannel mechanism. Two or four ports may be combined
79 * into a single aggregate interface.
81 * Interfaces are named fec0, fec1, etc. New nodes take the
82 * first available interface name.
84 * This node also includes Berkeley packet filter support.
86 * Note that this node doesn't need to connect to any other
87 * netgraph nodes in order to do its work.
90 #include <sys/param.h>
91 #include <sys/systm.h>
92 #include <sys/errno.h>
93 #include <sys/kernel.h>
94 #include <sys/malloc.h>
96 #include <sys/errno.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_types.h>
105 #include <net/if_arp.h>
106 #include <net/if_dl.h>
107 #include <net/if_media.h>
108 #include <net/intrq.h>
110 #include <net/ethernet.h>
112 #include "opt_inet.h"
113 #include "opt_inet6.h"
115 #include <netinet/in.h>
117 #include <netinet/in_systm.h>
118 #include <netinet/ip.h>
122 #include <netinet/ip6.h>
125 #include <netgraph/ng_message.h>
126 #include <netgraph/netgraph.h>
127 #include <netgraph/ng_parse.h>
130 #define IFP2NG(ifp) ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph)
131 #define FEC_INC(x, y) (x) = (x + 1) % y
134 * Current fast etherchannel implementations use either 2 or 4
135 * ports, so for now we limit the maximum bundle size to 4 interfaces.
137 #define FEC_BUNDLESIZ 4
139 struct ng_fec_portlist {
140 struct ifnet *fec_if;
143 struct ether_addr fec_mac;
144 TAILQ_ENTRY(ng_fec_portlist) fec_list;
147 struct ng_fec_bundle {
148 TAILQ_HEAD(,ng_fec_portlist) ng_fec_ports;
153 #define FEC_BTYPE_MAC 0x01
154 #define FEC_BTYPE_INET 0x02
155 #define FEC_BTYPE_INET6 0x03
157 /* Node private data */
158 struct ng_fec_private {
159 struct arpcom arpcom;
160 struct ifmedia ifmedia;
162 int if_error; /* XXX */
163 int unit; /* Interface unit number */
164 node_p node; /* Our netgraph node */
165 struct ng_fec_bundle fec_bundle;/* Aggregate bundle */
166 struct callout_handle fec_ch; /* callout handle for ticker */
168 typedef struct ng_fec_private *priv_p;
170 /* Interface methods */
171 static void ng_fec_input(struct ifnet *, struct mbuf **,
172 struct ether_header *);
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 MALLOC(newarray, int *, 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 FREE(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", __FUNCTION__, 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", __FUNCTION__, unit, ng_fec_units_len));
306 KASSERT((ng_fec_units[index] & (1 << bit)) == 0,
307 ("%s: unit=%d is free", __FUNCTION__, 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 FREE(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;
334 struct sockaddr_dl *sdl;
335 struct ng_fec_portlist *p, *new;
337 if (priv == NULL || iface == NULL)
340 b = &priv->fec_bundle;
341 ifp = &priv->arpcom.ac_if;
343 /* Find the interface */
344 bifp = ifunit(iface);
346 printf("fec%d: tried to add iface %s, which "
347 "doesn't seem to exist\n", priv->unit, iface);
351 /* See if we have room in the bundle */
352 if (b->fec_ifcnt == FEC_BUNDLESIZ) {
353 printf("fec%d: can't add new iface; bundle is full\n",
358 /* See if the interface is already in the bundle */
359 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
360 if (p->fec_if == bifp) {
361 printf("fec%d: iface %s is already in this "
362 "bundle\n", priv->unit, iface);
367 /* Allocate new list entry. */
368 MALLOC(new, struct ng_fec_portlist *,
369 sizeof(struct ng_fec_portlist), M_NETGRAPH, M_NOWAIT);
373 ac = (struct arpcom *)bifp;
374 ac->ac_netgraph = priv->node;
377 * If this is the first interface added to the bundle,
378 * use its MAC address for the virtual interface (and,
379 * by extension, all the other ports in the bundle).
381 if (b->fec_ifcnt == 0) {
382 ifa = ifnet_addrs[ifp->if_index - 1];
383 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
384 bcopy((char *)ac->ac_enaddr,
385 priv->arpcom.ac_enaddr, ETHER_ADDR_LEN);
386 bcopy((char *)ac->ac_enaddr,
387 LLADDR(sdl), ETHER_ADDR_LEN);
390 b->fec_btype = FEC_BTYPE_MAC;
391 new->fec_idx = b->fec_ifcnt;
394 /* Save the real MAC address. */
395 bcopy((char *)ac->ac_enaddr,
396 (char *)&new->fec_mac, ETHER_ADDR_LEN);
398 /* Set up phony MAC address. */
399 ifa = ifnet_addrs[bifp->if_index - 1];
400 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
401 bcopy(priv->arpcom.ac_enaddr, ac->ac_enaddr, ETHER_ADDR_LEN);
402 bcopy(priv->arpcom.ac_enaddr, LLADDR(sdl), ETHER_ADDR_LEN);
404 /* Add to the queue */
406 TAILQ_INSERT_TAIL(&b->ng_fec_ports, new, fec_list);
412 ng_fec_delport(struct ng_fec_private *priv, char *iface)
414 struct ng_fec_bundle *b;
415 struct ifnet *ifp, *bifp;
418 struct sockaddr_dl *sdl;
419 struct ng_fec_portlist *p;
421 if (priv == NULL || iface == NULL)
424 b = &priv->fec_bundle;
425 ifp = &priv->arpcom.ac_if;
427 /* Find the interface */
428 bifp = ifunit(iface);
430 printf("fec%d: tried to remove iface %s, which "
431 "doesn't seem to exist\n", priv->unit, iface);
435 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
436 if (p->fec_if == bifp)
441 printf("fec%d: tried to remove iface %s which "
442 "is not in our bundle\n", priv->unit, iface);
447 bifp->if_flags &= ~IFF_UP;
448 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL, NULL);
450 /* Restore MAC address. */
451 ac = (struct arpcom *)bifp;
452 ifa = ifnet_addrs[bifp->if_index - 1];
453 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
454 bcopy((char *)&p->fec_mac, ac->ac_enaddr, ETHER_ADDR_LEN);
455 bcopy((char *)&p->fec_mac, LLADDR(sdl), ETHER_ADDR_LEN);
458 TAILQ_REMOVE(&b->ng_fec_ports, p, fec_list);
466 * Pass an ioctl command down to all the underyling interfaces in a
467 * bundle. Used for setting multicast filters and flags.
471 ng_fec_setport(struct ifnet *ifp, u_long command, caddr_t data)
473 struct ng_fec_private *priv;
474 struct ng_fec_bundle *b;
476 struct ng_fec_portlist *p;
478 priv = ifp->if_softc;
479 b = &priv->fec_bundle;
481 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
484 (*oifp->if_ioctl)(oifp, command, data, NULL);
491 ng_fec_init(void *arg)
493 struct ng_fec_private *priv;
494 struct ng_fec_bundle *b;
495 struct ifnet *ifp, *bifp;
496 struct ng_fec_portlist *p;
499 priv = ifp->if_softc;
500 b = &priv->fec_bundle;
502 if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
503 printf("fec%d: invalid bundle "
504 "size: %d\n", priv->unit,
511 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
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 */
519 priv->fec_ch = timeout(ng_fec_tick, priv, hz);
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 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
537 bifp->if_flags &= ~IFF_UP;
538 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL, NULL);
541 untimeout(ng_fec_tick, priv, priv->fec_ch);
547 ng_fec_tick(void *arg)
549 struct ng_fec_private *priv;
550 struct ng_fec_bundle *b;
551 struct ifmediareq ifmr;
553 struct ng_fec_portlist *p;
557 b = &priv->fec_bundle;
559 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
560 bzero((char *)&ifmr, sizeof(ifmr));
562 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr,
565 printf("fec%d: failed to check status "
566 "of link %s\n", priv->unit, ifp->if_xname);
570 if (ifmr.ifm_status & IFM_AVALID &&
571 IFM_TYPE(ifmr.ifm_active) == IFM_ETHER) {
572 if (ifmr.ifm_status & IFM_ACTIVE) {
573 if (p->fec_ifstat == -1 ||
574 p->fec_ifstat == 0) {
576 printf("fec%d: port %s in bundle "
577 "is up\n", priv->unit,
581 if (p->fec_ifstat == -1 ||
582 p->fec_ifstat == 1) {
584 printf("fec%d: port %s in bundle "
585 "is down\n", priv->unit,
592 ifp = &priv->arpcom.ac_if;
593 if (ifp->if_flags & IFF_RUNNING)
594 priv->fec_ch = timeout(ng_fec_tick, priv, hz);
600 ng_fec_ifmedia_upd(struct ifnet *ifp)
605 static void ng_fec_ifmedia_sts(struct ifnet *ifp,
606 struct ifmediareq *ifmr)
608 struct ng_fec_private *priv;
609 struct ng_fec_bundle *b;
610 struct ng_fec_portlist *p;
612 priv = ifp->if_softc;
613 b = &priv->fec_bundle;
615 ifmr->ifm_status = IFM_AVALID;
616 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
618 ifmr->ifm_status |= IFM_ACTIVE;
627 * Process an ioctl for the virtual interface
630 ng_fec_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
632 struct ifreq *const ifr = (struct ifreq *) data;
634 struct ng_fec_private *priv;
635 struct ng_fec_bundle *b;
637 priv = ifp->if_softc;
638 b = &priv->fec_bundle;
641 ng_fec_print_ioctl(ifp, command, data);
646 /* These two are mostly handled at a higher layer */
650 error = ether_ioctl(ifp, command, data);
656 * If the interface is marked up and stopped, then start it.
657 * If it is marked down and running, then stop it.
659 if (ifr->ifr_flags & IFF_UP) {
660 if (!(ifp->if_flags & IFF_RUNNING)) {
662 if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
663 printf("fec%d: invalid bundle "
664 "size: %d\n", priv->unit,
669 ifp->if_flags &= ~(IFF_OACTIVE);
670 ifp->if_flags |= IFF_RUNNING;
674 * Bubble down changes in promisc mode to
675 * underlying interfaces.
677 if ((ifp->if_flags & IFF_PROMISC) !=
678 (priv->if_flags & IFF_PROMISC)) {
679 ng_fec_setport(ifp, command, data);
680 priv->if_flags = ifp->if_flags;
683 if (ifp->if_flags & IFF_RUNNING)
684 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
691 ng_fec_setport(ifp, command, data);
696 error = ifmedia_ioctl(ifp, ifr, &priv->ifmedia, command);
698 /* Stuff that's not supported */
712 * This routine spies on mbufs passing through ether_input(). If
713 * they come from one of the interfaces that are aggregated into
714 * our bundle, we fix up the ifnet pointer and increment our
715 * packet counters so that it looks like the frames are actually
719 ng_fec_input(struct ifnet *ifp, struct mbuf **m0,
720 struct ether_header *eh)
722 struct ng_node *node;
723 struct ng_fec_private *priv;
724 struct ng_fec_bundle *b;
727 struct ng_fec_portlist *p;
730 if (ifp == NULL || m0 == NULL || eh == NULL)
735 /* Sanity check part II */
739 priv = node->private;
740 b = &priv->fec_bundle;
741 bifp = &priv->arpcom.ac_if;
744 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
745 if (p->fec_if == m->m_pkthdr.rcvif)
749 /* Wasn't meant for us; leave this frame alone. */
753 /* Pretend this is our frame. */
754 m->m_pkthdr.rcvif = bifp;
756 bifp->if_ibytes += m->m_pkthdr.len + sizeof(struct ether_header);
758 /* Check for a BPF tap */
759 if (bifp->if_bpf != NULL) {
762 /* This kludge is OK; BPF treats the "mbuf" as read-only */
764 mh.mh_data = (char *)eh;
765 mh.mh_len = ETHER_HDR_LEN;
766 bpf_mtap(bifp, (struct mbuf *)&mh);
773 * Take a quick peek at the packet and see if it's ok for us to use
774 * the inet or inet6 hash methods on it, if they're enabled. We do
775 * this by setting flags in the mbuf header. Once we've made up our
776 * mind what to do, we pass the frame to ether_output() for further
781 ng_fec_output(struct ifnet *ifp, struct mbuf *m,
782 struct sockaddr *dst, struct rtentry *rt0)
784 const priv_p priv = (priv_p) ifp->if_softc;
785 struct ng_fec_bundle *b;
788 /* Check interface flags */
789 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
794 b = &priv->fec_bundle;
796 switch (b->fec_btype) {
798 m->m_flags |= M_FEC_MAC;
803 * We can't use the INET address port selection
804 * scheme if this isn't an INET packet.
806 if (dst->sa_family == AF_INET)
807 m->m_flags |= M_FEC_INET;
809 else if (dst->sa_family == AF_INET6)
810 m->m_flags |= M_FEC_INET6;
814 printf("%s: can't do inet aggregation of non "
815 "inet packet\n", ifp->if_xname);
817 m->m_flags |= M_FEC_MAC;
822 printf("%s: bogus hash type: %d\n", ifp->if_xname,
830 * Pass the frame to ether_output() for all the protocol
831 * handling. This will put the ethernet header on the packet
835 error = ether_output(ifp, m, dst, rt0);
836 if (priv->if_error && !error)
837 error = priv->if_error;
843 * Apply a hash to the source and destination addresses in the packet
844 * in order to select an interface. Also check link status and handle
845 * dead links accordingly.
849 ng_fec_choose_port(struct ng_fec_bundle *b,
850 struct mbuf *m, struct ifnet **ifp)
852 struct ether_header *eh;
861 struct ng_fec_portlist *p;
865 * If there are only two ports, mask off all but the
866 * last bit for XORing. If there are 4, mask off all
867 * but the last 2 bits.
869 mask = b->fec_ifcnt == 2 ? 0x1 : 0x3;
870 eh = mtod(m, struct ether_header *);
872 ip = (struct ip *)(mtod(m, char *) +
873 sizeof(struct ether_header));
875 ip6 = (struct ip6_hdr *)(mtod(m, char *) +
876 sizeof(struct ether_header));
881 * The fg_fec_output() routine is supposed to leave a
882 * flag for us in the mbuf that tells us what hash to
883 * use, but sometimes a new mbuf is prepended to the
884 * chain, so we have to search every mbuf in the chain
889 if (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6))
896 switch (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6)) {
898 port = (eh->ether_dhost[5] ^
899 eh->ether_shost[5]) & mask;
903 port = (ntohl(ip->ip_dst.s_addr) ^
904 ntohl(ip->ip_src.s_addr)) & mask;
908 port = (ip6->ip6_dst.s6_addr[15] ^
909 ip6->ip6_dst.s6_addr[15]) & mask;
918 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
919 if (port == p->fec_idx)
924 * Now that we've chosen a port, make sure it's
925 * alive. If it's not alive, cycle through the bundle
926 * looking for a port that is alive. If we don't find
927 * any, return an error.
929 if (p->fec_ifstat != 1) {
930 struct ng_fec_portlist *n = NULL;
932 n = TAILQ_NEXT(p, fec_list);
934 n = TAILQ_FIRST(&b->ng_fec_ports);
936 if (n->fec_ifstat == 1)
938 n = TAILQ_NEXT(n, fec_list);
940 n = TAILQ_FIRST(&b->ng_fec_ports);
953 * Now that the packet has been run through ether_output(), yank it
954 * off our own send queue and stick it on the queue for the appropriate
955 * underlying physical interface. Note that if the interface's send
956 * queue is full, we save an error status in our private netgraph
957 * space which will eventually be handed up to ng_fec_output(), which
958 * will return it to the rest of the IP stack. We need to do this
959 * in order to duplicate the effect of ether_output() returning ENOBUFS
960 * when it detects that an interface's send queue is full. There's no
961 * other way to signal the error status from here since the if_start()
962 * routine is spec'ed to return void.
964 * Once the frame is queued, we call ether_output_frame() to initiate
968 ng_fec_start(struct ifnet *ifp)
970 struct ng_fec_private *priv;
971 struct ng_fec_bundle *b;
972 struct ifnet *oifp = NULL;
976 priv = ifp->if_softc;
977 b = &priv->fec_bundle;
979 IF_DEQUEUE(&ifp->if_snd, m0);
986 /* Queue up packet on the proper port. */
987 error = ng_fec_choose_port(b, m0, &oifp);
991 priv->if_error = ENOBUFS;
996 priv->if_error = ether_output_frame(oifp, m0);
1002 * Display an ioctl to the virtual interface
1006 ng_fec_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
1010 switch (command & IOC_DIRMASK) {
1026 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
1031 IOCPARM_LEN(command));
1035 /************************************************************************
1037 ************************************************************************/
1040 * Constructor for a node
1043 ng_fec_constructor(node_p *nodep)
1045 char ifname[NG_FEC_FEC_NAME_MAX + 1];
1049 struct ng_fec_bundle *b;
1052 /* Allocate node and interface private structures */
1053 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT);
1056 bzero(priv, sizeof(*priv));
1058 ifp = &priv->arpcom.ac_if;
1059 b = &priv->fec_bundle;
1061 /* Link them together */
1062 ifp->if_softc = priv;
1064 /* Get an interface unit number */
1065 if ((error = ng_fec_get_unit(&priv->unit)) != 0) {
1066 FREE(ifp, M_NETGRAPH);
1067 FREE(priv, M_NETGRAPH);
1071 /* Call generic node constructor */
1072 if ((error = ng_make_node_common(&typestruct, nodep)) != 0) {
1073 ng_fec_free_unit(priv->unit);
1074 FREE(ifp, M_NETGRAPH);
1075 FREE(priv, M_NETGRAPH);
1080 /* Link together node and private info */
1081 node->private = priv;
1083 priv->arpcom.ac_netgraph = node;
1085 /* Initialize interface structure */
1086 if_initname(ifp, NG_FEC_FEC_NAME, priv->unit);
1087 ifp->if_output = ng_fec_output;
1088 ifp->if_start = ng_fec_start;
1089 ifp->if_ioctl = ng_fec_ioctl;
1090 ifp->if_init = ng_fec_init;
1091 ifp->if_watchdog = NULL;
1092 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
1093 ifp->if_mtu = NG_FEC_MTU_DEFAULT;
1094 ifp->if_flags = (IFF_SIMPLEX|IFF_BROADCAST|IFF_MULTICAST);
1095 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
1096 ifp->if_addrlen = 0; /* XXX */
1097 ifp->if_hdrlen = 0; /* XXX */
1098 ifp->if_baudrate = 100000000; /* XXX */
1099 TAILQ_INIT(&ifp->if_addrhead);
1101 /* Give this node the same name as the interface (if possible) */
1102 bzero(ifname, sizeof(ifname));
1103 strlcpy(ifname, ifp->if_xname, sizeof(ifname));
1104 if (ng_name_node(node, ifname) != 0)
1105 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
1107 /* Grab hold of the ether_input pipe. */
1108 if (ng_ether_input_p == NULL)
1109 ng_ether_input_p = ng_fec_input;
1111 /* Attach the interface */
1112 ether_ifattach(ifp, priv->arpcom.ac_enaddr);
1113 callout_handle_init(&priv->fec_ch);
1115 TAILQ_INIT(&b->ng_fec_ports);
1118 ifmedia_init(&priv->ifmedia, 0,
1119 ng_fec_ifmedia_upd, ng_fec_ifmedia_sts);
1120 ifmedia_add(&priv->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
1121 ifmedia_set(&priv->ifmedia, IFM_ETHER|IFM_NONE);
1128 * Receive a control message
1131 ng_fec_rcvmsg(node_p node, struct ng_mesg *msg,
1132 const char *retaddr, struct ng_mesg **rptr)
1134 const priv_p priv = node->private;
1135 struct ng_fec_bundle *b;
1136 struct ng_mesg *resp = NULL;
1140 b = &priv->fec_bundle;
1142 switch (msg->header.typecookie) {
1143 case NGM_FEC_COOKIE:
1144 switch (msg->header.cmd) {
1145 case NGM_FEC_ADD_IFACE:
1147 error = ng_fec_addport(priv, ifname);
1149 case NGM_FEC_DEL_IFACE:
1151 error = ng_fec_delport(priv, ifname);
1153 case NGM_FEC_SET_MODE_MAC:
1154 b->fec_btype = FEC_BTYPE_MAC;
1157 case NGM_FEC_SET_MODE_INET:
1158 b->fec_btype = FEC_BTYPE_INET;
1161 case NGM_FEC_SET_MODE_INET6:
1162 b->fec_btype = FEC_BTYPE_INET6;
1178 FREE(resp, M_NETGRAPH);
1179 FREE(msg, M_NETGRAPH);
1184 * Shutdown and remove the node and its associated interface.
1187 ng_fec_rmnode(node_p node)
1189 const priv_p priv = node->private;
1190 struct ng_fec_bundle *b;
1191 struct ng_fec_portlist *p;
1192 char ifname[IFNAMSIZ];
1194 b = &priv->fec_bundle;
1195 ng_fec_stop(&priv->arpcom.ac_if);
1197 while (!TAILQ_EMPTY(&b->ng_fec_ports)) {
1198 p = TAILQ_FIRST(&b->ng_fec_ports);
1199 sprintf(ifname, "%s",
1200 p->fec_if->if_xname); /* XXX: strings */
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);
1209 ifmedia_removeall(&priv->ifmedia);
1210 ng_fec_free_unit(priv->unit);
1211 FREE(priv, M_NETGRAPH);
1212 node->private = NULL;