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