]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/bonding/bond_main.c
[PATCH] bonding: version update
[net-next-2.6.git] / drivers / net / bonding / bond_main.c
CommitLineData
1da177e4
LT
1/*
2 * originally based on the dummy device.
3 *
4 * Copyright 1999, Thomas Davis, tadavis@lbl.gov.
5 * Licensed under the GPL. Based on dummy.c, and eql.c devices.
6 *
7 * bonding.c: an Ethernet Bonding driver
8 *
9 * This is useful to talk to a Cisco EtherChannel compatible equipment:
10 * Cisco 5500
11 * Sun Trunking (Solaris)
12 * Alteon AceDirector Trunks
13 * Linux Bonding
14 * and probably many L2 switches ...
15 *
16 * How it works:
17 * ifconfig bond0 ipaddress netmask up
18 * will setup a network device, with an ip address. No mac address
19 * will be assigned at this time. The hw mac address will come from
20 * the first slave bonded to the channel. All slaves will then use
21 * this hw mac address.
22 *
23 * ifconfig bond0 down
24 * will release all slaves, marking them as down.
25 *
26 * ifenslave bond0 eth0
27 * will attach eth0 to bond0 as a slave. eth0 hw mac address will either
28 * a: be used as initial mac address
29 * b: if a hw mac address already is there, eth0's hw mac address
30 * will then be set from bond0.
31 *
32 * v0.1 - first working version.
33 * v0.2 - changed stats to be calculated by summing slaves stats.
34 *
35 * Changes:
36 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
37 * - fix leaks on failure at bond_init
38 *
39 * 2000/09/30 - Willy Tarreau <willy at meta-x.org>
40 * - added trivial code to release a slave device.
41 * - fixed security bug (CAP_NET_ADMIN not checked)
42 * - implemented MII link monitoring to disable dead links :
43 * All MII capable slaves are checked every <miimon> milliseconds
44 * (100 ms seems good). This value can be changed by passing it to
45 * insmod. A value of zero disables the monitoring (default).
46 * - fixed an infinite loop in bond_xmit_roundrobin() when there's no
47 * good slave.
48 * - made the code hopefully SMP safe
49 *
50 * 2000/10/03 - Willy Tarreau <willy at meta-x.org>
51 * - optimized slave lists based on relevant suggestions from Thomas Davis
52 * - implemented active-backup method to obtain HA with two switches:
53 * stay as long as possible on the same active interface, while we
54 * also monitor the backup one (MII link status) because we want to know
55 * if we are able to switch at any time. ( pass "mode=1" to insmod )
56 * - lots of stress testings because we need it to be more robust than the
57 * wires ! :->
58 *
59 * 2000/10/09 - Willy Tarreau <willy at meta-x.org>
60 * - added up and down delays after link state change.
61 * - optimized the slaves chaining so that when we run forward, we never
62 * repass through the bond itself, but we can find it by searching
63 * backwards. Renders the deletion more difficult, but accelerates the
64 * scan.
65 * - smarter enslaving and releasing.
66 * - finer and more robust SMP locking
67 *
68 * 2000/10/17 - Willy Tarreau <willy at meta-x.org>
69 * - fixed two potential SMP race conditions
70 *
71 * 2000/10/18 - Willy Tarreau <willy at meta-x.org>
72 * - small fixes to the monitoring FSM in case of zero delays
73 * 2000/11/01 - Willy Tarreau <willy at meta-x.org>
74 * - fixed first slave not automatically used in trunk mode.
75 * 2000/11/10 : spelling of "EtherChannel" corrected.
76 * 2000/11/13 : fixed a race condition in case of concurrent accesses to ioctl().
77 * 2000/12/16 : fixed improper usage of rtnl_exlock_nowait().
78 *
79 * 2001/1/3 - Chad N. Tindel <ctindel at ieee dot org>
80 * - The bonding driver now simulates MII status monitoring, just like
81 * a normal network device. It will show that the link is down iff
82 * every slave in the bond shows that their links are down. If at least
83 * one slave is up, the bond's MII status will appear as up.
84 *
85 * 2001/2/7 - Chad N. Tindel <ctindel at ieee dot org>
86 * - Applications can now query the bond from user space to get
87 * information which may be useful. They do this by calling
88 * the BOND_INFO_QUERY ioctl. Once the app knows how many slaves
89 * are in the bond, it can call the BOND_SLAVE_INFO_QUERY ioctl to
90 * get slave specific information (# link failures, etc). See
91 * <linux/if_bonding.h> for more details. The structs of interest
92 * are ifbond and ifslave.
93 *
94 * 2001/4/5 - Chad N. Tindel <ctindel at ieee dot org>
95 * - Ported to 2.4 Kernel
96 *
97 * 2001/5/2 - Jeffrey E. Mast <jeff at mastfamily dot com>
98 * - When a device is detached from a bond, the slave device is no longer
99 * left thinking that is has a master.
100 *
101 * 2001/5/16 - Jeffrey E. Mast <jeff at mastfamily dot com>
102 * - memset did not appropriately initialized the bond rw_locks. Used
103 * rwlock_init to initialize to unlocked state to prevent deadlock when
104 * first attempting a lock
105 * - Called SET_MODULE_OWNER for bond device
106 *
107 * 2001/5/17 - Tim Anderson <tsa at mvista.com>
108 * - 2 paths for releasing for slave release; 1 through ioctl
109 * and 2) through close. Both paths need to release the same way.
110 * - the free slave in bond release is changing slave status before
111 * the free. The netdev_set_master() is intended to change slave state
112 * so it should not be done as part of the release process.
113 * - Simple rule for slave state at release: only the active in A/B and
114 * only one in the trunked case.
115 *
116 * 2001/6/01 - Tim Anderson <tsa at mvista.com>
117 * - Now call dev_close when releasing a slave so it doesn't screw up
118 * out routing table.
119 *
120 * 2001/6/01 - Chad N. Tindel <ctindel at ieee dot org>
121 * - Added /proc support for getting bond and slave information.
122 * Information is in /proc/net/<bond device>/info.
123 * - Changed the locking when calling bond_close to prevent deadlock.
124 *
125 * 2001/8/05 - Janice Girouard <girouard at us.ibm.com>
126 * - correct problem where refcnt of slave is not incremented in bond_ioctl
127 * so the system hangs when halting.
128 * - correct locking problem when unable to malloc in bond_enslave.
129 * - adding bond_xmit_xor logic.
130 * - adding multiple bond device support.
131 *
132 * 2001/8/13 - Erik Habbinga <erik_habbinga at hp dot com>
133 * - correct locking problem with rtnl_exlock_nowait
134 *
135 * 2001/8/23 - Janice Girouard <girouard at us.ibm.com>
136 * - bzero initial dev_bonds, to correct oops
137 * - convert SIOCDEVPRIVATE to new MII ioctl calls
138 *
139 * 2001/9/13 - Takao Indoh <indou dot takao at jp dot fujitsu dot com>
140 * - Add the BOND_CHANGE_ACTIVE ioctl implementation
141 *
142 * 2001/9/14 - Mark Huth <mhuth at mvista dot com>
143 * - Change MII_LINK_READY to not check for end of auto-negotiation,
144 * but only for an up link.
145 *
146 * 2001/9/20 - Chad N. Tindel <ctindel at ieee dot org>
147 * - Add the device field to bonding_t. Previously the net_device
148 * corresponding to a bond wasn't available from the bonding_t
149 * structure.
150 *
151 * 2001/9/25 - Janice Girouard <girouard at us.ibm.com>
152 * - add arp_monitor for active backup mode
153 *
154 * 2001/10/23 - Takao Indoh <indou dot takao at jp dot fujitsu dot com>
155 * - Various memory leak fixes
156 *
157 * 2001/11/5 - Mark Huth <mark dot huth at mvista dot com>
158 * - Don't take rtnl lock in bond_mii_monitor as it deadlocks under
159 * certain hotswap conditions.
160 * Note: this same change may be required in bond_arp_monitor ???
161 * - Remove possibility of calling bond_sethwaddr with NULL slave_dev ptr
162 * - Handle hot swap ethernet interface deregistration events to remove
163 * kernel oops following hot swap of enslaved interface
164 *
165 * 2002/1/2 - Chad N. Tindel <ctindel at ieee dot org>
166 * - Restore original slave flags at release time.
167 *
168 * 2002/02/18 - Erik Habbinga <erik_habbinga at hp dot com>
169 * - bond_release(): calling kfree on our_slave after call to
170 * bond_restore_slave_flags, not before
171 * - bond_enslave(): saving slave flags into original_flags before
172 * call to netdev_set_master, so the IFF_SLAVE flag doesn't end
173 * up in original_flags
174 *
175 * 2002/04/05 - Mark Smith <mark.smith at comdev dot cc> and
176 * Steve Mead <steve.mead at comdev dot cc>
177 * - Port Gleb Natapov's multicast support patchs from 2.4.12
178 * to 2.4.18 adding support for multicast.
179 *
180 * 2002/06/10 - Tony Cureington <tony.cureington * hp_com>
181 * - corrected uninitialized pointer (ifr.ifr_data) in bond_check_dev_link;
182 * actually changed function to use MIIPHY, then MIIREG, and finally
183 * ETHTOOL to determine the link status
184 * - fixed bad ifr_data pointer assignments in bond_ioctl
185 * - corrected mode 1 being reported as active-backup in bond_get_info;
186 * also added text to distinguish type of load balancing (rr or xor)
187 * - change arp_ip_target module param from "1-12s" (array of 12 ptrs)
188 * to "s" (a single ptr)
189 *
190 * 2002/08/30 - Jay Vosburgh <fubar at us dot ibm dot com>
191 * - Removed acquisition of xmit_lock in set_multicast_list; caused
192 * deadlock on SMP (lock is held by caller).
193 * - Revamped SIOCGMIIPHY, SIOCGMIIREG portion of bond_check_dev_link().
194 *
195 * 2002/09/18 - Jay Vosburgh <fubar at us dot ibm dot com>
196 * - Fixed up bond_check_dev_link() (and callers): removed some magic
197 * numbers, banished local MII_ defines, wrapped ioctl calls to
198 * prevent EFAULT errors
199 *
200 * 2002/9/30 - Jay Vosburgh <fubar at us dot ibm dot com>
201 * - make sure the ip target matches the arp_target before saving the
202 * hw address.
203 *
204 * 2002/9/30 - Dan Eisner <eisner at 2robots dot com>
205 * - make sure my_ip is set before taking down the link, since
206 * not all switches respond if the source ip is not set.
207 *
208 * 2002/10/8 - Janice Girouard <girouard at us dot ibm dot com>
209 * - read in the local ip address when enslaving a device
210 * - add primary support
211 * - make sure 2*arp_interval has passed when a new device
212 * is brought on-line before taking it down.
213 *
214 * 2002/09/11 - Philippe De Muyter <phdm at macqel dot be>
215 * - Added bond_xmit_broadcast logic.
216 * - Added bond_mode() support function.
217 *
218 * 2002/10/26 - Laurent Deniel <laurent.deniel at free.fr>
219 * - allow to register multicast addresses only on active slave
220 * (useful in active-backup mode)
221 * - add multicast module parameter
222 * - fix deletion of multicast groups after unloading module
223 *
224 * 2002/11/06 - Kameshwara Rayaprolu <kameshwara.rao * wipro_com>
225 * - Changes to prevent panic from closing the device twice; if we close
226 * the device in bond_release, we must set the original_flags to down
227 * so it won't be closed again by the network layer.
228 *
229 * 2002/11/07 - Tony Cureington <tony.cureington * hp_com>
230 * - Fix arp_target_hw_addr memory leak
231 * - Created activebackup_arp_monitor function to handle arp monitoring
232 * in active backup mode - the bond_arp_monitor had several problems...
233 * such as allowing slaves to tx arps sequentially without any delay
234 * for a response
235 * - Renamed bond_arp_monitor to loadbalance_arp_monitor and re-wrote
236 * this function to just handle arp monitoring in load-balancing mode;
237 * it is a lot more compact now
238 * - Changes to ensure one and only one slave transmits in active-backup
239 * mode
240 * - Robustesize parameters; warn users about bad combinations of
241 * parameters; also if miimon is specified and a network driver does
242 * not support MII or ETHTOOL, inform the user of this
243 * - Changes to support link_failure_count when in arp monitoring mode
244 * - Fix up/down delay reported in /proc
245 * - Added version; log version; make version available from "modinfo -d"
246 * - Fixed problem in bond_check_dev_link - if the first IOCTL (SIOCGMIIPH)
247 * failed, the ETHTOOL ioctl never got a chance
248 *
249 * 2002/11/16 - Laurent Deniel <laurent.deniel at free.fr>
250 * - fix multicast handling in activebackup_arp_monitor
251 * - remove one unnecessary and confusing curr_active_slave == slave test
252 * in activebackup_arp_monitor
253 *
254 * 2002/11/17 - Laurent Deniel <laurent.deniel at free.fr>
255 * - fix bond_slave_info_query when slave_id = num_slaves
256 *
257 * 2002/11/19 - Janice Girouard <girouard at us dot ibm dot com>
258 * - correct ifr_data reference. Update ifr_data reference
259 * to mii_ioctl_data struct values to avoid confusion.
260 *
261 * 2002/11/22 - Bert Barbe <bert.barbe at oracle dot com>
262 * - Add support for multiple arp_ip_target
263 *
264 * 2002/12/13 - Jay Vosburgh <fubar at us dot ibm dot com>
265 * - Changed to allow text strings for mode and multicast, e.g.,
266 * insmod bonding mode=active-backup. The numbers still work.
267 * One change: an invalid choice will cause module load failure,
268 * rather than the previous behavior of just picking one.
269 * - Minor cleanups; got rid of dup ctype stuff, atoi function
270 *
271 * 2003/02/07 - Jay Vosburgh <fubar at us dot ibm dot com>
272 * - Added use_carrier module parameter that causes miimon to
273 * use netif_carrier_ok() test instead of MII/ETHTOOL ioctls.
274 * - Minor cleanups; consolidated ioctl calls to one function.
275 *
276 * 2003/02/07 - Tony Cureington <tony.cureington * hp_com>
277 * - Fix bond_mii_monitor() logic error that could result in
278 * bonding round-robin mode ignoring links after failover/recovery
279 *
280 * 2003/03/17 - Jay Vosburgh <fubar at us dot ibm dot com>
281 * - kmalloc fix (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.
289 *
290 * 2003/03/18 - Tsippy Mendelson <tsippy.mendelson at intel dot com> and
291 * Shmulik Hen <shmulik.hen at intel dot com>
292 * - Make sure only bond_attach_slave() and bond_detach_slave() can
293 * manipulate the slave list, including slave_cnt, even when in
294 * bond_release_all().
295 * - Fixed hang in bond_release() with traffic running:
296 * netdev_set_master() must not be called from within the bond lock.
297 *
298 * 2003/03/18 - Tsippy Mendelson <tsippy.mendelson at intel dot com> and
299 * Shmulik Hen <shmulik.hen at intel dot com>
300 * - Fixed hang in bond_enslave() with traffic running:
301 * netdev_set_master() must not be called from within the bond lock.
302 *
303 * 2003/03/18 - Amir Noam <amir.noam at intel dot com>
304 * - Added support for getting slave's speed and duplex via ethtool.
305 * Needed for 802.3ad and other future modes.
306 *
307 * 2003/03/18 - Tsippy Mendelson <tsippy.mendelson at intel dot com> and
308 * Shmulik Hen <shmulik.hen at intel dot com>
309 * - Enable support of modes that need to use the unique mac address of
310 * each slave.
311 * * bond_enslave(): Moved setting the slave's mac address, and
312 * openning it, from the application to the driver. This breaks
313 * backward comaptibility with old versions of ifenslave that open
314 * the slave before enalsving it !!!.
315 * * bond_release(): The driver also takes care of closing the slave
316 * and restoring its original mac address.
317 * - Removed the code that restores all base driver's flags.
318 * Flags are automatically restored once all undo stages are done
319 * properly.
320 * - Block possibility of enslaving before the master is up. This
321 * prevents putting the system in an unstable state.
322 *
323 * 2003/03/18 - Amir Noam <amir.noam at intel dot com>,
324 * Tsippy Mendelson <tsippy.mendelson at intel dot com> and
325 * Shmulik Hen <shmulik.hen at intel dot com>
326 * - Added support for IEEE 802.3ad Dynamic link aggregation mode.
327 *
328 * 2003/05/01 - Amir Noam <amir.noam at intel dot com>
329 * - Added ABI version control to restore compatibility between
330 * new/old ifenslave and new/old bonding.
331 *
332 * 2003/05/01 - Shmulik Hen <shmulik.hen at intel dot com>
333 * - Fixed bug in bond_release_all(): save old value of 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.
342 *
343 * 2003/05/01 - Tsippy Mendelson <tsippy.mendelson at intel dot com> and
344 * Amir Noam <amir.noam at intel dot com>
345 * - Added support for lacp_rate module param.
346 * - Code beautification and style changes (mainly in comments).
347 * new version - 1.0.1
348 *
349 * 2003/05/01 - Shmulik Hen <shmulik.hen at intel dot com>
350 * - Based on discussion on mailing list, changed locking scheme
351 * to use lock/unlock or lock_bh/unlock_bh appropriately instead
352 * of lock_irqsave/unlock_irqrestore. The new scheme helps exposing
353 * hidden bugs and solves system hangs that occurred due to the fact
354 * that holding lock_irqsave doesn't prevent softirqs from running.
355 * This also increases total throughput since interrupts are not
356 * blocked on each transmitted packets or monitor timeout.
357 * new version - 2.0.0
358 *
359 * 2003/05/01 - Shmulik Hen <shmulik.hen at intel dot com>
360 * - Added support for Transmit load balancing mode.
361 * - Concentrate all assignments of 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
369 *
370 * 2003/05/01 - Tsippy Mendelson <tsippy.mendelson at intel dot com>
371 * - Added support for Adaptive load balancing mode which is
372 * equivalent to Transmit load balancing + Receive load balancing.
373 * new version - 2.2.0
374 *
375 * 2003/05/15 - Jay Vosburgh <fubar at us dot ibm dot com>
376 * - Applied fix to activebackup_arp_monitor posted to bonding-devel
377 * by Tony Cureington <tony.cureington * hp_com>. Fixes ARP
378 * monitor endless failover bug. Version to 2.2.10
379 *
380 * 2003/05/20 - Amir Noam <amir.noam at intel dot com>
381 * - Fixed bug in ABI version control - Don't commit to a specific
382 * ABI version if receiving unsupported ioctl commands.
383 *
384 * 2003/05/22 - Jay Vosburgh <fubar at us dot ibm dot com>
385 * - 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).
391 *
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.
395 *
396 * 2003/07/29 - Amir Noam <amir.noam at intel dot com>
397 * - Fixed ARP monitoring bug.
398 * Version to 2.2.15.
399 *
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.
404 *
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.
411 *
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.
423 *
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.
429 *
430 * 2003/11/20 - Amir Noam <amir.noam at intel dot com>
431 * - Fix /proc creation/destruction.
432 *
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.
455 *
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.
462 *
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.
466 *
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.
c3ade5ca
JV
478 * 2005/06/05 - Jay Vosburgh <fubar@us.ibm.com>
479 * - Support for generating gratuitous ARPs in active-backup mode.
480 * Includes support for VLAN tagging all bonding-generated ARPs
481 * as needed. Set version to 2.6.2.
169a3e66
JV
482 * 2005/06/08 - Jason Gabler <jygabler at lbl dot gov>
483 * - alternate hashing policy support for mode 2
484 * * Added kernel parameter "xmit_hash_policy" to allow the selection
485 * of different hashing policies for mode 2. The original mode 2
486 * policy is the default, now found in xmit_hash_policy_layer2().
487 * * Added xmit_hash_policy_layer34()
488 * - Modified by Jay Vosburgh <fubar@us.ibm.com> to also support mode 4.
489 * Set version to 2.6.3.
217df670
JV
490 * 2005/09/26 - Jay Vosburgh <fubar@us.ibm.com>
491 * - Removed backwards compatibility for old ifenslaves. Version 2.6.4.
1da177e4
LT
492 */
493
494//#define BONDING_DEBUG 1
495
496#include <linux/config.h>
497#include <linux/kernel.h>
498#include <linux/module.h>
499#include <linux/sched.h>
500#include <linux/types.h>
501#include <linux/fcntl.h>
502#include <linux/interrupt.h>
503#include <linux/ptrace.h>
504#include <linux/ioport.h>
505#include <linux/in.h>
169a3e66 506#include <net/ip.h>
1da177e4 507#include <linux/ip.h>
169a3e66
JV
508#include <linux/tcp.h>
509#include <linux/udp.h>
1da177e4
LT
510#include <linux/slab.h>
511#include <linux/string.h>
512#include <linux/init.h>
513#include <linux/timer.h>
514#include <linux/socket.h>
515#include <linux/ctype.h>
516#include <linux/inet.h>
517#include <linux/bitops.h>
518#include <asm/system.h>
519#include <asm/io.h>
520#include <asm/dma.h>
521#include <asm/uaccess.h>
522#include <linux/errno.h>
523#include <linux/netdevice.h>
524#include <linux/inetdevice.h>
525#include <linux/etherdevice.h>
526#include <linux/skbuff.h>
527#include <net/sock.h>
528#include <linux/rtnetlink.h>
529#include <linux/proc_fs.h>
530#include <linux/seq_file.h>
531#include <linux/smp.h>
532#include <linux/if_ether.h>
533#include <net/arp.h>
534#include <linux/mii.h>
535#include <linux/ethtool.h>
536#include <linux/if_vlan.h>
537#include <linux/if_bonding.h>
c3ade5ca 538#include <net/route.h>
1da177e4
LT
539#include "bonding.h"
540#include "bond_3ad.h"
541#include "bond_alb.h"
542
543/*---------------------------- Module parameters ----------------------------*/
544
545/* monitor all links that often (in milliseconds). <=0 disables monitoring */
546#define BOND_LINK_MON_INTERV 0
547#define BOND_LINK_ARP_INTERV 0
548
549static int max_bonds = BOND_DEFAULT_MAX_BONDS;
550static int miimon = BOND_LINK_MON_INTERV;
551static int updelay = 0;
552static int downdelay = 0;
553static int use_carrier = 1;
554static char *mode = NULL;
555static char *primary = NULL;
556static char *lacp_rate = NULL;
169a3e66 557static char *xmit_hash_policy = NULL;
1da177e4
LT
558static int arp_interval = BOND_LINK_ARP_INTERV;
559static char *arp_ip_target[BOND_MAX_ARP_TARGETS] = { NULL, };
12479f9a 560struct bond_params bonding_defaults;
1da177e4
LT
561
562module_param(max_bonds, int, 0);
563MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
564module_param(miimon, int, 0);
565MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
566module_param(updelay, int, 0);
567MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
568module_param(downdelay, int, 0);
2ac47660
MW
569MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
570 "in milliseconds");
1da177e4 571module_param(use_carrier, int, 0);
2ac47660
MW
572MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
573 "0 for off, 1 for on (default)");
1da177e4 574module_param(mode, charp, 0);
2ac47660
MW
575MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
576 "1 for active-backup, 2 for balance-xor, "
577 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
578 "6 for balance-alb");
1da177e4
LT
579module_param(primary, charp, 0);
580MODULE_PARM_DESC(primary, "Primary network device to use");
581module_param(lacp_rate, charp, 0);
2ac47660
MW
582MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
583 "(slow/fast)");
169a3e66 584module_param(xmit_hash_policy, charp, 0);
2ac47660
MW
585MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
586 ", 1 for layer 3+4");
1da177e4
LT
587module_param(arp_interval, int, 0);
588MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
589module_param_array(arp_ip_target, charp, NULL, 0);
590MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
591
592/*----------------------------- Global variables ----------------------------*/
593
594static const char *version =
595 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
596
12479f9a 597LIST_HEAD(bond_dev_list);
1da177e4
LT
598
599#ifdef CONFIG_PROC_FS
600static struct proc_dir_entry *bond_proc_dir = NULL;
601#endif
602
b76cdba9 603extern struct rw_semaphore bonding_rwsem;
1da177e4
LT
604static u32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
605static int arp_ip_count = 0;
1da177e4 606static int bond_mode = BOND_MODE_ROUNDROBIN;
169a3e66 607static int xmit_hashtype= BOND_XMIT_POLICY_LAYER2;
1da177e4 608static int lacp_fast = 0;
217df670 609
1da177e4 610
12479f9a 611struct bond_parm_tbl bond_lacp_tbl[] = {
1da177e4
LT
612{ "slow", AD_LACP_SLOW},
613{ "fast", AD_LACP_FAST},
614{ NULL, -1},
615};
616
12479f9a 617struct bond_parm_tbl bond_mode_tbl[] = {
1da177e4
LT
618{ "balance-rr", BOND_MODE_ROUNDROBIN},
619{ "active-backup", BOND_MODE_ACTIVEBACKUP},
620{ "balance-xor", BOND_MODE_XOR},
621{ "broadcast", BOND_MODE_BROADCAST},
622{ "802.3ad", BOND_MODE_8023AD},
623{ "balance-tlb", BOND_MODE_TLB},
624{ "balance-alb", BOND_MODE_ALB},
625{ NULL, -1},
626};
627
12479f9a 628struct bond_parm_tbl xmit_hashtype_tbl[] = {
169a3e66
JV
629{ "layer2", BOND_XMIT_POLICY_LAYER2},
630{ "layer3+4", BOND_XMIT_POLICY_LAYER34},
631{ NULL, -1},
632};
633
1da177e4
LT
634/*-------------------------- Forward declarations ---------------------------*/
635
c3ade5ca 636static void bond_send_gratuitous_arp(struct bonding *bond);
1da177e4
LT
637
638/*---------------------------- General routines -----------------------------*/
639
a77b5325 640const char *bond_mode_name(int mode)
1da177e4
LT
641{
642 switch (mode) {
643 case BOND_MODE_ROUNDROBIN :
644 return "load balancing (round-robin)";
645 case BOND_MODE_ACTIVEBACKUP :
646 return "fault-tolerance (active-backup)";
647 case BOND_MODE_XOR :
648 return "load balancing (xor)";
649 case BOND_MODE_BROADCAST :
650 return "fault-tolerance (broadcast)";
651 case BOND_MODE_8023AD:
652 return "IEEE 802.3ad Dynamic link aggregation";
653 case BOND_MODE_TLB:
654 return "transmit load balancing";
655 case BOND_MODE_ALB:
656 return "adaptive load balancing";
657 default:
658 return "unknown";
659 }
660}
661
662/*---------------------------------- VLAN -----------------------------------*/
663
664/**
665 * bond_add_vlan - add a new vlan id on bond
666 * @bond: bond that got the notification
667 * @vlan_id: the vlan id to add
668 *
669 * Returns -ENOMEM if allocation failed.
670 */
671static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
672{
673 struct vlan_entry *vlan;
674
675 dprintk("bond: %s, vlan id %d\n",
676 (bond ? bond->dev->name: "None"), vlan_id);
677
678 vlan = kmalloc(sizeof(struct vlan_entry), GFP_KERNEL);
679 if (!vlan) {
680 return -ENOMEM;
681 }
682
683 INIT_LIST_HEAD(&vlan->vlan_list);
684 vlan->vlan_id = vlan_id;
c3ade5ca 685 vlan->vlan_ip = 0;
1da177e4
LT
686
687 write_lock_bh(&bond->lock);
688
689 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
690
691 write_unlock_bh(&bond->lock);
692
693 dprintk("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
694
695 return 0;
696}
697
698/**
699 * bond_del_vlan - delete a vlan id from bond
700 * @bond: bond that got the notification
701 * @vlan_id: the vlan id to delete
702 *
703 * returns -ENODEV if @vlan_id was not found in @bond.
704 */
705static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
706{
707 struct vlan_entry *vlan, *next;
708 int res = -ENODEV;
709
710 dprintk("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
711
712 write_lock_bh(&bond->lock);
713
714 list_for_each_entry_safe(vlan, next, &bond->vlan_list, vlan_list) {
715 if (vlan->vlan_id == vlan_id) {
716 list_del(&vlan->vlan_list);
717
718 if ((bond->params.mode == BOND_MODE_TLB) ||
719 (bond->params.mode == BOND_MODE_ALB)) {
720 bond_alb_clear_vlan(bond, vlan_id);
721 }
722
723 dprintk("removed VLAN ID %d from bond %s\n", vlan_id,
724 bond->dev->name);
725
726 kfree(vlan);
727
728 if (list_empty(&bond->vlan_list) &&
729 (bond->slave_cnt == 0)) {
730 /* Last VLAN removed and no slaves, so
731 * restore block on adding VLANs. This will
732 * be removed once new slaves that are not
733 * VLAN challenged will be added.
734 */
735 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
736 }
737
738 res = 0;
739 goto out;
740 }
741 }
742
743 dprintk("couldn't find VLAN ID %d in bond %s\n", vlan_id,
744 bond->dev->name);
745
746out:
747 write_unlock_bh(&bond->lock);
748 return res;
749}
750
751/**
752 * bond_has_challenged_slaves
753 * @bond: the bond we're working on
754 *
755 * Searches the slave list. Returns 1 if a vlan challenged slave
756 * was found, 0 otherwise.
757 *
758 * Assumes bond->lock is held.
759 */
760static int bond_has_challenged_slaves(struct bonding *bond)
761{
762 struct slave *slave;
763 int i;
764
765 bond_for_each_slave(bond, slave, i) {
766 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
767 dprintk("found VLAN challenged slave - %s\n",
768 slave->dev->name);
769 return 1;
770 }
771 }
772
773 dprintk("no VLAN challenged slaves found\n");
774 return 0;
775}
776
777/**
778 * bond_next_vlan - safely skip to the next item in the vlans list.
779 * @bond: the bond we're working on
780 * @curr: item we're advancing from
781 *
782 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
783 * or @curr->next otherwise (even if it is @curr itself again).
784 *
785 * Caller must hold bond->lock
786 */
787struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
788{
789 struct vlan_entry *next, *last;
790
791 if (list_empty(&bond->vlan_list)) {
792 return NULL;
793 }
794
795 if (!curr) {
796 next = list_entry(bond->vlan_list.next,
797 struct vlan_entry, vlan_list);
798 } else {
799 last = list_entry(bond->vlan_list.prev,
800 struct vlan_entry, vlan_list);
801 if (last == curr) {
802 next = list_entry(bond->vlan_list.next,
803 struct vlan_entry, vlan_list);
804 } else {
805 next = list_entry(curr->vlan_list.next,
806 struct vlan_entry, vlan_list);
807 }
808 }
809
810 return next;
811}
812
813/**
814 * bond_dev_queue_xmit - Prepare skb for xmit.
815 *
816 * @bond: bond device that got this skb for tx.
817 * @skb: hw accel VLAN tagged skb to transmit
818 * @slave_dev: slave that is supposed to xmit this skbuff
819 *
820 * When the bond gets an skb to transmit that is
821 * already hardware accelerated VLAN tagged, and it
822 * needs to relay this skb to a slave that is not
823 * hw accel capable, the skb needs to be "unaccelerated",
824 * i.e. strip the hwaccel tag and re-insert it as part
825 * of the payload.
826 */
827int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
828{
829 unsigned short vlan_id;
830
831 if (!list_empty(&bond->vlan_list) &&
832 !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
833 vlan_get_tag(skb, &vlan_id) == 0) {
834 skb->dev = slave_dev;
835 skb = vlan_put_tag(skb, vlan_id);
836 if (!skb) {
837 /* vlan_put_tag() frees the skb in case of error,
838 * so return success here so the calling functions
839 * won't attempt to free is again.
840 */
841 return 0;
842 }
843 } else {
844 skb->dev = slave_dev;
845 }
846
847 skb->priority = 1;
848 dev_queue_xmit(skb);
849
850 return 0;
851}
852
853/*
854 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
855 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
856 * lock because:
857 * a. This operation is performed in IOCTL context,
858 * b. The operation is protected by the RTNL semaphore in the 8021q code,
859 * c. Holding a lock with BH disabled while directly calling a base driver
860 * entry point is generally a BAD idea.
861 *
862 * The design of synchronization/protection for this operation in the 8021q
863 * module is good for one or more VLAN devices over a single physical device
864 * and cannot be extended for a teaming solution like bonding, so there is a
865 * potential race condition here where a net device from the vlan group might
866 * be referenced (either by a base driver or the 8021q code) while it is being
867 * removed from the system. However, it turns out we're not making matters
868 * worse, and if it works for regular VLAN usage it will work here too.
869*/
870
871/**
872 * bond_vlan_rx_register - Propagates registration to slaves
873 * @bond_dev: bonding net device that got called
874 * @grp: vlan group being registered
875 */
876static void bond_vlan_rx_register(struct net_device *bond_dev, struct vlan_group *grp)
877{
878 struct bonding *bond = bond_dev->priv;
879 struct slave *slave;
880 int i;
881
882 bond->vlgrp = grp;
883
884 bond_for_each_slave(bond, slave, i) {
885 struct net_device *slave_dev = slave->dev;
886
887 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
888 slave_dev->vlan_rx_register) {
889 slave_dev->vlan_rx_register(slave_dev, grp);
890 }
891 }
892}
893
894/**
895 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
896 * @bond_dev: bonding net device that got called
897 * @vid: vlan id being added
898 */
899static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
900{
901 struct bonding *bond = bond_dev->priv;
902 struct slave *slave;
903 int i, res;
904
905 bond_for_each_slave(bond, slave, i) {
906 struct net_device *slave_dev = slave->dev;
907
908 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
909 slave_dev->vlan_rx_add_vid) {
910 slave_dev->vlan_rx_add_vid(slave_dev, vid);
911 }
912 }
913
914 res = bond_add_vlan(bond, vid);
915 if (res) {
916 printk(KERN_ERR DRV_NAME
4e0952c7 917 ": %s: Error: Failed to add vlan id %d\n",
1da177e4
LT
918 bond_dev->name, vid);
919 }
920}
921
922/**
923 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
924 * @bond_dev: bonding net device that got called
925 * @vid: vlan id being removed
926 */
927static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
928{
929 struct bonding *bond = bond_dev->priv;
930 struct slave *slave;
931 struct net_device *vlan_dev;
932 int i, res;
933
934 bond_for_each_slave(bond, slave, i) {
935 struct net_device *slave_dev = slave->dev;
936
937 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
938 slave_dev->vlan_rx_kill_vid) {
939 /* Save and then restore vlan_dev in the grp array,
940 * since the slave's driver might clear it.
941 */
942 vlan_dev = bond->vlgrp->vlan_devices[vid];
943 slave_dev->vlan_rx_kill_vid(slave_dev, vid);
944 bond->vlgrp->vlan_devices[vid] = vlan_dev;
945 }
946 }
947
948 res = bond_del_vlan(bond, vid);
949 if (res) {
950 printk(KERN_ERR DRV_NAME
4e0952c7 951 ": %s: Error: Failed to remove vlan id %d\n",
1da177e4
LT
952 bond_dev->name, vid);
953 }
954}
955
956static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
957{
958 struct vlan_entry *vlan;
959
960 write_lock_bh(&bond->lock);
961
962 if (list_empty(&bond->vlan_list)) {
963 goto out;
964 }
965
966 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
967 slave_dev->vlan_rx_register) {
968 slave_dev->vlan_rx_register(slave_dev, bond->vlgrp);
969 }
970
971 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
972 !(slave_dev->vlan_rx_add_vid)) {
973 goto out;
974 }
975
976 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
977 slave_dev->vlan_rx_add_vid(slave_dev, vlan->vlan_id);
978 }
979
980out:
981 write_unlock_bh(&bond->lock);
982}
983
984static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *slave_dev)
985{
986 struct vlan_entry *vlan;
987 struct net_device *vlan_dev;
988
989 write_lock_bh(&bond->lock);
990
991 if (list_empty(&bond->vlan_list)) {
992 goto out;
993 }
994
995 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
996 !(slave_dev->vlan_rx_kill_vid)) {
997 goto unreg;
998 }
999
1000 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
1001 /* Save and then restore vlan_dev in the grp array,
1002 * since the slave's driver might clear it.
1003 */
1004 vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
1005 slave_dev->vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
1006 bond->vlgrp->vlan_devices[vlan->vlan_id] = vlan_dev;
1007 }
1008
1009unreg:
1010 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
1011 slave_dev->vlan_rx_register) {
1012 slave_dev->vlan_rx_register(slave_dev, NULL);
1013 }
1014
1015out:
1016 write_unlock_bh(&bond->lock);
1017}
1018
1019/*------------------------------- Link status -------------------------------*/
1020
1021/*
1022 * Get link speed and duplex from the slave's base driver
1023 * using ethtool. If for some reason the call fails or the
1024 * values are invalid, fake speed and duplex to 100/Full
1025 * and return error.
1026 */
1027static int bond_update_speed_duplex(struct slave *slave)
1028{
1029 struct net_device *slave_dev = slave->dev;
1030 static int (* ioctl)(struct net_device *, struct ifreq *, int);
1031 struct ifreq ifr;
1032 struct ethtool_cmd etool;
1033
1034 /* Fake speed and duplex */
1035 slave->speed = SPEED_100;
1036 slave->duplex = DUPLEX_FULL;
1037
1038 if (slave_dev->ethtool_ops) {
1039 u32 res;
1040
1041 if (!slave_dev->ethtool_ops->get_settings) {
1042 return -1;
1043 }
1044
1045 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
1046 if (res < 0) {
1047 return -1;
1048 }
1049
1050 goto verify;
1051 }
1052
1053 ioctl = slave_dev->do_ioctl;
1054 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
1055 etool.cmd = ETHTOOL_GSET;
1056 ifr.ifr_data = (char*)&etool;
1057 if (!ioctl || (IOCTL(slave_dev, &ifr, SIOCETHTOOL) < 0)) {
1058 return -1;
1059 }
1060
1061verify:
1062 switch (etool.speed) {
1063 case SPEED_10:
1064 case SPEED_100:
1065 case SPEED_1000:
1066 break;
1067 default:
1068 return -1;
1069 }
1070
1071 switch (etool.duplex) {
1072 case DUPLEX_FULL:
1073 case DUPLEX_HALF:
1074 break;
1075 default:
1076 return -1;
1077 }
1078
1079 slave->speed = etool.speed;
1080 slave->duplex = etool.duplex;
1081
1082 return 0;
1083}
1084
1085/*
1086 * if <dev> supports MII link status reporting, check its link status.
1087 *
1088 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
1089 * depening upon the setting of the use_carrier parameter.
1090 *
1091 * Return either BMSR_LSTATUS, meaning that the link is up (or we
1092 * can't tell and just pretend it is), or 0, meaning that the link is
1093 * down.
1094 *
1095 * If reporting is non-zero, instead of faking link up, return -1 if
1096 * both ETHTOOL and MII ioctls fail (meaning the device does not
1097 * support them). If use_carrier is set, return whatever it says.
1098 * It'd be nice if there was a good way to tell if a driver supports
1099 * netif_carrier, but there really isn't.
1100 */
1101static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_dev, int reporting)
1102{
1103 static int (* ioctl)(struct net_device *, struct ifreq *, int);
1104 struct ifreq ifr;
1105 struct mii_ioctl_data *mii;
1106 struct ethtool_value etool;
1107
1108 if (bond->params.use_carrier) {
1109 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
1110 }
1111
1112 ioctl = slave_dev->do_ioctl;
1113 if (ioctl) {
1114 /* TODO: set pointer to correct ioctl on a per team member */
1115 /* bases to make this more efficient. that is, once */
1116 /* we determine the correct ioctl, we will always */
1117 /* call it and not the others for that team */
1118 /* member. */
1119
1120 /*
1121 * We cannot assume that SIOCGMIIPHY will also read a
1122 * register; not all network drivers (e.g., e100)
1123 * support that.
1124 */
1125
1126 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
1127 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
1128 mii = if_mii(&ifr);
1129 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
1130 mii->reg_num = MII_BMSR;
1131 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0) {
1132 return (mii->val_out & BMSR_LSTATUS);
1133 }
1134 }
1135 }
1136
1137 /* try SIOCETHTOOL ioctl, some drivers cache ETHTOOL_GLINK */
1138 /* for a period of time so we attempt to get link status */
1139 /* from it last if the above MII ioctls fail... */
1140 if (slave_dev->ethtool_ops) {
1141 if (slave_dev->ethtool_ops->get_link) {
1142 u32 link;
1143
1144 link = slave_dev->ethtool_ops->get_link(slave_dev);
1145
1146 return link ? BMSR_LSTATUS : 0;
1147 }
1148 }
1149
1150 if (ioctl) {
1151 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
1152 etool.cmd = ETHTOOL_GLINK;
1153 ifr.ifr_data = (char*)&etool;
1154 if (IOCTL(slave_dev, &ifr, SIOCETHTOOL) == 0) {
1155 if (etool.data == 1) {
1156 return BMSR_LSTATUS;
1157 } else {
1158 dprintk("SIOCETHTOOL shows link down\n");
1159 return 0;
1160 }
1161 }
1162 }
1163
1164 /*
1165 * If reporting, report that either there's no dev->do_ioctl,
1166 * or both SIOCGMIIREG and SIOCETHTOOL failed (meaning that we
1167 * cannot report link status). If not reporting, pretend
1168 * we're ok.
1169 */
1170 return (reporting ? -1 : BMSR_LSTATUS);
1171}
1172
1173/*----------------------------- Multicast list ------------------------------*/
1174
1175/*
1176 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
1177 */
1178static inline int bond_is_dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
1179{
1180 return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
1181 dmi1->dmi_addrlen == dmi2->dmi_addrlen;
1182}
1183
1184/*
1185 * returns dmi entry if found, NULL otherwise
1186 */
1187static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
1188{
1189 struct dev_mc_list *idmi;
1190
1191 for (idmi = mc_list; idmi; idmi = idmi->next) {
1192 if (bond_is_dmi_same(dmi, idmi)) {
1193 return idmi;
1194 }
1195 }
1196
1197 return NULL;
1198}
1199
1200/*
1201 * Push the promiscuity flag down to appropriate slaves
1202 */
1203static void bond_set_promiscuity(struct bonding *bond, int inc)
1204{
1205 if (USES_PRIMARY(bond->params.mode)) {
1206 /* write lock already acquired */
1207 if (bond->curr_active_slave) {
1208 dev_set_promiscuity(bond->curr_active_slave->dev, inc);
1209 }
1210 } else {
1211 struct slave *slave;
1212 int i;
1213 bond_for_each_slave(bond, slave, i) {
1214 dev_set_promiscuity(slave->dev, inc);
1215 }
1216 }
1217}
1218
1219/*
1220 * Push the allmulti flag down to all slaves
1221 */
1222static void bond_set_allmulti(struct bonding *bond, int inc)
1223{
1224 if (USES_PRIMARY(bond->params.mode)) {
1225 /* write lock already acquired */
1226 if (bond->curr_active_slave) {
1227 dev_set_allmulti(bond->curr_active_slave->dev, inc);
1228 }
1229 } else {
1230 struct slave *slave;
1231 int i;
1232 bond_for_each_slave(bond, slave, i) {
1233 dev_set_allmulti(slave->dev, inc);
1234 }
1235 }
1236}
1237
1238/*
1239 * Add a Multicast address to slaves
1240 * according to mode
1241 */
1242static void bond_mc_add(struct bonding *bond, void *addr, int alen)
1243{
1244 if (USES_PRIMARY(bond->params.mode)) {
1245 /* write lock already acquired */
1246 if (bond->curr_active_slave) {
1247 dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
1248 }
1249 } else {
1250 struct slave *slave;
1251 int i;
1252 bond_for_each_slave(bond, slave, i) {
1253 dev_mc_add(slave->dev, addr, alen, 0);
1254 }
1255 }
1256}
1257
1258/*
1259 * Remove a multicast address from slave
1260 * according to mode
1261 */
1262static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
1263{
1264 if (USES_PRIMARY(bond->params.mode)) {
1265 /* write lock already acquired */
1266 if (bond->curr_active_slave) {
1267 dev_mc_delete(bond->curr_active_slave->dev, addr, alen, 0);
1268 }
1269 } else {
1270 struct slave *slave;
1271 int i;
1272 bond_for_each_slave(bond, slave, i) {
1273 dev_mc_delete(slave->dev, addr, alen, 0);
1274 }
1275 }
1276}
1277
1278/*
1279 * Totally destroys the mc_list in bond
1280 */
1281static void bond_mc_list_destroy(struct bonding *bond)
1282{
1283 struct dev_mc_list *dmi;
1284
1285 dmi = bond->mc_list;
1286 while (dmi) {
1287 bond->mc_list = dmi->next;
1288 kfree(dmi);
1289 dmi = bond->mc_list;
1290 }
1291}
1292
1293/*
1294 * Copy all the Multicast addresses from src to the bonding device dst
1295 */
de54f390 1296static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
dd0fc66f 1297 gfp_t gfp_flag)
1da177e4
LT
1298{
1299 struct dev_mc_list *dmi, *new_dmi;
1300
1301 for (dmi = mc_list; dmi; dmi = dmi->next) {
de54f390 1302 new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
1da177e4
LT
1303
1304 if (!new_dmi) {
1305 /* FIXME: Potential memory leak !!! */
1306 return -ENOMEM;
1307 }
1308
1309 new_dmi->next = bond->mc_list;
1310 bond->mc_list = new_dmi;
1311 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
1312 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
1313 new_dmi->dmi_users = dmi->dmi_users;
1314 new_dmi->dmi_gusers = dmi->dmi_gusers;
1315 }
1316
1317 return 0;
1318}
1319
1320/*
1321 * flush all members of flush->mc_list from device dev->mc_list
1322 */
1323static void bond_mc_list_flush(struct net_device *bond_dev, struct net_device *slave_dev)
1324{
1325 struct bonding *bond = bond_dev->priv;
1326 struct dev_mc_list *dmi;
1327
1328 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1329 dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1330 }
1331
1332 if (bond->params.mode == BOND_MODE_8023AD) {
1333 /* del lacpdu mc addr from mc list */
1334 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1335
1336 dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1337 }
1338}
1339
1340/*--------------------------- Active slave change ---------------------------*/
1341
1342/*
1343 * Update the mc list and multicast-related flags for the new and
1344 * old active slaves (if any) according to the multicast mode, and
1345 * promiscuous flags unconditionally.
1346 */
1347static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct slave *old_active)
1348{
1349 struct dev_mc_list *dmi;
1350
1351 if (!USES_PRIMARY(bond->params.mode)) {
1352 /* nothing to do - mc list is already up-to-date on
1353 * all slaves
1354 */
1355 return;
1356 }
1357
1358 if (old_active) {
1359 if (bond->dev->flags & IFF_PROMISC) {
1360 dev_set_promiscuity(old_active->dev, -1);
1361 }
1362
1363 if (bond->dev->flags & IFF_ALLMULTI) {
1364 dev_set_allmulti(old_active->dev, -1);
1365 }
1366
1367 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
1368 dev_mc_delete(old_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1369 }
1370 }
1371
1372 if (new_active) {
1373 if (bond->dev->flags & IFF_PROMISC) {
1374 dev_set_promiscuity(new_active->dev, 1);
1375 }
1376
1377 if (bond->dev->flags & IFF_ALLMULTI) {
1378 dev_set_allmulti(new_active->dev, 1);
1379 }
1380
1381 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
1382 dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1383 }
1384 }
1385}
1386
1387/**
1388 * find_best_interface - select the best available slave to be the active one
1389 * @bond: our bonding struct
1390 *
1391 * Warning: Caller must hold curr_slave_lock for writing.
1392 */
1393static struct slave *bond_find_best_slave(struct bonding *bond)
1394{
1395 struct slave *new_active, *old_active;
1396 struct slave *bestslave = NULL;
1397 int mintime = bond->params.updelay;
1398 int i;
1399
1400 new_active = old_active = bond->curr_active_slave;
1401
1402 if (!new_active) { /* there were no active slaves left */
1403 if (bond->slave_cnt > 0) { /* found one slave */
1404 new_active = bond->first_slave;
1405 } else {
1406 return NULL; /* still no slave, return NULL */
1407 }
1408 }
1409
1410 /* first try the primary link; if arping, a link must tx/rx traffic
1411 * before it can be considered the curr_active_slave - also, we would skip
1412 * slaves between the curr_active_slave and primary_slave that may be up
1413 * and able to arp
1414 */
1415 if ((bond->primary_slave) &&
1416 (!bond->params.arp_interval) &&
1417 (IS_UP(bond->primary_slave->dev))) {
1418 new_active = bond->primary_slave;
1419 }
1420
1421 /* remember where to stop iterating over the slaves */
1422 old_active = new_active;
1423
1424 bond_for_each_slave_from(bond, new_active, i, old_active) {
1425 if (IS_UP(new_active->dev)) {
1426 if (new_active->link == BOND_LINK_UP) {
1427 return new_active;
1428 } else if (new_active->link == BOND_LINK_BACK) {
1429 /* link up, but waiting for stabilization */
1430 if (new_active->delay < mintime) {
1431 mintime = new_active->delay;
1432 bestslave = new_active;
1433 }
1434 }
1435 }
1436 }
1437
1438 return bestslave;
1439}
1440
1441/**
1442 * change_active_interface - change the active slave into the specified one
1443 * @bond: our bonding struct
1444 * @new: the new slave to make the active one
1445 *
1446 * Set the new slave to the bond's settings and unset them on the old
1447 * curr_active_slave.
1448 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1449 *
1450 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1451 * because it is apparently the best available slave we have, even though its
1452 * updelay hasn't timed out yet.
1453 *
1454 * Warning: Caller must hold curr_slave_lock for writing.
1455 */
a77b5325 1456void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
1da177e4
LT
1457{
1458 struct slave *old_active = bond->curr_active_slave;
1459
1460 if (old_active == new_active) {
1461 return;
1462 }
1463
1464 if (new_active) {
1465 if (new_active->link == BOND_LINK_BACK) {
1466 if (USES_PRIMARY(bond->params.mode)) {
1467 printk(KERN_INFO DRV_NAME
1468 ": %s: making interface %s the new "
1469 "active one %d ms earlier.\n",
1470 bond->dev->name, new_active->dev->name,
1471 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1472 }
1473
1474 new_active->delay = 0;
1475 new_active->link = BOND_LINK_UP;
1476 new_active->jiffies = jiffies;
1477
1478 if (bond->params.mode == BOND_MODE_8023AD) {
1479 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1480 }
1481
1482 if ((bond->params.mode == BOND_MODE_TLB) ||
1483 (bond->params.mode == BOND_MODE_ALB)) {
1484 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1485 }
1486 } else {
1487 if (USES_PRIMARY(bond->params.mode)) {
1488 printk(KERN_INFO DRV_NAME
1489 ": %s: making interface %s the new "
1490 "active one.\n",
1491 bond->dev->name, new_active->dev->name);
1492 }
1493 }
1494 }
1495
1da177e4
LT
1496 if (USES_PRIMARY(bond->params.mode)) {
1497 bond_mc_swap(bond, new_active, old_active);
1498 }
1499
1500 if ((bond->params.mode == BOND_MODE_TLB) ||
1501 (bond->params.mode == BOND_MODE_ALB)) {
1502 bond_alb_handle_active_change(bond, new_active);
1503 } else {
1504 bond->curr_active_slave = new_active;
1505 }
c3ade5ca
JV
1506
1507 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1508 if (old_active) {
1509 bond_set_slave_inactive_flags(old_active);
1510 }
1511
1512 if (new_active) {
1513 bond_set_slave_active_flags(new_active);
1514 }
1515 bond_send_gratuitous_arp(bond);
1516 }
1da177e4
LT
1517}
1518
1519/**
1520 * bond_select_active_slave - select a new active slave, if needed
1521 * @bond: our bonding struct
1522 *
1523 * This functions shoud be called when one of the following occurs:
1524 * - The old curr_active_slave has been released or lost its link.
1525 * - The primary_slave has got its link back.
1526 * - A slave has got its link back and there's no old curr_active_slave.
1527 *
1528 * Warning: Caller must hold curr_slave_lock for writing.
1529 */
a77b5325 1530void bond_select_active_slave(struct bonding *bond)
1da177e4
LT
1531{
1532 struct slave *best_slave;
1533
1534 best_slave = bond_find_best_slave(bond);
1535 if (best_slave != bond->curr_active_slave) {
1536 bond_change_active_slave(bond, best_slave);
1537 }
1538}
1539
1540/*--------------------------- slave list handling ---------------------------*/
1541
1542/*
1543 * This function attaches the slave to the end of list.
1544 *
1545 * bond->lock held for writing by caller.
1546 */
1547static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1548{
1549 if (bond->first_slave == NULL) { /* attaching the first slave */
1550 new_slave->next = new_slave;
1551 new_slave->prev = new_slave;
1552 bond->first_slave = new_slave;
1553 } else {
1554 new_slave->next = bond->first_slave;
1555 new_slave->prev = bond->first_slave->prev;
1556 new_slave->next->prev = new_slave;
1557 new_slave->prev->next = new_slave;
1558 }
1559
1560 bond->slave_cnt++;
1561}
1562
1563/*
1564 * This function detaches the slave from the list.
1565 * WARNING: no check is made to verify if the slave effectively
1566 * belongs to <bond>.
1567 * Nothing is freed on return, structures are just unchained.
1568 * If any slave pointer in bond was pointing to <slave>,
1569 * it should be changed by the calling function.
1570 *
1571 * bond->lock held for writing by caller.
1572 */
1573static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1574{
1575 if (slave->next) {
1576 slave->next->prev = slave->prev;
1577 }
1578
1579 if (slave->prev) {
1580 slave->prev->next = slave->next;
1581 }
1582
1583 if (bond->first_slave == slave) { /* slave is the first slave */
1584 if (bond->slave_cnt > 1) { /* there are more slave */
1585 bond->first_slave = slave->next;
1586 } else {
1587 bond->first_slave = NULL; /* slave was the last one */
1588 }
1589 }
1590
1591 slave->next = NULL;
1592 slave->prev = NULL;
1593 bond->slave_cnt--;
1594}
1595
1596/*---------------------------------- IOCTL ----------------------------------*/
1597
a77b5325 1598int bond_sethwaddr(struct net_device *bond_dev, struct net_device *slave_dev)
1da177e4
LT
1599{
1600 dprintk("bond_dev=%p\n", bond_dev);
1601 dprintk("slave_dev=%p\n", slave_dev);
1602 dprintk("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1603 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1604 return 0;
1605}
1606
8531c5ff
AK
1607#define BOND_INTERSECT_FEATURES \
1608 (NETIF_F_SG|NETIF_F_IP_CSUM|NETIF_F_NO_CSUM|NETIF_F_HW_CSUM)
1609
1610/*
8e3babcd
JV
1611 * Compute the common dev->feature set available to all slaves. Some
1612 * feature bits are managed elsewhere, so preserve feature bits set on
1613 * master device that are not part of the examined set.
8531c5ff
AK
1614 */
1615static int bond_compute_features(struct bonding *bond)
1616{
8e3babcd 1617 unsigned long features = BOND_INTERSECT_FEATURES;
8531c5ff
AK
1618 struct slave *slave;
1619 struct net_device *bond_dev = bond->dev;
8e3babcd 1620 int i;
8531c5ff 1621
8e3babcd
JV
1622 bond_for_each_slave(bond, slave, i)
1623 features &= (slave->dev->features & BOND_INTERSECT_FEATURES);
8531c5ff 1624
8531c5ff 1625 if ((features & NETIF_F_SG) &&
8e3babcd
JV
1626 !(features & (NETIF_F_IP_CSUM |
1627 NETIF_F_NO_CSUM |
1628 NETIF_F_HW_CSUM)))
8531c5ff 1629 features &= ~NETIF_F_SG;
8531c5ff 1630
8e3babcd 1631 features |= (bond_dev->features & ~BOND_INTERSECT_FEATURES);
8531c5ff
AK
1632 bond_dev->features = features;
1633
1634 return 0;
1635}
1636
1da177e4 1637/* enslave device <slave> to bond device <master> */
a77b5325 1638int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1da177e4
LT
1639{
1640 struct bonding *bond = bond_dev->priv;
1641 struct slave *new_slave = NULL;
1642 struct dev_mc_list *dmi;
1643 struct sockaddr addr;
1644 int link_reporting;
1645 int old_features = bond_dev->features;
1646 int res = 0;
1647
552709d5 1648 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1649 slave_dev->do_ioctl == NULL) {
1da177e4 1650 printk(KERN_WARNING DRV_NAME
4e0952c7
MW
1651 ": %s: Warning: no link monitoring support for %s\n",
1652 bond_dev->name, slave_dev->name);
1da177e4
LT
1653 }
1654
1655 /* bond must be initialized by bond_open() before enslaving */
1656 if (!(bond_dev->flags & IFF_UP)) {
1657 dprintk("Error, master_dev is not up\n");
1658 return -EPERM;
1659 }
1660
1661 /* already enslaved */
1662 if (slave_dev->flags & IFF_SLAVE) {
1663 dprintk("Error, Device was already enslaved\n");
1664 return -EBUSY;
1665 }
1666
1667 /* vlan challenged mutual exclusion */
1668 /* no need to lock since we're protected by rtnl_lock */
1669 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1670 dprintk("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1671 if (!list_empty(&bond->vlan_list)) {
1672 printk(KERN_ERR DRV_NAME
4e0952c7 1673 ": %s: Error: cannot enslave VLAN "
1da177e4 1674 "challenged slave %s on VLAN enabled "
4e0952c7 1675 "bond %s\n", bond_dev->name, slave_dev->name,
1da177e4
LT
1676 bond_dev->name);
1677 return -EPERM;
1678 } else {
1679 printk(KERN_WARNING DRV_NAME
4e0952c7 1680 ": %s: Warning: enslaved VLAN challenged "
1da177e4
LT
1681 "slave %s. Adding VLANs will be blocked as "
1682 "long as %s is part of bond %s\n",
4e0952c7 1683 bond_dev->name, slave_dev->name, slave_dev->name,
1da177e4
LT
1684 bond_dev->name);
1685 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1686 }
1687 } else {
1688 dprintk("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1689 if (bond->slave_cnt == 0) {
1690 /* First slave, and it is not VLAN challenged,
1691 * so remove the block of adding VLANs over the bond.
1692 */
1693 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1694 }
1695 }
1696
217df670
JV
1697 /*
1698 * Old ifenslave binaries are no longer supported. These can
1699 * be identified with moderate accurary by the state of the slave:
1700 * the current ifenslave will set the interface down prior to
1701 * enslaving it; the old ifenslave will not.
1702 */
1703 if ((slave_dev->flags & IFF_UP)) {
1704 printk(KERN_ERR DRV_NAME ": %s is up. "
1705 "This may be due to an out of date ifenslave.\n",
1706 slave_dev->name);
1707 res = -EPERM;
1708 goto err_undo_flags;
1709 }
1da177e4 1710
217df670
JV
1711 if (slave_dev->set_mac_address == NULL) {
1712 printk(KERN_ERR DRV_NAME
4e0952c7
MW
1713 ": %s: Error: The slave device you specified does "
1714 "not support setting the MAC address. "
1715 "Your kernel likely does not support slave "
1716 "devices.\n", bond_dev->name);
1717 res = -EOPNOTSUPP;
217df670 1718 goto err_undo_flags;
1da177e4
LT
1719 }
1720
1721 new_slave = kmalloc(sizeof(struct slave), GFP_KERNEL);
1722 if (!new_slave) {
1723 res = -ENOMEM;
1724 goto err_undo_flags;
1725 }
1726
1727 memset(new_slave, 0, sizeof(struct slave));
1728
1729 /* save slave's original flags before calling
1730 * netdev_set_master and dev_open
1731 */
1732 new_slave->original_flags = slave_dev->flags;
1733
217df670
JV
1734 /*
1735 * Save slave's original ("permanent") mac address for modes
1736 * that need it, and for restoring it upon release, and then
1737 * set it to the master's address
1738 */
1739 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1da177e4 1740
217df670
JV
1741 /*
1742 * Set slave to master's mac address. The application already
1743 * set the master's mac address to that of the first slave
1744 */
1745 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1746 addr.sa_family = slave_dev->type;
1747 res = dev_set_mac_address(slave_dev, &addr);
1748 if (res) {
1749 dprintk("Error %d calling set_mac_address\n", res);
1750 goto err_free;
1751 }
1da177e4 1752
217df670
JV
1753 /* open the slave since the application closed it */
1754 res = dev_open(slave_dev);
1755 if (res) {
1756 dprintk("Openning slave %s failed\n", slave_dev->name);
1757 goto err_restore_mac;
1da177e4
LT
1758 }
1759
1760 res = netdev_set_master(slave_dev, bond_dev);
1761 if (res) {
1762 dprintk("Error %d calling netdev_set_master\n", res);
217df670 1763 goto err_close;
1da177e4
LT
1764 }
1765
1766 new_slave->dev = slave_dev;
1767
1768 if ((bond->params.mode == BOND_MODE_TLB) ||
1769 (bond->params.mode == BOND_MODE_ALB)) {
1770 /* bond_alb_init_slave() must be called before all other stages since
1771 * it might fail and we do not want to have to undo everything
1772 */
1773 res = bond_alb_init_slave(bond, new_slave);
1774 if (res) {
1775 goto err_unset_master;
1776 }
1777 }
1778
1779 /* If the mode USES_PRIMARY, then the new slave gets the
1780 * master's promisc (and mc) settings only if it becomes the
1781 * curr_active_slave, and that is taken care of later when calling
1782 * bond_change_active()
1783 */
1784 if (!USES_PRIMARY(bond->params.mode)) {
1785 /* set promiscuity level to new slave */
1786 if (bond_dev->flags & IFF_PROMISC) {
1787 dev_set_promiscuity(slave_dev, 1);
1788 }
1789
1790 /* set allmulti level to new slave */
1791 if (bond_dev->flags & IFF_ALLMULTI) {
1792 dev_set_allmulti(slave_dev, 1);
1793 }
1794
1795 /* upload master's mc_list to new slave */
1796 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1797 dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1798 }
1799 }
1800
1801 if (bond->params.mode == BOND_MODE_8023AD) {
1802 /* add lacpdu mc addr to mc list */
1803 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1804
1805 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1806 }
1807
1808 bond_add_vlans_on_slave(bond, slave_dev);
1809
1810 write_lock_bh(&bond->lock);
1811
1812 bond_attach_slave(bond, new_slave);
1813
1814 new_slave->delay = 0;
1815 new_slave->link_failure_count = 0;
1816
8531c5ff
AK
1817 bond_compute_features(bond);
1818
1da177e4
LT
1819 if (bond->params.miimon && !bond->params.use_carrier) {
1820 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1821
1822 if ((link_reporting == -1) && !bond->params.arp_interval) {
1823 /*
1824 * miimon is set but a bonded network driver
1825 * does not support ETHTOOL/MII and
1826 * arp_interval is not set. Note: if
1827 * use_carrier is enabled, we will never go
1828 * here (because netif_carrier is always
1829 * supported); thus, we don't need to change
1830 * the messages for netif_carrier.
1831 */
1832 printk(KERN_WARNING DRV_NAME
4e0952c7 1833 ": %s: Warning: MII and ETHTOOL support not "
1da177e4
LT
1834 "available for interface %s, and "
1835 "arp_interval/arp_ip_target module parameters "
1836 "not specified, thus bonding will not detect "
1837 "link failures! see bonding.txt for details.\n",
4e0952c7 1838 bond_dev->name, slave_dev->name);
1da177e4
LT
1839 } else if (link_reporting == -1) {
1840 /* unable get link status using mii/ethtool */
1841 printk(KERN_WARNING DRV_NAME
4e0952c7 1842 ": %s: Warning: can't get link status from "
1da177e4
LT
1843 "interface %s; the network driver associated "
1844 "with this interface does not support MII or "
1845 "ETHTOOL link status reporting, thus miimon "
1846 "has no effect on this interface.\n",
4e0952c7 1847 bond_dev->name, slave_dev->name);
1da177e4
LT
1848 }
1849 }
1850
1851 /* check for initial state */
1852 if (!bond->params.miimon ||
1853 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1854 if (bond->params.updelay) {
1855 dprintk("Initial state of slave_dev is "
1856 "BOND_LINK_BACK\n");
1857 new_slave->link = BOND_LINK_BACK;
1858 new_slave->delay = bond->params.updelay;
1859 } else {
1860 dprintk("Initial state of slave_dev is "
1861 "BOND_LINK_UP\n");
1862 new_slave->link = BOND_LINK_UP;
1863 }
1864 new_slave->jiffies = jiffies;
1865 } else {
1866 dprintk("Initial state of slave_dev is "
1867 "BOND_LINK_DOWN\n");
1868 new_slave->link = BOND_LINK_DOWN;
1869 }
1870
1871 if (bond_update_speed_duplex(new_slave) &&
1872 (new_slave->link != BOND_LINK_DOWN)) {
1873 printk(KERN_WARNING DRV_NAME
4e0952c7 1874 ": %s: Warning: failed to get speed and duplex from %s, "
1da177e4 1875 "assumed to be 100Mb/sec and Full.\n",
4e0952c7 1876 bond_dev->name, new_slave->dev->name);
1da177e4
LT
1877
1878 if (bond->params.mode == BOND_MODE_8023AD) {
4e0952c7
MW
1879 printk(KERN_WARNING DRV_NAME
1880 ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
1da177e4 1881 "support in base driver for proper aggregator "
4e0952c7 1882 "selection.\n", bond_dev->name);
1da177e4
LT
1883 }
1884 }
1885
1886 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1887 /* if there is a primary slave, remember it */
1888 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1889 bond->primary_slave = new_slave;
1890 }
1891 }
1892
1893 switch (bond->params.mode) {
1894 case BOND_MODE_ACTIVEBACKUP:
1895 /* if we're in active-backup mode, we need one and only one active
1896 * interface. The backup interfaces will have their NOARP flag set
1897 * because we need them to be completely deaf and not to respond to
1898 * any ARP request on the network to avoid fooling a switch. Thus,
1899 * since we guarantee that curr_active_slave always point to the last
1900 * usable interface, we just have to verify this interface's flag.
1901 */
1902 if (((!bond->curr_active_slave) ||
1903 (bond->curr_active_slave->dev->flags & IFF_NOARP)) &&
1904 (new_slave->link != BOND_LINK_DOWN)) {
1905 dprintk("This is the first active slave\n");
1906 /* first slave or no active slave yet, and this link
1907 is OK, so make this interface the active one */
1908 bond_change_active_slave(bond, new_slave);
1909 } else {
1910 dprintk("This is just a backup slave\n");
1911 bond_set_slave_inactive_flags(new_slave);
1912 }
1913 break;
1914 case BOND_MODE_8023AD:
1915 /* in 802.3ad mode, the internal mechanism
1916 * will activate the slaves in the selected
1917 * aggregator
1918 */
1919 bond_set_slave_inactive_flags(new_slave);
1920 /* if this is the first slave */
1921 if (bond->slave_cnt == 1) {
1922 SLAVE_AD_INFO(new_slave).id = 1;
1923 /* Initialize AD with the number of times that the AD timer is called in 1 second
1924 * can be called only after the mac address of the bond is set
1925 */
1926 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1927 bond->params.lacp_fast);
1928 } else {
1929 SLAVE_AD_INFO(new_slave).id =
1930 SLAVE_AD_INFO(new_slave->prev).id + 1;
1931 }
1932
1933 bond_3ad_bind_slave(new_slave);
1934 break;
1935 case BOND_MODE_TLB:
1936 case BOND_MODE_ALB:
1937 new_slave->state = BOND_STATE_ACTIVE;
1938 if ((!bond->curr_active_slave) &&
1939 (new_slave->link != BOND_LINK_DOWN)) {
1940 /* first slave or no active slave yet, and this link
1941 * is OK, so make this interface the active one
1942 */
1943 bond_change_active_slave(bond, new_slave);
1944 }
1945 break;
1946 default:
1947 dprintk("This slave is always active in trunk mode\n");
1948
1949 /* always active in trunk mode */
1950 new_slave->state = BOND_STATE_ACTIVE;
1951
1952 /* In trunking mode there is little meaning to curr_active_slave
1953 * anyway (it holds no special properties of the bond device),
1954 * so we can change it without calling change_active_interface()
1955 */
1956 if (!bond->curr_active_slave) {
1957 bond->curr_active_slave = new_slave;
1958 }
1959 break;
1960 } /* switch(bond_mode) */
1961
1962 write_unlock_bh(&bond->lock);
1963
b76cdba9
MW
1964 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1965 if (res)
1966 goto err_unset_master;
1967
1da177e4
LT
1968 printk(KERN_INFO DRV_NAME
1969 ": %s: enslaving %s as a%s interface with a%s link.\n",
1970 bond_dev->name, slave_dev->name,
1971 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1972 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1973
1974 /* enslave is successful */
1975 return 0;
1976
1977/* Undo stages on error */
1978err_unset_master:
1979 netdev_set_master(slave_dev, NULL);
1980
1981err_close:
1982 dev_close(slave_dev);
1983
1984err_restore_mac:
1985 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1986 addr.sa_family = slave_dev->type;
1987 dev_set_mac_address(slave_dev, &addr);
1988
1989err_free:
1990 kfree(new_slave);
1991
1992err_undo_flags:
1993 bond_dev->features = old_features;
8531c5ff 1994
1da177e4
LT
1995 return res;
1996}
1997
1998/*
1999 * Try to release the slave device <slave> from the bond device <master>
2000 * It is legal to access curr_active_slave without a lock because all the function
2001 * is write-locked.
2002 *
2003 * The rules for slave state should be:
2004 * for Active/Backup:
2005 * Active stays on all backups go down
2006 * for Bonded connections:
2007 * The first up interface should be left on and all others downed.
2008 */
a77b5325 2009int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
1da177e4
LT
2010{
2011 struct bonding *bond = bond_dev->priv;
2012 struct slave *slave, *oldcurrent;
2013 struct sockaddr addr;
2014 int mac_addr_differ;
2015
2016 /* slave is not a slave or master is not master of this slave */
2017 if (!(slave_dev->flags & IFF_SLAVE) ||
2018 (slave_dev->master != bond_dev)) {
2019 printk(KERN_ERR DRV_NAME
4e0952c7 2020 ": %s: Error: cannot release %s.\n",
1da177e4
LT
2021 bond_dev->name, slave_dev->name);
2022 return -EINVAL;
2023 }
2024
2025 write_lock_bh(&bond->lock);
2026
2027 slave = bond_get_slave_by_dev(bond, slave_dev);
2028 if (!slave) {
2029 /* not a slave of this bond */
2030 printk(KERN_INFO DRV_NAME
2031 ": %s: %s not enslaved\n",
2032 bond_dev->name, slave_dev->name);
2033 return -EINVAL;
2034 }
2035
2036 mac_addr_differ = memcmp(bond_dev->dev_addr,
2037 slave->perm_hwaddr,
2038 ETH_ALEN);
2039 if (!mac_addr_differ && (bond->slave_cnt > 1)) {
2040 printk(KERN_WARNING DRV_NAME
4e0952c7 2041 ": %s: Warning: the permanent HWaddr of %s "
1da177e4
LT
2042 "- %02X:%02X:%02X:%02X:%02X:%02X - is "
2043 "still in use by %s. Set the HWaddr of "
2044 "%s to a different address to avoid "
2045 "conflicts.\n",
4e0952c7 2046 bond_dev->name,
1da177e4
LT
2047 slave_dev->name,
2048 slave->perm_hwaddr[0],
2049 slave->perm_hwaddr[1],
2050 slave->perm_hwaddr[2],
2051 slave->perm_hwaddr[3],
2052 slave->perm_hwaddr[4],
2053 slave->perm_hwaddr[5],
2054 bond_dev->name,
2055 slave_dev->name);
2056 }
2057
2058 /* Inform AD package of unbinding of slave. */
2059 if (bond->params.mode == BOND_MODE_8023AD) {
2060 /* must be called before the slave is
2061 * detached from the list
2062 */
2063 bond_3ad_unbind_slave(slave);
2064 }
2065
2066 printk(KERN_INFO DRV_NAME
2067 ": %s: releasing %s interface %s\n",
2068 bond_dev->name,
2069 (slave->state == BOND_STATE_ACTIVE)
2070 ? "active" : "backup",
2071 slave_dev->name);
2072
2073 oldcurrent = bond->curr_active_slave;
2074
2075 bond->current_arp_slave = NULL;
2076
2077 /* release the slave from its bond */
2078 bond_detach_slave(bond, slave);
2079
8531c5ff
AK
2080 bond_compute_features(bond);
2081
1da177e4
LT
2082 if (bond->primary_slave == slave) {
2083 bond->primary_slave = NULL;
2084 }
2085
2086 if (oldcurrent == slave) {
2087 bond_change_active_slave(bond, NULL);
2088 }
2089
2090 if ((bond->params.mode == BOND_MODE_TLB) ||
2091 (bond->params.mode == BOND_MODE_ALB)) {
2092 /* Must be called only after the slave has been
2093 * detached from the list and the curr_active_slave
2094 * has been cleared (if our_slave == old_current),
2095 * but before a new active slave is selected.
2096 */
2097 bond_alb_deinit_slave(bond, slave);
2098 }
2099
2100 if (oldcurrent == slave) {
2101 bond_select_active_slave(bond);
2102
2103 if (!bond->curr_active_slave) {
2104 printk(KERN_INFO DRV_NAME
2105 ": %s: now running without any active "
2106 "interface !\n",
2107 bond_dev->name);
2108 }
2109 }
2110
2111 if (bond->slave_cnt == 0) {
2112 /* if the last slave was removed, zero the mac address
2113 * of the master so it will be set by the application
2114 * to the mac address of the first slave
2115 */
2116 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2117
2118 if (list_empty(&bond->vlan_list)) {
2119 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2120 } else {
2121 printk(KERN_WARNING DRV_NAME
4e0952c7 2122 ": %s: Warning: clearing HW address of %s while it "
1da177e4 2123 "still has VLANs.\n",
4e0952c7 2124 bond_dev->name, bond_dev->name);
1da177e4 2125 printk(KERN_WARNING DRV_NAME
4e0952c7
MW
2126 ": %s: When re-adding slaves, make sure the bond's "
2127 "HW address matches its VLANs'.\n",
2128 bond_dev->name);
1da177e4
LT
2129 }
2130 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2131 !bond_has_challenged_slaves(bond)) {
2132 printk(KERN_INFO DRV_NAME
4e0952c7 2133 ": %s: last VLAN challenged slave %s "
1da177e4 2134 "left bond %s. VLAN blocking is removed\n",
4e0952c7 2135 bond_dev->name, slave_dev->name, bond_dev->name);
1da177e4
LT
2136 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
2137 }
2138
2139 write_unlock_bh(&bond->lock);
2140
b76cdba9
MW
2141 /* must do this from outside any spinlocks */
2142 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2143
1da177e4
LT
2144 bond_del_vlans_from_slave(bond, slave_dev);
2145
2146 /* If the mode USES_PRIMARY, then we should only remove its
2147 * promisc and mc settings if it was the curr_active_slave, but that was
2148 * already taken care of above when we detached the slave
2149 */
2150 if (!USES_PRIMARY(bond->params.mode)) {
2151 /* unset promiscuity level from slave */
2152 if (bond_dev->flags & IFF_PROMISC) {
2153 dev_set_promiscuity(slave_dev, -1);
2154 }
2155
2156 /* unset allmulti level from slave */
2157 if (bond_dev->flags & IFF_ALLMULTI) {
2158 dev_set_allmulti(slave_dev, -1);
2159 }
2160
2161 /* flush master's mc_list from slave */
2162 bond_mc_list_flush(bond_dev, slave_dev);
2163 }
2164
2165 netdev_set_master(slave_dev, NULL);
2166
2167 /* close slave before restoring its mac address */
2168 dev_close(slave_dev);
2169
217df670
JV
2170 /* restore original ("permanent") mac address */
2171 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2172 addr.sa_family = slave_dev->type;
2173 dev_set_mac_address(slave_dev, &addr);
1da177e4
LT
2174
2175 /* restore the original state of the
2176 * IFF_NOARP flag that might have been
2177 * set by bond_set_slave_inactive_flags()
2178 */
2179 if ((slave->original_flags & IFF_NOARP) == 0) {
2180 slave_dev->flags &= ~IFF_NOARP;
2181 }
2182
2183 kfree(slave);
2184
2185 return 0; /* deletion OK */
2186}
2187
2188/*
2189 * This function releases all slaves.
2190 */
2191static int bond_release_all(struct net_device *bond_dev)
2192{
2193 struct bonding *bond = bond_dev->priv;
2194 struct slave *slave;
2195 struct net_device *slave_dev;
2196 struct sockaddr addr;
2197
2198 write_lock_bh(&bond->lock);
2199
2200 if (bond->slave_cnt == 0) {
2201 goto out;
2202 }
2203
2204 bond->current_arp_slave = NULL;
2205 bond->primary_slave = NULL;
2206 bond_change_active_slave(bond, NULL);
2207
2208 while ((slave = bond->first_slave) != NULL) {
2209 /* Inform AD package of unbinding of slave
2210 * before slave is detached from the list.
2211 */
2212 if (bond->params.mode == BOND_MODE_8023AD) {
2213 bond_3ad_unbind_slave(slave);
2214 }
2215
2216 slave_dev = slave->dev;
2217 bond_detach_slave(bond, slave);
2218
2219 if ((bond->params.mode == BOND_MODE_TLB) ||
2220 (bond->params.mode == BOND_MODE_ALB)) {
2221 /* must be called only after the slave
2222 * has been detached from the list
2223 */
2224 bond_alb_deinit_slave(bond, slave);
2225 }
2226
8531c5ff
AK
2227 bond_compute_features(bond);
2228
1da177e4
LT
2229 /* now that the slave is detached, unlock and perform
2230 * all the undo steps that should not be called from
2231 * within a lock.
2232 */
2233 write_unlock_bh(&bond->lock);
2234
b76cdba9 2235 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1da177e4
LT
2236 bond_del_vlans_from_slave(bond, slave_dev);
2237
2238 /* If the mode USES_PRIMARY, then we should only remove its
2239 * promisc and mc settings if it was the curr_active_slave, but that was
2240 * already taken care of above when we detached the slave
2241 */
2242 if (!USES_PRIMARY(bond->params.mode)) {
2243 /* unset promiscuity level from slave */
2244 if (bond_dev->flags & IFF_PROMISC) {
2245 dev_set_promiscuity(slave_dev, -1);
2246 }
2247
2248 /* unset allmulti level from slave */
2249 if (bond_dev->flags & IFF_ALLMULTI) {
2250 dev_set_allmulti(slave_dev, -1);
2251 }
2252
2253 /* flush master's mc_list from slave */
2254 bond_mc_list_flush(bond_dev, slave_dev);
2255 }
2256
2257 netdev_set_master(slave_dev, NULL);
2258
2259 /* close slave before restoring its mac address */
2260 dev_close(slave_dev);
2261
217df670
JV
2262 /* restore original ("permanent") mac address*/
2263 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2264 addr.sa_family = slave_dev->type;
2265 dev_set_mac_address(slave_dev, &addr);
1da177e4
LT
2266
2267 /* restore the original state of the IFF_NOARP flag that might have
2268 * been set by bond_set_slave_inactive_flags()
2269 */
2270 if ((slave->original_flags & IFF_NOARP) == 0) {
2271 slave_dev->flags &= ~IFF_NOARP;
2272 }
2273
2274 kfree(slave);
2275
2276 /* re-acquire the lock before getting the next slave */
2277 write_lock_bh(&bond->lock);
2278 }
2279
2280 /* zero the mac address of the master so it will be
2281 * set by the application to the mac address of the
2282 * first slave
2283 */
2284 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2285
2286 if (list_empty(&bond->vlan_list)) {
2287 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2288 } else {
2289 printk(KERN_WARNING DRV_NAME
4e0952c7 2290 ": %s: Warning: clearing HW address of %s while it "
1da177e4 2291 "still has VLANs.\n",
4e0952c7 2292 bond_dev->name, bond_dev->name);
1da177e4 2293 printk(KERN_WARNING DRV_NAME
4e0952c7
MW
2294 ": %s: When re-adding slaves, make sure the bond's "
2295 "HW address matches its VLANs'.\n",
2296 bond_dev->name);
1da177e4
LT
2297 }
2298
2299 printk(KERN_INFO DRV_NAME
2300 ": %s: released all slaves\n",
2301 bond_dev->name);
2302
2303out:
2304 write_unlock_bh(&bond->lock);
2305
2306 return 0;
2307}
2308
2309/*
2310 * This function changes the active slave to slave <slave_dev>.
2311 * It returns -EINVAL in the following cases.
2312 * - <slave_dev> is not found in the list.
2313 * - There is not active slave now.
2314 * - <slave_dev> is already active.
2315 * - The link state of <slave_dev> is not BOND_LINK_UP.
2316 * - <slave_dev> is not running.
2317 * In these cases, this fuction does nothing.
2318 * In the other cases, currnt_slave pointer is changed and 0 is returned.
2319 */
2320static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2321{
2322 struct bonding *bond = bond_dev->priv;
2323 struct slave *old_active = NULL;
2324 struct slave *new_active = NULL;
2325 int res = 0;
2326
2327 if (!USES_PRIMARY(bond->params.mode)) {
2328 return -EINVAL;
2329 }
2330
2331 /* Verify that master_dev is indeed the master of slave_dev */
2332 if (!(slave_dev->flags & IFF_SLAVE) ||
2333 (slave_dev->master != bond_dev)) {
2334 return -EINVAL;
2335 }
2336
2337 write_lock_bh(&bond->lock);
2338
2339 old_active = bond->curr_active_slave;
2340 new_active = bond_get_slave_by_dev(bond, slave_dev);
2341
2342 /*
2343 * Changing to the current active: do nothing; return success.
2344 */
2345 if (new_active && (new_active == old_active)) {
2346 write_unlock_bh(&bond->lock);
2347 return 0;
2348 }
2349
2350 if ((new_active) &&
2351 (old_active) &&
2352 (new_active->link == BOND_LINK_UP) &&
2353 IS_UP(new_active->dev)) {
2354 bond_change_active_slave(bond, new_active);
2355 } else {
2356 res = -EINVAL;
2357 }
2358
2359 write_unlock_bh(&bond->lock);
2360
2361 return res;
2362}
2363
1da177e4
LT
2364static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2365{
2366 struct bonding *bond = bond_dev->priv;
2367
2368 info->bond_mode = bond->params.mode;
2369 info->miimon = bond->params.miimon;
2370
2371 read_lock_bh(&bond->lock);
2372 info->num_slaves = bond->slave_cnt;
2373 read_unlock_bh(&bond->lock);
2374
2375 return 0;
2376}
2377
2378static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2379{
2380 struct bonding *bond = bond_dev->priv;
2381 struct slave *slave;
2382 int i, found = 0;
2383
2384 if (info->slave_id < 0) {
2385 return -ENODEV;
2386 }
2387
2388 read_lock_bh(&bond->lock);
2389
2390 bond_for_each_slave(bond, slave, i) {
2391 if (i == (int)info->slave_id) {
2392 found = 1;
2393 break;
2394 }
2395 }
2396
2397 read_unlock_bh(&bond->lock);
2398
2399 if (found) {
2400 strcpy(info->slave_name, slave->dev->name);
2401 info->link = slave->link;
2402 info->state = slave->state;
2403 info->link_failure_count = slave->link_failure_count;
2404 } else {
2405 return -ENODEV;
2406 }
2407
2408 return 0;
2409}
2410
2411/*-------------------------------- Monitoring -------------------------------*/
2412
2413/* this function is called regularly to monitor each slave's link. */
a77b5325 2414void bond_mii_monitor(struct net_device *bond_dev)
1da177e4
LT
2415{
2416 struct bonding *bond = bond_dev->priv;
2417 struct slave *slave, *oldcurrent;
2418 int do_failover = 0;
2419 int delta_in_ticks;
2420 int i;
2421
2422 read_lock(&bond->lock);
2423
2424 delta_in_ticks = (bond->params.miimon * HZ) / 1000;
2425
2426 if (bond->kill_timers) {
2427 goto out;
2428 }
2429
2430 if (bond->slave_cnt == 0) {
2431 goto re_arm;
2432 }
2433
2434 /* we will try to read the link status of each of our slaves, and
2435 * set their IFF_RUNNING flag appropriately. For each slave not
2436 * supporting MII status, we won't do anything so that a user-space
2437 * program could monitor the link itself if needed.
2438 */
2439
2440 read_lock(&bond->curr_slave_lock);
2441 oldcurrent = bond->curr_active_slave;
2442 read_unlock(&bond->curr_slave_lock);
2443
2444 bond_for_each_slave(bond, slave, i) {
2445 struct net_device *slave_dev = slave->dev;
2446 int link_state;
2447 u16 old_speed = slave->speed;
2448 u8 old_duplex = slave->duplex;
2449
2450 link_state = bond_check_dev_link(bond, slave_dev, 0);
2451
2452 switch (slave->link) {
2453 case BOND_LINK_UP: /* the link was up */
2454 if (link_state == BMSR_LSTATUS) {
2455 /* link stays up, nothing more to do */
2456 break;
2457 } else { /* link going down */
2458 slave->link = BOND_LINK_FAIL;
2459 slave->delay = bond->params.downdelay;
2460
2461 if (slave->link_failure_count < UINT_MAX) {
2462 slave->link_failure_count++;
2463 }
2464
2465 if (bond->params.downdelay) {
2466 printk(KERN_INFO DRV_NAME
2467 ": %s: link status down for %s "
2468 "interface %s, disabling it in "
2469 "%d ms.\n",
2470 bond_dev->name,
2471 IS_UP(slave_dev)
2472 ? ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
2473 ? ((slave == oldcurrent)
2474 ? "active " : "backup ")
2475 : "")
2476 : "idle ",
2477 slave_dev->name,
2478 bond->params.downdelay * bond->params.miimon);
2479 }
2480 }
2481 /* no break ! fall through the BOND_LINK_FAIL test to
2482 ensure proper action to be taken
2483 */
2484 case BOND_LINK_FAIL: /* the link has just gone down */
2485 if (link_state != BMSR_LSTATUS) {
2486 /* link stays down */
2487 if (slave->delay <= 0) {
2488 /* link down for too long time */
2489 slave->link = BOND_LINK_DOWN;
2490
2491 /* in active/backup mode, we must
2492 * completely disable this interface
2493 */
2494 if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP) ||
2495 (bond->params.mode == BOND_MODE_8023AD)) {
2496 bond_set_slave_inactive_flags(slave);
2497 }
2498
2499 printk(KERN_INFO DRV_NAME
2500 ": %s: link status definitely "
2501 "down for interface %s, "
2502 "disabling it\n",
2503 bond_dev->name,
2504 slave_dev->name);
2505
2506 /* notify ad that the link status has changed */
2507 if (bond->params.mode == BOND_MODE_8023AD) {
2508 bond_3ad_handle_link_change(slave, BOND_LINK_DOWN);
2509 }
2510
2511 if ((bond->params.mode == BOND_MODE_TLB) ||
2512 (bond->params.mode == BOND_MODE_ALB)) {
2513 bond_alb_handle_link_change(bond, slave, BOND_LINK_DOWN);
2514 }
2515
2516 if (slave == oldcurrent) {
2517 do_failover = 1;
2518 }
2519 } else {
2520 slave->delay--;
2521 }
2522 } else {
2523 /* link up again */
2524 slave->link = BOND_LINK_UP;
2525 slave->jiffies = jiffies;
2526 printk(KERN_INFO DRV_NAME
2527 ": %s: link status up again after %d "
2528 "ms for interface %s.\n",
2529 bond_dev->name,
2530 (bond->params.downdelay - slave->delay) * bond->params.miimon,
2531 slave_dev->name);
2532 }
2533 break;
2534 case BOND_LINK_DOWN: /* the link was down */
2535 if (link_state != BMSR_LSTATUS) {
2536 /* the link stays down, nothing more to do */
2537 break;
2538 } else { /* link going up */
2539 slave->link = BOND_LINK_BACK;
2540 slave->delay = bond->params.updelay;
2541
2542 if (bond->params.updelay) {
2543 /* if updelay == 0, no need to
2544 advertise about a 0 ms delay */
2545 printk(KERN_INFO DRV_NAME
2546 ": %s: link status up for "
2547 "interface %s, enabling it "
2548 "in %d ms.\n",
2549 bond_dev->name,
2550 slave_dev->name,
2551 bond->params.updelay * bond->params.miimon);
2552 }
2553 }
2554 /* no break ! fall through the BOND_LINK_BACK state in
2555 case there's something to do.
2556 */
2557 case BOND_LINK_BACK: /* the link has just come back */
2558 if (link_state != BMSR_LSTATUS) {
2559 /* link down again */
2560 slave->link = BOND_LINK_DOWN;
2561
2562 printk(KERN_INFO DRV_NAME
2563 ": %s: link status down again after %d "
2564 "ms for interface %s.\n",
2565 bond_dev->name,
2566 (bond->params.updelay - slave->delay) * bond->params.miimon,
2567 slave_dev->name);
2568 } else {
2569 /* link stays up */
2570 if (slave->delay == 0) {
2571 /* now the link has been up for long time enough */
2572 slave->link = BOND_LINK_UP;
2573 slave->jiffies = jiffies;
2574
2575 if (bond->params.mode == BOND_MODE_8023AD) {
2576 /* prevent it from being the active one */
2577 slave->state = BOND_STATE_BACKUP;
2578 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2579 /* make it immediately active */
2580 slave->state = BOND_STATE_ACTIVE;
2581 } else if (slave != bond->primary_slave) {
2582 /* prevent it from being the active one */
2583 slave->state = BOND_STATE_BACKUP;
2584 }
2585
2586 printk(KERN_INFO DRV_NAME
2587 ": %s: link status definitely "
2588 "up for interface %s.\n",
2589 bond_dev->name,
2590 slave_dev->name);
2591
2592 /* notify ad that the link status has changed */
2593 if (bond->params.mode == BOND_MODE_8023AD) {
2594 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2595 }
2596
2597 if ((bond->params.mode == BOND_MODE_TLB) ||
2598 (bond->params.mode == BOND_MODE_ALB)) {
2599 bond_alb_handle_link_change(bond, slave, BOND_LINK_UP);
2600 }
2601
2602 if ((!oldcurrent) ||
2603 (slave == bond->primary_slave)) {
2604 do_failover = 1;
2605 }
2606 } else {
2607 slave->delay--;
2608 }
2609 }
2610 break;
2611 default:
2612 /* Should not happen */
4e0952c7
MW
2613 printk(KERN_ERR DRV_NAME
2614 ": %s: Error: %s Illegal value (link=%d)\n",
2615 bond_dev->name,
2616 slave->dev->name,
2617 slave->link);
1da177e4
LT
2618 goto out;
2619 } /* end of switch (slave->link) */
2620
2621 bond_update_speed_duplex(slave);
2622
2623 if (bond->params.mode == BOND_MODE_8023AD) {
2624 if (old_speed != slave->speed) {
2625 bond_3ad_adapter_speed_changed(slave);
2626 }
2627
2628 if (old_duplex != slave->duplex) {
2629 bond_3ad_adapter_duplex_changed(slave);
2630 }
2631 }
2632
2633 } /* end of for */
2634
2635 if (do_failover) {
2636 write_lock(&bond->curr_slave_lock);
2637
2638 bond_select_active_slave(bond);
2639
2640 if (oldcurrent && !bond->curr_active_slave) {
2641 printk(KERN_INFO DRV_NAME
2642 ": %s: now running without any active "
2643 "interface !\n",
2644 bond_dev->name);
2645 }
2646
2647 write_unlock(&bond->curr_slave_lock);
2648 }
2649
2650re_arm:
2651 if (bond->params.miimon) {
2652 mod_timer(&bond->mii_timer, jiffies + delta_in_ticks);
2653 }
2654out:
2655 read_unlock(&bond->lock);
2656}
2657
c3ade5ca
JV
2658
2659static u32 bond_glean_dev_ip(struct net_device *dev)
2660{
2661 struct in_device *idev;
2662 struct in_ifaddr *ifa;
2663 u32 addr = 0;
2664
2665 if (!dev)
2666 return 0;
2667
2668 rcu_read_lock();
e5ed6399 2669 idev = __in_dev_get_rcu(dev);
c3ade5ca
JV
2670 if (!idev)
2671 goto out;
2672
2673 ifa = idev->ifa_list;
2674 if (!ifa)
2675 goto out;
2676
2677 addr = ifa->ifa_local;
2678out:
2679 rcu_read_unlock();
2680 return addr;
2681}
2682
2683static int bond_has_ip(struct bonding *bond)
2684{
2685 struct vlan_entry *vlan, *vlan_next;
2686
2687 if (bond->master_ip)
2688 return 1;
2689
2690 if (list_empty(&bond->vlan_list))
2691 return 0;
2692
2693 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2694 vlan_list) {
2695 if (vlan->vlan_ip)
2696 return 1;
2697 }
2698
2699 return 0;
2700}
2701
2702/*
2703 * We go to the (large) trouble of VLAN tagging ARP frames because
2704 * switches in VLAN mode (especially if ports are configured as
2705 * "native" to a VLAN) might not pass non-tagged frames.
2706 */
2707static void bond_arp_send(struct net_device *slave_dev, int arp_op, u32 dest_ip, u32 src_ip, unsigned short vlan_id)
2708{
2709 struct sk_buff *skb;
2710
2711 dprintk("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2712 slave_dev->name, dest_ip, src_ip, vlan_id);
2713
2714 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2715 NULL, slave_dev->dev_addr, NULL);
2716
2717 if (!skb) {
2718 printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
2719 return;
2720 }
2721 if (vlan_id) {
2722 skb = vlan_put_tag(skb, vlan_id);
2723 if (!skb) {
2724 printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
2725 return;
2726 }
2727 }
2728 arp_xmit(skb);
2729}
2730
2731
1da177e4
LT
2732static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2733{
c3ade5ca 2734 int i, vlan_id, rv;
1da177e4 2735 u32 *targets = bond->params.arp_targets;
c3ade5ca
JV
2736 struct vlan_entry *vlan, *vlan_next;
2737 struct net_device *vlan_dev;
2738 struct flowi fl;
2739 struct rtable *rt;
1da177e4 2740
6b780567
MW
2741 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2742 if (!targets[i])
2743 continue;
c3ade5ca
JV
2744 dprintk("basa: target %x\n", targets[i]);
2745 if (list_empty(&bond->vlan_list)) {
2746 dprintk("basa: empty vlan: arp_send\n");
2747 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2748 bond->master_ip, 0);
2749 continue;
2750 }
2751
2752 /*
2753 * If VLANs are configured, we do a route lookup to
2754 * determine which VLAN interface would be used, so we
2755 * can tag the ARP with the proper VLAN tag.
2756 */
2757 memset(&fl, 0, sizeof(fl));
2758 fl.fl4_dst = targets[i];
2759 fl.fl4_tos = RTO_ONLINK;
2760
2761 rv = ip_route_output_key(&rt, &fl);
2762 if (rv) {
2763 if (net_ratelimit()) {
2764 printk(KERN_WARNING DRV_NAME
2765 ": %s: no route to arp_ip_target %u.%u.%u.%u\n",
2766 bond->dev->name, NIPQUAD(fl.fl4_dst));
2767 }
2768 continue;
2769 }
2770
2771 /*
2772 * This target is not on a VLAN
2773 */
2774 if (rt->u.dst.dev == bond->dev) {
ed4b9f80 2775 ip_rt_put(rt);
c3ade5ca
JV
2776 dprintk("basa: rtdev == bond->dev: arp_send\n");
2777 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2778 bond->master_ip, 0);
2779 continue;
2780 }
2781
2782 vlan_id = 0;
2783 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2784 vlan_list) {
2785 vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
2786 if (vlan_dev == rt->u.dst.dev) {
2787 vlan_id = vlan->vlan_id;
2788 dprintk("basa: vlan match on %s %d\n",
2789 vlan_dev->name, vlan_id);
2790 break;
2791 }
2792 }
2793
2794 if (vlan_id) {
ed4b9f80 2795 ip_rt_put(rt);
c3ade5ca
JV
2796 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2797 vlan->vlan_ip, vlan_id);
2798 continue;
2799 }
2800
2801 if (net_ratelimit()) {
2802 printk(KERN_WARNING DRV_NAME
2803 ": %s: no path to arp_ip_target %u.%u.%u.%u via rt.dev %s\n",
2804 bond->dev->name, NIPQUAD(fl.fl4_dst),
2805 rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2806 }
ed4b9f80 2807 ip_rt_put(rt);
c3ade5ca
JV
2808 }
2809}
2810
2811/*
2812 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2813 * for each VLAN above us.
2814 */
2815static void bond_send_gratuitous_arp(struct bonding *bond)
2816{
2817 struct slave *slave = bond->curr_active_slave;
2818 struct vlan_entry *vlan;
2819 struct net_device *vlan_dev;
2820
2821 dprintk("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
2822 slave ? slave->dev->name : "NULL");
2823 if (!slave)
2824 return;
2825
2826 if (bond->master_ip) {
2827 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
2828 bond->master_ip, 0);
2829 }
2830
2831 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
2832 vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
2833 if (vlan->vlan_ip) {
2834 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2835 vlan->vlan_ip, vlan->vlan_id);
2836 }
1da177e4
LT
2837 }
2838}
2839
2840/*
2841 * this function is called regularly to monitor each slave's link
2842 * ensuring that traffic is being sent and received when arp monitoring
2843 * is used in load-balancing mode. if the adapter has been dormant, then an
2844 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2845 * arp monitoring in active backup mode.
2846 */
a77b5325 2847void bond_loadbalance_arp_mon(struct net_device *bond_dev)
1da177e4
LT
2848{
2849 struct bonding *bond = bond_dev->priv;
2850 struct slave *slave, *oldcurrent;
2851 int do_failover = 0;
2852 int delta_in_ticks;
2853 int i;
2854
2855 read_lock(&bond->lock);
2856
2857 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2858
2859 if (bond->kill_timers) {
2860 goto out;
2861 }
2862
2863 if (bond->slave_cnt == 0) {
2864 goto re_arm;
2865 }
2866
2867 read_lock(&bond->curr_slave_lock);
2868 oldcurrent = bond->curr_active_slave;
2869 read_unlock(&bond->curr_slave_lock);
2870
2871 /* see if any of the previous devices are up now (i.e. they have
2872 * xmt and rcv traffic). the curr_active_slave does not come into
2873 * the picture unless it is null. also, slave->jiffies is not needed
2874 * here because we send an arp on each slave and give a slave as
2875 * long as it needs to get the tx/rx within the delta.
2876 * TODO: what about up/down delay in arp mode? it wasn't here before
2877 * so it can wait
2878 */
2879 bond_for_each_slave(bond, slave, i) {
2880 if (slave->link != BOND_LINK_UP) {
2881 if (((jiffies - slave->dev->trans_start) <= delta_in_ticks) &&
2882 ((jiffies - slave->dev->last_rx) <= delta_in_ticks)) {
2883
2884 slave->link = BOND_LINK_UP;
2885 slave->state = BOND_STATE_ACTIVE;
2886
2887 /* primary_slave has no meaning in round-robin
2888 * mode. the window of a slave being up and
2889 * curr_active_slave being null after enslaving
2890 * is closed.
2891 */
2892 if (!oldcurrent) {
2893 printk(KERN_INFO DRV_NAME
2894 ": %s: link status definitely "
2895 "up for interface %s, ",
2896 bond_dev->name,
2897 slave->dev->name);
2898 do_failover = 1;
2899 } else {
2900 printk(KERN_INFO DRV_NAME
2901 ": %s: interface %s is now up\n",
2902 bond_dev->name,
2903 slave->dev->name);
2904 }
2905 }
2906 } else {
2907 /* slave->link == BOND_LINK_UP */
2908
2909 /* not all switches will respond to an arp request
2910 * when the source ip is 0, so don't take the link down
2911 * if we don't know our ip yet
2912 */
2913 if (((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
2914 (((jiffies - slave->dev->last_rx) >= (2*delta_in_ticks)) &&
c3ade5ca 2915 bond_has_ip(bond))) {
1da177e4
LT
2916
2917 slave->link = BOND_LINK_DOWN;
2918 slave->state = BOND_STATE_BACKUP;
2919
2920 if (slave->link_failure_count < UINT_MAX) {
2921 slave->link_failure_count++;
2922 }
2923
2924 printk(KERN_INFO DRV_NAME
2925 ": %s: interface %s is now down.\n",
2926 bond_dev->name,
2927 slave->dev->name);
2928
2929 if (slave == oldcurrent) {
2930 do_failover = 1;
2931 }
2932 }
2933 }
2934
2935 /* note: if switch is in round-robin mode, all links
2936 * must tx arp to ensure all links rx an arp - otherwise
2937 * links may oscillate or not come up at all; if switch is
2938 * in something like xor mode, there is nothing we can
2939 * do - all replies will be rx'ed on same link causing slaves
2940 * to be unstable during low/no traffic periods
2941 */
2942 if (IS_UP(slave->dev)) {
2943 bond_arp_send_all(bond, slave);
2944 }
2945 }
2946
2947 if (do_failover) {
2948 write_lock(&bond->curr_slave_lock);
2949
2950 bond_select_active_slave(bond);
2951
2952 if (oldcurrent && !bond->curr_active_slave) {
2953 printk(KERN_INFO DRV_NAME
2954 ": %s: now running without any active "
2955 "interface !\n",
2956 bond_dev->name);
2957 }
2958
2959 write_unlock(&bond->curr_slave_lock);
2960 }
2961
2962re_arm:
2963 if (bond->params.arp_interval) {
2964 mod_timer(&bond->arp_timer, jiffies + delta_in_ticks);
2965 }
2966out:
2967 read_unlock(&bond->lock);
2968}
2969
2970/*
2971 * When using arp monitoring in active-backup mode, this function is
2972 * called to determine if any backup slaves have went down or a new
2973 * current slave needs to be found.
2974 * The backup slaves never generate traffic, they are considered up by merely
2975 * receiving traffic. If the current slave goes down, each backup slave will
2976 * be given the opportunity to tx/rx an arp before being taken down - this
2977 * prevents all slaves from being taken down due to the current slave not
2978 * sending any traffic for the backups to receive. The arps are not necessarily
2979 * necessary, any tx and rx traffic will keep the current slave up. While any
2980 * rx traffic will keep the backup slaves up, the current slave is responsible
2981 * for generating traffic to keep them up regardless of any other traffic they
2982 * may have received.
2983 * see loadbalance_arp_monitor for arp monitoring in load balancing mode
2984 */
a77b5325 2985void bond_activebackup_arp_mon(struct net_device *bond_dev)
1da177e4
LT
2986{
2987 struct bonding *bond = bond_dev->priv;
2988 struct slave *slave;
2989 int delta_in_ticks;
2990 int i;
2991
2992 read_lock(&bond->lock);
2993
2994 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2995
2996 if (bond->kill_timers) {
2997 goto out;
2998 }
2999
3000 if (bond->slave_cnt == 0) {
3001 goto re_arm;
3002 }
3003
3004 /* determine if any slave has come up or any backup slave has
3005 * gone down
3006 * TODO: what about up/down delay in arp mode? it wasn't here before
3007 * so it can wait
3008 */
3009 bond_for_each_slave(bond, slave, i) {
3010 if (slave->link != BOND_LINK_UP) {
3011 if ((jiffies - slave->dev->last_rx) <= delta_in_ticks) {
3012
3013 slave->link = BOND_LINK_UP;
3014
3015 write_lock(&bond->curr_slave_lock);
3016
3017 if ((!bond->curr_active_slave) &&
3018 ((jiffies - slave->dev->trans_start) <= delta_in_ticks)) {
3019 bond_change_active_slave(bond, slave);
3020 bond->current_arp_slave = NULL;
3021 } else if (bond->curr_active_slave != slave) {
3022 /* this slave has just come up but we
3023 * already have a current slave; this
3024 * can also happen if bond_enslave adds
3025 * a new slave that is up while we are
3026 * searching for a new slave
3027 */
3028 bond_set_slave_inactive_flags(slave);
3029 bond->current_arp_slave = NULL;
3030 }
3031
3032 if (slave == bond->curr_active_slave) {
3033 printk(KERN_INFO DRV_NAME
3034 ": %s: %s is up and now the "
3035 "active interface\n",
3036 bond_dev->name,
3037 slave->dev->name);
3038 } else {
3039 printk(KERN_INFO DRV_NAME
3040 ": %s: backup interface %s is "
3041 "now up\n",
3042 bond_dev->name,
3043 slave->dev->name);
3044 }
3045
3046 write_unlock(&bond->curr_slave_lock);
3047 }
3048 } else {
3049 read_lock(&bond->curr_slave_lock);
3050
3051 if ((slave != bond->curr_active_slave) &&
3052 (!bond->current_arp_slave) &&
3053 (((jiffies - slave->dev->last_rx) >= 3*delta_in_ticks) &&
c3ade5ca 3054 bond_has_ip(bond))) {
1da177e4
LT
3055 /* a backup slave has gone down; three times
3056 * the delta allows the current slave to be
3057 * taken out before the backup slave.
3058 * note: a non-null current_arp_slave indicates
3059 * the curr_active_slave went down and we are
3060 * searching for a new one; under this
3061 * condition we only take the curr_active_slave
3062 * down - this gives each slave a chance to
3063 * tx/rx traffic before being taken out
3064 */
3065
3066 read_unlock(&bond->curr_slave_lock);
3067
3068 slave->link = BOND_LINK_DOWN;
3069
3070 if (slave->link_failure_count < UINT_MAX) {
3071 slave->link_failure_count++;
3072 }
3073
3074 bond_set_slave_inactive_flags(slave);
3075
3076 printk(KERN_INFO DRV_NAME
3077 ": %s: backup interface %s is now down\n",
3078 bond_dev->name,
3079 slave->dev->name);
3080 } else {
3081 read_unlock(&bond->curr_slave_lock);
3082 }
3083 }
3084 }
3085
3086 read_lock(&bond->curr_slave_lock);
3087 slave = bond->curr_active_slave;
3088 read_unlock(&bond->curr_slave_lock);
3089
3090 if (slave) {
3091 /* if we have sent traffic in the past 2*arp_intervals but
3092 * haven't xmit and rx traffic in that time interval, select
3093 * a different slave. slave->jiffies is only updated when
3094 * a slave first becomes the curr_active_slave - not necessarily
3095 * after every arp; this ensures the slave has a full 2*delta
3096 * before being taken out. if a primary is being used, check
3097 * if it is up and needs to take over as the curr_active_slave
3098 */
3099 if ((((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
c3ade5ca
JV
3100 (((jiffies - slave->dev->last_rx) >= (2*delta_in_ticks)) &&
3101 bond_has_ip(bond))) &&
1da177e4
LT
3102 ((jiffies - slave->jiffies) >= 2*delta_in_ticks)) {
3103
3104 slave->link = BOND_LINK_DOWN;
3105
3106 if (slave->link_failure_count < UINT_MAX) {
3107 slave->link_failure_count++;
3108 }
3109
3110 printk(KERN_INFO DRV_NAME
3111 ": %s: link status down for active interface "
3112 "%s, disabling it\n",
3113 bond_dev->name,
3114 slave->dev->name);
3115
3116 write_lock(&bond->curr_slave_lock);
3117
3118 bond_select_active_slave(bond);
3119 slave = bond->curr_active_slave;
3120
3121 write_unlock(&bond->curr_slave_lock);
3122
3123 bond->current_arp_slave = slave;
3124
3125 if (slave) {
3126 slave->jiffies = jiffies;
3127 }
3128 } else if ((bond->primary_slave) &&
3129 (bond->primary_slave != slave) &&
3130 (bond->primary_slave->link == BOND_LINK_UP)) {
3131 /* at this point, slave is the curr_active_slave */
3132 printk(KERN_INFO DRV_NAME
3133 ": %s: changing from interface %s to primary "
3134 "interface %s\n",
3135 bond_dev->name,
3136 slave->dev->name,
3137 bond->primary_slave->dev->name);
3138
3139 /* primary is up so switch to it */
3140 write_lock(&bond->curr_slave_lock);
3141 bond_change_active_slave(bond, bond->primary_slave);
3142 write_unlock(&bond->curr_slave_lock);
3143
3144 slave = bond->primary_slave;
3145 slave->jiffies = jiffies;
3146 } else {
3147 bond->current_arp_slave = NULL;
3148 }
3149
3150 /* the current slave must tx an arp to ensure backup slaves
3151 * rx traffic
3152 */
c3ade5ca 3153 if (slave && bond_has_ip(bond)) {
1da177e4
LT
3154 bond_arp_send_all(bond, slave);
3155 }
3156 }
3157
3158 /* if we don't have a curr_active_slave, search for the next available
3159 * backup slave from the current_arp_slave and make it the candidate
3160 * for becoming the curr_active_slave
3161 */
3162 if (!slave) {
3163 if (!bond->current_arp_slave) {
3164 bond->current_arp_slave = bond->first_slave;
3165 }
3166
3167 if (bond->current_arp_slave) {
3168 bond_set_slave_inactive_flags(bond->current_arp_slave);
3169
3170 /* search for next candidate */
2f872f04 3171 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
1da177e4
LT
3172 if (IS_UP(slave->dev)) {
3173 slave->link = BOND_LINK_BACK;
3174 bond_set_slave_active_flags(slave);
3175 bond_arp_send_all(bond, slave);
3176 slave->jiffies = jiffies;
3177 bond->current_arp_slave = slave;
3178 break;
3179 }
3180
3181 /* if the link state is up at this point, we
3182 * mark it down - this can happen if we have
3183 * simultaneous link failures and
3184 * reselect_active_interface doesn't make this
3185 * one the current slave so it is still marked
3186 * up when it is actually down
3187 */
3188 if (slave->link == BOND_LINK_UP) {
3189 slave->link = BOND_LINK_DOWN;
3190 if (slave->link_failure_count < UINT_MAX) {
3191 slave->link_failure_count++;
3192 }
3193
3194 bond_set_slave_inactive_flags(slave);
3195
3196 printk(KERN_INFO DRV_NAME
3197 ": %s: backup interface %s is "
3198 "now down.\n",
3199 bond_dev->name,
3200 slave->dev->name);
3201 }
3202 }
3203 }
3204 }
3205
3206re_arm:
3207 if (bond->params.arp_interval) {
3208 mod_timer(&bond->arp_timer, jiffies + delta_in_ticks);
3209 }
3210out:
3211 read_unlock(&bond->lock);
3212}
3213
3214/*------------------------------ proc/seq_file-------------------------------*/
3215
3216#ifdef CONFIG_PROC_FS
3217
3218#define SEQ_START_TOKEN ((void *)1)
3219
3220static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
3221{
3222 struct bonding *bond = seq->private;
3223 loff_t off = 0;
3224 struct slave *slave;
3225 int i;
3226
3227 /* make sure the bond won't be taken away */
3228 read_lock(&dev_base_lock);
3229 read_lock_bh(&bond->lock);
3230
3231 if (*pos == 0) {
3232 return SEQ_START_TOKEN;
3233 }
3234
3235 bond_for_each_slave(bond, slave, i) {
3236 if (++off == *pos) {
3237 return slave;
3238 }
3239 }
3240
3241 return NULL;
3242}
3243
3244static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3245{
3246 struct bonding *bond = seq->private;
3247 struct slave *slave = v;
3248
3249 ++*pos;
3250 if (v == SEQ_START_TOKEN) {
3251 return bond->first_slave;
3252 }
3253
3254 slave = slave->next;
3255
3256 return (slave == bond->first_slave) ? NULL : slave;
3257}
3258
3259static void bond_info_seq_stop(struct seq_file *seq, void *v)
3260{
3261 struct bonding *bond = seq->private;
3262
3263 read_unlock_bh(&bond->lock);
3264 read_unlock(&dev_base_lock);
3265}
3266
3267static void bond_info_show_master(struct seq_file *seq)
3268{
3269 struct bonding *bond = seq->private;
3270 struct slave *curr;
4756b02f
MW
3271 int i;
3272 u32 target;
1da177e4
LT
3273
3274 read_lock(&bond->curr_slave_lock);
3275 curr = bond->curr_active_slave;
3276 read_unlock(&bond->curr_slave_lock);
3277
3278 seq_printf(seq, "Bonding Mode: %s\n",
3279 bond_mode_name(bond->params.mode));
3280
c61b75ad
MW
3281 if (bond->params.mode == BOND_MODE_XOR ||
3282 bond->params.mode == BOND_MODE_8023AD) {
3283 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3284 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3285 bond->params.xmit_policy);
3286 }
3287
1da177e4
LT
3288 if (USES_PRIMARY(bond->params.mode)) {
3289 seq_printf(seq, "Primary Slave: %s\n",
0f418b2a
MW
3290 (bond->primary_slave) ?
3291 bond->primary_slave->dev->name : "None");
1da177e4
LT
3292
3293 seq_printf(seq, "Currently Active Slave: %s\n",
3294 (curr) ? curr->dev->name : "None");
3295 }
3296
3297 seq_printf(seq, "MII Status: %s\n", (curr) ? "up" : "down");
3298 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3299 seq_printf(seq, "Up Delay (ms): %d\n",
3300 bond->params.updelay * bond->params.miimon);
3301 seq_printf(seq, "Down Delay (ms): %d\n",
3302 bond->params.downdelay * bond->params.miimon);
3303
4756b02f
MW
3304
3305 /* ARP information */
3306 if(bond->params.arp_interval > 0) {
3307 int printed=0;
3308 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3309 bond->params.arp_interval);
3310
3311 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3312
3313 for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
3314 if (!bond->params.arp_targets[i])
3315 continue;
3316 if (printed)
3317 seq_printf(seq, ",");
3318 target = ntohl(bond->params.arp_targets[i]);
3319 seq_printf(seq, " %d.%d.%d.%d", HIPQUAD(target));
3320 printed = 1;
3321 }
3322 seq_printf(seq, "\n");
3323 }
3324
1da177e4
LT
3325 if (bond->params.mode == BOND_MODE_8023AD) {
3326 struct ad_info ad_info;
3327
3328 seq_puts(seq, "\n802.3ad info\n");
3329 seq_printf(seq, "LACP rate: %s\n",
3330 (bond->params.lacp_fast) ? "fast" : "slow");
3331
3332 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3333 seq_printf(seq, "bond %s has no active aggregator\n",
3334 bond->dev->name);
3335 } else {
3336 seq_printf(seq, "Active Aggregator Info:\n");
3337
3338 seq_printf(seq, "\tAggregator ID: %d\n",
3339 ad_info.aggregator_id);
3340 seq_printf(seq, "\tNumber of ports: %d\n",
3341 ad_info.ports);
3342 seq_printf(seq, "\tActor Key: %d\n",
3343 ad_info.actor_key);
3344 seq_printf(seq, "\tPartner Key: %d\n",
3345 ad_info.partner_key);
3346 seq_printf(seq, "\tPartner Mac Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
3347 ad_info.partner_system[0],
3348 ad_info.partner_system[1],
3349 ad_info.partner_system[2],
3350 ad_info.partner_system[3],
3351 ad_info.partner_system[4],
3352 ad_info.partner_system[5]);
3353 }
3354 }
3355}
3356
3357static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
3358{
3359 struct bonding *bond = seq->private;
3360
3361 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3362 seq_printf(seq, "MII Status: %s\n",
3363 (slave->link == BOND_LINK_UP) ? "up" : "down");
3364 seq_printf(seq, "Link Failure Count: %d\n",
3365 slave->link_failure_count);
3366
217df670
JV
3367 seq_printf(seq,
3368 "Permanent HW addr: %02x:%02x:%02x:%02x:%02x:%02x\n",
3369 slave->perm_hwaddr[0], slave->perm_hwaddr[1],
3370 slave->perm_hwaddr[2], slave->perm_hwaddr[3],
3371 slave->perm_hwaddr[4], slave->perm_hwaddr[5]);
1da177e4
LT
3372
3373 if (bond->params.mode == BOND_MODE_8023AD) {
3374 const struct aggregator *agg
3375 = SLAVE_AD_INFO(slave).port.aggregator;
3376
3377 if (agg) {
3378 seq_printf(seq, "Aggregator ID: %d\n",
3379 agg->aggregator_identifier);
3380 } else {
3381 seq_puts(seq, "Aggregator ID: N/A\n");
3382 }
3383 }
3384}
3385
3386static int bond_info_seq_show(struct seq_file *seq, void *v)
3387{
3388 if (v == SEQ_START_TOKEN) {
3389 seq_printf(seq, "%s\n", version);
3390 bond_info_show_master(seq);
3391 } else {
3392 bond_info_show_slave(seq, v);
3393 }
3394
3395 return 0;
3396}
3397
3398static struct seq_operations bond_info_seq_ops = {
3399 .start = bond_info_seq_start,
3400 .next = bond_info_seq_next,
3401 .stop = bond_info_seq_stop,
3402 .show = bond_info_seq_show,
3403};
3404
3405static int bond_info_open(struct inode *inode, struct file *file)
3406{
3407 struct seq_file *seq;
3408 struct proc_dir_entry *proc;
3409 int res;
3410
3411 res = seq_open(file, &bond_info_seq_ops);
3412 if (!res) {
3413 /* recover the pointer buried in proc_dir_entry data */
3414 seq = file->private_data;
3415 proc = PDE(inode);
3416 seq->private = proc->data;
3417 }
3418
3419 return res;
3420}
3421
3422static struct file_operations bond_info_fops = {
3423 .owner = THIS_MODULE,
3424 .open = bond_info_open,
3425 .read = seq_read,
3426 .llseek = seq_lseek,
3427 .release = seq_release,
3428};
3429
3430static int bond_create_proc_entry(struct bonding *bond)
3431{
3432 struct net_device *bond_dev = bond->dev;
3433
3434 if (bond_proc_dir) {
3435 bond->proc_entry = create_proc_entry(bond_dev->name,
3436 S_IRUGO,
3437 bond_proc_dir);
3438 if (bond->proc_entry == NULL) {
3439 printk(KERN_WARNING DRV_NAME
3440 ": Warning: Cannot create /proc/net/%s/%s\n",
3441 DRV_NAME, bond_dev->name);
3442 } else {
3443 bond->proc_entry->data = bond;
3444 bond->proc_entry->proc_fops = &bond_info_fops;
3445 bond->proc_entry->owner = THIS_MODULE;
3446 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
3447 }
3448 }
3449
3450 return 0;
3451}
3452
3453static void bond_remove_proc_entry(struct bonding *bond)
3454{
3455 if (bond_proc_dir && bond->proc_entry) {
3456 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3457 memset(bond->proc_file_name, 0, IFNAMSIZ);
3458 bond->proc_entry = NULL;
3459 }
3460}
3461
3462/* Create the bonding directory under /proc/net, if doesn't exist yet.
3463 * Caller must hold rtnl_lock.
3464 */
3465static void bond_create_proc_dir(void)
3466{
3467 int len = strlen(DRV_NAME);
3468
3469 for (bond_proc_dir = proc_net->subdir; bond_proc_dir;
3470 bond_proc_dir = bond_proc_dir->next) {
3471 if ((bond_proc_dir->namelen == len) &&
3472 !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3473 break;
3474 }
3475 }
3476
3477 if (!bond_proc_dir) {
3478 bond_proc_dir = proc_mkdir(DRV_NAME, proc_net);
3479 if (bond_proc_dir) {
3480 bond_proc_dir->owner = THIS_MODULE;
3481 } else {
3482 printk(KERN_WARNING DRV_NAME
3483 ": Warning: cannot create /proc/net/%s\n",
3484 DRV_NAME);
3485 }
3486 }
3487}
3488
3489/* Destroy the bonding directory under /proc/net, if empty.
3490 * Caller must hold rtnl_lock.
3491 */
3492static void bond_destroy_proc_dir(void)
3493{
3494 struct proc_dir_entry *de;
3495
3496 if (!bond_proc_dir) {
3497 return;
3498 }
3499
3500 /* verify that the /proc dir is empty */
3501 for (de = bond_proc_dir->subdir; de; de = de->next) {
3502 /* ignore . and .. */
3503 if (*(de->name) != '.') {
3504 break;
3505 }
3506 }
3507
3508 if (de) {
3509 if (bond_proc_dir->owner == THIS_MODULE) {
3510 bond_proc_dir->owner = NULL;
3511 }
3512 } else {
3513 remove_proc_entry(DRV_NAME, proc_net);
3514 bond_proc_dir = NULL;
3515 }
3516}
3517#endif /* CONFIG_PROC_FS */
3518
3519/*-------------------------- netdev event handling --------------------------*/
3520
3521/*
3522 * Change device name
3523 */
3524static int bond_event_changename(struct bonding *bond)
3525{
3526#ifdef CONFIG_PROC_FS
3527 bond_remove_proc_entry(bond);
3528 bond_create_proc_entry(bond);
3529#endif
b76cdba9
MW
3530 down_write(&(bonding_rwsem));
3531 bond_destroy_sysfs_entry(bond);
3532 bond_create_sysfs_entry(bond);
3533 up_write(&(bonding_rwsem));
1da177e4
LT
3534 return NOTIFY_DONE;
3535}
3536
3537static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
3538{
3539 struct bonding *event_bond = bond_dev->priv;
3540
3541 switch (event) {
3542 case NETDEV_CHANGENAME:
3543 return bond_event_changename(event_bond);
3544 case NETDEV_UNREGISTER:
3545 /*
3546 * TODO: remove a bond from the list?
3547 */
3548 break;
3549 default:
3550 break;
3551 }
3552
3553 return NOTIFY_DONE;
3554}
3555
3556static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
3557{
3558 struct net_device *bond_dev = slave_dev->master;
8531c5ff 3559 struct bonding *bond = bond_dev->priv;
1da177e4
LT
3560
3561 switch (event) {
3562 case NETDEV_UNREGISTER:
3563 if (bond_dev) {
3564 bond_release(bond_dev, slave_dev);
3565 }
3566 break;
3567 case NETDEV_CHANGE:
3568 /*
3569 * TODO: is this what we get if somebody
3570 * sets up a hierarchical bond, then rmmod's
3571 * one of the slave bonding devices?
3572 */
3573 break;
3574 case NETDEV_DOWN:
3575 /*
3576 * ... Or is it this?
3577 */
3578 break;
3579 case NETDEV_CHANGEMTU:
3580 /*
3581 * TODO: Should slaves be allowed to
3582 * independently alter their MTU? For
3583 * an active-backup bond, slaves need
3584 * not be the same type of device, so
3585 * MTUs may vary. For other modes,
3586 * slaves arguably should have the
3587 * same MTUs. To do this, we'd need to
3588 * take over the slave's change_mtu
3589 * function for the duration of their
3590 * servitude.
3591 */
3592 break;
3593 case NETDEV_CHANGENAME:
3594 /*
3595 * TODO: handle changing the primary's name
3596 */
3597 break;
8531c5ff
AK
3598 case NETDEV_FEAT_CHANGE:
3599 bond_compute_features(bond);
3600 break;
1da177e4
LT
3601 default:
3602 break;
3603 }
3604
3605 return NOTIFY_DONE;
3606}
3607
3608/*
3609 * bond_netdev_event: handle netdev notifier chain events.
3610 *
3611 * This function receives events for the netdev chain. The caller (an
3612 * ioctl handler calling notifier_call_chain) holds the necessary
3613 * locks for us to safely manipulate the slave devices (RTNL lock,
3614 * dev_probe_lock).
3615 */
3616static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3617{
3618 struct net_device *event_dev = (struct net_device *)ptr;
3619
3620 dprintk("event_dev: %s, event: %lx\n",
3621 (event_dev ? event_dev->name : "None"),
3622 event);
3623
3624 if (event_dev->flags & IFF_MASTER) {
3625 dprintk("IFF_MASTER\n");
3626 return bond_master_netdev_event(event, event_dev);
3627 }
3628
3629 if (event_dev->flags & IFF_SLAVE) {
3630 dprintk("IFF_SLAVE\n");
3631 return bond_slave_netdev_event(event, event_dev);
3632 }
3633
3634 return NOTIFY_DONE;
3635}
3636
c3ade5ca
JV
3637/*
3638 * bond_inetaddr_event: handle inetaddr notifier chain events.
3639 *
3640 * We keep track of device IPs primarily to use as source addresses in
3641 * ARP monitor probes (rather than spewing out broadcasts all the time).
3642 *
3643 * We track one IP for the main device (if it has one), plus one per VLAN.
3644 */
3645static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3646{
3647 struct in_ifaddr *ifa = ptr;
3648 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
3649 struct bonding *bond, *bond_next;
3650 struct vlan_entry *vlan, *vlan_next;
3651
3652 list_for_each_entry_safe(bond, bond_next, &bond_dev_list, bond_list) {
3653 if (bond->dev == event_dev) {
3654 switch (event) {
3655 case NETDEV_UP:
3656 bond->master_ip = ifa->ifa_local;
3657 return NOTIFY_OK;
3658 case NETDEV_DOWN:
3659 bond->master_ip = bond_glean_dev_ip(bond->dev);
3660 return NOTIFY_OK;
3661 default:
3662 return NOTIFY_DONE;
3663 }
3664 }
3665
3666 if (list_empty(&bond->vlan_list))
3667 continue;
3668
3669 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
3670 vlan_list) {
3671 vlan_dev = bond->vlgrp->vlan_devices[vlan->vlan_id];
3672 if (vlan_dev == event_dev) {
3673 switch (event) {
3674 case NETDEV_UP:
3675 vlan->vlan_ip = ifa->ifa_local;
3676 return NOTIFY_OK;
3677 case NETDEV_DOWN:
3678 vlan->vlan_ip =
3679 bond_glean_dev_ip(vlan_dev);
3680 return NOTIFY_OK;
3681 default:
3682 return NOTIFY_DONE;
3683 }
3684 }
3685 }
3686 }
3687 return NOTIFY_DONE;
3688}
3689
1da177e4
LT
3690static struct notifier_block bond_netdev_notifier = {
3691 .notifier_call = bond_netdev_event,
3692};
3693
c3ade5ca
JV
3694static struct notifier_block bond_inetaddr_notifier = {
3695 .notifier_call = bond_inetaddr_event,
3696};
3697
1da177e4
LT
3698/*-------------------------- Packet type handling ---------------------------*/
3699
3700/* register to receive lacpdus on a bond */
3701static void bond_register_lacpdu(struct bonding *bond)
3702{
3703 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3704
3705 /* initialize packet type */
3706 pk_type->type = PKT_TYPE_LACPDU;
3707 pk_type->dev = bond->dev;
3708 pk_type->func = bond_3ad_lacpdu_recv;
3709
3710 dev_add_pack(pk_type);
3711}
3712
3713/* unregister to receive lacpdus on a bond */
3714static void bond_unregister_lacpdu(struct bonding *bond)
3715{
3716 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3717}
3718
169a3e66
JV
3719/*---------------------------- Hashing Policies -----------------------------*/
3720
3721/*
3722 * Hash for the the output device based upon layer 3 and layer 4 data. If
3723 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3724 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3725 */
3726static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
3727 struct net_device *bond_dev, int count)
3728{
3729 struct ethhdr *data = (struct ethhdr *)skb->data;
3730 struct iphdr *iph = skb->nh.iph;
3731 u16 *layer4hdr = (u16 *)((u32 *)iph + iph->ihl);
3732 int layer4_xor = 0;
3733
3734 if (skb->protocol == __constant_htons(ETH_P_IP)) {
3735 if (!(iph->frag_off & __constant_htons(IP_MF|IP_OFFSET)) &&
3736 (iph->protocol == IPPROTO_TCP ||
3737 iph->protocol == IPPROTO_UDP)) {
3738 layer4_xor = htons((*layer4hdr ^ *(layer4hdr + 1)));
3739 }
3740 return (layer4_xor ^
3741 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3742
3743 }
3744
3745 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3746}
3747
3748/*
3749 * Hash for the output device based upon layer 2 data
3750 */
3751static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
3752 struct net_device *bond_dev, int count)
3753{
3754 struct ethhdr *data = (struct ethhdr *)skb->data;
3755
3756 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3757}
3758
1da177e4
LT
3759/*-------------------------- Device entry points ----------------------------*/
3760
3761static int bond_open(struct net_device *bond_dev)
3762{
3763 struct bonding *bond = bond_dev->priv;
3764 struct timer_list *mii_timer = &bond->mii_timer;
3765 struct timer_list *arp_timer = &bond->arp_timer;
3766
3767 bond->kill_timers = 0;
3768
3769 if ((bond->params.mode == BOND_MODE_TLB) ||
3770 (bond->params.mode == BOND_MODE_ALB)) {
3771 struct timer_list *alb_timer = &(BOND_ALB_INFO(bond).alb_timer);
3772
3773 /* bond_alb_initialize must be called before the timer
3774 * is started.
3775 */
3776 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3777 /* something went wrong - fail the open operation */
3778 return -1;
3779 }
3780
3781 init_timer(alb_timer);
3782 alb_timer->expires = jiffies + 1;
3783 alb_timer->data = (unsigned long)bond;
3784 alb_timer->function = (void *)&bond_alb_monitor;
3785 add_timer(alb_timer);
3786 }
3787
3788 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3789 init_timer(mii_timer);
3790 mii_timer->expires = jiffies + 1;
3791 mii_timer->data = (unsigned long)bond_dev;
3792 mii_timer->function = (void *)&bond_mii_monitor;
3793 add_timer(mii_timer);
3794 }
3795
3796 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3797 init_timer(arp_timer);
3798 arp_timer->expires = jiffies + 1;
3799 arp_timer->data = (unsigned long)bond_dev;
3800 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
3801 arp_timer->function = (void *)&bond_activebackup_arp_mon;
3802 } else {
3803 arp_timer->function = (void *)&bond_loadbalance_arp_mon;
3804 }
3805 add_timer(arp_timer);
3806 }
3807
3808 if (bond->params.mode == BOND_MODE_8023AD) {
3809 struct timer_list *ad_timer = &(BOND_AD_INFO(bond).ad_timer);
3810 init_timer(ad_timer);
3811 ad_timer->expires = jiffies + 1;
3812 ad_timer->data = (unsigned long)bond;
3813 ad_timer->function = (void *)&bond_3ad_state_machine_handler;
3814 add_timer(ad_timer);
3815
3816 /* register to receive LACPDUs */
3817 bond_register_lacpdu(bond);
3818 }
3819
3820 return 0;
3821}
3822
3823static int bond_close(struct net_device *bond_dev)
3824{
3825 struct bonding *bond = bond_dev->priv;
3826
3827 if (bond->params.mode == BOND_MODE_8023AD) {
3828 /* Unregister the receive of LACPDUs */
3829 bond_unregister_lacpdu(bond);
3830 }
3831
3832 write_lock_bh(&bond->lock);
3833
3834 bond_mc_list_destroy(bond);
3835
3836 /* signal timers not to re-arm */
3837 bond->kill_timers = 1;
3838
3839 write_unlock_bh(&bond->lock);
3840
3841 /* del_timer_sync must run without holding the bond->lock
3842 * because a running timer might be trying to hold it too
3843 */
3844
3845 if (bond->params.miimon) { /* link check interval, in milliseconds. */
3846 del_timer_sync(&bond->mii_timer);
3847 }
3848
3849 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
3850 del_timer_sync(&bond->arp_timer);
3851 }
3852
3853 switch (bond->params.mode) {
3854 case BOND_MODE_8023AD:
3855 del_timer_sync(&(BOND_AD_INFO(bond).ad_timer));
3856 break;
3857 case BOND_MODE_TLB:
3858 case BOND_MODE_ALB:
3859 del_timer_sync(&(BOND_ALB_INFO(bond).alb_timer));
3860 break;
3861 default:
3862 break;
3863 }
3864
3865 /* Release the bonded slaves */
3866 bond_release_all(bond_dev);
3867
3868 if ((bond->params.mode == BOND_MODE_TLB) ||
3869 (bond->params.mode == BOND_MODE_ALB)) {
3870 /* Must be called only after all
3871 * slaves have been released
3872 */
3873 bond_alb_deinitialize(bond);
3874 }
3875
3876 return 0;
3877}
3878
3879static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3880{
3881 struct bonding *bond = bond_dev->priv;
3882 struct net_device_stats *stats = &(bond->stats), *sstats;
3883 struct slave *slave;
3884 int i;
3885
3886 memset(stats, 0, sizeof(struct net_device_stats));
3887
3888 read_lock_bh(&bond->lock);
3889
3890 bond_for_each_slave(bond, slave, i) {
3891 sstats = slave->dev->get_stats(slave->dev);
3892
3893 stats->rx_packets += sstats->rx_packets;
3894 stats->rx_bytes += sstats->rx_bytes;
3895 stats->rx_errors += sstats->rx_errors;
3896 stats->rx_dropped += sstats->rx_dropped;
3897
3898 stats->tx_packets += sstats->tx_packets;
3899 stats->tx_bytes += sstats->tx_bytes;
3900 stats->tx_errors += sstats->tx_errors;
3901 stats->tx_dropped += sstats->tx_dropped;
3902
3903 stats->multicast += sstats->multicast;
3904 stats->collisions += sstats->collisions;
3905
3906 stats->rx_length_errors += sstats->rx_length_errors;
3907 stats->rx_over_errors += sstats->rx_over_errors;
3908 stats->rx_crc_errors += sstats->rx_crc_errors;
3909 stats->rx_frame_errors += sstats->rx_frame_errors;
3910 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3911 stats->rx_missed_errors += sstats->rx_missed_errors;
3912
3913 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3914 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3915 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3916 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3917 stats->tx_window_errors += sstats->tx_window_errors;
3918 }
3919
3920 read_unlock_bh(&bond->lock);
3921
3922 return stats;
3923}
3924
3925static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3926{
3927 struct net_device *slave_dev = NULL;
3928 struct ifbond k_binfo;
3929 struct ifbond __user *u_binfo = NULL;
3930 struct ifslave k_sinfo;
3931 struct ifslave __user *u_sinfo = NULL;
3932 struct mii_ioctl_data *mii = NULL;
1da177e4
LT
3933 int res = 0;
3934
3935 dprintk("bond_ioctl: master=%s, cmd=%d\n",
3936 bond_dev->name, cmd);
3937
3938 switch (cmd) {
1da177e4
LT
3939 case SIOCGMIIPHY:
3940 mii = if_mii(ifr);
3941 if (!mii) {
3942 return -EINVAL;
3943 }
3944 mii->phy_id = 0;
3945 /* Fall Through */
3946 case SIOCGMIIREG:
3947 /*
3948 * We do this again just in case we were called by SIOCGMIIREG
3949 * instead of SIOCGMIIPHY.
3950 */
3951 mii = if_mii(ifr);
3952 if (!mii) {
3953 return -EINVAL;
3954 }
3955
3956 if (mii->reg_num == 1) {
3957 struct bonding *bond = bond_dev->priv;
3958 mii->val_out = 0;
3959 read_lock_bh(&bond->lock);
3960 read_lock(&bond->curr_slave_lock);
3961 if (bond->curr_active_slave) {
3962 mii->val_out = BMSR_LSTATUS;
3963 }
3964 read_unlock(&bond->curr_slave_lock);
3965 read_unlock_bh(&bond->lock);
3966 }
3967
3968 return 0;
3969 case BOND_INFO_QUERY_OLD:
3970 case SIOCBONDINFOQUERY:
3971 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3972
3973 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
3974 return -EFAULT;
3975 }
3976
3977 res = bond_info_query(bond_dev, &k_binfo);
3978 if (res == 0) {
3979 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
3980 return -EFAULT;
3981 }
3982 }
3983
3984 return res;
3985 case BOND_SLAVE_INFO_QUERY_OLD:
3986 case SIOCBONDSLAVEINFOQUERY:
3987 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3988
3989 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
3990 return -EFAULT;
3991 }
3992
3993 res = bond_slave_info_query(bond_dev, &k_sinfo);
3994 if (res == 0) {
3995 if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
3996 return -EFAULT;
3997 }
3998 }
3999
4000 return res;
4001 default:
4002 /* Go on */
4003 break;
4004 }
4005
4006 if (!capable(CAP_NET_ADMIN)) {
4007 return -EPERM;
4008 }
4009
b76cdba9 4010 down_write(&(bonding_rwsem));
1da177e4
LT
4011 slave_dev = dev_get_by_name(ifr->ifr_slave);
4012
4013 dprintk("slave_dev=%p: \n", slave_dev);
4014
4015 if (!slave_dev) {
4016 res = -ENODEV;
4017 } else {
4018 dprintk("slave_dev->name=%s: \n", slave_dev->name);
4019 switch (cmd) {
4020 case BOND_ENSLAVE_OLD:
4021 case SIOCBONDENSLAVE:
4022 res = bond_enslave(bond_dev, slave_dev);
4023 break;
4024 case BOND_RELEASE_OLD:
4025 case SIOCBONDRELEASE:
4026 res = bond_release(bond_dev, slave_dev);
4027 break;
4028 case BOND_SETHWADDR_OLD:
4029 case SIOCBONDSETHWADDR:
4030 res = bond_sethwaddr(bond_dev, slave_dev);
4031 break;
4032 case BOND_CHANGE_ACTIVE_OLD:
4033 case SIOCBONDCHANGEACTIVE:
4034 res = bond_ioctl_change_active(bond_dev, slave_dev);
4035 break;
4036 default:
4037 res = -EOPNOTSUPP;
4038 }
4039
4040 dev_put(slave_dev);
4041 }
4042
b76cdba9 4043 up_write(&(bonding_rwsem));
1da177e4
LT
4044 return res;
4045}
4046
4047static void bond_set_multicast_list(struct net_device *bond_dev)
4048{
4049 struct bonding *bond = bond_dev->priv;
4050 struct dev_mc_list *dmi;
4051
4052 write_lock_bh(&bond->lock);
4053
4054 /*
4055 * Do promisc before checking multicast_mode
4056 */
4057 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
4058 bond_set_promiscuity(bond, 1);
4059 }
4060
4061 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
4062 bond_set_promiscuity(bond, -1);
4063 }
4064
4065 /* set allmulti flag to slaves */
4066 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
4067 bond_set_allmulti(bond, 1);
4068 }
4069
4070 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
4071 bond_set_allmulti(bond, -1);
4072 }
4073
4074 bond->flags = bond_dev->flags;
4075
4076 /* looking for addresses to add to slaves' mc list */
4077 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
4078 if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
4079 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
4080 }
4081 }
4082
4083 /* looking for addresses to delete from slaves' list */
4084 for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
4085 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
4086 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
4087 }
4088 }
4089
4090 /* save master's multicast list */
4091 bond_mc_list_destroy(bond);
4092 bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
4093
4094 write_unlock_bh(&bond->lock);
4095}
4096
4097/*
4098 * Change the MTU of all of a master's slaves to match the master
4099 */
4100static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
4101{
4102 struct bonding *bond = bond_dev->priv;
4103 struct slave *slave, *stop_at;
4104 int res = 0;
4105 int i;
4106
4107 dprintk("bond=%p, name=%s, new_mtu=%d\n", bond,
4108 (bond_dev ? bond_dev->name : "None"), new_mtu);
4109
4110 /* Can't hold bond->lock with bh disabled here since
4111 * some base drivers panic. On the other hand we can't
4112 * hold bond->lock without bh disabled because we'll
4113 * deadlock. The only solution is to rely on the fact
4114 * that we're under rtnl_lock here, and the slaves
4115 * list won't change. This doesn't solve the problem
4116 * of setting the slave's MTU while it is
4117 * transmitting, but the assumption is that the base
4118 * driver can handle that.
4119 *
4120 * TODO: figure out a way to safely iterate the slaves
4121 * list, but without holding a lock around the actual
4122 * call to the base driver.
4123 */
4124
4125 bond_for_each_slave(bond, slave, i) {
4126 dprintk("s %p s->p %p c_m %p\n", slave,
4127 slave->prev, slave->dev->change_mtu);
4128 res = dev_set_mtu(slave->dev, new_mtu);
4129
4130 if (res) {
4131 /* If we failed to set the slave's mtu to the new value
4132 * we must abort the operation even in ACTIVE_BACKUP
4133 * mode, because if we allow the backup slaves to have
4134 * different mtu values than the active slave we'll
4135 * need to change their mtu when doing a failover. That
4136 * means changing their mtu from timer context, which
4137 * is probably not a good idea.
4138 */
4139 dprintk("err %d %s\n", res, slave->dev->name);
4140 goto unwind;
4141 }
4142 }
4143
4144 bond_dev->mtu = new_mtu;
4145
4146 return 0;
4147
4148unwind:
4149 /* unwind from head to the slave that failed */
4150 stop_at = slave;
4151 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4152 int tmp_res;
4153
4154 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4155 if (tmp_res) {
4156 dprintk("unwind err %d dev %s\n", tmp_res,
4157 slave->dev->name);
4158 }
4159 }
4160
4161 return res;
4162}
4163
4164/*
4165 * Change HW address
4166 *
4167 * Note that many devices must be down to change the HW address, and
4168 * downing the master releases all slaves. We can make bonds full of
4169 * bonding devices to test this, however.
4170 */
4171static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4172{
4173 struct bonding *bond = bond_dev->priv;
4174 struct sockaddr *sa = addr, tmp_sa;
4175 struct slave *slave, *stop_at;
4176 int res = 0;
4177 int i;
4178
4179 dprintk("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
4180
4181 if (!is_valid_ether_addr(sa->sa_data)) {
4182 return -EADDRNOTAVAIL;
4183 }
4184
4185 /* Can't hold bond->lock with bh disabled here since
4186 * some base drivers panic. On the other hand we can't
4187 * hold bond->lock without bh disabled because we'll
4188 * deadlock. The only solution is to rely on the fact
4189 * that we're under rtnl_lock here, and the slaves
4190 * list won't change. This doesn't solve the problem
4191 * of setting the slave's hw address while it is
4192 * transmitting, but the assumption is that the base
4193 * driver can handle that.
4194 *
4195 * TODO: figure out a way to safely iterate the slaves
4196 * list, but without holding a lock around the actual
4197 * call to the base driver.
4198 */
4199
4200 bond_for_each_slave(bond, slave, i) {
4201 dprintk("slave %p %s\n", slave, slave->dev->name);
4202
4203 if (slave->dev->set_mac_address == NULL) {
4204 res = -EOPNOTSUPP;
4205 dprintk("EOPNOTSUPP %s\n", slave->dev->name);
4206 goto unwind;
4207 }
4208
4209 res = dev_set_mac_address(slave->dev, addr);
4210 if (res) {
4211 /* TODO: consider downing the slave
4212 * and retry ?
4213 * User should expect communications
4214 * breakage anyway until ARP finish
4215 * updating, so...
4216 */
4217 dprintk("err %d %s\n", res, slave->dev->name);
4218 goto unwind;
4219 }
4220 }
4221
4222 /* success */
4223 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4224 return 0;
4225
4226unwind:
4227 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4228 tmp_sa.sa_family = bond_dev->type;
4229
4230 /* unwind from head to the slave that failed */
4231 stop_at = slave;
4232 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4233 int tmp_res;
4234
4235 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4236 if (tmp_res) {
4237 dprintk("unwind err %d dev %s\n", tmp_res,
4238 slave->dev->name);
4239 }
4240 }
4241
4242 return res;
4243}
4244
4245static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4246{
4247 struct bonding *bond = bond_dev->priv;
4248 struct slave *slave, *start_at;
4249 int i;
4250 int res = 1;
4251
4252 read_lock(&bond->lock);
4253
4254 if (!BOND_IS_OK(bond)) {
4255 goto out;
4256 }
4257
4258 read_lock(&bond->curr_slave_lock);
4259 slave = start_at = bond->curr_active_slave;
4260 read_unlock(&bond->curr_slave_lock);
4261
4262 if (!slave) {
4263 goto out;
4264 }
4265
4266 bond_for_each_slave_from(bond, slave, i, start_at) {
4267 if (IS_UP(slave->dev) &&
4268 (slave->link == BOND_LINK_UP) &&
4269 (slave->state == BOND_STATE_ACTIVE)) {
4270 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4271
4272 write_lock(&bond->curr_slave_lock);
4273 bond->curr_active_slave = slave->next;
4274 write_unlock(&bond->curr_slave_lock);
4275
4276 break;
4277 }
4278 }
4279
4280
4281out:
4282 if (res) {
4283 /* no suitable interface, frame not sent */
4284 dev_kfree_skb(skb);
4285 }
4286 read_unlock(&bond->lock);
4287 return 0;
4288}
4289
075897ce 4290static void bond_activebackup_xmit_copy(struct sk_buff *skb,
df49898a
JL
4291 struct bonding *bond,
4292 struct slave *slave)
075897ce
JL
4293{
4294 struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
4295 struct ethhdr *eth_data;
4296 u8 *hwaddr;
4297 int res;
4298
4299 if (!skb2) {
4300 printk(KERN_ERR DRV_NAME ": Error: "
4301 "bond_activebackup_xmit_copy(): skb_copy() failed\n");
4302 return;
4303 }
4304
4305 skb2->mac.raw = (unsigned char *)skb2->data;
4306 eth_data = eth_hdr(skb2);
4307
df49898a
JL
4308 /* Pick an appropriate source MAC address
4309 * -- use slave's perm MAC addr, unless used by bond
4310 * -- otherwise, borrow active slave's perm MAC addr
4311 * since that will not be used
4312 */
075897ce
JL
4313 hwaddr = slave->perm_hwaddr;
4314 if (!memcmp(eth_data->h_source, hwaddr, ETH_ALEN))
4315 hwaddr = bond->curr_active_slave->perm_hwaddr;
4316
4317 /* Set source MAC address appropriately */
4318 memcpy(eth_data->h_source, hwaddr, ETH_ALEN);
4319
4320 res = bond_dev_queue_xmit(bond, skb2, slave->dev);
4321 if (res)
4322 dev_kfree_skb(skb2);
4323
4324 return;
4325}
4326
1da177e4
LT
4327/*
4328 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4329 * the bond has a usable interface.
4330 */
4331static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4332{
4333 struct bonding *bond = bond_dev->priv;
4334 int res = 1;
4335
1da177e4
LT
4336 read_lock(&bond->lock);
4337 read_lock(&bond->curr_slave_lock);
4338
4339 if (!BOND_IS_OK(bond)) {
4340 goto out;
4341 }
4342
075897ce
JL
4343 if (!bond->curr_active_slave)
4344 goto out;
4345
4346 /* Xmit IGMP frames on all slaves to ensure rapid fail-over
4347 for multicast traffic on snooping switches */
4348 if (skb->protocol == __constant_htons(ETH_P_IP) &&
4349 skb->nh.iph->protocol == IPPROTO_IGMP) {
4350 struct slave *slave, *active_slave;
4351 int i;
4352
4353 active_slave = bond->curr_active_slave;
4354 bond_for_each_slave_from_to(bond, slave, i, active_slave->next,
4355 active_slave->prev)
4356 if (IS_UP(slave->dev) &&
4357 (slave->link == BOND_LINK_UP))
4358 bond_activebackup_xmit_copy(skb, bond, slave);
1da177e4
LT
4359 }
4360
075897ce
JL
4361 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4362
1da177e4
LT
4363out:
4364 if (res) {
4365 /* no suitable interface, frame not sent */
4366 dev_kfree_skb(skb);
4367 }
4368 read_unlock(&bond->curr_slave_lock);
4369 read_unlock(&bond->lock);
4370 return 0;
4371}
4372
4373/*
169a3e66
JV
4374 * In bond_xmit_xor() , we determine the output device by using a pre-
4375 * determined xmit_hash_policy(), If the selected device is not enabled,
4376 * find the next active slave.
1da177e4
LT
4377 */
4378static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4379{
4380 struct bonding *bond = bond_dev->priv;
1da177e4
LT
4381 struct slave *slave, *start_at;
4382 int slave_no;
4383 int i;
4384 int res = 1;
4385
4386 read_lock(&bond->lock);
4387
4388 if (!BOND_IS_OK(bond)) {
4389 goto out;
4390 }
4391
169a3e66 4392 slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
1da177e4
LT
4393
4394 bond_for_each_slave(bond, slave, i) {
4395 slave_no--;
4396 if (slave_no < 0) {
4397 break;
4398 }
4399 }
4400
4401 start_at = slave;
4402
4403 bond_for_each_slave_from(bond, slave, i, start_at) {
4404 if (IS_UP(slave->dev) &&
4405 (slave->link == BOND_LINK_UP) &&
4406 (slave->state == BOND_STATE_ACTIVE)) {
4407 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4408 break;
4409 }
4410 }
4411
4412out:
4413 if (res) {
4414 /* no suitable interface, frame not sent */
4415 dev_kfree_skb(skb);
4416 }
4417 read_unlock(&bond->lock);
4418 return 0;
4419}
4420
4421/*
4422 * in broadcast mode, we send everything to all usable interfaces.
4423 */
4424static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4425{
4426 struct bonding *bond = bond_dev->priv;
4427 struct slave *slave, *start_at;
4428 struct net_device *tx_dev = NULL;
4429 int i;
4430 int res = 1;
4431
4432 read_lock(&bond->lock);
4433
4434 if (!BOND_IS_OK(bond)) {
4435 goto out;
4436 }
4437
4438 read_lock(&bond->curr_slave_lock);
4439 start_at = bond->curr_active_slave;
4440 read_unlock(&bond->curr_slave_lock);
4441
4442 if (!start_at) {
4443 goto out;
4444 }
4445
4446 bond_for_each_slave_from(bond, slave, i, start_at) {
4447 if (IS_UP(slave->dev) &&
4448 (slave->link == BOND_LINK_UP) &&
4449 (slave->state == BOND_STATE_ACTIVE)) {
4450 if (tx_dev) {
4451 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4452 if (!skb2) {
4453 printk(KERN_ERR DRV_NAME
4e0952c7
MW
4454 ": %s: Error: bond_xmit_broadcast(): "
4455 "skb_clone() failed\n",
4456 bond_dev->name);
1da177e4
LT
4457 continue;
4458 }
4459
4460 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4461 if (res) {
4462 dev_kfree_skb(skb2);
4463 continue;
4464 }
4465 }
4466 tx_dev = slave->dev;
4467 }
4468 }
4469
4470 if (tx_dev) {
4471 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4472 }
4473
4474out:
4475 if (res) {
4476 /* no suitable interface, frame not sent */
4477 dev_kfree_skb(skb);
4478 }
4479 /* frame sent to all suitable interfaces */
4480 read_unlock(&bond->lock);
4481 return 0;
4482}
4483
4484/*------------------------- Device initialization ---------------------------*/
4485
4486/*
4487 * set bond mode specific net device operations
4488 */
a77b5325 4489void bond_set_mode_ops(struct bonding *bond, int mode)
1da177e4 4490{
169a3e66
JV
4491 struct net_device *bond_dev = bond->dev;
4492
1da177e4
LT
4493 switch (mode) {
4494 case BOND_MODE_ROUNDROBIN:
4495 bond_dev->hard_start_xmit = bond_xmit_roundrobin;
4496 break;
4497 case BOND_MODE_ACTIVEBACKUP:
4498 bond_dev->hard_start_xmit = bond_xmit_activebackup;
4499 break;
4500 case BOND_MODE_XOR:
4501 bond_dev->hard_start_xmit = bond_xmit_xor;
169a3e66
JV
4502 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
4503 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4504 else
4505 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
1da177e4
LT
4506 break;
4507 case BOND_MODE_BROADCAST:
4508 bond_dev->hard_start_xmit = bond_xmit_broadcast;
4509 break;
4510 case BOND_MODE_8023AD:
4511 bond_dev->hard_start_xmit = bond_3ad_xmit_xor;
169a3e66
JV
4512 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
4513 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4514 else
4515 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
1da177e4
LT
4516 break;
4517 case BOND_MODE_TLB:
4518 case BOND_MODE_ALB:
4519 bond_dev->hard_start_xmit = bond_alb_xmit;
4520 bond_dev->set_mac_address = bond_alb_set_mac_address;
4521 break;
4522 default:
4523 /* Should never happen, mode already checked */
4524 printk(KERN_ERR DRV_NAME
4e0952c7
MW
4525 ": %s: Error: Unknown bonding mode %d\n",
4526 bond_dev->name,
1da177e4
LT
4527 mode);
4528 break;
4529 }
4530}
4531
217df670
JV
4532static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4533 struct ethtool_drvinfo *drvinfo)
4534{
4535 strncpy(drvinfo->driver, DRV_NAME, 32);
4536 strncpy(drvinfo->version, DRV_VERSION, 32);
4537 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4538}
4539
8531c5ff
AK
4540static struct ethtool_ops bond_ethtool_ops = {
4541 .get_tx_csum = ethtool_op_get_tx_csum,
4542 .get_sg = ethtool_op_get_sg,
217df670 4543 .get_drvinfo = bond_ethtool_get_drvinfo,
8531c5ff
AK
4544};
4545
1da177e4
LT
4546/*
4547 * Does not allocate but creates a /proc entry.
4548 * Allowed to fail.
4549 */
3c535952 4550static int bond_init(struct net_device *bond_dev, struct bond_params *params)
1da177e4
LT
4551{
4552 struct bonding *bond = bond_dev->priv;
4553
4554 dprintk("Begin bond_init for %s\n", bond_dev->name);
4555
4556 /* initialize rwlocks */
4557 rwlock_init(&bond->lock);
4558 rwlock_init(&bond->curr_slave_lock);
4559
4560 bond->params = *params; /* copy params struct */
4561
4562 /* Initialize pointers */
4563 bond->first_slave = NULL;
4564 bond->curr_active_slave = NULL;
4565 bond->current_arp_slave = NULL;
4566 bond->primary_slave = NULL;
4567 bond->dev = bond_dev;
4568 INIT_LIST_HEAD(&bond->vlan_list);
4569
4570 /* Initialize the device entry points */
4571 bond_dev->open = bond_open;
4572 bond_dev->stop = bond_close;
4573 bond_dev->get_stats = bond_get_stats;
4574 bond_dev->do_ioctl = bond_do_ioctl;
8531c5ff 4575 bond_dev->ethtool_ops = &bond_ethtool_ops;
1da177e4
LT
4576 bond_dev->set_multicast_list = bond_set_multicast_list;
4577 bond_dev->change_mtu = bond_change_mtu;
4578 bond_dev->set_mac_address = bond_set_mac_address;
4579
169a3e66 4580 bond_set_mode_ops(bond, bond->params.mode);
1da177e4
LT
4581
4582 bond_dev->destructor = free_netdev;
4583
4584 /* Initialize the device options */
4585 bond_dev->tx_queue_len = 0;
4586 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
4587
4588 /* At first, we block adding VLANs. That's the only way to
4589 * prevent problems that occur when adding VLANs over an
4590 * empty bond. The block will be removed once non-challenged
4591 * slaves are enslaved.
4592 */
4593 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4594
4595 /* don't acquire bond device's xmit_lock when
4596 * transmitting */
4597 bond_dev->features |= NETIF_F_LLTX;
4598
4599 /* By default, we declare the bond to be fully
4600 * VLAN hardware accelerated capable. Special
4601 * care is taken in the various xmit functions
4602 * when there are slaves that are not hw accel
4603 * capable
4604 */
4605 bond_dev->vlan_rx_register = bond_vlan_rx_register;
4606 bond_dev->vlan_rx_add_vid = bond_vlan_rx_add_vid;
4607 bond_dev->vlan_rx_kill_vid = bond_vlan_rx_kill_vid;
4608 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4609 NETIF_F_HW_VLAN_RX |
4610 NETIF_F_HW_VLAN_FILTER);
4611
4612#ifdef CONFIG_PROC_FS
4613 bond_create_proc_entry(bond);
4614#endif
4615
4616 list_add_tail(&bond->bond_list, &bond_dev_list);
4617
4618 return 0;
4619}
4620
4621/* De-initialize device specific data.
4622 * Caller must hold rtnl_lock.
4623 */
a77b5325 4624void bond_deinit(struct net_device *bond_dev)
1da177e4
LT
4625{
4626 struct bonding *bond = bond_dev->priv;
4627
4628 list_del(&bond->bond_list);
4629
4630#ifdef CONFIG_PROC_FS
4631 bond_remove_proc_entry(bond);
4632#endif
4633}
4634
4635/* Unregister and free all bond devices.
4636 * Caller must hold rtnl_lock.
4637 */
4638static void bond_free_all(void)
4639{
4640 struct bonding *bond, *nxt;
4641
4642 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4643 struct net_device *bond_dev = bond->dev;
4644
4645 unregister_netdevice(bond_dev);
4646 bond_deinit(bond_dev);
4647 }
4648
4649#ifdef CONFIG_PROC_FS
4650 bond_destroy_proc_dir();
4651#endif
4652}
4653
4654/*------------------------- Module initialization ---------------------------*/
4655
4656/*
4657 * Convert string input module parms. Accept either the
4658 * number of the mode or its string name.
4659 */
a77b5325 4660int bond_parse_parm(char *mode_arg, struct bond_parm_tbl *tbl)
1da177e4
LT
4661{
4662 int i;
4663
4664 for (i = 0; tbl[i].modename; i++) {
4665 if ((isdigit(*mode_arg) &&
4666 tbl[i].mode == simple_strtol(mode_arg, NULL, 0)) ||
4667 (strncmp(mode_arg, tbl[i].modename,
4668 strlen(tbl[i].modename)) == 0)) {
4669 return tbl[i].mode;
4670 }
4671 }
4672
4673 return -1;
4674}
4675
4676static int bond_check_params(struct bond_params *params)
4677{
4678 /*
4679 * Convert string parameters.
4680 */
4681 if (mode) {
4682 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4683 if (bond_mode == -1) {
4684 printk(KERN_ERR DRV_NAME
4685 ": Error: Invalid bonding mode \"%s\"\n",
4686 mode == NULL ? "NULL" : mode);
4687 return -EINVAL;
4688 }
4689 }
4690
169a3e66
JV
4691 if (xmit_hash_policy) {
4692 if ((bond_mode != BOND_MODE_XOR) &&
4693 (bond_mode != BOND_MODE_8023AD)) {
4694 printk(KERN_INFO DRV_NAME
4695 ": xor_mode param is irrelevant in mode %s\n",
4696 bond_mode_name(bond_mode));
4697 } else {
4698 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4699 xmit_hashtype_tbl);
4700 if (xmit_hashtype == -1) {
4701 printk(KERN_ERR DRV_NAME
4702 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4703 xmit_hash_policy == NULL ? "NULL" :
4704 xmit_hash_policy);
4705 return -EINVAL;
4706 }
4707 }
4708 }
4709
1da177e4
LT
4710 if (lacp_rate) {
4711 if (bond_mode != BOND_MODE_8023AD) {
4712 printk(KERN_INFO DRV_NAME
4713 ": lacp_rate param is irrelevant in mode %s\n",
4714 bond_mode_name(bond_mode));
4715 } else {
4716 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4717 if (lacp_fast == -1) {
4718 printk(KERN_ERR DRV_NAME
4719 ": Error: Invalid lacp rate \"%s\"\n",
4720 lacp_rate == NULL ? "NULL" : lacp_rate);
4721 return -EINVAL;
4722 }
4723 }
4724 }
4725
4726 if (max_bonds < 1 || max_bonds > INT_MAX) {
4727 printk(KERN_WARNING DRV_NAME
4728 ": Warning: max_bonds (%d) not in range %d-%d, so it "
4e0952c7 4729 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
1da177e4
LT
4730 max_bonds, 1, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4731 max_bonds = BOND_DEFAULT_MAX_BONDS;
4732 }
4733
4734 if (miimon < 0) {
4735 printk(KERN_WARNING DRV_NAME
4736 ": Warning: miimon module parameter (%d), "
4737 "not in range 0-%d, so it was reset to %d\n",
4738 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4739 miimon = BOND_LINK_MON_INTERV;
4740 }
4741
4742 if (updelay < 0) {
4743 printk(KERN_WARNING DRV_NAME
4744 ": Warning: updelay module parameter (%d), "
4745 "not in range 0-%d, so it was reset to 0\n",
4746 updelay, INT_MAX);
4747 updelay = 0;
4748 }
4749
4750 if (downdelay < 0) {
4751 printk(KERN_WARNING DRV_NAME
4752 ": Warning: downdelay module parameter (%d), "
4753 "not in range 0-%d, so it was reset to 0\n",
4754 downdelay, INT_MAX);
4755 downdelay = 0;
4756 }
4757
4758 if ((use_carrier != 0) && (use_carrier != 1)) {
4759 printk(KERN_WARNING DRV_NAME
4760 ": Warning: use_carrier module parameter (%d), "
4761 "not of valid value (0/1), so it was set to 1\n",
4762 use_carrier);
4763 use_carrier = 1;
4764 }
4765
4766 /* reset values for 802.3ad */
4767 if (bond_mode == BOND_MODE_8023AD) {
4768 if (!miimon) {
4769 printk(KERN_WARNING DRV_NAME
4770 ": Warning: miimon must be specified, "
4771 "otherwise bonding will not detect link "
4772 "failure, speed and duplex which are "
4773 "essential for 802.3ad operation\n");
4774 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4775 miimon = 100;
4776 }
4777 }
4778
4779 /* reset values for TLB/ALB */
4780 if ((bond_mode == BOND_MODE_TLB) ||
4781 (bond_mode == BOND_MODE_ALB)) {
4782 if (!miimon) {
4783 printk(KERN_WARNING DRV_NAME
4784 ": Warning: miimon must be specified, "
4785 "otherwise bonding will not detect link "
4786 "failure and link speed which are essential "
4787 "for TLB/ALB load balancing\n");
4788 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4789 miimon = 100;
4790 }
4791 }
4792
4793 if (bond_mode == BOND_MODE_ALB) {
4794 printk(KERN_NOTICE DRV_NAME
4795 ": In ALB mode you might experience client "
4796 "disconnections upon reconnection of a link if the "
4797 "bonding module updelay parameter (%d msec) is "
4798 "incompatible with the forwarding delay time of the "
4799 "switch\n",
4800 updelay);
4801 }
4802
4803 if (!miimon) {
4804 if (updelay || downdelay) {
4805 /* just warn the user the up/down delay will have
4806 * no effect since miimon is zero...
4807 */
4808 printk(KERN_WARNING DRV_NAME
4809 ": Warning: miimon module parameter not set "
4810 "and updelay (%d) or downdelay (%d) module "
4811 "parameter is set; updelay and downdelay have "
4812 "no effect unless miimon is set\n",
4813 updelay, downdelay);
4814 }
4815 } else {
4816 /* don't allow arp monitoring */
4817 if (arp_interval) {
4818 printk(KERN_WARNING DRV_NAME
4819 ": Warning: miimon (%d) and arp_interval (%d) "
4820 "can't be used simultaneously, disabling ARP "
4821 "monitoring\n",
4822 miimon, arp_interval);
4823 arp_interval = 0;
4824 }
4825
4826 if ((updelay % miimon) != 0) {
4827 printk(KERN_WARNING DRV_NAME
4828 ": Warning: updelay (%d) is not a multiple "
4829 "of miimon (%d), updelay rounded to %d ms\n",
4830 updelay, miimon, (updelay / miimon) * miimon);
4831 }
4832
4833 updelay /= miimon;
4834
4835 if ((downdelay % miimon) != 0) {
4836 printk(KERN_WARNING DRV_NAME
4837 ": Warning: downdelay (%d) is not a multiple "
4838 "of miimon (%d), downdelay rounded to %d ms\n",
4839 downdelay, miimon,
4840 (downdelay / miimon) * miimon);
4841 }
4842
4843 downdelay /= miimon;
4844 }
4845
4846 if (arp_interval < 0) {
4847 printk(KERN_WARNING DRV_NAME
4848 ": Warning: arp_interval module parameter (%d) "
4849 ", not in range 0-%d, so it was reset to %d\n",
4850 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4851 arp_interval = BOND_LINK_ARP_INTERV;
4852 }
4853
4854 for (arp_ip_count = 0;
4855 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4856 arp_ip_count++) {
4857 /* not complete check, but should be good enough to
4858 catch mistakes */
4859 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4860 printk(KERN_WARNING DRV_NAME
4861 ": Warning: bad arp_ip_target module parameter "
4862 "(%s), ARP monitoring will not be performed\n",
4863 arp_ip_target[arp_ip_count]);
4864 arp_interval = 0;
4865 } else {
4866 u32 ip = in_aton(arp_ip_target[arp_ip_count]);
4867 arp_target[arp_ip_count] = ip;
4868 }
4869 }
4870
4871 if (arp_interval && !arp_ip_count) {
4872 /* don't allow arping if no arp_ip_target given... */
4873 printk(KERN_WARNING DRV_NAME
4874 ": Warning: arp_interval module parameter (%d) "
4875 "specified without providing an arp_ip_target "
4876 "parameter, arp_interval was reset to 0\n",
4877 arp_interval);
4878 arp_interval = 0;
4879 }
4880
4881 if (miimon) {
4882 printk(KERN_INFO DRV_NAME
4883 ": MII link monitoring set to %d ms\n",
4884 miimon);
4885 } else if (arp_interval) {
4886 int i;
4887
4888 printk(KERN_INFO DRV_NAME
4889 ": ARP monitoring set to %d ms with %d target(s):",
4890 arp_interval, arp_ip_count);
4891
4892 for (i = 0; i < arp_ip_count; i++)
4893 printk (" %s", arp_ip_target[i]);
4894
4895 printk("\n");
4896
4897 } else {
4898 /* miimon and arp_interval not set, we need one so things
4899 * work as expected, see bonding.txt for details
4900 */
4901 printk(KERN_WARNING DRV_NAME
4902 ": Warning: either miimon or arp_interval and "
4903 "arp_ip_target module parameters must be specified, "
4904 "otherwise bonding will not detect link failures! see "
4905 "bonding.txt for details.\n");
4906 }
4907
4908 if (primary && !USES_PRIMARY(bond_mode)) {
4909 /* currently, using a primary only makes sense
4910 * in active backup, TLB or ALB modes
4911 */
4912 printk(KERN_WARNING DRV_NAME
4913 ": Warning: %s primary device specified but has no "
4914 "effect in %s mode\n",
4915 primary, bond_mode_name(bond_mode));
4916 primary = NULL;
4917 }
4918
4919 /* fill params struct with the proper values */
4920 params->mode = bond_mode;
169a3e66 4921 params->xmit_policy = xmit_hashtype;
1da177e4
LT
4922 params->miimon = miimon;
4923 params->arp_interval = arp_interval;
4924 params->updelay = updelay;
4925 params->downdelay = downdelay;
4926 params->use_carrier = use_carrier;
4927 params->lacp_fast = lacp_fast;
4928 params->primary[0] = 0;
4929
4930 if (primary) {
4931 strncpy(params->primary, primary, IFNAMSIZ);
4932 params->primary[IFNAMSIZ - 1] = 0;
4933 }
4934
4935 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4936
4937 return 0;
4938}
4939
dfe60397
MW
4940/* Create a new bond based on the specified name and bonding parameters.
4941 * Caller must NOT hold rtnl_lock; we need to release it here before we
4942 * set up our sysfs entries.
4943 */
4944int bond_create(char *name, struct bond_params *params, struct bonding **newbond)
4945{
4946 struct net_device *bond_dev;
4947 int res;
4948
4949 rtnl_lock();
4950 bond_dev = alloc_netdev(sizeof(struct bonding), name, ether_setup);
4951 if (!bond_dev) {
4952 printk(KERN_ERR DRV_NAME
4953 ": %s: eek! can't alloc netdev!\n",
4954 name);
4955 res = -ENOMEM;
4956 goto out_rtnl;
4957 }
4958
4959 /* bond_init() must be called after dev_alloc_name() (for the
4960 * /proc files), but before register_netdevice(), because we
4961 * need to set function pointers.
4962 */
4963
4964 res = bond_init(bond_dev, params);
4965 if (res < 0) {
4966 goto out_netdev;
4967 }
4968
4969 SET_MODULE_OWNER(bond_dev);
4970
4971 res = register_netdevice(bond_dev);
4972 if (res < 0) {
4973 goto out_bond;
4974 }
4975 if (newbond)
4976 *newbond = bond_dev->priv;
4977
4978 rtnl_unlock(); /* allows sysfs registration of net device */
b76cdba9 4979 res = bond_create_sysfs_entry(bond_dev->priv);
dfe60397
MW
4980 goto done;
4981out_bond:
4982 bond_deinit(bond_dev);
4983out_netdev:
4984 free_netdev(bond_dev);
4985out_rtnl:
4986 rtnl_unlock();
4987done:
4988 return res;
4989}
4990
1da177e4
LT
4991static int __init bonding_init(void)
4992{
1da177e4
LT
4993 int i;
4994 int res;
dfe60397 4995 char new_bond_name[8]; /* Enough room for 999 bonds at init. */
1da177e4
LT
4996
4997 printk(KERN_INFO "%s", version);
4998
dfe60397 4999 res = bond_check_params(&bonding_defaults);
1da177e4 5000 if (res) {
dfe60397 5001 goto out;
1da177e4
LT
5002 }
5003
1da177e4
LT
5004#ifdef CONFIG_PROC_FS
5005 bond_create_proc_dir();
5006#endif
1da177e4 5007 for (i = 0; i < max_bonds; i++) {
dfe60397
MW
5008 sprintf(new_bond_name, "bond%d",i);
5009 res = bond_create(new_bond_name,&bonding_defaults, NULL);
5010 if (res)
5011 goto err;
1da177e4
LT
5012 }
5013
b76cdba9
MW
5014 res = bond_create_sysfs();
5015 if (res)
5016 goto err;
5017
1da177e4 5018 register_netdevice_notifier(&bond_netdev_notifier);
c3ade5ca 5019 register_inetaddr_notifier(&bond_inetaddr_notifier);
1da177e4 5020
dfe60397
MW
5021 goto out;
5022err:
40abc270 5023 rtnl_lock();
1da177e4 5024 bond_free_all();
b76cdba9 5025 bond_destroy_sysfs();
1da177e4 5026 rtnl_unlock();
dfe60397 5027out:
1da177e4 5028 return res;
dfe60397 5029
1da177e4
LT
5030}
5031
5032static void __exit bonding_exit(void)
5033{
5034 unregister_netdevice_notifier(&bond_netdev_notifier);
c3ade5ca 5035 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
1da177e4
LT
5036
5037 rtnl_lock();
5038 bond_free_all();
b76cdba9 5039 bond_destroy_sysfs();
1da177e4
LT
5040 rtnl_unlock();
5041}
5042
5043module_init(bonding_init);
5044module_exit(bonding_exit);
5045MODULE_LICENSE("GPL");
5046MODULE_VERSION(DRV_VERSION);
5047MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5048MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
5049MODULE_SUPPORTED_DEVICE("most ethernet devices");
5050
5051/*
5052 * Local variables:
5053 * c-indent-level: 8
5054 * c-basic-offset: 8
5055 * tab-width: 8
5056 * End:
5057 */
5058