blob: e5fab4be3982b55be99f24f6d168e482636dc9b4 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 */
33
34//#define BONDING_DEBUG 1
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/kernel.h>
37#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/types.h>
39#include <linux/fcntl.h>
40#include <linux/interrupt.h>
41#include <linux/ptrace.h>
42#include <linux/ioport.h>
43#include <linux/in.h>
Jay Vosburgh169a3e62005-06-26 17:54:11 -040044#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/ip.h>
Jay Vosburgh169a3e62005-06-26 17:54:11 -040046#include <linux/tcp.h>
47#include <linux/udp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/slab.h>
49#include <linux/string.h>
50#include <linux/init.h>
51#include <linux/timer.h>
52#include <linux/socket.h>
53#include <linux/ctype.h>
54#include <linux/inet.h>
55#include <linux/bitops.h>
56#include <asm/system.h>
57#include <asm/io.h>
58#include <asm/dma.h>
59#include <asm/uaccess.h>
60#include <linux/errno.h>
61#include <linux/netdevice.h>
62#include <linux/inetdevice.h>
Jay Vosburgha816c7c2007-02-28 17:03:37 -080063#include <linux/igmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/etherdevice.h>
65#include <linux/skbuff.h>
66#include <net/sock.h>
67#include <linux/rtnetlink.h>
68#include <linux/proc_fs.h>
69#include <linux/seq_file.h>
70#include <linux/smp.h>
71#include <linux/if_ether.h>
72#include <net/arp.h>
73#include <linux/mii.h>
74#include <linux/ethtool.h>
75#include <linux/if_vlan.h>
76#include <linux/if_bonding.h>
Jay Vosburghc3ade5c2005-06-26 17:52:20 -040077#include <net/route.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020078#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include "bonding.h"
80#include "bond_3ad.h"
81#include "bond_alb.h"
82
83/*---------------------------- Module parameters ----------------------------*/
84
85/* monitor all links that often (in milliseconds). <=0 disables monitoring */
86#define BOND_LINK_MON_INTERV 0
87#define BOND_LINK_ARP_INTERV 0
88
89static int max_bonds = BOND_DEFAULT_MAX_BONDS;
90static int miimon = BOND_LINK_MON_INTERV;
91static int updelay = 0;
92static int downdelay = 0;
93static int use_carrier = 1;
94static char *mode = NULL;
95static char *primary = NULL;
96static char *lacp_rate = NULL;
Jay Vosburgh169a3e62005-06-26 17:54:11 -040097static char *xmit_hash_policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static int arp_interval = BOND_LINK_ARP_INTERV;
99static char *arp_ip_target[BOND_MAX_ARP_TARGETS] = { NULL, };
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700100static char *arp_validate = NULL;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700101static int fail_over_mac = 0;
Mitch Williams12479f92005-11-09 10:35:44 -0800102struct bond_params bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104module_param(max_bonds, int, 0);
105MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
106module_param(miimon, int, 0);
107MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
108module_param(updelay, int, 0);
109MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
110module_param(downdelay, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800111MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
112 "in milliseconds");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113module_param(use_carrier, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800114MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
115 "0 for off, 1 for on (default)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116module_param(mode, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800117MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
118 "1 for active-backup, 2 for balance-xor, "
119 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
120 "6 for balance-alb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121module_param(primary, charp, 0);
122MODULE_PARM_DESC(primary, "Primary network device to use");
123module_param(lacp_rate, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800124MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
125 "(slow/fast)");
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400126module_param(xmit_hash_policy, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800127MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
128 ", 1 for layer 3+4");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129module_param(arp_interval, int, 0);
130MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
131module_param_array(arp_ip_target, charp, NULL, 0);
132MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700133module_param(arp_validate, charp, 0);
134MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
Jay Vosburghdd957c52007-10-09 19:57:24 -0700135module_param(fail_over_mac, int, 0);
136MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to the same MAC. 0 of off (default), 1 for on.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/*----------------------------- Global variables ----------------------------*/
139
Arjan van de Venf71e1302006-03-03 21:33:57 -0500140static const char * const version =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
142
Mitch Williams12479f92005-11-09 10:35:44 -0800143LIST_HEAD(bond_dev_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145#ifdef CONFIG_PROC_FS
146static struct proc_dir_entry *bond_proc_dir = NULL;
147#endif
148
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800149extern struct rw_semaphore bonding_rwsem;
Al Virod3bb52b2007-08-22 20:06:58 -0400150static __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static int arp_ip_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static int bond_mode = BOND_MODE_ROUNDROBIN;
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400153static int xmit_hashtype= BOND_XMIT_POLICY_LAYER2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static int lacp_fast = 0;
Jay Vosburgh217df672005-09-26 16:11:50 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Mitch Williams12479f92005-11-09 10:35:44 -0800157struct bond_parm_tbl bond_lacp_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{ "slow", AD_LACP_SLOW},
159{ "fast", AD_LACP_FAST},
160{ NULL, -1},
161};
162
Mitch Williams12479f92005-11-09 10:35:44 -0800163struct bond_parm_tbl bond_mode_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{ "balance-rr", BOND_MODE_ROUNDROBIN},
165{ "active-backup", BOND_MODE_ACTIVEBACKUP},
166{ "balance-xor", BOND_MODE_XOR},
167{ "broadcast", BOND_MODE_BROADCAST},
168{ "802.3ad", BOND_MODE_8023AD},
169{ "balance-tlb", BOND_MODE_TLB},
170{ "balance-alb", BOND_MODE_ALB},
171{ NULL, -1},
172};
173
Mitch Williams12479f92005-11-09 10:35:44 -0800174struct bond_parm_tbl xmit_hashtype_tbl[] = {
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400175{ "layer2", BOND_XMIT_POLICY_LAYER2},
176{ "layer3+4", BOND_XMIT_POLICY_LAYER34},
177{ NULL, -1},
178};
179
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700180struct bond_parm_tbl arp_validate_tbl[] = {
181{ "none", BOND_ARP_VALIDATE_NONE},
182{ "active", BOND_ARP_VALIDATE_ACTIVE},
183{ "backup", BOND_ARP_VALIDATE_BACKUP},
184{ "all", BOND_ARP_VALIDATE_ALL},
185{ NULL, -1},
186};
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/*-------------------------- Forward declarations ---------------------------*/
189
Jay Vosburghc3ade5c2005-06-26 17:52:20 -0400190static void bond_send_gratuitous_arp(struct bonding *bond);
Adrian Bunkc50b85d2007-10-24 18:23:17 +0200191static void bond_deinit(struct net_device *bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193/*---------------------------- General routines -----------------------------*/
194
Adrian Bunk4ad072c2007-07-09 11:51:12 -0700195static const char *bond_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 switch (mode) {
198 case BOND_MODE_ROUNDROBIN :
199 return "load balancing (round-robin)";
200 case BOND_MODE_ACTIVEBACKUP :
201 return "fault-tolerance (active-backup)";
202 case BOND_MODE_XOR :
203 return "load balancing (xor)";
204 case BOND_MODE_BROADCAST :
205 return "fault-tolerance (broadcast)";
206 case BOND_MODE_8023AD:
207 return "IEEE 802.3ad Dynamic link aggregation";
208 case BOND_MODE_TLB:
209 return "transmit load balancing";
210 case BOND_MODE_ALB:
211 return "adaptive load balancing";
212 default:
213 return "unknown";
214 }
215}
216
217/*---------------------------------- VLAN -----------------------------------*/
218
219/**
220 * bond_add_vlan - add a new vlan id on bond
221 * @bond: bond that got the notification
222 * @vlan_id: the vlan id to add
223 *
224 * Returns -ENOMEM if allocation failed.
225 */
226static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
227{
228 struct vlan_entry *vlan;
229
230 dprintk("bond: %s, vlan id %d\n",
231 (bond ? bond->dev->name: "None"), vlan_id);
232
233 vlan = kmalloc(sizeof(struct vlan_entry), GFP_KERNEL);
234 if (!vlan) {
235 return -ENOMEM;
236 }
237
238 INIT_LIST_HEAD(&vlan->vlan_list);
239 vlan->vlan_id = vlan_id;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -0400240 vlan->vlan_ip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 write_lock_bh(&bond->lock);
243
244 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
245
246 write_unlock_bh(&bond->lock);
247
248 dprintk("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
249
250 return 0;
251}
252
253/**
254 * bond_del_vlan - delete a vlan id from bond
255 * @bond: bond that got the notification
256 * @vlan_id: the vlan id to delete
257 *
258 * returns -ENODEV if @vlan_id was not found in @bond.
259 */
260static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
261{
262 struct vlan_entry *vlan, *next;
263 int res = -ENODEV;
264
265 dprintk("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
266
267 write_lock_bh(&bond->lock);
268
269 list_for_each_entry_safe(vlan, next, &bond->vlan_list, vlan_list) {
270 if (vlan->vlan_id == vlan_id) {
271 list_del(&vlan->vlan_list);
272
273 if ((bond->params.mode == BOND_MODE_TLB) ||
274 (bond->params.mode == BOND_MODE_ALB)) {
275 bond_alb_clear_vlan(bond, vlan_id);
276 }
277
278 dprintk("removed VLAN ID %d from bond %s\n", vlan_id,
279 bond->dev->name);
280
281 kfree(vlan);
282
283 if (list_empty(&bond->vlan_list) &&
284 (bond->slave_cnt == 0)) {
285 /* Last VLAN removed and no slaves, so
286 * restore block on adding VLANs. This will
287 * be removed once new slaves that are not
288 * VLAN challenged will be added.
289 */
290 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
291 }
292
293 res = 0;
294 goto out;
295 }
296 }
297
298 dprintk("couldn't find VLAN ID %d in bond %s\n", vlan_id,
299 bond->dev->name);
300
301out:
302 write_unlock_bh(&bond->lock);
303 return res;
304}
305
306/**
307 * bond_has_challenged_slaves
308 * @bond: the bond we're working on
309 *
310 * Searches the slave list. Returns 1 if a vlan challenged slave
311 * was found, 0 otherwise.
312 *
313 * Assumes bond->lock is held.
314 */
315static int bond_has_challenged_slaves(struct bonding *bond)
316{
317 struct slave *slave;
318 int i;
319
320 bond_for_each_slave(bond, slave, i) {
321 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
322 dprintk("found VLAN challenged slave - %s\n",
323 slave->dev->name);
324 return 1;
325 }
326 }
327
328 dprintk("no VLAN challenged slaves found\n");
329 return 0;
330}
331
332/**
333 * bond_next_vlan - safely skip to the next item in the vlans list.
334 * @bond: the bond we're working on
335 * @curr: item we're advancing from
336 *
337 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
338 * or @curr->next otherwise (even if it is @curr itself again).
339 *
340 * Caller must hold bond->lock
341 */
342struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
343{
344 struct vlan_entry *next, *last;
345
346 if (list_empty(&bond->vlan_list)) {
347 return NULL;
348 }
349
350 if (!curr) {
351 next = list_entry(bond->vlan_list.next,
352 struct vlan_entry, vlan_list);
353 } else {
354 last = list_entry(bond->vlan_list.prev,
355 struct vlan_entry, vlan_list);
356 if (last == curr) {
357 next = list_entry(bond->vlan_list.next,
358 struct vlan_entry, vlan_list);
359 } else {
360 next = list_entry(curr->vlan_list.next,
361 struct vlan_entry, vlan_list);
362 }
363 }
364
365 return next;
366}
367
368/**
369 * bond_dev_queue_xmit - Prepare skb for xmit.
370 *
371 * @bond: bond device that got this skb for tx.
372 * @skb: hw accel VLAN tagged skb to transmit
373 * @slave_dev: slave that is supposed to xmit this skbuff
374 *
375 * When the bond gets an skb to transmit that is
376 * already hardware accelerated VLAN tagged, and it
377 * needs to relay this skb to a slave that is not
378 * hw accel capable, the skb needs to be "unaccelerated",
379 * i.e. strip the hwaccel tag and re-insert it as part
380 * of the payload.
381 */
382int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
383{
384 unsigned short vlan_id;
385
386 if (!list_empty(&bond->vlan_list) &&
387 !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
388 vlan_get_tag(skb, &vlan_id) == 0) {
389 skb->dev = slave_dev;
390 skb = vlan_put_tag(skb, vlan_id);
391 if (!skb) {
392 /* vlan_put_tag() frees the skb in case of error,
393 * so return success here so the calling functions
394 * won't attempt to free is again.
395 */
396 return 0;
397 }
398 } else {
399 skb->dev = slave_dev;
400 }
401
402 skb->priority = 1;
403 dev_queue_xmit(skb);
404
405 return 0;
406}
407
408/*
409 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
410 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
411 * lock because:
412 * a. This operation is performed in IOCTL context,
413 * b. The operation is protected by the RTNL semaphore in the 8021q code,
414 * c. Holding a lock with BH disabled while directly calling a base driver
415 * entry point is generally a BAD idea.
416 *
417 * The design of synchronization/protection for this operation in the 8021q
418 * module is good for one or more VLAN devices over a single physical device
419 * and cannot be extended for a teaming solution like bonding, so there is a
420 * potential race condition here where a net device from the vlan group might
421 * be referenced (either by a base driver or the 8021q code) while it is being
422 * removed from the system. However, it turns out we're not making matters
423 * worse, and if it works for regular VLAN usage it will work here too.
424*/
425
426/**
427 * bond_vlan_rx_register - Propagates registration to slaves
428 * @bond_dev: bonding net device that got called
429 * @grp: vlan group being registered
430 */
431static void bond_vlan_rx_register(struct net_device *bond_dev, struct vlan_group *grp)
432{
433 struct bonding *bond = bond_dev->priv;
434 struct slave *slave;
435 int i;
436
437 bond->vlgrp = grp;
438
439 bond_for_each_slave(bond, slave, i) {
440 struct net_device *slave_dev = slave->dev;
441
442 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
443 slave_dev->vlan_rx_register) {
444 slave_dev->vlan_rx_register(slave_dev, grp);
445 }
446 }
447}
448
449/**
450 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
451 * @bond_dev: bonding net device that got called
452 * @vid: vlan id being added
453 */
454static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
455{
456 struct bonding *bond = bond_dev->priv;
457 struct slave *slave;
458 int i, res;
459
460 bond_for_each_slave(bond, slave, i) {
461 struct net_device *slave_dev = slave->dev;
462
463 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
464 slave_dev->vlan_rx_add_vid) {
465 slave_dev->vlan_rx_add_vid(slave_dev, vid);
466 }
467 }
468
469 res = bond_add_vlan(bond, vid);
470 if (res) {
471 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800472 ": %s: Error: Failed to add vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 bond_dev->name, vid);
474 }
475}
476
477/**
478 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
479 * @bond_dev: bonding net device that got called
480 * @vid: vlan id being removed
481 */
482static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
483{
484 struct bonding *bond = bond_dev->priv;
485 struct slave *slave;
486 struct net_device *vlan_dev;
487 int i, res;
488
489 bond_for_each_slave(bond, slave, i) {
490 struct net_device *slave_dev = slave->dev;
491
492 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
493 slave_dev->vlan_rx_kill_vid) {
494 /* Save and then restore vlan_dev in the grp array,
495 * since the slave's driver might clear it.
496 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800497 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 slave_dev->vlan_rx_kill_vid(slave_dev, vid);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800499 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501 }
502
503 res = bond_del_vlan(bond, vid);
504 if (res) {
505 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800506 ": %s: Error: Failed to remove vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 bond_dev->name, vid);
508 }
509}
510
511static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
512{
513 struct vlan_entry *vlan;
514
515 write_lock_bh(&bond->lock);
516
517 if (list_empty(&bond->vlan_list)) {
518 goto out;
519 }
520
521 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
522 slave_dev->vlan_rx_register) {
523 slave_dev->vlan_rx_register(slave_dev, bond->vlgrp);
524 }
525
526 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
527 !(slave_dev->vlan_rx_add_vid)) {
528 goto out;
529 }
530
531 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
532 slave_dev->vlan_rx_add_vid(slave_dev, vlan->vlan_id);
533 }
534
535out:
536 write_unlock_bh(&bond->lock);
537}
538
539static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *slave_dev)
540{
541 struct vlan_entry *vlan;
542 struct net_device *vlan_dev;
543
544 write_lock_bh(&bond->lock);
545
546 if (list_empty(&bond->vlan_list)) {
547 goto out;
548 }
549
550 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
551 !(slave_dev->vlan_rx_kill_vid)) {
552 goto unreg;
553 }
554
555 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
556 /* Save and then restore vlan_dev in the grp array,
557 * since the slave's driver might clear it.
558 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800559 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 slave_dev->vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800561 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563
564unreg:
565 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
566 slave_dev->vlan_rx_register) {
567 slave_dev->vlan_rx_register(slave_dev, NULL);
568 }
569
570out:
571 write_unlock_bh(&bond->lock);
572}
573
574/*------------------------------- Link status -------------------------------*/
575
576/*
Jay Vosburghff59c452006-03-27 13:27:43 -0800577 * Set the carrier state for the master according to the state of its
578 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
579 * do special 802.3ad magic.
580 *
581 * Returns zero if carrier state does not change, nonzero if it does.
582 */
583static int bond_set_carrier(struct bonding *bond)
584{
585 struct slave *slave;
586 int i;
587
588 if (bond->slave_cnt == 0)
589 goto down;
590
591 if (bond->params.mode == BOND_MODE_8023AD)
592 return bond_3ad_set_carrier(bond);
593
594 bond_for_each_slave(bond, slave, i) {
595 if (slave->link == BOND_LINK_UP) {
596 if (!netif_carrier_ok(bond->dev)) {
597 netif_carrier_on(bond->dev);
598 return 1;
599 }
600 return 0;
601 }
602 }
603
604down:
605 if (netif_carrier_ok(bond->dev)) {
606 netif_carrier_off(bond->dev);
607 return 1;
608 }
609 return 0;
610}
611
612/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 * Get link speed and duplex from the slave's base driver
614 * using ethtool. If for some reason the call fails or the
615 * values are invalid, fake speed and duplex to 100/Full
616 * and return error.
617 */
618static int bond_update_speed_duplex(struct slave *slave)
619{
620 struct net_device *slave_dev = slave->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 struct ethtool_cmd etool;
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700622 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 /* Fake speed and duplex */
625 slave->speed = SPEED_100;
626 slave->duplex = DUPLEX_FULL;
627
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700628 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700631 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
632 if (res < 0)
633 return -1;
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 switch (etool.speed) {
636 case SPEED_10:
637 case SPEED_100:
638 case SPEED_1000:
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700639 case SPEED_10000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 break;
641 default:
642 return -1;
643 }
644
645 switch (etool.duplex) {
646 case DUPLEX_FULL:
647 case DUPLEX_HALF:
648 break;
649 default:
650 return -1;
651 }
652
653 slave->speed = etool.speed;
654 slave->duplex = etool.duplex;
655
656 return 0;
657}
658
659/*
660 * if <dev> supports MII link status reporting, check its link status.
661 *
662 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
663 * depening upon the setting of the use_carrier parameter.
664 *
665 * Return either BMSR_LSTATUS, meaning that the link is up (or we
666 * can't tell and just pretend it is), or 0, meaning that the link is
667 * down.
668 *
669 * If reporting is non-zero, instead of faking link up, return -1 if
670 * both ETHTOOL and MII ioctls fail (meaning the device does not
671 * support them). If use_carrier is set, return whatever it says.
672 * It'd be nice if there was a good way to tell if a driver supports
673 * netif_carrier, but there really isn't.
674 */
675static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_dev, int reporting)
676{
677 static int (* ioctl)(struct net_device *, struct ifreq *, int);
678 struct ifreq ifr;
679 struct mii_ioctl_data *mii;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 if (bond->params.use_carrier) {
682 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
683 }
684
685 ioctl = slave_dev->do_ioctl;
686 if (ioctl) {
687 /* TODO: set pointer to correct ioctl on a per team member */
688 /* bases to make this more efficient. that is, once */
689 /* we determine the correct ioctl, we will always */
690 /* call it and not the others for that team */
691 /* member. */
692
693 /*
694 * We cannot assume that SIOCGMIIPHY will also read a
695 * register; not all network drivers (e.g., e100)
696 * support that.
697 */
698
699 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
700 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
701 mii = if_mii(&ifr);
702 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
703 mii->reg_num = MII_BMSR;
704 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0) {
705 return (mii->val_out & BMSR_LSTATUS);
706 }
707 }
708 }
709
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700710 /*
711 * Some drivers cache ETHTOOL_GLINK for a period of time so we only
712 * attempt to get link status from it if the above MII ioctls fail.
713 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (slave_dev->ethtool_ops) {
715 if (slave_dev->ethtool_ops->get_link) {
716 u32 link;
717
718 link = slave_dev->ethtool_ops->get_link(slave_dev);
719
720 return link ? BMSR_LSTATUS : 0;
721 }
722 }
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 /*
725 * If reporting, report that either there's no dev->do_ioctl,
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700726 * or both SIOCGMIIREG and get_link failed (meaning that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * cannot report link status). If not reporting, pretend
728 * we're ok.
729 */
730 return (reporting ? -1 : BMSR_LSTATUS);
731}
732
733/*----------------------------- Multicast list ------------------------------*/
734
735/*
736 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
737 */
738static inline int bond_is_dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
739{
740 return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
741 dmi1->dmi_addrlen == dmi2->dmi_addrlen;
742}
743
744/*
745 * returns dmi entry if found, NULL otherwise
746 */
747static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
748{
749 struct dev_mc_list *idmi;
750
751 for (idmi = mc_list; idmi; idmi = idmi->next) {
752 if (bond_is_dmi_same(dmi, idmi)) {
753 return idmi;
754 }
755 }
756
757 return NULL;
758}
759
760/*
761 * Push the promiscuity flag down to appropriate slaves
762 */
763static void bond_set_promiscuity(struct bonding *bond, int inc)
764{
765 if (USES_PRIMARY(bond->params.mode)) {
766 /* write lock already acquired */
767 if (bond->curr_active_slave) {
768 dev_set_promiscuity(bond->curr_active_slave->dev, inc);
769 }
770 } else {
771 struct slave *slave;
772 int i;
773 bond_for_each_slave(bond, slave, i) {
774 dev_set_promiscuity(slave->dev, inc);
775 }
776 }
777}
778
779/*
780 * Push the allmulti flag down to all slaves
781 */
782static void bond_set_allmulti(struct bonding *bond, int inc)
783{
784 if (USES_PRIMARY(bond->params.mode)) {
785 /* write lock already acquired */
786 if (bond->curr_active_slave) {
787 dev_set_allmulti(bond->curr_active_slave->dev, inc);
788 }
789 } else {
790 struct slave *slave;
791 int i;
792 bond_for_each_slave(bond, slave, i) {
793 dev_set_allmulti(slave->dev, inc);
794 }
795 }
796}
797
798/*
799 * Add a Multicast address to slaves
800 * according to mode
801 */
802static void bond_mc_add(struct bonding *bond, void *addr, int alen)
803{
804 if (USES_PRIMARY(bond->params.mode)) {
805 /* write lock already acquired */
806 if (bond->curr_active_slave) {
807 dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
808 }
809 } else {
810 struct slave *slave;
811 int i;
812 bond_for_each_slave(bond, slave, i) {
813 dev_mc_add(slave->dev, addr, alen, 0);
814 }
815 }
816}
817
818/*
819 * Remove a multicast address from slave
820 * according to mode
821 */
822static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
823{
824 if (USES_PRIMARY(bond->params.mode)) {
825 /* write lock already acquired */
826 if (bond->curr_active_slave) {
827 dev_mc_delete(bond->curr_active_slave->dev, addr, alen, 0);
828 }
829 } else {
830 struct slave *slave;
831 int i;
832 bond_for_each_slave(bond, slave, i) {
833 dev_mc_delete(slave->dev, addr, alen, 0);
834 }
835 }
836}
837
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800838
839/*
840 * Retrieve the list of registered multicast addresses for the bonding
841 * device and retransmit an IGMP JOIN request to the current active
842 * slave.
843 */
844static void bond_resend_igmp_join_requests(struct bonding *bond)
845{
846 struct in_device *in_dev;
847 struct ip_mc_list *im;
848
849 rcu_read_lock();
850 in_dev = __in_dev_get_rcu(bond->dev);
851 if (in_dev) {
852 for (im = in_dev->mc_list; im; im = im->next) {
853 ip_mc_rejoin_group(im);
854 }
855 }
856
857 rcu_read_unlock();
858}
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860/*
861 * Totally destroys the mc_list in bond
862 */
863static void bond_mc_list_destroy(struct bonding *bond)
864{
865 struct dev_mc_list *dmi;
866
867 dmi = bond->mc_list;
868 while (dmi) {
869 bond->mc_list = dmi->next;
870 kfree(dmi);
871 dmi = bond->mc_list;
872 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800873 bond->mc_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
876/*
877 * Copy all the Multicast addresses from src to the bonding device dst
878 */
Randy Dunlapde54f392005-10-04 22:39:41 -0700879static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
Al Virodd0fc662005-10-07 07:46:04 +0100880 gfp_t gfp_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 struct dev_mc_list *dmi, *new_dmi;
883
884 for (dmi = mc_list; dmi; dmi = dmi->next) {
Randy Dunlapde54f392005-10-04 22:39:41 -0700885 new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 if (!new_dmi) {
888 /* FIXME: Potential memory leak !!! */
889 return -ENOMEM;
890 }
891
892 new_dmi->next = bond->mc_list;
893 bond->mc_list = new_dmi;
894 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
895 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
896 new_dmi->dmi_users = dmi->dmi_users;
897 new_dmi->dmi_gusers = dmi->dmi_gusers;
898 }
899
900 return 0;
901}
902
903/*
904 * flush all members of flush->mc_list from device dev->mc_list
905 */
906static void bond_mc_list_flush(struct net_device *bond_dev, struct net_device *slave_dev)
907{
908 struct bonding *bond = bond_dev->priv;
909 struct dev_mc_list *dmi;
910
911 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
912 dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
913 }
914
915 if (bond->params.mode == BOND_MODE_8023AD) {
916 /* del lacpdu mc addr from mc list */
917 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
918
919 dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
920 }
921}
922
923/*--------------------------- Active slave change ---------------------------*/
924
925/*
926 * Update the mc list and multicast-related flags for the new and
927 * old active slaves (if any) according to the multicast mode, and
928 * promiscuous flags unconditionally.
929 */
930static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct slave *old_active)
931{
932 struct dev_mc_list *dmi;
933
934 if (!USES_PRIMARY(bond->params.mode)) {
935 /* nothing to do - mc list is already up-to-date on
936 * all slaves
937 */
938 return;
939 }
940
941 if (old_active) {
942 if (bond->dev->flags & IFF_PROMISC) {
943 dev_set_promiscuity(old_active->dev, -1);
944 }
945
946 if (bond->dev->flags & IFF_ALLMULTI) {
947 dev_set_allmulti(old_active->dev, -1);
948 }
949
950 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
951 dev_mc_delete(old_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
952 }
953 }
954
955 if (new_active) {
956 if (bond->dev->flags & IFF_PROMISC) {
957 dev_set_promiscuity(new_active->dev, 1);
958 }
959
960 if (bond->dev->flags & IFF_ALLMULTI) {
961 dev_set_allmulti(new_active->dev, 1);
962 }
963
964 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
965 dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
966 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800967 bond_resend_igmp_join_requests(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969}
970
971/**
972 * find_best_interface - select the best available slave to be the active one
973 * @bond: our bonding struct
974 *
975 * Warning: Caller must hold curr_slave_lock for writing.
976 */
977static struct slave *bond_find_best_slave(struct bonding *bond)
978{
979 struct slave *new_active, *old_active;
980 struct slave *bestslave = NULL;
981 int mintime = bond->params.updelay;
982 int i;
983
984 new_active = old_active = bond->curr_active_slave;
985
986 if (!new_active) { /* there were no active slaves left */
987 if (bond->slave_cnt > 0) { /* found one slave */
988 new_active = bond->first_slave;
989 } else {
990 return NULL; /* still no slave, return NULL */
991 }
992 }
993
994 /* first try the primary link; if arping, a link must tx/rx traffic
995 * before it can be considered the curr_active_slave - also, we would skip
996 * slaves between the curr_active_slave and primary_slave that may be up
997 * and able to arp
998 */
999 if ((bond->primary_slave) &&
1000 (!bond->params.arp_interval) &&
1001 (IS_UP(bond->primary_slave->dev))) {
1002 new_active = bond->primary_slave;
1003 }
1004
1005 /* remember where to stop iterating over the slaves */
1006 old_active = new_active;
1007
1008 bond_for_each_slave_from(bond, new_active, i, old_active) {
1009 if (IS_UP(new_active->dev)) {
1010 if (new_active->link == BOND_LINK_UP) {
1011 return new_active;
1012 } else if (new_active->link == BOND_LINK_BACK) {
1013 /* link up, but waiting for stabilization */
1014 if (new_active->delay < mintime) {
1015 mintime = new_active->delay;
1016 bestslave = new_active;
1017 }
1018 }
1019 }
1020 }
1021
1022 return bestslave;
1023}
1024
1025/**
1026 * change_active_interface - change the active slave into the specified one
1027 * @bond: our bonding struct
1028 * @new: the new slave to make the active one
1029 *
1030 * Set the new slave to the bond's settings and unset them on the old
1031 * curr_active_slave.
1032 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1033 *
1034 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1035 * because it is apparently the best available slave we have, even though its
1036 * updelay hasn't timed out yet.
1037 *
1038 * Warning: Caller must hold curr_slave_lock for writing.
1039 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001040void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 struct slave *old_active = bond->curr_active_slave;
1043
1044 if (old_active == new_active) {
1045 return;
1046 }
1047
1048 if (new_active) {
1049 if (new_active->link == BOND_LINK_BACK) {
1050 if (USES_PRIMARY(bond->params.mode)) {
1051 printk(KERN_INFO DRV_NAME
1052 ": %s: making interface %s the new "
1053 "active one %d ms earlier.\n",
1054 bond->dev->name, new_active->dev->name,
1055 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1056 }
1057
1058 new_active->delay = 0;
1059 new_active->link = BOND_LINK_UP;
1060 new_active->jiffies = jiffies;
1061
1062 if (bond->params.mode == BOND_MODE_8023AD) {
1063 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1064 }
1065
1066 if ((bond->params.mode == BOND_MODE_TLB) ||
1067 (bond->params.mode == BOND_MODE_ALB)) {
1068 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
1069 }
1070 } else {
1071 if (USES_PRIMARY(bond->params.mode)) {
1072 printk(KERN_INFO DRV_NAME
1073 ": %s: making interface %s the new "
1074 "active one.\n",
1075 bond->dev->name, new_active->dev->name);
1076 }
1077 }
1078 }
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (USES_PRIMARY(bond->params.mode)) {
1081 bond_mc_swap(bond, new_active, old_active);
1082 }
1083
1084 if ((bond->params.mode == BOND_MODE_TLB) ||
1085 (bond->params.mode == BOND_MODE_ALB)) {
1086 bond_alb_handle_active_change(bond, new_active);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001087 if (old_active)
1088 bond_set_slave_inactive_flags(old_active);
1089 if (new_active)
1090 bond_set_slave_active_flags(new_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 } else {
1092 bond->curr_active_slave = new_active;
1093 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001094
1095 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1096 if (old_active) {
1097 bond_set_slave_inactive_flags(old_active);
1098 }
1099
1100 if (new_active) {
1101 bond_set_slave_active_flags(new_active);
1102 }
Moni Shoua2ab82852007-10-09 19:43:39 -07001103
1104 /* when bonding does not set the slave MAC address, the bond MAC
1105 * address is the one of the active slave.
1106 */
Jay Vosburghdd957c52007-10-09 19:57:24 -07001107 if (new_active && bond->params.fail_over_mac)
Moni Shoua2ab82852007-10-09 19:43:39 -07001108 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
1109 new_active->dev->addr_len);
Moni Shoua1053f622007-10-09 19:43:42 -07001110 if (bond->curr_active_slave &&
1111 test_bit(__LINK_STATE_LINKWATCH_PENDING,
1112 &bond->curr_active_slave->dev->state)) {
1113 dprintk("delaying gratuitous arp on %s\n",
1114 bond->curr_active_slave->dev->name);
1115 bond->send_grat_arp = 1;
1116 } else
1117 bond_send_gratuitous_arp(bond);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
1121/**
1122 * bond_select_active_slave - select a new active slave, if needed
1123 * @bond: our bonding struct
1124 *
1125 * This functions shoud be called when one of the following occurs:
1126 * - The old curr_active_slave has been released or lost its link.
1127 * - The primary_slave has got its link back.
1128 * - A slave has got its link back and there's no old curr_active_slave.
1129 *
1130 * Warning: Caller must hold curr_slave_lock for writing.
1131 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001132void bond_select_active_slave(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
1134 struct slave *best_slave;
Jay Vosburghff59c452006-03-27 13:27:43 -08001135 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 best_slave = bond_find_best_slave(bond);
1138 if (best_slave != bond->curr_active_slave) {
1139 bond_change_active_slave(bond, best_slave);
Jay Vosburghff59c452006-03-27 13:27:43 -08001140 rv = bond_set_carrier(bond);
1141 if (!rv)
1142 return;
1143
1144 if (netif_carrier_ok(bond->dev)) {
1145 printk(KERN_INFO DRV_NAME
1146 ": %s: first active interface up!\n",
1147 bond->dev->name);
1148 } else {
1149 printk(KERN_INFO DRV_NAME ": %s: "
1150 "now running without any active interface !\n",
1151 bond->dev->name);
1152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154}
1155
1156/*--------------------------- slave list handling ---------------------------*/
1157
1158/*
1159 * This function attaches the slave to the end of list.
1160 *
1161 * bond->lock held for writing by caller.
1162 */
1163static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1164{
1165 if (bond->first_slave == NULL) { /* attaching the first slave */
1166 new_slave->next = new_slave;
1167 new_slave->prev = new_slave;
1168 bond->first_slave = new_slave;
1169 } else {
1170 new_slave->next = bond->first_slave;
1171 new_slave->prev = bond->first_slave->prev;
1172 new_slave->next->prev = new_slave;
1173 new_slave->prev->next = new_slave;
1174 }
1175
1176 bond->slave_cnt++;
1177}
1178
1179/*
1180 * This function detaches the slave from the list.
1181 * WARNING: no check is made to verify if the slave effectively
1182 * belongs to <bond>.
1183 * Nothing is freed on return, structures are just unchained.
1184 * If any slave pointer in bond was pointing to <slave>,
1185 * it should be changed by the calling function.
1186 *
1187 * bond->lock held for writing by caller.
1188 */
1189static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1190{
1191 if (slave->next) {
1192 slave->next->prev = slave->prev;
1193 }
1194
1195 if (slave->prev) {
1196 slave->prev->next = slave->next;
1197 }
1198
1199 if (bond->first_slave == slave) { /* slave is the first slave */
1200 if (bond->slave_cnt > 1) { /* there are more slave */
1201 bond->first_slave = slave->next;
1202 } else {
1203 bond->first_slave = NULL; /* slave was the last one */
1204 }
1205 }
1206
1207 slave->next = NULL;
1208 slave->prev = NULL;
1209 bond->slave_cnt--;
1210}
1211
1212/*---------------------------------- IOCTL ----------------------------------*/
1213
Adrian Bunk4ad072c2007-07-09 11:51:12 -07001214static int bond_sethwaddr(struct net_device *bond_dev,
1215 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
1217 dprintk("bond_dev=%p\n", bond_dev);
1218 dprintk("slave_dev=%p\n", slave_dev);
1219 dprintk("slave_dev->addr_len=%d\n", slave_dev->addr_len);
1220 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1221 return 0;
1222}
1223
Herbert Xu7f353bf2007-08-10 15:47:58 -07001224#define BOND_VLAN_FEATURES \
1225 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1226 NETIF_F_HW_VLAN_FILTER)
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001227
1228/*
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001229 * Compute the common dev->feature set available to all slaves. Some
Herbert Xu7f353bf2007-08-10 15:47:58 -07001230 * feature bits are managed elsewhere, so preserve those feature bits
1231 * on the master device.
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001232 */
1233static int bond_compute_features(struct bonding *bond)
1234{
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001235 struct slave *slave;
1236 struct net_device *bond_dev = bond->dev;
Herbert Xu7f353bf2007-08-10 15:47:58 -07001237 unsigned long features = bond_dev->features;
Moni Shoua3158bf72007-10-09 19:43:41 -07001238 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1239 bond_dev->hard_header_len);
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001240 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001241
Herbert Xu7f353bf2007-08-10 15:47:58 -07001242 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
1243 features |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
1244 NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1245
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001246 bond_for_each_slave(bond, slave, i) {
Herbert Xu7f353bf2007-08-10 15:47:58 -07001247 features = netdev_compute_features(features,
1248 slave->dev->features);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001249 if (slave->dev->hard_header_len > max_hard_header_len)
1250 max_hard_header_len = slave->dev->hard_header_len;
1251 }
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001252
Herbert Xu7f353bf2007-08-10 15:47:58 -07001253 features |= (bond_dev->features & BOND_VLAN_FEATURES);
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001254 bond_dev->features = features;
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001255 bond_dev->hard_header_len = max_hard_header_len;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001256
1257 return 0;
1258}
1259
Moni Shoua872254d2007-10-09 19:43:38 -07001260
1261static void bond_setup_by_slave(struct net_device *bond_dev,
1262 struct net_device *slave_dev)
1263{
Moni Shouad90a1622007-10-09 19:43:43 -07001264 struct bonding *bond = bond_dev->priv;
1265
Moni Shoua872254d2007-10-09 19:43:38 -07001266 bond_dev->neigh_setup = slave_dev->neigh_setup;
Jay Vosburgh1284cd32007-10-15 16:44:27 -07001267 bond_dev->header_ops = slave_dev->header_ops;
Moni Shoua872254d2007-10-09 19:43:38 -07001268
1269 bond_dev->type = slave_dev->type;
1270 bond_dev->hard_header_len = slave_dev->hard_header_len;
1271 bond_dev->addr_len = slave_dev->addr_len;
1272
1273 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1274 slave_dev->addr_len);
Moni Shouad90a1622007-10-09 19:43:43 -07001275 bond->setup_by_slave = 1;
Moni Shoua872254d2007-10-09 19:43:38 -07001276}
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278/* enslave device <slave> to bond device <master> */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001279int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 struct bonding *bond = bond_dev->priv;
1282 struct slave *new_slave = NULL;
1283 struct dev_mc_list *dmi;
1284 struct sockaddr addr;
1285 int link_reporting;
1286 int old_features = bond_dev->features;
1287 int res = 0;
1288
nsxfreddy@gmail.com552709d2005-09-21 14:18:04 -05001289 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
1290 slave_dev->do_ioctl == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001292 ": %s: Warning: no link monitoring support for %s\n",
1293 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
1295
1296 /* bond must be initialized by bond_open() before enslaving */
1297 if (!(bond_dev->flags & IFF_UP)) {
Moni Shoua6b1bf092007-10-09 19:43:40 -07001298 printk(KERN_WARNING DRV_NAME
1299 " %s: master_dev is not up in bond_enslave\n",
1300 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302
1303 /* already enslaved */
1304 if (slave_dev->flags & IFF_SLAVE) {
1305 dprintk("Error, Device was already enslaved\n");
1306 return -EBUSY;
1307 }
1308
1309 /* vlan challenged mutual exclusion */
1310 /* no need to lock since we're protected by rtnl_lock */
1311 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1312 dprintk("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1313 if (!list_empty(&bond->vlan_list)) {
1314 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001315 ": %s: Error: cannot enslave VLAN "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 "challenged slave %s on VLAN enabled "
Mitch Williams4e0952c2005-11-09 10:34:57 -08001317 "bond %s\n", bond_dev->name, slave_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 bond_dev->name);
1319 return -EPERM;
1320 } else {
1321 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001322 ": %s: Warning: enslaved VLAN challenged "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 "slave %s. Adding VLANs will be blocked as "
1324 "long as %s is part of bond %s\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001325 bond_dev->name, slave_dev->name, slave_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 bond_dev->name);
1327 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1328 }
1329 } else {
1330 dprintk("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
1331 if (bond->slave_cnt == 0) {
1332 /* First slave, and it is not VLAN challenged,
1333 * so remove the block of adding VLANs over the bond.
1334 */
1335 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1336 }
1337 }
1338
Jay Vosburgh217df672005-09-26 16:11:50 -07001339 /*
1340 * Old ifenslave binaries are no longer supported. These can
1341 * be identified with moderate accurary by the state of the slave:
1342 * the current ifenslave will set the interface down prior to
1343 * enslaving it; the old ifenslave will not.
1344 */
1345 if ((slave_dev->flags & IFF_UP)) {
1346 printk(KERN_ERR DRV_NAME ": %s is up. "
1347 "This may be due to an out of date ifenslave.\n",
1348 slave_dev->name);
1349 res = -EPERM;
1350 goto err_undo_flags;
1351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Moni Shoua872254d2007-10-09 19:43:38 -07001353 /* set bonding device ether type by slave - bonding netdevices are
1354 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1355 * there is a need to override some of the type dependent attribs/funcs.
1356 *
1357 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1358 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1359 */
1360 if (bond->slave_cnt == 0) {
1361 if (slave_dev->type != ARPHRD_ETHER)
1362 bond_setup_by_slave(bond_dev, slave_dev);
1363 } else if (bond_dev->type != slave_dev->type) {
1364 printk(KERN_ERR DRV_NAME ": %s ether type (%d) is different "
1365 "from other slaves (%d), can not enslave it.\n",
1366 slave_dev->name,
1367 slave_dev->type, bond_dev->type);
1368 res = -EINVAL;
1369 goto err_undo_flags;
1370 }
1371
Jay Vosburgh217df672005-09-26 16:11:50 -07001372 if (slave_dev->set_mac_address == NULL) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001373 if (bond->slave_cnt == 0) {
1374 printk(KERN_WARNING DRV_NAME
Jay Vosburghdd957c52007-10-09 19:57:24 -07001375 ": %s: Warning: The first slave device "
1376 "specified does not support setting the MAC "
1377 "address. Enabling the fail_over_mac option.",
1378 bond_dev->name);
1379 bond->params.fail_over_mac = 1;
1380 } else if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001381 printk(KERN_ERR DRV_NAME
Jay Vosburghdd957c52007-10-09 19:57:24 -07001382 ": %s: Error: The slave device specified "
1383 "does not support setting the MAC address, "
1384 "but fail_over_mac is not enabled.\n"
Moni Shoua2ab82852007-10-09 19:43:39 -07001385 , bond_dev->name);
1386 res = -EOPNOTSUPP;
1387 goto err_undo_flags;
1388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390
Joe Jin243cb4e2007-02-06 14:16:40 -08001391 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (!new_slave) {
1393 res = -ENOMEM;
1394 goto err_undo_flags;
1395 }
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 /* save slave's original flags before calling
1398 * netdev_set_master and dev_open
1399 */
1400 new_slave->original_flags = slave_dev->flags;
1401
Jay Vosburgh217df672005-09-26 16:11:50 -07001402 /*
1403 * Save slave's original ("permanent") mac address for modes
1404 * that need it, and for restoring it upon release, and then
1405 * set it to the master's address
1406 */
1407 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Jay Vosburghdd957c52007-10-09 19:57:24 -07001409 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001410 /*
1411 * Set slave to master's mac address. The application already
1412 * set the master's mac address to that of the first slave
1413 */
1414 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1415 addr.sa_family = slave_dev->type;
1416 res = dev_set_mac_address(slave_dev, &addr);
1417 if (res) {
1418 dprintk("Error %d calling set_mac_address\n", res);
1419 goto err_free;
1420 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001423 res = netdev_set_master(slave_dev, bond_dev);
1424 if (res) {
1425 dprintk("Error %d calling netdev_set_master\n", res);
1426 goto err_close;
1427 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001428 /* open the slave since the application closed it */
1429 res = dev_open(slave_dev);
1430 if (res) {
1431 dprintk("Openning slave %s failed\n", slave_dev->name);
1432 goto err_restore_mac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 new_slave->dev = slave_dev;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001436 slave_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 if ((bond->params.mode == BOND_MODE_TLB) ||
1439 (bond->params.mode == BOND_MODE_ALB)) {
1440 /* bond_alb_init_slave() must be called before all other stages since
1441 * it might fail and we do not want to have to undo everything
1442 */
1443 res = bond_alb_init_slave(bond, new_slave);
1444 if (res) {
1445 goto err_unset_master;
1446 }
1447 }
1448
1449 /* If the mode USES_PRIMARY, then the new slave gets the
1450 * master's promisc (and mc) settings only if it becomes the
1451 * curr_active_slave, and that is taken care of later when calling
1452 * bond_change_active()
1453 */
1454 if (!USES_PRIMARY(bond->params.mode)) {
1455 /* set promiscuity level to new slave */
1456 if (bond_dev->flags & IFF_PROMISC) {
1457 dev_set_promiscuity(slave_dev, 1);
1458 }
1459
1460 /* set allmulti level to new slave */
1461 if (bond_dev->flags & IFF_ALLMULTI) {
1462 dev_set_allmulti(slave_dev, 1);
1463 }
1464
1465 /* upload master's mc_list to new slave */
1466 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1467 dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1468 }
1469 }
1470
1471 if (bond->params.mode == BOND_MODE_8023AD) {
1472 /* add lacpdu mc addr to mc list */
1473 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1474
1475 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1476 }
1477
1478 bond_add_vlans_on_slave(bond, slave_dev);
1479
1480 write_lock_bh(&bond->lock);
1481
1482 bond_attach_slave(bond, new_slave);
1483
1484 new_slave->delay = 0;
1485 new_slave->link_failure_count = 0;
1486
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001487 bond_compute_features(bond);
1488
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001489 new_slave->last_arp_rx = jiffies;
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (bond->params.miimon && !bond->params.use_carrier) {
1492 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1493
1494 if ((link_reporting == -1) && !bond->params.arp_interval) {
1495 /*
1496 * miimon is set but a bonded network driver
1497 * does not support ETHTOOL/MII and
1498 * arp_interval is not set. Note: if
1499 * use_carrier is enabled, we will never go
1500 * here (because netif_carrier is always
1501 * supported); thus, we don't need to change
1502 * the messages for netif_carrier.
1503 */
1504 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001505 ": %s: Warning: MII and ETHTOOL support not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 "available for interface %s, and "
1507 "arp_interval/arp_ip_target module parameters "
1508 "not specified, thus bonding will not detect "
1509 "link failures! see bonding.txt for details.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001510 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 } else if (link_reporting == -1) {
1512 /* unable get link status using mii/ethtool */
1513 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001514 ": %s: Warning: can't get link status from "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 "interface %s; the network driver associated "
1516 "with this interface does not support MII or "
1517 "ETHTOOL link status reporting, thus miimon "
1518 "has no effect on this interface.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001519 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
1521 }
1522
1523 /* check for initial state */
1524 if (!bond->params.miimon ||
1525 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1526 if (bond->params.updelay) {
1527 dprintk("Initial state of slave_dev is "
1528 "BOND_LINK_BACK\n");
1529 new_slave->link = BOND_LINK_BACK;
1530 new_slave->delay = bond->params.updelay;
1531 } else {
1532 dprintk("Initial state of slave_dev is "
1533 "BOND_LINK_UP\n");
1534 new_slave->link = BOND_LINK_UP;
1535 }
1536 new_slave->jiffies = jiffies;
1537 } else {
1538 dprintk("Initial state of slave_dev is "
1539 "BOND_LINK_DOWN\n");
1540 new_slave->link = BOND_LINK_DOWN;
1541 }
1542
1543 if (bond_update_speed_duplex(new_slave) &&
1544 (new_slave->link != BOND_LINK_DOWN)) {
1545 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001546 ": %s: Warning: failed to get speed and duplex from %s, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 "assumed to be 100Mb/sec and Full.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001548 bond_dev->name, new_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 if (bond->params.mode == BOND_MODE_8023AD) {
Mitch Williams4e0952c2005-11-09 10:34:57 -08001551 printk(KERN_WARNING DRV_NAME
1552 ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 "support in base driver for proper aggregator "
Mitch Williams4e0952c2005-11-09 10:34:57 -08001554 "selection.\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556 }
1557
1558 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1559 /* if there is a primary slave, remember it */
1560 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1561 bond->primary_slave = new_slave;
1562 }
1563 }
1564
1565 switch (bond->params.mode) {
1566 case BOND_MODE_ACTIVEBACKUP:
Jay Vosburgh8a8e4472006-09-22 21:56:15 -07001567 bond_set_slave_inactive_flags(new_slave);
1568 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 break;
1570 case BOND_MODE_8023AD:
1571 /* in 802.3ad mode, the internal mechanism
1572 * will activate the slaves in the selected
1573 * aggregator
1574 */
1575 bond_set_slave_inactive_flags(new_slave);
1576 /* if this is the first slave */
1577 if (bond->slave_cnt == 1) {
1578 SLAVE_AD_INFO(new_slave).id = 1;
1579 /* Initialize AD with the number of times that the AD timer is called in 1 second
1580 * can be called only after the mac address of the bond is set
1581 */
1582 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1583 bond->params.lacp_fast);
1584 } else {
1585 SLAVE_AD_INFO(new_slave).id =
1586 SLAVE_AD_INFO(new_slave->prev).id + 1;
1587 }
1588
1589 bond_3ad_bind_slave(new_slave);
1590 break;
1591 case BOND_MODE_TLB:
1592 case BOND_MODE_ALB:
1593 new_slave->state = BOND_STATE_ACTIVE;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001594 bond_set_slave_inactive_flags(new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 break;
1596 default:
1597 dprintk("This slave is always active in trunk mode\n");
1598
1599 /* always active in trunk mode */
1600 new_slave->state = BOND_STATE_ACTIVE;
1601
1602 /* In trunking mode there is little meaning to curr_active_slave
1603 * anyway (it holds no special properties of the bond device),
1604 * so we can change it without calling change_active_interface()
1605 */
1606 if (!bond->curr_active_slave) {
1607 bond->curr_active_slave = new_slave;
1608 }
1609 break;
1610 } /* switch(bond_mode) */
1611
Jay Vosburghff59c452006-03-27 13:27:43 -08001612 bond_set_carrier(bond);
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 write_unlock_bh(&bond->lock);
1615
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001616 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1617 if (res)
1618 goto err_unset_master;
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 printk(KERN_INFO DRV_NAME
1621 ": %s: enslaving %s as a%s interface with a%s link.\n",
1622 bond_dev->name, slave_dev->name,
1623 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1624 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1625
1626 /* enslave is successful */
1627 return 0;
1628
1629/* Undo stages on error */
1630err_unset_master:
1631 netdev_set_master(slave_dev, NULL);
1632
1633err_close:
1634 dev_close(slave_dev);
1635
1636err_restore_mac:
Jay Vosburghdd957c52007-10-09 19:57:24 -07001637 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001638 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1639 addr.sa_family = slave_dev->type;
1640 dev_set_mac_address(slave_dev, &addr);
1641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643err_free:
1644 kfree(new_slave);
1645
1646err_undo_flags:
1647 bond_dev->features = old_features;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return res;
1650}
1651
1652/*
1653 * Try to release the slave device <slave> from the bond device <master>
1654 * It is legal to access curr_active_slave without a lock because all the function
1655 * is write-locked.
1656 *
1657 * The rules for slave state should be:
1658 * for Active/Backup:
1659 * Active stays on all backups go down
1660 * for Bonded connections:
1661 * The first up interface should be left on and all others downed.
1662 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001663int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
1665 struct bonding *bond = bond_dev->priv;
1666 struct slave *slave, *oldcurrent;
1667 struct sockaddr addr;
1668 int mac_addr_differ;
Joe Perches0795af52007-10-03 17:59:30 -07001669 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 /* slave is not a slave or master is not master of this slave */
1672 if (!(slave_dev->flags & IFF_SLAVE) ||
1673 (slave_dev->master != bond_dev)) {
1674 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001675 ": %s: Error: cannot release %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 bond_dev->name, slave_dev->name);
1677 return -EINVAL;
1678 }
1679
1680 write_lock_bh(&bond->lock);
1681
1682 slave = bond_get_slave_by_dev(bond, slave_dev);
1683 if (!slave) {
1684 /* not a slave of this bond */
1685 printk(KERN_INFO DRV_NAME
1686 ": %s: %s not enslaved\n",
1687 bond_dev->name, slave_dev->name);
Jay Vosburghf5e2a7b2006-02-07 21:17:22 -08001688 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return -EINVAL;
1690 }
1691
1692 mac_addr_differ = memcmp(bond_dev->dev_addr,
1693 slave->perm_hwaddr,
1694 ETH_ALEN);
1695 if (!mac_addr_differ && (bond->slave_cnt > 1)) {
1696 printk(KERN_WARNING DRV_NAME
Joe Perches0795af52007-10-03 17:59:30 -07001697 ": %s: Warning: the permanent HWaddr of %s - "
1698 "%s - is still in use by %s. "
1699 "Set the HWaddr of %s to a different address "
1700 "to avoid conflicts.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001701 bond_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 slave_dev->name,
Joe Perches0795af52007-10-03 17:59:30 -07001703 print_mac(mac, slave->perm_hwaddr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 bond_dev->name,
1705 slave_dev->name);
1706 }
1707
1708 /* Inform AD package of unbinding of slave. */
1709 if (bond->params.mode == BOND_MODE_8023AD) {
1710 /* must be called before the slave is
1711 * detached from the list
1712 */
1713 bond_3ad_unbind_slave(slave);
1714 }
1715
1716 printk(KERN_INFO DRV_NAME
1717 ": %s: releasing %s interface %s\n",
1718 bond_dev->name,
1719 (slave->state == BOND_STATE_ACTIVE)
1720 ? "active" : "backup",
1721 slave_dev->name);
1722
1723 oldcurrent = bond->curr_active_slave;
1724
1725 bond->current_arp_slave = NULL;
1726
1727 /* release the slave from its bond */
1728 bond_detach_slave(bond, slave);
1729
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001730 bond_compute_features(bond);
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if (bond->primary_slave == slave) {
1733 bond->primary_slave = NULL;
1734 }
1735
1736 if (oldcurrent == slave) {
1737 bond_change_active_slave(bond, NULL);
1738 }
1739
1740 if ((bond->params.mode == BOND_MODE_TLB) ||
1741 (bond->params.mode == BOND_MODE_ALB)) {
1742 /* Must be called only after the slave has been
1743 * detached from the list and the curr_active_slave
1744 * has been cleared (if our_slave == old_current),
1745 * but before a new active slave is selected.
1746 */
1747 bond_alb_deinit_slave(bond, slave);
1748 }
1749
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001750 if (oldcurrent == slave) {
1751 /*
1752 * Note that we hold RTNL over this sequence, so there
1753 * is no concern that another slave add/remove event
1754 * will interfere.
1755 */
1756 write_unlock_bh(&bond->lock);
1757 read_lock(&bond->lock);
1758 write_lock_bh(&bond->curr_slave_lock);
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 bond_select_active_slave(bond);
1761
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001762 write_unlock_bh(&bond->curr_slave_lock);
1763 read_unlock(&bond->lock);
1764 write_lock_bh(&bond->lock);
1765 }
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (bond->slave_cnt == 0) {
Jay Vosburghff59c452006-03-27 13:27:43 -08001768 bond_set_carrier(bond);
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 /* if the last slave was removed, zero the mac address
1771 * of the master so it will be set by the application
1772 * to the mac address of the first slave
1773 */
1774 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1775
1776 if (list_empty(&bond->vlan_list)) {
1777 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1778 } else {
1779 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001780 ": %s: Warning: clearing HW address of %s while it "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 "still has VLANs.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001782 bond_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001784 ": %s: When re-adding slaves, make sure the bond's "
1785 "HW address matches its VLANs'.\n",
1786 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
1788 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
1789 !bond_has_challenged_slaves(bond)) {
1790 printk(KERN_INFO DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001791 ": %s: last VLAN challenged slave %s "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 "left bond %s. VLAN blocking is removed\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001793 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1795 }
1796
1797 write_unlock_bh(&bond->lock);
1798
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001799 /* must do this from outside any spinlocks */
1800 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 bond_del_vlans_from_slave(bond, slave_dev);
1803
1804 /* If the mode USES_PRIMARY, then we should only remove its
1805 * promisc and mc settings if it was the curr_active_slave, but that was
1806 * already taken care of above when we detached the slave
1807 */
1808 if (!USES_PRIMARY(bond->params.mode)) {
1809 /* unset promiscuity level from slave */
1810 if (bond_dev->flags & IFF_PROMISC) {
1811 dev_set_promiscuity(slave_dev, -1);
1812 }
1813
1814 /* unset allmulti level from slave */
1815 if (bond_dev->flags & IFF_ALLMULTI) {
1816 dev_set_allmulti(slave_dev, -1);
1817 }
1818
1819 /* flush master's mc_list from slave */
1820 bond_mc_list_flush(bond_dev, slave_dev);
1821 }
1822
1823 netdev_set_master(slave_dev, NULL);
1824
1825 /* close slave before restoring its mac address */
1826 dev_close(slave_dev);
1827
Jay Vosburghdd957c52007-10-09 19:57:24 -07001828 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001829 /* restore original ("permanent") mac address */
1830 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1831 addr.sa_family = slave_dev->type;
1832 dev_set_mac_address(slave_dev, &addr);
1833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001835 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001836 IFF_SLAVE_INACTIVE | IFF_BONDING |
1837 IFF_SLAVE_NEEDARP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 kfree(slave);
1840
1841 return 0; /* deletion OK */
1842}
1843
1844/*
Moni Shouad90a1622007-10-09 19:43:43 -07001845* Destroy a bonding device.
1846* Must be under rtnl_lock when this function is called.
1847*/
1848void bond_destroy(struct bonding *bond)
1849{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001850 unregister_netdevice(bond->dev);
Moni Shouad90a1622007-10-09 19:43:43 -07001851 bond_deinit(bond->dev);
1852 bond_destroy_sysfs_entry(bond);
Moni Shouad90a1622007-10-09 19:43:43 -07001853}
1854
1855/*
1856* First release a slave and than destroy the bond if no more slaves iare left.
1857* Must be under rtnl_lock when this function is called.
1858*/
1859int bond_release_and_destroy(struct net_device *bond_dev, struct net_device *slave_dev)
1860{
1861 struct bonding *bond = bond_dev->priv;
1862 int ret;
1863
1864 ret = bond_release(bond_dev, slave_dev);
1865 if ((ret == 0) && (bond->slave_cnt == 0)) {
1866 printk(KERN_INFO DRV_NAME ": %s: destroying bond %s.\n",
1867 bond_dev->name, bond_dev->name);
1868 bond_destroy(bond);
1869 }
1870 return ret;
1871}
1872
1873/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 * This function releases all slaves.
1875 */
1876static int bond_release_all(struct net_device *bond_dev)
1877{
1878 struct bonding *bond = bond_dev->priv;
1879 struct slave *slave;
1880 struct net_device *slave_dev;
1881 struct sockaddr addr;
1882
1883 write_lock_bh(&bond->lock);
1884
Jay Vosburghff59c452006-03-27 13:27:43 -08001885 netif_carrier_off(bond_dev);
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 if (bond->slave_cnt == 0) {
1888 goto out;
1889 }
1890
1891 bond->current_arp_slave = NULL;
1892 bond->primary_slave = NULL;
1893 bond_change_active_slave(bond, NULL);
1894
1895 while ((slave = bond->first_slave) != NULL) {
1896 /* Inform AD package of unbinding of slave
1897 * before slave is detached from the list.
1898 */
1899 if (bond->params.mode == BOND_MODE_8023AD) {
1900 bond_3ad_unbind_slave(slave);
1901 }
1902
1903 slave_dev = slave->dev;
1904 bond_detach_slave(bond, slave);
1905
1906 if ((bond->params.mode == BOND_MODE_TLB) ||
1907 (bond->params.mode == BOND_MODE_ALB)) {
1908 /* must be called only after the slave
1909 * has been detached from the list
1910 */
1911 bond_alb_deinit_slave(bond, slave);
1912 }
1913
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001914 bond_compute_features(bond);
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 /* now that the slave is detached, unlock and perform
1917 * all the undo steps that should not be called from
1918 * within a lock.
1919 */
1920 write_unlock_bh(&bond->lock);
1921
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001922 bond_destroy_slave_symlinks(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 bond_del_vlans_from_slave(bond, slave_dev);
1924
1925 /* If the mode USES_PRIMARY, then we should only remove its
1926 * promisc and mc settings if it was the curr_active_slave, but that was
1927 * already taken care of above when we detached the slave
1928 */
1929 if (!USES_PRIMARY(bond->params.mode)) {
1930 /* unset promiscuity level from slave */
1931 if (bond_dev->flags & IFF_PROMISC) {
1932 dev_set_promiscuity(slave_dev, -1);
1933 }
1934
1935 /* unset allmulti level from slave */
1936 if (bond_dev->flags & IFF_ALLMULTI) {
1937 dev_set_allmulti(slave_dev, -1);
1938 }
1939
1940 /* flush master's mc_list from slave */
1941 bond_mc_list_flush(bond_dev, slave_dev);
1942 }
1943
1944 netdev_set_master(slave_dev, NULL);
1945
1946 /* close slave before restoring its mac address */
1947 dev_close(slave_dev);
1948
Jay Vosburghdd957c52007-10-09 19:57:24 -07001949 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001950 /* restore original ("permanent") mac address*/
1951 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1952 addr.sa_family = slave_dev->type;
1953 dev_set_mac_address(slave_dev, &addr);
1954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001956 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
1957 IFF_SLAVE_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 kfree(slave);
1960
1961 /* re-acquire the lock before getting the next slave */
1962 write_lock_bh(&bond->lock);
1963 }
1964
1965 /* zero the mac address of the master so it will be
1966 * set by the application to the mac address of the
1967 * first slave
1968 */
1969 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1970
1971 if (list_empty(&bond->vlan_list)) {
1972 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1973 } else {
1974 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001975 ": %s: Warning: clearing HW address of %s while it "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 "still has VLANs.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001977 bond_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001979 ": %s: When re-adding slaves, make sure the bond's "
1980 "HW address matches its VLANs'.\n",
1981 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
1983
1984 printk(KERN_INFO DRV_NAME
1985 ": %s: released all slaves\n",
1986 bond_dev->name);
1987
1988out:
1989 write_unlock_bh(&bond->lock);
1990
1991 return 0;
1992}
1993
1994/*
1995 * This function changes the active slave to slave <slave_dev>.
1996 * It returns -EINVAL in the following cases.
1997 * - <slave_dev> is not found in the list.
1998 * - There is not active slave now.
1999 * - <slave_dev> is already active.
2000 * - The link state of <slave_dev> is not BOND_LINK_UP.
2001 * - <slave_dev> is not running.
2002 * In these cases, this fuction does nothing.
2003 * In the other cases, currnt_slave pointer is changed and 0 is returned.
2004 */
2005static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2006{
2007 struct bonding *bond = bond_dev->priv;
2008 struct slave *old_active = NULL;
2009 struct slave *new_active = NULL;
2010 int res = 0;
2011
2012 if (!USES_PRIMARY(bond->params.mode)) {
2013 return -EINVAL;
2014 }
2015
2016 /* Verify that master_dev is indeed the master of slave_dev */
2017 if (!(slave_dev->flags & IFF_SLAVE) ||
2018 (slave_dev->master != bond_dev)) {
2019 return -EINVAL;
2020 }
2021
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002022 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002024 read_lock(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 old_active = bond->curr_active_slave;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002026 read_unlock(&bond->curr_slave_lock);
2027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 new_active = bond_get_slave_by_dev(bond, slave_dev);
2029
2030 /*
2031 * Changing to the current active: do nothing; return success.
2032 */
2033 if (new_active && (new_active == old_active)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002034 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 return 0;
2036 }
2037
2038 if ((new_active) &&
2039 (old_active) &&
2040 (new_active->link == BOND_LINK_UP) &&
2041 IS_UP(new_active->dev)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002042 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 bond_change_active_slave(bond, new_active);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002044 write_unlock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 } else {
2046 res = -EINVAL;
2047 }
2048
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002049 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051 return res;
2052}
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2055{
2056 struct bonding *bond = bond_dev->priv;
2057
2058 info->bond_mode = bond->params.mode;
2059 info->miimon = bond->params.miimon;
2060
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002061 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 info->num_slaves = bond->slave_cnt;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002063 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 return 0;
2066}
2067
2068static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2069{
2070 struct bonding *bond = bond_dev->priv;
2071 struct slave *slave;
2072 int i, found = 0;
2073
2074 if (info->slave_id < 0) {
2075 return -ENODEV;
2076 }
2077
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002078 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 bond_for_each_slave(bond, slave, i) {
2081 if (i == (int)info->slave_id) {
2082 found = 1;
2083 break;
2084 }
2085 }
2086
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002087 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 if (found) {
2090 strcpy(info->slave_name, slave->dev->name);
2091 info->link = slave->link;
2092 info->state = slave->state;
2093 info->link_failure_count = slave->link_failure_count;
2094 } else {
2095 return -ENODEV;
2096 }
2097
2098 return 0;
2099}
2100
2101/*-------------------------------- Monitoring -------------------------------*/
2102
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002103/*
2104 * if !have_locks, return nonzero if a failover is necessary. if
2105 * have_locks, do whatever failover activities are needed.
2106 *
2107 * This is to separate the inspection and failover steps for locking
2108 * purposes; failover requires rtnl, but acquiring it for every
2109 * inspection is undesirable, so a wrapper first does inspection, and
2110 * the acquires the necessary locks and calls again to perform
2111 * failover if needed. Since all locks are dropped, a complete
2112 * restart is needed between calls.
2113 */
2114static int __bond_mii_monitor(struct bonding *bond, int have_locks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 struct slave *slave, *oldcurrent;
2117 int do_failover = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 int i;
2119
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002120 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
2123 /* we will try to read the link status of each of our slaves, and
2124 * set their IFF_RUNNING flag appropriately. For each slave not
2125 * supporting MII status, we won't do anything so that a user-space
2126 * program could monitor the link itself if needed.
2127 */
2128
Moni Shoua1053f622007-10-09 19:43:42 -07002129 if (bond->send_grat_arp) {
2130 if (bond->curr_active_slave && test_bit(__LINK_STATE_LINKWATCH_PENDING,
2131 &bond->curr_active_slave->dev->state))
2132 dprintk("Needs to send gratuitous arp but not yet\n");
2133 else {
2134 dprintk("sending delayed gratuitous arp on on %s\n",
2135 bond->curr_active_slave->dev->name);
2136 bond_send_gratuitous_arp(bond);
2137 bond->send_grat_arp = 0;
2138 }
2139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 read_lock(&bond->curr_slave_lock);
2141 oldcurrent = bond->curr_active_slave;
2142 read_unlock(&bond->curr_slave_lock);
2143
2144 bond_for_each_slave(bond, slave, i) {
2145 struct net_device *slave_dev = slave->dev;
2146 int link_state;
2147 u16 old_speed = slave->speed;
2148 u8 old_duplex = slave->duplex;
2149
2150 link_state = bond_check_dev_link(bond, slave_dev, 0);
2151
2152 switch (slave->link) {
2153 case BOND_LINK_UP: /* the link was up */
2154 if (link_state == BMSR_LSTATUS) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002155 if (!oldcurrent) {
2156 if (!have_locks)
2157 return 1;
2158 do_failover = 1;
2159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 break;
2161 } else { /* link going down */
2162 slave->link = BOND_LINK_FAIL;
2163 slave->delay = bond->params.downdelay;
2164
2165 if (slave->link_failure_count < UINT_MAX) {
2166 slave->link_failure_count++;
2167 }
2168
2169 if (bond->params.downdelay) {
2170 printk(KERN_INFO DRV_NAME
2171 ": %s: link status down for %s "
2172 "interface %s, disabling it in "
2173 "%d ms.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002174 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 IS_UP(slave_dev)
2176 ? ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
2177 ? ((slave == oldcurrent)
2178 ? "active " : "backup ")
2179 : "")
2180 : "idle ",
2181 slave_dev->name,
2182 bond->params.downdelay * bond->params.miimon);
2183 }
2184 }
2185 /* no break ! fall through the BOND_LINK_FAIL test to
2186 ensure proper action to be taken
2187 */
2188 case BOND_LINK_FAIL: /* the link has just gone down */
2189 if (link_state != BMSR_LSTATUS) {
2190 /* link stays down */
2191 if (slave->delay <= 0) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002192 if (!have_locks)
2193 return 1;
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 /* link down for too long time */
2196 slave->link = BOND_LINK_DOWN;
2197
2198 /* in active/backup mode, we must
2199 * completely disable this interface
2200 */
2201 if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP) ||
2202 (bond->params.mode == BOND_MODE_8023AD)) {
2203 bond_set_slave_inactive_flags(slave);
2204 }
2205
2206 printk(KERN_INFO DRV_NAME
2207 ": %s: link status definitely "
2208 "down for interface %s, "
2209 "disabling it\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002210 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 slave_dev->name);
2212
2213 /* notify ad that the link status has changed */
2214 if (bond->params.mode == BOND_MODE_8023AD) {
2215 bond_3ad_handle_link_change(slave, BOND_LINK_DOWN);
2216 }
2217
2218 if ((bond->params.mode == BOND_MODE_TLB) ||
2219 (bond->params.mode == BOND_MODE_ALB)) {
2220 bond_alb_handle_link_change(bond, slave, BOND_LINK_DOWN);
2221 }
2222
2223 if (slave == oldcurrent) {
2224 do_failover = 1;
2225 }
2226 } else {
2227 slave->delay--;
2228 }
2229 } else {
2230 /* link up again */
2231 slave->link = BOND_LINK_UP;
2232 slave->jiffies = jiffies;
2233 printk(KERN_INFO DRV_NAME
2234 ": %s: link status up again after %d "
2235 "ms for interface %s.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002236 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 (bond->params.downdelay - slave->delay) * bond->params.miimon,
2238 slave_dev->name);
2239 }
2240 break;
2241 case BOND_LINK_DOWN: /* the link was down */
2242 if (link_state != BMSR_LSTATUS) {
2243 /* the link stays down, nothing more to do */
2244 break;
2245 } else { /* link going up */
2246 slave->link = BOND_LINK_BACK;
2247 slave->delay = bond->params.updelay;
2248
2249 if (bond->params.updelay) {
2250 /* if updelay == 0, no need to
2251 advertise about a 0 ms delay */
2252 printk(KERN_INFO DRV_NAME
2253 ": %s: link status up for "
2254 "interface %s, enabling it "
2255 "in %d ms.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002256 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 slave_dev->name,
2258 bond->params.updelay * bond->params.miimon);
2259 }
2260 }
2261 /* no break ! fall through the BOND_LINK_BACK state in
2262 case there's something to do.
2263 */
2264 case BOND_LINK_BACK: /* the link has just come back */
2265 if (link_state != BMSR_LSTATUS) {
2266 /* link down again */
2267 slave->link = BOND_LINK_DOWN;
2268
2269 printk(KERN_INFO DRV_NAME
2270 ": %s: link status down again after %d "
2271 "ms for interface %s.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002272 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 (bond->params.updelay - slave->delay) * bond->params.miimon,
2274 slave_dev->name);
2275 } else {
2276 /* link stays up */
2277 if (slave->delay == 0) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002278 if (!have_locks)
2279 return 1;
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 /* now the link has been up for long time enough */
2282 slave->link = BOND_LINK_UP;
2283 slave->jiffies = jiffies;
2284
2285 if (bond->params.mode == BOND_MODE_8023AD) {
2286 /* prevent it from being the active one */
2287 slave->state = BOND_STATE_BACKUP;
2288 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2289 /* make it immediately active */
2290 slave->state = BOND_STATE_ACTIVE;
2291 } else if (slave != bond->primary_slave) {
2292 /* prevent it from being the active one */
2293 slave->state = BOND_STATE_BACKUP;
2294 }
2295
2296 printk(KERN_INFO DRV_NAME
2297 ": %s: link status definitely "
2298 "up for interface %s.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002299 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 slave_dev->name);
2301
2302 /* notify ad that the link status has changed */
2303 if (bond->params.mode == BOND_MODE_8023AD) {
2304 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2305 }
2306
2307 if ((bond->params.mode == BOND_MODE_TLB) ||
2308 (bond->params.mode == BOND_MODE_ALB)) {
2309 bond_alb_handle_link_change(bond, slave, BOND_LINK_UP);
2310 }
2311
2312 if ((!oldcurrent) ||
2313 (slave == bond->primary_slave)) {
2314 do_failover = 1;
2315 }
2316 } else {
2317 slave->delay--;
2318 }
2319 }
2320 break;
2321 default:
2322 /* Should not happen */
Mitch Williams4e0952c2005-11-09 10:34:57 -08002323 printk(KERN_ERR DRV_NAME
2324 ": %s: Error: %s Illegal value (link=%d)\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002325 bond->dev->name,
Mitch Williams4e0952c2005-11-09 10:34:57 -08002326 slave->dev->name,
2327 slave->link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 goto out;
2329 } /* end of switch (slave->link) */
2330
2331 bond_update_speed_duplex(slave);
2332
2333 if (bond->params.mode == BOND_MODE_8023AD) {
2334 if (old_speed != slave->speed) {
2335 bond_3ad_adapter_speed_changed(slave);
2336 }
2337
2338 if (old_duplex != slave->duplex) {
2339 bond_3ad_adapter_duplex_changed(slave);
2340 }
2341 }
2342
2343 } /* end of for */
2344
2345 if (do_failover) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002346 ASSERT_RTNL();
2347
2348 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 bond_select_active_slave(bond);
2351
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002352 write_unlock_bh(&bond->curr_slave_lock);
2353
Jay Vosburghff59c452006-03-27 13:27:43 -08002354 } else
2355 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357out:
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359}
2360
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002361/*
2362 * bond_mii_monitor
2363 *
2364 * Really a wrapper that splits the mii monitor into two phases: an
2365 * inspection, then (if inspection indicates something needs to be
2366 * done) an acquisition of appropriate locks followed by another pass
2367 * to implement whatever link state changes are indicated.
2368 */
2369void bond_mii_monitor(struct work_struct *work)
2370{
2371 struct bonding *bond = container_of(work, struct bonding,
2372 mii_work.work);
2373 unsigned long delay;
2374
2375 read_lock(&bond->lock);
2376 if (bond->kill_timers) {
2377 read_unlock(&bond->lock);
2378 return;
2379 }
2380 if (__bond_mii_monitor(bond, 0)) {
2381 read_unlock(&bond->lock);
2382 rtnl_lock();
2383 read_lock(&bond->lock);
2384 __bond_mii_monitor(bond, 1);
2385 rtnl_unlock();
2386 }
2387
2388 delay = ((bond->params.miimon * HZ) / 1000) ? : 1;
2389 read_unlock(&bond->lock);
2390 queue_delayed_work(bond->wq, &bond->mii_work, delay);
2391}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002392
Al Virod3bb52b2007-08-22 20:06:58 -04002393static __be32 bond_glean_dev_ip(struct net_device *dev)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002394{
2395 struct in_device *idev;
2396 struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07002397 __be32 addr = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002398
2399 if (!dev)
2400 return 0;
2401
2402 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002403 idev = __in_dev_get_rcu(dev);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002404 if (!idev)
2405 goto out;
2406
2407 ifa = idev->ifa_list;
2408 if (!ifa)
2409 goto out;
2410
2411 addr = ifa->ifa_local;
2412out:
2413 rcu_read_unlock();
2414 return addr;
2415}
2416
2417static int bond_has_ip(struct bonding *bond)
2418{
2419 struct vlan_entry *vlan, *vlan_next;
2420
2421 if (bond->master_ip)
2422 return 1;
2423
2424 if (list_empty(&bond->vlan_list))
2425 return 0;
2426
2427 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2428 vlan_list) {
2429 if (vlan->vlan_ip)
2430 return 1;
2431 }
2432
2433 return 0;
2434}
2435
Al Virod3bb52b2007-08-22 20:06:58 -04002436static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002437{
2438 struct vlan_entry *vlan, *vlan_next;
2439
2440 if (ip == bond->master_ip)
2441 return 1;
2442
2443 if (list_empty(&bond->vlan_list))
2444 return 0;
2445
2446 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2447 vlan_list) {
2448 if (ip == vlan->vlan_ip)
2449 return 1;
2450 }
2451
2452 return 0;
2453}
2454
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002455/*
2456 * We go to the (large) trouble of VLAN tagging ARP frames because
2457 * switches in VLAN mode (especially if ports are configured as
2458 * "native" to a VLAN) might not pass non-tagged frames.
2459 */
Al Virod3bb52b2007-08-22 20:06:58 -04002460static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002461{
2462 struct sk_buff *skb;
2463
2464 dprintk("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
2465 slave_dev->name, dest_ip, src_ip, vlan_id);
2466
2467 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2468 NULL, slave_dev->dev_addr, NULL);
2469
2470 if (!skb) {
2471 printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
2472 return;
2473 }
2474 if (vlan_id) {
2475 skb = vlan_put_tag(skb, vlan_id);
2476 if (!skb) {
2477 printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
2478 return;
2479 }
2480 }
2481 arp_xmit(skb);
2482}
2483
2484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2486{
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002487 int i, vlan_id, rv;
Al Virod3bb52b2007-08-22 20:06:58 -04002488 __be32 *targets = bond->params.arp_targets;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002489 struct vlan_entry *vlan, *vlan_next;
2490 struct net_device *vlan_dev;
2491 struct flowi fl;
2492 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Mitch Williams6b780562005-11-09 10:36:19 -08002494 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2495 if (!targets[i])
2496 continue;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002497 dprintk("basa: target %x\n", targets[i]);
2498 if (list_empty(&bond->vlan_list)) {
2499 dprintk("basa: empty vlan: arp_send\n");
2500 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2501 bond->master_ip, 0);
2502 continue;
2503 }
2504
2505 /*
2506 * If VLANs are configured, we do a route lookup to
2507 * determine which VLAN interface would be used, so we
2508 * can tag the ARP with the proper VLAN tag.
2509 */
2510 memset(&fl, 0, sizeof(fl));
2511 fl.fl4_dst = targets[i];
2512 fl.fl4_tos = RTO_ONLINK;
2513
2514 rv = ip_route_output_key(&rt, &fl);
2515 if (rv) {
2516 if (net_ratelimit()) {
2517 printk(KERN_WARNING DRV_NAME
2518 ": %s: no route to arp_ip_target %u.%u.%u.%u\n",
2519 bond->dev->name, NIPQUAD(fl.fl4_dst));
2520 }
2521 continue;
2522 }
2523
2524 /*
2525 * This target is not on a VLAN
2526 */
2527 if (rt->u.dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002528 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002529 dprintk("basa: rtdev == bond->dev: arp_send\n");
2530 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2531 bond->master_ip, 0);
2532 continue;
2533 }
2534
2535 vlan_id = 0;
2536 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
2537 vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002538 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002539 if (vlan_dev == rt->u.dst.dev) {
2540 vlan_id = vlan->vlan_id;
2541 dprintk("basa: vlan match on %s %d\n",
2542 vlan_dev->name, vlan_id);
2543 break;
2544 }
2545 }
2546
2547 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002548 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002549 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2550 vlan->vlan_ip, vlan_id);
2551 continue;
2552 }
2553
2554 if (net_ratelimit()) {
2555 printk(KERN_WARNING DRV_NAME
2556 ": %s: no path to arp_ip_target %u.%u.%u.%u via rt.dev %s\n",
2557 bond->dev->name, NIPQUAD(fl.fl4_dst),
2558 rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2559 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002560 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002561 }
2562}
2563
2564/*
2565 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2566 * for each VLAN above us.
2567 */
2568static void bond_send_gratuitous_arp(struct bonding *bond)
2569{
2570 struct slave *slave = bond->curr_active_slave;
2571 struct vlan_entry *vlan;
2572 struct net_device *vlan_dev;
2573
2574 dprintk("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
2575 slave ? slave->dev->name : "NULL");
2576 if (!slave)
2577 return;
2578
2579 if (bond->master_ip) {
2580 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
Moni Shoua1053f622007-10-09 19:43:42 -07002581 bond->master_ip, 0);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002582 }
2583
2584 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002585 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002586 if (vlan->vlan_ip) {
2587 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2588 vlan->vlan_ip, vlan->vlan_id);
2589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 }
2591}
2592
Al Virod3bb52b2007-08-22 20:06:58 -04002593static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002594{
2595 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002596 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002597
2598 targets = bond->params.arp_targets;
2599 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
2600 dprintk("bva: sip %u.%u.%u.%u tip %u.%u.%u.%u t[%d] "
2601 "%u.%u.%u.%u bhti(tip) %d\n",
2602 NIPQUAD(sip), NIPQUAD(tip), i, NIPQUAD(targets[i]),
2603 bond_has_this_ip(bond, tip));
2604 if (sip == targets[i]) {
2605 if (bond_has_this_ip(bond, tip))
2606 slave->last_arp_rx = jiffies;
2607 return;
2608 }
2609 }
2610}
2611
2612static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2613{
2614 struct arphdr *arp;
2615 struct slave *slave;
2616 struct bonding *bond;
2617 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002618 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002619
Eric W. Biedermane730c152007-09-17 11:53:39 -07002620 if (dev->nd_net != &init_net)
2621 goto out;
2622
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002623 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2624 goto out;
2625
2626 bond = dev->priv;
2627 read_lock(&bond->lock);
2628
2629 dprintk("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
2630 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2631 orig_dev ? orig_dev->name : "NULL");
2632
2633 slave = bond_get_slave_by_dev(bond, orig_dev);
2634 if (!slave || !slave_do_arp_validate(bond, slave))
2635 goto out_unlock;
2636
2637 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
2638 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
2639 (2 * dev->addr_len) +
2640 (2 * sizeof(u32)))))
2641 goto out_unlock;
2642
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002643 arp = arp_hdr(skb);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002644 if (arp->ar_hln != dev->addr_len ||
2645 skb->pkt_type == PACKET_OTHERHOST ||
2646 skb->pkt_type == PACKET_LOOPBACK ||
2647 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2648 arp->ar_pro != htons(ETH_P_IP) ||
2649 arp->ar_pln != 4)
2650 goto out_unlock;
2651
2652 arp_ptr = (unsigned char *)(arp + 1);
2653 arp_ptr += dev->addr_len;
2654 memcpy(&sip, arp_ptr, 4);
2655 arp_ptr += 4 + dev->addr_len;
2656 memcpy(&tip, arp_ptr, 4);
2657
2658 dprintk("bond_arp_rcv: %s %s/%d av %d sv %d sip %u.%u.%u.%u"
2659 " tip %u.%u.%u.%u\n", bond->dev->name, slave->dev->name,
2660 slave->state, bond->params.arp_validate,
2661 slave_do_arp_validate(bond, slave), NIPQUAD(sip), NIPQUAD(tip));
2662
2663 /*
2664 * Backup slaves won't see the ARP reply, but do come through
2665 * here for each ARP probe (so we swap the sip/tip to validate
2666 * the probe). In a "redundant switch, common router" type of
2667 * configuration, the ARP probe will (hopefully) travel from
2668 * the active, through one switch, the router, then the other
2669 * switch before reaching the backup.
2670 */
2671 if (slave->state == BOND_STATE_ACTIVE)
2672 bond_validate_arp(bond, slave, sip, tip);
2673 else
2674 bond_validate_arp(bond, slave, tip, sip);
2675
2676out_unlock:
2677 read_unlock(&bond->lock);
2678out:
2679 dev_kfree_skb(skb);
2680 return NET_RX_SUCCESS;
2681}
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683/*
2684 * this function is called regularly to monitor each slave's link
2685 * ensuring that traffic is being sent and received when arp monitoring
2686 * is used in load-balancing mode. if the adapter has been dormant, then an
2687 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2688 * arp monitoring in active backup mode.
2689 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002690void bond_loadbalance_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002692 struct bonding *bond = container_of(work, struct bonding,
2693 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 struct slave *slave, *oldcurrent;
2695 int do_failover = 0;
2696 int delta_in_ticks;
2697 int i;
2698
2699 read_lock(&bond->lock);
2700
2701 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2702
2703 if (bond->kill_timers) {
2704 goto out;
2705 }
2706
2707 if (bond->slave_cnt == 0) {
2708 goto re_arm;
2709 }
2710
2711 read_lock(&bond->curr_slave_lock);
2712 oldcurrent = bond->curr_active_slave;
2713 read_unlock(&bond->curr_slave_lock);
2714
2715 /* see if any of the previous devices are up now (i.e. they have
2716 * xmt and rcv traffic). the curr_active_slave does not come into
2717 * the picture unless it is null. also, slave->jiffies is not needed
2718 * here because we send an arp on each slave and give a slave as
2719 * long as it needs to get the tx/rx within the delta.
2720 * TODO: what about up/down delay in arp mode? it wasn't here before
2721 * so it can wait
2722 */
2723 bond_for_each_slave(bond, slave, i) {
2724 if (slave->link != BOND_LINK_UP) {
2725 if (((jiffies - slave->dev->trans_start) <= delta_in_ticks) &&
2726 ((jiffies - slave->dev->last_rx) <= delta_in_ticks)) {
2727
2728 slave->link = BOND_LINK_UP;
2729 slave->state = BOND_STATE_ACTIVE;
2730
2731 /* primary_slave has no meaning in round-robin
2732 * mode. the window of a slave being up and
2733 * curr_active_slave being null after enslaving
2734 * is closed.
2735 */
2736 if (!oldcurrent) {
2737 printk(KERN_INFO DRV_NAME
2738 ": %s: link status definitely "
2739 "up for interface %s, ",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002740 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 slave->dev->name);
2742 do_failover = 1;
2743 } else {
2744 printk(KERN_INFO DRV_NAME
2745 ": %s: interface %s is now up\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002746 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 slave->dev->name);
2748 }
2749 }
2750 } else {
2751 /* slave->link == BOND_LINK_UP */
2752
2753 /* not all switches will respond to an arp request
2754 * when the source ip is 0, so don't take the link down
2755 * if we don't know our ip yet
2756 */
2757 if (((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
2758 (((jiffies - slave->dev->last_rx) >= (2*delta_in_ticks)) &&
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002759 bond_has_ip(bond))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
2761 slave->link = BOND_LINK_DOWN;
2762 slave->state = BOND_STATE_BACKUP;
2763
2764 if (slave->link_failure_count < UINT_MAX) {
2765 slave->link_failure_count++;
2766 }
2767
2768 printk(KERN_INFO DRV_NAME
2769 ": %s: interface %s is now down.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002770 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 slave->dev->name);
2772
2773 if (slave == oldcurrent) {
2774 do_failover = 1;
2775 }
2776 }
2777 }
2778
2779 /* note: if switch is in round-robin mode, all links
2780 * must tx arp to ensure all links rx an arp - otherwise
2781 * links may oscillate or not come up at all; if switch is
2782 * in something like xor mode, there is nothing we can
2783 * do - all replies will be rx'ed on same link causing slaves
2784 * to be unstable during low/no traffic periods
2785 */
2786 if (IS_UP(slave->dev)) {
2787 bond_arp_send_all(bond, slave);
2788 }
2789 }
2790
2791 if (do_failover) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002792 rtnl_lock();
2793 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
2795 bond_select_active_slave(bond);
2796
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002797 write_unlock_bh(&bond->curr_slave_lock);
2798 rtnl_unlock();
2799
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 }
2801
2802re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002803 if (bond->params.arp_interval)
2804 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805out:
2806 read_unlock(&bond->lock);
2807}
2808
2809/*
2810 * When using arp monitoring in active-backup mode, this function is
2811 * called to determine if any backup slaves have went down or a new
2812 * current slave needs to be found.
2813 * The backup slaves never generate traffic, they are considered up by merely
2814 * receiving traffic. If the current slave goes down, each backup slave will
2815 * be given the opportunity to tx/rx an arp before being taken down - this
2816 * prevents all slaves from being taken down due to the current slave not
2817 * sending any traffic for the backups to receive. The arps are not necessarily
2818 * necessary, any tx and rx traffic will keep the current slave up. While any
2819 * rx traffic will keep the backup slaves up, the current slave is responsible
2820 * for generating traffic to keep them up regardless of any other traffic they
2821 * may have received.
2822 * see loadbalance_arp_monitor for arp monitoring in load balancing mode
2823 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002824void bond_activebackup_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002826 struct bonding *bond = container_of(work, struct bonding,
2827 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 struct slave *slave;
2829 int delta_in_ticks;
2830 int i;
2831
2832 read_lock(&bond->lock);
2833
2834 delta_in_ticks = (bond->params.arp_interval * HZ) / 1000;
2835
2836 if (bond->kill_timers) {
2837 goto out;
2838 }
2839
2840 if (bond->slave_cnt == 0) {
2841 goto re_arm;
2842 }
2843
2844 /* determine if any slave has come up or any backup slave has
2845 * gone down
2846 * TODO: what about up/down delay in arp mode? it wasn't here before
2847 * so it can wait
2848 */
2849 bond_for_each_slave(bond, slave, i) {
2850 if (slave->link != BOND_LINK_UP) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002851 if ((jiffies - slave_last_rx(bond, slave)) <=
2852 delta_in_ticks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
2854 slave->link = BOND_LINK_UP;
2855
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002856 rtnl_lock();
2857
2858 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 if ((!bond->curr_active_slave) &&
2861 ((jiffies - slave->dev->trans_start) <= delta_in_ticks)) {
2862 bond_change_active_slave(bond, slave);
2863 bond->current_arp_slave = NULL;
2864 } else if (bond->curr_active_slave != slave) {
2865 /* this slave has just come up but we
2866 * already have a current slave; this
2867 * can also happen if bond_enslave adds
2868 * a new slave that is up while we are
2869 * searching for a new slave
2870 */
2871 bond_set_slave_inactive_flags(slave);
2872 bond->current_arp_slave = NULL;
2873 }
2874
Jay Vosburghff59c452006-03-27 13:27:43 -08002875 bond_set_carrier(bond);
2876
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (slave == bond->curr_active_slave) {
2878 printk(KERN_INFO DRV_NAME
2879 ": %s: %s is up and now the "
2880 "active interface\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002881 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 slave->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08002883 netif_carrier_on(bond->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 } else {
2885 printk(KERN_INFO DRV_NAME
2886 ": %s: backup interface %s is "
2887 "now up\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002888 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 slave->dev->name);
2890 }
2891
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002892 write_unlock_bh(&bond->curr_slave_lock);
2893 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
2895 } else {
2896 read_lock(&bond->curr_slave_lock);
2897
2898 if ((slave != bond->curr_active_slave) &&
2899 (!bond->current_arp_slave) &&
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002900 (((jiffies - slave_last_rx(bond, slave)) >= 3*delta_in_ticks) &&
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002901 bond_has_ip(bond))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 /* a backup slave has gone down; three times
2903 * the delta allows the current slave to be
2904 * taken out before the backup slave.
2905 * note: a non-null current_arp_slave indicates
2906 * the curr_active_slave went down and we are
2907 * searching for a new one; under this
2908 * condition we only take the curr_active_slave
2909 * down - this gives each slave a chance to
2910 * tx/rx traffic before being taken out
2911 */
2912
2913 read_unlock(&bond->curr_slave_lock);
2914
2915 slave->link = BOND_LINK_DOWN;
2916
2917 if (slave->link_failure_count < UINT_MAX) {
2918 slave->link_failure_count++;
2919 }
2920
2921 bond_set_slave_inactive_flags(slave);
2922
2923 printk(KERN_INFO DRV_NAME
2924 ": %s: backup interface %s is now down\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002925 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 slave->dev->name);
2927 } else {
2928 read_unlock(&bond->curr_slave_lock);
2929 }
2930 }
2931 }
2932
2933 read_lock(&bond->curr_slave_lock);
2934 slave = bond->curr_active_slave;
2935 read_unlock(&bond->curr_slave_lock);
2936
2937 if (slave) {
2938 /* if we have sent traffic in the past 2*arp_intervals but
2939 * haven't xmit and rx traffic in that time interval, select
2940 * a different slave. slave->jiffies is only updated when
2941 * a slave first becomes the curr_active_slave - not necessarily
2942 * after every arp; this ensures the slave has a full 2*delta
2943 * before being taken out. if a primary is being used, check
2944 * if it is up and needs to take over as the curr_active_slave
2945 */
2946 if ((((jiffies - slave->dev->trans_start) >= (2*delta_in_ticks)) ||
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002947 (((jiffies - slave_last_rx(bond, slave)) >= (2*delta_in_ticks)) &&
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002948 bond_has_ip(bond))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 ((jiffies - slave->jiffies) >= 2*delta_in_ticks)) {
2950
2951 slave->link = BOND_LINK_DOWN;
2952
2953 if (slave->link_failure_count < UINT_MAX) {
2954 slave->link_failure_count++;
2955 }
2956
2957 printk(KERN_INFO DRV_NAME
2958 ": %s: link status down for active interface "
2959 "%s, disabling it\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002960 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 slave->dev->name);
2962
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002963 rtnl_lock();
2964 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
2966 bond_select_active_slave(bond);
2967 slave = bond->curr_active_slave;
2968
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002969 write_unlock_bh(&bond->curr_slave_lock);
2970
2971 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973 bond->current_arp_slave = slave;
2974
2975 if (slave) {
2976 slave->jiffies = jiffies;
2977 }
2978 } else if ((bond->primary_slave) &&
2979 (bond->primary_slave != slave) &&
2980 (bond->primary_slave->link == BOND_LINK_UP)) {
2981 /* at this point, slave is the curr_active_slave */
2982 printk(KERN_INFO DRV_NAME
2983 ": %s: changing from interface %s to primary "
2984 "interface %s\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002985 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 slave->dev->name,
2987 bond->primary_slave->dev->name);
2988
2989 /* primary is up so switch to it */
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002990 rtnl_lock();
2991 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 bond_change_active_slave(bond, bond->primary_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002993 write_unlock_bh(&bond->curr_slave_lock);
2994
2995 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
2997 slave = bond->primary_slave;
2998 slave->jiffies = jiffies;
2999 } else {
3000 bond->current_arp_slave = NULL;
3001 }
3002
3003 /* the current slave must tx an arp to ensure backup slaves
3004 * rx traffic
3005 */
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003006 if (slave && bond_has_ip(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 bond_arp_send_all(bond, slave);
3008 }
3009 }
3010
3011 /* if we don't have a curr_active_slave, search for the next available
3012 * backup slave from the current_arp_slave and make it the candidate
3013 * for becoming the curr_active_slave
3014 */
3015 if (!slave) {
3016 if (!bond->current_arp_slave) {
3017 bond->current_arp_slave = bond->first_slave;
3018 }
3019
3020 if (bond->current_arp_slave) {
3021 bond_set_slave_inactive_flags(bond->current_arp_slave);
3022
3023 /* search for next candidate */
Jay Vosburgh2f872f02005-05-26 12:56:59 -07003024 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 if (IS_UP(slave->dev)) {
3026 slave->link = BOND_LINK_BACK;
3027 bond_set_slave_active_flags(slave);
3028 bond_arp_send_all(bond, slave);
3029 slave->jiffies = jiffies;
3030 bond->current_arp_slave = slave;
3031 break;
3032 }
3033
3034 /* if the link state is up at this point, we
3035 * mark it down - this can happen if we have
3036 * simultaneous link failures and
3037 * reselect_active_interface doesn't make this
3038 * one the current slave so it is still marked
3039 * up when it is actually down
3040 */
3041 if (slave->link == BOND_LINK_UP) {
3042 slave->link = BOND_LINK_DOWN;
3043 if (slave->link_failure_count < UINT_MAX) {
3044 slave->link_failure_count++;
3045 }
3046
3047 bond_set_slave_inactive_flags(slave);
3048
3049 printk(KERN_INFO DRV_NAME
3050 ": %s: backup interface %s is "
3051 "now down.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003052 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 slave->dev->name);
3054 }
3055 }
3056 }
3057 }
3058
3059re_arm:
3060 if (bond->params.arp_interval) {
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003061 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 }
3063out:
3064 read_unlock(&bond->lock);
3065}
3066
3067/*------------------------------ proc/seq_file-------------------------------*/
3068
3069#ifdef CONFIG_PROC_FS
3070
3071#define SEQ_START_TOKEN ((void *)1)
3072
3073static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
3074{
3075 struct bonding *bond = seq->private;
3076 loff_t off = 0;
3077 struct slave *slave;
3078 int i;
3079
3080 /* make sure the bond won't be taken away */
3081 read_lock(&dev_base_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003082 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
3084 if (*pos == 0) {
3085 return SEQ_START_TOKEN;
3086 }
3087
3088 bond_for_each_slave(bond, slave, i) {
3089 if (++off == *pos) {
3090 return slave;
3091 }
3092 }
3093
3094 return NULL;
3095}
3096
3097static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3098{
3099 struct bonding *bond = seq->private;
3100 struct slave *slave = v;
3101
3102 ++*pos;
3103 if (v == SEQ_START_TOKEN) {
3104 return bond->first_slave;
3105 }
3106
3107 slave = slave->next;
3108
3109 return (slave == bond->first_slave) ? NULL : slave;
3110}
3111
3112static void bond_info_seq_stop(struct seq_file *seq, void *v)
3113{
3114 struct bonding *bond = seq->private;
3115
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003116 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 read_unlock(&dev_base_lock);
3118}
3119
3120static void bond_info_show_master(struct seq_file *seq)
3121{
3122 struct bonding *bond = seq->private;
3123 struct slave *curr;
Mitch Williams4756b022005-11-09 10:36:25 -08003124 int i;
3125 u32 target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
3127 read_lock(&bond->curr_slave_lock);
3128 curr = bond->curr_active_slave;
3129 read_unlock(&bond->curr_slave_lock);
3130
Jay Vosburghdd957c52007-10-09 19:57:24 -07003131 seq_printf(seq, "Bonding Mode: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 bond_mode_name(bond->params.mode));
3133
Jay Vosburghdd957c52007-10-09 19:57:24 -07003134 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
3135 bond->params.fail_over_mac)
3136 seq_printf(seq, " (fail_over_mac)");
3137
3138 seq_printf(seq, "\n");
3139
Mitch Williamsc61b75a2005-11-09 10:35:13 -08003140 if (bond->params.mode == BOND_MODE_XOR ||
3141 bond->params.mode == BOND_MODE_8023AD) {
3142 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3143 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3144 bond->params.xmit_policy);
3145 }
3146
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 if (USES_PRIMARY(bond->params.mode)) {
3148 seq_printf(seq, "Primary Slave: %s\n",
Mitch Williams0f418b22005-11-09 10:35:21 -08003149 (bond->primary_slave) ?
3150 bond->primary_slave->dev->name : "None");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
3152 seq_printf(seq, "Currently Active Slave: %s\n",
3153 (curr) ? curr->dev->name : "None");
3154 }
3155
Jay Vosburghff59c452006-03-27 13:27:43 -08003156 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3157 "up" : "down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3159 seq_printf(seq, "Up Delay (ms): %d\n",
3160 bond->params.updelay * bond->params.miimon);
3161 seq_printf(seq, "Down Delay (ms): %d\n",
3162 bond->params.downdelay * bond->params.miimon);
3163
Mitch Williams4756b022005-11-09 10:36:25 -08003164
3165 /* ARP information */
3166 if(bond->params.arp_interval > 0) {
3167 int printed=0;
3168 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3169 bond->params.arp_interval);
3170
3171 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3172
3173 for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
3174 if (!bond->params.arp_targets[i])
3175 continue;
3176 if (printed)
3177 seq_printf(seq, ",");
3178 target = ntohl(bond->params.arp_targets[i]);
3179 seq_printf(seq, " %d.%d.%d.%d", HIPQUAD(target));
3180 printed = 1;
3181 }
3182 seq_printf(seq, "\n");
3183 }
3184
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 if (bond->params.mode == BOND_MODE_8023AD) {
3186 struct ad_info ad_info;
Joe Perches0795af52007-10-03 17:59:30 -07003187 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
3189 seq_puts(seq, "\n802.3ad info\n");
3190 seq_printf(seq, "LACP rate: %s\n",
3191 (bond->params.lacp_fast) ? "fast" : "slow");
3192
3193 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3194 seq_printf(seq, "bond %s has no active aggregator\n",
3195 bond->dev->name);
3196 } else {
3197 seq_printf(seq, "Active Aggregator Info:\n");
3198
3199 seq_printf(seq, "\tAggregator ID: %d\n",
3200 ad_info.aggregator_id);
3201 seq_printf(seq, "\tNumber of ports: %d\n",
3202 ad_info.ports);
3203 seq_printf(seq, "\tActor Key: %d\n",
3204 ad_info.actor_key);
3205 seq_printf(seq, "\tPartner Key: %d\n",
3206 ad_info.partner_key);
Joe Perches0795af52007-10-03 17:59:30 -07003207 seq_printf(seq, "\tPartner Mac Address: %s\n",
3208 print_mac(mac, ad_info.partner_system));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 }
3210 }
3211}
3212
3213static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
3214{
3215 struct bonding *bond = seq->private;
Joe Perches0795af52007-10-03 17:59:30 -07003216 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
3218 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3219 seq_printf(seq, "MII Status: %s\n",
3220 (slave->link == BOND_LINK_UP) ? "up" : "down");
Kenzo Iwami655096452006-09-22 21:53:08 -07003221 seq_printf(seq, "Link Failure Count: %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 slave->link_failure_count);
3223
Jay Vosburgh217df672005-09-26 16:11:50 -07003224 seq_printf(seq,
Joe Perches0795af52007-10-03 17:59:30 -07003225 "Permanent HW addr: %s\n",
3226 print_mac(mac, slave->perm_hwaddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
3228 if (bond->params.mode == BOND_MODE_8023AD) {
3229 const struct aggregator *agg
3230 = SLAVE_AD_INFO(slave).port.aggregator;
3231
3232 if (agg) {
3233 seq_printf(seq, "Aggregator ID: %d\n",
3234 agg->aggregator_identifier);
3235 } else {
3236 seq_puts(seq, "Aggregator ID: N/A\n");
3237 }
3238 }
3239}
3240
3241static int bond_info_seq_show(struct seq_file *seq, void *v)
3242{
3243 if (v == SEQ_START_TOKEN) {
3244 seq_printf(seq, "%s\n", version);
3245 bond_info_show_master(seq);
3246 } else {
3247 bond_info_show_slave(seq, v);
3248 }
3249
3250 return 0;
3251}
3252
3253static struct seq_operations bond_info_seq_ops = {
3254 .start = bond_info_seq_start,
3255 .next = bond_info_seq_next,
3256 .stop = bond_info_seq_stop,
3257 .show = bond_info_seq_show,
3258};
3259
3260static int bond_info_open(struct inode *inode, struct file *file)
3261{
3262 struct seq_file *seq;
3263 struct proc_dir_entry *proc;
3264 int res;
3265
3266 res = seq_open(file, &bond_info_seq_ops);
3267 if (!res) {
3268 /* recover the pointer buried in proc_dir_entry data */
3269 seq = file->private_data;
3270 proc = PDE(inode);
3271 seq->private = proc->data;
3272 }
3273
3274 return res;
3275}
3276
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08003277static const struct file_operations bond_info_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 .owner = THIS_MODULE,
3279 .open = bond_info_open,
3280 .read = seq_read,
3281 .llseek = seq_lseek,
3282 .release = seq_release,
3283};
3284
3285static int bond_create_proc_entry(struct bonding *bond)
3286{
3287 struct net_device *bond_dev = bond->dev;
3288
3289 if (bond_proc_dir) {
3290 bond->proc_entry = create_proc_entry(bond_dev->name,
3291 S_IRUGO,
3292 bond_proc_dir);
3293 if (bond->proc_entry == NULL) {
3294 printk(KERN_WARNING DRV_NAME
3295 ": Warning: Cannot create /proc/net/%s/%s\n",
3296 DRV_NAME, bond_dev->name);
3297 } else {
3298 bond->proc_entry->data = bond;
3299 bond->proc_entry->proc_fops = &bond_info_fops;
3300 bond->proc_entry->owner = THIS_MODULE;
3301 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
3302 }
3303 }
3304
3305 return 0;
3306}
3307
3308static void bond_remove_proc_entry(struct bonding *bond)
3309{
3310 if (bond_proc_dir && bond->proc_entry) {
3311 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3312 memset(bond->proc_file_name, 0, IFNAMSIZ);
3313 bond->proc_entry = NULL;
3314 }
3315}
3316
3317/* Create the bonding directory under /proc/net, if doesn't exist yet.
3318 * Caller must hold rtnl_lock.
3319 */
3320static void bond_create_proc_dir(void)
3321{
3322 int len = strlen(DRV_NAME);
3323
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003324 for (bond_proc_dir = init_net.proc_net->subdir; bond_proc_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 bond_proc_dir = bond_proc_dir->next) {
3326 if ((bond_proc_dir->namelen == len) &&
3327 !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3328 break;
3329 }
3330 }
3331
3332 if (!bond_proc_dir) {
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003333 bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 if (bond_proc_dir) {
3335 bond_proc_dir->owner = THIS_MODULE;
3336 } else {
3337 printk(KERN_WARNING DRV_NAME
3338 ": Warning: cannot create /proc/net/%s\n",
3339 DRV_NAME);
3340 }
3341 }
3342}
3343
3344/* Destroy the bonding directory under /proc/net, if empty.
3345 * Caller must hold rtnl_lock.
3346 */
3347static void bond_destroy_proc_dir(void)
3348{
3349 struct proc_dir_entry *de;
3350
3351 if (!bond_proc_dir) {
3352 return;
3353 }
3354
3355 /* verify that the /proc dir is empty */
3356 for (de = bond_proc_dir->subdir; de; de = de->next) {
3357 /* ignore . and .. */
3358 if (*(de->name) != '.') {
3359 break;
3360 }
3361 }
3362
3363 if (de) {
3364 if (bond_proc_dir->owner == THIS_MODULE) {
3365 bond_proc_dir->owner = NULL;
3366 }
3367 } else {
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003368 remove_proc_entry(DRV_NAME, init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 bond_proc_dir = NULL;
3370 }
3371}
3372#endif /* CONFIG_PROC_FS */
3373
3374/*-------------------------- netdev event handling --------------------------*/
3375
3376/*
3377 * Change device name
3378 */
3379static int bond_event_changename(struct bonding *bond)
3380{
3381#ifdef CONFIG_PROC_FS
3382 bond_remove_proc_entry(bond);
3383 bond_create_proc_entry(bond);
3384#endif
Mitch Williamsb76cdba2005-11-09 10:36:41 -08003385 down_write(&(bonding_rwsem));
3386 bond_destroy_sysfs_entry(bond);
3387 bond_create_sysfs_entry(bond);
3388 up_write(&(bonding_rwsem));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 return NOTIFY_DONE;
3390}
3391
3392static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
3393{
3394 struct bonding *event_bond = bond_dev->priv;
3395
3396 switch (event) {
3397 case NETDEV_CHANGENAME:
3398 return bond_event_changename(event_bond);
3399 case NETDEV_UNREGISTER:
3400 /*
3401 * TODO: remove a bond from the list?
3402 */
3403 break;
3404 default:
3405 break;
3406 }
3407
3408 return NOTIFY_DONE;
3409}
3410
3411static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
3412{
3413 struct net_device *bond_dev = slave_dev->master;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003414 struct bonding *bond = bond_dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
3416 switch (event) {
3417 case NETDEV_UNREGISTER:
3418 if (bond_dev) {
Jay Vosburgh1284cd32007-10-15 16:44:27 -07003419 if (bond->setup_by_slave)
3420 bond_release_and_destroy(bond_dev, slave_dev);
3421 else
3422 bond_release(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 }
3424 break;
3425 case NETDEV_CHANGE:
3426 /*
3427 * TODO: is this what we get if somebody
3428 * sets up a hierarchical bond, then rmmod's
3429 * one of the slave bonding devices?
3430 */
3431 break;
3432 case NETDEV_DOWN:
3433 /*
3434 * ... Or is it this?
3435 */
3436 break;
3437 case NETDEV_CHANGEMTU:
3438 /*
3439 * TODO: Should slaves be allowed to
3440 * independently alter their MTU? For
3441 * an active-backup bond, slaves need
3442 * not be the same type of device, so
3443 * MTUs may vary. For other modes,
3444 * slaves arguably should have the
3445 * same MTUs. To do this, we'd need to
3446 * take over the slave's change_mtu
3447 * function for the duration of their
3448 * servitude.
3449 */
3450 break;
3451 case NETDEV_CHANGENAME:
3452 /*
3453 * TODO: handle changing the primary's name
3454 */
3455 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003456 case NETDEV_FEAT_CHANGE:
3457 bond_compute_features(bond);
3458 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 default:
3460 break;
3461 }
3462
3463 return NOTIFY_DONE;
3464}
3465
3466/*
3467 * bond_netdev_event: handle netdev notifier chain events.
3468 *
3469 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003470 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 * locks for us to safely manipulate the slave devices (RTNL lock,
3472 * dev_probe_lock).
3473 */
3474static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3475{
3476 struct net_device *event_dev = (struct net_device *)ptr;
3477
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02003478 if (event_dev->nd_net != &init_net)
3479 return NOTIFY_DONE;
3480
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 dprintk("event_dev: %s, event: %lx\n",
3482 (event_dev ? event_dev->name : "None"),
3483 event);
3484
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003485 if (!(event_dev->priv_flags & IFF_BONDING))
3486 return NOTIFY_DONE;
3487
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 if (event_dev->flags & IFF_MASTER) {
3489 dprintk("IFF_MASTER\n");
3490 return bond_master_netdev_event(event, event_dev);
3491 }
3492
3493 if (event_dev->flags & IFF_SLAVE) {
3494 dprintk("IFF_SLAVE\n");
3495 return bond_slave_netdev_event(event, event_dev);
3496 }
3497
3498 return NOTIFY_DONE;
3499}
3500
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003501/*
3502 * bond_inetaddr_event: handle inetaddr notifier chain events.
3503 *
3504 * We keep track of device IPs primarily to use as source addresses in
3505 * ARP monitor probes (rather than spewing out broadcasts all the time).
3506 *
3507 * We track one IP for the main device (if it has one), plus one per VLAN.
3508 */
3509static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3510{
3511 struct in_ifaddr *ifa = ptr;
3512 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
3513 struct bonding *bond, *bond_next;
3514 struct vlan_entry *vlan, *vlan_next;
3515
3516 list_for_each_entry_safe(bond, bond_next, &bond_dev_list, bond_list) {
3517 if (bond->dev == event_dev) {
3518 switch (event) {
3519 case NETDEV_UP:
3520 bond->master_ip = ifa->ifa_local;
3521 return NOTIFY_OK;
3522 case NETDEV_DOWN:
3523 bond->master_ip = bond_glean_dev_ip(bond->dev);
3524 return NOTIFY_OK;
3525 default:
3526 return NOTIFY_DONE;
3527 }
3528 }
3529
3530 if (list_empty(&bond->vlan_list))
3531 continue;
3532
3533 list_for_each_entry_safe(vlan, vlan_next, &bond->vlan_list,
3534 vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08003535 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003536 if (vlan_dev == event_dev) {
3537 switch (event) {
3538 case NETDEV_UP:
3539 vlan->vlan_ip = ifa->ifa_local;
3540 return NOTIFY_OK;
3541 case NETDEV_DOWN:
3542 vlan->vlan_ip =
3543 bond_glean_dev_ip(vlan_dev);
3544 return NOTIFY_OK;
3545 default:
3546 return NOTIFY_DONE;
3547 }
3548 }
3549 }
3550 }
3551 return NOTIFY_DONE;
3552}
3553
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554static struct notifier_block bond_netdev_notifier = {
3555 .notifier_call = bond_netdev_event,
3556};
3557
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003558static struct notifier_block bond_inetaddr_notifier = {
3559 .notifier_call = bond_inetaddr_event,
3560};
3561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562/*-------------------------- Packet type handling ---------------------------*/
3563
3564/* register to receive lacpdus on a bond */
3565static void bond_register_lacpdu(struct bonding *bond)
3566{
3567 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3568
3569 /* initialize packet type */
3570 pk_type->type = PKT_TYPE_LACPDU;
3571 pk_type->dev = bond->dev;
3572 pk_type->func = bond_3ad_lacpdu_recv;
3573
3574 dev_add_pack(pk_type);
3575}
3576
3577/* unregister to receive lacpdus on a bond */
3578static void bond_unregister_lacpdu(struct bonding *bond)
3579{
3580 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3581}
3582
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003583void bond_register_arp(struct bonding *bond)
3584{
3585 struct packet_type *pt = &bond->arp_mon_pt;
3586
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003587 if (pt->type)
3588 return;
3589
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003590 pt->type = htons(ETH_P_ARP);
Jay Vosburghe245cb72007-02-28 17:03:27 -08003591 pt->dev = bond->dev;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003592 pt->func = bond_arp_rcv;
3593 dev_add_pack(pt);
3594}
3595
3596void bond_unregister_arp(struct bonding *bond)
3597{
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003598 struct packet_type *pt = &bond->arp_mon_pt;
3599
3600 dev_remove_pack(pt);
3601 pt->type = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003602}
3603
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003604/*---------------------------- Hashing Policies -----------------------------*/
3605
3606/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003607 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003608 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3609 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3610 */
3611static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
3612 struct net_device *bond_dev, int count)
3613{
3614 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003615 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003616 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003617 int layer4_xor = 0;
3618
3619 if (skb->protocol == __constant_htons(ETH_P_IP)) {
3620 if (!(iph->frag_off & __constant_htons(IP_MF|IP_OFFSET)) &&
3621 (iph->protocol == IPPROTO_TCP ||
3622 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003623 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003624 }
3625 return (layer4_xor ^
3626 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3627
3628 }
3629
3630 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3631}
3632
3633/*
3634 * Hash for the output device based upon layer 2 data
3635 */
3636static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
3637 struct net_device *bond_dev, int count)
3638{
3639 struct ethhdr *data = (struct ethhdr *)skb->data;
3640
3641 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3642}
3643
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644/*-------------------------- Device entry points ----------------------------*/
3645
3646static int bond_open(struct net_device *bond_dev)
3647{
3648 struct bonding *bond = bond_dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
3650 bond->kill_timers = 0;
3651
3652 if ((bond->params.mode == BOND_MODE_TLB) ||
3653 (bond->params.mode == BOND_MODE_ALB)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 /* bond_alb_initialize must be called before the timer
3655 * is started.
3656 */
3657 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3658 /* something went wrong - fail the open operation */
3659 return -1;
3660 }
3661
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003662 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3663 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 }
3665
3666 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003667 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3668 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 }
3670
3671 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003672 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3673 INIT_DELAYED_WORK(&bond->arp_work,
3674 bond_activebackup_arp_mon);
3675 else
3676 INIT_DELAYED_WORK(&bond->arp_work,
3677 bond_loadbalance_arp_mon);
3678
3679 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003680 if (bond->params.arp_validate)
3681 bond_register_arp(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 }
3683
3684 if (bond->params.mode == BOND_MODE_8023AD) {
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003685 INIT_DELAYED_WORK(&bond->ad_work, bond_alb_monitor);
3686 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 /* register to receive LACPDUs */
3688 bond_register_lacpdu(bond);
3689 }
3690
3691 return 0;
3692}
3693
3694static int bond_close(struct net_device *bond_dev)
3695{
3696 struct bonding *bond = bond_dev->priv;
3697
3698 if (bond->params.mode == BOND_MODE_8023AD) {
3699 /* Unregister the receive of LACPDUs */
3700 bond_unregister_lacpdu(bond);
3701 }
3702
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003703 if (bond->params.arp_validate)
3704 bond_unregister_arp(bond);
3705
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 write_lock_bh(&bond->lock);
3707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
3709 /* signal timers not to re-arm */
3710 bond->kill_timers = 1;
3711
3712 write_unlock_bh(&bond->lock);
3713
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003715 cancel_delayed_work(&bond->mii_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 }
3717
3718 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003719 cancel_delayed_work(&bond->arp_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 }
3721
3722 switch (bond->params.mode) {
3723 case BOND_MODE_8023AD:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003724 cancel_delayed_work(&bond->ad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 break;
3726 case BOND_MODE_TLB:
3727 case BOND_MODE_ALB:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003728 cancel_delayed_work(&bond->alb_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 break;
3730 default:
3731 break;
3732 }
3733
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
3735 if ((bond->params.mode == BOND_MODE_TLB) ||
3736 (bond->params.mode == BOND_MODE_ALB)) {
3737 /* Must be called only after all
3738 * slaves have been released
3739 */
3740 bond_alb_deinitialize(bond);
3741 }
3742
3743 return 0;
3744}
3745
3746static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3747{
3748 struct bonding *bond = bond_dev->priv;
3749 struct net_device_stats *stats = &(bond->stats), *sstats;
3750 struct slave *slave;
3751 int i;
3752
3753 memset(stats, 0, sizeof(struct net_device_stats));
3754
3755 read_lock_bh(&bond->lock);
3756
3757 bond_for_each_slave(bond, slave, i) {
Rusty Russellc45d2862007-03-28 14:29:08 -07003758 sstats = slave->dev->get_stats(slave->dev);
Rusty Russell5a1b5892007-04-28 21:04:03 -07003759 stats->rx_packets += sstats->rx_packets;
3760 stats->rx_bytes += sstats->rx_bytes;
3761 stats->rx_errors += sstats->rx_errors;
3762 stats->rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
Rusty Russell5a1b5892007-04-28 21:04:03 -07003764 stats->tx_packets += sstats->tx_packets;
3765 stats->tx_bytes += sstats->tx_bytes;
3766 stats->tx_errors += sstats->tx_errors;
3767 stats->tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
Rusty Russell5a1b5892007-04-28 21:04:03 -07003769 stats->multicast += sstats->multicast;
3770 stats->collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
Rusty Russell5a1b5892007-04-28 21:04:03 -07003772 stats->rx_length_errors += sstats->rx_length_errors;
3773 stats->rx_over_errors += sstats->rx_over_errors;
3774 stats->rx_crc_errors += sstats->rx_crc_errors;
3775 stats->rx_frame_errors += sstats->rx_frame_errors;
3776 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3777 stats->rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778
Rusty Russell5a1b5892007-04-28 21:04:03 -07003779 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3780 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3781 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3782 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3783 stats->tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 }
3785
3786 read_unlock_bh(&bond->lock);
3787
3788 return stats;
3789}
3790
3791static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3792{
3793 struct net_device *slave_dev = NULL;
3794 struct ifbond k_binfo;
3795 struct ifbond __user *u_binfo = NULL;
3796 struct ifslave k_sinfo;
3797 struct ifslave __user *u_sinfo = NULL;
3798 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 int res = 0;
3800
3801 dprintk("bond_ioctl: master=%s, cmd=%d\n",
3802 bond_dev->name, cmd);
3803
3804 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 case SIOCGMIIPHY:
3806 mii = if_mii(ifr);
3807 if (!mii) {
3808 return -EINVAL;
3809 }
3810 mii->phy_id = 0;
3811 /* Fall Through */
3812 case SIOCGMIIREG:
3813 /*
3814 * We do this again just in case we were called by SIOCGMIIREG
3815 * instead of SIOCGMIIPHY.
3816 */
3817 mii = if_mii(ifr);
3818 if (!mii) {
3819 return -EINVAL;
3820 }
3821
3822 if (mii->reg_num == 1) {
3823 struct bonding *bond = bond_dev->priv;
3824 mii->val_out = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003825 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 read_lock(&bond->curr_slave_lock);
Andy Gospodarek4e140072006-12-04 15:04:54 -08003827 if (netif_carrier_ok(bond->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 mii->val_out = BMSR_LSTATUS;
3829 }
3830 read_unlock(&bond->curr_slave_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003831 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 }
3833
3834 return 0;
3835 case BOND_INFO_QUERY_OLD:
3836 case SIOCBONDINFOQUERY:
3837 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3838
3839 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
3840 return -EFAULT;
3841 }
3842
3843 res = bond_info_query(bond_dev, &k_binfo);
3844 if (res == 0) {
3845 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
3846 return -EFAULT;
3847 }
3848 }
3849
3850 return res;
3851 case BOND_SLAVE_INFO_QUERY_OLD:
3852 case SIOCBONDSLAVEINFOQUERY:
3853 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3854
3855 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
3856 return -EFAULT;
3857 }
3858
3859 res = bond_slave_info_query(bond_dev, &k_sinfo);
3860 if (res == 0) {
3861 if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
3862 return -EFAULT;
3863 }
3864 }
3865
3866 return res;
3867 default:
3868 /* Go on */
3869 break;
3870 }
3871
3872 if (!capable(CAP_NET_ADMIN)) {
3873 return -EPERM;
3874 }
3875
Mitch Williamsb76cdba2005-11-09 10:36:41 -08003876 down_write(&(bonding_rwsem));
Eric W. Biederman881d9662007-09-17 11:56:21 -07003877 slave_dev = dev_get_by_name(&init_net, ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
3879 dprintk("slave_dev=%p: \n", slave_dev);
3880
3881 if (!slave_dev) {
3882 res = -ENODEV;
3883 } else {
3884 dprintk("slave_dev->name=%s: \n", slave_dev->name);
3885 switch (cmd) {
3886 case BOND_ENSLAVE_OLD:
3887 case SIOCBONDENSLAVE:
3888 res = bond_enslave(bond_dev, slave_dev);
3889 break;
3890 case BOND_RELEASE_OLD:
3891 case SIOCBONDRELEASE:
3892 res = bond_release(bond_dev, slave_dev);
3893 break;
3894 case BOND_SETHWADDR_OLD:
3895 case SIOCBONDSETHWADDR:
3896 res = bond_sethwaddr(bond_dev, slave_dev);
3897 break;
3898 case BOND_CHANGE_ACTIVE_OLD:
3899 case SIOCBONDCHANGEACTIVE:
3900 res = bond_ioctl_change_active(bond_dev, slave_dev);
3901 break;
3902 default:
3903 res = -EOPNOTSUPP;
3904 }
3905
3906 dev_put(slave_dev);
3907 }
3908
Mitch Williamsb76cdba2005-11-09 10:36:41 -08003909 up_write(&(bonding_rwsem));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 return res;
3911}
3912
3913static void bond_set_multicast_list(struct net_device *bond_dev)
3914{
3915 struct bonding *bond = bond_dev->priv;
3916 struct dev_mc_list *dmi;
3917
3918 write_lock_bh(&bond->lock);
3919
3920 /*
3921 * Do promisc before checking multicast_mode
3922 */
3923 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
3924 bond_set_promiscuity(bond, 1);
3925 }
3926
3927 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
3928 bond_set_promiscuity(bond, -1);
3929 }
3930
3931 /* set allmulti flag to slaves */
3932 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
3933 bond_set_allmulti(bond, 1);
3934 }
3935
3936 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
3937 bond_set_allmulti(bond, -1);
3938 }
3939
3940 bond->flags = bond_dev->flags;
3941
3942 /* looking for addresses to add to slaves' mc list */
3943 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
3944 if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
3945 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3946 }
3947 }
3948
3949 /* looking for addresses to delete from slaves' list */
3950 for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
3951 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
3952 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
3953 }
3954 }
3955
3956 /* save master's multicast list */
3957 bond_mc_list_destroy(bond);
3958 bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
3959
3960 write_unlock_bh(&bond->lock);
3961}
3962
3963/*
3964 * Change the MTU of all of a master's slaves to match the master
3965 */
3966static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3967{
3968 struct bonding *bond = bond_dev->priv;
3969 struct slave *slave, *stop_at;
3970 int res = 0;
3971 int i;
3972
3973 dprintk("bond=%p, name=%s, new_mtu=%d\n", bond,
3974 (bond_dev ? bond_dev->name : "None"), new_mtu);
3975
3976 /* Can't hold bond->lock with bh disabled here since
3977 * some base drivers panic. On the other hand we can't
3978 * hold bond->lock without bh disabled because we'll
3979 * deadlock. The only solution is to rely on the fact
3980 * that we're under rtnl_lock here, and the slaves
3981 * list won't change. This doesn't solve the problem
3982 * of setting the slave's MTU while it is
3983 * transmitting, but the assumption is that the base
3984 * driver can handle that.
3985 *
3986 * TODO: figure out a way to safely iterate the slaves
3987 * list, but without holding a lock around the actual
3988 * call to the base driver.
3989 */
3990
3991 bond_for_each_slave(bond, slave, i) {
3992 dprintk("s %p s->p %p c_m %p\n", slave,
3993 slave->prev, slave->dev->change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08003994
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 res = dev_set_mtu(slave->dev, new_mtu);
3996
3997 if (res) {
3998 /* If we failed to set the slave's mtu to the new value
3999 * we must abort the operation even in ACTIVE_BACKUP
4000 * mode, because if we allow the backup slaves to have
4001 * different mtu values than the active slave we'll
4002 * need to change their mtu when doing a failover. That
4003 * means changing their mtu from timer context, which
4004 * is probably not a good idea.
4005 */
4006 dprintk("err %d %s\n", res, slave->dev->name);
4007 goto unwind;
4008 }
4009 }
4010
4011 bond_dev->mtu = new_mtu;
4012
4013 return 0;
4014
4015unwind:
4016 /* unwind from head to the slave that failed */
4017 stop_at = slave;
4018 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4019 int tmp_res;
4020
4021 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4022 if (tmp_res) {
4023 dprintk("unwind err %d dev %s\n", tmp_res,
4024 slave->dev->name);
4025 }
4026 }
4027
4028 return res;
4029}
4030
4031/*
4032 * Change HW address
4033 *
4034 * Note that many devices must be down to change the HW address, and
4035 * downing the master releases all slaves. We can make bonds full of
4036 * bonding devices to test this, however.
4037 */
4038static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4039{
4040 struct bonding *bond = bond_dev->priv;
4041 struct sockaddr *sa = addr, tmp_sa;
4042 struct slave *slave, *stop_at;
4043 int res = 0;
4044 int i;
4045
4046 dprintk("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
4047
Jay Vosburghdd957c52007-10-09 19:57:24 -07004048 /*
4049 * If fail_over_mac is enabled, do nothing and return success.
4050 * Returning an error causes ifenslave to fail.
4051 */
4052 if (bond->params.fail_over_mac)
4053 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07004054
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 if (!is_valid_ether_addr(sa->sa_data)) {
4056 return -EADDRNOTAVAIL;
4057 }
4058
4059 /* Can't hold bond->lock with bh disabled here since
4060 * some base drivers panic. On the other hand we can't
4061 * hold bond->lock without bh disabled because we'll
4062 * deadlock. The only solution is to rely on the fact
4063 * that we're under rtnl_lock here, and the slaves
4064 * list won't change. This doesn't solve the problem
4065 * of setting the slave's hw address while it is
4066 * transmitting, but the assumption is that the base
4067 * driver can handle that.
4068 *
4069 * TODO: figure out a way to safely iterate the slaves
4070 * list, but without holding a lock around the actual
4071 * call to the base driver.
4072 */
4073
4074 bond_for_each_slave(bond, slave, i) {
4075 dprintk("slave %p %s\n", slave, slave->dev->name);
4076
4077 if (slave->dev->set_mac_address == NULL) {
4078 res = -EOPNOTSUPP;
4079 dprintk("EOPNOTSUPP %s\n", slave->dev->name);
4080 goto unwind;
4081 }
4082
4083 res = dev_set_mac_address(slave->dev, addr);
4084 if (res) {
4085 /* TODO: consider downing the slave
4086 * and retry ?
4087 * User should expect communications
4088 * breakage anyway until ARP finish
4089 * updating, so...
4090 */
4091 dprintk("err %d %s\n", res, slave->dev->name);
4092 goto unwind;
4093 }
4094 }
4095
4096 /* success */
4097 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4098 return 0;
4099
4100unwind:
4101 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4102 tmp_sa.sa_family = bond_dev->type;
4103
4104 /* unwind from head to the slave that failed */
4105 stop_at = slave;
4106 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4107 int tmp_res;
4108
4109 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4110 if (tmp_res) {
4111 dprintk("unwind err %d dev %s\n", tmp_res,
4112 slave->dev->name);
4113 }
4114 }
4115
4116 return res;
4117}
4118
4119static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4120{
4121 struct bonding *bond = bond_dev->priv;
4122 struct slave *slave, *start_at;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004123 int i, slave_no, res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124
4125 read_lock(&bond->lock);
4126
4127 if (!BOND_IS_OK(bond)) {
4128 goto out;
4129 }
4130
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004131 /*
4132 * Concurrent TX may collide on rr_tx_counter; we accept that
4133 * as being rare enough not to justify using an atomic op here
4134 */
4135 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004137 bond_for_each_slave(bond, slave, i) {
4138 slave_no--;
4139 if (slave_no < 0) {
4140 break;
4141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 }
4143
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004144 start_at = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 bond_for_each_slave_from(bond, slave, i, start_at) {
4146 if (IS_UP(slave->dev) &&
4147 (slave->link == BOND_LINK_UP) &&
4148 (slave->state == BOND_STATE_ACTIVE)) {
4149 res = bond_dev_queue_xmit(bond, skb, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 break;
4151 }
4152 }
4153
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154out:
4155 if (res) {
4156 /* no suitable interface, frame not sent */
4157 dev_kfree_skb(skb);
4158 }
4159 read_unlock(&bond->lock);
4160 return 0;
4161}
4162
John W. Linville075897c2005-09-28 17:50:53 -04004163
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164/*
4165 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4166 * the bond has a usable interface.
4167 */
4168static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4169{
4170 struct bonding *bond = bond_dev->priv;
4171 int res = 1;
4172
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 read_lock(&bond->lock);
4174 read_lock(&bond->curr_slave_lock);
4175
4176 if (!BOND_IS_OK(bond)) {
4177 goto out;
4178 }
4179
John W. Linville075897c2005-09-28 17:50:53 -04004180 if (!bond->curr_active_slave)
4181 goto out;
4182
John W. Linville075897c2005-09-28 17:50:53 -04004183 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4184
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185out:
4186 if (res) {
4187 /* no suitable interface, frame not sent */
4188 dev_kfree_skb(skb);
4189 }
4190 read_unlock(&bond->curr_slave_lock);
4191 read_unlock(&bond->lock);
4192 return 0;
4193}
4194
4195/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004196 * In bond_xmit_xor() , we determine the output device by using a pre-
4197 * determined xmit_hash_policy(), If the selected device is not enabled,
4198 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 */
4200static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4201{
4202 struct bonding *bond = bond_dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 struct slave *slave, *start_at;
4204 int slave_no;
4205 int i;
4206 int res = 1;
4207
4208 read_lock(&bond->lock);
4209
4210 if (!BOND_IS_OK(bond)) {
4211 goto out;
4212 }
4213
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004214 slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
4216 bond_for_each_slave(bond, slave, i) {
4217 slave_no--;
4218 if (slave_no < 0) {
4219 break;
4220 }
4221 }
4222
4223 start_at = slave;
4224
4225 bond_for_each_slave_from(bond, slave, i, start_at) {
4226 if (IS_UP(slave->dev) &&
4227 (slave->link == BOND_LINK_UP) &&
4228 (slave->state == BOND_STATE_ACTIVE)) {
4229 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4230 break;
4231 }
4232 }
4233
4234out:
4235 if (res) {
4236 /* no suitable interface, frame not sent */
4237 dev_kfree_skb(skb);
4238 }
4239 read_unlock(&bond->lock);
4240 return 0;
4241}
4242
4243/*
4244 * in broadcast mode, we send everything to all usable interfaces.
4245 */
4246static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4247{
4248 struct bonding *bond = bond_dev->priv;
4249 struct slave *slave, *start_at;
4250 struct net_device *tx_dev = NULL;
4251 int i;
4252 int res = 1;
4253
4254 read_lock(&bond->lock);
4255
4256 if (!BOND_IS_OK(bond)) {
4257 goto out;
4258 }
4259
4260 read_lock(&bond->curr_slave_lock);
4261 start_at = bond->curr_active_slave;
4262 read_unlock(&bond->curr_slave_lock);
4263
4264 if (!start_at) {
4265 goto out;
4266 }
4267
4268 bond_for_each_slave_from(bond, slave, i, start_at) {
4269 if (IS_UP(slave->dev) &&
4270 (slave->link == BOND_LINK_UP) &&
4271 (slave->state == BOND_STATE_ACTIVE)) {
4272 if (tx_dev) {
4273 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4274 if (!skb2) {
4275 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08004276 ": %s: Error: bond_xmit_broadcast(): "
4277 "skb_clone() failed\n",
4278 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 continue;
4280 }
4281
4282 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4283 if (res) {
4284 dev_kfree_skb(skb2);
4285 continue;
4286 }
4287 }
4288 tx_dev = slave->dev;
4289 }
4290 }
4291
4292 if (tx_dev) {
4293 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4294 }
4295
4296out:
4297 if (res) {
4298 /* no suitable interface, frame not sent */
4299 dev_kfree_skb(skb);
4300 }
4301 /* frame sent to all suitable interfaces */
4302 read_unlock(&bond->lock);
4303 return 0;
4304}
4305
4306/*------------------------- Device initialization ---------------------------*/
4307
4308/*
4309 * set bond mode specific net device operations
4310 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004311void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004313 struct net_device *bond_dev = bond->dev;
4314
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 switch (mode) {
4316 case BOND_MODE_ROUNDROBIN:
4317 bond_dev->hard_start_xmit = bond_xmit_roundrobin;
4318 break;
4319 case BOND_MODE_ACTIVEBACKUP:
4320 bond_dev->hard_start_xmit = bond_xmit_activebackup;
4321 break;
4322 case BOND_MODE_XOR:
4323 bond_dev->hard_start_xmit = bond_xmit_xor;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004324 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
4325 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4326 else
4327 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 break;
4329 case BOND_MODE_BROADCAST:
4330 bond_dev->hard_start_xmit = bond_xmit_broadcast;
4331 break;
4332 case BOND_MODE_8023AD:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004333 bond_set_master_3ad_flags(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 bond_dev->hard_start_xmit = bond_3ad_xmit_xor;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004335 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
4336 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4337 else
4338 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004341 bond_set_master_alb_flags(bond);
4342 /* FALLTHRU */
4343 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 bond_dev->hard_start_xmit = bond_alb_xmit;
4345 bond_dev->set_mac_address = bond_alb_set_mac_address;
4346 break;
4347 default:
4348 /* Should never happen, mode already checked */
4349 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08004350 ": %s: Error: Unknown bonding mode %d\n",
4351 bond_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 mode);
4353 break;
4354 }
4355}
4356
Jay Vosburgh217df672005-09-26 16:11:50 -07004357static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4358 struct ethtool_drvinfo *drvinfo)
4359{
4360 strncpy(drvinfo->driver, DRV_NAME, 32);
4361 strncpy(drvinfo->version, DRV_VERSION, 32);
4362 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4363}
4364
Jeff Garzik7282d492006-09-13 14:30:00 -04004365static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004366 .get_drvinfo = bond_ethtool_get_drvinfo,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004367};
4368
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369/*
4370 * Does not allocate but creates a /proc entry.
4371 * Allowed to fail.
4372 */
Mitch Williams3c535952005-11-09 10:36:11 -08004373static int bond_init(struct net_device *bond_dev, struct bond_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374{
4375 struct bonding *bond = bond_dev->priv;
4376
4377 dprintk("Begin bond_init for %s\n", bond_dev->name);
4378
4379 /* initialize rwlocks */
4380 rwlock_init(&bond->lock);
4381 rwlock_init(&bond->curr_slave_lock);
4382
4383 bond->params = *params; /* copy params struct */
4384
Jay Vosburgh1b76b312007-10-17 17:37:45 -07004385 bond->wq = create_singlethread_workqueue(bond_dev->name);
4386 if (!bond->wq)
4387 return -ENOMEM;
4388
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 /* Initialize pointers */
4390 bond->first_slave = NULL;
4391 bond->curr_active_slave = NULL;
4392 bond->current_arp_slave = NULL;
4393 bond->primary_slave = NULL;
4394 bond->dev = bond_dev;
Moni Shoua1053f622007-10-09 19:43:42 -07004395 bond->send_grat_arp = 0;
Moni Shouad90a1622007-10-09 19:43:43 -07004396 bond->setup_by_slave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 INIT_LIST_HEAD(&bond->vlan_list);
4398
4399 /* Initialize the device entry points */
4400 bond_dev->open = bond_open;
4401 bond_dev->stop = bond_close;
4402 bond_dev->get_stats = bond_get_stats;
4403 bond_dev->do_ioctl = bond_do_ioctl;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004404 bond_dev->ethtool_ops = &bond_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 bond_dev->set_multicast_list = bond_set_multicast_list;
4406 bond_dev->change_mtu = bond_change_mtu;
4407 bond_dev->set_mac_address = bond_set_mac_address;
4408
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004409 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
4411 bond_dev->destructor = free_netdev;
4412
4413 /* Initialize the device options */
4414 bond_dev->tx_queue_len = 0;
4415 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004416 bond_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
4418 /* At first, we block adding VLANs. That's the only way to
4419 * prevent problems that occur when adding VLANs over an
4420 * empty bond. The block will be removed once non-challenged
4421 * slaves are enslaved.
4422 */
4423 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4424
Herbert Xu932ff272006-06-09 12:20:56 -07004425 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 * transmitting */
4427 bond_dev->features |= NETIF_F_LLTX;
4428
4429 /* By default, we declare the bond to be fully
4430 * VLAN hardware accelerated capable. Special
4431 * care is taken in the various xmit functions
4432 * when there are slaves that are not hw accel
4433 * capable
4434 */
4435 bond_dev->vlan_rx_register = bond_vlan_rx_register;
4436 bond_dev->vlan_rx_add_vid = bond_vlan_rx_add_vid;
4437 bond_dev->vlan_rx_kill_vid = bond_vlan_rx_kill_vid;
4438 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4439 NETIF_F_HW_VLAN_RX |
4440 NETIF_F_HW_VLAN_FILTER);
4441
4442#ifdef CONFIG_PROC_FS
4443 bond_create_proc_entry(bond);
4444#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 list_add_tail(&bond->bond_list, &bond_dev_list);
4446
4447 return 0;
4448}
4449
4450/* De-initialize device specific data.
4451 * Caller must hold rtnl_lock.
4452 */
Adrian Bunkc50b85d2007-10-24 18:23:17 +02004453static void bond_deinit(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454{
4455 struct bonding *bond = bond_dev->priv;
4456
4457 list_del(&bond->bond_list);
4458
4459#ifdef CONFIG_PROC_FS
4460 bond_remove_proc_entry(bond);
4461#endif
4462}
4463
4464/* Unregister and free all bond devices.
4465 * Caller must hold rtnl_lock.
4466 */
4467static void bond_free_all(void)
4468{
4469 struct bonding *bond, *nxt;
4470
4471 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4472 struct net_device *bond_dev = bond->dev;
4473
jamal702987052006-09-22 21:54:37 -07004474 bond_mc_list_destroy(bond);
4475 /* Release the bonded slaves */
4476 bond_release_all(bond_dev);
Jay Vosburgh3201e652007-06-19 11:12:12 -07004477 unregister_netdevice(bond_dev);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07004478 bond_deinit(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 }
4480
4481#ifdef CONFIG_PROC_FS
4482 bond_destroy_proc_dir();
4483#endif
4484}
4485
4486/*------------------------- Module initialization ---------------------------*/
4487
4488/*
4489 * Convert string input module parms. Accept either the
4490 * number of the mode or its string name.
4491 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004492int bond_parse_parm(char *mode_arg, struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493{
4494 int i;
4495
4496 for (i = 0; tbl[i].modename; i++) {
4497 if ((isdigit(*mode_arg) &&
4498 tbl[i].mode == simple_strtol(mode_arg, NULL, 0)) ||
4499 (strncmp(mode_arg, tbl[i].modename,
4500 strlen(tbl[i].modename)) == 0)) {
4501 return tbl[i].mode;
4502 }
4503 }
4504
4505 return -1;
4506}
4507
4508static int bond_check_params(struct bond_params *params)
4509{
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004510 int arp_validate_value;
4511
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 /*
4513 * Convert string parameters.
4514 */
4515 if (mode) {
4516 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4517 if (bond_mode == -1) {
4518 printk(KERN_ERR DRV_NAME
4519 ": Error: Invalid bonding mode \"%s\"\n",
4520 mode == NULL ? "NULL" : mode);
4521 return -EINVAL;
4522 }
4523 }
4524
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004525 if (xmit_hash_policy) {
4526 if ((bond_mode != BOND_MODE_XOR) &&
4527 (bond_mode != BOND_MODE_8023AD)) {
4528 printk(KERN_INFO DRV_NAME
4529 ": xor_mode param is irrelevant in mode %s\n",
4530 bond_mode_name(bond_mode));
4531 } else {
4532 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4533 xmit_hashtype_tbl);
4534 if (xmit_hashtype == -1) {
4535 printk(KERN_ERR DRV_NAME
4536 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4537 xmit_hash_policy == NULL ? "NULL" :
4538 xmit_hash_policy);
4539 return -EINVAL;
4540 }
4541 }
4542 }
4543
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 if (lacp_rate) {
4545 if (bond_mode != BOND_MODE_8023AD) {
4546 printk(KERN_INFO DRV_NAME
4547 ": lacp_rate param is irrelevant in mode %s\n",
4548 bond_mode_name(bond_mode));
4549 } else {
4550 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4551 if (lacp_fast == -1) {
4552 printk(KERN_ERR DRV_NAME
4553 ": Error: Invalid lacp rate \"%s\"\n",
4554 lacp_rate == NULL ? "NULL" : lacp_rate);
4555 return -EINVAL;
4556 }
4557 }
4558 }
4559
4560 if (max_bonds < 1 || max_bonds > INT_MAX) {
4561 printk(KERN_WARNING DRV_NAME
4562 ": Warning: max_bonds (%d) not in range %d-%d, so it "
Mitch Williams4e0952c2005-11-09 10:34:57 -08004563 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 max_bonds, 1, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4565 max_bonds = BOND_DEFAULT_MAX_BONDS;
4566 }
4567
4568 if (miimon < 0) {
4569 printk(KERN_WARNING DRV_NAME
4570 ": Warning: miimon module parameter (%d), "
4571 "not in range 0-%d, so it was reset to %d\n",
4572 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4573 miimon = BOND_LINK_MON_INTERV;
4574 }
4575
4576 if (updelay < 0) {
4577 printk(KERN_WARNING DRV_NAME
4578 ": Warning: updelay module parameter (%d), "
4579 "not in range 0-%d, so it was reset to 0\n",
4580 updelay, INT_MAX);
4581 updelay = 0;
4582 }
4583
4584 if (downdelay < 0) {
4585 printk(KERN_WARNING DRV_NAME
4586 ": Warning: downdelay module parameter (%d), "
4587 "not in range 0-%d, so it was reset to 0\n",
4588 downdelay, INT_MAX);
4589 downdelay = 0;
4590 }
4591
4592 if ((use_carrier != 0) && (use_carrier != 1)) {
4593 printk(KERN_WARNING DRV_NAME
4594 ": Warning: use_carrier module parameter (%d), "
4595 "not of valid value (0/1), so it was set to 1\n",
4596 use_carrier);
4597 use_carrier = 1;
4598 }
4599
4600 /* reset values for 802.3ad */
4601 if (bond_mode == BOND_MODE_8023AD) {
4602 if (!miimon) {
4603 printk(KERN_WARNING DRV_NAME
4604 ": Warning: miimon must be specified, "
4605 "otherwise bonding will not detect link "
4606 "failure, speed and duplex which are "
4607 "essential for 802.3ad operation\n");
4608 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4609 miimon = 100;
4610 }
4611 }
4612
4613 /* reset values for TLB/ALB */
4614 if ((bond_mode == BOND_MODE_TLB) ||
4615 (bond_mode == BOND_MODE_ALB)) {
4616 if (!miimon) {
4617 printk(KERN_WARNING DRV_NAME
4618 ": Warning: miimon must be specified, "
4619 "otherwise bonding will not detect link "
4620 "failure and link speed which are essential "
4621 "for TLB/ALB load balancing\n");
4622 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4623 miimon = 100;
4624 }
4625 }
4626
4627 if (bond_mode == BOND_MODE_ALB) {
4628 printk(KERN_NOTICE DRV_NAME
4629 ": In ALB mode you might experience client "
4630 "disconnections upon reconnection of a link if the "
4631 "bonding module updelay parameter (%d msec) is "
4632 "incompatible with the forwarding delay time of the "
4633 "switch\n",
4634 updelay);
4635 }
4636
4637 if (!miimon) {
4638 if (updelay || downdelay) {
4639 /* just warn the user the up/down delay will have
4640 * no effect since miimon is zero...
4641 */
4642 printk(KERN_WARNING DRV_NAME
4643 ": Warning: miimon module parameter not set "
4644 "and updelay (%d) or downdelay (%d) module "
4645 "parameter is set; updelay and downdelay have "
4646 "no effect unless miimon is set\n",
4647 updelay, downdelay);
4648 }
4649 } else {
4650 /* don't allow arp monitoring */
4651 if (arp_interval) {
4652 printk(KERN_WARNING DRV_NAME
4653 ": Warning: miimon (%d) and arp_interval (%d) "
4654 "can't be used simultaneously, disabling ARP "
4655 "monitoring\n",
4656 miimon, arp_interval);
4657 arp_interval = 0;
4658 }
4659
4660 if ((updelay % miimon) != 0) {
4661 printk(KERN_WARNING DRV_NAME
4662 ": Warning: updelay (%d) is not a multiple "
4663 "of miimon (%d), updelay rounded to %d ms\n",
4664 updelay, miimon, (updelay / miimon) * miimon);
4665 }
4666
4667 updelay /= miimon;
4668
4669 if ((downdelay % miimon) != 0) {
4670 printk(KERN_WARNING DRV_NAME
4671 ": Warning: downdelay (%d) is not a multiple "
4672 "of miimon (%d), downdelay rounded to %d ms\n",
4673 downdelay, miimon,
4674 (downdelay / miimon) * miimon);
4675 }
4676
4677 downdelay /= miimon;
4678 }
4679
4680 if (arp_interval < 0) {
4681 printk(KERN_WARNING DRV_NAME
4682 ": Warning: arp_interval module parameter (%d) "
4683 ", not in range 0-%d, so it was reset to %d\n",
4684 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4685 arp_interval = BOND_LINK_ARP_INTERV;
4686 }
4687
4688 for (arp_ip_count = 0;
4689 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4690 arp_ip_count++) {
4691 /* not complete check, but should be good enough to
4692 catch mistakes */
4693 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4694 printk(KERN_WARNING DRV_NAME
4695 ": Warning: bad arp_ip_target module parameter "
4696 "(%s), ARP monitoring will not be performed\n",
4697 arp_ip_target[arp_ip_count]);
4698 arp_interval = 0;
4699 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04004700 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 arp_target[arp_ip_count] = ip;
4702 }
4703 }
4704
4705 if (arp_interval && !arp_ip_count) {
4706 /* don't allow arping if no arp_ip_target given... */
4707 printk(KERN_WARNING DRV_NAME
4708 ": Warning: arp_interval module parameter (%d) "
4709 "specified without providing an arp_ip_target "
4710 "parameter, arp_interval was reset to 0\n",
4711 arp_interval);
4712 arp_interval = 0;
4713 }
4714
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004715 if (arp_validate) {
4716 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
4717 printk(KERN_ERR DRV_NAME
4718 ": arp_validate only supported in active-backup mode\n");
4719 return -EINVAL;
4720 }
4721 if (!arp_interval) {
4722 printk(KERN_ERR DRV_NAME
4723 ": arp_validate requires arp_interval\n");
4724 return -EINVAL;
4725 }
4726
4727 arp_validate_value = bond_parse_parm(arp_validate,
4728 arp_validate_tbl);
4729 if (arp_validate_value == -1) {
4730 printk(KERN_ERR DRV_NAME
4731 ": Error: invalid arp_validate \"%s\"\n",
4732 arp_validate == NULL ? "NULL" : arp_validate);
4733 return -EINVAL;
4734 }
4735 } else
4736 arp_validate_value = 0;
4737
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 if (miimon) {
4739 printk(KERN_INFO DRV_NAME
4740 ": MII link monitoring set to %d ms\n",
4741 miimon);
4742 } else if (arp_interval) {
4743 int i;
4744
4745 printk(KERN_INFO DRV_NAME
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004746 ": ARP monitoring set to %d ms, validate %s, with %d target(s):",
4747 arp_interval,
4748 arp_validate_tbl[arp_validate_value].modename,
4749 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750
4751 for (i = 0; i < arp_ip_count; i++)
4752 printk (" %s", arp_ip_target[i]);
4753
4754 printk("\n");
4755
4756 } else {
4757 /* miimon and arp_interval not set, we need one so things
4758 * work as expected, see bonding.txt for details
4759 */
4760 printk(KERN_WARNING DRV_NAME
4761 ": Warning: either miimon or arp_interval and "
4762 "arp_ip_target module parameters must be specified, "
4763 "otherwise bonding will not detect link failures! see "
4764 "bonding.txt for details.\n");
4765 }
4766
4767 if (primary && !USES_PRIMARY(bond_mode)) {
4768 /* currently, using a primary only makes sense
4769 * in active backup, TLB or ALB modes
4770 */
4771 printk(KERN_WARNING DRV_NAME
4772 ": Warning: %s primary device specified but has no "
4773 "effect in %s mode\n",
4774 primary, bond_mode_name(bond_mode));
4775 primary = NULL;
4776 }
4777
Jay Vosburghdd957c52007-10-09 19:57:24 -07004778 if (fail_over_mac && (bond_mode != BOND_MODE_ACTIVEBACKUP))
4779 printk(KERN_WARNING DRV_NAME
4780 ": Warning: fail_over_mac only affects "
4781 "active-backup mode.\n");
4782
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 /* fill params struct with the proper values */
4784 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004785 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 params->miimon = miimon;
4787 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004788 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 params->updelay = updelay;
4790 params->downdelay = downdelay;
4791 params->use_carrier = use_carrier;
4792 params->lacp_fast = lacp_fast;
4793 params->primary[0] = 0;
Jay Vosburghdd957c52007-10-09 19:57:24 -07004794 params->fail_over_mac = fail_over_mac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
4796 if (primary) {
4797 strncpy(params->primary, primary, IFNAMSIZ);
4798 params->primary[IFNAMSIZ - 1] = 0;
4799 }
4800
4801 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4802
4803 return 0;
4804}
4805
Peter Zijlstra0daa23032006-11-08 19:51:01 -08004806static struct lock_class_key bonding_netdev_xmit_lock_key;
4807
Mitch Williamsdfe60392005-11-09 10:36:04 -08004808/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004809 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08004810 * Caller must NOT hold rtnl_lock; we need to release it here before we
4811 * set up our sysfs entries.
4812 */
4813int bond_create(char *name, struct bond_params *params, struct bonding **newbond)
4814{
4815 struct net_device *bond_dev;
4816 int res;
4817
4818 rtnl_lock();
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004819 bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
4820 ether_setup);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004821 if (!bond_dev) {
4822 printk(KERN_ERR DRV_NAME
4823 ": %s: eek! can't alloc netdev!\n",
4824 name);
4825 res = -ENOMEM;
4826 goto out_rtnl;
4827 }
4828
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004829 if (!name) {
4830 res = dev_alloc_name(bond_dev, "bond%d");
4831 if (res < 0)
4832 goto out_netdev;
4833 }
4834
Mitch Williamsdfe60392005-11-09 10:36:04 -08004835 /* bond_init() must be called after dev_alloc_name() (for the
4836 * /proc files), but before register_netdevice(), because we
4837 * need to set function pointers.
4838 */
4839
4840 res = bond_init(bond_dev, params);
4841 if (res < 0) {
4842 goto out_netdev;
4843 }
4844
Mitch Williamsdfe60392005-11-09 10:36:04 -08004845 res = register_netdevice(bond_dev);
4846 if (res < 0) {
4847 goto out_bond;
4848 }
Peter Zijlstra0daa23032006-11-08 19:51:01 -08004849
4850 lockdep_set_class(&bond_dev->_xmit_lock, &bonding_netdev_xmit_lock_key);
4851
Mitch Williamsdfe60392005-11-09 10:36:04 -08004852 if (newbond)
4853 *newbond = bond_dev->priv;
4854
Jay Vosburghff59c452006-03-27 13:27:43 -08004855 netif_carrier_off(bond_dev);
4856
Mitch Williamsdfe60392005-11-09 10:36:04 -08004857 rtnl_unlock(); /* allows sysfs registration of net device */
Mitch Williamsb76cdba2005-11-09 10:36:41 -08004858 res = bond_create_sysfs_entry(bond_dev->priv);
Jay Vosburgh09c89272007-01-19 18:15:38 -08004859 if (res < 0) {
4860 rtnl_lock();
4861 goto out_bond;
4862 }
4863
4864 return 0;
4865
Mitch Williamsdfe60392005-11-09 10:36:04 -08004866out_bond:
4867 bond_deinit(bond_dev);
4868out_netdev:
4869 free_netdev(bond_dev);
4870out_rtnl:
4871 rtnl_unlock();
Mitch Williamsdfe60392005-11-09 10:36:04 -08004872 return res;
4873}
4874
Jay Vosburgh1b76b312007-10-17 17:37:45 -07004875static void bond_work_cancel_all(struct bonding *bond)
4876{
4877 write_lock_bh(&bond->lock);
4878 bond->kill_timers = 1;
4879 write_unlock_bh(&bond->lock);
4880
4881 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4882 cancel_delayed_work(&bond->mii_work);
4883
4884 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4885 cancel_delayed_work(&bond->arp_work);
4886
4887 if (bond->params.mode == BOND_MODE_ALB &&
4888 delayed_work_pending(&bond->alb_work))
4889 cancel_delayed_work(&bond->alb_work);
4890
4891 if (bond->params.mode == BOND_MODE_8023AD &&
4892 delayed_work_pending(&bond->ad_work))
4893 cancel_delayed_work(&bond->ad_work);
4894}
4895
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896static int __init bonding_init(void)
4897{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 int i;
4899 int res;
Jay Vosburgh1b76b312007-10-17 17:37:45 -07004900 struct bonding *bond, *nxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
4902 printk(KERN_INFO "%s", version);
4903
Mitch Williamsdfe60392005-11-09 10:36:04 -08004904 res = bond_check_params(&bonding_defaults);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 if (res) {
Mitch Williamsdfe60392005-11-09 10:36:04 -08004906 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 }
4908
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909#ifdef CONFIG_PROC_FS
4910 bond_create_proc_dir();
4911#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 for (i = 0; i < max_bonds; i++) {
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004913 res = bond_create(NULL, &bonding_defaults, NULL);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004914 if (res)
4915 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 }
4917
Mitch Williamsb76cdba2005-11-09 10:36:41 -08004918 res = bond_create_sysfs();
4919 if (res)
4920 goto err;
4921
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04004923 register_inetaddr_notifier(&bond_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
Mitch Williamsdfe60392005-11-09 10:36:04 -08004925 goto out;
4926err:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07004927 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4928 bond_work_cancel_all(bond);
4929 destroy_workqueue(bond->wq);
4930 }
4931
Florin Malita40abc272005-09-18 00:24:12 -07004932 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 bond_free_all();
Mitch Williamsb76cdba2005-11-09 10:36:41 -08004934 bond_destroy_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 rtnl_unlock();
Mitch Williamsdfe60392005-11-09 10:36:04 -08004936out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 return res;
Mitch Williamsdfe60392005-11-09 10:36:04 -08004938
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939}
4940
4941static void __exit bonding_exit(void)
4942{
4943 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04004944 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945
4946 rtnl_lock();
4947 bond_free_all();
Mitch Williamsb76cdba2005-11-09 10:36:41 -08004948 bond_destroy_sysfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 rtnl_unlock();
4950}
4951
4952module_init(bonding_init);
4953module_exit(bonding_exit);
4954MODULE_LICENSE("GPL");
4955MODULE_VERSION(DRV_VERSION);
4956MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
4957MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
4958MODULE_SUPPORTED_DEVICE("most ethernet devices");
4959
4960/*
4961 * Local variables:
4962 * c-indent-level: 8
4963 * c-basic-offset: 8
4964 * tab-width: 8
4965 * End:
4966 */
4967