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