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.2/sys/netgraph/ng_fec.c 121816 2003-10-31 18:32:15Z brooks $
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\n", priv->unit, ifp->if_xname);
571 continue;
572 }
573
574 if (ifmr.ifm_status & IFM_AVALID &&
575 IFM_TYPE(ifmr.ifm_active) == IFM_ETHER) {
576 if (ifmr.ifm_status & IFM_ACTIVE) {
577 if (p->fec_ifstat == -1 ||
578 p->fec_ifstat == 0) {
579 p->fec_ifstat = 1;
580 printf("fec%d: port %s in bundle "
581 "is up\n", priv->unit,
582 ifp->if_xname);
583 }
584 } else {
585 if (p->fec_ifstat == -1 ||
586 p->fec_ifstat == 1) {
587 p->fec_ifstat = 0;
588 printf("fec%d: port %s in bundle "
589 "is down\n", priv->unit,
590 ifp->if_xname);
591 }
592 }
593 }
594 }
595
596 ifp = &priv->arpcom.ac_if;
597 if (ifp->if_flags & IFF_RUNNING)
598 priv->fec_ch = timeout(ng_fec_tick, priv, hz);
599
600 return;
601 }
602
603 static int
604 ng_fec_ifmedia_upd(struct ifnet *ifp)
605 {
606 return(0);
607 }
608
609 static void ng_fec_ifmedia_sts(struct ifnet *ifp,
610 struct ifmediareq *ifmr)
611 {
612 struct ng_fec_private *priv;
613 struct ng_fec_bundle *b;
614 struct ng_fec_portlist *p;
615
616 priv = ifp->if_softc;
617 b = &priv->fec_bundle;
618
619 ifmr->ifm_status = IFM_AVALID;
620 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
621 if (p->fec_ifstat) {
622 ifmr->ifm_status |= IFM_ACTIVE;
623 break;
624 }
625 }
626
627 return;
628 }
629
630 /*
631 * Process an ioctl for the virtual interface
632 */
633 static int
634 ng_fec_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
635 {
636 struct ifreq *const ifr = (struct ifreq *) data;
637 int s, error = 0;
638 struct ng_fec_private *priv;
639 struct ng_fec_bundle *b;
640
641 priv = ifp->if_softc;
642 b = &priv->fec_bundle;
643
644 #ifdef DEBUG
645 ng_fec_print_ioctl(ifp, command, data);
646 #endif
647 s = splimp();
648 switch (command) {
649
650 /* These two are mostly handled at a higher layer */
651 case SIOCSIFADDR:
652 case SIOCGIFADDR:
653 case SIOCSIFMTU:
654 error = ether_ioctl(ifp, command, data);
655 break;
656
657 /* Set flags */
658 case SIOCSIFFLAGS:
659 /*
660 * If the interface is marked up and stopped, then start it.
661 * If it is marked down and running, then stop it.
662 */
663 if (ifr->ifr_flags & IFF_UP) {
664 if (!(ifp->if_flags & IFF_RUNNING)) {
665 /* Sanity. */
666 if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
667 printf("fec%d: invalid bundle "
668 "size: %d\n", priv->unit,
669 b->fec_ifcnt);
670 error = EINVAL;
671 break;
672 }
673 ifp->if_flags &= ~(IFF_OACTIVE);
674 ifp->if_flags |= IFF_RUNNING;
675 ng_fec_init(ifp);
676 }
677 /*
678 * Bubble down changes in promisc mode to
679 * underlying interfaces.
680 */
681 if ((ifp->if_flags & IFF_PROMISC) !=
682 (priv->if_flags & IFF_PROMISC)) {
683 ng_fec_setport(ifp, command, data);
684 priv->if_flags = ifp->if_flags;
685 }
686 } else {
687 if (ifp->if_flags & IFF_RUNNING)
688 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
689 ng_fec_stop(ifp);
690 }
691 break;
692
693 case SIOCADDMULTI:
694 case SIOCDELMULTI:
695 ng_fec_setport(ifp, command, data);
696 error = 0;
697 break;
698 case SIOCGIFMEDIA:
699 case SIOCSIFMEDIA:
700 error = ifmedia_ioctl(ifp, ifr, &priv->ifmedia, command);
701 break;
702 /* Stuff that's not supported */
703 case SIOCSIFPHYS:
704 error = EOPNOTSUPP;
705 break;
706
707 default:
708 error = EINVAL;
709 break;
710 }
711 (void) splx(s);
712 return (error);
713 }
714
715 /*
716 * This routine spies on mbufs received by underlying network device
717 * drivers. When we add an interface to our bundle, we override its
718 * if_input routine with a pointer to ng_fec_input(). This means we
719 * get to look at all the device's packets before sending them to the
720 * real ether_input() for processing by the stack. Once we verify the
721 * packet comes from an interface that's been aggregated into
722 * our bundle, we fix up the rcvif pointer and increment our
723 * packet counters so that it looks like the frames are actually
724 * coming from us.
725 */
726 static void
727 ng_fec_input(struct ifnet *ifp, struct mbuf *m0)
728 {
729 struct ng_node *node;
730 struct ng_fec_private *priv;
731 struct ng_fec_bundle *b;
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 = NG_NODE_PRIVATE(node);
746 b = &priv->fec_bundle;
747 bifp = &priv->arpcom.ac_if;
748
749 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
750 if (p->fec_if == m0->m_pkthdr.rcvif)
751 break;
752 }
753
754 /* Wasn't meant for us; leave this frame alone. */
755 if (p == NULL)
756 return;
757
758 /*
759 * Check for a BPF tap on the underlying interface. This
760 * is mainly a debugging aid: it allows tcpdump-ing of an
761 * individual interface in a bundle to work, which it
762 * otherwise would not. BPF tapping of our own aggregate
763 * interface will occur once we call ether_input().
764 */
765 BPF_MTAP(m0->m_pkthdr.rcvif, m0);
766
767 /* Convince the system that this is our frame. */
768 m0->m_pkthdr.rcvif = bifp;
769 bifp->if_ipackets++;
770 bifp->if_ibytes += m0->m_pkthdr.len + sizeof(struct ether_header);
771
772 (*bifp->if_input)(bifp, m0);
773
774 return;
775 }
776
777 /*
778 * Take a quick peek at the packet and see if it's ok for us to use
779 * the inet or inet6 hash methods on it, if they're enabled. We do
780 * this by setting flags in the mbuf header. Once we've made up our
781 * mind what to do, we pass the frame to ether_output() for further
782 * processing.
783 */
784
785 static int
786 ng_fec_output(struct ifnet *ifp, struct mbuf *m,
787 struct sockaddr *dst, struct rtentry *rt0)
788 {
789 const priv_p priv = (priv_p) ifp->if_softc;
790 struct ng_fec_bundle *b;
791 int error;
792
793 /* Check interface flags */
794 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
795 m_freem(m);
796 return (ENETDOWN);
797 }
798
799 b = &priv->fec_bundle;
800
801 switch (b->fec_btype) {
802 case FEC_BTYPE_MAC:
803 m->m_flags |= M_FEC_MAC;
804 break;
805 #ifdef INET
806 case FEC_BTYPE_INET:
807 /*
808 * We can't use the INET address port selection
809 * scheme if this isn't an INET packet.
810 */
811 if (dst->sa_family == AF_INET)
812 m->m_flags |= M_FEC_INET;
813 #ifdef INET6
814 else if (dst->sa_family == AF_INET6)
815 m->m_flags |= M_FEC_INET6;
816 #endif
817 else {
818 #ifdef DEBUG
819 if_printf(ifp, "can't do inet aggregation of non "
820 "inet packet\n");
821 #endif
822 m->m_flags |= M_FEC_MAC;
823 }
824 break;
825 #endif
826 default:
827 if_printf(ifp, "bogus hash type: %d\n",
828 b->fec_btype);
829 m_freem(m);
830 return(EINVAL);
831 break;
832 }
833
834 /*
835 * Pass the frame to ether_output() for all the protocol
836 * handling. This will put the ethernet header on the packet
837 * for us.
838 */
839 priv->if_error = 0;
840 error = ether_output(ifp, m, dst, rt0);
841 if (priv->if_error && !error)
842 error = priv->if_error;
843
844 return(error);
845 }
846
847 /*
848 * Apply a hash to the source and destination addresses in the packet
849 * in order to select an interface. Also check link status and handle
850 * dead links accordingly.
851 */
852
853 static int
854 ng_fec_choose_port(struct ng_fec_bundle *b,
855 struct mbuf *m, struct ifnet **ifp)
856 {
857 struct ether_header *eh;
858 struct mbuf *m0;
859 #ifdef INET
860 struct ip *ip;
861 #ifdef INET6
862 struct ip6_hdr *ip6;
863 #endif
864 #endif
865
866 struct ng_fec_portlist *p;
867 int port = 0, mask;
868
869 /*
870 * If there are only two ports, mask off all but the
871 * last bit for XORing. If there are 4, mask off all
872 * but the last 2 bits.
873 */
874 mask = b->fec_ifcnt == 2 ? 0x1 : 0x3;
875 eh = mtod(m, struct ether_header *);
876 #ifdef INET
877 ip = (struct ip *)(mtod(m, char *) +
878 sizeof(struct ether_header));
879 #ifdef INET6
880 ip6 = (struct ip6_hdr *)(mtod(m, char *) +
881 sizeof(struct ether_header));
882 #endif
883 #endif
884
885 /*
886 * The fg_fec_output() routine is supposed to leave a
887 * flag for us in the mbuf that tells us what hash to
888 * use, but sometimes a new mbuf is prepended to the
889 * chain, so we have to search every mbuf in the chain
890 * to find the flags.
891 */
892 m0 = m;
893 while (m0) {
894 if (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6))
895 break;
896 m0 = m0->m_next;
897 }
898 if (m0 == NULL)
899 return(EINVAL);
900
901 switch (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6)) {
902 case M_FEC_MAC:
903 port = (eh->ether_dhost[5] ^
904 eh->ether_shost[5]) & mask;
905 break;
906 #ifdef INET
907 case M_FEC_INET:
908 port = (ntohl(ip->ip_dst.s_addr) ^
909 ntohl(ip->ip_src.s_addr)) & mask;
910 break;
911 #ifdef INET6
912 case M_FEC_INET6:
913 port = (ip6->ip6_dst.s6_addr[15] ^
914 ip6->ip6_dst.s6_addr[15]) & mask;
915 break;
916 #endif
917 #endif
918 default:
919 return(EINVAL);
920 break;
921 }
922
923 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
924 if (port == p->fec_idx)
925 break;
926 }
927
928 /*
929 * Now that we've chosen a port, make sure it's
930 * alive. If it's not alive, cycle through the bundle
931 * looking for a port that is alive. If we don't find
932 * any, return an error.
933 */
934 if (p->fec_ifstat != 1) {
935 struct ng_fec_portlist *n = NULL;
936
937 n = TAILQ_NEXT(p, fec_list);
938 if (n == NULL)
939 n = TAILQ_FIRST(&b->ng_fec_ports);
940 while (n != p) {
941 if (n->fec_ifstat == 1)
942 break;
943 n = TAILQ_NEXT(n, fec_list);
944 if (n == NULL)
945 n = TAILQ_FIRST(&b->ng_fec_ports);
946 }
947 if (n == p)
948 return(EAGAIN);
949 p = n;
950 }
951
952 *ifp = p->fec_if;
953
954 return(0);
955 }
956
957 /*
958 * Now that the packet has been run through ether_output(), yank it
959 * off our own send queue and stick it on the queue for the appropriate
960 * underlying physical interface. Note that if the interface's send
961 * queue is full, we save an error status in our private netgraph
962 * space which will eventually be handed up to ng_fec_output(), which
963 * will return it to the rest of the IP stack. We need to do this
964 * in order to duplicate the effect of ether_output() returning ENOBUFS
965 * when it detects that an interface's send queue is full. There's no
966 * other way to signal the error status from here since the if_start()
967 * routine is spec'ed to return void.
968 *
969 * Once the frame is queued, we call ether_output_frame() to initiate
970 * transmission.
971 */
972 static void
973 ng_fec_start(struct ifnet *ifp)
974 {
975 struct ng_fec_private *priv;
976 struct ng_fec_bundle *b;
977 struct ifnet *oifp = NULL;
978 struct mbuf *m0;
979 int error;
980
981 priv = ifp->if_softc;
982 b = &priv->fec_bundle;
983
984 IF_DEQUEUE(&ifp->if_snd, m0);
985 if (m0 == NULL)
986 return;
987
988 BPF_MTAP(ifp, m0);
989
990 /* Queue up packet on the proper port. */
991 error = ng_fec_choose_port(b, m0, &oifp);
992 if (error) {
993 ifp->if_ierrors++;
994 m_freem(m0);
995 priv->if_error = ENOBUFS;
996 return;
997 }
998 ifp->if_opackets++;
999
1000 priv->if_error = ether_output_frame(oifp, m0);
1001
1002 return;
1003 }
1004
1005 #ifdef DEBUG
1006 /*
1007 * Display an ioctl to the virtual interface
1008 */
1009
1010 static void
1011 ng_fec_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
1012 {
1013 char *str;
1014
1015 switch (command & IOC_DIRMASK) {
1016 case IOC_VOID:
1017 str = "IO";
1018 break;
1019 case IOC_OUT:
1020 str = "IOR";
1021 break;
1022 case IOC_IN:
1023 str = "IOW";
1024 break;
1025 case IOC_INOUT:
1026 str = "IORW";
1027 break;
1028 default:
1029 str = "IO??";
1030 }
1031 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
1032 ifp->if_xname,
1033 str,
1034 IOCGROUP(command),
1035 command & 0xff,
1036 IOCPARM_LEN(command));
1037 }
1038 #endif /* DEBUG */
1039
1040 /************************************************************************
1041 NETGRAPH NODE STUFF
1042 ************************************************************************/
1043
1044 /*
1045 * Constructor for a node
1046 */
1047 static int
1048 ng_fec_constructor(node_p node)
1049 {
1050 char ifname[NG_FEC_FEC_NAME_MAX + 1];
1051 struct ifnet *ifp;
1052 priv_p priv;
1053 struct ng_fec_bundle *b;
1054 int error = 0;
1055
1056 /* Allocate node and interface private structures */
1057 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT);
1058 if (priv == NULL)
1059 return (ENOMEM);
1060 bzero(priv, sizeof(*priv));
1061
1062 ifp = &priv->arpcom.ac_if;
1063 b = &priv->fec_bundle;
1064
1065 /* Link them together */
1066 ifp->if_softc = priv;
1067
1068 /* Get an interface unit number */
1069 if ((error = ng_fec_get_unit(&priv->unit)) != 0) {
1070 FREE(ifp, M_NETGRAPH);
1071 FREE(priv, M_NETGRAPH);
1072 return (error);
1073 }
1074
1075 /* Link together node and private info */
1076 NG_NODE_SET_PRIVATE(node, priv);
1077 priv->node = node;
1078 priv->arpcom.ac_netgraph = node;
1079
1080 /* Initialize interface structure */
1081 if_initname(ifp, NG_FEC_FEC_NAME, priv->unit);
1082 ifp->if_start = ng_fec_start;
1083 ifp->if_ioctl = ng_fec_ioctl;
1084 ifp->if_init = ng_fec_init;
1085 ifp->if_watchdog = NULL;
1086 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
1087 ifp->if_mtu = NG_FEC_MTU_DEFAULT;
1088 ifp->if_flags = (IFF_SIMPLEX|IFF_BROADCAST|IFF_MULTICAST);
1089 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
1090 ifp->if_addrlen = 0; /* XXX */
1091 ifp->if_hdrlen = 0; /* XXX */
1092 ifp->if_baudrate = 100000000; /* XXX */
1093 TAILQ_INIT(&ifp->if_addrhead);
1094
1095 /* Give this node the same name as the interface (if possible) */
1096 bzero(ifname, sizeof(ifname));
1097 strlcpy(ifname, ifp->if_xname, sizeof(ifname));
1098 if (ng_name_node(node, ifname) != 0)
1099 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
1100
1101 /* Attach the interface */
1102 ether_ifattach(ifp, priv->arpcom.ac_enaddr);
1103 callout_handle_init(&priv->fec_ch);
1104
1105 /* Override output method with our own */
1106 ifp->if_output = ng_fec_output;
1107
1108 TAILQ_INIT(&b->ng_fec_ports);
1109 b->fec_ifcnt = 0;
1110
1111 ifmedia_init(&priv->ifmedia, 0,
1112 ng_fec_ifmedia_upd, ng_fec_ifmedia_sts);
1113 ifmedia_add(&priv->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
1114 ifmedia_set(&priv->ifmedia, IFM_ETHER|IFM_NONE);
1115
1116 /* Done */
1117 return (0);
1118 }
1119
1120 /*
1121 * Receive a control message
1122 */
1123 static int
1124 ng_fec_rcvmsg(node_p node, item_p item, hook_p lasthook)
1125 {
1126 const priv_p priv = NG_NODE_PRIVATE(node);
1127 struct ng_fec_bundle *b;
1128 struct ng_mesg *resp = NULL;
1129 struct ng_mesg *msg;
1130 char *ifname;
1131 int error = 0;
1132
1133 NGI_GET_MSG(item, msg);
1134 b = &priv->fec_bundle;
1135
1136 switch (msg->header.typecookie) {
1137 case NGM_FEC_COOKIE:
1138 switch (msg->header.cmd) {
1139 case NGM_FEC_ADD_IFACE:
1140 ifname = msg->data;
1141 error = ng_fec_addport(priv, ifname);
1142 break;
1143 case NGM_FEC_DEL_IFACE:
1144 ifname = msg->data;
1145 error = ng_fec_delport(priv, ifname);
1146 break;
1147 case NGM_FEC_SET_MODE_MAC:
1148 b->fec_btype = FEC_BTYPE_MAC;
1149 break;
1150 #ifdef INET
1151 case NGM_FEC_SET_MODE_INET:
1152 b->fec_btype = FEC_BTYPE_INET;
1153 break;
1154 #ifdef INET6
1155 case NGM_FEC_SET_MODE_INET6:
1156 b->fec_btype = FEC_BTYPE_INET6;
1157 break;
1158 #endif
1159 #endif
1160 default:
1161 error = EINVAL;
1162 break;
1163 }
1164 break;
1165 default:
1166 error = EINVAL;
1167 break;
1168 }
1169 NG_RESPOND_MSG(error, node, item, resp);
1170 NG_FREE_MSG(msg);
1171 return (error);
1172 }
1173
1174 /*
1175 * Shutdown and remove the node and its associated interface.
1176 */
1177 static int
1178 ng_fec_shutdown(node_p node)
1179 {
1180 const priv_p priv = NG_NODE_PRIVATE(node);
1181 struct ng_fec_bundle *b;
1182 struct ng_fec_portlist *p;
1183
1184 b = &priv->fec_bundle;
1185 ng_fec_stop(&priv->arpcom.ac_if);
1186
1187 while (!TAILQ_EMPTY(&b->ng_fec_ports)) {
1188 p = TAILQ_FIRST(&b->ng_fec_ports);
1189 ng_fec_delport(priv, p->fec_if->if_xname);
1190 }
1191
1192 ether_ifdetach(&priv->arpcom.ac_if);
1193 ifmedia_removeall(&priv->ifmedia);
1194 ng_fec_free_unit(priv->unit);
1195 FREE(priv, M_NETGRAPH);
1196 NG_NODE_SET_PRIVATE(node, NULL);
1197 NG_NODE_UNREF(node);
1198 return (0);
1199 }
Cache object: 45f5b0ded789b6a939fc7086f5ef9653
|