2 * originally based on the dummy device.
4 * Copyright 1999, Thomas Davis, tadavis@lbl.gov.
5 * Licensed under the GPL. Based on dummy.c, and eql.c devices.
7 * bonding.c: an Ethernet Bonding driver
9 * This is useful to talk to a Cisco EtherChannel compatible equipment:
11 * Sun Trunking (Solaris)
12 * Alteon AceDirector Trunks
14 * and probably many L2 switches ...
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.
24 * will release all slaves, marking them as down.
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.
32 * v0.1 - first working version.
33 * v0.2 - changed stats to be calculated by summing slaves stats.
36 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
37 * - fix leaks on failure at bond_init
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
48 * - made the code hopefully SMP safe
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
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
65 * - smarter enslaving and releasing.
66 * - finer and more robust SMP locking
68 * 2000/10/17 - Willy Tarreau <willy at meta-x.org>
69 * - fixed two potential SMP race conditions
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().
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.
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.
94 * 2001/4/5 - Chad N. Tindel <ctindel at ieee dot org>
95 * - Ported to 2.4 Kernel
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.
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
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.
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
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.
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.
132 * 2001/8/13 - Erik Habbinga <erik_habbinga at hp dot com>
133 * - correct locking problem with rtnl_exlock_nowait
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
139 * 2001/9/13 - Takao Indoh <indou dot takao at jp dot fujitsu dot com>
140 * - Add the BOND_CHANGE_ACTIVE ioctl implementation
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.
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
151 * 2001/9/25 - Janice Girouard <girouard at us.ibm.com>
152 * - add arp_monitor for active backup mode
154 * 2001/10/23 - Takao Indoh <indou dot takao at jp dot fujitsu dot com>
155 * - Various memory leak fixes
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
165 * 2002/1/2 - Chad N. Tindel <ctindel at ieee dot org>
166 * - Restore original slave flags at release time.
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
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.
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)
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().
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
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
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.
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.
214 * 2002/09/11 - Philippe De Muyter <phdm at macqel dot be>
215 * - Added bond_xmit_broadcast logic.
216 * - Added bond_mode() support function.
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
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.
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
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
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
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
254 * 2002/11/17 - Laurent Deniel <laurent.deniel at free.fr>
255 * - fix bond_slave_info_query when slave_id = num_slaves
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.
261 * 2002/11/22 - Bert Barbe <bert.barbe at oracle dot com>
262 * - Add support for multiple arp_ip_target
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
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.
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
280 * 2003/03/17 - Jay Vosburgh <fubar at us dot ibm dot com>
281 * - kmalloc fix (GFP_KERNEL to GFP_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.
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.
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.
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.
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
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
320 * - Block possibility of enslaving before the master is up. This
321 * prevents putting the system in an unstable state.
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.
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.
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.
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
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
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
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
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
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
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.
384 * 2003/05/22 - Jay Vosburgh <fubar at us dot ibm dot com>
385 * - Fix ifenslave -c causing bond to loose existing routes;
386 * added bond_set_mac_address() that doesn't require the
388 * - In conjunction with fix for ifenslave -c, in
389 * bond_change_active(), changing to the already active slave
390 * is no longer an error (it successfully does nothing).
392 * 2003/06/30 - Amir Noam <amir.noam at intel dot com>
393 * - Fixed bond_change_active() for ALB/TLB modes.
396 * 2003/07/29 - Amir Noam <amir.noam at intel dot com>
397 * - Fixed ARP monitoring bug.
400 * 2003/07/31 - Willy Tarreau <willy at ods dot org>
401 * - Fixed kernel panic when using ARP monitoring without
402 * setting bond's IP address.
405 * 2003/08/06 - Amir Noam <amir.noam at intel dot com>
406 * - Back port from 2.6: use alloc_netdev(); fix /proc handling;
407 * made stats a part of bond struct so no need to allocate
408 * and free it separately; use standard list operations instead
409 * of pre-allocated array of bonds.
412 * 2003/08/07 - Jay Vosburgh <fubar at us dot ibm dot com>,
413 * Amir Noam <amir.noam at intel dot com> and
414 * Shmulik Hen <shmulik.hen at intel dot com>
415 * - Propagating master's settings: Distinguish between modes that
416 * use a primary slave from those that don't, and propagate settings
417 * accordingly; Consolidate change_active opeartions and add
418 * reselect_active and find_best opeartions; Decouple promiscuous
419 * handling from the multicast mode setting; Add support for changing
420 * HW address and MTU with proper unwind; Consolidate procfs code,
421 * add CHANGENAME handler; Enhance netdev notification handling.
424 * 2003/09/15 - Stephen Hemminger <shemminger at osdl dot org>,
425 * Amir Noam <amir.noam at intel dot com>
426 * - Convert /proc to seq_file interface.
427 * Change /proc/net/bondX/info to /proc/net/bonding/bondX.
428 * Set version to 2.4.1.
431 #include <linux/config.h>
432 #include <linux/kernel.h>
433 #include <linux/module.h>
434 #include <linux/sched.h>
435 #include <linux/types.h>
436 #include <linux/fcntl.h>
437 #include <linux/interrupt.h>
438 #include <linux/ptrace.h>
439 #include <linux/ioport.h>
440 #include <linux/in.h>
441 #include <linux/ip.h>
442 #include <linux/slab.h>
443 #include <linux/string.h>
444 #include <linux/init.h>
445 #include <linux/timer.h>
446 #include <linux/socket.h>
447 #include <linux/ctype.h>
448 #include <linux/inet.h>
449 #include <asm/system.h>
450 #include <asm/bitops.h>
453 #include <asm/uaccess.h>
454 #include <linux/errno.h>
456 #include <linux/netdevice.h>
457 #include <linux/inetdevice.h>
458 #include <linux/etherdevice.h>
459 #include <linux/skbuff.h>
460 #include <net/sock.h>
461 #include <linux/rtnetlink.h>
462 #include <linux/proc_fs.h>
463 #include <linux/seq_file.h>
465 #include <linux/if_bonding.h>
466 #include <linux/smp.h>
467 #include <linux/if_ether.h>
469 #include <linux/mii.h>
470 #include <linux/ethtool.h>
472 #include "bond_3ad.h"
473 #include "bond_alb.h"
475 #define DRV_VERSION "2.4.1"
476 #define DRV_RELDATE "September 15, 2003"
477 #define DRV_NAME "bonding"
478 #define DRV_DESCRIPTION "Ethernet Channel Bonding Driver"
480 static const char *version =
481 DRV_NAME ".c:v" DRV_VERSION " (" DRV_RELDATE ")\n";
483 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
484 #ifndef BOND_LINK_MON_INTERV
485 #define BOND_LINK_MON_INTERV 0
488 #ifndef BOND_LINK_ARP_INTERV
489 #define BOND_LINK_ARP_INTERV 0
492 #ifndef MAX_ARP_IP_TARGETS
493 #define MAX_ARP_IP_TARGETS 16
496 #define USES_PRIMARY(mode) \
497 (((mode) == BOND_MODE_ACTIVEBACKUP) || \
498 ((mode) == BOND_MODE_TLB) || \
499 ((mode) == BOND_MODE_ALB))
501 struct bond_parm_tbl {
506 static int arp_interval = BOND_LINK_ARP_INTERV;
507 static char *arp_ip_target[MAX_ARP_IP_TARGETS] = { NULL, };
508 static u32 arp_target[MAX_ARP_IP_TARGETS] = { 0, } ;
509 static int arp_ip_count = 0;
510 static u32 my_ip = 0;
511 char *arp_target_hw_addr = NULL;
513 static char *primary= NULL;
515 static int app_abi_ver = 0;
516 static int orig_app_abi_ver = -1; /* This is used to save the first ABI version
517 * we receive from the application. Once set,
518 * it won't be changed, and the module will
519 * refuse to enslave/release interfaces if the
520 * command comes from an application using
521 * another ABI version.
524 static int max_bonds = BOND_DEFAULT_MAX_BONDS;
525 static int miimon = BOND_LINK_MON_INTERV;
526 static int use_carrier = 1;
527 static int bond_mode = BOND_MODE_ROUNDROBIN;
528 static int updelay = 0;
529 static int downdelay = 0;
531 static char *mode = NULL;
533 static struct bond_parm_tbl bond_mode_tbl[] = {
534 { "balance-rr", BOND_MODE_ROUNDROBIN},
535 { "active-backup", BOND_MODE_ACTIVEBACKUP},
536 { "balance-xor", BOND_MODE_XOR},
537 { "broadcast", BOND_MODE_BROADCAST},
538 { "802.3ad", BOND_MODE_8023AD},
539 { "balance-tlb", BOND_MODE_TLB},
540 { "balance-alb", BOND_MODE_ALB},
544 static int multicast_mode = BOND_MULTICAST_ALL;
545 static char *multicast = NULL;
547 static struct bond_parm_tbl bond_mc_tbl[] = {
548 { "disabled", BOND_MULTICAST_DISABLED},
549 { "active", BOND_MULTICAST_ACTIVE},
550 { "all", BOND_MULTICAST_ALL},
554 static int lacp_fast = 0;
555 static char *lacp_rate = NULL;
557 static struct bond_parm_tbl bond_lacp_tbl[] = {
558 { "slow", AD_LACP_SLOW},
559 { "fast", AD_LACP_FAST},
563 static LIST_HEAD(bond_dev_list);
564 #ifdef CONFIG_PROC_FS
565 static struct proc_dir_entry *bond_proc_dir = NULL;
568 MODULE_PARM(max_bonds, "i");
569 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
570 MODULE_PARM(miimon, "i");
571 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
572 MODULE_PARM(use_carrier, "i");
573 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; 0 for off, 1 for on (default)");
574 MODULE_PARM(mode, "s");
575 MODULE_PARM_DESC(mode, "Mode of operation : 0 for round robin, 1 for active-backup, 2 for xor");
576 MODULE_PARM(arp_interval, "i");
577 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
578 MODULE_PARM(arp_ip_target, "1-" __MODULE_STRING(MAX_ARP_IP_TARGETS) "s");
579 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
580 MODULE_PARM(updelay, "i");
581 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
582 MODULE_PARM(downdelay, "i");
583 MODULE_PARM_DESC(downdelay, "Delay before considering link down, in milliseconds");
584 MODULE_PARM(primary, "s");
585 MODULE_PARM_DESC(primary, "Primary network device to use");
586 MODULE_PARM(multicast, "s");
587 MODULE_PARM_DESC(multicast, "Mode for multicast support : 0 for none, 1 for active slave, 2 for all slaves (default)");
588 MODULE_PARM(lacp_rate, "s");
589 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner (slow/fast)");
591 static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *dev);
592 static int bond_xmit_xor(struct sk_buff *skb, struct net_device *dev);
593 static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *dev);
594 static struct net_device_stats *bond_get_stats(struct net_device *dev);
595 static void bond_mii_monitor(struct net_device *dev);
596 static void loadbalance_arp_monitor(struct net_device *dev);
597 static void activebackup_arp_monitor(struct net_device *dev);
598 static void bond_mc_list_destroy(struct bonding *bond);
599 static void bond_mc_add(bonding_t *bond, void *addr, int alen);
600 static void bond_mc_delete(bonding_t *bond, void *addr, int alen);
601 static int bond_mc_list_copy (struct dev_mc_list *src, struct bonding *dst, int gpf_flag);
602 static inline int dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2);
603 static void bond_set_promiscuity(bonding_t *bond, int inc);
604 static void bond_set_allmulti(bonding_t *bond, int inc);
605 static struct dev_mc_list* bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list);
606 static void bond_mc_update(bonding_t *bond, slave_t *new, slave_t *old);
607 static int bond_enslave(struct net_device *master, struct net_device *slave);
608 static int bond_release(struct net_device *master, struct net_device *slave);
609 static int bond_release_all(struct net_device *master);
610 static int bond_sethwaddr(struct net_device *master, struct net_device *slave);
611 static void change_active_interface(struct bonding *bond, struct slave *new);
612 static void reselect_active_interface(struct bonding *bond);
613 static struct slave *find_best_interface(struct bonding *bond);
616 /* #define BONDING_DEBUG 1 */
618 #define dprintk(x...) printk(x...)
619 #else /* BONDING_DEBUG */
620 #define dprintk(x...) do {} while (0)
621 #endif /* BONDING_DEBUG */
625 static void arp_send_all(slave_t *slave)
629 for (i = 0; (i<MAX_ARP_IP_TARGETS) && arp_target[i]; i++) {
630 arp_send(ARPOP_REQUEST, ETH_P_ARP, arp_target[i], slave->dev,
631 my_ip, arp_target_hw_addr, slave->dev->dev_addr,
641 case BOND_MODE_ROUNDROBIN :
642 return "load balancing (round-robin)";
643 case BOND_MODE_ACTIVEBACKUP :
644 return "fault-tolerance (active-backup)";
646 return "load balancing (xor)";
647 case BOND_MODE_BROADCAST :
648 return "fault-tolerance (broadcast)";
649 case BOND_MODE_8023AD:
650 return "IEEE 802.3ad Dynamic link aggregation";
652 return "transmit load balancing";
654 return "adaptive load balancing";
661 multicast_mode_name(void)
663 switch(multicast_mode) {
664 case BOND_MULTICAST_DISABLED :
666 case BOND_MULTICAST_ACTIVE :
667 return "active slave only";
668 case BOND_MULTICAST_ALL :
675 void bond_set_slave_inactive_flags(slave_t *slave)
677 slave->state = BOND_STATE_BACKUP;
678 slave->dev->flags |= IFF_NOARP;
681 void bond_set_slave_active_flags(slave_t *slave)
683 slave->state = BOND_STATE_ACTIVE;
684 slave->dev->flags &= ~IFF_NOARP;
688 * This function counts and verifies the the number of attached
689 * slaves, checking the count against the expected value (given that incr
690 * is either 1 or -1, for add or removal of a slave). Only
691 * bond_xmit_xor() uses the slave_cnt value, but this is still a good
695 update_slave_cnt(bonding_t *bond, int incr)
697 slave_t *slave = NULL;
698 int expect = bond->slave_cnt + incr;
701 for (slave = bond->prev; slave != (slave_t*)bond;
702 slave = slave->prev) {
706 if (expect != bond->slave_cnt)
711 * This function detaches the slave <slave> from the list <bond>.
712 * WARNING: no check is made to verify if the slave effectively
713 * belongs to <bond>. It returns <slave> in case it's needed.
714 * Nothing is freed on return, structures are just unchained.
715 * If the bond->current_slave pointer was pointing to <slave>,
716 * it should be changed by the calling function.
718 * bond->lock held for writing by caller.
721 bond_detach_slave(bonding_t *bond, slave_t *slave)
723 if ((bond == NULL) || (slave == NULL) ||
724 ((void *)bond == (void *)slave)) {
726 "bond_detach_slave(): trying to detach "
727 "slave %p from bond %p\n", bond, slave);
731 if (bond->next == slave) { /* is the slave at the head ? */
732 if (bond->prev == slave) { /* is the slave alone ? */
733 bond->prev = bond->next = (slave_t *)bond;
734 } else { /* not alone */
735 bond->next = slave->next;
736 slave->next->prev = (slave_t *)bond;
737 bond->prev->next = slave->next;
740 slave->prev->next = slave->next;
741 if (bond->prev == slave) { /* is this slave the last one ? */
742 bond->prev = slave->prev;
744 slave->next->prev = slave->prev;
748 update_slave_cnt(bond, -1);
754 * This function attaches the slave <slave> to the list <bond>.
756 * bond->lock held for writing by caller.
759 bond_attach_slave(struct bonding *bond, struct slave *new_slave)
762 * queue to the end of the slaves list, make the first element its
763 * successor, the last one its predecessor, and make it the bond's
766 * Just to clarify, so future bonding driver hackers don't go through
767 * the same confusion stage I did trying to figure this out, the
768 * slaves are stored in a double linked circular list, sortof.
769 * In the ->next direction, the last slave points to the first slave,
770 * bypassing bond; only the slaves are in the ->next direction.
771 * In the ->prev direction, however, the first slave points to bond
772 * and bond points to the last slave.
774 * It looks like a circle with a little bubble hanging off one side
775 * in the ->prev direction only.
777 * When going through the list once, its best to start at bond->prev
778 * and go in the ->prev direction, testing for bond. Doing this
779 * in the ->next direction doesn't work. Trust me, I know this now.
782 new_slave->prev = bond->prev;
783 new_slave->prev->next = new_slave;
784 bond->prev = new_slave;
785 new_slave->next = bond->next;
787 update_slave_cnt(bond, 1);
792 * Less bad way to call ioctl from within the kernel; this needs to be
793 * done some other way to get the call out of interrupt context.
794 * Needs "ioctl" variable to be supplied by calling context.
796 #define IOCTL(dev, arg, cmd) ({ \
798 mm_segment_t fs = get_fs(); \
800 ret = ioctl(dev, arg, cmd); \
805 * Get link speed and duplex from the slave's base driver
806 * using ethtool. If for some reason the call fails or the
807 * values are invalid, fake speed and duplex to 100/Full
810 static int bond_update_speed_duplex(struct slave *slave)
812 struct net_device *dev = slave->dev;
813 static int (* ioctl)(struct net_device *, struct ifreq *, int);
815 struct ethtool_cmd etool;
817 ioctl = dev->do_ioctl;
819 etool.cmd = ETHTOOL_GSET;
820 ifr.ifr_data = (char*)&etool;
821 if (IOCTL(dev, &ifr, SIOCETHTOOL) == 0) {
822 slave->speed = etool.speed;
823 slave->duplex = etool.duplex;
831 switch (slave->speed) {
840 switch (slave->duplex) {
851 /* Fake speed and duplex */
852 slave->speed = SPEED_100;
853 slave->duplex = DUPLEX_FULL;
858 * if <dev> supports MII link status reporting, check its link status.
860 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
861 * depening upon the setting of the use_carrier parameter.
863 * Return either BMSR_LSTATUS, meaning that the link is up (or we
864 * can't tell and just pretend it is), or 0, meaning that the link is
867 * If reporting is non-zero, instead of faking link up, return -1 if
868 * both ETHTOOL and MII ioctls fail (meaning the device does not
869 * support them). If use_carrier is set, return whatever it says.
870 * It'd be nice if there was a good way to tell if a driver supports
871 * netif_carrier, but there really isn't.
874 bond_check_dev_link(struct net_device *dev, int reporting)
876 static int (* ioctl)(struct net_device *, struct ifreq *, int);
878 struct mii_ioctl_data *mii;
879 struct ethtool_value etool;
882 return netif_carrier_ok(dev) ? BMSR_LSTATUS : 0;
885 ioctl = dev->do_ioctl;
887 /* TODO: set pointer to correct ioctl on a per team member */
888 /* bases to make this more efficient. that is, once */
889 /* we determine the correct ioctl, we will always */
890 /* call it and not the others for that team */
894 * We cannot assume that SIOCGMIIPHY will also read a
895 * register; not all network drivers (e.g., e100)
899 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
900 mii = (struct mii_ioctl_data *)&ifr.ifr_data;
901 if (IOCTL(dev, &ifr, SIOCGMIIPHY) == 0) {
902 mii->reg_num = MII_BMSR;
903 if (IOCTL(dev, &ifr, SIOCGMIIREG) == 0) {
904 return mii->val_out & BMSR_LSTATUS;
908 /* try SIOCETHTOOL ioctl, some drivers cache ETHTOOL_GLINK */
909 /* for a period of time so we attempt to get link status */
910 /* from it last if the above MII ioctls fail... */
911 etool.cmd = ETHTOOL_GLINK;
912 ifr.ifr_data = (char*)&etool;
913 if (IOCTL(dev, &ifr, SIOCETHTOOL) == 0) {
914 if (etool.data == 1) {
919 ":: SIOCETHTOOL shows link down \n");
928 * If reporting, report that either there's no dev->do_ioctl,
929 * or both SIOCGMIIREG and SIOCETHTOOL failed (meaning that we
930 * cannot report link status). If not reporting, pretend
933 return reporting ? -1 : BMSR_LSTATUS;
936 static u16 bond_check_mii_link(bonding_t *bond)
938 int has_active_interface = 0;
940 read_lock_bh(&bond->lock);
941 read_lock(&bond->ptrlock);
942 has_active_interface = (bond->current_slave != NULL);
943 read_unlock(&bond->ptrlock);
944 read_unlock_bh(&bond->lock);
946 return (has_active_interface ? BMSR_LSTATUS : 0);
949 /* register to receive lacpdus on a bond */
950 static void bond_register_lacpdu(struct bonding *bond)
952 struct packet_type* pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
954 /* initialize packet type */
955 pk_type->type = PKT_TYPE_LACPDU;
956 pk_type->dev = bond->device;
957 pk_type->func = bond_3ad_lacpdu_recv;
959 dev_add_pack(pk_type);
962 /* unregister to receive lacpdus on a bond */
963 static void bond_unregister_lacpdu(struct bonding *bond)
965 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
968 static int bond_open(struct net_device *dev)
970 struct bonding *bond = (struct bonding *)(dev->priv);
971 struct timer_list *timer = &((struct bonding *)(dev->priv))->mii_timer;
972 struct timer_list *arp_timer = &((struct bonding *)(dev->priv))->arp_timer;
974 if ((bond_mode == BOND_MODE_TLB) ||
975 (bond_mode == BOND_MODE_ALB)) {
976 struct timer_list *alb_timer = &(BOND_ALB_INFO(bond).alb_timer);
978 /* bond_alb_initialize must be called before the timer
981 if (bond_alb_initialize(bond, (bond_mode == BOND_MODE_ALB))) {
982 /* something went wrong - fail the open operation */
986 init_timer(alb_timer);
987 alb_timer->expires = jiffies + 1;
988 alb_timer->data = (unsigned long)bond;
989 alb_timer->function = (void *)&bond_alb_monitor;
990 add_timer(alb_timer);
993 if (miimon > 0) { /* link check interval, in milliseconds. */
995 timer->expires = jiffies + (miimon * HZ / 1000);
996 timer->data = (unsigned long)dev;
997 timer->function = (void *)&bond_mii_monitor;
1001 if (arp_interval> 0) { /* arp interval, in milliseconds. */
1002 init_timer(arp_timer);
1003 arp_timer->expires = jiffies + (arp_interval * HZ / 1000);
1004 arp_timer->data = (unsigned long)dev;
1005 if (bond_mode == BOND_MODE_ACTIVEBACKUP) {
1006 arp_timer->function = (void *)&activebackup_arp_monitor;
1008 arp_timer->function = (void *)&loadbalance_arp_monitor;
1010 add_timer(arp_timer);
1013 if (bond_mode == BOND_MODE_8023AD) {
1014 struct timer_list *ad_timer = &(BOND_AD_INFO(bond).ad_timer);
1015 init_timer(ad_timer);
1016 ad_timer->expires = jiffies + (AD_TIMER_INTERVAL * HZ / 1000);
1017 ad_timer->data = (unsigned long)bond;
1018 ad_timer->function = (void *)&bond_3ad_state_machine_handler;
1019 add_timer(ad_timer);
1021 /* register to receive LACPDUs */
1022 bond_register_lacpdu(bond);
1028 static int bond_close(struct net_device *master)
1030 bonding_t *bond = (struct bonding *) master->priv;
1032 write_lock_bh(&bond->lock);
1034 if (miimon > 0) { /* link check interval, in milliseconds. */
1035 del_timer(&bond->mii_timer);
1037 if (arp_interval> 0) { /* arp interval, in milliseconds. */
1038 del_timer(&bond->arp_timer);
1039 if (arp_target_hw_addr != NULL) {
1040 kfree(arp_target_hw_addr);
1041 arp_target_hw_addr = NULL;
1045 if (bond_mode == BOND_MODE_8023AD) {
1046 del_timer_sync(&(BOND_AD_INFO(bond).ad_timer));
1048 /* Unregister the receive of LACPDUs */
1049 bond_unregister_lacpdu(bond);
1052 bond_mc_list_destroy (bond);
1054 write_unlock_bh(&bond->lock);
1056 /* Release the bonded slaves */
1057 bond_release_all(master);
1059 if ((bond_mode == BOND_MODE_TLB) ||
1060 (bond_mode == BOND_MODE_ALB)) {
1061 del_timer_sync(&(BOND_ALB_INFO(bond).alb_timer));
1063 bond_alb_deinitialize(bond);
1070 * flush all members of flush->mc_list from device dev->mc_list
1072 static void bond_mc_list_flush(struct net_device *dev, struct net_device *flush)
1074 struct dev_mc_list *dmi;
1076 for (dmi = flush->mc_list; dmi != NULL; dmi = dmi->next)
1077 dev_mc_delete(dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1079 if (bond_mode == BOND_MODE_8023AD) {
1080 /* del lacpdu mc addr from mc list */
1081 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1083 dev_mc_delete(dev, lacpdu_multicast, ETH_ALEN, 0);
1088 * Totally destroys the mc_list in bond
1090 static void bond_mc_list_destroy(struct bonding *bond)
1092 struct dev_mc_list *dmi;
1094 dmi = bond->mc_list;
1096 bond->mc_list = dmi->next;
1098 dmi = bond->mc_list;
1103 * Add a Multicast address to every slave in the bonding group
1105 static void bond_mc_add(bonding_t *bond, void *addr, int alen)
1108 switch (multicast_mode) {
1109 case BOND_MULTICAST_ACTIVE :
1110 /* write lock already acquired */
1111 if (bond->current_slave != NULL)
1112 dev_mc_add(bond->current_slave->dev, addr, alen, 0);
1114 case BOND_MULTICAST_ALL :
1115 for (slave = bond->prev; slave != (slave_t*)bond; slave = slave->prev)
1116 dev_mc_add(slave->dev, addr, alen, 0);
1118 case BOND_MULTICAST_DISABLED :
1124 * Remove a multicast address from every slave in the bonding group
1126 static void bond_mc_delete(bonding_t *bond, void *addr, int alen)
1129 switch (multicast_mode) {
1130 case BOND_MULTICAST_ACTIVE :
1131 /* write lock already acquired */
1132 if (bond->current_slave != NULL)
1133 dev_mc_delete(bond->current_slave->dev, addr, alen, 0);
1135 case BOND_MULTICAST_ALL :
1136 for (slave = bond->prev; slave != (slave_t*)bond; slave = slave->prev)
1137 dev_mc_delete(slave->dev, addr, alen, 0);
1139 case BOND_MULTICAST_DISABLED :
1145 * Copy all the Multicast addresses from src to the bonding device dst
1147 static int bond_mc_list_copy (struct dev_mc_list *src, struct bonding *dst,
1150 struct dev_mc_list *dmi, *new_dmi;
1152 for (dmi = src; dmi != NULL; dmi = dmi->next) {
1153 new_dmi = kmalloc(sizeof(struct dev_mc_list), gpf_flag);
1155 if (new_dmi == NULL) {
1159 new_dmi->next = dst->mc_list;
1160 dst->mc_list = new_dmi;
1162 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
1163 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
1164 new_dmi->dmi_users = dmi->dmi_users;
1165 new_dmi->dmi_gusers = dmi->dmi_gusers;
1171 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
1173 static inline int dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
1175 return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
1176 dmi1->dmi_addrlen == dmi2->dmi_addrlen;
1180 * Push the promiscuity flag down to appropriate slaves
1182 static void bond_set_promiscuity(bonding_t *bond, int inc)
1186 if (USES_PRIMARY(bond_mode)) {
1187 if (bond->current_slave) {
1188 dev_set_promiscuity(bond->current_slave->dev, inc);
1192 for (slave = bond->prev; slave != (slave_t*)bond;
1193 slave = slave->prev) {
1194 dev_set_promiscuity(slave->dev, inc);
1200 * Push the allmulti flag down to all slaves
1202 static void bond_set_allmulti(bonding_t *bond, int inc)
1205 switch (multicast_mode) {
1206 case BOND_MULTICAST_ACTIVE :
1207 /* write lock already acquired */
1208 if (bond->current_slave != NULL)
1209 dev_set_allmulti(bond->current_slave->dev, inc);
1211 case BOND_MULTICAST_ALL :
1212 for (slave = bond->prev; slave != (slave_t*)bond; slave = slave->prev)
1213 dev_set_allmulti(slave->dev, inc);
1215 case BOND_MULTICAST_DISABLED :
1221 * returns dmi entry if found, NULL otherwise
1223 static struct dev_mc_list* bond_mc_list_find_dmi(struct dev_mc_list *dmi,
1224 struct dev_mc_list *mc_list)
1226 struct dev_mc_list *idmi;
1228 for (idmi = mc_list; idmi != NULL; idmi = idmi->next) {
1229 if (dmi_same(dmi, idmi)) {
1236 static void set_multicast_list(struct net_device *master)
1238 bonding_t *bond = master->priv;
1239 struct dev_mc_list *dmi;
1241 write_lock_bh(&bond->lock);
1244 * Do promisc before checking multicast_mode
1246 if ( (master->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC) )
1247 bond_set_promiscuity(bond, 1);
1249 if ( !(master->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC) )
1250 bond_set_promiscuity(bond, -1);
1252 if (multicast_mode == BOND_MULTICAST_DISABLED) {
1253 bond->flags = master->flags;
1254 write_unlock_bh(&bond->lock);
1258 /* set allmulti flag to slaves */
1259 if ( (master->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI) )
1260 bond_set_allmulti(bond, 1);
1262 if ( !(master->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI) )
1263 bond_set_allmulti(bond, -1);
1265 bond->flags = master->flags;
1267 /* looking for addresses to add to slaves' mc list */
1268 for (dmi = master->mc_list; dmi != NULL; dmi = dmi->next) {
1269 if (bond_mc_list_find_dmi(dmi, bond->mc_list) == NULL)
1270 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
1273 /* looking for addresses to delete from slaves' list */
1274 for (dmi = bond->mc_list; dmi != NULL; dmi = dmi->next) {
1275 if (bond_mc_list_find_dmi(dmi, master->mc_list) == NULL)
1276 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
1280 /* save master's multicast list */
1281 bond_mc_list_destroy (bond);
1282 bond_mc_list_copy (master->mc_list, bond, GFP_ATOMIC);
1284 write_unlock_bh(&bond->lock);
1288 * Update the mc list and multicast-related flags for the new and
1289 * old active slaves (if any) according to the multicast mode, and
1290 * promiscuous flags unconditionally.
1292 static void bond_mc_update(bonding_t *bond, slave_t *new, slave_t *old)
1294 struct dev_mc_list *dmi;
1296 if (USES_PRIMARY(bond_mode)) {
1297 if (bond->device->flags & IFF_PROMISC) {
1299 dev_set_promiscuity(old->dev, -1);
1301 dev_set_promiscuity(new->dev, 1);
1305 switch(multicast_mode) {
1306 case BOND_MULTICAST_ACTIVE :
1307 if (bond->device->flags & IFF_ALLMULTI) {
1309 dev_set_allmulti(old->dev, -1);
1311 dev_set_allmulti(new->dev, 1);
1313 /* first remove all mc addresses from old slave if any,
1314 and _then_ add them to new active slave */
1316 for (dmi = bond->device->mc_list; dmi != NULL; dmi = dmi->next)
1317 dev_mc_delete(old->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1320 for (dmi = bond->device->mc_list; dmi != NULL; dmi = dmi->next)
1321 dev_mc_add(new->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1324 case BOND_MULTICAST_ALL :
1325 /* nothing to do: mc list is already up-to-date on all slaves */
1327 case BOND_MULTICAST_DISABLED :
1332 /* enslave device <slave> to bond device <master> */
1333 static int bond_enslave(struct net_device *master_dev,
1334 struct net_device *slave_dev)
1336 bonding_t *bond = NULL;
1337 slave_t *new_slave = NULL;
1338 unsigned long rflags = 0;
1340 struct dev_mc_list *dmi;
1341 struct in_ifaddr **ifap;
1342 struct in_ifaddr *ifa;
1344 struct sockaddr addr;
1346 if (master_dev == NULL || slave_dev == NULL) {
1349 bond = (struct bonding *) master_dev->priv;
1351 if (slave_dev->do_ioctl == NULL) {
1353 "Warning : no link monitoring support for %s\n",
1358 /* bond must be initialized by bond_open() before enslaving */
1359 if (!(master_dev->flags & IFF_UP)) {
1360 #ifdef BONDING_DEBUG
1361 printk(KERN_CRIT "Error, master_dev is not up\n");
1366 /* already enslaved */
1367 if (master_dev->flags & IFF_SLAVE || slave_dev->flags & IFF_SLAVE) {
1368 #ifdef BONDING_DEBUG
1369 printk(KERN_CRIT "Error, Device was already enslaved\n");
1374 if (app_abi_ver >= 1) {
1375 /* The application is using an ABI, which requires the
1376 * slave interface to be closed.
1378 if ((slave_dev->flags & IFF_UP)) {
1379 #ifdef BONDING_DEBUG
1380 printk(KERN_CRIT "Error, slave_dev is up\n");
1385 if (slave_dev->set_mac_address == NULL) {
1387 "The slave device you specified does not support"
1388 " setting the MAC address.\n");
1390 "Your kernel likely does not support slave"
1396 /* The application is not using an ABI, which requires the
1397 * slave interface to be open.
1399 if (!(slave_dev->flags & IFF_UP)) {
1400 #ifdef BONDING_DEBUG
1401 printk(KERN_CRIT "Error, slave_dev is not running\n");
1406 if ((bond_mode == BOND_MODE_8023AD) ||
1407 (bond_mode == BOND_MODE_TLB) ||
1408 (bond_mode == BOND_MODE_ALB)) {
1410 "bonding: Error: to use %s mode, you must "
1411 "upgrade ifenslave.\n", bond_mode_name());
1416 if ((new_slave = kmalloc(sizeof(slave_t), GFP_KERNEL)) == NULL) {
1419 memset(new_slave, 0, sizeof(slave_t));
1421 /* save slave's original flags before calling
1422 * netdev_set_master and dev_open
1424 new_slave->original_flags = slave_dev->flags;
1426 if (app_abi_ver >= 1) {
1427 /* save slave's original ("permanent") mac address for
1428 * modes that needs it, and for restoring it upon release,
1429 * and then set it to the master's address
1431 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1433 if (bond->slave_cnt > 0) {
1434 /* set slave to master's mac address
1435 * The application already set the master's
1436 * mac address to that of the first slave
1438 memcpy(addr.sa_data, master_dev->dev_addr, master_dev->addr_len);
1439 addr.sa_family = slave_dev->type;
1440 err = slave_dev->set_mac_address(slave_dev, &addr);
1442 #ifdef BONDING_DEBUG
1443 printk(KERN_CRIT "Error %d calling set_mac_address\n", err);
1449 /* open the slave since the application closed it */
1450 err = dev_open(slave_dev);
1452 #ifdef BONDING_DEBUG
1453 printk(KERN_CRIT "Openning slave %s failed\n", slave_dev->name);
1455 goto err_restore_mac;
1459 err = netdev_set_master(slave_dev, master_dev);
1461 #ifdef BONDING_DEBUG
1462 printk(KERN_CRIT "Error %d calling netdev_set_master\n", err);
1464 if (app_abi_ver < 1) {
1471 new_slave->dev = slave_dev;
1473 if ((bond_mode == BOND_MODE_TLB) ||
1474 (bond_mode == BOND_MODE_ALB)) {
1475 /* bond_alb_init_slave() must be called before all other stages since
1476 * it might fail and we do not want to have to undo everything
1478 err = bond_alb_init_slave(bond, new_slave);
1480 goto err_unset_master;
1484 /* set promiscuity level to new slave */
1485 if (master_dev->flags & IFF_PROMISC) {
1486 /* If the mode USES_PRIMARY, then the new slave gets the
1487 * master's promisc (and mc) settings only if it becomes the
1488 * current_slave, and that is taken care of later when calling
1489 * bond_change_active()
1491 if (!USES_PRIMARY(bond_mode)) {
1492 dev_set_promiscuity(slave_dev, 1);
1496 if (multicast_mode == BOND_MULTICAST_ALL) {
1497 /* set allmulti level to new slave */
1498 if (master_dev->flags & IFF_ALLMULTI)
1499 dev_set_allmulti(slave_dev, 1);
1501 /* upload master's mc_list to new slave */
1502 for (dmi = master_dev->mc_list; dmi != NULL; dmi = dmi->next)
1503 dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1506 if (bond_mode == BOND_MODE_8023AD) {
1507 /* add lacpdu mc addr to mc list */
1508 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1510 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1513 write_lock_bh(&bond->lock);
1515 bond_attach_slave(bond, new_slave);
1516 new_slave->delay = 0;
1517 new_slave->link_failure_count = 0;
1519 if (miimon > 0 && !use_carrier) {
1520 link_reporting = bond_check_dev_link(slave_dev, 1);
1522 if ((link_reporting == -1) && (arp_interval == 0)) {
1524 * miimon is set but a bonded network driver
1525 * does not support ETHTOOL/MII and
1526 * arp_interval is not set. Note: if
1527 * use_carrier is enabled, we will never go
1528 * here (because netif_carrier is always
1529 * supported); thus, we don't need to change
1530 * the messages for netif_carrier.
1533 "bond_enslave(): MII and ETHTOOL support not "
1534 "available for interface %s, and "
1535 "arp_interval/arp_ip_target module parameters "
1536 "not specified, thus bonding will not detect "
1537 "link failures! see bonding.txt for details.\n",
1539 } else if (link_reporting == -1) {
1540 /* unable get link status using mii/ethtool */
1542 "bond_enslave: can't get link status from "
1543 "interface %s; the network driver associated "
1544 "with this interface does not support "
1545 "MII or ETHTOOL link status reporting, thus "
1546 "miimon has no effect on this interface.\n",
1551 /* check for initial state */
1552 if ((miimon <= 0) ||
1553 (bond_check_dev_link(slave_dev, 0) == BMSR_LSTATUS)) {
1555 #ifdef BONDING_DEBUG
1556 printk(KERN_CRIT "Initial state of slave_dev is "
1557 "BOND_LINK_BACK\n");
1559 new_slave->link = BOND_LINK_BACK;
1560 new_slave->delay = updelay;
1563 #ifdef BONDING_DEBUG
1564 printk(KERN_DEBUG "Initial state of slave_dev is "
1567 new_slave->link = BOND_LINK_UP;
1569 new_slave->jiffies = jiffies;
1572 #ifdef BONDING_DEBUG
1573 printk(KERN_CRIT "Initial state of slave_dev is "
1574 "BOND_LINK_DOWN\n");
1576 new_slave->link = BOND_LINK_DOWN;
1579 if (bond_update_speed_duplex(new_slave) &&
1580 (new_slave->link != BOND_LINK_DOWN)) {
1583 "bond_enslave(): failed to get speed/duplex from %s, "
1584 "speed forced to 100Mbps, duplex forced to Full.\n",
1585 new_slave->dev->name);
1586 if (bond_mode == BOND_MODE_8023AD) {
1588 "Operation of 802.3ad mode requires ETHTOOL support "
1589 "in base driver for proper aggregator selection.\n");
1593 /* if we're in active-backup mode, we need one and only one active
1594 * interface. The backup interfaces will have their NOARP flag set
1595 * because we need them to be completely deaf and not to respond to
1596 * any ARP request on the network to avoid fooling a switch. Thus,
1597 * since we guarantee that current_slave always point to the last
1598 * usable interface, we just have to verify this interface's flag.
1600 if (bond_mode == BOND_MODE_ACTIVEBACKUP) {
1601 if (((bond->current_slave == NULL)
1602 || (bond->current_slave->dev->flags & IFF_NOARP))
1603 && (new_slave->link != BOND_LINK_DOWN)) {
1604 #ifdef BONDING_DEBUG
1605 printk(KERN_CRIT "This is the first active slave\n");
1607 /* first slave or no active slave yet, and this link
1608 is OK, so make this interface the active one */
1609 change_active_interface(bond, new_slave);
1612 #ifdef BONDING_DEBUG
1613 printk(KERN_CRIT "This is just a backup slave\n");
1615 bond_set_slave_inactive_flags(new_slave);
1617 if (((struct in_device *)slave_dev->ip_ptr) != NULL) {
1618 read_lock_irqsave(&(((struct in_device *)slave_dev->ip_ptr)->lock), rflags);
1619 ifap= &(((struct in_device *)slave_dev->ip_ptr)->ifa_list);
1622 my_ip = ifa->ifa_address;
1623 read_unlock_irqrestore(&(((struct in_device *)slave_dev->ip_ptr)->lock), rflags);
1626 /* if there is a primary slave, remember it */
1627 if (primary != NULL) {
1628 if (strcmp(primary, new_slave->dev->name) == 0) {
1629 bond->primary_slave = new_slave;
1632 } else if (bond_mode == BOND_MODE_8023AD) {
1633 /* in 802.3ad mode, the internal mechanism
1634 * will activate the slaves in the selected
1637 bond_set_slave_inactive_flags(new_slave);
1638 /* if this is the first slave */
1639 if (new_slave == bond->next) {
1640 SLAVE_AD_INFO(new_slave).id = 1;
1641 /* Initialize AD with the number of times that the AD timer is called in 1 second
1642 * can be called only after the mac address of the bond is set
1644 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1647 SLAVE_AD_INFO(new_slave).id =
1648 SLAVE_AD_INFO(new_slave->prev).id + 1;
1651 bond_3ad_bind_slave(new_slave);
1652 } else if ((bond_mode == BOND_MODE_TLB) ||
1653 (bond_mode == BOND_MODE_ALB)) {
1654 new_slave->state = BOND_STATE_ACTIVE;
1655 if ((bond->current_slave == NULL) && (new_slave->link != BOND_LINK_DOWN)) {
1656 /* first slave or no active slave yet, and this link
1657 * is OK, so make this interface the active one
1659 change_active_interface(bond, new_slave);
1662 /* if there is a primary slave, remember it */
1663 if (primary != NULL) {
1664 if (strcmp(primary, new_slave->dev->name) == 0) {
1665 bond->primary_slave = new_slave;
1669 #ifdef BONDING_DEBUG
1670 printk(KERN_CRIT "This slave is always active in trunk mode\n");
1672 /* always active in trunk mode */
1673 new_slave->state = BOND_STATE_ACTIVE;
1675 /* In trunking mode there is little meaning to current_slave
1676 * anyway (it holds no special properties of the bond device),
1677 * so we can change it without calling change_active_interface()
1679 if (bond->current_slave == NULL)
1680 bond->current_slave = new_slave;
1683 write_unlock_bh(&bond->lock);
1685 if (app_abi_ver < 1) {
1687 * !!! This is to support old versions of ifenslave.
1688 * We can remove this in 2.5 because our ifenslave takes
1689 * care of this for us.
1690 * We check to see if the master has a mac address yet.
1691 * If not, we'll give it the mac address of our slave device.
1695 for (ndx = 0; ndx < slave_dev->addr_len; ndx++) {
1696 #ifdef BONDING_DEBUG
1698 "Checking ndx=%d of master_dev->dev_addr\n", ndx);
1700 if (master_dev->dev_addr[ndx] != 0) {
1701 #ifdef BONDING_DEBUG
1703 "Found non-zero byte at ndx=%d\n", ndx);
1708 if (ndx == slave_dev->addr_len) {
1710 * We got all the way through the address and it was
1713 #ifdef BONDING_DEBUG
1714 printk(KERN_DEBUG "%s doesn't have a MAC address yet. ",
1716 printk(KERN_DEBUG "Going to give assign it from %s.\n",
1719 bond_sethwaddr(master_dev, slave_dev);
1723 printk (KERN_INFO "%s: enslaving %s as a%s interface with a%s link.\n",
1724 master_dev->name, slave_dev->name,
1725 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1726 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1728 /* enslave is successful */
1731 /* Undo stages on error */
1733 netdev_set_master(slave_dev, NULL);
1736 dev_close(slave_dev);
1739 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1740 addr.sa_family = slave_dev->type;
1741 slave_dev->set_mac_address(slave_dev, &addr);
1749 * This function changes the active slave to slave <slave_dev>.
1750 * It returns -EINVAL in the following cases.
1751 * - <slave_dev> is not found in the list.
1752 * - There is not active slave now.
1753 * - <slave_dev> is already active.
1754 * - The link state of <slave_dev> is not BOND_LINK_UP.
1755 * - <slave_dev> is not running.
1756 * In these cases, this fuction does nothing.
1757 * In the other cases, currnt_slave pointer is changed and 0 is returned.
1759 static int bond_change_active(struct net_device *master_dev, struct net_device *slave_dev)
1763 slave_t *oldactive = NULL;
1764 slave_t *newactive = NULL;
1767 if (master_dev == NULL || slave_dev == NULL) {
1771 /* Verify that master_dev is indeed the master of slave_dev */
1772 if (!(slave_dev->flags & IFF_SLAVE) ||
1773 (slave_dev->master != master_dev)) {
1778 bond = (struct bonding *) master_dev->priv;
1779 write_lock_bh(&bond->lock);
1780 slave = (slave_t *)bond;
1781 oldactive = bond->current_slave;
1783 while ((slave = slave->prev) != (slave_t *)bond) {
1784 if(slave_dev == slave->dev) {
1791 * Changing to the current active: do nothing; return success.
1793 if (newactive && (newactive == oldactive)) {
1794 write_unlock_bh(&bond->lock);
1798 if ((newactive != NULL)&&
1799 (oldactive != NULL)&&
1800 (newactive->link == BOND_LINK_UP)&&
1801 IS_UP(newactive->dev)) {
1802 change_active_interface(bond, newactive);
1806 write_unlock_bh(&bond->lock);
1811 * find_best_interface - select the best available slave to be the active one
1812 * @bond: our bonding struct
1814 * Warning: Caller must hold ptrlock for writing.
1816 static struct slave *find_best_interface(struct bonding *bond)
1818 struct slave *newslave, *oldslave;
1819 struct slave *bestslave = NULL;
1822 newslave = oldslave = bond->current_slave;
1824 if (newslave == NULL) { /* there were no active slaves left */
1825 if (bond->next != (slave_t *)bond) { /* found one slave */
1826 newslave = bond->next;
1828 return NULL; /* still no slave, return NULL */
1834 /* first try the primary link; if arping, a link must tx/rx traffic
1835 * before it can be considered the current_slave - also, we would skip
1836 * slaves between the current_slave and primary_slave that may be up
1839 if ((bond->primary_slave != NULL) && (arp_interval == 0)) {
1840 if (IS_UP(bond->primary_slave->dev))
1841 newslave = bond->primary_slave;
1844 /* remember where to stop iterating over the slaves */
1845 oldslave = newslave;
1848 if (IS_UP(newslave->dev)) {
1849 if (newslave->link == BOND_LINK_UP) {
1852 else if (newslave->link == BOND_LINK_BACK) {
1853 /* link up, but waiting for stabilization */
1854 if (newslave->delay < mintime) {
1855 mintime = newslave->delay;
1856 bestslave = newslave;
1860 } while ((newslave = newslave->next) != oldslave);
1866 * change_active_interface - change the active slave into the specified one
1867 * @bond: our bonding struct
1868 * @new: the new slave to make the active one
1870 * Set the new slave to the bond's settings and unset them on the old
1872 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1874 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1875 * because it is apparently the best available slave we have, even though its
1876 * updelay hasn't timed out yet.
1878 * Warning: Caller must hold ptrlock for writing.
1880 static void change_active_interface(struct bonding *bond, struct slave *new)
1882 struct slave *old = bond->current_slave;
1889 if (new->link == BOND_LINK_BACK) {
1890 if (USES_PRIMARY(bond_mode)) {
1892 "%s: making interface %s the new "
1893 "active one %d ms earlier.\n",
1894 bond->device->name, new->dev->name,
1895 (updelay - new->delay) * miimon);
1899 new->link = BOND_LINK_UP;
1900 new->jiffies = jiffies;
1902 if (bond_mode == BOND_MODE_8023AD) {
1903 bond_3ad_handle_link_change(new, BOND_LINK_UP);
1906 if ((bond_mode == BOND_MODE_TLB) ||
1907 (bond_mode == BOND_MODE_ALB)) {
1908 bond_alb_handle_link_change(bond, new, BOND_LINK_UP);
1911 if (USES_PRIMARY(bond_mode)) {
1913 "%s: making interface %s the new active one.\n",
1914 bond->device->name, new->dev->name);
1919 if (bond_mode == BOND_MODE_ACTIVEBACKUP) {
1921 bond_set_slave_inactive_flags(old);
1925 bond_set_slave_active_flags(new);
1929 if (USES_PRIMARY(bond_mode)) {
1930 bond_mc_update(bond, new, old);
1933 if ((bond_mode == BOND_MODE_TLB) ||
1934 (bond_mode == BOND_MODE_ALB)) {
1935 bond_alb_assign_current_slave(bond, new);
1937 bond->current_slave = new;
1942 * reselect_active_interface - select a new active slave, if needed
1943 * @bond: our bonding struct
1945 * This functions shoud be called when one of the following occurs:
1946 * - The old current_slave has been released or lost its link.
1947 * - The primary_slave has got its link back.
1948 * - A slave has got its link back and there's no old current_slave.
1950 * Warning: Caller must hold ptrlock for writing.
1952 static void reselect_active_interface(struct bonding *bond)
1954 struct slave *best_slave;
1956 best_slave = find_best_interface(bond);
1958 if (best_slave != bond->current_slave) {
1959 change_active_interface(bond, best_slave);
1964 * Try to release the slave device <slave> from the bond device <master>
1965 * It is legal to access current_slave without a lock because all the function
1968 * The rules for slave state should be:
1969 * for Active/Backup:
1970 * Active stays on all backups go down
1971 * for Bonded connections:
1972 * The first up interface should be left on and all others downed.
1974 static int bond_release(struct net_device *master, struct net_device *slave)
1977 slave_t *our_slave, *old_current;
1978 struct sockaddr addr;
1980 if (master == NULL || slave == NULL) {
1984 bond = (struct bonding *) master->priv;
1986 /* master already enslaved, or slave not enslaved,
1987 or no slave for this master */
1988 if ((master->flags & IFF_SLAVE) || !(slave->flags & IFF_SLAVE)) {
1989 printk (KERN_DEBUG "%s: cannot release %s.\n", master->name, slave->name);
1993 write_lock_bh(&bond->lock);
1994 bond->current_arp_slave = NULL;
1995 our_slave = (slave_t *)bond;
1996 old_current = bond->current_slave;
1997 while ((our_slave = our_slave->prev) != (slave_t *)bond) {
1998 if (our_slave->dev == slave) {
1999 int mac_addr_differ = memcmp(bond->device->dev_addr,
2000 our_slave->perm_hwaddr,
2002 if (!mac_addr_differ && (bond->slave_cnt > 1)) {
2003 printk(KERN_WARNING "WARNING: the permanent HWaddr of %s "
2004 "- %02X:%02X:%02X:%02X:%02X:%02X - "
2005 "is still in use by %s. Set the HWaddr "
2006 "of %s to a different address "
2007 "to avoid conflicts.\n",
2009 our_slave->perm_hwaddr[0],
2010 our_slave->perm_hwaddr[1],
2011 our_slave->perm_hwaddr[2],
2012 our_slave->perm_hwaddr[3],
2013 our_slave->perm_hwaddr[4],
2014 our_slave->perm_hwaddr[5],
2019 /* Inform AD package of unbinding of slave. */
2020 if (bond_mode == BOND_MODE_8023AD) {
2021 /* must be called before the slave is
2022 * detached from the list
2024 bond_3ad_unbind_slave(our_slave);
2027 printk (KERN_INFO "%s: releasing %s interface %s\n",
2029 (our_slave->state == BOND_STATE_ACTIVE) ? "active" : "backup",
2032 /* release the slave from its bond */
2033 bond_detach_slave(bond, our_slave);
2035 if (bond->primary_slave == our_slave) {
2036 bond->primary_slave = NULL;
2039 if (bond->current_slave == our_slave) {
2040 change_active_interface(bond, NULL);
2041 reselect_active_interface(bond);
2044 if (bond->current_slave == NULL) {
2046 "%s: now running without any active interface !\n",
2050 if ((bond_mode == BOND_MODE_TLB) ||
2051 (bond_mode == BOND_MODE_ALB)) {
2052 /* must be called only after the slave has been
2053 * detached from the list and the current_slave
2054 * has been replaced (if our_slave == old_current)
2056 bond_alb_deinit_slave(bond, our_slave);
2063 write_unlock_bh(&bond->lock);
2065 if (our_slave == (slave_t *)bond) {
2066 /* if we get here, it's because the device was not found */
2067 printk (KERN_INFO "%s: %s not enslaved\n", master->name, slave->name);
2071 /* unset promiscuity level from slave */
2072 if (master->flags & IFF_PROMISC) {
2073 /* If the mode USES_PRIMARY, then we should only remove its
2074 * promisc settings if it was the current_slave, but that was
2075 * already taken care of above when we detached the slave
2077 if (!USES_PRIMARY(bond_mode)) {
2078 dev_set_promiscuity(slave, -1);
2082 /* undo settings and restore original values */
2083 if (multicast_mode == BOND_MULTICAST_ALL) {
2084 /* flush master's mc_list from slave */
2085 bond_mc_list_flush (slave, master);
2087 /* unset allmulti level from slave */
2088 if (master->flags & IFF_ALLMULTI)
2089 dev_set_allmulti(slave, -1);
2092 netdev_set_master(slave, NULL);
2094 /* close slave before restoring its mac address */
2097 if (app_abi_ver >= 1) {
2098 /* restore original ("permanent") mac address */
2099 memcpy(addr.sa_data, our_slave->perm_hwaddr, ETH_ALEN);
2100 addr.sa_family = slave->type;
2101 slave->set_mac_address(slave, &addr);
2104 /* restore the original state of the
2105 * IFF_NOARP flag that might have been
2106 * set by bond_set_slave_inactive_flags()
2108 if ((our_slave->original_flags & IFF_NOARP) == 0) {
2109 slave->flags &= ~IFF_NOARP;
2114 /* if the last slave was removed, zero the mac address
2115 * of the master so it will be set by the application
2116 * to the mac address of the first slave
2118 if (bond->next == (slave_t*)bond) {
2119 memset(master->dev_addr, 0, master->addr_len);
2122 return 0; /* deletion OK */
2126 * This function releases all slaves.
2128 static int bond_release_all(struct net_device *master)
2131 slave_t *our_slave, *old_current;
2132 struct net_device *slave_dev;
2133 struct sockaddr addr;
2136 if (master == NULL) {
2140 if (master->flags & IFF_SLAVE) {
2144 bond = (struct bonding *) master->priv;
2146 write_lock_bh(&bond->lock);
2147 if (bond->next == (struct slave *) bond) {
2152 old_current = bond->current_slave;
2153 change_active_interface(bond, NULL);
2154 bond->current_arp_slave = NULL;
2155 bond->primary_slave = NULL;
2157 while ((our_slave = bond->prev) != (slave_t *)bond) {
2158 /* Inform AD package of unbinding of slave
2159 * before slave is detached from the list.
2161 if (bond_mode == BOND_MODE_8023AD) {
2162 bond_3ad_unbind_slave(our_slave);
2165 slave_dev = our_slave->dev;
2166 bond_detach_slave(bond, our_slave);
2168 if ((bond_mode == BOND_MODE_TLB) ||
2169 (bond_mode == BOND_MODE_ALB)) {
2170 /* must be called only after the slave
2171 * has been detached from the list
2173 bond_alb_deinit_slave(bond, our_slave);
2176 /* now that the slave is detached, unlock and perform
2177 * all the undo steps that should not be called from
2180 write_unlock_bh(&bond->lock);
2182 /* unset promiscuity level from slave */
2183 if (master->flags & IFF_PROMISC) {
2184 if (!USES_PRIMARY(bond_mode)) {
2185 dev_set_promiscuity(slave_dev, -1);
2189 if (multicast_mode == BOND_MULTICAST_ALL) {
2190 /* flush master's mc_list from slave */
2191 bond_mc_list_flush (slave_dev, master);
2193 /* unset allmulti level from slave */
2194 if (master->flags & IFF_ALLMULTI)
2195 dev_set_allmulti(slave_dev, -1);
2198 netdev_set_master(slave_dev, NULL);
2200 /* close slave before restoring its mac address */
2201 dev_close(slave_dev);
2203 if (app_abi_ver >= 1) {
2204 /* restore original ("permanent") mac address*/
2205 memcpy(addr.sa_data, our_slave->perm_hwaddr, ETH_ALEN);
2206 addr.sa_family = slave_dev->type;
2207 slave_dev->set_mac_address(slave_dev, &addr);
2210 /* restore the original state of the IFF_NOARP flag that might have
2211 * been set by bond_set_slave_inactive_flags()
2213 if ((our_slave->original_flags & IFF_NOARP) == 0) {
2214 slave_dev->flags &= ~IFF_NOARP;
2219 /* re-acquire the lock before getting the next slave */
2220 write_lock_bh(&bond->lock);
2223 /* zero the mac address of the master so it will be
2224 * set by the application to the mac address of the
2227 memset(master->dev_addr, 0, master->addr_len);
2229 printk (KERN_INFO "%s: released all slaves\n", master->name);
2232 write_unlock_bh(&bond->lock);
2237 /* this function is called regularly to monitor each slave's link. */
2238 static void bond_mii_monitor(struct net_device *master)
2240 bonding_t *bond = (struct bonding *) master->priv;
2241 slave_t *slave, *oldcurrent;
2243 int do_failover = 0;
2245 read_lock(&bond->lock);
2247 /* we will try to read the link status of each of our slaves, and
2248 * set their IFF_RUNNING flag appropriately. For each slave not
2249 * supporting MII status, we won't do anything so that a user-space
2250 * program could monitor the link itself if needed.
2253 slave = (slave_t *)bond;
2255 read_lock(&bond->ptrlock);
2256 oldcurrent = bond->current_slave;
2257 read_unlock(&bond->ptrlock);
2259 while ((slave = slave->prev) != (slave_t *)bond) {
2260 struct net_device *dev = slave->dev;
2262 u16 old_speed = slave->speed;
2263 u8 old_duplex = slave->duplex;
2265 link_state = bond_check_dev_link(dev, 0);
2267 switch (slave->link) {
2268 case BOND_LINK_UP: /* the link was up */
2269 if (link_state == BMSR_LSTATUS) {
2270 /* link stays up, nothing more to do */
2273 else { /* link going down */
2274 slave->link = BOND_LINK_FAIL;
2275 slave->delay = downdelay;
2276 if (slave->link_failure_count < UINT_MAX) {
2277 slave->link_failure_count++;
2279 if (downdelay > 0) {
2281 "%s: link status down for %sinterface "
2282 "%s, disabling it in %d ms.\n",
2285 ? ((bond_mode == BOND_MODE_ACTIVEBACKUP)
2286 ? ((slave == oldcurrent)
2287 ? "active " : "backup ")
2291 downdelay * miimon);
2294 /* no break ! fall through the BOND_LINK_FAIL test to
2295 ensure proper action to be taken
2297 case BOND_LINK_FAIL: /* the link has just gone down */
2298 if (link_state != BMSR_LSTATUS) {
2299 /* link stays down */
2300 if (slave->delay <= 0) {
2301 /* link down for too long time */
2302 slave->link = BOND_LINK_DOWN;
2303 /* in active/backup mode, we must
2304 * completely disable this interface
2306 if ((bond_mode == BOND_MODE_ACTIVEBACKUP) ||
2307 (bond_mode == BOND_MODE_8023AD)) {
2308 bond_set_slave_inactive_flags(slave);
2312 "%s: link status definitely down "
2313 "for interface %s, disabling it",
2317 /* notify ad that the link status has changed */
2318 if (bond_mode == BOND_MODE_8023AD) {
2319 bond_3ad_handle_link_change(slave, BOND_LINK_DOWN);
2322 if ((bond_mode == BOND_MODE_TLB) ||
2323 (bond_mode == BOND_MODE_ALB)) {
2324 bond_alb_handle_link_change(bond, slave, BOND_LINK_DOWN);
2327 if (slave == oldcurrent) {
2337 slave->link = BOND_LINK_UP;
2338 slave->jiffies = jiffies;
2340 "%s: link status up again after %d ms "
2341 "for interface %s.\n",
2343 (downdelay - slave->delay) * miimon,
2347 case BOND_LINK_DOWN: /* the link was down */
2348 if (link_state != BMSR_LSTATUS) {
2349 /* the link stays down, nothing more to do */
2351 } else { /* link going up */
2352 slave->link = BOND_LINK_BACK;
2353 slave->delay = updelay;
2356 /* if updelay == 0, no need to
2357 advertise about a 0 ms delay */
2359 "%s: link status up for interface"
2360 " %s, enabling it in %d ms.\n",
2366 /* no break ! fall through the BOND_LINK_BACK state in
2367 case there's something to do.
2369 case BOND_LINK_BACK: /* the link has just come back */
2370 if (link_state != BMSR_LSTATUS) {
2371 /* link down again */
2372 slave->link = BOND_LINK_DOWN;
2374 "%s: link status down again after %d ms "
2375 "for interface %s.\n",
2377 (updelay - slave->delay) * miimon,
2381 if (slave->delay == 0) {
2382 /* now the link has been up for long time enough */
2383 slave->link = BOND_LINK_UP;
2384 slave->jiffies = jiffies;
2386 if (bond_mode == BOND_MODE_8023AD) {
2387 /* prevent it from being the active one */
2388 slave->state = BOND_STATE_BACKUP;
2390 else if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
2391 /* make it immediately active */
2392 slave->state = BOND_STATE_ACTIVE;
2393 } else if (slave != bond->primary_slave) {
2394 /* prevent it from being the active one */
2395 slave->state = BOND_STATE_BACKUP;
2399 "%s: link status definitely up "
2400 "for interface %s.\n",
2404 /* notify ad that the link status has changed */
2405 if (bond_mode == BOND_MODE_8023AD) {
2406 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2409 if ((bond_mode == BOND_MODE_TLB) ||
2410 (bond_mode == BOND_MODE_ALB)) {
2411 bond_alb_handle_link_change(bond, slave, BOND_LINK_UP);
2414 if ((oldcurrent == NULL) ||
2415 (slave == bond->primary_slave)) {
2423 } /* end of switch */
2425 bond_update_speed_duplex(slave);
2427 if (bond_mode == BOND_MODE_8023AD) {
2428 if (old_speed != slave->speed) {
2429 bond_3ad_adapter_speed_changed(slave);
2431 if (old_duplex != slave->duplex) {
2432 bond_3ad_adapter_duplex_changed(slave);
2436 } /* end of while */
2439 write_lock(&bond->ptrlock);
2441 reselect_active_interface(bond);
2442 if (oldcurrent && !bond->current_slave) {
2444 "%s: now running without any active interface !\n",
2448 write_unlock(&bond->ptrlock);
2451 read_unlock(&bond->lock);
2452 /* re-arm the timer */
2453 mod_timer(&bond->mii_timer, jiffies + (miimon * HZ / 1000));
2457 * this function is called regularly to monitor each slave's link
2458 * ensuring that traffic is being sent and received when arp monitoring
2459 * is used in load-balancing mode. if the adapter has been dormant, then an
2460 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2461 * arp monitoring in active backup mode.
2463 static void loadbalance_arp_monitor(struct net_device *master)
2466 slave_t *slave, *oldcurrent;
2467 int the_delta_in_ticks = arp_interval * HZ / 1000;
2468 int next_timer = jiffies + (arp_interval * HZ / 1000);
2469 int do_failover = 0;
2471 bond = (struct bonding *) master->priv;
2472 if (master->priv == NULL) {
2473 mod_timer(&bond->arp_timer, next_timer);
2477 /* TODO: investigate why rtnl_shlock_nowait and rtnl_exlock_nowait
2478 * are called below and add comment why they are required...
2480 if ((!IS_UP(master)) || rtnl_shlock_nowait()) {
2481 mod_timer(&bond->arp_timer, next_timer);
2485 if (rtnl_exlock_nowait()) {
2487 mod_timer(&bond->arp_timer, next_timer);
2491 read_lock(&bond->lock);
2493 read_lock(&bond->ptrlock);
2494 oldcurrent = bond->current_slave;
2495 read_unlock(&bond->ptrlock);
2497 /* see if any of the previous devices are up now (i.e. they have
2498 * xmt and rcv traffic). the current_slave does not come into
2499 * the picture unless it is null. also, slave->jiffies is not needed
2500 * here because we send an arp on each slave and give a slave as
2501 * long as it needs to get the tx/rx within the delta.
2502 * TODO: what about up/down delay in arp mode? it wasn't here before
2505 slave = (slave_t *)bond;
2506 while ((slave = slave->prev) != (slave_t *)bond) {
2508 if (slave->link != BOND_LINK_UP) {
2510 if (((jiffies - slave->dev->trans_start) <=
2511 the_delta_in_ticks) &&
2512 ((jiffies - slave->dev->last_rx) <=
2513 the_delta_in_ticks)) {
2515 slave->link = BOND_LINK_UP;
2516 slave->state = BOND_STATE_ACTIVE;
2518 /* primary_slave has no meaning in round-robin
2519 * mode. the window of a slave being up and
2520 * current_slave being null after enslaving
2523 if (oldcurrent == NULL) {
2525 "%s: link status definitely up "
2526 "for interface %s, ",
2532 "%s: interface %s is now up\n",
2538 /* slave->link == BOND_LINK_UP */
2540 /* not all switches will respond to an arp request
2541 * when the source ip is 0, so don't take the link down
2542 * if we don't know our ip yet
2544 if (((jiffies - slave->dev->trans_start) >=
2545 (2*the_delta_in_ticks)) ||
2546 (((jiffies - slave->dev->last_rx) >=
2547 (2*the_delta_in_ticks)) && my_ip !=0)) {
2548 slave->link = BOND_LINK_DOWN;
2549 slave->state = BOND_STATE_BACKUP;
2550 if (slave->link_failure_count < UINT_MAX) {
2551 slave->link_failure_count++;
2554 "%s: interface %s is now down.\n",
2558 if (slave == oldcurrent) {
2564 /* note: if switch is in round-robin mode, all links
2565 * must tx arp to ensure all links rx an arp - otherwise
2566 * links may oscillate or not come up at all; if switch is
2567 * in something like xor mode, there is nothing we can
2568 * do - all replies will be rx'ed on same link causing slaves
2569 * to be unstable during low/no traffic periods
2571 if (IS_UP(slave->dev)) {
2572 arp_send_all(slave);
2577 write_lock(&bond->ptrlock);
2579 reselect_active_interface(bond);
2580 if (oldcurrent && !bond->current_slave) {
2582 "%s: now running without any active interface !\n",
2586 write_unlock(&bond->ptrlock);
2589 read_unlock(&bond->lock);
2593 /* re-arm the timer */
2594 mod_timer(&bond->arp_timer, next_timer);
2598 * When using arp monitoring in active-backup mode, this function is
2599 * called to determine if any backup slaves have went down or a new
2600 * current slave needs to be found.
2601 * The backup slaves never generate traffic, they are considered up by merely
2602 * receiving traffic. If the current slave goes down, each backup slave will
2603 * be given the opportunity to tx/rx an arp before being taken down - this
2604 * prevents all slaves from being taken down due to the current slave not
2605 * sending any traffic for the backups to receive. The arps are not necessarily
2606 * necessary, any tx and rx traffic will keep the current slave up. While any
2607 * rx traffic will keep the backup slaves up, the current slave is responsible
2608 * for generating traffic to keep them up regardless of any other traffic they
2609 * may have received.
2610 * see loadbalance_arp_monitor for arp monitoring in load balancing mode
2612 static void activebackup_arp_monitor(struct net_device *master)
2616 int the_delta_in_ticks = arp_interval * HZ / 1000;
2617 int next_timer = jiffies + (arp_interval * HZ / 1000);
2619 bond = (struct bonding *) master->priv;
2620 if (master->priv == NULL) {
2621 mod_timer(&bond->arp_timer, next_timer);
2625 if (!IS_UP(master)) {
2626 mod_timer(&bond->arp_timer, next_timer);
2630 read_lock(&bond->lock);
2632 /* determine if any slave has come up or any backup slave has
2634 * TODO: what about up/down delay in arp mode? it wasn't here before
2637 slave = (slave_t *)bond;
2638 while ((slave = slave->prev) != (slave_t *)bond) {
2640 if (slave->link != BOND_LINK_UP) {
2641 if ((jiffies - slave->dev->last_rx) <=
2642 the_delta_in_ticks) {
2644 slave->link = BOND_LINK_UP;
2645 write_lock(&bond->ptrlock);
2646 if ((bond->current_slave == NULL) &&
2647 ((jiffies - slave->dev->trans_start) <=
2648 the_delta_in_ticks)) {
2649 change_active_interface(bond, slave);
2650 bond->current_arp_slave = NULL;
2651 } else if (bond->current_slave != slave) {
2652 /* this slave has just come up but we
2653 * already have a current slave; this
2654 * can also happen if bond_enslave adds
2655 * a new slave that is up while we are
2656 * searching for a new slave
2658 bond_set_slave_inactive_flags(slave);
2659 bond->current_arp_slave = NULL;
2662 if (slave == bond->current_slave) {
2664 "%s: %s is up and now the "
2665 "active interface\n",
2670 "%s: backup interface %s is "
2676 write_unlock(&bond->ptrlock);
2679 read_lock(&bond->ptrlock);
2680 if ((slave != bond->current_slave) &&
2681 (bond->current_arp_slave == NULL) &&
2682 (((jiffies - slave->dev->last_rx) >=
2683 3*the_delta_in_ticks) && (my_ip != 0))) {
2684 /* a backup slave has gone down; three times
2685 * the delta allows the current slave to be
2686 * taken out before the backup slave.
2687 * note: a non-null current_arp_slave indicates
2688 * the current_slave went down and we are
2689 * searching for a new one; under this
2690 * condition we only take the current_slave
2691 * down - this gives each slave a chance to
2692 * tx/rx traffic before being taken out
2694 read_unlock(&bond->ptrlock);
2695 slave->link = BOND_LINK_DOWN;
2696 if (slave->link_failure_count < UINT_MAX) {
2697 slave->link_failure_count++;
2699 bond_set_slave_inactive_flags(slave);
2701 "%s: backup interface %s is now down\n",
2705 read_unlock(&bond->ptrlock);
2710 read_lock(&bond->ptrlock);
2711 slave = bond->current_slave;
2712 read_unlock(&bond->ptrlock);
2714 if (slave != NULL) {
2716 /* if we have sent traffic in the past 2*arp_intervals but
2717 * haven't xmit and rx traffic in that time interval, select
2718 * a different slave. slave->jiffies is only updated when
2719 * a slave first becomes the current_slave - not necessarily
2720 * after every arp; this ensures the slave has a full 2*delta
2721 * before being taken out. if a primary is being used, check
2722 * if it is up and needs to take over as the current_slave
2724 if ((((jiffies - slave->dev->trans_start) >=
2725 (2*the_delta_in_ticks)) ||
2726 (((jiffies - slave->dev->last_rx) >=
2727 (2*the_delta_in_ticks)) && (my_ip != 0))) &&
2728 ((jiffies - slave->jiffies) >= 2*the_delta_in_ticks)) {
2730 slave->link = BOND_LINK_DOWN;
2731 if (slave->link_failure_count < UINT_MAX) {
2732 slave->link_failure_count++;
2734 printk(KERN_INFO "%s: link status down for "
2735 "active interface %s, disabling it",
2738 write_lock(&bond->ptrlock);
2739 reselect_active_interface(bond);
2740 slave = bond->current_slave;
2741 write_unlock(&bond->ptrlock);
2742 bond->current_arp_slave = slave;
2743 if (slave != NULL) {
2744 slave->jiffies = jiffies;
2747 } else if ((bond->primary_slave != NULL) &&
2748 (bond->primary_slave != slave) &&
2749 (bond->primary_slave->link == BOND_LINK_UP)) {
2750 /* at this point, slave is the current_slave */
2752 "%s: changing from interface %s to primary "
2756 bond->primary_slave->dev->name);
2758 /* primary is up so switch to it */
2759 write_lock(&bond->ptrlock);
2760 change_active_interface(bond, bond->primary_slave);
2761 write_unlock(&bond->ptrlock);
2762 slave = bond->primary_slave;
2763 slave->jiffies = jiffies;
2765 bond->current_arp_slave = NULL;
2768 /* the current slave must tx an arp to ensure backup slaves
2771 if ((slave != NULL) && (my_ip != 0)) {
2772 arp_send_all(slave);
2776 /* if we don't have a current_slave, search for the next available
2777 * backup slave from the current_arp_slave and make it the candidate
2778 * for becoming the current_slave
2780 if (slave == NULL) {
2782 if ((bond->current_arp_slave == NULL) ||
2783 (bond->current_arp_slave == (slave_t *)bond)) {
2784 bond->current_arp_slave = bond->prev;
2787 if (bond->current_arp_slave != (slave_t *)bond) {
2788 bond_set_slave_inactive_flags(bond->current_arp_slave);
2789 slave = bond->current_arp_slave->next;
2791 /* search for next candidate */
2793 if (IS_UP(slave->dev)) {
2794 slave->link = BOND_LINK_BACK;
2795 bond_set_slave_active_flags(slave);
2796 arp_send_all(slave);
2797 slave->jiffies = jiffies;
2798 bond->current_arp_slave = slave;
2802 /* if the link state is up at this point, we
2803 * mark it down - this can happen if we have
2804 * simultaneous link failures and
2805 * reselect_active_interface doesn't make this
2806 * one the current slave so it is still marked
2807 * up when it is actually down
2809 if (slave->link == BOND_LINK_UP) {
2810 slave->link = BOND_LINK_DOWN;
2811 if (slave->link_failure_count <
2813 slave->link_failure_count++;
2816 bond_set_slave_inactive_flags(slave);
2818 "%s: backup interface "
2819 "%s is now down.\n",
2823 } while ((slave = slave->next) !=
2824 bond->current_arp_slave->next);
2828 read_unlock(&bond->lock);
2829 mod_timer(&bond->arp_timer, next_timer);
2832 static int bond_sethwaddr(struct net_device *master, struct net_device *slave)
2834 #ifdef BONDING_DEBUG
2835 printk(KERN_CRIT "bond_sethwaddr: master=%x\n", (unsigned int)master);
2836 printk(KERN_CRIT "bond_sethwaddr: slave=%x\n", (unsigned int)slave);
2837 printk(KERN_CRIT "bond_sethwaddr: slave->addr_len=%d\n", slave->addr_len);
2839 memcpy(master->dev_addr, slave->dev_addr, slave->addr_len);
2843 static int bond_info_query(struct net_device *master, struct ifbond *info)
2845 bonding_t *bond = (struct bonding *) master->priv;
2848 info->bond_mode = bond_mode;
2849 info->num_slaves = 0;
2850 info->miimon = miimon;
2852 read_lock_bh(&bond->lock);
2853 for (slave = bond->prev; slave != (slave_t *)bond; slave = slave->prev) {
2856 read_unlock_bh(&bond->lock);
2861 static int bond_slave_info_query(struct net_device *master,
2862 struct ifslave *info)
2864 bonding_t *bond = (struct bonding *) master->priv;
2868 if (info->slave_id < 0) {
2872 read_lock_bh(&bond->lock);
2873 for (slave = bond->prev;
2874 slave != (slave_t *)bond && cur_ndx < info->slave_id;
2875 slave = slave->prev) {
2878 read_unlock_bh(&bond->lock);
2880 if (slave != (slave_t *)bond) {
2881 strcpy(info->slave_name, slave->dev->name);
2882 info->link = slave->link;
2883 info->state = slave->state;
2884 info->link_failure_count = slave->link_failure_count;
2892 static int bond_ethtool_ioctl(struct net_device *master_dev, struct ifreq *ifr)
2894 void *addr = ifr->ifr_data;
2897 if (get_user(cmd, (uint32_t *) addr))
2902 case ETHTOOL_GDRVINFO:
2904 struct ethtool_drvinfo info;
2907 if (copy_from_user(&info, addr, sizeof(info)))
2910 if (strcmp(info.driver, "ifenslave") == 0) {
2913 new_abi_ver = simple_strtoul(info.fw_version,
2917 "bonding: Error: got invalid ABI"
2918 " version from application\n");
2923 if (orig_app_abi_ver == -1) {
2924 orig_app_abi_ver = new_abi_ver;
2927 app_abi_ver = new_abi_ver;
2930 strncpy(info.driver, DRV_NAME, 32);
2931 strncpy(info.version, DRV_VERSION, 32);
2932 snprintf(info.fw_version, 32, "%d", BOND_ABI_VERSION);
2934 if (copy_to_user(addr, &info, sizeof(info)))
2945 static int bond_ioctl(struct net_device *master_dev, struct ifreq *ifr, int cmd)
2947 struct net_device *slave_dev = NULL;
2948 struct ifbond *u_binfo = NULL, k_binfo;
2949 struct ifslave *u_sinfo = NULL, k_sinfo;
2950 struct mii_ioctl_data *mii = NULL;
2951 int prev_abi_ver = orig_app_abi_ver;
2954 #ifdef BONDING_DEBUG
2955 printk(KERN_INFO "bond_ioctl: master=%s, cmd=%d\n",
2956 master_dev->name, cmd);
2961 return bond_ethtool_ioctl(master_dev, ifr);
2964 mii = (struct mii_ioctl_data *)&ifr->ifr_data;
2972 * We do this again just in case we were called by SIOCGMIIREG
2973 * instead of SIOCGMIIPHY.
2975 mii = (struct mii_ioctl_data *)&ifr->ifr_data;
2979 if (mii->reg_num == 1) {
2980 mii->val_out = bond_check_mii_link(
2981 (struct bonding *)master_dev->priv);
2984 case BOND_INFO_QUERY_OLD:
2985 case SIOCBONDINFOQUERY:
2986 u_binfo = (struct ifbond *)ifr->ifr_data;
2987 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
2990 ret = bond_info_query(master_dev, &k_binfo);
2992 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
2997 case BOND_SLAVE_INFO_QUERY_OLD:
2998 case SIOCBONDSLAVEINFOQUERY:
2999 u_sinfo = (struct ifslave *)ifr->ifr_data;
3000 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
3003 ret = bond_slave_info_query(master_dev, &k_sinfo);
3005 if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
3012 if (!capable(CAP_NET_ADMIN)) {
3016 if (orig_app_abi_ver == -1) {
3017 /* no orig_app_abi_ver was provided yet, so we'll use the
3018 * current one from now on, even if it's 0
3020 orig_app_abi_ver = app_abi_ver;
3022 } else if (orig_app_abi_ver != app_abi_ver) {
3024 "bonding: Error: already using ifenslave ABI "
3025 "version %d; to upgrade ifenslave to version %d, "
3026 "you must first reload bonding.\n",
3027 orig_app_abi_ver, app_abi_ver);
3031 slave_dev = dev_get_by_name(ifr->ifr_slave);
3033 #ifdef BONDING_DEBUG
3034 printk(KERN_INFO "slave_dev=%x: \n", (unsigned int)slave_dev);
3035 printk(KERN_INFO "slave_dev->name=%s: \n", slave_dev->name);
3038 if (slave_dev == NULL) {
3042 case BOND_ENSLAVE_OLD:
3043 case SIOCBONDENSLAVE:
3044 ret = bond_enslave(master_dev, slave_dev);
3046 case BOND_RELEASE_OLD:
3047 case SIOCBONDRELEASE:
3048 ret = bond_release(master_dev, slave_dev);
3050 case BOND_SETHWADDR_OLD:
3051 case SIOCBONDSETHWADDR:
3052 ret = bond_sethwaddr(master_dev, slave_dev);
3054 case BOND_CHANGE_ACTIVE_OLD:
3055 case SIOCBONDCHANGEACTIVE:
3056 if (USES_PRIMARY(bond_mode)) {
3057 ret = bond_change_active(master_dev, slave_dev);
3070 /* The ioctl failed, so there's no point in changing the
3071 * orig_app_abi_ver. We'll restore it's value just in case
3072 * we've changed it earlier in this function.
3074 orig_app_abi_ver = prev_abi_ver;
3080 #ifdef CONFIG_NET_FASTROUTE
3081 static int bond_accept_fastpath(struct net_device *dev, struct dst_entry *dst)
3088 * in broadcast mode, we send everything to all usable interfaces.
3090 static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *dev)
3092 slave_t *slave, *start_at;
3093 struct bonding *bond = (struct bonding *) dev->priv;
3094 struct net_device *device_we_should_send_to = 0;
3096 if (!IS_UP(dev)) { /* bond down */
3101 read_lock(&bond->lock);
3103 read_lock(&bond->ptrlock);
3104 slave = start_at = bond->current_slave;
3105 read_unlock(&bond->ptrlock);
3107 if (slave == NULL) { /* we're at the root, get the first slave */
3108 /* no suitable interface, frame not sent */
3109 read_unlock(&bond->lock);
3115 if (IS_UP(slave->dev)
3116 && (slave->link == BOND_LINK_UP)
3117 && (slave->state == BOND_STATE_ACTIVE)) {
3118 if (device_we_should_send_to) {
3119 struct sk_buff *skb2;
3120 if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
3121 printk(KERN_ERR "bond_xmit_broadcast: skb_clone() failed\n");
3125 skb2->dev = device_we_should_send_to;
3127 dev_queue_xmit(skb2);
3129 device_we_should_send_to = slave->dev;
3131 } while ((slave = slave->next) != start_at);
3133 if (device_we_should_send_to) {
3134 skb->dev = device_we_should_send_to;
3136 dev_queue_xmit(skb);
3140 /* frame sent to all suitable interfaces */
3141 read_unlock(&bond->lock);
3145 static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *dev)
3147 slave_t *slave, *start_at;
3148 struct bonding *bond = (struct bonding *) dev->priv;
3150 if (!IS_UP(dev)) { /* bond down */
3155 read_lock(&bond->lock);
3157 read_lock(&bond->ptrlock);
3158 slave = start_at = bond->current_slave;
3159 read_unlock(&bond->ptrlock);
3161 if (slave == NULL) { /* we're at the root, get the first slave */
3162 /* no suitable interface, frame not sent */
3164 read_unlock(&bond->lock);
3169 if (IS_UP(slave->dev)
3170 && (slave->link == BOND_LINK_UP)
3171 && (slave->state == BOND_STATE_ACTIVE)) {
3173 skb->dev = slave->dev;
3175 dev_queue_xmit(skb);
3177 write_lock(&bond->ptrlock);
3178 bond->current_slave = slave->next;
3179 write_unlock(&bond->ptrlock);
3181 read_unlock(&bond->lock);
3184 } while ((slave = slave->next) != start_at);
3186 /* no suitable interface, frame not sent */
3188 read_unlock(&bond->lock);
3193 * in XOR mode, we determine the output device by performing xor on
3194 * the source and destination hw adresses. If this device is not
3195 * enabled, find the next slave following this xor slave.
3197 static int bond_xmit_xor(struct sk_buff *skb, struct net_device *dev)
3199 slave_t *slave, *start_at;
3200 struct bonding *bond = (struct bonding *) dev->priv;
3201 struct ethhdr *data = (struct ethhdr *)skb->data;
3204 if (!IS_UP(dev)) { /* bond down */
3209 read_lock(&bond->lock);
3212 /* we're at the root, get the first slave */
3213 if (bond->slave_cnt == 0) {
3214 /* no suitable interface, frame not sent */
3216 read_unlock(&bond->lock);
3220 slave_no = (data->h_dest[5]^slave->dev->dev_addr[5]) % bond->slave_cnt;
3222 while ( (slave_no > 0) && (slave != (slave_t *)bond) ) {
3223 slave = slave->prev;
3229 if (IS_UP(slave->dev)
3230 && (slave->link == BOND_LINK_UP)
3231 && (slave->state == BOND_STATE_ACTIVE)) {
3233 skb->dev = slave->dev;
3235 dev_queue_xmit(skb);
3237 read_unlock(&bond->lock);
3240 } while ((slave = slave->next) != start_at);
3242 /* no suitable interface, frame not sent */
3244 read_unlock(&bond->lock);
3249 * in active-backup mode, we know that bond->current_slave is always valid if
3250 * the bond has a usable interface.
3252 static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *dev)
3254 struct bonding *bond = (struct bonding *) dev->priv;
3257 if (!IS_UP(dev)) { /* bond down */
3262 /* if we are sending arp packets, try to at least
3263 identify our own ip address */
3264 if ( (arp_interval > 0) && (my_ip == 0) &&
3265 (skb->protocol == __constant_htons(ETH_P_ARP) ) ) {
3266 char *the_ip = (((char *)skb->data))
3267 + sizeof(struct ethhdr)
3268 + sizeof(struct arphdr) +
3270 memcpy(&my_ip, the_ip, 4);
3273 /* if we are sending arp packets and don't know
3274 * the target hw address, save it so we don't need
3275 * to use a broadcast address.
3276 * don't do this if in active backup mode because the slaves must
3277 * receive packets to stay up, and the only ones they receive are
3280 if ( (bond_mode != BOND_MODE_ACTIVEBACKUP) &&
3281 (arp_ip_count == 1) &&
3282 (arp_interval > 0) && (arp_target_hw_addr == NULL) &&
3283 (skb->protocol == __constant_htons(ETH_P_IP) ) ) {
3284 struct ethhdr *eth_hdr =
3285 (struct ethhdr *) (((char *)skb->data));
3286 struct iphdr *ip_hdr = (struct iphdr *)(eth_hdr + 1);
3288 if (arp_target[0] == ip_hdr->daddr) {
3289 arp_target_hw_addr = kmalloc(ETH_ALEN, GFP_KERNEL);
3290 if (arp_target_hw_addr != NULL)
3291 memcpy(arp_target_hw_addr, eth_hdr->h_dest, ETH_ALEN);
3295 read_lock(&bond->lock);
3297 read_lock(&bond->ptrlock);
3298 if (bond->current_slave != NULL) { /* one usable interface */
3299 skb->dev = bond->current_slave->dev;
3300 read_unlock(&bond->ptrlock);
3302 ret = dev_queue_xmit(skb);
3303 read_unlock(&bond->lock);
3307 read_unlock(&bond->ptrlock);
3310 /* no suitable interface, frame not sent */
3311 #ifdef BONDING_DEBUG
3312 printk(KERN_INFO "There was no suitable interface, so we don't transmit\n");
3315 read_unlock(&bond->lock);
3319 static struct net_device_stats *bond_get_stats(struct net_device *dev)
3321 bonding_t *bond = dev->priv;
3322 struct net_device_stats *stats = &(bond->stats), *sstats;
3325 memset(stats, 0, sizeof(struct net_device_stats));
3327 read_lock_bh(&bond->lock);
3329 for (slave = bond->prev; slave != (slave_t *)bond; slave = slave->prev) {
3330 sstats = slave->dev->get_stats(slave->dev);
3332 stats->rx_packets += sstats->rx_packets;
3333 stats->rx_bytes += sstats->rx_bytes;
3334 stats->rx_errors += sstats->rx_errors;
3335 stats->rx_dropped += sstats->rx_dropped;
3337 stats->tx_packets += sstats->tx_packets;
3338 stats->tx_bytes += sstats->tx_bytes;
3339 stats->tx_errors += sstats->tx_errors;
3340 stats->tx_dropped += sstats->tx_dropped;
3342 stats->multicast += sstats->multicast;
3343 stats->collisions += sstats->collisions;
3345 stats->rx_length_errors += sstats->rx_length_errors;
3346 stats->rx_over_errors += sstats->rx_over_errors;
3347 stats->rx_crc_errors += sstats->rx_crc_errors;
3348 stats->rx_frame_errors += sstats->rx_frame_errors;
3349 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3350 stats->rx_missed_errors += sstats->rx_missed_errors;
3352 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3353 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3354 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3355 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3356 stats->tx_window_errors += sstats->tx_window_errors;
3360 read_unlock_bh(&bond->lock);
3364 #ifdef CONFIG_PROC_FS
3366 #define SEQ_START_TOKEN ((void *)1)
3368 static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
3370 struct bonding *bond = seq->private;
3372 struct slave *slave;
3374 /* make sure the bond won't be taken away */
3375 read_lock(&dev_base_lock);
3376 read_lock_bh(&bond->lock);
3379 return SEQ_START_TOKEN;
3382 for (slave = bond->prev; slave != (slave_t *)bond;
3383 slave = slave->prev) {
3385 if (++off == *pos) {
3393 static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3395 struct bonding *bond = seq->private;
3396 struct slave *slave = v;
3399 if (v == SEQ_START_TOKEN) {
3402 slave = slave->prev;
3405 return (slave == (struct slave *) bond) ? NULL : slave;
3408 static void bond_info_seq_stop(struct seq_file *seq, void *v)
3410 struct bonding *bond = seq->private;
3412 read_unlock_bh(&bond->lock);
3413 read_unlock(&dev_base_lock);
3416 static void bond_info_show_master(struct seq_file *seq, struct bonding *bond)
3420 read_lock(&bond->ptrlock);
3421 curr = bond->current_slave;
3422 read_unlock(&bond->ptrlock);
3424 seq_printf(seq, "Bonding Mode: %s\n", bond_mode_name());
3426 if (USES_PRIMARY(bond_mode)) {
3429 "Currently Active Slave: %s\n",
3434 seq_printf(seq, "MII Status: %s\n", (curr) ? "up" : "down");
3435 seq_printf(seq, "MII Polling Interval (ms): %d\n", miimon);
3436 seq_printf(seq, "Up Delay (ms): %d\n", updelay * miimon);
3437 seq_printf(seq, "Down Delay (ms): %d\n", downdelay * miimon);
3438 seq_printf(seq, "Multicast Mode: %s\n", multicast_mode_name());
3440 if (bond_mode == BOND_MODE_8023AD) {
3441 struct ad_info ad_info;
3443 seq_puts(seq, "\n802.3ad info\n");
3445 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3446 seq_printf(seq, "bond %s has no active aggregator\n",
3447 bond->device->name);
3449 seq_printf(seq, "Active Aggregator Info:\n");
3451 seq_printf(seq, "\tAggregator ID: %d\n",
3452 ad_info.aggregator_id);
3453 seq_printf(seq, "\tNumber of ports: %d\n",
3455 seq_printf(seq, "\tActor Key: %d\n",
3457 seq_printf(seq, "\tPartner Key: %d\n",
3458 ad_info.partner_key);
3459 seq_printf(seq, "\tPartner Mac Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
3460 ad_info.partner_system[0],
3461 ad_info.partner_system[1],
3462 ad_info.partner_system[2],
3463 ad_info.partner_system[3],
3464 ad_info.partner_system[4],
3465 ad_info.partner_system[5]);
3470 static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
3472 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3473 seq_printf(seq, "MII Status: %s\n",
3474 (slave->link == BOND_LINK_UP) ? "up" : "down");
3475 seq_printf(seq, "Link Failure Count: %d\n",
3476 slave->link_failure_count);
3478 if (app_abi_ver >= 1) {
3480 "Permanent HW addr: %02x:%02x:%02x:%02x:%02x:%02x\n",
3481 slave->perm_hwaddr[0],
3482 slave->perm_hwaddr[1],
3483 slave->perm_hwaddr[2],
3484 slave->perm_hwaddr[3],
3485 slave->perm_hwaddr[4],
3486 slave->perm_hwaddr[5]);
3489 if (bond_mode == BOND_MODE_8023AD) {
3490 const struct aggregator *agg
3491 = SLAVE_AD_INFO(slave).port.aggregator;
3494 seq_printf(seq, "Aggregator ID: %d\n",
3495 agg->aggregator_identifier);
3497 seq_puts(seq, "Aggregator ID: N/A\n");
3502 static int bond_info_seq_show(struct seq_file *seq, void *v)
3504 if (v == SEQ_START_TOKEN) {
3505 seq_printf(seq, "%s\n", version);
3506 bond_info_show_master(seq, seq->private);
3508 bond_info_show_slave(seq, v);
3514 static struct seq_operations bond_info_seq_ops = {
3515 .start = bond_info_seq_start,
3516 .next = bond_info_seq_next,
3517 .stop = bond_info_seq_stop,
3518 .show = bond_info_seq_show,
3521 static int bond_info_open(struct inode *inode, struct file *file)
3523 struct seq_file *seq;
3524 struct proc_dir_entry *proc;
3527 rc = seq_open(file, &bond_info_seq_ops);
3529 /* recover the pointer buried in proc_dir_entry data */
3530 seq = file->private_data;
3532 seq->private = proc->data;
3537 static struct file_operations bond_info_fops = {
3538 .owner = THIS_MODULE,
3539 .open = bond_info_open,
3541 .llseek = seq_lseek,
3542 .release = seq_release,
3545 static int bond_create_proc_info(struct bonding *bond)
3547 struct net_device *dev = bond->device;
3549 if (bond_proc_dir) {
3550 bond->bond_proc_file = create_proc_entry(dev->name,
3553 if (bond->bond_proc_file == NULL) {
3555 "%s: Cannot create /proc/net/bonding/%s\n",
3556 dev->name, dev->name);
3558 bond->bond_proc_file->data = bond;
3559 bond->bond_proc_file->proc_fops = &bond_info_fops;
3560 bond->bond_proc_file->owner = THIS_MODULE;
3561 memcpy(bond->procdir_name, dev->name, IFNAMSIZ);
3568 static void bond_destroy_proc_info(struct bonding *bond)
3570 if (bond_proc_dir && bond->bond_proc_file) {
3571 remove_proc_entry(bond->procdir_name, bond_proc_dir);
3572 memset(bond->procdir_name, 0, IFNAMSIZ);
3573 bond->bond_proc_file = NULL;
3577 /* Create the bonding directory under /proc/net, if doesn't exist yet.
3578 * Caller must hold rtnl_lock.
3580 static void bond_create_proc_dir(void)
3582 int len = strlen(DRV_NAME);
3584 for (bond_proc_dir = proc_net->subdir; bond_proc_dir;
3585 bond_proc_dir = bond_proc_dir->next) {
3586 if ((bond_proc_dir->namelen == len) &&
3587 !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3592 if (!bond_proc_dir) {
3593 bond_proc_dir = proc_mkdir(DRV_NAME, proc_net);
3594 if (bond_proc_dir) {
3595 bond_proc_dir->owner = THIS_MODULE;
3597 printk(KERN_WARNING DRV_NAME
3598 ": Warning: cannot create /proc/net/%s\n",
3604 /* Destroy the bonding directory under /proc/net, if empty.
3605 * Caller must hold rtnl_lock.
3607 static void bond_destroy_proc_dir(void)
3609 struct proc_dir_entry *de;
3611 if (!bond_proc_dir) {
3615 /* verify that the /proc dir is empty */
3616 for (de = bond_proc_dir->subdir; de; de = de->next) {
3617 /* ignore . and .. */
3618 if (*(de->name) != '.') {
3624 if (bond_proc_dir->owner == THIS_MODULE) {
3625 bond_proc_dir->owner = NULL;
3628 remove_proc_entry(DRV_NAME, proc_net);
3629 bond_proc_dir = NULL;
3632 #endif /* CONFIG_PROC_FS */
3637 * Note that many devices must be down to change the HW address, and
3638 * downing the master releases all slaves. We can make bonds full of
3639 * bonding devices to test this, however.
3642 bond_set_mac_address(struct net_device *dev, void *addr)
3644 struct bonding *bond = dev->priv;
3645 struct sockaddr *sa = addr, tmp_sa;
3646 struct slave *slave;
3649 dprintk(KERN_INFO "bond_set_mac_address %p %s\n", dev,
3652 if (!is_valid_ether_addr(sa->sa_data)) {
3653 return -EADDRNOTAVAIL;
3656 for (slave = bond->prev; slave != (struct slave *)bond;
3657 slave = slave->prev) {
3658 dprintk(KERN_INFO "bond_set_mac: slave %p %s\n", slave,
3660 if (slave->dev->set_mac_address == NULL) {
3661 error = -EOPNOTSUPP;
3662 dprintk(KERN_INFO "bond_set_mac EOPNOTSUPP %s\n",
3667 error = slave->dev->set_mac_address(slave->dev, addr);
3669 /* TODO: consider downing the slave
3671 * User should expect communications
3672 * breakage anyway until ARP finish
3675 dprintk(KERN_INFO "bond_set_mac err %d %s\n",
3676 error, slave->dev->name);
3682 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
3686 memcpy(tmp_sa.sa_data, dev->dev_addr, dev->addr_len);
3687 tmp_sa.sa_family = dev->type;
3689 for (slave = slave->next; slave != bond->next;
3690 slave = slave->next) {
3693 tmp_error = slave->dev->set_mac_address(slave->dev, &tmp_sa);
3695 dprintk(KERN_INFO "bond_set_mac_address: "
3696 "unwind err %d dev %s\n",
3697 tmp_error, slave->dev->name);
3705 * Change the MTU of all of a master's slaves to match the master
3708 bond_change_mtu(struct net_device *dev, int newmtu)
3710 bonding_t *bond = dev->priv;
3714 dprintk(KERN_INFO "CM: b %p nm %d\n", bond, newmtu);
3715 for (slave = bond->prev; slave != (slave_t *)bond;
3716 slave = slave->prev) {
3717 dprintk(KERN_INFO "CM: s %p s->p %p c_m %p\n", slave,
3718 slave->prev, slave->dev->change_mtu);
3719 if (slave->dev->change_mtu) {
3720 error = slave->dev->change_mtu(slave->dev, newmtu);
3722 slave->dev->mtu = newmtu;
3727 /* If we failed to set the slave's mtu to the new value
3728 * we must abort the operation even in ACTIVE_BACKUP
3729 * mode, because if we allow the backup slaves to have
3730 * different mtu values than the active slave we'll
3731 * need to change their mtu when doing a failover. That
3732 * means changing their mtu from timer context, which
3733 * is probably not a good idea.
3735 dprintk(KERN_INFO "bond_change_mtu err %d %s\n",
3736 error, slave->dev->name);
3746 for (slave = slave->next; slave != bond->next;
3747 slave = slave->next) {
3749 if (slave->dev->change_mtu) {
3750 slave->dev->change_mtu(slave->dev, dev->mtu);
3752 slave->dev->mtu = dev->mtu;
3760 * Change device name
3762 static inline int bond_event_changename(struct bonding *bond)
3764 #ifdef CONFIG_PROC_FS
3765 bond_destroy_proc_info(bond);
3766 bond_create_proc_info(bond);
3772 static int bond_master_netdev_event(unsigned long event, struct net_device *event_dev)
3774 struct bonding *bond, *event_bond = NULL;
3776 list_for_each_entry(bond, &bond_dev_list, bond_list) {
3777 if (bond == event_dev->priv) {
3783 if (event_bond == NULL) {
3788 case NETDEV_CHANGENAME:
3789 return bond_event_changename(event_bond);
3790 case NETDEV_UNREGISTER:
3792 * TODO: remove a bond from the list?
3802 static int bond_slave_netdev_event(unsigned long event, struct net_device *event_dev)
3804 struct net_device *master = event_dev->master;
3807 case NETDEV_UNREGISTER:
3808 if (master != NULL) {
3809 bond_release(master, event_dev);
3814 * TODO: is this what we get if somebody
3815 * sets up a hierarchical bond, then rmmod's
3816 * one of the slave bonding devices?
3821 * ... Or is it this?
3824 case NETDEV_CHANGEMTU:
3826 * TODO: Should slaves be allowed to
3827 * independently alter their MTU? For
3828 * an active-backup bond, slaves need
3829 * not be the same type of device, so
3830 * MTUs may vary. For other modes,
3831 * slaves arguably should have the
3832 * same MTUs. To do this, we'd need to
3833 * take over the slave's change_mtu
3834 * function for the duration of their
3838 case NETDEV_CHANGENAME:
3840 * TODO: handle changing the primary's name
3851 * bond_netdev_event: handle netdev notifier chain events.
3853 * This function receives events for the netdev chain. The caller (an
3854 * ioctl handler calling notifier_call_chain) holds the necessary
3855 * locks for us to safely manipulate the slave devices (RTNL lock,
3858 static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3860 struct net_device *event_dev = (struct net_device *)ptr;
3861 unsigned short flags;
3862 int res = NOTIFY_DONE;
3864 dprintk(KERN_INFO "bond_netdev_event n_b %p ev %lx ptr %p\n",
3867 flags = event_dev->flags & (IFF_MASTER | IFF_SLAVE);
3870 res = bond_master_netdev_event(event, event_dev);
3873 res = bond_slave_netdev_event(event, event_dev);
3876 /* A master that is also a slave ? */
3883 static struct notifier_block bond_netdev_notifier = {
3884 .notifier_call = bond_netdev_event,
3887 /* De-initialize device specific data.
3888 * Caller must hold rtnl_lock.
3890 static inline void bond_deinit(struct net_device *dev)
3892 struct bonding *bond = dev->priv;
3894 list_del(&bond->bond_list);
3896 #ifdef CONFIG_PROC_FS
3897 bond_destroy_proc_info(bond);
3901 /* Unregister and free all bond devices.
3902 * Caller must hold rtnl_lock.
3904 static void bond_free_all(void)
3906 struct bonding *bond, *nxt;
3908 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
3909 struct net_device *dev = bond->device;
3911 unregister_netdevice(dev);
3915 #ifdef CONFIG_PROC_FS
3916 bond_destroy_proc_dir();
3921 * Does not allocate but creates a /proc entry.
3924 static int __init bond_init(struct net_device *dev)
3926 struct bonding *bond;
3929 #ifdef BONDING_DEBUG
3930 printk (KERN_INFO "Begin bond_init for %s\n", dev->name);
3934 /* initialize rwlocks */
3935 rwlock_init(&bond->lock);
3936 rwlock_init(&bond->ptrlock);
3938 /* Initialize pointers */
3939 bond->next = bond->prev = (slave_t *)bond;
3940 bond->current_slave = NULL;
3941 bond->current_arp_slave = NULL;
3944 /* Initialize the device structure. */
3945 dev->set_mac_address = bond_set_mac_address;
3947 switch (bond_mode) {
3948 case BOND_MODE_ACTIVEBACKUP:
3949 dev->hard_start_xmit = bond_xmit_activebackup;
3951 case BOND_MODE_ROUNDROBIN:
3952 dev->hard_start_xmit = bond_xmit_roundrobin;
3955 dev->hard_start_xmit = bond_xmit_xor;
3957 case BOND_MODE_BROADCAST:
3958 dev->hard_start_xmit = bond_xmit_broadcast;
3960 case BOND_MODE_8023AD:
3961 dev->hard_start_xmit = bond_3ad_xmit_xor;
3965 dev->hard_start_xmit = bond_alb_xmit;
3966 dev->set_mac_address = bond_alb_set_mac_address;
3969 printk(KERN_ERR "Unknown bonding mode %d\n", bond_mode);
3973 dev->get_stats = bond_get_stats;
3974 dev->open = bond_open;
3975 dev->stop = bond_close;
3976 dev->set_multicast_list = set_multicast_list;
3977 dev->do_ioctl = bond_ioctl;
3978 dev->change_mtu = bond_change_mtu;
3979 dev->tx_queue_len = 0;
3980 dev->flags |= IFF_MASTER|IFF_MULTICAST;
3981 #ifdef CONFIG_NET_FASTROUTE
3982 dev->accept_fastpath = bond_accept_fastpath;
3985 printk(KERN_INFO "%s registered with", dev->name);
3987 printk(" MII link monitoring set to %d ms", miimon);
3989 downdelay /= miimon;
3991 printk("out MII link monitoring");
3993 printk(", in %s mode.\n", bond_mode_name());
3995 printk(KERN_INFO "%s registered with", dev->name);
3996 if (arp_interval > 0) {
3997 printk(" ARP monitoring set to %d ms with %d target(s):",
3998 arp_interval, arp_ip_count);
3999 for (count=0 ; count<arp_ip_count ; count++)
4000 printk (" %s", arp_ip_target[count]);
4003 printk("out ARP monitoring\n");
4006 #ifdef CONFIG_PROC_FS
4007 bond_create_proc_info(bond);
4010 dev->destructor = free_netdev;
4012 list_add_tail(&bond->bond_list, &bond_dev_list);
4018 static int __init bond_probe(struct net_device *dev)
4026 * Convert string input module parms. Accept either the
4027 * number of the mode or its string name.
4030 bond_parse_parm(char *mode_arg, struct bond_parm_tbl *tbl)
4034 for (i = 0; tbl[i].modename != NULL; i++) {
4035 if ((isdigit(*mode_arg) &&
4036 tbl[i].mode == simple_strtol(mode_arg, NULL, 0)) ||
4037 (0 == strncmp(mode_arg, tbl[i].modename,
4038 strlen(tbl[i].modename)))) {
4047 static int __init bonding_init(void)
4052 printk(KERN_INFO "%s", version);
4055 * Convert string parameters.
4058 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4059 if (bond_mode == -1) {
4061 "bonding_init(): Invalid bonding mode \"%s\"\n",
4062 mode == NULL ? "NULL" : mode);
4067 if (USES_PRIMARY(bond_mode)) {
4068 multicast_mode = BOND_MULTICAST_ACTIVE;
4070 multicast_mode = BOND_MULTICAST_ALL;
4074 multicast_mode = bond_parse_parm(multicast, bond_mc_tbl);
4075 if (multicast_mode == -1) {
4077 "bonding_init(): Invalid multicast mode \"%s\"\n",
4078 multicast == NULL ? "NULL" : multicast);
4084 if (bond_mode != BOND_MODE_8023AD) {
4086 "lacp_rate param is irrelevant in mode %s\n",
4089 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4090 if (lacp_fast == -1) {
4092 "bonding_init(): Invalid lacp rate "
4094 lacp_rate == NULL ? "NULL" : lacp_rate);
4101 if (max_bonds < 1 || max_bonds > INT_MAX) {
4103 "bonding_init(): max_bonds (%d) not in range %d-%d, "
4104 "so it was reset to BOND_DEFAULT_MAX_BONDS (%d)",
4105 max_bonds, 1, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4106 max_bonds = BOND_DEFAULT_MAX_BONDS;
4111 "bonding_init(): miimon module parameter (%d), "
4112 "not in range 0-%d, so it was reset to %d\n",
4113 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4114 miimon = BOND_LINK_MON_INTERV;
4119 "bonding_init(): updelay module parameter (%d), "
4120 "not in range 0-%d, so it was reset to 0\n",
4125 if (downdelay < 0) {
4127 "bonding_init(): downdelay module parameter (%d), "
4128 "not in range 0-%d, so it was reset to 0\n",
4129 downdelay, INT_MAX);
4133 /* reset values for 802.3ad */
4134 if (bond_mode == BOND_MODE_8023AD) {
4135 if (arp_interval != 0) {
4136 printk(KERN_WARNING "bonding_init(): ARP monitoring"
4137 "can't be used simultaneously with 802.3ad, "
4138 "disabling ARP monitoring\n");
4144 "bonding_init(): miimon must be specified, "
4145 "otherwise bonding will not detect link failure, "
4146 "speed and duplex which are essential "
4147 "for 802.3ad operation\n");
4148 printk(KERN_ERR "Forcing miimon to 100msec\n");
4152 if (multicast_mode != BOND_MULTICAST_ALL) {
4154 "bonding_init(): Multicast mode must "
4155 "be set to ALL for 802.3ad\n");
4156 printk(KERN_ERR "Forcing Multicast mode to ALL\n");
4157 multicast_mode = BOND_MULTICAST_ALL;
4161 /* reset values for TLB/ALB */
4162 if ((bond_mode == BOND_MODE_TLB) ||
4163 (bond_mode == BOND_MODE_ALB)) {
4166 "bonding_init(): miimon must be specified, "
4167 "otherwise bonding will not detect link failure "
4168 "and link speed which are essential "
4169 "for TLB/ALB load balancing\n");
4170 printk(KERN_ERR "Forcing miimon to 100msec\n");
4174 if (multicast_mode != BOND_MULTICAST_ACTIVE) {
4176 "bonding_init(): Multicast mode must "
4177 "be set to ACTIVE for TLB/ALB\n");
4178 printk(KERN_ERR "Forcing Multicast mode to ACTIVE\n");
4179 multicast_mode = BOND_MULTICAST_ACTIVE;
4183 if (bond_mode == BOND_MODE_ALB) {
4185 "In ALB mode you might experience client disconnections"
4186 " upon reconnection of a link if the bonding module"
4187 " updelay parameter (%d msec) is incompatible with the"
4188 " forwarding delay time of the switch\n", updelay);
4192 if ((updelay != 0) || (downdelay != 0)) {
4193 /* just warn the user the up/down delay will have
4194 * no effect since miimon is zero...
4197 "bonding_init(): miimon module parameter not "
4198 "set and updelay (%d) or downdelay (%d) module "
4199 "parameter is set; updelay and downdelay have "
4200 "no effect unless miimon is set\n",
4201 updelay, downdelay);
4204 /* don't allow arp monitoring */
4205 if (arp_interval != 0) {
4207 "bonding_init(): miimon (%d) and arp_interval "
4208 "(%d) can't be used simultaneously, "
4209 "disabling ARP monitoring\n",
4210 miimon, arp_interval);
4214 if ((updelay % miimon) != 0) {
4215 /* updelay will be rounded in bond_init() when it
4216 * is divided by miimon, we just inform user here
4219 "bonding_init(): updelay (%d) is not a multiple "
4220 "of miimon (%d), updelay rounded to %d ms\n",
4221 updelay, miimon, (updelay / miimon) * miimon);
4224 if ((downdelay % miimon) != 0) {
4225 /* downdelay will be rounded in bond_init() when it
4226 * is divided by miimon, we just inform user here
4229 "bonding_init(): downdelay (%d) is not a "
4230 "multiple of miimon (%d), downdelay rounded "
4233 (downdelay / miimon) * miimon);
4237 if (arp_interval < 0) {
4239 "bonding_init(): arp_interval module parameter (%d), "
4240 "not in range 0-%d, so it was reset to %d\n",
4241 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4242 arp_interval = BOND_LINK_ARP_INTERV;
4245 for (arp_ip_count=0 ;
4246 (arp_ip_count < MAX_ARP_IP_TARGETS) && arp_ip_target[arp_ip_count];
4248 /* not complete check, but should be good enough to
4250 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4252 "bonding_init(): bad arp_ip_target module "
4253 "parameter (%s), ARP monitoring will not be "
4255 arp_ip_target[arp_ip_count]);
4258 u32 ip = in_aton(arp_ip_target[arp_ip_count]);
4259 arp_target[arp_ip_count] = ip;
4264 if ( (arp_interval > 0) && (arp_ip_count==0)) {
4265 /* don't allow arping if no arp_ip_target given... */
4267 "bonding_init(): arp_interval module parameter "
4268 "(%d) specified without providing an arp_ip_target "
4269 "parameter, arp_interval was reset to 0\n",
4274 if ((miimon == 0) && (arp_interval == 0)) {
4275 /* miimon and arp_interval not set, we need one so things
4276 * work as expected, see bonding.txt for details
4279 "bonding_init(): either miimon or "
4280 "arp_interval and arp_ip_target module parameters "
4281 "must be specified, otherwise bonding will not detect "
4282 "link failures! see bonding.txt for details.\n");
4285 if ((primary != NULL) && !USES_PRIMARY(bond_mode)) {
4286 /* currently, using a primary only makes sense
4287 * in active backup, TLB or ALB modes
4290 "bonding_init(): %s primary device specified but has "
4291 "no effect in %s mode\n",
4292 primary, bond_mode_name());
4298 #ifdef CONFIG_PROC_FS
4299 bond_create_proc_dir();
4303 for (no = 0; no < max_bonds; no++) {
4304 struct net_device *dev;
4306 dev = alloc_netdev(sizeof(struct bonding), "", ether_setup);
4312 err = dev_alloc_name(dev, "bond%d");
4318 /* bond_init() must be called after dev_alloc_name() (for the
4319 * /proc files), but before register_netdevice(), because we
4320 * need to set function pointers.
4322 err = bond_init(dev);
4328 SET_MODULE_OWNER(dev);
4330 err = register_netdevice(dev);
4339 register_netdevice_notifier(&bond_netdev_notifier);
4344 /* free and unregister all bonds that were successfully added */
4352 static void __exit bonding_exit(void)
4354 unregister_netdevice_notifier(&bond_netdev_notifier);
4361 module_init(bonding_init);
4362 module_exit(bonding_exit);
4363 MODULE_LICENSE("GPL");
4364 MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);