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