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