blob: 19cd5c1e1bd3470a8f576fe9babcff2dbfcf5f72 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/kernel.h>
35#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/types.h>
37#include <linux/fcntl.h>
38#include <linux/interrupt.h>
39#include <linux/ptrace.h>
40#include <linux/ioport.h>
41#include <linux/in.h>
Jay Vosburgh169a3e62005-06-26 17:54:11 -040042#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/ip.h>
Jay Vosburgh169a3e62005-06-26 17:54:11 -040044#include <linux/tcp.h>
45#include <linux/udp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/slab.h>
47#include <linux/string.h>
48#include <linux/init.h>
49#include <linux/timer.h>
50#include <linux/socket.h>
51#include <linux/ctype.h>
52#include <linux/inet.h>
53#include <linux/bitops.h>
54#include <asm/system.h>
55#include <asm/io.h>
56#include <asm/dma.h>
57#include <asm/uaccess.h>
58#include <linux/errno.h>
59#include <linux/netdevice.h>
60#include <linux/inetdevice.h>
Jay Vosburgha816c7c2007-02-28 17:03:37 -080061#include <linux/igmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/etherdevice.h>
63#include <linux/skbuff.h>
64#include <net/sock.h>
65#include <linux/rtnetlink.h>
66#include <linux/proc_fs.h>
67#include <linux/seq_file.h>
68#include <linux/smp.h>
69#include <linux/if_ether.h>
70#include <net/arp.h>
71#include <linux/mii.h>
72#include <linux/ethtool.h>
73#include <linux/if_vlan.h>
74#include <linux/if_bonding.h>
David Sterbab63bb732007-12-06 23:40:33 -080075#include <linux/jiffies.h>
Jay Vosburghc3ade5c2005-06-26 17:52:20 -040076#include <net/route.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020077#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include "bonding.h"
79#include "bond_3ad.h"
80#include "bond_alb.h"
81
82/*---------------------------- Module parameters ----------------------------*/
83
84/* monitor all links that often (in milliseconds). <=0 disables monitoring */
85#define BOND_LINK_MON_INTERV 0
86#define BOND_LINK_ARP_INTERV 0
87
88static int max_bonds = BOND_DEFAULT_MAX_BONDS;
Moni Shoua7893b242008-05-17 21:10:12 -070089static int num_grat_arp = 1;
Brian Haley305d5522008-11-04 17:51:14 -080090static int num_unsol_na = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int miimon = BOND_LINK_MON_INTERV;
92static int updelay = 0;
93static int downdelay = 0;
94static int use_carrier = 1;
95static char *mode = NULL;
96static char *primary = NULL;
97static char *lacp_rate = NULL;
Jay Vosburghfd989c82008-11-04 17:51:16 -080098static char *ad_select = NULL;
Jay Vosburgh169a3e62005-06-26 17:54:11 -040099static char *xmit_hash_policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static int arp_interval = BOND_LINK_ARP_INTERV;
101static char *arp_ip_target[BOND_MAX_ARP_TARGETS] = { NULL, };
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700102static char *arp_validate = NULL;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700103static char *fail_over_mac = NULL;
Mitch Williams12479f92005-11-09 10:35:44 -0800104struct bond_params bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106module_param(max_bonds, int, 0);
107MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
Moni Shoua7893b242008-05-17 21:10:12 -0700108module_param(num_grat_arp, int, 0644);
109MODULE_PARM_DESC(num_grat_arp, "Number of gratuitous ARP packets to send on failover event");
Brian Haley305d5522008-11-04 17:51:14 -0800110module_param(num_unsol_na, int, 0644);
111MODULE_PARM_DESC(num_unsol_na, "Number of unsolicited IPv6 Neighbor Advertisements packets to send on failover event");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112module_param(miimon, int, 0);
113MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
114module_param(updelay, int, 0);
115MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
116module_param(downdelay, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800117MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
118 "in milliseconds");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119module_param(use_carrier, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800120MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
121 "0 for off, 1 for on (default)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122module_param(mode, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800123MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
124 "1 for active-backup, 2 for balance-xor, "
125 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
126 "6 for balance-alb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127module_param(primary, charp, 0);
128MODULE_PARM_DESC(primary, "Primary network device to use");
129module_param(lacp_rate, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800130MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
131 "(slow/fast)");
Jay Vosburghfd989c82008-11-04 17:51:16 -0800132module_param(ad_select, charp, 0);
133MODULE_PARM_DESC(ad_select, "803.ad aggregation selection logic: stable (0, default), bandwidth (1), count (2)");
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400134module_param(xmit_hash_policy, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800135MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
136 ", 1 for layer 3+4");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137module_param(arp_interval, int, 0);
138MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
139module_param_array(arp_ip_target, charp, NULL, 0);
140MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700141module_param(arp_validate, charp, 0);
142MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700143module_param(fail_over_mac, charp, 0);
144MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to the same MAC. none (default), active or follow");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146/*----------------------------- Global variables ----------------------------*/
147
Arjan van de Venf71e1302006-03-03 21:33:57 -0500148static const char * const version =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
150
Mitch Williams12479f92005-11-09 10:35:44 -0800151LIST_HEAD(bond_dev_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153#ifdef CONFIG_PROC_FS
154static struct proc_dir_entry *bond_proc_dir = NULL;
155#endif
156
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800157extern struct rw_semaphore bonding_rwsem;
Al Virod3bb52b2007-08-22 20:06:58 -0400158static __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0, } ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static int arp_ip_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static int bond_mode = BOND_MODE_ROUNDROBIN;
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400161static int xmit_hashtype= BOND_XMIT_POLICY_LAYER2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static int lacp_fast = 0;
Jay Vosburgh217df672005-09-26 16:11:50 -0700163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Mitch Williams12479f92005-11-09 10:35:44 -0800165struct bond_parm_tbl bond_lacp_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{ "slow", AD_LACP_SLOW},
167{ "fast", AD_LACP_FAST},
168{ NULL, -1},
169};
170
Mitch Williams12479f92005-11-09 10:35:44 -0800171struct bond_parm_tbl bond_mode_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{ "balance-rr", BOND_MODE_ROUNDROBIN},
173{ "active-backup", BOND_MODE_ACTIVEBACKUP},
174{ "balance-xor", BOND_MODE_XOR},
175{ "broadcast", BOND_MODE_BROADCAST},
176{ "802.3ad", BOND_MODE_8023AD},
177{ "balance-tlb", BOND_MODE_TLB},
178{ "balance-alb", BOND_MODE_ALB},
179{ NULL, -1},
180};
181
Mitch Williams12479f92005-11-09 10:35:44 -0800182struct bond_parm_tbl xmit_hashtype_tbl[] = {
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400183{ "layer2", BOND_XMIT_POLICY_LAYER2},
184{ "layer3+4", BOND_XMIT_POLICY_LAYER34},
Jay Vosburgh6f6652b2007-12-06 23:40:34 -0800185{ "layer2+3", BOND_XMIT_POLICY_LAYER23},
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400186{ NULL, -1},
187};
188
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700189struct bond_parm_tbl arp_validate_tbl[] = {
190{ "none", BOND_ARP_VALIDATE_NONE},
191{ "active", BOND_ARP_VALIDATE_ACTIVE},
192{ "backup", BOND_ARP_VALIDATE_BACKUP},
193{ "all", BOND_ARP_VALIDATE_ALL},
194{ NULL, -1},
195};
196
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700197struct bond_parm_tbl fail_over_mac_tbl[] = {
198{ "none", BOND_FOM_NONE},
199{ "active", BOND_FOM_ACTIVE},
200{ "follow", BOND_FOM_FOLLOW},
201{ NULL, -1},
202};
203
Jay Vosburghfd989c82008-11-04 17:51:16 -0800204struct bond_parm_tbl ad_select_tbl[] = {
205{ "stable", BOND_AD_STABLE},
206{ "bandwidth", BOND_AD_BANDWIDTH},
207{ "count", BOND_AD_COUNT},
208{ NULL, -1},
209};
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*-------------------------- Forward declarations ---------------------------*/
212
Jay Vosburghc3ade5c2005-06-26 17:52:20 -0400213static void bond_send_gratuitous_arp(struct bonding *bond);
Adrian Bunkc50b85d2007-10-24 18:23:17 +0200214static void bond_deinit(struct net_device *bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216/*---------------------------- General routines -----------------------------*/
217
Adrian Bunk4ad072c2007-07-09 11:51:12 -0700218static const char *bond_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800220 static const char *names[] = {
221 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
222 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
223 [BOND_MODE_XOR] = "load balancing (xor)",
224 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
225 [BOND_MODE_8023AD]= "IEEE 802.3ad Dynamic link aggregation",
226 [BOND_MODE_TLB] = "transmit load balancing",
227 [BOND_MODE_ALB] = "adaptive load balancing",
228 };
229
230 if (mode < 0 || mode > BOND_MODE_ALB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return "unknown";
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800232
233 return names[mode];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236/*---------------------------------- VLAN -----------------------------------*/
237
238/**
239 * bond_add_vlan - add a new vlan id on bond
240 * @bond: bond that got the notification
241 * @vlan_id: the vlan id to add
242 *
243 * Returns -ENOMEM if allocation failed.
244 */
245static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
246{
247 struct vlan_entry *vlan;
248
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800249 pr_debug("bond: %s, vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 (bond ? bond->dev->name: "None"), vlan_id);
251
Brian Haley305d5522008-11-04 17:51:14 -0800252 vlan = kzalloc(sizeof(struct vlan_entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (!vlan) {
254 return -ENOMEM;
255 }
256
257 INIT_LIST_HEAD(&vlan->vlan_list);
258 vlan->vlan_id = vlan_id;
259
260 write_lock_bh(&bond->lock);
261
262 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
263
264 write_unlock_bh(&bond->lock);
265
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800266 pr_debug("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 return 0;
269}
270
271/**
272 * bond_del_vlan - delete a vlan id from bond
273 * @bond: bond that got the notification
274 * @vlan_id: the vlan id to delete
275 *
276 * returns -ENODEV if @vlan_id was not found in @bond.
277 */
278static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
279{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700280 struct vlan_entry *vlan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 int res = -ENODEV;
282
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800283 pr_debug("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 write_lock_bh(&bond->lock);
286
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700287 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (vlan->vlan_id == vlan_id) {
289 list_del(&vlan->vlan_list);
290
Holger Eitzenberger58402052008-12-09 23:07:13 -0800291 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 bond_alb_clear_vlan(bond, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800294 pr_debug("removed VLAN ID %d from bond %s\n", vlan_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 bond->dev->name);
296
297 kfree(vlan);
298
299 if (list_empty(&bond->vlan_list) &&
300 (bond->slave_cnt == 0)) {
301 /* Last VLAN removed and no slaves, so
302 * restore block on adding VLANs. This will
303 * be removed once new slaves that are not
304 * VLAN challenged will be added.
305 */
306 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
307 }
308
309 res = 0;
310 goto out;
311 }
312 }
313
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800314 pr_debug("couldn't find VLAN ID %d in bond %s\n", vlan_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 bond->dev->name);
316
317out:
318 write_unlock_bh(&bond->lock);
319 return res;
320}
321
322/**
323 * bond_has_challenged_slaves
324 * @bond: the bond we're working on
325 *
326 * Searches the slave list. Returns 1 if a vlan challenged slave
327 * was found, 0 otherwise.
328 *
329 * Assumes bond->lock is held.
330 */
331static int bond_has_challenged_slaves(struct bonding *bond)
332{
333 struct slave *slave;
334 int i;
335
336 bond_for_each_slave(bond, slave, i) {
337 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800338 pr_debug("found VLAN challenged slave - %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 slave->dev->name);
340 return 1;
341 }
342 }
343
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800344 pr_debug("no VLAN challenged slaves found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return 0;
346}
347
348/**
349 * bond_next_vlan - safely skip to the next item in the vlans list.
350 * @bond: the bond we're working on
351 * @curr: item we're advancing from
352 *
353 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
354 * or @curr->next otherwise (even if it is @curr itself again).
355 *
356 * Caller must hold bond->lock
357 */
358struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
359{
360 struct vlan_entry *next, *last;
361
362 if (list_empty(&bond->vlan_list)) {
363 return NULL;
364 }
365
366 if (!curr) {
367 next = list_entry(bond->vlan_list.next,
368 struct vlan_entry, vlan_list);
369 } else {
370 last = list_entry(bond->vlan_list.prev,
371 struct vlan_entry, vlan_list);
372 if (last == curr) {
373 next = list_entry(bond->vlan_list.next,
374 struct vlan_entry, vlan_list);
375 } else {
376 next = list_entry(curr->vlan_list.next,
377 struct vlan_entry, vlan_list);
378 }
379 }
380
381 return next;
382}
383
384/**
385 * bond_dev_queue_xmit - Prepare skb for xmit.
386 *
387 * @bond: bond device that got this skb for tx.
388 * @skb: hw accel VLAN tagged skb to transmit
389 * @slave_dev: slave that is supposed to xmit this skbuff
390 *
391 * When the bond gets an skb to transmit that is
392 * already hardware accelerated VLAN tagged, and it
393 * needs to relay this skb to a slave that is not
394 * hw accel capable, the skb needs to be "unaccelerated",
395 * i.e. strip the hwaccel tag and re-insert it as part
396 * of the payload.
397 */
398int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev)
399{
Jay Vosburgh966bc6f2008-03-21 22:29:34 -0700400 unsigned short uninitialized_var(vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 if (!list_empty(&bond->vlan_list) &&
403 !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
404 vlan_get_tag(skb, &vlan_id) == 0) {
405 skb->dev = slave_dev;
406 skb = vlan_put_tag(skb, vlan_id);
407 if (!skb) {
408 /* vlan_put_tag() frees the skb in case of error,
409 * so return success here so the calling functions
410 * won't attempt to free is again.
411 */
412 return 0;
413 }
414 } else {
415 skb->dev = slave_dev;
416 }
417
418 skb->priority = 1;
419 dev_queue_xmit(skb);
420
421 return 0;
422}
423
424/*
425 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
426 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
427 * lock because:
428 * a. This operation is performed in IOCTL context,
429 * b. The operation is protected by the RTNL semaphore in the 8021q code,
430 * c. Holding a lock with BH disabled while directly calling a base driver
431 * entry point is generally a BAD idea.
432 *
433 * The design of synchronization/protection for this operation in the 8021q
434 * module is good for one or more VLAN devices over a single physical device
435 * and cannot be extended for a teaming solution like bonding, so there is a
436 * potential race condition here where a net device from the vlan group might
437 * be referenced (either by a base driver or the 8021q code) while it is being
438 * removed from the system. However, it turns out we're not making matters
439 * worse, and if it works for regular VLAN usage it will work here too.
440*/
441
442/**
443 * bond_vlan_rx_register - Propagates registration to slaves
444 * @bond_dev: bonding net device that got called
445 * @grp: vlan group being registered
446 */
447static void bond_vlan_rx_register(struct net_device *bond_dev, struct vlan_group *grp)
448{
Wang Chen454d7c92008-11-12 23:37:49 -0800449 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 struct slave *slave;
451 int i;
452
453 bond->vlgrp = grp;
454
455 bond_for_each_slave(bond, slave, i) {
456 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800457 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800460 slave_ops->ndo_vlan_rx_register) {
461 slave_ops->ndo_vlan_rx_register(slave_dev, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463 }
464}
465
466/**
467 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
468 * @bond_dev: bonding net device that got called
469 * @vid: vlan id being added
470 */
471static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
472{
Wang Chen454d7c92008-11-12 23:37:49 -0800473 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct slave *slave;
475 int i, res;
476
477 bond_for_each_slave(bond, slave, i) {
478 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800479 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800482 slave_ops->ndo_vlan_rx_add_vid) {
483 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485 }
486
487 res = bond_add_vlan(bond, vid);
488 if (res) {
489 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800490 ": %s: Error: Failed to add vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 bond_dev->name, vid);
492 }
493}
494
495/**
496 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
497 * @bond_dev: bonding net device that got called
498 * @vid: vlan id being removed
499 */
500static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
501{
Wang Chen454d7c92008-11-12 23:37:49 -0800502 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct slave *slave;
504 struct net_device *vlan_dev;
505 int i, res;
506
507 bond_for_each_slave(bond, slave, i) {
508 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800509 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800512 slave_ops->ndo_vlan_rx_kill_vid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /* Save and then restore vlan_dev in the grp array,
514 * since the slave's driver might clear it.
515 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800516 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800517 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vid);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800518 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 }
521
522 res = bond_del_vlan(bond, vid);
523 if (res) {
524 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800525 ": %s: Error: Failed to remove vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 bond_dev->name, vid);
527 }
528}
529
530static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
531{
532 struct vlan_entry *vlan;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800533 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 write_lock_bh(&bond->lock);
536
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800537 if (list_empty(&bond->vlan_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800541 slave_ops->ndo_vlan_rx_register)
542 slave_ops->ndo_vlan_rx_register(slave_dev, bond->vlgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800545 !(slave_ops->ndo_vlan_rx_add_vid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800548 list_for_each_entry(vlan, &bond->vlan_list, vlan_list)
549 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vlan->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551out:
552 write_unlock_bh(&bond->lock);
553}
554
555static void bond_del_vlans_from_slave(struct bonding *bond, struct net_device *slave_dev)
556{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800557 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 struct vlan_entry *vlan;
559 struct net_device *vlan_dev;
560
561 write_lock_bh(&bond->lock);
562
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800563 if (list_empty(&bond->vlan_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800567 !(slave_ops->ndo_vlan_rx_kill_vid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 goto unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
571 /* Save and then restore vlan_dev in the grp array,
572 * since the slave's driver might clear it.
573 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800574 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800575 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800576 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578
579unreg:
580 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800581 slave_ops->ndo_vlan_rx_register)
582 slave_ops->ndo_vlan_rx_register(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584out:
585 write_unlock_bh(&bond->lock);
586}
587
588/*------------------------------- Link status -------------------------------*/
589
590/*
Jay Vosburghff59c452006-03-27 13:27:43 -0800591 * Set the carrier state for the master according to the state of its
592 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
593 * do special 802.3ad magic.
594 *
595 * Returns zero if carrier state does not change, nonzero if it does.
596 */
597static int bond_set_carrier(struct bonding *bond)
598{
599 struct slave *slave;
600 int i;
601
602 if (bond->slave_cnt == 0)
603 goto down;
604
605 if (bond->params.mode == BOND_MODE_8023AD)
606 return bond_3ad_set_carrier(bond);
607
608 bond_for_each_slave(bond, slave, i) {
609 if (slave->link == BOND_LINK_UP) {
610 if (!netif_carrier_ok(bond->dev)) {
611 netif_carrier_on(bond->dev);
612 return 1;
613 }
614 return 0;
615 }
616 }
617
618down:
619 if (netif_carrier_ok(bond->dev)) {
620 netif_carrier_off(bond->dev);
621 return 1;
622 }
623 return 0;
624}
625
626/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 * Get link speed and duplex from the slave's base driver
628 * using ethtool. If for some reason the call fails or the
629 * values are invalid, fake speed and duplex to 100/Full
630 * and return error.
631 */
632static int bond_update_speed_duplex(struct slave *slave)
633{
634 struct net_device *slave_dev = slave->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 struct ethtool_cmd etool;
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700636 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 /* Fake speed and duplex */
639 slave->speed = SPEED_100;
640 slave->duplex = DUPLEX_FULL;
641
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700642 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700645 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
646 if (res < 0)
647 return -1;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 switch (etool.speed) {
650 case SPEED_10:
651 case SPEED_100:
652 case SPEED_1000:
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700653 case SPEED_10000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 break;
655 default:
656 return -1;
657 }
658
659 switch (etool.duplex) {
660 case DUPLEX_FULL:
661 case DUPLEX_HALF:
662 break;
663 default:
664 return -1;
665 }
666
667 slave->speed = etool.speed;
668 slave->duplex = etool.duplex;
669
670 return 0;
671}
672
673/*
674 * if <dev> supports MII link status reporting, check its link status.
675 *
676 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
677 * depening upon the setting of the use_carrier parameter.
678 *
679 * Return either BMSR_LSTATUS, meaning that the link is up (or we
680 * can't tell and just pretend it is), or 0, meaning that the link is
681 * down.
682 *
683 * If reporting is non-zero, instead of faking link up, return -1 if
684 * both ETHTOOL and MII ioctls fail (meaning the device does not
685 * support them). If use_carrier is set, return whatever it says.
686 * It'd be nice if there was a good way to tell if a driver supports
687 * netif_carrier, but there really isn't.
688 */
689static int bond_check_dev_link(struct bonding *bond, struct net_device *slave_dev, int reporting)
690{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800691 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 static int (* ioctl)(struct net_device *, struct ifreq *, int);
693 struct ifreq ifr;
694 struct mii_ioctl_data *mii;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800696 if (bond->params.use_carrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800699 ioctl = slave_ops->ndo_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (ioctl) {
701 /* TODO: set pointer to correct ioctl on a per team member */
702 /* bases to make this more efficient. that is, once */
703 /* we determine the correct ioctl, we will always */
704 /* call it and not the others for that team */
705 /* member. */
706
707 /*
708 * We cannot assume that SIOCGMIIPHY will also read a
709 * register; not all network drivers (e.g., e100)
710 * support that.
711 */
712
713 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
714 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
715 mii = if_mii(&ifr);
716 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
717 mii->reg_num = MII_BMSR;
718 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0) {
719 return (mii->val_out & BMSR_LSTATUS);
720 }
721 }
722 }
723
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700724 /*
725 * Some drivers cache ETHTOOL_GLINK for a period of time so we only
726 * attempt to get link status from it if the above MII ioctls fail.
727 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (slave_dev->ethtool_ops) {
729 if (slave_dev->ethtool_ops->get_link) {
730 u32 link;
731
732 link = slave_dev->ethtool_ops->get_link(slave_dev);
733
734 return link ? BMSR_LSTATUS : 0;
735 }
736 }
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 /*
739 * If reporting, report that either there's no dev->do_ioctl,
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700740 * or both SIOCGMIIREG and get_link failed (meaning that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 * cannot report link status). If not reporting, pretend
742 * we're ok.
743 */
744 return (reporting ? -1 : BMSR_LSTATUS);
745}
746
747/*----------------------------- Multicast list ------------------------------*/
748
749/*
750 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
751 */
752static inline int bond_is_dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
753{
754 return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
755 dmi1->dmi_addrlen == dmi2->dmi_addrlen;
756}
757
758/*
759 * returns dmi entry if found, NULL otherwise
760 */
761static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list)
762{
763 struct dev_mc_list *idmi;
764
765 for (idmi = mc_list; idmi; idmi = idmi->next) {
766 if (bond_is_dmi_same(dmi, idmi)) {
767 return idmi;
768 }
769 }
770
771 return NULL;
772}
773
774/*
775 * Push the promiscuity flag down to appropriate slaves
776 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700777static int bond_set_promiscuity(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700779 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (USES_PRIMARY(bond->params.mode)) {
781 /* write lock already acquired */
782 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700783 err = dev_set_promiscuity(bond->curr_active_slave->dev,
784 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786 } else {
787 struct slave *slave;
788 int i;
789 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700790 err = dev_set_promiscuity(slave->dev, inc);
791 if (err)
792 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700795 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
798/*
799 * Push the allmulti flag down to all slaves
800 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700801static int bond_set_allmulti(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700803 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (USES_PRIMARY(bond->params.mode)) {
805 /* write lock already acquired */
806 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700807 err = dev_set_allmulti(bond->curr_active_slave->dev,
808 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810 } else {
811 struct slave *slave;
812 int i;
813 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700814 err = dev_set_allmulti(slave->dev, inc);
815 if (err)
816 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700819 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
822/*
823 * Add a Multicast address to slaves
824 * according to mode
825 */
826static void bond_mc_add(struct bonding *bond, void *addr, int alen)
827{
828 if (USES_PRIMARY(bond->params.mode)) {
829 /* write lock already acquired */
830 if (bond->curr_active_slave) {
831 dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
832 }
833 } else {
834 struct slave *slave;
835 int i;
836 bond_for_each_slave(bond, slave, i) {
837 dev_mc_add(slave->dev, addr, alen, 0);
838 }
839 }
840}
841
842/*
843 * Remove a multicast address from slave
844 * according to mode
845 */
846static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
847{
848 if (USES_PRIMARY(bond->params.mode)) {
849 /* write lock already acquired */
850 if (bond->curr_active_slave) {
851 dev_mc_delete(bond->curr_active_slave->dev, addr, alen, 0);
852 }
853 } else {
854 struct slave *slave;
855 int i;
856 bond_for_each_slave(bond, slave, i) {
857 dev_mc_delete(slave->dev, addr, alen, 0);
858 }
859 }
860}
861
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800862
863/*
864 * Retrieve the list of registered multicast addresses for the bonding
865 * device and retransmit an IGMP JOIN request to the current active
866 * slave.
867 */
868static void bond_resend_igmp_join_requests(struct bonding *bond)
869{
870 struct in_device *in_dev;
871 struct ip_mc_list *im;
872
873 rcu_read_lock();
874 in_dev = __in_dev_get_rcu(bond->dev);
875 if (in_dev) {
876 for (im = in_dev->mc_list; im; im = im->next) {
877 ip_mc_rejoin_group(im);
878 }
879 }
880
881 rcu_read_unlock();
882}
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884/*
885 * Totally destroys the mc_list in bond
886 */
887static void bond_mc_list_destroy(struct bonding *bond)
888{
889 struct dev_mc_list *dmi;
890
891 dmi = bond->mc_list;
892 while (dmi) {
893 bond->mc_list = dmi->next;
894 kfree(dmi);
895 dmi = bond->mc_list;
896 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800897 bond->mc_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
900/*
901 * Copy all the Multicast addresses from src to the bonding device dst
902 */
Randy Dunlapde54f392005-10-04 22:39:41 -0700903static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
Al Virodd0fc662005-10-07 07:46:04 +0100904 gfp_t gfp_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 struct dev_mc_list *dmi, *new_dmi;
907
908 for (dmi = mc_list; dmi; dmi = dmi->next) {
Randy Dunlapde54f392005-10-04 22:39:41 -0700909 new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 if (!new_dmi) {
912 /* FIXME: Potential memory leak !!! */
913 return -ENOMEM;
914 }
915
916 new_dmi->next = bond->mc_list;
917 bond->mc_list = new_dmi;
918 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
919 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
920 new_dmi->dmi_users = dmi->dmi_users;
921 new_dmi->dmi_gusers = dmi->dmi_gusers;
922 }
923
924 return 0;
925}
926
927/*
928 * flush all members of flush->mc_list from device dev->mc_list
929 */
930static void bond_mc_list_flush(struct net_device *bond_dev, struct net_device *slave_dev)
931{
Wang Chen454d7c92008-11-12 23:37:49 -0800932 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 struct dev_mc_list *dmi;
934
935 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
936 dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
937 }
938
939 if (bond->params.mode == BOND_MODE_8023AD) {
940 /* del lacpdu mc addr from mc list */
941 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
942
943 dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
944 }
945}
946
947/*--------------------------- Active slave change ---------------------------*/
948
949/*
950 * Update the mc list and multicast-related flags for the new and
951 * old active slaves (if any) according to the multicast mode, and
952 * promiscuous flags unconditionally.
953 */
954static void bond_mc_swap(struct bonding *bond, struct slave *new_active, struct slave *old_active)
955{
956 struct dev_mc_list *dmi;
957
958 if (!USES_PRIMARY(bond->params.mode)) {
959 /* nothing to do - mc list is already up-to-date on
960 * all slaves
961 */
962 return;
963 }
964
965 if (old_active) {
966 if (bond->dev->flags & IFF_PROMISC) {
967 dev_set_promiscuity(old_active->dev, -1);
968 }
969
970 if (bond->dev->flags & IFF_ALLMULTI) {
971 dev_set_allmulti(old_active->dev, -1);
972 }
973
974 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
975 dev_mc_delete(old_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
976 }
977 }
978
979 if (new_active) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700980 /* FIXME: Signal errors upstream. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (bond->dev->flags & IFF_PROMISC) {
982 dev_set_promiscuity(new_active->dev, 1);
983 }
984
985 if (bond->dev->flags & IFF_ALLMULTI) {
986 dev_set_allmulti(new_active->dev, 1);
987 }
988
989 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next) {
990 dev_mc_add(new_active->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
991 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800992 bond_resend_igmp_join_requests(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994}
995
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700996/*
997 * bond_do_fail_over_mac
998 *
999 * Perform special MAC address swapping for fail_over_mac settings
1000 *
1001 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
1002 */
1003static void bond_do_fail_over_mac(struct bonding *bond,
1004 struct slave *new_active,
1005 struct slave *old_active)
1006{
1007 u8 tmp_mac[ETH_ALEN];
1008 struct sockaddr saddr;
1009 int rv;
1010
1011 switch (bond->params.fail_over_mac) {
1012 case BOND_FOM_ACTIVE:
1013 if (new_active)
1014 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
1015 new_active->dev->addr_len);
1016 break;
1017 case BOND_FOM_FOLLOW:
1018 /*
1019 * if new_active && old_active, swap them
1020 * if just old_active, do nothing (going to no active slave)
1021 * if just new_active, set new_active to bond's MAC
1022 */
1023 if (!new_active)
1024 return;
1025
1026 write_unlock_bh(&bond->curr_slave_lock);
1027 read_unlock(&bond->lock);
1028
1029 if (old_active) {
1030 memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
1031 memcpy(saddr.sa_data, old_active->dev->dev_addr,
1032 ETH_ALEN);
1033 saddr.sa_family = new_active->dev->type;
1034 } else {
1035 memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
1036 saddr.sa_family = bond->dev->type;
1037 }
1038
1039 rv = dev_set_mac_address(new_active->dev, &saddr);
1040 if (rv) {
1041 printk(KERN_ERR DRV_NAME
1042 ": %s: Error %d setting MAC of slave %s\n",
1043 bond->dev->name, -rv, new_active->dev->name);
1044 goto out;
1045 }
1046
1047 if (!old_active)
1048 goto out;
1049
1050 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
1051 saddr.sa_family = old_active->dev->type;
1052
1053 rv = dev_set_mac_address(old_active->dev, &saddr);
1054 if (rv)
1055 printk(KERN_ERR DRV_NAME
1056 ": %s: Error %d setting MAC of slave %s\n",
1057 bond->dev->name, -rv, new_active->dev->name);
1058out:
1059 read_lock(&bond->lock);
1060 write_lock_bh(&bond->curr_slave_lock);
1061 break;
1062 default:
1063 printk(KERN_ERR DRV_NAME
1064 ": %s: bond_do_fail_over_mac impossible: bad policy %d\n",
1065 bond->dev->name, bond->params.fail_over_mac);
1066 break;
1067 }
1068
1069}
1070
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072/**
1073 * find_best_interface - select the best available slave to be the active one
1074 * @bond: our bonding struct
1075 *
1076 * Warning: Caller must hold curr_slave_lock for writing.
1077 */
1078static struct slave *bond_find_best_slave(struct bonding *bond)
1079{
1080 struct slave *new_active, *old_active;
1081 struct slave *bestslave = NULL;
1082 int mintime = bond->params.updelay;
1083 int i;
1084
1085 new_active = old_active = bond->curr_active_slave;
1086
1087 if (!new_active) { /* there were no active slaves left */
1088 if (bond->slave_cnt > 0) { /* found one slave */
1089 new_active = bond->first_slave;
1090 } else {
1091 return NULL; /* still no slave, return NULL */
1092 }
1093 }
1094
1095 /* first try the primary link; if arping, a link must tx/rx traffic
1096 * before it can be considered the curr_active_slave - also, we would skip
1097 * slaves between the curr_active_slave and primary_slave that may be up
1098 * and able to arp
1099 */
1100 if ((bond->primary_slave) &&
1101 (!bond->params.arp_interval) &&
1102 (IS_UP(bond->primary_slave->dev))) {
1103 new_active = bond->primary_slave;
1104 }
1105
1106 /* remember where to stop iterating over the slaves */
1107 old_active = new_active;
1108
1109 bond_for_each_slave_from(bond, new_active, i, old_active) {
1110 if (IS_UP(new_active->dev)) {
1111 if (new_active->link == BOND_LINK_UP) {
1112 return new_active;
1113 } else if (new_active->link == BOND_LINK_BACK) {
1114 /* link up, but waiting for stabilization */
1115 if (new_active->delay < mintime) {
1116 mintime = new_active->delay;
1117 bestslave = new_active;
1118 }
1119 }
1120 }
1121 }
1122
1123 return bestslave;
1124}
1125
1126/**
1127 * change_active_interface - change the active slave into the specified one
1128 * @bond: our bonding struct
1129 * @new: the new slave to make the active one
1130 *
1131 * Set the new slave to the bond's settings and unset them on the old
1132 * curr_active_slave.
1133 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1134 *
1135 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1136 * because it is apparently the best available slave we have, even though its
1137 * updelay hasn't timed out yet.
1138 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001139 * If new_active is not NULL, caller must hold bond->lock for read and
1140 * curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001142void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
1144 struct slave *old_active = bond->curr_active_slave;
1145
1146 if (old_active == new_active) {
1147 return;
1148 }
1149
1150 if (new_active) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07001151 new_active->jiffies = jiffies;
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (new_active->link == BOND_LINK_BACK) {
1154 if (USES_PRIMARY(bond->params.mode)) {
1155 printk(KERN_INFO DRV_NAME
1156 ": %s: making interface %s the new "
1157 "active one %d ms earlier.\n",
1158 bond->dev->name, new_active->dev->name,
1159 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1160 }
1161
1162 new_active->delay = 0;
1163 new_active->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 if (bond->params.mode == BOND_MODE_8023AD) {
1166 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
1167 }
1168
Holger Eitzenberger58402052008-12-09 23:07:13 -08001169 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 } else {
1172 if (USES_PRIMARY(bond->params.mode)) {
1173 printk(KERN_INFO DRV_NAME
1174 ": %s: making interface %s the new "
1175 "active one.\n",
1176 bond->dev->name, new_active->dev->name);
1177 }
1178 }
1179 }
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (USES_PRIMARY(bond->params.mode)) {
1182 bond_mc_swap(bond, new_active, old_active);
1183 }
1184
Holger Eitzenberger58402052008-12-09 23:07:13 -08001185 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 bond_alb_handle_active_change(bond, new_active);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001187 if (old_active)
1188 bond_set_slave_inactive_flags(old_active);
1189 if (new_active)
1190 bond_set_slave_active_flags(new_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 } else {
1192 bond->curr_active_slave = new_active;
1193 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001194
1195 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
1196 if (old_active) {
1197 bond_set_slave_inactive_flags(old_active);
1198 }
1199
1200 if (new_active) {
1201 bond_set_slave_active_flags(new_active);
Moni Shoua2ab82852007-10-09 19:43:39 -07001202
Or Gerlitz709f8a42008-06-13 18:12:01 -07001203 if (bond->params.fail_over_mac)
1204 bond_do_fail_over_mac(bond, new_active,
1205 old_active);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001206
Or Gerlitz709f8a42008-06-13 18:12:01 -07001207 bond->send_grat_arp = bond->params.num_grat_arp;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07001208 bond_send_gratuitous_arp(bond);
Or Gerlitz01f31092008-06-13 18:12:02 -07001209
Brian Haley305d5522008-11-04 17:51:14 -08001210 bond->send_unsol_na = bond->params.num_unsol_na;
1211 bond_send_unsolicited_na(bond);
1212
Or Gerlitz01f31092008-06-13 18:12:02 -07001213 write_unlock_bh(&bond->curr_slave_lock);
1214 read_unlock(&bond->lock);
1215
1216 netdev_bonding_change(bond->dev);
1217
1218 read_lock(&bond->lock);
1219 write_lock_bh(&bond->curr_slave_lock);
Moni Shoua7893b242008-05-17 21:10:12 -07001220 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
1224/**
1225 * bond_select_active_slave - select a new active slave, if needed
1226 * @bond: our bonding struct
1227 *
1228 * This functions shoud be called when one of the following occurs:
1229 * - The old curr_active_slave has been released or lost its link.
1230 * - The primary_slave has got its link back.
1231 * - A slave has got its link back and there's no old curr_active_slave.
1232 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001233 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001235void bond_select_active_slave(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
1237 struct slave *best_slave;
Jay Vosburghff59c452006-03-27 13:27:43 -08001238 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 best_slave = bond_find_best_slave(bond);
1241 if (best_slave != bond->curr_active_slave) {
1242 bond_change_active_slave(bond, best_slave);
Jay Vosburghff59c452006-03-27 13:27:43 -08001243 rv = bond_set_carrier(bond);
1244 if (!rv)
1245 return;
1246
1247 if (netif_carrier_ok(bond->dev)) {
1248 printk(KERN_INFO DRV_NAME
1249 ": %s: first active interface up!\n",
1250 bond->dev->name);
1251 } else {
1252 printk(KERN_INFO DRV_NAME ": %s: "
1253 "now running without any active interface !\n",
1254 bond->dev->name);
1255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257}
1258
1259/*--------------------------- slave list handling ---------------------------*/
1260
1261/*
1262 * This function attaches the slave to the end of list.
1263 *
1264 * bond->lock held for writing by caller.
1265 */
1266static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1267{
1268 if (bond->first_slave == NULL) { /* attaching the first slave */
1269 new_slave->next = new_slave;
1270 new_slave->prev = new_slave;
1271 bond->first_slave = new_slave;
1272 } else {
1273 new_slave->next = bond->first_slave;
1274 new_slave->prev = bond->first_slave->prev;
1275 new_slave->next->prev = new_slave;
1276 new_slave->prev->next = new_slave;
1277 }
1278
1279 bond->slave_cnt++;
1280}
1281
1282/*
1283 * This function detaches the slave from the list.
1284 * WARNING: no check is made to verify if the slave effectively
1285 * belongs to <bond>.
1286 * Nothing is freed on return, structures are just unchained.
1287 * If any slave pointer in bond was pointing to <slave>,
1288 * it should be changed by the calling function.
1289 *
1290 * bond->lock held for writing by caller.
1291 */
1292static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1293{
1294 if (slave->next) {
1295 slave->next->prev = slave->prev;
1296 }
1297
1298 if (slave->prev) {
1299 slave->prev->next = slave->next;
1300 }
1301
1302 if (bond->first_slave == slave) { /* slave is the first slave */
1303 if (bond->slave_cnt > 1) { /* there are more slave */
1304 bond->first_slave = slave->next;
1305 } else {
1306 bond->first_slave = NULL; /* slave was the last one */
1307 }
1308 }
1309
1310 slave->next = NULL;
1311 slave->prev = NULL;
1312 bond->slave_cnt--;
1313}
1314
1315/*---------------------------------- IOCTL ----------------------------------*/
1316
Adrian Bunk4ad072c2007-07-09 11:51:12 -07001317static int bond_sethwaddr(struct net_device *bond_dev,
1318 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001320 pr_debug("bond_dev=%p\n", bond_dev);
1321 pr_debug("slave_dev=%p\n", slave_dev);
1322 pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1324 return 0;
1325}
1326
Herbert Xu7f353bf2007-08-10 15:47:58 -07001327#define BOND_VLAN_FEATURES \
1328 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1329 NETIF_F_HW_VLAN_FILTER)
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001330
1331/*
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001332 * Compute the common dev->feature set available to all slaves. Some
Herbert Xu7f353bf2007-08-10 15:47:58 -07001333 * feature bits are managed elsewhere, so preserve those feature bits
1334 * on the master device.
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001335 */
1336static int bond_compute_features(struct bonding *bond)
1337{
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001338 struct slave *slave;
1339 struct net_device *bond_dev = bond->dev;
Herbert Xu7f353bf2007-08-10 15:47:58 -07001340 unsigned long features = bond_dev->features;
Moni Shoua3158bf72007-10-09 19:43:41 -07001341 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1342 bond_dev->hard_header_len);
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001343 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001344
Herbert Xu7f353bf2007-08-10 15:47:58 -07001345 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
Herbert Xub63365a2008-10-23 01:11:29 -07001346 features |= NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1347
1348 if (!bond->first_slave)
1349 goto done;
1350
1351 features &= ~NETIF_F_ONE_FOR_ALL;
Herbert Xu7f353bf2007-08-10 15:47:58 -07001352
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001353 bond_for_each_slave(bond, slave, i) {
Herbert Xub63365a2008-10-23 01:11:29 -07001354 features = netdev_increment_features(features,
1355 slave->dev->features,
1356 NETIF_F_ONE_FOR_ALL);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001357 if (slave->dev->hard_header_len > max_hard_header_len)
1358 max_hard_header_len = slave->dev->hard_header_len;
1359 }
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001360
Herbert Xub63365a2008-10-23 01:11:29 -07001361done:
Herbert Xu7f353bf2007-08-10 15:47:58 -07001362 features |= (bond_dev->features & BOND_VLAN_FEATURES);
Herbert Xub63365a2008-10-23 01:11:29 -07001363 bond_dev->features = netdev_fix_features(features, NULL);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001364 bond_dev->hard_header_len = max_hard_header_len;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001365
1366 return 0;
1367}
1368
Moni Shoua872254d2007-10-09 19:43:38 -07001369static void bond_setup_by_slave(struct net_device *bond_dev,
1370 struct net_device *slave_dev)
1371{
Wang Chen454d7c92008-11-12 23:37:49 -08001372 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07001373
Stephen Hemminger00829822008-11-20 20:14:53 -08001374 bond_dev->header_ops = slave_dev->header_ops;
Moni Shoua872254d2007-10-09 19:43:38 -07001375
1376 bond_dev->type = slave_dev->type;
1377 bond_dev->hard_header_len = slave_dev->hard_header_len;
1378 bond_dev->addr_len = slave_dev->addr_len;
1379
1380 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1381 slave_dev->addr_len);
Moni Shouad90a1622007-10-09 19:43:43 -07001382 bond->setup_by_slave = 1;
Moni Shoua872254d2007-10-09 19:43:38 -07001383}
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385/* enslave device <slave> to bond device <master> */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001386int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Wang Chen454d7c92008-11-12 23:37:49 -08001388 struct bonding *bond = netdev_priv(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001389 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 struct slave *new_slave = NULL;
1391 struct dev_mc_list *dmi;
1392 struct sockaddr addr;
1393 int link_reporting;
1394 int old_features = bond_dev->features;
1395 int res = 0;
1396
nsxfreddy@gmail.com552709d2005-09-21 14:18:04 -05001397 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001398 slave_ops->ndo_do_ioctl == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001400 ": %s: Warning: no link monitoring support for %s\n",
1401 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403
1404 /* bond must be initialized by bond_open() before enslaving */
1405 if (!(bond_dev->flags & IFF_UP)) {
Moni Shoua6b1bf092007-10-09 19:43:40 -07001406 printk(KERN_WARNING DRV_NAME
1407 " %s: master_dev is not up in bond_enslave\n",
1408 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410
1411 /* already enslaved */
1412 if (slave_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001413 pr_debug("Error, Device was already enslaved\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return -EBUSY;
1415 }
1416
1417 /* vlan challenged mutual exclusion */
1418 /* no need to lock since we're protected by rtnl_lock */
1419 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001420 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (!list_empty(&bond->vlan_list)) {
1422 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001423 ": %s: Error: cannot enslave VLAN "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 "challenged slave %s on VLAN enabled "
Mitch Williams4e0952c2005-11-09 10:34:57 -08001425 "bond %s\n", bond_dev->name, slave_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 bond_dev->name);
1427 return -EPERM;
1428 } else {
1429 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001430 ": %s: Warning: enslaved VLAN challenged "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 "slave %s. Adding VLANs will be blocked as "
1432 "long as %s is part of bond %s\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001433 bond_dev->name, slave_dev->name, slave_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 bond_dev->name);
1435 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1436 }
1437 } else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001438 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (bond->slave_cnt == 0) {
1440 /* First slave, and it is not VLAN challenged,
1441 * so remove the block of adding VLANs over the bond.
1442 */
1443 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1444 }
1445 }
1446
Jay Vosburgh217df672005-09-26 16:11:50 -07001447 /*
1448 * Old ifenslave binaries are no longer supported. These can
1449 * be identified with moderate accurary by the state of the slave:
1450 * the current ifenslave will set the interface down prior to
1451 * enslaving it; the old ifenslave will not.
1452 */
1453 if ((slave_dev->flags & IFF_UP)) {
1454 printk(KERN_ERR DRV_NAME ": %s is up. "
1455 "This may be due to an out of date ifenslave.\n",
1456 slave_dev->name);
1457 res = -EPERM;
1458 goto err_undo_flags;
1459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Moni Shoua872254d2007-10-09 19:43:38 -07001461 /* set bonding device ether type by slave - bonding netdevices are
1462 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1463 * there is a need to override some of the type dependent attribs/funcs.
1464 *
1465 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1466 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1467 */
1468 if (bond->slave_cnt == 0) {
1469 if (slave_dev->type != ARPHRD_ETHER)
1470 bond_setup_by_slave(bond_dev, slave_dev);
1471 } else if (bond_dev->type != slave_dev->type) {
1472 printk(KERN_ERR DRV_NAME ": %s ether type (%d) is different "
1473 "from other slaves (%d), can not enslave it.\n",
1474 slave_dev->name,
1475 slave_dev->type, bond_dev->type);
1476 res = -EINVAL;
1477 goto err_undo_flags;
1478 }
1479
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001480 if (slave_ops->ndo_set_mac_address == NULL) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001481 if (bond->slave_cnt == 0) {
1482 printk(KERN_WARNING DRV_NAME
Jay Vosburghdd957c52007-10-09 19:57:24 -07001483 ": %s: Warning: The first slave device "
1484 "specified does not support setting the MAC "
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001485 "address. Setting fail_over_mac to active.",
Jay Vosburghdd957c52007-10-09 19:57:24 -07001486 bond_dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001487 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1488 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001489 printk(KERN_ERR DRV_NAME
Jay Vosburghdd957c52007-10-09 19:57:24 -07001490 ": %s: Error: The slave device specified "
1491 "does not support setting the MAC address, "
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001492 "but fail_over_mac is not set to active.\n"
Moni Shoua2ab82852007-10-09 19:43:39 -07001493 , bond_dev->name);
1494 res = -EOPNOTSUPP;
1495 goto err_undo_flags;
1496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
1498
Joe Jin243cb4e2007-02-06 14:16:40 -08001499 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (!new_slave) {
1501 res = -ENOMEM;
1502 goto err_undo_flags;
1503 }
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 /* save slave's original flags before calling
1506 * netdev_set_master and dev_open
1507 */
1508 new_slave->original_flags = slave_dev->flags;
1509
Jay Vosburgh217df672005-09-26 16:11:50 -07001510 /*
1511 * Save slave's original ("permanent") mac address for modes
1512 * that need it, and for restoring it upon release, and then
1513 * set it to the master's address
1514 */
1515 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Jay Vosburghdd957c52007-10-09 19:57:24 -07001517 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001518 /*
1519 * Set slave to master's mac address. The application already
1520 * set the master's mac address to that of the first slave
1521 */
1522 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1523 addr.sa_family = slave_dev->type;
1524 res = dev_set_mac_address(slave_dev, &addr);
1525 if (res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001526 pr_debug("Error %d calling set_mac_address\n", res);
Moni Shoua2ab82852007-10-09 19:43:39 -07001527 goto err_free;
1528 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001531 res = netdev_set_master(slave_dev, bond_dev);
1532 if (res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001533 pr_debug("Error %d calling netdev_set_master\n", res);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001534 goto err_restore_mac;
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001535 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001536 /* open the slave since the application closed it */
1537 res = dev_open(slave_dev);
1538 if (res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001539 pr_debug("Openning slave %s failed\n", slave_dev->name);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001540 goto err_unset_master;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 new_slave->dev = slave_dev;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001544 slave_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Holger Eitzenberger58402052008-12-09 23:07:13 -08001546 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 /* bond_alb_init_slave() must be called before all other stages since
1548 * it might fail and we do not want to have to undo everything
1549 */
1550 res = bond_alb_init_slave(bond, new_slave);
1551 if (res) {
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001552 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
1554 }
1555
1556 /* If the mode USES_PRIMARY, then the new slave gets the
1557 * master's promisc (and mc) settings only if it becomes the
1558 * curr_active_slave, and that is taken care of later when calling
1559 * bond_change_active()
1560 */
1561 if (!USES_PRIMARY(bond->params.mode)) {
1562 /* set promiscuity level to new slave */
1563 if (bond_dev->flags & IFF_PROMISC) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001564 res = dev_set_promiscuity(slave_dev, 1);
1565 if (res)
1566 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568
1569 /* set allmulti level to new slave */
1570 if (bond_dev->flags & IFF_ALLMULTI) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001571 res = dev_set_allmulti(slave_dev, 1);
1572 if (res)
1573 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
1575
David S. Millerb9e40852008-07-15 00:15:08 -07001576 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 /* upload master's mc_list to new slave */
1578 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
1579 dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1580 }
David S. Millerb9e40852008-07-15 00:15:08 -07001581 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
1583
1584 if (bond->params.mode == BOND_MODE_8023AD) {
1585 /* add lacpdu mc addr to mc list */
1586 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1587
1588 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1589 }
1590
1591 bond_add_vlans_on_slave(bond, slave_dev);
1592
1593 write_lock_bh(&bond->lock);
1594
1595 bond_attach_slave(bond, new_slave);
1596
1597 new_slave->delay = 0;
1598 new_slave->link_failure_count = 0;
1599
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001600 bond_compute_features(bond);
1601
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001602 write_unlock_bh(&bond->lock);
1603
1604 read_lock(&bond->lock);
1605
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001606 new_slave->last_arp_rx = jiffies;
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (bond->params.miimon && !bond->params.use_carrier) {
1609 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1610
1611 if ((link_reporting == -1) && !bond->params.arp_interval) {
1612 /*
1613 * miimon is set but a bonded network driver
1614 * does not support ETHTOOL/MII and
1615 * arp_interval is not set. Note: if
1616 * use_carrier is enabled, we will never go
1617 * here (because netif_carrier is always
1618 * supported); thus, we don't need to change
1619 * the messages for netif_carrier.
1620 */
1621 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001622 ": %s: Warning: MII and ETHTOOL support not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 "available for interface %s, and "
1624 "arp_interval/arp_ip_target module parameters "
1625 "not specified, thus bonding will not detect "
1626 "link failures! see bonding.txt for details.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001627 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 } else if (link_reporting == -1) {
1629 /* unable get link status using mii/ethtool */
1630 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001631 ": %s: Warning: can't get link status from "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 "interface %s; the network driver associated "
1633 "with this interface does not support MII or "
1634 "ETHTOOL link status reporting, thus miimon "
1635 "has no effect on this interface.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001636 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
1638 }
1639
1640 /* check for initial state */
1641 if (!bond->params.miimon ||
1642 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1643 if (bond->params.updelay) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001644 pr_debug("Initial state of slave_dev is "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 "BOND_LINK_BACK\n");
1646 new_slave->link = BOND_LINK_BACK;
1647 new_slave->delay = bond->params.updelay;
1648 } else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001649 pr_debug("Initial state of slave_dev is "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 "BOND_LINK_UP\n");
1651 new_slave->link = BOND_LINK_UP;
1652 }
1653 new_slave->jiffies = jiffies;
1654 } else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001655 pr_debug("Initial state of slave_dev is "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 "BOND_LINK_DOWN\n");
1657 new_slave->link = BOND_LINK_DOWN;
1658 }
1659
1660 if (bond_update_speed_duplex(new_slave) &&
1661 (new_slave->link != BOND_LINK_DOWN)) {
1662 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001663 ": %s: Warning: failed to get speed and duplex from %s, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 "assumed to be 100Mb/sec and Full.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001665 bond_dev->name, new_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 if (bond->params.mode == BOND_MODE_8023AD) {
Mitch Williams4e0952c2005-11-09 10:34:57 -08001668 printk(KERN_WARNING DRV_NAME
1669 ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 "support in base driver for proper aggregator "
Mitch Williams4e0952c2005-11-09 10:34:57 -08001671 "selection.\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673 }
1674
1675 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1676 /* if there is a primary slave, remember it */
1677 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
1678 bond->primary_slave = new_slave;
1679 }
1680 }
1681
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001682 write_lock_bh(&bond->curr_slave_lock);
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 switch (bond->params.mode) {
1685 case BOND_MODE_ACTIVEBACKUP:
Jay Vosburgh8a8e4472006-09-22 21:56:15 -07001686 bond_set_slave_inactive_flags(new_slave);
1687 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 break;
1689 case BOND_MODE_8023AD:
1690 /* in 802.3ad mode, the internal mechanism
1691 * will activate the slaves in the selected
1692 * aggregator
1693 */
1694 bond_set_slave_inactive_flags(new_slave);
1695 /* if this is the first slave */
1696 if (bond->slave_cnt == 1) {
1697 SLAVE_AD_INFO(new_slave).id = 1;
1698 /* Initialize AD with the number of times that the AD timer is called in 1 second
1699 * can be called only after the mac address of the bond is set
1700 */
1701 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1702 bond->params.lacp_fast);
1703 } else {
1704 SLAVE_AD_INFO(new_slave).id =
1705 SLAVE_AD_INFO(new_slave->prev).id + 1;
1706 }
1707
1708 bond_3ad_bind_slave(new_slave);
1709 break;
1710 case BOND_MODE_TLB:
1711 case BOND_MODE_ALB:
1712 new_slave->state = BOND_STATE_ACTIVE;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001713 bond_set_slave_inactive_flags(new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 break;
1715 default:
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001716 pr_debug("This slave is always active in trunk mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 /* always active in trunk mode */
1719 new_slave->state = BOND_STATE_ACTIVE;
1720
1721 /* In trunking mode there is little meaning to curr_active_slave
1722 * anyway (it holds no special properties of the bond device),
1723 * so we can change it without calling change_active_interface()
1724 */
1725 if (!bond->curr_active_slave) {
1726 bond->curr_active_slave = new_slave;
1727 }
1728 break;
1729 } /* switch(bond_mode) */
1730
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001731 write_unlock_bh(&bond->curr_slave_lock);
1732
Jay Vosburghff59c452006-03-27 13:27:43 -08001733 bond_set_carrier(bond);
1734
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001735 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001737 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1738 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001739 goto err_close;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 printk(KERN_INFO DRV_NAME
1742 ": %s: enslaving %s as a%s interface with a%s link.\n",
1743 bond_dev->name, slave_dev->name,
1744 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1745 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1746
1747 /* enslave is successful */
1748 return 0;
1749
1750/* Undo stages on error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751err_close:
1752 dev_close(slave_dev);
1753
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001754err_unset_master:
1755 netdev_set_master(slave_dev, NULL);
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757err_restore_mac:
Jay Vosburghdd957c52007-10-09 19:57:24 -07001758 if (!bond->params.fail_over_mac) {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001759 /* XXX TODO - fom follow mode needs to change master's
1760 * MAC if this slave's MAC is in use by the bond, or at
1761 * least print a warning.
1762 */
Moni Shoua2ab82852007-10-09 19:43:39 -07001763 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1764 addr.sa_family = slave_dev->type;
1765 dev_set_mac_address(slave_dev, &addr);
1766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768err_free:
1769 kfree(new_slave);
1770
1771err_undo_flags:
1772 bond_dev->features = old_features;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return res;
1775}
1776
1777/*
1778 * Try to release the slave device <slave> from the bond device <master>
1779 * It is legal to access curr_active_slave without a lock because all the function
1780 * is write-locked.
1781 *
1782 * The rules for slave state should be:
1783 * for Active/Backup:
1784 * Active stays on all backups go down
1785 * for Bonded connections:
1786 * The first up interface should be left on and all others downed.
1787 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001788int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
Wang Chen454d7c92008-11-12 23:37:49 -08001790 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 struct slave *slave, *oldcurrent;
1792 struct sockaddr addr;
1793 int mac_addr_differ;
1794
1795 /* slave is not a slave or master is not master of this slave */
1796 if (!(slave_dev->flags & IFF_SLAVE) ||
1797 (slave_dev->master != bond_dev)) {
1798 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001799 ": %s: Error: cannot release %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 bond_dev->name, slave_dev->name);
1801 return -EINVAL;
1802 }
1803
1804 write_lock_bh(&bond->lock);
1805
1806 slave = bond_get_slave_by_dev(bond, slave_dev);
1807 if (!slave) {
1808 /* not a slave of this bond */
1809 printk(KERN_INFO DRV_NAME
1810 ": %s: %s not enslaved\n",
1811 bond_dev->name, slave_dev->name);
Jay Vosburghf5e2a7b2006-02-07 21:17:22 -08001812 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return -EINVAL;
1814 }
1815
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001816 if (!bond->params.fail_over_mac) {
1817 mac_addr_differ = memcmp(bond_dev->dev_addr, slave->perm_hwaddr,
1818 ETH_ALEN);
1819 if (!mac_addr_differ && (bond->slave_cnt > 1))
1820 printk(KERN_WARNING DRV_NAME
1821 ": %s: Warning: the permanent HWaddr of %s - "
Johannes Berge1749612008-10-27 15:59:26 -07001822 "%pM - is still in use by %s. "
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001823 "Set the HWaddr of %s to a different address "
1824 "to avoid conflicts.\n",
1825 bond_dev->name, slave_dev->name,
Johannes Berge1749612008-10-27 15:59:26 -07001826 slave->perm_hwaddr,
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001827 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
1829
1830 /* Inform AD package of unbinding of slave. */
1831 if (bond->params.mode == BOND_MODE_8023AD) {
1832 /* must be called before the slave is
1833 * detached from the list
1834 */
1835 bond_3ad_unbind_slave(slave);
1836 }
1837
1838 printk(KERN_INFO DRV_NAME
1839 ": %s: releasing %s interface %s\n",
1840 bond_dev->name,
1841 (slave->state == BOND_STATE_ACTIVE)
1842 ? "active" : "backup",
1843 slave_dev->name);
1844
1845 oldcurrent = bond->curr_active_slave;
1846
1847 bond->current_arp_slave = NULL;
1848
1849 /* release the slave from its bond */
1850 bond_detach_slave(bond, slave);
1851
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001852 bond_compute_features(bond);
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (bond->primary_slave == slave) {
1855 bond->primary_slave = NULL;
1856 }
1857
1858 if (oldcurrent == slave) {
1859 bond_change_active_slave(bond, NULL);
1860 }
1861
Holger Eitzenberger58402052008-12-09 23:07:13 -08001862 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 /* Must be called only after the slave has been
1864 * detached from the list and the curr_active_slave
1865 * has been cleared (if our_slave == old_current),
1866 * but before a new active slave is selected.
1867 */
Jay Vosburgh25433312008-01-17 16:24:59 -08001868 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 bond_alb_deinit_slave(bond, slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08001870 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
1872
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001873 if (oldcurrent == slave) {
1874 /*
1875 * Note that we hold RTNL over this sequence, so there
1876 * is no concern that another slave add/remove event
1877 * will interfere.
1878 */
1879 write_unlock_bh(&bond->lock);
1880 read_lock(&bond->lock);
1881 write_lock_bh(&bond->curr_slave_lock);
1882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 bond_select_active_slave(bond);
1884
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001885 write_unlock_bh(&bond->curr_slave_lock);
1886 read_unlock(&bond->lock);
1887 write_lock_bh(&bond->lock);
1888 }
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (bond->slave_cnt == 0) {
Jay Vosburghff59c452006-03-27 13:27:43 -08001891 bond_set_carrier(bond);
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 /* if the last slave was removed, zero the mac address
1894 * of the master so it will be set by the application
1895 * to the mac address of the first slave
1896 */
1897 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1898
1899 if (list_empty(&bond->vlan_list)) {
1900 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1901 } else {
1902 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001903 ": %s: Warning: clearing HW address of %s while it "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 "still has VLANs.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001905 bond_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001907 ": %s: When re-adding slaves, make sure the bond's "
1908 "HW address matches its VLANs'.\n",
1909 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
1912 !bond_has_challenged_slaves(bond)) {
1913 printk(KERN_INFO DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001914 ": %s: last VLAN challenged slave %s "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 "left bond %s. VLAN blocking is removed\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001916 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1918 }
1919
1920 write_unlock_bh(&bond->lock);
1921
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001922 /* must do this from outside any spinlocks */
1923 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 bond_del_vlans_from_slave(bond, slave_dev);
1926
1927 /* If the mode USES_PRIMARY, then we should only remove its
1928 * promisc and mc settings if it was the curr_active_slave, but that was
1929 * already taken care of above when we detached the slave
1930 */
1931 if (!USES_PRIMARY(bond->params.mode)) {
1932 /* unset promiscuity level from slave */
1933 if (bond_dev->flags & IFF_PROMISC) {
1934 dev_set_promiscuity(slave_dev, -1);
1935 }
1936
1937 /* unset allmulti level from slave */
1938 if (bond_dev->flags & IFF_ALLMULTI) {
1939 dev_set_allmulti(slave_dev, -1);
1940 }
1941
1942 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07001943 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07001945 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
1947
1948 netdev_set_master(slave_dev, NULL);
1949
1950 /* close slave before restoring its mac address */
1951 dev_close(slave_dev);
1952
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001953 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001954 /* restore original ("permanent") mac address */
1955 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1956 addr.sa_family = slave_dev->type;
1957 dev_set_mac_address(slave_dev, &addr);
1958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001960 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001961 IFF_SLAVE_INACTIVE | IFF_BONDING |
1962 IFF_SLAVE_NEEDARP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 kfree(slave);
1965
1966 return 0; /* deletion OK */
1967}
1968
1969/*
Moni Shouad90a1622007-10-09 19:43:43 -07001970* Destroy a bonding device.
1971* Must be under rtnl_lock when this function is called.
1972*/
1973void bond_destroy(struct bonding *bond)
1974{
1975 bond_deinit(bond->dev);
1976 bond_destroy_sysfs_entry(bond);
Jay Vosburgh8cbdeec2007-11-13 21:16:29 -08001977 unregister_netdevice(bond->dev);
Moni Shouad90a1622007-10-09 19:43:43 -07001978}
1979
Jay Vosburgha434e432008-10-30 17:41:15 -07001980static void bond_destructor(struct net_device *bond_dev)
1981{
Wang Chen244ef9b2008-12-03 21:14:04 -08001982 struct bonding *bond = netdev_priv(bond_dev);
Jay Vosburgha434e432008-10-30 17:41:15 -07001983
1984 if (bond->wq)
1985 destroy_workqueue(bond->wq);
1986
1987 netif_addr_lock_bh(bond_dev);
1988 bond_mc_list_destroy(bond);
1989 netif_addr_unlock_bh(bond_dev);
1990
1991 free_netdev(bond_dev);
1992}
1993
Moni Shouad90a1622007-10-09 19:43:43 -07001994/*
1995* First release a slave and than destroy the bond if no more slaves iare left.
1996* Must be under rtnl_lock when this function is called.
1997*/
1998int bond_release_and_destroy(struct net_device *bond_dev, struct net_device *slave_dev)
1999{
Wang Chen454d7c92008-11-12 23:37:49 -08002000 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002001 int ret;
2002
2003 ret = bond_release(bond_dev, slave_dev);
2004 if ((ret == 0) && (bond->slave_cnt == 0)) {
2005 printk(KERN_INFO DRV_NAME ": %s: destroying bond %s.\n",
2006 bond_dev->name, bond_dev->name);
2007 bond_destroy(bond);
2008 }
2009 return ret;
2010}
2011
2012/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 * This function releases all slaves.
2014 */
2015static int bond_release_all(struct net_device *bond_dev)
2016{
Wang Chen454d7c92008-11-12 23:37:49 -08002017 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 struct slave *slave;
2019 struct net_device *slave_dev;
2020 struct sockaddr addr;
2021
2022 write_lock_bh(&bond->lock);
2023
Jay Vosburghff59c452006-03-27 13:27:43 -08002024 netif_carrier_off(bond_dev);
2025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if (bond->slave_cnt == 0) {
2027 goto out;
2028 }
2029
2030 bond->current_arp_slave = NULL;
2031 bond->primary_slave = NULL;
2032 bond_change_active_slave(bond, NULL);
2033
2034 while ((slave = bond->first_slave) != NULL) {
2035 /* Inform AD package of unbinding of slave
2036 * before slave is detached from the list.
2037 */
2038 if (bond->params.mode == BOND_MODE_8023AD) {
2039 bond_3ad_unbind_slave(slave);
2040 }
2041
2042 slave_dev = slave->dev;
2043 bond_detach_slave(bond, slave);
2044
Jay Vosburgh25433312008-01-17 16:24:59 -08002045 /* now that the slave is detached, unlock and perform
2046 * all the undo steps that should not be called from
2047 * within a lock.
2048 */
2049 write_unlock_bh(&bond->lock);
2050
Holger Eitzenberger58402052008-12-09 23:07:13 -08002051 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 /* must be called only after the slave
2053 * has been detached from the list
2054 */
2055 bond_alb_deinit_slave(bond, slave);
2056 }
2057
Arthur Kepner8531c5f2005-08-23 01:34:53 -04002058 bond_compute_features(bond);
2059
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002060 bond_destroy_slave_symlinks(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 bond_del_vlans_from_slave(bond, slave_dev);
2062
2063 /* If the mode USES_PRIMARY, then we should only remove its
2064 * promisc and mc settings if it was the curr_active_slave, but that was
2065 * already taken care of above when we detached the slave
2066 */
2067 if (!USES_PRIMARY(bond->params.mode)) {
2068 /* unset promiscuity level from slave */
2069 if (bond_dev->flags & IFF_PROMISC) {
2070 dev_set_promiscuity(slave_dev, -1);
2071 }
2072
2073 /* unset allmulti level from slave */
2074 if (bond_dev->flags & IFF_ALLMULTI) {
2075 dev_set_allmulti(slave_dev, -1);
2076 }
2077
2078 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002079 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002081 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
2083
2084 netdev_set_master(slave_dev, NULL);
2085
2086 /* close slave before restoring its mac address */
2087 dev_close(slave_dev);
2088
Jay Vosburghdd957c52007-10-09 19:57:24 -07002089 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002090 /* restore original ("permanent") mac address*/
2091 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2092 addr.sa_family = slave_dev->type;
2093 dev_set_mac_address(slave_dev, &addr);
2094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002096 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
2097 IFF_SLAVE_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 kfree(slave);
2100
2101 /* re-acquire the lock before getting the next slave */
2102 write_lock_bh(&bond->lock);
2103 }
2104
2105 /* zero the mac address of the master so it will be
2106 * set by the application to the mac address of the
2107 * first slave
2108 */
2109 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2110
2111 if (list_empty(&bond->vlan_list)) {
2112 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2113 } else {
2114 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08002115 ": %s: Warning: clearing HW address of %s while it "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 "still has VLANs.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08002117 bond_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08002119 ": %s: When re-adding slaves, make sure the bond's "
2120 "HW address matches its VLANs'.\n",
2121 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 }
2123
2124 printk(KERN_INFO DRV_NAME
2125 ": %s: released all slaves\n",
2126 bond_dev->name);
2127
2128out:
2129 write_unlock_bh(&bond->lock);
2130
2131 return 0;
2132}
2133
2134/*
2135 * This function changes the active slave to slave <slave_dev>.
2136 * It returns -EINVAL in the following cases.
2137 * - <slave_dev> is not found in the list.
2138 * - There is not active slave now.
2139 * - <slave_dev> is already active.
2140 * - The link state of <slave_dev> is not BOND_LINK_UP.
2141 * - <slave_dev> is not running.
2142 * In these cases, this fuction does nothing.
2143 * In the other cases, currnt_slave pointer is changed and 0 is returned.
2144 */
2145static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2146{
Wang Chen454d7c92008-11-12 23:37:49 -08002147 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 struct slave *old_active = NULL;
2149 struct slave *new_active = NULL;
2150 int res = 0;
2151
2152 if (!USES_PRIMARY(bond->params.mode)) {
2153 return -EINVAL;
2154 }
2155
2156 /* Verify that master_dev is indeed the master of slave_dev */
2157 if (!(slave_dev->flags & IFF_SLAVE) ||
2158 (slave_dev->master != bond_dev)) {
2159 return -EINVAL;
2160 }
2161
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002162 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002164 read_lock(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 old_active = bond->curr_active_slave;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002166 read_unlock(&bond->curr_slave_lock);
2167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 new_active = bond_get_slave_by_dev(bond, slave_dev);
2169
2170 /*
2171 * Changing to the current active: do nothing; return success.
2172 */
2173 if (new_active && (new_active == old_active)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002174 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 return 0;
2176 }
2177
2178 if ((new_active) &&
2179 (old_active) &&
2180 (new_active->link == BOND_LINK_UP) &&
2181 IS_UP(new_active->dev)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002182 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 bond_change_active_slave(bond, new_active);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002184 write_unlock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 } else {
2186 res = -EINVAL;
2187 }
2188
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002189 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 return res;
2192}
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2195{
Wang Chen454d7c92008-11-12 23:37:49 -08002196 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 info->bond_mode = bond->params.mode;
2199 info->miimon = bond->params.miimon;
2200
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002201 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 info->num_slaves = bond->slave_cnt;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002203 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 return 0;
2206}
2207
2208static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2209{
Wang Chen454d7c92008-11-12 23:37:49 -08002210 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 struct slave *slave;
2212 int i, found = 0;
2213
2214 if (info->slave_id < 0) {
2215 return -ENODEV;
2216 }
2217
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002218 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
2220 bond_for_each_slave(bond, slave, i) {
2221 if (i == (int)info->slave_id) {
2222 found = 1;
2223 break;
2224 }
2225 }
2226
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002227 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 if (found) {
2230 strcpy(info->slave_name, slave->dev->name);
2231 info->link = slave->link;
2232 info->state = slave->state;
2233 info->link_failure_count = slave->link_failure_count;
2234 } else {
2235 return -ENODEV;
2236 }
2237
2238 return 0;
2239}
2240
2241/*-------------------------------- Monitoring -------------------------------*/
2242
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002243
2244static int bond_miimon_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002246 struct slave *slave;
2247 int i, link_state, commit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
2249 bond_for_each_slave(bond, slave, i) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002250 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002252 link_state = bond_check_dev_link(bond, slave->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 switch (slave->link) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002255 case BOND_LINK_UP:
2256 if (link_state)
2257 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002259 slave->link = BOND_LINK_FAIL;
2260 slave->delay = bond->params.downdelay;
2261 if (slave->delay) {
2262 printk(KERN_INFO DRV_NAME
2263 ": %s: link status down for %s"
2264 "interface %s, disabling it in %d ms.\n",
2265 bond->dev->name,
2266 (bond->params.mode ==
2267 BOND_MODE_ACTIVEBACKUP) ?
2268 ((slave->state == BOND_STATE_ACTIVE) ?
2269 "active " : "backup ") : "",
2270 slave->dev->name,
2271 bond->params.downdelay * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002273 /*FALLTHRU*/
2274 case BOND_LINK_FAIL:
2275 if (link_state) {
2276 /*
2277 * recovered before downdelay expired
2278 */
2279 slave->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 slave->jiffies = jiffies;
2281 printk(KERN_INFO DRV_NAME
2282 ": %s: link status up again after %d "
2283 "ms for interface %s.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002284 bond->dev->name,
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002285 (bond->params.downdelay - slave->delay) *
2286 bond->params.miimon,
2287 slave->dev->name);
2288 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002290
2291 if (slave->delay <= 0) {
2292 slave->new_link = BOND_LINK_DOWN;
2293 commit++;
2294 continue;
2295 }
2296
2297 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002300 case BOND_LINK_DOWN:
2301 if (!link_state)
2302 continue;
2303
2304 slave->link = BOND_LINK_BACK;
2305 slave->delay = bond->params.updelay;
2306
2307 if (slave->delay) {
2308 printk(KERN_INFO DRV_NAME
2309 ": %s: link status up for "
2310 "interface %s, enabling it in %d ms.\n",
2311 bond->dev->name, slave->dev->name,
2312 bond->params.updelay *
2313 bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002315 /*FALLTHRU*/
2316 case BOND_LINK_BACK:
2317 if (!link_state) {
2318 slave->link = BOND_LINK_DOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 printk(KERN_INFO DRV_NAME
2320 ": %s: link status down again after %d "
2321 "ms for interface %s.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002322 bond->dev->name,
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002323 (bond->params.updelay - slave->delay) *
2324 bond->params.miimon,
2325 slave->dev->name);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002326
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002327 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002329
2330 if (slave->delay <= 0) {
2331 slave->new_link = BOND_LINK_UP;
2332 commit++;
2333 continue;
2334 }
2335
2336 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 break;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002338 }
2339 }
2340
2341 return commit;
2342}
2343
2344static void bond_miimon_commit(struct bonding *bond)
2345{
2346 struct slave *slave;
2347 int i;
2348
2349 bond_for_each_slave(bond, slave, i) {
2350 switch (slave->new_link) {
2351 case BOND_LINK_NOCHANGE:
2352 continue;
2353
2354 case BOND_LINK_UP:
2355 slave->link = BOND_LINK_UP;
2356 slave->jiffies = jiffies;
2357
2358 if (bond->params.mode == BOND_MODE_8023AD) {
2359 /* prevent it from being the active one */
2360 slave->state = BOND_STATE_BACKUP;
2361 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2362 /* make it immediately active */
2363 slave->state = BOND_STATE_ACTIVE;
2364 } else if (slave != bond->primary_slave) {
2365 /* prevent it from being the active one */
2366 slave->state = BOND_STATE_BACKUP;
2367 }
2368
2369 printk(KERN_INFO DRV_NAME
2370 ": %s: link status definitely "
2371 "up for interface %s.\n",
2372 bond->dev->name, slave->dev->name);
2373
2374 /* notify ad that the link status has changed */
2375 if (bond->params.mode == BOND_MODE_8023AD)
2376 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2377
Holger Eitzenberger58402052008-12-09 23:07:13 -08002378 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002379 bond_alb_handle_link_change(bond, slave,
2380 BOND_LINK_UP);
2381
2382 if (!bond->curr_active_slave ||
2383 (slave == bond->primary_slave))
2384 goto do_failover;
2385
2386 continue;
2387
2388 case BOND_LINK_DOWN:
Jay Vosburghfba4acd2008-10-30 17:41:14 -07002389 if (slave->link_failure_count < UINT_MAX)
2390 slave->link_failure_count++;
2391
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002392 slave->link = BOND_LINK_DOWN;
2393
2394 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2395 bond->params.mode == BOND_MODE_8023AD)
2396 bond_set_slave_inactive_flags(slave);
2397
2398 printk(KERN_INFO DRV_NAME
2399 ": %s: link status definitely down for "
2400 "interface %s, disabling it\n",
2401 bond->dev->name, slave->dev->name);
2402
2403 if (bond->params.mode == BOND_MODE_8023AD)
2404 bond_3ad_handle_link_change(slave,
2405 BOND_LINK_DOWN);
2406
2407 if (bond->params.mode == BOND_MODE_TLB ||
2408 bond->params.mode == BOND_MODE_ALB)
2409 bond_alb_handle_link_change(bond, slave,
2410 BOND_LINK_DOWN);
2411
2412 if (slave == bond->curr_active_slave)
2413 goto do_failover;
2414
2415 continue;
2416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 default:
Mitch Williams4e0952c2005-11-09 10:34:57 -08002418 printk(KERN_ERR DRV_NAME
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002419 ": %s: invalid new link %d on slave %s\n",
2420 bond->dev->name, slave->new_link,
2421 slave->dev->name);
2422 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002424 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 }
2426
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002427do_failover:
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002428 ASSERT_RTNL();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002429 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 bond_select_active_slave(bond);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002431 write_unlock_bh(&bond->curr_slave_lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002432 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002433
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002434 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
2436
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002437/*
2438 * bond_mii_monitor
2439 *
2440 * Really a wrapper that splits the mii monitor into two phases: an
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002441 * inspection, then (if inspection indicates something needs to be done)
2442 * an acquisition of appropriate locks followed by a commit phase to
2443 * implement whatever link state changes are indicated.
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002444 */
2445void bond_mii_monitor(struct work_struct *work)
2446{
2447 struct bonding *bond = container_of(work, struct bonding,
2448 mii_work.work);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002449
2450 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002451 if (bond->kill_timers)
2452 goto out;
2453
2454 if (bond->slave_cnt == 0)
2455 goto re_arm;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002456
2457 if (bond->send_grat_arp) {
2458 read_lock(&bond->curr_slave_lock);
2459 bond_send_gratuitous_arp(bond);
2460 read_unlock(&bond->curr_slave_lock);
2461 }
2462
Brian Haley305d5522008-11-04 17:51:14 -08002463 if (bond->send_unsol_na) {
2464 read_lock(&bond->curr_slave_lock);
2465 bond_send_unsolicited_na(bond);
2466 read_unlock(&bond->curr_slave_lock);
2467 }
2468
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002469 if (bond_miimon_inspect(bond)) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002470 read_unlock(&bond->lock);
2471 rtnl_lock();
2472 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002473
2474 bond_miimon_commit(bond);
2475
Jay Vosburgh56556622008-01-17 16:25:03 -08002476 read_unlock(&bond->lock);
2477 rtnl_unlock(); /* might sleep, hold no other locks */
2478 read_lock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002479 }
2480
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002481re_arm:
2482 if (bond->params.miimon)
2483 queue_delayed_work(bond->wq, &bond->mii_work,
2484 msecs_to_jiffies(bond->params.miimon));
2485out:
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002486 read_unlock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002487}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002488
Al Virod3bb52b2007-08-22 20:06:58 -04002489static __be32 bond_glean_dev_ip(struct net_device *dev)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002490{
2491 struct in_device *idev;
2492 struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07002493 __be32 addr = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002494
2495 if (!dev)
2496 return 0;
2497
2498 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002499 idev = __in_dev_get_rcu(dev);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002500 if (!idev)
2501 goto out;
2502
2503 ifa = idev->ifa_list;
2504 if (!ifa)
2505 goto out;
2506
2507 addr = ifa->ifa_local;
2508out:
2509 rcu_read_unlock();
2510 return addr;
2511}
2512
Al Virod3bb52b2007-08-22 20:06:58 -04002513static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002514{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002515 struct vlan_entry *vlan;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002516
2517 if (ip == bond->master_ip)
2518 return 1;
2519
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002520 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002521 if (ip == vlan->vlan_ip)
2522 return 1;
2523 }
2524
2525 return 0;
2526}
2527
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002528/*
2529 * We go to the (large) trouble of VLAN tagging ARP frames because
2530 * switches in VLAN mode (especially if ports are configured as
2531 * "native" to a VLAN) might not pass non-tagged frames.
2532 */
Al Virod3bb52b2007-08-22 20:06:58 -04002533static 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 -04002534{
2535 struct sk_buff *skb;
2536
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002537 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002538 slave_dev->name, dest_ip, src_ip, vlan_id);
2539
2540 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2541 NULL, slave_dev->dev_addr, NULL);
2542
2543 if (!skb) {
2544 printk(KERN_ERR DRV_NAME ": ARP packet allocation failed\n");
2545 return;
2546 }
2547 if (vlan_id) {
2548 skb = vlan_put_tag(skb, vlan_id);
2549 if (!skb) {
2550 printk(KERN_ERR DRV_NAME ": failed to insert VLAN tag\n");
2551 return;
2552 }
2553 }
2554 arp_xmit(skb);
2555}
2556
2557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2559{
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002560 int i, vlan_id, rv;
Al Virod3bb52b2007-08-22 20:06:58 -04002561 __be32 *targets = bond->params.arp_targets;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002562 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002563 struct net_device *vlan_dev;
2564 struct flowi fl;
2565 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Mitch Williams6b780562005-11-09 10:36:19 -08002567 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2568 if (!targets[i])
2569 continue;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002570 pr_debug("basa: target %x\n", targets[i]);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002571 if (list_empty(&bond->vlan_list)) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002572 pr_debug("basa: empty vlan: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002573 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2574 bond->master_ip, 0);
2575 continue;
2576 }
2577
2578 /*
2579 * If VLANs are configured, we do a route lookup to
2580 * determine which VLAN interface would be used, so we
2581 * can tag the ARP with the proper VLAN tag.
2582 */
2583 memset(&fl, 0, sizeof(fl));
2584 fl.fl4_dst = targets[i];
2585 fl.fl4_tos = RTO_ONLINK;
2586
Denis V. Lunevf2063512008-01-22 22:07:34 -08002587 rv = ip_route_output_key(&init_net, &rt, &fl);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002588 if (rv) {
2589 if (net_ratelimit()) {
2590 printk(KERN_WARNING DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -07002591 ": %s: no route to arp_ip_target %pI4\n",
2592 bond->dev->name, &fl.fl4_dst);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002593 }
2594 continue;
2595 }
2596
2597 /*
2598 * This target is not on a VLAN
2599 */
2600 if (rt->u.dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002601 ip_rt_put(rt);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002602 pr_debug("basa: rtdev == bond->dev: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002603 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2604 bond->master_ip, 0);
2605 continue;
2606 }
2607
2608 vlan_id = 0;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002609 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002610 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002611 if (vlan_dev == rt->u.dst.dev) {
2612 vlan_id = vlan->vlan_id;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002613 pr_debug("basa: vlan match on %s %d\n",
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002614 vlan_dev->name, vlan_id);
2615 break;
2616 }
2617 }
2618
2619 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002620 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002621 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2622 vlan->vlan_ip, vlan_id);
2623 continue;
2624 }
2625
2626 if (net_ratelimit()) {
2627 printk(KERN_WARNING DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -07002628 ": %s: no path to arp_ip_target %pI4 via rt.dev %s\n",
2629 bond->dev->name, &fl.fl4_dst,
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002630 rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2631 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002632 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002633 }
2634}
2635
2636/*
2637 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2638 * for each VLAN above us.
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002639 *
2640 * Caller must hold curr_slave_lock for read or better
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002641 */
2642static void bond_send_gratuitous_arp(struct bonding *bond)
2643{
2644 struct slave *slave = bond->curr_active_slave;
2645 struct vlan_entry *vlan;
2646 struct net_device *vlan_dev;
2647
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002648 pr_debug("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002649 slave ? slave->dev->name : "NULL");
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002650
2651 if (!slave || !bond->send_grat_arp ||
2652 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002653 return;
2654
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002655 bond->send_grat_arp--;
2656
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002657 if (bond->master_ip) {
2658 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
Moni Shoua1053f622007-10-09 19:43:42 -07002659 bond->master_ip, 0);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002660 }
2661
2662 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002663 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002664 if (vlan->vlan_ip) {
2665 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2666 vlan->vlan_ip, vlan->vlan_id);
2667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 }
2669}
2670
Al Virod3bb52b2007-08-22 20:06:58 -04002671static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002672{
2673 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002674 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002675
2676 targets = bond->params.arp_targets;
2677 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002678 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
Harvey Harrison63779432008-10-31 00:56:00 -07002679 &sip, &tip, i, &targets[i], bond_has_this_ip(bond, tip));
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002680 if (sip == targets[i]) {
2681 if (bond_has_this_ip(bond, tip))
2682 slave->last_arp_rx = jiffies;
2683 return;
2684 }
2685 }
2686}
2687
2688static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2689{
2690 struct arphdr *arp;
2691 struct slave *slave;
2692 struct bonding *bond;
2693 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002694 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002695
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002696 if (dev_net(dev) != &init_net)
Eric W. Biedermane730c152007-09-17 11:53:39 -07002697 goto out;
2698
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002699 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2700 goto out;
2701
Wang Chen454d7c92008-11-12 23:37:49 -08002702 bond = netdev_priv(dev);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002703 read_lock(&bond->lock);
2704
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002705 pr_debug("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002706 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2707 orig_dev ? orig_dev->name : "NULL");
2708
2709 slave = bond_get_slave_by_dev(bond, orig_dev);
2710 if (!slave || !slave_do_arp_validate(bond, slave))
2711 goto out_unlock;
2712
Pavel Emelyanov988b7052008-03-03 12:20:57 -08002713 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002714 goto out_unlock;
2715
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002716 arp = arp_hdr(skb);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002717 if (arp->ar_hln != dev->addr_len ||
2718 skb->pkt_type == PACKET_OTHERHOST ||
2719 skb->pkt_type == PACKET_LOOPBACK ||
2720 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2721 arp->ar_pro != htons(ETH_P_IP) ||
2722 arp->ar_pln != 4)
2723 goto out_unlock;
2724
2725 arp_ptr = (unsigned char *)(arp + 1);
2726 arp_ptr += dev->addr_len;
2727 memcpy(&sip, arp_ptr, 4);
2728 arp_ptr += 4 + dev->addr_len;
2729 memcpy(&tip, arp_ptr, 4);
2730
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002731 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
Harvey Harrison63779432008-10-31 00:56:00 -07002732 bond->dev->name, slave->dev->name, slave->state,
2733 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2734 &sip, &tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002735
2736 /*
2737 * Backup slaves won't see the ARP reply, but do come through
2738 * here for each ARP probe (so we swap the sip/tip to validate
2739 * the probe). In a "redundant switch, common router" type of
2740 * configuration, the ARP probe will (hopefully) travel from
2741 * the active, through one switch, the router, then the other
2742 * switch before reaching the backup.
2743 */
2744 if (slave->state == BOND_STATE_ACTIVE)
2745 bond_validate_arp(bond, slave, sip, tip);
2746 else
2747 bond_validate_arp(bond, slave, tip, sip);
2748
2749out_unlock:
2750 read_unlock(&bond->lock);
2751out:
2752 dev_kfree_skb(skb);
2753 return NET_RX_SUCCESS;
2754}
2755
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756/*
2757 * this function is called regularly to monitor each slave's link
2758 * ensuring that traffic is being sent and received when arp monitoring
2759 * is used in load-balancing mode. if the adapter has been dormant, then an
2760 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2761 * arp monitoring in active backup mode.
2762 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002763void bond_loadbalance_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002765 struct bonding *bond = container_of(work, struct bonding,
2766 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 struct slave *slave, *oldcurrent;
2768 int do_failover = 0;
2769 int delta_in_ticks;
2770 int i;
2771
2772 read_lock(&bond->lock);
2773
Jay Vosburgh5ce0da82008-05-17 21:10:07 -07002774 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
2776 if (bond->kill_timers) {
2777 goto out;
2778 }
2779
2780 if (bond->slave_cnt == 0) {
2781 goto re_arm;
2782 }
2783
2784 read_lock(&bond->curr_slave_lock);
2785 oldcurrent = bond->curr_active_slave;
2786 read_unlock(&bond->curr_slave_lock);
2787
2788 /* see if any of the previous devices are up now (i.e. they have
2789 * xmt and rcv traffic). the curr_active_slave does not come into
2790 * the picture unless it is null. also, slave->jiffies is not needed
2791 * here because we send an arp on each slave and give a slave as
2792 * long as it needs to get the tx/rx within the delta.
2793 * TODO: what about up/down delay in arp mode? it wasn't here before
2794 * so it can wait
2795 */
2796 bond_for_each_slave(bond, slave, i) {
2797 if (slave->link != BOND_LINK_UP) {
David Sterbab63bb732007-12-06 23:40:33 -08002798 if (time_before_eq(jiffies, slave->dev->trans_start + delta_in_ticks) &&
2799 time_before_eq(jiffies, slave->dev->last_rx + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
2801 slave->link = BOND_LINK_UP;
2802 slave->state = BOND_STATE_ACTIVE;
2803
2804 /* primary_slave has no meaning in round-robin
2805 * mode. the window of a slave being up and
2806 * curr_active_slave being null after enslaving
2807 * is closed.
2808 */
2809 if (!oldcurrent) {
2810 printk(KERN_INFO DRV_NAME
2811 ": %s: link status definitely "
2812 "up for interface %s, ",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002813 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 slave->dev->name);
2815 do_failover = 1;
2816 } else {
2817 printk(KERN_INFO DRV_NAME
2818 ": %s: interface %s is now up\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002819 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 slave->dev->name);
2821 }
2822 }
2823 } else {
2824 /* slave->link == BOND_LINK_UP */
2825
2826 /* not all switches will respond to an arp request
2827 * when the source ip is 0, so don't take the link down
2828 * if we don't know our ip yet
2829 */
David Sterbab63bb732007-12-06 23:40:33 -08002830 if (time_after_eq(jiffies, slave->dev->trans_start + 2*delta_in_ticks) ||
Jay Vosburgh4b8a9232008-05-17 21:10:08 -07002831 (time_after_eq(jiffies, slave->dev->last_rx + 2*delta_in_ticks))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833 slave->link = BOND_LINK_DOWN;
2834 slave->state = BOND_STATE_BACKUP;
2835
2836 if (slave->link_failure_count < UINT_MAX) {
2837 slave->link_failure_count++;
2838 }
2839
2840 printk(KERN_INFO DRV_NAME
2841 ": %s: interface %s is now down.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002842 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 slave->dev->name);
2844
2845 if (slave == oldcurrent) {
2846 do_failover = 1;
2847 }
2848 }
2849 }
2850
2851 /* note: if switch is in round-robin mode, all links
2852 * must tx arp to ensure all links rx an arp - otherwise
2853 * links may oscillate or not come up at all; if switch is
2854 * in something like xor mode, there is nothing we can
2855 * do - all replies will be rx'ed on same link causing slaves
2856 * to be unstable during low/no traffic periods
2857 */
2858 if (IS_UP(slave->dev)) {
2859 bond_arp_send_all(bond, slave);
2860 }
2861 }
2862
2863 if (do_failover) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002864 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
2866 bond_select_active_slave(bond);
2867
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002868 write_unlock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 }
2870
2871re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002872 if (bond->params.arp_interval)
2873 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874out:
2875 read_unlock(&bond->lock);
2876}
2877
2878/*
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002879 * Called to inspect slaves for active-backup mode ARP monitor link state
2880 * changes. Sets new_link in slaves to specify what action should take
2881 * place for the slave. Returns 0 if no changes are found, >0 if changes
2882 * to link states must be committed.
2883 *
2884 * Called with bond->lock held for read.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002886static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 struct slave *slave;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002889 int i, commit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 bond_for_each_slave(bond, slave, i) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002892 slave->new_link = BOND_LINK_NOCHANGE;
2893
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 if (slave->link != BOND_LINK_UP) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002895 if (time_before_eq(jiffies, slave_last_rx(bond, slave) +
2896 delta_in_ticks)) {
2897 slave->new_link = BOND_LINK_UP;
2898 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002901 continue;
2902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002904 /*
2905 * Give slaves 2*delta after being enslaved or made
2906 * active. This avoids bouncing, as the last receive
2907 * times need a full ARP monitor cycle to be updated.
2908 */
2909 if (!time_after_eq(jiffies, slave->jiffies +
2910 2 * delta_in_ticks))
2911 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002913 /*
2914 * Backup slave is down if:
2915 * - No current_arp_slave AND
2916 * - more than 3*delta since last receive AND
2917 * - the bond has an IP address
2918 *
2919 * Note: a non-null current_arp_slave indicates
2920 * the curr_active_slave went down and we are
2921 * searching for a new one; under this condition
2922 * we only take the curr_active_slave down - this
2923 * gives each slave a chance to tx/rx traffic
2924 * before being taken out
2925 */
2926 if (slave->state == BOND_STATE_BACKUP &&
2927 !bond->current_arp_slave &&
2928 time_after(jiffies, slave_last_rx(bond, slave) +
2929 3 * delta_in_ticks)) {
2930 slave->new_link = BOND_LINK_DOWN;
2931 commit++;
2932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002934 /*
2935 * Active slave is down if:
2936 * - more than 2*delta since transmitting OR
2937 * - (more than 2*delta since receive AND
2938 * the bond has an IP address)
2939 */
2940 if ((slave->state == BOND_STATE_ACTIVE) &&
2941 (time_after_eq(jiffies, slave->dev->trans_start +
2942 2 * delta_in_ticks) ||
2943 (time_after_eq(jiffies, slave_last_rx(bond, slave)
2944 + 2 * delta_in_ticks)))) {
2945 slave->new_link = BOND_LINK_DOWN;
2946 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 }
2948 }
2949
2950 read_lock(&bond->curr_slave_lock);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002951
2952 /*
2953 * Trigger a commit if the primary option setting has changed.
2954 */
2955 if (bond->primary_slave &&
2956 (bond->primary_slave != bond->curr_active_slave) &&
2957 (bond->primary_slave->link == BOND_LINK_UP))
2958 commit++;
2959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 read_unlock(&bond->curr_slave_lock);
2961
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002962 return commit;
2963}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002965/*
2966 * Called to commit link state changes noted by inspection step of
2967 * active-backup mode ARP monitor.
2968 *
2969 * Called with RTNL and bond->lock for read.
2970 */
2971static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
2972{
2973 struct slave *slave;
2974 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002976 bond_for_each_slave(bond, slave, i) {
2977 switch (slave->new_link) {
2978 case BOND_LINK_NOCHANGE:
2979 continue;
2980
2981 case BOND_LINK_UP:
2982 write_lock_bh(&bond->curr_slave_lock);
2983
2984 if (!bond->curr_active_slave &&
2985 time_before_eq(jiffies, slave->dev->trans_start +
2986 delta_in_ticks)) {
2987 slave->link = BOND_LINK_UP;
2988 bond_change_active_slave(bond, slave);
2989 bond->current_arp_slave = NULL;
2990
2991 printk(KERN_INFO DRV_NAME
2992 ": %s: %s is up and now the "
2993 "active interface\n",
2994 bond->dev->name, slave->dev->name);
2995
2996 } else if (bond->curr_active_slave != slave) {
2997 /* this slave has just come up but we
2998 * already have a current slave; this can
2999 * also happen if bond_enslave adds a new
3000 * slave that is up while we are searching
3001 * for a new slave
3002 */
3003 slave->link = BOND_LINK_UP;
3004 bond_set_slave_inactive_flags(slave);
3005 bond->current_arp_slave = NULL;
3006
3007 printk(KERN_INFO DRV_NAME
3008 ": %s: backup interface %s is now up\n",
3009 bond->dev->name, slave->dev->name);
3010 }
3011
3012 write_unlock_bh(&bond->curr_slave_lock);
3013
3014 break;
3015
3016 case BOND_LINK_DOWN:
3017 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003020 slave->link = BOND_LINK_DOWN;
3021
3022 if (slave == bond->curr_active_slave) {
3023 printk(KERN_INFO DRV_NAME
3024 ": %s: link status down for active "
3025 "interface %s, disabling it\n",
3026 bond->dev->name, slave->dev->name);
3027
3028 bond_set_slave_inactive_flags(slave);
3029
3030 write_lock_bh(&bond->curr_slave_lock);
3031
3032 bond_select_active_slave(bond);
3033 if (bond->curr_active_slave)
3034 bond->curr_active_slave->jiffies =
3035 jiffies;
3036
3037 write_unlock_bh(&bond->curr_slave_lock);
3038
3039 bond->current_arp_slave = NULL;
3040
3041 } else if (slave->state == BOND_STATE_BACKUP) {
3042 printk(KERN_INFO DRV_NAME
3043 ": %s: backup interface %s is now down\n",
3044 bond->dev->name, slave->dev->name);
3045
3046 bond_set_slave_inactive_flags(slave);
3047 }
3048 break;
3049
3050 default:
3051 printk(KERN_ERR DRV_NAME
3052 ": %s: impossible: new_link %d on slave %s\n",
3053 bond->dev->name, slave->new_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 }
3057
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003058 /*
3059 * No race with changes to primary via sysfs, as we hold rtnl.
3060 */
3061 if (bond->primary_slave &&
3062 (bond->primary_slave != bond->curr_active_slave) &&
3063 (bond->primary_slave->link == BOND_LINK_UP)) {
3064 write_lock_bh(&bond->curr_slave_lock);
3065 bond_change_active_slave(bond, bond->primary_slave);
3066 write_unlock_bh(&bond->curr_slave_lock);
3067 }
3068
3069 bond_set_carrier(bond);
3070}
3071
3072/*
3073 * Send ARP probes for active-backup mode ARP monitor.
3074 *
3075 * Called with bond->lock held for read.
3076 */
3077static void bond_ab_arp_probe(struct bonding *bond)
3078{
3079 struct slave *slave;
3080 int i;
3081
3082 read_lock(&bond->curr_slave_lock);
3083
3084 if (bond->current_arp_slave && bond->curr_active_slave)
3085 printk("PROBE: c_arp %s && cas %s BAD\n",
3086 bond->current_arp_slave->dev->name,
3087 bond->curr_active_slave->dev->name);
3088
3089 if (bond->curr_active_slave) {
3090 bond_arp_send_all(bond, bond->curr_active_slave);
3091 read_unlock(&bond->curr_slave_lock);
3092 return;
3093 }
3094
3095 read_unlock(&bond->curr_slave_lock);
3096
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 /* if we don't have a curr_active_slave, search for the next available
3098 * backup slave from the current_arp_slave and make it the candidate
3099 * for becoming the curr_active_slave
3100 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003101
3102 if (!bond->current_arp_slave) {
3103 bond->current_arp_slave = bond->first_slave;
3104 if (!bond->current_arp_slave)
3105 return;
3106 }
3107
3108 bond_set_slave_inactive_flags(bond->current_arp_slave);
3109
3110 /* search for next candidate */
3111 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3112 if (IS_UP(slave->dev)) {
3113 slave->link = BOND_LINK_BACK;
3114 bond_set_slave_active_flags(slave);
3115 bond_arp_send_all(bond, slave);
3116 slave->jiffies = jiffies;
3117 bond->current_arp_slave = slave;
3118 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 }
3120
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003121 /* if the link state is up at this point, we
3122 * mark it down - this can happen if we have
3123 * simultaneous link failures and
3124 * reselect_active_interface doesn't make this
3125 * one the current slave so it is still marked
3126 * up when it is actually down
3127 */
3128 if (slave->link == BOND_LINK_UP) {
3129 slave->link = BOND_LINK_DOWN;
3130 if (slave->link_failure_count < UINT_MAX)
3131 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003133 bond_set_slave_inactive_flags(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003135 printk(KERN_INFO DRV_NAME
3136 ": %s: backup interface %s is now down.\n",
3137 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 }
3139 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003140}
3141
3142void bond_activebackup_arp_mon(struct work_struct *work)
3143{
3144 struct bonding *bond = container_of(work, struct bonding,
3145 arp_work.work);
3146 int delta_in_ticks;
3147
3148 read_lock(&bond->lock);
3149
3150 if (bond->kill_timers)
3151 goto out;
3152
3153 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3154
3155 if (bond->slave_cnt == 0)
3156 goto re_arm;
3157
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003158 if (bond->send_grat_arp) {
3159 read_lock(&bond->curr_slave_lock);
3160 bond_send_gratuitous_arp(bond);
3161 read_unlock(&bond->curr_slave_lock);
3162 }
3163
Brian Haley305d5522008-11-04 17:51:14 -08003164 if (bond->send_unsol_na) {
3165 read_lock(&bond->curr_slave_lock);
3166 bond_send_unsolicited_na(bond);
3167 read_unlock(&bond->curr_slave_lock);
3168 }
3169
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003170 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3171 read_unlock(&bond->lock);
3172 rtnl_lock();
3173 read_lock(&bond->lock);
3174
3175 bond_ab_arp_commit(bond, delta_in_ticks);
3176
3177 read_unlock(&bond->lock);
3178 rtnl_unlock();
3179 read_lock(&bond->lock);
3180 }
3181
3182 bond_ab_arp_probe(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
3184re_arm:
3185 if (bond->params.arp_interval) {
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003186 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 }
3188out:
3189 read_unlock(&bond->lock);
3190}
3191
3192/*------------------------------ proc/seq_file-------------------------------*/
3193
3194#ifdef CONFIG_PROC_FS
3195
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
3197{
3198 struct bonding *bond = seq->private;
3199 loff_t off = 0;
3200 struct slave *slave;
3201 int i;
3202
3203 /* make sure the bond won't be taken away */
3204 read_lock(&dev_base_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003205 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
3207 if (*pos == 0) {
3208 return SEQ_START_TOKEN;
3209 }
3210
3211 bond_for_each_slave(bond, slave, i) {
3212 if (++off == *pos) {
3213 return slave;
3214 }
3215 }
3216
3217 return NULL;
3218}
3219
3220static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3221{
3222 struct bonding *bond = seq->private;
3223 struct slave *slave = v;
3224
3225 ++*pos;
3226 if (v == SEQ_START_TOKEN) {
3227 return bond->first_slave;
3228 }
3229
3230 slave = slave->next;
3231
3232 return (slave == bond->first_slave) ? NULL : slave;
3233}
3234
3235static void bond_info_seq_stop(struct seq_file *seq, void *v)
3236{
3237 struct bonding *bond = seq->private;
3238
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003239 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 read_unlock(&dev_base_lock);
3241}
3242
3243static void bond_info_show_master(struct seq_file *seq)
3244{
3245 struct bonding *bond = seq->private;
3246 struct slave *curr;
Mitch Williams4756b022005-11-09 10:36:25 -08003247 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
3249 read_lock(&bond->curr_slave_lock);
3250 curr = bond->curr_active_slave;
3251 read_unlock(&bond->curr_slave_lock);
3252
Jay Vosburghdd957c52007-10-09 19:57:24 -07003253 seq_printf(seq, "Bonding Mode: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 bond_mode_name(bond->params.mode));
3255
Jay Vosburghdd957c52007-10-09 19:57:24 -07003256 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
3257 bond->params.fail_over_mac)
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07003258 seq_printf(seq, " (fail_over_mac %s)",
3259 fail_over_mac_tbl[bond->params.fail_over_mac].modename);
Jay Vosburghdd957c52007-10-09 19:57:24 -07003260
3261 seq_printf(seq, "\n");
3262
Mitch Williamsc61b75a2005-11-09 10:35:13 -08003263 if (bond->params.mode == BOND_MODE_XOR ||
3264 bond->params.mode == BOND_MODE_8023AD) {
3265 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3266 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3267 bond->params.xmit_policy);
3268 }
3269
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 if (USES_PRIMARY(bond->params.mode)) {
3271 seq_printf(seq, "Primary Slave: %s\n",
Mitch Williams0f418b22005-11-09 10:35:21 -08003272 (bond->primary_slave) ?
3273 bond->primary_slave->dev->name : "None");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
3275 seq_printf(seq, "Currently Active Slave: %s\n",
3276 (curr) ? curr->dev->name : "None");
3277 }
3278
Jay Vosburghff59c452006-03-27 13:27:43 -08003279 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3280 "up" : "down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3282 seq_printf(seq, "Up Delay (ms): %d\n",
3283 bond->params.updelay * bond->params.miimon);
3284 seq_printf(seq, "Down Delay (ms): %d\n",
3285 bond->params.downdelay * bond->params.miimon);
3286
Mitch Williams4756b022005-11-09 10:36:25 -08003287
3288 /* ARP information */
3289 if(bond->params.arp_interval > 0) {
3290 int printed=0;
3291 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3292 bond->params.arp_interval);
3293
3294 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3295
3296 for(i = 0; (i < BOND_MAX_ARP_TARGETS) ;i++) {
3297 if (!bond->params.arp_targets[i])
3298 continue;
3299 if (printed)
3300 seq_printf(seq, ",");
Harvey Harrison8cf14e32008-10-29 22:43:33 -07003301 seq_printf(seq, " %pI4", &bond->params.arp_targets[i]);
Mitch Williams4756b022005-11-09 10:36:25 -08003302 printed = 1;
3303 }
3304 seq_printf(seq, "\n");
3305 }
3306
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 if (bond->params.mode == BOND_MODE_8023AD) {
3308 struct ad_info ad_info;
3309
3310 seq_puts(seq, "\n802.3ad info\n");
3311 seq_printf(seq, "LACP rate: %s\n",
3312 (bond->params.lacp_fast) ? "fast" : "slow");
Jay Vosburghfd989c82008-11-04 17:51:16 -08003313 seq_printf(seq, "Aggregator selection policy (ad_select): %s\n",
3314 ad_select_tbl[bond->params.ad_select].modename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
3316 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3317 seq_printf(seq, "bond %s has no active aggregator\n",
3318 bond->dev->name);
3319 } else {
3320 seq_printf(seq, "Active Aggregator Info:\n");
3321
3322 seq_printf(seq, "\tAggregator ID: %d\n",
3323 ad_info.aggregator_id);
3324 seq_printf(seq, "\tNumber of ports: %d\n",
3325 ad_info.ports);
3326 seq_printf(seq, "\tActor Key: %d\n",
3327 ad_info.actor_key);
3328 seq_printf(seq, "\tPartner Key: %d\n",
3329 ad_info.partner_key);
Johannes Berge1749612008-10-27 15:59:26 -07003330 seq_printf(seq, "\tPartner Mac Address: %pM\n",
3331 ad_info.partner_system);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 }
3333 }
3334}
3335
3336static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
3337{
3338 struct bonding *bond = seq->private;
3339
3340 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3341 seq_printf(seq, "MII Status: %s\n",
3342 (slave->link == BOND_LINK_UP) ? "up" : "down");
Kenzo Iwami655096452006-09-22 21:53:08 -07003343 seq_printf(seq, "Link Failure Count: %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 slave->link_failure_count);
3345
Johannes Berge1749612008-10-27 15:59:26 -07003346 seq_printf(seq, "Permanent HW addr: %pM\n", slave->perm_hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
3348 if (bond->params.mode == BOND_MODE_8023AD) {
3349 const struct aggregator *agg
3350 = SLAVE_AD_INFO(slave).port.aggregator;
3351
3352 if (agg) {
3353 seq_printf(seq, "Aggregator ID: %d\n",
3354 agg->aggregator_identifier);
3355 } else {
3356 seq_puts(seq, "Aggregator ID: N/A\n");
3357 }
3358 }
3359}
3360
3361static int bond_info_seq_show(struct seq_file *seq, void *v)
3362{
3363 if (v == SEQ_START_TOKEN) {
3364 seq_printf(seq, "%s\n", version);
3365 bond_info_show_master(seq);
3366 } else {
3367 bond_info_show_slave(seq, v);
3368 }
3369
3370 return 0;
3371}
3372
3373static struct seq_operations bond_info_seq_ops = {
3374 .start = bond_info_seq_start,
3375 .next = bond_info_seq_next,
3376 .stop = bond_info_seq_stop,
3377 .show = bond_info_seq_show,
3378};
3379
3380static int bond_info_open(struct inode *inode, struct file *file)
3381{
3382 struct seq_file *seq;
3383 struct proc_dir_entry *proc;
3384 int res;
3385
3386 res = seq_open(file, &bond_info_seq_ops);
3387 if (!res) {
3388 /* recover the pointer buried in proc_dir_entry data */
3389 seq = file->private_data;
3390 proc = PDE(inode);
3391 seq->private = proc->data;
3392 }
3393
3394 return res;
3395}
3396
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08003397static const struct file_operations bond_info_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 .owner = THIS_MODULE,
3399 .open = bond_info_open,
3400 .read = seq_read,
3401 .llseek = seq_lseek,
3402 .release = seq_release,
3403};
3404
3405static int bond_create_proc_entry(struct bonding *bond)
3406{
3407 struct net_device *bond_dev = bond->dev;
3408
3409 if (bond_proc_dir) {
Denis V. Luneva95609c2008-04-29 01:02:29 -07003410 bond->proc_entry = proc_create_data(bond_dev->name,
3411 S_IRUGO, bond_proc_dir,
3412 &bond_info_fops, bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 if (bond->proc_entry == NULL) {
3414 printk(KERN_WARNING DRV_NAME
3415 ": Warning: Cannot create /proc/net/%s/%s\n",
3416 DRV_NAME, bond_dev->name);
3417 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
3419 }
3420 }
3421
3422 return 0;
3423}
3424
3425static void bond_remove_proc_entry(struct bonding *bond)
3426{
3427 if (bond_proc_dir && bond->proc_entry) {
3428 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3429 memset(bond->proc_file_name, 0, IFNAMSIZ);
3430 bond->proc_entry = NULL;
3431 }
3432}
3433
3434/* Create the bonding directory under /proc/net, if doesn't exist yet.
3435 * Caller must hold rtnl_lock.
3436 */
3437static void bond_create_proc_dir(void)
3438{
3439 int len = strlen(DRV_NAME);
3440
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003441 for (bond_proc_dir = init_net.proc_net->subdir; bond_proc_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 bond_proc_dir = bond_proc_dir->next) {
3443 if ((bond_proc_dir->namelen == len) &&
3444 !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3445 break;
3446 }
3447 }
3448
3449 if (!bond_proc_dir) {
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003450 bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 if (bond_proc_dir) {
3452 bond_proc_dir->owner = THIS_MODULE;
3453 } else {
3454 printk(KERN_WARNING DRV_NAME
3455 ": Warning: cannot create /proc/net/%s\n",
3456 DRV_NAME);
3457 }
3458 }
3459}
3460
3461/* Destroy the bonding directory under /proc/net, if empty.
3462 * Caller must hold rtnl_lock.
3463 */
3464static void bond_destroy_proc_dir(void)
3465{
3466 struct proc_dir_entry *de;
3467
3468 if (!bond_proc_dir) {
3469 return;
3470 }
3471
3472 /* verify that the /proc dir is empty */
3473 for (de = bond_proc_dir->subdir; de; de = de->next) {
3474 /* ignore . and .. */
3475 if (*(de->name) != '.') {
3476 break;
3477 }
3478 }
3479
3480 if (de) {
3481 if (bond_proc_dir->owner == THIS_MODULE) {
3482 bond_proc_dir->owner = NULL;
3483 }
3484 } else {
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003485 remove_proc_entry(DRV_NAME, init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 bond_proc_dir = NULL;
3487 }
3488}
3489#endif /* CONFIG_PROC_FS */
3490
3491/*-------------------------- netdev event handling --------------------------*/
3492
3493/*
3494 * Change device name
3495 */
3496static int bond_event_changename(struct bonding *bond)
3497{
3498#ifdef CONFIG_PROC_FS
3499 bond_remove_proc_entry(bond);
3500 bond_create_proc_entry(bond);
3501#endif
Mitch Williamsb76cdba2005-11-09 10:36:41 -08003502 down_write(&(bonding_rwsem));
3503 bond_destroy_sysfs_entry(bond);
3504 bond_create_sysfs_entry(bond);
3505 up_write(&(bonding_rwsem));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 return NOTIFY_DONE;
3507}
3508
3509static int bond_master_netdev_event(unsigned long event, struct net_device *bond_dev)
3510{
Wang Chen454d7c92008-11-12 23:37:49 -08003511 struct bonding *event_bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
3513 switch (event) {
3514 case NETDEV_CHANGENAME:
3515 return bond_event_changename(event_bond);
3516 case NETDEV_UNREGISTER:
Jay Vosburgh3b96c852008-01-17 16:25:00 -08003517 bond_release_all(event_bond->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 break;
3519 default:
3520 break;
3521 }
3522
3523 return NOTIFY_DONE;
3524}
3525
3526static int bond_slave_netdev_event(unsigned long event, struct net_device *slave_dev)
3527{
3528 struct net_device *bond_dev = slave_dev->master;
Wang Chen454d7c92008-11-12 23:37:49 -08003529 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
3531 switch (event) {
3532 case NETDEV_UNREGISTER:
3533 if (bond_dev) {
Jay Vosburgh1284cd32007-10-15 16:44:27 -07003534 if (bond->setup_by_slave)
3535 bond_release_and_destroy(bond_dev, slave_dev);
3536 else
3537 bond_release(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 }
3539 break;
3540 case NETDEV_CHANGE:
3541 /*
3542 * TODO: is this what we get if somebody
3543 * sets up a hierarchical bond, then rmmod's
3544 * one of the slave bonding devices?
3545 */
3546 break;
3547 case NETDEV_DOWN:
3548 /*
3549 * ... Or is it this?
3550 */
3551 break;
3552 case NETDEV_CHANGEMTU:
3553 /*
3554 * TODO: Should slaves be allowed to
3555 * independently alter their MTU? For
3556 * an active-backup bond, slaves need
3557 * not be the same type of device, so
3558 * MTUs may vary. For other modes,
3559 * slaves arguably should have the
3560 * same MTUs. To do this, we'd need to
3561 * take over the slave's change_mtu
3562 * function for the duration of their
3563 * servitude.
3564 */
3565 break;
3566 case NETDEV_CHANGENAME:
3567 /*
3568 * TODO: handle changing the primary's name
3569 */
3570 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003571 case NETDEV_FEAT_CHANGE:
3572 bond_compute_features(bond);
3573 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 default:
3575 break;
3576 }
3577
3578 return NOTIFY_DONE;
3579}
3580
3581/*
3582 * bond_netdev_event: handle netdev notifier chain events.
3583 *
3584 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003585 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 * locks for us to safely manipulate the slave devices (RTNL lock,
3587 * dev_probe_lock).
3588 */
3589static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3590{
3591 struct net_device *event_dev = (struct net_device *)ptr;
3592
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003593 if (dev_net(event_dev) != &init_net)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02003594 return NOTIFY_DONE;
3595
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003596 pr_debug("event_dev: %s, event: %lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 (event_dev ? event_dev->name : "None"),
3598 event);
3599
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003600 if (!(event_dev->priv_flags & IFF_BONDING))
3601 return NOTIFY_DONE;
3602
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 if (event_dev->flags & IFF_MASTER) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003604 pr_debug("IFF_MASTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 return bond_master_netdev_event(event, event_dev);
3606 }
3607
3608 if (event_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003609 pr_debug("IFF_SLAVE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 return bond_slave_netdev_event(event, event_dev);
3611 }
3612
3613 return NOTIFY_DONE;
3614}
3615
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003616/*
3617 * bond_inetaddr_event: handle inetaddr notifier chain events.
3618 *
3619 * We keep track of device IPs primarily to use as source addresses in
3620 * ARP monitor probes (rather than spewing out broadcasts all the time).
3621 *
3622 * We track one IP for the main device (if it has one), plus one per VLAN.
3623 */
3624static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3625{
3626 struct in_ifaddr *ifa = ptr;
3627 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003628 struct bonding *bond;
3629 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003630
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003631 if (dev_net(ifa->ifa_dev->dev) != &init_net)
Denis V. Lunev6133fb12008-02-28 20:46:17 -08003632 return NOTIFY_DONE;
3633
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003634 list_for_each_entry(bond, &bond_dev_list, bond_list) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003635 if (bond->dev == event_dev) {
3636 switch (event) {
3637 case NETDEV_UP:
3638 bond->master_ip = ifa->ifa_local;
3639 return NOTIFY_OK;
3640 case NETDEV_DOWN:
3641 bond->master_ip = bond_glean_dev_ip(bond->dev);
3642 return NOTIFY_OK;
3643 default:
3644 return NOTIFY_DONE;
3645 }
3646 }
3647
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003648 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08003649 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003650 if (vlan_dev == event_dev) {
3651 switch (event) {
3652 case NETDEV_UP:
3653 vlan->vlan_ip = ifa->ifa_local;
3654 return NOTIFY_OK;
3655 case NETDEV_DOWN:
3656 vlan->vlan_ip =
3657 bond_glean_dev_ip(vlan_dev);
3658 return NOTIFY_OK;
3659 default:
3660 return NOTIFY_DONE;
3661 }
3662 }
3663 }
3664 }
3665 return NOTIFY_DONE;
3666}
3667
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668static struct notifier_block bond_netdev_notifier = {
3669 .notifier_call = bond_netdev_event,
3670};
3671
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003672static struct notifier_block bond_inetaddr_notifier = {
3673 .notifier_call = bond_inetaddr_event,
3674};
3675
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676/*-------------------------- Packet type handling ---------------------------*/
3677
3678/* register to receive lacpdus on a bond */
3679static void bond_register_lacpdu(struct bonding *bond)
3680{
3681 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3682
3683 /* initialize packet type */
3684 pk_type->type = PKT_TYPE_LACPDU;
3685 pk_type->dev = bond->dev;
3686 pk_type->func = bond_3ad_lacpdu_recv;
3687
3688 dev_add_pack(pk_type);
3689}
3690
3691/* unregister to receive lacpdus on a bond */
3692static void bond_unregister_lacpdu(struct bonding *bond)
3693{
3694 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3695}
3696
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003697void bond_register_arp(struct bonding *bond)
3698{
3699 struct packet_type *pt = &bond->arp_mon_pt;
3700
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003701 if (pt->type)
3702 return;
3703
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003704 pt->type = htons(ETH_P_ARP);
Jay Vosburghe245cb72007-02-28 17:03:27 -08003705 pt->dev = bond->dev;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003706 pt->func = bond_arp_rcv;
3707 dev_add_pack(pt);
3708}
3709
3710void bond_unregister_arp(struct bonding *bond)
3711{
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003712 struct packet_type *pt = &bond->arp_mon_pt;
3713
3714 dev_remove_pack(pt);
3715 pt->type = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003716}
3717
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003718/*---------------------------- Hashing Policies -----------------------------*/
3719
3720/*
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003721 * Hash for the output device based upon layer 2 and layer 3 data. If
3722 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3723 */
3724static int bond_xmit_hash_policy_l23(struct sk_buff *skb,
3725 struct net_device *bond_dev, int count)
3726{
3727 struct ethhdr *data = (struct ethhdr *)skb->data;
3728 struct iphdr *iph = ip_hdr(skb);
3729
Brian Haleyf14c4e42008-09-02 10:08:08 -04003730 if (skb->protocol == htons(ETH_P_IP)) {
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003731 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
3732 (data->h_dest[5] ^ bond_dev->dev_addr[5])) % count;
3733 }
3734
3735 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3736}
3737
3738/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003739 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003740 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3741 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3742 */
3743static int bond_xmit_hash_policy_l34(struct sk_buff *skb,
3744 struct net_device *bond_dev, int count)
3745{
3746 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003747 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003748 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003749 int layer4_xor = 0;
3750
Brian Haleyf14c4e42008-09-02 10:08:08 -04003751 if (skb->protocol == htons(ETH_P_IP)) {
3752 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003753 (iph->protocol == IPPROTO_TCP ||
3754 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003755 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003756 }
3757 return (layer4_xor ^
3758 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3759
3760 }
3761
3762 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3763}
3764
3765/*
3766 * Hash for the output device based upon layer 2 data
3767 */
3768static int bond_xmit_hash_policy_l2(struct sk_buff *skb,
3769 struct net_device *bond_dev, int count)
3770{
3771 struct ethhdr *data = (struct ethhdr *)skb->data;
3772
3773 return (data->h_dest[5] ^ bond_dev->dev_addr[5]) % count;
3774}
3775
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776/*-------------------------- Device entry points ----------------------------*/
3777
3778static int bond_open(struct net_device *bond_dev)
3779{
Wang Chen454d7c92008-11-12 23:37:49 -08003780 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
3782 bond->kill_timers = 0;
3783
Holger Eitzenberger58402052008-12-09 23:07:13 -08003784 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 /* bond_alb_initialize must be called before the timer
3786 * is started.
3787 */
3788 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3789 /* something went wrong - fail the open operation */
3790 return -1;
3791 }
3792
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003793 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3794 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 }
3796
3797 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003798 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3799 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 }
3801
3802 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003803 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3804 INIT_DELAYED_WORK(&bond->arp_work,
3805 bond_activebackup_arp_mon);
3806 else
3807 INIT_DELAYED_WORK(&bond->arp_work,
3808 bond_loadbalance_arp_mon);
3809
3810 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003811 if (bond->params.arp_validate)
3812 bond_register_arp(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 }
3814
3815 if (bond->params.mode == BOND_MODE_8023AD) {
Adrian Bunka40745f2007-10-24 18:27:43 +02003816 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003817 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 /* register to receive LACPDUs */
3819 bond_register_lacpdu(bond);
Jay Vosburghfd989c82008-11-04 17:51:16 -08003820 bond_3ad_initiate_agg_selection(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 }
3822
3823 return 0;
3824}
3825
3826static int bond_close(struct net_device *bond_dev)
3827{
Wang Chen454d7c92008-11-12 23:37:49 -08003828 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
3830 if (bond->params.mode == BOND_MODE_8023AD) {
3831 /* Unregister the receive of LACPDUs */
3832 bond_unregister_lacpdu(bond);
3833 }
3834
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003835 if (bond->params.arp_validate)
3836 bond_unregister_arp(bond);
3837
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 write_lock_bh(&bond->lock);
3839
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003840 bond->send_grat_arp = 0;
Brian Haley305d5522008-11-04 17:51:14 -08003841 bond->send_unsol_na = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
3843 /* signal timers not to re-arm */
3844 bond->kill_timers = 1;
3845
3846 write_unlock_bh(&bond->lock);
3847
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003849 cancel_delayed_work(&bond->mii_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 }
3851
3852 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003853 cancel_delayed_work(&bond->arp_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 }
3855
3856 switch (bond->params.mode) {
3857 case BOND_MODE_8023AD:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003858 cancel_delayed_work(&bond->ad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 break;
3860 case BOND_MODE_TLB:
3861 case BOND_MODE_ALB:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003862 cancel_delayed_work(&bond->alb_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 break;
3864 default:
3865 break;
3866 }
3867
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868
Holger Eitzenberger58402052008-12-09 23:07:13 -08003869 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 /* Must be called only after all
3871 * slaves have been released
3872 */
3873 bond_alb_deinitialize(bond);
3874 }
3875
3876 return 0;
3877}
3878
3879static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3880{
Wang Chen454d7c92008-11-12 23:37:49 -08003881 struct bonding *bond = netdev_priv(bond_dev);
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08003882 struct net_device_stats *stats = &bond->stats;
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003883 struct net_device_stats local_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 struct slave *slave;
3885 int i;
3886
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003887 memset(&local_stats, 0, sizeof(struct net_device_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
3889 read_lock_bh(&bond->lock);
3890
3891 bond_for_each_slave(bond, slave, i) {
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08003892 const struct net_device_stats *sstats = dev_get_stats(slave->dev);
3893
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003894 local_stats.rx_packets += sstats->rx_packets;
3895 local_stats.rx_bytes += sstats->rx_bytes;
3896 local_stats.rx_errors += sstats->rx_errors;
3897 local_stats.rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003899 local_stats.tx_packets += sstats->tx_packets;
3900 local_stats.tx_bytes += sstats->tx_bytes;
3901 local_stats.tx_errors += sstats->tx_errors;
3902 local_stats.tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003904 local_stats.multicast += sstats->multicast;
3905 local_stats.collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003907 local_stats.rx_length_errors += sstats->rx_length_errors;
3908 local_stats.rx_over_errors += sstats->rx_over_errors;
3909 local_stats.rx_crc_errors += sstats->rx_crc_errors;
3910 local_stats.rx_frame_errors += sstats->rx_frame_errors;
3911 local_stats.rx_fifo_errors += sstats->rx_fifo_errors;
3912 local_stats.rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003914 local_stats.tx_aborted_errors += sstats->tx_aborted_errors;
3915 local_stats.tx_carrier_errors += sstats->tx_carrier_errors;
3916 local_stats.tx_fifo_errors += sstats->tx_fifo_errors;
3917 local_stats.tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3918 local_stats.tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 }
3920
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003921 memcpy(stats, &local_stats, sizeof(struct net_device_stats));
3922
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 read_unlock_bh(&bond->lock);
3924
3925 return stats;
3926}
3927
3928static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3929{
3930 struct net_device *slave_dev = NULL;
3931 struct ifbond k_binfo;
3932 struct ifbond __user *u_binfo = NULL;
3933 struct ifslave k_sinfo;
3934 struct ifslave __user *u_sinfo = NULL;
3935 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 int res = 0;
3937
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003938 pr_debug("bond_ioctl: master=%s, cmd=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 bond_dev->name, cmd);
3940
3941 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 case SIOCGMIIPHY:
3943 mii = if_mii(ifr);
3944 if (!mii) {
3945 return -EINVAL;
3946 }
3947 mii->phy_id = 0;
3948 /* Fall Through */
3949 case SIOCGMIIREG:
3950 /*
3951 * We do this again just in case we were called by SIOCGMIIREG
3952 * instead of SIOCGMIIPHY.
3953 */
3954 mii = if_mii(ifr);
3955 if (!mii) {
3956 return -EINVAL;
3957 }
3958
3959 if (mii->reg_num == 1) {
Wang Chen454d7c92008-11-12 23:37:49 -08003960 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 mii->val_out = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003962 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 read_lock(&bond->curr_slave_lock);
Andy Gospodarek4e140072006-12-04 15:04:54 -08003964 if (netif_carrier_ok(bond->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 mii->val_out = BMSR_LSTATUS;
3966 }
3967 read_unlock(&bond->curr_slave_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003968 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 }
3970
3971 return 0;
3972 case BOND_INFO_QUERY_OLD:
3973 case SIOCBONDINFOQUERY:
3974 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3975
3976 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
3977 return -EFAULT;
3978 }
3979
3980 res = bond_info_query(bond_dev, &k_binfo);
3981 if (res == 0) {
3982 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
3983 return -EFAULT;
3984 }
3985 }
3986
3987 return res;
3988 case BOND_SLAVE_INFO_QUERY_OLD:
3989 case SIOCBONDSLAVEINFOQUERY:
3990 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3991
3992 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
3993 return -EFAULT;
3994 }
3995
3996 res = bond_slave_info_query(bond_dev, &k_sinfo);
3997 if (res == 0) {
3998 if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
3999 return -EFAULT;
4000 }
4001 }
4002
4003 return res;
4004 default:
4005 /* Go on */
4006 break;
4007 }
4008
4009 if (!capable(CAP_NET_ADMIN)) {
4010 return -EPERM;
4011 }
4012
Mitch Williamsb76cdba2005-11-09 10:36:41 -08004013 down_write(&(bonding_rwsem));
Eric W. Biederman881d9662007-09-17 11:56:21 -07004014 slave_dev = dev_get_by_name(&init_net, ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004016 pr_debug("slave_dev=%p: \n", slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017
4018 if (!slave_dev) {
4019 res = -ENODEV;
4020 } else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004021 pr_debug("slave_dev->name=%s: \n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 switch (cmd) {
4023 case BOND_ENSLAVE_OLD:
4024 case SIOCBONDENSLAVE:
4025 res = bond_enslave(bond_dev, slave_dev);
4026 break;
4027 case BOND_RELEASE_OLD:
4028 case SIOCBONDRELEASE:
4029 res = bond_release(bond_dev, slave_dev);
4030 break;
4031 case BOND_SETHWADDR_OLD:
4032 case SIOCBONDSETHWADDR:
4033 res = bond_sethwaddr(bond_dev, slave_dev);
4034 break;
4035 case BOND_CHANGE_ACTIVE_OLD:
4036 case SIOCBONDCHANGEACTIVE:
4037 res = bond_ioctl_change_active(bond_dev, slave_dev);
4038 break;
4039 default:
4040 res = -EOPNOTSUPP;
4041 }
4042
4043 dev_put(slave_dev);
4044 }
4045
Mitch Williamsb76cdba2005-11-09 10:36:41 -08004046 up_write(&(bonding_rwsem));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 return res;
4048}
4049
4050static void bond_set_multicast_list(struct net_device *bond_dev)
4051{
Wang Chen454d7c92008-11-12 23:37:49 -08004052 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 struct dev_mc_list *dmi;
4054
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 /*
4056 * Do promisc before checking multicast_mode
4057 */
4058 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC)) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07004059 /*
4060 * FIXME: Need to handle the error when one of the multi-slaves
4061 * encounters error.
4062 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 bond_set_promiscuity(bond, 1);
4064 }
4065
4066 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC)) {
4067 bond_set_promiscuity(bond, -1);
4068 }
4069
4070 /* set allmulti flag to slaves */
4071 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI)) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07004072 /*
4073 * FIXME: Need to handle the error when one of the multi-slaves
4074 * encounters error.
4075 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 bond_set_allmulti(bond, 1);
4077 }
4078
4079 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI)) {
4080 bond_set_allmulti(bond, -1);
4081 }
4082
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004083 read_lock(&bond->lock);
4084
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 bond->flags = bond_dev->flags;
4086
4087 /* looking for addresses to add to slaves' mc list */
4088 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
4089 if (!bond_mc_list_find_dmi(dmi, bond->mc_list)) {
4090 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
4091 }
4092 }
4093
4094 /* looking for addresses to delete from slaves' list */
4095 for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
4096 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list)) {
4097 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
4098 }
4099 }
4100
4101 /* save master's multicast list */
4102 bond_mc_list_destroy(bond);
4103 bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
4104
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004105 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106}
4107
Stephen Hemminger00829822008-11-20 20:14:53 -08004108static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
4109{
4110 struct bonding *bond = netdev_priv(dev);
4111 struct slave *slave = bond->first_slave;
4112
4113 if (slave) {
4114 const struct net_device_ops *slave_ops
4115 = slave->dev->netdev_ops;
4116 if (slave_ops->ndo_neigh_setup)
4117 return slave_ops->ndo_neigh_setup(dev, parms);
4118 }
4119 return 0;
4120}
4121
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122/*
4123 * Change the MTU of all of a master's slaves to match the master
4124 */
4125static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
4126{
Wang Chen454d7c92008-11-12 23:37:49 -08004127 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 struct slave *slave, *stop_at;
4129 int res = 0;
4130 int i;
4131
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004132 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 (bond_dev ? bond_dev->name : "None"), new_mtu);
4134
4135 /* Can't hold bond->lock with bh disabled here since
4136 * some base drivers panic. On the other hand we can't
4137 * hold bond->lock without bh disabled because we'll
4138 * deadlock. The only solution is to rely on the fact
4139 * that we're under rtnl_lock here, and the slaves
4140 * list won't change. This doesn't solve the problem
4141 * of setting the slave's MTU while it is
4142 * transmitting, but the assumption is that the base
4143 * driver can handle that.
4144 *
4145 * TODO: figure out a way to safely iterate the slaves
4146 * list, but without holding a lock around the actual
4147 * call to the base driver.
4148 */
4149
4150 bond_for_each_slave(bond, slave, i) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004151 pr_debug("s %p s->p %p c_m %p\n", slave,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 slave->prev, slave->dev->change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08004153
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 res = dev_set_mtu(slave->dev, new_mtu);
4155
4156 if (res) {
4157 /* If we failed to set the slave's mtu to the new value
4158 * we must abort the operation even in ACTIVE_BACKUP
4159 * mode, because if we allow the backup slaves to have
4160 * different mtu values than the active slave we'll
4161 * need to change their mtu when doing a failover. That
4162 * means changing their mtu from timer context, which
4163 * is probably not a good idea.
4164 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004165 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 goto unwind;
4167 }
4168 }
4169
4170 bond_dev->mtu = new_mtu;
4171
4172 return 0;
4173
4174unwind:
4175 /* unwind from head to the slave that failed */
4176 stop_at = slave;
4177 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4178 int tmp_res;
4179
4180 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4181 if (tmp_res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004182 pr_debug("unwind err %d dev %s\n", tmp_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 slave->dev->name);
4184 }
4185 }
4186
4187 return res;
4188}
4189
4190/*
4191 * Change HW address
4192 *
4193 * Note that many devices must be down to change the HW address, and
4194 * downing the master releases all slaves. We can make bonds full of
4195 * bonding devices to test this, however.
4196 */
4197static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4198{
Wang Chen454d7c92008-11-12 23:37:49 -08004199 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 struct sockaddr *sa = addr, tmp_sa;
4201 struct slave *slave, *stop_at;
4202 int res = 0;
4203 int i;
4204
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004205 if (bond->params.mode == BOND_MODE_ALB)
4206 return bond_alb_set_mac_address(bond_dev, addr);
4207
4208
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004209 pr_debug("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
Jay Vosburghdd957c52007-10-09 19:57:24 -07004211 /*
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004212 * If fail_over_mac is set to active, do nothing and return
4213 * success. Returning an error causes ifenslave to fail.
Jay Vosburghdd957c52007-10-09 19:57:24 -07004214 */
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004215 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
Jay Vosburghdd957c52007-10-09 19:57:24 -07004216 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07004217
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 if (!is_valid_ether_addr(sa->sa_data)) {
4219 return -EADDRNOTAVAIL;
4220 }
4221
4222 /* Can't hold bond->lock with bh disabled here since
4223 * some base drivers panic. On the other hand we can't
4224 * hold bond->lock without bh disabled because we'll
4225 * deadlock. The only solution is to rely on the fact
4226 * that we're under rtnl_lock here, and the slaves
4227 * list won't change. This doesn't solve the problem
4228 * of setting the slave's hw address while it is
4229 * transmitting, but the assumption is that the base
4230 * driver can handle that.
4231 *
4232 * TODO: figure out a way to safely iterate the slaves
4233 * list, but without holding a lock around the actual
4234 * call to the base driver.
4235 */
4236
4237 bond_for_each_slave(bond, slave, i) {
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004238 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004239 pr_debug("slave %p %s\n", slave, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004241 if (slave_ops->ndo_set_mac_address == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 res = -EOPNOTSUPP;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004243 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 goto unwind;
4245 }
4246
4247 res = dev_set_mac_address(slave->dev, addr);
4248 if (res) {
4249 /* TODO: consider downing the slave
4250 * and retry ?
4251 * User should expect communications
4252 * breakage anyway until ARP finish
4253 * updating, so...
4254 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004255 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 goto unwind;
4257 }
4258 }
4259
4260 /* success */
4261 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4262 return 0;
4263
4264unwind:
4265 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4266 tmp_sa.sa_family = bond_dev->type;
4267
4268 /* unwind from head to the slave that failed */
4269 stop_at = slave;
4270 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4271 int tmp_res;
4272
4273 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4274 if (tmp_res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004275 pr_debug("unwind err %d dev %s\n", tmp_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 slave->dev->name);
4277 }
4278 }
4279
4280 return res;
4281}
4282
4283static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4284{
Wang Chen454d7c92008-11-12 23:37:49 -08004285 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 struct slave *slave, *start_at;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004287 int i, slave_no, res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
4289 read_lock(&bond->lock);
4290
4291 if (!BOND_IS_OK(bond)) {
4292 goto out;
4293 }
4294
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004295 /*
4296 * Concurrent TX may collide on rr_tx_counter; we accept that
4297 * as being rare enough not to justify using an atomic op here
4298 */
4299 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004301 bond_for_each_slave(bond, slave, i) {
4302 slave_no--;
4303 if (slave_no < 0) {
4304 break;
4305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 }
4307
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004308 start_at = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 bond_for_each_slave_from(bond, slave, i, start_at) {
4310 if (IS_UP(slave->dev) &&
4311 (slave->link == BOND_LINK_UP) &&
4312 (slave->state == BOND_STATE_ACTIVE)) {
4313 res = bond_dev_queue_xmit(bond, skb, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 break;
4315 }
4316 }
4317
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318out:
4319 if (res) {
4320 /* no suitable interface, frame not sent */
4321 dev_kfree_skb(skb);
4322 }
4323 read_unlock(&bond->lock);
4324 return 0;
4325}
4326
John W. Linville075897c2005-09-28 17:50:53 -04004327
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328/*
4329 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4330 * the bond has a usable interface.
4331 */
4332static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4333{
Wang Chen454d7c92008-11-12 23:37:49 -08004334 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 int res = 1;
4336
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 read_lock(&bond->lock);
4338 read_lock(&bond->curr_slave_lock);
4339
4340 if (!BOND_IS_OK(bond)) {
4341 goto out;
4342 }
4343
John W. Linville075897c2005-09-28 17:50:53 -04004344 if (!bond->curr_active_slave)
4345 goto out;
4346
John W. Linville075897c2005-09-28 17:50:53 -04004347 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4348
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349out:
4350 if (res) {
4351 /* no suitable interface, frame not sent */
4352 dev_kfree_skb(skb);
4353 }
4354 read_unlock(&bond->curr_slave_lock);
4355 read_unlock(&bond->lock);
4356 return 0;
4357}
4358
4359/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004360 * In bond_xmit_xor() , we determine the output device by using a pre-
4361 * determined xmit_hash_policy(), If the selected device is not enabled,
4362 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 */
4364static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4365{
Wang Chen454d7c92008-11-12 23:37:49 -08004366 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 struct slave *slave, *start_at;
4368 int slave_no;
4369 int i;
4370 int res = 1;
4371
4372 read_lock(&bond->lock);
4373
4374 if (!BOND_IS_OK(bond)) {
4375 goto out;
4376 }
4377
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004378 slave_no = bond->xmit_hash_policy(skb, bond_dev, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
4380 bond_for_each_slave(bond, slave, i) {
4381 slave_no--;
4382 if (slave_no < 0) {
4383 break;
4384 }
4385 }
4386
4387 start_at = slave;
4388
4389 bond_for_each_slave_from(bond, slave, i, start_at) {
4390 if (IS_UP(slave->dev) &&
4391 (slave->link == BOND_LINK_UP) &&
4392 (slave->state == BOND_STATE_ACTIVE)) {
4393 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4394 break;
4395 }
4396 }
4397
4398out:
4399 if (res) {
4400 /* no suitable interface, frame not sent */
4401 dev_kfree_skb(skb);
4402 }
4403 read_unlock(&bond->lock);
4404 return 0;
4405}
4406
4407/*
4408 * in broadcast mode, we send everything to all usable interfaces.
4409 */
4410static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4411{
Wang Chen454d7c92008-11-12 23:37:49 -08004412 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 struct slave *slave, *start_at;
4414 struct net_device *tx_dev = NULL;
4415 int i;
4416 int res = 1;
4417
4418 read_lock(&bond->lock);
4419
4420 if (!BOND_IS_OK(bond)) {
4421 goto out;
4422 }
4423
4424 read_lock(&bond->curr_slave_lock);
4425 start_at = bond->curr_active_slave;
4426 read_unlock(&bond->curr_slave_lock);
4427
4428 if (!start_at) {
4429 goto out;
4430 }
4431
4432 bond_for_each_slave_from(bond, slave, i, start_at) {
4433 if (IS_UP(slave->dev) &&
4434 (slave->link == BOND_LINK_UP) &&
4435 (slave->state == BOND_STATE_ACTIVE)) {
4436 if (tx_dev) {
4437 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4438 if (!skb2) {
4439 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08004440 ": %s: Error: bond_xmit_broadcast(): "
4441 "skb_clone() failed\n",
4442 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 continue;
4444 }
4445
4446 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4447 if (res) {
4448 dev_kfree_skb(skb2);
4449 continue;
4450 }
4451 }
4452 tx_dev = slave->dev;
4453 }
4454 }
4455
4456 if (tx_dev) {
4457 res = bond_dev_queue_xmit(bond, skb, tx_dev);
4458 }
4459
4460out:
4461 if (res) {
4462 /* no suitable interface, frame not sent */
4463 dev_kfree_skb(skb);
4464 }
4465 /* frame sent to all suitable interfaces */
4466 read_unlock(&bond->lock);
4467 return 0;
4468}
4469
4470/*------------------------- Device initialization ---------------------------*/
4471
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004472static void bond_set_xmit_hash_policy(struct bonding *bond)
4473{
4474 switch (bond->params.xmit_policy) {
4475 case BOND_XMIT_POLICY_LAYER23:
4476 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4477 break;
4478 case BOND_XMIT_POLICY_LAYER34:
4479 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4480 break;
4481 case BOND_XMIT_POLICY_LAYER2:
4482 default:
4483 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4484 break;
4485 }
4486}
4487
Stephen Hemminger00829822008-11-20 20:14:53 -08004488static int bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
4489{
4490 const struct bonding *bond = netdev_priv(dev);
4491
4492 switch (bond->params.mode) {
4493 case BOND_MODE_ROUNDROBIN:
4494 return bond_xmit_roundrobin(skb, dev);
4495 case BOND_MODE_ACTIVEBACKUP:
4496 return bond_xmit_activebackup(skb, dev);
4497 case BOND_MODE_XOR:
4498 return bond_xmit_xor(skb, dev);
4499 case BOND_MODE_BROADCAST:
4500 return bond_xmit_broadcast(skb, dev);
4501 case BOND_MODE_8023AD:
4502 return bond_3ad_xmit_xor(skb, dev);
4503 case BOND_MODE_ALB:
4504 case BOND_MODE_TLB:
4505 return bond_alb_xmit(skb, dev);
4506 default:
4507 /* Should never happen, mode already checked */
4508 printk(KERN_ERR DRV_NAME ": %s: Error: Unknown bonding mode %d\n",
4509 dev->name, bond->params.mode);
4510 WARN_ON_ONCE(1);
4511 dev_kfree_skb(skb);
4512 return NETDEV_TX_OK;
4513 }
4514}
4515
4516
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517/*
4518 * set bond mode specific net device operations
4519 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004520void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004522 struct net_device *bond_dev = bond->dev;
4523
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 switch (mode) {
4525 case BOND_MODE_ROUNDROBIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 break;
4527 case BOND_MODE_ACTIVEBACKUP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 break;
4529 case BOND_MODE_XOR:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004530 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 break;
4532 case BOND_MODE_BROADCAST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 break;
4534 case BOND_MODE_8023AD:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004535 bond_set_master_3ad_flags(bond);
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004536 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004539 bond_set_master_alb_flags(bond);
4540 /* FALLTHRU */
4541 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 break;
4543 default:
4544 /* Should never happen, mode already checked */
4545 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08004546 ": %s: Error: Unknown bonding mode %d\n",
4547 bond_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 mode);
4549 break;
4550 }
4551}
4552
Jay Vosburgh217df672005-09-26 16:11:50 -07004553static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4554 struct ethtool_drvinfo *drvinfo)
4555{
4556 strncpy(drvinfo->driver, DRV_NAME, 32);
4557 strncpy(drvinfo->version, DRV_VERSION, 32);
4558 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4559}
4560
Jeff Garzik7282d492006-09-13 14:30:00 -04004561static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004562 .get_drvinfo = bond_ethtool_get_drvinfo,
Stephen Hemmingerfa53eba2008-09-13 21:17:09 -04004563 .get_link = ethtool_op_get_link,
4564 .get_tx_csum = ethtool_op_get_tx_csum,
4565 .get_sg = ethtool_op_get_sg,
4566 .get_tso = ethtool_op_get_tso,
4567 .get_ufo = ethtool_op_get_ufo,
4568 .get_flags = ethtool_op_get_flags,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004569};
4570
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004571static const struct net_device_ops bond_netdev_ops = {
4572 .ndo_open = bond_open,
4573 .ndo_stop = bond_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08004574 .ndo_start_xmit = bond_start_xmit,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004575 .ndo_get_stats = bond_get_stats,
4576 .ndo_do_ioctl = bond_do_ioctl,
4577 .ndo_set_multicast_list = bond_set_multicast_list,
4578 .ndo_change_mtu = bond_change_mtu,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004579 .ndo_set_mac_address = bond_set_mac_address,
Stephen Hemminger00829822008-11-20 20:14:53 -08004580 .ndo_neigh_setup = bond_neigh_setup,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004581 .ndo_vlan_rx_register = bond_vlan_rx_register,
4582 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4583 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
4584};
4585
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586/*
4587 * Does not allocate but creates a /proc entry.
4588 * Allowed to fail.
4589 */
Mitch Williams3c535952005-11-09 10:36:11 -08004590static int bond_init(struct net_device *bond_dev, struct bond_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591{
Wang Chen454d7c92008-11-12 23:37:49 -08004592 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004594 pr_debug("Begin bond_init for %s\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595
4596 /* initialize rwlocks */
4597 rwlock_init(&bond->lock);
4598 rwlock_init(&bond->curr_slave_lock);
4599
4600 bond->params = *params; /* copy params struct */
4601
Jay Vosburgh1b76b312007-10-17 17:37:45 -07004602 bond->wq = create_singlethread_workqueue(bond_dev->name);
4603 if (!bond->wq)
4604 return -ENOMEM;
4605
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 /* Initialize pointers */
4607 bond->first_slave = NULL;
4608 bond->curr_active_slave = NULL;
4609 bond->current_arp_slave = NULL;
4610 bond->primary_slave = NULL;
4611 bond->dev = bond_dev;
Moni Shoua1053f622007-10-09 19:43:42 -07004612 bond->send_grat_arp = 0;
Brian Haley305d5522008-11-04 17:51:14 -08004613 bond->send_unsol_na = 0;
Moni Shouad90a1622007-10-09 19:43:43 -07004614 bond->setup_by_slave = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 INIT_LIST_HEAD(&bond->vlan_list);
4616
4617 /* Initialize the device entry points */
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004618 bond_dev->netdev_ops = &bond_netdev_ops;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004619 bond_dev->ethtool_ops = &bond_ethtool_ops;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004620 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
Jay Vosburgha434e432008-10-30 17:41:15 -07004622 bond_dev->destructor = bond_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
4624 /* Initialize the device options */
4625 bond_dev->tx_queue_len = 0;
4626 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004627 bond_dev->priv_flags |= IFF_BONDING;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -08004628 if (bond->params.arp_interval)
4629 bond_dev->priv_flags |= IFF_MASTER_ARPMON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
4631 /* At first, we block adding VLANs. That's the only way to
4632 * prevent problems that occur when adding VLANs over an
4633 * empty bond. The block will be removed once non-challenged
4634 * slaves are enslaved.
4635 */
4636 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4637
Herbert Xu932ff272006-06-09 12:20:56 -07004638 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 * transmitting */
4640 bond_dev->features |= NETIF_F_LLTX;
4641
4642 /* By default, we declare the bond to be fully
4643 * VLAN hardware accelerated capable. Special
4644 * care is taken in the various xmit functions
4645 * when there are slaves that are not hw accel
4646 * capable
4647 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4649 NETIF_F_HW_VLAN_RX |
4650 NETIF_F_HW_VLAN_FILTER);
4651
4652#ifdef CONFIG_PROC_FS
4653 bond_create_proc_entry(bond);
4654#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 list_add_tail(&bond->bond_list, &bond_dev_list);
4656
4657 return 0;
4658}
4659
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004660static void bond_work_cancel_all(struct bonding *bond)
4661{
4662 write_lock_bh(&bond->lock);
4663 bond->kill_timers = 1;
4664 write_unlock_bh(&bond->lock);
4665
4666 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4667 cancel_delayed_work(&bond->mii_work);
4668
4669 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4670 cancel_delayed_work(&bond->arp_work);
4671
4672 if (bond->params.mode == BOND_MODE_ALB &&
4673 delayed_work_pending(&bond->alb_work))
4674 cancel_delayed_work(&bond->alb_work);
4675
4676 if (bond->params.mode == BOND_MODE_8023AD &&
4677 delayed_work_pending(&bond->ad_work))
4678 cancel_delayed_work(&bond->ad_work);
4679}
4680
Jay Vosburgha434e432008-10-30 17:41:15 -07004681/* De-initialize device specific data.
4682 * Caller must hold rtnl_lock.
4683 */
4684static void bond_deinit(struct net_device *bond_dev)
4685{
Wang Chen454d7c92008-11-12 23:37:49 -08004686 struct bonding *bond = netdev_priv(bond_dev);
Jay Vosburgha434e432008-10-30 17:41:15 -07004687
4688 list_del(&bond->bond_list);
4689
4690 bond_work_cancel_all(bond);
4691
4692#ifdef CONFIG_PROC_FS
4693 bond_remove_proc_entry(bond);
4694#endif
4695}
4696
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697/* Unregister and free all bond devices.
4698 * Caller must hold rtnl_lock.
4699 */
4700static void bond_free_all(void)
4701{
4702 struct bonding *bond, *nxt;
4703
4704 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4705 struct net_device *bond_dev = bond->dev;
4706
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004707 bond_work_cancel_all(bond);
jamal702987052006-09-22 21:54:37 -07004708 /* Release the bonded slaves */
4709 bond_release_all(bond_dev);
Libor Pechacek92b41da2008-03-21 22:29:35 -07004710 bond_destroy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 }
4712
4713#ifdef CONFIG_PROC_FS
4714 bond_destroy_proc_dir();
4715#endif
4716}
4717
4718/*------------------------- Module initialization ---------------------------*/
4719
4720/*
4721 * Convert string input module parms. Accept either the
Jay Vosburghece95f72008-01-17 16:25:01 -08004722 * number of the mode or its string name. A bit complicated because
4723 * some mode names are substrings of other names, and calls from sysfs
4724 * may have whitespace in the name (trailing newlines, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 */
Jay Vosburghece95f72008-01-17 16:25:01 -08004726int bond_parse_parm(const char *buf, struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727{
Jay Vosburghece95f72008-01-17 16:25:01 -08004728 int mode = -1, i, rv;
Jay Vosburgha42e5342008-01-29 18:07:43 -08004729 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
Jay Vosburghece95f72008-01-17 16:25:01 -08004730
Jay Vosburgha42e5342008-01-29 18:07:43 -08004731 for (p = (char *)buf; *p; p++)
4732 if (!(isdigit(*p) || isspace(*p)))
4733 break;
4734
4735 if (*p)
Jay Vosburghece95f72008-01-17 16:25:01 -08004736 rv = sscanf(buf, "%20s", modestr);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004737 else
4738 rv = sscanf(buf, "%d", &mode);
4739
4740 if (!rv)
4741 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742
4743 for (i = 0; tbl[i].modename; i++) {
Jay Vosburghece95f72008-01-17 16:25:01 -08004744 if (mode == tbl[i].mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 return tbl[i].mode;
Jay Vosburghece95f72008-01-17 16:25:01 -08004746 if (strcmp(modestr, tbl[i].modename) == 0)
4747 return tbl[i].mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 }
4749
4750 return -1;
4751}
4752
4753static int bond_check_params(struct bond_params *params)
4754{
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004755 int arp_validate_value, fail_over_mac_value;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004756
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 /*
4758 * Convert string parameters.
4759 */
4760 if (mode) {
4761 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4762 if (bond_mode == -1) {
4763 printk(KERN_ERR DRV_NAME
4764 ": Error: Invalid bonding mode \"%s\"\n",
4765 mode == NULL ? "NULL" : mode);
4766 return -EINVAL;
4767 }
4768 }
4769
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004770 if (xmit_hash_policy) {
4771 if ((bond_mode != BOND_MODE_XOR) &&
4772 (bond_mode != BOND_MODE_8023AD)) {
4773 printk(KERN_INFO DRV_NAME
4774 ": xor_mode param is irrelevant in mode %s\n",
4775 bond_mode_name(bond_mode));
4776 } else {
4777 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4778 xmit_hashtype_tbl);
4779 if (xmit_hashtype == -1) {
4780 printk(KERN_ERR DRV_NAME
4781 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4782 xmit_hash_policy == NULL ? "NULL" :
4783 xmit_hash_policy);
4784 return -EINVAL;
4785 }
4786 }
4787 }
4788
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 if (lacp_rate) {
4790 if (bond_mode != BOND_MODE_8023AD) {
4791 printk(KERN_INFO DRV_NAME
4792 ": lacp_rate param is irrelevant in mode %s\n",
4793 bond_mode_name(bond_mode));
4794 } else {
4795 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4796 if (lacp_fast == -1) {
4797 printk(KERN_ERR DRV_NAME
4798 ": Error: Invalid lacp rate \"%s\"\n",
4799 lacp_rate == NULL ? "NULL" : lacp_rate);
4800 return -EINVAL;
4801 }
4802 }
4803 }
4804
Jay Vosburghfd989c82008-11-04 17:51:16 -08004805 if (ad_select) {
4806 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4807 if (params->ad_select == -1) {
4808 printk(KERN_ERR DRV_NAME
4809 ": Error: Invalid ad_select \"%s\"\n",
4810 ad_select == NULL ? "NULL" : ad_select);
4811 return -EINVAL;
4812 }
4813
4814 if (bond_mode != BOND_MODE_8023AD) {
4815 printk(KERN_WARNING DRV_NAME
4816 ": ad_select param only affects 802.3ad mode\n");
4817 }
4818 } else {
4819 params->ad_select = BOND_AD_STABLE;
4820 }
4821
Jay Vosburghb8a97872008-06-13 18:12:04 -07004822 if (max_bonds < 0 || max_bonds > INT_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 printk(KERN_WARNING DRV_NAME
4824 ": Warning: max_bonds (%d) not in range %d-%d, so it "
Mitch Williams4e0952c2005-11-09 10:34:57 -08004825 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
Jay Vosburghb8a97872008-06-13 18:12:04 -07004826 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 max_bonds = BOND_DEFAULT_MAX_BONDS;
4828 }
4829
4830 if (miimon < 0) {
4831 printk(KERN_WARNING DRV_NAME
4832 ": Warning: miimon module parameter (%d), "
4833 "not in range 0-%d, so it was reset to %d\n",
4834 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4835 miimon = BOND_LINK_MON_INTERV;
4836 }
4837
4838 if (updelay < 0) {
4839 printk(KERN_WARNING DRV_NAME
4840 ": Warning: updelay module parameter (%d), "
4841 "not in range 0-%d, so it was reset to 0\n",
4842 updelay, INT_MAX);
4843 updelay = 0;
4844 }
4845
4846 if (downdelay < 0) {
4847 printk(KERN_WARNING DRV_NAME
4848 ": Warning: downdelay module parameter (%d), "
4849 "not in range 0-%d, so it was reset to 0\n",
4850 downdelay, INT_MAX);
4851 downdelay = 0;
4852 }
4853
4854 if ((use_carrier != 0) && (use_carrier != 1)) {
4855 printk(KERN_WARNING DRV_NAME
4856 ": Warning: use_carrier module parameter (%d), "
4857 "not of valid value (0/1), so it was set to 1\n",
4858 use_carrier);
4859 use_carrier = 1;
4860 }
4861
Moni Shoua7893b242008-05-17 21:10:12 -07004862 if (num_grat_arp < 0 || num_grat_arp > 255) {
4863 printk(KERN_WARNING DRV_NAME
4864 ": Warning: num_grat_arp (%d) not in range 0-255 so it "
4865 "was reset to 1 \n", num_grat_arp);
4866 num_grat_arp = 1;
4867 }
4868
Brian Haley305d5522008-11-04 17:51:14 -08004869 if (num_unsol_na < 0 || num_unsol_na > 255) {
4870 printk(KERN_WARNING DRV_NAME
4871 ": Warning: num_unsol_na (%d) not in range 0-255 so it "
4872 "was reset to 1 \n", num_unsol_na);
4873 num_unsol_na = 1;
4874 }
4875
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 /* reset values for 802.3ad */
4877 if (bond_mode == BOND_MODE_8023AD) {
4878 if (!miimon) {
4879 printk(KERN_WARNING DRV_NAME
4880 ": Warning: miimon must be specified, "
4881 "otherwise bonding will not detect link "
4882 "failure, speed and duplex which are "
4883 "essential for 802.3ad operation\n");
4884 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4885 miimon = 100;
4886 }
4887 }
4888
4889 /* reset values for TLB/ALB */
4890 if ((bond_mode == BOND_MODE_TLB) ||
4891 (bond_mode == BOND_MODE_ALB)) {
4892 if (!miimon) {
4893 printk(KERN_WARNING DRV_NAME
4894 ": Warning: miimon must be specified, "
4895 "otherwise bonding will not detect link "
4896 "failure and link speed which are essential "
4897 "for TLB/ALB load balancing\n");
4898 printk(KERN_WARNING "Forcing miimon to 100msec\n");
4899 miimon = 100;
4900 }
4901 }
4902
4903 if (bond_mode == BOND_MODE_ALB) {
4904 printk(KERN_NOTICE DRV_NAME
4905 ": In ALB mode you might experience client "
4906 "disconnections upon reconnection of a link if the "
4907 "bonding module updelay parameter (%d msec) is "
4908 "incompatible with the forwarding delay time of the "
4909 "switch\n",
4910 updelay);
4911 }
4912
4913 if (!miimon) {
4914 if (updelay || downdelay) {
4915 /* just warn the user the up/down delay will have
4916 * no effect since miimon is zero...
4917 */
4918 printk(KERN_WARNING DRV_NAME
4919 ": Warning: miimon module parameter not set "
4920 "and updelay (%d) or downdelay (%d) module "
4921 "parameter is set; updelay and downdelay have "
4922 "no effect unless miimon is set\n",
4923 updelay, downdelay);
4924 }
4925 } else {
4926 /* don't allow arp monitoring */
4927 if (arp_interval) {
4928 printk(KERN_WARNING DRV_NAME
4929 ": Warning: miimon (%d) and arp_interval (%d) "
4930 "can't be used simultaneously, disabling ARP "
4931 "monitoring\n",
4932 miimon, arp_interval);
4933 arp_interval = 0;
4934 }
4935
4936 if ((updelay % miimon) != 0) {
4937 printk(KERN_WARNING DRV_NAME
4938 ": Warning: updelay (%d) is not a multiple "
4939 "of miimon (%d), updelay rounded to %d ms\n",
4940 updelay, miimon, (updelay / miimon) * miimon);
4941 }
4942
4943 updelay /= miimon;
4944
4945 if ((downdelay % miimon) != 0) {
4946 printk(KERN_WARNING DRV_NAME
4947 ": Warning: downdelay (%d) is not a multiple "
4948 "of miimon (%d), downdelay rounded to %d ms\n",
4949 downdelay, miimon,
4950 (downdelay / miimon) * miimon);
4951 }
4952
4953 downdelay /= miimon;
4954 }
4955
4956 if (arp_interval < 0) {
4957 printk(KERN_WARNING DRV_NAME
4958 ": Warning: arp_interval module parameter (%d) "
4959 ", not in range 0-%d, so it was reset to %d\n",
4960 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4961 arp_interval = BOND_LINK_ARP_INTERV;
4962 }
4963
4964 for (arp_ip_count = 0;
4965 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4966 arp_ip_count++) {
4967 /* not complete check, but should be good enough to
4968 catch mistakes */
4969 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4970 printk(KERN_WARNING DRV_NAME
4971 ": Warning: bad arp_ip_target module parameter "
4972 "(%s), ARP monitoring will not be performed\n",
4973 arp_ip_target[arp_ip_count]);
4974 arp_interval = 0;
4975 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04004976 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 arp_target[arp_ip_count] = ip;
4978 }
4979 }
4980
4981 if (arp_interval && !arp_ip_count) {
4982 /* don't allow arping if no arp_ip_target given... */
4983 printk(KERN_WARNING DRV_NAME
4984 ": Warning: arp_interval module parameter (%d) "
4985 "specified without providing an arp_ip_target "
4986 "parameter, arp_interval was reset to 0\n",
4987 arp_interval);
4988 arp_interval = 0;
4989 }
4990
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004991 if (arp_validate) {
4992 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
4993 printk(KERN_ERR DRV_NAME
4994 ": arp_validate only supported in active-backup mode\n");
4995 return -EINVAL;
4996 }
4997 if (!arp_interval) {
4998 printk(KERN_ERR DRV_NAME
4999 ": arp_validate requires arp_interval\n");
5000 return -EINVAL;
5001 }
5002
5003 arp_validate_value = bond_parse_parm(arp_validate,
5004 arp_validate_tbl);
5005 if (arp_validate_value == -1) {
5006 printk(KERN_ERR DRV_NAME
5007 ": Error: invalid arp_validate \"%s\"\n",
5008 arp_validate == NULL ? "NULL" : arp_validate);
5009 return -EINVAL;
5010 }
5011 } else
5012 arp_validate_value = 0;
5013
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 if (miimon) {
5015 printk(KERN_INFO DRV_NAME
5016 ": MII link monitoring set to %d ms\n",
5017 miimon);
5018 } else if (arp_interval) {
5019 int i;
5020
5021 printk(KERN_INFO DRV_NAME
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005022 ": ARP monitoring set to %d ms, validate %s, with %d target(s):",
5023 arp_interval,
5024 arp_validate_tbl[arp_validate_value].modename,
5025 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
5027 for (i = 0; i < arp_ip_count; i++)
5028 printk (" %s", arp_ip_target[i]);
5029
5030 printk("\n");
5031
Jay Vosburghb8a97872008-06-13 18:12:04 -07005032 } else if (max_bonds) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 /* miimon and arp_interval not set, we need one so things
5034 * work as expected, see bonding.txt for details
5035 */
5036 printk(KERN_WARNING DRV_NAME
5037 ": Warning: either miimon or arp_interval and "
5038 "arp_ip_target module parameters must be specified, "
5039 "otherwise bonding will not detect link failures! see "
5040 "bonding.txt for details.\n");
5041 }
5042
5043 if (primary && !USES_PRIMARY(bond_mode)) {
5044 /* currently, using a primary only makes sense
5045 * in active backup, TLB or ALB modes
5046 */
5047 printk(KERN_WARNING DRV_NAME
5048 ": Warning: %s primary device specified but has no "
5049 "effect in %s mode\n",
5050 primary, bond_mode_name(bond_mode));
5051 primary = NULL;
5052 }
5053
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005054 if (fail_over_mac) {
5055 fail_over_mac_value = bond_parse_parm(fail_over_mac,
5056 fail_over_mac_tbl);
5057 if (fail_over_mac_value == -1) {
5058 printk(KERN_ERR DRV_NAME
5059 ": Error: invalid fail_over_mac \"%s\"\n",
5060 arp_validate == NULL ? "NULL" : arp_validate);
5061 return -EINVAL;
5062 }
5063
5064 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
5065 printk(KERN_WARNING DRV_NAME
5066 ": Warning: fail_over_mac only affects "
5067 "active-backup mode.\n");
5068 } else {
5069 fail_over_mac_value = BOND_FOM_NONE;
5070 }
Jay Vosburghdd957c52007-10-09 19:57:24 -07005071
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 /* fill params struct with the proper values */
5073 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04005074 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 params->miimon = miimon;
Moni Shoua7893b242008-05-17 21:10:12 -07005076 params->num_grat_arp = num_grat_arp;
Brian Haley305d5522008-11-04 17:51:14 -08005077 params->num_unsol_na = num_unsol_na;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005079 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 params->updelay = updelay;
5081 params->downdelay = downdelay;
5082 params->use_carrier = use_carrier;
5083 params->lacp_fast = lacp_fast;
5084 params->primary[0] = 0;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005085 params->fail_over_mac = fail_over_mac_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086
5087 if (primary) {
5088 strncpy(params->primary, primary, IFNAMSIZ);
5089 params->primary[IFNAMSIZ - 1] = 0;
5090 }
5091
5092 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
5093
5094 return 0;
5095}
5096
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005097static struct lock_class_key bonding_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -07005098static struct lock_class_key bonding_netdev_addr_lock_key;
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005099
David S. Millere8a04642008-07-17 00:34:19 -07005100static void bond_set_lockdep_class_one(struct net_device *dev,
5101 struct netdev_queue *txq,
5102 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -07005103{
5104 lockdep_set_class(&txq->_xmit_lock,
5105 &bonding_netdev_xmit_lock_key);
5106}
5107
5108static void bond_set_lockdep_class(struct net_device *dev)
5109{
David S. Millercf508b12008-07-22 14:16:42 -07005110 lockdep_set_class(&dev->addr_list_lock,
5111 &bonding_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -07005112 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -07005113}
5114
Mitch Williamsdfe60392005-11-09 10:36:04 -08005115/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005116 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08005117 * Caller must NOT hold rtnl_lock; we need to release it here before we
5118 * set up our sysfs entries.
5119 */
Pavel Emelyanov0dd646f2008-05-17 21:10:09 -07005120int bond_create(char *name, struct bond_params *params)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005121{
5122 struct net_device *bond_dev;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07005123 struct bonding *bond;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005124 int res;
5125
5126 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08005127 down_write(&bonding_rwsem);
5128
5129 /* Check to see if the bond already exists. */
Jay Vosburgh4fe47632008-01-29 18:07:45 -08005130 if (name) {
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07005131 list_for_each_entry(bond, &bond_dev_list, bond_list)
Jay Vosburgh4fe47632008-01-29 18:07:45 -08005132 if (strnicmp(bond->dev->name, name, IFNAMSIZ) == 0) {
5133 printk(KERN_ERR DRV_NAME
Jay Vosburgh027ea042008-01-17 16:25:02 -08005134 ": cannot add bond %s; it already exists\n",
Jay Vosburgh4fe47632008-01-29 18:07:45 -08005135 name);
5136 res = -EPERM;
5137 goto out_rtnl;
5138 }
5139 }
Jay Vosburgh027ea042008-01-17 16:25:02 -08005140
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005141 bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
5142 ether_setup);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005143 if (!bond_dev) {
5144 printk(KERN_ERR DRV_NAME
5145 ": %s: eek! can't alloc netdev!\n",
5146 name);
5147 res = -ENOMEM;
5148 goto out_rtnl;
5149 }
5150
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005151 if (!name) {
5152 res = dev_alloc_name(bond_dev, "bond%d");
5153 if (res < 0)
5154 goto out_netdev;
5155 }
5156
Mitch Williamsdfe60392005-11-09 10:36:04 -08005157 /* bond_init() must be called after dev_alloc_name() (for the
5158 * /proc files), but before register_netdevice(), because we
5159 * need to set function pointers.
5160 */
5161
5162 res = bond_init(bond_dev, params);
5163 if (res < 0) {
5164 goto out_netdev;
5165 }
5166
Mitch Williamsdfe60392005-11-09 10:36:04 -08005167 res = register_netdevice(bond_dev);
5168 if (res < 0) {
5169 goto out_bond;
5170 }
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005171
David S. Millerc773e842008-07-08 23:13:53 -07005172 bond_set_lockdep_class(bond_dev);
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005173
Jay Vosburghff59c452006-03-27 13:27:43 -08005174 netif_carrier_off(bond_dev);
5175
Jay Vosburgh027ea042008-01-17 16:25:02 -08005176 up_write(&bonding_rwsem);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005177 rtnl_unlock(); /* allows sysfs registration of net device */
Wang Chen454d7c92008-11-12 23:37:49 -08005178 res = bond_create_sysfs_entry(netdev_priv(bond_dev));
Jay Vosburgh09c89272007-01-19 18:15:38 -08005179 if (res < 0) {
5180 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08005181 down_write(&bonding_rwsem);
Pavel Emelyanov822973b2008-05-02 17:49:37 -07005182 bond_deinit(bond_dev);
5183 unregister_netdevice(bond_dev);
5184 goto out_rtnl;
Jay Vosburgh09c89272007-01-19 18:15:38 -08005185 }
5186
5187 return 0;
5188
Mitch Williamsdfe60392005-11-09 10:36:04 -08005189out_bond:
5190 bond_deinit(bond_dev);
5191out_netdev:
5192 free_netdev(bond_dev);
5193out_rtnl:
Jay Vosburgh027ea042008-01-17 16:25:02 -08005194 up_write(&bonding_rwsem);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005195 rtnl_unlock();
Mitch Williamsdfe60392005-11-09 10:36:04 -08005196 return res;
5197}
5198
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199static int __init bonding_init(void)
5200{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 int i;
5202 int res;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07005203 struct bonding *bond;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204
5205 printk(KERN_INFO "%s", version);
5206
Mitch Williamsdfe60392005-11-09 10:36:04 -08005207 res = bond_check_params(&bonding_defaults);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 if (res) {
Mitch Williamsdfe60392005-11-09 10:36:04 -08005209 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 }
5211
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212#ifdef CONFIG_PROC_FS
5213 bond_create_proc_dir();
5214#endif
Jay Vosburgh027ea042008-01-17 16:25:02 -08005215
5216 init_rwsem(&bonding_rwsem);
5217
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 for (i = 0; i < max_bonds; i++) {
Pavel Emelyanov0dd646f2008-05-17 21:10:09 -07005219 res = bond_create(NULL, &bonding_defaults);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005220 if (res)
5221 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 }
5223
Mitch Williamsb76cdba2005-11-09 10:36:41 -08005224 res = bond_create_sysfs();
5225 if (res)
5226 goto err;
5227
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005229 register_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005230 bond_register_ipv6_notifier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231
Mitch Williamsdfe60392005-11-09 10:36:04 -08005232 goto out;
5233err:
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07005234 list_for_each_entry(bond, &bond_dev_list, bond_list) {
Jay Vosburgh1b76b312007-10-17 17:37:45 -07005235 bond_work_cancel_all(bond);
5236 destroy_workqueue(bond->wq);
5237 }
5238
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005239 bond_destroy_sysfs();
5240
Florin Malita40abc272005-09-18 00:24:12 -07005241 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 bond_free_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 rtnl_unlock();
Mitch Williamsdfe60392005-11-09 10:36:04 -08005244out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 return res;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005246
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247}
5248
5249static void __exit bonding_exit(void)
5250{
5251 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005252 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005253 bond_unregister_ipv6_notifier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005255 bond_destroy_sysfs();
5256
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 rtnl_lock();
5258 bond_free_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 rtnl_unlock();
5260}
5261
5262module_init(bonding_init);
5263module_exit(bonding_exit);
5264MODULE_LICENSE("GPL");
5265MODULE_VERSION(DRV_VERSION);
5266MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5267MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
5268MODULE_SUPPORTED_DEVICE("most ethernet devices");
5269
5270/*
5271 * Local variables:
5272 * c-indent-level: 8
5273 * c-basic-offset: 8
5274 * tab-width: 8
5275 * End:
5276 */
5277