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