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.3/sys/netgraph/ng_fec.c 136588 2004-10-16 08:43:07Z cvs2svn $
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 /*
129 * We need a way to stash a pointer to our netgraph node in the
130 * ifnet structure so that receive handling works. As far as I can
131 * tell, although there is an AF_NETGRAPH address family, it's only
132 * used to identify sockaddr_ng structures: there is no netgraph address
133 * family domain. This means the AF_NETGRAPH entry in ifp->if_afdata
134 * should be unused, so we can use to hold our node context.
135 */
136 #define IFP2NG(ifp) (struct ng_node *)(ifp->if_afdata[AF_NETGRAPH])
137 #define IFP2NG_SET(ifp, val) ifp->if_afdata[AF_NETGRAPH] = (val);
138 #define FEC_INC(x, y) (x) = (x + 1) % y
139
140 /*
141 * Current fast etherchannel implementations use either 2 or 4
142 * ports, so for now we limit the maximum bundle size to 4 interfaces.
143 */
144 #define FEC_BUNDLESIZ 4
145
146 struct ng_fec_portlist {
147 struct ifnet *fec_if;
148 void (*fec_if_input) (struct ifnet *,
149 struct mbuf *);
150 int fec_idx;
151 int fec_ifstat;
152 struct ether_addr fec_mac;
153 TAILQ_ENTRY(ng_fec_portlist) fec_list;
154 };
155
156 struct ng_fec_bundle {
157 TAILQ_HEAD(,ng_fec_portlist) ng_fec_ports;
158 int fec_ifcnt;
159 int fec_btype;
160 int (*fec_if_output) (struct ifnet *,
161 struct mbuf *,
162 struct sockaddr *,
163 struct rtentry *);
164 };
165
166 #define FEC_BTYPE_MAC 0x01
167 #define FEC_BTYPE_INET 0x02
168 #define FEC_BTYPE_INET6 0x03
169
170 /* Node private data */
171 struct ng_fec_private {
172 struct arpcom arpcom;
173 struct ifmedia ifmedia;
174 int if_flags;
175 int if_error; /* XXX */
176 int unit; /* Interface unit number */
177 node_p node; /* Our netgraph node */
178 struct ng_fec_bundle fec_bundle;/* Aggregate bundle */
179 struct callout_handle fec_ch; /* callout handle for ticker */
180 };
181 typedef struct ng_fec_private *priv_p;
182
183 /* Interface methods */
184 static void ng_fec_input(struct ifnet *, struct mbuf *);
185 static void ng_fec_start(struct ifnet *ifp);
186 static int ng_fec_choose_port(struct ng_fec_bundle *b,
187 struct mbuf *m, struct ifnet **ifp);
188 static int ng_fec_setport(struct ifnet *ifp, u_long cmd, caddr_t data);
189 static void ng_fec_init(void *arg);
190 static void ng_fec_stop(struct ifnet *ifp);
191 static int ng_fec_ifmedia_upd(struct ifnet *ifp);
192 static void ng_fec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
193 static int ng_fec_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
194 static int ng_fec_output(struct ifnet *ifp, struct mbuf *m0,
195 struct sockaddr *dst, struct rtentry *rt0);
196 static void ng_fec_tick(void *arg);
197 static int ng_fec_addport(struct ng_fec_private *priv, char *iface);
198 static int ng_fec_delport(struct ng_fec_private *priv, char *iface);
199
200 #ifdef DEBUG
201 static void ng_fec_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
202 #endif
203
204 /* Netgraph methods */
205 static ng_constructor_t ng_fec_constructor;
206 static ng_rcvmsg_t ng_fec_rcvmsg;
207 static ng_shutdown_t ng_fec_shutdown;
208
209 /* List of commands and how to convert arguments to/from ASCII */
210 static const struct ng_cmdlist ng_fec_cmds[] = {
211 {
212 NGM_FEC_COOKIE,
213 NGM_FEC_ADD_IFACE,
214 "add_iface",
215 &ng_parse_string_type,
216 NULL,
217 },
218 {
219 NGM_FEC_COOKIE,
220 NGM_FEC_DEL_IFACE,
221 "del_iface",
222 &ng_parse_string_type,
223 NULL,
224 },
225 {
226 NGM_FEC_COOKIE,
227 NGM_FEC_SET_MODE_MAC,
228 "set_mode_mac",
229 NULL,
230 NULL,
231 },
232 {
233 NGM_FEC_COOKIE,
234 NGM_FEC_SET_MODE_INET,
235 "set_mode_inet",
236 NULL,
237 NULL,
238 },
239 { 0 }
240 };
241
242 /* Node type descriptor */
243 static struct ng_type typestruct = {
244 .version = NG_ABI_VERSION,
245 .name = NG_FEC_NODE_TYPE,
246 .constructor = ng_fec_constructor,
247 .rcvmsg = ng_fec_rcvmsg,
248 .shutdown = ng_fec_shutdown,
249 .cmdlist = ng_fec_cmds,
250 };
251 NETGRAPH_INIT(fec, &typestruct);
252
253 /* We keep a bitmap indicating which unit numbers are free.
254 One means the unit number is free, zero means it's taken. */
255 static int *ng_fec_units = NULL;
256 static int ng_fec_units_len = 0;
257 static int ng_units_in_use = 0;
258
259 #define UNITS_BITSPERWORD (sizeof(*ng_fec_units) * NBBY)
260
261 static struct mtx ng_fec_mtx;
262 MTX_SYSINIT(ng_fec, &ng_fec_mtx, "ng_fec", MTX_DEF);
263
264 /*
265 * Find the first free unit number for a new interface.
266 * Increase the size of the unit bitmap as necessary.
267 */
268 static __inline int
269 ng_fec_get_unit(int *unit)
270 {
271 int index, bit;
272
273 mtx_lock(&ng_fec_mtx);
274 for (index = 0; index < ng_fec_units_len
275 && ng_fec_units[index] == 0; index++);
276 if (index == ng_fec_units_len) { /* extend array */
277 int i, *newarray, newlen;
278
279 newlen = (2 * ng_fec_units_len) + 4;
280 MALLOC(newarray, int *, newlen * sizeof(*ng_fec_units),
281 M_NETGRAPH, M_NOWAIT);
282 if (newarray == NULL) {
283 mtx_unlock(&ng_fec_mtx);
284 return (ENOMEM);
285 }
286 bcopy(ng_fec_units, newarray,
287 ng_fec_units_len * sizeof(*ng_fec_units));
288 for (i = ng_fec_units_len; i < newlen; i++)
289 newarray[i] = ~0;
290 if (ng_fec_units != NULL)
291 FREE(ng_fec_units, M_NETGRAPH);
292 ng_fec_units = newarray;
293 ng_fec_units_len = newlen;
294 }
295 bit = ffs(ng_fec_units[index]) - 1;
296 KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
297 ("%s: word=%d bit=%d", __FUNCTION__, ng_fec_units[index], bit));
298 ng_fec_units[index] &= ~(1 << bit);
299 *unit = (index * UNITS_BITSPERWORD) + bit;
300 ng_units_in_use++;
301 mtx_unlock(&ng_fec_mtx);
302 return (0);
303 }
304
305 /*
306 * Free a no longer needed unit number.
307 */
308 static __inline void
309 ng_fec_free_unit(int unit)
310 {
311 int index, bit;
312
313 index = unit / UNITS_BITSPERWORD;
314 bit = unit % UNITS_BITSPERWORD;
315 mtx_lock(&ng_fec_mtx);
316 KASSERT(index < ng_fec_units_len,
317 ("%s: unit=%d len=%d", __FUNCTION__, unit, ng_fec_units_len));
318 KASSERT((ng_fec_units[index] & (1 << bit)) == 0,
319 ("%s: unit=%d is free", __FUNCTION__, unit));
320 ng_fec_units[index] |= (1 << bit);
321 /*
322 * XXX We could think about reducing the size of ng_fec_units[]
323 * XXX here if the last portion is all ones
324 * XXX At least free it if no more units
325 * Needed if we are to eventually be able to unload.
326 */
327 ng_units_in_use--;
328 if (ng_units_in_use == 0) { /* XXX make SMP safe */
329 FREE(ng_fec_units, M_NETGRAPH);
330 ng_fec_units_len = 0;
331 ng_fec_units = NULL;
332 }
333 mtx_unlock(&ng_fec_mtx);
334 }
335
336 /************************************************************************
337 INTERFACE STUFF
338 ************************************************************************/
339
340 static int
341 ng_fec_addport(struct ng_fec_private *priv, char *iface)
342 {
343 struct ng_fec_bundle *b;
344 struct ifnet *ifp, *bifp;
345 struct arpcom *ac;
346 struct ifaddr *ifa;
347 struct sockaddr_dl *sdl;
348 struct ng_fec_portlist *p, *new;
349
350 if (priv == NULL || iface == NULL)
351 return(EINVAL);
352
353 b = &priv->fec_bundle;
354 ifp = &priv->arpcom.ac_if;
355
356 /* Find the interface */
357 bifp = ifunit(iface);
358 if (bifp == NULL) {
359 printf("fec%d: tried to add iface %s, which "
360 "doesn't seem to exist\n", priv->unit, iface);
361 return(ENOENT);
362 }
363
364 /* See if we have room in the bundle */
365 if (b->fec_ifcnt == FEC_BUNDLESIZ) {
366 printf("fec%d: can't add new iface; bundle is full\n",
367 priv->unit);
368 return(ENOSPC);
369 }
370
371 /* See if the interface is already in the bundle */
372 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
373 if (p->fec_if == bifp) {
374 printf("fec%d: iface %s is already in this "
375 "bundle\n", priv->unit, iface);
376 return(EINVAL);
377 }
378 }
379
380 /*
381 * All interfaces must use the same output vector. Once the
382 * user attaches an interface of one type, make all subsequent
383 * interfaces have the same output vector.
384 */
385 if (b->fec_if_output != NULL) {
386 if (b->fec_if_output != bifp->if_output) {
387 printf("fec%d: iface %s is not the same type "
388 "as the other interface(s) already in "
389 "the bundle\n", priv->unit, iface);
390 return(EINVAL);
391 }
392 }
393
394 /* Allocate new list entry. */
395 MALLOC(new, struct ng_fec_portlist *,
396 sizeof(struct ng_fec_portlist), M_NETGRAPH, M_NOWAIT);
397 if (new == NULL)
398 return(ENOMEM);
399
400 ac = (struct arpcom *)bifp;
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((char *)ac->ac_enaddr,
415 priv->arpcom.ac_enaddr, ETHER_ADDR_LEN);
416 bcopy((char *)ac->ac_enaddr,
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((char *)ac->ac_enaddr,
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(priv->arpcom.ac_enaddr, ac->ac_enaddr, ETHER_ADDR_LEN);
432 bcopy(priv->arpcom.ac_enaddr, 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 arpcom *ac;
457 struct ifaddr *ifa;
458 struct sockaddr_dl *sdl;
459 struct ng_fec_portlist *p;
460
461 if (priv == NULL || iface == NULL)
462 return(EINVAL);
463
464 b = &priv->fec_bundle;
465 ifp = &priv->arpcom.ac_if;
466
467 /* Find the interface */
468 bifp = ifunit(iface);
469 if (bifp == NULL) {
470 printf("fec%d: tried to remove iface %s, which "
471 "doesn't seem to exist\n", priv->unit, iface);
472 return(ENOENT);
473 }
474
475 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
476 if (p->fec_if == bifp)
477 break;
478 }
479
480 if (p == NULL) {
481 printf("fec%d: tried to remove iface %s which "
482 "is not in our bundle\n", priv->unit, iface);
483 return(EINVAL);
484 }
485
486 /* Stop interface */
487 bifp->if_flags &= ~IFF_UP;
488 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
489
490 /* Restore MAC address. */
491 ac = (struct arpcom *)bifp;
492 ifa = ifaddr_byindex(bifp->if_index);
493 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
494 bcopy((char *)&p->fec_mac, ac->ac_enaddr, ETHER_ADDR_LEN);
495 bcopy((char *)&p->fec_mac, LLADDR(sdl), ETHER_ADDR_LEN);
496
497 /* Restore input vector */
498 bifp->if_input = p->fec_if_input;
499
500 /* Remove our node context pointer. */
501 IF_AFDATA_LOCK(bifp);
502 bifp->if_afdata[AF_NETGRAPH] = NULL;
503 IF_AFDATA_UNLOCK(bifp);
504
505 /* Delete port */
506 TAILQ_REMOVE(&b->ng_fec_ports, p, fec_list);
507 FREE(p, M_NETGRAPH);
508 b->fec_ifcnt--;
509
510 if (b->fec_ifcnt == 0)
511 b->fec_if_output = NULL;
512
513 return(0);
514 }
515
516 /*
517 * Pass an ioctl command down to all the underyling interfaces in a
518 * bundle. Used for setting multicast filters and flags.
519 */
520
521 static int
522 ng_fec_setport(struct ifnet *ifp, u_long command, caddr_t data)
523 {
524 struct ng_fec_private *priv;
525 struct ng_fec_bundle *b;
526 struct ifnet *oifp;
527 struct ng_fec_portlist *p;
528
529 priv = ifp->if_softc;
530 b = &priv->fec_bundle;
531
532 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
533 oifp = p->fec_if;
534 if (oifp != NULL)
535 (*oifp->if_ioctl)(oifp, command, data);
536 }
537
538 return(0);
539 }
540
541 static void
542 ng_fec_init(void *arg)
543 {
544 struct ng_fec_private *priv;
545 struct ng_fec_bundle *b;
546 struct ifnet *ifp, *bifp;
547 struct ng_fec_portlist *p;
548
549 ifp = arg;
550 priv = ifp->if_softc;
551 b = &priv->fec_bundle;
552
553 if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
554 printf("fec%d: invalid bundle "
555 "size: %d\n", priv->unit,
556 b->fec_ifcnt);
557 return;
558 }
559
560 ng_fec_stop(ifp);
561
562 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
563 bifp = p->fec_if;
564 bifp->if_flags |= IFF_UP;
565 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
566 /* mark iface as up and let the monitor check it */
567 p->fec_ifstat = -1;
568 }
569
570 priv->fec_ch = timeout(ng_fec_tick, priv, hz);
571
572 return;
573 }
574
575 static void
576 ng_fec_stop(struct ifnet *ifp)
577 {
578 struct ng_fec_private *priv;
579 struct ng_fec_bundle *b;
580 struct ifnet *bifp;
581 struct ng_fec_portlist *p;
582
583 priv = ifp->if_softc;
584 b = &priv->fec_bundle;
585
586 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
587 bifp = p->fec_if;
588 bifp->if_flags &= ~IFF_UP;
589 (*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
590 }
591
592 untimeout(ng_fec_tick, priv, priv->fec_ch);
593
594 return;
595 }
596
597 static void
598 ng_fec_tick(void *arg)
599 {
600 struct ng_fec_private *priv;
601 struct ng_fec_bundle *b;
602 struct ifmediareq ifmr;
603 struct ifnet *ifp;
604 struct ng_fec_portlist *p;
605 int error = 0;
606
607 priv = arg;
608 b = &priv->fec_bundle;
609
610 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
611 bzero((char *)&ifmr, sizeof(ifmr));
612 ifp = p->fec_if;
613 error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
614 if (error) {
615 printf("fec%d: failed to check status "
616 "of link %s\n", priv->unit, ifp->if_xname);
617 continue;
618 }
619
620 if (ifmr.ifm_status & IFM_AVALID) {
621 if (ifmr.ifm_status & IFM_ACTIVE) {
622 if (p->fec_ifstat == -1 ||
623 p->fec_ifstat == 0) {
624 p->fec_ifstat = 1;
625 printf("fec%d: port %s in bundle "
626 "is up\n", priv->unit,
627 ifp->if_xname);
628 }
629 } else {
630 if (p->fec_ifstat == -1 ||
631 p->fec_ifstat == 1) {
632 p->fec_ifstat = 0;
633 printf("fec%d: port %s in bundle "
634 "is down\n", priv->unit,
635 ifp->if_xname);
636 }
637 }
638 }
639 }
640
641 ifp = &priv->arpcom.ac_if;
642 if (ifp->if_flags & IFF_RUNNING)
643 priv->fec_ch = timeout(ng_fec_tick, priv, hz);
644
645 return;
646 }
647
648 static int
649 ng_fec_ifmedia_upd(struct ifnet *ifp)
650 {
651 return(0);
652 }
653
654 static void ng_fec_ifmedia_sts(struct ifnet *ifp,
655 struct ifmediareq *ifmr)
656 {
657 struct ng_fec_private *priv;
658 struct ng_fec_bundle *b;
659 struct ng_fec_portlist *p;
660
661 priv = ifp->if_softc;
662 b = &priv->fec_bundle;
663
664 ifmr->ifm_status = IFM_AVALID;
665 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
666 if (p->fec_ifstat) {
667 ifmr->ifm_status |= IFM_ACTIVE;
668 break;
669 }
670 }
671
672 return;
673 }
674
675 /*
676 * Process an ioctl for the virtual interface
677 */
678 static int
679 ng_fec_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
680 {
681 struct ifreq *const ifr = (struct ifreq *) data;
682 int s, error = 0;
683 struct ng_fec_private *priv;
684 struct ng_fec_bundle *b;
685
686 priv = ifp->if_softc;
687 b = &priv->fec_bundle;
688
689 #ifdef DEBUG
690 ng_fec_print_ioctl(ifp, command, data);
691 #endif
692 s = splimp();
693 switch (command) {
694
695 /* These two are mostly handled at a higher layer */
696 case SIOCSIFADDR:
697 case SIOCGIFADDR:
698 case SIOCSIFMTU:
699 error = ether_ioctl(ifp, command, data);
700 break;
701
702 /* Set flags */
703 case SIOCSIFFLAGS:
704 /*
705 * If the interface is marked up and stopped, then start it.
706 * If it is marked down and running, then stop it.
707 */
708 if (ifr->ifr_flags & IFF_UP) {
709 if (!(ifp->if_flags & IFF_RUNNING)) {
710 /* Sanity. */
711 if (b->fec_ifcnt == 1 || b->fec_ifcnt == 3) {
712 printf("fec%d: invalid bundle "
713 "size: %d\n", priv->unit,
714 b->fec_ifcnt);
715 error = EINVAL;
716 break;
717 }
718 ifp->if_flags &= ~(IFF_OACTIVE);
719 ifp->if_flags |= IFF_RUNNING;
720 ng_fec_init(ifp);
721 }
722 /*
723 * Bubble down changes in promisc mode to
724 * underlying interfaces.
725 */
726 if ((ifp->if_flags & IFF_PROMISC) !=
727 (priv->if_flags & IFF_PROMISC)) {
728 ng_fec_setport(ifp, command, data);
729 priv->if_flags = ifp->if_flags;
730 }
731 } else {
732 if (ifp->if_flags & IFF_RUNNING)
733 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
734 ng_fec_stop(ifp);
735 }
736 break;
737
738 case SIOCADDMULTI:
739 case SIOCDELMULTI:
740 ng_fec_setport(ifp, command, data);
741 error = 0;
742 break;
743 case SIOCGIFMEDIA:
744 case SIOCSIFMEDIA:
745 error = ifmedia_ioctl(ifp, ifr, &priv->ifmedia, command);
746 break;
747 /* Stuff that's not supported */
748 case SIOCSIFPHYS:
749 error = EOPNOTSUPP;
750 break;
751
752 default:
753 error = EINVAL;
754 break;
755 }
756 (void) splx(s);
757 return (error);
758 }
759
760 /*
761 * This routine spies on mbufs received by underlying network device
762 * drivers. When we add an interface to our bundle, we override its
763 * if_input routine with a pointer to ng_fec_input(). This means we
764 * get to look at all the device's packets before sending them to the
765 * real ether_input() for processing by the stack. Once we verify the
766 * packet comes from an interface that's been aggregated into
767 * our bundle, we fix up the rcvif pointer and increment our
768 * packet counters so that it looks like the frames are actually
769 * coming from us.
770 */
771 static void
772 ng_fec_input(struct ifnet *ifp, struct mbuf *m0)
773 {
774 struct ng_node *node;
775 struct ng_fec_private *priv;
776 struct ng_fec_bundle *b;
777 struct ifnet *bifp;
778 struct ng_fec_portlist *p;
779
780 /* Sanity check */
781 if (ifp == NULL || m0 == NULL)
782 return;
783
784 node = IFP2NG(ifp);
785
786 /* Sanity check part II */
787 if (node == NULL)
788 return;
789
790 priv = NG_NODE_PRIVATE(node);
791 b = &priv->fec_bundle;
792 bifp = &priv->arpcom.ac_if;
793
794 TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
795 if (p->fec_if == m0->m_pkthdr.rcvif)
796 break;
797 }
798
799 /* Wasn't meant for us; leave this frame alone. */
800 if (p == NULL)
801 return;
802
803 /*
804 * Check for a BPF tap on the underlying interface. This
805 * is mainly a debugging aid: it allows tcpdump-ing of an
806 * individual interface in a bundle to work, which it
807 * otherwise would not. BPF tapping of our own aggregate
808 * interface will occur once we call ether_input().
809 */
810 BPF_MTAP(m0->m_pkthdr.rcvif, m0);
811
812 /* Convince the system that this is our frame. */
813 m0->m_pkthdr.rcvif = bifp;
814 bifp->if_ipackets++;
815 bifp->if_ibytes += m0->m_pkthdr.len + sizeof(struct ether_header);
816
817 (*bifp->if_input)(bifp, m0);
818
819 return;
820 }
821
822 /*
823 * Take a quick peek at the packet and see if it's ok for us to use
824 * the inet or inet6 hash methods on it, if they're enabled. We do
825 * this by setting flags in the mbuf header. Once we've made up our
826 * mind what to do, we pass the frame to output vector for further
827 * processing.
828 */
829
830 static int
831 ng_fec_output(struct ifnet *ifp, struct mbuf *m,
832 struct sockaddr *dst, struct rtentry *rt0)
833 {
834 const priv_p priv = (priv_p) ifp->if_softc;
835 struct ng_fec_bundle *b;
836 int error;
837
838 /* Check interface flags */
839 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_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);
1103 if (priv == NULL)
1104 return (ENOMEM);
1105 bzero(priv, sizeof(*priv));
1106
1107 ifp = &priv->arpcom.ac_if;
1108 b = &priv->fec_bundle;
1109
1110 /* Link them together */
1111 ifp->if_softc = priv;
1112
1113 /* Get an interface unit number */
1114 if ((error = ng_fec_get_unit(&priv->unit)) != 0) {
1115 FREE(ifp, M_NETGRAPH);
1116 FREE(priv, M_NETGRAPH);
1117 return (error);
1118 }
1119
1120 /* Link together node and private info */
1121 NG_NODE_SET_PRIVATE(node, priv);
1122 priv->node = node;
1123
1124 /* Initialize interface structure */
1125 if_initname(ifp, NG_FEC_FEC_NAME, priv->unit);
1126 ifp->if_start = ng_fec_start;
1127 ifp->if_ioctl = ng_fec_ioctl;
1128 ifp->if_init = ng_fec_init;
1129 ifp->if_watchdog = NULL;
1130 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
1131 ifp->if_mtu = NG_FEC_MTU_DEFAULT;
1132 ifp->if_flags = (IFF_SIMPLEX|IFF_BROADCAST|IFF_MULTICAST);
1133 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
1134 ifp->if_addrlen = 0; /* XXX */
1135 ifp->if_hdrlen = 0; /* XXX */
1136 ifp->if_baudrate = 100000000; /* XXX */
1137 TAILQ_INIT(&ifp->if_addrhead); /* XXX useless - done in if_attach */
1138
1139 /* Give this node the same name as the interface (if possible) */
1140 bzero(ifname, sizeof(ifname));
1141 strlcpy(ifname, ifp->if_xname, sizeof(ifname));
1142 if (ng_name_node(node, ifname) != 0)
1143 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
1144
1145 /* Attach the interface */
1146 ether_ifattach(ifp, priv->arpcom.ac_enaddr);
1147 callout_handle_init(&priv->fec_ch);
1148
1149 /* Override output method with our own */
1150 ifp->if_output = ng_fec_output;
1151
1152 TAILQ_INIT(&b->ng_fec_ports);
1153 b->fec_ifcnt = 0;
1154
1155 ifmedia_init(&priv->ifmedia, 0,
1156 ng_fec_ifmedia_upd, ng_fec_ifmedia_sts);
1157 ifmedia_add(&priv->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
1158 ifmedia_set(&priv->ifmedia, IFM_ETHER|IFM_NONE);
1159
1160 /* Done */
1161 return (0);
1162 }
1163
1164 /*
1165 * Receive a control message
1166 */
1167 static int
1168 ng_fec_rcvmsg(node_p node, item_p item, hook_p lasthook)
1169 {
1170 const priv_p priv = NG_NODE_PRIVATE(node);
1171 struct ng_fec_bundle *b;
1172 struct ng_mesg *resp = NULL;
1173 struct ng_mesg *msg;
1174 char *ifname;
1175 int error = 0;
1176
1177 NGI_GET_MSG(item, msg);
1178 b = &priv->fec_bundle;
1179
1180 switch (msg->header.typecookie) {
1181 case NGM_FEC_COOKIE:
1182 switch (msg->header.cmd) {
1183 case NGM_FEC_ADD_IFACE:
1184 ifname = msg->data;
1185 error = ng_fec_addport(priv, ifname);
1186 break;
1187 case NGM_FEC_DEL_IFACE:
1188 ifname = msg->data;
1189 error = ng_fec_delport(priv, ifname);
1190 break;
1191 case NGM_FEC_SET_MODE_MAC:
1192 b->fec_btype = FEC_BTYPE_MAC;
1193 break;
1194 #ifdef INET
1195 case NGM_FEC_SET_MODE_INET:
1196 b->fec_btype = FEC_BTYPE_INET;
1197 break;
1198 #ifdef INET6
1199 case NGM_FEC_SET_MODE_INET6:
1200 b->fec_btype = FEC_BTYPE_INET6;
1201 break;
1202 #endif
1203 #endif
1204 default:
1205 error = EINVAL;
1206 break;
1207 }
1208 break;
1209 default:
1210 error = EINVAL;
1211 break;
1212 }
1213 NG_RESPOND_MSG(error, node, item, resp);
1214 NG_FREE_MSG(msg);
1215 return (error);
1216 }
1217
1218 /*
1219 * Shutdown and remove the node and its associated interface.
1220 */
1221 static int
1222 ng_fec_shutdown(node_p node)
1223 {
1224 const priv_p priv = NG_NODE_PRIVATE(node);
1225 struct ng_fec_bundle *b;
1226 struct ng_fec_portlist *p;
1227
1228 b = &priv->fec_bundle;
1229 ng_fec_stop(&priv->arpcom.ac_if);
1230
1231 while (!TAILQ_EMPTY(&b->ng_fec_ports)) {
1232 p = TAILQ_FIRST(&b->ng_fec_ports);
1233 ng_fec_delport(priv, p->fec_if->if_xname);
1234 }
1235
1236 ether_ifdetach(&priv->arpcom.ac_if);
1237 ifmedia_removeall(&priv->ifmedia);
1238 ng_fec_free_unit(priv->unit);
1239 FREE(priv, M_NETGRAPH);
1240 NG_NODE_SET_PRIVATE(node, NULL);
1241 NG_NODE_UNREF(node);
1242 return (0);
1243 }
Cache object: efac0add73c8838b02f4366be41fc873
|