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