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