blob: 8c5ebfb0680fe27538228b2a5b9eb6c8c2c88152 [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>
Stephen Hemminger3d632c32009-06-12 19:02:48 +000054#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/dma.h>
Stephen Hemminger3d632c32009-06-12 19:02:48 +000057#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#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;
Stephen Hemminger3d632c32009-06-12 19:02:48 +000092static int updelay;
93static int downdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static int use_carrier = 1;
Stephen Hemminger3d632c32009-06-12 19:02:48 +000095static char *mode;
96static char *primary;
Jiri Pirkoa5499522009-09-25 03:28:09 +000097static char *primary_reselect;
Stephen Hemminger3d632c32009-06-12 19:02:48 +000098static char *lacp_rate;
99static char *ad_select;
100static char *xmit_hash_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static int arp_interval = BOND_LINK_ARP_INTERV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000102static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
103static char *arp_validate;
104static char *fail_over_mac;
Stephen Hemmingerd2991f72009-06-12 19:02:44 +0000105static struct bond_params bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107module_param(max_bonds, int, 0);
108MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
Moni Shoua7893b242008-05-17 21:10:12 -0700109module_param(num_grat_arp, int, 0644);
110MODULE_PARM_DESC(num_grat_arp, "Number of gratuitous ARP packets to send on failover event");
Brian Haley305d5522008-11-04 17:51:14 -0800111module_param(num_unsol_na, int, 0644);
112MODULE_PARM_DESC(num_unsol_na, "Number of unsolicited IPv6 Neighbor Advertisements packets to send on failover event");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113module_param(miimon, int, 0);
114MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
115module_param(updelay, int, 0);
116MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
117module_param(downdelay, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800118MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
119 "in milliseconds");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120module_param(use_carrier, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800121MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
122 "0 for off, 1 for on (default)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123module_param(mode, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800124MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
125 "1 for active-backup, 2 for balance-xor, "
126 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
127 "6 for balance-alb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128module_param(primary, charp, 0);
129MODULE_PARM_DESC(primary, "Primary network device to use");
Jiri Pirkoa5499522009-09-25 03:28:09 +0000130module_param(primary_reselect, charp, 0);
131MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
132 "once it comes up; "
133 "0 for always (default), "
134 "1 for only if speed of primary is "
135 "better, "
136 "2 for only on active slave "
137 "failure");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138module_param(lacp_rate, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800139MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
140 "(slow/fast)");
Jay Vosburghfd989c82008-11-04 17:51:16 -0800141module_param(ad_select, charp, 0);
142MODULE_PARM_DESC(ad_select, "803.ad aggregation selection logic: stable (0, default), bandwidth (1), count (2)");
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400143module_param(xmit_hash_policy, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800144MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
145 ", 1 for layer 3+4");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146module_param(arp_interval, int, 0);
147MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
148module_param_array(arp_ip_target, charp, NULL, 0);
149MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700150module_param(arp_validate, charp, 0);
151MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700152module_param(fail_over_mac, charp, 0);
153MODULE_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 -0700154
155/*----------------------------- Global variables ----------------------------*/
156
Arjan van de Venf71e1302006-03-03 21:33:57 -0500157static const char * const version =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
159
Mitch Williams12479f92005-11-09 10:35:44 -0800160LIST_HEAD(bond_dev_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162#ifdef CONFIG_PROC_FS
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000163static struct proc_dir_entry *bond_proc_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#endif
165
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000166static __be32 arp_target[BOND_MAX_ARP_TARGETS];
167static int arp_ip_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static int bond_mode = BOND_MODE_ROUNDROBIN;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000169static int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
170static int lacp_fast;
Jay Vosburgh217df672005-09-26 16:11:50 -0700171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800173const struct bond_parm_tbl bond_lacp_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{ "slow", AD_LACP_SLOW},
175{ "fast", AD_LACP_FAST},
176{ NULL, -1},
177};
178
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800179const struct bond_parm_tbl bond_mode_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{ "balance-rr", BOND_MODE_ROUNDROBIN},
181{ "active-backup", BOND_MODE_ACTIVEBACKUP},
182{ "balance-xor", BOND_MODE_XOR},
183{ "broadcast", BOND_MODE_BROADCAST},
184{ "802.3ad", BOND_MODE_8023AD},
185{ "balance-tlb", BOND_MODE_TLB},
186{ "balance-alb", BOND_MODE_ALB},
187{ NULL, -1},
188};
189
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800190const struct bond_parm_tbl xmit_hashtype_tbl[] = {
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400191{ "layer2", BOND_XMIT_POLICY_LAYER2},
192{ "layer3+4", BOND_XMIT_POLICY_LAYER34},
Jay Vosburgh6f6652b2007-12-06 23:40:34 -0800193{ "layer2+3", BOND_XMIT_POLICY_LAYER23},
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400194{ NULL, -1},
195};
196
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800197const struct bond_parm_tbl arp_validate_tbl[] = {
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700198{ "none", BOND_ARP_VALIDATE_NONE},
199{ "active", BOND_ARP_VALIDATE_ACTIVE},
200{ "backup", BOND_ARP_VALIDATE_BACKUP},
201{ "all", BOND_ARP_VALIDATE_ALL},
202{ NULL, -1},
203};
204
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800205const struct bond_parm_tbl fail_over_mac_tbl[] = {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700206{ "none", BOND_FOM_NONE},
207{ "active", BOND_FOM_ACTIVE},
208{ "follow", BOND_FOM_FOLLOW},
209{ NULL, -1},
210};
211
Jiri Pirkoa5499522009-09-25 03:28:09 +0000212const struct bond_parm_tbl pri_reselect_tbl[] = {
213{ "always", BOND_PRI_RESELECT_ALWAYS},
214{ "better", BOND_PRI_RESELECT_BETTER},
215{ "failure", BOND_PRI_RESELECT_FAILURE},
216{ NULL, -1},
217};
218
Jay Vosburghfd989c82008-11-04 17:51:16 -0800219struct bond_parm_tbl ad_select_tbl[] = {
220{ "stable", BOND_AD_STABLE},
221{ "bandwidth", BOND_AD_BANDWIDTH},
222{ "count", BOND_AD_COUNT},
223{ NULL, -1},
224};
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/*-------------------------- Forward declarations ---------------------------*/
227
Jay Vosburghc3ade5c2005-06-26 17:52:20 -0400228static void bond_send_gratuitous_arp(struct bonding *bond);
Stephen Hemminger181470f2009-06-12 19:02:52 +0000229static int bond_init(struct net_device *bond_dev);
Adrian Bunkc50b85d2007-10-24 18:23:17 +0200230static void bond_deinit(struct net_device *bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232/*---------------------------- General routines -----------------------------*/
233
Adrian Bunk4ad072c2007-07-09 11:51:12 -0700234static const char *bond_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800236 static const char *names[] = {
237 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
238 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
239 [BOND_MODE_XOR] = "load balancing (xor)",
240 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000241 [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800242 [BOND_MODE_TLB] = "transmit load balancing",
243 [BOND_MODE_ALB] = "adaptive load balancing",
244 };
245
246 if (mode < 0 || mode > BOND_MODE_ALB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return "unknown";
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800248
249 return names[mode];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252/*---------------------------------- VLAN -----------------------------------*/
253
254/**
255 * bond_add_vlan - add a new vlan id on bond
256 * @bond: bond that got the notification
257 * @vlan_id: the vlan id to add
258 *
259 * Returns -ENOMEM if allocation failed.
260 */
261static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
262{
263 struct vlan_entry *vlan;
264
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800265 pr_debug("bond: %s, vlan id %d\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000266 (bond ? bond->dev->name : "None"), vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Brian Haley305d5522008-11-04 17:51:14 -0800268 vlan = kzalloc(sizeof(struct vlan_entry), GFP_KERNEL);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000269 if (!vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 INIT_LIST_HEAD(&vlan->vlan_list);
273 vlan->vlan_id = vlan_id;
274
275 write_lock_bh(&bond->lock);
276
277 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
278
279 write_unlock_bh(&bond->lock);
280
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800281 pr_debug("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 return 0;
284}
285
286/**
287 * bond_del_vlan - delete a vlan id from bond
288 * @bond: bond that got the notification
289 * @vlan_id: the vlan id to delete
290 *
291 * returns -ENODEV if @vlan_id was not found in @bond.
292 */
293static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
294{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700295 struct vlan_entry *vlan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 int res = -ENODEV;
297
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800298 pr_debug("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 write_lock_bh(&bond->lock);
301
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700302 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (vlan->vlan_id == vlan_id) {
304 list_del(&vlan->vlan_list);
305
Holger Eitzenberger58402052008-12-09 23:07:13 -0800306 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 bond_alb_clear_vlan(bond, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800309 pr_debug("removed VLAN ID %d from bond %s\n", vlan_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 bond->dev->name);
311
312 kfree(vlan);
313
314 if (list_empty(&bond->vlan_list) &&
315 (bond->slave_cnt == 0)) {
316 /* Last VLAN removed and no slaves, so
317 * restore block on adding VLANs. This will
318 * be removed once new slaves that are not
319 * VLAN challenged will be added.
320 */
321 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
322 }
323
324 res = 0;
325 goto out;
326 }
327 }
328
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800329 pr_debug("couldn't find VLAN ID %d in bond %s\n", vlan_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 bond->dev->name);
331
332out:
333 write_unlock_bh(&bond->lock);
334 return res;
335}
336
337/**
338 * bond_has_challenged_slaves
339 * @bond: the bond we're working on
340 *
341 * Searches the slave list. Returns 1 if a vlan challenged slave
342 * was found, 0 otherwise.
343 *
344 * Assumes bond->lock is held.
345 */
346static int bond_has_challenged_slaves(struct bonding *bond)
347{
348 struct slave *slave;
349 int i;
350
351 bond_for_each_slave(bond, slave, i) {
352 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800353 pr_debug("found VLAN challenged slave - %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 slave->dev->name);
355 return 1;
356 }
357 }
358
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800359 pr_debug("no VLAN challenged slaves found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
361}
362
363/**
364 * bond_next_vlan - safely skip to the next item in the vlans list.
365 * @bond: the bond we're working on
366 * @curr: item we're advancing from
367 *
368 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
369 * or @curr->next otherwise (even if it is @curr itself again).
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000370 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 * Caller must hold bond->lock
372 */
373struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
374{
375 struct vlan_entry *next, *last;
376
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000377 if (list_empty(&bond->vlan_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 if (!curr) {
381 next = list_entry(bond->vlan_list.next,
382 struct vlan_entry, vlan_list);
383 } else {
384 last = list_entry(bond->vlan_list.prev,
385 struct vlan_entry, vlan_list);
386 if (last == curr) {
387 next = list_entry(bond->vlan_list.next,
388 struct vlan_entry, vlan_list);
389 } else {
390 next = list_entry(curr->vlan_list.next,
391 struct vlan_entry, vlan_list);
392 }
393 }
394
395 return next;
396}
397
398/**
399 * bond_dev_queue_xmit - Prepare skb for xmit.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000400 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 * @bond: bond device that got this skb for tx.
402 * @skb: hw accel VLAN tagged skb to transmit
403 * @slave_dev: slave that is supposed to xmit this skbuff
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000404 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 * When the bond gets an skb to transmit that is
406 * already hardware accelerated VLAN tagged, and it
407 * needs to relay this skb to a slave that is not
408 * hw accel capable, the skb needs to be "unaccelerated",
409 * i.e. strip the hwaccel tag and re-insert it as part
410 * of the payload.
411 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000412int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
413 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Jay Vosburgh966bc6f2008-03-21 22:29:34 -0700415 unsigned short uninitialized_var(vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 if (!list_empty(&bond->vlan_list) &&
418 !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
419 vlan_get_tag(skb, &vlan_id) == 0) {
420 skb->dev = slave_dev;
421 skb = vlan_put_tag(skb, vlan_id);
422 if (!skb) {
423 /* vlan_put_tag() frees the skb in case of error,
424 * so return success here so the calling functions
425 * won't attempt to free is again.
426 */
427 return 0;
428 }
429 } else {
430 skb->dev = slave_dev;
431 }
432
433 skb->priority = 1;
434 dev_queue_xmit(skb);
435
436 return 0;
437}
438
439/*
440 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
441 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
442 * lock because:
443 * a. This operation is performed in IOCTL context,
444 * b. The operation is protected by the RTNL semaphore in the 8021q code,
445 * c. Holding a lock with BH disabled while directly calling a base driver
446 * entry point is generally a BAD idea.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000447 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 * The design of synchronization/protection for this operation in the 8021q
449 * module is good for one or more VLAN devices over a single physical device
450 * and cannot be extended for a teaming solution like bonding, so there is a
451 * potential race condition here where a net device from the vlan group might
452 * be referenced (either by a base driver or the 8021q code) while it is being
453 * removed from the system. However, it turns out we're not making matters
454 * worse, and if it works for regular VLAN usage it will work here too.
455*/
456
457/**
458 * bond_vlan_rx_register - Propagates registration to slaves
459 * @bond_dev: bonding net device that got called
460 * @grp: vlan group being registered
461 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000462static void bond_vlan_rx_register(struct net_device *bond_dev,
463 struct vlan_group *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Wang Chen454d7c92008-11-12 23:37:49 -0800465 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 struct slave *slave;
467 int i;
468
469 bond->vlgrp = grp;
470
471 bond_for_each_slave(bond, slave, i) {
472 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800473 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800476 slave_ops->ndo_vlan_rx_register) {
477 slave_ops->ndo_vlan_rx_register(slave_dev, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479 }
480}
481
482/**
483 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
484 * @bond_dev: bonding net device that got called
485 * @vid: vlan id being added
486 */
487static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
488{
Wang Chen454d7c92008-11-12 23:37:49 -0800489 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 struct slave *slave;
491 int i, res;
492
493 bond_for_each_slave(bond, slave, i) {
494 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800495 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800498 slave_ops->ndo_vlan_rx_add_vid) {
499 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501 }
502
503 res = bond_add_vlan(bond, vid);
504 if (res) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000505 pr_err(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800506 ": %s: Error: Failed to add vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 bond_dev->name, vid);
508 }
509}
510
511/**
512 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
513 * @bond_dev: bonding net device that got called
514 * @vid: vlan id being removed
515 */
516static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
517{
Wang Chen454d7c92008-11-12 23:37:49 -0800518 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 struct slave *slave;
520 struct net_device *vlan_dev;
521 int i, res;
522
523 bond_for_each_slave(bond, slave, i) {
524 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800525 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800528 slave_ops->ndo_vlan_rx_kill_vid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* Save and then restore vlan_dev in the grp array,
530 * since the slave's driver might clear it.
531 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800532 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800533 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vid);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800534 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536 }
537
538 res = bond_del_vlan(bond, vid);
539 if (res) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000540 pr_err(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800541 ": %s: Error: Failed to remove vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 bond_dev->name, vid);
543 }
544}
545
546static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
547{
548 struct vlan_entry *vlan;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800549 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 write_lock_bh(&bond->lock);
552
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800553 if (list_empty(&bond->vlan_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800557 slave_ops->ndo_vlan_rx_register)
558 slave_ops->ndo_vlan_rx_register(slave_dev, bond->vlgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800561 !(slave_ops->ndo_vlan_rx_add_vid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800564 list_for_each_entry(vlan, &bond->vlan_list, vlan_list)
565 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vlan->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567out:
568 write_unlock_bh(&bond->lock);
569}
570
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000571static void bond_del_vlans_from_slave(struct bonding *bond,
572 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800574 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct vlan_entry *vlan;
576 struct net_device *vlan_dev;
577
578 write_lock_bh(&bond->lock);
579
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800580 if (list_empty(&bond->vlan_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800584 !(slave_ops->ndo_vlan_rx_kill_vid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 goto unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
588 /* Save and then restore vlan_dev in the grp array,
589 * since the slave's driver might clear it.
590 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800591 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800592 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800593 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595
596unreg:
597 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800598 slave_ops->ndo_vlan_rx_register)
599 slave_ops->ndo_vlan_rx_register(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601out:
602 write_unlock_bh(&bond->lock);
603}
604
605/*------------------------------- Link status -------------------------------*/
606
607/*
Jay Vosburghff59c452006-03-27 13:27:43 -0800608 * Set the carrier state for the master according to the state of its
609 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
610 * do special 802.3ad magic.
611 *
612 * Returns zero if carrier state does not change, nonzero if it does.
613 */
614static int bond_set_carrier(struct bonding *bond)
615{
616 struct slave *slave;
617 int i;
618
619 if (bond->slave_cnt == 0)
620 goto down;
621
622 if (bond->params.mode == BOND_MODE_8023AD)
623 return bond_3ad_set_carrier(bond);
624
625 bond_for_each_slave(bond, slave, i) {
626 if (slave->link == BOND_LINK_UP) {
627 if (!netif_carrier_ok(bond->dev)) {
628 netif_carrier_on(bond->dev);
629 return 1;
630 }
631 return 0;
632 }
633 }
634
635down:
636 if (netif_carrier_ok(bond->dev)) {
637 netif_carrier_off(bond->dev);
638 return 1;
639 }
640 return 0;
641}
642
643/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 * Get link speed and duplex from the slave's base driver
645 * using ethtool. If for some reason the call fails or the
646 * values are invalid, fake speed and duplex to 100/Full
647 * and return error.
648 */
649static int bond_update_speed_duplex(struct slave *slave)
650{
651 struct net_device *slave_dev = slave->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 struct ethtool_cmd etool;
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700653 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 /* Fake speed and duplex */
656 slave->speed = SPEED_100;
657 slave->duplex = DUPLEX_FULL;
658
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700659 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700662 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
663 if (res < 0)
664 return -1;
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 switch (etool.speed) {
667 case SPEED_10:
668 case SPEED_100:
669 case SPEED_1000:
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700670 case SPEED_10000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 break;
672 default:
673 return -1;
674 }
675
676 switch (etool.duplex) {
677 case DUPLEX_FULL:
678 case DUPLEX_HALF:
679 break;
680 default:
681 return -1;
682 }
683
684 slave->speed = etool.speed;
685 slave->duplex = etool.duplex;
686
687 return 0;
688}
689
690/*
691 * if <dev> supports MII link status reporting, check its link status.
692 *
693 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000694 * depending upon the setting of the use_carrier parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 *
696 * Return either BMSR_LSTATUS, meaning that the link is up (or we
697 * can't tell and just pretend it is), or 0, meaning that the link is
698 * down.
699 *
700 * If reporting is non-zero, instead of faking link up, return -1 if
701 * both ETHTOOL and MII ioctls fail (meaning the device does not
702 * support them). If use_carrier is set, return whatever it says.
703 * It'd be nice if there was a good way to tell if a driver supports
704 * netif_carrier, but there really isn't.
705 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000706static int bond_check_dev_link(struct bonding *bond,
707 struct net_device *slave_dev, int reporting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800709 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000710 static int (*ioctl)(struct net_device *, struct ifreq *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 struct ifreq ifr;
712 struct mii_ioctl_data *mii;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Petri Gynther6c988852009-08-28 12:05:15 +0000714 if (!reporting && !netif_running(slave_dev))
715 return 0;
716
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800717 if (bond->params.use_carrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Jiri Pirko29112f42009-04-24 01:58:23 +0000720 /* Try to get link status using Ethtool first. */
721 if (slave_dev->ethtool_ops) {
722 if (slave_dev->ethtool_ops->get_link) {
723 u32 link;
724
725 link = slave_dev->ethtool_ops->get_link(slave_dev);
726
727 return link ? BMSR_LSTATUS : 0;
728 }
729 }
730
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000731 /* Ethtool can't be used, fallback to MII ioctls. */
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800732 ioctl = slave_ops->ndo_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (ioctl) {
734 /* TODO: set pointer to correct ioctl on a per team member */
735 /* bases to make this more efficient. that is, once */
736 /* we determine the correct ioctl, we will always */
737 /* call it and not the others for that team */
738 /* member. */
739
740 /*
741 * We cannot assume that SIOCGMIIPHY will also read a
742 * register; not all network drivers (e.g., e100)
743 * support that.
744 */
745
746 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
747 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
748 mii = if_mii(&ifr);
749 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
750 mii->reg_num = MII_BMSR;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000751 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0)
752 return mii->val_out & BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754 }
755
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700756 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 * If reporting, report that either there's no dev->do_ioctl,
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700758 * or both SIOCGMIIREG and get_link failed (meaning that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 * cannot report link status). If not reporting, pretend
760 * we're ok.
761 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000762 return reporting ? -1 : BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765/*----------------------------- Multicast list ------------------------------*/
766
767/*
768 * Returns 0 if dmi1 and dmi2 are the same, non-0 otherwise
769 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000770static inline int bond_is_dmi_same(const struct dev_mc_list *dmi1,
771 const struct dev_mc_list *dmi2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
774 dmi1->dmi_addrlen == dmi2->dmi_addrlen;
775}
776
777/*
778 * returns dmi entry if found, NULL otherwise
779 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000780static struct dev_mc_list *bond_mc_list_find_dmi(struct dev_mc_list *dmi,
781 struct dev_mc_list *mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 struct dev_mc_list *idmi;
784
785 for (idmi = mc_list; idmi; idmi = idmi->next) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000786 if (bond_is_dmi_same(dmi, idmi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return idmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789
790 return NULL;
791}
792
793/*
794 * Push the promiscuity flag down to appropriate slaves
795 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700796static int bond_set_promiscuity(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700798 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (USES_PRIMARY(bond->params.mode)) {
800 /* write lock already acquired */
801 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700802 err = dev_set_promiscuity(bond->curr_active_slave->dev,
803 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805 } else {
806 struct slave *slave;
807 int i;
808 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700809 err = dev_set_promiscuity(slave->dev, inc);
810 if (err)
811 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700814 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
817/*
818 * Push the allmulti flag down to all slaves
819 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700820static int bond_set_allmulti(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700822 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (USES_PRIMARY(bond->params.mode)) {
824 /* write lock already acquired */
825 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700826 err = dev_set_allmulti(bond->curr_active_slave->dev,
827 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829 } else {
830 struct slave *slave;
831 int i;
832 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700833 err = dev_set_allmulti(slave->dev, inc);
834 if (err)
835 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700838 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841/*
842 * Add a Multicast address to slaves
843 * according to mode
844 */
845static void bond_mc_add(struct bonding *bond, void *addr, int alen)
846{
847 if (USES_PRIMARY(bond->params.mode)) {
848 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000849 if (bond->curr_active_slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 dev_mc_add(bond->curr_active_slave->dev, addr, alen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 } else {
852 struct slave *slave;
853 int i;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000854
855 bond_for_each_slave(bond, slave, i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 dev_mc_add(slave->dev, addr, alen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858}
859
860/*
861 * Remove a multicast address from slave
862 * according to mode
863 */
864static void bond_mc_delete(struct bonding *bond, void *addr, int alen)
865{
866 if (USES_PRIMARY(bond->params.mode)) {
867 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000868 if (bond->curr_active_slave)
869 dev_mc_delete(bond->curr_active_slave->dev, addr,
870 alen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 } else {
872 struct slave *slave;
873 int i;
874 bond_for_each_slave(bond, slave, i) {
875 dev_mc_delete(slave->dev, addr, alen, 0);
876 }
877 }
878}
879
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800880
881/*
882 * Retrieve the list of registered multicast addresses for the bonding
883 * device and retransmit an IGMP JOIN request to the current active
884 * slave.
885 */
886static void bond_resend_igmp_join_requests(struct bonding *bond)
887{
888 struct in_device *in_dev;
889 struct ip_mc_list *im;
890
891 rcu_read_lock();
892 in_dev = __in_dev_get_rcu(bond->dev);
893 if (in_dev) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000894 for (im = in_dev->mc_list; im; im = im->next)
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800895 ip_mc_rejoin_group(im);
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800896 }
897
898 rcu_read_unlock();
899}
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901/*
902 * Totally destroys the mc_list in bond
903 */
904static void bond_mc_list_destroy(struct bonding *bond)
905{
906 struct dev_mc_list *dmi;
907
908 dmi = bond->mc_list;
909 while (dmi) {
910 bond->mc_list = dmi->next;
911 kfree(dmi);
912 dmi = bond->mc_list;
913 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000914
915 bond->mc_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
918/*
919 * Copy all the Multicast addresses from src to the bonding device dst
920 */
Randy Dunlapde54f392005-10-04 22:39:41 -0700921static int bond_mc_list_copy(struct dev_mc_list *mc_list, struct bonding *bond,
Al Virodd0fc662005-10-07 07:46:04 +0100922 gfp_t gfp_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 struct dev_mc_list *dmi, *new_dmi;
925
926 for (dmi = mc_list; dmi; dmi = dmi->next) {
Randy Dunlapde54f392005-10-04 22:39:41 -0700927 new_dmi = kmalloc(sizeof(struct dev_mc_list), gfp_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 if (!new_dmi) {
930 /* FIXME: Potential memory leak !!! */
931 return -ENOMEM;
932 }
933
934 new_dmi->next = bond->mc_list;
935 bond->mc_list = new_dmi;
936 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
937 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
938 new_dmi->dmi_users = dmi->dmi_users;
939 new_dmi->dmi_gusers = dmi->dmi_gusers;
940 }
941
942 return 0;
943}
944
945/*
946 * flush all members of flush->mc_list from device dev->mc_list
947 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000948static void bond_mc_list_flush(struct net_device *bond_dev,
949 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Wang Chen454d7c92008-11-12 23:37:49 -0800951 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 struct dev_mc_list *dmi;
953
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000954 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 dev_mc_delete(slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 if (bond->params.mode == BOND_MODE_8023AD) {
958 /* del lacpdu mc addr from mc list */
959 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
960
961 dev_mc_delete(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
962 }
963}
964
965/*--------------------------- Active slave change ---------------------------*/
966
967/*
968 * Update the mc list and multicast-related flags for the new and
969 * old active slaves (if any) according to the multicast mode, and
970 * promiscuous flags unconditionally.
971 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000972static void bond_mc_swap(struct bonding *bond, struct slave *new_active,
973 struct slave *old_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 struct dev_mc_list *dmi;
976
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000977 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 /* nothing to do - mc list is already up-to-date on
979 * all slaves
980 */
981 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 if (old_active) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000984 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 dev_set_promiscuity(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000987 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 dev_set_allmulti(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000990 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next)
991 dev_mc_delete(old_active->dev, dmi->dmi_addr,
992 dmi->dmi_addrlen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994
995 if (new_active) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700996 /* FIXME: Signal errors upstream. */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000997 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 dev_set_promiscuity(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001000 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 dev_set_allmulti(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001003 for (dmi = bond->dev->mc_list; dmi; dmi = dmi->next)
1004 dev_mc_add(new_active->dev, dmi->dmi_addr,
1005 dmi->dmi_addrlen, 0);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001006 bond_resend_igmp_join_requests(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008}
1009
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001010/*
1011 * bond_do_fail_over_mac
1012 *
1013 * Perform special MAC address swapping for fail_over_mac settings
1014 *
1015 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
1016 */
1017static void bond_do_fail_over_mac(struct bonding *bond,
1018 struct slave *new_active,
1019 struct slave *old_active)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00001020 __releases(&bond->curr_slave_lock)
1021 __releases(&bond->lock)
1022 __acquires(&bond->lock)
1023 __acquires(&bond->curr_slave_lock)
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001024{
1025 u8 tmp_mac[ETH_ALEN];
1026 struct sockaddr saddr;
1027 int rv;
1028
1029 switch (bond->params.fail_over_mac) {
1030 case BOND_FOM_ACTIVE:
1031 if (new_active)
1032 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
1033 new_active->dev->addr_len);
1034 break;
1035 case BOND_FOM_FOLLOW:
1036 /*
1037 * if new_active && old_active, swap them
1038 * if just old_active, do nothing (going to no active slave)
1039 * if just new_active, set new_active to bond's MAC
1040 */
1041 if (!new_active)
1042 return;
1043
1044 write_unlock_bh(&bond->curr_slave_lock);
1045 read_unlock(&bond->lock);
1046
1047 if (old_active) {
1048 memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
1049 memcpy(saddr.sa_data, old_active->dev->dev_addr,
1050 ETH_ALEN);
1051 saddr.sa_family = new_active->dev->type;
1052 } else {
1053 memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
1054 saddr.sa_family = bond->dev->type;
1055 }
1056
1057 rv = dev_set_mac_address(new_active->dev, &saddr);
1058 if (rv) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001059 pr_err(DRV_NAME
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001060 ": %s: Error %d setting MAC of slave %s\n",
1061 bond->dev->name, -rv, new_active->dev->name);
1062 goto out;
1063 }
1064
1065 if (!old_active)
1066 goto out;
1067
1068 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
1069 saddr.sa_family = old_active->dev->type;
1070
1071 rv = dev_set_mac_address(old_active->dev, &saddr);
1072 if (rv)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001073 pr_err(DRV_NAME
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001074 ": %s: Error %d setting MAC of slave %s\n",
1075 bond->dev->name, -rv, new_active->dev->name);
1076out:
1077 read_lock(&bond->lock);
1078 write_lock_bh(&bond->curr_slave_lock);
1079 break;
1080 default:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001081 pr_err(DRV_NAME
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001082 ": %s: bond_do_fail_over_mac impossible: bad policy %d\n",
1083 bond->dev->name, bond->params.fail_over_mac);
1084 break;
1085 }
1086
1087}
1088
Jiri Pirkoa5499522009-09-25 03:28:09 +00001089static bool bond_should_change_active(struct bonding *bond)
1090{
1091 struct slave *prim = bond->primary_slave;
1092 struct slave *curr = bond->curr_active_slave;
1093
1094 if (!prim || !curr || curr->link != BOND_LINK_UP)
1095 return true;
1096 if (bond->force_primary) {
1097 bond->force_primary = false;
1098 return true;
1099 }
1100 if (bond->params.primary_reselect == BOND_PRI_RESELECT_BETTER &&
1101 (prim->speed < curr->speed ||
1102 (prim->speed == curr->speed && prim->duplex <= curr->duplex)))
1103 return false;
1104 if (bond->params.primary_reselect == BOND_PRI_RESELECT_FAILURE)
1105 return false;
1106 return true;
1107}
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109/**
1110 * find_best_interface - select the best available slave to be the active one
1111 * @bond: our bonding struct
1112 *
1113 * Warning: Caller must hold curr_slave_lock for writing.
1114 */
1115static struct slave *bond_find_best_slave(struct bonding *bond)
1116{
1117 struct slave *new_active, *old_active;
1118 struct slave *bestslave = NULL;
1119 int mintime = bond->params.updelay;
1120 int i;
1121
Nicolas de Pesloüan49b4ad92009-10-07 14:11:00 -07001122 new_active = bond->curr_active_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 if (!new_active) { /* there were no active slaves left */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001125 if (bond->slave_cnt > 0) /* found one slave */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 new_active = bond->first_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001127 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return NULL; /* still no slave, return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if ((bond->primary_slave) &&
Jiri Pirkoa5499522009-09-25 03:28:09 +00001132 bond->primary_slave->link == BOND_LINK_UP &&
1133 bond_should_change_active(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 new_active = bond->primary_slave;
1135 }
1136
1137 /* remember where to stop iterating over the slaves */
1138 old_active = new_active;
1139
1140 bond_for_each_slave_from(bond, new_active, i, old_active) {
Jiri Pirkob9f60252009-08-31 11:09:38 +00001141 if (new_active->link == BOND_LINK_UP) {
1142 return new_active;
1143 } else if (new_active->link == BOND_LINK_BACK &&
1144 IS_UP(new_active->dev)) {
1145 /* link up, but waiting for stabilization */
1146 if (new_active->delay < mintime) {
1147 mintime = new_active->delay;
1148 bestslave = new_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
1150 }
1151 }
1152
1153 return bestslave;
1154}
1155
1156/**
1157 * change_active_interface - change the active slave into the specified one
1158 * @bond: our bonding struct
1159 * @new: the new slave to make the active one
1160 *
1161 * Set the new slave to the bond's settings and unset them on the old
1162 * curr_active_slave.
1163 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1164 *
1165 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1166 * because it is apparently the best available slave we have, even though its
1167 * updelay hasn't timed out yet.
1168 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001169 * If new_active is not NULL, caller must hold bond->lock for read and
1170 * curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001172void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
1174 struct slave *old_active = bond->curr_active_slave;
1175
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001176 if (old_active == new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 if (new_active) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07001180 new_active->jiffies = jiffies;
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (new_active->link == BOND_LINK_BACK) {
1183 if (USES_PRIMARY(bond->params.mode)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001184 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 ": %s: making interface %s the new "
1186 "active one %d ms earlier.\n",
1187 bond->dev->name, new_active->dev->name,
1188 (bond->params.updelay - new_active->delay) * bond->params.miimon);
1189 }
1190
1191 new_active->delay = 0;
1192 new_active->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001194 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Holger Eitzenberger58402052008-12-09 23:07:13 -08001197 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 } else {
1200 if (USES_PRIMARY(bond->params.mode)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001201 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 ": %s: making interface %s the new "
1203 "active one.\n",
1204 bond->dev->name, new_active->dev->name);
1205 }
1206 }
1207 }
1208
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001209 if (USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 bond_mc_swap(bond, new_active, old_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Holger Eitzenberger58402052008-12-09 23:07:13 -08001212 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 bond_alb_handle_active_change(bond, new_active);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001214 if (old_active)
1215 bond_set_slave_inactive_flags(old_active);
1216 if (new_active)
1217 bond_set_slave_active_flags(new_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 } else {
1219 bond->curr_active_slave = new_active;
1220 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001221
1222 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001223 if (old_active)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001224 bond_set_slave_inactive_flags(old_active);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001225
1226 if (new_active) {
1227 bond_set_slave_active_flags(new_active);
Moni Shoua2ab82852007-10-09 19:43:39 -07001228
Or Gerlitz709f8a42008-06-13 18:12:01 -07001229 if (bond->params.fail_over_mac)
1230 bond_do_fail_over_mac(bond, new_active,
1231 old_active);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001232
Or Gerlitz709f8a42008-06-13 18:12:01 -07001233 bond->send_grat_arp = bond->params.num_grat_arp;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07001234 bond_send_gratuitous_arp(bond);
Or Gerlitz01f31092008-06-13 18:12:02 -07001235
Brian Haley305d5522008-11-04 17:51:14 -08001236 bond->send_unsol_na = bond->params.num_unsol_na;
1237 bond_send_unsolicited_na(bond);
1238
Or Gerlitz01f31092008-06-13 18:12:02 -07001239 write_unlock_bh(&bond->curr_slave_lock);
1240 read_unlock(&bond->lock);
1241
Moni Shoua75c78502009-09-15 02:37:40 -07001242 netdev_bonding_change(bond->dev, NETDEV_BONDING_FAILOVER);
Or Gerlitz01f31092008-06-13 18:12:02 -07001243
1244 read_lock(&bond->lock);
1245 write_lock_bh(&bond->curr_slave_lock);
Moni Shoua7893b242008-05-17 21:10:12 -07001246 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
1250/**
1251 * bond_select_active_slave - select a new active slave, if needed
1252 * @bond: our bonding struct
1253 *
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001254 * This functions should be called when one of the following occurs:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 * - The old curr_active_slave has been released or lost its link.
1256 * - The primary_slave has got its link back.
1257 * - A slave has got its link back and there's no old curr_active_slave.
1258 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001259 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001261void bond_select_active_slave(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 struct slave *best_slave;
Jay Vosburghff59c452006-03-27 13:27:43 -08001264 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 best_slave = bond_find_best_slave(bond);
1267 if (best_slave != bond->curr_active_slave) {
1268 bond_change_active_slave(bond, best_slave);
Jay Vosburghff59c452006-03-27 13:27:43 -08001269 rv = bond_set_carrier(bond);
1270 if (!rv)
1271 return;
1272
1273 if (netif_carrier_ok(bond->dev)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001274 pr_info(DRV_NAME
Jay Vosburghff59c452006-03-27 13:27:43 -08001275 ": %s: first active interface up!\n",
1276 bond->dev->name);
1277 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001278 pr_info(DRV_NAME ": %s: "
Jay Vosburghff59c452006-03-27 13:27:43 -08001279 "now running without any active interface !\n",
1280 bond->dev->name);
1281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283}
1284
1285/*--------------------------- slave list handling ---------------------------*/
1286
1287/*
1288 * This function attaches the slave to the end of list.
1289 *
1290 * bond->lock held for writing by caller.
1291 */
1292static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1293{
1294 if (bond->first_slave == NULL) { /* attaching the first slave */
1295 new_slave->next = new_slave;
1296 new_slave->prev = new_slave;
1297 bond->first_slave = new_slave;
1298 } else {
1299 new_slave->next = bond->first_slave;
1300 new_slave->prev = bond->first_slave->prev;
1301 new_slave->next->prev = new_slave;
1302 new_slave->prev->next = new_slave;
1303 }
1304
1305 bond->slave_cnt++;
1306}
1307
1308/*
1309 * This function detaches the slave from the list.
1310 * WARNING: no check is made to verify if the slave effectively
1311 * belongs to <bond>.
1312 * Nothing is freed on return, structures are just unchained.
1313 * If any slave pointer in bond was pointing to <slave>,
1314 * it should be changed by the calling function.
1315 *
1316 * bond->lock held for writing by caller.
1317 */
1318static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1319{
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001320 if (slave->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 slave->next->prev = slave->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001323 if (slave->prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 slave->prev->next = slave->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 if (bond->first_slave == slave) { /* slave is the first slave */
1327 if (bond->slave_cnt > 1) { /* there are more slave */
1328 bond->first_slave = slave->next;
1329 } else {
1330 bond->first_slave = NULL; /* slave was the last one */
1331 }
1332 }
1333
1334 slave->next = NULL;
1335 slave->prev = NULL;
1336 bond->slave_cnt--;
1337}
1338
1339/*---------------------------------- IOCTL ----------------------------------*/
1340
Adrian Bunk4ad072c2007-07-09 11:51:12 -07001341static int bond_sethwaddr(struct net_device *bond_dev,
1342 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001344 pr_debug("bond_dev=%p\n", bond_dev);
1345 pr_debug("slave_dev=%p\n", slave_dev);
1346 pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1348 return 0;
1349}
1350
Herbert Xu7f353bf2007-08-10 15:47:58 -07001351#define BOND_VLAN_FEATURES \
1352 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1353 NETIF_F_HW_VLAN_FILTER)
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001354
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001355/*
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001356 * Compute the common dev->feature set available to all slaves. Some
Herbert Xu7f353bf2007-08-10 15:47:58 -07001357 * feature bits are managed elsewhere, so preserve those feature bits
1358 * on the master device.
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001359 */
1360static int bond_compute_features(struct bonding *bond)
1361{
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001362 struct slave *slave;
1363 struct net_device *bond_dev = bond->dev;
Herbert Xu7f353bf2007-08-10 15:47:58 -07001364 unsigned long features = bond_dev->features;
Jay Vosburgh278339a2009-08-28 12:05:12 +00001365 unsigned long vlan_features = 0;
Moni Shoua3158bf72007-10-09 19:43:41 -07001366 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1367 bond_dev->hard_header_len);
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001368 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001369
Herbert Xu7f353bf2007-08-10 15:47:58 -07001370 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
Herbert Xub63365a2008-10-23 01:11:29 -07001371 features |= NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1372
1373 if (!bond->first_slave)
1374 goto done;
1375
1376 features &= ~NETIF_F_ONE_FOR_ALL;
Herbert Xu7f353bf2007-08-10 15:47:58 -07001377
Jay Vosburgh278339a2009-08-28 12:05:12 +00001378 vlan_features = bond->first_slave->dev->vlan_features;
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001379 bond_for_each_slave(bond, slave, i) {
Herbert Xub63365a2008-10-23 01:11:29 -07001380 features = netdev_increment_features(features,
1381 slave->dev->features,
1382 NETIF_F_ONE_FOR_ALL);
Jay Vosburgh278339a2009-08-28 12:05:12 +00001383 vlan_features = netdev_increment_features(vlan_features,
1384 slave->dev->vlan_features,
1385 NETIF_F_ONE_FOR_ALL);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001386 if (slave->dev->hard_header_len > max_hard_header_len)
1387 max_hard_header_len = slave->dev->hard_header_len;
1388 }
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001389
Herbert Xub63365a2008-10-23 01:11:29 -07001390done:
Herbert Xu7f353bf2007-08-10 15:47:58 -07001391 features |= (bond_dev->features & BOND_VLAN_FEATURES);
Herbert Xub63365a2008-10-23 01:11:29 -07001392 bond_dev->features = netdev_fix_features(features, NULL);
Jay Vosburgh278339a2009-08-28 12:05:12 +00001393 bond_dev->vlan_features = netdev_fix_features(vlan_features, NULL);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001394 bond_dev->hard_header_len = max_hard_header_len;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001395
1396 return 0;
1397}
1398
Moni Shoua872254d2007-10-09 19:43:38 -07001399static void bond_setup_by_slave(struct net_device *bond_dev,
1400 struct net_device *slave_dev)
1401{
Wang Chen454d7c92008-11-12 23:37:49 -08001402 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07001403
Stephen Hemminger00829822008-11-20 20:14:53 -08001404 bond_dev->header_ops = slave_dev->header_ops;
Moni Shoua872254d2007-10-09 19:43:38 -07001405
1406 bond_dev->type = slave_dev->type;
1407 bond_dev->hard_header_len = slave_dev->hard_header_len;
1408 bond_dev->addr_len = slave_dev->addr_len;
1409
1410 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1411 slave_dev->addr_len);
Moni Shouad90a1622007-10-09 19:43:43 -07001412 bond->setup_by_slave = 1;
Moni Shoua872254d2007-10-09 19:43:38 -07001413}
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415/* enslave device <slave> to bond device <master> */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001416int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Wang Chen454d7c92008-11-12 23:37:49 -08001418 struct bonding *bond = netdev_priv(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001419 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 struct slave *new_slave = NULL;
1421 struct dev_mc_list *dmi;
1422 struct sockaddr addr;
1423 int link_reporting;
1424 int old_features = bond_dev->features;
1425 int res = 0;
1426
nsxfreddy@gmail.com552709d2005-09-21 14:18:04 -05001427 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001428 slave_ops->ndo_do_ioctl == NULL) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001429 pr_warning(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001430 ": %s: Warning: no link monitoring support for %s\n",
1431 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
1433
1434 /* bond must be initialized by bond_open() before enslaving */
1435 if (!(bond_dev->flags & IFF_UP)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001436 pr_warning(DRV_NAME
Moni Shoua6b1bf092007-10-09 19:43:40 -07001437 " %s: master_dev is not up in bond_enslave\n",
1438 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440
1441 /* already enslaved */
1442 if (slave_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001443 pr_debug("Error, Device was already enslaved\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return -EBUSY;
1445 }
1446
1447 /* vlan challenged mutual exclusion */
1448 /* no need to lock since we're protected by rtnl_lock */
1449 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001450 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (!list_empty(&bond->vlan_list)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001452 pr_err(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001453 ": %s: Error: cannot enslave VLAN "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 "challenged slave %s on VLAN enabled "
Mitch Williams4e0952c2005-11-09 10:34:57 -08001455 "bond %s\n", bond_dev->name, slave_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 bond_dev->name);
1457 return -EPERM;
1458 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001459 pr_warning(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001460 ": %s: Warning: enslaved VLAN challenged "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 "slave %s. Adding VLANs will be blocked as "
1462 "long as %s is part of bond %s\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001463 bond_dev->name, slave_dev->name, slave_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 bond_dev->name);
1465 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1466 }
1467 } else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001468 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 if (bond->slave_cnt == 0) {
1470 /* First slave, and it is not VLAN challenged,
1471 * so remove the block of adding VLANs over the bond.
1472 */
1473 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1474 }
1475 }
1476
Jay Vosburgh217df672005-09-26 16:11:50 -07001477 /*
1478 * Old ifenslave binaries are no longer supported. These can
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001479 * be identified with moderate accuracy by the state of the slave:
Jay Vosburgh217df672005-09-26 16:11:50 -07001480 * the current ifenslave will set the interface down prior to
1481 * enslaving it; the old ifenslave will not.
1482 */
1483 if ((slave_dev->flags & IFF_UP)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001484 pr_err(DRV_NAME ": %s is up. "
Jay Vosburgh217df672005-09-26 16:11:50 -07001485 "This may be due to an out of date ifenslave.\n",
1486 slave_dev->name);
1487 res = -EPERM;
1488 goto err_undo_flags;
1489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Moni Shoua872254d2007-10-09 19:43:38 -07001491 /* set bonding device ether type by slave - bonding netdevices are
1492 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1493 * there is a need to override some of the type dependent attribs/funcs.
1494 *
1495 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1496 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1497 */
1498 if (bond->slave_cnt == 0) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001499 if (bond_dev->type != slave_dev->type) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001500 pr_debug("%s: change device type from %d to %d\n",
1501 bond_dev->name, bond_dev->type, slave_dev->type);
Moni Shoua75c78502009-09-15 02:37:40 -07001502
1503 netdev_bonding_change(bond_dev, NETDEV_BONDING_OLDTYPE);
1504
Moni Shouae36b9d12009-07-15 04:56:31 +00001505 if (slave_dev->type != ARPHRD_ETHER)
1506 bond_setup_by_slave(bond_dev, slave_dev);
1507 else
1508 ether_setup(bond_dev);
Moni Shoua75c78502009-09-15 02:37:40 -07001509
1510 netdev_bonding_change(bond_dev, NETDEV_BONDING_NEWTYPE);
Moni Shouae36b9d12009-07-15 04:56:31 +00001511 }
Moni Shoua872254d2007-10-09 19:43:38 -07001512 } else if (bond_dev->type != slave_dev->type) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001513 pr_err(DRV_NAME ": %s ether type (%d) is different "
Moni Shoua872254d2007-10-09 19:43:38 -07001514 "from other slaves (%d), can not enslave it.\n",
1515 slave_dev->name,
1516 slave_dev->type, bond_dev->type);
1517 res = -EINVAL;
1518 goto err_undo_flags;
1519 }
1520
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001521 if (slave_ops->ndo_set_mac_address == NULL) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001522 if (bond->slave_cnt == 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001523 pr_warning(DRV_NAME
Jay Vosburghdd957c52007-10-09 19:57:24 -07001524 ": %s: Warning: The first slave device "
1525 "specified does not support setting the MAC "
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001526 "address. Setting fail_over_mac to active.",
Jay Vosburghdd957c52007-10-09 19:57:24 -07001527 bond_dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001528 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1529 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001530 pr_err(DRV_NAME
Jay Vosburghdd957c52007-10-09 19:57:24 -07001531 ": %s: Error: The slave device specified "
1532 "does not support setting the MAC address, "
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001533 "but fail_over_mac is not set to active.\n"
Moni Shoua2ab82852007-10-09 19:43:39 -07001534 , bond_dev->name);
1535 res = -EOPNOTSUPP;
1536 goto err_undo_flags;
1537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539
Joe Jin243cb4e2007-02-06 14:16:40 -08001540 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (!new_slave) {
1542 res = -ENOMEM;
1543 goto err_undo_flags;
1544 }
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 /* save slave's original flags before calling
1547 * netdev_set_master and dev_open
1548 */
1549 new_slave->original_flags = slave_dev->flags;
1550
Jay Vosburgh217df672005-09-26 16:11:50 -07001551 /*
1552 * Save slave's original ("permanent") mac address for modes
1553 * that need it, and for restoring it upon release, and then
1554 * set it to the master's address
1555 */
1556 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Jay Vosburghdd957c52007-10-09 19:57:24 -07001558 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001559 /*
1560 * Set slave to master's mac address. The application already
1561 * set the master's mac address to that of the first slave
1562 */
1563 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1564 addr.sa_family = slave_dev->type;
1565 res = dev_set_mac_address(slave_dev, &addr);
1566 if (res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001567 pr_debug("Error %d calling set_mac_address\n", res);
Moni Shoua2ab82852007-10-09 19:43:39 -07001568 goto err_free;
1569 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001572 res = netdev_set_master(slave_dev, bond_dev);
1573 if (res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001574 pr_debug("Error %d calling netdev_set_master\n", res);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001575 goto err_restore_mac;
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001576 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001577 /* open the slave since the application closed it */
1578 res = dev_open(slave_dev);
1579 if (res) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001580 pr_debug("Opening slave %s failed\n", slave_dev->name);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001581 goto err_unset_master;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 new_slave->dev = slave_dev;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001585 slave_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Holger Eitzenberger58402052008-12-09 23:07:13 -08001587 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 /* bond_alb_init_slave() must be called before all other stages since
1589 * it might fail and we do not want to have to undo everything
1590 */
1591 res = bond_alb_init_slave(bond, new_slave);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001592 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001593 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
1595
1596 /* If the mode USES_PRIMARY, then the new slave gets the
1597 * master's promisc (and mc) settings only if it becomes the
1598 * curr_active_slave, and that is taken care of later when calling
1599 * bond_change_active()
1600 */
1601 if (!USES_PRIMARY(bond->params.mode)) {
1602 /* set promiscuity level to new slave */
1603 if (bond_dev->flags & IFF_PROMISC) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001604 res = dev_set_promiscuity(slave_dev, 1);
1605 if (res)
1606 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
1608
1609 /* set allmulti level to new slave */
1610 if (bond_dev->flags & IFF_ALLMULTI) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001611 res = dev_set_allmulti(slave_dev, 1);
1612 if (res)
1613 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
1615
David S. Millerb9e40852008-07-15 00:15:08 -07001616 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 /* upload master's mc_list to new slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001618 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next)
1619 dev_mc_add(slave_dev, dmi->dmi_addr,
1620 dmi->dmi_addrlen, 0);
David S. Millerb9e40852008-07-15 00:15:08 -07001621 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
1623
1624 if (bond->params.mode == BOND_MODE_8023AD) {
1625 /* add lacpdu mc addr to mc list */
1626 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1627
1628 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1629 }
1630
1631 bond_add_vlans_on_slave(bond, slave_dev);
1632
1633 write_lock_bh(&bond->lock);
1634
1635 bond_attach_slave(bond, new_slave);
1636
1637 new_slave->delay = 0;
1638 new_slave->link_failure_count = 0;
1639
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001640 bond_compute_features(bond);
1641
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001642 write_unlock_bh(&bond->lock);
1643
1644 read_lock(&bond->lock);
1645
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001646 new_slave->last_arp_rx = jiffies;
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (bond->params.miimon && !bond->params.use_carrier) {
1649 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1650
1651 if ((link_reporting == -1) && !bond->params.arp_interval) {
1652 /*
1653 * miimon is set but a bonded network driver
1654 * does not support ETHTOOL/MII and
1655 * arp_interval is not set. Note: if
1656 * use_carrier is enabled, we will never go
1657 * here (because netif_carrier is always
1658 * supported); thus, we don't need to change
1659 * the messages for netif_carrier.
1660 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001661 pr_warning(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001662 ": %s: Warning: MII and ETHTOOL support not "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 "available for interface %s, and "
1664 "arp_interval/arp_ip_target module parameters "
1665 "not specified, thus bonding will not detect "
1666 "link failures! see bonding.txt for details.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001667 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 } else if (link_reporting == -1) {
1669 /* unable get link status using mii/ethtool */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001670 pr_warning(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001671 ": %s: Warning: can't get link status from "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 "interface %s; the network driver associated "
1673 "with this interface does not support MII or "
1674 "ETHTOOL link status reporting, thus miimon "
1675 "has no effect on this interface.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001676 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
1678 }
1679
1680 /* check for initial state */
1681 if (!bond->params.miimon ||
1682 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1683 if (bond->params.updelay) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001684 pr_debug("Initial state of slave_dev is "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 "BOND_LINK_BACK\n");
1686 new_slave->link = BOND_LINK_BACK;
1687 new_slave->delay = bond->params.updelay;
1688 } else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001689 pr_debug("Initial state of slave_dev is "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 "BOND_LINK_UP\n");
1691 new_slave->link = BOND_LINK_UP;
1692 }
1693 new_slave->jiffies = jiffies;
1694 } else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001695 pr_debug("Initial state of slave_dev is "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 "BOND_LINK_DOWN\n");
1697 new_slave->link = BOND_LINK_DOWN;
1698 }
1699
1700 if (bond_update_speed_duplex(new_slave) &&
1701 (new_slave->link != BOND_LINK_DOWN)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001702 pr_warning(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001703 ": %s: Warning: failed to get speed and duplex from %s, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 "assumed to be 100Mb/sec and Full.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001705 bond_dev->name, new_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 if (bond->params.mode == BOND_MODE_8023AD) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001708 pr_warning(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001709 ": %s: Warning: Operation of 802.3ad mode requires ETHTOOL "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 "support in base driver for proper aggregator "
Mitch Williams4e0952c2005-11-09 10:34:57 -08001711 "selection.\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
1713 }
1714
1715 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1716 /* if there is a primary slave, remember it */
Jiri Pirkoa5499522009-09-25 03:28:09 +00001717 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 bond->primary_slave = new_slave;
Jiri Pirkoa5499522009-09-25 03:28:09 +00001719 bond->force_primary = true;
1720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001723 write_lock_bh(&bond->curr_slave_lock);
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 switch (bond->params.mode) {
1726 case BOND_MODE_ACTIVEBACKUP:
Jay Vosburgh8a8e4472006-09-22 21:56:15 -07001727 bond_set_slave_inactive_flags(new_slave);
1728 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 break;
1730 case BOND_MODE_8023AD:
1731 /* in 802.3ad mode, the internal mechanism
1732 * will activate the slaves in the selected
1733 * aggregator
1734 */
1735 bond_set_slave_inactive_flags(new_slave);
1736 /* if this is the first slave */
1737 if (bond->slave_cnt == 1) {
1738 SLAVE_AD_INFO(new_slave).id = 1;
1739 /* Initialize AD with the number of times that the AD timer is called in 1 second
1740 * can be called only after the mac address of the bond is set
1741 */
1742 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1743 bond->params.lacp_fast);
1744 } else {
1745 SLAVE_AD_INFO(new_slave).id =
1746 SLAVE_AD_INFO(new_slave->prev).id + 1;
1747 }
1748
1749 bond_3ad_bind_slave(new_slave);
1750 break;
1751 case BOND_MODE_TLB:
1752 case BOND_MODE_ALB:
1753 new_slave->state = BOND_STATE_ACTIVE;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001754 bond_set_slave_inactive_flags(new_slave);
Jiri Pirko5a29f782009-03-25 17:23:38 -07001755 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 break;
1757 default:
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001758 pr_debug("This slave is always active in trunk mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
1760 /* always active in trunk mode */
1761 new_slave->state = BOND_STATE_ACTIVE;
1762
1763 /* In trunking mode there is little meaning to curr_active_slave
1764 * anyway (it holds no special properties of the bond device),
1765 * so we can change it without calling change_active_interface()
1766 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001767 if (!bond->curr_active_slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 bond->curr_active_slave = new_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 break;
1771 } /* switch(bond_mode) */
1772
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001773 write_unlock_bh(&bond->curr_slave_lock);
1774
Jay Vosburghff59c452006-03-27 13:27:43 -08001775 bond_set_carrier(bond);
1776
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001777 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001779 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1780 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001781 goto err_close;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001782
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001783 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 ": %s: enslaving %s as a%s interface with a%s link.\n",
1785 bond_dev->name, slave_dev->name,
1786 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1787 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1788
1789 /* enslave is successful */
1790 return 0;
1791
1792/* Undo stages on error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793err_close:
1794 dev_close(slave_dev);
1795
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001796err_unset_master:
1797 netdev_set_master(slave_dev, NULL);
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799err_restore_mac:
Jay Vosburghdd957c52007-10-09 19:57:24 -07001800 if (!bond->params.fail_over_mac) {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001801 /* XXX TODO - fom follow mode needs to change master's
1802 * MAC if this slave's MAC is in use by the bond, or at
1803 * least print a warning.
1804 */
Moni Shoua2ab82852007-10-09 19:43:39 -07001805 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1806 addr.sa_family = slave_dev->type;
1807 dev_set_mac_address(slave_dev, &addr);
1808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810err_free:
1811 kfree(new_slave);
1812
1813err_undo_flags:
1814 bond_dev->features = old_features;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return res;
1817}
1818
1819/*
1820 * Try to release the slave device <slave> from the bond device <master>
1821 * It is legal to access curr_active_slave without a lock because all the function
1822 * is write-locked.
1823 *
1824 * The rules for slave state should be:
1825 * for Active/Backup:
1826 * Active stays on all backups go down
1827 * for Bonded connections:
1828 * The first up interface should be left on and all others downed.
1829 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001830int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
Wang Chen454d7c92008-11-12 23:37:49 -08001832 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 struct slave *slave, *oldcurrent;
1834 struct sockaddr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836 /* slave is not a slave or master is not master of this slave */
1837 if (!(slave_dev->flags & IFF_SLAVE) ||
1838 (slave_dev->master != bond_dev)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001839 pr_err(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001840 ": %s: Error: cannot release %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 bond_dev->name, slave_dev->name);
1842 return -EINVAL;
1843 }
1844
1845 write_lock_bh(&bond->lock);
1846
1847 slave = bond_get_slave_by_dev(bond, slave_dev);
1848 if (!slave) {
1849 /* not a slave of this bond */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001850 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 ": %s: %s not enslaved\n",
1852 bond_dev->name, slave_dev->name);
Jay Vosburghf5e2a7b2006-02-07 21:17:22 -08001853 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return -EINVAL;
1855 }
1856
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001857 if (!bond->params.fail_over_mac) {
Stephen Hemminger89c76c62009-08-28 12:05:13 +00001858 if (!compare_ether_addr(bond_dev->dev_addr, slave->perm_hwaddr)
1859 && bond->slave_cnt > 1)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001860 pr_warning(DRV_NAME
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001861 ": %s: Warning: the permanent HWaddr of %s - "
Johannes Berge1749612008-10-27 15:59:26 -07001862 "%pM - is still in use by %s. "
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001863 "Set the HWaddr of %s to a different address "
1864 "to avoid conflicts.\n",
1865 bond_dev->name, slave_dev->name,
Johannes Berge1749612008-10-27 15:59:26 -07001866 slave->perm_hwaddr,
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001867 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
1869
1870 /* Inform AD package of unbinding of slave. */
1871 if (bond->params.mode == BOND_MODE_8023AD) {
1872 /* must be called before the slave is
1873 * detached from the list
1874 */
1875 bond_3ad_unbind_slave(slave);
1876 }
1877
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001878 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 ": %s: releasing %s interface %s\n",
1880 bond_dev->name,
1881 (slave->state == BOND_STATE_ACTIVE)
1882 ? "active" : "backup",
1883 slave_dev->name);
1884
1885 oldcurrent = bond->curr_active_slave;
1886
1887 bond->current_arp_slave = NULL;
1888
1889 /* release the slave from its bond */
1890 bond_detach_slave(bond, slave);
1891
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001892 bond_compute_features(bond);
1893
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001894 if (bond->primary_slave == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 bond->primary_slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001897 if (oldcurrent == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 bond_change_active_slave(bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Holger Eitzenberger58402052008-12-09 23:07:13 -08001900 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 /* Must be called only after the slave has been
1902 * detached from the list and the curr_active_slave
1903 * has been cleared (if our_slave == old_current),
1904 * but before a new active slave is selected.
1905 */
Jay Vosburgh25433312008-01-17 16:24:59 -08001906 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 bond_alb_deinit_slave(bond, slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08001908 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
1910
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001911 if (oldcurrent == slave) {
1912 /*
1913 * Note that we hold RTNL over this sequence, so there
1914 * is no concern that another slave add/remove event
1915 * will interfere.
1916 */
1917 write_unlock_bh(&bond->lock);
1918 read_lock(&bond->lock);
1919 write_lock_bh(&bond->curr_slave_lock);
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 bond_select_active_slave(bond);
1922
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001923 write_unlock_bh(&bond->curr_slave_lock);
1924 read_unlock(&bond->lock);
1925 write_lock_bh(&bond->lock);
1926 }
1927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 if (bond->slave_cnt == 0) {
Jay Vosburghff59c452006-03-27 13:27:43 -08001929 bond_set_carrier(bond);
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 /* if the last slave was removed, zero the mac address
1932 * of the master so it will be set by the application
1933 * to the mac address of the first slave
1934 */
1935 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1936
1937 if (list_empty(&bond->vlan_list)) {
1938 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1939 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001940 pr_warning(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001941 ": %s: Warning: clearing HW address of %s while it "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 "still has VLANs.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001943 bond_dev->name, bond_dev->name);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001944 pr_warning(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001945 ": %s: When re-adding slaves, make sure the bond's "
1946 "HW address matches its VLANs'.\n",
1947 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
1949 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
1950 !bond_has_challenged_slaves(bond)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001951 pr_info(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001952 ": %s: last VLAN challenged slave %s "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 "left bond %s. VLAN blocking is removed\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001954 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1956 }
1957
1958 write_unlock_bh(&bond->lock);
1959
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001960 /* must do this from outside any spinlocks */
1961 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 bond_del_vlans_from_slave(bond, slave_dev);
1964
1965 /* If the mode USES_PRIMARY, then we should only remove its
1966 * promisc and mc settings if it was the curr_active_slave, but that was
1967 * already taken care of above when we detached the slave
1968 */
1969 if (!USES_PRIMARY(bond->params.mode)) {
1970 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001971 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001975 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07001979 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07001981 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
1983
1984 netdev_set_master(slave_dev, NULL);
1985
1986 /* close slave before restoring its mac address */
1987 dev_close(slave_dev);
1988
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001989 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001990 /* restore original ("permanent") mac address */
1991 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
1992 addr.sa_family = slave_dev->type;
1993 dev_set_mac_address(slave_dev, &addr);
1994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001996 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001997 IFF_SLAVE_INACTIVE | IFF_BONDING |
1998 IFF_SLAVE_NEEDARP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 kfree(slave);
2001
2002 return 0; /* deletion OK */
2003}
2004
2005/*
Moni Shouad90a1622007-10-09 19:43:43 -07002006* Destroy a bonding device.
2007* Must be under rtnl_lock when this function is called.
2008*/
Stephen Hemminger9e716262009-06-12 19:02:47 +00002009static void bond_uninit(struct net_device *bond_dev)
Jay Vosburgha434e432008-10-30 17:41:15 -07002010{
Wang Chen244ef9b2008-12-03 21:14:04 -08002011 struct bonding *bond = netdev_priv(bond_dev);
Jay Vosburgha434e432008-10-30 17:41:15 -07002012
Stephen Hemminger9e716262009-06-12 19:02:47 +00002013 bond_deinit(bond_dev);
2014 bond_destroy_sysfs_entry(bond);
2015
Jay Vosburgha434e432008-10-30 17:41:15 -07002016 if (bond->wq)
2017 destroy_workqueue(bond->wq);
2018
2019 netif_addr_lock_bh(bond_dev);
2020 bond_mc_list_destroy(bond);
2021 netif_addr_unlock_bh(bond_dev);
Jay Vosburgha434e432008-10-30 17:41:15 -07002022}
2023
Moni Shouad90a1622007-10-09 19:43:43 -07002024/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002025* First release a slave and than destroy the bond if no more slaves are left.
Moni Shouad90a1622007-10-09 19:43:43 -07002026* Must be under rtnl_lock when this function is called.
2027*/
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002028int bond_release_and_destroy(struct net_device *bond_dev,
2029 struct net_device *slave_dev)
Moni Shouad90a1622007-10-09 19:43:43 -07002030{
Wang Chen454d7c92008-11-12 23:37:49 -08002031 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002032 int ret;
2033
2034 ret = bond_release(bond_dev, slave_dev);
2035 if ((ret == 0) && (bond->slave_cnt == 0)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002036 pr_info(DRV_NAME ": %s: destroying bond %s.\n",
Moni Shouad90a1622007-10-09 19:43:43 -07002037 bond_dev->name, bond_dev->name);
Stephen Hemminger9e716262009-06-12 19:02:47 +00002038 unregister_netdevice(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002039 }
2040 return ret;
2041}
2042
2043/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 * This function releases all slaves.
2045 */
2046static int bond_release_all(struct net_device *bond_dev)
2047{
Wang Chen454d7c92008-11-12 23:37:49 -08002048 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 struct slave *slave;
2050 struct net_device *slave_dev;
2051 struct sockaddr addr;
2052
2053 write_lock_bh(&bond->lock);
2054
Jay Vosburghff59c452006-03-27 13:27:43 -08002055 netif_carrier_off(bond_dev);
2056
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002057 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 bond->current_arp_slave = NULL;
2061 bond->primary_slave = NULL;
2062 bond_change_active_slave(bond, NULL);
2063
2064 while ((slave = bond->first_slave) != NULL) {
2065 /* Inform AD package of unbinding of slave
2066 * before slave is detached from the list.
2067 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002068 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 bond_3ad_unbind_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 slave_dev = slave->dev;
2072 bond_detach_slave(bond, slave);
2073
Jay Vosburgh25433312008-01-17 16:24:59 -08002074 /* now that the slave is detached, unlock and perform
2075 * all the undo steps that should not be called from
2076 * within a lock.
2077 */
2078 write_unlock_bh(&bond->lock);
2079
Holger Eitzenberger58402052008-12-09 23:07:13 -08002080 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 /* must be called only after the slave
2082 * has been detached from the list
2083 */
2084 bond_alb_deinit_slave(bond, slave);
2085 }
2086
Arthur Kepner8531c5f2005-08-23 01:34:53 -04002087 bond_compute_features(bond);
2088
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002089 bond_destroy_slave_symlinks(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 bond_del_vlans_from_slave(bond, slave_dev);
2091
2092 /* If the mode USES_PRIMARY, then we should only remove its
2093 * promisc and mc settings if it was the curr_active_slave, but that was
2094 * already taken care of above when we detached the slave
2095 */
2096 if (!USES_PRIMARY(bond->params.mode)) {
2097 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002098 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002102 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002106 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002108 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
2110
2111 netdev_set_master(slave_dev, NULL);
2112
2113 /* close slave before restoring its mac address */
2114 dev_close(slave_dev);
2115
Jay Vosburghdd957c52007-10-09 19:57:24 -07002116 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002117 /* restore original ("permanent") mac address*/
2118 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2119 addr.sa_family = slave_dev->type;
2120 dev_set_mac_address(slave_dev, &addr);
2121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002123 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
2124 IFF_SLAVE_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 kfree(slave);
2127
2128 /* re-acquire the lock before getting the next slave */
2129 write_lock_bh(&bond->lock);
2130 }
2131
2132 /* zero the mac address of the master so it will be
2133 * set by the application to the mac address of the
2134 * first slave
2135 */
2136 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2137
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002138 if (list_empty(&bond->vlan_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002140 else {
2141 pr_warning(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08002142 ": %s: Warning: clearing HW address of %s while it "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 "still has VLANs.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08002144 bond_dev->name, bond_dev->name);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002145 pr_warning(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08002146 ": %s: When re-adding slaves, make sure the bond's "
2147 "HW address matches its VLANs'.\n",
2148 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
2150
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002151 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 ": %s: released all slaves\n",
2153 bond_dev->name);
2154
2155out:
2156 write_unlock_bh(&bond->lock);
2157
2158 return 0;
2159}
2160
2161/*
2162 * This function changes the active slave to slave <slave_dev>.
2163 * It returns -EINVAL in the following cases.
2164 * - <slave_dev> is not found in the list.
2165 * - There is not active slave now.
2166 * - <slave_dev> is already active.
2167 * - The link state of <slave_dev> is not BOND_LINK_UP.
2168 * - <slave_dev> is not running.
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002169 * In these cases, this function does nothing.
2170 * In the other cases, current_slave pointer is changed and 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 */
2172static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2173{
Wang Chen454d7c92008-11-12 23:37:49 -08002174 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 struct slave *old_active = NULL;
2176 struct slave *new_active = NULL;
2177 int res = 0;
2178
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002179 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 /* Verify that master_dev is indeed the master of slave_dev */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002183 if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002186 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002188 read_lock(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 old_active = bond->curr_active_slave;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002190 read_unlock(&bond->curr_slave_lock);
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 new_active = bond_get_slave_by_dev(bond, slave_dev);
2193
2194 /*
2195 * Changing to the current active: do nothing; return success.
2196 */
2197 if (new_active && (new_active == old_active)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002198 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 return 0;
2200 }
2201
2202 if ((new_active) &&
2203 (old_active) &&
2204 (new_active->link == BOND_LINK_UP) &&
2205 IS_UP(new_active->dev)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002206 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 bond_change_active_slave(bond, new_active);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002208 write_unlock_bh(&bond->curr_slave_lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002209 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002212 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 return res;
2215}
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2218{
Wang Chen454d7c92008-11-12 23:37:49 -08002219 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
2221 info->bond_mode = bond->params.mode;
2222 info->miimon = bond->params.miimon;
2223
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002224 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 info->num_slaves = bond->slave_cnt;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002226 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
2228 return 0;
2229}
2230
2231static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2232{
Wang Chen454d7c92008-11-12 23:37:49 -08002233 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 struct slave *slave;
Eric Dumazet689c96c2009-04-23 03:39:04 +00002235 int i, res = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002237 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239 bond_for_each_slave(bond, slave, i) {
2240 if (i == (int)info->slave_id) {
Eric Dumazet689c96c2009-04-23 03:39:04 +00002241 res = 0;
2242 strcpy(info->slave_name, slave->dev->name);
2243 info->link = slave->link;
2244 info->state = slave->state;
2245 info->link_failure_count = slave->link_failure_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 break;
2247 }
2248 }
2249
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002250 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Eric Dumazet689c96c2009-04-23 03:39:04 +00002252 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253}
2254
2255/*-------------------------------- Monitoring -------------------------------*/
2256
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002257
2258static int bond_miimon_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002260 struct slave *slave;
2261 int i, link_state, commit = 0;
Jiri Pirko41f89102009-04-24 03:57:29 +00002262 bool ignore_updelay;
2263
2264 ignore_updelay = !bond->curr_active_slave ? true : false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 bond_for_each_slave(bond, slave, i) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002267 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002269 link_state = bond_check_dev_link(bond, slave->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271 switch (slave->link) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002272 case BOND_LINK_UP:
2273 if (link_state)
2274 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002276 slave->link = BOND_LINK_FAIL;
2277 slave->delay = bond->params.downdelay;
2278 if (slave->delay) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002279 pr_info(DRV_NAME
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002280 ": %s: link status down for %s"
2281 "interface %s, disabling it in %d ms.\n",
2282 bond->dev->name,
2283 (bond->params.mode ==
2284 BOND_MODE_ACTIVEBACKUP) ?
2285 ((slave->state == BOND_STATE_ACTIVE) ?
2286 "active " : "backup ") : "",
2287 slave->dev->name,
2288 bond->params.downdelay * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002290 /*FALLTHRU*/
2291 case BOND_LINK_FAIL:
2292 if (link_state) {
2293 /*
2294 * recovered before downdelay expired
2295 */
2296 slave->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 slave->jiffies = jiffies;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002298 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 ": %s: link status up again after %d "
2300 "ms for interface %s.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002301 bond->dev->name,
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002302 (bond->params.downdelay - slave->delay) *
2303 bond->params.miimon,
2304 slave->dev->name);
2305 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002307
2308 if (slave->delay <= 0) {
2309 slave->new_link = BOND_LINK_DOWN;
2310 commit++;
2311 continue;
2312 }
2313
2314 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002317 case BOND_LINK_DOWN:
2318 if (!link_state)
2319 continue;
2320
2321 slave->link = BOND_LINK_BACK;
2322 slave->delay = bond->params.updelay;
2323
2324 if (slave->delay) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002325 pr_info(DRV_NAME
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002326 ": %s: link status up for "
2327 "interface %s, enabling it in %d ms.\n",
2328 bond->dev->name, slave->dev->name,
Jiri Pirko41f89102009-04-24 03:57:29 +00002329 ignore_updelay ? 0 :
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002330 bond->params.updelay *
2331 bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002333 /*FALLTHRU*/
2334 case BOND_LINK_BACK:
2335 if (!link_state) {
2336 slave->link = BOND_LINK_DOWN;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002337 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 ": %s: link status down again after %d "
2339 "ms for interface %s.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002340 bond->dev->name,
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002341 (bond->params.updelay - slave->delay) *
2342 bond->params.miimon,
2343 slave->dev->name);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002344
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002345 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002347
Jiri Pirko41f89102009-04-24 03:57:29 +00002348 if (ignore_updelay)
2349 slave->delay = 0;
2350
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002351 if (slave->delay <= 0) {
2352 slave->new_link = BOND_LINK_UP;
2353 commit++;
Jiri Pirko41f89102009-04-24 03:57:29 +00002354 ignore_updelay = false;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002355 continue;
2356 }
2357
2358 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 break;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002360 }
2361 }
2362
2363 return commit;
2364}
2365
2366static void bond_miimon_commit(struct bonding *bond)
2367{
2368 struct slave *slave;
2369 int i;
2370
2371 bond_for_each_slave(bond, slave, i) {
2372 switch (slave->new_link) {
2373 case BOND_LINK_NOCHANGE:
2374 continue;
2375
2376 case BOND_LINK_UP:
2377 slave->link = BOND_LINK_UP;
2378 slave->jiffies = jiffies;
2379
2380 if (bond->params.mode == BOND_MODE_8023AD) {
2381 /* prevent it from being the active one */
2382 slave->state = BOND_STATE_BACKUP;
2383 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2384 /* make it immediately active */
2385 slave->state = BOND_STATE_ACTIVE;
2386 } else if (slave != bond->primary_slave) {
2387 /* prevent it from being the active one */
2388 slave->state = BOND_STATE_BACKUP;
2389 }
2390
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002391 pr_info(DRV_NAME
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002392 ": %s: link status definitely "
2393 "up for interface %s.\n",
2394 bond->dev->name, slave->dev->name);
2395
2396 /* notify ad that the link status has changed */
2397 if (bond->params.mode == BOND_MODE_8023AD)
2398 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2399
Holger Eitzenberger58402052008-12-09 23:07:13 -08002400 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002401 bond_alb_handle_link_change(bond, slave,
2402 BOND_LINK_UP);
2403
2404 if (!bond->curr_active_slave ||
2405 (slave == bond->primary_slave))
2406 goto do_failover;
2407
2408 continue;
2409
2410 case BOND_LINK_DOWN:
Jay Vosburghfba4acd2008-10-30 17:41:14 -07002411 if (slave->link_failure_count < UINT_MAX)
2412 slave->link_failure_count++;
2413
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002414 slave->link = BOND_LINK_DOWN;
2415
2416 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2417 bond->params.mode == BOND_MODE_8023AD)
2418 bond_set_slave_inactive_flags(slave);
2419
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002420 pr_info(DRV_NAME
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002421 ": %s: link status definitely down for "
2422 "interface %s, disabling it\n",
2423 bond->dev->name, slave->dev->name);
2424
2425 if (bond->params.mode == BOND_MODE_8023AD)
2426 bond_3ad_handle_link_change(slave,
2427 BOND_LINK_DOWN);
2428
Jiri Pirkoae63e802009-05-27 05:42:36 +00002429 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002430 bond_alb_handle_link_change(bond, slave,
2431 BOND_LINK_DOWN);
2432
2433 if (slave == bond->curr_active_slave)
2434 goto do_failover;
2435
2436 continue;
2437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 default:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002439 pr_err(DRV_NAME
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002440 ": %s: invalid new link %d on slave %s\n",
2441 bond->dev->name, slave->new_link,
2442 slave->dev->name);
2443 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002445 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
2447
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002448do_failover:
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002449 ASSERT_RTNL();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002450 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 bond_select_active_slave(bond);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002452 write_unlock_bh(&bond->curr_slave_lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002453 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002454
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002455 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
2457
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002458/*
2459 * bond_mii_monitor
2460 *
2461 * Really a wrapper that splits the mii monitor into two phases: an
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002462 * inspection, then (if inspection indicates something needs to be done)
2463 * an acquisition of appropriate locks followed by a commit phase to
2464 * implement whatever link state changes are indicated.
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002465 */
2466void bond_mii_monitor(struct work_struct *work)
2467{
2468 struct bonding *bond = container_of(work, struct bonding,
2469 mii_work.work);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002470
2471 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002472 if (bond->kill_timers)
2473 goto out;
2474
2475 if (bond->slave_cnt == 0)
2476 goto re_arm;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002477
2478 if (bond->send_grat_arp) {
2479 read_lock(&bond->curr_slave_lock);
2480 bond_send_gratuitous_arp(bond);
2481 read_unlock(&bond->curr_slave_lock);
2482 }
2483
Brian Haley305d5522008-11-04 17:51:14 -08002484 if (bond->send_unsol_na) {
2485 read_lock(&bond->curr_slave_lock);
2486 bond_send_unsolicited_na(bond);
2487 read_unlock(&bond->curr_slave_lock);
2488 }
2489
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002490 if (bond_miimon_inspect(bond)) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002491 read_unlock(&bond->lock);
2492 rtnl_lock();
2493 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002494
2495 bond_miimon_commit(bond);
2496
Jay Vosburgh56556622008-01-17 16:25:03 -08002497 read_unlock(&bond->lock);
2498 rtnl_unlock(); /* might sleep, hold no other locks */
2499 read_lock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002500 }
2501
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002502re_arm:
2503 if (bond->params.miimon)
2504 queue_delayed_work(bond->wq, &bond->mii_work,
2505 msecs_to_jiffies(bond->params.miimon));
2506out:
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002507 read_unlock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002508}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002509
Al Virod3bb52b2007-08-22 20:06:58 -04002510static __be32 bond_glean_dev_ip(struct net_device *dev)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002511{
2512 struct in_device *idev;
2513 struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07002514 __be32 addr = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002515
2516 if (!dev)
2517 return 0;
2518
2519 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002520 idev = __in_dev_get_rcu(dev);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002521 if (!idev)
2522 goto out;
2523
2524 ifa = idev->ifa_list;
2525 if (!ifa)
2526 goto out;
2527
2528 addr = ifa->ifa_local;
2529out:
2530 rcu_read_unlock();
2531 return addr;
2532}
2533
Al Virod3bb52b2007-08-22 20:06:58 -04002534static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002535{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002536 struct vlan_entry *vlan;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002537
2538 if (ip == bond->master_ip)
2539 return 1;
2540
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002541 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002542 if (ip == vlan->vlan_ip)
2543 return 1;
2544 }
2545
2546 return 0;
2547}
2548
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002549/*
2550 * We go to the (large) trouble of VLAN tagging ARP frames because
2551 * switches in VLAN mode (especially if ports are configured as
2552 * "native" to a VLAN) might not pass non-tagged frames.
2553 */
Al Virod3bb52b2007-08-22 20:06:58 -04002554static 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 -04002555{
2556 struct sk_buff *skb;
2557
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002558 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002559 slave_dev->name, dest_ip, src_ip, vlan_id);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002560
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002561 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2562 NULL, slave_dev->dev_addr, NULL);
2563
2564 if (!skb) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002565 pr_err(DRV_NAME ": ARP packet allocation failed\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002566 return;
2567 }
2568 if (vlan_id) {
2569 skb = vlan_put_tag(skb, vlan_id);
2570 if (!skb) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002571 pr_err(DRV_NAME ": failed to insert VLAN tag\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002572 return;
2573 }
2574 }
2575 arp_xmit(skb);
2576}
2577
2578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2580{
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002581 int i, vlan_id, rv;
Al Virod3bb52b2007-08-22 20:06:58 -04002582 __be32 *targets = bond->params.arp_targets;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002583 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002584 struct net_device *vlan_dev;
2585 struct flowi fl;
2586 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Mitch Williams6b780562005-11-09 10:36:19 -08002588 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2589 if (!targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07002590 break;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002591 pr_debug("basa: target %x\n", targets[i]);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002592 if (list_empty(&bond->vlan_list)) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002593 pr_debug("basa: empty vlan: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002594 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2595 bond->master_ip, 0);
2596 continue;
2597 }
2598
2599 /*
2600 * If VLANs are configured, we do a route lookup to
2601 * determine which VLAN interface would be used, so we
2602 * can tag the ARP with the proper VLAN tag.
2603 */
2604 memset(&fl, 0, sizeof(fl));
2605 fl.fl4_dst = targets[i];
2606 fl.fl4_tos = RTO_ONLINK;
2607
Denis V. Lunevf2063512008-01-22 22:07:34 -08002608 rv = ip_route_output_key(&init_net, &rt, &fl);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002609 if (rv) {
2610 if (net_ratelimit()) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002611 pr_warning(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -07002612 ": %s: no route to arp_ip_target %pI4\n",
2613 bond->dev->name, &fl.fl4_dst);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002614 }
2615 continue;
2616 }
2617
2618 /*
2619 * This target is not on a VLAN
2620 */
2621 if (rt->u.dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002622 ip_rt_put(rt);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002623 pr_debug("basa: rtdev == bond->dev: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002624 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2625 bond->master_ip, 0);
2626 continue;
2627 }
2628
2629 vlan_id = 0;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002630 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002631 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002632 if (vlan_dev == rt->u.dst.dev) {
2633 vlan_id = vlan->vlan_id;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002634 pr_debug("basa: vlan match on %s %d\n",
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002635 vlan_dev->name, vlan_id);
2636 break;
2637 }
2638 }
2639
2640 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002641 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002642 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2643 vlan->vlan_ip, vlan_id);
2644 continue;
2645 }
2646
2647 if (net_ratelimit()) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002648 pr_warning(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -07002649 ": %s: no path to arp_ip_target %pI4 via rt.dev %s\n",
2650 bond->dev->name, &fl.fl4_dst,
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002651 rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2652 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002653 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002654 }
2655}
2656
2657/*
2658 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2659 * for each VLAN above us.
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002660 *
2661 * Caller must hold curr_slave_lock for read or better
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002662 */
2663static void bond_send_gratuitous_arp(struct bonding *bond)
2664{
2665 struct slave *slave = bond->curr_active_slave;
2666 struct vlan_entry *vlan;
2667 struct net_device *vlan_dev;
2668
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002669 pr_debug("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002670 slave ? slave->dev->name : "NULL");
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002671
2672 if (!slave || !bond->send_grat_arp ||
2673 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002674 return;
2675
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002676 bond->send_grat_arp--;
2677
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002678 if (bond->master_ip) {
2679 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
Moni Shoua1053f622007-10-09 19:43:42 -07002680 bond->master_ip, 0);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002681 }
2682
2683 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002684 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002685 if (vlan->vlan_ip) {
2686 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2687 vlan->vlan_ip, vlan->vlan_id);
2688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 }
2690}
2691
Al Virod3bb52b2007-08-22 20:06:58 -04002692static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002693{
2694 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002695 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002696
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002697 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002698 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
Harvey Harrison63779432008-10-31 00:56:00 -07002699 &sip, &tip, i, &targets[i], bond_has_this_ip(bond, tip));
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002700 if (sip == targets[i]) {
2701 if (bond_has_this_ip(bond, tip))
2702 slave->last_arp_rx = jiffies;
2703 return;
2704 }
2705 }
2706}
2707
2708static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2709{
2710 struct arphdr *arp;
2711 struct slave *slave;
2712 struct bonding *bond;
2713 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002714 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002715
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002716 if (dev_net(dev) != &init_net)
Eric W. Biedermane730c152007-09-17 11:53:39 -07002717 goto out;
2718
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002719 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2720 goto out;
2721
Wang Chen454d7c92008-11-12 23:37:49 -08002722 bond = netdev_priv(dev);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002723 read_lock(&bond->lock);
2724
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002725 pr_debug("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002726 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2727 orig_dev ? orig_dev->name : "NULL");
2728
2729 slave = bond_get_slave_by_dev(bond, orig_dev);
2730 if (!slave || !slave_do_arp_validate(bond, slave))
2731 goto out_unlock;
2732
Pavel Emelyanov988b7052008-03-03 12:20:57 -08002733 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002734 goto out_unlock;
2735
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002736 arp = arp_hdr(skb);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002737 if (arp->ar_hln != dev->addr_len ||
2738 skb->pkt_type == PACKET_OTHERHOST ||
2739 skb->pkt_type == PACKET_LOOPBACK ||
2740 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2741 arp->ar_pro != htons(ETH_P_IP) ||
2742 arp->ar_pln != 4)
2743 goto out_unlock;
2744
2745 arp_ptr = (unsigned char *)(arp + 1);
2746 arp_ptr += dev->addr_len;
2747 memcpy(&sip, arp_ptr, 4);
2748 arp_ptr += 4 + dev->addr_len;
2749 memcpy(&tip, arp_ptr, 4);
2750
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002751 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
Harvey Harrison63779432008-10-31 00:56:00 -07002752 bond->dev->name, slave->dev->name, slave->state,
2753 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2754 &sip, &tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002755
2756 /*
2757 * Backup slaves won't see the ARP reply, but do come through
2758 * here for each ARP probe (so we swap the sip/tip to validate
2759 * the probe). In a "redundant switch, common router" type of
2760 * configuration, the ARP probe will (hopefully) travel from
2761 * the active, through one switch, the router, then the other
2762 * switch before reaching the backup.
2763 */
2764 if (slave->state == BOND_STATE_ACTIVE)
2765 bond_validate_arp(bond, slave, sip, tip);
2766 else
2767 bond_validate_arp(bond, slave, tip, sip);
2768
2769out_unlock:
2770 read_unlock(&bond->lock);
2771out:
2772 dev_kfree_skb(skb);
2773 return NET_RX_SUCCESS;
2774}
2775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776/*
2777 * this function is called regularly to monitor each slave's link
2778 * ensuring that traffic is being sent and received when arp monitoring
2779 * is used in load-balancing mode. if the adapter has been dormant, then an
2780 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2781 * arp monitoring in active backup mode.
2782 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002783void bond_loadbalance_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002785 struct bonding *bond = container_of(work, struct bonding,
2786 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 struct slave *slave, *oldcurrent;
2788 int do_failover = 0;
2789 int delta_in_ticks;
2790 int i;
2791
2792 read_lock(&bond->lock);
2793
Jay Vosburgh5ce0da82008-05-17 21:10:07 -07002794 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002796 if (bond->kill_timers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002799 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
2802 read_lock(&bond->curr_slave_lock);
2803 oldcurrent = bond->curr_active_slave;
2804 read_unlock(&bond->curr_slave_lock);
2805
2806 /* see if any of the previous devices are up now (i.e. they have
2807 * xmt and rcv traffic). the curr_active_slave does not come into
2808 * the picture unless it is null. also, slave->jiffies is not needed
2809 * here because we send an arp on each slave and give a slave as
2810 * long as it needs to get the tx/rx within the delta.
2811 * TODO: what about up/down delay in arp mode? it wasn't here before
2812 * so it can wait
2813 */
2814 bond_for_each_slave(bond, slave, i) {
2815 if (slave->link != BOND_LINK_UP) {
Eric Dumazet9d214932009-05-17 20:55:16 -07002816 if (time_before_eq(jiffies, dev_trans_start(slave->dev) + delta_in_ticks) &&
David Sterbab63bb732007-12-06 23:40:33 -08002817 time_before_eq(jiffies, slave->dev->last_rx + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
2819 slave->link = BOND_LINK_UP;
2820 slave->state = BOND_STATE_ACTIVE;
2821
2822 /* primary_slave has no meaning in round-robin
2823 * mode. the window of a slave being up and
2824 * curr_active_slave being null after enslaving
2825 * is closed.
2826 */
2827 if (!oldcurrent) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002828 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 ": %s: link status definitely "
2830 "up for interface %s, ",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002831 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 slave->dev->name);
2833 do_failover = 1;
2834 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002835 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 ": %s: interface %s is now up\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002837 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 slave->dev->name);
2839 }
2840 }
2841 } else {
2842 /* slave->link == BOND_LINK_UP */
2843
2844 /* not all switches will respond to an arp request
2845 * when the source ip is 0, so don't take the link down
2846 * if we don't know our ip yet
2847 */
Eric Dumazet9d214932009-05-17 20:55:16 -07002848 if (time_after_eq(jiffies, dev_trans_start(slave->dev) + 2*delta_in_ticks) ||
Jay Vosburgh4b8a9232008-05-17 21:10:08 -07002849 (time_after_eq(jiffies, slave->dev->last_rx + 2*delta_in_ticks))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
2851 slave->link = BOND_LINK_DOWN;
2852 slave->state = BOND_STATE_BACKUP;
2853
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002854 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002857 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 ": %s: interface %s is now down.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002859 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 slave->dev->name);
2861
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002862 if (slave == oldcurrent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 do_failover = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 }
2865 }
2866
2867 /* note: if switch is in round-robin mode, all links
2868 * must tx arp to ensure all links rx an arp - otherwise
2869 * links may oscillate or not come up at all; if switch is
2870 * in something like xor mode, there is nothing we can
2871 * do - all replies will be rx'ed on same link causing slaves
2872 * to be unstable during low/no traffic periods
2873 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002874 if (IS_UP(slave->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 bond_arp_send_all(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 }
2877
2878 if (do_failover) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002879 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
2881 bond_select_active_slave(bond);
2882
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002883 write_unlock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 }
2885
2886re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002887 if (bond->params.arp_interval)
2888 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889out:
2890 read_unlock(&bond->lock);
2891}
2892
2893/*
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002894 * Called to inspect slaves for active-backup mode ARP monitor link state
2895 * changes. Sets new_link in slaves to specify what action should take
2896 * place for the slave. Returns 0 if no changes are found, >0 if changes
2897 * to link states must be committed.
2898 *
2899 * Called with bond->lock held for read.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002901static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 struct slave *slave;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002904 int i, commit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 bond_for_each_slave(bond, slave, i) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002907 slave->new_link = BOND_LINK_NOCHANGE;
2908
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 if (slave->link != BOND_LINK_UP) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002910 if (time_before_eq(jiffies, slave_last_rx(bond, slave) +
2911 delta_in_ticks)) {
2912 slave->new_link = BOND_LINK_UP;
2913 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002916 continue;
2917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002919 /*
2920 * Give slaves 2*delta after being enslaved or made
2921 * active. This avoids bouncing, as the last receive
2922 * times need a full ARP monitor cycle to be updated.
2923 */
2924 if (!time_after_eq(jiffies, slave->jiffies +
2925 2 * delta_in_ticks))
2926 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002928 /*
2929 * Backup slave is down if:
2930 * - No current_arp_slave AND
2931 * - more than 3*delta since last receive AND
2932 * - the bond has an IP address
2933 *
2934 * Note: a non-null current_arp_slave indicates
2935 * the curr_active_slave went down and we are
2936 * searching for a new one; under this condition
2937 * we only take the curr_active_slave down - this
2938 * gives each slave a chance to tx/rx traffic
2939 * before being taken out
2940 */
2941 if (slave->state == BOND_STATE_BACKUP &&
2942 !bond->current_arp_slave &&
2943 time_after(jiffies, slave_last_rx(bond, slave) +
2944 3 * delta_in_ticks)) {
2945 slave->new_link = BOND_LINK_DOWN;
2946 commit++;
2947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002949 /*
2950 * Active slave is down if:
2951 * - more than 2*delta since transmitting OR
2952 * - (more than 2*delta since receive AND
2953 * the bond has an IP address)
2954 */
2955 if ((slave->state == BOND_STATE_ACTIVE) &&
Eric Dumazet9d214932009-05-17 20:55:16 -07002956 (time_after_eq(jiffies, dev_trans_start(slave->dev) +
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002957 2 * delta_in_ticks) ||
2958 (time_after_eq(jiffies, slave_last_rx(bond, slave)
2959 + 2 * delta_in_ticks)))) {
2960 slave->new_link = BOND_LINK_DOWN;
2961 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 }
2963 }
2964
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002965 return commit;
2966}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002968/*
2969 * Called to commit link state changes noted by inspection step of
2970 * active-backup mode ARP monitor.
2971 *
2972 * Called with RTNL and bond->lock for read.
2973 */
2974static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
2975{
2976 struct slave *slave;
2977 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002979 bond_for_each_slave(bond, slave, i) {
2980 switch (slave->new_link) {
2981 case BOND_LINK_NOCHANGE:
2982 continue;
2983
2984 case BOND_LINK_UP:
Jiri Pirkob9f60252009-08-31 11:09:38 +00002985 if ((!bond->curr_active_slave &&
2986 time_before_eq(jiffies,
2987 dev_trans_start(slave->dev) +
2988 delta_in_ticks)) ||
2989 bond->curr_active_slave != slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002990 slave->link = BOND_LINK_UP;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002991 bond->current_arp_slave = NULL;
2992
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002993 pr_info(DRV_NAME
Jiri Pirkob9f60252009-08-31 11:09:38 +00002994 ": %s: link status definitely "
2995 "up for interface %s.\n",
2996 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002997
Jiri Pirkob9f60252009-08-31 11:09:38 +00002998 if (!bond->curr_active_slave ||
2999 (slave == bond->primary_slave))
3000 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003001
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003002 }
3003
Jiri Pirkob9f60252009-08-31 11:09:38 +00003004 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003005
3006 case BOND_LINK_DOWN:
3007 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003010 slave->link = BOND_LINK_DOWN;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003011 bond_set_slave_inactive_flags(slave);
3012
3013 pr_info(DRV_NAME
3014 ": %s: link status definitely down for "
3015 "interface %s, disabling it\n",
3016 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003017
3018 if (slave == bond->curr_active_slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003019 bond->current_arp_slave = NULL;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003020 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003021 }
Jiri Pirkob9f60252009-08-31 11:09:38 +00003022
3023 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003024
3025 default:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003026 pr_err(DRV_NAME
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003027 ": %s: impossible: new_link %d on slave %s\n",
3028 bond->dev->name, slave->new_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 slave->dev->name);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003030 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Jiri Pirkob9f60252009-08-31 11:09:38 +00003033do_failover:
3034 ASSERT_RTNL();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003035 write_lock_bh(&bond->curr_slave_lock);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003036 bond_select_active_slave(bond);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003037 write_unlock_bh(&bond->curr_slave_lock);
3038 }
3039
3040 bond_set_carrier(bond);
3041}
3042
3043/*
3044 * Send ARP probes for active-backup mode ARP monitor.
3045 *
3046 * Called with bond->lock held for read.
3047 */
3048static void bond_ab_arp_probe(struct bonding *bond)
3049{
3050 struct slave *slave;
3051 int i;
3052
3053 read_lock(&bond->curr_slave_lock);
3054
3055 if (bond->current_arp_slave && bond->curr_active_slave)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003056 pr_info(DRV_NAME "PROBE: c_arp %s && cas %s BAD\n",
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003057 bond->current_arp_slave->dev->name,
3058 bond->curr_active_slave->dev->name);
3059
3060 if (bond->curr_active_slave) {
3061 bond_arp_send_all(bond, bond->curr_active_slave);
3062 read_unlock(&bond->curr_slave_lock);
3063 return;
3064 }
3065
3066 read_unlock(&bond->curr_slave_lock);
3067
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 /* if we don't have a curr_active_slave, search for the next available
3069 * backup slave from the current_arp_slave and make it the candidate
3070 * for becoming the curr_active_slave
3071 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003072
3073 if (!bond->current_arp_slave) {
3074 bond->current_arp_slave = bond->first_slave;
3075 if (!bond->current_arp_slave)
3076 return;
3077 }
3078
3079 bond_set_slave_inactive_flags(bond->current_arp_slave);
3080
3081 /* search for next candidate */
3082 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3083 if (IS_UP(slave->dev)) {
3084 slave->link = BOND_LINK_BACK;
3085 bond_set_slave_active_flags(slave);
3086 bond_arp_send_all(bond, slave);
3087 slave->jiffies = jiffies;
3088 bond->current_arp_slave = slave;
3089 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 }
3091
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003092 /* if the link state is up at this point, we
3093 * mark it down - this can happen if we have
3094 * simultaneous link failures and
3095 * reselect_active_interface doesn't make this
3096 * one the current slave so it is still marked
3097 * up when it is actually down
3098 */
3099 if (slave->link == BOND_LINK_UP) {
3100 slave->link = BOND_LINK_DOWN;
3101 if (slave->link_failure_count < UINT_MAX)
3102 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003104 bond_set_slave_inactive_flags(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003106 pr_info(DRV_NAME
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003107 ": %s: backup interface %s is now down.\n",
3108 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 }
3110 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003111}
3112
3113void bond_activebackup_arp_mon(struct work_struct *work)
3114{
3115 struct bonding *bond = container_of(work, struct bonding,
3116 arp_work.work);
3117 int delta_in_ticks;
3118
3119 read_lock(&bond->lock);
3120
3121 if (bond->kill_timers)
3122 goto out;
3123
3124 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3125
3126 if (bond->slave_cnt == 0)
3127 goto re_arm;
3128
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003129 if (bond->send_grat_arp) {
3130 read_lock(&bond->curr_slave_lock);
3131 bond_send_gratuitous_arp(bond);
3132 read_unlock(&bond->curr_slave_lock);
3133 }
3134
Brian Haley305d5522008-11-04 17:51:14 -08003135 if (bond->send_unsol_na) {
3136 read_lock(&bond->curr_slave_lock);
3137 bond_send_unsolicited_na(bond);
3138 read_unlock(&bond->curr_slave_lock);
3139 }
3140
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003141 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3142 read_unlock(&bond->lock);
3143 rtnl_lock();
3144 read_lock(&bond->lock);
3145
3146 bond_ab_arp_commit(bond, delta_in_ticks);
3147
3148 read_unlock(&bond->lock);
3149 rtnl_unlock();
3150 read_lock(&bond->lock);
3151 }
3152
3153 bond_ab_arp_probe(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
3155re_arm:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003156 if (bond->params.arp_interval)
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003157 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158out:
3159 read_unlock(&bond->lock);
3160}
3161
3162/*------------------------------ proc/seq_file-------------------------------*/
3163
3164#ifdef CONFIG_PROC_FS
3165
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00003167 __acquires(&dev_base_lock)
3168 __acquires(&bond->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
3170 struct bonding *bond = seq->private;
3171 loff_t off = 0;
3172 struct slave *slave;
3173 int i;
3174
3175 /* make sure the bond won't be taken away */
3176 read_lock(&dev_base_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003177 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003179 if (*pos == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 return SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
3182 bond_for_each_slave(bond, slave, i) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003183 if (++off == *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 return slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 }
3186
3187 return NULL;
3188}
3189
3190static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3191{
3192 struct bonding *bond = seq->private;
3193 struct slave *slave = v;
3194
3195 ++*pos;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003196 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 return bond->first_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
3199 slave = slave->next;
3200
3201 return (slave == bond->first_slave) ? NULL : slave;
3202}
3203
3204static void bond_info_seq_stop(struct seq_file *seq, void *v)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00003205 __releases(&bond->lock)
3206 __releases(&dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207{
3208 struct bonding *bond = seq->private;
3209
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003210 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 read_unlock(&dev_base_lock);
3212}
3213
3214static void bond_info_show_master(struct seq_file *seq)
3215{
3216 struct bonding *bond = seq->private;
3217 struct slave *curr;
Mitch Williams4756b022005-11-09 10:36:25 -08003218 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
3220 read_lock(&bond->curr_slave_lock);
3221 curr = bond->curr_active_slave;
3222 read_unlock(&bond->curr_slave_lock);
3223
Jay Vosburghdd957c52007-10-09 19:57:24 -07003224 seq_printf(seq, "Bonding Mode: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 bond_mode_name(bond->params.mode));
3226
Jay Vosburghdd957c52007-10-09 19:57:24 -07003227 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
3228 bond->params.fail_over_mac)
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07003229 seq_printf(seq, " (fail_over_mac %s)",
3230 fail_over_mac_tbl[bond->params.fail_over_mac].modename);
Jay Vosburghdd957c52007-10-09 19:57:24 -07003231
3232 seq_printf(seq, "\n");
3233
Mitch Williamsc61b75a2005-11-09 10:35:13 -08003234 if (bond->params.mode == BOND_MODE_XOR ||
3235 bond->params.mode == BOND_MODE_8023AD) {
3236 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3237 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3238 bond->params.xmit_policy);
3239 }
3240
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 if (USES_PRIMARY(bond->params.mode)) {
Jiri Pirkoa5499522009-09-25 03:28:09 +00003242 seq_printf(seq, "Primary Slave: %s",
Mitch Williams0f418b22005-11-09 10:35:21 -08003243 (bond->primary_slave) ?
3244 bond->primary_slave->dev->name : "None");
Jiri Pirkoa5499522009-09-25 03:28:09 +00003245 if (bond->primary_slave)
3246 seq_printf(seq, " (primary_reselect %s)",
3247 pri_reselect_tbl[bond->params.primary_reselect].modename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
Jiri Pirkoa5499522009-09-25 03:28:09 +00003249 seq_printf(seq, "\nCurrently Active Slave: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 (curr) ? curr->dev->name : "None");
3251 }
3252
Jay Vosburghff59c452006-03-27 13:27:43 -08003253 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3254 "up" : "down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3256 seq_printf(seq, "Up Delay (ms): %d\n",
3257 bond->params.updelay * bond->params.miimon);
3258 seq_printf(seq, "Down Delay (ms): %d\n",
3259 bond->params.downdelay * bond->params.miimon);
3260
Mitch Williams4756b022005-11-09 10:36:25 -08003261
3262 /* ARP information */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003263 if (bond->params.arp_interval > 0) {
3264 int printed = 0;
Mitch Williams4756b022005-11-09 10:36:25 -08003265 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3266 bond->params.arp_interval);
3267
3268 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3269
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003270 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
Mitch Williams4756b022005-11-09 10:36:25 -08003271 if (!bond->params.arp_targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07003272 break;
Mitch Williams4756b022005-11-09 10:36:25 -08003273 if (printed)
3274 seq_printf(seq, ",");
Harvey Harrison8cf14e32008-10-29 22:43:33 -07003275 seq_printf(seq, " %pI4", &bond->params.arp_targets[i]);
Mitch Williams4756b022005-11-09 10:36:25 -08003276 printed = 1;
3277 }
3278 seq_printf(seq, "\n");
3279 }
3280
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 if (bond->params.mode == BOND_MODE_8023AD) {
3282 struct ad_info ad_info;
3283
3284 seq_puts(seq, "\n802.3ad info\n");
3285 seq_printf(seq, "LACP rate: %s\n",
3286 (bond->params.lacp_fast) ? "fast" : "slow");
Jay Vosburghfd989c82008-11-04 17:51:16 -08003287 seq_printf(seq, "Aggregator selection policy (ad_select): %s\n",
3288 ad_select_tbl[bond->params.ad_select].modename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
3290 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3291 seq_printf(seq, "bond %s has no active aggregator\n",
3292 bond->dev->name);
3293 } else {
3294 seq_printf(seq, "Active Aggregator Info:\n");
3295
3296 seq_printf(seq, "\tAggregator ID: %d\n",
3297 ad_info.aggregator_id);
3298 seq_printf(seq, "\tNumber of ports: %d\n",
3299 ad_info.ports);
3300 seq_printf(seq, "\tActor Key: %d\n",
3301 ad_info.actor_key);
3302 seq_printf(seq, "\tPartner Key: %d\n",
3303 ad_info.partner_key);
Johannes Berge1749612008-10-27 15:59:26 -07003304 seq_printf(seq, "\tPartner Mac Address: %pM\n",
3305 ad_info.partner_system);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 }
3307 }
3308}
3309
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003310static void bond_info_show_slave(struct seq_file *seq,
3311 const struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312{
3313 struct bonding *bond = seq->private;
3314
3315 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3316 seq_printf(seq, "MII Status: %s\n",
3317 (slave->link == BOND_LINK_UP) ? "up" : "down");
Kenzo Iwami655096452006-09-22 21:53:08 -07003318 seq_printf(seq, "Link Failure Count: %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 slave->link_failure_count);
3320
Johannes Berge1749612008-10-27 15:59:26 -07003321 seq_printf(seq, "Permanent HW addr: %pM\n", slave->perm_hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
3323 if (bond->params.mode == BOND_MODE_8023AD) {
3324 const struct aggregator *agg
3325 = SLAVE_AD_INFO(slave).port.aggregator;
3326
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003327 if (agg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 seq_printf(seq, "Aggregator ID: %d\n",
3329 agg->aggregator_identifier);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003330 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 seq_puts(seq, "Aggregator ID: N/A\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 }
3333}
3334
3335static int bond_info_seq_show(struct seq_file *seq, void *v)
3336{
3337 if (v == SEQ_START_TOKEN) {
3338 seq_printf(seq, "%s\n", version);
3339 bond_info_show_master(seq);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003340 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 bond_info_show_slave(seq, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
3343 return 0;
3344}
3345
Jan Engelhardt4101dec2009-01-14 13:52:18 -08003346static const struct seq_operations bond_info_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 .start = bond_info_seq_start,
3348 .next = bond_info_seq_next,
3349 .stop = bond_info_seq_stop,
3350 .show = bond_info_seq_show,
3351};
3352
3353static int bond_info_open(struct inode *inode, struct file *file)
3354{
3355 struct seq_file *seq;
3356 struct proc_dir_entry *proc;
3357 int res;
3358
3359 res = seq_open(file, &bond_info_seq_ops);
3360 if (!res) {
3361 /* recover the pointer buried in proc_dir_entry data */
3362 seq = file->private_data;
3363 proc = PDE(inode);
3364 seq->private = proc->data;
3365 }
3366
3367 return res;
3368}
3369
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08003370static const struct file_operations bond_info_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 .owner = THIS_MODULE,
3372 .open = bond_info_open,
3373 .read = seq_read,
3374 .llseek = seq_lseek,
3375 .release = seq_release,
3376};
3377
Nicolas de Pesloüan38fc0022009-10-13 00:45:06 -07003378static void bond_create_proc_entry(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379{
3380 struct net_device *bond_dev = bond->dev;
3381
3382 if (bond_proc_dir) {
Denis V. Luneva95609c2008-04-29 01:02:29 -07003383 bond->proc_entry = proc_create_data(bond_dev->name,
3384 S_IRUGO, bond_proc_dir,
3385 &bond_info_fops, bond);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003386 if (bond->proc_entry == NULL)
3387 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 ": Warning: Cannot create /proc/net/%s/%s\n",
3389 DRV_NAME, bond_dev->name);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003390 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393}
3394
3395static void bond_remove_proc_entry(struct bonding *bond)
3396{
3397 if (bond_proc_dir && bond->proc_entry) {
3398 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3399 memset(bond->proc_file_name, 0, IFNAMSIZ);
3400 bond->proc_entry = NULL;
3401 }
3402}
3403
3404/* Create the bonding directory under /proc/net, if doesn't exist yet.
3405 * Caller must hold rtnl_lock.
3406 */
3407static void bond_create_proc_dir(void)
3408{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 if (!bond_proc_dir) {
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003410 bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
Alexey Dobriyan99b76232009-03-25 22:48:06 +03003411 if (!bond_proc_dir)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003412 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 ": Warning: cannot create /proc/net/%s\n",
3414 DRV_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 }
3416}
3417
3418/* Destroy the bonding directory under /proc/net, if empty.
3419 * Caller must hold rtnl_lock.
3420 */
3421static void bond_destroy_proc_dir(void)
3422{
Alexey Dobriyan99b76232009-03-25 22:48:06 +03003423 if (bond_proc_dir) {
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003424 remove_proc_entry(DRV_NAME, init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 bond_proc_dir = NULL;
3426 }
3427}
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003428
3429#else /* !CONFIG_PROC_FS */
3430
Nicolas de Pesloüan38fc0022009-10-13 00:45:06 -07003431static void bond_create_proc_entry(struct bonding *bond)
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003432{
3433}
3434
3435static void bond_remove_proc_entry(struct bonding *bond)
3436{
3437}
3438
3439static void bond_create_proc_dir(void)
3440{
3441}
3442
3443static void bond_destroy_proc_dir(void)
3444{
3445}
3446
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447#endif /* CONFIG_PROC_FS */
3448
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003449
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450/*-------------------------- netdev event handling --------------------------*/
3451
3452/*
3453 * Change device name
3454 */
3455static int bond_event_changename(struct bonding *bond)
3456{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 bond_remove_proc_entry(bond);
3458 bond_create_proc_entry(bond);
Stephen Hemminger7e083842009-06-12 19:02:46 +00003459
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003460 bond_destroy_sysfs_entry(bond);
3461 bond_create_sysfs_entry(bond);
Stephen Hemminger7e083842009-06-12 19:02:46 +00003462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 return NOTIFY_DONE;
3464}
3465
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003466static int bond_master_netdev_event(unsigned long event,
3467 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468{
Wang Chen454d7c92008-11-12 23:37:49 -08003469 struct bonding *event_bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
3471 switch (event) {
3472 case NETDEV_CHANGENAME:
3473 return bond_event_changename(event_bond);
3474 case NETDEV_UNREGISTER:
Jay Vosburgh3b96c852008-01-17 16:25:00 -08003475 bond_release_all(event_bond->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 break;
3477 default:
3478 break;
3479 }
3480
3481 return NOTIFY_DONE;
3482}
3483
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003484static int bond_slave_netdev_event(unsigned long event,
3485 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486{
3487 struct net_device *bond_dev = slave_dev->master;
Wang Chen454d7c92008-11-12 23:37:49 -08003488 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
3490 switch (event) {
3491 case NETDEV_UNREGISTER:
3492 if (bond_dev) {
Jay Vosburgh1284cd32007-10-15 16:44:27 -07003493 if (bond->setup_by_slave)
3494 bond_release_and_destroy(bond_dev, slave_dev);
3495 else
3496 bond_release(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 }
3498 break;
3499 case NETDEV_CHANGE:
Jay Vosburgh17d04502009-03-18 18:38:25 -07003500 if (bond->params.mode == BOND_MODE_8023AD || bond_is_lb(bond)) {
3501 struct slave *slave;
3502
3503 slave = bond_get_slave_by_dev(bond, slave_dev);
3504 if (slave) {
3505 u16 old_speed = slave->speed;
3506 u16 old_duplex = slave->duplex;
3507
3508 bond_update_speed_duplex(slave);
3509
3510 if (bond_is_lb(bond))
3511 break;
3512
3513 if (old_speed != slave->speed)
3514 bond_3ad_adapter_speed_changed(slave);
3515 if (old_duplex != slave->duplex)
3516 bond_3ad_adapter_duplex_changed(slave);
3517 }
3518 }
3519
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 break;
3521 case NETDEV_DOWN:
3522 /*
3523 * ... Or is it this?
3524 */
3525 break;
3526 case NETDEV_CHANGEMTU:
3527 /*
3528 * TODO: Should slaves be allowed to
3529 * independently alter their MTU? For
3530 * an active-backup bond, slaves need
3531 * not be the same type of device, so
3532 * MTUs may vary. For other modes,
3533 * slaves arguably should have the
3534 * same MTUs. To do this, we'd need to
3535 * take over the slave's change_mtu
3536 * function for the duration of their
3537 * servitude.
3538 */
3539 break;
3540 case NETDEV_CHANGENAME:
3541 /*
3542 * TODO: handle changing the primary's name
3543 */
3544 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003545 case NETDEV_FEAT_CHANGE:
3546 bond_compute_features(bond);
3547 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 default:
3549 break;
3550 }
3551
3552 return NOTIFY_DONE;
3553}
3554
3555/*
3556 * bond_netdev_event: handle netdev notifier chain events.
3557 *
3558 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003559 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 * locks for us to safely manipulate the slave devices (RTNL lock,
3561 * dev_probe_lock).
3562 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003563static int bond_netdev_event(struct notifier_block *this,
3564 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565{
3566 struct net_device *event_dev = (struct net_device *)ptr;
3567
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003568 if (dev_net(event_dev) != &init_net)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02003569 return NOTIFY_DONE;
3570
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003571 pr_debug("event_dev: %s, event: %lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 (event_dev ? event_dev->name : "None"),
3573 event);
3574
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003575 if (!(event_dev->priv_flags & IFF_BONDING))
3576 return NOTIFY_DONE;
3577
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 if (event_dev->flags & IFF_MASTER) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003579 pr_debug("IFF_MASTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 return bond_master_netdev_event(event, event_dev);
3581 }
3582
3583 if (event_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003584 pr_debug("IFF_SLAVE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 return bond_slave_netdev_event(event, event_dev);
3586 }
3587
3588 return NOTIFY_DONE;
3589}
3590
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003591/*
3592 * bond_inetaddr_event: handle inetaddr notifier chain events.
3593 *
3594 * We keep track of device IPs primarily to use as source addresses in
3595 * ARP monitor probes (rather than spewing out broadcasts all the time).
3596 *
3597 * We track one IP for the main device (if it has one), plus one per VLAN.
3598 */
3599static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3600{
3601 struct in_ifaddr *ifa = ptr;
3602 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003603 struct bonding *bond;
3604 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003605
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003606 if (dev_net(ifa->ifa_dev->dev) != &init_net)
Denis V. Lunev6133fb12008-02-28 20:46:17 -08003607 return NOTIFY_DONE;
3608
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003609 list_for_each_entry(bond, &bond_dev_list, bond_list) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003610 if (bond->dev == event_dev) {
3611 switch (event) {
3612 case NETDEV_UP:
3613 bond->master_ip = ifa->ifa_local;
3614 return NOTIFY_OK;
3615 case NETDEV_DOWN:
3616 bond->master_ip = bond_glean_dev_ip(bond->dev);
3617 return NOTIFY_OK;
3618 default:
3619 return NOTIFY_DONE;
3620 }
3621 }
3622
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003623 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08003624 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003625 if (vlan_dev == event_dev) {
3626 switch (event) {
3627 case NETDEV_UP:
3628 vlan->vlan_ip = ifa->ifa_local;
3629 return NOTIFY_OK;
3630 case NETDEV_DOWN:
3631 vlan->vlan_ip =
3632 bond_glean_dev_ip(vlan_dev);
3633 return NOTIFY_OK;
3634 default:
3635 return NOTIFY_DONE;
3636 }
3637 }
3638 }
3639 }
3640 return NOTIFY_DONE;
3641}
3642
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643static struct notifier_block bond_netdev_notifier = {
3644 .notifier_call = bond_netdev_event,
3645};
3646
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003647static struct notifier_block bond_inetaddr_notifier = {
3648 .notifier_call = bond_inetaddr_event,
3649};
3650
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651/*-------------------------- Packet type handling ---------------------------*/
3652
3653/* register to receive lacpdus on a bond */
3654static void bond_register_lacpdu(struct bonding *bond)
3655{
3656 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3657
3658 /* initialize packet type */
3659 pk_type->type = PKT_TYPE_LACPDU;
3660 pk_type->dev = bond->dev;
3661 pk_type->func = bond_3ad_lacpdu_recv;
3662
3663 dev_add_pack(pk_type);
3664}
3665
3666/* unregister to receive lacpdus on a bond */
3667static void bond_unregister_lacpdu(struct bonding *bond)
3668{
3669 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3670}
3671
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003672void bond_register_arp(struct bonding *bond)
3673{
3674 struct packet_type *pt = &bond->arp_mon_pt;
3675
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003676 if (pt->type)
3677 return;
3678
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003679 pt->type = htons(ETH_P_ARP);
Jay Vosburghe245cb72007-02-28 17:03:27 -08003680 pt->dev = bond->dev;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003681 pt->func = bond_arp_rcv;
3682 dev_add_pack(pt);
3683}
3684
3685void bond_unregister_arp(struct bonding *bond)
3686{
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003687 struct packet_type *pt = &bond->arp_mon_pt;
3688
3689 dev_remove_pack(pt);
3690 pt->type = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003691}
3692
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003693/*---------------------------- Hashing Policies -----------------------------*/
3694
3695/*
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003696 * Hash for the output device based upon layer 2 and layer 3 data. If
3697 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3698 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003699static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003700{
3701 struct ethhdr *data = (struct ethhdr *)skb->data;
3702 struct iphdr *iph = ip_hdr(skb);
3703
Brian Haleyf14c4e42008-09-02 10:08:08 -04003704 if (skb->protocol == htons(ETH_P_IP)) {
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003705 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
Jasper Spaansd3da6832009-10-23 04:08:46 +00003706 (data->h_dest[5] ^ data->h_source[5])) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003707 }
3708
Jasper Spaansd3da6832009-10-23 04:08:46 +00003709 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003710}
3711
3712/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003713 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003714 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3715 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3716 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003717static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003718{
3719 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003720 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003721 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003722 int layer4_xor = 0;
3723
Brian Haleyf14c4e42008-09-02 10:08:08 -04003724 if (skb->protocol == htons(ETH_P_IP)) {
3725 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003726 (iph->protocol == IPPROTO_TCP ||
3727 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003728 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003729 }
3730 return (layer4_xor ^
3731 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3732
3733 }
3734
Jasper Spaansd3da6832009-10-23 04:08:46 +00003735 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003736}
3737
3738/*
3739 * Hash for the output device based upon layer 2 data
3740 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003741static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003742{
3743 struct ethhdr *data = (struct ethhdr *)skb->data;
3744
Jasper Spaansd3da6832009-10-23 04:08:46 +00003745 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003746}
3747
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748/*-------------------------- Device entry points ----------------------------*/
3749
3750static int bond_open(struct net_device *bond_dev)
3751{
Wang Chen454d7c92008-11-12 23:37:49 -08003752 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
3754 bond->kill_timers = 0;
3755
Holger Eitzenberger58402052008-12-09 23:07:13 -08003756 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 /* bond_alb_initialize must be called before the timer
3758 * is started.
3759 */
3760 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3761 /* something went wrong - fail the open operation */
3762 return -1;
3763 }
3764
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003765 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3766 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 }
3768
3769 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003770 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3771 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 }
3773
3774 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003775 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3776 INIT_DELAYED_WORK(&bond->arp_work,
3777 bond_activebackup_arp_mon);
3778 else
3779 INIT_DELAYED_WORK(&bond->arp_work,
3780 bond_loadbalance_arp_mon);
3781
3782 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003783 if (bond->params.arp_validate)
3784 bond_register_arp(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 }
3786
3787 if (bond->params.mode == BOND_MODE_8023AD) {
Adrian Bunka40745f2007-10-24 18:27:43 +02003788 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003789 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 /* register to receive LACPDUs */
3791 bond_register_lacpdu(bond);
Jay Vosburghfd989c82008-11-04 17:51:16 -08003792 bond_3ad_initiate_agg_selection(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 }
3794
3795 return 0;
3796}
3797
3798static int bond_close(struct net_device *bond_dev)
3799{
Wang Chen454d7c92008-11-12 23:37:49 -08003800 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
3802 if (bond->params.mode == BOND_MODE_8023AD) {
3803 /* Unregister the receive of LACPDUs */
3804 bond_unregister_lacpdu(bond);
3805 }
3806
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003807 if (bond->params.arp_validate)
3808 bond_unregister_arp(bond);
3809
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 write_lock_bh(&bond->lock);
3811
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003812 bond->send_grat_arp = 0;
Brian Haley305d5522008-11-04 17:51:14 -08003813 bond->send_unsol_na = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
3815 /* signal timers not to re-arm */
3816 bond->kill_timers = 1;
3817
3818 write_unlock_bh(&bond->lock);
3819
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003821 cancel_delayed_work(&bond->mii_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 }
3823
3824 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003825 cancel_delayed_work(&bond->arp_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 }
3827
3828 switch (bond->params.mode) {
3829 case BOND_MODE_8023AD:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003830 cancel_delayed_work(&bond->ad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 break;
3832 case BOND_MODE_TLB:
3833 case BOND_MODE_ALB:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003834 cancel_delayed_work(&bond->alb_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 break;
3836 default:
3837 break;
3838 }
3839
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840
Holger Eitzenberger58402052008-12-09 23:07:13 -08003841 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 /* Must be called only after all
3843 * slaves have been released
3844 */
3845 bond_alb_deinitialize(bond);
3846 }
3847
3848 return 0;
3849}
3850
3851static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3852{
Wang Chen454d7c92008-11-12 23:37:49 -08003853 struct bonding *bond = netdev_priv(bond_dev);
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08003854 struct net_device_stats *stats = &bond->stats;
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003855 struct net_device_stats local_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 struct slave *slave;
3857 int i;
3858
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003859 memset(&local_stats, 0, sizeof(struct net_device_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
3861 read_lock_bh(&bond->lock);
3862
3863 bond_for_each_slave(bond, slave, i) {
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08003864 const struct net_device_stats *sstats = dev_get_stats(slave->dev);
3865
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003866 local_stats.rx_packets += sstats->rx_packets;
3867 local_stats.rx_bytes += sstats->rx_bytes;
3868 local_stats.rx_errors += sstats->rx_errors;
3869 local_stats.rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003871 local_stats.tx_packets += sstats->tx_packets;
3872 local_stats.tx_bytes += sstats->tx_bytes;
3873 local_stats.tx_errors += sstats->tx_errors;
3874 local_stats.tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003876 local_stats.multicast += sstats->multicast;
3877 local_stats.collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003879 local_stats.rx_length_errors += sstats->rx_length_errors;
3880 local_stats.rx_over_errors += sstats->rx_over_errors;
3881 local_stats.rx_crc_errors += sstats->rx_crc_errors;
3882 local_stats.rx_frame_errors += sstats->rx_frame_errors;
3883 local_stats.rx_fifo_errors += sstats->rx_fifo_errors;
3884 local_stats.rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003886 local_stats.tx_aborted_errors += sstats->tx_aborted_errors;
3887 local_stats.tx_carrier_errors += sstats->tx_carrier_errors;
3888 local_stats.tx_fifo_errors += sstats->tx_fifo_errors;
3889 local_stats.tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3890 local_stats.tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 }
3892
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003893 memcpy(stats, &local_stats, sizeof(struct net_device_stats));
3894
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 read_unlock_bh(&bond->lock);
3896
3897 return stats;
3898}
3899
3900static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3901{
3902 struct net_device *slave_dev = NULL;
3903 struct ifbond k_binfo;
3904 struct ifbond __user *u_binfo = NULL;
3905 struct ifslave k_sinfo;
3906 struct ifslave __user *u_sinfo = NULL;
3907 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 int res = 0;
3909
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003910 pr_debug("bond_ioctl: master=%s, cmd=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 bond_dev->name, cmd);
3912
3913 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 case SIOCGMIIPHY:
3915 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003916 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003918
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 mii->phy_id = 0;
3920 /* Fall Through */
3921 case SIOCGMIIREG:
3922 /*
3923 * We do this again just in case we were called by SIOCGMIIREG
3924 * instead of SIOCGMIIPHY.
3925 */
3926 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003927 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003929
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
3931 if (mii->reg_num == 1) {
Wang Chen454d7c92008-11-12 23:37:49 -08003932 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 mii->val_out = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003934 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 read_lock(&bond->curr_slave_lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003936 if (netif_carrier_ok(bond->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 mii->val_out = BMSR_LSTATUS;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003938
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 read_unlock(&bond->curr_slave_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003940 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 }
3942
3943 return 0;
3944 case BOND_INFO_QUERY_OLD:
3945 case SIOCBONDINFOQUERY:
3946 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3947
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003948 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950
3951 res = bond_info_query(bond_dev, &k_binfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003952 if (res == 0 &&
3953 copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
3954 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
3956 return res;
3957 case BOND_SLAVE_INFO_QUERY_OLD:
3958 case SIOCBONDSLAVEINFOQUERY:
3959 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3960
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003961 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
3964 res = bond_slave_info_query(bond_dev, &k_sinfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003965 if (res == 0 &&
3966 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
3967 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968
3969 return res;
3970 default:
3971 /* Go on */
3972 break;
3973 }
3974
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003975 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977
Eric W. Biederman881d9662007-09-17 11:56:21 -07003978 slave_dev = dev_get_by_name(&init_net, ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003980 pr_debug("slave_dev=%p: \n", slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003982 if (!slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 res = -ENODEV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003984 else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003985 pr_debug("slave_dev->name=%s: \n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 switch (cmd) {
3987 case BOND_ENSLAVE_OLD:
3988 case SIOCBONDENSLAVE:
3989 res = bond_enslave(bond_dev, slave_dev);
3990 break;
3991 case BOND_RELEASE_OLD:
3992 case SIOCBONDRELEASE:
3993 res = bond_release(bond_dev, slave_dev);
3994 break;
3995 case BOND_SETHWADDR_OLD:
3996 case SIOCBONDSETHWADDR:
3997 res = bond_sethwaddr(bond_dev, slave_dev);
3998 break;
3999 case BOND_CHANGE_ACTIVE_OLD:
4000 case SIOCBONDCHANGEACTIVE:
4001 res = bond_ioctl_change_active(bond_dev, slave_dev);
4002 break;
4003 default:
4004 res = -EOPNOTSUPP;
4005 }
4006
4007 dev_put(slave_dev);
4008 }
4009
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 return res;
4011}
4012
4013static void bond_set_multicast_list(struct net_device *bond_dev)
4014{
Wang Chen454d7c92008-11-12 23:37:49 -08004015 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 struct dev_mc_list *dmi;
4017
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 /*
4019 * Do promisc before checking multicast_mode
4020 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004021 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07004022 /*
4023 * FIXME: Need to handle the error when one of the multi-slaves
4024 * encounters error.
4025 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 bond_set_promiscuity(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004028
4029 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 bond_set_promiscuity(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004031
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
4033 /* set allmulti flag to slaves */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004034 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07004035 /*
4036 * FIXME: Need to handle the error when one of the multi-slaves
4037 * encounters error.
4038 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 bond_set_allmulti(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004041
4042 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 bond_set_allmulti(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004044
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004046 read_lock(&bond->lock);
4047
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 bond->flags = bond_dev->flags;
4049
4050 /* looking for addresses to add to slaves' mc list */
4051 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004052 if (!bond_mc_list_find_dmi(dmi, bond->mc_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 }
4055
4056 /* looking for addresses to delete from slaves' list */
4057 for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004058 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 }
4061
4062 /* save master's multicast list */
4063 bond_mc_list_destroy(bond);
4064 bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
4065
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004066 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067}
4068
Stephen Hemminger00829822008-11-20 20:14:53 -08004069static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
4070{
4071 struct bonding *bond = netdev_priv(dev);
4072 struct slave *slave = bond->first_slave;
4073
4074 if (slave) {
4075 const struct net_device_ops *slave_ops
4076 = slave->dev->netdev_ops;
4077 if (slave_ops->ndo_neigh_setup)
Patrick McHardy72e22402009-03-05 01:57:44 -08004078 return slave_ops->ndo_neigh_setup(slave->dev, parms);
Stephen Hemminger00829822008-11-20 20:14:53 -08004079 }
4080 return 0;
4081}
4082
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083/*
4084 * Change the MTU of all of a master's slaves to match the master
4085 */
4086static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
4087{
Wang Chen454d7c92008-11-12 23:37:49 -08004088 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 struct slave *slave, *stop_at;
4090 int res = 0;
4091 int i;
4092
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004093 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 (bond_dev ? bond_dev->name : "None"), new_mtu);
4095
4096 /* Can't hold bond->lock with bh disabled here since
4097 * some base drivers panic. On the other hand we can't
4098 * hold bond->lock without bh disabled because we'll
4099 * deadlock. The only solution is to rely on the fact
4100 * that we're under rtnl_lock here, and the slaves
4101 * list won't change. This doesn't solve the problem
4102 * of setting the slave's MTU while it is
4103 * transmitting, but the assumption is that the base
4104 * driver can handle that.
4105 *
4106 * TODO: figure out a way to safely iterate the slaves
4107 * list, but without holding a lock around the actual
4108 * call to the base driver.
4109 */
4110
4111 bond_for_each_slave(bond, slave, i) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004112 pr_debug("s %p s->p %p c_m %p\n", slave,
Stephen Hemminger53a32942009-01-06 10:41:56 -08004113 slave->prev, slave->dev->netdev_ops->ndo_change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08004114
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 res = dev_set_mtu(slave->dev, new_mtu);
4116
4117 if (res) {
4118 /* If we failed to set the slave's mtu to the new value
4119 * we must abort the operation even in ACTIVE_BACKUP
4120 * mode, because if we allow the backup slaves to have
4121 * different mtu values than the active slave we'll
4122 * need to change their mtu when doing a failover. That
4123 * means changing their mtu from timer context, which
4124 * is probably not a good idea.
4125 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004126 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 goto unwind;
4128 }
4129 }
4130
4131 bond_dev->mtu = new_mtu;
4132
4133 return 0;
4134
4135unwind:
4136 /* unwind from head to the slave that failed */
4137 stop_at = slave;
4138 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4139 int tmp_res;
4140
4141 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4142 if (tmp_res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004143 pr_debug("unwind err %d dev %s\n", tmp_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 slave->dev->name);
4145 }
4146 }
4147
4148 return res;
4149}
4150
4151/*
4152 * Change HW address
4153 *
4154 * Note that many devices must be down to change the HW address, and
4155 * downing the master releases all slaves. We can make bonds full of
4156 * bonding devices to test this, however.
4157 */
4158static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4159{
Wang Chen454d7c92008-11-12 23:37:49 -08004160 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 struct sockaddr *sa = addr, tmp_sa;
4162 struct slave *slave, *stop_at;
4163 int res = 0;
4164 int i;
4165
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004166 if (bond->params.mode == BOND_MODE_ALB)
4167 return bond_alb_set_mac_address(bond_dev, addr);
4168
4169
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004170 pr_debug("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
Jay Vosburghdd957c52007-10-09 19:57:24 -07004172 /*
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004173 * If fail_over_mac is set to active, do nothing and return
4174 * success. Returning an error causes ifenslave to fail.
Jay Vosburghdd957c52007-10-09 19:57:24 -07004175 */
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004176 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
Jay Vosburghdd957c52007-10-09 19:57:24 -07004177 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07004178
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004179 if (!is_valid_ether_addr(sa->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
4182 /* Can't hold bond->lock with bh disabled here since
4183 * some base drivers panic. On the other hand we can't
4184 * hold bond->lock without bh disabled because we'll
4185 * deadlock. The only solution is to rely on the fact
4186 * that we're under rtnl_lock here, and the slaves
4187 * list won't change. This doesn't solve the problem
4188 * of setting the slave's hw address while it is
4189 * transmitting, but the assumption is that the base
4190 * driver can handle that.
4191 *
4192 * TODO: figure out a way to safely iterate the slaves
4193 * list, but without holding a lock around the actual
4194 * call to the base driver.
4195 */
4196
4197 bond_for_each_slave(bond, slave, i) {
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004198 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004199 pr_debug("slave %p %s\n", slave, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004201 if (slave_ops->ndo_set_mac_address == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 res = -EOPNOTSUPP;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004203 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 goto unwind;
4205 }
4206
4207 res = dev_set_mac_address(slave->dev, addr);
4208 if (res) {
4209 /* TODO: consider downing the slave
4210 * and retry ?
4211 * User should expect communications
4212 * breakage anyway until ARP finish
4213 * updating, so...
4214 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004215 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 goto unwind;
4217 }
4218 }
4219
4220 /* success */
4221 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4222 return 0;
4223
4224unwind:
4225 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4226 tmp_sa.sa_family = bond_dev->type;
4227
4228 /* unwind from head to the slave that failed */
4229 stop_at = slave;
4230 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4231 int tmp_res;
4232
4233 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4234 if (tmp_res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004235 pr_debug("unwind err %d dev %s\n", tmp_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 slave->dev->name);
4237 }
4238 }
4239
4240 return res;
4241}
4242
4243static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4244{
Wang Chen454d7c92008-11-12 23:37:49 -08004245 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 struct slave *slave, *start_at;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004247 int i, slave_no, res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
4249 read_lock(&bond->lock);
4250
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004251 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004254 /*
4255 * Concurrent TX may collide on rr_tx_counter; we accept that
4256 * as being rare enough not to justify using an atomic op here
4257 */
4258 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004260 bond_for_each_slave(bond, slave, i) {
4261 slave_no--;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004262 if (slave_no < 0)
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004263 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 }
4265
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004266 start_at = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 bond_for_each_slave_from(bond, slave, i, start_at) {
4268 if (IS_UP(slave->dev) &&
4269 (slave->link == BOND_LINK_UP) &&
4270 (slave->state == BOND_STATE_ACTIVE)) {
4271 res = bond_dev_queue_xmit(bond, skb, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 break;
4273 }
4274 }
4275
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276out:
4277 if (res) {
4278 /* no suitable interface, frame not sent */
4279 dev_kfree_skb(skb);
4280 }
4281 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004282 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283}
4284
John W. Linville075897c2005-09-28 17:50:53 -04004285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286/*
4287 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4288 * the bond has a usable interface.
4289 */
4290static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4291{
Wang Chen454d7c92008-11-12 23:37:49 -08004292 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 int res = 1;
4294
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 read_lock(&bond->lock);
4296 read_lock(&bond->curr_slave_lock);
4297
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004298 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
John W. Linville075897c2005-09-28 17:50:53 -04004301 if (!bond->curr_active_slave)
4302 goto out;
4303
John W. Linville075897c2005-09-28 17:50:53 -04004304 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4305
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004307 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 /* no suitable interface, frame not sent */
4309 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004310
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 read_unlock(&bond->curr_slave_lock);
4312 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004313 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314}
4315
4316/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004317 * In bond_xmit_xor() , we determine the output device by using a pre-
4318 * determined xmit_hash_policy(), If the selected device is not enabled,
4319 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 */
4321static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4322{
Wang Chen454d7c92008-11-12 23:37:49 -08004323 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 struct slave *slave, *start_at;
4325 int slave_no;
4326 int i;
4327 int res = 1;
4328
4329 read_lock(&bond->lock);
4330
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004331 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
Jasper Spaansa361c832009-10-23 04:09:24 +00004334 slave_no = bond->xmit_hash_policy(skb, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
4336 bond_for_each_slave(bond, slave, i) {
4337 slave_no--;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004338 if (slave_no < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 }
4341
4342 start_at = slave;
4343
4344 bond_for_each_slave_from(bond, slave, i, start_at) {
4345 if (IS_UP(slave->dev) &&
4346 (slave->link == BOND_LINK_UP) &&
4347 (slave->state == BOND_STATE_ACTIVE)) {
4348 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4349 break;
4350 }
4351 }
4352
4353out:
4354 if (res) {
4355 /* no suitable interface, frame not sent */
4356 dev_kfree_skb(skb);
4357 }
4358 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004359 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360}
4361
4362/*
4363 * in broadcast mode, we send everything to all usable interfaces.
4364 */
4365static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4366{
Wang Chen454d7c92008-11-12 23:37:49 -08004367 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 struct slave *slave, *start_at;
4369 struct net_device *tx_dev = NULL;
4370 int i;
4371 int res = 1;
4372
4373 read_lock(&bond->lock);
4374
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004375 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
4378 read_lock(&bond->curr_slave_lock);
4379 start_at = bond->curr_active_slave;
4380 read_unlock(&bond->curr_slave_lock);
4381
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004382 if (!start_at)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
4385 bond_for_each_slave_from(bond, slave, i, start_at) {
4386 if (IS_UP(slave->dev) &&
4387 (slave->link == BOND_LINK_UP) &&
4388 (slave->state == BOND_STATE_ACTIVE)) {
4389 if (tx_dev) {
4390 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4391 if (!skb2) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004392 pr_err(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08004393 ": %s: Error: bond_xmit_broadcast(): "
4394 "skb_clone() failed\n",
4395 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 continue;
4397 }
4398
4399 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4400 if (res) {
4401 dev_kfree_skb(skb2);
4402 continue;
4403 }
4404 }
4405 tx_dev = slave->dev;
4406 }
4407 }
4408
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004409 if (tx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 res = bond_dev_queue_xmit(bond, skb, tx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411
4412out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004413 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 /* no suitable interface, frame not sent */
4415 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004416
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 /* frame sent to all suitable interfaces */
4418 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004419 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420}
4421
4422/*------------------------- Device initialization ---------------------------*/
4423
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004424static void bond_set_xmit_hash_policy(struct bonding *bond)
4425{
4426 switch (bond->params.xmit_policy) {
4427 case BOND_XMIT_POLICY_LAYER23:
4428 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4429 break;
4430 case BOND_XMIT_POLICY_LAYER34:
4431 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4432 break;
4433 case BOND_XMIT_POLICY_LAYER2:
4434 default:
4435 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4436 break;
4437 }
4438}
4439
Stephen Hemminger424efe92009-08-31 19:50:51 +00004440static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
Stephen Hemminger00829822008-11-20 20:14:53 -08004441{
4442 const struct bonding *bond = netdev_priv(dev);
4443
4444 switch (bond->params.mode) {
4445 case BOND_MODE_ROUNDROBIN:
4446 return bond_xmit_roundrobin(skb, dev);
4447 case BOND_MODE_ACTIVEBACKUP:
4448 return bond_xmit_activebackup(skb, dev);
4449 case BOND_MODE_XOR:
4450 return bond_xmit_xor(skb, dev);
4451 case BOND_MODE_BROADCAST:
4452 return bond_xmit_broadcast(skb, dev);
4453 case BOND_MODE_8023AD:
4454 return bond_3ad_xmit_xor(skb, dev);
4455 case BOND_MODE_ALB:
4456 case BOND_MODE_TLB:
4457 return bond_alb_xmit(skb, dev);
4458 default:
4459 /* Should never happen, mode already checked */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004460 pr_err(DRV_NAME ": %s: Error: Unknown bonding mode %d\n",
Stephen Hemminger00829822008-11-20 20:14:53 -08004461 dev->name, bond->params.mode);
4462 WARN_ON_ONCE(1);
4463 dev_kfree_skb(skb);
4464 return NETDEV_TX_OK;
4465 }
4466}
4467
4468
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469/*
4470 * set bond mode specific net device operations
4471 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004472void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004474 struct net_device *bond_dev = bond->dev;
4475
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 switch (mode) {
4477 case BOND_MODE_ROUNDROBIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 break;
4479 case BOND_MODE_ACTIVEBACKUP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 break;
4481 case BOND_MODE_XOR:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004482 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 break;
4484 case BOND_MODE_BROADCAST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 break;
4486 case BOND_MODE_8023AD:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004487 bond_set_master_3ad_flags(bond);
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004488 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004491 bond_set_master_alb_flags(bond);
4492 /* FALLTHRU */
4493 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 break;
4495 default:
4496 /* Should never happen, mode already checked */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004497 pr_err(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08004498 ": %s: Error: Unknown bonding mode %d\n",
4499 bond_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 mode);
4501 break;
4502 }
4503}
4504
Jay Vosburgh217df672005-09-26 16:11:50 -07004505static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4506 struct ethtool_drvinfo *drvinfo)
4507{
4508 strncpy(drvinfo->driver, DRV_NAME, 32);
4509 strncpy(drvinfo->version, DRV_VERSION, 32);
4510 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4511}
4512
Jeff Garzik7282d492006-09-13 14:30:00 -04004513static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004514 .get_drvinfo = bond_ethtool_get_drvinfo,
Stephen Hemmingerfa53eba2008-09-13 21:17:09 -04004515 .get_link = ethtool_op_get_link,
4516 .get_tx_csum = ethtool_op_get_tx_csum,
4517 .get_sg = ethtool_op_get_sg,
4518 .get_tso = ethtool_op_get_tso,
4519 .get_ufo = ethtool_op_get_ufo,
4520 .get_flags = ethtool_op_get_flags,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004521};
4522
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004523static const struct net_device_ops bond_netdev_ops = {
Stephen Hemminger181470f2009-06-12 19:02:52 +00004524 .ndo_init = bond_init,
Stephen Hemminger9e716262009-06-12 19:02:47 +00004525 .ndo_uninit = bond_uninit,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004526 .ndo_open = bond_open,
4527 .ndo_stop = bond_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08004528 .ndo_start_xmit = bond_start_xmit,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004529 .ndo_get_stats = bond_get_stats,
4530 .ndo_do_ioctl = bond_do_ioctl,
4531 .ndo_set_multicast_list = bond_set_multicast_list,
4532 .ndo_change_mtu = bond_change_mtu,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004533 .ndo_set_mac_address = bond_set_mac_address,
Stephen Hemminger00829822008-11-20 20:14:53 -08004534 .ndo_neigh_setup = bond_neigh_setup,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004535 .ndo_vlan_rx_register = bond_vlan_rx_register,
4536 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4537 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
4538};
4539
Stephen Hemminger181470f2009-06-12 19:02:52 +00004540static void bond_setup(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541{
Wang Chen454d7c92008-11-12 23:37:49 -08004542 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 /* initialize rwlocks */
4545 rwlock_init(&bond->lock);
4546 rwlock_init(&bond->curr_slave_lock);
4547
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00004548 bond->params = bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549
4550 /* Initialize pointers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 bond->dev = bond_dev;
4552 INIT_LIST_HEAD(&bond->vlan_list);
4553
4554 /* Initialize the device entry points */
Stephen Hemminger181470f2009-06-12 19:02:52 +00004555 ether_setup(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004556 bond_dev->netdev_ops = &bond_netdev_ops;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004557 bond_dev->ethtool_ops = &bond_ethtool_ops;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004558 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
Stephen Hemminger9e716262009-06-12 19:02:47 +00004560 bond_dev->destructor = free_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
4562 /* Initialize the device options */
4563 bond_dev->tx_queue_len = 0;
4564 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004565 bond_dev->priv_flags |= IFF_BONDING;
Stephen Hemminger181470f2009-06-12 19:02:52 +00004566 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
4567
Jay Vosburgh6cf3f412008-11-03 18:16:50 -08004568 if (bond->params.arp_interval)
4569 bond_dev->priv_flags |= IFF_MASTER_ARPMON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
4571 /* At first, we block adding VLANs. That's the only way to
4572 * prevent problems that occur when adding VLANs over an
4573 * empty bond. The block will be removed once non-challenged
4574 * slaves are enslaved.
4575 */
4576 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4577
Herbert Xu932ff272006-06-09 12:20:56 -07004578 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 * transmitting */
4580 bond_dev->features |= NETIF_F_LLTX;
4581
4582 /* By default, we declare the bond to be fully
4583 * VLAN hardware accelerated capable. Special
4584 * care is taken in the various xmit functions
4585 * when there are slaves that are not hw accel
4586 * capable
4587 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4589 NETIF_F_HW_VLAN_RX |
4590 NETIF_F_HW_VLAN_FILTER);
4591
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592}
4593
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004594static void bond_work_cancel_all(struct bonding *bond)
4595{
4596 write_lock_bh(&bond->lock);
4597 bond->kill_timers = 1;
4598 write_unlock_bh(&bond->lock);
4599
4600 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4601 cancel_delayed_work(&bond->mii_work);
4602
4603 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4604 cancel_delayed_work(&bond->arp_work);
4605
4606 if (bond->params.mode == BOND_MODE_ALB &&
4607 delayed_work_pending(&bond->alb_work))
4608 cancel_delayed_work(&bond->alb_work);
4609
4610 if (bond->params.mode == BOND_MODE_8023AD &&
4611 delayed_work_pending(&bond->ad_work))
4612 cancel_delayed_work(&bond->ad_work);
4613}
4614
Jay Vosburgha434e432008-10-30 17:41:15 -07004615/* De-initialize device specific data.
4616 * Caller must hold rtnl_lock.
4617 */
4618static void bond_deinit(struct net_device *bond_dev)
4619{
Wang Chen454d7c92008-11-12 23:37:49 -08004620 struct bonding *bond = netdev_priv(bond_dev);
Jay Vosburgha434e432008-10-30 17:41:15 -07004621
4622 list_del(&bond->bond_list);
4623
4624 bond_work_cancel_all(bond);
4625
Jay Vosburgha434e432008-10-30 17:41:15 -07004626 bond_remove_proc_entry(bond);
Jay Vosburgha434e432008-10-30 17:41:15 -07004627}
4628
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629/* Unregister and free all bond devices.
4630 * Caller must hold rtnl_lock.
4631 */
4632static void bond_free_all(void)
4633{
4634 struct bonding *bond, *nxt;
4635
4636 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4637 struct net_device *bond_dev = bond->dev;
4638
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004639 bond_work_cancel_all(bond);
jamal702987052006-09-22 21:54:37 -07004640 /* Release the bonded slaves */
4641 bond_release_all(bond_dev);
Stephen Hemminger9e716262009-06-12 19:02:47 +00004642 unregister_netdevice(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 }
4644
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 bond_destroy_proc_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646}
4647
4648/*------------------------- Module initialization ---------------------------*/
4649
4650/*
4651 * Convert string input module parms. Accept either the
Jay Vosburghece95f72008-01-17 16:25:01 -08004652 * number of the mode or its string name. A bit complicated because
4653 * some mode names are substrings of other names, and calls from sysfs
4654 * may have whitespace in the name (trailing newlines, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 */
Holger Eitzenberger325dcf72008-12-09 23:10:17 -08004656int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657{
Hannes Eder54b87322009-02-14 11:15:49 +00004658 int modeint = -1, i, rv;
Jay Vosburgha42e5342008-01-29 18:07:43 -08004659 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
Jay Vosburghece95f72008-01-17 16:25:01 -08004660
Jay Vosburgha42e5342008-01-29 18:07:43 -08004661 for (p = (char *)buf; *p; p++)
4662 if (!(isdigit(*p) || isspace(*p)))
4663 break;
4664
4665 if (*p)
Jay Vosburghece95f72008-01-17 16:25:01 -08004666 rv = sscanf(buf, "%20s", modestr);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004667 else
Hannes Eder54b87322009-02-14 11:15:49 +00004668 rv = sscanf(buf, "%d", &modeint);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004669
4670 if (!rv)
4671 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
4673 for (i = 0; tbl[i].modename; i++) {
Hannes Eder54b87322009-02-14 11:15:49 +00004674 if (modeint == tbl[i].mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 return tbl[i].mode;
Jay Vosburghece95f72008-01-17 16:25:01 -08004676 if (strcmp(modestr, tbl[i].modename) == 0)
4677 return tbl[i].mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 }
4679
4680 return -1;
4681}
4682
4683static int bond_check_params(struct bond_params *params)
4684{
Jiri Pirkoa5499522009-09-25 03:28:09 +00004685 int arp_validate_value, fail_over_mac_value, primary_reselect_value;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004686
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 /*
4688 * Convert string parameters.
4689 */
4690 if (mode) {
4691 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4692 if (bond_mode == -1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004693 pr_err(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 ": Error: Invalid bonding mode \"%s\"\n",
4695 mode == NULL ? "NULL" : mode);
4696 return -EINVAL;
4697 }
4698 }
4699
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004700 if (xmit_hash_policy) {
4701 if ((bond_mode != BOND_MODE_XOR) &&
4702 (bond_mode != BOND_MODE_8023AD)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004703 pr_info(DRV_NAME
Nicolas de Pesloüan3c6aaa22009-10-07 14:10:36 -07004704 ": xmit_hash_policy param is irrelevant in"
4705 " mode %s\n",
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004706 bond_mode_name(bond_mode));
4707 } else {
4708 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4709 xmit_hashtype_tbl);
4710 if (xmit_hashtype == -1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004711 pr_err(DRV_NAME
4712 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4713 xmit_hash_policy == NULL ? "NULL" :
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004714 xmit_hash_policy);
4715 return -EINVAL;
4716 }
4717 }
4718 }
4719
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 if (lacp_rate) {
4721 if (bond_mode != BOND_MODE_8023AD) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004722 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 ": lacp_rate param is irrelevant in mode %s\n",
4724 bond_mode_name(bond_mode));
4725 } else {
4726 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4727 if (lacp_fast == -1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004728 pr_err(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 ": Error: Invalid lacp rate \"%s\"\n",
4730 lacp_rate == NULL ? "NULL" : lacp_rate);
4731 return -EINVAL;
4732 }
4733 }
4734 }
4735
Jay Vosburghfd989c82008-11-04 17:51:16 -08004736 if (ad_select) {
4737 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4738 if (params->ad_select == -1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004739 pr_err(DRV_NAME
Jay Vosburghfd989c82008-11-04 17:51:16 -08004740 ": Error: Invalid ad_select \"%s\"\n",
4741 ad_select == NULL ? "NULL" : ad_select);
4742 return -EINVAL;
4743 }
4744
4745 if (bond_mode != BOND_MODE_8023AD) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004746 pr_warning(DRV_NAME
Jay Vosburghfd989c82008-11-04 17:51:16 -08004747 ": ad_select param only affects 802.3ad mode\n");
4748 }
4749 } else {
4750 params->ad_select = BOND_AD_STABLE;
4751 }
4752
Nicolas de Pesloüanf5841302009-08-28 13:18:34 +00004753 if (max_bonds < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004754 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 ": Warning: max_bonds (%d) not in range %d-%d, so it "
Mitch Williams4e0952c2005-11-09 10:34:57 -08004756 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
Jay Vosburghb8a97872008-06-13 18:12:04 -07004757 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 max_bonds = BOND_DEFAULT_MAX_BONDS;
4759 }
4760
4761 if (miimon < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004762 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 ": Warning: miimon module parameter (%d), "
4764 "not in range 0-%d, so it was reset to %d\n",
4765 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4766 miimon = BOND_LINK_MON_INTERV;
4767 }
4768
4769 if (updelay < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004770 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 ": Warning: updelay module parameter (%d), "
4772 "not in range 0-%d, so it was reset to 0\n",
4773 updelay, INT_MAX);
4774 updelay = 0;
4775 }
4776
4777 if (downdelay < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004778 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 ": Warning: downdelay module parameter (%d), "
4780 "not in range 0-%d, so it was reset to 0\n",
4781 downdelay, INT_MAX);
4782 downdelay = 0;
4783 }
4784
4785 if ((use_carrier != 0) && (use_carrier != 1)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004786 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 ": Warning: use_carrier module parameter (%d), "
4788 "not of valid value (0/1), so it was set to 1\n",
4789 use_carrier);
4790 use_carrier = 1;
4791 }
4792
Moni Shoua7893b242008-05-17 21:10:12 -07004793 if (num_grat_arp < 0 || num_grat_arp > 255) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004794 pr_warning(DRV_NAME
Moni Shoua7893b242008-05-17 21:10:12 -07004795 ": Warning: num_grat_arp (%d) not in range 0-255 so it "
4796 "was reset to 1 \n", num_grat_arp);
4797 num_grat_arp = 1;
4798 }
4799
Brian Haley305d5522008-11-04 17:51:14 -08004800 if (num_unsol_na < 0 || num_unsol_na > 255) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004801 pr_warning(DRV_NAME
Brian Haley305d5522008-11-04 17:51:14 -08004802 ": Warning: num_unsol_na (%d) not in range 0-255 so it "
4803 "was reset to 1 \n", num_unsol_na);
4804 num_unsol_na = 1;
4805 }
4806
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 /* reset values for 802.3ad */
4808 if (bond_mode == BOND_MODE_8023AD) {
4809 if (!miimon) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004810 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 ": Warning: miimon must be specified, "
4812 "otherwise bonding will not detect link "
4813 "failure, speed and duplex which are "
4814 "essential for 802.3ad operation\n");
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004815 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 miimon = 100;
4817 }
4818 }
4819
4820 /* reset values for TLB/ALB */
4821 if ((bond_mode == BOND_MODE_TLB) ||
4822 (bond_mode == BOND_MODE_ALB)) {
4823 if (!miimon) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004824 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 ": Warning: miimon must be specified, "
4826 "otherwise bonding will not detect link "
4827 "failure and link speed which are essential "
4828 "for TLB/ALB load balancing\n");
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004829 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 miimon = 100;
4831 }
4832 }
4833
4834 if (bond_mode == BOND_MODE_ALB) {
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00004835 pr_notice(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 ": In ALB mode you might experience client "
4837 "disconnections upon reconnection of a link if the "
4838 "bonding module updelay parameter (%d msec) is "
4839 "incompatible with the forwarding delay time of the "
4840 "switch\n",
4841 updelay);
4842 }
4843
4844 if (!miimon) {
4845 if (updelay || downdelay) {
4846 /* just warn the user the up/down delay will have
4847 * no effect since miimon is zero...
4848 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004849 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 ": Warning: miimon module parameter not set "
4851 "and updelay (%d) or downdelay (%d) module "
4852 "parameter is set; updelay and downdelay have "
4853 "no effect unless miimon is set\n",
4854 updelay, downdelay);
4855 }
4856 } else {
4857 /* don't allow arp monitoring */
4858 if (arp_interval) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004859 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 ": Warning: miimon (%d) and arp_interval (%d) "
4861 "can't be used simultaneously, disabling ARP "
4862 "monitoring\n",
4863 miimon, arp_interval);
4864 arp_interval = 0;
4865 }
4866
4867 if ((updelay % miimon) != 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004868 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 ": Warning: updelay (%d) is not a multiple "
4870 "of miimon (%d), updelay rounded to %d ms\n",
4871 updelay, miimon, (updelay / miimon) * miimon);
4872 }
4873
4874 updelay /= miimon;
4875
4876 if ((downdelay % miimon) != 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004877 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 ": Warning: downdelay (%d) is not a multiple "
4879 "of miimon (%d), downdelay rounded to %d ms\n",
4880 downdelay, miimon,
4881 (downdelay / miimon) * miimon);
4882 }
4883
4884 downdelay /= miimon;
4885 }
4886
4887 if (arp_interval < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004888 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 ": Warning: arp_interval module parameter (%d) "
4890 ", not in range 0-%d, so it was reset to %d\n",
4891 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4892 arp_interval = BOND_LINK_ARP_INTERV;
4893 }
4894
4895 for (arp_ip_count = 0;
4896 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4897 arp_ip_count++) {
4898 /* not complete check, but should be good enough to
4899 catch mistakes */
4900 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004901 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 ": Warning: bad arp_ip_target module parameter "
4903 "(%s), ARP monitoring will not be performed\n",
4904 arp_ip_target[arp_ip_count]);
4905 arp_interval = 0;
4906 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04004907 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 arp_target[arp_ip_count] = ip;
4909 }
4910 }
4911
4912 if (arp_interval && !arp_ip_count) {
4913 /* don't allow arping if no arp_ip_target given... */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004914 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 ": Warning: arp_interval module parameter (%d) "
4916 "specified without providing an arp_ip_target "
4917 "parameter, arp_interval was reset to 0\n",
4918 arp_interval);
4919 arp_interval = 0;
4920 }
4921
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004922 if (arp_validate) {
4923 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004924 pr_err(DRV_NAME
4925 ": arp_validate only supported in active-backup mode\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004926 return -EINVAL;
4927 }
4928 if (!arp_interval) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004929 pr_err(DRV_NAME
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004930 ": arp_validate requires arp_interval\n");
4931 return -EINVAL;
4932 }
4933
4934 arp_validate_value = bond_parse_parm(arp_validate,
4935 arp_validate_tbl);
4936 if (arp_validate_value == -1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004937 pr_err(DRV_NAME
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004938 ": Error: invalid arp_validate \"%s\"\n",
4939 arp_validate == NULL ? "NULL" : arp_validate);
4940 return -EINVAL;
4941 }
4942 } else
4943 arp_validate_value = 0;
4944
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 if (miimon) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004946 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 ": MII link monitoring set to %d ms\n",
4948 miimon);
4949 } else if (arp_interval) {
4950 int i;
4951
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004952 pr_info(DRV_NAME ": ARP monitoring set to %d ms,"
4953 " validate %s, with %d target(s):",
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004954 arp_interval,
4955 arp_validate_tbl[arp_validate_value].modename,
4956 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
4958 for (i = 0; i < arp_ip_count; i++)
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00004959 pr_info(" %s", arp_ip_target[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00004961 pr_info("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962
Jay Vosburghb8a97872008-06-13 18:12:04 -07004963 } else if (max_bonds) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 /* miimon and arp_interval not set, we need one so things
4965 * work as expected, see bonding.txt for details
4966 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004967 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 ": Warning: either miimon or arp_interval and "
4969 "arp_ip_target module parameters must be specified, "
4970 "otherwise bonding will not detect link failures! see "
4971 "bonding.txt for details.\n");
4972 }
4973
4974 if (primary && !USES_PRIMARY(bond_mode)) {
4975 /* currently, using a primary only makes sense
4976 * in active backup, TLB or ALB modes
4977 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004978 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 ": Warning: %s primary device specified but has no "
4980 "effect in %s mode\n",
4981 primary, bond_mode_name(bond_mode));
4982 primary = NULL;
4983 }
4984
Jiri Pirkoa5499522009-09-25 03:28:09 +00004985 if (primary && primary_reselect) {
4986 primary_reselect_value = bond_parse_parm(primary_reselect,
4987 pri_reselect_tbl);
4988 if (primary_reselect_value == -1) {
4989 pr_err(DRV_NAME
4990 ": Error: Invalid primary_reselect \"%s\"\n",
4991 primary_reselect ==
4992 NULL ? "NULL" : primary_reselect);
4993 return -EINVAL;
4994 }
4995 } else {
4996 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
4997 }
4998
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004999 if (fail_over_mac) {
5000 fail_over_mac_value = bond_parse_parm(fail_over_mac,
5001 fail_over_mac_tbl);
5002 if (fail_over_mac_value == -1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005003 pr_err(DRV_NAME
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005004 ": Error: invalid fail_over_mac \"%s\"\n",
5005 arp_validate == NULL ? "NULL" : arp_validate);
5006 return -EINVAL;
5007 }
5008
5009 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005010 pr_warning(DRV_NAME
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005011 ": Warning: fail_over_mac only affects "
5012 "active-backup mode.\n");
5013 } else {
5014 fail_over_mac_value = BOND_FOM_NONE;
5015 }
Jay Vosburghdd957c52007-10-09 19:57:24 -07005016
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 /* fill params struct with the proper values */
5018 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04005019 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 params->miimon = miimon;
Moni Shoua7893b242008-05-17 21:10:12 -07005021 params->num_grat_arp = num_grat_arp;
Brian Haley305d5522008-11-04 17:51:14 -08005022 params->num_unsol_na = num_unsol_na;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005024 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 params->updelay = updelay;
5026 params->downdelay = downdelay;
5027 params->use_carrier = use_carrier;
5028 params->lacp_fast = lacp_fast;
5029 params->primary[0] = 0;
Jiri Pirkoa5499522009-09-25 03:28:09 +00005030 params->primary_reselect = primary_reselect_value;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005031 params->fail_over_mac = fail_over_mac_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032
5033 if (primary) {
5034 strncpy(params->primary, primary, IFNAMSIZ);
5035 params->primary[IFNAMSIZ - 1] = 0;
5036 }
5037
5038 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
5039
5040 return 0;
5041}
5042
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005043static struct lock_class_key bonding_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -07005044static struct lock_class_key bonding_netdev_addr_lock_key;
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005045
David S. Millere8a04642008-07-17 00:34:19 -07005046static void bond_set_lockdep_class_one(struct net_device *dev,
5047 struct netdev_queue *txq,
5048 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -07005049{
5050 lockdep_set_class(&txq->_xmit_lock,
5051 &bonding_netdev_xmit_lock_key);
5052}
5053
5054static void bond_set_lockdep_class(struct net_device *dev)
5055{
David S. Millercf508b12008-07-22 14:16:42 -07005056 lockdep_set_class(&dev->addr_list_lock,
5057 &bonding_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -07005058 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -07005059}
5060
Stephen Hemminger181470f2009-06-12 19:02:52 +00005061/*
5062 * Called from registration process
5063 */
5064static int bond_init(struct net_device *bond_dev)
5065{
5066 struct bonding *bond = netdev_priv(bond_dev);
5067
5068 pr_debug("Begin bond_init for %s\n", bond_dev->name);
5069
5070 bond->wq = create_singlethread_workqueue(bond_dev->name);
5071 if (!bond->wq)
5072 return -ENOMEM;
5073
5074 bond_set_lockdep_class(bond_dev);
5075
5076 netif_carrier_off(bond_dev);
5077
5078 bond_create_proc_entry(bond);
5079 list_add_tail(&bond->bond_list, &bond_dev_list);
5080
5081 return 0;
5082}
5083
Mitch Williamsdfe60392005-11-09 10:36:04 -08005084/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005085 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08005086 * Caller must NOT hold rtnl_lock; we need to release it here before we
5087 * set up our sysfs entries.
5088 */
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00005089int bond_create(const char *name)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005090{
5091 struct net_device *bond_dev;
5092 int res;
5093
5094 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08005095 /* Check to see if the bond already exists. */
Stephen Hemminger373500d2009-06-12 19:02:50 +00005096 /* FIXME: pass netns from caller */
5097 if (name && __dev_get_by_name(&init_net, name)) {
5098 pr_err(DRV_NAME ": cannot add bond %s; already exists\n",
5099 name);
5100 res = -EEXIST;
5101 goto out_rtnl;
Jay Vosburgh4fe47632008-01-29 18:07:45 -08005102 }
Jay Vosburgh027ea042008-01-17 16:25:02 -08005103
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005104 bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
Stephen Hemminger181470f2009-06-12 19:02:52 +00005105 bond_setup);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005106 if (!bond_dev) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005107 pr_err(DRV_NAME ": %s: eek! can't alloc netdev!\n",
Mitch Williamsdfe60392005-11-09 10:36:04 -08005108 name);
5109 res = -ENOMEM;
5110 goto out_rtnl;
5111 }
5112
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005113 if (!name) {
5114 res = dev_alloc_name(bond_dev, "bond%d");
5115 if (res < 0)
5116 goto out_netdev;
5117 }
5118
Mitch Williamsdfe60392005-11-09 10:36:04 -08005119 res = register_netdevice(bond_dev);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005120 if (res < 0)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005121 goto out_bond;
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005122
Wang Chen454d7c92008-11-12 23:37:49 -08005123 res = bond_create_sysfs_entry(netdev_priv(bond_dev));
Jiri Pirko1363d9b2009-05-01 15:35:28 -07005124 if (res < 0)
5125 goto out_unreg;
Jay Vosburgh09c89272007-01-19 18:15:38 -08005126
Stephen Hemminger7e083842009-06-12 19:02:46 +00005127 rtnl_unlock();
Jay Vosburgh09c89272007-01-19 18:15:38 -08005128 return 0;
5129
Jiri Pirko1363d9b2009-05-01 15:35:28 -07005130out_unreg:
Jiri Pirko1363d9b2009-05-01 15:35:28 -07005131 unregister_netdevice(bond_dev);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005132out_bond:
5133 bond_deinit(bond_dev);
5134out_netdev:
5135 free_netdev(bond_dev);
5136out_rtnl:
5137 rtnl_unlock();
Mitch Williamsdfe60392005-11-09 10:36:04 -08005138 return res;
5139}
5140
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141static int __init bonding_init(void)
5142{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 int i;
5144 int res;
5145
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005146 pr_info("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
Mitch Williamsdfe60392005-11-09 10:36:04 -08005148 res = bond_check_params(&bonding_defaults);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005149 if (res)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005150 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 bond_create_proc_dir();
Jay Vosburgh027ea042008-01-17 16:25:02 -08005153
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 for (i = 0; i < max_bonds; i++) {
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00005155 res = bond_create(NULL);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005156 if (res)
5157 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 }
5159
Mitch Williamsb76cdba2005-11-09 10:36:41 -08005160 res = bond_create_sysfs();
5161 if (res)
5162 goto err;
5163
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005165 register_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005166 bond_register_ipv6_notifier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167
Mitch Williamsdfe60392005-11-09 10:36:04 -08005168 goto out;
5169err:
Florin Malita40abc272005-09-18 00:24:12 -07005170 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 bond_free_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 rtnl_unlock();
Mitch Williamsdfe60392005-11-09 10:36:04 -08005173out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 return res;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005175
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176}
5177
5178static void __exit bonding_exit(void)
5179{
5180 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005181 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005182 bond_unregister_ipv6_notifier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005184 bond_destroy_sysfs();
5185
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 rtnl_lock();
5187 bond_free_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 rtnl_unlock();
5189}
5190
5191module_init(bonding_init);
5192module_exit(bonding_exit);
5193MODULE_LICENSE("GPL");
5194MODULE_VERSION(DRV_VERSION);
5195MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5196MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");