blob: a58a60859da9625c7d9136eb107f5d35dfcb9cab [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;
Jiri Bohacd9d52832009-10-28 22:23:54 -0700710 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);
Stephen Hemminger9e716262009-06-12 19:02:47 +00002014
Jay Vosburgha434e432008-10-30 17:41:15 -07002015 if (bond->wq)
2016 destroy_workqueue(bond->wq);
2017
2018 netif_addr_lock_bh(bond_dev);
2019 bond_mc_list_destroy(bond);
2020 netif_addr_unlock_bh(bond_dev);
Jay Vosburgha434e432008-10-30 17:41:15 -07002021}
2022
Moni Shouad90a1622007-10-09 19:43:43 -07002023/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002024* First release a slave and than destroy the bond if no more slaves are left.
Moni Shouad90a1622007-10-09 19:43:43 -07002025* Must be under rtnl_lock when this function is called.
2026*/
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002027int bond_release_and_destroy(struct net_device *bond_dev,
2028 struct net_device *slave_dev)
Moni Shouad90a1622007-10-09 19:43:43 -07002029{
Wang Chen454d7c92008-11-12 23:37:49 -08002030 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002031 int ret;
2032
2033 ret = bond_release(bond_dev, slave_dev);
2034 if ((ret == 0) && (bond->slave_cnt == 0)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002035 pr_info(DRV_NAME ": %s: destroying bond %s.\n",
Moni Shouad90a1622007-10-09 19:43:43 -07002036 bond_dev->name, bond_dev->name);
Stephen Hemminger9e716262009-06-12 19:02:47 +00002037 unregister_netdevice(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002038 }
2039 return ret;
2040}
2041
2042/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 * This function releases all slaves.
2044 */
2045static int bond_release_all(struct net_device *bond_dev)
2046{
Wang Chen454d7c92008-11-12 23:37:49 -08002047 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 struct slave *slave;
2049 struct net_device *slave_dev;
2050 struct sockaddr addr;
2051
2052 write_lock_bh(&bond->lock);
2053
Jay Vosburghff59c452006-03-27 13:27:43 -08002054 netif_carrier_off(bond_dev);
2055
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002056 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059 bond->current_arp_slave = NULL;
2060 bond->primary_slave = NULL;
2061 bond_change_active_slave(bond, NULL);
2062
2063 while ((slave = bond->first_slave) != NULL) {
2064 /* Inform AD package of unbinding of slave
2065 * before slave is detached from the list.
2066 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002067 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 bond_3ad_unbind_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 slave_dev = slave->dev;
2071 bond_detach_slave(bond, slave);
2072
Jay Vosburgh25433312008-01-17 16:24:59 -08002073 /* now that the slave is detached, unlock and perform
2074 * all the undo steps that should not be called from
2075 * within a lock.
2076 */
2077 write_unlock_bh(&bond->lock);
2078
Holger Eitzenberger58402052008-12-09 23:07:13 -08002079 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 /* must be called only after the slave
2081 * has been detached from the list
2082 */
2083 bond_alb_deinit_slave(bond, slave);
2084 }
2085
Arthur Kepner8531c5f2005-08-23 01:34:53 -04002086 bond_compute_features(bond);
2087
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002088 bond_destroy_slave_symlinks(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 bond_del_vlans_from_slave(bond, slave_dev);
2090
2091 /* If the mode USES_PRIMARY, then we should only remove its
2092 * promisc and mc settings if it was the curr_active_slave, but that was
2093 * already taken care of above when we detached the slave
2094 */
2095 if (!USES_PRIMARY(bond->params.mode)) {
2096 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002097 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002101 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002105 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002107 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
2109
2110 netdev_set_master(slave_dev, NULL);
2111
2112 /* close slave before restoring its mac address */
2113 dev_close(slave_dev);
2114
Jay Vosburghdd957c52007-10-09 19:57:24 -07002115 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002116 /* restore original ("permanent") mac address*/
2117 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2118 addr.sa_family = slave_dev->type;
2119 dev_set_mac_address(slave_dev, &addr);
2120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002122 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
2123 IFF_SLAVE_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 kfree(slave);
2126
2127 /* re-acquire the lock before getting the next slave */
2128 write_lock_bh(&bond->lock);
2129 }
2130
2131 /* zero the mac address of the master so it will be
2132 * set by the application to the mac address of the
2133 * first slave
2134 */
2135 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2136
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002137 if (list_empty(&bond->vlan_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002139 else {
2140 pr_warning(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08002141 ": %s: Warning: clearing HW address of %s while it "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 "still has VLANs.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08002143 bond_dev->name, bond_dev->name);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002144 pr_warning(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08002145 ": %s: When re-adding slaves, make sure the bond's "
2146 "HW address matches its VLANs'.\n",
2147 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 }
2149
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002150 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 ": %s: released all slaves\n",
2152 bond_dev->name);
2153
2154out:
2155 write_unlock_bh(&bond->lock);
2156
2157 return 0;
2158}
2159
2160/*
2161 * This function changes the active slave to slave <slave_dev>.
2162 * It returns -EINVAL in the following cases.
2163 * - <slave_dev> is not found in the list.
2164 * - There is not active slave now.
2165 * - <slave_dev> is already active.
2166 * - The link state of <slave_dev> is not BOND_LINK_UP.
2167 * - <slave_dev> is not running.
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002168 * In these cases, this function does nothing.
2169 * In the other cases, current_slave pointer is changed and 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 */
2171static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2172{
Wang Chen454d7c92008-11-12 23:37:49 -08002173 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 struct slave *old_active = NULL;
2175 struct slave *new_active = NULL;
2176 int res = 0;
2177
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002178 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 /* Verify that master_dev is indeed the master of slave_dev */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002182 if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002185 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002187 read_lock(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 old_active = bond->curr_active_slave;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002189 read_unlock(&bond->curr_slave_lock);
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 new_active = bond_get_slave_by_dev(bond, slave_dev);
2192
2193 /*
2194 * Changing to the current active: do nothing; return success.
2195 */
2196 if (new_active && (new_active == old_active)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002197 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return 0;
2199 }
2200
2201 if ((new_active) &&
2202 (old_active) &&
2203 (new_active->link == BOND_LINK_UP) &&
2204 IS_UP(new_active->dev)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002205 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 bond_change_active_slave(bond, new_active);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002207 write_unlock_bh(&bond->curr_slave_lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002208 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002211 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 return res;
2214}
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2217{
Wang Chen454d7c92008-11-12 23:37:49 -08002218 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
2220 info->bond_mode = bond->params.mode;
2221 info->miimon = bond->params.miimon;
2222
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002223 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 info->num_slaves = bond->slave_cnt;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002225 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227 return 0;
2228}
2229
2230static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2231{
Wang Chen454d7c92008-11-12 23:37:49 -08002232 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 struct slave *slave;
Eric Dumazet689c96c2009-04-23 03:39:04 +00002234 int i, res = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002236 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 bond_for_each_slave(bond, slave, i) {
2239 if (i == (int)info->slave_id) {
Eric Dumazet689c96c2009-04-23 03:39:04 +00002240 res = 0;
2241 strcpy(info->slave_name, slave->dev->name);
2242 info->link = slave->link;
2243 info->state = slave->state;
2244 info->link_failure_count = slave->link_failure_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 break;
2246 }
2247 }
2248
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002249 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Eric Dumazet689c96c2009-04-23 03:39:04 +00002251 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253
2254/*-------------------------------- Monitoring -------------------------------*/
2255
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002256
2257static int bond_miimon_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002259 struct slave *slave;
2260 int i, link_state, commit = 0;
Jiri Pirko41f89102009-04-24 03:57:29 +00002261 bool ignore_updelay;
2262
2263 ignore_updelay = !bond->curr_active_slave ? true : false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265 bond_for_each_slave(bond, slave, i) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002266 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002268 link_state = bond_check_dev_link(bond, slave->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270 switch (slave->link) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002271 case BOND_LINK_UP:
2272 if (link_state)
2273 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002275 slave->link = BOND_LINK_FAIL;
2276 slave->delay = bond->params.downdelay;
2277 if (slave->delay) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002278 pr_info(DRV_NAME
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002279 ": %s: link status down for %s"
2280 "interface %s, disabling it in %d ms.\n",
2281 bond->dev->name,
2282 (bond->params.mode ==
2283 BOND_MODE_ACTIVEBACKUP) ?
2284 ((slave->state == BOND_STATE_ACTIVE) ?
2285 "active " : "backup ") : "",
2286 slave->dev->name,
2287 bond->params.downdelay * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002289 /*FALLTHRU*/
2290 case BOND_LINK_FAIL:
2291 if (link_state) {
2292 /*
2293 * recovered before downdelay expired
2294 */
2295 slave->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 slave->jiffies = jiffies;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002297 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 ": %s: link status up again after %d "
2299 "ms for interface %s.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002300 bond->dev->name,
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002301 (bond->params.downdelay - slave->delay) *
2302 bond->params.miimon,
2303 slave->dev->name);
2304 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002306
2307 if (slave->delay <= 0) {
2308 slave->new_link = BOND_LINK_DOWN;
2309 commit++;
2310 continue;
2311 }
2312
2313 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002316 case BOND_LINK_DOWN:
2317 if (!link_state)
2318 continue;
2319
2320 slave->link = BOND_LINK_BACK;
2321 slave->delay = bond->params.updelay;
2322
2323 if (slave->delay) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002324 pr_info(DRV_NAME
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002325 ": %s: link status up for "
2326 "interface %s, enabling it in %d ms.\n",
2327 bond->dev->name, slave->dev->name,
Jiri Pirko41f89102009-04-24 03:57:29 +00002328 ignore_updelay ? 0 :
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002329 bond->params.updelay *
2330 bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002332 /*FALLTHRU*/
2333 case BOND_LINK_BACK:
2334 if (!link_state) {
2335 slave->link = BOND_LINK_DOWN;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002336 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 ": %s: link status down again after %d "
2338 "ms for interface %s.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002339 bond->dev->name,
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002340 (bond->params.updelay - slave->delay) *
2341 bond->params.miimon,
2342 slave->dev->name);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002343
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002344 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002346
Jiri Pirko41f89102009-04-24 03:57:29 +00002347 if (ignore_updelay)
2348 slave->delay = 0;
2349
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002350 if (slave->delay <= 0) {
2351 slave->new_link = BOND_LINK_UP;
2352 commit++;
Jiri Pirko41f89102009-04-24 03:57:29 +00002353 ignore_updelay = false;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002354 continue;
2355 }
2356
2357 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 break;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002359 }
2360 }
2361
2362 return commit;
2363}
2364
2365static void bond_miimon_commit(struct bonding *bond)
2366{
2367 struct slave *slave;
2368 int i;
2369
2370 bond_for_each_slave(bond, slave, i) {
2371 switch (slave->new_link) {
2372 case BOND_LINK_NOCHANGE:
2373 continue;
2374
2375 case BOND_LINK_UP:
2376 slave->link = BOND_LINK_UP;
2377 slave->jiffies = jiffies;
2378
2379 if (bond->params.mode == BOND_MODE_8023AD) {
2380 /* prevent it from being the active one */
2381 slave->state = BOND_STATE_BACKUP;
2382 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2383 /* make it immediately active */
2384 slave->state = BOND_STATE_ACTIVE;
2385 } else if (slave != bond->primary_slave) {
2386 /* prevent it from being the active one */
2387 slave->state = BOND_STATE_BACKUP;
2388 }
2389
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002390 pr_info(DRV_NAME
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002391 ": %s: link status definitely "
2392 "up for interface %s.\n",
2393 bond->dev->name, slave->dev->name);
2394
2395 /* notify ad that the link status has changed */
2396 if (bond->params.mode == BOND_MODE_8023AD)
2397 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2398
Holger Eitzenberger58402052008-12-09 23:07:13 -08002399 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002400 bond_alb_handle_link_change(bond, slave,
2401 BOND_LINK_UP);
2402
2403 if (!bond->curr_active_slave ||
2404 (slave == bond->primary_slave))
2405 goto do_failover;
2406
2407 continue;
2408
2409 case BOND_LINK_DOWN:
Jay Vosburghfba4acd2008-10-30 17:41:14 -07002410 if (slave->link_failure_count < UINT_MAX)
2411 slave->link_failure_count++;
2412
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002413 slave->link = BOND_LINK_DOWN;
2414
2415 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2416 bond->params.mode == BOND_MODE_8023AD)
2417 bond_set_slave_inactive_flags(slave);
2418
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002419 pr_info(DRV_NAME
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002420 ": %s: link status definitely down for "
2421 "interface %s, disabling it\n",
2422 bond->dev->name, slave->dev->name);
2423
2424 if (bond->params.mode == BOND_MODE_8023AD)
2425 bond_3ad_handle_link_change(slave,
2426 BOND_LINK_DOWN);
2427
Jiri Pirkoae63e802009-05-27 05:42:36 +00002428 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002429 bond_alb_handle_link_change(bond, slave,
2430 BOND_LINK_DOWN);
2431
2432 if (slave == bond->curr_active_slave)
2433 goto do_failover;
2434
2435 continue;
2436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 default:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002438 pr_err(DRV_NAME
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002439 ": %s: invalid new link %d on slave %s\n",
2440 bond->dev->name, slave->new_link,
2441 slave->dev->name);
2442 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002444 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
2446
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002447do_failover:
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002448 ASSERT_RTNL();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002449 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 bond_select_active_slave(bond);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002451 write_unlock_bh(&bond->curr_slave_lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002452 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002453
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002454 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455}
2456
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002457/*
2458 * bond_mii_monitor
2459 *
2460 * Really a wrapper that splits the mii monitor into two phases: an
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002461 * inspection, then (if inspection indicates something needs to be done)
2462 * an acquisition of appropriate locks followed by a commit phase to
2463 * implement whatever link state changes are indicated.
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002464 */
2465void bond_mii_monitor(struct work_struct *work)
2466{
2467 struct bonding *bond = container_of(work, struct bonding,
2468 mii_work.work);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002469
2470 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002471 if (bond->kill_timers)
2472 goto out;
2473
2474 if (bond->slave_cnt == 0)
2475 goto re_arm;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002476
2477 if (bond->send_grat_arp) {
2478 read_lock(&bond->curr_slave_lock);
2479 bond_send_gratuitous_arp(bond);
2480 read_unlock(&bond->curr_slave_lock);
2481 }
2482
Brian Haley305d5522008-11-04 17:51:14 -08002483 if (bond->send_unsol_na) {
2484 read_lock(&bond->curr_slave_lock);
2485 bond_send_unsolicited_na(bond);
2486 read_unlock(&bond->curr_slave_lock);
2487 }
2488
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002489 if (bond_miimon_inspect(bond)) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002490 read_unlock(&bond->lock);
2491 rtnl_lock();
2492 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002493
2494 bond_miimon_commit(bond);
2495
Jay Vosburgh56556622008-01-17 16:25:03 -08002496 read_unlock(&bond->lock);
2497 rtnl_unlock(); /* might sleep, hold no other locks */
2498 read_lock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002499 }
2500
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002501re_arm:
2502 if (bond->params.miimon)
2503 queue_delayed_work(bond->wq, &bond->mii_work,
2504 msecs_to_jiffies(bond->params.miimon));
2505out:
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002506 read_unlock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002507}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002508
Al Virod3bb52b2007-08-22 20:06:58 -04002509static __be32 bond_glean_dev_ip(struct net_device *dev)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002510{
2511 struct in_device *idev;
2512 struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07002513 __be32 addr = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002514
2515 if (!dev)
2516 return 0;
2517
2518 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002519 idev = __in_dev_get_rcu(dev);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002520 if (!idev)
2521 goto out;
2522
2523 ifa = idev->ifa_list;
2524 if (!ifa)
2525 goto out;
2526
2527 addr = ifa->ifa_local;
2528out:
2529 rcu_read_unlock();
2530 return addr;
2531}
2532
Al Virod3bb52b2007-08-22 20:06:58 -04002533static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002534{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002535 struct vlan_entry *vlan;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002536
2537 if (ip == bond->master_ip)
2538 return 1;
2539
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002540 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002541 if (ip == vlan->vlan_ip)
2542 return 1;
2543 }
2544
2545 return 0;
2546}
2547
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002548/*
2549 * We go to the (large) trouble of VLAN tagging ARP frames because
2550 * switches in VLAN mode (especially if ports are configured as
2551 * "native" to a VLAN) might not pass non-tagged frames.
2552 */
Al Virod3bb52b2007-08-22 20:06:58 -04002553static 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 -04002554{
2555 struct sk_buff *skb;
2556
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002557 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002558 slave_dev->name, dest_ip, src_ip, vlan_id);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002559
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002560 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2561 NULL, slave_dev->dev_addr, NULL);
2562
2563 if (!skb) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002564 pr_err(DRV_NAME ": ARP packet allocation failed\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002565 return;
2566 }
2567 if (vlan_id) {
2568 skb = vlan_put_tag(skb, vlan_id);
2569 if (!skb) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002570 pr_err(DRV_NAME ": failed to insert VLAN tag\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002571 return;
2572 }
2573 }
2574 arp_xmit(skb);
2575}
2576
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2579{
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002580 int i, vlan_id, rv;
Al Virod3bb52b2007-08-22 20:06:58 -04002581 __be32 *targets = bond->params.arp_targets;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002582 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002583 struct net_device *vlan_dev;
2584 struct flowi fl;
2585 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Mitch Williams6b780562005-11-09 10:36:19 -08002587 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2588 if (!targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07002589 break;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002590 pr_debug("basa: target %x\n", targets[i]);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002591 if (list_empty(&bond->vlan_list)) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002592 pr_debug("basa: empty vlan: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002593 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2594 bond->master_ip, 0);
2595 continue;
2596 }
2597
2598 /*
2599 * If VLANs are configured, we do a route lookup to
2600 * determine which VLAN interface would be used, so we
2601 * can tag the ARP with the proper VLAN tag.
2602 */
2603 memset(&fl, 0, sizeof(fl));
2604 fl.fl4_dst = targets[i];
2605 fl.fl4_tos = RTO_ONLINK;
2606
Denis V. Lunevf2063512008-01-22 22:07:34 -08002607 rv = ip_route_output_key(&init_net, &rt, &fl);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002608 if (rv) {
2609 if (net_ratelimit()) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002610 pr_warning(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -07002611 ": %s: no route to arp_ip_target %pI4\n",
2612 bond->dev->name, &fl.fl4_dst);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002613 }
2614 continue;
2615 }
2616
2617 /*
2618 * This target is not on a VLAN
2619 */
2620 if (rt->u.dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002621 ip_rt_put(rt);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002622 pr_debug("basa: rtdev == bond->dev: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002623 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2624 bond->master_ip, 0);
2625 continue;
2626 }
2627
2628 vlan_id = 0;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002629 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002630 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002631 if (vlan_dev == rt->u.dst.dev) {
2632 vlan_id = vlan->vlan_id;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002633 pr_debug("basa: vlan match on %s %d\n",
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002634 vlan_dev->name, vlan_id);
2635 break;
2636 }
2637 }
2638
2639 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002640 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002641 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2642 vlan->vlan_ip, vlan_id);
2643 continue;
2644 }
2645
2646 if (net_ratelimit()) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002647 pr_warning(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -07002648 ": %s: no path to arp_ip_target %pI4 via rt.dev %s\n",
2649 bond->dev->name, &fl.fl4_dst,
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002650 rt->u.dst.dev ? rt->u.dst.dev->name : "NULL");
2651 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002652 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002653 }
2654}
2655
2656/*
2657 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2658 * for each VLAN above us.
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002659 *
2660 * Caller must hold curr_slave_lock for read or better
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002661 */
2662static void bond_send_gratuitous_arp(struct bonding *bond)
2663{
2664 struct slave *slave = bond->curr_active_slave;
2665 struct vlan_entry *vlan;
2666 struct net_device *vlan_dev;
2667
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002668 pr_debug("bond_send_grat_arp: bond %s slave %s\n", bond->dev->name,
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002669 slave ? slave->dev->name : "NULL");
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002670
2671 if (!slave || !bond->send_grat_arp ||
2672 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002673 return;
2674
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002675 bond->send_grat_arp--;
2676
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002677 if (bond->master_ip) {
2678 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
Moni Shoua1053f622007-10-09 19:43:42 -07002679 bond->master_ip, 0);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002680 }
2681
2682 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002683 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002684 if (vlan->vlan_ip) {
2685 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2686 vlan->vlan_ip, vlan->vlan_id);
2687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 }
2689}
2690
Al Virod3bb52b2007-08-22 20:06:58 -04002691static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002692{
2693 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002694 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002695
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002696 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002697 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
Harvey Harrison63779432008-10-31 00:56:00 -07002698 &sip, &tip, i, &targets[i], bond_has_this_ip(bond, tip));
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002699 if (sip == targets[i]) {
2700 if (bond_has_this_ip(bond, tip))
2701 slave->last_arp_rx = jiffies;
2702 return;
2703 }
2704 }
2705}
2706
2707static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2708{
2709 struct arphdr *arp;
2710 struct slave *slave;
2711 struct bonding *bond;
2712 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002713 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002714
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002715 if (dev_net(dev) != &init_net)
Eric W. Biedermane730c152007-09-17 11:53:39 -07002716 goto out;
2717
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002718 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2719 goto out;
2720
Wang Chen454d7c92008-11-12 23:37:49 -08002721 bond = netdev_priv(dev);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002722 read_lock(&bond->lock);
2723
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002724 pr_debug("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002725 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2726 orig_dev ? orig_dev->name : "NULL");
2727
2728 slave = bond_get_slave_by_dev(bond, orig_dev);
2729 if (!slave || !slave_do_arp_validate(bond, slave))
2730 goto out_unlock;
2731
Pavel Emelyanov988b7052008-03-03 12:20:57 -08002732 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002733 goto out_unlock;
2734
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002735 arp = arp_hdr(skb);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002736 if (arp->ar_hln != dev->addr_len ||
2737 skb->pkt_type == PACKET_OTHERHOST ||
2738 skb->pkt_type == PACKET_LOOPBACK ||
2739 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2740 arp->ar_pro != htons(ETH_P_IP) ||
2741 arp->ar_pln != 4)
2742 goto out_unlock;
2743
2744 arp_ptr = (unsigned char *)(arp + 1);
2745 arp_ptr += dev->addr_len;
2746 memcpy(&sip, arp_ptr, 4);
2747 arp_ptr += 4 + dev->addr_len;
2748 memcpy(&tip, arp_ptr, 4);
2749
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002750 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
Harvey Harrison63779432008-10-31 00:56:00 -07002751 bond->dev->name, slave->dev->name, slave->state,
2752 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2753 &sip, &tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002754
2755 /*
2756 * Backup slaves won't see the ARP reply, but do come through
2757 * here for each ARP probe (so we swap the sip/tip to validate
2758 * the probe). In a "redundant switch, common router" type of
2759 * configuration, the ARP probe will (hopefully) travel from
2760 * the active, through one switch, the router, then the other
2761 * switch before reaching the backup.
2762 */
2763 if (slave->state == BOND_STATE_ACTIVE)
2764 bond_validate_arp(bond, slave, sip, tip);
2765 else
2766 bond_validate_arp(bond, slave, tip, sip);
2767
2768out_unlock:
2769 read_unlock(&bond->lock);
2770out:
2771 dev_kfree_skb(skb);
2772 return NET_RX_SUCCESS;
2773}
2774
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775/*
2776 * this function is called regularly to monitor each slave's link
2777 * ensuring that traffic is being sent and received when arp monitoring
2778 * is used in load-balancing mode. if the adapter has been dormant, then an
2779 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2780 * arp monitoring in active backup mode.
2781 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002782void bond_loadbalance_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002784 struct bonding *bond = container_of(work, struct bonding,
2785 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 struct slave *slave, *oldcurrent;
2787 int do_failover = 0;
2788 int delta_in_ticks;
2789 int i;
2790
2791 read_lock(&bond->lock);
2792
Jay Vosburgh5ce0da82008-05-17 21:10:07 -07002793 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002795 if (bond->kill_timers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002798 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
2801 read_lock(&bond->curr_slave_lock);
2802 oldcurrent = bond->curr_active_slave;
2803 read_unlock(&bond->curr_slave_lock);
2804
2805 /* see if any of the previous devices are up now (i.e. they have
2806 * xmt and rcv traffic). the curr_active_slave does not come into
2807 * the picture unless it is null. also, slave->jiffies is not needed
2808 * here because we send an arp on each slave and give a slave as
2809 * long as it needs to get the tx/rx within the delta.
2810 * TODO: what about up/down delay in arp mode? it wasn't here before
2811 * so it can wait
2812 */
2813 bond_for_each_slave(bond, slave, i) {
2814 if (slave->link != BOND_LINK_UP) {
Eric Dumazet9d214932009-05-17 20:55:16 -07002815 if (time_before_eq(jiffies, dev_trans_start(slave->dev) + delta_in_ticks) &&
David Sterbab63bb732007-12-06 23:40:33 -08002816 time_before_eq(jiffies, slave->dev->last_rx + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
2818 slave->link = BOND_LINK_UP;
2819 slave->state = BOND_STATE_ACTIVE;
2820
2821 /* primary_slave has no meaning in round-robin
2822 * mode. the window of a slave being up and
2823 * curr_active_slave being null after enslaving
2824 * is closed.
2825 */
2826 if (!oldcurrent) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002827 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 ": %s: link status definitely "
2829 "up for interface %s, ",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002830 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 slave->dev->name);
2832 do_failover = 1;
2833 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002834 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 ": %s: interface %s is now up\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002836 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 slave->dev->name);
2838 }
2839 }
2840 } else {
2841 /* slave->link == BOND_LINK_UP */
2842
2843 /* not all switches will respond to an arp request
2844 * when the source ip is 0, so don't take the link down
2845 * if we don't know our ip yet
2846 */
Eric Dumazet9d214932009-05-17 20:55:16 -07002847 if (time_after_eq(jiffies, dev_trans_start(slave->dev) + 2*delta_in_ticks) ||
Jay Vosburgh4b8a9232008-05-17 21:10:08 -07002848 (time_after_eq(jiffies, slave->dev->last_rx + 2*delta_in_ticks))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 slave->link = BOND_LINK_DOWN;
2851 slave->state = BOND_STATE_BACKUP;
2852
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002853 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002856 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 ": %s: interface %s is now down.\n",
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002858 bond->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 slave->dev->name);
2860
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002861 if (slave == oldcurrent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 do_failover = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 }
2864 }
2865
2866 /* note: if switch is in round-robin mode, all links
2867 * must tx arp to ensure all links rx an arp - otherwise
2868 * links may oscillate or not come up at all; if switch is
2869 * in something like xor mode, there is nothing we can
2870 * do - all replies will be rx'ed on same link causing slaves
2871 * to be unstable during low/no traffic periods
2872 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002873 if (IS_UP(slave->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 bond_arp_send_all(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
2876
2877 if (do_failover) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002878 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880 bond_select_active_slave(bond);
2881
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002882 write_unlock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 }
2884
2885re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002886 if (bond->params.arp_interval)
2887 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888out:
2889 read_unlock(&bond->lock);
2890}
2891
2892/*
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002893 * Called to inspect slaves for active-backup mode ARP monitor link state
2894 * changes. Sets new_link in slaves to specify what action should take
2895 * place for the slave. Returns 0 if no changes are found, >0 if changes
2896 * to link states must be committed.
2897 *
2898 * Called with bond->lock held for read.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002900static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 struct slave *slave;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002903 int i, commit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 bond_for_each_slave(bond, slave, i) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002906 slave->new_link = BOND_LINK_NOCHANGE;
2907
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 if (slave->link != BOND_LINK_UP) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002909 if (time_before_eq(jiffies, slave_last_rx(bond, slave) +
2910 delta_in_ticks)) {
2911 slave->new_link = BOND_LINK_UP;
2912 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002915 continue;
2916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002918 /*
2919 * Give slaves 2*delta after being enslaved or made
2920 * active. This avoids bouncing, as the last receive
2921 * times need a full ARP monitor cycle to be updated.
2922 */
2923 if (!time_after_eq(jiffies, slave->jiffies +
2924 2 * delta_in_ticks))
2925 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002927 /*
2928 * Backup slave is down if:
2929 * - No current_arp_slave AND
2930 * - more than 3*delta since last receive AND
2931 * - the bond has an IP address
2932 *
2933 * Note: a non-null current_arp_slave indicates
2934 * the curr_active_slave went down and we are
2935 * searching for a new one; under this condition
2936 * we only take the curr_active_slave down - this
2937 * gives each slave a chance to tx/rx traffic
2938 * before being taken out
2939 */
2940 if (slave->state == BOND_STATE_BACKUP &&
2941 !bond->current_arp_slave &&
2942 time_after(jiffies, slave_last_rx(bond, slave) +
2943 3 * delta_in_ticks)) {
2944 slave->new_link = BOND_LINK_DOWN;
2945 commit++;
2946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002948 /*
2949 * Active slave is down if:
2950 * - more than 2*delta since transmitting OR
2951 * - (more than 2*delta since receive AND
2952 * the bond has an IP address)
2953 */
2954 if ((slave->state == BOND_STATE_ACTIVE) &&
Eric Dumazet9d214932009-05-17 20:55:16 -07002955 (time_after_eq(jiffies, dev_trans_start(slave->dev) +
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002956 2 * delta_in_ticks) ||
2957 (time_after_eq(jiffies, slave_last_rx(bond, slave)
2958 + 2 * delta_in_ticks)))) {
2959 slave->new_link = BOND_LINK_DOWN;
2960 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 }
2962 }
2963
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002964 return commit;
2965}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002967/*
2968 * Called to commit link state changes noted by inspection step of
2969 * active-backup mode ARP monitor.
2970 *
2971 * Called with RTNL and bond->lock for read.
2972 */
2973static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
2974{
2975 struct slave *slave;
2976 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002978 bond_for_each_slave(bond, slave, i) {
2979 switch (slave->new_link) {
2980 case BOND_LINK_NOCHANGE:
2981 continue;
2982
2983 case BOND_LINK_UP:
Jiri Pirkob9f60252009-08-31 11:09:38 +00002984 if ((!bond->curr_active_slave &&
2985 time_before_eq(jiffies,
2986 dev_trans_start(slave->dev) +
2987 delta_in_ticks)) ||
2988 bond->curr_active_slave != slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002989 slave->link = BOND_LINK_UP;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002990 bond->current_arp_slave = NULL;
2991
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002992 pr_info(DRV_NAME
Jiri Pirkob9f60252009-08-31 11:09:38 +00002993 ": %s: link status definitely "
2994 "up for interface %s.\n",
2995 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002996
Jiri Pirkob9f60252009-08-31 11:09:38 +00002997 if (!bond->curr_active_slave ||
2998 (slave == bond->primary_slave))
2999 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003000
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003001 }
3002
Jiri Pirkob9f60252009-08-31 11:09:38 +00003003 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003004
3005 case BOND_LINK_DOWN:
3006 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003009 slave->link = BOND_LINK_DOWN;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003010 bond_set_slave_inactive_flags(slave);
3011
3012 pr_info(DRV_NAME
3013 ": %s: link status definitely down for "
3014 "interface %s, disabling it\n",
3015 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003016
3017 if (slave == bond->curr_active_slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003018 bond->current_arp_slave = NULL;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003019 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003020 }
Jiri Pirkob9f60252009-08-31 11:09:38 +00003021
3022 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003023
3024 default:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003025 pr_err(DRV_NAME
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003026 ": %s: impossible: new_link %d on slave %s\n",
3027 bond->dev->name, slave->new_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 slave->dev->name);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003029 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Jiri Pirkob9f60252009-08-31 11:09:38 +00003032do_failover:
3033 ASSERT_RTNL();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003034 write_lock_bh(&bond->curr_slave_lock);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003035 bond_select_active_slave(bond);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003036 write_unlock_bh(&bond->curr_slave_lock);
3037 }
3038
3039 bond_set_carrier(bond);
3040}
3041
3042/*
3043 * Send ARP probes for active-backup mode ARP monitor.
3044 *
3045 * Called with bond->lock held for read.
3046 */
3047static void bond_ab_arp_probe(struct bonding *bond)
3048{
3049 struct slave *slave;
3050 int i;
3051
3052 read_lock(&bond->curr_slave_lock);
3053
3054 if (bond->current_arp_slave && bond->curr_active_slave)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003055 pr_info(DRV_NAME "PROBE: c_arp %s && cas %s BAD\n",
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003056 bond->current_arp_slave->dev->name,
3057 bond->curr_active_slave->dev->name);
3058
3059 if (bond->curr_active_slave) {
3060 bond_arp_send_all(bond, bond->curr_active_slave);
3061 read_unlock(&bond->curr_slave_lock);
3062 return;
3063 }
3064
3065 read_unlock(&bond->curr_slave_lock);
3066
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 /* if we don't have a curr_active_slave, search for the next available
3068 * backup slave from the current_arp_slave and make it the candidate
3069 * for becoming the curr_active_slave
3070 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003071
3072 if (!bond->current_arp_slave) {
3073 bond->current_arp_slave = bond->first_slave;
3074 if (!bond->current_arp_slave)
3075 return;
3076 }
3077
3078 bond_set_slave_inactive_flags(bond->current_arp_slave);
3079
3080 /* search for next candidate */
3081 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3082 if (IS_UP(slave->dev)) {
3083 slave->link = BOND_LINK_BACK;
3084 bond_set_slave_active_flags(slave);
3085 bond_arp_send_all(bond, slave);
3086 slave->jiffies = jiffies;
3087 bond->current_arp_slave = slave;
3088 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 }
3090
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003091 /* if the link state is up at this point, we
3092 * mark it down - this can happen if we have
3093 * simultaneous link failures and
3094 * reselect_active_interface doesn't make this
3095 * one the current slave so it is still marked
3096 * up when it is actually down
3097 */
3098 if (slave->link == BOND_LINK_UP) {
3099 slave->link = BOND_LINK_DOWN;
3100 if (slave->link_failure_count < UINT_MAX)
3101 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003103 bond_set_slave_inactive_flags(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003105 pr_info(DRV_NAME
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003106 ": %s: backup interface %s is now down.\n",
3107 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 }
3109 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003110}
3111
3112void bond_activebackup_arp_mon(struct work_struct *work)
3113{
3114 struct bonding *bond = container_of(work, struct bonding,
3115 arp_work.work);
3116 int delta_in_ticks;
3117
3118 read_lock(&bond->lock);
3119
3120 if (bond->kill_timers)
3121 goto out;
3122
3123 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3124
3125 if (bond->slave_cnt == 0)
3126 goto re_arm;
3127
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003128 if (bond->send_grat_arp) {
3129 read_lock(&bond->curr_slave_lock);
3130 bond_send_gratuitous_arp(bond);
3131 read_unlock(&bond->curr_slave_lock);
3132 }
3133
Brian Haley305d5522008-11-04 17:51:14 -08003134 if (bond->send_unsol_na) {
3135 read_lock(&bond->curr_slave_lock);
3136 bond_send_unsolicited_na(bond);
3137 read_unlock(&bond->curr_slave_lock);
3138 }
3139
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003140 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3141 read_unlock(&bond->lock);
3142 rtnl_lock();
3143 read_lock(&bond->lock);
3144
3145 bond_ab_arp_commit(bond, delta_in_ticks);
3146
3147 read_unlock(&bond->lock);
3148 rtnl_unlock();
3149 read_lock(&bond->lock);
3150 }
3151
3152 bond_ab_arp_probe(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
3154re_arm:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003155 if (bond->params.arp_interval)
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003156 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157out:
3158 read_unlock(&bond->lock);
3159}
3160
3161/*------------------------------ proc/seq_file-------------------------------*/
3162
3163#ifdef CONFIG_PROC_FS
3164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00003166 __acquires(&dev_base_lock)
3167 __acquires(&bond->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168{
3169 struct bonding *bond = seq->private;
3170 loff_t off = 0;
3171 struct slave *slave;
3172 int i;
3173
3174 /* make sure the bond won't be taken away */
3175 read_lock(&dev_base_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003176 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003178 if (*pos == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 return SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
3181 bond_for_each_slave(bond, slave, i) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003182 if (++off == *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 return slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 }
3185
3186 return NULL;
3187}
3188
3189static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3190{
3191 struct bonding *bond = seq->private;
3192 struct slave *slave = v;
3193
3194 ++*pos;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003195 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 return bond->first_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
3198 slave = slave->next;
3199
3200 return (slave == bond->first_slave) ? NULL : slave;
3201}
3202
3203static void bond_info_seq_stop(struct seq_file *seq, void *v)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00003204 __releases(&bond->lock)
3205 __releases(&dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
3207 struct bonding *bond = seq->private;
3208
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003209 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 read_unlock(&dev_base_lock);
3211}
3212
3213static void bond_info_show_master(struct seq_file *seq)
3214{
3215 struct bonding *bond = seq->private;
3216 struct slave *curr;
Mitch Williams4756b022005-11-09 10:36:25 -08003217 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
3219 read_lock(&bond->curr_slave_lock);
3220 curr = bond->curr_active_slave;
3221 read_unlock(&bond->curr_slave_lock);
3222
Jay Vosburghdd957c52007-10-09 19:57:24 -07003223 seq_printf(seq, "Bonding Mode: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 bond_mode_name(bond->params.mode));
3225
Jay Vosburghdd957c52007-10-09 19:57:24 -07003226 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
3227 bond->params.fail_over_mac)
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07003228 seq_printf(seq, " (fail_over_mac %s)",
3229 fail_over_mac_tbl[bond->params.fail_over_mac].modename);
Jay Vosburghdd957c52007-10-09 19:57:24 -07003230
3231 seq_printf(seq, "\n");
3232
Mitch Williamsc61b75a2005-11-09 10:35:13 -08003233 if (bond->params.mode == BOND_MODE_XOR ||
3234 bond->params.mode == BOND_MODE_8023AD) {
3235 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3236 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3237 bond->params.xmit_policy);
3238 }
3239
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 if (USES_PRIMARY(bond->params.mode)) {
Jiri Pirkoa5499522009-09-25 03:28:09 +00003241 seq_printf(seq, "Primary Slave: %s",
Mitch Williams0f418b22005-11-09 10:35:21 -08003242 (bond->primary_slave) ?
3243 bond->primary_slave->dev->name : "None");
Jiri Pirkoa5499522009-09-25 03:28:09 +00003244 if (bond->primary_slave)
3245 seq_printf(seq, " (primary_reselect %s)",
3246 pri_reselect_tbl[bond->params.primary_reselect].modename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Jiri Pirkoa5499522009-09-25 03:28:09 +00003248 seq_printf(seq, "\nCurrently Active Slave: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 (curr) ? curr->dev->name : "None");
3250 }
3251
Jay Vosburghff59c452006-03-27 13:27:43 -08003252 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3253 "up" : "down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3255 seq_printf(seq, "Up Delay (ms): %d\n",
3256 bond->params.updelay * bond->params.miimon);
3257 seq_printf(seq, "Down Delay (ms): %d\n",
3258 bond->params.downdelay * bond->params.miimon);
3259
Mitch Williams4756b022005-11-09 10:36:25 -08003260
3261 /* ARP information */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003262 if (bond->params.arp_interval > 0) {
3263 int printed = 0;
Mitch Williams4756b022005-11-09 10:36:25 -08003264 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3265 bond->params.arp_interval);
3266
3267 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3268
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003269 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
Mitch Williams4756b022005-11-09 10:36:25 -08003270 if (!bond->params.arp_targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07003271 break;
Mitch Williams4756b022005-11-09 10:36:25 -08003272 if (printed)
3273 seq_printf(seq, ",");
Harvey Harrison8cf14e32008-10-29 22:43:33 -07003274 seq_printf(seq, " %pI4", &bond->params.arp_targets[i]);
Mitch Williams4756b022005-11-09 10:36:25 -08003275 printed = 1;
3276 }
3277 seq_printf(seq, "\n");
3278 }
3279
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 if (bond->params.mode == BOND_MODE_8023AD) {
3281 struct ad_info ad_info;
3282
3283 seq_puts(seq, "\n802.3ad info\n");
3284 seq_printf(seq, "LACP rate: %s\n",
3285 (bond->params.lacp_fast) ? "fast" : "slow");
Jay Vosburghfd989c82008-11-04 17:51:16 -08003286 seq_printf(seq, "Aggregator selection policy (ad_select): %s\n",
3287 ad_select_tbl[bond->params.ad_select].modename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
3289 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3290 seq_printf(seq, "bond %s has no active aggregator\n",
3291 bond->dev->name);
3292 } else {
3293 seq_printf(seq, "Active Aggregator Info:\n");
3294
3295 seq_printf(seq, "\tAggregator ID: %d\n",
3296 ad_info.aggregator_id);
3297 seq_printf(seq, "\tNumber of ports: %d\n",
3298 ad_info.ports);
3299 seq_printf(seq, "\tActor Key: %d\n",
3300 ad_info.actor_key);
3301 seq_printf(seq, "\tPartner Key: %d\n",
3302 ad_info.partner_key);
Johannes Berge1749612008-10-27 15:59:26 -07003303 seq_printf(seq, "\tPartner Mac Address: %pM\n",
3304 ad_info.partner_system);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 }
3306 }
3307}
3308
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003309static void bond_info_show_slave(struct seq_file *seq,
3310 const struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311{
3312 struct bonding *bond = seq->private;
3313
3314 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3315 seq_printf(seq, "MII Status: %s\n",
3316 (slave->link == BOND_LINK_UP) ? "up" : "down");
Kenzo Iwami655096452006-09-22 21:53:08 -07003317 seq_printf(seq, "Link Failure Count: %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 slave->link_failure_count);
3319
Johannes Berge1749612008-10-27 15:59:26 -07003320 seq_printf(seq, "Permanent HW addr: %pM\n", slave->perm_hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
3322 if (bond->params.mode == BOND_MODE_8023AD) {
3323 const struct aggregator *agg
3324 = SLAVE_AD_INFO(slave).port.aggregator;
3325
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003326 if (agg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 seq_printf(seq, "Aggregator ID: %d\n",
3328 agg->aggregator_identifier);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003329 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 seq_puts(seq, "Aggregator ID: N/A\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 }
3332}
3333
3334static int bond_info_seq_show(struct seq_file *seq, void *v)
3335{
3336 if (v == SEQ_START_TOKEN) {
3337 seq_printf(seq, "%s\n", version);
3338 bond_info_show_master(seq);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003339 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 bond_info_show_slave(seq, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
3342 return 0;
3343}
3344
Jan Engelhardt4101dec2009-01-14 13:52:18 -08003345static const struct seq_operations bond_info_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 .start = bond_info_seq_start,
3347 .next = bond_info_seq_next,
3348 .stop = bond_info_seq_stop,
3349 .show = bond_info_seq_show,
3350};
3351
3352static int bond_info_open(struct inode *inode, struct file *file)
3353{
3354 struct seq_file *seq;
3355 struct proc_dir_entry *proc;
3356 int res;
3357
3358 res = seq_open(file, &bond_info_seq_ops);
3359 if (!res) {
3360 /* recover the pointer buried in proc_dir_entry data */
3361 seq = file->private_data;
3362 proc = PDE(inode);
3363 seq->private = proc->data;
3364 }
3365
3366 return res;
3367}
3368
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08003369static const struct file_operations bond_info_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 .owner = THIS_MODULE,
3371 .open = bond_info_open,
3372 .read = seq_read,
3373 .llseek = seq_lseek,
3374 .release = seq_release,
3375};
3376
Nicolas de Pesloüan38fc0022009-10-13 00:45:06 -07003377static void bond_create_proc_entry(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378{
3379 struct net_device *bond_dev = bond->dev;
3380
3381 if (bond_proc_dir) {
Denis V. Luneva95609c2008-04-29 01:02:29 -07003382 bond->proc_entry = proc_create_data(bond_dev->name,
3383 S_IRUGO, bond_proc_dir,
3384 &bond_info_fops, bond);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003385 if (bond->proc_entry == NULL)
3386 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 ": Warning: Cannot create /proc/net/%s/%s\n",
3388 DRV_NAME, bond_dev->name);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003389 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392}
3393
3394static void bond_remove_proc_entry(struct bonding *bond)
3395{
3396 if (bond_proc_dir && bond->proc_entry) {
3397 remove_proc_entry(bond->proc_file_name, bond_proc_dir);
3398 memset(bond->proc_file_name, 0, IFNAMSIZ);
3399 bond->proc_entry = NULL;
3400 }
3401}
3402
3403/* Create the bonding directory under /proc/net, if doesn't exist yet.
3404 * Caller must hold rtnl_lock.
3405 */
3406static void bond_create_proc_dir(void)
3407{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 if (!bond_proc_dir) {
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003409 bond_proc_dir = proc_mkdir(DRV_NAME, init_net.proc_net);
Alexey Dobriyan99b76232009-03-25 22:48:06 +03003410 if (!bond_proc_dir)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003411 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 ": Warning: cannot create /proc/net/%s\n",
3413 DRV_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 }
3415}
3416
3417/* Destroy the bonding directory under /proc/net, if empty.
3418 * Caller must hold rtnl_lock.
3419 */
3420static void bond_destroy_proc_dir(void)
3421{
Alexey Dobriyan99b76232009-03-25 22:48:06 +03003422 if (bond_proc_dir) {
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02003423 remove_proc_entry(DRV_NAME, init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 bond_proc_dir = NULL;
3425 }
3426}
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003427
3428#else /* !CONFIG_PROC_FS */
3429
Nicolas de Pesloüan38fc0022009-10-13 00:45:06 -07003430static void bond_create_proc_entry(struct bonding *bond)
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003431{
3432}
3433
3434static void bond_remove_proc_entry(struct bonding *bond)
3435{
3436}
3437
3438static void bond_create_proc_dir(void)
3439{
3440}
3441
3442static void bond_destroy_proc_dir(void)
3443{
3444}
3445
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446#endif /* CONFIG_PROC_FS */
3447
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003448
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449/*-------------------------- netdev event handling --------------------------*/
3450
3451/*
3452 * Change device name
3453 */
3454static int bond_event_changename(struct bonding *bond)
3455{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 bond_remove_proc_entry(bond);
3457 bond_create_proc_entry(bond);
Stephen Hemminger7e083842009-06-12 19:02:46 +00003458
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 return NOTIFY_DONE;
3460}
3461
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003462static int bond_master_netdev_event(unsigned long event,
3463 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464{
Wang Chen454d7c92008-11-12 23:37:49 -08003465 struct bonding *event_bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
3467 switch (event) {
3468 case NETDEV_CHANGENAME:
3469 return bond_event_changename(event_bond);
3470 case NETDEV_UNREGISTER:
Jay Vosburgh3b96c852008-01-17 16:25:00 -08003471 bond_release_all(event_bond->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 break;
3473 default:
3474 break;
3475 }
3476
3477 return NOTIFY_DONE;
3478}
3479
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003480static int bond_slave_netdev_event(unsigned long event,
3481 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482{
3483 struct net_device *bond_dev = slave_dev->master;
Wang Chen454d7c92008-11-12 23:37:49 -08003484 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
3486 switch (event) {
3487 case NETDEV_UNREGISTER:
3488 if (bond_dev) {
Jay Vosburgh1284cd32007-10-15 16:44:27 -07003489 if (bond->setup_by_slave)
3490 bond_release_and_destroy(bond_dev, slave_dev);
3491 else
3492 bond_release(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 }
3494 break;
3495 case NETDEV_CHANGE:
Jay Vosburgh17d04502009-03-18 18:38:25 -07003496 if (bond->params.mode == BOND_MODE_8023AD || bond_is_lb(bond)) {
3497 struct slave *slave;
3498
3499 slave = bond_get_slave_by_dev(bond, slave_dev);
3500 if (slave) {
3501 u16 old_speed = slave->speed;
3502 u16 old_duplex = slave->duplex;
3503
3504 bond_update_speed_duplex(slave);
3505
3506 if (bond_is_lb(bond))
3507 break;
3508
3509 if (old_speed != slave->speed)
3510 bond_3ad_adapter_speed_changed(slave);
3511 if (old_duplex != slave->duplex)
3512 bond_3ad_adapter_duplex_changed(slave);
3513 }
3514 }
3515
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 break;
3517 case NETDEV_DOWN:
3518 /*
3519 * ... Or is it this?
3520 */
3521 break;
3522 case NETDEV_CHANGEMTU:
3523 /*
3524 * TODO: Should slaves be allowed to
3525 * independently alter their MTU? For
3526 * an active-backup bond, slaves need
3527 * not be the same type of device, so
3528 * MTUs may vary. For other modes,
3529 * slaves arguably should have the
3530 * same MTUs. To do this, we'd need to
3531 * take over the slave's change_mtu
3532 * function for the duration of their
3533 * servitude.
3534 */
3535 break;
3536 case NETDEV_CHANGENAME:
3537 /*
3538 * TODO: handle changing the primary's name
3539 */
3540 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003541 case NETDEV_FEAT_CHANGE:
3542 bond_compute_features(bond);
3543 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 default:
3545 break;
3546 }
3547
3548 return NOTIFY_DONE;
3549}
3550
3551/*
3552 * bond_netdev_event: handle netdev notifier chain events.
3553 *
3554 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003555 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 * locks for us to safely manipulate the slave devices (RTNL lock,
3557 * dev_probe_lock).
3558 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003559static int bond_netdev_event(struct notifier_block *this,
3560 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561{
3562 struct net_device *event_dev = (struct net_device *)ptr;
3563
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003564 if (dev_net(event_dev) != &init_net)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02003565 return NOTIFY_DONE;
3566
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003567 pr_debug("event_dev: %s, event: %lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 (event_dev ? event_dev->name : "None"),
3569 event);
3570
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003571 if (!(event_dev->priv_flags & IFF_BONDING))
3572 return NOTIFY_DONE;
3573
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 if (event_dev->flags & IFF_MASTER) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003575 pr_debug("IFF_MASTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 return bond_master_netdev_event(event, event_dev);
3577 }
3578
3579 if (event_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003580 pr_debug("IFF_SLAVE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 return bond_slave_netdev_event(event, event_dev);
3582 }
3583
3584 return NOTIFY_DONE;
3585}
3586
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003587/*
3588 * bond_inetaddr_event: handle inetaddr notifier chain events.
3589 *
3590 * We keep track of device IPs primarily to use as source addresses in
3591 * ARP monitor probes (rather than spewing out broadcasts all the time).
3592 *
3593 * We track one IP for the main device (if it has one), plus one per VLAN.
3594 */
3595static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3596{
3597 struct in_ifaddr *ifa = ptr;
3598 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003599 struct bonding *bond;
3600 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003601
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003602 if (dev_net(ifa->ifa_dev->dev) != &init_net)
Denis V. Lunev6133fb12008-02-28 20:46:17 -08003603 return NOTIFY_DONE;
3604
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003605 list_for_each_entry(bond, &bond_dev_list, bond_list) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003606 if (bond->dev == event_dev) {
3607 switch (event) {
3608 case NETDEV_UP:
3609 bond->master_ip = ifa->ifa_local;
3610 return NOTIFY_OK;
3611 case NETDEV_DOWN:
3612 bond->master_ip = bond_glean_dev_ip(bond->dev);
3613 return NOTIFY_OK;
3614 default:
3615 return NOTIFY_DONE;
3616 }
3617 }
3618
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003619 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08003620 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003621 if (vlan_dev == event_dev) {
3622 switch (event) {
3623 case NETDEV_UP:
3624 vlan->vlan_ip = ifa->ifa_local;
3625 return NOTIFY_OK;
3626 case NETDEV_DOWN:
3627 vlan->vlan_ip =
3628 bond_glean_dev_ip(vlan_dev);
3629 return NOTIFY_OK;
3630 default:
3631 return NOTIFY_DONE;
3632 }
3633 }
3634 }
3635 }
3636 return NOTIFY_DONE;
3637}
3638
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639static struct notifier_block bond_netdev_notifier = {
3640 .notifier_call = bond_netdev_event,
3641};
3642
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003643static struct notifier_block bond_inetaddr_notifier = {
3644 .notifier_call = bond_inetaddr_event,
3645};
3646
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647/*-------------------------- Packet type handling ---------------------------*/
3648
3649/* register to receive lacpdus on a bond */
3650static void bond_register_lacpdu(struct bonding *bond)
3651{
3652 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3653
3654 /* initialize packet type */
3655 pk_type->type = PKT_TYPE_LACPDU;
3656 pk_type->dev = bond->dev;
3657 pk_type->func = bond_3ad_lacpdu_recv;
3658
3659 dev_add_pack(pk_type);
3660}
3661
3662/* unregister to receive lacpdus on a bond */
3663static void bond_unregister_lacpdu(struct bonding *bond)
3664{
3665 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3666}
3667
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003668void bond_register_arp(struct bonding *bond)
3669{
3670 struct packet_type *pt = &bond->arp_mon_pt;
3671
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003672 if (pt->type)
3673 return;
3674
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003675 pt->type = htons(ETH_P_ARP);
Jay Vosburghe245cb72007-02-28 17:03:27 -08003676 pt->dev = bond->dev;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003677 pt->func = bond_arp_rcv;
3678 dev_add_pack(pt);
3679}
3680
3681void bond_unregister_arp(struct bonding *bond)
3682{
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003683 struct packet_type *pt = &bond->arp_mon_pt;
3684
3685 dev_remove_pack(pt);
3686 pt->type = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003687}
3688
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003689/*---------------------------- Hashing Policies -----------------------------*/
3690
3691/*
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003692 * Hash for the output device based upon layer 2 and layer 3 data. If
3693 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3694 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003695static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003696{
3697 struct ethhdr *data = (struct ethhdr *)skb->data;
3698 struct iphdr *iph = ip_hdr(skb);
3699
Brian Haleyf14c4e42008-09-02 10:08:08 -04003700 if (skb->protocol == htons(ETH_P_IP)) {
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003701 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
Jasper Spaansd3da6832009-10-23 04:08:46 +00003702 (data->h_dest[5] ^ data->h_source[5])) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003703 }
3704
Jasper Spaansd3da6832009-10-23 04:08:46 +00003705 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003706}
3707
3708/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003709 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003710 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3711 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3712 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003713static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003714{
3715 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003716 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003717 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003718 int layer4_xor = 0;
3719
Brian Haleyf14c4e42008-09-02 10:08:08 -04003720 if (skb->protocol == htons(ETH_P_IP)) {
3721 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003722 (iph->protocol == IPPROTO_TCP ||
3723 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003724 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003725 }
3726 return (layer4_xor ^
3727 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3728
3729 }
3730
Jasper Spaansd3da6832009-10-23 04:08:46 +00003731 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003732}
3733
3734/*
3735 * Hash for the output device based upon layer 2 data
3736 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003737static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003738{
3739 struct ethhdr *data = (struct ethhdr *)skb->data;
3740
Jasper Spaansd3da6832009-10-23 04:08:46 +00003741 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003742}
3743
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744/*-------------------------- Device entry points ----------------------------*/
3745
3746static int bond_open(struct net_device *bond_dev)
3747{
Wang Chen454d7c92008-11-12 23:37:49 -08003748 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
3750 bond->kill_timers = 0;
3751
Holger Eitzenberger58402052008-12-09 23:07:13 -08003752 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 /* bond_alb_initialize must be called before the timer
3754 * is started.
3755 */
3756 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3757 /* something went wrong - fail the open operation */
3758 return -1;
3759 }
3760
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003761 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3762 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 }
3764
3765 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003766 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3767 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 }
3769
3770 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003771 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3772 INIT_DELAYED_WORK(&bond->arp_work,
3773 bond_activebackup_arp_mon);
3774 else
3775 INIT_DELAYED_WORK(&bond->arp_work,
3776 bond_loadbalance_arp_mon);
3777
3778 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003779 if (bond->params.arp_validate)
3780 bond_register_arp(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 }
3782
3783 if (bond->params.mode == BOND_MODE_8023AD) {
Adrian Bunka40745f2007-10-24 18:27:43 +02003784 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003785 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 /* register to receive LACPDUs */
3787 bond_register_lacpdu(bond);
Jay Vosburghfd989c82008-11-04 17:51:16 -08003788 bond_3ad_initiate_agg_selection(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 }
3790
3791 return 0;
3792}
3793
3794static int bond_close(struct net_device *bond_dev)
3795{
Wang Chen454d7c92008-11-12 23:37:49 -08003796 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
3798 if (bond->params.mode == BOND_MODE_8023AD) {
3799 /* Unregister the receive of LACPDUs */
3800 bond_unregister_lacpdu(bond);
3801 }
3802
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003803 if (bond->params.arp_validate)
3804 bond_unregister_arp(bond);
3805
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 write_lock_bh(&bond->lock);
3807
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003808 bond->send_grat_arp = 0;
Brian Haley305d5522008-11-04 17:51:14 -08003809 bond->send_unsol_na = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810
3811 /* signal timers not to re-arm */
3812 bond->kill_timers = 1;
3813
3814 write_unlock_bh(&bond->lock);
3815
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003817 cancel_delayed_work(&bond->mii_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 }
3819
3820 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003821 cancel_delayed_work(&bond->arp_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 }
3823
3824 switch (bond->params.mode) {
3825 case BOND_MODE_8023AD:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003826 cancel_delayed_work(&bond->ad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 break;
3828 case BOND_MODE_TLB:
3829 case BOND_MODE_ALB:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003830 cancel_delayed_work(&bond->alb_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 break;
3832 default:
3833 break;
3834 }
3835
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
Holger Eitzenberger58402052008-12-09 23:07:13 -08003837 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 /* Must be called only after all
3839 * slaves have been released
3840 */
3841 bond_alb_deinitialize(bond);
3842 }
3843
3844 return 0;
3845}
3846
3847static struct net_device_stats *bond_get_stats(struct net_device *bond_dev)
3848{
Wang Chen454d7c92008-11-12 23:37:49 -08003849 struct bonding *bond = netdev_priv(bond_dev);
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08003850 struct net_device_stats *stats = &bond->stats;
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003851 struct net_device_stats local_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 struct slave *slave;
3853 int i;
3854
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003855 memset(&local_stats, 0, sizeof(struct net_device_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
3857 read_lock_bh(&bond->lock);
3858
3859 bond_for_each_slave(bond, slave, i) {
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08003860 const struct net_device_stats *sstats = dev_get_stats(slave->dev);
3861
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003862 local_stats.rx_packets += sstats->rx_packets;
3863 local_stats.rx_bytes += sstats->rx_bytes;
3864 local_stats.rx_errors += sstats->rx_errors;
3865 local_stats.rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003867 local_stats.tx_packets += sstats->tx_packets;
3868 local_stats.tx_bytes += sstats->tx_bytes;
3869 local_stats.tx_errors += sstats->tx_errors;
3870 local_stats.tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003872 local_stats.multicast += sstats->multicast;
3873 local_stats.collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003875 local_stats.rx_length_errors += sstats->rx_length_errors;
3876 local_stats.rx_over_errors += sstats->rx_over_errors;
3877 local_stats.rx_crc_errors += sstats->rx_crc_errors;
3878 local_stats.rx_frame_errors += sstats->rx_frame_errors;
3879 local_stats.rx_fifo_errors += sstats->rx_fifo_errors;
3880 local_stats.rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003882 local_stats.tx_aborted_errors += sstats->tx_aborted_errors;
3883 local_stats.tx_carrier_errors += sstats->tx_carrier_errors;
3884 local_stats.tx_fifo_errors += sstats->tx_fifo_errors;
3885 local_stats.tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3886 local_stats.tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 }
3888
Andy Gospodarek2439f9e2008-01-29 18:07:46 -08003889 memcpy(stats, &local_stats, sizeof(struct net_device_stats));
3890
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 read_unlock_bh(&bond->lock);
3892
3893 return stats;
3894}
3895
3896static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3897{
3898 struct net_device *slave_dev = NULL;
3899 struct ifbond k_binfo;
3900 struct ifbond __user *u_binfo = NULL;
3901 struct ifslave k_sinfo;
3902 struct ifslave __user *u_sinfo = NULL;
3903 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 int res = 0;
3905
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003906 pr_debug("bond_ioctl: master=%s, cmd=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 bond_dev->name, cmd);
3908
3909 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 case SIOCGMIIPHY:
3911 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003912 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003914
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 mii->phy_id = 0;
3916 /* Fall Through */
3917 case SIOCGMIIREG:
3918 /*
3919 * We do this again just in case we were called by SIOCGMIIREG
3920 * instead of SIOCGMIIPHY.
3921 */
3922 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003923 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003925
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
3927 if (mii->reg_num == 1) {
Wang Chen454d7c92008-11-12 23:37:49 -08003928 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 mii->val_out = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003930 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 read_lock(&bond->curr_slave_lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003932 if (netif_carrier_ok(bond->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 mii->val_out = BMSR_LSTATUS;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003934
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 read_unlock(&bond->curr_slave_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003936 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 }
3938
3939 return 0;
3940 case BOND_INFO_QUERY_OLD:
3941 case SIOCBONDINFOQUERY:
3942 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3943
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003944 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
3947 res = bond_info_query(bond_dev, &k_binfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003948 if (res == 0 &&
3949 copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
3950 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
3952 return res;
3953 case BOND_SLAVE_INFO_QUERY_OLD:
3954 case SIOCBONDSLAVEINFOQUERY:
3955 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3956
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003957 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
3960 res = bond_slave_info_query(bond_dev, &k_sinfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003961 if (res == 0 &&
3962 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
3963 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964
3965 return res;
3966 default:
3967 /* Go on */
3968 break;
3969 }
3970
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003971 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
Eric W. Biederman881d9662007-09-17 11:56:21 -07003974 slave_dev = dev_get_by_name(&init_net, ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003976 pr_debug("slave_dev=%p: \n", slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003978 if (!slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 res = -ENODEV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003980 else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003981 pr_debug("slave_dev->name=%s: \n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 switch (cmd) {
3983 case BOND_ENSLAVE_OLD:
3984 case SIOCBONDENSLAVE:
3985 res = bond_enslave(bond_dev, slave_dev);
3986 break;
3987 case BOND_RELEASE_OLD:
3988 case SIOCBONDRELEASE:
3989 res = bond_release(bond_dev, slave_dev);
3990 break;
3991 case BOND_SETHWADDR_OLD:
3992 case SIOCBONDSETHWADDR:
3993 res = bond_sethwaddr(bond_dev, slave_dev);
3994 break;
3995 case BOND_CHANGE_ACTIVE_OLD:
3996 case SIOCBONDCHANGEACTIVE:
3997 res = bond_ioctl_change_active(bond_dev, slave_dev);
3998 break;
3999 default:
4000 res = -EOPNOTSUPP;
4001 }
4002
4003 dev_put(slave_dev);
4004 }
4005
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 return res;
4007}
4008
4009static void bond_set_multicast_list(struct net_device *bond_dev)
4010{
Wang Chen454d7c92008-11-12 23:37:49 -08004011 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 struct dev_mc_list *dmi;
4013
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 /*
4015 * Do promisc before checking multicast_mode
4016 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004017 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07004018 /*
4019 * FIXME: Need to handle the error when one of the multi-slaves
4020 * encounters error.
4021 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 bond_set_promiscuity(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004024
4025 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 bond_set_promiscuity(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004027
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
4029 /* set allmulti flag to slaves */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004030 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07004031 /*
4032 * FIXME: Need to handle the error when one of the multi-slaves
4033 * encounters error.
4034 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 bond_set_allmulti(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004037
4038 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 bond_set_allmulti(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004040
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004042 read_lock(&bond->lock);
4043
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 bond->flags = bond_dev->flags;
4045
4046 /* looking for addresses to add to slaves' mc list */
4047 for (dmi = bond_dev->mc_list; dmi; dmi = dmi->next) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004048 if (!bond_mc_list_find_dmi(dmi, bond->mc_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 }
4051
4052 /* looking for addresses to delete from slaves' list */
4053 for (dmi = bond->mc_list; dmi; dmi = dmi->next) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004054 if (!bond_mc_list_find_dmi(dmi, bond_dev->mc_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 }
4057
4058 /* save master's multicast list */
4059 bond_mc_list_destroy(bond);
4060 bond_mc_list_copy(bond_dev->mc_list, bond, GFP_ATOMIC);
4061
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004062 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063}
4064
Stephen Hemminger00829822008-11-20 20:14:53 -08004065static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
4066{
4067 struct bonding *bond = netdev_priv(dev);
4068 struct slave *slave = bond->first_slave;
4069
4070 if (slave) {
4071 const struct net_device_ops *slave_ops
4072 = slave->dev->netdev_ops;
4073 if (slave_ops->ndo_neigh_setup)
Patrick McHardy72e22402009-03-05 01:57:44 -08004074 return slave_ops->ndo_neigh_setup(slave->dev, parms);
Stephen Hemminger00829822008-11-20 20:14:53 -08004075 }
4076 return 0;
4077}
4078
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079/*
4080 * Change the MTU of all of a master's slaves to match the master
4081 */
4082static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
4083{
Wang Chen454d7c92008-11-12 23:37:49 -08004084 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 struct slave *slave, *stop_at;
4086 int res = 0;
4087 int i;
4088
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004089 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 (bond_dev ? bond_dev->name : "None"), new_mtu);
4091
4092 /* Can't hold bond->lock with bh disabled here since
4093 * some base drivers panic. On the other hand we can't
4094 * hold bond->lock without bh disabled because we'll
4095 * deadlock. The only solution is to rely on the fact
4096 * that we're under rtnl_lock here, and the slaves
4097 * list won't change. This doesn't solve the problem
4098 * of setting the slave's MTU while it is
4099 * transmitting, but the assumption is that the base
4100 * driver can handle that.
4101 *
4102 * TODO: figure out a way to safely iterate the slaves
4103 * list, but without holding a lock around the actual
4104 * call to the base driver.
4105 */
4106
4107 bond_for_each_slave(bond, slave, i) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004108 pr_debug("s %p s->p %p c_m %p\n", slave,
Stephen Hemminger53a32942009-01-06 10:41:56 -08004109 slave->prev, slave->dev->netdev_ops->ndo_change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08004110
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 res = dev_set_mtu(slave->dev, new_mtu);
4112
4113 if (res) {
4114 /* If we failed to set the slave's mtu to the new value
4115 * we must abort the operation even in ACTIVE_BACKUP
4116 * mode, because if we allow the backup slaves to have
4117 * different mtu values than the active slave we'll
4118 * need to change their mtu when doing a failover. That
4119 * means changing their mtu from timer context, which
4120 * is probably not a good idea.
4121 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004122 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 goto unwind;
4124 }
4125 }
4126
4127 bond_dev->mtu = new_mtu;
4128
4129 return 0;
4130
4131unwind:
4132 /* unwind from head to the slave that failed */
4133 stop_at = slave;
4134 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4135 int tmp_res;
4136
4137 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4138 if (tmp_res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004139 pr_debug("unwind err %d dev %s\n", tmp_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 slave->dev->name);
4141 }
4142 }
4143
4144 return res;
4145}
4146
4147/*
4148 * Change HW address
4149 *
4150 * Note that many devices must be down to change the HW address, and
4151 * downing the master releases all slaves. We can make bonds full of
4152 * bonding devices to test this, however.
4153 */
4154static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4155{
Wang Chen454d7c92008-11-12 23:37:49 -08004156 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 struct sockaddr *sa = addr, tmp_sa;
4158 struct slave *slave, *stop_at;
4159 int res = 0;
4160 int i;
4161
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004162 if (bond->params.mode == BOND_MODE_ALB)
4163 return bond_alb_set_mac_address(bond_dev, addr);
4164
4165
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004166 pr_debug("bond=%p, name=%s\n", bond, (bond_dev ? bond_dev->name : "None"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
Jay Vosburghdd957c52007-10-09 19:57:24 -07004168 /*
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004169 * If fail_over_mac is set to active, do nothing and return
4170 * success. Returning an error causes ifenslave to fail.
Jay Vosburghdd957c52007-10-09 19:57:24 -07004171 */
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004172 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
Jay Vosburghdd957c52007-10-09 19:57:24 -07004173 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07004174
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004175 if (!is_valid_ether_addr(sa->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
4178 /* Can't hold bond->lock with bh disabled here since
4179 * some base drivers panic. On the other hand we can't
4180 * hold bond->lock without bh disabled because we'll
4181 * deadlock. The only solution is to rely on the fact
4182 * that we're under rtnl_lock here, and the slaves
4183 * list won't change. This doesn't solve the problem
4184 * of setting the slave's hw address while it is
4185 * transmitting, but the assumption is that the base
4186 * driver can handle that.
4187 *
4188 * TODO: figure out a way to safely iterate the slaves
4189 * list, but without holding a lock around the actual
4190 * call to the base driver.
4191 */
4192
4193 bond_for_each_slave(bond, slave, i) {
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004194 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004195 pr_debug("slave %p %s\n", slave, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004197 if (slave_ops->ndo_set_mac_address == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 res = -EOPNOTSUPP;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004199 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 goto unwind;
4201 }
4202
4203 res = dev_set_mac_address(slave->dev, addr);
4204 if (res) {
4205 /* TODO: consider downing the slave
4206 * and retry ?
4207 * User should expect communications
4208 * breakage anyway until ARP finish
4209 * updating, so...
4210 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004211 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 goto unwind;
4213 }
4214 }
4215
4216 /* success */
4217 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4218 return 0;
4219
4220unwind:
4221 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4222 tmp_sa.sa_family = bond_dev->type;
4223
4224 /* unwind from head to the slave that failed */
4225 stop_at = slave;
4226 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4227 int tmp_res;
4228
4229 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4230 if (tmp_res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004231 pr_debug("unwind err %d dev %s\n", tmp_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 slave->dev->name);
4233 }
4234 }
4235
4236 return res;
4237}
4238
4239static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4240{
Wang Chen454d7c92008-11-12 23:37:49 -08004241 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 struct slave *slave, *start_at;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004243 int i, slave_no, res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244
4245 read_lock(&bond->lock);
4246
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004247 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004250 /*
4251 * Concurrent TX may collide on rr_tx_counter; we accept that
4252 * as being rare enough not to justify using an atomic op here
4253 */
4254 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004256 bond_for_each_slave(bond, slave, i) {
4257 slave_no--;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004258 if (slave_no < 0)
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004259 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 }
4261
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004262 start_at = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 bond_for_each_slave_from(bond, slave, i, start_at) {
4264 if (IS_UP(slave->dev) &&
4265 (slave->link == BOND_LINK_UP) &&
4266 (slave->state == BOND_STATE_ACTIVE)) {
4267 res = bond_dev_queue_xmit(bond, skb, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 break;
4269 }
4270 }
4271
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272out:
4273 if (res) {
4274 /* no suitable interface, frame not sent */
4275 dev_kfree_skb(skb);
4276 }
4277 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004278 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279}
4280
John W. Linville075897c2005-09-28 17:50:53 -04004281
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282/*
4283 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4284 * the bond has a usable interface.
4285 */
4286static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4287{
Wang Chen454d7c92008-11-12 23:37:49 -08004288 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 int res = 1;
4290
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 read_lock(&bond->lock);
4292 read_lock(&bond->curr_slave_lock);
4293
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004294 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296
John W. Linville075897c2005-09-28 17:50:53 -04004297 if (!bond->curr_active_slave)
4298 goto out;
4299
John W. Linville075897c2005-09-28 17:50:53 -04004300 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4301
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004303 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 /* no suitable interface, frame not sent */
4305 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004306
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 read_unlock(&bond->curr_slave_lock);
4308 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004309 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310}
4311
4312/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004313 * In bond_xmit_xor() , we determine the output device by using a pre-
4314 * determined xmit_hash_policy(), If the selected device is not enabled,
4315 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 */
4317static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4318{
Wang Chen454d7c92008-11-12 23:37:49 -08004319 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 struct slave *slave, *start_at;
4321 int slave_no;
4322 int i;
4323 int res = 1;
4324
4325 read_lock(&bond->lock);
4326
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004327 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329
Jasper Spaansa361c832009-10-23 04:09:24 +00004330 slave_no = bond->xmit_hash_policy(skb, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331
4332 bond_for_each_slave(bond, slave, i) {
4333 slave_no--;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004334 if (slave_no < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 }
4337
4338 start_at = slave;
4339
4340 bond_for_each_slave_from(bond, slave, i, start_at) {
4341 if (IS_UP(slave->dev) &&
4342 (slave->link == BOND_LINK_UP) &&
4343 (slave->state == BOND_STATE_ACTIVE)) {
4344 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4345 break;
4346 }
4347 }
4348
4349out:
4350 if (res) {
4351 /* no suitable interface, frame not sent */
4352 dev_kfree_skb(skb);
4353 }
4354 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004355 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356}
4357
4358/*
4359 * in broadcast mode, we send everything to all usable interfaces.
4360 */
4361static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4362{
Wang Chen454d7c92008-11-12 23:37:49 -08004363 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 struct slave *slave, *start_at;
4365 struct net_device *tx_dev = NULL;
4366 int i;
4367 int res = 1;
4368
4369 read_lock(&bond->lock);
4370
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004371 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
4374 read_lock(&bond->curr_slave_lock);
4375 start_at = bond->curr_active_slave;
4376 read_unlock(&bond->curr_slave_lock);
4377
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004378 if (!start_at)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380
4381 bond_for_each_slave_from(bond, slave, i, start_at) {
4382 if (IS_UP(slave->dev) &&
4383 (slave->link == BOND_LINK_UP) &&
4384 (slave->state == BOND_STATE_ACTIVE)) {
4385 if (tx_dev) {
4386 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4387 if (!skb2) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004388 pr_err(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08004389 ": %s: Error: bond_xmit_broadcast(): "
4390 "skb_clone() failed\n",
4391 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 continue;
4393 }
4394
4395 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4396 if (res) {
4397 dev_kfree_skb(skb2);
4398 continue;
4399 }
4400 }
4401 tx_dev = slave->dev;
4402 }
4403 }
4404
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004405 if (tx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 res = bond_dev_queue_xmit(bond, skb, tx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407
4408out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004409 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 /* no suitable interface, frame not sent */
4411 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004412
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 /* frame sent to all suitable interfaces */
4414 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004415 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416}
4417
4418/*------------------------- Device initialization ---------------------------*/
4419
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004420static void bond_set_xmit_hash_policy(struct bonding *bond)
4421{
4422 switch (bond->params.xmit_policy) {
4423 case BOND_XMIT_POLICY_LAYER23:
4424 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4425 break;
4426 case BOND_XMIT_POLICY_LAYER34:
4427 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4428 break;
4429 case BOND_XMIT_POLICY_LAYER2:
4430 default:
4431 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4432 break;
4433 }
4434}
4435
Stephen Hemminger424efe92009-08-31 19:50:51 +00004436static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
Stephen Hemminger00829822008-11-20 20:14:53 -08004437{
4438 const struct bonding *bond = netdev_priv(dev);
4439
4440 switch (bond->params.mode) {
4441 case BOND_MODE_ROUNDROBIN:
4442 return bond_xmit_roundrobin(skb, dev);
4443 case BOND_MODE_ACTIVEBACKUP:
4444 return bond_xmit_activebackup(skb, dev);
4445 case BOND_MODE_XOR:
4446 return bond_xmit_xor(skb, dev);
4447 case BOND_MODE_BROADCAST:
4448 return bond_xmit_broadcast(skb, dev);
4449 case BOND_MODE_8023AD:
4450 return bond_3ad_xmit_xor(skb, dev);
4451 case BOND_MODE_ALB:
4452 case BOND_MODE_TLB:
4453 return bond_alb_xmit(skb, dev);
4454 default:
4455 /* Should never happen, mode already checked */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004456 pr_err(DRV_NAME ": %s: Error: Unknown bonding mode %d\n",
Stephen Hemminger00829822008-11-20 20:14:53 -08004457 dev->name, bond->params.mode);
4458 WARN_ON_ONCE(1);
4459 dev_kfree_skb(skb);
4460 return NETDEV_TX_OK;
4461 }
4462}
4463
4464
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465/*
4466 * set bond mode specific net device operations
4467 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004468void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004470 struct net_device *bond_dev = bond->dev;
4471
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 switch (mode) {
4473 case BOND_MODE_ROUNDROBIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 break;
4475 case BOND_MODE_ACTIVEBACKUP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 break;
4477 case BOND_MODE_XOR:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004478 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 break;
4480 case BOND_MODE_BROADCAST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 break;
4482 case BOND_MODE_8023AD:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004483 bond_set_master_3ad_flags(bond);
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004484 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004487 bond_set_master_alb_flags(bond);
4488 /* FALLTHRU */
4489 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 break;
4491 default:
4492 /* Should never happen, mode already checked */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004493 pr_err(DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08004494 ": %s: Error: Unknown bonding mode %d\n",
4495 bond_dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 mode);
4497 break;
4498 }
4499}
4500
Jay Vosburgh217df672005-09-26 16:11:50 -07004501static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4502 struct ethtool_drvinfo *drvinfo)
4503{
4504 strncpy(drvinfo->driver, DRV_NAME, 32);
4505 strncpy(drvinfo->version, DRV_VERSION, 32);
4506 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4507}
4508
Jeff Garzik7282d492006-09-13 14:30:00 -04004509static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004510 .get_drvinfo = bond_ethtool_get_drvinfo,
Stephen Hemmingerfa53eba2008-09-13 21:17:09 -04004511 .get_link = ethtool_op_get_link,
4512 .get_tx_csum = ethtool_op_get_tx_csum,
4513 .get_sg = ethtool_op_get_sg,
4514 .get_tso = ethtool_op_get_tso,
4515 .get_ufo = ethtool_op_get_ufo,
4516 .get_flags = ethtool_op_get_flags,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004517};
4518
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004519static const struct net_device_ops bond_netdev_ops = {
Stephen Hemminger181470f2009-06-12 19:02:52 +00004520 .ndo_init = bond_init,
Stephen Hemminger9e716262009-06-12 19:02:47 +00004521 .ndo_uninit = bond_uninit,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004522 .ndo_open = bond_open,
4523 .ndo_stop = bond_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08004524 .ndo_start_xmit = bond_start_xmit,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004525 .ndo_get_stats = bond_get_stats,
4526 .ndo_do_ioctl = bond_do_ioctl,
4527 .ndo_set_multicast_list = bond_set_multicast_list,
4528 .ndo_change_mtu = bond_change_mtu,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004529 .ndo_set_mac_address = bond_set_mac_address,
Stephen Hemminger00829822008-11-20 20:14:53 -08004530 .ndo_neigh_setup = bond_neigh_setup,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004531 .ndo_vlan_rx_register = bond_vlan_rx_register,
4532 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4533 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
4534};
4535
Stephen Hemminger181470f2009-06-12 19:02:52 +00004536static void bond_setup(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537{
Wang Chen454d7c92008-11-12 23:37:49 -08004538 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 /* initialize rwlocks */
4541 rwlock_init(&bond->lock);
4542 rwlock_init(&bond->curr_slave_lock);
4543
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00004544 bond->params = bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545
4546 /* Initialize pointers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 bond->dev = bond_dev;
4548 INIT_LIST_HEAD(&bond->vlan_list);
4549
4550 /* Initialize the device entry points */
Stephen Hemminger181470f2009-06-12 19:02:52 +00004551 ether_setup(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004552 bond_dev->netdev_ops = &bond_netdev_ops;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004553 bond_dev->ethtool_ops = &bond_ethtool_ops;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004554 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
Stephen Hemminger9e716262009-06-12 19:02:47 +00004556 bond_dev->destructor = free_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
4558 /* Initialize the device options */
4559 bond_dev->tx_queue_len = 0;
4560 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004561 bond_dev->priv_flags |= IFF_BONDING;
Stephen Hemminger181470f2009-06-12 19:02:52 +00004562 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
4563
Jay Vosburgh6cf3f412008-11-03 18:16:50 -08004564 if (bond->params.arp_interval)
4565 bond_dev->priv_flags |= IFF_MASTER_ARPMON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
4567 /* At first, we block adding VLANs. That's the only way to
4568 * prevent problems that occur when adding VLANs over an
4569 * empty bond. The block will be removed once non-challenged
4570 * slaves are enslaved.
4571 */
4572 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4573
Herbert Xu932ff272006-06-09 12:20:56 -07004574 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 * transmitting */
4576 bond_dev->features |= NETIF_F_LLTX;
4577
4578 /* By default, we declare the bond to be fully
4579 * VLAN hardware accelerated capable. Special
4580 * care is taken in the various xmit functions
4581 * when there are slaves that are not hw accel
4582 * capable
4583 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4585 NETIF_F_HW_VLAN_RX |
4586 NETIF_F_HW_VLAN_FILTER);
4587
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588}
4589
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004590static void bond_work_cancel_all(struct bonding *bond)
4591{
4592 write_lock_bh(&bond->lock);
4593 bond->kill_timers = 1;
4594 write_unlock_bh(&bond->lock);
4595
4596 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4597 cancel_delayed_work(&bond->mii_work);
4598
4599 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4600 cancel_delayed_work(&bond->arp_work);
4601
4602 if (bond->params.mode == BOND_MODE_ALB &&
4603 delayed_work_pending(&bond->alb_work))
4604 cancel_delayed_work(&bond->alb_work);
4605
4606 if (bond->params.mode == BOND_MODE_8023AD &&
4607 delayed_work_pending(&bond->ad_work))
4608 cancel_delayed_work(&bond->ad_work);
4609}
4610
Jay Vosburgha434e432008-10-30 17:41:15 -07004611/* De-initialize device specific data.
4612 * Caller must hold rtnl_lock.
4613 */
4614static void bond_deinit(struct net_device *bond_dev)
4615{
Wang Chen454d7c92008-11-12 23:37:49 -08004616 struct bonding *bond = netdev_priv(bond_dev);
Jay Vosburgha434e432008-10-30 17:41:15 -07004617
4618 list_del(&bond->bond_list);
4619
4620 bond_work_cancel_all(bond);
4621
Jay Vosburgha434e432008-10-30 17:41:15 -07004622 bond_remove_proc_entry(bond);
Jay Vosburgha434e432008-10-30 17:41:15 -07004623}
4624
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625/* Unregister and free all bond devices.
4626 * Caller must hold rtnl_lock.
4627 */
4628static void bond_free_all(void)
4629{
4630 struct bonding *bond, *nxt;
4631
4632 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
4633 struct net_device *bond_dev = bond->dev;
4634
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004635 bond_work_cancel_all(bond);
jamal702987052006-09-22 21:54:37 -07004636 /* Release the bonded slaves */
4637 bond_release_all(bond_dev);
Stephen Hemminger9e716262009-06-12 19:02:47 +00004638 unregister_netdevice(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 }
4640
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 bond_destroy_proc_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642}
4643
4644/*------------------------- Module initialization ---------------------------*/
4645
4646/*
4647 * Convert string input module parms. Accept either the
Jay Vosburghece95f72008-01-17 16:25:01 -08004648 * number of the mode or its string name. A bit complicated because
4649 * some mode names are substrings of other names, and calls from sysfs
4650 * may have whitespace in the name (trailing newlines, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 */
Holger Eitzenberger325dcf72008-12-09 23:10:17 -08004652int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653{
Hannes Eder54b87322009-02-14 11:15:49 +00004654 int modeint = -1, i, rv;
Jay Vosburgha42e5342008-01-29 18:07:43 -08004655 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
Jay Vosburghece95f72008-01-17 16:25:01 -08004656
Jay Vosburgha42e5342008-01-29 18:07:43 -08004657 for (p = (char *)buf; *p; p++)
4658 if (!(isdigit(*p) || isspace(*p)))
4659 break;
4660
4661 if (*p)
Jay Vosburghece95f72008-01-17 16:25:01 -08004662 rv = sscanf(buf, "%20s", modestr);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004663 else
Hannes Eder54b87322009-02-14 11:15:49 +00004664 rv = sscanf(buf, "%d", &modeint);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004665
4666 if (!rv)
4667 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
4669 for (i = 0; tbl[i].modename; i++) {
Hannes Eder54b87322009-02-14 11:15:49 +00004670 if (modeint == tbl[i].mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 return tbl[i].mode;
Jay Vosburghece95f72008-01-17 16:25:01 -08004672 if (strcmp(modestr, tbl[i].modename) == 0)
4673 return tbl[i].mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 }
4675
4676 return -1;
4677}
4678
4679static int bond_check_params(struct bond_params *params)
4680{
Jiri Pirkoa5499522009-09-25 03:28:09 +00004681 int arp_validate_value, fail_over_mac_value, primary_reselect_value;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004682
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 /*
4684 * Convert string parameters.
4685 */
4686 if (mode) {
4687 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4688 if (bond_mode == -1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004689 pr_err(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 ": Error: Invalid bonding mode \"%s\"\n",
4691 mode == NULL ? "NULL" : mode);
4692 return -EINVAL;
4693 }
4694 }
4695
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004696 if (xmit_hash_policy) {
4697 if ((bond_mode != BOND_MODE_XOR) &&
4698 (bond_mode != BOND_MODE_8023AD)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004699 pr_info(DRV_NAME
Nicolas de Pesloüan3c6aaa22009-10-07 14:10:36 -07004700 ": xmit_hash_policy param is irrelevant in"
4701 " mode %s\n",
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004702 bond_mode_name(bond_mode));
4703 } else {
4704 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4705 xmit_hashtype_tbl);
4706 if (xmit_hashtype == -1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004707 pr_err(DRV_NAME
4708 ": Error: Invalid xmit_hash_policy \"%s\"\n",
4709 xmit_hash_policy == NULL ? "NULL" :
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004710 xmit_hash_policy);
4711 return -EINVAL;
4712 }
4713 }
4714 }
4715
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 if (lacp_rate) {
4717 if (bond_mode != BOND_MODE_8023AD) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004718 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 ": lacp_rate param is irrelevant in mode %s\n",
4720 bond_mode_name(bond_mode));
4721 } else {
4722 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4723 if (lacp_fast == -1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004724 pr_err(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 ": Error: Invalid lacp rate \"%s\"\n",
4726 lacp_rate == NULL ? "NULL" : lacp_rate);
4727 return -EINVAL;
4728 }
4729 }
4730 }
4731
Jay Vosburghfd989c82008-11-04 17:51:16 -08004732 if (ad_select) {
4733 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4734 if (params->ad_select == -1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004735 pr_err(DRV_NAME
Jay Vosburghfd989c82008-11-04 17:51:16 -08004736 ": Error: Invalid ad_select \"%s\"\n",
4737 ad_select == NULL ? "NULL" : ad_select);
4738 return -EINVAL;
4739 }
4740
4741 if (bond_mode != BOND_MODE_8023AD) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004742 pr_warning(DRV_NAME
Jay Vosburghfd989c82008-11-04 17:51:16 -08004743 ": ad_select param only affects 802.3ad mode\n");
4744 }
4745 } else {
4746 params->ad_select = BOND_AD_STABLE;
4747 }
4748
Nicolas de Pesloüanf5841302009-08-28 13:18:34 +00004749 if (max_bonds < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004750 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 ": Warning: max_bonds (%d) not in range %d-%d, so it "
Mitch Williams4e0952c2005-11-09 10:34:57 -08004752 "was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
Jay Vosburghb8a97872008-06-13 18:12:04 -07004753 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 max_bonds = BOND_DEFAULT_MAX_BONDS;
4755 }
4756
4757 if (miimon < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004758 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 ": Warning: miimon module parameter (%d), "
4760 "not in range 0-%d, so it was reset to %d\n",
4761 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4762 miimon = BOND_LINK_MON_INTERV;
4763 }
4764
4765 if (updelay < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004766 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 ": Warning: updelay module parameter (%d), "
4768 "not in range 0-%d, so it was reset to 0\n",
4769 updelay, INT_MAX);
4770 updelay = 0;
4771 }
4772
4773 if (downdelay < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004774 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 ": Warning: downdelay module parameter (%d), "
4776 "not in range 0-%d, so it was reset to 0\n",
4777 downdelay, INT_MAX);
4778 downdelay = 0;
4779 }
4780
4781 if ((use_carrier != 0) && (use_carrier != 1)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004782 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 ": Warning: use_carrier module parameter (%d), "
4784 "not of valid value (0/1), so it was set to 1\n",
4785 use_carrier);
4786 use_carrier = 1;
4787 }
4788
Moni Shoua7893b242008-05-17 21:10:12 -07004789 if (num_grat_arp < 0 || num_grat_arp > 255) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004790 pr_warning(DRV_NAME
Moni Shoua7893b242008-05-17 21:10:12 -07004791 ": Warning: num_grat_arp (%d) not in range 0-255 so it "
4792 "was reset to 1 \n", num_grat_arp);
4793 num_grat_arp = 1;
4794 }
4795
Brian Haley305d5522008-11-04 17:51:14 -08004796 if (num_unsol_na < 0 || num_unsol_na > 255) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004797 pr_warning(DRV_NAME
Brian Haley305d5522008-11-04 17:51:14 -08004798 ": Warning: num_unsol_na (%d) not in range 0-255 so it "
4799 "was reset to 1 \n", num_unsol_na);
4800 num_unsol_na = 1;
4801 }
4802
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 /* reset values for 802.3ad */
4804 if (bond_mode == BOND_MODE_8023AD) {
4805 if (!miimon) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004806 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 ": Warning: miimon must be specified, "
4808 "otherwise bonding will not detect link "
4809 "failure, speed and duplex which are "
4810 "essential for 802.3ad operation\n");
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004811 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 miimon = 100;
4813 }
4814 }
4815
4816 /* reset values for TLB/ALB */
4817 if ((bond_mode == BOND_MODE_TLB) ||
4818 (bond_mode == BOND_MODE_ALB)) {
4819 if (!miimon) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004820 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 ": Warning: miimon must be specified, "
4822 "otherwise bonding will not detect link "
4823 "failure and link speed which are essential "
4824 "for TLB/ALB load balancing\n");
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004825 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 miimon = 100;
4827 }
4828 }
4829
4830 if (bond_mode == BOND_MODE_ALB) {
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00004831 pr_notice(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 ": In ALB mode you might experience client "
4833 "disconnections upon reconnection of a link if the "
4834 "bonding module updelay parameter (%d msec) is "
4835 "incompatible with the forwarding delay time of the "
4836 "switch\n",
4837 updelay);
4838 }
4839
4840 if (!miimon) {
4841 if (updelay || downdelay) {
4842 /* just warn the user the up/down delay will have
4843 * no effect since miimon is zero...
4844 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004845 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 ": Warning: miimon module parameter not set "
4847 "and updelay (%d) or downdelay (%d) module "
4848 "parameter is set; updelay and downdelay have "
4849 "no effect unless miimon is set\n",
4850 updelay, downdelay);
4851 }
4852 } else {
4853 /* don't allow arp monitoring */
4854 if (arp_interval) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004855 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 ": Warning: miimon (%d) and arp_interval (%d) "
4857 "can't be used simultaneously, disabling ARP "
4858 "monitoring\n",
4859 miimon, arp_interval);
4860 arp_interval = 0;
4861 }
4862
4863 if ((updelay % miimon) != 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004864 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 ": Warning: updelay (%d) is not a multiple "
4866 "of miimon (%d), updelay rounded to %d ms\n",
4867 updelay, miimon, (updelay / miimon) * miimon);
4868 }
4869
4870 updelay /= miimon;
4871
4872 if ((downdelay % miimon) != 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004873 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 ": Warning: downdelay (%d) is not a multiple "
4875 "of miimon (%d), downdelay rounded to %d ms\n",
4876 downdelay, miimon,
4877 (downdelay / miimon) * miimon);
4878 }
4879
4880 downdelay /= miimon;
4881 }
4882
4883 if (arp_interval < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004884 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 ": Warning: arp_interval module parameter (%d) "
4886 ", not in range 0-%d, so it was reset to %d\n",
4887 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4888 arp_interval = BOND_LINK_ARP_INTERV;
4889 }
4890
4891 for (arp_ip_count = 0;
4892 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4893 arp_ip_count++) {
4894 /* not complete check, but should be good enough to
4895 catch mistakes */
4896 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004897 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 ": Warning: bad arp_ip_target module parameter "
4899 "(%s), ARP monitoring will not be performed\n",
4900 arp_ip_target[arp_ip_count]);
4901 arp_interval = 0;
4902 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04004903 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 arp_target[arp_ip_count] = ip;
4905 }
4906 }
4907
4908 if (arp_interval && !arp_ip_count) {
4909 /* don't allow arping if no arp_ip_target given... */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004910 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 ": Warning: arp_interval module parameter (%d) "
4912 "specified without providing an arp_ip_target "
4913 "parameter, arp_interval was reset to 0\n",
4914 arp_interval);
4915 arp_interval = 0;
4916 }
4917
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004918 if (arp_validate) {
4919 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004920 pr_err(DRV_NAME
4921 ": arp_validate only supported in active-backup mode\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004922 return -EINVAL;
4923 }
4924 if (!arp_interval) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004925 pr_err(DRV_NAME
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004926 ": arp_validate requires arp_interval\n");
4927 return -EINVAL;
4928 }
4929
4930 arp_validate_value = bond_parse_parm(arp_validate,
4931 arp_validate_tbl);
4932 if (arp_validate_value == -1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004933 pr_err(DRV_NAME
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004934 ": Error: invalid arp_validate \"%s\"\n",
4935 arp_validate == NULL ? "NULL" : arp_validate);
4936 return -EINVAL;
4937 }
4938 } else
4939 arp_validate_value = 0;
4940
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 if (miimon) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004942 pr_info(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 ": MII link monitoring set to %d ms\n",
4944 miimon);
4945 } else if (arp_interval) {
4946 int i;
4947
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004948 pr_info(DRV_NAME ": ARP monitoring set to %d ms,"
4949 " validate %s, with %d target(s):",
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004950 arp_interval,
4951 arp_validate_tbl[arp_validate_value].modename,
4952 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953
4954 for (i = 0; i < arp_ip_count; i++)
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00004955 pr_info(" %s", arp_ip_target[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00004957 pr_info("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958
Jay Vosburghb8a97872008-06-13 18:12:04 -07004959 } else if (max_bonds) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 /* miimon and arp_interval not set, we need one so things
4961 * work as expected, see bonding.txt for details
4962 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004963 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 ": Warning: either miimon or arp_interval and "
4965 "arp_ip_target module parameters must be specified, "
4966 "otherwise bonding will not detect link failures! see "
4967 "bonding.txt for details.\n");
4968 }
4969
4970 if (primary && !USES_PRIMARY(bond_mode)) {
4971 /* currently, using a primary only makes sense
4972 * in active backup, TLB or ALB modes
4973 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004974 pr_warning(DRV_NAME
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 ": Warning: %s primary device specified but has no "
4976 "effect in %s mode\n",
4977 primary, bond_mode_name(bond_mode));
4978 primary = NULL;
4979 }
4980
Jiri Pirkoa5499522009-09-25 03:28:09 +00004981 if (primary && primary_reselect) {
4982 primary_reselect_value = bond_parse_parm(primary_reselect,
4983 pri_reselect_tbl);
4984 if (primary_reselect_value == -1) {
4985 pr_err(DRV_NAME
4986 ": Error: Invalid primary_reselect \"%s\"\n",
4987 primary_reselect ==
4988 NULL ? "NULL" : primary_reselect);
4989 return -EINVAL;
4990 }
4991 } else {
4992 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
4993 }
4994
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004995 if (fail_over_mac) {
4996 fail_over_mac_value = bond_parse_parm(fail_over_mac,
4997 fail_over_mac_tbl);
4998 if (fail_over_mac_value == -1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004999 pr_err(DRV_NAME
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005000 ": Error: invalid fail_over_mac \"%s\"\n",
5001 arp_validate == NULL ? "NULL" : arp_validate);
5002 return -EINVAL;
5003 }
5004
5005 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005006 pr_warning(DRV_NAME
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005007 ": Warning: fail_over_mac only affects "
5008 "active-backup mode.\n");
5009 } else {
5010 fail_over_mac_value = BOND_FOM_NONE;
5011 }
Jay Vosburghdd957c52007-10-09 19:57:24 -07005012
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 /* fill params struct with the proper values */
5014 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04005015 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 params->miimon = miimon;
Moni Shoua7893b242008-05-17 21:10:12 -07005017 params->num_grat_arp = num_grat_arp;
Brian Haley305d5522008-11-04 17:51:14 -08005018 params->num_unsol_na = num_unsol_na;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005020 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 params->updelay = updelay;
5022 params->downdelay = downdelay;
5023 params->use_carrier = use_carrier;
5024 params->lacp_fast = lacp_fast;
5025 params->primary[0] = 0;
Jiri Pirkoa5499522009-09-25 03:28:09 +00005026 params->primary_reselect = primary_reselect_value;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005027 params->fail_over_mac = fail_over_mac_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028
5029 if (primary) {
5030 strncpy(params->primary, primary, IFNAMSIZ);
5031 params->primary[IFNAMSIZ - 1] = 0;
5032 }
5033
5034 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
5035
5036 return 0;
5037}
5038
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005039static struct lock_class_key bonding_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -07005040static struct lock_class_key bonding_netdev_addr_lock_key;
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005041
David S. Millere8a04642008-07-17 00:34:19 -07005042static void bond_set_lockdep_class_one(struct net_device *dev,
5043 struct netdev_queue *txq,
5044 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -07005045{
5046 lockdep_set_class(&txq->_xmit_lock,
5047 &bonding_netdev_xmit_lock_key);
5048}
5049
5050static void bond_set_lockdep_class(struct net_device *dev)
5051{
David S. Millercf508b12008-07-22 14:16:42 -07005052 lockdep_set_class(&dev->addr_list_lock,
5053 &bonding_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -07005054 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -07005055}
5056
Stephen Hemminger181470f2009-06-12 19:02:52 +00005057/*
5058 * Called from registration process
5059 */
5060static int bond_init(struct net_device *bond_dev)
5061{
5062 struct bonding *bond = netdev_priv(bond_dev);
5063
5064 pr_debug("Begin bond_init for %s\n", bond_dev->name);
5065
5066 bond->wq = create_singlethread_workqueue(bond_dev->name);
5067 if (!bond->wq)
5068 return -ENOMEM;
5069
5070 bond_set_lockdep_class(bond_dev);
5071
5072 netif_carrier_off(bond_dev);
5073
5074 bond_create_proc_entry(bond);
5075 list_add_tail(&bond->bond_list, &bond_dev_list);
5076
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00005077 bond_prepare_sysfs_group(bond);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005078 return 0;
5079}
5080
Mitch Williamsdfe60392005-11-09 10:36:04 -08005081/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005082 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08005083 * Caller must NOT hold rtnl_lock; we need to release it here before we
5084 * set up our sysfs entries.
5085 */
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00005086int bond_create(const char *name)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005087{
5088 struct net_device *bond_dev;
5089 int res;
5090
5091 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08005092 /* Check to see if the bond already exists. */
Stephen Hemminger373500d2009-06-12 19:02:50 +00005093 /* FIXME: pass netns from caller */
5094 if (name && __dev_get_by_name(&init_net, name)) {
5095 pr_err(DRV_NAME ": cannot add bond %s; already exists\n",
5096 name);
5097 res = -EEXIST;
5098 goto out_rtnl;
Jay Vosburgh4fe47632008-01-29 18:07:45 -08005099 }
Jay Vosburgh027ea042008-01-17 16:25:02 -08005100
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005101 bond_dev = alloc_netdev(sizeof(struct bonding), name ? name : "",
Stephen Hemminger181470f2009-06-12 19:02:52 +00005102 bond_setup);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005103 if (!bond_dev) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005104 pr_err(DRV_NAME ": %s: eek! can't alloc netdev!\n",
Mitch Williamsdfe60392005-11-09 10:36:04 -08005105 name);
5106 res = -ENOMEM;
5107 goto out_rtnl;
5108 }
5109
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005110 if (!name) {
5111 res = dev_alloc_name(bond_dev, "bond%d");
5112 if (res < 0)
5113 goto out_netdev;
5114 }
5115
Mitch Williamsdfe60392005-11-09 10:36:04 -08005116 res = register_netdevice(bond_dev);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005117 if (res < 0)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005118 goto out_bond;
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005119
Stephen Hemminger7e083842009-06-12 19:02:46 +00005120 rtnl_unlock();
Jay Vosburgh09c89272007-01-19 18:15:38 -08005121 return 0;
5122
Mitch Williamsdfe60392005-11-09 10:36:04 -08005123out_bond:
5124 bond_deinit(bond_dev);
5125out_netdev:
5126 free_netdev(bond_dev);
5127out_rtnl:
5128 rtnl_unlock();
Mitch Williamsdfe60392005-11-09 10:36:04 -08005129 return res;
5130}
5131
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132static int __init bonding_init(void)
5133{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 int i;
5135 int res;
5136
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005137 pr_info("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138
Mitch Williamsdfe60392005-11-09 10:36:04 -08005139 res = bond_check_params(&bonding_defaults);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005140 if (res)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005141 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 bond_create_proc_dir();
Jay Vosburgh027ea042008-01-17 16:25:02 -08005144
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 for (i = 0; i < max_bonds; i++) {
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00005146 res = bond_create(NULL);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005147 if (res)
5148 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 }
5150
Mitch Williamsb76cdba2005-11-09 10:36:41 -08005151 res = bond_create_sysfs();
5152 if (res)
5153 goto err;
5154
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005156 register_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005157 bond_register_ipv6_notifier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158
Mitch Williamsdfe60392005-11-09 10:36:04 -08005159 goto out;
5160err:
Florin Malita40abc272005-09-18 00:24:12 -07005161 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 bond_free_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 rtnl_unlock();
Mitch Williamsdfe60392005-11-09 10:36:04 -08005164out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 return res;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005166
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167}
5168
5169static void __exit bonding_exit(void)
5170{
5171 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005172 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005173 bond_unregister_ipv6_notifier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005175 bond_destroy_sysfs();
5176
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 rtnl_lock();
5178 bond_free_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 rtnl_unlock();
5180}
5181
5182module_init(bonding_init);
5183module_exit(bonding_exit);
5184MODULE_LICENSE("GPL");
5185MODULE_VERSION(DRV_VERSION);
5186MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5187MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");