The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/drivers/net/bonding/bond_main.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * originally based on the dummy device.
    3  *
    4  * Copyright 1999, Thomas Davis, tadavis@lbl.gov.  
    5  * Licensed under the GPL. Based on dummy.c, and eql.c devices.
    6  *
    7  * bonding.c: an Ethernet Bonding driver
    8  *
    9  * This is useful to talk to a Cisco EtherChannel compatible equipment:
   10  *      Cisco 5500
   11  *      Sun Trunking (Solaris)
   12  *      Alteon AceDirector Trunks
   13  *      Linux Bonding
   14  *      and probably many L2 switches ...
   15  *
   16  * How it works:
   17  *    ifconfig bond0 ipaddress netmask up
   18  *      will setup a network device, with an ip address.  No mac address 
   19  *      will be assigned at this time.  The hw mac address will come from 
   20  *      the first slave bonded to the channel.  All slaves will then use 
   21  *      this hw mac address.
   22  *
   23  *    ifconfig bond0 down
   24  *         will release all slaves, marking them as down.
   25  *
   26  *    ifenslave bond0 eth0
   27  *      will attach eth0 to bond0 as a slave.  eth0 hw mac address will either
   28  *      a: be used as initial mac address
   29  *      b: if a hw mac address already is there, eth0's hw mac address 
   30  *         will then be set from bond0.
   31  *
   32  * v0.1 - first working version.
   33  * v0.2 - changed stats to be calculated by summing slaves stats.
   34  *
   35  * Changes:
   36  * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
   37  * - fix leaks on failure at bond_init
   38  *
   39  * 2000/09/30 - Willy Tarreau <willy at meta-x.org>
   40  *     - added trivial code to release a slave device.
   41  *     - fixed security bug (CAP_NET_ADMIN not checked)
   42  *     - implemented MII link monitoring to disable dead links :
   43  *       All MII capable slaves are checked every <miimon> milliseconds
   44  *       (100 ms seems good). This value can be changed by passing it to
   45  *       insmod. A value of zero disables the monitoring (default).
   46  *     - fixed an infinite loop in bond_xmit_roundrobin() when there's no
   47  *       good slave.
   48  *     - made the code hopefully SMP safe
   49  *
   50  * 2000/10/03 - Willy Tarreau <willy at meta-x.org>
   51  *     - optimized slave lists based on relevant suggestions from Thomas Davis
   52  *     - implemented active-backup method to obtain HA with two switches:
   53  *       stay as long as possible on the same active interface, while we
   54  *       also monitor the backup one (MII link status) because we want to know
   55  *       if we are able to switch at any time. ( pass "mode=1" to insmod )
   56  *     - lots of stress testings because we need it to be more robust than the
   57  *       wires ! :->
   58  *
   59  * 2000/10/09 - Willy Tarreau <willy at meta-x.org>
   60  *     - added up and down delays after link state change.
   61  *     - optimized the slaves chaining so that when we run forward, we never
   62  *       repass through the bond itself, but we can find it by searching
   63  *       backwards. Renders the deletion more difficult, but accelerates the
   64  *       scan.
   65  *     - smarter enslaving and releasing.
   66  *     - finer and more robust SMP locking
   67  *
   68  * 2000/10/17 - Willy Tarreau <willy at meta-x.org>
   69  *     - fixed two potential SMP race conditions
   70  *
   71  * 2000/10/18 - Willy Tarreau <willy at meta-x.org>
   72  *     - small fixes to the monitoring FSM in case of zero delays
   73  * 2000/11/01 - Willy Tarreau <willy at meta-x.org>
   74  *     - fixed first slave not automatically used in trunk mode.
   75  * 2000/11/10 : spelling of "EtherChannel" corrected.
   76  * 2000/11/13 : fixed a race condition in case of concurrent accesses to ioctl().
   77  * 2000/12/16 : fixed improper usage of rtnl_exlock_nowait().
   78  *
   79  * 2001/1/3 - Chad N. Tindel <ctindel at ieee dot org>
   80  *     - The bonding driver now simulates MII status monitoring, just like
   81  *       a normal network device.  It will show that the link is down iff
   82  *       every slave in the bond shows that their links are down.  If at least
   83  *       one slave is up, the bond's MII status will appear as up.
   84  *
   85  * 2001/2/7 - Chad N. Tindel <ctindel at ieee dot org>
   86  *     - Applications can now query the bond from user space to get
   87  *       information which may be useful.  They do this by calling
   88  *       the BOND_INFO_QUERY ioctl.  Once the app knows how many slaves
   89  *       are in the bond, it can call the BOND_SLAVE_INFO_QUERY ioctl to
   90  *       get slave specific information (# link failures, etc).  See
   91  *       <linux/if_bonding.h> for more details.  The structs of interest
   92  *       are ifbond and ifslave.
   93  *
   94  * 2001/4/5 - Chad N. Tindel <ctindel at ieee dot org>
   95  *     - Ported to 2.4 Kernel
   96  * 
   97  * 2001/5/2 - Jeffrey E. Mast <jeff at mastfamily dot com>
   98  *     - When a device is detached from a bond, the slave device is no longer
   99  *       left thinking that is has a master.
  100  *
  101  * 2001/5/16 - Jeffrey E. Mast <jeff at mastfamily dot com>
  102  *     - memset did not appropriately initialized the bond rw_locks. Used 
  103  *       rwlock_init to initialize to unlocked state to prevent deadlock when 
  104  *       first attempting a lock
  105  *     - Called SET_MODULE_OWNER for bond device
  106  *
  107  * 2001/5/17 - Tim Anderson <tsa at mvista.com>
  108  *     - 2 paths for releasing for slave release; 1 through ioctl
  109  *       and 2) through close. Both paths need to release the same way.
  110  *     - the free slave in bond release is changing slave status before
  111  *       the free. The netdev_set_master() is intended to change slave state
  112  *       so it should not be done as part of the release process.
  113  *     - Simple rule for slave state at release: only the active in A/B and
  114  *       only one in the trunked case.
  115  *
  116  * 2001/6/01 - Tim Anderson <tsa at mvista.com>
  117  *     - Now call dev_close when releasing a slave so it doesn't screw up
  118  *       out routing table.
  119  *
  120  * 2001/6/01 - Chad N. Tindel <ctindel at ieee dot org>
  121  *     - Added /proc support for getting bond and slave information.
  122  *       Information is in /proc/net/<bond device>/info. 
  123  *     - Changed the locking when calling bond_close to prevent deadlock.
  124  *
  125  * 2001/8/05 - Janice Girouard <girouard at us.ibm.com>
  126  *     - correct problem where refcnt of slave is not incremented in bond_ioctl
  127  *       so the system hangs when halting.
  128  *     - correct locking problem when unable to malloc in bond_enslave.
  129  *     - adding bond_xmit_xor logic.
  130  *     - adding multiple bond device support.
  131  *
  132  * 2001/8/13 - Erik Habbinga <erik_habbinga at hp dot com>
  133  *     - correct locking problem with rtnl_exlock_nowait
  134  *
  135  * 2001/8/23 - Janice Girouard <girouard at us.ibm.com>
  136  *     - bzero initial dev_bonds, to correct oops
  137  *     - convert SIOCDEVPRIVATE to new MII ioctl calls
  138  *
  139  * 2001/9/13 - Takao Indoh <indou dot takao at jp dot fujitsu dot com>
  140  *     - Add the BOND_CHANGE_ACTIVE ioctl implementation
  141  *
  142  * 2001/9/14 - Mark Huth <mhuth at mvista dot com>
  143  *     - Change MII_LINK_READY to not check for end of auto-negotiation,
  144  *       but only for an up link.
  145  *
  146  * 2001/9/20 - Chad N. Tindel <ctindel at ieee dot org>
  147  *     - Add the device field to bonding_t.  Previously the net_device 
  148  *       corresponding to a bond wasn't available from the bonding_t 
  149  *       structure.
  150  *
  151  * 2001/9/25 - Janice Girouard <girouard at us.ibm.com>
  152  *     - add arp_monitor for active backup mode
  153  *
  154  * 2001/10/23 - Takao Indoh <indou dot takao at jp dot fujitsu dot com>
  155  *     - Various memory leak fixes
  156  *
  157  * 2001/11/5 - Mark Huth <mark dot huth at mvista dot com>
  158  *     - Don't take rtnl lock in bond_mii_monitor as it deadlocks under 
  159  *       certain hotswap conditions.  
  160  *       Note:  this same change may be required in bond_arp_monitor ???
  161  *     - Remove possibility of calling bond_sethwaddr with NULL slave_dev ptr 
  162  *     - Handle hot swap ethernet interface deregistration events to remove
  163  *       kernel oops following hot swap of enslaved interface
  164  *
  165  * 2002/1/2 - Chad N. Tindel <ctindel at ieee dot org>
  166  *     - Restore original slave flags at release time.
  167  *
  168  * 2002/02/18 - Erik Habbinga <erik_habbinga at hp dot com>
  169  *     - bond_release(): calling kfree on our_slave after call to
  170  *       bond_restore_slave_flags, not before
  171  *     - bond_enslave(): saving slave flags into original_flags before
  172  *       call to netdev_set_master, so the IFF_SLAVE flag doesn't end
  173  *       up in original_flags
  174  *
  175  * 2002/04/05 - Mark Smith <mark.smith at comdev dot cc> and
  176  *              Steve Mead <steve.mead at comdev dot cc>
  177  *     - Port Gleb Natapov's multicast support patchs from 2.4.12
  178  *       to 2.4.18 adding support for multicast.
  179  *
  180  * 2002/06/10 - Tony Cureington <tony.cureington * hp_com>
  181  *     - corrected uninitialized pointer (ifr.ifr_data) in bond_check_dev_link;
  182  *       actually changed function to use MIIPHY, then MIIREG, and finally
  183  *       ETHTOOL to determine the link status
  184  *     - fixed bad ifr_data pointer assignments in bond_ioctl
  185  *     - corrected mode 1 being reported as active-backup in bond_get_info;
  186  *       also added text to distinguish type of load balancing (rr or xor)
  187  *     - change arp_ip_target module param from "1-12s" (array of 12 ptrs)
  188  *       to "s" (a single ptr)
  189  *
  190  * 2002/08/30 - Jay Vosburgh <fubar at us dot ibm dot com>
  191  *     - Removed acquisition of xmit_lock in set_multicast_list; caused
  192  *       deadlock on SMP (lock is held by caller).
  193  *     - Revamped SIOCGMIIPHY, SIOCGMIIREG portion of bond_check_dev_link().
  194  *
  195  * 2002/09/18 - Jay Vosburgh <fubar at us dot ibm dot com>
  196  *     - Fixed up bond_check_dev_link() (and callers): removed some magic
  197  *       numbers, banished local MII_ defines, wrapped ioctl calls to
  198  *       prevent EFAULT errors
  199  *
  200  * 2002/9/30 - Jay Vosburgh <fubar at us dot ibm dot com>
  201  *     - make sure the ip target matches the arp_target before saving the
  202  *       hw address.
  203  *
  204  * 2002/9/30 - Dan Eisner <eisner at 2robots dot com>
  205  *     - make sure my_ip is set before taking down the link, since
  206  *       not all switches respond if the source ip is not set.
  207  *
  208  * 2002/10/8 - Janice Girouard <girouard at us dot ibm dot com>
  209  *     - read in the local ip address when enslaving a device
  210  *     - add primary support
  211  *     - make sure 2*arp_interval has passed when a new device
  212  *       is brought on-line before taking it down.
  213  *
  214  * 2002/09/11 - Philippe De Muyter <phdm at macqel dot be>
  215  *     - Added bond_xmit_broadcast logic.
  216  *     - Added bond_mode() support function.
  217  *
  218  * 2002/10/26 - Laurent Deniel <laurent.deniel at free.fr>
  219  *     - allow to register multicast addresses only on active slave
  220  *       (useful in active-backup mode)
  221  *     - add multicast module parameter
  222  *     - fix deletion of multicast groups after unloading module
  223  *
  224  * 2002/11/06 - Kameshwara Rayaprolu <kameshwara.rao * wipro_com>
  225  *     - Changes to prevent panic from closing the device twice; if we close 
  226  *       the device in bond_release, we must set the original_flags to down 
  227  *       so it won't be closed again by the network layer.
  228  *
  229  * 2002/11/07 - Tony Cureington <tony.cureington * hp_com>
  230  *     - Fix arp_target_hw_addr memory leak
  231  *     - Created activebackup_arp_monitor function to handle arp monitoring 
  232  *       in active backup mode - the bond_arp_monitor had several problems... 
  233  *       such as allowing slaves to tx arps sequentially without any delay 
  234  *       for a response
  235  *     - Renamed bond_arp_monitor to loadbalance_arp_monitor and re-wrote
  236  *       this function to just handle arp monitoring in load-balancing mode;
  237  *       it is a lot more compact now
  238  *     - Changes to ensure one and only one slave transmits in active-backup 
  239  *       mode
  240  *     - Robustesize parameters; warn users about bad combinations of 
  241  *       parameters; also if miimon is specified and a network driver does 
  242  *       not support MII or ETHTOOL, inform the user of this
  243  *     - Changes to support link_failure_count when in arp monitoring mode
  244  *     - Fix up/down delay reported in /proc
  245  *     - Added version; log version; make version available from "modinfo -d"
  246  *     - Fixed problem in bond_check_dev_link - if the first IOCTL (SIOCGMIIPH)
  247  *       failed, the ETHTOOL ioctl never got a chance
  248  *
  249  * 2002/11/16 - Laurent Deniel <laurent.deniel at free.fr>
  250  *     - fix multicast handling in activebackup_arp_monitor
  251  *     - remove one unnecessary and confusing current_slave == slave test 
  252  *       in activebackup_arp_monitor
  253  *
  254  *  2002/11/17 - Laurent Deniel <laurent.deniel at free.fr>
  255  *     - fix bond_slave_info_query when slave_id = num_slaves
  256  *
  257  *  2002/11/19 - Janice Girouard <girouard at us dot ibm dot com>
  258  *     - correct ifr_data reference.  Update ifr_data reference
  259  *       to mii_ioctl_data struct values to avoid confusion.
  260  *
  261  *  2002/11/22 - Bert Barbe <bert.barbe at oracle dot com>
  262  *      - Add support for multiple arp_ip_target
  263  *
  264  *  2002/12/13 - Jay Vosburgh <fubar at us dot ibm dot com>
  265  *      - Changed to allow text strings for mode and multicast, e.g.,
  266  *        insmod bonding mode=active-backup.  The numbers still work.
  267  *        One change: an invalid choice will cause module load failure,
  268  *        rather than the previous behavior of just picking one.
  269  *      - Minor cleanups; got rid of dup ctype stuff, atoi function
  270  * 
  271  * 2003/02/07 - Jay Vosburgh <fubar at us dot ibm dot com>
  272  *      - Added use_carrier module parameter that causes miimon to
  273  *        use netif_carrier_ok() test instead of MII/ETHTOOL ioctls.
  274  *      - Minor cleanups; consolidated ioctl calls to one function.
  275  *
  276  * 2003/02/07 - Tony Cureington <tony.cureington * hp_com>
  277  *      - Fix bond_mii_monitor() logic error that could result in
  278  *        bonding round-robin mode ignoring links after failover/recovery
  279  *
  280  * 2003/03/17 - Jay Vosburgh <fubar at us dot ibm dot com>
  281  *      - kmalloc fix (GPF_KERNEL to GPF_ATOMIC) reported by
  282  *        Shmulik dot Hen at intel.com.
  283  *      - Based on discussion on mailing list, changed use of
  284  *        update_slave_cnt(), created wrapper functions for adding/removing
  285  *        slaves, changed bond_xmit_xor() to check slave_cnt instead of
  286  *        checking slave and slave->dev (which only worked by accident).
  287  *      - Misc code cleanup: get arp_send() prototype from header file,
  288  *        add max_bonds to bonding.txt.
  289  *
  290  * 2003/03/18 - Tsippy Mendelson <tsippy.mendelson at intel dot com> and
  291  *              Shmulik Hen <shmulik.hen at intel dot com>
  292  *      - Make sure only bond_attach_slave() and bond_detach_slave() can
  293  *        manipulate the slave list, including slave_cnt, even when in
  294  *        bond_release_all().
  295  *      - Fixed hang in bond_release() with traffic running:
  296  *        netdev_set_master() must not be called from within the bond lock.
  297  *
  298  * 2003/03/18 - Tsippy Mendelson <tsippy.mendelson at intel dot com> and
  299  *              Shmulik Hen <shmulik.hen at intel dot com>
  300  *      - Fixed hang in bond_enslave() with traffic running:
  301  *        netdev_set_master() must not be called from within the bond lock.
  302  *
  303  * 2003/03/18 - Amir Noam <amir.noam at intel dot com>
  304  *      - Added support for getting slave's speed and duplex via ethtool.
  305  *        Needed for 802.3ad and other future modes.
  306  *
  307  * 2003/03/18 - Tsippy Mendelson <tsippy.mendelson at intel dot com> and
  308  *              Shmulik Hen <shmulik.hen at intel dot com>
  309  *      - Enable support of modes that need to use the unique mac address of
  310  *        each slave.
  311  *        * bond_enslave(): Moved setting the slave's mac address, and
  312  *          openning it, from the application to the driver. This breaks
  313  *          backward comaptibility with old versions of ifenslave that open
  314  *           the slave before enalsving it !!!.
  315  *        * bond_release(): The driver also takes care of closing the slave
  316  *          and restoring its original mac address.
  317  *      - Removed the code that restores all base driver's flags.
  318  *        Flags are automatically restored once all undo stages are done
  319  *        properly.
  320  *      - Block possibility of enslaving before the master is up. This
  321  *        prevents putting the system in an unstable state.
  322  *
  323  * 2003/03/18 - Amir Noam <amir.noam at intel dot com>,
  324  *              Tsippy Mendelson <tsippy.mendelson at intel dot com> and
  325  *              Shmulik Hen <shmulik.hen at intel dot com>
  326  *    - Added support for IEEE 802.3ad Dynamic link aggregation mode.
  327  *
  328  * 2003/05/01 - Amir Noam <amir.noam at intel dot com>
  329  *    - Added ABI version control to restore compatibility between
  330  *      new/old ifenslave and new/old bonding.
  331  *
  332  * 2003/05/01 - Shmulik Hen <shmulik.hen at intel dot com>
  333  *    - Fixed bug in bond_release_all(): save old value of current_slave
  334  *      before setting it to NULL.
  335  *    - Changed driver versioning scheme to include version number instead
  336  *      of release date (that is already in another field). There are 3
  337  *      fields X.Y.Z where:
  338  *            X - Major version - big behavior changes
  339  *            Y - Minor version - addition of features
  340  *            Z - Extra version - minor changes and bug fixes
  341  *      The current version is 1.0.0 as a base line.
  342  *
  343  * 2003/05/01 - Tsippy Mendelson <tsippy.mendelson at intel dot com> and
  344  *              Amir Noam <amir.noam at intel dot com>
  345  *      - Added support for lacp_rate module param.
  346  *      - Code beautification and style changes (mainly in comments).
  347  *        new version - 1.0.1
  348  *
  349  * 2003/05/01 - Shmulik Hen <shmulik.hen at intel dot com>
  350  *      - Based on discussion on mailing list, changed locking scheme
  351  *        to use lock/unlock or lock_bh/unlock_bh appropriately instead
  352  *        of lock_irqsave/unlock_irqrestore. The new scheme helps exposing
  353  *        hidden bugs and solves system hangs that occurred due to the fact
  354  *        that holding lock_irqsave doesn't prevent softirqs from running.
  355  *        This also increases total throughput since interrupts are not
  356  *        blocked on each transmitted packets or monitor timeout.
  357  *        new version - 2.0.0
  358  *
  359  * 2003/05/01 - Shmulik Hen <shmulik.hen at intel dot com>
  360  *      - Added support for Transmit load balancing mode.
  361  *      - Concentrate all assignments of current_slave to a single point
  362  *        so specific modes can take actions when the primary adapter is
  363  *        changed.
  364  *      - Take the updelay parameter into consideration during bond_enslave
  365  *        since some adapters loose their link during setting the device.
  366  *      - Renamed bond_3ad_link_status_changed() to
  367  *        bond_3ad_handle_link_change() for compatibility with TLB.
  368  *        new version - 2.1.0
  369  *
  370  * 2003/05/01 - Tsippy Mendelson <tsippy.mendelson at intel dot com>
  371  *      - Added support for Adaptive load balancing mode which is
  372  *        equivalent to Transmit load balancing + Receive load balancing.
  373  *        new version - 2.2.0
  374  *
  375  * 2003/05/15 - Jay Vosburgh <fubar at us dot ibm dot com>
  376  *      - Applied fix to activebackup_arp_monitor posted to bonding-devel
  377  *        by Tony Cureington <tony.cureington * hp_com>.  Fixes ARP
  378  *        monitor endless failover bug.  Version to 2.2.10
  379  *
  380  * 2003/05/20 - Amir Noam <amir.noam at intel dot com>
  381  *      - Fixed bug in ABI version control - Don't commit to a specific
  382  *        ABI version if receiving unsupported ioctl commands.
  383  *
  384  * 2003/05/22 - Jay Vosburgh <fubar at us dot ibm dot com>
  385  *      - In conjunction with fix for ifenslave -c, in
  386  *        bond_change_active(), changing to the already active slave
  387  *        is no longer an error (it successfully does nothing).
  388  *
  389  * 2003/06/30 - Amir Noam <amir.noam at intel dot com>
  390  *      - Fixed bond_change_active() for ALB/TLB modes.
  391  */
  392 
  393 #include <linux/config.h>
  394 #include <linux/kernel.h>
  395 #include <linux/module.h>
  396 #include <linux/sched.h>
  397 #include <linux/types.h>
  398 #include <linux/fcntl.h>
  399 #include <linux/interrupt.h>
  400 #include <linux/ptrace.h>
  401 #include <linux/ioport.h>
  402 #include <linux/in.h>
  403 #include <linux/ip.h>
  404 #include <linux/slab.h>
  405 #include <linux/string.h>
  406 #include <linux/init.h>
  407 #include <linux/timer.h>
  408 #include <linux/socket.h>
  409 #include <linux/ctype.h>
  410 #include <linux/inet.h>
  411 #include <asm/system.h>
  412 #include <asm/bitops.h>
  413 #include <asm/io.h>
  414 #include <asm/dma.h>
  415 #include <asm/uaccess.h>
  416 #include <linux/errno.h>
  417 
  418 #include <linux/netdevice.h>
  419 #include <linux/inetdevice.h>
  420 #include <linux/etherdevice.h>
  421 #include <linux/skbuff.h>
  422 #include <net/sock.h>
  423 #include <linux/rtnetlink.h>
  424 
  425 #include <linux/if_bonding.h>
  426 #include <linux/smp.h>
  427 #include <linux/if_ether.h>
  428 #include <net/arp.h>
  429 #include <linux/mii.h>
  430 #include <linux/ethtool.h>
  431 #include "bonding.h"
  432 #include "bond_3ad.h"
  433 #include "bond_alb.h"
  434 
  435 #define DRV_VERSION             "2.2.14"
  436 #define DRV_RELDATE             "June 30, 2003"
  437 #define DRV_NAME                "bonding"
  438 #define DRV_DESCRIPTION         "Ethernet Channel Bonding Driver"
  439 
  440 static const char *version =
  441 DRV_NAME ".c:v" DRV_VERSION " (" DRV_RELDATE ")\n";
  442 
  443 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
  444 #ifndef BOND_LINK_MON_INTERV
  445 #define BOND_LINK_MON_INTERV    0
  446 #endif
  447 
  448 #ifndef BOND_LINK_ARP_INTERV
  449 #define BOND_LINK_ARP_INTERV    0
  450 #endif
  451 
  452 #ifndef MAX_ARP_IP_TARGETS
  453 #define MAX_ARP_IP_TARGETS 16
  454 #endif
  455 
  456 struct bond_parm_tbl {
  457         char *modename;
  458         int mode;
  459 };
  460 
  461 static int arp_interval = BOND_LINK_ARP_INTERV;
  462 static char *arp_ip_target[MAX_ARP_IP_TARGETS] = { NULL, };
  463 static u32 arp_target[MAX_ARP_IP_TARGETS] = { 0, } ;
  464 static int arp_ip_count = 0;
  465 static u32 my_ip = 0;
  466 char *arp_target_hw_addr = NULL;
  467 
  468 static char *primary= NULL;
  469 
  470 static int app_abi_ver = 0;
  471 static int orig_app_abi_ver = -1; /* This is used to save the first ABI version
  472                                    * we receive from the application. Once set,
  473                                    * it won't be changed, and the module will
  474                                    * refuse to enslave/release interfaces if the
  475                                    * command comes from an application using
  476                                    * another ABI version.
  477                                    */
  478 
  479 static int max_bonds    = BOND_DEFAULT_MAX_BONDS;
  480 static int miimon       = BOND_LINK_MON_INTERV;
  481 static int use_carrier  = 1;
  482 static int bond_mode    = BOND_MODE_ROUNDROBIN;
  483 static int updelay      = 0;
  484 static int downdelay    = 0;
  485 
  486 static char *mode       = NULL;
  487 
  488 static struct bond_parm_tbl bond_mode_tbl[] = {
  489 {       "balance-rr",           BOND_MODE_ROUNDROBIN},
  490 {       "active-backup",        BOND_MODE_ACTIVEBACKUP},
  491 {       "balance-xor",          BOND_MODE_XOR},
  492 {       "broadcast",            BOND_MODE_BROADCAST},
  493 {       "802.3ad",              BOND_MODE_8023AD},
  494 {       "balance-tlb",          BOND_MODE_TLB},
  495 {       "balance-alb",          BOND_MODE_ALB},
  496 {       NULL,                   -1},
  497 };
  498 
  499 static int multicast_mode       = BOND_MULTICAST_ALL;
  500 static char *multicast          = NULL;
  501 
  502 static struct bond_parm_tbl bond_mc_tbl[] = {
  503 {       "disabled",             BOND_MULTICAST_DISABLED},
  504 {       "active",               BOND_MULTICAST_ACTIVE},
  505 {       "all",                  BOND_MULTICAST_ALL},
  506 {       NULL,                   -1},
  507 };
  508 
  509 static int lacp_fast            = 0;
  510 static char *lacp_rate          = NULL;
  511 
  512 static struct bond_parm_tbl bond_lacp_tbl[] = {
  513 {       "slow",         AD_LACP_SLOW},
  514 {       "fast",         AD_LACP_FAST},
  515 {       NULL,           -1},
  516 };
  517 
  518 static int first_pass   = 1;
  519 static struct bonding *these_bonds =  NULL;
  520 static struct net_device *dev_bonds = NULL;
  521 
  522 MODULE_PARM(max_bonds, "i");
  523 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
  524 MODULE_PARM(miimon, "i");
  525 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
  526 MODULE_PARM(use_carrier, "i");
  527 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; 0 for off, 1 for on (default)");
  528 MODULE_PARM(mode, "s");
  529 MODULE_PARM_DESC(mode, "Mode of operation : 0 for round robin, 1 for active-backup, 2 for xor");
  530 MODULE_PARM(arp_interval, "i");
  531 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
  532 MODULE_PARM(arp_ip_target, "1-" __MODULE_STRING(MAX_ARP_IP_TARGETS) "s");
  533 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
  534 MODULE_PARM(updelay, "i");
  535 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
  536 MODULE_PARM(downdelay, "i");
  537 MODULE_PARM_DESC(downdelay, "Delay before considering link down, in milliseconds");
  538 MODULE_PARM(primary, "s");
  539 MODULE_PARM_DESC(primary, "Primary network device to use");
  540 MODULE_PARM(multicast, "s");
  541 MODULE_PARM_DESC(multicast, "Mode for multicast support : 0 for none, 1 for active slave, 2 for all slaves (default)");
  542 MODULE_PARM(lacp_rate, "s");
  543 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner (slow/fast)");
  544 
  545 static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *dev);
  546 static int bond_xmit_xor(struct sk_buff *skb, struct net_device *dev);
  547 static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *dev);
  548 static struct net_device_stats *bond_get_stats(struct net_device *dev);
  549 static void bond_mii_monitor(struct net_device *dev);
  550 static void loadbalance_arp_monitor(struct net_device *dev);
  551 static void activebackup_arp_monitor(struct net_device *dev);
  552 static int bond_event(struct notifier_block *this, unsigned long event, void *ptr);
  553 static void bond_mc_list_destroy(struct bonding *bond);
  554 static void bond_mc_add(bonding_t *bond, void *addr, int alen);
  555 static void bond_mc_delete(bonding_t *bond, void *addr, int alen);
  556 static int bond_mc_list_copy (struct dev_mc_list *src, struct bonding *dst, int gpf_flag);
  557 static inline int dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2);
  558 static void bond_set_promiscuity(bonding_t *bond, int inc);
  559 static void bond_set_allmulti(bonding_t *bond, int inc);
  560 static struct dev_mc_list* bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list);
  561 static void bond_mc_update(bonding_t *bond, slave_t *new, slave_t *old);
  562 static int bond_enslave(struct net_device *master, struct net_device *slave);
  563 static int bond_release(struct net_device *master, struct net_device *slave);
  564 static int bond_release_all(struct net_device *master);
  565 static int bond_sethwaddr(struct net_device *master, struct net_device *slave);
  566 
  567 /*
  568  * bond_get_info is the interface into the /proc filesystem.  This is
  569  * a different interface than the BOND_INFO_QUERY ioctl.  That is done
  570  * through the generic networking ioctl interface, and bond_info_query
  571  * is the internal function which provides that information.
  572  */
  573 static int bond_get_info(char *buf, char **start, off_t offset, int length);
  574 
  575 /* Caller must hold bond->ptrlock for write */
  576 static inline struct slave*
  577 bond_assign_current_slave(struct bonding *bond,struct slave *newslave)
  578 {
  579         if ((bond_mode == BOND_MODE_TLB) ||
  580             (bond_mode == BOND_MODE_ALB)) {
  581                 bond_alb_assign_current_slave(bond, newslave);
  582         } else {
  583                 bond->current_slave = newslave;
  584         }
  585 
  586         return bond->current_slave;
  587 }
  588 
  589 /* #define BONDING_DEBUG 1 */
  590 
  591 /* several macros */
  592 
  593 static void arp_send_all(slave_t *slave)
  594 {       
  595         int i; 
  596 
  597         for (i = 0; (i<MAX_ARP_IP_TARGETS) && arp_target[i]; i++) { 
  598                 arp_send(ARPOP_REQUEST, ETH_P_ARP, arp_target[i], slave->dev, 
  599                          my_ip, arp_target_hw_addr, slave->dev->dev_addr,
  600                          arp_target_hw_addr); 
  601         } 
  602 }
  603  
  604 
  605 static const char *
  606 bond_mode_name(void)
  607 {
  608         switch (bond_mode) {
  609         case BOND_MODE_ROUNDROBIN :
  610                 return "load balancing (round-robin)";
  611         case BOND_MODE_ACTIVEBACKUP :
  612                 return "fault-tolerance (active-backup)";
  613         case BOND_MODE_XOR :
  614                 return "load balancing (xor)";
  615         case BOND_MODE_BROADCAST :
  616                 return "fault-tolerance (broadcast)";
  617         case BOND_MODE_8023AD:
  618                 return "IEEE 802.3ad Dynamic link aggregation";
  619         case BOND_MODE_TLB:
  620                 return "transmit load balancing";
  621         case BOND_MODE_ALB:
  622                 return "adaptive load balancing";
  623         default:
  624                 return "unknown";
  625         }
  626 }
  627 
  628 static const char *
  629 multicast_mode_name(void)
  630 {
  631         switch(multicast_mode) {
  632         case BOND_MULTICAST_DISABLED :
  633                 return "disabled";
  634         case BOND_MULTICAST_ACTIVE :
  635                 return "active slave only";
  636         case BOND_MULTICAST_ALL :
  637                 return "all slaves";
  638         default :
  639                 return "unknown";
  640         }
  641 }
  642 
  643 void bond_set_slave_inactive_flags(slave_t *slave)
  644 {
  645         slave->state = BOND_STATE_BACKUP;
  646         slave->dev->flags |= IFF_NOARP;
  647 }
  648 
  649 void bond_set_slave_active_flags(slave_t *slave)
  650 {
  651         slave->state = BOND_STATE_ACTIVE;
  652         slave->dev->flags &= ~IFF_NOARP;
  653 }
  654 
  655 /*
  656  * This function counts and verifies the the number of attached
  657  * slaves, checking the count against the expected value (given that incr
  658  * is either 1 or -1, for add or removal of a slave).  Only
  659  * bond_xmit_xor() uses the slave_cnt value, but this is still a good
  660  * consistency check.
  661  */
  662 static inline void
  663 update_slave_cnt(bonding_t *bond, int incr)
  664 {
  665         slave_t *slave = NULL;
  666         int expect = bond->slave_cnt + incr;
  667 
  668         bond->slave_cnt = 0;
  669         for (slave = bond->prev; slave != (slave_t*)bond;
  670              slave = slave->prev) {
  671                 bond->slave_cnt++;
  672         }
  673 
  674         if (expect != bond->slave_cnt)
  675                 BUG();
  676 }
  677 
  678 /*
  679  * Set MAC.  Differs from eth_mac_addr in that we allow changes while
  680  * netif_running().
  681  */
  682 static int
  683 bond_set_mac_address(struct net_device *dev, void *p)
  684 {
  685         struct sockaddr *addr = p;
  686 
  687         memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  688         return 0;
  689 }
  690 
  691 /* 
  692  * This function detaches the slave <slave> from the list <bond>.
  693  * WARNING: no check is made to verify if the slave effectively
  694  * belongs to <bond>. It returns <slave> in case it's needed.
  695  * Nothing is freed on return, structures are just unchained.
  696  * If the bond->current_slave pointer was pointing to <slave>,
  697  * it's replaced with bond->next, or NULL if not applicable.
  698  *
  699  * bond->lock held for writing by caller.
  700  */
  701 static slave_t *
  702 bond_detach_slave(bonding_t *bond, slave_t *slave)
  703 {
  704         if ((bond == NULL) || (slave == NULL) ||
  705            ((void *)bond == (void *)slave)) {
  706                 printk(KERN_ERR
  707                         "bond_detach_slave(): trying to detach "
  708                         "slave %p from bond %p\n", bond, slave);
  709                 return slave;
  710         }
  711 
  712         if (bond->next == slave) {  /* is the slave at the head ? */
  713                 if (bond->prev == slave) {  /* is the slave alone ? */
  714                         bond->prev = bond->next = (slave_t *)bond;
  715                 } else { /* not alone */
  716                         bond->next        = slave->next;
  717                         slave->next->prev = (slave_t *)bond;
  718                         bond->prev->next  = slave->next;
  719                 }
  720         } else {
  721                 slave->prev->next = slave->next;
  722                 if (bond->prev == slave) {  /* is this slave the last one ? */
  723                         bond->prev = slave->prev;
  724                 } else {
  725                         slave->next->prev = slave->prev;
  726                 }
  727         }
  728 
  729         update_slave_cnt(bond, -1);
  730 
  731         /* no need to hold ptrlock since bond lock is
  732          * already held for writing
  733          */
  734         if (slave == bond->current_slave) {
  735                 if ( bond->next != (slave_t *)bond) {  /* found one slave */
  736                         bond_assign_current_slave(bond, bond->next);
  737                 } else {
  738                         bond_assign_current_slave(bond, NULL);
  739                 }
  740         }
  741 
  742         return slave;
  743 }
  744 
  745 /*
  746  * This function attaches the slave <slave> to the list <bond>.
  747  *
  748  * bond->lock held for writing by caller.
  749  */
  750 static void
  751 bond_attach_slave(struct bonding *bond, struct slave *new_slave)
  752 {
  753         /* 
  754          * queue to the end of the slaves list, make the first element its
  755          * successor, the last one its predecessor, and make it the bond's
  756          * predecessor. 
  757          *
  758          * Just to clarify, so future bonding driver hackers don't go through
  759          * the same confusion stage I did trying to figure this out, the
  760          * slaves are stored in a double linked circular list, sortof.
  761          * In the ->next direction, the last slave points to the first slave,
  762          * bypassing bond; only the slaves are in the ->next direction.
  763          * In the ->prev direction, however, the first slave points to bond
  764          * and bond points to the last slave.
  765          *
  766          * It looks like a circle with a little bubble hanging off one side
  767          * in the ->prev direction only.
  768          *
  769          * When going through the list once, its best to start at bond->prev
  770          * and go in the ->prev direction, testing for bond.  Doing this
  771          * in the ->next direction doesn't work.  Trust me, I know this now.
  772          * :)  -mts 2002.03.14
  773          */
  774         new_slave->prev       = bond->prev;
  775         new_slave->prev->next = new_slave;
  776         bond->prev            = new_slave;
  777         new_slave->next       = bond->next;
  778 
  779         update_slave_cnt(bond, 1);
  780 }
  781 
  782 
  783 /*
  784  * Less bad way to call ioctl from within the kernel; this needs to be
  785  * done some other way to get the call out of interrupt context.
  786  * Needs "ioctl" variable to be supplied by calling context.
  787  */
  788 #define IOCTL(dev, arg, cmd) ({         \
  789         int ret;                        \
  790         mm_segment_t fs = get_fs();     \
  791         set_fs(get_ds());               \
  792         ret = ioctl(dev, arg, cmd);     \
  793         set_fs(fs);                     \
  794         ret; })
  795 
  796 /*
  797  * Get link speed and duplex from the slave's base driver
  798  * using ethtool. If for some reason the call fails or the
  799  * values are invalid, fake speed and duplex to 100/Full
  800  * and return error.
  801  */
  802 static int bond_update_speed_duplex(struct slave *slave)
  803 {
  804         struct net_device *dev = slave->dev;
  805         static int (* ioctl)(struct net_device *, struct ifreq *, int);
  806         struct ifreq ifr;
  807         struct ethtool_cmd etool;
  808 
  809         ioctl = dev->do_ioctl;
  810         if (ioctl) {
  811                 etool.cmd = ETHTOOL_GSET;
  812                 ifr.ifr_data = (char*)&etool;
  813                 if (IOCTL(dev, &ifr, SIOCETHTOOL) == 0) {
  814                         slave->speed = etool.speed;
  815                         slave->duplex = etool.duplex;
  816                 } else {
  817                         goto err_out;
  818                 }
  819         } else {
  820                 goto err_out;
  821         }
  822 
  823         switch (slave->speed) {
  824                 case SPEED_10:
  825                 case SPEED_100:
  826                 case SPEED_1000:
  827                         break;
  828                 default:
  829                         goto err_out;
  830         }
  831 
  832         switch (slave->duplex) {
  833                 case DUPLEX_FULL:
  834                 case DUPLEX_HALF:
  835                         break;
  836                 default:
  837                         goto err_out;
  838         }
  839 
  840         return 0;
  841 
  842 err_out:
  843         /* Fake speed and duplex */
  844         slave->speed = SPEED_100;
  845         slave->duplex = DUPLEX_FULL;
  846         return -1;
  847 }
  848 
  849 /* 
  850  * if <dev> supports MII link status reporting, check its link status.
  851  *
  852  * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
  853  * depening upon the setting of the use_carrier parameter.
  854  *
  855  * Return either BMSR_LSTATUS, meaning that the link is up (or we
  856  * can't tell and just pretend it is), or 0, meaning that the link is
  857  * down.
  858  *
  859  * If reporting is non-zero, instead of faking link up, return -1 if
  860  * both ETHTOOL and MII ioctls fail (meaning the device does not
  861  * support them).  If use_carrier is set, return whatever it says.
  862  * It'd be nice if there was a good way to tell if a driver supports
  863  * netif_carrier, but there really isn't.
  864  */
  865 static int
  866 bond_check_dev_link(struct net_device *dev, int reporting)
  867 {
  868         static int (* ioctl)(struct net_device *, struct ifreq *, int);
  869         struct ifreq ifr;
  870         struct mii_ioctl_data *mii;
  871         struct ethtool_value etool;
  872 
  873         if (use_carrier) {
  874                 return netif_carrier_ok(dev) ? BMSR_LSTATUS : 0;
  875         }
  876 
  877         ioctl = dev->do_ioctl;
  878         if (ioctl) {
  879                 /* TODO: set pointer to correct ioctl on a per team member */
  880                 /*       bases to make this more efficient. that is, once  */
  881                 /*       we determine the correct ioctl, we will always    */
  882                 /*       call it and not the others for that team          */
  883                 /*       member.                                           */
  884 
  885                 /*
  886                  * We cannot assume that SIOCGMIIPHY will also read a
  887                  * register; not all network drivers (e.g., e100)
  888                  * support that.
  889                  */
  890 
  891                 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
  892                 mii = (struct mii_ioctl_data *)&ifr.ifr_data;
  893                 if (IOCTL(dev, &ifr, SIOCGMIIPHY) == 0) {
  894                         mii->reg_num = MII_BMSR;
  895                         if (IOCTL(dev, &ifr, SIOCGMIIREG) == 0) {
  896                                 return mii->val_out & BMSR_LSTATUS;
  897                         }
  898                 }
  899 
  900                 /* try SIOCETHTOOL ioctl, some drivers cache ETHTOOL_GLINK */
  901                 /* for a period of time so we attempt to get link status   */
  902                 /* from it last if the above MII ioctls fail...            */
  903                 etool.cmd = ETHTOOL_GLINK;
  904                 ifr.ifr_data = (char*)&etool;
  905                 if (IOCTL(dev, &ifr, SIOCETHTOOL) == 0) {
  906                         if (etool.data == 1) {
  907                                 return BMSR_LSTATUS;
  908                         } else { 
  909 #ifdef BONDING_DEBUG
  910                                 printk(KERN_INFO 
  911                                         ":: SIOCETHTOOL shows link down \n");
  912 #endif
  913                                 return 0;
  914                         } 
  915                 }
  916 
  917         }
  918  
  919         /*
  920          * If reporting, report that either there's no dev->do_ioctl,
  921          * or both SIOCGMIIREG and SIOCETHTOOL failed (meaning that we
  922          * cannot report link status).  If not reporting, pretend
  923          * we're ok.
  924          */
  925         return reporting ? -1 : BMSR_LSTATUS;
  926 }
  927 
  928 static u16 bond_check_mii_link(bonding_t *bond)
  929 {
  930         int has_active_interface = 0;
  931 
  932         read_lock_bh(&bond->lock);
  933         read_lock(&bond->ptrlock);
  934         has_active_interface = (bond->current_slave != NULL);
  935         read_unlock(&bond->ptrlock);
  936         read_unlock_bh(&bond->lock);
  937 
  938         return (has_active_interface ? BMSR_LSTATUS : 0);
  939 }
  940 
  941 /* register to receive lacpdus on a bond */
  942 static void bond_register_lacpdu(struct bonding *bond)
  943 {
  944         struct packet_type* pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
  945 
  946         /* initialize packet type */
  947         pk_type->type = PKT_TYPE_LACPDU;
  948         pk_type->dev = bond->device;
  949         pk_type->func = bond_3ad_lacpdu_recv;
  950         pk_type->data = (void*)1;  /* understand shared skbs */
  951 
  952         dev_add_pack(pk_type);
  953 }
  954 
  955 /* unregister to receive lacpdus on a bond */
  956 static void bond_unregister_lacpdu(struct bonding *bond)
  957 {
  958         dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
  959 }
  960 
  961 static int bond_open(struct net_device *dev)
  962 {
  963         struct bonding *bond = (struct bonding *)(dev->priv);
  964         struct timer_list *timer = &((struct bonding *)(dev->priv))->mii_timer;
  965         struct timer_list *arp_timer = &((struct bonding *)(dev->priv))->arp_timer;
  966 
  967         if ((bond_mode == BOND_MODE_TLB) ||
  968             (bond_mode == BOND_MODE_ALB)) {
  969                 struct timer_list *alb_timer = &(BOND_ALB_INFO(bond).alb_timer);
  970 
  971                 /* bond_alb_initialize must be called before the timer
  972                  * is started.
  973                  */
  974                 if (bond_alb_initialize(bond, (bond_mode == BOND_MODE_ALB))) {
  975                         /* something went wrong - fail the open operation */
  976                         return -1;
  977                 }
  978 
  979                 init_timer(alb_timer);
  980                 alb_timer->expires  = jiffies + 1;
  981                 alb_timer->data     = (unsigned long)bond;
  982                 alb_timer->function = (void *)&bond_alb_monitor;
  983                 add_timer(alb_timer);
  984         }
  985 
  986         MOD_INC_USE_COUNT;
  987 
  988         if (miimon > 0) {  /* link check interval, in milliseconds. */
  989                 init_timer(timer);
  990                 timer->expires  = jiffies + (miimon * HZ / 1000);
  991                 timer->data     = (unsigned long)dev;
  992                 timer->function = (void *)&bond_mii_monitor;
  993                 add_timer(timer);
  994         }
  995 
  996         if (arp_interval> 0) {  /* arp interval, in milliseconds. */
  997                 init_timer(arp_timer);
  998                 arp_timer->expires  = jiffies + (arp_interval * HZ / 1000);
  999                 arp_timer->data     = (unsigned long)dev;
 1000                 if (bond_mode == BOND_MODE_ACTIVEBACKUP) {
 1001                         arp_timer->function = (void *)&activebackup_arp_monitor;
 1002                 } else {
 1003                         arp_timer->function = (void *)&loadbalance_arp_monitor;
 1004                 }
 1005                 add_timer(arp_timer);
 1006         }
 1007 
 1008         if (bond_mode == BOND_MODE_8023AD) {
 1009                 struct timer_list *ad_timer = &(BOND_AD_INFO(bond).ad_timer);
 1010                 init_timer(ad_timer);
 1011                 ad_timer->expires  = jiffies + (AD_TIMER_INTERVAL * HZ / 1000);
 1012                 ad_timer->data     = (unsigned long)bond;
 1013                 ad_timer->function = (void *)&bond_3ad_state_machine_handler;
 1014                 add_timer(ad_timer);
 1015 
 1016                 /* register to receive LACPDUs */
 1017                 bond_register_lacpdu(bond);
 1018         }
 1019 
 1020         return 0;
 1021 }
 1022 
 1023 static int bond_close(struct net_device *master)
 1024 {
 1025         bonding_t *bond = (struct bonding *) master->priv;
 1026 
 1027         write_lock_bh(&bond->lock);
 1028 
 1029         if (miimon > 0) {  /* link check interval, in milliseconds. */
 1030                 del_timer(&bond->mii_timer);
 1031         }
 1032         if (arp_interval> 0) {  /* arp interval, in milliseconds. */
 1033                 del_timer(&bond->arp_timer);
 1034                 if (arp_target_hw_addr != NULL) {
 1035                         kfree(arp_target_hw_addr); 
 1036                         arp_target_hw_addr = NULL;
 1037                 }
 1038         }
 1039 
 1040         if (bond_mode == BOND_MODE_8023AD) {
 1041                 del_timer_sync(&(BOND_AD_INFO(bond).ad_timer));
 1042 
 1043                 /* Unregister the receive of LACPDUs */
 1044                 bond_unregister_lacpdu(bond);
 1045         }
 1046 
 1047         bond_mc_list_destroy (bond);
 1048 
 1049         write_unlock_bh(&bond->lock);
 1050 
 1051         /* Release the bonded slaves */
 1052         bond_release_all(master);
 1053 
 1054         if ((bond_mode == BOND_MODE_TLB) ||
 1055             (bond_mode == BOND_MODE_ALB)) {
 1056                 del_timer_sync(&(BOND_ALB_INFO(bond).alb_timer));
 1057 
 1058                 bond_alb_deinitialize(bond);
 1059         }
 1060 
 1061         MOD_DEC_USE_COUNT;
 1062         return 0;
 1063 }
 1064 
 1065 /* 
 1066  * flush all members of flush->mc_list from device dev->mc_list
 1067  */
 1068 static void bond_mc_list_flush(struct net_device *dev, struct net_device *flush)
 1069 { 
 1070         struct dev_mc_list *dmi; 
 1071  
 1072         for (dmi = flush->mc_list; dmi != NULL; dmi = dmi->next) 
 1073                 dev_mc_delete(dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
 1074 
 1075         if (bond_mode == BOND_MODE_8023AD) {
 1076                 /* del lacpdu mc addr from mc list */
 1077                 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
 1078 
 1079                 dev_mc_delete(dev, lacpdu_multicast, ETH_ALEN, 0);
 1080         }
 1081 }
 1082 
 1083 /*
 1084  * Totally destroys the mc_list in bond
 1085  */
 1086 static void bond_mc_list_destroy(struct bonding *bond)
 1087 {
 1088         struct dev_mc_list *dmi;
 1089 
 1090         dmi = bond->mc_list; 
 1091         while (dmi) { 
 1092                 bond->mc_list = dmi->next; 
 1093                 kfree(dmi); 
 1094                 dmi = bond->mc_list; 
 1095         }
 1096 }
 1097 
 1098 /*
 1099  * Add a Multicast address to every slave in the bonding group
 1100  */
 1101 static void bond_mc_add(bonding_t *bond, void *addr, int alen)
 1102 { 
 1103         slave_t *slave;
 1104         switch (multicast_mode) {
 1105         case BOND_MULTICAST_ACTIVE :
 1106                 /* write lock already acquired */
 1107                 if (bond->current_slave != NULL)
 1108                         dev_mc_add(bond->current_slave->dev, addr, alen, 0);
 1109                 break;
 1110         case BOND_MULTICAST_ALL :
 1111                 for (slave = bond->prev; slave != (slave_t*)bond; slave = slave->prev)
 1112                         dev_mc_add(slave->dev, addr, alen, 0);
 1113                 break;
 1114         case BOND_MULTICAST_DISABLED :
 1115                 break;
 1116         }
 1117 } 
 1118 
 1119 /*
 1120  * Remove a multicast address from every slave in the bonding group
 1121  */
 1122 static void bond_mc_delete(bonding_t *bond, void *addr, int alen)
 1123 { 
 1124         slave_t *slave; 
 1125         switch (multicast_mode) {
 1126         case BOND_MULTICAST_ACTIVE :
 1127                 /* write lock already acquired */
 1128                 if (bond->current_slave != NULL)
 1129                         dev_mc_delete(bond->current_slave->dev, addr, alen, 0);
 1130                 break;
 1131         case BOND_MULTICAST_ALL :
 1132                 for (slave = bond->prev; slave != (slave_t*)bond; slave = slave->prev)
 1133                         dev_mc_delete(slave->dev, addr, alen, 0);
 1134                 break;
 1135         case BOND_MULTICAST_DISABLED :
 1136                 break;
 1137         }
 1138 } 
 1139 
 1140 /*
 1141  * Copy all the Multicast addresses from src to the bonding device dst
 1142  */
 1143 static int bond_mc_list_copy (struct dev_mc_list *src, struct bonding *dst,
 1144  int gpf_flag)
 1145 {
 1146         struct dev_mc_list *dmi, *new_dmi;
 1147 
 1148         for (dmi = src; dmi != NULL; dmi = dmi->next) { 
 1149                 new_dmi = kmalloc(sizeof(struct dev_mc_list), gpf_flag);
 1150 
 1151                 if (new_dmi == NULL) {
 1152                         return -ENOMEM; 
 1153                 }
 1154 
 1155                 new_dmi->next = dst->mc_list; 
 1156                 dst->mc_list = new_dmi;
 1157 
 1158                 new_dmi->dmi_addrlen = dmi->dmi_addrlen; 
 1159                 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen); 
 1160                 new_dmi->dmi_users = dmi->dmi_users;
 1161                 new_dmi->dmi_gusers = dmi->dmi_gusers; 
 1162         } 
 1163         return 0;
 1164 }
 1165 
 1166 /*
 1167  * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
 1168  */
 1169 static inline int dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
 1170 { 
 1171         return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
 1172          dmi1->dmi_addrlen == dmi2->dmi_addrlen;
 1173 } 
 1174 
 1175 /*
 1176  * Push the promiscuity flag down to all slaves
 1177  */
 1178 static void bond_set_promiscuity(bonding_t *bond, int inc)
 1179 { 
 1180         slave_t *slave; 
 1181         switch (multicast_mode) {
 1182         case BOND_MULTICAST_ACTIVE :
 1183                 /* write lock already acquired */
 1184                 if (bond->current_slave != NULL)
 1185                         dev_set_promiscuity(bond->current_slave->dev, inc);
 1186                 break;
 1187         case BOND_MULTICAST_ALL :
 1188                 for (slave = bond->prev; slave != (slave_t*)bond; slave = slave->prev)
 1189                         dev_set_promiscuity(slave->dev, inc);
 1190                 break;
 1191         case BOND_MULTICAST_DISABLED :
 1192                 break;
 1193         }
 1194 } 
 1195 
 1196 /*
 1197  * Push the allmulti flag down to all slaves
 1198  */
 1199 static void bond_set_allmulti(bonding_t *bond, int inc)
 1200 { 
 1201         slave_t *slave; 
 1202         switch (multicast_mode) {
 1203         case BOND_MULTICAST_ACTIVE : 
 1204                 /* write lock already acquired */
 1205                 if (bond->current_slave != NULL)
 1206                         dev_set_allmulti(bond->current_slave->dev, inc);
 1207                 break;
 1208         case BOND_MULTICAST_ALL :
 1209                 for (slave = bond->prev; slave != (slave_t*)bond; slave = slave->prev)
 1210                         dev_set_allmulti(slave->dev, inc);
 1211                 break;
 1212         case BOND_MULTICAST_DISABLED :
 1213                 break;
 1214         }
 1215 } 
 1216 
 1217 /* 
 1218  * returns dmi entry if found, NULL otherwise 
 1219  */
 1220 static struct dev_mc_list* bond_mc_list_find_dmi(struct dev_mc_list *dmi,
 1221  struct dev_mc_list *mc_list)
 1222 { 
 1223         struct dev_mc_list *idmi;
 1224 
 1225         for (idmi = mc_list; idmi != NULL; idmi = idmi->next) {
 1226                 if (dmi_same(dmi, idmi)) {
 1227                         return idmi; 
 1228                 }
 1229         }
 1230         return NULL;
 1231 } 
 1232 
 1233 static void set_multicast_list(struct net_device *master)
 1234 {
 1235         bonding_t *bond = master->priv;
 1236         struct dev_mc_list *dmi;
 1237 
 1238         if (multicast_mode == BOND_MULTICAST_DISABLED)
 1239                 return;
 1240         /*
 1241          * Lock the private data for the master
 1242          */
 1243         write_lock_bh(&bond->lock);
 1244 
 1245         /* set promiscuity flag to slaves */
 1246         if ( (master->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC) )
 1247                 bond_set_promiscuity(bond, 1); 
 1248 
 1249         if ( !(master->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC) ) 
 1250                 bond_set_promiscuity(bond, -1); 
 1251 
 1252         /* set allmulti flag to slaves */ 
 1253         if ( (master->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI) ) 
 1254                 bond_set_allmulti(bond, 1); 
 1255 
 1256         if ( !(master->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI) )
 1257                 bond_set_allmulti(bond, -1); 
 1258 
 1259         bond->flags = master->flags; 
 1260 
 1261         /* looking for addresses to add to slaves' mc list */ 
 1262         for (dmi = master->mc_list; dmi != NULL; dmi = dmi->next) { 
 1263                 if (bond_mc_list_find_dmi(dmi, bond->mc_list) == NULL) 
 1264                  bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen); 
 1265         } 
 1266 
 1267         /* looking for addresses to delete from slaves' list */ 
 1268         for (dmi = bond->mc_list; dmi != NULL; dmi = dmi->next) { 
 1269                 if (bond_mc_list_find_dmi(dmi, master->mc_list) == NULL) 
 1270                  bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen); 
 1271         }
 1272 
 1273 
 1274         /* save master's multicast list */ 
 1275         bond_mc_list_destroy (bond);
 1276         bond_mc_list_copy (master->mc_list, bond, GFP_ATOMIC);
 1277 
 1278         write_unlock_bh(&bond->lock);
 1279 }
 1280 
 1281 /*
 1282  * Update the mc list and multicast-related flags for the new and 
 1283  * old active slaves (if any) according to the multicast mode
 1284  */
 1285 static void bond_mc_update(bonding_t *bond, slave_t *new, slave_t *old)
 1286 {
 1287         struct dev_mc_list *dmi;
 1288 
 1289         switch(multicast_mode) {
 1290         case BOND_MULTICAST_ACTIVE :            
 1291                 if (bond->device->flags & IFF_PROMISC) {
 1292                         if (old != NULL && new != old)
 1293                                 dev_set_promiscuity(old->dev, -1);
 1294                         dev_set_promiscuity(new->dev, 1);
 1295                 }
 1296                 if (bond->device->flags & IFF_ALLMULTI) {
 1297                         if (old != NULL && new != old)
 1298                                 dev_set_allmulti(old->dev, -1);
 1299                         dev_set_allmulti(new->dev, 1);
 1300                 }
 1301                 /* first remove all mc addresses from old slave if any,
 1302                    and _then_ add them to new active slave */
 1303                 if (old != NULL && new != old) {
 1304                         for (dmi = bond->device->mc_list; dmi != NULL; dmi = dmi->next)
 1305                                 dev_mc_delete(old->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
 1306                 }
 1307                 for (dmi = bond->device->mc_list; dmi != NULL; dmi = dmi->next)
 1308                         dev_mc_add(new->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
 1309                 break;
 1310         case BOND_MULTICAST_ALL :
 1311                 /* nothing to do: mc list is already up-to-date on all slaves */
 1312                 break;
 1313         case BOND_MULTICAST_DISABLED :
 1314                 break;
 1315         }
 1316 }
 1317 
 1318 /* enslave device <slave> to bond device <master> */
 1319 static int bond_enslave(struct net_device *master_dev, 
 1320                         struct net_device *slave_dev)
 1321 {
 1322         bonding_t *bond = NULL;
 1323         slave_t *new_slave = NULL;
 1324         unsigned long rflags = 0;
 1325         int err = 0;
 1326         struct dev_mc_list *dmi;
 1327         struct in_ifaddr **ifap;
 1328         struct in_ifaddr *ifa;
 1329         int link_reporting;
 1330         struct sockaddr addr;
 1331 
 1332         if (master_dev == NULL || slave_dev == NULL) {
 1333                 return -ENODEV;
 1334         }
 1335         bond = (struct bonding *) master_dev->priv;
 1336 
 1337         if (slave_dev->do_ioctl == NULL) {
 1338                 printk(KERN_DEBUG
 1339                         "Warning : no link monitoring support for %s\n",
 1340                         slave_dev->name);
 1341         }
 1342 
 1343 
 1344         /* bond must be initialized by bond_open() before enslaving */
 1345         if (!(master_dev->flags & IFF_UP)) {
 1346 #ifdef BONDING_DEBUG
 1347                 printk(KERN_CRIT "Error, master_dev is not up\n");
 1348 #endif
 1349                 return -EPERM;
 1350         }
 1351 
 1352         /* already enslaved */
 1353         if (master_dev->flags & IFF_SLAVE || slave_dev->flags & IFF_SLAVE) {
 1354 #ifdef BONDING_DEBUG
 1355                 printk(KERN_CRIT "Error, Device was already enslaved\n");
 1356 #endif
 1357                 return -EBUSY;
 1358         }
 1359 
 1360         if (app_abi_ver >= 1) {
 1361                 /* The application is using an ABI, which requires the
 1362                  * slave interface to be closed.
 1363                  */
 1364                 if ((slave_dev->flags & IFF_UP)) {
 1365 #ifdef BONDING_DEBUG
 1366                         printk(KERN_CRIT "Error, slave_dev is up\n");
 1367 #endif
 1368                         return -EPERM;
 1369                 }
 1370 
 1371                 if (slave_dev->set_mac_address == NULL) {
 1372                         printk(KERN_CRIT
 1373                                "The slave device you specified does not support"
 1374                                " setting the MAC address.\n");
 1375                         printk(KERN_CRIT
 1376                                "Your kernel likely does not support slave"
 1377                                " devices.\n");
 1378 
 1379                         return -EOPNOTSUPP;
 1380                 }
 1381         } else {
 1382                 /* The application is not using an ABI, which requires the
 1383                  * slave interface to be open.
 1384                  */
 1385                 if (!(slave_dev->flags & IFF_UP)) {
 1386 #ifdef BONDING_DEBUG
 1387                         printk(KERN_CRIT "Error, slave_dev is not running\n");
 1388 #endif
 1389                         return -EINVAL;
 1390                 }
 1391 
 1392                 if ((bond_mode == BOND_MODE_8023AD) ||
 1393                     (bond_mode == BOND_MODE_TLB) ||
 1394                     (bond_mode == BOND_MODE_ALB)) {
 1395                         printk(KERN_ERR
 1396                                "bonding: Error: to use %s mode, you must "
 1397                                "upgrade ifenslave.\n", bond_mode_name());
 1398                         return -EOPNOTSUPP;
 1399                 }
 1400         }
 1401 
 1402         if ((new_slave = kmalloc(sizeof(slave_t), GFP_KERNEL)) == NULL) {
 1403                 return -ENOMEM;
 1404         }
 1405         memset(new_slave, 0, sizeof(slave_t));
 1406 
 1407         /* save slave's original flags before calling
 1408          * netdev_set_master and dev_open
 1409          */
 1410         new_slave->original_flags = slave_dev->flags;
 1411 
 1412         if (app_abi_ver >= 1) {
 1413                 /* save slave's original ("permanent") mac address for
 1414                  * modes that needs it, and for restoring it upon release,
 1415                  * and then set it to the master's address
 1416                  */
 1417                 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
 1418 
 1419                 if (bond->slave_cnt > 0) {
 1420                         /* set slave to master's mac address
 1421                          * The application already set the master's
 1422                          * mac address to that of the first slave
 1423                          */
 1424                         memcpy(addr.sa_data, master_dev->dev_addr, ETH_ALEN);
 1425                         addr.sa_family = slave_dev->type;
 1426                         err = slave_dev->set_mac_address(slave_dev, &addr);
 1427                         if (err) {
 1428 #ifdef BONDING_DEBUG
 1429                                 printk(KERN_CRIT "Error %d calling set_mac_address\n", err);
 1430 #endif
 1431                                 goto err_free;
 1432                         }
 1433                 }
 1434 
 1435                 /* open the slave since the application closed it */
 1436                 err = dev_open(slave_dev);
 1437                 if (err) {
 1438 #ifdef BONDING_DEBUG
 1439                         printk(KERN_CRIT "Openning slave %s failed\n", slave_dev->name);
 1440 #endif
 1441                         goto err_restore_mac;
 1442                 }
 1443         }
 1444 
 1445         err = netdev_set_master(slave_dev, master_dev);
 1446         if (err) {
 1447 #ifdef BONDING_DEBUG
 1448                 printk(KERN_CRIT "Error %d calling netdev_set_master\n", err);
 1449 #endif
 1450                 if (app_abi_ver < 1) {
 1451                         goto err_free;
 1452                 } else {
 1453                         goto err_close;
 1454                 }
 1455         }
 1456 
 1457         new_slave->dev = slave_dev;
 1458 
 1459         if ((bond_mode == BOND_MODE_TLB) ||
 1460             (bond_mode == BOND_MODE_ALB)) {
 1461                 /* bond_alb_init_slave() must be called before all other stages since
 1462                  * it might fail and we do not want to have to undo everything
 1463                  */
 1464                 err = bond_alb_init_slave(bond, new_slave);
 1465                 if (err) {
 1466                         goto err_unset_master;
 1467                 }
 1468         }
 1469 
 1470         if (multicast_mode == BOND_MULTICAST_ALL) {
 1471                 /* set promiscuity level to new slave */ 
 1472                 if (master_dev->flags & IFF_PROMISC)
 1473                         dev_set_promiscuity(slave_dev, 1); 
 1474  
 1475                 /* set allmulti level to new slave */
 1476                 if (master_dev->flags & IFF_ALLMULTI) 
 1477                         dev_set_allmulti(slave_dev, 1); 
 1478                 
 1479                 /* upload master's mc_list to new slave */ 
 1480                 for (dmi = master_dev->mc_list; dmi != NULL; dmi = dmi->next) 
 1481                         dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
 1482         }
 1483 
 1484         if (bond_mode == BOND_MODE_8023AD) {
 1485                 /* add lacpdu mc addr to mc list */
 1486                 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
 1487 
 1488                 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
 1489         }
 1490 
 1491         write_lock_bh(&bond->lock);
 1492         
 1493         bond_attach_slave(bond, new_slave);
 1494         new_slave->delay = 0;
 1495         new_slave->link_failure_count = 0;
 1496 
 1497         if (miimon > 0 && !use_carrier) {
 1498                 link_reporting = bond_check_dev_link(slave_dev, 1);
 1499 
 1500                 if ((link_reporting == -1) && (arp_interval == 0)) {
 1501                         /*
 1502                          * miimon is set but a bonded network driver
 1503                          * does not support ETHTOOL/MII and
 1504                          * arp_interval is not set.  Note: if
 1505                          * use_carrier is enabled, we will never go
 1506                          * here (because netif_carrier is always
 1507                          * supported); thus, we don't need to change
 1508                          * the messages for netif_carrier.
 1509                          */ 
 1510                         printk(KERN_ERR
 1511                                 "bond_enslave(): MII and ETHTOOL support not "
 1512                                 "available for interface %s, and "
 1513                                 "arp_interval/arp_ip_target module parameters "
 1514                                 "not specified, thus bonding will not detect "
 1515                                 "link failures! see bonding.txt for details.\n",
 1516                                 slave_dev->name);
 1517                 } else if (link_reporting == -1) {
 1518                         /* unable  get link status using mii/ethtool */
 1519                         printk(KERN_WARNING 
 1520                                "bond_enslave: can't get link status from "
 1521                                "interface %s; the network driver associated "
 1522                                "with this interface does not support "
 1523                                "MII or ETHTOOL link status reporting, thus "
 1524                                "miimon has no effect on this interface.\n", 
 1525                                slave_dev->name);
 1526                 }
 1527         }
 1528 
 1529         /* check for initial state */
 1530         if ((miimon <= 0) ||
 1531             (bond_check_dev_link(slave_dev, 0) == BMSR_LSTATUS)) {
 1532                 if (updelay) {
 1533 #ifdef BONDING_DEBUG
 1534                         printk(KERN_CRIT "Initial state of slave_dev is "
 1535                                "BOND_LINK_BACK\n");
 1536 #endif
 1537                         new_slave->link  = BOND_LINK_BACK;
 1538                         new_slave->delay = updelay;
 1539                 }
 1540                 else {
 1541 #ifdef BONDING_DEBUG
 1542                         printk(KERN_DEBUG "Initial state of slave_dev is "
 1543                                 "BOND_LINK_UP\n");
 1544 #endif
 1545                         new_slave->link  = BOND_LINK_UP;
 1546                 }
 1547                 new_slave->jiffies = jiffies;
 1548         }
 1549         else {
 1550 #ifdef BONDING_DEBUG
 1551                 printk(KERN_CRIT "Initial state of slave_dev is "
 1552                         "BOND_LINK_DOWN\n");
 1553 #endif
 1554                 new_slave->link  = BOND_LINK_DOWN;
 1555         }
 1556 
 1557         if (bond_update_speed_duplex(new_slave) &&
 1558             (new_slave->link != BOND_LINK_DOWN)) {
 1559 
 1560                 printk(KERN_WARNING
 1561                        "bond_enslave(): failed to get speed/duplex from %s, "
 1562                        "speed forced to 100Mbps, duplex forced to Full.\n",
 1563                        new_slave->dev->name);
 1564                 if (bond_mode == BOND_MODE_8023AD) {
 1565                         printk(KERN_WARNING
 1566                                "Operation of 802.3ad mode requires ETHTOOL support "
 1567                                "in base driver for proper aggregator selection.\n");
 1568                 }
 1569         }
 1570 
 1571         /* if we're in active-backup mode, we need one and only one active
 1572          * interface. The backup interfaces will have their NOARP flag set
 1573          * because we need them to be completely deaf and not to respond to
 1574          * any ARP request on the network to avoid fooling a switch. Thus,
 1575          * since we guarantee that current_slave always point to the last
 1576          * usable interface, we just have to verify this interface's flag.
 1577          */
 1578         if (bond_mode == BOND_MODE_ACTIVEBACKUP) {
 1579                 if (((bond->current_slave == NULL)
 1580                         || (bond->current_slave->dev->flags & IFF_NOARP))
 1581                         && (new_slave->link != BOND_LINK_DOWN)) {
 1582 #ifdef BONDING_DEBUG
 1583                         printk(KERN_CRIT "This is the first active slave\n");
 1584 #endif
 1585                         /* first slave or no active slave yet, and this link
 1586                            is OK, so make this interface the active one */
 1587                         bond_assign_current_slave(bond, new_slave);
 1588                         bond_set_slave_active_flags(new_slave);
 1589                         bond_mc_update(bond, new_slave, NULL);
 1590                 }
 1591                 else {
 1592 #ifdef BONDING_DEBUG
 1593                         printk(KERN_CRIT "This is just a backup slave\n");
 1594 #endif
 1595                         bond_set_slave_inactive_flags(new_slave);
 1596                 }
 1597                 if (((struct in_device *)slave_dev->ip_ptr) != NULL) {
 1598                         read_lock_irqsave(&(((struct in_device *)slave_dev->ip_ptr)->lock), rflags);
 1599                         ifap= &(((struct in_device *)slave_dev->ip_ptr)->ifa_list);
 1600                         ifa = *ifap;
 1601                         if (ifa != NULL)
 1602                                 my_ip = ifa->ifa_address;
 1603                         read_unlock_irqrestore(&(((struct in_device *)slave_dev->ip_ptr)->lock), rflags);
 1604                 }
 1605 
 1606                 /* if there is a primary slave, remember it */
 1607                 if (primary != NULL) {
 1608                         if (strcmp(primary, new_slave->dev->name) == 0) {
 1609                                 bond->primary_slave = new_slave;
 1610                         }
 1611                 }
 1612         } else if (bond_mode == BOND_MODE_8023AD) {
 1613                 /* in 802.3ad mode, the internal mechanism
 1614                  * will activate the slaves in the selected
 1615                  * aggregator
 1616                  */
 1617                 bond_set_slave_inactive_flags(new_slave);
 1618                 /* if this is the first slave */
 1619                 if (new_slave == bond->next) {
 1620                         SLAVE_AD_INFO(new_slave).id = 1;
 1621                         /* Initialize AD with the number of times that the AD timer is called in 1 second
 1622                          * can be called only after the mac address of the bond is set
 1623                          */
 1624                         bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
 1625                                             lacp_fast);
 1626                 } else {
 1627                         SLAVE_AD_INFO(new_slave).id =
 1628                         SLAVE_AD_INFO(new_slave->prev).id + 1;
 1629                 }
 1630 
 1631                 bond_3ad_bind_slave(new_slave);
 1632         } else if ((bond_mode == BOND_MODE_TLB) ||
 1633                    (bond_mode == BOND_MODE_ALB)) {
 1634                 new_slave->state = BOND_STATE_ACTIVE;
 1635                 if ((bond->current_slave == NULL) && (new_slave->link != BOND_LINK_DOWN)) {
 1636                         /* first slave or no active slave yet, and this link
 1637                          * is OK, so make this interface the active one
 1638                          */
 1639                         bond_assign_current_slave(bond, new_slave);
 1640                 }
 1641 
 1642                 /* if there is a primary slave, remember it */
 1643                 if (primary != NULL) {
 1644                         if (strcmp(primary, new_slave->dev->name) == 0) {
 1645                                 bond->primary_slave = new_slave;
 1646                         }
 1647                 }
 1648         } else {
 1649 #ifdef BONDING_DEBUG
 1650                 printk(KERN_CRIT "This slave is always active in trunk mode\n");
 1651 #endif
 1652                 /* always active in trunk mode */
 1653                 new_slave->state = BOND_STATE_ACTIVE;
 1654                 if (bond->current_slave == NULL) 
 1655                         bond_assign_current_slave(bond, new_slave);
 1656         }
 1657 
 1658         write_unlock_bh(&bond->lock);
 1659 
 1660         if (app_abi_ver < 1) {
 1661                 /*
 1662                  * !!! This is to support old versions of ifenslave.
 1663                  * We can remove this in 2.5 because our ifenslave takes
 1664                  * care of this for us.
 1665                  * We check to see if the master has a mac address yet.
 1666                  * If not, we'll give it the mac address of our slave device.
 1667                  */
 1668                 int ndx = 0;
 1669 
 1670                 for (ndx = 0; ndx < slave_dev->addr_len; ndx++) {
 1671 #ifdef BONDING_DEBUG
 1672                         printk(KERN_DEBUG
 1673                                "Checking ndx=%d of master_dev->dev_addr\n", ndx);
 1674 #endif
 1675                         if (master_dev->dev_addr[ndx] != 0) {
 1676 #ifdef BONDING_DEBUG
 1677                                 printk(KERN_DEBUG
 1678                                        "Found non-zero byte at ndx=%d\n", ndx);
 1679 #endif
 1680                                 break;
 1681                         }
 1682                 }
 1683                 if (ndx == slave_dev->addr_len) {
 1684                         /*
 1685                          * We got all the way through the address and it was
 1686                          * all 0's.
 1687                          */
 1688 #ifdef BONDING_DEBUG
 1689                         printk(KERN_DEBUG "%s doesn't have a MAC address yet.  ",
 1690                                master_dev->name);
 1691                         printk(KERN_DEBUG "Going to give assign it from %s.\n",
 1692                                slave_dev->name);
 1693 #endif
 1694                         bond_sethwaddr(master_dev, slave_dev);
 1695                 }
 1696         }
 1697 
 1698         printk (KERN_INFO "%s: enslaving %s as a%s interface with a%s link.\n",
 1699                 master_dev->name, slave_dev->name,
 1700                 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
 1701                 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
 1702 
 1703         /* enslave is successful */
 1704         return 0;
 1705 
 1706 /* Undo stages on error */
 1707 err_unset_master:
 1708         netdev_set_master(slave_dev, NULL);
 1709 
 1710 err_close:
 1711         dev_close(slave_dev);
 1712 
 1713 err_restore_mac:
 1714         memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
 1715         addr.sa_family = slave_dev->type;
 1716         slave_dev->set_mac_address(slave_dev, &addr);
 1717 
 1718 err_free:
 1719         kfree(new_slave);
 1720         return err;
 1721 }
 1722 
 1723 /* 
 1724  * This function changes the active slave to slave <slave_dev>.
 1725  * It returns -EINVAL in the following cases.
 1726  *  - <slave_dev> is not found in the list.
 1727  *  - There is not active slave now.
 1728  *  - <slave_dev> is already active.
 1729  *  - The link state of <slave_dev> is not BOND_LINK_UP.
 1730  *  - <slave_dev> is not running.
 1731  * In these cases, this fuction does nothing.
 1732  * In the other cases, currnt_slave pointer is changed and 0 is returned.
 1733  */
 1734 static int bond_change_active(struct net_device *master_dev, struct net_device *slave_dev)
 1735 {
 1736         bonding_t *bond;
 1737         slave_t *slave;
 1738         slave_t *oldactive = NULL;
 1739         slave_t *newactive = NULL;
 1740         int ret = 0;
 1741 
 1742         if (master_dev == NULL || slave_dev == NULL) {
 1743                 return -ENODEV;
 1744         }
 1745 
 1746         bond = (struct bonding *) master_dev->priv;
 1747         write_lock_bh(&bond->lock);
 1748         slave = (slave_t *)bond;
 1749         oldactive = bond->current_slave;
 1750 
 1751         while ((slave = slave->prev) != (slave_t *)bond) {
 1752                 if(slave_dev == slave->dev) {
 1753                         newactive = slave;
 1754                         break;
 1755                 }
 1756         }
 1757 
 1758         /*
 1759          * Changing to the current active: do nothing; return success.
 1760          */
 1761         if (newactive && (newactive == oldactive)) {
 1762                 write_unlock_bh(&bond->lock);
 1763                 return 0;
 1764         }
 1765 
 1766         if ((newactive != NULL)&&
 1767             (oldactive != NULL)&&
 1768             (newactive->link == BOND_LINK_UP)&&
 1769             IS_UP(newactive->dev)) {
 1770                 if (bond_mode == BOND_MODE_ACTIVEBACKUP) {
 1771                         bond_set_slave_inactive_flags(oldactive);
 1772                         bond_set_slave_active_flags(newactive);
 1773                 }
 1774 
 1775                 bond_mc_update(bond, newactive, oldactive);
 1776                 bond_assign_current_slave(bond, newactive);
 1777                 printk("%s : activate %s(old : %s)\n",
 1778                         master_dev->name, newactive->dev->name, 
 1779                         oldactive->dev->name);
 1780         } else {
 1781                 ret = -EINVAL;
 1782         }
 1783         write_unlock_bh(&bond->lock);
 1784         return ret;
 1785 }
 1786 
 1787 /* Choose a new valid interface from the pool, set it active
 1788  * and make it the current slave. If no valid interface is
 1789  * found, the oldest slave in BACK state is choosen and
 1790  * activated. If none is found, it's considered as no
 1791  * interfaces left so the current slave is set to NULL.
 1792  * The result is a pointer to the current slave.
 1793  *
 1794  * Since this function sends messages tails through printk, the caller
 1795  * must have started something like `printk(KERN_INFO "xxxx ");'.
 1796  *
 1797  * Warning: Caller must hold ptrlock for writing.
 1798  */
 1799 slave_t *change_active_interface(bonding_t *bond)
 1800 {
 1801         slave_t *newslave, *oldslave;
 1802         slave_t *bestslave = NULL;
 1803         int mintime;
 1804 
 1805         newslave = oldslave = bond->current_slave;
 1806 
 1807         if (newslave == NULL) { /* there were no active slaves left */
 1808                 if (bond->next != (slave_t *)bond) {  /* found one slave */
 1809                         newslave = bond_assign_current_slave(bond, bond->next);
 1810                 } else {
 1811 
 1812                         printk (" but could not find any %s interface.\n",
 1813                                 (bond_mode == BOND_MODE_ACTIVEBACKUP) ? "backup":"other");
 1814                         bond_assign_current_slave(bond, NULL);
 1815                         return NULL; /* still no slave, return NULL */
 1816                 }
 1817         } else if (bond_mode == BOND_MODE_ACTIVEBACKUP) {
 1818                 /* make sure oldslave doesn't send arps - this could
 1819                  * cause a ping-pong effect between interfaces since they
 1820                  * would be able to tx arps - in active backup only one
 1821                  * slave should be able to tx arps, and that should be
 1822                  * the current_slave; the only exception is when all
 1823                  * slaves have gone down, then only one non-current slave can
 1824                  * send arps at a time; clearing oldslaves' mc list is handled
 1825                  * later in this function.
 1826                  */
 1827                 bond_set_slave_inactive_flags(oldslave);
 1828         }
 1829 
 1830         mintime = updelay;
 1831 
 1832         /* first try the primary link; if arping, a link must tx/rx traffic 
 1833          * before it can be considered the current_slave - also, we would skip 
 1834          * slaves between the current_slave and primary_slave that may be up 
 1835          * and able to arp
 1836          */
 1837         if ((bond->primary_slave != NULL) && (arp_interval == 0)) {
 1838                 if (IS_UP(bond->primary_slave->dev)) 
 1839                         newslave = bond->primary_slave;
 1840         }
 1841 
 1842         do {
 1843                 if (IS_UP(newslave->dev)) {
 1844                         if (newslave->link == BOND_LINK_UP) {
 1845                                 /* this one is immediately usable */
 1846                                 if (bond_mode == BOND_MODE_ACTIVEBACKUP) {
 1847                                         bond_set_slave_active_flags(newslave);
 1848                                         bond_mc_update(bond, newslave, oldslave);
 1849                                         printk (" and making interface %s the active one.\n",
 1850                                                 newslave->dev->name);
 1851                                 }
 1852                                 else {
 1853                                         printk (" and setting pointer to interface %s.\n",
 1854                                                 newslave->dev->name);
 1855                                 }
 1856 
 1857                                 bond_assign_current_slave(bond, newslave);
 1858                                 return newslave;
 1859                         }
 1860                         else if (newslave->link == BOND_LINK_BACK) {
 1861                                 /* link up, but waiting for stabilization */
 1862                                 if (newslave->delay < mintime) {
 1863                                         mintime = newslave->delay;
 1864                                         bestslave = newslave;
 1865                                 }
 1866                         }
 1867                 }
 1868         } while ((newslave = newslave->next) != oldslave);
 1869 
 1870         /* no usable backup found, we'll see if we at least got a link that was
 1871            coming back for a long time, and could possibly already be usable.
 1872         */
 1873 
 1874         if (bestslave != NULL) {
 1875                 /* early take-over. */
 1876                 printk (" and making interface %s the active one %d ms earlier.\n",
 1877                         bestslave->dev->name,
 1878                         (updelay - bestslave->delay)*miimon);
 1879 
 1880                 bestslave->delay = 0;
 1881                 bestslave->link = BOND_LINK_UP;
 1882                 bestslave->jiffies = jiffies;
 1883                 bond_set_slave_active_flags(bestslave);
 1884                 bond_mc_update(bond, bestslave, oldslave);
 1885                 bond_assign_current_slave(bond, bestslave);
 1886                 return bestslave;
 1887         }
 1888 
 1889         if ((bond_mode == BOND_MODE_ACTIVEBACKUP) &&
 1890             (multicast_mode == BOND_MULTICAST_ACTIVE) &&
 1891             (oldslave != NULL)) {
 1892                 /* flush bonds (master's) mc_list from oldslave since it wasn't
 1893                  * updated (and deleted) above
 1894                  */ 
 1895                 bond_mc_list_flush(oldslave->dev, bond->device); 
 1896                 if (bond->device->flags & IFF_PROMISC) {
 1897                         dev_set_promiscuity(oldslave->dev, -1);
 1898                 }
 1899                 if (bond->device->flags & IFF_ALLMULTI) {
 1900                         dev_set_allmulti(oldslave->dev, -1);
 1901                 }
 1902         }
 1903 
 1904         printk (" but could not find any %s interface.\n",
 1905                 (bond_mode == BOND_MODE_ACTIVEBACKUP) ? "backup":"other");
 1906         
 1907         /* absolutely nothing found. let's return NULL */
 1908         bond_assign_current_slave(bond, NULL);
 1909         return NULL;
 1910 }
 1911 
 1912 /*
 1913  * Try to release the slave device <slave> from the bond device <master>
 1914  * It is legal to access current_slave without a lock because all the function
 1915  * is write-locked.
 1916  *
 1917  * The rules for slave state should be:
 1918  *   for Active/Backup:
 1919  *     Active stays on all backups go down
 1920  *   for Bonded connections:
 1921  *     The first up interface should be left on and all others downed.
 1922  */
 1923 static int bond_release(struct net_device *master, struct net_device *slave)
 1924 {
 1925         bonding_t *bond;
 1926         slave_t *our_slave, *old_current;
 1927         struct sockaddr addr;
 1928         
 1929         if (master == NULL || slave == NULL)  {
 1930                 return -ENODEV;
 1931         }
 1932 
 1933         bond = (struct bonding *) master->priv;
 1934 
 1935         /* master already enslaved, or slave not enslaved,
 1936            or no slave for this master */
 1937         if ((master->flags & IFF_SLAVE) || !(slave->flags & IFF_SLAVE)) {
 1938                 printk (KERN_DEBUG "%s: cannot release %s.\n", master->name, slave->name);
 1939                 return -EINVAL;
 1940         }
 1941 
 1942         write_lock_bh(&bond->lock);
 1943         bond->current_arp_slave = NULL;
 1944         our_slave = (slave_t *)bond;
 1945         old_current = bond->current_slave;
 1946         while ((our_slave = our_slave->prev) != (slave_t *)bond) {
 1947                 if (our_slave->dev == slave) {
 1948                         int mac_addr_differ = memcmp(bond->device->dev_addr,
 1949                                                  our_slave->perm_hwaddr,
 1950                                                  ETH_ALEN);
 1951                         if (!mac_addr_differ && (bond->slave_cnt > 1)) {
 1952                                 printk(KERN_WARNING "WARNING: the permanent HWaddr of %s "
 1953                                 "- %02X:%02X:%02X:%02X:%02X:%02X - "
 1954                                 "is still in use by %s. Set the HWaddr "
 1955                                 "of %s to a different address "
 1956                                 "to avoid conflicts.\n",
 1957                                        slave->name,
 1958                                        slave->dev_addr[0],
 1959                                        slave->dev_addr[1],
 1960                                        slave->dev_addr[2],
 1961                                        slave->dev_addr[3],
 1962                                        slave->dev_addr[4],
 1963                                        slave->dev_addr[5],
 1964                                        bond->device->name,
 1965                                        slave->name);
 1966                         }
 1967 
 1968                         /* Inform AD package of unbinding of slave. */
 1969                         if (bond_mode == BOND_MODE_8023AD) {
 1970                                 /* must be called before the slave is
 1971                                  * detached from the list
 1972                                  */
 1973                                 bond_3ad_unbind_slave(our_slave);
 1974                         }
 1975 
 1976                         /* release the slave from its bond */
 1977                         bond_detach_slave(bond, our_slave);
 1978 
 1979                         if (bond->primary_slave == our_slave) {
 1980                                 bond->primary_slave = NULL;
 1981                         }
 1982 
 1983                         printk (KERN_INFO "%s: releasing %s interface %s",
 1984                                 master->name,
 1985                                 (our_slave->state == BOND_STATE_ACTIVE) ? "active" : "backup",
 1986                                 slave->name);
 1987 
 1988                         if (our_slave == old_current) {
 1989                                 /* find a new interface and be verbose */
 1990                                 change_active_interface(bond); 
 1991                         } else {
 1992                                 printk(".\n");
 1993                         }
 1994                         
 1995                         if (bond->current_slave == NULL) {
 1996                                 printk(KERN_INFO
 1997                                         "%s: now running without any active interface !\n",
 1998                                         master->name);
 1999                         }
 2000 
 2001                         if ((bond_mode == BOND_MODE_TLB) ||
 2002                             (bond_mode == BOND_MODE_ALB)) {
 2003                                 /* must be called only after the slave has been
 2004                                  * detached from the list and the current_slave
 2005                                  * has been replaced (if our_slave == old_current)
 2006                                  */
 2007                                 bond_alb_deinit_slave(bond, our_slave);
 2008                         }
 2009 
 2010                         break;
 2011                 }
 2012 
 2013         }
 2014         write_unlock_bh(&bond->lock);
 2015         
 2016         if (our_slave == (slave_t *)bond) {
 2017                 /* if we get here, it's because the device was not found */
 2018                 printk (KERN_INFO "%s: %s not enslaved\n", master->name, slave->name);
 2019                 return -EINVAL;
 2020         }
 2021 
 2022         /* undo settings and restore original values */
 2023         
 2024         if (multicast_mode == BOND_MULTICAST_ALL) {
 2025                 /* flush master's mc_list from slave */ 
 2026                 bond_mc_list_flush (slave, master); 
 2027 
 2028                 /* unset promiscuity level from slave */
 2029                 if (master->flags & IFF_PROMISC) 
 2030                         dev_set_promiscuity(slave, -1); 
 2031 
 2032                 /* unset allmulti level from slave */ 
 2033                 if (master->flags & IFF_ALLMULTI)
 2034                         dev_set_allmulti(slave, -1); 
 2035         }
 2036 
 2037         netdev_set_master(slave, NULL);
 2038 
 2039         /* close slave before restoring its mac address */
 2040         dev_close(slave);
 2041 
 2042         if (app_abi_ver >= 1) {
 2043                 /* restore original ("permanent") mac address */
 2044                 memcpy(addr.sa_data, our_slave->perm_hwaddr, ETH_ALEN);
 2045                 addr.sa_family = slave->type;
 2046                 slave->set_mac_address(slave, &addr);
 2047         }
 2048 
 2049         /* restore the original state of the
 2050          * IFF_NOARP flag that might have been
 2051          * set by bond_set_slave_inactive_flags()
 2052          */
 2053         if ((our_slave->original_flags & IFF_NOARP) == 0) {
 2054                 slave->flags &= ~IFF_NOARP;
 2055         }
 2056 
 2057         kfree(our_slave);
 2058 
 2059         /* if the last slave was removed, zero the mac address
 2060          * of the master so it will be set by the application
 2061          * to the mac address of the first slave
 2062          */
 2063         if (bond->next == (slave_t*)bond) {
 2064                 memset(master->dev_addr, 0, master->addr_len);
 2065         }
 2066 
 2067         return 0;  /* deletion OK */
 2068 }
 2069 
 2070 /* 
 2071  * This function releases all slaves.
 2072  */
 2073 static int bond_release_all(struct net_device *master)
 2074 {
 2075         bonding_t *bond;
 2076         slave_t *our_slave, *old_current;
 2077         struct net_device *slave_dev;
 2078         struct sockaddr addr;
 2079         int err = 0;
 2080 
 2081         if (master == NULL)  {
 2082                 return -ENODEV;
 2083         }
 2084 
 2085         if (master->flags & IFF_SLAVE) {
 2086                 return -EINVAL;
 2087         }
 2088 
 2089         bond = (struct bonding *) master->priv;
 2090 
 2091         write_lock_bh(&bond->lock);
 2092         if (bond->next == (struct slave *) bond) {
 2093                 err = -EINVAL;
 2094                 goto out;
 2095         }
 2096 
 2097         old_current = bond->current_slave;
 2098         bond_assign_current_slave(bond, NULL);
 2099         bond->current_arp_slave = NULL;
 2100         bond->primary_slave = NULL;
 2101 
 2102         while ((our_slave = bond->prev) != (slave_t *)bond) {
 2103                 /* Inform AD package of unbinding of slave
 2104                  * before slave is detached from the list.
 2105                  */
 2106                 if (bond_mode == BOND_MODE_8023AD) {
 2107                         bond_3ad_unbind_slave(our_slave);
 2108                 }
 2109 
 2110                 slave_dev = our_slave->dev;
 2111                 bond_detach_slave(bond, our_slave);
 2112 
 2113                 if ((bond_mode == BOND_MODE_TLB) ||
 2114                     (bond_mode == BOND_MODE_ALB)) {
 2115                         /* must be called only after the slave
 2116                          * has been detached from the list
 2117                          */
 2118                         bond_alb_deinit_slave(bond, our_slave);
 2119                 }
 2120 
 2121                 /* now that the slave is detached, unlock and perform
 2122                  * all the undo steps that should not be called from
 2123                  * within a lock.
 2124                  */
 2125                 write_unlock_bh(&bond->lock);
 2126 
 2127                 if (multicast_mode == BOND_MULTICAST_ALL 
 2128                     || (multicast_mode == BOND_MULTICAST_ACTIVE 
 2129                         && old_current == our_slave)) {
 2130 
 2131                         /* flush master's mc_list from slave */ 
 2132                         bond_mc_list_flush (slave_dev, master); 
 2133 
 2134                         /* unset promiscuity level from slave */
 2135                         if (master->flags & IFF_PROMISC) 
 2136                                 dev_set_promiscuity(slave_dev, -1); 
 2137 
 2138                         /* unset allmulti level from slave */ 
 2139                         if (master->flags & IFF_ALLMULTI)
 2140                                 dev_set_allmulti(slave_dev, -1); 
 2141                 }
 2142 
 2143                 netdev_set_master(slave_dev, NULL);
 2144 
 2145                 /* close slave before restoring its mac address */
 2146                 dev_close(slave_dev);
 2147 
 2148                 if (app_abi_ver >= 1) {
 2149                         /* restore original ("permanent") mac address*/
 2150                         memcpy(addr.sa_data, our_slave->perm_hwaddr, ETH_ALEN);
 2151                         addr.sa_family = slave_dev->type;
 2152                         slave_dev->set_mac_address(slave_dev, &addr);
 2153                 }
 2154 
 2155                 /* restore the original state of the IFF_NOARP flag that might have
 2156                  * been set by bond_set_slave_inactive_flags()
 2157                  */
 2158                 if ((our_slave->original_flags & IFF_NOARP) == 0) {
 2159                         slave_dev->flags &= ~IFF_NOARP;
 2160                 }
 2161 
 2162                 kfree(our_slave);
 2163 
 2164                 /* re-acquire the lock before getting the next slave */
 2165                 write_lock_bh(&bond->lock);
 2166         }
 2167 
 2168         /* zero the mac address of the master so it will be
 2169          * set by the application to the mac address of the
 2170          * first slave
 2171          */
 2172         memset(master->dev_addr, 0, master->addr_len);
 2173 
 2174         printk (KERN_INFO "%s: released all slaves\n", master->name);
 2175 
 2176 out:
 2177         write_unlock_bh(&bond->lock);
 2178 
 2179         return err;
 2180 }
 2181 
 2182 /* this function is called regularly to monitor each slave's link. */
 2183 static void bond_mii_monitor(struct net_device *master)
 2184 {
 2185         bonding_t *bond = (struct bonding *) master->priv;
 2186         slave_t *slave, *bestslave, *oldcurrent;
 2187         int slave_died = 0;
 2188 
 2189         read_lock(&bond->lock);
 2190 
 2191         /* we will try to read the link status of each of our slaves, and
 2192          * set their IFF_RUNNING flag appropriately. For each slave not
 2193          * supporting MII status, we won't do anything so that a user-space
 2194          * program could monitor the link itself if needed.
 2195          */
 2196 
 2197         bestslave = NULL;
 2198         slave = (slave_t *)bond;
 2199 
 2200         read_lock(&bond->ptrlock);
 2201         oldcurrent = bond->current_slave;
 2202         read_unlock(&bond->ptrlock);
 2203 
 2204         while ((slave = slave->prev) != (slave_t *)bond) {
 2205                 /* use updelay+1 to match an UP slave even when updelay is 0 */
 2206                 int mindelay = updelay + 1;
 2207                 struct net_device *dev = slave->dev;
 2208                 int link_state;
 2209                 u16 old_speed = slave->speed;
 2210                 u8 old_duplex = slave->duplex;
 2211                 
 2212                 link_state = bond_check_dev_link(dev, 0);
 2213 
 2214                 switch (slave->link) {
 2215                 case BOND_LINK_UP:      /* the link was up */
 2216                         if (link_state == BMSR_LSTATUS) {
 2217                                 /* link stays up, tell that this one
 2218                                    is immediately available */
 2219                                 if (IS_UP(dev) && (mindelay > -2)) {
 2220                                         /* -2 is the best case :
 2221                                            this slave was already up */
 2222                                         mindelay = -2;
 2223                                         bestslave = slave;
 2224                                 }
 2225                                 break;
 2226                         }
 2227                         else { /* link going down */
 2228                                 slave->link  = BOND_LINK_FAIL;
 2229                                 slave->delay = downdelay;
 2230                                 if (slave->link_failure_count < UINT_MAX) {
 2231                                         slave->link_failure_count++;
 2232                                 }
 2233                                 if (downdelay > 0) {
 2234                                         printk (KERN_INFO
 2235                                                 "%s: link status down for %sinterface "
 2236                                                 "%s, disabling it in %d ms.\n",
 2237                                                 master->name,
 2238                                                 IS_UP(dev)
 2239                                                 ? ((bond_mode == BOND_MODE_ACTIVEBACKUP)
 2240                                                    ? ((slave == oldcurrent)
 2241                                                       ? "active " : "backup ")
 2242                                                    : "")
 2243                                                 : "idle ",
 2244                                                 dev->name,
 2245                                                 downdelay * miimon);
 2246                                         }
 2247                         }
 2248                         /* no break ! fall through the BOND_LINK_FAIL test to
 2249                            ensure proper action to be taken
 2250                         */
 2251                 case BOND_LINK_FAIL:    /* the link has just gone down */
 2252                         if (link_state != BMSR_LSTATUS) {
 2253                                 /* link stays down */
 2254                                 if (slave->delay <= 0) {
 2255                                         /* link down for too long time */
 2256                                         slave->link = BOND_LINK_DOWN;
 2257                                         /* in active/backup mode, we must
 2258                                          * completely disable this interface
 2259                                          */
 2260                                         if ((bond_mode == BOND_MODE_ACTIVEBACKUP) ||
 2261                                             (bond_mode == BOND_MODE_8023AD)) {
 2262                                                 bond_set_slave_inactive_flags(slave);
 2263                                         }
 2264                                         printk(KERN_INFO
 2265                                                 "%s: link status definitely down "
 2266                                                 "for interface %s, disabling it",
 2267                                                 master->name,
 2268                                                 dev->name);
 2269 
 2270                                         /* notify ad that the link status has changed */
 2271                                         if (bond_mode == BOND_MODE_8023AD) {
 2272                                                 bond_3ad_handle_link_change(slave, BOND_LINK_DOWN);
 2273                                         }
 2274 
 2275                                         if ((bond_mode == BOND_MODE_TLB) ||
 2276                                             (bond_mode == BOND_MODE_ALB)) {
 2277                                                 bond_alb_handle_link_change(bond, slave, BOND_LINK_DOWN);
 2278                                         }
 2279 
 2280                                         write_lock(&bond->ptrlock);
 2281                                         if (slave == bond->current_slave) {
 2282                                                 /* find a new interface and be verbose */
 2283                                                 change_active_interface(bond);
 2284                                         } else {
 2285                                                 printk(".\n");
 2286                                         }
 2287                                         write_unlock(&bond->ptrlock);
 2288                                         slave_died = 1;
 2289                                 } else {
 2290                                         slave->delay--;
 2291                                 }
 2292                         } else {
 2293                                 /* link up again */
 2294                                 slave->link  = BOND_LINK_UP;
 2295                                 slave->jiffies = jiffies;
 2296                                 printk(KERN_INFO
 2297                                         "%s: link status up again after %d ms "
 2298                                         "for interface %s.\n",
 2299                                         master->name,
 2300                                         (downdelay - slave->delay) * miimon,
 2301                                         dev->name);
 2302 
 2303                                 if (IS_UP(dev) && (mindelay > -1)) {
 2304                                         /* -1 is a good case : this slave went
 2305                                            down only for a short time */
 2306                                         mindelay = -1;
 2307                                         bestslave = slave;
 2308                                 }
 2309                         }
 2310                         break;
 2311                 case BOND_LINK_DOWN:    /* the link was down */
 2312                         if (link_state != BMSR_LSTATUS) {
 2313                                 /* the link stays down, nothing more to do */
 2314                                 break;
 2315                         } else {        /* link going up */
 2316                                 slave->link  = BOND_LINK_BACK;
 2317                                 slave->delay = updelay;
 2318                                 
 2319                                 if (updelay > 0) {
 2320                                         /* if updelay == 0, no need to
 2321                                            advertise about a 0 ms delay */
 2322                                         printk (KERN_INFO
 2323                                                 "%s: link status up for interface"
 2324                                                 " %s, enabling it in %d ms.\n",
 2325                                                 master->name,
 2326                                                 dev->name,
 2327                                                 updelay * miimon);
 2328                                 }
 2329                         }
 2330                         /* no break ! fall through the BOND_LINK_BACK state in
 2331                            case there's something to do.
 2332                         */
 2333                 case BOND_LINK_BACK:    /* the link has just come back */
 2334                         if (link_state != BMSR_LSTATUS) {
 2335                                 /* link down again */
 2336                                 slave->link  = BOND_LINK_DOWN;
 2337                                 printk(KERN_INFO
 2338                                         "%s: link status down again after %d ms "
 2339                                         "for interface %s.\n",
 2340                                         master->name,
 2341                                         (updelay - slave->delay) * miimon,
 2342                                         dev->name);
 2343                         } else {
 2344                                 /* link stays up */
 2345                                 if (slave->delay == 0) {
 2346                                         /* now the link has been up for long time enough */
 2347                                         slave->link = BOND_LINK_UP;
 2348                                         slave->jiffies = jiffies;
 2349 
 2350                                         if (bond_mode == BOND_MODE_8023AD) {
 2351                                                 /* prevent it from being the active one */
 2352                                                 slave->state = BOND_STATE_BACKUP;
 2353                                         }
 2354                                         else if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
 2355                                                 /* make it immediately active */
 2356                                                 slave->state = BOND_STATE_ACTIVE;
 2357                                         } else if (slave != bond->primary_slave) {
 2358                                                 /* prevent it from being the active one */
 2359                                                 slave->state = BOND_STATE_BACKUP;
 2360                                         }
 2361 
 2362                                         printk(KERN_INFO
 2363                                                 "%s: link status definitely up "
 2364                                                 "for interface %s.\n",
 2365                                                 master->name,
 2366                                                 dev->name);
 2367         
 2368                                         /* notify ad that the link status has changed */
 2369                                         if (bond_mode == BOND_MODE_8023AD) {
 2370                                                 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
 2371                                         }
 2372 
 2373                                         if ((bond_mode == BOND_MODE_TLB) ||
 2374                                             (bond_mode == BOND_MODE_ALB)) {
 2375                                                 bond_alb_handle_link_change(bond, slave, BOND_LINK_UP);
 2376                                         }
 2377 
 2378                                         write_lock(&bond->ptrlock);
 2379                                         if ( (bond->primary_slave != NULL)
 2380                                           && (slave == bond->primary_slave) )
 2381                                                 change_active_interface(bond); 
 2382                                         write_unlock(&bond->ptrlock);
 2383                                 }
 2384                                 else
 2385                                         slave->delay--;
 2386 
 2387                                 /* we'll also look for the mostly eligible slave */
 2388                                 if (bond->primary_slave == NULL)  {
 2389                                     if (IS_UP(dev) && (slave->delay < mindelay)) {
 2390                                         mindelay = slave->delay;
 2391                                         bestslave = slave;
 2392                                     } 
 2393                                 } else if ( (IS_UP(bond->primary_slave->dev))  || 
 2394                                           ( (!IS_UP(bond->primary_slave->dev))  && 
 2395                                           (IS_UP(dev) && (slave->delay < mindelay)) ) ) {
 2396                                         mindelay = slave->delay;
 2397                                         bestslave = slave;
 2398                                 }
 2399                         }
 2400                         break;
 2401                 } /* end of switch */
 2402 
 2403                 bond_update_speed_duplex(slave);
 2404 
 2405                 if (bond_mode == BOND_MODE_8023AD) {
 2406                         if (old_speed != slave->speed) {
 2407                                 bond_3ad_adapter_speed_changed(slave);
 2408                         }
 2409                         if (old_duplex != slave->duplex) {
 2410                                 bond_3ad_adapter_duplex_changed(slave);
 2411                         }
 2412                 }
 2413 
 2414         } /* end of while */
 2415 
 2416         /* 
 2417          * if there's no active interface and we discovered that one
 2418          * of the slaves could be activated earlier, so we do it.
 2419          */
 2420         read_lock(&bond->ptrlock);
 2421         oldcurrent = bond->current_slave;
 2422         read_unlock(&bond->ptrlock);
 2423 
 2424         /* no active interface at the moment or need to bring up the primary */
 2425         if (oldcurrent == NULL)  { /* no active interface at the moment */
 2426                 if (bestslave != NULL) { /* last chance to find one ? */
 2427                         if (bestslave->link == BOND_LINK_UP) {
 2428                                 printk (KERN_INFO
 2429                                         "%s: making interface %s the new active one.\n",
 2430                                         master->name, bestslave->dev->name);
 2431                         } else {
 2432                                 printk (KERN_INFO
 2433                                         "%s: making interface %s the new "
 2434                                         "active one %d ms earlier.\n",
 2435                                         master->name, bestslave->dev->name,
 2436                                         (updelay - bestslave->delay) * miimon);
 2437 
 2438                                 bestslave->delay = 0;
 2439                                 bestslave->link  = BOND_LINK_UP;
 2440                                 bestslave->jiffies = jiffies;
 2441 
 2442                                 /* notify ad that the link status has changed */
 2443                                 if (bond_mode == BOND_MODE_8023AD) {
 2444                                         bond_3ad_handle_link_change(bestslave, BOND_LINK_UP);
 2445                                 }
 2446 
 2447                                 if ((bond_mode == BOND_MODE_TLB) ||
 2448                                     (bond_mode == BOND_MODE_ALB)) {
 2449                                         bond_alb_handle_link_change(bond, bestslave, BOND_LINK_UP);
 2450                                 }
 2451                         }
 2452 
 2453                         if (bond_mode == BOND_MODE_ACTIVEBACKUP) {
 2454                                 bond_set_slave_active_flags(bestslave);
 2455                                 bond_mc_update(bond, bestslave, NULL);
 2456                         } else if (bond_mode != BOND_MODE_8023AD) {
 2457                                 bestslave->state = BOND_STATE_ACTIVE;
 2458                         }
 2459                         write_lock(&bond->ptrlock);
 2460                         bond_assign_current_slave(bond, bestslave);
 2461                         write_unlock(&bond->ptrlock);
 2462                 } else if (slave_died) {
 2463                         /* print this message only once a slave has just died */
 2464                         printk(KERN_INFO
 2465                                 "%s: now running without any active interface !\n",
 2466                                 master->name);
 2467                 }
 2468         }
 2469 
 2470         read_unlock(&bond->lock);
 2471         /* re-arm the timer */
 2472         mod_timer(&bond->mii_timer, jiffies + (miimon * HZ / 1000));
 2473 }
 2474 
 2475 /* 
 2476  * this function is called regularly to monitor each slave's link 
 2477  * ensuring that traffic is being sent and received when arp monitoring
 2478  * is used in load-balancing mode. if the adapter has been dormant, then an 
 2479  * arp is transmitted to generate traffic. see activebackup_arp_monitor for 
 2480  * arp monitoring in active backup mode. 
 2481  */
 2482 static void loadbalance_arp_monitor(struct net_device *master)
 2483 {
 2484         bonding_t *bond;
 2485         slave_t *slave;
 2486         int the_delta_in_ticks =  arp_interval * HZ / 1000;
 2487         int next_timer = jiffies + (arp_interval * HZ / 1000);
 2488 
 2489         bond = (struct bonding *) master->priv; 
 2490         if (master->priv == NULL) {
 2491                 mod_timer(&bond->arp_timer, next_timer);
 2492                 return;
 2493         }
 2494 
 2495         /* TODO: investigate why rtnl_shlock_nowait and rtnl_exlock_nowait
 2496          * are called below and add comment why they are required... 
 2497          */
 2498         if ((!IS_UP(master)) || rtnl_shlock_nowait()) {
 2499                 mod_timer(&bond->arp_timer, next_timer);
 2500                 return;
 2501         }
 2502 
 2503         if (rtnl_exlock_nowait()) {
 2504                 rtnl_shunlock();
 2505                 mod_timer(&bond->arp_timer, next_timer);
 2506                 return;
 2507         }
 2508 
 2509         read_lock(&bond->lock);
 2510 
 2511         /* see if any of the previous devices are up now (i.e. they have
 2512          * xmt and rcv traffic). the current_slave does not come into
 2513          * the picture unless it is null. also, slave->jiffies is not needed
 2514          * here because we send an arp on each slave and give a slave as
 2515          * long as it needs to get the tx/rx within the delta.
 2516          * TODO: what about up/down delay in arp mode? it wasn't here before
 2517          *       so it can wait 
 2518          */
 2519         slave = (slave_t *)bond;
 2520         while ((slave = slave->prev) != (slave_t *)bond)  {
 2521 
 2522                 if (slave->link != BOND_LINK_UP) {
 2523 
 2524                         if (((jiffies - slave->dev->trans_start) <= 
 2525                                                 the_delta_in_ticks) &&  
 2526                              ((jiffies - slave->dev->last_rx) <= 
 2527                                                 the_delta_in_ticks)) {
 2528 
 2529                                 slave->link  = BOND_LINK_UP;
 2530                                 slave->state = BOND_STATE_ACTIVE;
 2531 
 2532                                 /* primary_slave has no meaning in round-robin
 2533                                  * mode. the window of a slave being up and 
 2534                                  * current_slave being null after enslaving
 2535                                  * is closed.
 2536                                  */
 2537                                 write_lock(&bond->ptrlock);
 2538                                 if (bond->current_slave == NULL) {
 2539                                         printk(KERN_INFO
 2540                                                 "%s: link status definitely up "
 2541                                                 "for interface %s, ",
 2542                                                 master->name,
 2543                                                 slave->dev->name);
 2544                                         change_active_interface(bond); 
 2545                                 } else {
 2546                                         printk(KERN_INFO
 2547                                                 "%s: interface %s is now up\n",
 2548                                                 master->name,
 2549                                                 slave->dev->name);
 2550                                 }
 2551                                 write_unlock(&bond->ptrlock);
 2552                         } 
 2553                 } else {
 2554                         /* slave->link == BOND_LINK_UP */
 2555 
 2556                         /* not all switches will respond to an arp request
 2557                          * when the source ip is 0, so don't take the link down
 2558                          * if we don't know our ip yet
 2559                          */
 2560                         if (((jiffies - slave->dev->trans_start) >= 
 2561                               (2*the_delta_in_ticks)) ||
 2562                              (((jiffies - slave->dev->last_rx) >= 
 2563                                (2*the_delta_in_ticks)) && my_ip !=0)) {
 2564                                 slave->link  = BOND_LINK_DOWN;
 2565                                 slave->state = BOND_STATE_BACKUP;
 2566                                 if (slave->link_failure_count < UINT_MAX) {
 2567                                         slave->link_failure_count++;
 2568                                 }
 2569                                 printk(KERN_INFO
 2570                                        "%s: interface %s is now down.\n",
 2571                                        master->name,
 2572                                        slave->dev->name);
 2573 
 2574                                 write_lock(&bond->ptrlock);
 2575                                 if (slave == bond->current_slave) {
 2576                                         change_active_interface(bond);
 2577                                 }
 2578                                 write_unlock(&bond->ptrlock);
 2579                         }
 2580                 } 
 2581 
 2582                 /* note: if switch is in round-robin mode, all links 
 2583                  * must tx arp to ensure all links rx an arp - otherwise
 2584                  * links may oscillate or not come up at all; if switch is 
 2585                  * in something like xor mode, there is nothing we can 
 2586                  * do - all replies will be rx'ed on same link causing slaves 
 2587                  * to be unstable during low/no traffic periods
 2588                  */
 2589                 if (IS_UP(slave->dev)) {
 2590                         arp_send_all(slave);
 2591                 }
 2592         }
 2593 
 2594         read_unlock(&bond->lock);
 2595         rtnl_exunlock();
 2596         rtnl_shunlock();
 2597 
 2598         /* re-arm the timer */
 2599         mod_timer(&bond->arp_timer, next_timer);
 2600 }
 2601 
 2602 /* 
 2603  * When using arp monitoring in active-backup mode, this function is
 2604  * called to determine if any backup slaves have went down or a new
 2605  * current slave needs to be found.
 2606  * The backup slaves never generate traffic, they are considered up by merely 
 2607  * receiving traffic. If the current slave goes down, each backup slave will 
 2608  * be given the opportunity to tx/rx an arp before being taken down - this 
 2609  * prevents all slaves from being taken down due to the current slave not 
 2610  * sending any traffic for the backups to receive. The arps are not necessarily
 2611  * necessary, any tx and rx traffic will keep the current slave up. While any 
 2612  * rx traffic will keep the backup slaves up, the current slave is responsible 
 2613  * for generating traffic to keep them up regardless of any other traffic they 
 2614  * may have received.
 2615  * see loadbalance_arp_monitor for arp monitoring in load balancing mode
 2616  */
 2617 static void activebackup_arp_monitor(struct net_device *master)
 2618 {
 2619         bonding_t *bond;
 2620         slave_t *slave;
 2621         int the_delta_in_ticks =  arp_interval * HZ / 1000;
 2622         int next_timer = jiffies + (arp_interval * HZ / 1000);
 2623 
 2624         bond = (struct bonding *) master->priv; 
 2625         if (master->priv == NULL) {
 2626                 mod_timer(&bond->arp_timer, next_timer);
 2627                 return;
 2628         }
 2629 
 2630         if (!IS_UP(master)) {
 2631                 mod_timer(&bond->arp_timer, next_timer);
 2632                 return;
 2633         }
 2634 
 2635         read_lock(&bond->lock);
 2636 
 2637         /* determine if any slave has come up or any backup slave has 
 2638          * gone down 
 2639          * TODO: what about up/down delay in arp mode? it wasn't here before
 2640          *       so it can wait 
 2641          */
 2642         slave = (slave_t *)bond;
 2643         while ((slave = slave->prev) != (slave_t *)bond)  {
 2644 
 2645                 if (slave->link != BOND_LINK_UP) {
 2646                         if ((jiffies - slave->dev->last_rx) <=
 2647                             the_delta_in_ticks) {
 2648 
 2649                                 slave->link = BOND_LINK_UP;
 2650                                 write_lock(&bond->ptrlock);
 2651                                 if ((bond->current_slave == NULL) &&
 2652                                     ((jiffies - slave->dev->trans_start) <=
 2653                                      the_delta_in_ticks)) {
 2654                                         bond_assign_current_slave(bond, slave);
 2655                                         bond_set_slave_active_flags(slave);
 2656                                         bond_mc_update(bond, slave, NULL);
 2657                                         bond->current_arp_slave = NULL;
 2658                                 } else if (bond->current_slave != slave) {
 2659                                         /* this slave has just come up but we 
 2660                                          * already have a current slave; this
 2661                                          * can also happen if bond_enslave adds
 2662                                          * a new slave that is up while we are 
 2663                                          * searching for a new slave
 2664                                          */
 2665                                         bond_set_slave_inactive_flags(slave);
 2666                                         bond->current_arp_slave = NULL;
 2667                                 }
 2668 
 2669                                 if (slave == bond->current_slave) {
 2670                                         printk(KERN_INFO
 2671                                                 "%s: %s is up and now the "
 2672                                                 "active interface\n",
 2673                                                 master->name,
 2674                                                 slave->dev->name);
 2675                                 } else {
 2676                                         printk(KERN_INFO
 2677                                                 "%s: backup interface %s is "
 2678                                                 "now up\n",
 2679                                                 master->name,
 2680                                                 slave->dev->name);
 2681                                 }
 2682 
 2683                                 write_unlock(&bond->ptrlock);
 2684                         }
 2685                 } else {
 2686                         read_lock(&bond->ptrlock);
 2687                         if ((slave != bond->current_slave) &&
 2688                             (bond->current_arp_slave == NULL) &&
 2689                             (((jiffies - slave->dev->last_rx) >=
 2690                              3*the_delta_in_ticks) && (my_ip != 0))) {
 2691                                 /* a backup slave has gone down; three times 
 2692                                  * the delta allows the current slave to be 
 2693                                  * taken out before the backup slave.
 2694                                  * note: a non-null current_arp_slave indicates
 2695                                  * the current_slave went down and we are 
 2696                                  * searching for a new one; under this 
 2697                                  * condition we only take the current_slave 
 2698                                  * down - this gives each slave a chance to 
 2699                                  * tx/rx traffic before being taken out
 2700                                  */
 2701                                 read_unlock(&bond->ptrlock);
 2702                                 slave->link  = BOND_LINK_DOWN;
 2703                                 if (slave->link_failure_count < UINT_MAX) {
 2704                                         slave->link_failure_count++;
 2705                                 }
 2706                                 bond_set_slave_inactive_flags(slave);
 2707                                 printk(KERN_INFO
 2708                                         "%s: backup interface %s is now down\n",
 2709                                         master->name,
 2710                                         slave->dev->name);
 2711                         } else {
 2712                                 read_unlock(&bond->ptrlock);
 2713                         }
 2714                 }
 2715         }
 2716 
 2717         read_lock(&bond->ptrlock);
 2718         slave = bond->current_slave;
 2719         read_unlock(&bond->ptrlock);
 2720 
 2721         if (slave != NULL) {
 2722 
 2723                 /* if we have sent traffic in the past 2*arp_intervals but
 2724                  * haven't xmit and rx traffic in that time interval, select 
 2725                  * a different slave. slave->jiffies is only updated when
 2726                  * a slave first becomes the current_slave - not necessarily
 2727                  * after every arp; this ensures the slave has a full 2*delta 
 2728                  * before being taken out. if a primary is being used, check 
 2729                  * if it is up and needs to take over as the current_slave
 2730                  */
 2731                 if ((((jiffies - slave->dev->trans_start) >= 
 2732                        (2*the_delta_in_ticks)) ||
 2733                      (((jiffies - slave->dev->last_rx) >= 
 2734                        (2*the_delta_in_ticks)) && (my_ip != 0))) &&
 2735                     ((jiffies - slave->jiffies) >= 2*the_delta_in_ticks)) {
 2736 
 2737                         slave->link  = BOND_LINK_DOWN;
 2738                         if (slave->link_failure_count < UINT_MAX) {
 2739                                 slave->link_failure_count++;
 2740                         }
 2741                         printk(KERN_INFO "%s: link status down for "
 2742                                          "active interface %s, disabling it",
 2743                                master->name,
 2744                                slave->dev->name);
 2745                         write_lock(&bond->ptrlock);
 2746                         slave = change_active_interface(bond);
 2747                         write_unlock(&bond->ptrlock);
 2748                         bond->current_arp_slave = slave;
 2749                         if (slave != NULL) {
 2750                                 slave->jiffies = jiffies;
 2751                         }
 2752 
 2753                 } else if ((bond->primary_slave != NULL) && 
 2754                            (bond->primary_slave != slave) && 
 2755                            (bond->primary_slave->link == BOND_LINK_UP)) {
 2756                         /* at this point, slave is the current_slave */
 2757                         printk(KERN_INFO 
 2758                                "%s: changing from interface %s to primary "
 2759                                "interface %s\n",
 2760                                master->name, 
 2761                                slave->dev->name, 
 2762                                bond->primary_slave->dev->name);
 2763                                
 2764                         /* primary is up so switch to it */
 2765                         bond_set_slave_inactive_flags(slave);
 2766                         bond_mc_update(bond, bond->primary_slave, slave);
 2767                         write_lock(&bond->ptrlock);
 2768                         bond_assign_current_slave(bond, bond->primary_slave);
 2769                         write_unlock(&bond->ptrlock);
 2770                         slave = bond->primary_slave;
 2771                         bond_set_slave_active_flags(slave);
 2772                         slave->jiffies = jiffies;
 2773                 } else {
 2774                         bond->current_arp_slave = NULL;
 2775                 }
 2776 
 2777                 /* the current slave must tx an arp to ensure backup slaves
 2778                  * rx traffic
 2779                  */
 2780                 if ((slave != NULL) && (my_ip != 0)) {
 2781                         arp_send_all(slave);
 2782                 }
 2783         }
 2784 
 2785         /* if we don't have a current_slave, search for the next available 
 2786          * backup slave from the current_arp_slave and make it the candidate 
 2787          * for becoming the current_slave
 2788          */
 2789         if (slave == NULL) { 
 2790 
 2791                 if ((bond->current_arp_slave == NULL) ||
 2792                     (bond->current_arp_slave == (slave_t *)bond)) {
 2793                         bond->current_arp_slave = bond->prev;
 2794                 } 
 2795 
 2796                 if (bond->current_arp_slave != (slave_t *)bond) {
 2797                         bond_set_slave_inactive_flags(bond->current_arp_slave);
 2798                         slave = bond->current_arp_slave->next;
 2799 
 2800                         /* search for next candidate */
 2801                         do {
 2802                                 if (IS_UP(slave->dev)) {
 2803                                         slave->link = BOND_LINK_BACK;
 2804                                         bond_set_slave_active_flags(slave);
 2805                                         arp_send_all(slave);
 2806                                         slave->jiffies = jiffies;
 2807                                         bond->current_arp_slave = slave;
 2808                                         break;
 2809                                 }
 2810 
 2811                                 /* if the link state is up at this point, we 
 2812                                  * mark it down - this can happen if we have 
 2813                                  * simultaneous link failures and 
 2814                                  * change_active_interface doesn't make this 
 2815                                  * one the current slave so it is still marked 
 2816                                  * up when it is actually down
 2817                                  */
 2818                                 if (slave->link == BOND_LINK_UP) {
 2819                                         slave->link  = BOND_LINK_DOWN;
 2820                                         if (slave->link_failure_count < 
 2821                                                         UINT_MAX) {
 2822                                                 slave->link_failure_count++;
 2823                                         }
 2824 
 2825                                         bond_set_slave_inactive_flags(slave);
 2826                                         printk(KERN_INFO
 2827                                                 "%s: backup interface "
 2828                                                 "%s is now down.\n",
 2829                                                 master->name,
 2830                                                 slave->dev->name);
 2831                                 }
 2832                         } while ((slave = slave->next) != 
 2833                                         bond->current_arp_slave->next);
 2834                 }
 2835         }
 2836 
 2837         read_unlock(&bond->lock);
 2838         mod_timer(&bond->arp_timer, next_timer);
 2839 }
 2840 
 2841 static int bond_sethwaddr(struct net_device *master, struct net_device *slave)
 2842 {
 2843 #ifdef BONDING_DEBUG
 2844         printk(KERN_CRIT "bond_sethwaddr: master=%x\n", (unsigned int)master);
 2845         printk(KERN_CRIT "bond_sethwaddr: slave=%x\n", (unsigned int)slave);
 2846         printk(KERN_CRIT "bond_sethwaddr: slave->addr_len=%d\n", slave->addr_len);
 2847 #endif
 2848         memcpy(master->dev_addr, slave->dev_addr, slave->addr_len);
 2849         return 0;
 2850 }
 2851 
 2852 static int bond_info_query(struct net_device *master, struct ifbond *info)
 2853 {
 2854         bonding_t *bond = (struct bonding *) master->priv;
 2855         slave_t *slave;
 2856 
 2857         info->bond_mode = bond_mode;
 2858         info->num_slaves = 0;
 2859         info->miimon = miimon;
 2860 
 2861         read_lock_bh(&bond->lock);
 2862         for (slave = bond->prev; slave != (slave_t *)bond; slave = slave->prev) {
 2863                 info->num_slaves++;
 2864         }
 2865         read_unlock_bh(&bond->lock);
 2866 
 2867         return 0;
 2868 }
 2869 
 2870 static int bond_slave_info_query(struct net_device *master, 
 2871                                         struct ifslave *info)
 2872 {
 2873         bonding_t *bond = (struct bonding *) master->priv;
 2874         slave_t *slave;
 2875         int cur_ndx = 0;
 2876 
 2877         if (info->slave_id < 0) {
 2878                 return -ENODEV;
 2879         }
 2880 
 2881         read_lock_bh(&bond->lock);
 2882         for (slave = bond->prev; 
 2883                  slave != (slave_t *)bond && cur_ndx < info->slave_id; 
 2884                  slave = slave->prev) {
 2885                 cur_ndx++;
 2886         }
 2887         read_unlock_bh(&bond->lock);
 2888 
 2889         if (slave != (slave_t *)bond) {
 2890                 strcpy(info->slave_name, slave->dev->name);
 2891                 info->link = slave->link;
 2892                 info->state = slave->state;
 2893                 info->link_failure_count = slave->link_failure_count;
 2894         } else {
 2895                 return -ENODEV;
 2896         }
 2897 
 2898         return 0;
 2899 }
 2900 
 2901 static int bond_ethtool_ioctl(struct net_device *master_dev, struct ifreq *ifr)
 2902 {
 2903         void *addr = ifr->ifr_data;
 2904         uint32_t cmd;
 2905 
 2906         if (get_user(cmd, (uint32_t *) addr))
 2907                 return -EFAULT;
 2908 
 2909         switch (cmd) {
 2910 
 2911         case ETHTOOL_GDRVINFO:
 2912                 {
 2913                         struct ethtool_drvinfo info;
 2914                         char *endptr;
 2915 
 2916                         if (copy_from_user(&info, addr, sizeof(info)))
 2917                                 return -EFAULT;
 2918 
 2919                         if (strcmp(info.driver, "ifenslave") == 0) {
 2920                                 int new_abi_ver;
 2921 
 2922                                 new_abi_ver = simple_strtoul(info.fw_version,
 2923                                                              &endptr, 0);
 2924                                 if (*endptr) {
 2925                                         printk(KERN_ERR
 2926                                                "bonding: Error: got invalid ABI"
 2927                                                " version from application\n");
 2928 
 2929                                         return -EINVAL;
 2930                                 }
 2931 
 2932                                 if (orig_app_abi_ver == -1) {
 2933                                         orig_app_abi_ver  = new_abi_ver;
 2934                                 }
 2935 
 2936                                 app_abi_ver = new_abi_ver;
 2937                         }
 2938 
 2939                         strncpy(info.driver,  DRV_NAME, 32);
 2940                         strncpy(info.version, DRV_VERSION, 32);
 2941                         snprintf(info.fw_version, 32, "%d", BOND_ABI_VERSION);
 2942 
 2943                         if (copy_to_user(addr, &info, sizeof(info)))
 2944                                 return -EFAULT;
 2945 
 2946                         return 0;
 2947                 }
 2948                 break;
 2949         default:
 2950                 return -EOPNOTSUPP;
 2951         }
 2952 }
 2953 
 2954 static int bond_ioctl(struct net_device *master_dev, struct ifreq *ifr, int cmd)
 2955 {
 2956         struct net_device *slave_dev = NULL;
 2957         struct ifbond *u_binfo = NULL, k_binfo;
 2958         struct ifslave *u_sinfo = NULL, k_sinfo;
 2959         struct mii_ioctl_data *mii = NULL;
 2960         int prev_abi_ver = orig_app_abi_ver;
 2961         int ret = 0;
 2962 
 2963 #ifdef BONDING_DEBUG
 2964         printk(KERN_INFO "bond_ioctl: master=%s, cmd=%d\n", 
 2965                 master_dev->name, cmd);
 2966 #endif
 2967 
 2968         switch (cmd) {
 2969         case SIOCETHTOOL:
 2970                 return bond_ethtool_ioctl(master_dev, ifr);
 2971 
 2972         case SIOCGMIIPHY:
 2973                 mii = (struct mii_ioctl_data *)&ifr->ifr_data;
 2974                 if (mii == NULL) {
 2975                         return -EINVAL;
 2976                 }
 2977                 mii->phy_id = 0;
 2978                 /* Fall Through */
 2979         case SIOCGMIIREG:
 2980                 /* 
 2981                  * We do this again just in case we were called by SIOCGMIIREG
 2982                  * instead of SIOCGMIIPHY.
 2983                  */
 2984                 mii = (struct mii_ioctl_data *)&ifr->ifr_data;
 2985                 if (mii == NULL) {
 2986                         return -EINVAL;
 2987                 }
 2988                 if (mii->reg_num == 1) {
 2989                         mii->val_out = bond_check_mii_link(
 2990                                 (struct bonding *)master_dev->priv);
 2991                 }
 2992                 return 0;
 2993         case BOND_INFO_QUERY_OLD:
 2994         case SIOCBONDINFOQUERY:
 2995                 u_binfo = (struct ifbond *)ifr->ifr_data;
 2996                 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
 2997                         return -EFAULT;
 2998                 }
 2999                 ret = bond_info_query(master_dev, &k_binfo);
 3000                 if (ret == 0) {
 3001                         if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
 3002                                 return -EFAULT;
 3003                         }
 3004                 }
 3005                 return ret;
 3006         case BOND_SLAVE_INFO_QUERY_OLD:
 3007         case SIOCBONDSLAVEINFOQUERY:
 3008                 u_sinfo = (struct ifslave *)ifr->ifr_data;
 3009                 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
 3010                         return -EFAULT;
 3011                 }
 3012                 ret = bond_slave_info_query(master_dev, &k_sinfo);
 3013                 if (ret == 0) {
 3014                         if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
 3015                                 return -EFAULT;
 3016                         }
 3017                 }
 3018                 return ret;
 3019         }
 3020 
 3021         if (!capable(CAP_NET_ADMIN)) {
 3022                 return -EPERM;
 3023         }
 3024 
 3025         if (orig_app_abi_ver == -1) {
 3026                 /* no orig_app_abi_ver was provided yet, so we'll use the
 3027                  * current one from now on, even if it's 0
 3028                  */
 3029                 orig_app_abi_ver = app_abi_ver;
 3030 
 3031         } else if (orig_app_abi_ver != app_abi_ver) {
 3032                 printk(KERN_ERR
 3033                        "bonding: Error: already using ifenslave ABI "
 3034                        "version %d; to upgrade ifenslave to version %d, "
 3035                        "you must first reload bonding.\n",
 3036                        orig_app_abi_ver, app_abi_ver);
 3037                 return -EINVAL;
 3038         }
 3039 
 3040         slave_dev = dev_get_by_name(ifr->ifr_slave);
 3041 
 3042 #ifdef BONDING_DEBUG
 3043         printk(KERN_INFO "slave_dev=%x: \n", (unsigned int)slave_dev);
 3044         printk(KERN_INFO "slave_dev->name=%s: \n", slave_dev->name);
 3045 #endif
 3046 
 3047         if (slave_dev == NULL) {
 3048                 ret = -ENODEV;
 3049         } else {
 3050                 switch (cmd) {
 3051                 case BOND_ENSLAVE_OLD:
 3052                 case SIOCBONDENSLAVE:           
 3053                         ret = bond_enslave(master_dev, slave_dev);
 3054                         break;
 3055                 case BOND_RELEASE_OLD:                  
 3056                 case SIOCBONDRELEASE:   
 3057                         ret = bond_release(master_dev, slave_dev); 
 3058                         break;
 3059                 case BOND_SETHWADDR_OLD:
 3060                 case SIOCBONDSETHWADDR: 
 3061                         ret = bond_sethwaddr(master_dev, slave_dev);
 3062                         break;
 3063                 case BOND_CHANGE_ACTIVE_OLD:
 3064                 case SIOCBONDCHANGEACTIVE:
 3065                         if ((bond_mode == BOND_MODE_ACTIVEBACKUP) ||
 3066                             (bond_mode == BOND_MODE_TLB) ||
 3067                             (bond_mode == BOND_MODE_ALB)) {
 3068                                 ret = bond_change_active(master_dev, slave_dev);
 3069                         }
 3070                         else {
 3071                                 ret = -EINVAL;
 3072                         }
 3073                         break;
 3074                 default:
 3075                         ret = -EOPNOTSUPP;
 3076                 }
 3077                 dev_put(slave_dev);
 3078         }
 3079 
 3080         if (ret < 0) {
 3081                 /* The ioctl failed, so there's no point in changing the
 3082                  * orig_app_abi_ver. We'll restore it's value just in case
 3083                  * we've changed it earlier in this function.
 3084                  */
 3085                 orig_app_abi_ver = prev_abi_ver;
 3086         }
 3087 
 3088         return ret;
 3089 }
 3090 
 3091 #ifdef CONFIG_NET_FASTROUTE
 3092 static int bond_accept_fastpath(struct net_device *dev, struct dst_entry *dst)
 3093 {
 3094         return -1;
 3095 }
 3096 #endif
 3097 
 3098 /* 
 3099  * in broadcast mode, we send everything to all usable interfaces.
 3100  */
 3101 static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *dev)
 3102 {
 3103         slave_t *slave, *start_at;
 3104         struct bonding *bond = (struct bonding *) dev->priv;
 3105         struct net_device *device_we_should_send_to = 0;
 3106 
 3107         if (!IS_UP(dev)) { /* bond down */
 3108                 dev_kfree_skb(skb);
 3109                 return 0;
 3110         }
 3111 
 3112         read_lock(&bond->lock);
 3113 
 3114         read_lock(&bond->ptrlock);
 3115         slave = start_at = bond->current_slave;
 3116         read_unlock(&bond->ptrlock);
 3117 
 3118         if (slave == NULL) { /* we're at the root, get the first slave */
 3119                 /* no suitable interface, frame not sent */
 3120                 read_unlock(&bond->lock);
 3121                 dev_kfree_skb(skb);
 3122                 return 0;
 3123         }
 3124 
 3125         do {
 3126                 if (IS_UP(slave->dev)
 3127                     && (slave->link == BOND_LINK_UP)
 3128                     && (slave->state == BOND_STATE_ACTIVE)) {
 3129                         if (device_we_should_send_to) {
 3130                                 struct sk_buff *skb2;
 3131                                 if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
 3132                                         printk(KERN_ERR "bond_xmit_broadcast: skb_clone() failed\n");
 3133                                         continue;
 3134                                 }
 3135 
 3136                                 skb2->dev = device_we_should_send_to;
 3137                                 skb2->priority = 1;
 3138                                 dev_queue_xmit(skb2);
 3139                         }
 3140                         device_we_should_send_to = slave->dev;
 3141                 }
 3142         } while ((slave = slave->next) != start_at);
 3143 
 3144         if (device_we_should_send_to) {
 3145                 skb->dev = device_we_should_send_to;
 3146                 skb->priority = 1;
 3147                 dev_queue_xmit(skb);
 3148         } else
 3149                 dev_kfree_skb(skb);
 3150 
 3151         /* frame sent to all suitable interfaces */
 3152         read_unlock(&bond->lock);
 3153         return 0;
 3154 }
 3155 
 3156 static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *dev)
 3157 {
 3158         slave_t *slave, *start_at;
 3159         struct bonding *bond = (struct bonding *) dev->priv;
 3160 
 3161         if (!IS_UP(dev)) { /* bond down */
 3162                 dev_kfree_skb(skb);
 3163                 return 0;
 3164         }
 3165 
 3166         read_lock(&bond->lock);
 3167 
 3168         read_lock(&bond->ptrlock);
 3169         slave = start_at = bond->current_slave;
 3170         read_unlock(&bond->ptrlock);
 3171 
 3172         if (slave == NULL) { /* we're at the root, get the first slave */
 3173                 /* no suitable interface, frame not sent */
 3174                 dev_kfree_skb(skb);
 3175                 read_unlock(&bond->lock);
 3176                 return 0;
 3177         }
 3178 
 3179         do {
 3180                 if (IS_UP(slave->dev)
 3181                     && (slave->link == BOND_LINK_UP)
 3182                     && (slave->state == BOND_STATE_ACTIVE)) {
 3183 
 3184                         skb->dev = slave->dev;
 3185                         skb->priority = 1;
 3186                         dev_queue_xmit(skb);
 3187 
 3188                         write_lock(&bond->ptrlock);
 3189                         bond_assign_current_slave(bond, slave->next);
 3190                         write_unlock(&bond->ptrlock);
 3191 
 3192                         read_unlock(&bond->lock);
 3193                         return 0;
 3194                 }
 3195         } while ((slave = slave->next) != start_at);
 3196 
 3197         /* no suitable interface, frame not sent */
 3198         dev_kfree_skb(skb);
 3199         read_unlock(&bond->lock);
 3200         return 0;
 3201 }
 3202 
 3203 /* 
 3204  * in XOR mode, we determine the output device by performing xor on
 3205  * the source and destination hw adresses.  If this device is not 
 3206  * enabled, find the next slave following this xor slave. 
 3207  */
 3208 static int bond_xmit_xor(struct sk_buff *skb, struct net_device *dev)
 3209 {
 3210         slave_t *slave, *start_at;
 3211         struct bonding *bond = (struct bonding *) dev->priv;
 3212         struct ethhdr *data = (struct ethhdr *)skb->data;
 3213         int slave_no;
 3214 
 3215         if (!IS_UP(dev)) { /* bond down */
 3216                 dev_kfree_skb(skb);
 3217                 return 0;
 3218         }
 3219 
 3220         read_lock(&bond->lock);
 3221         slave = bond->prev;
 3222 
 3223         /* we're at the root, get the first slave */
 3224         if (bond->slave_cnt == 0) {
 3225                 /* no suitable interface, frame not sent */
 3226                 dev_kfree_skb(skb);
 3227                 read_unlock(&bond->lock);
 3228                 return 0;
 3229         }
 3230 
 3231         slave_no = (data->h_dest[5]^slave->dev->dev_addr[5]) % bond->slave_cnt;
 3232 
 3233         while ( (slave_no > 0) && (slave != (slave_t *)bond) ) {
 3234                 slave = slave->prev;
 3235                 slave_no--;
 3236         } 
 3237         start_at = slave;
 3238 
 3239         do {
 3240                 if (IS_UP(slave->dev)
 3241                     && (slave->link == BOND_LINK_UP)
 3242                     && (slave->state == BOND_STATE_ACTIVE)) {
 3243 
 3244                         skb->dev = slave->dev;
 3245                         skb->priority = 1;
 3246                         dev_queue_xmit(skb);
 3247 
 3248                         read_unlock(&bond->lock);
 3249                         return 0;
 3250                 }
 3251         } while ((slave = slave->next) != start_at);
 3252 
 3253         /* no suitable interface, frame not sent */
 3254         dev_kfree_skb(skb);
 3255         read_unlock(&bond->lock);
 3256         return 0;
 3257 }
 3258 
 3259 /* 
 3260  * in active-backup mode, we know that bond->current_slave is always valid if
 3261  * the bond has a usable interface.
 3262  */
 3263 static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *dev)
 3264 {
 3265         struct bonding *bond = (struct bonding *) dev->priv;
 3266         int ret;
 3267 
 3268         if (!IS_UP(dev)) { /* bond down */
 3269                 dev_kfree_skb(skb);
 3270                 return 0;
 3271         }
 3272 
 3273         /* if we are sending arp packets, try to at least 
 3274            identify our own ip address */
 3275         if ( (arp_interval > 0) && (my_ip == 0) &&
 3276                 (skb->protocol == __constant_htons(ETH_P_ARP) ) ) {
 3277                 char *the_ip = (((char *)skb->data)) 
 3278                                 + sizeof(struct ethhdr)  
 3279                                 + sizeof(struct arphdr) + 
 3280                                 ETH_ALEN;
 3281                 memcpy(&my_ip, the_ip, 4);
 3282         }
 3283 
 3284         /* if we are sending arp packets and don't know 
 3285          * the target hw address, save it so we don't need 
 3286          * to use a broadcast address.
 3287          * don't do this if in active backup mode because the slaves must 
 3288          * receive packets to stay up, and the only ones they receive are 
 3289          * broadcasts. 
 3290          */
 3291         if ( (bond_mode != BOND_MODE_ACTIVEBACKUP) && 
 3292              (arp_ip_count == 1) &&
 3293              (arp_interval > 0) && (arp_target_hw_addr == NULL) &&
 3294              (skb->protocol == __constant_htons(ETH_P_IP) ) ) {
 3295                 struct ethhdr *eth_hdr = 
 3296                         (struct ethhdr *) (((char *)skb->data));
 3297                 struct iphdr *ip_hdr = (struct iphdr *)(eth_hdr + 1);
 3298 
 3299                 if (arp_target[0] == ip_hdr->daddr) {
 3300                         arp_target_hw_addr = kmalloc(ETH_ALEN, GFP_KERNEL);
 3301                         if (arp_target_hw_addr != NULL)
 3302                                 memcpy(arp_target_hw_addr, eth_hdr->h_dest, ETH_ALEN);
 3303                 }
 3304         }
 3305 
 3306         read_lock(&bond->lock);
 3307 
 3308         read_lock(&bond->ptrlock);
 3309         if (bond->current_slave != NULL) { /* one usable interface */
 3310                 skb->dev = bond->current_slave->dev;
 3311                 read_unlock(&bond->ptrlock);
 3312                 skb->priority = 1;
 3313                 ret = dev_queue_xmit(skb);
 3314                 read_unlock(&bond->lock);
 3315                 return 0;
 3316         }
 3317         else {
 3318                 read_unlock(&bond->ptrlock);
 3319         }
 3320 
 3321         /* no suitable interface, frame not sent */
 3322 #ifdef BONDING_DEBUG
 3323         printk(KERN_INFO "There was no suitable interface, so we don't transmit\n");
 3324 #endif
 3325         dev_kfree_skb(skb);
 3326         read_unlock(&bond->lock);
 3327         return 0;
 3328 }
 3329 
 3330 static struct net_device_stats *bond_get_stats(struct net_device *dev)
 3331 {
 3332         bonding_t *bond = dev->priv;
 3333         struct net_device_stats *stats = bond->stats, *sstats;
 3334         slave_t *slave;
 3335 
 3336         memset(bond->stats, 0, sizeof(struct net_device_stats));
 3337 
 3338         read_lock_bh(&bond->lock);
 3339 
 3340         for (slave = bond->prev; slave != (slave_t *)bond; slave = slave->prev) {
 3341                 sstats = slave->dev->get_stats(slave->dev);
 3342  
 3343                 stats->rx_packets += sstats->rx_packets;
 3344                 stats->rx_bytes += sstats->rx_bytes;
 3345                 stats->rx_errors += sstats->rx_errors;
 3346                 stats->rx_dropped += sstats->rx_dropped;
 3347 
 3348                 stats->tx_packets += sstats->tx_packets;
 3349                 stats->tx_bytes += sstats->tx_bytes;
 3350                 stats->tx_errors += sstats->tx_errors;
 3351                 stats->tx_dropped += sstats->tx_dropped;
 3352 
 3353                 stats->multicast += sstats->multicast;
 3354                 stats->collisions += sstats->collisions;
 3355 
 3356                 stats->rx_length_errors += sstats->rx_length_errors;
 3357                 stats->rx_over_errors += sstats->rx_over_errors;
 3358                 stats->rx_crc_errors += sstats->rx_crc_errors;
 3359                 stats->rx_frame_errors += sstats->rx_frame_errors;
 3360                 stats->rx_fifo_errors += sstats->rx_fifo_errors;        
 3361                 stats->rx_missed_errors += sstats->rx_missed_errors;
 3362         
 3363                 stats->tx_aborted_errors += sstats->tx_aborted_errors;
 3364                 stats->tx_carrier_errors += sstats->tx_carrier_errors;
 3365                 stats->tx_fifo_errors += sstats->tx_fifo_errors;
 3366                 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
 3367                 stats->tx_window_errors += sstats->tx_window_errors;
 3368 
 3369         }
 3370 
 3371         read_unlock_bh(&bond->lock);
 3372         return stats;
 3373 }
 3374 
 3375 static int bond_get_info(char *buf, char **start, off_t offset, int length)
 3376 {
 3377         bonding_t *bond = these_bonds;
 3378         int len = 0;
 3379         off_t begin = 0;
 3380         u16 link;
 3381         slave_t *slave = NULL;
 3382 
 3383         len += sprintf(buf + len, "%s\n", version);
 3384 
 3385         while (bond != NULL) {
 3386                 /*
 3387                  * This function locks the mutex, so we can't lock it until 
 3388                  * afterwards
 3389                  */
 3390                 link = bond_check_mii_link(bond);
 3391 
 3392                 len += sprintf(buf + len, "Bonding Mode: %s\n",
 3393                                bond_mode_name());
 3394 
 3395                 if ((bond_mode == BOND_MODE_ACTIVEBACKUP) ||
 3396                     (bond_mode == BOND_MODE_TLB) ||
 3397                     (bond_mode == BOND_MODE_ALB)) {
 3398                         read_lock_bh(&bond->lock);
 3399                         read_lock(&bond->ptrlock);
 3400                         if (bond->current_slave != NULL) {
 3401                                 len += sprintf(buf + len, 
 3402                                         "Currently Active Slave: %s\n", 
 3403                                         bond->current_slave->dev->name);
 3404                         }
 3405                         read_unlock(&bond->ptrlock);
 3406                         read_unlock_bh(&bond->lock);
 3407                 }
 3408 
 3409                 len += sprintf(buf + len, "MII Status: ");
 3410                 len += sprintf(buf + len, 
 3411                                 link == BMSR_LSTATUS ? "up\n" : "down\n");
 3412                 len += sprintf(buf + len, "MII Polling Interval (ms): %d\n", 
 3413                                 miimon);
 3414                 len += sprintf(buf + len, "Up Delay (ms): %d\n", 
 3415                                 updelay * miimon);
 3416                 len += sprintf(buf + len, "Down Delay (ms): %d\n", 
 3417                                 downdelay * miimon);
 3418                 len += sprintf(buf + len, "Multicast Mode: %s\n",
 3419                                multicast_mode_name());
 3420 
 3421                 read_lock_bh(&bond->lock);
 3422 
 3423                 if (bond_mode == BOND_MODE_8023AD) {
 3424                         struct ad_info ad_info;
 3425 
 3426                         len += sprintf(buf + len, "\n802.3ad info\n");
 3427 
 3428                         if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
 3429                                 len += sprintf(buf + len, "bond %s has no active aggregator\n", bond->device->name);
 3430                         } else {
 3431                                 len += sprintf(buf + len, "Active Aggregator Info:\n");
 3432 
 3433                                 len += sprintf(buf + len, "\tAggregator ID: %d\n", ad_info.aggregator_id);
 3434                                 len += sprintf(buf + len, "\tNumber of ports: %d\n", ad_info.ports);
 3435                                 len += sprintf(buf + len, "\tActor Key: %d\n", ad_info.actor_key);
 3436                                 len += sprintf(buf + len, "\tPartner Key: %d\n", ad_info.partner_key);
 3437                                 len += sprintf(buf + len, "\tPartner Mac Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
 3438                                                ad_info.partner_system[0],
 3439                                                ad_info.partner_system[1],
 3440                                                ad_info.partner_system[2],
 3441                                                ad_info.partner_system[3],
 3442                                                ad_info.partner_system[4],
 3443                                                ad_info.partner_system[5]);
 3444                         }
 3445                 }
 3446 
 3447                 for (slave = bond->prev; slave != (slave_t *)bond; 
 3448                      slave = slave->prev) {
 3449                         len += sprintf(buf + len, "\nSlave Interface: %s\n", slave->dev->name);
 3450 
 3451                         len += sprintf(buf + len, "MII Status: ");
 3452 
 3453                         len += sprintf(buf + len, 
 3454                                 slave->link == BOND_LINK_UP ? 
 3455                                 "up\n" : "down\n");
 3456                         len += sprintf(buf + len, "Link Failure Count: %d\n", 
 3457                                 slave->link_failure_count);
 3458 
 3459                         if (app_abi_ver >= 1) {
 3460                                 len += sprintf(buf + len,
 3461                                                "Permanent HW addr: %02x:%02x:%02x:%02x:%02x:%02x\n",
 3462                                                slave->perm_hwaddr[0],
 3463                                                slave->perm_hwaddr[1],
 3464                                                slave->perm_hwaddr[2],
 3465                                                slave->perm_hwaddr[3],
 3466                                                slave->perm_hwaddr[4],
 3467                                                slave->perm_hwaddr[5]);
 3468                         }
 3469 
 3470                         if (bond_mode == BOND_MODE_8023AD) {
 3471                                 struct aggregator *agg = SLAVE_AD_INFO(slave).port.aggregator;
 3472 
 3473                                 if (agg) {
 3474                                         len += sprintf(buf + len, "Aggregator ID: %d\n",
 3475                                                        agg->aggregator_identifier);
 3476                                 } else {
 3477                                         len += sprintf(buf + len, "Aggregator ID: N/A\n");
 3478                                 }
 3479                         }
 3480                 }
 3481                 read_unlock_bh(&bond->lock);
 3482 
 3483                 /*
 3484                  * Figure out the calcs for the /proc/net interface
 3485                  */
 3486                 *start = buf + (offset - begin);
 3487                 len -= (offset - begin);
 3488                 if (len > length) {
 3489                         len = length;
 3490                 }
 3491                 if (len < 0) {
 3492                         len = 0;
 3493                 }
 3494 
 3495 
 3496                 bond = bond->next_bond;
 3497         }
 3498         return len;
 3499 }
 3500 
 3501 static int bond_event(struct notifier_block *this, unsigned long event, 
 3502                         void *ptr)
 3503 {
 3504         struct bonding *this_bond = (struct bonding *)these_bonds;
 3505         struct bonding *last_bond;
 3506         struct net_device *event_dev = (struct net_device *)ptr;
 3507 
 3508         /* while there are bonds configured */
 3509         while (this_bond != NULL) {
 3510                 if (this_bond == event_dev->priv ) {
 3511                         switch (event) {
 3512                         case NETDEV_UNREGISTER:
 3513                                 /* 
 3514                                  * remove this bond from a linked list of 
 3515                                  * bonds 
 3516                                  */
 3517                                 if (this_bond == these_bonds) {
 3518                                         these_bonds = this_bond->next_bond;
 3519                                 } else {
 3520                                         for (last_bond = these_bonds; 
 3521                                              last_bond != NULL; 
 3522                                              last_bond = last_bond->next_bond) {
 3523                                                 if (last_bond->next_bond == 
 3524                                                     this_bond) {
 3525                                                         last_bond->next_bond = 
 3526                                                         this_bond->next_bond;
 3527                                                 }
 3528                                         }
 3529                                 }
 3530                                 return NOTIFY_DONE;
 3531 
 3532                         default:
 3533                                 return NOTIFY_DONE;
 3534                         }
 3535                 } else if (this_bond->device == event_dev->master) {
 3536                         switch (event) {
 3537                         case NETDEV_UNREGISTER:
 3538                                 bond_release(this_bond->device, event_dev);
 3539                                 break;
 3540                         }
 3541                         return NOTIFY_DONE;
 3542                 }
 3543                 this_bond = this_bond->next_bond;
 3544         }
 3545         return NOTIFY_DONE;
 3546 }
 3547 
 3548 static struct notifier_block bond_netdev_notifier = {
 3549         notifier_call: bond_event,
 3550 };
 3551 
 3552 static int __init bond_init(struct net_device *dev)
 3553 {
 3554         bonding_t *bond, *this_bond, *last_bond;
 3555         int count;
 3556 
 3557 #ifdef BONDING_DEBUG
 3558         printk (KERN_INFO "Begin bond_init for %s\n", dev->name);
 3559 #endif
 3560         bond = kmalloc(sizeof(struct bonding), GFP_KERNEL);
 3561         if (bond == NULL) {
 3562                 return -ENOMEM;
 3563         }
 3564         memset(bond, 0, sizeof(struct bonding));
 3565 
 3566         /* initialize rwlocks */
 3567         rwlock_init(&bond->lock);
 3568         rwlock_init(&bond->ptrlock);
 3569         
 3570         bond->stats = kmalloc(sizeof(struct net_device_stats), GFP_KERNEL);
 3571         if (bond->stats == NULL) {
 3572                 kfree(bond);
 3573                 return -ENOMEM;
 3574         }
 3575         memset(bond->stats, 0, sizeof(struct net_device_stats));
 3576 
 3577         bond->next = bond->prev = (slave_t *)bond;
 3578         bond->current_slave = NULL;
 3579         bond->current_arp_slave = NULL;
 3580         bond->device = dev;
 3581         dev->priv = bond;
 3582 
 3583         /* Initialize the device structure. */
 3584         switch (bond_mode) {
 3585         case BOND_MODE_ACTIVEBACKUP:
 3586                 dev->hard_start_xmit = bond_xmit_activebackup;
 3587                 break;
 3588         case BOND_MODE_ROUNDROBIN:
 3589                 dev->hard_start_xmit = bond_xmit_roundrobin;
 3590                 break;
 3591         case BOND_MODE_XOR:
 3592                 dev->hard_start_xmit = bond_xmit_xor;
 3593                 break;
 3594         case BOND_MODE_BROADCAST:
 3595                 dev->hard_start_xmit = bond_xmit_broadcast;
 3596                 break;
 3597         case BOND_MODE_8023AD:
 3598                 dev->hard_start_xmit = bond_3ad_xmit_xor;
 3599                 break;
 3600         case BOND_MODE_TLB:
 3601         case BOND_MODE_ALB:
 3602                 dev->hard_start_xmit = bond_alb_xmit;
 3603                 break;
 3604         default:
 3605                 printk(KERN_ERR "Unknown bonding mode %d\n", bond_mode);
 3606                 kfree(bond->stats);
 3607                 kfree(bond);
 3608                 return -EINVAL;
 3609         }
 3610 
 3611         dev->get_stats = bond_get_stats;
 3612         dev->open = bond_open;
 3613         dev->stop = bond_close;
 3614         dev->set_multicast_list = set_multicast_list;
 3615         dev->do_ioctl = bond_ioctl;
 3616 
 3617         /* 
 3618          * Fill in the fields of the device structure with ethernet-generic 
 3619          * values. 
 3620          */
 3621 
 3622         ether_setup(dev);
 3623 
 3624         dev->set_mac_address = bond_set_mac_address;
 3625         dev->tx_queue_len = 0;
 3626         dev->flags |= IFF_MASTER|IFF_MULTICAST;
 3627 #ifdef CONFIG_NET_FASTROUTE
 3628         dev->accept_fastpath = bond_accept_fastpath;
 3629 #endif
 3630 
 3631         printk(KERN_INFO "%s registered with", dev->name);
 3632         if (miimon > 0) {
 3633                 printk(" MII link monitoring set to %d ms", miimon);
 3634                 updelay /= miimon;
 3635                 downdelay /= miimon;
 3636         } else {
 3637                 printk("out MII link monitoring");
 3638         }
 3639         printk(", in %s mode.\n", bond_mode_name());
 3640 
 3641         printk(KERN_INFO "%s registered with", dev->name);
 3642         if (arp_interval > 0) {
 3643                 printk(" ARP monitoring set to %d ms with %d target(s):", 
 3644                         arp_interval, arp_ip_count);
 3645                 for (count=0 ; count<arp_ip_count ; count++)
 3646                         printk (" %s", arp_ip_target[count]);
 3647                 printk("\n");
 3648         } else {
 3649                 printk("out ARP monitoring\n");
 3650         }
 3651 
 3652 #ifdef CONFIG_PROC_FS
 3653         bond->bond_proc_dir = proc_mkdir(dev->name, proc_net);
 3654         if (bond->bond_proc_dir == NULL) {
 3655                 printk(KERN_ERR "%s: Cannot init /proc/net/%s/\n", 
 3656                         dev->name, dev->name);
 3657                 kfree(bond->stats);
 3658                 kfree(bond);
 3659                 return -ENOMEM;
 3660         }
 3661         bond->bond_proc_info_file = 
 3662                 create_proc_info_entry("info", 0, bond->bond_proc_dir, 
 3663                                         bond_get_info);
 3664         if (bond->bond_proc_info_file == NULL) {
 3665                 printk(KERN_ERR "%s: Cannot init /proc/net/%s/info\n", 
 3666                         dev->name, dev->name);
 3667                 remove_proc_entry(dev->name, proc_net);
 3668                 kfree(bond->stats);
 3669                 kfree(bond);
 3670                 return -ENOMEM;
 3671         }
 3672 #endif /* CONFIG_PROC_FS */
 3673 
 3674         if (first_pass == 1) {
 3675                 these_bonds = bond;
 3676                 register_netdevice_notifier(&bond_netdev_notifier);
 3677                 first_pass = 0;
 3678         } else {
 3679                 last_bond = these_bonds;
 3680                 this_bond = these_bonds->next_bond;
 3681                 while (this_bond != NULL) {
 3682                         last_bond = this_bond;
 3683                         this_bond = this_bond->next_bond;
 3684                 }
 3685                 last_bond->next_bond = bond;
 3686         } 
 3687 
 3688         return 0;
 3689 }
 3690 
 3691 /*
 3692 static int __init bond_probe(struct net_device *dev)
 3693 {
 3694         bond_init(dev);
 3695         return 0;
 3696 }
 3697  */
 3698 
 3699 /*
 3700  * Convert string input module parms.  Accept either the
 3701  * number of the mode or its string name.
 3702  */
 3703 static inline int
 3704 bond_parse_parm(char *mode_arg, struct bond_parm_tbl *tbl)
 3705 {
 3706         int i;
 3707 
 3708         for (i = 0; tbl[i].modename != NULL; i++) {
 3709                 if ((isdigit(*mode_arg) &&
 3710                     tbl[i].mode == simple_strtol(mode_arg, NULL, 0)) ||
 3711                     (0 == strncmp(mode_arg, tbl[i].modename,
 3712                                   strlen(tbl[i].modename)))) {
 3713                         return tbl[i].mode;
 3714                 }
 3715         }
 3716 
 3717         return -1;
 3718 }
 3719 
 3720 
 3721 static int __init bonding_init(void)
 3722 {
 3723         int no;
 3724         int err;
 3725 
 3726         /* Find a name for this unit */
 3727         static struct net_device *dev_bond = NULL;
 3728 
 3729         printk(KERN_INFO "%s", version);
 3730 
 3731         /*
 3732          * Convert string parameters.
 3733          */
 3734         if (mode) {
 3735                 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
 3736                 if (bond_mode == -1) {
 3737                         printk(KERN_WARNING
 3738                                "bonding_init(): Invalid bonding mode \"%s\"\n",
 3739                                mode == NULL ? "NULL" : mode);
 3740                         return -EINVAL;
 3741                 }
 3742         }
 3743 
 3744         if (multicast) {
 3745                 multicast_mode = bond_parse_parm(multicast, bond_mc_tbl);
 3746                 if (multicast_mode == -1) {
 3747                         printk(KERN_WARNING 
 3748                        "bonding_init(): Invalid multicast mode \"%s\"\n",
 3749                                multicast == NULL ? "NULL" : multicast);
 3750                         return -EINVAL;
 3751                 }
 3752         }
 3753 
 3754         if (lacp_rate) {
 3755                 if (bond_mode != BOND_MODE_8023AD) {
 3756                         printk(KERN_WARNING
 3757                                "lacp_rate param is irrelevant in mode %s\n",
 3758                                bond_mode_name());
 3759                 } else {
 3760                         lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
 3761                         if (lacp_fast == -1) {
 3762                                 printk(KERN_WARNING
 3763                                        "bonding_init(): Invalid lacp rate "
 3764                                        "\"%s\"\n",
 3765                                        lacp_rate == NULL ? "NULL" : lacp_rate);
 3766 
 3767                                 return -EINVAL;
 3768                         }
 3769                 }
 3770         }
 3771 
 3772         if (max_bonds < 1 || max_bonds > INT_MAX) {
 3773                 printk(KERN_WARNING 
 3774                        "bonding_init(): max_bonds (%d) not in range %d-%d, "
 3775                        "so it was reset to BOND_DEFAULT_MAX_BONDS (%d)",
 3776                        max_bonds, 1, INT_MAX, BOND_DEFAULT_MAX_BONDS);
 3777                 max_bonds = BOND_DEFAULT_MAX_BONDS;
 3778         }
 3779         dev_bond = dev_bonds = kmalloc(max_bonds*sizeof(struct net_device), 
 3780                                         GFP_KERNEL);
 3781         if (dev_bond == NULL) {
 3782                 return -ENOMEM;
 3783         }
 3784         memset(dev_bonds, 0, max_bonds*sizeof(struct net_device));
 3785 
 3786         if (miimon < 0) {
 3787                 printk(KERN_WARNING 
 3788                        "bonding_init(): miimon module parameter (%d), "
 3789                        "not in range 0-%d, so it was reset to %d\n",
 3790                        miimon, INT_MAX, BOND_LINK_MON_INTERV);
 3791                 miimon = BOND_LINK_MON_INTERV;
 3792         }
 3793 
 3794         if (updelay < 0) {
 3795                 printk(KERN_WARNING 
 3796                        "bonding_init(): updelay module parameter (%d), "
 3797                        "not in range 0-%d, so it was reset to 0\n",
 3798                        updelay, INT_MAX);
 3799                 updelay = 0;
 3800         }
 3801 
 3802         if (downdelay < 0) {
 3803                 printk(KERN_WARNING 
 3804                        "bonding_init(): downdelay module parameter (%d), "
 3805                        "not in range 0-%d, so it was reset to 0\n",
 3806                        downdelay, INT_MAX);
 3807                 downdelay = 0;
 3808         }
 3809 
 3810         /* reset values for 802.3ad */
 3811         if (bond_mode == BOND_MODE_8023AD) {
 3812                 if (arp_interval != 0) {
 3813                         printk(KERN_WARNING "bonding_init(): ARP monitoring"
 3814                                "can't be used simultaneously with 802.3ad, "
 3815                                "disabling ARP monitoring\n");
 3816                         arp_interval = 0;
 3817                 }
 3818 
 3819                 if (miimon == 0) {
 3820                         printk(KERN_ERR
 3821                                "bonding_init(): miimon must be specified, "
 3822                                "otherwise bonding will not detect link failure, "
 3823                                "speed and duplex which are essential "
 3824                                "for 802.3ad operation\n");
 3825                         printk(KERN_ERR "Forcing miimon to 100msec\n");
 3826                         miimon = 100;
 3827                 }
 3828 
 3829                 if (multicast_mode != BOND_MULTICAST_ALL) {
 3830                         printk(KERN_ERR
 3831                                "bonding_init(): Multicast mode must "
 3832                                "be set to ALL for 802.3ad\n");
 3833                         printk(KERN_ERR "Forcing Multicast mode to ALL\n");
 3834                         multicast_mode = BOND_MULTICAST_ALL;
 3835                 }
 3836         }
 3837 
 3838         /* reset values for TLB/ALB */
 3839         if ((bond_mode == BOND_MODE_TLB) ||
 3840             (bond_mode == BOND_MODE_ALB)) {
 3841                 if (miimon == 0) {
 3842                         printk(KERN_ERR
 3843                                "bonding_init(): miimon must be specified, "
 3844                                "otherwise bonding will not detect link failure "
 3845                                "and link speed which are essential "
 3846                                "for TLB/ALB load balancing\n");
 3847                         printk(KERN_ERR "Forcing miimon to 100msec\n");
 3848                         miimon = 100;
 3849                 }
 3850 
 3851                 if (multicast_mode != BOND_MULTICAST_ACTIVE) {
 3852                         printk(KERN_ERR
 3853                                "bonding_init(): Multicast mode must "
 3854                                "be set to ACTIVE for TLB/ALB\n");
 3855                         printk(KERN_ERR "Forcing Multicast mode to ACTIVE\n");
 3856                         multicast_mode = BOND_MULTICAST_ACTIVE;
 3857                 }
 3858         }
 3859 
 3860         if (bond_mode == BOND_MODE_ALB) {
 3861                 printk(KERN_INFO
 3862                        "In ALB mode you might experience client disconnections"
 3863                        " upon reconnection of a link if the bonding module"
 3864                        " updelay parameter (%d msec) is incompatible with the"
 3865                        " forwarding delay time of the switch\n", updelay);
 3866         }
 3867 
 3868         if (miimon == 0) {
 3869                 if ((updelay != 0) || (downdelay != 0)) {
 3870                         /* just warn the user the up/down delay will have
 3871                          * no effect since miimon is zero...
 3872                          */
 3873                         printk(KERN_WARNING 
 3874                                "bonding_init(): miimon module parameter not "
 3875                                "set and updelay (%d) or downdelay (%d) module "
 3876                                "parameter is set; updelay and downdelay have "
 3877                                "no effect unless miimon is set\n",
 3878                                updelay, downdelay);
 3879                 }
 3880         } else {
 3881                 /* don't allow arp monitoring */
 3882                 if (arp_interval != 0) {
 3883                         printk(KERN_WARNING 
 3884                                "bonding_init(): miimon (%d) and arp_interval "
 3885                                "(%d) can't be used simultaneously, "
 3886                                "disabling ARP monitoring\n",
 3887                                miimon, arp_interval);
 3888                         arp_interval = 0;
 3889                 }
 3890 
 3891                 if ((updelay % miimon) != 0) {
 3892                         /* updelay will be rounded in bond_init() when it
 3893                          * is divided by miimon, we just inform user here
 3894                          */
 3895                         printk(KERN_WARNING 
 3896                                "bonding_init(): updelay (%d) is not a multiple "
 3897                                "of miimon (%d), updelay rounded to %d ms\n",
 3898                                updelay, miimon, (updelay / miimon) * miimon);
 3899                 }
 3900 
 3901                 if ((downdelay % miimon) != 0) {
 3902                         /* downdelay will be rounded in bond_init() when it
 3903                          * is divided by miimon, we just inform user here
 3904                          */
 3905                         printk(KERN_WARNING 
 3906                                "bonding_init(): downdelay (%d) is not a "
 3907                                "multiple of miimon (%d), downdelay rounded "
 3908                                "to %d ms\n",
 3909                                downdelay, miimon, 
 3910                                (downdelay / miimon) * miimon);
 3911                 }
 3912         }
 3913 
 3914         if (arp_interval < 0) {
 3915                 printk(KERN_WARNING 
 3916                        "bonding_init(): arp_interval module parameter (%d), "
 3917                        "not in range 0-%d, so it was reset to %d\n",
 3918                        arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
 3919                 arp_interval = BOND_LINK_ARP_INTERV;
 3920         }
 3921 
 3922         for (arp_ip_count=0 ;
 3923              (arp_ip_count < MAX_ARP_IP_TARGETS) && arp_ip_target[arp_ip_count];
 3924               arp_ip_count++ ) {
 3925                 /* not complete check, but should be good enough to
 3926                    catch mistakes */
 3927                 if (!isdigit(arp_ip_target[arp_ip_count][0])) { 
 3928                         printk(KERN_WARNING
 3929                                "bonding_init(): bad arp_ip_target module "
 3930                                "parameter (%s), ARP monitoring will not be "
 3931                                "performed\n",
 3932                                arp_ip_target[arp_ip_count]);
 3933                         arp_interval = 0;
 3934                 } else { 
 3935                         u32 ip = in_aton(arp_ip_target[arp_ip_count]); 
 3936                         arp_target[arp_ip_count] = ip;
 3937                 }
 3938         }
 3939 
 3940 
 3941         if ( (arp_interval > 0) && (arp_ip_count==0)) {
 3942                 /* don't allow arping if no arp_ip_target given... */
 3943                 printk(KERN_WARNING 
 3944                        "bonding_init(): arp_interval module parameter "
 3945                        "(%d) specified without providing an arp_ip_target "
 3946                        "parameter, arp_interval was reset to 0\n",
 3947                        arp_interval);
 3948                 arp_interval = 0;
 3949         }
 3950 
 3951         if ((miimon == 0) && (arp_interval == 0)) {
 3952                 /* miimon and arp_interval not set, we need one so things
 3953                  * work as expected, see bonding.txt for details
 3954                  */
 3955                 printk(KERN_ERR 
 3956                        "bonding_init(): either miimon or "
 3957                        "arp_interval and arp_ip_target module parameters "
 3958                        "must be specified, otherwise bonding will not detect "
 3959                        "link failures! see bonding.txt for details.\n");
 3960         }
 3961 
 3962         if ((primary != NULL) && (bond_mode != BOND_MODE_ACTIVEBACKUP) &&
 3963             (bond_mode != BOND_MODE_TLB) &&
 3964             (bond_mode != BOND_MODE_ALB)){
 3965                 /* currently, using a primary only makes sense
 3966                  * in active backup, TLB or ALB modes
 3967                  */
 3968                 printk(KERN_WARNING 
 3969                        "bonding_init(): %s primary device specified but has "
 3970                        "no effect in %s mode\n",
 3971                        primary, bond_mode_name());
 3972                 primary = NULL;
 3973         }
 3974 
 3975 
 3976         for (no = 0; no < max_bonds; no++) {
 3977                 dev_bond->init = bond_init;
 3978         
 3979                 err = dev_alloc_name(dev_bond,"bond%d");
 3980                 if (err < 0) {
 3981                         kfree(dev_bonds);
 3982                         return err;
 3983                 }
 3984                 SET_MODULE_OWNER(dev_bond);
 3985                 if (register_netdev(dev_bond) != 0) {
 3986                         kfree(dev_bonds);
 3987                         return -EIO;
 3988                 }       
 3989                 dev_bond++;
 3990         }
 3991         return 0;
 3992 }
 3993 
 3994 static void __exit bonding_exit(void)
 3995 {
 3996         struct net_device *dev_bond = dev_bonds;
 3997         struct bonding *bond;
 3998         int no;
 3999 
 4000         unregister_netdevice_notifier(&bond_netdev_notifier);
 4001                  
 4002         for (no = 0; no < max_bonds; no++) {
 4003 
 4004 #ifdef CONFIG_PROC_FS
 4005                 bond = (struct bonding *) dev_bond->priv;
 4006                 remove_proc_entry("info", bond->bond_proc_dir);
 4007                 remove_proc_entry(dev_bond->name, proc_net);
 4008 #endif
 4009                 unregister_netdev(dev_bond);
 4010                 kfree(bond->stats);
 4011                 kfree(dev_bond->priv);
 4012                 
 4013                 dev_bond->priv = NULL;
 4014                 dev_bond++;
 4015         }
 4016         kfree(dev_bonds);
 4017 }
 4018 
 4019 module_init(bonding_init);
 4020 module_exit(bonding_exit);
 4021 MODULE_LICENSE("GPL");
 4022 MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
 4023 
 4024 /*
 4025  * Local variables:
 4026  *  c-indent-level: 8
 4027  *  c-basic-offset: 8
 4028  *  tab-width: 8
 4029  * End:
 4030  */

Cache object: 5efea603a3f77a9aa528e22822972c06


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.