FreeBSD/Linux Kernel Cross Reference
sys/netgraph/ng_fec.c
1 /*
2 * ng_fec.c
3 */
4
5 /*-
6 * Copyright (c) 2001 Berkeley Software Design, Inc.
7 * Copyright (c) 2000, 2001
8 * Bill Paul <wpaul@osd.bsdi.com>. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Bill Paul.
21 * 4. Neither the name of the author nor the names of any co-contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35 * THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * $FreeBSD: releng/6.0/sys/netgraph/ng_fec.c 149582 2005-08-29 16:23:21Z emax $
38 */
39 /*-
40 * Copyright (c) 1996-1999 Whistle Communications, Inc.
41 * All rights reserved.
42 *
43 * Subject to the following obligations and disclaimer of warranty, use and
44 * redistribution of this software, in source or object code forms, with or
45 * without modifications are expressly permitted by Whistle Communications;
46 * provided, however, that:
47 * 1. Any and all reproductions of the source or object code must include the
48 * copyright notice above and the following disclaimer of warranties; and
49 * 2. No rights are granted, in any manner or form, to use Whistle
50 * Communications, Inc. trademarks, including the mark "WHISTLE
51 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
52 * such appears in the above copyright notice or in the software.
53 *
54 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
55 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
56 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
57 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
58 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
59 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
60 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
61 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
62 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
63 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
64 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
65 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
66 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
67 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
68 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
69 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
70 * OF SUCH DAMAGE.
71 *
72 * Author: Archie Cobbs <archie@freebsd.org>
73 *
74 * $Whistle: ng_fec.c,v 1.33 1999/11/01 09:24:51 julian Exp $
75 */
76
77 /*
78 * This module implements ethernet channel bonding using the Cisco
79 * Fast EtherChannel mechanism. Two or four ports may be combined
80 * into a single aggregate interface.
81 *
82 * Interfaces are named fec0, fec1, etc. New nodes take the
83 * first available interface name.
84 *
85 * This node also includes Berkeley packet filter support.
86 *
87 * Note that this node doesn't need to connect to any other
88 * netgraph nodes in order to do its work.
89 */
90
91 #include <sys/param.h>
92 #include <sys/systm.h>
93 #include <sys/errno.h>
94 #include <sys/kernel.h>
95 #include <sys/malloc.h>
96 #include <sys/mbuf.h>
97 #include <sys/errno.h>
98 #include <sys/sockio.h>
99 #include <sys/socket.h>
100 #include <sys/syslog.h>
101 #include <sys/libkern.h>
102 #include <sys/queue.h>
103
104 #include <net/if.h>
105 #include <net/if_types.h>
106 #include <net/if_arp.h>
107 #include <net/if_dl.h>
108 #include <net/if_media.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 <netgraph/ng_fec.h>
129
130 /*
131 * We need a way to stash a pointer to our netgraph node in the
132 * ifnet structure so that receive handling works. As far as I can
133 * tell, although there is an AF_NETGRAPH address family, it's only
134 * used to identify sockaddr_ng structures: there is no netgraph address
135 * family domain. This means the AF_NETGRAPH entry in ifp->if_afdata
136 * should be unused, so we can use to hold our node context.
137 */
138 #define IFP2NG(ifp) (struct ng_node *)(ifp->if_afdata[AF_NETGRAPH])
139 #define IFP2NG_SET(ifp, val) ifp->if_afdata[AF_NETGRAPH] = (val);
140 #define FEC_INC(x, y) (x) = (x + 1) % y
141
142 /*
143 * Current fast etherchannel implementations use either 2 or 4
144 * ports, so for now we limit the maximum bundle size to 4 interfaces.
145 */
146 #define FEC_BUNDLESIZ 4
147
148 struct ng_fec_portlist {
149 struct ifnet *fec_if;
150 void (*fec_if_input) (struct ifnet *,
151 struct mbuf *);
152 int fec_idx;
153 int fec_ifstat;
154 struct ether_addr fec_mac;
155 TAILQ_ENTRY(ng_fec_portlist) fec_list;
156 };
157
158 struct ng_fec_bundle {
159 TAILQ_HEAD(,ng_fec_portlist) ng_fec_ports;
160 int fec_ifcnt;
161 int fec_btype;
162 int (*fec_if_output) (struct ifnet *,
163 struct mbuf *,
164 struct sockaddr *,
165 struct rtentry *);
166 };
167
168 #define FEC_BTYPE_MAC 0x01
169 #define FEC_BTYPE_INET 0x02
170 #define FEC_BTYPE_INET6 0x03
171
172 /* Node private data */
173 struct ng_fec_private {
174 struct ifnet *ifp;
175 struct ifmedia ifmedia;
176 int if_flags;
177 int if_error; /* XXX */
178 int unit; /* Interface unit number */
179 node_p node; /* Our netgraph node */
180 struct ng_fec_bundle fec_bundle;/* Aggregate bundle */
181 struct callout_handle fec_ch; /* callout handle for ticker */
182 };
183 typedef struct ng_fec_private *priv_p;
184
185 /* Interface methods */
186 static void ng_fec_input(struct ifnet *, struct mbuf *);
187 static void ng_fec_start(struct ifnet *ifp);
188 static int ng_fec_choose_port(struct ng_fec_bundle *b,
189 struct mbuf *m, struct ifnet **ifp);
190 static int ng_fec_setport(struct ifnet *ifp, u_long cmd, caddr_t data);
191 static void ng_fec_init(void *arg);
192 static void ng_fec_stop(struct ifnet *ifp);
193 static int ng_fec_ifmedia_upd(struct ifnet *ifp);
194 static void ng_fec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
195 static int ng_fec_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
196 static int ng_fec_output(struct ifnet *ifp, struct mbuf *m0,
197 struct sockaddr *dst, struct rtentry *rt0);
198 static void ng_fec_tick(void *arg);
199 static int ng_fec_addport(struct ng_fec_private *priv, char *iface);
200 static int ng_fec_delport(struct ng_fec_private *priv, char *iface);
201
202 #ifdef DEBUG
203 static void ng_fec_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
204 #endif
205
206 /* Netgraph methods */
207 static int ng_fec_mod_event(module_t, int, void *);
208 static ng_constructor_t ng_fec_constructor;
209 static ng_rcvmsg_t ng_fec_rcvmsg;
210 static ng_shutdown_t ng_fec_shutdown;
211
212 /* List of commands and how to convert arguments to/from ASCII */
213 static const struct ng_cmdlist ng_fec_cmds[] = {
214 {
215 NGM_FEC_COOKIE,
216 NGM_FEC_ADD_IFACE,
217 "add_iface",
218 &ng_parse_string_type,
219 NULL,
220 },
221 {
222 NGM_FEC_COOKIE,
223 NGM_FEC_DEL_IFACE,
224 "del_iface",
225 &ng_parse_string_type,
226 NULL,
227 },
228 {
229 NGM_FEC_COOKIE,
230 NGM_FEC_SET_MODE_MAC,
231 "set_mode_mac",
232 NULL,
233 NULL,
234 },
235 {
236 NGM_FEC_COOKIE,
237 NGM_FEC_SET_MODE_INET,
238 "set_mode_inet",
239 NULL,
240 NULL,
241 },
242 { 0 }
243 };
244
245 /* Node type descriptor */
246 static struct ng_type typestruct = {
247 .version = NG_ABI_VERSION,
248 .name = NG_FEC_NODE_TYPE,
249 .mod_event = ng_fec_mod_event,
250 .constructor = ng_fec_constructor,
251 .rcvmsg = ng_fec_rcvmsg,
252 .shutdown = ng_fec_shutdown,
253 .cmdlist = ng_fec_cmds,
254 };
255 NETGRAPH_INIT(fec, &typestruct);
256
257 /* We keep a bitmap indicating which unit numbers are free.
258 One means the unit number is free, zero means it's taken. */
259 static int *ng_fec_units = NULL;
260 static int ng_fec_units_len = 0;
261 static int ng_units_in_use = 0;
262
263 #define UNITS_BITSPERWORD (sizeof(*ng_fec_units) * NBBY)
264
265 static struct mtx ng_fec_mtx;
266
267 /*
268 * Find the first free unit number for a new interface.
269 * Increase the size of the unit bitmap as necessary.
270 */
271 static __inline int
272 ng_fec_get_unit(int *unit)
273 {
274 int index, bit;
275
276 mtx_lock(&ng_fec_mtx);
277 for (index = 0; index < ng_fec_units_len
278 && ng_fec_units[index] == 0; index++);
279 if (index == ng_fec_units_len) { /* extend array */
280 int i, *newarray, newlen;
281
282 newlen = (2 * ng_fec_units_len) + 4;
283 MALLOC(newarray, int *, newlen * sizeof(*ng_fec_units),
284 M_NETGRAPH, M_NOWAIT);
285 if (newarray == NULL) {
286 mtx_unlock(&ng_fec_mtx);
287 return (ENOMEM);
288 }
289 bcopy(ng_fec_units, newarray,
290 ng_fec_units_len * sizeof(*ng_fec_units));
291 for (i = ng_fec_units_len; i < newlen; i++)
292 newarray[i] = ~0;
293 if (ng_fec_units != NULL)
294 FREE(ng_fec_units, M_NETGRAPH);
295 ng_fec_units = newarray;
296 ng_fec_units_len = newlen;
297 }
298 bit = ffs(ng_fec_units[index]) - 1;
299 KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
300 ("%s: word=%d bit=%d", __FUNCTION__, ng_fec_units[index], bit));
301 ng_fec_units[index] &= ~(1 << bit);
302 *unit = (index * UNITS_BITSPERWORD) + bit;
303 ng_units_in_use++;
304 mtx_unlock(&ng_fec_mtx);
305 return (0);
306 }
307
308 /*
309 * Free a no longer needed unit number.
310 */
311 static __inline void
312 ng_fec_free_unit(int unit)
313 {
314 int index, bit;
315
316 index = unit / UNITS_BITSPERWORD;
317 bit = unit % UNITS_BITSPERWORD;
318 mtx_lock(&ng_fec_mtx);
319 KASSERT(index < ng_fec_units_len,
320 ("%s: unit=%d len=%d", __FUNCTION__, unit, ng_fec_units_len));
321 KASSERT((ng_fec_units[index] & (1 << bit)) == 0,
322 ("%s: unit=%d is free", __FUNCTION__, unit));
323 ng_fec_units[index] |= (1 << bit);
324 /*
325 * XXX We could think about reducing the size of ng_fec_units[]
326 * XXX here if the last portion is all ones
327 * XXX At least free it if no more units
328 * Needed if we are to eventually be able to unload.
329 */
330 ng_units_in_use--;
331 if (ng_units_in_use == 0) { /* XXX make SMP safe */
332 FREE(ng_fec_units, M_NETGRAPH);
333 ng_fec_units_len = 0;
334 ng_fec_units = NULL;
335 }
336 mtx_unlock(&ng_fec_mtx);
337 }
338
339 /************************************************************************
340 INTERFACE STUFF
341 ************************************************************************/
342
343 static int
344 ng_fec_addport(struct ng_fec_private *priv, char *iface)
345 {
346 struct ng_fec_bundle *b;
347 struct ifnet *ifp, *bifp;
348 struct ifaddr *ifa;
349 struct sockaddr_dl *sdl;
350 struct ng_fec_portlist *p, *new;
351
352 if (priv == NULL || iface == NULL)
353 return(EINVAL);
354
355 b = &priv->fec_bundle;
356 ifp = priv->ifp;
357
358 /* Find the interface */
359 bifp = ifunit(iface);
360 if (bifp == NULL) {
361 printf("fec%d: tried to add iface %s, which "
362 "doesn't seem to exist\n", priv->unit, iface);
363 return(ENOENT);
364 }
365
366 /* See if we have room in the bundle */
367 if (b->fec_ifcnt == FEC_BUNDLESIZ) {
368 printf("fec%d: can't add new iface; bundle is full\n",
369 priv->unit);
370 return(ENOSPC);
371 }
372
373 /* See if the interface is already in the bundle */
374 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
375 if (p->fec_if == bifp) {
376 printf("fec%d: iface %s is already in this "
377 "bundle\n", priv->unit, iface);
378 return(EINVAL);
379 }
380 }
381
382 /*
383 * All interfaces must use the same output vector. Once the
384 * user attaches an interface of one type, make all subsequent
385 * interfaces have the same output vector.
386 */
387 if (b->fec_if_output != NULL) {
388 if (b->fec_if_output != bifp->if_output) {
389 printf("fec%d: iface %s is not the same type "
390 "as the other interface(s) already in "
391 "the bundle\n", priv->unit, iface);
392 return(EINVAL);
393 }
394 }
395
396 /* Allocate new list entry. */
397 MALLOC(new, struct ng_fec_portlist *,
398 sizeof(struct ng_fec_portlist), M_NETGRAPH, M_NOWAIT);
399 if (new == NULL)
400 return(ENOMEM);
401
402 IF_AFDATA_LOCK(bifp);
403 bifp->if_afdata[AF_NETGRAPH] = priv->node;
404 IF_AFDATA_UNLOCK(bifp);
405
406 /*
407 * If this is the first interface added to the bundle,
408 * use its MAC address for the virtual interface (and,
409 * by extension, all the other ports in the bundle).
410 */
411 if (b->fec_ifcnt == 0) {
412 ifa = ifaddr_byindex(ifp->if_index);
413 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
414 bcopy(IFP2ENADDR(bifp),
415 IFP2ENADDR(priv->ifp), ETHER_ADDR_LEN);
416 bcopy(IFP2ENADDR(bifp),
417 LLADDR(sdl), ETHER_ADDR_LEN);
418 }
419
420 b->fec_btype = FEC_BTYPE_MAC;
421 new->fec_idx = b->fec_ifcnt;
422 b->fec_ifcnt++;
423
424 /* Save the real MAC address. */
425 bcopy(IFP2ENADDR(bifp),
426 (char *)&new->fec_mac, ETHER_ADDR_LEN);
427
428 /* Set up phony MAC address. */
429 ifa = ifaddr_byindex(bifp->if_index);
430 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
431 bcopy(IFP2ENADDR(priv->ifp), IFP2ENADDR(bifp), ETHER_ADDR_LEN);
432 bcopy(IFP2ENADDR(priv->ifp), LLADDR(sdl), ETHER_ADDR_LEN);
433
434 /* Save original input vector */
435 new->fec_if_input = bifp->if_input;
436
437 /* Override it with our own */
438 bifp->if_input = ng_fec_input;
439
440 /* Save output vector too. */
441 if (b->fec_if_output == NULL)
442 b->fec_if_output = bifp->if_output;
443
444 /* Add to the queue */
445 new->fec_if = bifp;
446 TAILQ_INSERT_TAIL(&b->ng_fec_ports, new, fec_list);
447
448 return(0);
449 }
450
451 static int
452 ng_fec_delport(struct ng_fec_private *priv, char *iface)
453 {
454 struct ng_fec_bundle *b;
455 struct ifnet *ifp, *bifp;
456 struct ifaddr *ifa;
457 struct sockaddr_dl *sdl;
458 struct ng_fec_portlist *p;
459
460 if (priv == NULL || iface == NULL)
461 return(EINVAL);
462
463 b = &priv->fec_bundle;
464 ifp = priv->ifp;
465
466 /* Find the interface */
467 bifp = ifunit(iface);
468 if (bifp == NULL) {
469 printf("fec%d: tried to remove iface %s, which "
470 "doesn't seem to exist\n", priv->unit, iface);
471 return(ENOENT);
472 }
473
474 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
475 if (p->fec_if == bifp)
476 break;
477 }
478
479 if (p == NULL) {
480 printf("fec%d: tried to remove iface %s which "
481 "is not in our bundle\n", priv->unit, iface);
482 return(EINVAL);
483 }
484
485 /* Stop interface */
486 bifp->if_flags &= ~IFF_UP;
487 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
488
489 /* Restore MAC address. */
490 ifa = ifaddr_byindex(bifp->if_index);
491 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
492 bcopy((char *)&p->fec_mac, IFP2ENADDR(bifp), ETHER_ADDR_LEN);
493 bcopy((char *)&p->fec_mac, LLADDR(sdl), ETHER_ADDR_LEN);
494
495 /* Restore input vector */
496 bifp->if_input = p->fec_if_input;
497
498 /* Remove our node context pointer. */
499 IF_AFDATA_LOCK(bifp);
500 bifp->if_afdata[AF_NETGRAPH] = NULL;
501 IF_AFDATA_UNLOCK(bifp);
502
503 /* Delete port */
504 TAILQ_REMOVE(&b->ng_fec_ports, p, fec_list);
505 FREE(p, M_NETGRAPH);
506 b->fec_ifcnt--;
507
508 if (b->fec_ifcnt == 0)
509 b->fec_if_output = NULL;
510
511 return(0);
512 }
513
514 /*
515 * Pass an ioctl command down to all the underyling interfaces in a
516 * bundle. Used for setting multicast filters and flags.
517 */
518
519 static int
520 ng_fec_setport(struct ifnet *ifp, u_long command, caddr_t data)
521 {
522 struct ng_fec_private *priv;
523 struct ng_fec_bundle *b;
524 struct ifnet *oifp;
525 struct ng_fec_portlist *p;
526
527 priv = ifp->if_softc;
528 b = &priv->fec_bundle;
529
530 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
531 oifp = p->fec_if;
532 if (oifp != NULL)
533 (*oifp->if_ioctl)(oifp, command, data);
534 }
535
536 return(0);
537 }
538
539 static void
540 ng_fec_init(void *arg)
541 {
542 struct ng_fec_private *priv;
543 struct ng_fec_bundle *b;
544 struct ifnet *ifp, *bifp;
545 struct ng_fec_portlist *p;
546
547 priv = arg;
548 ifp = priv->ifp;
549 b = &priv->fec_bundle;
550
551 if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
552 printf("fec%d: invalid bundle "
553 "size: %d\n", priv->unit,
554 b->fec_ifcnt);
555 return;
556 }
557
558 ng_fec_stop(ifp);
559
560 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
561 bifp = p->fec_if;
562 bifp->if_flags |= IFF_UP;
563 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
564 /* mark iface as up and let the monitor check it */
565 p->fec_ifstat = -1;
566 }
567
568 priv->fec_ch = timeout(ng_fec_tick, priv, hz);
569
570 return;
571 }
572
573 static void
574 ng_fec_stop(struct ifnet *ifp)
575 {
576 struct ng_fec_private *priv;
577 struct ng_fec_bundle *b;
578 struct ifnet *bifp;
579 struct ng_fec_portlist *p;
580
581 priv = ifp->if_softc;
582 b = &priv->fec_bundle;
583
584 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
585 bifp = p->fec_if;
586 bifp->if_flags &= ~IFF_UP;
587 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
588 }
589
590 untimeout(ng_fec_tick, priv, priv->fec_ch);
591
592 return;
593 }
594
595 static void
596 ng_fec_tick(void *arg)
597 {
598 struct ng_fec_private *priv;
599 struct ng_fec_bundle *b;
600 struct ifmediareq ifmr;
601 struct ifnet *ifp;
602 struct ng_fec_portlist *p;
603 int error = 0;
604
605 priv = arg;
606 b = &priv->fec_bundle;
607
608 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
609 bzero((char *)&ifmr, sizeof(ifmr));
610 ifp = p->fec_if;
611 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
612 if (error) {
613 printf("fec%d: failed to check status "
614 "of link %s\n", priv->unit, ifp->if_xname);
615 continue;
616 }
617
618 if (ifmr.ifm_status & IFM_AVALID) {
619 if (ifmr.ifm_status & IFM_ACTIVE) {
620 if (p->fec_ifstat == -1 ||
621 p->fec_ifstat == 0) {
622 p->fec_ifstat = 1;
623 printf("fec%d: port %s in bundle "
624 "is up\n", priv->unit,
625 ifp->if_xname);
626 }
627 } else {
628 if (p->fec_ifstat == -1 ||
629 p->fec_ifstat == 1) {
630 p->fec_ifstat = 0;
631 printf("fec%d: port %s in bundle "
632 "is down\n", priv->unit,
633 ifp->if_xname);
634 }
635 }
636 }
637 }
638
639 ifp = priv->ifp;
640 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
641 priv->fec_ch = timeout(ng_fec_tick, priv, hz);
642
643 return;
644 }
645
646 static int
647 ng_fec_ifmedia_upd(struct ifnet *ifp)
648 {
649 return(0);
650 }
651
652 static void ng_fec_ifmedia_sts(struct ifnet *ifp,
653 struct ifmediareq *ifmr)
654 {
655 struct ng_fec_private *priv;
656 struct ng_fec_bundle *b;
657 struct ng_fec_portlist *p;
658
659 priv = ifp->if_softc;
660 b = &priv->fec_bundle;
661
662 ifmr->ifm_status = IFM_AVALID;
663 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
664 if (p->fec_ifstat) {
665 ifmr->ifm_status |= IFM_ACTIVE;
666 break;
667 }
668 }
669
670 return;
671 }
672
673 /*
674 * Process an ioctl for the virtual interface
675 */
676 static int
677 ng_fec_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
678 {
679 struct ifreq *const ifr = (struct ifreq *) data;
680 int s, error = 0;
681 struct ng_fec_private *priv;
682 struct ng_fec_bundle *b;
683
684 priv = ifp->if_softc;
685 b = &priv->fec_bundle;
686
687 #ifdef DEBUG
688 ng_fec_print_ioctl(ifp, command, data);
689 #endif
690 s = splimp();
691 switch (command) {
692
693 /* These two are mostly handled at a higher layer */
694 case SIOCSIFADDR:
695 case SIOCGIFADDR:
696 case SIOCSIFMTU:
697 error = ether_ioctl(ifp, command, data);
698 break;
699
700 /* Set flags */
701 case SIOCSIFFLAGS:
702 /*
703 * If the interface is marked up and stopped, then start it.
704 * If it is marked down and running, then stop it.
705 */
706 if (ifr->ifr_flags & IFF_UP) {
707 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
708 /* Sanity. */
709 if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
710 printf("fec%d: invalid bundle "
711 "size: %d\n", priv->unit,
712 b->fec_ifcnt);
713 error = EINVAL;
714 break;
715 }
716 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
717 ifp->if_drv_flags |= IFF_DRV_RUNNING;
718 ng_fec_init(priv);
719 }
720 /*
721 * Bubble down changes in promisc mode to
722 * underlying interfaces.
723 */
724 if ((ifp->if_flags & IFF_PROMISC) !=
725 (priv->if_flags & IFF_PROMISC)) {
726 ng_fec_setport(ifp, command, data);
727 priv->if_flags = ifp->if_flags;
728 }
729 } else {
730 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
731 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
732 IFF_DRV_OACTIVE);
733 ng_fec_stop(ifp);
734 }
735 break;
736
737 case SIOCADDMULTI:
738 case SIOCDELMULTI:
739 ng_fec_setport(ifp, command, data);
740 error = 0;
741 break;
742 case SIOCGIFMEDIA:
743 case SIOCSIFMEDIA:
744 error = ifmedia_ioctl(ifp, ifr, &priv->ifmedia, command);
745 break;
746 /* Stuff that's not supported */
747 case SIOCSIFPHYS:
748 error = EOPNOTSUPP;
749 break;
750
751 default:
752 error = EINVAL;
753 break;
754 }
755 (void) splx(s);
756 return (error);
757 }
758
759 /*
760 * This routine spies on mbufs received by underlying network device
761 * drivers. When we add an interface to our bundle, we override its
762 * if_input routine with a pointer to ng_fec_input(). This means we
763 * get to look at all the device's packets before sending them to the
764 * real ether_input() for processing by the stack. Once we verify the
765 * packet comes from an interface that's been aggregated into
766 * our bundle, we fix up the rcvif pointer and increment our
767 * packet counters so that it looks like the frames are actually
768 * coming from us.
769 */
770 static void
771 ng_fec_input(struct ifnet *ifp, struct mbuf *m0)
772 {
773 struct ng_node *node;
774 struct ng_fec_private *priv;
775 struct ng_fec_bundle *b;
776 struct ifnet *bifp;
777 struct ng_fec_portlist *p;
778
779 /* Sanity check */
780 if (ifp == NULL || m0 == NULL)
781 return;
782
783 node = IFP2NG(ifp);
784
785 /* Sanity check part II */
786 if (node == NULL)
787 return;
788
789 priv = NG_NODE_PRIVATE(node);
790 b = &priv->fec_bundle;
791 bifp = priv->ifp;
792
793 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
794 if (p->fec_if == m0->m_pkthdr.rcvif)
795 break;
796 }
797
798 /* Wasn't meant for us; leave this frame alone. */
799 if (p == NULL)
800 return;
801
802 /*
803 * Check for a BPF tap on the underlying interface. This
804 * is mainly a debugging aid: it allows tcpdump-ing of an
805 * individual interface in a bundle to work, which it
806 * otherwise would not. BPF tapping of our own aggregate
807 * interface will occur once we call ether_input().
808 */
809 BPF_MTAP(m0->m_pkthdr.rcvif, m0);
810
811 /* Convince the system that this is our frame. */
812 m0->m_pkthdr.rcvif = bifp;
813 bifp->if_ipackets++;
814 bifp->if_ibytes += m0->m_pkthdr.len + sizeof(struct ether_header);
815
816 (*bifp->if_input)(bifp, m0);
817
818 return;
819 }
820
821 /*
822 * Take a quick peek at the packet and see if it's ok for us to use
823 * the inet or inet6 hash methods on it, if they're enabled. We do
824 * this by setting flags in the mbuf header. Once we've made up our
825 * mind what to do, we pass the frame to output vector for further
826 * processing.
827 */
828
829 static int
830 ng_fec_output(struct ifnet *ifp, struct mbuf *m,
831 struct sockaddr *dst, struct rtentry *rt0)
832 {
833 const priv_p priv = (priv_p) ifp->if_softc;
834 struct ng_fec_bundle *b;
835 int error;
836
837 /* Check interface flags */
838 if (!((ifp->if_flags & IFF_UP) &&
839 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
840 m_freem(m);
841 return (ENETDOWN);
842 }
843
844 b = &priv->fec_bundle;
845
846 switch (b->fec_btype) {
847 case FEC_BTYPE_MAC:
848 m->m_flags |= M_FEC_MAC;
849 break;
850 #ifdef INET
851 case FEC_BTYPE_INET:
852 /*
853 * We can't use the INET address port selection
854 * scheme if this isn't an INET packet.
855 */
856 if (dst->sa_family == AF_INET)
857 m->m_flags |= M_FEC_INET;
858 #ifdef INET6
859 else if (dst->sa_family == AF_INET6)
860 m->m_flags |= M_FEC_INET6;
861 #endif
862 else {
863 #ifdef DEBUG
864 if_printf(ifp, "can't do inet aggregation of non "
865 "inet packet\n");
866 #endif
867 m->m_flags |= M_FEC_MAC;
868 }
869 break;
870 #endif
871 default:
872 if_printf(ifp, "bogus hash type: %d\n",
873 b->fec_btype);
874 m_freem(m);
875 return(EINVAL);
876 break;
877 }
878
879 /*
880 * Pass the frame to the output vector for all the protocol
881 * handling. This will put the ethernet header on the packet
882 * for us.
883 */
884 priv->if_error = 0;
885 error = (*b->fec_if_output)(ifp, m, dst, rt0);
886 if (priv->if_error && !error)
887 error = priv->if_error;
888
889 return(error);
890 }
891
892 /*
893 * Apply a hash to the source and destination addresses in the packet
894 * in order to select an interface. Also check link status and handle
895 * dead links accordingly.
896 */
897
898 static int
899 ng_fec_choose_port(struct ng_fec_bundle *b,
900 struct mbuf *m, struct ifnet **ifp)
901 {
902 struct ether_header *eh;
903 struct mbuf *m0;
904 #ifdef INET
905 struct ip *ip;
906 #ifdef INET6
907 struct ip6_hdr *ip6;
908 #endif
909 #endif
910
911 struct ng_fec_portlist *p;
912 int port = 0, mask;
913
914 /*
915 * If there are only two ports, mask off all but the
916 * last bit for XORing. If there are 4, mask off all
917 * but the last 2 bits.
918 */
919 mask = b->fec_ifcnt == 2 ? 0x1 : 0x3;
920 eh = mtod(m, struct ether_header *);
921 #ifdef INET
922 ip = (struct ip *)(mtod(m, char *) +
923 sizeof(struct ether_header));
924 #ifdef INET6
925 ip6 = (struct ip6_hdr *)(mtod(m, char *) +
926 sizeof(struct ether_header));
927 #endif
928 #endif
929
930 /*
931 * The fg_fec_output() routine is supposed to leave a
932 * flag for us in the mbuf that tells us what hash to
933 * use, but sometimes a new mbuf is prepended to the
934 * chain, so we have to search every mbuf in the chain
935 * to find the flags.
936 */
937 m0 = m;
938 while (m0) {
939 if (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6))
940 break;
941 m0 = m0->m_next;
942 }
943 if (m0 == NULL)
944 return(EINVAL);
945
946 switch (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6)) {
947 case M_FEC_MAC:
948 port = (eh->ether_dhost[5] ^
949 eh->ether_shost[5]) & mask;
950 break;
951 #ifdef INET
952 case M_FEC_INET:
953 port = (ntohl(ip->ip_dst.s_addr) ^
954 ntohl(ip->ip_src.s_addr)) & mask;
955 break;
956 #ifdef INET6
957 case M_FEC_INET6:
958 port = (ip6->ip6_dst.s6_addr[15] ^
959 ip6->ip6_dst.s6_addr[15]) & mask;
960 break;
961 #endif
962 #endif
963 default:
964 return(EINVAL);
965 break;
966 }
967
968 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
969 if (port == p->fec_idx)
970 break;
971 }
972
973 /*
974 * Now that we've chosen a port, make sure it's
975 * alive. If it's not alive, cycle through the bundle
976 * looking for a port that is alive. If we don't find
977 * any, return an error.
978 */
979 if (p->fec_ifstat != 1) {
980 struct ng_fec_portlist *n = NULL;
981
982 n = TAILQ_NEXT(p, fec_list);
983 if (n == NULL)
984 n = TAILQ_FIRST(&b->ng_fec_ports);
985 while (n != p) {
986 if (n->fec_ifstat == 1)
987 break;
988 n = TAILQ_NEXT(n, fec_list);
989 if (n == NULL)
990 n = TAILQ_FIRST(&b->ng_fec_ports);
991 }
992 if (n == p)
993 return(EAGAIN);
994 p = n;
995 }
996
997 *ifp = p->fec_if;
998
999 return(0);
1000 }
1001
1002 /*
1003 * Now that the packet has been run through ether_output(), yank it
1004 * off our own send queue and stick it on the queue for the appropriate
1005 * underlying physical interface. Note that if the interface's send
1006 * queue is full, we save an error status in our private netgraph
1007 * space which will eventually be handed up to ng_fec_output(), which
1008 * will return it to the rest of the IP stack. We need to do this
1009 * in order to duplicate the effect of ether_output() returning ENOBUFS
1010 * when it detects that an interface's send queue is full. There's no
1011 * other way to signal the error status from here since the if_start()
1012 * routine is spec'ed to return void.
1013 *
1014 * Once the frame is queued, we call ether_output_frame() to initiate
1015 * transmission.
1016 */
1017 static void
1018 ng_fec_start(struct ifnet *ifp)
1019 {
1020 struct ng_fec_private *priv;
1021 struct ng_fec_bundle *b;
1022 struct ifnet *oifp = NULL;
1023 struct mbuf *m0;
1024 int error;
1025
1026 priv = ifp->if_softc;
1027 b = &priv->fec_bundle;
1028
1029 IF_DEQUEUE(&ifp->if_snd, m0);
1030 if (m0 == NULL)
1031 return;
1032
1033 BPF_MTAP(ifp, m0);
1034
1035 /* Queue up packet on the proper port. */
1036 error = ng_fec_choose_port(b, m0, &oifp);
1037 if (error) {
1038 ifp->if_ierrors++;
1039 m_freem(m0);
1040 priv->if_error = ENOBUFS;
1041 return;
1042 }
1043 ifp->if_opackets++;
1044
1045 priv->if_error = IF_HANDOFF(&oifp->if_snd, m0, oifp) ? 0 : ENOBUFS;
1046
1047 return;
1048 }
1049
1050 #ifdef DEBUG
1051 /*
1052 * Display an ioctl to the virtual interface
1053 */
1054
1055 static void
1056 ng_fec_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
1057 {
1058 char *str;
1059
1060 switch (command & IOC_DIRMASK) {
1061 case IOC_VOID:
1062 str = "IO";
1063 break;
1064 case IOC_OUT:
1065 str = "IOR";
1066 break;
1067 case IOC_IN:
1068 str = "IOW";
1069 break;
1070 case IOC_INOUT:
1071 str = "IORW";
1072 break;
1073 default:
1074 str = "IO??";
1075 }
1076 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
1077 ifp->if_xname,
1078 str,
1079 IOCGROUP(command),
1080 command & 0xff,
1081 IOCPARM_LEN(command));
1082 }
1083 #endif /* DEBUG */
1084
1085 /************************************************************************
1086 NETGRAPH NODE STUFF
1087 ************************************************************************/
1088
1089 /*
1090 * Constructor for a node
1091 */
1092 static int
1093 ng_fec_constructor(node_p node)
1094 {
1095 char ifname[NG_FEC_FEC_NAME_MAX + 1];
1096 struct ifnet *ifp;
1097 priv_p priv;
1098 struct ng_fec_bundle *b;
1099 int error = 0;
1100
1101 /* Allocate node and interface private structures */
1102 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
1103 if (priv == NULL)
1104 return (ENOMEM);
1105
1106 ifp = priv->ifp = if_alloc(IFT_ETHER);
1107 if (ifp == NULL) {
1108 FREE(priv, M_NETGRAPH);
1109 return (ENOSPC);
1110 }
1111 b = &priv->fec_bundle;
1112
1113 /* Link them together */
1114 ifp->if_softc = priv;
1115
1116 /* Get an interface unit number */
1117 if ((error = ng_fec_get_unit(&priv->unit)) != 0) {
1118 if_free(ifp);
1119 FREE(priv, M_NETGRAPH);
1120 return (error);
1121 }
1122
1123 /* Link together node and private info */
1124 NG_NODE_SET_PRIVATE(node, priv);
1125 priv->node = node;
1126
1127 /* Initialize interface structure */
1128 if_initname(ifp, NG_FEC_FEC_NAME, priv->unit);
1129 ifp->if_start = ng_fec_start;
1130 ifp->if_ioctl = ng_fec_ioctl;
1131 ifp->if_init = ng_fec_init;
1132 ifp->if_watchdog = NULL;
1133 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
1134 ifp->if_mtu = NG_FEC_MTU_DEFAULT;
1135 ifp->if_flags = (IFF_SIMPLEX|IFF_BROADCAST|IFF_MULTICAST);
1136 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
1137 ifp->if_addrlen = 0; /* XXX */
1138 ifp->if_hdrlen = 0; /* XXX */
1139 ifp->if_baudrate = 100000000; /* XXX */
1140 TAILQ_INIT(&ifp->if_addrhead); /* XXX useless - done in if_attach */
1141
1142 /* Give this node the same name as the interface (if possible) */
1143 bzero(ifname, sizeof(ifname));
1144 strlcpy(ifname, ifp->if_xname, sizeof(ifname));
1145 if (ng_name_node(node, ifname) != 0)
1146 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
1147
1148 /* Attach the interface */
1149 ether_ifattach(ifp, IFP2ENADDR(priv->ifp));
1150 callout_handle_init(&priv->fec_ch);
1151
1152 /* Override output method with our own */
1153 ifp->if_output = ng_fec_output;
1154
1155 TAILQ_INIT(&b->ng_fec_ports);
1156 b->fec_ifcnt = 0;
1157
1158 ifmedia_init(&priv->ifmedia, 0,
1159 ng_fec_ifmedia_upd, ng_fec_ifmedia_sts);
1160 ifmedia_add(&priv->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
1161 ifmedia_set(&priv->ifmedia, IFM_ETHER|IFM_NONE);
1162
1163 /* Done */
1164 return (0);
1165 }
1166
1167 /*
1168 * Receive a control message
1169 */
1170 static int
1171 ng_fec_rcvmsg(node_p node, item_p item, hook_p lasthook)
1172 {
1173 const priv_p priv = NG_NODE_PRIVATE(node);
1174 struct ng_fec_bundle *b;
1175 struct ng_mesg *resp = NULL;
1176 struct ng_mesg *msg;
1177 char *ifname;
1178 int error = 0;
1179
1180 NGI_GET_MSG(item, msg);
1181 b = &priv->fec_bundle;
1182
1183 switch (msg->header.typecookie) {
1184 case NGM_FEC_COOKIE:
1185 switch (msg->header.cmd) {
1186 case NGM_FEC_ADD_IFACE:
1187 ifname = msg->data;
1188 error = ng_fec_addport(priv, ifname);
1189 break;
1190 case NGM_FEC_DEL_IFACE:
1191 ifname = msg->data;
1192 error = ng_fec_delport(priv, ifname);
1193 break;
1194 case NGM_FEC_SET_MODE_MAC:
1195 b->fec_btype = FEC_BTYPE_MAC;
1196 break;
1197 #ifdef INET
1198 case NGM_FEC_SET_MODE_INET:
1199 b->fec_btype = FEC_BTYPE_INET;
1200 break;
1201 #ifdef INET6
1202 case NGM_FEC_SET_MODE_INET6:
1203 b->fec_btype = FEC_BTYPE_INET6;
1204 break;
1205 #endif
1206 #endif
1207 default:
1208 error = EINVAL;
1209 break;
1210 }
1211 break;
1212 default:
1213 error = EINVAL;
1214 break;
1215 }
1216 NG_RESPOND_MSG(error, node, item, resp);
1217 NG_FREE_MSG(msg);
1218 return (error);
1219 }
1220
1221 /*
1222 * Shutdown and remove the node and its associated interface.
1223 */
1224 static int
1225 ng_fec_shutdown(node_p node)
1226 {
1227 const priv_p priv = NG_NODE_PRIVATE(node);
1228 struct ng_fec_bundle *b;
1229 struct ng_fec_portlist *p;
1230
1231 b = &priv->fec_bundle;
1232 ng_fec_stop(priv->ifp);
1233
1234 while (!TAILQ_EMPTY(&b->ng_fec_ports)) {
1235 p = TAILQ_FIRST(&b->ng_fec_ports);
1236 ng_fec_delport(priv, p->fec_if->if_xname);
1237 }
1238
1239 ether_ifdetach(priv->ifp);
1240 if_free_type(priv->ifp, IFT_ETHER);
1241 ifmedia_removeall(&priv->ifmedia);
1242 ng_fec_free_unit(priv->unit);
1243 FREE(priv, M_NETGRAPH);
1244 NG_NODE_SET_PRIVATE(node, NULL);
1245 NG_NODE_UNREF(node);
1246 return (0);
1247 }
1248
1249 /*
1250 * Handle loading and unloading for this node type.
1251 */
1252 static int
1253 ng_fec_mod_event(module_t mod, int event, void *data)
1254 {
1255 int error = 0;
1256
1257 switch (event) {
1258 case MOD_LOAD:
1259 mtx_init(&ng_fec_mtx, "ng_fec", NULL, MTX_DEF);
1260 break;
1261 case MOD_UNLOAD:
1262 mtx_destroy(&ng_fec_mtx);
1263 break;
1264 default:
1265 error = EOPNOTSUPP;
1266 break;
1267 }
1268 return (error);
1269 }
Cache object: 17a0a56fa36f64be50c500da7c0890a6
|