Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / bonding / bond_main.c
blob770e28f98fd5329a7434c47be96d3f85f1cf33dc
1 /*
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:
10 * Cisco 5500
11 * Sun Trunking (Solaris)
12 * Alteon AceDirector Trunks
13 * Linux Bonding
14 * and probably many L2 switches ...
16 * How it works:
17 * ifconfig bond0 ipaddress netmask up
18 * will setup a network device, with an ip address. No mac address
19 * will be assigned at this time. The hw mac address will come from
20 * the first slave bonded to the channel. All slaves will then use
21 * this hw mac address.
23 * ifconfig bond0 down
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.
35 * Changes:
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
47 * good slave.
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
57 * wires ! :->
59 * 2000/10/09 - Willy Tarreau <willy at meta-x.org>
60 * - added up and down delays after link state change.
61 * - optimized the slaves chaining so that when we run forward, we never
62 * repass through the bond itself, but we can find it by searching
63 * backwards. Renders the deletion more difficult, but accelerates the
64 * scan.
65 * - smarter enslaving and releasing.
66 * - finer and more robust SMP locking
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
118 * out routing table.
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
149 * structure.
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
202 * hw address.
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
234 * for a response
235 * - Renamed bond_arp_monitor to loadbalance_arp_monitor and re-wrote
236 * this function to just handle arp monitoring in load-balancing mode;
237 * it is a lot more compact now
238 * - Changes to ensure one and only one slave transmits in active-backup
239 * mode
240 * - Robustesize parameters; warn users about bad combinations of
241 * parameters; also if miimon is specified and a network driver does
242 * not support MII or ETHTOOL, inform the user of this
243 * - Changes to support link_failure_count when in arp monitoring mode
244 * - Fix up/down delay reported in /proc
245 * - Added version; log version; make version available from "modinfo -d"
246 * - Fixed problem in bond_check_dev_link - if the first IOCTL (SIOCGMIIPH)
247 * failed, the ETHTOOL ioctl never got a chance
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 curr_active_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
310 * each slave.
311 * * bond_enslave(): Moved setting the slave's mac address, and
312 * openning it, from the application to the driver. This breaks
313 * backward comaptibility with old versions of ifenslave that open
314 * the slave before enalsving it !!!.
315 * * bond_release(): The driver also takes care of closing the slave
316 * and restoring its original mac address.
317 * - Removed the code that restores all base driver's flags.
318 * Flags are automatically restored once all undo stages are done
319 * properly.
320 * - Block possibility of enslaving before the master is up. This
321 * prevents putting the system in an unstable state.
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 curr_active_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 curr_active_slave to a single point
362 * so specific modes can take actions when the primary adapter is
363 * changed.
364 * - Take the updelay parameter into consideration during bond_enslave
365 * since some adapters loose their link during setting the device.
366 * - Renamed bond_3ad_link_status_changed() to
367 * bond_3ad_handle_link_change() for compatibility with TLB.
368 * new version - 2.1.0
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
387 * bond to be down.
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.
394 * Version to 2.2.14.
396 * 2003/07/29 - Amir Noam <amir.noam at intel dot com>
397 * - Fixed ARP monitoring bug.
398 * Version to 2.2.15.
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.
403 * Version to 2.2.16.
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.
410 * Version to 2.3.0.
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.
422 * Version to 2.4.0.
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.
430 * 2003/11/20 - Amir Noam <amir.noam at intel dot com>
431 * - Fix /proc creation/destruction.
433 * 2003/12/01 - Shmulik Hen <shmulik.hen at intel dot com>
434 * - Massive cleanup - Set version to 2.5.0
435 * Code changes:
436 * o Consolidate format of prints and debug prints.
437 * o Remove bonding_t/slave_t typedefs and consolidate all casts.
438 * o Remove dead code and unnecessary checks.
439 * o Consolidate starting/stopping timers.
440 * o Consolidate handling of primary module param throughout the code.
441 * o Removed multicast module param support - all settings are done
442 * according to mode.
443 * o Slave list iteration - bond is no longer part of the list,
444 * added cyclic list iteration macros.
445 * o Consolidate error handling in all xmit functions.
446 * Style changes:
447 * o Consolidate function naming and declarations.
448 * o Consolidate function params and local variables names.
449 * o Consolidate return values.
450 * o Consolidate curly braces.
451 * o Consolidate conditionals format.
452 * o Change struct member names and types.
453 * o Chomp trailing spaces, remove empty lines, fix indentations.
454 * o Re-organize code according to context.
456 * 2003/12/30 - Amir Noam <amir.noam at intel dot com>
457 * - Fixed: Cannot remove and re-enslave the original active slave.
458 * - Fixed: Releasing the original active slave causes mac address
459 * duplication.
460 * - Add support for slaves that use ethtool_ops.
461 * Set version to 2.5.3.
463 * 2004/01/05 - Amir Noam <amir.noam at intel dot com>
464 * - Save bonding parameters per bond instead of using the global values.
465 * Set version to 2.5.4.
467 * 2004/01/14 - Shmulik Hen <shmulik.hen at intel dot com>
468 * - Enhance VLAN support:
469 * * Add support for VLAN hardware acceleration capable slaves.
470 * * Add capability to tag self generated packets in ALB/TLB modes.
471 * Set version to 2.6.0.
472 * 2004/10/29 - Mitch Williams <mitch.a.williams at intel dot com>
473 * - Fixed bug when unloading module while using 802.3ad. If
474 * spinlock debugging is turned on, this causes a stack dump.
475 * Solution is to move call to dev_remove_pack outside of the
476 * spinlock.
477 * Set version to 2.6.1.
481 //#define BONDING_DEBUG 1
483 #include <linux/config.h>
484 #include <linux/kernel.h>
485 #include <linux/module.h>
486 #include <linux/sched.h>
487 #include <linux/types.h>
488 #include <linux/fcntl.h>
489 #include <linux/interrupt.h>
490 #include <linux/ptrace.h>
491 #include <linux/ioport.h>
492 #include <linux/in.h>
493 #include <linux/ip.h>
494 #include <linux/slab.h>
495 #include <linux/string.h>
496 #include <linux/init.h>
497 #include <linux/timer.h>
498 #include <linux/socket.h>
499 #include <linux/ctype.h>
500 #include <linux/inet.h>
501 #include <linux/bitops.h>
502 #include <asm/system.h>
503 #include <asm/io.h>
504 #include <asm/dma.h>
505 #include <asm/uaccess.h>
506 #include <linux/errno.h>
507 #include <linux/netdevice.h>
508 #include <linux/inetdevice.h>
509 #include <linux/etherdevice.h>
510 #include <linux/skbuff.h>
511 #include <net/sock.h>
512 #include <linux/rtnetlink.h>
513 #include <linux/proc_fs.h>
514 #include <linux/seq_file.h>
515 #include <linux/smp.h>
516 #include <linux/if_ether.h>
517 #include <net/arp.h>
518 #include <linux/mii.h>
519 #include <linux/ethtool.h>
520 #include <linux/if_vlan.h>
521 #include <linux/if_bonding.h>
522 #include "bonding.h"
523 #include "bond_3ad.h"
524 #include "bond_alb.h"
526 /*---------------------------- Module parameters ----------------------------*/
528 /* monitor all links that often (in milliseconds). <=0 disables monitoring */
529 #define BOND_LINK_MON_INTERV 0
530 #define BOND_LINK_ARP_INTERV 0
532 static int max_bonds = BOND_DEFAULT_MAX_BONDS;
533 static int miimon = BOND_LINK_MON_INTERV;
534 static int updelay = 0;
535 static int downdelay = 0;
536 static int use_carrier = 1;
537 static char *mode = NULL;
538 static char *primary = NULL;
539 static char *lacp_rate = NULL;
540 static int arp_interval = BOND_LINK_ARP_INTERV;
541 static char *arp_ip_target[BOND_MAX_ARP_TARGETS] = { NULL, };
543 module_param(max_bonds, int, 0);
544 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
545 module_param(miimon, int, 0);
546 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
547 module_param(updelay, int, 0);
548 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
549 module_param(downdelay, int, 0);
550 MODULE_PARM_DESC(downdelay, "Delay before considering link down, in milliseconds");
551 module_param(use_carrier, int, 0);
552 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; 0 for off, 1 for on (default)");
553 module_param(mode, charp, 0);
554 MODULE_PARM_DESC(mode, "Mode of operation : 0 for round robin, 1 for active-backup, 2 for xor");
555 module_param(primary, charp, 0);
556 MODULE_PARM_DESC(primary, "Primary network device to use");
557 module_param(lacp_rate, charp, 0);
558 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner (slow/fast)");
559 module_param(arp_interval, int, 0);
560 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
561 module_param_array(arp_ip_target, charp, NULL, 0);
562 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
564 /*----------------------------- Global variables ----------------------------*/
566 static const char *version =
567 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
569 static LIST_HEAD(bond_dev_list);
571 #ifdef CONFIG_PROC_FS
572 static struct proc_dir_entry *bond_proc_dir = NULL;
573 #endif
575 static u32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
576 static int arp_ip_count = 0;
577 static u32 my_ip = 0;
578 static int bond_mode = BOND_MODE_ROUNDROBIN;
579 static int lacp_fast = 0;
580 static int app_abi_ver = 0;
581 static int orig_app_abi_ver = -1; /* This is used to save the first ABI version
582 * we receive from the application. Once set,
583 * it won't be changed, and the module will
584 * refuse to enslave/release interfaces if the
585 * command comes from an application using
586 * another ABI version.
589 struct bond_parm_tbl {
590 char *modename;
591 int mode;
594 static struct bond_parm_tbl bond_lacp_tbl[] = {
595 { "slow", AD_LACP_SLOW},
596 { "fast", AD_LACP_FAST},
597 { NULL, -1},
600 static struct bond_parm_tbl bond_mode_tbl[] = {
601 { "balance-rr", BOND_MODE_ROUNDROBIN},
602 { "active-backup", BOND_MODE_ACTIVEBACKUP},
603 { "balance-xor", BOND_MODE_XOR},
604 { "broadcast", BOND_MODE_BROADCAST},
605 { "802.3ad", BOND_MODE_8023AD},
606 { "balance-tlb", BOND_MODE_TLB},
607 { "balance-alb", BOND_MODE_ALB},
608 { NULL, -1},
611 /*-------------------------- Forward declarations ---------------------------*/
613 static inline void bond_set_mode_ops(struct net_device *bond_dev, int mode);
615 /*---------------------------- General routines -----------------------------*/
617 static const char *bond_mode_name(int mode)
619 switch (mode) {
620 case BOND_MODE_ROUNDROBIN :
621 return "load balancing (round-robin)";
622 case BOND_MODE_ACTIVEBACKUP :
623 return "fault-tolerance (active-backup)";
624 case BOND_MODE_XOR :
625 return "load balancing (xor)";
626 case BOND_MODE_BROADCAST :
627 return "fault-tolerance (broadcast)";
628 case BOND_MODE_8023AD:
629 return "IEEE 802.3ad Dynamic link aggregation";
630 case BOND_MODE_TLB:
631 return "transmit load balancing";
632 case BOND_MODE_ALB:
633 return "adaptive load balancing";
634 default:
635 return "unknown";
639 /*---------------------------------- VLAN -----------------------------------*/
642 * bond_add_vlan - add a new vlan id on bond
643 * @bond: bond that got the notification
644 * @vlan_id: the vlan id to add
646 * Returns -ENOMEM if allocation failed.
648 static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
650 struct vlan_entry *vlan;
652 dprintk("bond: %s, vlan id %d\n",
653 (bond ? bond->dev->name: "None"), vlan_id);
655 vlan = kmalloc(sizeof(struct vlan_entry), GFP_KERNEL);
656 if (!vlan) {
657 return -ENOMEM;
660 INIT_LIST_HEAD(&vlan->vlan_list);
661 vlan->vlan_id = vlan_id;
663 write_lock_bh(&bond->lock);
665 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
667 write_unlock_bh(&bond->lock);
669 dprintk("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
671 return 0;
675 * bond_del_vlan - delete a vlan id from bond
676 * @bond: bond that got the notification
677 * @vlan_id: the vlan id to delete
679 * returns -ENODEV if @vlan_id was not found in @bond.
681 static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
683 struct vlan_entry *vlan, *next;
684 int res = -ENODEV;
686 dprintk("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
688 write_lock_bh(&bond->lock);
690 list_for_each_entry_safe(vlan, next, &bond->vlan_list, vlan_list) {
691 if (vlan->vlan_id == vlan_id) {
692 list_del(&vlan->vlan_list);
694 if ((bond->params.mode == BOND_MODE_TLB) ||
695 (bond->params.mode == BOND_MODE_ALB)) {
696 bond_alb_clear_vlan(bond, vlan_id);
699 dprintk("removed VLAN ID %d from bond %s\n", vlan_id,
700 bond->dev->name);
702 kfree(vlan);
704 if (list_empty(&bond->vlan_list) &&
705 (bond->slave_cnt == 0)) {
706 /* Last VLAN removed and no slaves, so
707 * restore block on adding VLANs. This will
708 * be removed once new slaves that are not
709 * VLAN challenged will be added.
711 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
714 res = 0;
715 goto out;
719 dprintk("couldn't find VLAN ID %d in bond %s\n", vlan_id,
720 bond->dev->name);
722 out:
723 write_unlock_bh(&bond->lock);
724 return res;
728 * bond_has_challenged_slaves
729 * @bond: the bond we're working on
731 * Searches the slave list. Returns 1 if a vlan challenged slave
732 * was found, 0 otherwise.
734 * Assumes bond->lock is held.
736 static int bond_has_challenged_slaves(struct bonding *bond)
738 struct slave *slave;
739 int i;
741 bond_for_each_slave(bond, slave, i) {
742 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
743 dprintk("found VLAN challenged slave - %s\n",
744 slave->dev->name);
745 return 1;
749 dprintk("no VLAN challenged slaves found\n");
750 return 0;
754 * bond_next_vlan - safely skip to the next item in the vlans list.
755 * @bond: the bond we're working on
756 * @curr: item we're advancing from
758 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
759 * or @curr->next otherwise (even if it is @curr itself again).
761 * Caller must hold bond->lock
763 struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
765 struct vlan_entry *next, *last;
767 if (list_empty(&bond->vlan_list)) {
768 return NULL;
771 if (!curr) {
772 next = list_entry(bond->vlan_list.next,
773 struct vlan_entry, vlan_list);
774 } else {
775 last = list_entry(bond->vlan_list.prev,
776 struct vlan_entry, vlan_list);
777 if (last == curr) {
778 next = list_entry(bond->vlan_list.next,
779 struct vlan_entry, vlan_list);
780 } else {
781 next = list_entry(curr->vlan_list.next,
782 struct vlan_entry, vlan_list);
786 return next;
790 * bond_dev_queue_xmit - Prepare skb for xmit.
792 * @bond: bond device that got this skb for tx.
793 * @skb: hw accel VLAN tagged skb to transmit
794 * @slave_dev: slave that is supposed to xmit this skbuff
796 * When the bond gets an skb to transmit that is
797 * already hardware accelerated VLAN tagged, and it
798 * needs to relay this skb to a slave that is not
799 * hw accel capable, the skb needs to be "unaccelerated",
800 * i.e. strip the hwaccel tag and re-insert it as part
801 * of the payload.
803 int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
805 unsigned short vlan_id;
807 if (!list_empty(&bond->vlan_list) &&
808 !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
809 vlan_get_tag(skb, &vlan_id) == 0) {
810 skb->dev = slave_dev;
811 skb = vlan_put_tag(skb, vlan_id);
812 if (!skb) {
813 /* vlan_put_tag() frees the skb in case of error,
814 * so return success here so the calling functions
815 * won't attempt to free is again.
817 return 0;
819 } else {
820 skb->dev = slave_dev;
823 skb->priority = 1;
824 dev_queue_xmit(skb);
826 return 0;
830 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
831 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
832 * lock because:
833 * a. This operation is performed in IOCTL context,
834 * b. The operation is protected by the RTNL semaphore in the 8021q code,
835 * c. Holding a lock with BH disabled while directly calling a base driver
836 * entry point is generally a BAD idea.
838 * The design of synchronization/protection for this operation in the 8021q
839 * module is good for one or more VLAN devices over a single physical device
840 * and cannot be extended for a teaming solution like bonding, so there is a
841 * potential race condition here where a net device from the vlan group might
842 * be referenced (either by a base driver or the 8021q code) while it is being
843 * removed from the system. However, it turns out we're not making matters
844 * worse, and if it works for regular VLAN usage it will work here too.
848 * bond_vlan_rx_register - Propagates registration to slaves
849 * @bond_dev: bonding net device that got called
850 * @grp: vlan group being registered
852 static void bond_vlan_rx_register(struct net_device *bond_dev, struct vlan_group *grp)
854 struct bonding *bond = bond_dev->priv;
855 struct slave *slave;
856 int i;
858 bond->vlgrp = grp;
860 bond_for_each_slave(bond, slave, i) {
861 struct net_device *slave_dev = slave->dev;
863 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
864 slave_dev->vlan_rx_register) {
865 slave_dev->vlan_rx_register(slave_dev, grp);
871 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
872 * @bond_dev: bonding net device that got called
873 * @vid: vlan id being added
875 static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
877 struct bonding *bond = bond_dev->priv;
878 struct slave *slave;
879 int i, res;
881 bond_for_each_slave(bond, slave, i) {
882 struct net_device *slave_dev = slave->dev;
884 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
885 slave_dev->vlan_rx_add_vid) {
886 slave_dev->vlan_rx_add_vid(slave_dev, vid);
890 res = bond_add_vlan(bond, vid);
891 if (res) {
892 printk(KERN_ERR DRV_NAME
893 ": %s: Failed to add vlan id %d\n",
894 bond_dev->name, vid);
899 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
900 * @bond_dev: bonding net device that got called
901 * @vid: vlan id being removed
903 static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
905 struct bonding *bond = bond_dev->priv;
906 struct slave *slave;
907 struct net_device *vlan_dev;
908 int i, res;
910 bond_for_each_slave(bond, slave, i) {
911 struct net_device *slave_dev = slave->dev;
913 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
914 slave_dev->vlan_rx_kill_vid) {
915 /* Save and then restore vlan_dev in the grp array,
916 * since the slave's driver might clear it.
918 vlan_dev = bond->vlgrp->vlan_devices[vid];
919 slave_dev->vlan_rx_kill_vid(slave_dev, vid);
920 bond->vlgrp->vlan_devices[vid] = vlan_dev;
924 res = bond_del_vlan(bond, vid);
925 if (res) {
926 printk(KERN_ERR DRV_NAME
927 ": %s: Failed to remove vlan id %d\n",
928 bond_dev->name, vid);
932 static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
934 struct vlan_entry *vlan;
936 write_lock_bh(&bond->lock);
938 if (list_empty(&bond->vlan_list)) {
939 goto out;
942 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
943 slave_dev->vlan_rx_register) {
944 slave_dev->vlan_rx_register(slave_dev, bond->vlgrp);
947 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
948 !(slave_dev->vlan_rx_add_vid)) {
949 goto out;
952 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
953 slave_dev->vlan_rx_add_vid(slave_dev, vlan->vlan_id);
956 out:
957 write_unlock_bh(&bond->lock);
960 static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *slave_dev)
962 struct vlan_entry *vlan;
963 struct net_device *vlan_dev;
965 write_lock_bh(&bond->lock);
967 if (list_empty(&bond->vlan_list)) {
968 goto out;
971 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
972 !(slave_dev->vlan_rx_kill_vid)) {
973 goto unreg;
976 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
977 /* Save and then restore vlan_dev in the grp array,
978 * since the slave's driver might clear it.
980 vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
981 slave_dev->vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
982 bond->vlgrp->vlan_devices[vlan->vlan_id] = vlan_dev;
985 unreg:
986 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
987 slave_dev->vlan_rx_register) {
988 slave_dev->vlan_rx_register(slave_dev, NULL);
991 out:
992 write_unlock_bh(&bond->lock);
995 /*------------------------------- Link status -------------------------------*/
998 * Get link speed and duplex from the slave's base driver
999 * using ethtool. If for some reason the call fails or the
1000 * values are invalid, fake speed and duplex to 100/Full
1001 * and return error.
1003 static int bond_update_speed_duplex(struct slave *slave)
1005 struct net_device *slave_dev = slave->dev;
1006 static int (* ioctl)(struct net_device *, struct ifreq *, int);
1007 struct ifreq ifr;
1008 struct ethtool_cmd etool;
1010 /* Fake speed and duplex */
1011 slave->speed = SPEED_100;
1012 slave->duplex = DUPLEX_FULL;
1014 if (slave_dev->ethtool_ops) {
1015 u32 res;
1017 if (!slave_dev->ethtool_ops->get_settings) {
1018 return -1;
1021 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
1022 if (res < 0) {
1023 return -1;
1026 goto verify;
1029 ioctl = slave_dev->do_ioctl;
1030 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
1031 etool.cmd = ETHTOOL_GSET;
1032 ifr.ifr_data = (char*)&etool;
1033 if (!ioctl || (IOCTL(slave_dev, &ifr, SIOCETHTOOL) < 0)) {
1034 return -1;
1037 verify:
1038 switch (etool.speed) {
1039 case SPEED_10:
1040 case SPEED_100:
1041 case SPEED_1000:
1042 break;
1043 default:
1044 return -1;
1047 switch (etool.duplex) {
1048 case DUPLEX_FULL:
1049 case DUPLEX_HALF:
1050 break;
1051 default:
1052 return -1;
1055 slave->speed = etool.speed;
1056 slave->duplex = etool.duplex;
1058 return 0;
1062 * if <dev> supports MII link status reporting, check its link status.
1064 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
1065 * depening upon the setting of the use_carrier parameter.
1067 * Return either BMSR_LSTATUS, meaning that the link is up (or we
1068 * can't tell and just pretend it is), or 0, meaning that the link is
1069 * down.
1071 * If reporting is non-zero, instead of faking link up, return -1 if
1072 * both ETHTOOL and MII ioctls fail (meaning the device does not
1073 * support them). If use_carrier is set, return whatever it says.
1074 * It'd be nice if there was a good way to tell if a driver supports
1075 * netif_carrier, but there really isn't.
1077 static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_dev, int reporting)
1079 static int (* ioctl)(struct net_device *, struct ifreq *, int);
1080 struct ifreq ifr;
1081 struct mii_ioctl_data *mii;
1082 struct ethtool_value etool;
1084 if (bond->params.use_carrier) {
1085 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
1088 ioctl = slave_dev->do_ioctl;
1089 if (ioctl) {
1090 /* TODO: set pointer to correct ioctl on a per team member */
1091 /* bases to make this more efficient. that is, once */
1092 /* we determine the correct ioctl, we will always */
1093 /* call it and not the others for that team */
1094 /* member. */
1097 * We cannot assume that SIOCGMIIPHY will also read a
1098 * register; not all network drivers (e.g., e100)
1099 * support that.
1102 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
1103 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
1104 mii = if_mii(&ifr);
1105 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
1106 mii->reg_num = MII_BMSR;
1107 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0) {
1108 return (mii->val_out & BMSR_LSTATUS);
1113 /* try SIOCETHTOOL ioctl, some drivers cache ETHTOOL_GLINK */
1114 /* for a period of time so we attempt to get link status */
1115 /* from it last if the above MII ioctls fail... */
1116 if (slave_dev->ethtool_ops) {
1117 if (slave_dev->ethtool_ops->get_link) {
1118 u32 link;
1120 link = slave_dev->ethtool_ops->get_link(slave_dev);
1122 return link ? BMSR_LSTATUS : 0;
1126 if (ioctl) {
1127 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
1128 etool.cmd = ETHTOOL_GLINK;
1129 ifr.ifr_data = (char*)&etool;
1130 if (IOCTL(slave_dev, &ifr, SIOCETHTOOL) == 0) {
1131 if (etool.data == 1) {
1132 return BMSR_LSTATUS;
1133 } else {
1134 dprintk("SIOCETHTOOL shows link down\n");
1135 return 0;
1141 * If reporting, report that either there's no dev->do_ioctl,
1142 * or both SIOCGMIIREG and SIOCETHTOOL failed (meaning that we
1143 * cannot report link status). If not reporting, pretend
1144 * we're ok.
1146 return (reporting ? -1 : BMSR_LSTATUS);
1149 /*----------------------------- Multicast list ------------------------------*/
1152 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
1154 static inline int bond_is_dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
1156 return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
1157 dmi1->dmi_addrlen == dmi2->dmi_addrlen;
1161 * returns dmi entry if found, NULL otherwise
1163 static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
1165 struct dev_mc_list *idmi;
1167 for (idmi = mc_list; idmi; idmi = idmi->next) {
1168 if (bond_is_dmi_same(dmi, idmi)) {
1169 return idmi;
1173 return NULL;
1177 * Push the promiscuity flag down to appropriate slaves
1179 static void bond_set_promiscuity(struct bonding *bond, int inc)
1181 if (USES_PRIMARY(bond->params.mode)) {
1182 /* write lock already acquired */
1183 if (bond->curr_active_slave) {
1184 dev_set_promiscuity(bond->curr_active_slave->dev, inc);
1186 } else {
1187 struct slave *slave;
1188 int i;
1189 bond_for_each_slave(bond, slave, i) {
1190 dev_set_promiscuity(slave->dev, inc);
1196 * Push the allmulti flag down to all slaves
1198 static void bond_set_allmulti(struct bonding *bond, int inc)
1200 if (USES_PRIMARY(bond->params.mode)) {
1201 /* write lock already acquired */
1202 if (bond->curr_active_slave) {
1203 dev_set_allmulti(bond->curr_active_slave->dev, inc);
1205 } else {
1206 struct slave *slave;
1207 int i;
1208 bond_for_each_slave(bond, slave, i) {
1209 dev_set_allmulti(slave->dev, inc);
1215 * Add a Multicast address to slaves
1216 * according to mode
1218 static void bond_mc_add(struct bonding *bond, void *addr, int alen)
1220 if (USES_PRIMARY(bond->params.mode)) {
1221 /* write lock already acquired */
1222 if (bond->curr_active_slave) {
1223 dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
1225 } else {
1226 struct slave *slave;
1227 int i;
1228 bond_for_each_slave(bond, slave, i) {
1229 dev_mc_add(slave->dev, addr, alen, 0);
1235 * Remove a multicast address from slave
1236 * according to mode
1238 static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
1240 if (USES_PRIMARY(bond->params.mode)) {
1241 /* write lock already acquired */
1242 if (bond->curr_active_slave) {
1243 dev_mc_delete(bond->curr_active_slave->dev, addr, alen, 0);
1245 } else {
1246 struct slave *slave;
1247 int i;
1248 bond_for_each_slave(bond, slave, i) {
1249 dev_mc_delete(slave->dev, addr, alen, 0);
1255 * Totally destroys the mc_list in bond
1257 static void bond_mc_list_destroy(struct bonding *bond)
1259 struct dev_mc_list *dmi;
1261 dmi = bond->mc_list;
1262 while (dmi) {
1263 bond->mc_list = dmi->next;
1264 kfree(dmi);
1265 dmi = bond->mc_list;
1270 * Copy all the Multicast addresses from src to the bonding device dst
1272 static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond, int gpf_flag)
1274 struct dev_mc_list *dmi, *new_dmi;
1276 for (dmi = mc_list; dmi; dmi = dmi->next) {
1277 new_dmi = kmalloc(sizeof(struct dev_mc_list), gpf_flag);
1279 if (!new_dmi) {
1280 /* FIXME: Potential memory leak !!! */
1281 return -ENOMEM;
1284 new_dmi->next = bond->mc_list;
1285 bond->mc_list = new_dmi;
1286 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
1287 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
1288 new_dmi->dmi_users = dmi->dmi_users;
1289 new_dmi->dmi_gusers = dmi->dmi_gusers;
1292 return 0;
1296 * flush all members of flush->mc_list from device dev->mc_list
1298 static void bond_mc_list_flush(struct net_device *bond_dev, struct net_device *slave_dev)
1300 struct bonding *bond = bond_dev->priv;
1301 struct dev_mc_list *dmi;
1303 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1304 dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1307 if (bond->params.mode == BOND_MODE_8023AD) {
1308 /* del lacpdu mc addr from mc list */
1309 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1311 dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1315 /*--------------------------- Active slave change ---------------------------*/
1318 * Update the mc list and multicast-related flags for the new and
1319 * old active slaves (if any) according to the multicast mode, and
1320 * promiscuous flags unconditionally.
1322 static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct slave *old_active)
1324 struct dev_mc_list *dmi;
1326 if (!USES_PRIMARY(bond->params.mode)) {
1327 /* nothing to do - mc list is already up-to-date on
1328 * all slaves
1330 return;
1333 if (old_active) {
1334 if (bond->dev->flags & IFF_PROMISC) {
1335 dev_set_promiscuity(old_active->dev, -1);
1338 if (bond->dev->flags & IFF_ALLMULTI) {
1339 dev_set_allmulti(old_active->dev, -1);
1342 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
1343 dev_mc_delete(old_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1347 if (new_active) {
1348 if (bond->dev->flags & IFF_PROMISC) {
1349 dev_set_promiscuity(new_active->dev, 1);
1352 if (bond->dev->flags & IFF_ALLMULTI) {
1353 dev_set_allmulti(new_active->dev, 1);
1356 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
1357 dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1363 * find_best_interface - select the best available slave to be the active one
1364 * @bond: our bonding struct
1366 * Warning: Caller must hold curr_slave_lock for writing.
1368 static struct slave *bond_find_best_slave(struct bonding *bond)
1370 struct slave *new_active, *old_active;
1371 struct slave *bestslave = NULL;
1372 int mintime = bond->params.updelay;
1373 int i;
1375 new_active = old_active = bond->curr_active_slave;
1377 if (!new_active) { /* there were no active slaves left */
1378 if (bond->slave_cnt > 0) { /* found one slave */
1379 new_active = bond->first_slave;
1380 } else {
1381 return NULL; /* still no slave, return NULL */
1385 /* first try the primary link; if arping, a link must tx/rx traffic
1386 * before it can be considered the curr_active_slave - also, we would skip
1387 * slaves between the curr_active_slave and primary_slave that may be up
1388 * and able to arp
1390 if ((bond->primary_slave) &&
1391 (!bond->params.arp_interval) &&
1392 (IS_UP(bond->primary_slave->dev))) {
1393 new_active = bond->primary_slave;
1396 /* remember where to stop iterating over the slaves */
1397 old_active = new_active;
1399 bond_for_each_slave_from(bond, new_active, i, old_active) {
1400 if (IS_UP(new_active->dev)) {
1401 if (new_active->link == BOND_LINK_UP) {
1402 return new_active;
1403 } else if (new_active->link == BOND_LINK_BACK) {
1404 /* link up, but waiting for stabilization */
1405 if (new_active->delay < mintime) {
1406 mintime = new_active->delay;
1407 bestslave = new_active;
1413 return bestslave;
1417 * change_active_interface - change the active slave into the specified one
1418 * @bond: our bonding struct
1419 * @new: the new slave to make the active one
1421 * Set the new slave to the bond's settings and unset them on the old
1422 * curr_active_slave.
1423 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1425 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1426 * because it is apparently the best available slave we have, even though its
1427 * updelay hasn't timed out yet.
1429 * Warning: Caller must hold curr_slave_lock for writing.
1431 static void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
1433 struct slave *old_active = bond->curr_active_slave;
1435 if (old_active == new_active) {
1436 return;
1439 if (new_active) {
1440 if (new_active->link == BOND_LINK_BACK) {
1441 if (USES_PRIMARY(bond->params.mode)) {
1442 printk(KERN_INFO DRV_NAME
1443 ": %s: making interface %s the new "
1444 "active one %d ms earlier.\n",
1445 bond->dev->name, new_active->dev->name,
1446 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1449 new_active->delay = 0;
1450 new_active->link = BOND_LINK_UP;
1451 new_active->jiffies = jiffies;
1453 if (bond->params.mode == BOND_MODE_8023AD) {
1454 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1457 if ((bond->params.mode == BOND_MODE_TLB) ||
1458 (bond->params.mode == BOND_MODE_ALB)) {
1459 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1461 } else {
1462 if (USES_PRIMARY(bond->params.mode)) {
1463 printk(KERN_INFO DRV_NAME
1464 ": %s: making interface %s the new "
1465 "active one.\n",
1466 bond->dev->name, new_active->dev->name);
1471 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1472 if (old_active) {
1473 bond_set_slave_inactive_flags(old_active);
1476 if (new_active) {
1477 bond_set_slave_active_flags(new_active);
1481 if (USES_PRIMARY(bond->params.mode)) {
1482 bond_mc_swap(bond, new_active, old_active);
1485 if ((bond->params.mode == BOND_MODE_TLB) ||
1486 (bond->params.mode == BOND_MODE_ALB)) {
1487 bond_alb_handle_active_change(bond, new_active);
1488 } else {
1489 bond->curr_active_slave = new_active;
1494 * bond_select_active_slave - select a new active slave, if needed
1495 * @bond: our bonding struct
1497 * This functions shoud be called when one of the following occurs:
1498 * - The old curr_active_slave has been released or lost its link.
1499 * - The primary_slave has got its link back.
1500 * - A slave has got its link back and there's no old curr_active_slave.
1502 * Warning: Caller must hold curr_slave_lock for writing.
1504 static void bond_select_active_slave(struct bonding *bond)
1506 struct slave *best_slave;
1508 best_slave = bond_find_best_slave(bond);
1509 if (best_slave != bond->curr_active_slave) {
1510 bond_change_active_slave(bond, best_slave);
1514 /*--------------------------- slave list handling ---------------------------*/
1517 * This function attaches the slave to the end of list.
1519 * bond->lock held for writing by caller.
1521 static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1523 if (bond->first_slave == NULL) { /* attaching the first slave */
1524 new_slave->next = new_slave;
1525 new_slave->prev = new_slave;
1526 bond->first_slave = new_slave;
1527 } else {
1528 new_slave->next = bond->first_slave;
1529 new_slave->prev = bond->first_slave->prev;
1530 new_slave->next->prev = new_slave;
1531 new_slave->prev->next = new_slave;
1534 bond->slave_cnt++;
1538 * This function detaches the slave from the list.
1539 * WARNING: no check is made to verify if the slave effectively
1540 * belongs to <bond>.
1541 * Nothing is freed on return, structures are just unchained.
1542 * If any slave pointer in bond was pointing to <slave>,
1543 * it should be changed by the calling function.
1545 * bond->lock held for writing by caller.
1547 static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1549 if (slave->next) {
1550 slave->next->prev = slave->prev;
1553 if (slave->prev) {
1554 slave->prev->next = slave->next;
1557 if (bond->first_slave == slave) { /* slave is the first slave */
1558 if (bond->slave_cnt > 1) { /* there are more slave */
1559 bond->first_slave = slave->next;
1560 } else {
1561 bond->first_slave = NULL; /* slave was the last one */
1565 slave->next = NULL;
1566 slave->prev = NULL;
1567 bond->slave_cnt--;
1570 /*---------------------------------- IOCTL ----------------------------------*/
1572 static int bond_sethwaddr(struct net_device *bond_dev, struct net_device *slave_dev)
1574 dprintk("bond_dev=%p\n", bond_dev);
1575 dprintk("slave_dev=%p\n", slave_dev);
1576 dprintk("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1577 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1578 return 0;
1581 /* enslave device <slave> to bond device <master> */
1582 static int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1584 struct bonding *bond = bond_dev->priv;
1585 struct slave *new_slave = NULL;
1586 struct dev_mc_list *dmi;
1587 struct sockaddr addr;
1588 int link_reporting;
1589 int old_features = bond_dev->features;
1590 int res = 0;
1592 if (slave_dev->do_ioctl == NULL) {
1593 printk(KERN_WARNING DRV_NAME
1594 ": Warning : no link monitoring support for %s\n",
1595 slave_dev->name);
1598 /* bond must be initialized by bond_open() before enslaving */
1599 if (!(bond_dev->flags & IFF_UP)) {
1600 dprintk("Error, master_dev is not up\n");
1601 return -EPERM;
1604 /* already enslaved */
1605 if (slave_dev->flags & IFF_SLAVE) {
1606 dprintk("Error, Device was already enslaved\n");
1607 return -EBUSY;
1610 /* vlan challenged mutual exclusion */
1611 /* no need to lock since we're protected by rtnl_lock */
1612 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1613 dprintk("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1614 if (!list_empty(&bond->vlan_list)) {
1615 printk(KERN_ERR DRV_NAME
1616 ": Error: cannot enslave VLAN "
1617 "challenged slave %s on VLAN enabled "
1618 "bond %s\n", slave_dev->name,
1619 bond_dev->name);
1620 return -EPERM;
1621 } else {
1622 printk(KERN_WARNING DRV_NAME
1623 ": Warning: enslaved VLAN challenged "
1624 "slave %s. Adding VLANs will be blocked as "
1625 "long as %s is part of bond %s\n",
1626 slave_dev->name, slave_dev->name,
1627 bond_dev->name);
1628 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1630 } else {
1631 dprintk("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1632 if (bond->slave_cnt == 0) {
1633 /* First slave, and it is not VLAN challenged,
1634 * so remove the block of adding VLANs over the bond.
1636 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1640 if (app_abi_ver >= 1) {
1641 /* The application is using an ABI, which requires the
1642 * slave interface to be closed.
1644 if ((slave_dev->flags & IFF_UP)) {
1645 printk(KERN_ERR DRV_NAME
1646 ": Error: %s is up\n",
1647 slave_dev->name);
1648 res = -EPERM;
1649 goto err_undo_flags;
1652 if (slave_dev->set_mac_address == NULL) {
1653 printk(KERN_ERR DRV_NAME
1654 ": Error: The slave device you specified does "
1655 "not support setting the MAC address.\n");
1656 printk(KERN_ERR
1657 "Your kernel likely does not support slave "
1658 "devices.\n");
1660 res = -EOPNOTSUPP;
1661 goto err_undo_flags;
1663 } else {
1664 /* The application is not using an ABI, which requires the
1665 * slave interface to be open.
1667 if (!(slave_dev->flags & IFF_UP)) {
1668 printk(KERN_ERR DRV_NAME
1669 ": Error: %s is not running\n",
1670 slave_dev->name);
1671 res = -EINVAL;
1672 goto err_undo_flags;
1675 if ((bond->params.mode == BOND_MODE_8023AD) ||
1676 (bond->params.mode == BOND_MODE_TLB) ||
1677 (bond->params.mode == BOND_MODE_ALB)) {
1678 printk(KERN_ERR DRV_NAME
1679 ": Error: to use %s mode, you must upgrade "
1680 "ifenslave.\n",
1681 bond_mode_name(bond->params.mode));
1682 res = -EOPNOTSUPP;
1683 goto err_undo_flags;
1687 new_slave = kmalloc(sizeof(struct slave), GFP_KERNEL);
1688 if (!new_slave) {
1689 res = -ENOMEM;
1690 goto err_undo_flags;
1693 memset(new_slave, 0, sizeof(struct slave));
1695 /* save slave's original flags before calling
1696 * netdev_set_master and dev_open
1698 new_slave->original_flags = slave_dev->flags;
1700 if (app_abi_ver >= 1) {
1701 /* save slave's original ("permanent") mac address for
1702 * modes that needs it, and for restoring it upon release,
1703 * and then set it to the master's address
1705 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1707 /* set slave to master's mac address
1708 * The application already set the master's
1709 * mac address to that of the first slave
1711 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1712 addr.sa_family = slave_dev->type;
1713 res = dev_set_mac_address(slave_dev, &addr);
1714 if (res) {
1715 dprintk("Error %d calling set_mac_address\n", res);
1716 goto err_free;
1719 /* open the slave since the application closed it */
1720 res = dev_open(slave_dev);
1721 if (res) {
1722 dprintk("Openning slave %s failed\n", slave_dev->name);
1723 goto err_restore_mac;
1727 res = netdev_set_master(slave_dev, bond_dev);
1728 if (res) {
1729 dprintk("Error %d calling netdev_set_master\n", res);
1730 if (app_abi_ver < 1) {
1731 goto err_free;
1732 } else {
1733 goto err_close;
1737 new_slave->dev = slave_dev;
1739 if ((bond->params.mode == BOND_MODE_TLB) ||
1740 (bond->params.mode == BOND_MODE_ALB)) {
1741 /* bond_alb_init_slave() must be called before all other stages since
1742 * it might fail and we do not want to have to undo everything
1744 res = bond_alb_init_slave(bond, new_slave);
1745 if (res) {
1746 goto err_unset_master;
1750 /* If the mode USES_PRIMARY, then the new slave gets the
1751 * master's promisc (and mc) settings only if it becomes the
1752 * curr_active_slave, and that is taken care of later when calling
1753 * bond_change_active()
1755 if (!USES_PRIMARY(bond->params.mode)) {
1756 /* set promiscuity level to new slave */
1757 if (bond_dev->flags & IFF_PROMISC) {
1758 dev_set_promiscuity(slave_dev, 1);
1761 /* set allmulti level to new slave */
1762 if (bond_dev->flags & IFF_ALLMULTI) {
1763 dev_set_allmulti(slave_dev, 1);
1766 /* upload master's mc_list to new slave */
1767 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1768 dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1772 if (bond->params.mode == BOND_MODE_8023AD) {
1773 /* add lacpdu mc addr to mc list */
1774 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1776 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1779 bond_add_vlans_on_slave(bond, slave_dev);
1781 write_lock_bh(&bond->lock);
1783 bond_attach_slave(bond, new_slave);
1785 new_slave->delay = 0;
1786 new_slave->link_failure_count = 0;
1788 if (bond->params.miimon && !bond->params.use_carrier) {
1789 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1791 if ((link_reporting == -1) && !bond->params.arp_interval) {
1793 * miimon is set but a bonded network driver
1794 * does not support ETHTOOL/MII and
1795 * arp_interval is not set. Note: if
1796 * use_carrier is enabled, we will never go
1797 * here (because netif_carrier is always
1798 * supported); thus, we don't need to change
1799 * the messages for netif_carrier.
1801 printk(KERN_WARNING DRV_NAME
1802 ": Warning: MII and ETHTOOL support not "
1803 "available for interface %s, and "
1804 "arp_interval/arp_ip_target module parameters "
1805 "not specified, thus bonding will not detect "
1806 "link failures! see bonding.txt for details.\n",
1807 slave_dev->name);
1808 } else if (link_reporting == -1) {
1809 /* unable get link status using mii/ethtool */
1810 printk(KERN_WARNING DRV_NAME
1811 ": Warning: can't get link status from "
1812 "interface %s; the network driver associated "
1813 "with this interface does not support MII or "
1814 "ETHTOOL link status reporting, thus miimon "
1815 "has no effect on this interface.\n",
1816 slave_dev->name);
1820 /* check for initial state */
1821 if (!bond->params.miimon ||
1822 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1823 if (bond->params.updelay) {
1824 dprintk("Initial state of slave_dev is "
1825 "BOND_LINK_BACK\n");
1826 new_slave->link = BOND_LINK_BACK;
1827 new_slave->delay = bond->params.updelay;
1828 } else {
1829 dprintk("Initial state of slave_dev is "
1830 "BOND_LINK_UP\n");
1831 new_slave->link = BOND_LINK_UP;
1833 new_slave->jiffies = jiffies;
1834 } else {
1835 dprintk("Initial state of slave_dev is "
1836 "BOND_LINK_DOWN\n");
1837 new_slave->link = BOND_LINK_DOWN;
1840 if (bond_update_speed_duplex(new_slave) &&
1841 (new_slave->link != BOND_LINK_DOWN)) {
1842 printk(KERN_WARNING DRV_NAME
1843 ": Warning: failed to get speed and duplex from %s, "
1844 "assumed to be 100Mb/sec and Full.\n",
1845 new_slave->dev->name);
1847 if (bond->params.mode == BOND_MODE_8023AD) {
1848 printk(KERN_WARNING
1849 "Operation of 802.3ad mode requires ETHTOOL "
1850 "support in base driver for proper aggregator "
1851 "selection.\n");
1855 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1856 /* if there is a primary slave, remember it */
1857 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1858 bond->primary_slave = new_slave;
1862 switch (bond->params.mode) {
1863 case BOND_MODE_ACTIVEBACKUP:
1864 /* if we're in active-backup mode, we need one and only one active
1865 * interface. The backup interfaces will have their NOARP flag set
1866 * because we need them to be completely deaf and not to respond to
1867 * any ARP request on the network to avoid fooling a switch. Thus,
1868 * since we guarantee that curr_active_slave always point to the last
1869 * usable interface, we just have to verify this interface's flag.
1871 if (((!bond->curr_active_slave) ||
1872 (bond->curr_active_slave->dev->flags & IFF_NOARP)) &&
1873 (new_slave->link != BOND_LINK_DOWN)) {
1874 dprintk("This is the first active slave\n");
1875 /* first slave or no active slave yet, and this link
1876 is OK, so make this interface the active one */
1877 bond_change_active_slave(bond, new_slave);
1878 } else {
1879 dprintk("This is just a backup slave\n");
1880 bond_set_slave_inactive_flags(new_slave);
1882 break;
1883 case BOND_MODE_8023AD:
1884 /* in 802.3ad mode, the internal mechanism
1885 * will activate the slaves in the selected
1886 * aggregator
1888 bond_set_slave_inactive_flags(new_slave);
1889 /* if this is the first slave */
1890 if (bond->slave_cnt == 1) {
1891 SLAVE_AD_INFO(new_slave).id = 1;
1892 /* Initialize AD with the number of times that the AD timer is called in 1 second
1893 * can be called only after the mac address of the bond is set
1895 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1896 bond->params.lacp_fast);
1897 } else {
1898 SLAVE_AD_INFO(new_slave).id =
1899 SLAVE_AD_INFO(new_slave->prev).id + 1;
1902 bond_3ad_bind_slave(new_slave);
1903 break;
1904 case BOND_MODE_TLB:
1905 case BOND_MODE_ALB:
1906 new_slave->state = BOND_STATE_ACTIVE;
1907 if ((!bond->curr_active_slave) &&
1908 (new_slave->link != BOND_LINK_DOWN)) {
1909 /* first slave or no active slave yet, and this link
1910 * is OK, so make this interface the active one
1912 bond_change_active_slave(bond, new_slave);
1914 break;
1915 default:
1916 dprintk("This slave is always active in trunk mode\n");
1918 /* always active in trunk mode */
1919 new_slave->state = BOND_STATE_ACTIVE;
1921 /* In trunking mode there is little meaning to curr_active_slave
1922 * anyway (it holds no special properties of the bond device),
1923 * so we can change it without calling change_active_interface()
1925 if (!bond->curr_active_slave) {
1926 bond->curr_active_slave = new_slave;
1928 break;
1929 } /* switch(bond_mode) */
1931 write_unlock_bh(&bond->lock);
1933 if (app_abi_ver < 1) {
1935 * !!! This is to support old versions of ifenslave.
1936 * We can remove this in 2.5 because our ifenslave takes
1937 * care of this for us.
1938 * We check to see if the master has a mac address yet.
1939 * If not, we'll give it the mac address of our slave device.
1941 int ndx = 0;
1943 for (ndx = 0; ndx < bond_dev->addr_len; ndx++) {
1944 dprintk("Checking ndx=%d of bond_dev->dev_addr\n",
1945 ndx);
1946 if (bond_dev->dev_addr[ndx] != 0) {
1947 dprintk("Found non-zero byte at ndx=%d\n",
1948 ndx);
1949 break;
1953 if (ndx == bond_dev->addr_len) {
1955 * We got all the way through the address and it was
1956 * all 0's.
1958 dprintk("%s doesn't have a MAC address yet. \n",
1959 bond_dev->name);
1960 dprintk("Going to give assign it from %s.\n",
1961 slave_dev->name);
1962 bond_sethwaddr(bond_dev, slave_dev);
1966 printk(KERN_INFO DRV_NAME
1967 ": %s: enslaving %s as a%s interface with a%s link.\n",
1968 bond_dev->name, slave_dev->name,
1969 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1970 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1972 /* enslave is successful */
1973 return 0;
1975 /* Undo stages on error */
1976 err_unset_master:
1977 netdev_set_master(slave_dev, NULL);
1979 err_close:
1980 dev_close(slave_dev);
1982 err_restore_mac:
1983 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1984 addr.sa_family = slave_dev->type;
1985 dev_set_mac_address(slave_dev, &addr);
1987 err_free:
1988 kfree(new_slave);
1990 err_undo_flags:
1991 bond_dev->features = old_features;
1993 return res;
1997 * Try to release the slave device <slave> from the bond device <master>
1998 * It is legal to access curr_active_slave without a lock because all the function
1999 * is write-locked.
2001 * The rules for slave state should be:
2002 * for Active/Backup:
2003 * Active stays on all backups go down
2004 * for Bonded connections:
2005 * The first up interface should be left on and all others downed.
2007 static int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
2009 struct bonding *bond = bond_dev->priv;
2010 struct slave *slave, *oldcurrent;
2011 struct sockaddr addr;
2012 int mac_addr_differ;
2014 /* slave is not a slave or master is not master of this slave */
2015 if (!(slave_dev->flags & IFF_SLAVE) ||
2016 (slave_dev->master != bond_dev)) {
2017 printk(KERN_ERR DRV_NAME
2018 ": Error: %s: cannot release %s.\n",
2019 bond_dev->name, slave_dev->name);
2020 return -EINVAL;
2023 write_lock_bh(&bond->lock);
2025 slave = bond_get_slave_by_dev(bond, slave_dev);
2026 if (!slave) {
2027 /* not a slave of this bond */
2028 printk(KERN_INFO DRV_NAME
2029 ": %s: %s not enslaved\n",
2030 bond_dev->name, slave_dev->name);
2031 return -EINVAL;
2034 mac_addr_differ = memcmp(bond_dev->dev_addr,
2035 slave->perm_hwaddr,
2036 ETH_ALEN);
2037 if (!mac_addr_differ && (bond->slave_cnt > 1)) {
2038 printk(KERN_WARNING DRV_NAME
2039 ": Warning: the permanent HWaddr of %s "
2040 "- %02X:%02X:%02X:%02X:%02X:%02X - is "
2041 "still in use by %s. Set the HWaddr of "
2042 "%s to a different address to avoid "
2043 "conflicts.\n",
2044 slave_dev->name,
2045 slave->perm_hwaddr[0],
2046 slave->perm_hwaddr[1],
2047 slave->perm_hwaddr[2],
2048 slave->perm_hwaddr[3],
2049 slave->perm_hwaddr[4],
2050 slave->perm_hwaddr[5],
2051 bond_dev->name,
2052 slave_dev->name);
2055 /* Inform AD package of unbinding of slave. */
2056 if (bond->params.mode == BOND_MODE_8023AD) {
2057 /* must be called before the slave is
2058 * detached from the list
2060 bond_3ad_unbind_slave(slave);
2063 printk(KERN_INFO DRV_NAME
2064 ": %s: releasing %s interface %s\n",
2065 bond_dev->name,
2066 (slave->state == BOND_STATE_ACTIVE)
2067 ? "active" : "backup",
2068 slave_dev->name);
2070 oldcurrent = bond->curr_active_slave;
2072 bond->current_arp_slave = NULL;
2074 /* release the slave from its bond */
2075 bond_detach_slave(bond, slave);
2077 if (bond->primary_slave == slave) {
2078 bond->primary_slave = NULL;
2081 if (oldcurrent == slave) {
2082 bond_change_active_slave(bond, NULL);
2085 if ((bond->params.mode == BOND_MODE_TLB) ||
2086 (bond->params.mode == BOND_MODE_ALB)) {
2087 /* Must be called only after the slave has been
2088 * detached from the list and the curr_active_slave
2089 * has been cleared (if our_slave == old_current),
2090 * but before a new active slave is selected.
2092 bond_alb_deinit_slave(bond, slave);
2095 if (oldcurrent == slave) {
2096 bond_select_active_slave(bond);
2098 if (!bond->curr_active_slave) {
2099 printk(KERN_INFO DRV_NAME
2100 ": %s: now running without any active "
2101 "interface !\n",
2102 bond_dev->name);
2106 if (bond->slave_cnt == 0) {
2107 /* if the last slave was removed, zero the mac address
2108 * of the master so it will be set by the application
2109 * to the mac address of the first slave
2111 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2113 if (list_empty(&bond->vlan_list)) {
2114 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2115 } else {
2116 printk(KERN_WARNING DRV_NAME
2117 ": Warning: clearing HW address of %s while it "
2118 "still has VLANs.\n",
2119 bond_dev->name);
2120 printk(KERN_WARNING DRV_NAME
2121 ": When re-adding slaves, make sure the bond's "
2122 "HW address matches its VLANs'.\n");
2124 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2125 !bond_has_challenged_slaves(bond)) {
2126 printk(KERN_INFO DRV_NAME
2127 ": last VLAN challenged slave %s "
2128 "left bond %s. VLAN blocking is removed\n",
2129 slave_dev->name, bond_dev->name);
2130 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
2133 write_unlock_bh(&bond->lock);
2135 bond_del_vlans_from_slave(bond, slave_dev);
2137 /* If the mode USES_PRIMARY, then we should only remove its
2138 * promisc and mc settings if it was the curr_active_slave, but that was
2139 * already taken care of above when we detached the slave
2141 if (!USES_PRIMARY(bond->params.mode)) {
2142 /* unset promiscuity level from slave */
2143 if (bond_dev->flags & IFF_PROMISC) {
2144 dev_set_promiscuity(slave_dev, -1);
2147 /* unset allmulti level from slave */
2148 if (bond_dev->flags & IFF_ALLMULTI) {
2149 dev_set_allmulti(slave_dev, -1);
2152 /* flush master's mc_list from slave */
2153 bond_mc_list_flush(bond_dev, slave_dev);
2156 netdev_set_master(slave_dev, NULL);
2158 /* close slave before restoring its mac address */
2159 dev_close(slave_dev);
2161 if (app_abi_ver >= 1) {
2162 /* restore original ("permanent") mac address */
2163 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2164 addr.sa_family = slave_dev->type;
2165 dev_set_mac_address(slave_dev, &addr);
2168 /* restore the original state of the
2169 * IFF_NOARP flag that might have been
2170 * set by bond_set_slave_inactive_flags()
2172 if ((slave->original_flags & IFF_NOARP) == 0) {
2173 slave_dev->flags &= ~IFF_NOARP;
2176 kfree(slave);
2178 return 0; /* deletion OK */
2182 * This function releases all slaves.
2184 static int bond_release_all(struct net_device *bond_dev)
2186 struct bonding *bond = bond_dev->priv;
2187 struct slave *slave;
2188 struct net_device *slave_dev;
2189 struct sockaddr addr;
2191 write_lock_bh(&bond->lock);
2193 if (bond->slave_cnt == 0) {
2194 goto out;
2197 bond->current_arp_slave = NULL;
2198 bond->primary_slave = NULL;
2199 bond_change_active_slave(bond, NULL);
2201 while ((slave = bond->first_slave) != NULL) {
2202 /* Inform AD package of unbinding of slave
2203 * before slave is detached from the list.
2205 if (bond->params.mode == BOND_MODE_8023AD) {
2206 bond_3ad_unbind_slave(slave);
2209 slave_dev = slave->dev;
2210 bond_detach_slave(bond, slave);
2212 if ((bond->params.mode == BOND_MODE_TLB) ||
2213 (bond->params.mode == BOND_MODE_ALB)) {
2214 /* must be called only after the slave
2215 * has been detached from the list
2217 bond_alb_deinit_slave(bond, slave);
2220 /* now that the slave is detached, unlock and perform
2221 * all the undo steps that should not be called from
2222 * within a lock.
2224 write_unlock_bh(&bond->lock);
2226 bond_del_vlans_from_slave(bond, slave_dev);
2228 /* If the mode USES_PRIMARY, then we should only remove its
2229 * promisc and mc settings if it was the curr_active_slave, but that was
2230 * already taken care of above when we detached the slave
2232 if (!USES_PRIMARY(bond->params.mode)) {
2233 /* unset promiscuity level from slave */
2234 if (bond_dev->flags & IFF_PROMISC) {
2235 dev_set_promiscuity(slave_dev, -1);
2238 /* unset allmulti level from slave */
2239 if (bond_dev->flags & IFF_ALLMULTI) {
2240 dev_set_allmulti(slave_dev, -1);
2243 /* flush master's mc_list from slave */
2244 bond_mc_list_flush(bond_dev, slave_dev);
2247 netdev_set_master(slave_dev, NULL);
2249 /* close slave before restoring its mac address */
2250 dev_close(slave_dev);
2252 if (app_abi_ver >= 1) {
2253 /* restore original ("permanent") mac address*/
2254 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2255 addr.sa_family = slave_dev->type;
2256 dev_set_mac_address(slave_dev, &addr);
2259 /* restore the original state of the IFF_NOARP flag that might have
2260 * been set by bond_set_slave_inactive_flags()
2262 if ((slave->original_flags & IFF_NOARP) == 0) {
2263 slave_dev->flags &= ~IFF_NOARP;
2266 kfree(slave);
2268 /* re-acquire the lock before getting the next slave */
2269 write_lock_bh(&bond->lock);
2272 /* zero the mac address of the master so it will be
2273 * set by the application to the mac address of the
2274 * first slave
2276 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2278 if (list_empty(&bond->vlan_list)) {
2279 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2280 } else {
2281 printk(KERN_WARNING DRV_NAME
2282 ": Warning: clearing HW address of %s while it "
2283 "still has VLANs.\n",
2284 bond_dev->name);
2285 printk(KERN_WARNING DRV_NAME
2286 ": When re-adding slaves, make sure the bond's "
2287 "HW address matches its VLANs'.\n");
2290 printk(KERN_INFO DRV_NAME
2291 ": %s: released all slaves\n",
2292 bond_dev->name);
2294 out:
2295 write_unlock_bh(&bond->lock);
2297 return 0;
2301 * This function changes the active slave to slave <slave_dev>.
2302 * It returns -EINVAL in the following cases.
2303 * - <slave_dev> is not found in the list.
2304 * - There is not active slave now.
2305 * - <slave_dev> is already active.
2306 * - The link state of <slave_dev> is not BOND_LINK_UP.
2307 * - <slave_dev> is not running.
2308 * In these cases, this fuction does nothing.
2309 * In the other cases, currnt_slave pointer is changed and 0 is returned.
2311 static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2313 struct bonding *bond = bond_dev->priv;
2314 struct slave *old_active = NULL;
2315 struct slave *new_active = NULL;
2316 int res = 0;
2318 if (!USES_PRIMARY(bond->params.mode)) {
2319 return -EINVAL;
2322 /* Verify that master_dev is indeed the master of slave_dev */
2323 if (!(slave_dev->flags & IFF_SLAVE) ||
2324 (slave_dev->master != bond_dev)) {
2325 return -EINVAL;
2328 write_lock_bh(&bond->lock);
2330 old_active = bond->curr_active_slave;
2331 new_active = bond_get_slave_by_dev(bond, slave_dev);
2334 * Changing to the current active: do nothing; return success.
2336 if (new_active && (new_active == old_active)) {
2337 write_unlock_bh(&bond->lock);
2338 return 0;
2341 if ((new_active) &&
2342 (old_active) &&
2343 (new_active->link == BOND_LINK_UP) &&
2344 IS_UP(new_active->dev)) {
2345 bond_change_active_slave(bond, new_active);
2346 } else {
2347 res = -EINVAL;
2350 write_unlock_bh(&bond->lock);
2352 return res;
2355 static int bond_ethtool_ioctl(struct net_device *bond_dev, struct ifreq *ifr)
2357 struct ethtool_drvinfo info;
2358 void __user *addr = ifr->ifr_data;
2359 uint32_t cmd;
2361 if (get_user(cmd, (uint32_t __user *)addr)) {
2362 return -EFAULT;
2365 switch (cmd) {
2366 case ETHTOOL_GDRVINFO:
2367 if (copy_from_user(&info, addr, sizeof(info))) {
2368 return -EFAULT;
2371 if (strcmp(info.driver, "ifenslave") == 0) {
2372 int new_abi_ver;
2373 char *endptr;
2375 new_abi_ver = simple_strtoul(info.fw_version,
2376 &endptr, 0);
2377 if (*endptr) {
2378 printk(KERN_ERR DRV_NAME
2379 ": Error: got invalid ABI "
2380 "version from application\n");
2382 return -EINVAL;
2385 if (orig_app_abi_ver == -1) {
2386 orig_app_abi_ver = new_abi_ver;
2389 app_abi_ver = new_abi_ver;
2392 strncpy(info.driver, DRV_NAME, 32);
2393 strncpy(info.version, DRV_VERSION, 32);
2394 snprintf(info.fw_version, 32, "%d", BOND_ABI_VERSION);
2396 if (copy_to_user(addr, &info, sizeof(info))) {
2397 return -EFAULT;
2400 return 0;
2401 default:
2402 return -EOPNOTSUPP;
2406 static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2408 struct bonding *bond = bond_dev->priv;
2410 info->bond_mode = bond->params.mode;
2411 info->miimon = bond->params.miimon;
2413 read_lock_bh(&bond->lock);
2414 info->num_slaves = bond->slave_cnt;
2415 read_unlock_bh(&bond->lock);
2417 return 0;
2420 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2422 struct bonding *bond = bond_dev->priv;
2423 struct slave *slave;
2424 int i, found = 0;
2426 if (info->slave_id < 0) {
2427 return -ENODEV;
2430 read_lock_bh(&bond->lock);
2432 bond_for_each_slave(bond, slave, i) {
2433 if (i == (int)info->slave_id) {
2434 found = 1;
2435 break;
2439 read_unlock_bh(&bond->lock);
2441 if (found) {
2442 strcpy(info->slave_name, slave->dev->name);
2443 info->link = slave->link;
2444 info->state = slave->state;
2445 info->link_failure_count = slave->link_failure_count;
2446 } else {
2447 return -ENODEV;
2450 return 0;
2453 /*-------------------------------- Monitoring -------------------------------*/
2455 /* this function is called regularly to monitor each slave's link. */
2456 static void bond_mii_monitor(struct net_device *bond_dev)
2458 struct bonding *bond = bond_dev->priv;
2459 struct slave *slave, *oldcurrent;
2460 int do_failover = 0;
2461 int delta_in_ticks;
2462 int i;
2464 read_lock(&bond->lock);
2466 delta_in_ticks = (bond->params.miimon * HZ) / 1000;
2468 if (bond->kill_timers) {
2469 goto out;
2472 if (bond->slave_cnt == 0) {
2473 goto re_arm;
2476 /* we will try to read the link status of each of our slaves, and
2477 * set their IFF_RUNNING flag appropriately. For each slave not
2478 * supporting MII status, we won't do anything so that a user-space
2479 * program could monitor the link itself if needed.
2482 read_lock(&bond->curr_slave_lock);
2483 oldcurrent = bond->curr_active_slave;
2484 read_unlock(&bond->curr_slave_lock);
2486 bond_for_each_slave(bond, slave, i) {
2487 struct net_device *slave_dev = slave->dev;
2488 int link_state;
2489 u16 old_speed = slave->speed;
2490 u8 old_duplex = slave->duplex;
2492 link_state = bond_check_dev_link(bond, slave_dev, 0);
2494 switch (slave->link) {
2495 case BOND_LINK_UP: /* the link was up */
2496 if (link_state == BMSR_LSTATUS) {
2497 /* link stays up, nothing more to do */
2498 break;
2499 } else { /* link going down */
2500 slave->link = BOND_LINK_FAIL;
2501 slave->delay = bond->params.downdelay;
2503 if (slave->link_failure_count < UINT_MAX) {
2504 slave->link_failure_count++;
2507 if (bond->params.downdelay) {
2508 printk(KERN_INFO DRV_NAME
2509 ": %s: link status down for %s "
2510 "interface %s, disabling it in "
2511 "%d ms.\n",
2512 bond_dev->name,
2513 IS_UP(slave_dev)
2514 ? ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
2515 ? ((slave == oldcurrent)
2516 ? "active " : "backup ")
2517 : "")
2518 : "idle ",
2519 slave_dev->name,
2520 bond->params.downdelay * bond->params.miimon);
2523 /* no break ! fall through the BOND_LINK_FAIL test to
2524 ensure proper action to be taken
2526 case BOND_LINK_FAIL: /* the link has just gone down */
2527 if (link_state != BMSR_LSTATUS) {
2528 /* link stays down */
2529 if (slave->delay <= 0) {
2530 /* link down for too long time */
2531 slave->link = BOND_LINK_DOWN;
2533 /* in active/backup mode, we must
2534 * completely disable this interface
2536 if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP) ||
2537 (bond->params.mode == BOND_MODE_8023AD)) {
2538 bond_set_slave_inactive_flags(slave);
2541 printk(KERN_INFO DRV_NAME
2542 ": %s: link status definitely "
2543 "down for interface %s, "
2544 "disabling it\n",
2545 bond_dev->name,
2546 slave_dev->name);
2548 /* notify ad that the link status has changed */
2549 if (bond->params.mode == BOND_MODE_8023AD) {
2550 bond_3ad_handle_link_change(slave, BOND_LINK_DOWN);
2553 if ((bond->params.mode == BOND_MODE_TLB) ||
2554 (bond->params.mode == BOND_MODE_ALB)) {
2555 bond_alb_handle_link_change(bond, slave, BOND_LINK_DOWN);
2558 if (slave == oldcurrent) {
2559 do_failover = 1;
2561 } else {
2562 slave->delay--;
2564 } else {
2565 /* link up again */
2566 slave->link = BOND_LINK_UP;
2567 slave->jiffies = jiffies;
2568 printk(KERN_INFO DRV_NAME
2569 ": %s: link status up again after %d "
2570 "ms for interface %s.\n",
2571 bond_dev->name,
2572 (bond->params.downdelay - slave->delay) * bond->params.miimon,
2573 slave_dev->name);
2575 break;
2576 case BOND_LINK_DOWN: /* the link was down */
2577 if (link_state != BMSR_LSTATUS) {
2578 /* the link stays down, nothing more to do */
2579 break;
2580 } else { /* link going up */
2581 slave->link = BOND_LINK_BACK;
2582 slave->delay = bond->params.updelay;
2584 if (bond->params.updelay) {
2585 /* if updelay == 0, no need to
2586 advertise about a 0 ms delay */
2587 printk(KERN_INFO DRV_NAME
2588 ": %s: link status up for "
2589 "interface %s, enabling it "
2590 "in %d ms.\n",
2591 bond_dev->name,
2592 slave_dev->name,
2593 bond->params.updelay * bond->params.miimon);
2596 /* no break ! fall through the BOND_LINK_BACK state in
2597 case there's something to do.
2599 case BOND_LINK_BACK: /* the link has just come back */
2600 if (link_state != BMSR_LSTATUS) {
2601 /* link down again */
2602 slave->link = BOND_LINK_DOWN;
2604 printk(KERN_INFO DRV_NAME
2605 ": %s: link status down again after %d "
2606 "ms for interface %s.\n",
2607 bond_dev->name,
2608 (bond->params.updelay - slave->delay) * bond->params.miimon,
2609 slave_dev->name);
2610 } else {
2611 /* link stays up */
2612 if (slave->delay == 0) {
2613 /* now the link has been up for long time enough */
2614 slave->link = BOND_LINK_UP;
2615 slave->jiffies = jiffies;
2617 if (bond->params.mode == BOND_MODE_8023AD) {
2618 /* prevent it from being the active one */
2619 slave->state = BOND_STATE_BACKUP;
2620 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2621 /* make it immediately active */
2622 slave->state = BOND_STATE_ACTIVE;
2623 } else if (slave != bond->primary_slave) {
2624 /* prevent it from being the active one */
2625 slave->state = BOND_STATE_BACKUP;
2628 printk(KERN_INFO DRV_NAME
2629 ": %s: link status definitely "
2630 "up for interface %s.\n",
2631 bond_dev->name,
2632 slave_dev->name);
2634 /* notify ad that the link status has changed */
2635 if (bond->params.mode == BOND_MODE_8023AD) {
2636 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2639 if ((bond->params.mode == BOND_MODE_TLB) ||
2640 (bond->params.mode == BOND_MODE_ALB)) {
2641 bond_alb_handle_link_change(bond, slave, BOND_LINK_UP);
2644 if ((!oldcurrent) ||
2645 (slave == bond->primary_slave)) {
2646 do_failover = 1;
2648 } else {
2649 slave->delay--;
2652 break;
2653 default:
2654 /* Should not happen */
2655 printk(KERN_ERR "bonding: Error: %s Illegal value (link=%d)\n",
2656 slave->dev->name, slave->link);
2657 goto out;
2658 } /* end of switch (slave->link) */
2660 bond_update_speed_duplex(slave);
2662 if (bond->params.mode == BOND_MODE_8023AD) {
2663 if (old_speed != slave->speed) {
2664 bond_3ad_adapter_speed_changed(slave);
2667 if (old_duplex != slave->duplex) {
2668 bond_3ad_adapter_duplex_changed(slave);
2672 } /* end of for */
2674 if (do_failover) {
2675 write_lock(&bond->curr_slave_lock);
2677 bond_select_active_slave(bond);
2679 if (oldcurrent && !bond->curr_active_slave) {
2680 printk(KERN_INFO DRV_NAME
2681 ": %s: now running without any active "
2682 "interface !\n",
2683 bond_dev->name);
2686 write_unlock(&bond->curr_slave_lock);
2689 re_arm:
2690 if (bond->params.miimon) {
2691 mod_timer(&bond->mii_timer, jiffies + delta_in_ticks);
2693 out:
2694 read_unlock(&bond->lock);
2697 static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2699 int i;
2700 u32 *targets = bond->params.arp_targets;
2702 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
2703 arp_send(ARPOP_REQUEST, ETH_P_ARP, targets[i], slave->dev,
2704 my_ip, NULL, slave->dev->dev_addr,
2705 NULL);
2710 * this function is called regularly to monitor each slave's link
2711 * ensuring that traffic is being sent and received when arp monitoring
2712 * is used in load-balancing mode. if the adapter has been dormant, then an
2713 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2714 * arp monitoring in active backup mode.
2716 static void bond_loadbalance_arp_mon(struct net_device *bond_dev)
2718 struct bonding *bond = bond_dev->priv;
2719 struct slave *slave, *oldcurrent;
2720 int do_failover = 0;
2721 int delta_in_ticks;
2722 int i;
2724 read_lock(&bond->lock);
2726 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2728 if (bond->kill_timers) {
2729 goto out;
2732 if (bond->slave_cnt == 0) {
2733 goto re_arm;
2736 read_lock(&bond->curr_slave_lock);
2737 oldcurrent = bond->curr_active_slave;
2738 read_unlock(&bond->curr_slave_lock);
2740 /* see if any of the previous devices are up now (i.e. they have
2741 * xmt and rcv traffic). the curr_active_slave does not come into
2742 * the picture unless it is null. also, slave->jiffies is not needed
2743 * here because we send an arp on each slave and give a slave as
2744 * long as it needs to get the tx/rx within the delta.
2745 * TODO: what about up/down delay in arp mode? it wasn't here before
2746 * so it can wait
2748 bond_for_each_slave(bond, slave, i) {
2749 if (slave->link != BOND_LINK_UP) {
2750 if (((jiffies - slave->dev->trans_start) <= delta_in_ticks) &&
2751 ((jiffies - slave->dev->last_rx) <= delta_in_ticks)) {
2753 slave->link = BOND_LINK_UP;
2754 slave->state = BOND_STATE_ACTIVE;
2756 /* primary_slave has no meaning in round-robin
2757 * mode. the window of a slave being up and
2758 * curr_active_slave being null after enslaving
2759 * is closed.
2761 if (!oldcurrent) {
2762 printk(KERN_INFO DRV_NAME
2763 ": %s: link status definitely "
2764 "up for interface %s, ",
2765 bond_dev->name,
2766 slave->dev->name);
2767 do_failover = 1;
2768 } else {
2769 printk(KERN_INFO DRV_NAME
2770 ": %s: interface %s is now up\n",
2771 bond_dev->name,
2772 slave->dev->name);
2775 } else {
2776 /* slave->link == BOND_LINK_UP */
2778 /* not all switches will respond to an arp request
2779 * when the source ip is 0, so don't take the link down
2780 * if we don't know our ip yet
2782 if (((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
2783 (((jiffies - slave->dev->last_rx) >= (2*delta_in_ticks)) &&
2784 my_ip)) {
2786 slave->link = BOND_LINK_DOWN;
2787 slave->state = BOND_STATE_BACKUP;
2789 if (slave->link_failure_count < UINT_MAX) {
2790 slave->link_failure_count++;
2793 printk(KERN_INFO DRV_NAME
2794 ": %s: interface %s is now down.\n",
2795 bond_dev->name,
2796 slave->dev->name);
2798 if (slave == oldcurrent) {
2799 do_failover = 1;
2804 /* note: if switch is in round-robin mode, all links
2805 * must tx arp to ensure all links rx an arp - otherwise
2806 * links may oscillate or not come up at all; if switch is
2807 * in something like xor mode, there is nothing we can
2808 * do - all replies will be rx'ed on same link causing slaves
2809 * to be unstable during low/no traffic periods
2811 if (IS_UP(slave->dev)) {
2812 bond_arp_send_all(bond, slave);
2816 if (do_failover) {
2817 write_lock(&bond->curr_slave_lock);
2819 bond_select_active_slave(bond);
2821 if (oldcurrent && !bond->curr_active_slave) {
2822 printk(KERN_INFO DRV_NAME
2823 ": %s: now running without any active "
2824 "interface !\n",
2825 bond_dev->name);
2828 write_unlock(&bond->curr_slave_lock);
2831 re_arm:
2832 if (bond->params.arp_interval) {
2833 mod_timer(&bond->arp_timer, jiffies + delta_in_ticks);
2835 out:
2836 read_unlock(&bond->lock);
2840 * When using arp monitoring in active-backup mode, this function is
2841 * called to determine if any backup slaves have went down or a new
2842 * current slave needs to be found.
2843 * The backup slaves never generate traffic, they are considered up by merely
2844 * receiving traffic. If the current slave goes down, each backup slave will
2845 * be given the opportunity to tx/rx an arp before being taken down - this
2846 * prevents all slaves from being taken down due to the current slave not
2847 * sending any traffic for the backups to receive. The arps are not necessarily
2848 * necessary, any tx and rx traffic will keep the current slave up. While any
2849 * rx traffic will keep the backup slaves up, the current slave is responsible
2850 * for generating traffic to keep them up regardless of any other traffic they
2851 * may have received.
2852 * see loadbalance_arp_monitor for arp monitoring in load balancing mode
2854 static void bond_activebackup_arp_mon(struct net_device *bond_dev)
2856 struct bonding *bond = bond_dev->priv;
2857 struct slave *slave;
2858 int delta_in_ticks;
2859 int i;
2861 read_lock(&bond->lock);
2863 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2865 if (bond->kill_timers) {
2866 goto out;
2869 if (bond->slave_cnt == 0) {
2870 goto re_arm;
2873 /* determine if any slave has come up or any backup slave has
2874 * gone down
2875 * TODO: what about up/down delay in arp mode? it wasn't here before
2876 * so it can wait
2878 bond_for_each_slave(bond, slave, i) {
2879 if (slave->link != BOND_LINK_UP) {
2880 if ((jiffies - slave->dev->last_rx) <= delta_in_ticks) {
2882 slave->link = BOND_LINK_UP;
2884 write_lock(&bond->curr_slave_lock);
2886 if ((!bond->curr_active_slave) &&
2887 ((jiffies - slave->dev->trans_start) <= delta_in_ticks)) {
2888 bond_change_active_slave(bond, slave);
2889 bond->current_arp_slave = NULL;
2890 } else if (bond->curr_active_slave != slave) {
2891 /* this slave has just come up but we
2892 * already have a current slave; this
2893 * can also happen if bond_enslave adds
2894 * a new slave that is up while we are
2895 * searching for a new slave
2897 bond_set_slave_inactive_flags(slave);
2898 bond->current_arp_slave = NULL;
2901 if (slave == bond->curr_active_slave) {
2902 printk(KERN_INFO DRV_NAME
2903 ": %s: %s is up and now the "
2904 "active interface\n",
2905 bond_dev->name,
2906 slave->dev->name);
2907 } else {
2908 printk(KERN_INFO DRV_NAME
2909 ": %s: backup interface %s is "
2910 "now up\n",
2911 bond_dev->name,
2912 slave->dev->name);
2915 write_unlock(&bond->curr_slave_lock);
2917 } else {
2918 read_lock(&bond->curr_slave_lock);
2920 if ((slave != bond->curr_active_slave) &&
2921 (!bond->current_arp_slave) &&
2922 (((jiffies - slave->dev->last_rx) >= 3*delta_in_ticks) &&
2923 my_ip)) {
2924 /* a backup slave has gone down; three times
2925 * the delta allows the current slave to be
2926 * taken out before the backup slave.
2927 * note: a non-null current_arp_slave indicates
2928 * the curr_active_slave went down and we are
2929 * searching for a new one; under this
2930 * condition we only take the curr_active_slave
2931 * down - this gives each slave a chance to
2932 * tx/rx traffic before being taken out
2935 read_unlock(&bond->curr_slave_lock);
2937 slave->link = BOND_LINK_DOWN;
2939 if (slave->link_failure_count < UINT_MAX) {
2940 slave->link_failure_count++;
2943 bond_set_slave_inactive_flags(slave);
2945 printk(KERN_INFO DRV_NAME
2946 ": %s: backup interface %s is now down\n",
2947 bond_dev->name,
2948 slave->dev->name);
2949 } else {
2950 read_unlock(&bond->curr_slave_lock);
2955 read_lock(&bond->curr_slave_lock);
2956 slave = bond->curr_active_slave;
2957 read_unlock(&bond->curr_slave_lock);
2959 if (slave) {
2960 /* if we have sent traffic in the past 2*arp_intervals but
2961 * haven't xmit and rx traffic in that time interval, select
2962 * a different slave. slave->jiffies is only updated when
2963 * a slave first becomes the curr_active_slave - not necessarily
2964 * after every arp; this ensures the slave has a full 2*delta
2965 * before being taken out. if a primary is being used, check
2966 * if it is up and needs to take over as the curr_active_slave
2968 if ((((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
2969 (((jiffies - slave->dev->last_rx) >= (2*delta_in_ticks)) &&
2970 my_ip)) &&
2971 ((jiffies - slave->jiffies) >= 2*delta_in_ticks)) {
2973 slave->link = BOND_LINK_DOWN;
2975 if (slave->link_failure_count < UINT_MAX) {
2976 slave->link_failure_count++;
2979 printk(KERN_INFO DRV_NAME
2980 ": %s: link status down for active interface "
2981 "%s, disabling it\n",
2982 bond_dev->name,
2983 slave->dev->name);
2985 write_lock(&bond->curr_slave_lock);
2987 bond_select_active_slave(bond);
2988 slave = bond->curr_active_slave;
2990 write_unlock(&bond->curr_slave_lock);
2992 bond->current_arp_slave = slave;
2994 if (slave) {
2995 slave->jiffies = jiffies;
2997 } else if ((bond->primary_slave) &&
2998 (bond->primary_slave != slave) &&
2999 (bond->primary_slave->link == BOND_LINK_UP)) {
3000 /* at this point, slave is the curr_active_slave */
3001 printk(KERN_INFO DRV_NAME
3002 ": %s: changing from interface %s to primary "
3003 "interface %s\n",
3004 bond_dev->name,
3005 slave->dev->name,
3006 bond->primary_slave->dev->name);
3008 /* primary is up so switch to it */
3009 write_lock(&bond->curr_slave_lock);
3010 bond_change_active_slave(bond, bond->primary_slave);
3011 write_unlock(&bond->curr_slave_lock);
3013 slave = bond->primary_slave;
3014 slave->jiffies = jiffies;
3015 } else {
3016 bond->current_arp_slave = NULL;
3019 /* the current slave must tx an arp to ensure backup slaves
3020 * rx traffic
3022 if (slave && my_ip) {
3023 bond_arp_send_all(bond, slave);
3027 /* if we don't have a curr_active_slave, search for the next available
3028 * backup slave from the current_arp_slave and make it the candidate
3029 * for becoming the curr_active_slave
3031 if (!slave) {
3032 if (!bond->current_arp_slave) {
3033 bond->current_arp_slave = bond->first_slave;
3036 if (bond->current_arp_slave) {
3037 bond_set_slave_inactive_flags(bond->current_arp_slave);
3039 /* search for next candidate */
3040 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave) {
3041 if (IS_UP(slave->dev)) {
3042 slave->link = BOND_LINK_BACK;
3043 bond_set_slave_active_flags(slave);
3044 bond_arp_send_all(bond, slave);
3045 slave->jiffies = jiffies;
3046 bond->current_arp_slave = slave;
3047 break;
3050 /* if the link state is up at this point, we
3051 * mark it down - this can happen if we have
3052 * simultaneous link failures and
3053 * reselect_active_interface doesn't make this
3054 * one the current slave so it is still marked
3055 * up when it is actually down
3057 if (slave->link == BOND_LINK_UP) {
3058 slave->link = BOND_LINK_DOWN;
3059 if (slave->link_failure_count < UINT_MAX) {
3060 slave->link_failure_count++;
3063 bond_set_slave_inactive_flags(slave);
3065 printk(KERN_INFO DRV_NAME
3066 ": %s: backup interface %s is "
3067 "now down.\n",
3068 bond_dev->name,
3069 slave->dev->name);
3075 re_arm:
3076 if (bond->params.arp_interval) {
3077 mod_timer(&bond->arp_timer, jiffies + delta_in_ticks);
3079 out:
3080 read_unlock(&bond->lock);
3083 /*------------------------------ proc/seq_file-------------------------------*/
3085 #ifdef CONFIG_PROC_FS
3087 #define SEQ_START_TOKEN ((void *)1)
3089 static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
3091 struct bonding *bond = seq->private;
3092 loff_t off = 0;
3093 struct slave *slave;
3094 int i;
3096 /* make sure the bond won't be taken away */
3097 read_lock(&dev_base_lock);
3098 read_lock_bh(&bond->lock);
3100 if (*pos == 0) {
3101 return SEQ_START_TOKEN;
3104 bond_for_each_slave(bond, slave, i) {
3105 if (++off == *pos) {
3106 return slave;
3110 return NULL;
3113 static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3115 struct bonding *bond = seq->private;
3116 struct slave *slave = v;
3118 ++*pos;
3119 if (v == SEQ_START_TOKEN) {
3120 return bond->first_slave;
3123 slave = slave->next;
3125 return (slave == bond->first_slave) ? NULL : slave;
3128 static void bond_info_seq_stop(struct seq_file *seq, void *v)
3130 struct bonding *bond = seq->private;
3132 read_unlock_bh(&bond->lock);
3133 read_unlock(&dev_base_lock);
3136 static void bond_info_show_master(struct seq_file *seq)
3138 struct bonding *bond = seq->private;
3139 struct slave *curr;
3141 read_lock(&bond->curr_slave_lock);
3142 curr = bond->curr_active_slave;
3143 read_unlock(&bond->curr_slave_lock);
3145 seq_printf(seq, "Bonding Mode: %s\n",
3146 bond_mode_name(bond->params.mode));
3148 if (USES_PRIMARY(bond->params.mode)) {
3149 seq_printf(seq, "Primary Slave: %s\n",
3150 (bond->params.primary[0]) ?
3151 bond->params.primary : "None");
3153 seq_printf(seq, "Currently Active Slave: %s\n",
3154 (curr) ? curr->dev->name : "None");
3157 seq_printf(seq, "MII Status: %s\n", (curr) ? "up" : "down");
3158 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3159 seq_printf(seq, "Up Delay (ms): %d\n",
3160 bond->params.updelay * bond->params.miimon);
3161 seq_printf(seq, "Down Delay (ms): %d\n",
3162 bond->params.downdelay * bond->params.miimon);
3164 if (bond->params.mode == BOND_MODE_8023AD) {
3165 struct ad_info ad_info;
3167 seq_puts(seq, "\n802.3ad info\n");
3168 seq_printf(seq, "LACP rate: %s\n",
3169 (bond->params.lacp_fast) ? "fast" : "slow");
3171 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3172 seq_printf(seq, "bond %s has no active aggregator\n",
3173 bond->dev->name);
3174 } else {
3175 seq_printf(seq, "Active Aggregator Info:\n");
3177 seq_printf(seq, "\tAggregator ID: %d\n",
3178 ad_info.aggregator_id);
3179 seq_printf(seq, "\tNumber of ports: %d\n",
3180 ad_info.ports);
3181 seq_printf(seq, "\tActor Key: %d\n",
3182 ad_info.actor_key);
3183 seq_printf(seq, "\tPartner Key: %d\n",
3184 ad_info.partner_key);
3185 seq_printf(seq, "\tPartner Mac Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
3186 ad_info.partner_system[0],
3187 ad_info.partner_system[1],
3188 ad_info.partner_system[2],
3189 ad_info.partner_system[3],
3190 ad_info.partner_system[4],
3191 ad_info.partner_system[5]);
3196 static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
3198 struct bonding *bond = seq->private;
3200 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3201 seq_printf(seq, "MII Status: %s\n",
3202 (slave->link == BOND_LINK_UP) ? "up" : "down");
3203 seq_printf(seq, "Link Failure Count: %d\n",
3204 slave->link_failure_count);
3206 if (app_abi_ver >= 1) {
3207 seq_printf(seq,
3208 "Permanent HW addr: %02x:%02x:%02x:%02x:%02x:%02x\n",
3209 slave->perm_hwaddr[0],
3210 slave->perm_hwaddr[1],
3211 slave->perm_hwaddr[2],
3212 slave->perm_hwaddr[3],
3213 slave->perm_hwaddr[4],
3214 slave->perm_hwaddr[5]);
3217 if (bond->params.mode == BOND_MODE_8023AD) {
3218 const struct aggregator *agg
3219 = SLAVE_AD_INFO(slave).port.aggregator;
3221 if (agg) {
3222 seq_printf(seq, "Aggregator ID: %d\n",
3223 agg->aggregator_identifier);
3224 } else {
3225 seq_puts(seq, "Aggregator ID: N/A\n");
3230 static int bond_info_seq_show(struct seq_file *seq, void *v)
3232 if (v == SEQ_START_TOKEN) {
3233 seq_printf(seq, "%s\n", version);
3234 bond_info_show_master(seq);
3235 } else {
3236 bond_info_show_slave(seq, v);
3239 return 0;
3242 static struct seq_operations bond_info_seq_ops = {
3243 .start = bond_info_seq_start,
3244 .next = bond_info_seq_next,
3245 .stop = bond_info_seq_stop,
3246 .show = bond_info_seq_show,
3249 static int bond_info_open(struct inode *inode, struct file *file)
3251 struct seq_file *seq;
3252 struct proc_dir_entry *proc;
3253 int res;
3255 res = seq_open(file, &bond_info_seq_ops);
3256 if (!res) {
3257 /* recover the pointer buried in proc_dir_entry data */
3258 seq = file->private_data;
3259 proc = PDE(inode);
3260 seq->private = proc->data;
3263 return res;
3266 static struct file_operations bond_info_fops = {
3267 .owner = THIS_MODULE,
3268 .open = bond_info_open,
3269 .read = seq_read,
3270 .llseek = seq_lseek,
3271 .release = seq_release,
3274 static int bond_create_proc_entry(struct bonding *bond)
3276 struct net_device *bond_dev = bond->dev;
3278 if (bond_proc_dir) {
3279 bond->proc_entry = create_proc_entry(bond_dev->name,
3280 S_IRUGO,
3281 bond_proc_dir);
3282 if (bond->proc_entry == NULL) {
3283 printk(KERN_WARNING DRV_NAME
3284 ": Warning: Cannot create /proc/net/%s/%s\n",
3285 DRV_NAME, bond_dev->name);
3286 } else {
3287 bond->proc_entry->data = bond;
3288 bond->proc_entry->proc_fops = &bond_info_fops;
3289 bond->proc_entry->owner = THIS_MODULE;
3290 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
3294 return 0;
3297 static void bond_remove_proc_entry(struct bonding *bond)
3299 if (bond_proc_dir && bond->proc_entry) {
3300 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3301 memset(bond->proc_file_name, 0, IFNAMSIZ);
3302 bond->proc_entry = NULL;
3306 /* Create the bonding directory under /proc/net, if doesn't exist yet.
3307 * Caller must hold rtnl_lock.
3309 static void bond_create_proc_dir(void)
3311 int len = strlen(DRV_NAME);
3313 for (bond_proc_dir = proc_net->subdir; bond_proc_dir;
3314 bond_proc_dir = bond_proc_dir->next) {
3315 if ((bond_proc_dir->namelen == len) &&
3316 !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3317 break;
3321 if (!bond_proc_dir) {
3322 bond_proc_dir = proc_mkdir(DRV_NAME, proc_net);
3323 if (bond_proc_dir) {
3324 bond_proc_dir->owner = THIS_MODULE;
3325 } else {
3326 printk(KERN_WARNING DRV_NAME
3327 ": Warning: cannot create /proc/net/%s\n",
3328 DRV_NAME);
3333 /* Destroy the bonding directory under /proc/net, if empty.
3334 * Caller must hold rtnl_lock.
3336 static void bond_destroy_proc_dir(void)
3338 struct proc_dir_entry *de;
3340 if (!bond_proc_dir) {
3341 return;
3344 /* verify that the /proc dir is empty */
3345 for (de = bond_proc_dir->subdir; de; de = de->next) {
3346 /* ignore . and .. */
3347 if (*(de->name) != '.') {
3348 break;
3352 if (de) {
3353 if (bond_proc_dir->owner == THIS_MODULE) {
3354 bond_proc_dir->owner = NULL;
3356 } else {
3357 remove_proc_entry(DRV_NAME, proc_net);
3358 bond_proc_dir = NULL;
3361 #endif /* CONFIG_PROC_FS */
3363 /*-------------------------- netdev event handling --------------------------*/
3366 * Change device name
3368 static int bond_event_changename(struct bonding *bond)
3370 #ifdef CONFIG_PROC_FS
3371 bond_remove_proc_entry(bond);
3372 bond_create_proc_entry(bond);
3373 #endif
3375 return NOTIFY_DONE;
3378 static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
3380 struct bonding *event_bond = bond_dev->priv;
3382 switch (event) {
3383 case NETDEV_CHANGENAME:
3384 return bond_event_changename(event_bond);
3385 case NETDEV_UNREGISTER:
3387 * TODO: remove a bond from the list?
3389 break;
3390 default:
3391 break;
3394 return NOTIFY_DONE;
3397 static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
3399 struct net_device *bond_dev = slave_dev->master;
3401 switch (event) {
3402 case NETDEV_UNREGISTER:
3403 if (bond_dev) {
3404 bond_release(bond_dev, slave_dev);
3406 break;
3407 case NETDEV_CHANGE:
3409 * TODO: is this what we get if somebody
3410 * sets up a hierarchical bond, then rmmod's
3411 * one of the slave bonding devices?
3413 break;
3414 case NETDEV_DOWN:
3416 * ... Or is it this?
3418 break;
3419 case NETDEV_CHANGEMTU:
3421 * TODO: Should slaves be allowed to
3422 * independently alter their MTU? For
3423 * an active-backup bond, slaves need
3424 * not be the same type of device, so
3425 * MTUs may vary. For other modes,
3426 * slaves arguably should have the
3427 * same MTUs. To do this, we'd need to
3428 * take over the slave's change_mtu
3429 * function for the duration of their
3430 * servitude.
3432 break;
3433 case NETDEV_CHANGENAME:
3435 * TODO: handle changing the primary's name
3437 break;
3438 default:
3439 break;
3442 return NOTIFY_DONE;
3446 * bond_netdev_event: handle netdev notifier chain events.
3448 * This function receives events for the netdev chain. The caller (an
3449 * ioctl handler calling notifier_call_chain) holds the necessary
3450 * locks for us to safely manipulate the slave devices (RTNL lock,
3451 * dev_probe_lock).
3453 static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3455 struct net_device *event_dev = (struct net_device *)ptr;
3457 dprintk("event_dev: %s, event: %lx\n",
3458 (event_dev ? event_dev->name : "None"),
3459 event);
3461 if (event_dev->flags & IFF_MASTER) {
3462 dprintk("IFF_MASTER\n");
3463 return bond_master_netdev_event(event, event_dev);
3466 if (event_dev->flags & IFF_SLAVE) {
3467 dprintk("IFF_SLAVE\n");
3468 return bond_slave_netdev_event(event, event_dev);
3471 return NOTIFY_DONE;
3474 static struct notifier_block bond_netdev_notifier = {
3475 .notifier_call = bond_netdev_event,
3478 /*-------------------------- Packet type handling ---------------------------*/
3480 /* register to receive lacpdus on a bond */
3481 static void bond_register_lacpdu(struct bonding *bond)
3483 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3485 /* initialize packet type */
3486 pk_type->type = PKT_TYPE_LACPDU;
3487 pk_type->dev = bond->dev;
3488 pk_type->func = bond_3ad_lacpdu_recv;
3490 dev_add_pack(pk_type);
3493 /* unregister to receive lacpdus on a bond */
3494 static void bond_unregister_lacpdu(struct bonding *bond)
3496 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3499 /*-------------------------- Device entry points ----------------------------*/
3501 static int bond_open(struct net_device *bond_dev)
3503 struct bonding *bond = bond_dev->priv;
3504 struct timer_list *mii_timer = &bond->mii_timer;
3505 struct timer_list *arp_timer = &bond->arp_timer;
3507 bond->kill_timers = 0;
3509 if ((bond->params.mode == BOND_MODE_TLB) ||
3510 (bond->params.mode == BOND_MODE_ALB)) {
3511 struct timer_list *alb_timer = &(BOND_ALB_INFO(bond).alb_timer);
3513 /* bond_alb_initialize must be called before the timer
3514 * is started.
3516 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3517 /* something went wrong - fail the open operation */
3518 return -1;
3521 init_timer(alb_timer);
3522 alb_timer->expires = jiffies + 1;
3523 alb_timer->data = (unsigned long)bond;
3524 alb_timer->function = (void *)&bond_alb_monitor;
3525 add_timer(alb_timer);
3528 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3529 init_timer(mii_timer);
3530 mii_timer->expires = jiffies + 1;
3531 mii_timer->data = (unsigned long)bond_dev;
3532 mii_timer->function = (void *)&bond_mii_monitor;
3533 add_timer(mii_timer);
3536 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3537 init_timer(arp_timer);
3538 arp_timer->expires = jiffies + 1;
3539 arp_timer->data = (unsigned long)bond_dev;
3540 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
3541 arp_timer->function = (void *)&bond_activebackup_arp_mon;
3542 } else {
3543 arp_timer->function = (void *)&bond_loadbalance_arp_mon;
3545 add_timer(arp_timer);
3548 if (bond->params.mode == BOND_MODE_8023AD) {
3549 struct timer_list *ad_timer = &(BOND_AD_INFO(bond).ad_timer);
3550 init_timer(ad_timer);
3551 ad_timer->expires = jiffies + 1;
3552 ad_timer->data = (unsigned long)bond;
3553 ad_timer->function = (void *)&bond_3ad_state_machine_handler;
3554 add_timer(ad_timer);
3556 /* register to receive LACPDUs */
3557 bond_register_lacpdu(bond);
3560 return 0;
3563 static int bond_close(struct net_device *bond_dev)
3565 struct bonding *bond = bond_dev->priv;
3567 if (bond->params.mode == BOND_MODE_8023AD) {
3568 /* Unregister the receive of LACPDUs */
3569 bond_unregister_lacpdu(bond);
3572 write_lock_bh(&bond->lock);
3574 bond_mc_list_destroy(bond);
3576 /* signal timers not to re-arm */
3577 bond->kill_timers = 1;
3579 write_unlock_bh(&bond->lock);
3581 /* del_timer_sync must run without holding the bond->lock
3582 * because a running timer might be trying to hold it too
3585 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3586 del_timer_sync(&bond->mii_timer);
3589 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3590 del_timer_sync(&bond->arp_timer);
3593 switch (bond->params.mode) {
3594 case BOND_MODE_8023AD:
3595 del_timer_sync(&(BOND_AD_INFO(bond).ad_timer));
3596 break;
3597 case BOND_MODE_TLB:
3598 case BOND_MODE_ALB:
3599 del_timer_sync(&(BOND_ALB_INFO(bond).alb_timer));
3600 break;
3601 default:
3602 break;
3605 /* Release the bonded slaves */
3606 bond_release_all(bond_dev);
3608 if ((bond->params.mode == BOND_MODE_TLB) ||
3609 (bond->params.mode == BOND_MODE_ALB)) {
3610 /* Must be called only after all
3611 * slaves have been released
3613 bond_alb_deinitialize(bond);
3616 return 0;
3619 static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3621 struct bonding *bond = bond_dev->priv;
3622 struct net_device_stats *stats = &(bond->stats), *sstats;
3623 struct slave *slave;
3624 int i;
3626 memset(stats, 0, sizeof(struct net_device_stats));
3628 read_lock_bh(&bond->lock);
3630 bond_for_each_slave(bond, slave, i) {
3631 sstats = slave->dev->get_stats(slave->dev);
3633 stats->rx_packets += sstats->rx_packets;
3634 stats->rx_bytes += sstats->rx_bytes;
3635 stats->rx_errors += sstats->rx_errors;
3636 stats->rx_dropped += sstats->rx_dropped;
3638 stats->tx_packets += sstats->tx_packets;
3639 stats->tx_bytes += sstats->tx_bytes;
3640 stats->tx_errors += sstats->tx_errors;
3641 stats->tx_dropped += sstats->tx_dropped;
3643 stats->multicast += sstats->multicast;
3644 stats->collisions += sstats->collisions;
3646 stats->rx_length_errors += sstats->rx_length_errors;
3647 stats->rx_over_errors += sstats->rx_over_errors;
3648 stats->rx_crc_errors += sstats->rx_crc_errors;
3649 stats->rx_frame_errors += sstats->rx_frame_errors;
3650 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3651 stats->rx_missed_errors += sstats->rx_missed_errors;
3653 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3654 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3655 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3656 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3657 stats->tx_window_errors += sstats->tx_window_errors;
3660 read_unlock_bh(&bond->lock);
3662 return stats;
3665 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3667 struct net_device *slave_dev = NULL;
3668 struct ifbond k_binfo;
3669 struct ifbond __user *u_binfo = NULL;
3670 struct ifslave k_sinfo;
3671 struct ifslave __user *u_sinfo = NULL;
3672 struct mii_ioctl_data *mii = NULL;
3673 int prev_abi_ver = orig_app_abi_ver;
3674 int res = 0;
3676 dprintk("bond_ioctl: master=%s, cmd=%d\n",
3677 bond_dev->name, cmd);
3679 switch (cmd) {
3680 case SIOCETHTOOL:
3681 return bond_ethtool_ioctl(bond_dev, ifr);
3682 case SIOCGMIIPHY:
3683 mii = if_mii(ifr);
3684 if (!mii) {
3685 return -EINVAL;
3687 mii->phy_id = 0;
3688 /* Fall Through */
3689 case SIOCGMIIREG:
3691 * We do this again just in case we were called by SIOCGMIIREG
3692 * instead of SIOCGMIIPHY.
3694 mii = if_mii(ifr);
3695 if (!mii) {
3696 return -EINVAL;
3699 if (mii->reg_num == 1) {
3700 struct bonding *bond = bond_dev->priv;
3701 mii->val_out = 0;
3702 read_lock_bh(&bond->lock);
3703 read_lock(&bond->curr_slave_lock);
3704 if (bond->curr_active_slave) {
3705 mii->val_out = BMSR_LSTATUS;
3707 read_unlock(&bond->curr_slave_lock);
3708 read_unlock_bh(&bond->lock);
3711 return 0;
3712 case BOND_INFO_QUERY_OLD:
3713 case SIOCBONDINFOQUERY:
3714 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3716 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
3717 return -EFAULT;
3720 res = bond_info_query(bond_dev, &k_binfo);
3721 if (res == 0) {
3722 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
3723 return -EFAULT;
3727 return res;
3728 case BOND_SLAVE_INFO_QUERY_OLD:
3729 case SIOCBONDSLAVEINFOQUERY:
3730 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3732 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
3733 return -EFAULT;
3736 res = bond_slave_info_query(bond_dev, &k_sinfo);
3737 if (res == 0) {
3738 if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
3739 return -EFAULT;
3743 return res;
3744 default:
3745 /* Go on */
3746 break;
3749 if (!capable(CAP_NET_ADMIN)) {
3750 return -EPERM;
3753 if (orig_app_abi_ver == -1) {
3754 /* no orig_app_abi_ver was provided yet, so we'll use the
3755 * current one from now on, even if it's 0
3757 orig_app_abi_ver = app_abi_ver;
3759 } else if (orig_app_abi_ver != app_abi_ver) {
3760 printk(KERN_ERR DRV_NAME
3761 ": Error: already using ifenslave ABI version %d; to "
3762 "upgrade ifenslave to version %d, you must first "
3763 "reload bonding.\n",
3764 orig_app_abi_ver, app_abi_ver);
3765 return -EINVAL;
3768 slave_dev = dev_get_by_name(ifr->ifr_slave);
3770 dprintk("slave_dev=%p: \n", slave_dev);
3772 if (!slave_dev) {
3773 res = -ENODEV;
3774 } else {
3775 dprintk("slave_dev->name=%s: \n", slave_dev->name);
3776 switch (cmd) {
3777 case BOND_ENSLAVE_OLD:
3778 case SIOCBONDENSLAVE:
3779 res = bond_enslave(bond_dev, slave_dev);
3780 break;
3781 case BOND_RELEASE_OLD:
3782 case SIOCBONDRELEASE:
3783 res = bond_release(bond_dev, slave_dev);
3784 break;
3785 case BOND_SETHWADDR_OLD:
3786 case SIOCBONDSETHWADDR:
3787 res = bond_sethwaddr(bond_dev, slave_dev);
3788 break;
3789 case BOND_CHANGE_ACTIVE_OLD:
3790 case SIOCBONDCHANGEACTIVE:
3791 res = bond_ioctl_change_active(bond_dev, slave_dev);
3792 break;
3793 default:
3794 res = -EOPNOTSUPP;
3797 dev_put(slave_dev);
3800 if (res < 0) {
3801 /* The ioctl failed, so there's no point in changing the
3802 * orig_app_abi_ver. We'll restore it's value just in case
3803 * we've changed it earlier in this function.
3805 orig_app_abi_ver = prev_abi_ver;
3808 return res;
3811 static void bond_set_multicast_list(struct net_device *bond_dev)
3813 struct bonding *bond = bond_dev->priv;
3814 struct dev_mc_list *dmi;
3816 write_lock_bh(&bond->lock);
3819 * Do promisc before checking multicast_mode
3821 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
3822 bond_set_promiscuity(bond, 1);
3825 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
3826 bond_set_promiscuity(bond, -1);
3829 /* set allmulti flag to slaves */
3830 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
3831 bond_set_allmulti(bond, 1);
3834 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
3835 bond_set_allmulti(bond, -1);
3838 bond->flags = bond_dev->flags;
3840 /* looking for addresses to add to slaves' mc list */
3841 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
3842 if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
3843 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3847 /* looking for addresses to delete from slaves' list */
3848 for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
3849 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
3850 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3854 /* save master's multicast list */
3855 bond_mc_list_destroy(bond);
3856 bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
3858 write_unlock_bh(&bond->lock);
3862 * Change the MTU of all of a master's slaves to match the master
3864 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3866 struct bonding *bond = bond_dev->priv;
3867 struct slave *slave, *stop_at;
3868 int res = 0;
3869 int i;
3871 dprintk("bond=%p, name=%s, new_mtu=%d\n", bond,
3872 (bond_dev ? bond_dev->name : "None"), new_mtu);
3874 /* Can't hold bond->lock with bh disabled here since
3875 * some base drivers panic. On the other hand we can't
3876 * hold bond->lock without bh disabled because we'll
3877 * deadlock. The only solution is to rely on the fact
3878 * that we're under rtnl_lock here, and the slaves
3879 * list won't change. This doesn't solve the problem
3880 * of setting the slave's MTU while it is
3881 * transmitting, but the assumption is that the base
3882 * driver can handle that.
3884 * TODO: figure out a way to safely iterate the slaves
3885 * list, but without holding a lock around the actual
3886 * call to the base driver.
3889 bond_for_each_slave(bond, slave, i) {
3890 dprintk("s %p s->p %p c_m %p\n", slave,
3891 slave->prev, slave->dev->change_mtu);
3892 res = dev_set_mtu(slave->dev, new_mtu);
3894 if (res) {
3895 /* If we failed to set the slave's mtu to the new value
3896 * we must abort the operation even in ACTIVE_BACKUP
3897 * mode, because if we allow the backup slaves to have
3898 * different mtu values than the active slave we'll
3899 * need to change their mtu when doing a failover. That
3900 * means changing their mtu from timer context, which
3901 * is probably not a good idea.
3903 dprintk("err %d %s\n", res, slave->dev->name);
3904 goto unwind;
3908 bond_dev->mtu = new_mtu;
3910 return 0;
3912 unwind:
3913 /* unwind from head to the slave that failed */
3914 stop_at = slave;
3915 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3916 int tmp_res;
3918 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
3919 if (tmp_res) {
3920 dprintk("unwind err %d dev %s\n", tmp_res,
3921 slave->dev->name);
3925 return res;
3929 * Change HW address
3931 * Note that many devices must be down to change the HW address, and
3932 * downing the master releases all slaves. We can make bonds full of
3933 * bonding devices to test this, however.
3935 static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
3937 struct bonding *bond = bond_dev->priv;
3938 struct sockaddr *sa = addr, tmp_sa;
3939 struct slave *slave, *stop_at;
3940 int res = 0;
3941 int i;
3943 dprintk("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
3945 if (!is_valid_ether_addr(sa->sa_data)) {
3946 return -EADDRNOTAVAIL;
3949 /* Can't hold bond->lock with bh disabled here since
3950 * some base drivers panic. On the other hand we can't
3951 * hold bond->lock without bh disabled because we'll
3952 * deadlock. The only solution is to rely on the fact
3953 * that we're under rtnl_lock here, and the slaves
3954 * list won't change. This doesn't solve the problem
3955 * of setting the slave's hw address while it is
3956 * transmitting, but the assumption is that the base
3957 * driver can handle that.
3959 * TODO: figure out a way to safely iterate the slaves
3960 * list, but without holding a lock around the actual
3961 * call to the base driver.
3964 bond_for_each_slave(bond, slave, i) {
3965 dprintk("slave %p %s\n", slave, slave->dev->name);
3967 if (slave->dev->set_mac_address == NULL) {
3968 res = -EOPNOTSUPP;
3969 dprintk("EOPNOTSUPP %s\n", slave->dev->name);
3970 goto unwind;
3973 res = dev_set_mac_address(slave->dev, addr);
3974 if (res) {
3975 /* TODO: consider downing the slave
3976 * and retry ?
3977 * User should expect communications
3978 * breakage anyway until ARP finish
3979 * updating, so...
3981 dprintk("err %d %s\n", res, slave->dev->name);
3982 goto unwind;
3986 /* success */
3987 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
3988 return 0;
3990 unwind:
3991 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
3992 tmp_sa.sa_family = bond_dev->type;
3994 /* unwind from head to the slave that failed */
3995 stop_at = slave;
3996 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3997 int tmp_res;
3999 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4000 if (tmp_res) {
4001 dprintk("unwind err %d dev %s\n", tmp_res,
4002 slave->dev->name);
4006 return res;
4009 static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4011 struct bonding *bond = bond_dev->priv;
4012 struct slave *slave, *start_at;
4013 int i;
4014 int res = 1;
4016 read_lock(&bond->lock);
4018 if (!BOND_IS_OK(bond)) {
4019 goto out;
4022 read_lock(&bond->curr_slave_lock);
4023 slave = start_at = bond->curr_active_slave;
4024 read_unlock(&bond->curr_slave_lock);
4026 if (!slave) {
4027 goto out;
4030 bond_for_each_slave_from(bond, slave, i, start_at) {
4031 if (IS_UP(slave->dev) &&
4032 (slave->link == BOND_LINK_UP) &&
4033 (slave->state == BOND_STATE_ACTIVE)) {
4034 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4036 write_lock(&bond->curr_slave_lock);
4037 bond->curr_active_slave = slave->next;
4038 write_unlock(&bond->curr_slave_lock);
4040 break;
4045 out:
4046 if (res) {
4047 /* no suitable interface, frame not sent */
4048 dev_kfree_skb(skb);
4050 read_unlock(&bond->lock);
4051 return 0;
4055 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4056 * the bond has a usable interface.
4058 static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4060 struct bonding *bond = bond_dev->priv;
4061 int res = 1;
4063 /* if we are sending arp packets, try to at least
4064 identify our own ip address */
4065 if (bond->params.arp_interval && !my_ip &&
4066 (skb->protocol == __constant_htons(ETH_P_ARP))) {
4067 char *the_ip = (char *)skb->data +
4068 sizeof(struct ethhdr) +
4069 sizeof(struct arphdr) +
4070 ETH_ALEN;
4071 memcpy(&my_ip, the_ip, 4);
4074 read_lock(&bond->lock);
4075 read_lock(&bond->curr_slave_lock);
4077 if (!BOND_IS_OK(bond)) {
4078 goto out;
4081 if (bond->curr_active_slave) { /* one usable interface */
4082 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4085 out:
4086 if (res) {
4087 /* no suitable interface, frame not sent */
4088 dev_kfree_skb(skb);
4090 read_unlock(&bond->curr_slave_lock);
4091 read_unlock(&bond->lock);
4092 return 0;
4096 * in XOR mode, we determine the output device by performing xor on
4097 * the source and destination hw adresses. If this device is not
4098 * enabled, find the next slave following this xor slave.
4100 static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4102 struct bonding *bond = bond_dev->priv;
4103 struct ethhdr *data = (struct ethhdr *)skb->data;
4104 struct slave *slave, *start_at;
4105 int slave_no;
4106 int i;
4107 int res = 1;
4109 read_lock(&bond->lock);
4111 if (!BOND_IS_OK(bond)) {
4112 goto out;
4115 slave_no = (data->h_dest[5]^bond_dev->dev_addr[5]) % bond->slave_cnt;
4117 bond_for_each_slave(bond, slave, i) {
4118 slave_no--;
4119 if (slave_no < 0) {
4120 break;
4124 start_at = slave;
4126 bond_for_each_slave_from(bond, slave, i, start_at) {
4127 if (IS_UP(slave->dev) &&
4128 (slave->link == BOND_LINK_UP) &&
4129 (slave->state == BOND_STATE_ACTIVE)) {
4130 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4131 break;
4135 out:
4136 if (res) {
4137 /* no suitable interface, frame not sent */
4138 dev_kfree_skb(skb);
4140 read_unlock(&bond->lock);
4141 return 0;
4145 * in broadcast mode, we send everything to all usable interfaces.
4147 static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4149 struct bonding *bond = bond_dev->priv;
4150 struct slave *slave, *start_at;
4151 struct net_device *tx_dev = NULL;
4152 int i;
4153 int res = 1;
4155 read_lock(&bond->lock);
4157 if (!BOND_IS_OK(bond)) {
4158 goto out;
4161 read_lock(&bond->curr_slave_lock);
4162 start_at = bond->curr_active_slave;
4163 read_unlock(&bond->curr_slave_lock);
4165 if (!start_at) {
4166 goto out;
4169 bond_for_each_slave_from(bond, slave, i, start_at) {
4170 if (IS_UP(slave->dev) &&
4171 (slave->link == BOND_LINK_UP) &&
4172 (slave->state == BOND_STATE_ACTIVE)) {
4173 if (tx_dev) {
4174 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4175 if (!skb2) {
4176 printk(KERN_ERR DRV_NAME
4177 ": Error: bond_xmit_broadcast(): "
4178 "skb_clone() failed\n");
4179 continue;
4182 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4183 if (res) {
4184 dev_kfree_skb(skb2);
4185 continue;
4188 tx_dev = slave->dev;
4192 if (tx_dev) {
4193 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4196 out:
4197 if (res) {
4198 /* no suitable interface, frame not sent */
4199 dev_kfree_skb(skb);
4201 /* frame sent to all suitable interfaces */
4202 read_unlock(&bond->lock);
4203 return 0;
4206 /*------------------------- Device initialization ---------------------------*/
4209 * set bond mode specific net device operations
4211 static inline void bond_set_mode_ops(struct net_device *bond_dev, int mode)
4213 switch (mode) {
4214 case BOND_MODE_ROUNDROBIN:
4215 bond_dev->hard_start_xmit = bond_xmit_roundrobin;
4216 break;
4217 case BOND_MODE_ACTIVEBACKUP:
4218 bond_dev->hard_start_xmit = bond_xmit_activebackup;
4219 break;
4220 case BOND_MODE_XOR:
4221 bond_dev->hard_start_xmit = bond_xmit_xor;
4222 break;
4223 case BOND_MODE_BROADCAST:
4224 bond_dev->hard_start_xmit = bond_xmit_broadcast;
4225 break;
4226 case BOND_MODE_8023AD:
4227 bond_dev->hard_start_xmit = bond_3ad_xmit_xor;
4228 break;
4229 case BOND_MODE_TLB:
4230 case BOND_MODE_ALB:
4231 bond_dev->hard_start_xmit = bond_alb_xmit;
4232 bond_dev->set_mac_address = bond_alb_set_mac_address;
4233 break;
4234 default:
4235 /* Should never happen, mode already checked */
4236 printk(KERN_ERR DRV_NAME
4237 ": Error: Unknown bonding mode %d\n",
4238 mode);
4239 break;
4244 * Does not allocate but creates a /proc entry.
4245 * Allowed to fail.
4247 static int __init bond_init(struct net_device *bond_dev, struct bond_params *params)
4249 struct bonding *bond = bond_dev->priv;
4251 dprintk("Begin bond_init for %s\n", bond_dev->name);
4253 /* initialize rwlocks */
4254 rwlock_init(&bond->lock);
4255 rwlock_init(&bond->curr_slave_lock);
4257 bond->params = *params; /* copy params struct */
4259 /* Initialize pointers */
4260 bond->first_slave = NULL;
4261 bond->curr_active_slave = NULL;
4262 bond->current_arp_slave = NULL;
4263 bond->primary_slave = NULL;
4264 bond->dev = bond_dev;
4265 INIT_LIST_HEAD(&bond->vlan_list);
4267 /* Initialize the device entry points */
4268 bond_dev->open = bond_open;
4269 bond_dev->stop = bond_close;
4270 bond_dev->get_stats = bond_get_stats;
4271 bond_dev->do_ioctl = bond_do_ioctl;
4272 bond_dev->set_multicast_list = bond_set_multicast_list;
4273 bond_dev->change_mtu = bond_change_mtu;
4274 bond_dev->set_mac_address = bond_set_mac_address;
4276 bond_set_mode_ops(bond_dev, bond->params.mode);
4278 bond_dev->destructor = free_netdev;
4280 /* Initialize the device options */
4281 bond_dev->tx_queue_len = 0;
4282 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
4284 /* At first, we block adding VLANs. That's the only way to
4285 * prevent problems that occur when adding VLANs over an
4286 * empty bond. The block will be removed once non-challenged
4287 * slaves are enslaved.
4289 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4291 /* don't acquire bond device's xmit_lock when
4292 * transmitting */
4293 bond_dev->features |= NETIF_F_LLTX;
4295 /* By default, we declare the bond to be fully
4296 * VLAN hardware accelerated capable. Special
4297 * care is taken in the various xmit functions
4298 * when there are slaves that are not hw accel
4299 * capable
4301 bond_dev->vlan_rx_register = bond_vlan_rx_register;
4302 bond_dev->vlan_rx_add_vid = bond_vlan_rx_add_vid;
4303 bond_dev->vlan_rx_kill_vid = bond_vlan_rx_kill_vid;
4304 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4305 NETIF_F_HW_VLAN_RX |
4306 NETIF_F_HW_VLAN_FILTER);
4308 #ifdef CONFIG_PROC_FS
4309 bond_create_proc_entry(bond);
4310 #endif
4312 list_add_tail(&bond->bond_list, &bond_dev_list);
4314 return 0;
4317 /* De-initialize device specific data.
4318 * Caller must hold rtnl_lock.
4320 static inline void bond_deinit(struct net_device *bond_dev)
4322 struct bonding *bond = bond_dev->priv;
4324 list_del(&bond->bond_list);
4326 #ifdef CONFIG_PROC_FS
4327 bond_remove_proc_entry(bond);
4328 #endif
4331 /* Unregister and free all bond devices.
4332 * Caller must hold rtnl_lock.
4334 static void bond_free_all(void)
4336 struct bonding *bond, *nxt;
4338 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4339 struct net_device *bond_dev = bond->dev;
4341 unregister_netdevice(bond_dev);
4342 bond_deinit(bond_dev);
4345 #ifdef CONFIG_PROC_FS
4346 bond_destroy_proc_dir();
4347 #endif
4350 /*------------------------- Module initialization ---------------------------*/
4353 * Convert string input module parms. Accept either the
4354 * number of the mode or its string name.
4356 static inline int bond_parse_parm(char *mode_arg, struct bond_parm_tbl *tbl)
4358 int i;
4360 for (i = 0; tbl[i].modename; i++) {
4361 if ((isdigit(*mode_arg) &&
4362 tbl[i].mode == simple_strtol(mode_arg, NULL, 0)) ||
4363 (strncmp(mode_arg, tbl[i].modename,
4364 strlen(tbl[i].modename)) == 0)) {
4365 return tbl[i].mode;
4369 return -1;
4372 static int bond_check_params(struct bond_params *params)
4375 * Convert string parameters.
4377 if (mode) {
4378 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4379 if (bond_mode == -1) {
4380 printk(KERN_ERR DRV_NAME
4381 ": Error: Invalid bonding mode \"%s\"\n",
4382 mode == NULL ? "NULL" : mode);
4383 return -EINVAL;
4387 if (lacp_rate) {
4388 if (bond_mode != BOND_MODE_8023AD) {
4389 printk(KERN_INFO DRV_NAME
4390 ": lacp_rate param is irrelevant in mode %s\n",
4391 bond_mode_name(bond_mode));
4392 } else {
4393 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4394 if (lacp_fast == -1) {
4395 printk(KERN_ERR DRV_NAME
4396 ": Error: Invalid lacp rate \"%s\"\n",
4397 lacp_rate == NULL ? "NULL" : lacp_rate);
4398 return -EINVAL;
4403 if (max_bonds < 1 || max_bonds > INT_MAX) {
4404 printk(KERN_WARNING DRV_NAME
4405 ": Warning: max_bonds (%d) not in range %d-%d, so it "
4406 "was reset to BOND_DEFAULT_MAX_BONDS (%d)",
4407 max_bonds, 1, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4408 max_bonds = BOND_DEFAULT_MAX_BONDS;
4411 if (miimon < 0) {
4412 printk(KERN_WARNING DRV_NAME
4413 ": Warning: miimon module parameter (%d), "
4414 "not in range 0-%d, so it was reset to %d\n",
4415 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4416 miimon = BOND_LINK_MON_INTERV;
4419 if (updelay < 0) {
4420 printk(KERN_WARNING DRV_NAME
4421 ": Warning: updelay module parameter (%d), "
4422 "not in range 0-%d, so it was reset to 0\n",
4423 updelay, INT_MAX);
4424 updelay = 0;
4427 if (downdelay < 0) {
4428 printk(KERN_WARNING DRV_NAME
4429 ": Warning: downdelay module parameter (%d), "
4430 "not in range 0-%d, so it was reset to 0\n",
4431 downdelay, INT_MAX);
4432 downdelay = 0;
4435 if ((use_carrier != 0) && (use_carrier != 1)) {
4436 printk(KERN_WARNING DRV_NAME
4437 ": Warning: use_carrier module parameter (%d), "
4438 "not of valid value (0/1), so it was set to 1\n",
4439 use_carrier);
4440 use_carrier = 1;
4443 /* reset values for 802.3ad */
4444 if (bond_mode == BOND_MODE_8023AD) {
4445 if (!miimon) {
4446 printk(KERN_WARNING DRV_NAME
4447 ": Warning: miimon must be specified, "
4448 "otherwise bonding will not detect link "
4449 "failure, speed and duplex which are "
4450 "essential for 802.3ad operation\n");
4451 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4452 miimon = 100;
4456 /* reset values for TLB/ALB */
4457 if ((bond_mode == BOND_MODE_TLB) ||
4458 (bond_mode == BOND_MODE_ALB)) {
4459 if (!miimon) {
4460 printk(KERN_WARNING DRV_NAME
4461 ": Warning: miimon must be specified, "
4462 "otherwise bonding will not detect link "
4463 "failure and link speed which are essential "
4464 "for TLB/ALB load balancing\n");
4465 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4466 miimon = 100;
4470 if (bond_mode == BOND_MODE_ALB) {
4471 printk(KERN_NOTICE DRV_NAME
4472 ": In ALB mode you might experience client "
4473 "disconnections upon reconnection of a link if the "
4474 "bonding module updelay parameter (%d msec) is "
4475 "incompatible with the forwarding delay time of the "
4476 "switch\n",
4477 updelay);
4480 if (!miimon) {
4481 if (updelay || downdelay) {
4482 /* just warn the user the up/down delay will have
4483 * no effect since miimon is zero...
4485 printk(KERN_WARNING DRV_NAME
4486 ": Warning: miimon module parameter not set "
4487 "and updelay (%d) or downdelay (%d) module "
4488 "parameter is set; updelay and downdelay have "
4489 "no effect unless miimon is set\n",
4490 updelay, downdelay);
4492 } else {
4493 /* don't allow arp monitoring */
4494 if (arp_interval) {
4495 printk(KERN_WARNING DRV_NAME
4496 ": Warning: miimon (%d) and arp_interval (%d) "
4497 "can't be used simultaneously, disabling ARP "
4498 "monitoring\n",
4499 miimon, arp_interval);
4500 arp_interval = 0;
4503 if ((updelay % miimon) != 0) {
4504 printk(KERN_WARNING DRV_NAME
4505 ": Warning: updelay (%d) is not a multiple "
4506 "of miimon (%d), updelay rounded to %d ms\n",
4507 updelay, miimon, (updelay / miimon) * miimon);
4510 updelay /= miimon;
4512 if ((downdelay % miimon) != 0) {
4513 printk(KERN_WARNING DRV_NAME
4514 ": Warning: downdelay (%d) is not a multiple "
4515 "of miimon (%d), downdelay rounded to %d ms\n",
4516 downdelay, miimon,
4517 (downdelay / miimon) * miimon);
4520 downdelay /= miimon;
4523 if (arp_interval < 0) {
4524 printk(KERN_WARNING DRV_NAME
4525 ": Warning: arp_interval module parameter (%d) "
4526 ", not in range 0-%d, so it was reset to %d\n",
4527 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4528 arp_interval = BOND_LINK_ARP_INTERV;
4531 for (arp_ip_count = 0;
4532 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4533 arp_ip_count++) {
4534 /* not complete check, but should be good enough to
4535 catch mistakes */
4536 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4537 printk(KERN_WARNING DRV_NAME
4538 ": Warning: bad arp_ip_target module parameter "
4539 "(%s), ARP monitoring will not be performed\n",
4540 arp_ip_target[arp_ip_count]);
4541 arp_interval = 0;
4542 } else {
4543 u32 ip = in_aton(arp_ip_target[arp_ip_count]);
4544 arp_target[arp_ip_count] = ip;
4548 if (arp_interval && !arp_ip_count) {
4549 /* don't allow arping if no arp_ip_target given... */
4550 printk(KERN_WARNING DRV_NAME
4551 ": Warning: arp_interval module parameter (%d) "
4552 "specified without providing an arp_ip_target "
4553 "parameter, arp_interval was reset to 0\n",
4554 arp_interval);
4555 arp_interval = 0;
4558 if (miimon) {
4559 printk(KERN_INFO DRV_NAME
4560 ": MII link monitoring set to %d ms\n",
4561 miimon);
4562 } else if (arp_interval) {
4563 int i;
4565 printk(KERN_INFO DRV_NAME
4566 ": ARP monitoring set to %d ms with %d target(s):",
4567 arp_interval, arp_ip_count);
4569 for (i = 0; i < arp_ip_count; i++)
4570 printk (" %s", arp_ip_target[i]);
4572 printk("\n");
4574 } else {
4575 /* miimon and arp_interval not set, we need one so things
4576 * work as expected, see bonding.txt for details
4578 printk(KERN_WARNING DRV_NAME
4579 ": Warning: either miimon or arp_interval and "
4580 "arp_ip_target module parameters must be specified, "
4581 "otherwise bonding will not detect link failures! see "
4582 "bonding.txt for details.\n");
4585 if (primary && !USES_PRIMARY(bond_mode)) {
4586 /* currently, using a primary only makes sense
4587 * in active backup, TLB or ALB modes
4589 printk(KERN_WARNING DRV_NAME
4590 ": Warning: %s primary device specified but has no "
4591 "effect in %s mode\n",
4592 primary, bond_mode_name(bond_mode));
4593 primary = NULL;
4596 /* fill params struct with the proper values */
4597 params->mode = bond_mode;
4598 params->miimon = miimon;
4599 params->arp_interval = arp_interval;
4600 params->updelay = updelay;
4601 params->downdelay = downdelay;
4602 params->use_carrier = use_carrier;
4603 params->lacp_fast = lacp_fast;
4604 params->primary[0] = 0;
4606 if (primary) {
4607 strncpy(params->primary, primary, IFNAMSIZ);
4608 params->primary[IFNAMSIZ - 1] = 0;
4611 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4613 return 0;
4616 static int __init bonding_init(void)
4618 struct bond_params params;
4619 int i;
4620 int res;
4622 printk(KERN_INFO "%s", version);
4624 res = bond_check_params(&params);
4625 if (res) {
4626 return res;
4629 rtnl_lock();
4631 #ifdef CONFIG_PROC_FS
4632 bond_create_proc_dir();
4633 #endif
4635 for (i = 0; i < max_bonds; i++) {
4636 struct net_device *bond_dev;
4638 bond_dev = alloc_netdev(sizeof(struct bonding), "", ether_setup);
4639 if (!bond_dev) {
4640 res = -ENOMEM;
4641 goto out_err;
4644 res = dev_alloc_name(bond_dev, "bond%d");
4645 if (res < 0) {
4646 free_netdev(bond_dev);
4647 goto out_err;
4650 /* bond_init() must be called after dev_alloc_name() (for the
4651 * /proc files), but before register_netdevice(), because we
4652 * need to set function pointers.
4654 res = bond_init(bond_dev, &params);
4655 if (res < 0) {
4656 free_netdev(bond_dev);
4657 goto out_err;
4660 SET_MODULE_OWNER(bond_dev);
4662 res = register_netdevice(bond_dev);
4663 if (res < 0) {
4664 bond_deinit(bond_dev);
4665 free_netdev(bond_dev);
4666 goto out_err;
4670 rtnl_unlock();
4671 register_netdevice_notifier(&bond_netdev_notifier);
4673 return 0;
4675 out_err:
4676 /* free and unregister all bonds that were successfully added */
4677 bond_free_all();
4679 rtnl_unlock();
4681 return res;
4684 static void __exit bonding_exit(void)
4686 unregister_netdevice_notifier(&bond_netdev_notifier);
4688 rtnl_lock();
4689 bond_free_all();
4690 rtnl_unlock();
4693 module_init(bonding_init);
4694 module_exit(bonding_exit);
4695 MODULE_LICENSE("GPL");
4696 MODULE_VERSION(DRV_VERSION);
4697 MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
4698 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
4699 MODULE_SUPPORTED_DEVICE("most ethernet devices");
4702 * Local variables:
4703 * c-indent-level: 8
4704 * c-basic-offset: 8
4705 * tab-width: 8
4706 * End: