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