6 * Copyright (c) 2001 Berkeley Software Design, Inc.
7 * Copyright (c) 2000, 2001
8 * Bill Paul <wpaul@osd.bsdi.com>. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Bill Paul.
21 * 4. Neither the name of the author nor the names of any co-contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35 * THE POSSIBILITY OF SUCH DAMAGE.
37 * $FreeBSD: src/sys/netgraph/ng_fec.c,v 1.30 2007/05/18 15:05:49 dwmalone Exp $
38 * $DragonFly: src/sys/netgraph7/ng_fec.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
41 * Copyright (c) 1996-1999 Whistle Communications, Inc.
42 * All rights reserved.
44 * Subject to the following obligations and disclaimer of warranty, use and
45 * redistribution of this software, in source or object code forms, with or
46 * without modifications are expressly permitted by Whistle Communications;
47 * provided, however, that:
48 * 1. Any and all reproductions of the source or object code must include the
49 * copyright notice above and the following disclaimer of warranties; and
50 * 2. No rights are granted, in any manner or form, to use Whistle
51 * Communications, Inc. trademarks, including the mark "WHISTLE
52 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
53 * such appears in the above copyright notice or in the software.
55 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
56 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
57 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
58 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
59 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
60 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
61 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
62 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
63 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
64 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
65 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
66 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
67 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
68 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
69 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
70 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
73 * Author: Archie Cobbs <archie@freebsd.org>
75 * $Whistle: ng_fec.c,v 1.33 1999/11/01 09:24:51 julian Exp $
79 * This module implements ethernet channel bonding using the Cisco
80 * Fast EtherChannel mechanism. Two or four ports may be combined
81 * into a single aggregate interface.
83 * Interfaces are named fec0, fec1, etc. New nodes take the
84 * first available interface name.
86 * This node also includes Berkeley packet filter support.
88 * Note that this node doesn't need to connect to any other
89 * netgraph nodes in order to do its work.
92 #include <sys/param.h>
93 #include <sys/systm.h>
94 #include <sys/errno.h>
95 #include <sys/kernel.h>
96 #include <sys/malloc.h>
98 #include <sys/errno.h>
99 #include <sys/sockio.h>
100 #include <sys/socket.h>
101 #include <sys/syslog.h>
102 #include <sys/libkern.h>
103 #include <sys/queue.h>
106 #include <net/if_dl.h>
107 #include <net/if_types.h>
108 #include <net/if_media.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 "ng_message.h"
126 #include "netgraph.h"
127 #include "ng_parse.h"
131 * We need a way to stash a pointer to our netgraph node in the
132 * ifnet structure so that receive handling works. As far as I can
133 * tell, although there is an AF_NETGRAPH address family, it's only
134 * used to identify sockaddr_ng structures: there is no netgraph address
135 * family domain. This means the AF_NETGRAPH entry in ifp->if_afdata
136 * should be unused, so we can use to hold our node context.
138 #define IFP2NG(ifp) ((ifp)->if_afdata[AF_NETGRAPH])
141 * Current fast etherchannel implementations use either 2 or 4
142 * ports, so for now we limit the maximum bundle size to 4 interfaces.
144 #define FEC_BUNDLESIZ 4
146 struct ng_fec_portlist
{
147 struct ifnet
*fec_if
;
148 void (*fec_if_input
) (struct ifnet
*,
152 struct ether_addr fec_mac
;
153 SLIST_HEAD(__mclhd
, ng_fec_mc
) fec_mc_head
;
154 TAILQ_ENTRY(ng_fec_portlist
) fec_list
;
158 struct ifmultiaddr
*mc_ifma
;
159 SLIST_ENTRY(ng_fec_mc
) mc_entries
;
162 struct ng_fec_bundle
{
163 TAILQ_HEAD(,ng_fec_portlist
) ng_fec_ports
;
166 int (*fec_if_output
) (struct ifnet
*,
172 #define FEC_BTYPE_MAC 0x01
173 #define FEC_BTYPE_INET 0x02
174 #define FEC_BTYPE_INET6 0x03
176 /* Node private data */
177 struct ng_fec_private
{
179 struct ifmedia ifmedia
;
181 int if_error
; /* XXX */
182 int unit
; /* Interface unit number */
183 node_p node
; /* Our netgraph node */
184 struct ng_fec_bundle fec_bundle
;/* Aggregate bundle */
185 struct callout_handle fec_ch
; /* callout handle for ticker */
187 typedef struct ng_fec_private
*priv_p
;
189 /* Interface methods */
190 static void ng_fec_input(struct ifnet
*, struct mbuf
*);
191 static void ng_fec_start(struct ifnet
*ifp
);
192 static int ng_fec_choose_port(struct ng_fec_bundle
*b
,
193 struct mbuf
*m
, struct ifnet
**ifp
);
194 static int ng_fec_setport(struct ifnet
*ifp
, u_long cmd
, caddr_t data
);
195 static void ng_fec_init(void *arg
);
196 static void ng_fec_stop(struct ifnet
*ifp
);
197 static int ng_fec_ifmedia_upd(struct ifnet
*ifp
);
198 static void ng_fec_ifmedia_sts(struct ifnet
*ifp
, struct ifmediareq
*ifmr
);
199 static int ng_fec_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
);
200 static int ng_fec_output(struct ifnet
*ifp
, struct mbuf
*m0
,
201 struct sockaddr
*dst
, struct rtentry
*rt0
);
202 static void ng_fec_tick(void *arg
);
203 static int ng_fec_addport(struct ng_fec_private
*priv
, char *iface
);
204 static int ng_fec_delport(struct ng_fec_private
*priv
, char *iface
);
205 static int ng_fec_ether_cmdmulti(struct ifnet
*trifp
, struct ng_fec_portlist
*p
, int set
);
208 static void ng_fec_print_ioctl(struct ifnet
*ifp
, int cmd
, caddr_t data
);
211 /* Netgraph methods */
212 static int ng_fec_mod_event(module_t
, int, void *);
213 static ng_constructor_t ng_fec_constructor
;
214 static ng_rcvmsg_t ng_fec_rcvmsg
;
215 static ng_shutdown_t ng_fec_shutdown
;
217 /* List of commands and how to convert arguments to/from ASCII */
218 static const struct ng_cmdlist ng_fec_cmds
[] = {
223 &ng_parse_string_type
,
230 &ng_parse_string_type
,
235 NGM_FEC_SET_MODE_MAC
,
242 NGM_FEC_SET_MODE_INET
,
250 /* Node type descriptor */
251 static struct ng_type typestruct
= {
252 .version
= NG_ABI_VERSION
,
253 .name
= NG_FEC_NODE_TYPE
,
254 .mod_event
= ng_fec_mod_event
,
255 .constructor
= ng_fec_constructor
,
256 .rcvmsg
= ng_fec_rcvmsg
,
257 .shutdown
= ng_fec_shutdown
,
258 .cmdlist
= ng_fec_cmds
,
260 NETGRAPH_INIT(fec
, &typestruct
);
262 /* We keep a bitmap indicating which unit numbers are free.
263 One means the unit number is free, zero means it's taken. */
264 static int *ng_fec_units
= NULL
;
265 static int ng_fec_units_len
= 0;
266 static int ng_units_in_use
= 0;
268 #define UNITS_BITSPERWORD (sizeof(*ng_fec_units) * NBBY)
270 static struct mtx ng_fec_mtx
;
273 * Find the first free unit number for a new interface.
274 * Increase the size of the unit bitmap as necessary.
277 ng_fec_get_unit(int *unit
)
281 mtx_lock(&ng_fec_mtx
);
282 for (index
= 0; index
< ng_fec_units_len
283 && ng_fec_units
[index
] == 0; index
++);
284 if (index
== ng_fec_units_len
) { /* extend array */
285 int i
, *newarray
, newlen
;
287 newlen
= (2 * ng_fec_units_len
) + 4;
288 MALLOC(newarray
, int *, newlen
* sizeof(*ng_fec_units
),
289 M_NETGRAPH
, M_WAITOK
| M_NULLOK
);
290 if (newarray
== NULL
) {
291 mtx_unlock(&ng_fec_mtx
);
294 bcopy(ng_fec_units
, newarray
,
295 ng_fec_units_len
* sizeof(*ng_fec_units
));
296 for (i
= ng_fec_units_len
; i
< newlen
; i
++)
298 if (ng_fec_units
!= NULL
)
299 FREE(ng_fec_units
, M_NETGRAPH
);
300 ng_fec_units
= newarray
;
301 ng_fec_units_len
= newlen
;
303 bit
= ffs(ng_fec_units
[index
]) - 1;
304 KASSERT(bit
>= 0 && bit
<= UNITS_BITSPERWORD
- 1,
305 ("%s: word=%d bit=%d", __func__
, ng_fec_units
[index
], bit
));
306 ng_fec_units
[index
] &= ~(1 << bit
);
307 *unit
= (index
* UNITS_BITSPERWORD
) + bit
;
309 mtx_unlock(&ng_fec_mtx
);
314 * Free a no longer needed unit number.
317 ng_fec_free_unit(int unit
)
321 index
= unit
/ UNITS_BITSPERWORD
;
322 bit
= unit
% UNITS_BITSPERWORD
;
323 mtx_lock(&ng_fec_mtx
);
324 KASSERT(index
< ng_fec_units_len
,
325 ("%s: unit=%d len=%d", __func__
, unit
, ng_fec_units_len
));
326 KASSERT((ng_fec_units
[index
] & (1 << bit
)) == 0,
327 ("%s: unit=%d is free", __func__
, unit
));
328 ng_fec_units
[index
] |= (1 << bit
);
330 * XXX We could think about reducing the size of ng_fec_units[]
331 * XXX here if the last portion is all ones
332 * XXX At least free it if no more units
333 * Needed if we are to eventually be able to unload.
336 if (ng_units_in_use
== 0) { /* XXX make SMP safe */
337 FREE(ng_fec_units
, M_NETGRAPH
);
338 ng_fec_units_len
= 0;
341 mtx_unlock(&ng_fec_mtx
);
344 /************************************************************************
346 ************************************************************************/
349 ng_fec_addport(struct ng_fec_private
*priv
, char *iface
)
351 struct ng_fec_bundle
*b
;
352 struct ifnet
*ifp
, *bifp
;
353 struct ng_fec_portlist
*p
, *new;
355 if (priv
== NULL
|| iface
== NULL
)
358 b
= &priv
->fec_bundle
;
361 /* Only allow reconfiguration if not running. */
362 if (ifp
->if_drv_flags
& IFF_DRV_RUNNING
) {
363 printf("fec%d: can't add new iface; bundle is running\n",
368 /* Find the interface */
369 bifp
= ifunit(iface
);
371 printf("fec%d: tried to add iface %s, which "
372 "doesn't seem to exist\n", priv
->unit
, iface
);
376 /* See if we have room in the bundle */
377 if (b
->fec_ifcnt
== FEC_BUNDLESIZ
) {
378 printf("fec%d: can't add new iface; bundle is full\n",
383 /* See if the interface is already in the bundle */
384 TAILQ_FOREACH(p
, &b
->ng_fec_ports
, fec_list
) {
385 if (p
->fec_if
== bifp
) {
386 printf("fec%d: iface %s is already in this "
387 "bundle\n", priv
->unit
, iface
);
393 * All interfaces must use the same output vector. Once the
394 * user attaches an interface of one type, make all subsequent
395 * interfaces have the same output vector.
397 if (b
->fec_if_output
!= NULL
) {
398 if (b
->fec_if_output
!= bifp
->if_output
) {
399 printf("fec%d: iface %s is not the same type "
400 "as the other interface(s) already in "
401 "the bundle\n", priv
->unit
, iface
);
406 /* Allocate new list entry. */
407 MALLOC(new, struct ng_fec_portlist
*,
408 sizeof(struct ng_fec_portlist
), M_NETGRAPH
, M_WAITOK
| M_NULLOK
);
412 IF_AFDATA_LOCK(bifp
);
413 IFP2NG(bifp
) = priv
->node
;
414 IF_AFDATA_UNLOCK(bifp
);
417 * If this is the first interface added to the bundle,
418 * use its MAC address for the virtual interface (and,
419 * by extension, all the other ports in the bundle).
421 if (b
->fec_ifcnt
== 0)
422 if_setlladdr(ifp
, IF_LLADDR(bifp
), ETHER_ADDR_LEN
);
424 b
->fec_btype
= FEC_BTYPE_MAC
;
425 new->fec_idx
= b
->fec_ifcnt
;
428 /* Initialise the list of multicast addresses that we own. */
429 SLIST_INIT(&new->fec_mc_head
);
431 /* Save the real MAC address. */
432 bcopy(IF_LLADDR(bifp
),
433 (char *)&new->fec_mac
, ETHER_ADDR_LEN
);
435 /* Set up phony MAC address. */
436 if_setlladdr(bifp
, IF_LLADDR(ifp
), ETHER_ADDR_LEN
);
438 /* Save original input vector */
439 new->fec_if_input
= bifp
->if_input
;
441 /* Override it with our own */
442 bifp
->if_input
= ng_fec_input
;
444 /* Save output vector too. */
445 if (b
->fec_if_output
== NULL
)
446 b
->fec_if_output
= bifp
->if_output
;
448 /* Add to the queue */
450 new->fec_ifstat
= -1;
451 TAILQ_INSERT_TAIL(&b
->ng_fec_ports
, new, fec_list
);
453 /* Add multicast addresses to this port. */
454 ng_fec_ether_cmdmulti(ifp
, new, 1);
460 ng_fec_delport(struct ng_fec_private
*priv
, char *iface
)
462 struct ng_fec_bundle
*b
;
463 struct ifnet
*ifp
, *bifp
;
464 struct ng_fec_portlist
*p
;
466 if (priv
== NULL
|| iface
== NULL
)
469 b
= &priv
->fec_bundle
;
472 /* Only allow reconfiguration if not running. */
473 if (ifp
->if_drv_flags
& IFF_DRV_RUNNING
) {
474 printf("fec%d: can't remove iface; bundle is running\n",
479 /* Find the interface */
480 bifp
= ifunit(iface
);
482 printf("fec%d: tried to remove iface %s, which "
483 "doesn't seem to exist\n", priv
->unit
, iface
);
487 TAILQ_FOREACH(p
, &b
->ng_fec_ports
, fec_list
) {
488 if (p
->fec_if
== bifp
)
493 printf("fec%d: tried to remove iface %s which "
494 "is not in our bundle\n", priv
->unit
, iface
);
499 bifp
->if_flags
&= ~IFF_UP
;
500 (*bifp
->if_ioctl
)(bifp
, SIOCSIFFLAGS
, NULL
);
502 /* Restore MAC address. */
503 if_setlladdr(bifp
, (u_char
*)&p
->fec_mac
, ETHER_ADDR_LEN
);
505 /* Restore input vector */
506 bifp
->if_input
= p
->fec_if_input
;
508 /* Remove our node context pointer. */
509 IF_AFDATA_LOCK(bifp
);
511 IF_AFDATA_UNLOCK(bifp
);
514 TAILQ_REMOVE(&b
->ng_fec_ports
, p
, fec_list
);
518 if (b
->fec_ifcnt
== 0)
519 b
->fec_if_output
= NULL
;
525 ng_fec_ether_cmdmulti(struct ifnet
*trifp
, struct ng_fec_portlist
*p
, int set
)
527 struct ifnet
*ifp
= p
->fec_if
;
528 struct ng_fec_mc
*mc
;
529 struct ifmultiaddr
*ifma
, *rifma
= NULL
;
530 struct sockaddr_dl sdl
;
533 bzero((char *)&sdl
, sizeof(sdl
));
534 sdl
.sdl_len
= sizeof(sdl
);
535 sdl
.sdl_family
= AF_LINK
;
536 sdl
.sdl_type
= IFT_ETHER
;
537 sdl
.sdl_alen
= ETHER_ADDR_LEN
;
538 sdl
.sdl_index
= ifp
->if_index
;
541 TAILQ_FOREACH(ifma
, &trifp
->if_multiaddrs
, ifma_link
) {
542 if (ifma
->ifma_addr
->sa_family
!= AF_LINK
)
544 bcopy(LLADDR((struct sockaddr_dl
*)ifma
->ifma_addr
),
545 LLADDR(&sdl
), ETHER_ADDR_LEN
);
547 error
= if_addmulti(ifp
, (struct sockaddr
*)&sdl
, &rifma
);
550 mc
= kmalloc(sizeof(struct ng_fec_mc
), M_DEVBUF
, M_WAITOK
| M_NULLOK
);
554 SLIST_INSERT_HEAD(&p
->fec_mc_head
, mc
, mc_entries
);
557 while ((mc
= SLIST_FIRST(&p
->fec_mc_head
)) != NULL
) {
558 SLIST_REMOVE(&p
->fec_mc_head
, mc
, ng_fec_mc
, mc_entries
);
559 if_delmulti_ifma(mc
->mc_ifma
);
567 ng_fec_ether_setmulti(struct ifnet
*ifp
)
569 struct ng_fec_private
*priv
;
570 struct ng_fec_bundle
*b
;
571 struct ng_fec_portlist
*p
;
573 priv
= ifp
->if_softc
;
574 b
= &priv
->fec_bundle
;
576 TAILQ_FOREACH(p
, &b
->ng_fec_ports
, fec_list
) {
577 /* First, remove any existing filter entries. */
578 ng_fec_ether_cmdmulti(ifp
, p
, 0);
579 /* copy all addresses from the fec interface to the port */
580 ng_fec_ether_cmdmulti(ifp
, p
, 1);
586 * Pass an ioctl command down to all the underyling interfaces in a
587 * bundle. Used for setting flags.
591 ng_fec_setport(struct ifnet
*ifp
, u_long command
, caddr_t data
)
593 struct ng_fec_private
*priv
;
594 struct ng_fec_bundle
*b
;
596 struct ng_fec_portlist
*p
;
598 priv
= ifp
->if_softc
;
599 b
= &priv
->fec_bundle
;
601 TAILQ_FOREACH(p
, &b
->ng_fec_ports
, fec_list
) {
604 (*oifp
->if_ioctl
)(oifp
, command
, data
);
611 ng_fec_init(void *arg
)
613 struct ng_fec_private
*priv
;
614 struct ng_fec_bundle
*b
;
615 struct ifnet
*ifp
, *bifp
;
616 struct ng_fec_portlist
*p
;
620 b
= &priv
->fec_bundle
;
622 if (b
->fec_ifcnt
!= 2 && b
->fec_ifcnt
!= FEC_BUNDLESIZ
) {
623 printf("fec%d: invalid bundle "
624 "size: %d\n", priv
->unit
,
631 TAILQ_FOREACH(p
, &b
->ng_fec_ports
, fec_list
) {
633 bifp
->if_flags
|= IFF_UP
;
634 (*bifp
->if_ioctl
)(bifp
, SIOCSIFFLAGS
, NULL
);
635 /* mark iface as up and let the monitor check it */
639 ifp
->if_drv_flags
&= ~(IFF_DRV_OACTIVE
);
640 ifp
->if_drv_flags
|= IFF_DRV_RUNNING
;
642 priv
->fec_ch
= timeout(ng_fec_tick
, priv
, hz
);
648 ng_fec_stop(struct ifnet
*ifp
)
650 struct ng_fec_private
*priv
;
651 struct ng_fec_bundle
*b
;
653 struct ng_fec_portlist
*p
;
655 priv
= ifp
->if_softc
;
656 b
= &priv
->fec_bundle
;
658 TAILQ_FOREACH(p
, &b
->ng_fec_ports
, fec_list
) {
660 bifp
->if_flags
&= ~IFF_UP
;
661 (*bifp
->if_ioctl
)(bifp
, SIOCSIFFLAGS
, NULL
);
664 untimeout(ng_fec_tick
, priv
, priv
->fec_ch
);
666 ifp
->if_drv_flags
&= ~(IFF_DRV_RUNNING
| IFF_DRV_OACTIVE
);
672 ng_fec_tick(void *arg
)
674 struct ng_fec_private
*priv
;
675 struct ng_fec_bundle
*b
;
676 struct ifmediareq ifmr
;
678 struct ng_fec_portlist
*p
;
682 b
= &priv
->fec_bundle
;
684 TAILQ_FOREACH(p
, &b
->ng_fec_ports
, fec_list
) {
685 bzero((char *)&ifmr
, sizeof(ifmr
));
687 error
= (*ifp
->if_ioctl
)(ifp
, SIOCGIFMEDIA
, (caddr_t
)&ifmr
);
689 printf("fec%d: failed to check status "
690 "of link %s\n", priv
->unit
, ifp
->if_xname
);
694 if (ifmr
.ifm_status
& IFM_AVALID
) {
695 if (ifmr
.ifm_status
& IFM_ACTIVE
) {
696 if (p
->fec_ifstat
== -1 ||
697 p
->fec_ifstat
== 0) {
699 printf("fec%d: port %s in bundle "
700 "is up\n", priv
->unit
,
704 if (p
->fec_ifstat
== -1 ||
705 p
->fec_ifstat
== 1) {
707 printf("fec%d: port %s in bundle "
708 "is down\n", priv
->unit
,
716 if (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)
717 priv
->fec_ch
= timeout(ng_fec_tick
, priv
, hz
);
723 ng_fec_ifmedia_upd(struct ifnet
*ifp
)
728 static void ng_fec_ifmedia_sts(struct ifnet
*ifp
,
729 struct ifmediareq
*ifmr
)
731 struct ng_fec_private
*priv
;
732 struct ng_fec_bundle
*b
;
733 struct ng_fec_portlist
*p
;
735 priv
= ifp
->if_softc
;
736 b
= &priv
->fec_bundle
;
738 ifmr
->ifm_status
= IFM_AVALID
;
739 TAILQ_FOREACH(p
, &b
->ng_fec_ports
, fec_list
) {
740 if (p
->fec_ifstat
== 1) {
741 ifmr
->ifm_status
|= IFM_ACTIVE
;
750 * Process an ioctl for the virtual interface
753 ng_fec_ioctl(struct ifnet
*ifp
, u_long command
, caddr_t data
)
755 struct ifreq
*const ifr
= (struct ifreq
*) data
;
757 struct ng_fec_private
*priv
;
758 struct ng_fec_bundle
*b
;
760 priv
= ifp
->if_softc
;
761 b
= &priv
->fec_bundle
;
764 ng_fec_print_ioctl(ifp
, command
, data
);
769 /* These two are mostly handled at a higher layer */
772 error
= ether_ioctl(ifp
, command
, data
);
776 if (ifr
->ifr_mtu
>= NG_FEC_MTU_MIN
&&
777 ifr
->ifr_mtu
<= NG_FEC_MTU_MAX
) {
778 struct ng_fec_portlist
*p
;
781 TAILQ_FOREACH(p
, &b
->ng_fec_ports
, fec_list
) {
783 error
= (*bifp
->if_ioctl
)(bifp
, SIOCSIFMTU
,
789 ifp
->if_mtu
= ifr
->ifr_mtu
;
797 * If the interface is marked up and stopped, then start it.
798 * If it is marked down and running, then stop it.
800 if (ifr
->ifr_flags
& IFF_UP
) {
801 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
)) {
803 if (b
->fec_ifcnt
!= 2 &&
804 b
->fec_ifcnt
!= FEC_BUNDLESIZ
) {
805 printf("fec%d: invalid bundle "
806 "size: %d\n", priv
->unit
,
814 * Bubble down changes in promisc mode to
815 * underlying interfaces.
817 if ((ifp
->if_flags
& IFF_PROMISC
) !=
818 (priv
->if_flags
& IFF_PROMISC
)) {
819 ng_fec_setport(ifp
, command
, data
);
820 priv
->if_flags
= ifp
->if_flags
;
823 if (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)
830 ng_fec_ether_setmulti(ifp
);
835 error
= ifmedia_ioctl(ifp
, ifr
, &priv
->ifmedia
, command
);
837 /* Stuff that's not supported */
851 * This routine spies on mbufs received by underlying network device
852 * drivers. When we add an interface to our bundle, we override its
853 * if_input routine with a pointer to ng_fec_input(). This means we
854 * get to look at all the device's packets before sending them to the
855 * real ether_input() for processing by the stack. Once we verify the
856 * packet comes from an interface that's been aggregated into
857 * our bundle, we fix up the rcvif pointer and increment our
858 * packet counters so that it looks like the frames are actually
862 ng_fec_input(struct ifnet
*ifp
, struct mbuf
*m0
)
864 struct ng_node
*node
;
865 struct ng_fec_private
*priv
;
866 struct ng_fec_bundle
*b
;
868 struct ng_fec_portlist
*p
;
871 if (ifp
== NULL
|| m0
== NULL
)
876 /* Sanity check part II */
880 priv
= NG_NODE_PRIVATE(node
);
881 b
= &priv
->fec_bundle
;
884 TAILQ_FOREACH(p
, &b
->ng_fec_ports
, fec_list
) {
885 if (p
->fec_if
== m0
->m_pkthdr
.rcvif
)
889 /* Wasn't meant for us; leave this frame alone. */
894 * Check for a BPF tap on the underlying interface. This
895 * is mainly a debugging aid: it allows tcpdump-ing of an
896 * individual interface in a bundle to work, which it
897 * otherwise would not. BPF tapping of our own aggregate
898 * interface will occur once we call ether_input().
900 BPF_MTAP(m0
->m_pkthdr
.rcvif
, m0
);
902 /* Convince the system that this is our frame. */
903 m0
->m_pkthdr
.rcvif
= bifp
;
906 * Count bytes on an individual interface in a bundle.
907 * The bytes will also be added to the aggregate interface
908 * once we call ether_input().
910 ifp
->if_ibytes
+= m0
->m_pkthdr
.len
;
913 (*bifp
->if_input
)(bifp
, m0
);
919 * Take a quick peek at the packet and see if it's ok for us to use
920 * the inet or inet6 hash methods on it, if they're enabled. We do
921 * this by setting flags in the mbuf header. Once we've made up our
922 * mind what to do, we pass the frame to output vector for further
927 ng_fec_output(struct ifnet
*ifp
, struct mbuf
*m
,
928 struct sockaddr
*dst
, struct rtentry
*rt0
)
930 const priv_p priv
= (priv_p
) ifp
->if_softc
;
931 struct ng_fec_bundle
*b
;
934 /* Check interface flags */
935 if (!((ifp
->if_flags
& IFF_UP
) &&
936 (ifp
->if_drv_flags
& IFF_DRV_RUNNING
))) {
941 b
= &priv
->fec_bundle
;
943 switch (b
->fec_btype
) {
945 m
->m_flags
|= M_FEC_MAC
;
950 * We can't use the INET address port selection
951 * scheme if this isn't an INET packet.
953 if (dst
->sa_family
== AF_INET
)
954 m
->m_flags
|= M_FEC_INET
;
956 else if (dst
->sa_family
== AF_INET6
)
957 m
->m_flags
|= M_FEC_INET6
;
961 if_printf(ifp
, "can't do inet aggregation of non "
964 m
->m_flags
|= M_FEC_MAC
;
969 if_printf(ifp
, "bogus hash type: %d\n",
977 * Pass the frame to the output vector for all the protocol
978 * handling. This will put the ethernet header on the packet
982 error
= (*b
->fec_if_output
)(ifp
, m
, dst
, rt0
);
983 if (priv
->if_error
&& !error
)
984 error
= priv
->if_error
;
990 * Apply a hash to the source and destination addresses in the packet
991 * in order to select an interface. Also check link status and handle
992 * dead links accordingly.
996 ng_fec_choose_port(struct ng_fec_bundle
*b
,
997 struct mbuf
*m
, struct ifnet
**ifp
)
999 struct ether_header
*eh
;
1004 struct ip6_hdr
*ip6
;
1008 struct ng_fec_portlist
*p
;
1012 * If there are only two ports, mask off all but the
1013 * last bit for XORing. If there are 4, mask off all
1014 * but the last 2 bits.
1016 mask
= b
->fec_ifcnt
== 2 ? 0x1 : 0x3;
1017 eh
= mtod(m
, struct ether_header
*);
1019 ip
= (struct ip
*)(mtod(m
, char *) +
1020 sizeof(struct ether_header
));
1022 ip6
= (struct ip6_hdr
*)(mtod(m
, char *) +
1023 sizeof(struct ether_header
));
1028 * The fg_fec_output() routine is supposed to leave a
1029 * flag for us in the mbuf that tells us what hash to
1030 * use, but sometimes a new mbuf is prepended to the
1031 * chain, so we have to search every mbuf in the chain
1032 * to find the flags.
1036 if (m0
->m_flags
& (M_FEC_MAC
|M_FEC_INET
|M_FEC_INET6
))
1043 switch (m0
->m_flags
& (M_FEC_MAC
|M_FEC_INET
|M_FEC_INET6
)) {
1045 port
= (eh
->ether_dhost
[5] ^
1046 eh
->ether_shost
[5]) & mask
;
1050 port
= (ntohl(ip
->ip_dst
.s_addr
) ^
1051 ntohl(ip
->ip_src
.s_addr
)) & mask
;
1055 port
= (ip6
->ip6_dst
.s6_addr
[15] ^
1056 ip6
->ip6_dst
.s6_addr
[15]) & mask
;
1065 TAILQ_FOREACH(p
, &b
->ng_fec_ports
, fec_list
) {
1066 if (port
== p
->fec_idx
)
1071 * Now that we've chosen a port, make sure it's
1072 * alive. If it's not alive, cycle through the bundle
1073 * looking for a port that is alive. If we don't find
1074 * any, return an error.
1076 if (p
->fec_ifstat
!= 1) {
1077 struct ng_fec_portlist
*n
= NULL
;
1079 n
= TAILQ_NEXT(p
, fec_list
);
1081 n
= TAILQ_FIRST(&b
->ng_fec_ports
);
1083 if (n
->fec_ifstat
== 1)
1085 n
= TAILQ_NEXT(n
, fec_list
);
1087 n
= TAILQ_FIRST(&b
->ng_fec_ports
);
1100 * Now that the packet has been run through ether_output(), yank it
1101 * off our own send queue and stick it on the queue for the appropriate
1102 * underlying physical interface. Note that if the interface's send
1103 * queue is full, we save an error status in our private netgraph
1104 * space which will eventually be handed up to ng_fec_output(), which
1105 * will return it to the rest of the IP stack. We need to do this
1106 * in order to duplicate the effect of ether_output() returning ENOBUFS
1107 * when it detects that an interface's send queue is full. There's no
1108 * other way to signal the error status from here since the if_start()
1109 * routine is spec'ed to return void.
1111 * Once the frame is queued, we call ether_output_frame() to initiate
1115 ng_fec_start(struct ifnet
*ifp
)
1117 struct ng_fec_private
*priv
;
1118 struct ng_fec_bundle
*b
;
1119 struct ifnet
*oifp
= NULL
;
1123 priv
= ifp
->if_softc
;
1124 b
= &priv
->fec_bundle
;
1126 IF_DEQUEUE(&ifp
->if_snd
, m0
);
1132 /* Queue up packet on the proper port. */
1133 error
= ng_fec_choose_port(b
, m0
, &oifp
);
1137 priv
->if_error
= ENOBUFS
;
1142 priv
->if_error
= IF_HANDOFF(&oifp
->if_snd
, m0
, oifp
) ? 0 : ENOBUFS
;
1149 * Display an ioctl to the virtual interface
1153 ng_fec_print_ioctl(struct ifnet
*ifp
, int command
, caddr_t data
)
1157 switch (command
& IOC_DIRMASK
) {
1173 log(LOG_DEBUG
, "%s: %s('%c', %d, char[%d])\n",
1178 IOCPARM_LEN(command
));
1182 /************************************************************************
1184 ************************************************************************/
1187 * Constructor for a node
1190 ng_fec_constructor(node_p node
)
1192 char ifname
[NG_FEC_FEC_NAME_MAX
+ 1];
1195 const uint8_t eaddr
[ETHER_ADDR_LEN
] = {0, 0, 0, 0, 0, 0};
1196 struct ng_fec_bundle
*b
;
1199 /* Allocate node and interface private structures */
1200 MALLOC(priv
, priv_p
, sizeof(*priv
), M_NETGRAPH
, M_WAITOK
| M_NULLOK
| M_ZERO
);
1204 ifp
= priv
->ifp
= if_alloc(IFT_ETHER
);
1206 FREE(priv
, M_NETGRAPH
);
1209 b
= &priv
->fec_bundle
;
1211 /* Link them together */
1212 ifp
->if_softc
= priv
;
1214 /* Get an interface unit number */
1215 if ((error
= ng_fec_get_unit(&priv
->unit
)) != 0) {
1217 FREE(priv
, M_NETGRAPH
);
1221 /* Link together node and private info */
1222 NG_NODE_SET_PRIVATE(node
, priv
);
1225 /* Initialize interface structure */
1226 if_initname(ifp
, NG_FEC_FEC_NAME
, priv
->unit
);
1227 ifp
->if_start
= ng_fec_start
;
1228 ifp
->if_ioctl
= ng_fec_ioctl
;
1229 ifp
->if_init
= ng_fec_init
;
1230 ifp
->if_watchdog
= NULL
;
1231 ifp
->if_snd
.ifq_maxlen
= IFQ_MAXLEN
;
1232 ifp
->if_mtu
= NG_FEC_MTU_DEFAULT
;
1233 ifp
->if_flags
= (IFF_SIMPLEX
|IFF_BROADCAST
|IFF_MULTICAST
);
1234 ifp
->if_addrlen
= 0; /* XXX */
1235 ifp
->if_hdrlen
= 0; /* XXX */
1236 ifp
->if_baudrate
= 100000000; /* XXX */
1237 TAILQ_INIT(&ifp
->if_addrhead
); /* XXX useless - done in if_attach */
1239 /* Give this node the same name as the interface (if possible) */
1240 bzero(ifname
, sizeof(ifname
));
1241 strlcpy(ifname
, ifp
->if_xname
, sizeof(ifname
));
1242 if (ng_name_node(node
, ifname
) != 0)
1243 log(LOG_WARNING
, "%s: can't acquire netgraph name\n", ifname
);
1245 /* Attach the interface */
1246 ether_ifattach(ifp
, eaddr
);
1247 callout_handle_init(&priv
->fec_ch
);
1249 /* Override output method with our own */
1250 ifp
->if_output
= ng_fec_output
;
1252 TAILQ_INIT(&b
->ng_fec_ports
);
1255 ifmedia_init(&priv
->ifmedia
, 0,
1256 ng_fec_ifmedia_upd
, ng_fec_ifmedia_sts
);
1257 ifmedia_add(&priv
->ifmedia
, IFM_ETHER
|IFM_NONE
, 0, NULL
);
1258 ifmedia_set(&priv
->ifmedia
, IFM_ETHER
|IFM_NONE
);
1265 * Receive a control message
1268 ng_fec_rcvmsg(node_p node
, item_p item
, hook_p lasthook
)
1270 const priv_p priv
= NG_NODE_PRIVATE(node
);
1271 struct ng_fec_bundle
*b
;
1272 struct ng_mesg
*resp
= NULL
;
1273 struct ng_mesg
*msg
;
1277 NGI_GET_MSG(item
, msg
);
1278 b
= &priv
->fec_bundle
;
1280 switch (msg
->header
.typecookie
) {
1281 case NGM_FEC_COOKIE
:
1282 switch (msg
->header
.cmd
) {
1283 case NGM_FEC_ADD_IFACE
:
1285 error
= ng_fec_addport(priv
, ifname
);
1287 case NGM_FEC_DEL_IFACE
:
1289 error
= ng_fec_delport(priv
, ifname
);
1291 case NGM_FEC_SET_MODE_MAC
:
1292 b
->fec_btype
= FEC_BTYPE_MAC
;
1295 case NGM_FEC_SET_MODE_INET
:
1296 b
->fec_btype
= FEC_BTYPE_INET
;
1299 case NGM_FEC_SET_MODE_INET6
:
1300 b
->fec_btype
= FEC_BTYPE_INET6
;
1313 NG_RESPOND_MSG(error
, node
, item
, resp
);
1319 * Shutdown and remove the node and its associated interface.
1322 ng_fec_shutdown(node_p node
)
1324 const priv_p priv
= NG_NODE_PRIVATE(node
);
1325 struct ng_fec_bundle
*b
;
1326 struct ng_fec_portlist
*p
;
1328 b
= &priv
->fec_bundle
;
1329 ng_fec_stop(priv
->ifp
);
1331 while (!TAILQ_EMPTY(&b
->ng_fec_ports
)) {
1332 p
= TAILQ_FIRST(&b
->ng_fec_ports
);
1333 ng_fec_ether_cmdmulti(priv
->ifp
, p
, 0);
1334 ng_fec_delport(priv
, p
->fec_if
->if_xname
);
1337 ether_ifdetach(priv
->ifp
);
1338 if_free_type(priv
->ifp
, IFT_ETHER
);
1339 ifmedia_removeall(&priv
->ifmedia
);
1340 ng_fec_free_unit(priv
->unit
);
1341 FREE(priv
, M_NETGRAPH
);
1342 NG_NODE_SET_PRIVATE(node
, NULL
);
1343 NG_NODE_UNREF(node
);
1348 * Handle loading and unloading for this node type.
1351 ng_fec_mod_event(module_t mod
, int event
, void *data
)
1357 mtx_init(&ng_fec_mtx
, "ng_fec", NULL
, MTX_DEF
);
1360 mtx_destroy(&ng_fec_mtx
);