if: Move IFF_OACTIVE bit into ifaltq; prepare multiple TX queues support
[dragonfly.git] / sys / netgraph / fec / ng_fec.c
1 /*
2  * ng_fec.c
3  *
4  * Copyright (c) 2001 Berkeley Software Design, Inc.
5  * Copyright (c) 2000, 2001
6  *      Bill Paul <wpaul@osd.bsdi.com>.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
22  *
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.
34  *
35  * $FreeBSD: src/sys/netgraph/ng_fec.c,v 1.1.2.1 2002/11/01 21:39:31 julian Exp $
36  */
37 /*
38  * Copyright (c) 1996-1999 Whistle Communications, Inc.
39  * All rights reserved.
40  * 
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.
51  * 
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
68  * OF SUCH DAMAGE.
69  *
70  * Author: Archie Cobbs <archie@freebsd.org>
71  *
72  * $Whistle: ng_fec.c,v 1.33 1999/11/01 09:24:51 julian Exp $
73  */
74
75 /*
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.
79  *
80  * Interfaces are named fec0, fec1, etc.  New nodes take the
81  * first available interface name.
82  *
83  * This node also includes Berkeley packet filter support.
84  *
85  * Note that this node doesn't need to connect to any other
86  * netgraph nodes in order to do its work.
87  */
88
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>
94 #include <sys/mbuf.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>
102
103 #include <net/if.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/ifq_var.h>
109 #include <net/bpf.h>
110 #include <net/ethernet.h>
111
112 #include "opt_inet.h"
113 #include "opt_inet6.h"
114
115 #include <netinet/in.h>
116 #ifdef INET
117 #include <netinet/in_systm.h>
118 #include <netinet/ip.h>
119 #endif
120
121 #ifdef INET6
122 #include <netinet/ip6.h>
123 #endif
124
125 #include <netgraph/ng_message.h>
126 #include <netgraph/netgraph.h>
127 #include <netgraph/ng_parse.h>
128 #include "ng_fec.h"
129
130 #define IFP2NG(ifp)  ((struct ng_node *)((struct arpcom *)(ifp))->ac_netgraph)
131 #define FEC_INC(x, y)   (x) = (x + 1) % y
132
133 /*
134  * Current fast etherchannel implementations use either 2 or 4
135  * ports, so for now we limit the maximum bundle size to 4 interfaces.
136  */
137 #define FEC_BUNDLESIZ   4
138
139 struct ng_fec_portlist {
140         struct ifnet            *fec_if;
141         int                     fec_idx;
142         int                     fec_ifstat;
143         struct ether_addr       fec_mac;
144         TAILQ_ENTRY(ng_fec_portlist) fec_list;
145 };
146
147 struct ng_fec_bundle {
148         TAILQ_HEAD(,ng_fec_portlist) ng_fec_ports;
149         int                     fec_ifcnt;
150         int                     fec_btype;
151 };
152
153 #define FEC_BTYPE_MAC           0x01
154 #define FEC_BTYPE_INET          0x02
155 #define FEC_BTYPE_INET6         0x03
156
157 /* Node private data */
158 struct ng_fec_private {
159         struct arpcom arpcom;
160         struct ifmedia ifmedia;
161         int     if_flags;
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 fec_timeout;     /* callout for ticker */
167         int     (*real_if_output)(struct ifnet *, struct mbuf *,
168                                   struct sockaddr *, struct rtentry *);
169 };
170 typedef struct ng_fec_private *priv_p;
171
172 /* Interface methods */
173 static void     ng_fec_input(struct ifnet *, struct mbuf **);
174 static void     ng_fec_start(struct ifnet *ifp);
175 static int      ng_fec_choose_port(struct ng_fec_bundle *b,
176                         struct mbuf *m, struct ifnet **ifp);
177 static int      ng_fec_setport(struct ifnet *ifp, u_long cmd, caddr_t data);
178 static void     ng_fec_init(void *arg);
179 static void     ng_fec_stop(struct ifnet *ifp);
180 static int      ng_fec_ifmedia_upd(struct ifnet *ifp);
181 static void     ng_fec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
182 static int      ng_fec_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,
183                              struct ucred *);
184 static int      ng_fec_output(struct ifnet *ifp, struct mbuf *m0,
185                         struct sockaddr *dst, struct rtentry *rt0);
186 static void     ng_fec_tick(void *arg);
187 static int      ng_fec_addport(struct ng_fec_private *priv, char *iface);
188 static int      ng_fec_delport(struct ng_fec_private *priv, char *iface);
189
190 #ifdef DEBUG
191 static void     ng_fec_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
192 #endif
193
194 /* Netgraph methods */
195 static ng_constructor_t ng_fec_constructor;
196 static ng_rcvmsg_t      ng_fec_rcvmsg;
197 static ng_shutdown_t    ng_fec_rmnode;
198
199 /* List of commands and how to convert arguments to/from ASCII */
200 static const struct ng_cmdlist ng_fec_cmds[] = {
201         {
202           NGM_FEC_COOKIE,
203           NGM_FEC_ADD_IFACE,
204           "add_iface",
205           &ng_parse_string_type,
206           NULL,
207         },
208         {
209           NGM_FEC_COOKIE,
210           NGM_FEC_DEL_IFACE,
211           "del_iface",
212           &ng_parse_string_type,
213           NULL,
214         },
215         {
216           NGM_FEC_COOKIE,
217           NGM_FEC_SET_MODE_MAC,
218           "set_mode_mac",
219           NULL,
220           NULL,
221         },
222         {
223           NGM_FEC_COOKIE,
224           NGM_FEC_SET_MODE_INET,
225           "set_mode_inet",
226           NULL,
227           NULL,
228         },
229         { 0 }
230 };
231
232 /* Node type descriptor */
233 static struct ng_type typestruct = {
234         NG_VERSION,
235         NG_FEC_NODE_TYPE,
236         NULL,
237         ng_fec_constructor,
238         ng_fec_rcvmsg,
239         ng_fec_rmnode,
240         NULL,
241         NULL,
242         NULL,
243         NULL,
244         NULL,
245         NULL,
246         ng_fec_cmds
247 };
248 NETGRAPH_INIT(fec, &typestruct);
249
250 /* We keep a bitmap indicating which unit numbers are free.
251    One means the unit number is free, zero means it's taken. */
252 static int      *ng_fec_units = NULL;
253 static int      ng_fec_units_len = 0;
254 static int      ng_units_in_use = 0;
255
256 #define UNITS_BITSPERWORD       (sizeof(*ng_fec_units) * NBBY)
257
258 /*
259  * Find the first free unit number for a new interface.
260  * Increase the size of the unit bitmap as necessary.
261  */
262 static __inline__ int
263 ng_fec_get_unit(int *unit)
264 {
265         int index, bit;
266
267         for (index = 0; index < ng_fec_units_len
268             && ng_fec_units[index] == 0; index++);
269         if (index == ng_fec_units_len) {                /* extend array */
270                 int i, *newarray, newlen;
271
272                 newlen = (2 * ng_fec_units_len) + 4;
273                 newarray = kmalloc(newlen * sizeof(*ng_fec_units),
274                                    M_NETGRAPH, M_NOWAIT);
275                 if (newarray == NULL)
276                         return (ENOMEM);
277                 bcopy(ng_fec_units, newarray,
278                     ng_fec_units_len * sizeof(*ng_fec_units));
279                 for (i = ng_fec_units_len; i < newlen; i++)
280                         newarray[i] = ~0;
281                 if (ng_fec_units != NULL)
282                         kfree(ng_fec_units, M_NETGRAPH);
283                 ng_fec_units = newarray;
284                 ng_fec_units_len = newlen;
285         }
286         bit = ffs(ng_fec_units[index]) - 1;
287         KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
288             ("%s: word=%d bit=%d", __func__, ng_fec_units[index], bit));
289         ng_fec_units[index] &= ~(1 << bit);
290         *unit = (index * UNITS_BITSPERWORD) + bit;
291         ng_units_in_use++;
292         return (0);
293 }
294
295 /*
296  * Free a no longer needed unit number.
297  */
298 static __inline__ void
299 ng_fec_free_unit(int unit)
300 {
301         int index, bit;
302
303         index = unit / UNITS_BITSPERWORD;
304         bit = unit % UNITS_BITSPERWORD;
305         KASSERT(index < ng_fec_units_len,
306             ("%s: unit=%d len=%d", __func__, unit, ng_fec_units_len));
307         KASSERT((ng_fec_units[index] & (1 << bit)) == 0,
308             ("%s: unit=%d is free", __func__, unit));
309         ng_fec_units[index] |= (1 << bit);
310         /*
311          * XXX We could think about reducing the size of ng_fec_units[]
312          * XXX here if the last portion is all ones
313          * XXX At least free it if no more units.
314          * Needed if we are eventually be able to unload.
315          */
316         ng_units_in_use++;
317         if (ng_units_in_use == 0) { /* XXX make SMP safe */
318                 kfree(ng_fec_units, M_NETGRAPH);
319                 ng_fec_units_len = 0;
320                 ng_fec_units = NULL;
321         }
322 }
323
324 /************************************************************************
325                         INTERFACE STUFF
326  ************************************************************************/
327
328 static int
329 ng_fec_addport(struct ng_fec_private *priv, char *iface)
330 {
331         struct ng_fec_bundle    *b;
332         struct ifnet            *ifp, *bifp;
333         struct arpcom           *ac;
334         struct sockaddr_dl      *sdl;
335         struct ng_fec_portlist  *p, *new;
336
337         if (priv == NULL || iface == NULL)
338                 return(EINVAL);
339
340         b = &priv->fec_bundle;
341         ifp = &priv->arpcom.ac_if;
342
343         /* Find the interface */
344         bifp = ifunit(iface);
345         if (bifp == NULL) {
346                 kprintf("fec%d: tried to add iface %s, which "
347                     "doesn't seem to exist\n", priv->unit, iface);
348                 return(ENOENT);
349         }
350
351         /* See if we have room in the bundle */
352         if (b->fec_ifcnt == FEC_BUNDLESIZ) {
353                 kprintf("fec%d: can't add new iface; bundle is full\n",
354                     priv->unit);
355                 return(ENOSPC);
356         }
357
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                         kprintf("fec%d: iface %s is already in this "
362                             "bundle\n", priv->unit, iface);
363                         return(EINVAL);
364                 }
365         }
366
367         /* Allocate new list entry. */
368         new = kmalloc(sizeof(struct ng_fec_portlist), M_NETGRAPH, M_NOWAIT);
369         if (new == NULL)
370                 return(ENOMEM);
371
372         ac = (struct arpcom *)bifp;
373         ac->ac_netgraph = priv->node;
374
375         /*
376          * If this is the first interface added to the bundle,
377          * use its MAC address for the virtual interface (and,
378          * by extension, all the other ports in the bundle).
379          */
380         if (b->fec_ifcnt == 0) {
381                 sdl = IF_LLSOCKADDR(ifp);
382                 bcopy((char *)ac->ac_enaddr,
383                     priv->arpcom.ac_enaddr, ETHER_ADDR_LEN);
384                 bcopy((char *)ac->ac_enaddr,
385                     LLADDR(sdl), ETHER_ADDR_LEN);
386         }
387
388         b->fec_btype = FEC_BTYPE_MAC;
389         new->fec_idx = b->fec_ifcnt;
390         b->fec_ifcnt++;
391
392         /* Save the real MAC address. */
393         bcopy((char *)ac->ac_enaddr,
394             (char *)&new->fec_mac, ETHER_ADDR_LEN);
395
396         /* Set up phony MAC address. */
397         sdl = IF_LLSOCKADDR(bifp);
398         bcopy(priv->arpcom.ac_enaddr, ac->ac_enaddr, ETHER_ADDR_LEN);
399         bcopy(priv->arpcom.ac_enaddr, LLADDR(sdl), ETHER_ADDR_LEN);
400
401         /* Add to the queue */
402         new->fec_if = bifp;
403         TAILQ_INSERT_TAIL(&b->ng_fec_ports, new, fec_list);
404
405         return(0);
406 }
407
408 static int
409 ng_fec_delport(struct ng_fec_private *priv, char *iface)
410 {
411         struct ng_fec_bundle    *b;
412         struct ifnet            *ifp, *bifp;
413         struct arpcom           *ac;
414         struct sockaddr_dl      *sdl;
415         struct ng_fec_portlist  *p;
416
417         if (priv == NULL || iface == NULL)
418                 return(EINVAL);
419
420         b = &priv->fec_bundle;
421         ifp = &priv->arpcom.ac_if;
422
423         /* Find the interface */
424         bifp = ifunit(iface);
425         if (bifp == NULL) {
426                 kprintf("fec%d: tried to remove iface %s, which "
427                     "doesn't seem to exist\n", priv->unit, iface);
428                 return(ENOENT);
429         }
430
431         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
432                 if (p->fec_if == bifp)
433                         break;
434         }
435
436         if (p == NULL) {
437                 kprintf("fec%d: tried to remove iface %s which "
438                     "is not in our bundle\n", priv->unit, iface);
439                 return(EINVAL);
440         }
441
442         /* Stop interface */
443         bifp->if_flags &= ~IFF_UP;
444         bifp->if_ioctl(bifp, SIOCSIFFLAGS, NULL, NULL);
445
446         /* Restore MAC address. */
447         ac = (struct arpcom *)bifp;
448         sdl = IF_LLSOCKADDR(bifp);
449         bcopy((char *)&p->fec_mac, ac->ac_enaddr, ETHER_ADDR_LEN);
450         bcopy((char *)&p->fec_mac, LLADDR(sdl), ETHER_ADDR_LEN);
451
452         /* Delete port */
453         TAILQ_REMOVE(&b->ng_fec_ports, p, fec_list);
454         kfree(p, M_NETGRAPH);
455         b->fec_ifcnt--;
456
457         return(0);
458 }
459
460 /*
461  * Pass an ioctl command down to all the underyling interfaces in a
462  * bundle. Used for setting multicast filters and flags.
463  */
464 static int 
465 ng_fec_setport(struct ifnet *ifp, u_long command, caddr_t data)
466 {
467         struct ng_fec_private   *priv;
468         struct ng_fec_bundle    *b;
469         struct ifnet            *oifp;
470         struct ng_fec_portlist  *p;
471
472         priv = ifp->if_softc;
473         b = &priv->fec_bundle;
474
475         ifnet_deserialize_all(ifp);     /* XXX */
476         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
477                 oifp = p->fec_if;
478                 if (oifp != NULL) {
479                         ifnet_serialize_all(oifp);
480                         oifp->if_ioctl(oifp, command, data, NULL);
481                         ifnet_deserialize_all(oifp);
482                 }
483         }
484         ifnet_serialize_all(ifp);
485
486         return(0);
487 }
488
489 static void
490 ng_fec_init(void *arg)
491 {
492         struct ng_fec_private   *priv;
493         struct ng_fec_bundle    *b;
494         struct ifnet            *ifp, *bifp;
495         struct ng_fec_portlist  *p;
496
497         ifp = arg;
498         priv = ifp->if_softc;
499         b = &priv->fec_bundle;
500
501         if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
502                 kprintf("fec%d: invalid bundle "
503                     "size: %d\n", priv->unit,
504                     b->fec_ifcnt);
505                 return;
506         }
507
508         ng_fec_stop(ifp);
509
510         ifnet_deserialize_all(ifp);     /* XXX */
511         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
512                 bifp = p->fec_if;
513                 ifnet_serialize_all(bifp);
514                 bifp->if_flags |= IFF_UP;
515                 bifp->if_ioctl(bifp, SIOCSIFFLAGS, NULL, NULL);
516                 /* mark iface as up and let the monitor check it */
517                 p->fec_ifstat = -1;
518                 ifnet_deserialize_all(bifp);
519         }
520         ifnet_serialize_all(ifp);
521
522         callout_reset(&priv->fec_timeout, hz, ng_fec_tick, priv);
523 }
524
525 static void
526 ng_fec_stop(struct ifnet *ifp)
527 {
528         struct ng_fec_private   *priv;
529         struct ng_fec_bundle    *b;
530         struct ifnet            *bifp;
531         struct ng_fec_portlist  *p;
532
533         priv = ifp->if_softc;
534         b = &priv->fec_bundle;
535
536         ifnet_deserialize_all(ifp);     /* XXX */
537         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
538                 bifp = p->fec_if;
539                 ifnet_serialize_all(bifp);
540                 bifp->if_flags &= ~IFF_UP;
541                 bifp->if_ioctl(bifp, SIOCSIFFLAGS, NULL, NULL);
542                 ifnet_deserialize_all(bifp);
543         }
544         ifnet_serialize_all(ifp);
545
546         callout_stop(&priv->fec_timeout);
547 }
548
549 static void
550 ng_fec_tick(void *arg)
551 {
552         struct ng_fec_private   *priv;
553         struct ng_fec_bundle    *b;
554         struct ifmediareq       ifmr;
555         struct ifnet            *ifp;
556         struct ng_fec_portlist  *p;
557         int                     error = 0;
558
559         priv = arg;
560         b = &priv->fec_bundle;
561
562         /*
563          * Note: serializer for parent interface not held on entry, and
564          * cannot be held during the loop to avoid a deadlock.
565          */
566         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
567                 bzero((char *)&ifmr, sizeof(ifmr));
568                 ifp = p->fec_if;
569                 ifnet_serialize_all(ifp);
570                 error = ifp->if_ioctl(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr, NULL);
571                 if (error) {
572                         kprintf("fec%d: failed to check status "
573                             "of link %s\n", priv->unit, ifp->if_xname);
574                         ifnet_deserialize_all(ifp);
575                         continue;
576                 }
577
578                 if (ifmr.ifm_status & IFM_AVALID &&
579                     IFM_TYPE(ifmr.ifm_active) == IFM_ETHER) {
580                         if (ifmr.ifm_status & IFM_ACTIVE) {
581                                 if (p->fec_ifstat == -1 ||
582                                     p->fec_ifstat == 0) {
583                                         p->fec_ifstat = 1;
584                                         kprintf("fec%d: port %s in bundle "
585                                             "is up\n", priv->unit,
586                                             ifp->if_xname);
587                                 }
588                         } else {
589                                 if (p->fec_ifstat == -1 ||
590                                     p->fec_ifstat == 1) {
591                                         p->fec_ifstat = 0;
592                                         kprintf("fec%d: port %s in bundle "
593                                             "is down\n", priv->unit,
594                                             ifp->if_xname);
595                                 }
596                         }
597                 }
598                 ifnet_deserialize_all(ifp);
599         }
600
601         ifp = &priv->arpcom.ac_if;
602         if (ifp->if_flags & IFF_RUNNING)
603                 callout_reset(&priv->fec_timeout, hz, ng_fec_tick, priv);
604 }
605
606 static int
607 ng_fec_ifmedia_upd(struct ifnet *ifp)
608 {
609         return(0);
610 }
611
612 static void
613 ng_fec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
614 {
615         struct ng_fec_private   *priv;
616         struct ng_fec_bundle    *b;
617         struct ng_fec_portlist  *p;
618
619         priv = ifp->if_softc;
620         b = &priv->fec_bundle;
621
622         ifmr->ifm_status = IFM_AVALID;
623         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
624                 if (p->fec_ifstat) {
625                         ifmr->ifm_status |= IFM_ACTIVE;
626                         break;
627                 }
628         }
629 }
630
631 /*
632  * Process an ioctl for the virtual interface
633  */
634 static int
635 ng_fec_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
636 {
637         struct ifreq *const ifr = (struct ifreq *) data;
638         int error = 0;
639         struct ng_fec_private   *priv;
640         struct ng_fec_bundle    *b;
641
642         priv = ifp->if_softc;
643         b = &priv->fec_bundle;
644
645 #ifdef DEBUG
646         ng_fec_print_ioctl(ifp, command, data);
647 #endif
648         crit_enter();
649         switch (command) {
650
651         /* These two are mostly handled at a higher layer */
652         case SIOCSIFADDR:
653         case SIOCGIFADDR:
654         case SIOCSIFMTU:
655                 error = ether_ioctl(ifp, command, data);
656                 break;
657
658         /* Set flags */
659         case SIOCSIFFLAGS:
660                 /*
661                  * If the interface is marked up and stopped, then start it.
662                  * If it is marked down and running, then stop it.
663                  */
664                 if (ifr->ifr_flags & IFF_UP) {
665                         if (!(ifp->if_flags & IFF_RUNNING)) {
666                                 /* Sanity. */
667                                 if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
668                                         kprintf("fec%d: invalid bundle "
669                                             "size: %d\n", priv->unit,
670                                             b->fec_ifcnt);
671                                         error = EINVAL;
672                                         break;
673                                 }
674                                 ifq_clr_oactive(&ifp->if_snd);
675                                 ifp->if_flags |= IFF_RUNNING;
676                                 ng_fec_init(ifp);
677                         }
678                         /*
679                          * Bubble down changes in promisc mode to
680                          * underlying interfaces.
681                          */
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;
686                         }
687                 } else {
688                         if (ifp->if_flags & IFF_RUNNING) {
689                                 ifp->if_flags &= ~IFF_RUNNING;
690                                 ifq_clr_oactive(&ifp->if_snd);
691                         }
692                         ng_fec_stop(ifp);
693                 }
694                 break;
695
696         case SIOCADDMULTI:
697         case SIOCDELMULTI:
698                 ng_fec_setport(ifp, command, data);
699                 error = 0;
700                 break;
701         case SIOCGIFMEDIA:
702         case SIOCSIFMEDIA:
703                 error = ifmedia_ioctl(ifp, ifr, &priv->ifmedia, command);
704                 break;
705         /* Stuff that's not supported */
706         case SIOCSIFPHYS:
707                 error = EOPNOTSUPP;
708                 break;
709
710         default:
711                 error = EINVAL;
712                 break;
713         }
714         crit_exit();
715         return (error);
716 }
717
718 /*
719  * This routine spies on mbufs passing through ether_input(). If
720  * they come from one of the interfaces that are aggregated into
721  * our bundle, we fix up the ifnet pointer and increment our
722  * packet counters so that it looks like the frames are actually
723  * coming from us.
724  */
725 static void 
726 ng_fec_input(struct ifnet *ifp, struct mbuf **m0)
727 {
728         struct ng_node          *node;
729         struct ng_fec_private   *priv;
730         struct ng_fec_bundle    *b;
731         struct mbuf             *m;
732         struct ifnet            *bifp;
733         struct ng_fec_portlist  *p;
734
735         /* Sanity check */
736         if (ifp == NULL || m0 == NULL)
737                 return;
738
739         node = IFP2NG(ifp);
740
741         /* Sanity check part II */
742         if (node == NULL)
743                 return;
744
745         priv = node->private;
746         b = &priv->fec_bundle;
747         bifp = &priv->arpcom.ac_if;
748
749         m = *m0;
750         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
751                 if (p->fec_if == m->m_pkthdr.rcvif)
752                         break;
753         }
754
755         /* Wasn't meant for us; leave this frame alone. */
756         if (p == NULL)
757                 return;
758
759         /* Pretend this is our frame. */
760         m->m_pkthdr.rcvif = bifp;
761         bifp->if_ipackets++;
762         bifp->if_ibytes += m->m_pkthdr.len;
763
764         if (bifp->if_bpf) {
765                 bpf_gettoken();
766                 if (bifp->if_bpf)
767                         bpf_mtap(bifp->if_bpf, m);
768                 bpf_reltoken();
769         }
770 }
771
772 /*
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
777  * processing.
778  */
779
780 static int
781 ng_fec_output_serialized(struct ifnet *ifp, struct mbuf *m,
782                          struct sockaddr *dst, struct rtentry *rt0)
783 {
784         const priv_p priv = (priv_p) ifp->if_softc;
785         struct ng_fec_bundle *b;
786         int error;
787
788         /* Check interface flags */
789         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
790                 m_freem(m);
791                 return (ENETDOWN);
792         }
793
794         b = &priv->fec_bundle;
795
796         switch (b->fec_btype) {
797         case FEC_BTYPE_MAC:
798                 m->m_flags |= M_FEC_MAC;
799                 break;
800 #ifdef INET
801         case FEC_BTYPE_INET:
802                 /*
803                  * We can't use the INET address port selection
804                  * scheme if this isn't an INET packet.
805                  */
806                 if (dst->sa_family == AF_INET)
807                         m->m_flags |= M_FEC_INET;
808 #ifdef INET6
809                 else if (dst->sa_family == AF_INET6)
810                         m->m_flags |= M_FEC_INET6;
811 #endif
812                 else {
813 #ifdef DEBUG
814                         kprintf("%s: can't do inet aggregation of non "
815                             "inet packet\n", ifp->if_xname);
816 #endif
817                         m->m_flags |= M_FEC_MAC;
818                 }
819                 break;
820 #endif
821         default:
822                 kprintf("%s: bogus hash type: %d\n", ifp->if_xname,
823                     b->fec_btype);
824                 m_freem(m);
825                 return(EINVAL);
826                 break;
827         }
828
829         /*
830          * Pass the frame to ether_output() for all the protocol
831          * handling. This will put the ethernet header on the packet
832          * for us.
833          */
834         priv->if_error = 0;
835         error = priv->real_if_output(ifp, m, dst, rt0);
836         if (priv->if_error && !error)
837                 error = priv->if_error;
838
839         return(error);
840 }
841
842 static int
843 ng_fec_output(struct ifnet *ifp, struct mbuf *m,
844               struct sockaddr *dst, struct rtentry *rt0)
845 {
846         int error;
847
848         ifnet_serialize_tx(ifp);
849         error = ng_fec_output_serialized(ifp, m, dst, rt0);
850         ifnet_deserialize_tx(ifp);
851
852         return error;
853 }
854
855 /*
856  * Apply a hash to the source and destination addresses in the packet
857  * in order to select an interface. Also check link status and handle
858  * dead links accordingly.
859  */
860
861 static int
862 ng_fec_choose_port(struct ng_fec_bundle *b,
863         struct mbuf *m, struct ifnet **ifp)
864 {
865         struct ether_header     *eh;
866         struct mbuf             *m0;
867 #ifdef INET
868         struct ip               *ip;
869 #ifdef INET6
870         struct ip6_hdr          *ip6;
871 #endif
872 #endif
873
874         struct ng_fec_portlist  *p;
875         int                     port = 0, mask;
876
877         /*
878          * If there are only two ports, mask off all but the
879          * last bit for XORing. If there are 4, mask off all
880          * but the last 2 bits.
881          */
882         mask = b->fec_ifcnt == 2 ? 0x1 : 0x3;
883         eh = mtod(m, struct ether_header *);
884 #ifdef INET
885         ip = (struct ip *)(mtod(m, char *) +
886             sizeof(struct ether_header));
887 #ifdef INET6
888         ip6 = (struct ip6_hdr *)(mtod(m, char *) +
889             sizeof(struct ether_header));
890 #endif
891 #endif
892
893         /*
894          * The fg_fec_output() routine is supposed to leave a
895          * flag for us in the mbuf that tells us what hash to
896          * use, but sometimes a new mbuf is prepended to the
897          * chain, so we have to search every mbuf in the chain
898          * to find the flags.
899          */
900         m0 = m;
901         while (m0) {
902                 if (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6))
903                         break;
904                 m0 = m0->m_next;
905         }
906         if (m0 == NULL)
907                 return(EINVAL);
908
909         switch (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6)) {
910         case M_FEC_MAC:
911                 port = (eh->ether_dhost[5] ^
912                     eh->ether_shost[5]) & mask;
913                 break;
914 #ifdef INET
915         case M_FEC_INET:
916                 port = (ntohl(ip->ip_dst.s_addr) ^
917                     ntohl(ip->ip_src.s_addr)) & mask;
918                 break;
919 #ifdef INET6
920         case M_FEC_INET6:
921                 port = (ip6->ip6_dst.s6_addr[15] ^
922                     ip6->ip6_dst.s6_addr[15]) & mask;
923                 break;
924 #endif
925 #endif
926         default:
927                 return(EINVAL);
928                         break;
929         }
930
931         TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
932                 if (port == p->fec_idx)
933                         break;
934         }
935
936         /*
937          * Now that we've chosen a port, make sure it's
938          * alive. If it's not alive, cycle through the bundle
939          * looking for a port that is alive. If we don't find
940          * any, return an error.
941          */
942         if (p->fec_ifstat != 1) {
943                 struct ng_fec_portlist  *n = NULL;
944
945                 n = TAILQ_NEXT(p, fec_list);
946                 if (n == NULL)
947                         n = TAILQ_FIRST(&b->ng_fec_ports);
948                 while (n != p) {
949                         if (n->fec_ifstat == 1)
950                                 break;
951                         n = TAILQ_NEXT(n, fec_list);
952                         if (n == NULL)
953                                 n = TAILQ_FIRST(&b->ng_fec_ports);
954                 }
955                 if (n == p)
956                         return(EAGAIN);
957                 p = n;
958         }
959
960         *ifp = p->fec_if;
961
962         return(0);
963 }
964
965 /*
966  * Now that the packet has been run through ether_output(), yank it
967  * off our own send queue and stick it on the queue for the appropriate
968  * underlying physical interface. Note that if the interface's send
969  * queue is full, we save an error status in our private netgraph
970  * space which will eventually be handed up to ng_fec_output(), which
971  * will return it to the rest of the IP stack. We need to do this
972  * in order to duplicate the effect of ether_output() returning ENOBUFS
973  * when it detects that an interface's send queue is full. There's no
974  * other way to signal the error status from here since the if_start()
975  * routine is spec'ed to return void.
976  *
977  * Once the frame is queued, we call ether_output_frame() to initiate
978  * transmission.
979  */
980 static void
981 ng_fec_start(struct ifnet *ifp)
982 {
983         struct ng_fec_private   *priv;
984         struct ng_fec_bundle    *b;
985         struct ifnet            *oifp = NULL;
986         struct mbuf             *m0;
987         int                     error;
988
989         priv = ifp->if_softc;
990         b = &priv->fec_bundle;
991
992         IF_DEQUEUE(&ifp->if_snd, m0);
993         if (m0 == NULL)
994                 return;
995
996         BPF_MTAP(ifp, m0);
997
998         /* Queue up packet on the proper port. */
999         error = ng_fec_choose_port(b, m0, &oifp);
1000         if (error) {
1001                 ifp->if_ierrors++;
1002                 m_freem(m0);
1003                 priv->if_error = ENOBUFS;
1004                 return;
1005         }
1006         ifp->if_opackets++;
1007
1008         /*
1009          * Release current iface's serializer to avoid possible dead lock
1010          */
1011         priv->if_error = ether_output_frame(oifp, m0);
1012 }
1013
1014 #ifdef DEBUG
1015 /*
1016  * Display an ioctl to the virtual interface
1017  */
1018
1019 static void
1020 ng_fec_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
1021 {
1022         char   *str;
1023
1024         switch (command & IOC_DIRMASK) {
1025         case IOC_VOID:
1026                 str = "IO";
1027                 break;
1028         case IOC_OUT:
1029                 str = "IOR";
1030                 break;
1031         case IOC_IN:
1032                 str = "IOW";
1033                 break;
1034         case IOC_INOUT:
1035                 str = "IORW";
1036                 break;
1037         default:
1038                 str = "IO??";
1039         }
1040         log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
1041                ifp->if_xname,
1042                str,
1043                IOCGROUP(command),
1044                command & 0xff,
1045                IOCPARM_LEN(command));
1046 }
1047 #endif /* DEBUG */
1048
1049 /************************************************************************
1050                         NETGRAPH NODE STUFF
1051  ************************************************************************/
1052
1053 /*
1054  * Constructor for a node
1055  */
1056 static int
1057 ng_fec_constructor(node_p *nodep)
1058 {
1059         char ifname[NG_FEC_FEC_NAME_MAX + 1];
1060         struct ifnet *ifp;
1061         node_p node;
1062         priv_p priv;
1063         struct ng_fec_bundle *b;
1064         int error = 0;
1065
1066         /* Allocate node and interface private structures */
1067         priv = kmalloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
1068         if (priv == NULL)
1069                 return (ENOMEM);
1070
1071         ifp = &priv->arpcom.ac_if;
1072         b = &priv->fec_bundle;
1073
1074         /* Link them together */
1075         ifp->if_softc = priv;
1076
1077         /* Get an interface unit number */
1078         if ((error = ng_fec_get_unit(&priv->unit)) != 0) {
1079                 kfree(ifp, M_NETGRAPH);
1080                 kfree(priv, M_NETGRAPH);
1081                 return (error);
1082         }
1083
1084         /* Call generic node constructor */
1085         if ((error = ng_make_node_common(&typestruct, nodep)) != 0) {
1086                 ng_fec_free_unit(priv->unit);
1087                 kfree(ifp, M_NETGRAPH);
1088                 kfree(priv, M_NETGRAPH);
1089                 return (error);
1090         }
1091         node = *nodep;
1092
1093         /* Link together node and private info */
1094         node->private = priv;
1095         priv->node = node;
1096         priv->arpcom.ac_netgraph = node;
1097
1098         /* Initialize interface structure */
1099         if_initname(ifp, NG_FEC_FEC_NAME, priv->unit);
1100         ifp->if_start = ng_fec_start;
1101         ifp->if_ioctl = ng_fec_ioctl;
1102         ifp->if_init = ng_fec_init;
1103         ifp->if_watchdog = NULL;
1104         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
1105         ifp->if_mtu = NG_FEC_MTU_DEFAULT;
1106         ifp->if_flags = (IFF_SIMPLEX|IFF_BROADCAST|IFF_MULTICAST);
1107         ifp->if_type = IFT_PROPVIRTUAL;         /* XXX */
1108         ifp->if_addrlen = 0;                    /* XXX */
1109         ifp->if_hdrlen = 0;                     /* XXX */
1110         ifp->if_baudrate = 100000000;           /* XXX */
1111
1112         /* Give this node the same name as the interface (if possible) */
1113         bzero(ifname, sizeof(ifname));
1114         strlcpy(ifname, ifp->if_xname, sizeof(ifname));
1115         if (ng_name_node(node, ifname) != 0)
1116                 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
1117
1118         /* Grab hold of the ether_input pipe. */
1119         if (ng_ether_input_p == NULL)
1120                 ng_ether_input_p = ng_fec_input;
1121
1122         /* Attach the interface */
1123         ether_ifattach(ifp, priv->arpcom.ac_enaddr, NULL);
1124         priv->real_if_output = ifp->if_output;
1125         ifp->if_output = ng_fec_output;
1126         callout_init(&priv->fec_timeout);
1127
1128         TAILQ_INIT(&b->ng_fec_ports);
1129         b->fec_ifcnt = 0;
1130
1131         ifmedia_init(&priv->ifmedia, 0,
1132             ng_fec_ifmedia_upd, ng_fec_ifmedia_sts);
1133         ifmedia_add(&priv->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
1134         ifmedia_set(&priv->ifmedia, IFM_ETHER|IFM_NONE);
1135
1136         /* Done */
1137         return (0);
1138 }
1139
1140 /*
1141  * Receive a control message
1142  */
1143 static int
1144 ng_fec_rcvmsg(node_p node, struct ng_mesg *msg,
1145                 const char *retaddr, struct ng_mesg **rptr)
1146 {
1147         const priv_p priv = node->private;
1148         struct ng_fec_bundle    *b;
1149         struct ng_mesg *resp = NULL;
1150         char *ifname;
1151         int error = 0;
1152
1153         b = &priv->fec_bundle;
1154
1155         switch (msg->header.typecookie) {
1156         case NGM_FEC_COOKIE:
1157                 switch (msg->header.cmd) {
1158                 case NGM_FEC_ADD_IFACE:
1159                         ifname = msg->data;
1160                         error = ng_fec_addport(priv, ifname);
1161                         break;
1162                 case NGM_FEC_DEL_IFACE:
1163                         ifname = msg->data;
1164                         error = ng_fec_delport(priv, ifname);
1165                         break;
1166                 case NGM_FEC_SET_MODE_MAC:
1167                         b->fec_btype = FEC_BTYPE_MAC;
1168                         break;
1169 #ifdef INET
1170                 case NGM_FEC_SET_MODE_INET:
1171                         b->fec_btype = FEC_BTYPE_INET;
1172                         break;
1173 #ifdef INET6
1174                 case NGM_FEC_SET_MODE_INET6:
1175                         b->fec_btype = FEC_BTYPE_INET6;
1176                         break;
1177 #endif
1178 #endif
1179                 default:
1180                         error = EINVAL;
1181                         break;
1182                 }
1183                 break;
1184         default:
1185                 error = EINVAL;
1186                 break;
1187         }
1188         if (rptr)
1189                 *rptr = resp;
1190         else if (resp)
1191                 kfree(resp, M_NETGRAPH);
1192         kfree(msg, M_NETGRAPH);
1193         return (error);
1194 }
1195
1196 /*
1197  * Shutdown and remove the node and its associated interface.
1198  */
1199 static int
1200 ng_fec_rmnode(node_p node)
1201 {
1202         const priv_p priv = node->private;
1203         struct ng_fec_bundle *b;
1204         struct ng_fec_portlist  *p;
1205         char ifname[IFNAMSIZ];
1206
1207         b = &priv->fec_bundle;
1208         ng_fec_stop(&priv->arpcom.ac_if);
1209
1210         while (!TAILQ_EMPTY(&b->ng_fec_ports)) {
1211                 p = TAILQ_FIRST(&b->ng_fec_ports);
1212                 ksprintf(ifname, "%s",
1213                     p->fec_if->if_xname); /* XXX: strings */
1214                 ng_fec_delport(priv, ifname);
1215         }
1216
1217         ng_cutlinks(node);
1218         ng_unname(node);
1219         if (ng_ether_input_p != NULL)
1220                 ng_ether_input_p = NULL;
1221         ether_ifdetach(&priv->arpcom.ac_if);
1222         ifmedia_removeall(&priv->ifmedia);
1223         ng_fec_free_unit(priv->unit);
1224         kfree(priv, M_NETGRAPH);
1225         node->private = NULL;
1226         ng_unref(node);
1227         return (0);
1228 }