blob: b46cb139477d2901433ea039208b15fdbdcf2633 [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
Joe Perchesa4aee5c2009-12-13 20:06:07 -080034#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/kernel.h>
37#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/types.h>
39#include <linux/fcntl.h>
40#include <linux/interrupt.h>
41#include <linux/ptrace.h>
42#include <linux/ioport.h>
43#include <linux/in.h>
Jay Vosburgh169a3e62005-06-26 17:54:11 -040044#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/ip.h>
Jay Vosburgh169a3e62005-06-26 17:54:11 -040046#include <linux/tcp.h>
47#include <linux/udp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/slab.h>
49#include <linux/string.h>
50#include <linux/init.h>
51#include <linux/timer.h>
52#include <linux/socket.h>
53#include <linux/ctype.h>
54#include <linux/inet.h>
55#include <linux/bitops.h>
Stephen Hemminger3d632c32009-06-12 19:02:48 +000056#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <asm/dma.h>
Stephen Hemminger3d632c32009-06-12 19:02:48 +000059#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/errno.h>
61#include <linux/netdevice.h>
WANG Congf6dc31a2010-05-06 00:48:51 -070062#include <linux/netpoll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/inetdevice.h>
Jay Vosburgha816c7c2007-02-28 17:03:37 -080064#include <linux/igmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/etherdevice.h>
66#include <linux/skbuff.h>
67#include <net/sock.h>
68#include <linux/rtnetlink.h>
69#include <linux/proc_fs.h>
70#include <linux/seq_file.h>
71#include <linux/smp.h>
72#include <linux/if_ether.h>
73#include <net/arp.h>
74#include <linux/mii.h>
75#include <linux/ethtool.h>
76#include <linux/if_vlan.h>
77#include <linux/if_bonding.h>
David Sterbab63bb732007-12-06 23:40:33 -080078#include <linux/jiffies.h>
Neil Hormane843fa52010-10-13 16:01:50 +000079#include <linux/preempt.h>
Jay Vosburghc3ade5c2005-06-26 17:52:20 -040080#include <net/route.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020081#include <net/net_namespace.h>
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000082#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include "bonding.h"
84#include "bond_3ad.h"
85#include "bond_alb.h"
86
87/*---------------------------- Module parameters ----------------------------*/
88
89/* monitor all links that often (in milliseconds). <=0 disables monitoring */
90#define BOND_LINK_MON_INTERV 0
91#define BOND_LINK_ARP_INTERV 0
92
93static int max_bonds = BOND_DEFAULT_MAX_BONDS;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +000094static int tx_queues = BOND_DEFAULT_TX_QUEUES;
Moni Shoua7893b242008-05-17 21:10:12 -070095static int num_grat_arp = 1;
Brian Haley305d5522008-11-04 17:51:14 -080096static int num_unsol_na = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static int miimon = BOND_LINK_MON_INTERV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +000098static int updelay;
99static int downdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static int use_carrier = 1;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000101static char *mode;
102static char *primary;
Jiri Pirkoa5499522009-09-25 03:28:09 +0000103static char *primary_reselect;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000104static char *lacp_rate;
105static char *ad_select;
106static char *xmit_hash_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static int arp_interval = BOND_LINK_ARP_INTERV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000108static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
109static char *arp_validate;
110static char *fail_over_mac;
Andy Gospodarekebd8e492010-06-02 08:39:21 +0000111static int all_slaves_active = 0;
Stephen Hemmingerd2991f72009-06-12 19:02:44 +0000112static struct bond_params bonding_defaults;
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000113static int resend_igmp = BOND_DEFAULT_RESEND_IGMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115module_param(max_bonds, int, 0);
116MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
Andy Gospodarekbb1d9122010-06-02 08:40:18 +0000117module_param(tx_queues, int, 0);
118MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)");
Moni Shoua7893b242008-05-17 21:10:12 -0700119module_param(num_grat_arp, int, 0644);
120MODULE_PARM_DESC(num_grat_arp, "Number of gratuitous ARP packets to send on failover event");
Brian Haley305d5522008-11-04 17:51:14 -0800121module_param(num_unsol_na, int, 0644);
122MODULE_PARM_DESC(num_unsol_na, "Number of unsolicited IPv6 Neighbor Advertisements packets to send on failover event");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123module_param(miimon, int, 0);
124MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
125module_param(updelay, int, 0);
126MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
127module_param(downdelay, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800128MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
129 "in milliseconds");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130module_param(use_carrier, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800131MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
132 "0 for off, 1 for on (default)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133module_param(mode, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800134MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
135 "1 for active-backup, 2 for balance-xor, "
136 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
137 "6 for balance-alb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138module_param(primary, charp, 0);
139MODULE_PARM_DESC(primary, "Primary network device to use");
Jiri Pirkoa5499522009-09-25 03:28:09 +0000140module_param(primary_reselect, charp, 0);
141MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
142 "once it comes up; "
143 "0 for always (default), "
144 "1 for only if speed of primary is "
145 "better, "
146 "2 for only on active slave "
147 "failure");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148module_param(lacp_rate, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800149MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
150 "(slow/fast)");
Jay Vosburghfd989c82008-11-04 17:51:16 -0800151module_param(ad_select, charp, 0);
152MODULE_PARM_DESC(ad_select, "803.ad aggregation selection logic: stable (0, default), bandwidth (1), count (2)");
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400153module_param(xmit_hash_policy, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800154MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
155 ", 1 for layer 3+4");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156module_param(arp_interval, int, 0);
157MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
158module_param_array(arp_ip_target, charp, NULL, 0);
159MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700160module_param(arp_validate, charp, 0);
161MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700162module_param(fail_over_mac, charp, 0);
163MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to the same MAC. none (default), active or follow");
Andy Gospodarekebd8e492010-06-02 08:39:21 +0000164module_param(all_slaves_active, int, 0);
165MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface"
166 "by setting active flag for all slaves. "
167 "0 for never (default), 1 for always.");
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000168module_param(resend_igmp, int, 0);
169MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on link failure");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/*----------------------------- Global variables ----------------------------*/
172
Neil Hormane843fa52010-10-13 16:01:50 +0000173#ifdef CONFIG_NET_POLL_CONTROLLER
174cpumask_var_t netpoll_block_tx;
175#endif
176
Arjan van de Venf71e1302006-03-03 21:33:57 -0500177static const char * const version =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
179
Eric Dumazetf99189b2009-11-17 10:42:49 +0000180int bond_net_id __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000182static __be32 arp_target[BOND_MAX_ARP_TARGETS];
183static int arp_ip_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static int bond_mode = BOND_MODE_ROUNDROBIN;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000185static int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
186static int lacp_fast;
Eric Dumazet90e17952010-07-19 06:52:36 +0000187#ifdef CONFIG_NET_POLL_CONTROLLER
Andy Gospodarekc22d7ac2010-06-25 09:50:44 +0000188static int disable_netpoll = 1;
Eric Dumazet90e17952010-07-19 06:52:36 +0000189#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800191const struct bond_parm_tbl bond_lacp_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{ "slow", AD_LACP_SLOW},
193{ "fast", AD_LACP_FAST},
194{ NULL, -1},
195};
196
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800197const struct bond_parm_tbl bond_mode_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{ "balance-rr", BOND_MODE_ROUNDROBIN},
199{ "active-backup", BOND_MODE_ACTIVEBACKUP},
200{ "balance-xor", BOND_MODE_XOR},
201{ "broadcast", BOND_MODE_BROADCAST},
202{ "802.3ad", BOND_MODE_8023AD},
203{ "balance-tlb", BOND_MODE_TLB},
204{ "balance-alb", BOND_MODE_ALB},
205{ NULL, -1},
206};
207
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800208const struct bond_parm_tbl xmit_hashtype_tbl[] = {
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400209{ "layer2", BOND_XMIT_POLICY_LAYER2},
210{ "layer3+4", BOND_XMIT_POLICY_LAYER34},
Jay Vosburgh6f6652b2007-12-06 23:40:34 -0800211{ "layer2+3", BOND_XMIT_POLICY_LAYER23},
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400212{ NULL, -1},
213};
214
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800215const struct bond_parm_tbl arp_validate_tbl[] = {
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700216{ "none", BOND_ARP_VALIDATE_NONE},
217{ "active", BOND_ARP_VALIDATE_ACTIVE},
218{ "backup", BOND_ARP_VALIDATE_BACKUP},
219{ "all", BOND_ARP_VALIDATE_ALL},
220{ NULL, -1},
221};
222
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800223const struct bond_parm_tbl fail_over_mac_tbl[] = {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700224{ "none", BOND_FOM_NONE},
225{ "active", BOND_FOM_ACTIVE},
226{ "follow", BOND_FOM_FOLLOW},
227{ NULL, -1},
228};
229
Jiri Pirkoa5499522009-09-25 03:28:09 +0000230const struct bond_parm_tbl pri_reselect_tbl[] = {
231{ "always", BOND_PRI_RESELECT_ALWAYS},
232{ "better", BOND_PRI_RESELECT_BETTER},
233{ "failure", BOND_PRI_RESELECT_FAILURE},
234{ NULL, -1},
235};
236
Jay Vosburghfd989c82008-11-04 17:51:16 -0800237struct bond_parm_tbl ad_select_tbl[] = {
238{ "stable", BOND_AD_STABLE},
239{ "bandwidth", BOND_AD_BANDWIDTH},
240{ "count", BOND_AD_COUNT},
241{ NULL, -1},
242};
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/*-------------------------- Forward declarations ---------------------------*/
245
Jay Vosburghc3ade5c2005-06-26 17:52:20 -0400246static void bond_send_gratuitous_arp(struct bonding *bond);
Stephen Hemminger181470f2009-06-12 19:02:52 +0000247static int bond_init(struct net_device *bond_dev);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +0000248static void bond_uninit(struct net_device *bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250/*---------------------------- General routines -----------------------------*/
251
Adrian Bunk4ad072c2007-07-09 11:51:12 -0700252static const char *bond_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800254 static const char *names[] = {
255 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
256 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
257 [BOND_MODE_XOR] = "load balancing (xor)",
258 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000259 [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800260 [BOND_MODE_TLB] = "transmit load balancing",
261 [BOND_MODE_ALB] = "adaptive load balancing",
262 };
263
264 if (mode < 0 || mode > BOND_MODE_ALB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return "unknown";
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800266
267 return names[mode];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270/*---------------------------------- VLAN -----------------------------------*/
271
272/**
273 * bond_add_vlan - add a new vlan id on bond
274 * @bond: bond that got the notification
275 * @vlan_id: the vlan id to add
276 *
277 * Returns -ENOMEM if allocation failed.
278 */
279static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
280{
281 struct vlan_entry *vlan;
282
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800283 pr_debug("bond: %s, vlan id %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800284 (bond ? bond->dev->name : "None"), vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Brian Haley305d5522008-11-04 17:51:14 -0800286 vlan = kzalloc(sizeof(struct vlan_entry), GFP_KERNEL);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000287 if (!vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 INIT_LIST_HEAD(&vlan->vlan_list);
291 vlan->vlan_id = vlan_id;
292
293 write_lock_bh(&bond->lock);
294
295 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
296
297 write_unlock_bh(&bond->lock);
298
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800299 pr_debug("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 return 0;
302}
303
304/**
305 * bond_del_vlan - delete a vlan id from bond
306 * @bond: bond that got the notification
307 * @vlan_id: the vlan id to delete
308 *
309 * returns -ENODEV if @vlan_id was not found in @bond.
310 */
311static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
312{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700313 struct vlan_entry *vlan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 int res = -ENODEV;
315
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800316 pr_debug("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Neil Hormane843fa52010-10-13 16:01:50 +0000318 block_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 write_lock_bh(&bond->lock);
320
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700321 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (vlan->vlan_id == vlan_id) {
323 list_del(&vlan->vlan_list);
324
Holger Eitzenberger58402052008-12-09 23:07:13 -0800325 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 bond_alb_clear_vlan(bond, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800328 pr_debug("removed VLAN ID %d from bond %s\n",
329 vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 kfree(vlan);
332
333 if (list_empty(&bond->vlan_list) &&
334 (bond->slave_cnt == 0)) {
335 /* Last VLAN removed and no slaves, so
336 * restore block on adding VLANs. This will
337 * be removed once new slaves that are not
338 * VLAN challenged will be added.
339 */
340 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
341 }
342
343 res = 0;
344 goto out;
345 }
346 }
347
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800348 pr_debug("couldn't find VLAN ID %d in bond %s\n",
349 vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351out:
352 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +0000353 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return res;
355}
356
357/**
358 * bond_has_challenged_slaves
359 * @bond: the bond we're working on
360 *
361 * Searches the slave list. Returns 1 if a vlan challenged slave
362 * was found, 0 otherwise.
363 *
364 * Assumes bond->lock is held.
365 */
366static int bond_has_challenged_slaves(struct bonding *bond)
367{
368 struct slave *slave;
369 int i;
370
371 bond_for_each_slave(bond, slave, i) {
372 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800373 pr_debug("found VLAN challenged slave - %s\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800374 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return 1;
376 }
377 }
378
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800379 pr_debug("no VLAN challenged slaves found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return 0;
381}
382
383/**
384 * bond_next_vlan - safely skip to the next item in the vlans list.
385 * @bond: the bond we're working on
386 * @curr: item we're advancing from
387 *
388 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
389 * or @curr->next otherwise (even if it is @curr itself again).
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000390 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 * Caller must hold bond->lock
392 */
393struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
394{
395 struct vlan_entry *next, *last;
396
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000397 if (list_empty(&bond->vlan_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 if (!curr) {
401 next = list_entry(bond->vlan_list.next,
402 struct vlan_entry, vlan_list);
403 } else {
404 last = list_entry(bond->vlan_list.prev,
405 struct vlan_entry, vlan_list);
406 if (last == curr) {
407 next = list_entry(bond->vlan_list.next,
408 struct vlan_entry, vlan_list);
409 } else {
410 next = list_entry(curr->vlan_list.next,
411 struct vlan_entry, vlan_list);
412 }
413 }
414
415 return next;
416}
417
418/**
419 * bond_dev_queue_xmit - Prepare skb for xmit.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000420 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 * @bond: bond device that got this skb for tx.
422 * @skb: hw accel VLAN tagged skb to transmit
423 * @slave_dev: slave that is supposed to xmit this skbuff
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000424 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * When the bond gets an skb to transmit that is
426 * already hardware accelerated VLAN tagged, and it
427 * needs to relay this skb to a slave that is not
428 * hw accel capable, the skb needs to be "unaccelerated",
429 * i.e. strip the hwaccel tag and re-insert it as part
430 * of the payload.
431 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000432int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
433 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Jay Vosburgh966bc6f2008-03-21 22:29:34 -0700435 unsigned short uninitialized_var(vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Jay Vosburghf35188f2010-07-21 12:14:47 +0000437 /* Test vlan_list not vlgrp to catch and handle 802.1p tags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (!list_empty(&bond->vlan_list) &&
439 !(slave_dev->features & NETIF_F_HW_VLAN_TX) &&
440 vlan_get_tag(skb, &vlan_id) == 0) {
441 skb->dev = slave_dev;
442 skb = vlan_put_tag(skb, vlan_id);
443 if (!skb) {
444 /* vlan_put_tag() frees the skb in case of error,
445 * so return success here so the calling functions
446 * won't attempt to free is again.
447 */
448 return 0;
449 }
450 } else {
451 skb->dev = slave_dev;
452 }
453
454 skb->priority = 1;
WANG Congf6dc31a2010-05-06 00:48:51 -0700455#ifdef CONFIG_NET_POLL_CONTROLLER
456 if (unlikely(bond->dev->priv_flags & IFF_IN_NETPOLL)) {
457 struct netpoll *np = bond->dev->npinfo->netpoll;
458 slave_dev->npinfo = bond->dev->npinfo;
WANG Congf6dc31a2010-05-06 00:48:51 -0700459 slave_dev->priv_flags |= IFF_IN_NETPOLL;
Neil Hormanc2355e12010-10-13 16:01:49 +0000460 netpoll_send_skb_on_dev(np, skb, slave_dev);
WANG Congf6dc31a2010-05-06 00:48:51 -0700461 slave_dev->priv_flags &= ~IFF_IN_NETPOLL;
WANG Congf6dc31a2010-05-06 00:48:51 -0700462 } else
463#endif
464 dev_queue_xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 return 0;
467}
468
469/*
470 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
471 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
472 * lock because:
473 * a. This operation is performed in IOCTL context,
474 * b. The operation is protected by the RTNL semaphore in the 8021q code,
475 * c. Holding a lock with BH disabled while directly calling a base driver
476 * entry point is generally a BAD idea.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000477 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 * The design of synchronization/protection for this operation in the 8021q
479 * module is good for one or more VLAN devices over a single physical device
480 * and cannot be extended for a teaming solution like bonding, so there is a
481 * potential race condition here where a net device from the vlan group might
482 * be referenced (either by a base driver or the 8021q code) while it is being
483 * removed from the system. However, it turns out we're not making matters
484 * worse, and if it works for regular VLAN usage it will work here too.
485*/
486
487/**
488 * bond_vlan_rx_register - Propagates registration to slaves
489 * @bond_dev: bonding net device that got called
490 * @grp: vlan group being registered
491 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000492static void bond_vlan_rx_register(struct net_device *bond_dev,
493 struct vlan_group *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Wang Chen454d7c92008-11-12 23:37:49 -0800495 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 struct slave *slave;
497 int i;
498
Jay Vosburghf35188f2010-07-21 12:14:47 +0000499 write_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 bond->vlgrp = grp;
Jay Vosburghf35188f2010-07-21 12:14:47 +0000501 write_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 bond_for_each_slave(bond, slave, i) {
504 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800505 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800508 slave_ops->ndo_vlan_rx_register) {
509 slave_ops->ndo_vlan_rx_register(slave_dev, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511 }
512}
513
514/**
515 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
516 * @bond_dev: bonding net device that got called
517 * @vid: vlan id being added
518 */
519static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
520{
Wang Chen454d7c92008-11-12 23:37:49 -0800521 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 struct slave *slave;
523 int i, res;
524
525 bond_for_each_slave(bond, slave, i) {
526 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800527 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800530 slave_ops->ndo_vlan_rx_add_vid) {
531 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533 }
534
535 res = bond_add_vlan(bond, vid);
536 if (res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800537 pr_err("%s: Error: Failed to add vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 bond_dev->name, vid);
539 }
540}
541
542/**
543 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
544 * @bond_dev: bonding net device that got called
545 * @vid: vlan id being removed
546 */
547static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
548{
Wang Chen454d7c92008-11-12 23:37:49 -0800549 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 struct slave *slave;
551 struct net_device *vlan_dev;
552 int i, res;
553
554 bond_for_each_slave(bond, slave, i) {
555 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800556 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800559 slave_ops->ndo_vlan_rx_kill_vid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /* Save and then restore vlan_dev in the grp array,
561 * since the slave's driver might clear it.
562 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800563 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800564 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vid);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800565 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567 }
568
569 res = bond_del_vlan(bond, vid);
570 if (res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800571 pr_err("%s: Error: Failed to remove vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 bond_dev->name, vid);
573 }
574}
575
576static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
577{
578 struct vlan_entry *vlan;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800579 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Jay Vosburghf35188f2010-07-21 12:14:47 +0000581 if (!bond->vlgrp)
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000582 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800585 slave_ops->ndo_vlan_rx_register)
586 slave_ops->ndo_vlan_rx_register(slave_dev, bond->vlgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800589 !(slave_ops->ndo_vlan_rx_add_vid))
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000590 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800592 list_for_each_entry(vlan, &bond->vlan_list, vlan_list)
593 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vlan->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000596static void bond_del_vlans_from_slave(struct bonding *bond,
597 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800599 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 struct vlan_entry *vlan;
601 struct net_device *vlan_dev;
602
Jay Vosburghf35188f2010-07-21 12:14:47 +0000603 if (!bond->vlgrp)
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000604 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800607 !(slave_ops->ndo_vlan_rx_kill_vid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 goto unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +0000611 if (!vlan->vlan_id)
612 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* Save and then restore vlan_dev in the grp array,
614 * since the slave's driver might clear it.
615 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800616 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800617 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800618 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620
621unreg:
622 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800623 slave_ops->ndo_vlan_rx_register)
624 slave_ops->ndo_vlan_rx_register(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
627/*------------------------------- Link status -------------------------------*/
628
629/*
Jay Vosburghff59c452006-03-27 13:27:43 -0800630 * Set the carrier state for the master according to the state of its
631 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
632 * do special 802.3ad magic.
633 *
634 * Returns zero if carrier state does not change, nonzero if it does.
635 */
636static int bond_set_carrier(struct bonding *bond)
637{
638 struct slave *slave;
639 int i;
640
641 if (bond->slave_cnt == 0)
642 goto down;
643
644 if (bond->params.mode == BOND_MODE_8023AD)
645 return bond_3ad_set_carrier(bond);
646
647 bond_for_each_slave(bond, slave, i) {
648 if (slave->link == BOND_LINK_UP) {
649 if (!netif_carrier_ok(bond->dev)) {
650 netif_carrier_on(bond->dev);
651 return 1;
652 }
653 return 0;
654 }
655 }
656
657down:
658 if (netif_carrier_ok(bond->dev)) {
659 netif_carrier_off(bond->dev);
660 return 1;
661 }
662 return 0;
663}
664
665/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 * Get link speed and duplex from the slave's base driver
667 * using ethtool. If for some reason the call fails or the
668 * values are invalid, fake speed and duplex to 100/Full
669 * and return error.
670 */
671static int bond_update_speed_duplex(struct slave *slave)
672{
673 struct net_device *slave_dev = slave->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 struct ethtool_cmd etool;
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700675 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 /* Fake speed and duplex */
678 slave->speed = SPEED_100;
679 slave->duplex = DUPLEX_FULL;
680
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700681 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700684 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
685 if (res < 0)
686 return -1;
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 switch (etool.speed) {
689 case SPEED_10:
690 case SPEED_100:
691 case SPEED_1000:
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700692 case SPEED_10000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 break;
694 default:
695 return -1;
696 }
697
698 switch (etool.duplex) {
699 case DUPLEX_FULL:
700 case DUPLEX_HALF:
701 break;
702 default:
703 return -1;
704 }
705
706 slave->speed = etool.speed;
707 slave->duplex = etool.duplex;
708
709 return 0;
710}
711
712/*
713 * if <dev> supports MII link status reporting, check its link status.
714 *
715 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000716 * depending upon the setting of the use_carrier parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 *
718 * Return either BMSR_LSTATUS, meaning that the link is up (or we
719 * can't tell and just pretend it is), or 0, meaning that the link is
720 * down.
721 *
722 * If reporting is non-zero, instead of faking link up, return -1 if
723 * both ETHTOOL and MII ioctls fail (meaning the device does not
724 * support them). If use_carrier is set, return whatever it says.
725 * It'd be nice if there was a good way to tell if a driver supports
726 * netif_carrier, but there really isn't.
727 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000728static int bond_check_dev_link(struct bonding *bond,
729 struct net_device *slave_dev, int reporting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800731 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Jiri Bohacd9d52832009-10-28 22:23:54 -0700732 int (*ioctl)(struct net_device *, struct ifreq *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 struct ifreq ifr;
734 struct mii_ioctl_data *mii;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Petri Gynther6c988852009-08-28 12:05:15 +0000736 if (!reporting && !netif_running(slave_dev))
737 return 0;
738
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800739 if (bond->params.use_carrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Jiri Pirko29112f42009-04-24 01:58:23 +0000742 /* Try to get link status using Ethtool first. */
743 if (slave_dev->ethtool_ops) {
744 if (slave_dev->ethtool_ops->get_link) {
745 u32 link;
746
747 link = slave_dev->ethtool_ops->get_link(slave_dev);
748
749 return link ? BMSR_LSTATUS : 0;
750 }
751 }
752
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000753 /* Ethtool can't be used, fallback to MII ioctls. */
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800754 ioctl = slave_ops->ndo_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (ioctl) {
756 /* TODO: set pointer to correct ioctl on a per team member */
757 /* bases to make this more efficient. that is, once */
758 /* we determine the correct ioctl, we will always */
759 /* call it and not the others for that team */
760 /* member. */
761
762 /*
763 * We cannot assume that SIOCGMIIPHY will also read a
764 * register; not all network drivers (e.g., e100)
765 * support that.
766 */
767
768 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
769 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
770 mii = if_mii(&ifr);
771 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
772 mii->reg_num = MII_BMSR;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000773 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0)
774 return mii->val_out & BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776 }
777
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700778 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 * If reporting, report that either there's no dev->do_ioctl,
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700780 * or both SIOCGMIIREG and get_link failed (meaning that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 * cannot report link status). If not reporting, pretend
782 * we're ok.
783 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000784 return reporting ? -1 : BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
787/*----------------------------- Multicast list ------------------------------*/
788
789/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 * Push the promiscuity flag down to appropriate slaves
791 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700792static int bond_set_promiscuity(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700794 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (USES_PRIMARY(bond->params.mode)) {
796 /* write lock already acquired */
797 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700798 err = dev_set_promiscuity(bond->curr_active_slave->dev,
799 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801 } else {
802 struct slave *slave;
803 int i;
804 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700805 err = dev_set_promiscuity(slave->dev, inc);
806 if (err)
807 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700810 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
813/*
814 * Push the allmulti flag down to all slaves
815 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700816static int bond_set_allmulti(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700818 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (USES_PRIMARY(bond->params.mode)) {
820 /* write lock already acquired */
821 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700822 err = dev_set_allmulti(bond->curr_active_slave->dev,
823 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825 } else {
826 struct slave *slave;
827 int i;
828 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700829 err = dev_set_allmulti(slave->dev, inc);
830 if (err)
831 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700834 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
837/*
838 * Add a Multicast address to slaves
839 * according to mode
840 */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000841static void bond_mc_add(struct bonding *bond, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 if (USES_PRIMARY(bond->params.mode)) {
844 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000845 if (bond->curr_active_slave)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000846 dev_mc_add(bond->curr_active_slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 } else {
848 struct slave *slave;
849 int i;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000850
851 bond_for_each_slave(bond, slave, i)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000852 dev_mc_add(slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854}
855
856/*
857 * Remove a multicast address from slave
858 * according to mode
859 */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000860static void bond_mc_del(struct bonding *bond, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 if (USES_PRIMARY(bond->params.mode)) {
863 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000864 if (bond->curr_active_slave)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000865 dev_mc_del(bond->curr_active_slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 } else {
867 struct slave *slave;
868 int i;
869 bond_for_each_slave(bond, slave, i) {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000870 dev_mc_del(slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872 }
873}
874
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800875
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000876static void __bond_resend_igmp_join_requests(struct net_device *dev)
877{
878 struct in_device *in_dev;
879 struct ip_mc_list *im;
880
881 rcu_read_lock();
882 in_dev = __in_dev_get_rcu(dev);
883 if (in_dev) {
884 for (im = in_dev->mc_list; im; im = im->next)
885 ip_mc_rejoin_group(im);
886 }
887
888 rcu_read_unlock();
889}
890
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800891/*
892 * Retrieve the list of registered multicast addresses for the bonding
893 * device and retransmit an IGMP JOIN request to the current active
894 * slave.
895 */
896static void bond_resend_igmp_join_requests(struct bonding *bond)
897{
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000898 struct net_device *vlan_dev;
899 struct vlan_entry *vlan;
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800900
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000901 read_lock(&bond->lock);
902
903 /* rejoin all groups on bond device */
904 __bond_resend_igmp_join_requests(bond->dev);
905
906 /* rejoin all groups on vlan devices */
907 if (bond->vlgrp) {
908 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
909 vlan_dev = vlan_group_get_device(bond->vlgrp,
910 vlan->vlan_id);
911 if (vlan_dev)
912 __bond_resend_igmp_join_requests(vlan_dev);
913 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800914 }
915
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000916 if (--bond->igmp_retrans > 0)
917 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
918
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000919 read_unlock(&bond->lock);
920}
921
922void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
923{
924 struct bonding *bond = container_of(work, struct bonding,
925 mcast_work.work);
926 bond_resend_igmp_join_requests(bond);
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800927}
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 * flush all members of flush->mc_list from device dev->mc_list
931 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000932static void bond_mc_list_flush(struct net_device *bond_dev,
933 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Wang Chen454d7c92008-11-12 23:37:49 -0800935 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000936 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Jiri Pirko22bedad32010-04-01 21:22:57 +0000938 netdev_for_each_mc_addr(ha, bond_dev)
939 dev_mc_del(slave_dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 if (bond->params.mode == BOND_MODE_8023AD) {
942 /* del lacpdu mc addr from mc list */
943 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
944
Jiri Pirko22bedad32010-04-01 21:22:57 +0000945 dev_mc_del(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947}
948
949/*--------------------------- Active slave change ---------------------------*/
950
951/*
952 * Update the mc list and multicast-related flags for the new and
953 * old active slaves (if any) according to the multicast mode, and
954 * promiscuous flags unconditionally.
955 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000956static void bond_mc_swap(struct bonding *bond, struct slave *new_active,
957 struct slave *old_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Jiri Pirko22bedad32010-04-01 21:22:57 +0000959 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000961 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 /* nothing to do - mc list is already up-to-date on
963 * all slaves
964 */
965 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 if (old_active) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000968 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 dev_set_promiscuity(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000971 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 dev_set_allmulti(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Jiri Pirko22bedad32010-04-01 21:22:57 +0000974 netdev_for_each_mc_addr(ha, bond->dev)
975 dev_mc_del(old_active->dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977
978 if (new_active) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700979 /* FIXME: Signal errors upstream. */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000980 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 dev_set_promiscuity(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000983 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 dev_set_allmulti(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Jiri Pirko22bedad32010-04-01 21:22:57 +0000986 netdev_for_each_mc_addr(ha, bond->dev)
987 dev_mc_add(new_active->dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989}
990
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700991/*
992 * bond_do_fail_over_mac
993 *
994 * Perform special MAC address swapping for fail_over_mac settings
995 *
996 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
997 */
998static void bond_do_fail_over_mac(struct bonding *bond,
999 struct slave *new_active,
1000 struct slave *old_active)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00001001 __releases(&bond->curr_slave_lock)
1002 __releases(&bond->lock)
1003 __acquires(&bond->lock)
1004 __acquires(&bond->curr_slave_lock)
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001005{
1006 u8 tmp_mac[ETH_ALEN];
1007 struct sockaddr saddr;
1008 int rv;
1009
1010 switch (bond->params.fail_over_mac) {
1011 case BOND_FOM_ACTIVE:
1012 if (new_active)
1013 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
1014 new_active->dev->addr_len);
1015 break;
1016 case BOND_FOM_FOLLOW:
1017 /*
1018 * if new_active && old_active, swap them
1019 * if just old_active, do nothing (going to no active slave)
1020 * if just new_active, set new_active to bond's MAC
1021 */
1022 if (!new_active)
1023 return;
1024
1025 write_unlock_bh(&bond->curr_slave_lock);
1026 read_unlock(&bond->lock);
1027
1028 if (old_active) {
1029 memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
1030 memcpy(saddr.sa_data, old_active->dev->dev_addr,
1031 ETH_ALEN);
1032 saddr.sa_family = new_active->dev->type;
1033 } else {
1034 memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
1035 saddr.sa_family = bond->dev->type;
1036 }
1037
1038 rv = dev_set_mac_address(new_active->dev, &saddr);
1039 if (rv) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001040 pr_err("%s: Error %d setting MAC of slave %s\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001041 bond->dev->name, -rv, new_active->dev->name);
1042 goto out;
1043 }
1044
1045 if (!old_active)
1046 goto out;
1047
1048 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
1049 saddr.sa_family = old_active->dev->type;
1050
1051 rv = dev_set_mac_address(old_active->dev, &saddr);
1052 if (rv)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001053 pr_err("%s: Error %d setting MAC of slave %s\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001054 bond->dev->name, -rv, new_active->dev->name);
1055out:
1056 read_lock(&bond->lock);
1057 write_lock_bh(&bond->curr_slave_lock);
1058 break;
1059 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001060 pr_err("%s: bond_do_fail_over_mac impossible: bad policy %d\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001061 bond->dev->name, bond->params.fail_over_mac);
1062 break;
1063 }
1064
1065}
1066
Jiri Pirkoa5499522009-09-25 03:28:09 +00001067static bool bond_should_change_active(struct bonding *bond)
1068{
1069 struct slave *prim = bond->primary_slave;
1070 struct slave *curr = bond->curr_active_slave;
1071
1072 if (!prim || !curr || curr->link != BOND_LINK_UP)
1073 return true;
1074 if (bond->force_primary) {
1075 bond->force_primary = false;
1076 return true;
1077 }
1078 if (bond->params.primary_reselect == BOND_PRI_RESELECT_BETTER &&
1079 (prim->speed < curr->speed ||
1080 (prim->speed == curr->speed && prim->duplex <= curr->duplex)))
1081 return false;
1082 if (bond->params.primary_reselect == BOND_PRI_RESELECT_FAILURE)
1083 return false;
1084 return true;
1085}
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087/**
1088 * find_best_interface - select the best available slave to be the active one
1089 * @bond: our bonding struct
1090 *
1091 * Warning: Caller must hold curr_slave_lock for writing.
1092 */
1093static struct slave *bond_find_best_slave(struct bonding *bond)
1094{
1095 struct slave *new_active, *old_active;
1096 struct slave *bestslave = NULL;
1097 int mintime = bond->params.updelay;
1098 int i;
1099
Nicolas de Pesloüan49b4ad92009-10-07 14:11:00 -07001100 new_active = bond->curr_active_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 if (!new_active) { /* there were no active slaves left */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001103 if (bond->slave_cnt > 0) /* found one slave */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 new_active = bond->first_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001105 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return NULL; /* still no slave, return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if ((bond->primary_slave) &&
Jiri Pirkoa5499522009-09-25 03:28:09 +00001110 bond->primary_slave->link == BOND_LINK_UP &&
1111 bond_should_change_active(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 new_active = bond->primary_slave;
1113 }
1114
1115 /* remember where to stop iterating over the slaves */
1116 old_active = new_active;
1117
1118 bond_for_each_slave_from(bond, new_active, i, old_active) {
Jiri Pirkob9f60252009-08-31 11:09:38 +00001119 if (new_active->link == BOND_LINK_UP) {
1120 return new_active;
1121 } else if (new_active->link == BOND_LINK_BACK &&
1122 IS_UP(new_active->dev)) {
1123 /* link up, but waiting for stabilization */
1124 if (new_active->delay < mintime) {
1125 mintime = new_active->delay;
1126 bestslave = new_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128 }
1129 }
1130
1131 return bestslave;
1132}
1133
1134/**
1135 * change_active_interface - change the active slave into the specified one
1136 * @bond: our bonding struct
1137 * @new: the new slave to make the active one
1138 *
1139 * Set the new slave to the bond's settings and unset them on the old
1140 * curr_active_slave.
1141 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1142 *
1143 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1144 * because it is apparently the best available slave we have, even though its
1145 * updelay hasn't timed out yet.
1146 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001147 * If new_active is not NULL, caller must hold bond->lock for read and
1148 * curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001150void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 struct slave *old_active = bond->curr_active_slave;
1153
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001154 if (old_active == new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 if (new_active) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07001158 new_active->jiffies = jiffies;
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (new_active->link == BOND_LINK_BACK) {
1161 if (USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001162 pr_info("%s: making interface %s the new active one %d ms earlier.\n",
1163 bond->dev->name, new_active->dev->name,
1164 (bond->params.updelay - new_active->delay) * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166
1167 new_active->delay = 0;
1168 new_active->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001170 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Holger Eitzenberger58402052008-12-09 23:07:13 -08001173 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 } else {
1176 if (USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001177 pr_info("%s: making interface %s the new active one.\n",
1178 bond->dev->name, new_active->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180 }
1181 }
1182
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001183 if (USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 bond_mc_swap(bond, new_active, old_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Holger Eitzenberger58402052008-12-09 23:07:13 -08001186 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 bond_alb_handle_active_change(bond, new_active);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001188 if (old_active)
1189 bond_set_slave_inactive_flags(old_active);
1190 if (new_active)
1191 bond_set_slave_active_flags(new_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 } else {
1193 bond->curr_active_slave = new_active;
1194 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001195
1196 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001197 if (old_active)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001198 bond_set_slave_inactive_flags(old_active);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001199
1200 if (new_active) {
1201 bond_set_slave_active_flags(new_active);
Moni Shoua2ab82852007-10-09 19:43:39 -07001202
Or Gerlitz709f8a42008-06-13 18:12:01 -07001203 if (bond->params.fail_over_mac)
1204 bond_do_fail_over_mac(bond, new_active,
1205 old_active);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001206
Or Gerlitz709f8a42008-06-13 18:12:01 -07001207 bond->send_grat_arp = bond->params.num_grat_arp;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07001208 bond_send_gratuitous_arp(bond);
Or Gerlitz01f31092008-06-13 18:12:02 -07001209
Brian Haley305d5522008-11-04 17:51:14 -08001210 bond->send_unsol_na = bond->params.num_unsol_na;
1211 bond_send_unsolicited_na(bond);
1212
Or Gerlitz01f31092008-06-13 18:12:02 -07001213 write_unlock_bh(&bond->curr_slave_lock);
1214 read_unlock(&bond->lock);
1215
Moni Shoua75c78502009-09-15 02:37:40 -07001216 netdev_bonding_change(bond->dev, NETDEV_BONDING_FAILOVER);
Or Gerlitz01f31092008-06-13 18:12:02 -07001217
1218 read_lock(&bond->lock);
1219 write_lock_bh(&bond->curr_slave_lock);
Moni Shoua7893b242008-05-17 21:10:12 -07001220 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001221 }
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001222
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001223 /* resend IGMP joins since active slave has changed or
1224 * all were sent on curr_active_slave */
1225 if ((USES_PRIMARY(bond->params.mode) && new_active) ||
1226 bond->params.mode == BOND_MODE_ROUNDROBIN) {
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001227 bond->igmp_retrans = bond->params.resend_igmp;
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001228 queue_delayed_work(bond->wq, &bond->mcast_work, 0);
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
1232/**
1233 * bond_select_active_slave - select a new active slave, if needed
1234 * @bond: our bonding struct
1235 *
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001236 * This functions should be called when one of the following occurs:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 * - The old curr_active_slave has been released or lost its link.
1238 * - The primary_slave has got its link back.
1239 * - A slave has got its link back and there's no old curr_active_slave.
1240 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001241 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001243void bond_select_active_slave(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
1245 struct slave *best_slave;
Jay Vosburghff59c452006-03-27 13:27:43 -08001246 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 best_slave = bond_find_best_slave(bond);
1249 if (best_slave != bond->curr_active_slave) {
1250 bond_change_active_slave(bond, best_slave);
Jay Vosburghff59c452006-03-27 13:27:43 -08001251 rv = bond_set_carrier(bond);
1252 if (!rv)
1253 return;
1254
1255 if (netif_carrier_ok(bond->dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001256 pr_info("%s: first active interface up!\n",
1257 bond->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08001258 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001259 pr_info("%s: now running without any active interface !\n",
1260 bond->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08001261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263}
1264
1265/*--------------------------- slave list handling ---------------------------*/
1266
1267/*
1268 * This function attaches the slave to the end of list.
1269 *
1270 * bond->lock held for writing by caller.
1271 */
1272static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1273{
1274 if (bond->first_slave == NULL) { /* attaching the first slave */
1275 new_slave->next = new_slave;
1276 new_slave->prev = new_slave;
1277 bond->first_slave = new_slave;
1278 } else {
1279 new_slave->next = bond->first_slave;
1280 new_slave->prev = bond->first_slave->prev;
1281 new_slave->next->prev = new_slave;
1282 new_slave->prev->next = new_slave;
1283 }
1284
1285 bond->slave_cnt++;
1286}
1287
1288/*
1289 * This function detaches the slave from the list.
1290 * WARNING: no check is made to verify if the slave effectively
1291 * belongs to <bond>.
1292 * Nothing is freed on return, structures are just unchained.
1293 * If any slave pointer in bond was pointing to <slave>,
1294 * it should be changed by the calling function.
1295 *
1296 * bond->lock held for writing by caller.
1297 */
1298static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1299{
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001300 if (slave->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 slave->next->prev = slave->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001303 if (slave->prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 slave->prev->next = slave->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 if (bond->first_slave == slave) { /* slave is the first slave */
1307 if (bond->slave_cnt > 1) { /* there are more slave */
1308 bond->first_slave = slave->next;
1309 } else {
1310 bond->first_slave = NULL; /* slave was the last one */
1311 }
1312 }
1313
1314 slave->next = NULL;
1315 slave->prev = NULL;
1316 bond->slave_cnt--;
1317}
1318
WANG Congf6dc31a2010-05-06 00:48:51 -07001319#ifdef CONFIG_NET_POLL_CONTROLLER
1320/*
1321 * You must hold read lock on bond->lock before calling this.
1322 */
1323static bool slaves_support_netpoll(struct net_device *bond_dev)
1324{
1325 struct bonding *bond = netdev_priv(bond_dev);
1326 struct slave *slave;
1327 int i = 0;
1328 bool ret = true;
1329
1330 bond_for_each_slave(bond, slave, i) {
1331 if ((slave->dev->priv_flags & IFF_DISABLE_NETPOLL) ||
1332 !slave->dev->netdev_ops->ndo_poll_controller)
1333 ret = false;
1334 }
1335 return i != 0 && ret;
1336}
1337
1338static void bond_poll_controller(struct net_device *bond_dev)
1339{
Neil Hormanc2355e12010-10-13 16:01:49 +00001340 struct bonding *bond = netdev_priv(bond_dev);
1341 struct slave *slave;
1342 int i;
1343
1344 bond_for_each_slave(bond, slave, i) {
1345 if (slave->dev && IS_UP(slave->dev))
1346 netpoll_poll_dev(slave->dev);
1347 }
WANG Congf6dc31a2010-05-06 00:48:51 -07001348}
1349
1350static void bond_netpoll_cleanup(struct net_device *bond_dev)
1351{
1352 struct bonding *bond = netdev_priv(bond_dev);
1353 struct slave *slave;
1354 const struct net_device_ops *ops;
1355 int i;
1356
1357 read_lock(&bond->lock);
1358 bond_dev->npinfo = NULL;
1359 bond_for_each_slave(bond, slave, i) {
1360 if (slave->dev) {
1361 ops = slave->dev->netdev_ops;
1362 if (ops->ndo_netpoll_cleanup)
1363 ops->ndo_netpoll_cleanup(slave->dev);
1364 else
1365 slave->dev->npinfo = NULL;
1366 }
1367 }
1368 read_unlock(&bond->lock);
1369}
1370
1371#else
1372
1373static void bond_netpoll_cleanup(struct net_device *bond_dev)
1374{
1375}
1376
1377#endif
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379/*---------------------------------- IOCTL ----------------------------------*/
1380
Adrian Bunk4ad072c2007-07-09 11:51:12 -07001381static int bond_sethwaddr(struct net_device *bond_dev,
1382 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001384 pr_debug("bond_dev=%p\n", bond_dev);
1385 pr_debug("slave_dev=%p\n", slave_dev);
1386 pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1388 return 0;
1389}
1390
Herbert Xu7f353bf2007-08-10 15:47:58 -07001391#define BOND_VLAN_FEATURES \
1392 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1393 NETIF_F_HW_VLAN_FILTER)
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001394
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001395/*
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001396 * Compute the common dev->feature set available to all slaves. Some
Herbert Xu7f353bf2007-08-10 15:47:58 -07001397 * feature bits are managed elsewhere, so preserve those feature bits
1398 * on the master device.
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001399 */
1400static int bond_compute_features(struct bonding *bond)
1401{
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001402 struct slave *slave;
1403 struct net_device *bond_dev = bond->dev;
Herbert Xu7f353bf2007-08-10 15:47:58 -07001404 unsigned long features = bond_dev->features;
Jay Vosburgh278339a2009-08-28 12:05:12 +00001405 unsigned long vlan_features = 0;
Moni Shoua3158bf72007-10-09 19:43:41 -07001406 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1407 bond_dev->hard_header_len);
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001408 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001409
Herbert Xu7f353bf2007-08-10 15:47:58 -07001410 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
Herbert Xub63365a2008-10-23 01:11:29 -07001411 features |= NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1412
1413 if (!bond->first_slave)
1414 goto done;
1415
1416 features &= ~NETIF_F_ONE_FOR_ALL;
Herbert Xu7f353bf2007-08-10 15:47:58 -07001417
Jay Vosburgh278339a2009-08-28 12:05:12 +00001418 vlan_features = bond->first_slave->dev->vlan_features;
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001419 bond_for_each_slave(bond, slave, i) {
Herbert Xub63365a2008-10-23 01:11:29 -07001420 features = netdev_increment_features(features,
1421 slave->dev->features,
1422 NETIF_F_ONE_FOR_ALL);
Jay Vosburgh278339a2009-08-28 12:05:12 +00001423 vlan_features = netdev_increment_features(vlan_features,
1424 slave->dev->vlan_features,
1425 NETIF_F_ONE_FOR_ALL);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001426 if (slave->dev->hard_header_len > max_hard_header_len)
1427 max_hard_header_len = slave->dev->hard_header_len;
1428 }
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001429
Herbert Xub63365a2008-10-23 01:11:29 -07001430done:
Herbert Xu7f353bf2007-08-10 15:47:58 -07001431 features |= (bond_dev->features & BOND_VLAN_FEATURES);
Herbert Xub63365a2008-10-23 01:11:29 -07001432 bond_dev->features = netdev_fix_features(features, NULL);
Jay Vosburgh278339a2009-08-28 12:05:12 +00001433 bond_dev->vlan_features = netdev_fix_features(vlan_features, NULL);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001434 bond_dev->hard_header_len = max_hard_header_len;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001435
1436 return 0;
1437}
1438
Moni Shoua872254d2007-10-09 19:43:38 -07001439static void bond_setup_by_slave(struct net_device *bond_dev,
1440 struct net_device *slave_dev)
1441{
Wang Chen454d7c92008-11-12 23:37:49 -08001442 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07001443
Stephen Hemminger00829822008-11-20 20:14:53 -08001444 bond_dev->header_ops = slave_dev->header_ops;
Moni Shoua872254d2007-10-09 19:43:38 -07001445
1446 bond_dev->type = slave_dev->type;
1447 bond_dev->hard_header_len = slave_dev->hard_header_len;
1448 bond_dev->addr_len = slave_dev->addr_len;
1449
1450 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1451 slave_dev->addr_len);
Moni Shouad90a1622007-10-09 19:43:43 -07001452 bond->setup_by_slave = 1;
Moni Shoua872254d2007-10-09 19:43:38 -07001453}
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455/* enslave device <slave> to bond device <master> */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001456int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Wang Chen454d7c92008-11-12 23:37:49 -08001458 struct bonding *bond = netdev_priv(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001459 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 struct slave *new_slave = NULL;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001461 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 struct sockaddr addr;
1463 int link_reporting;
1464 int old_features = bond_dev->features;
1465 int res = 0;
1466
nsxfreddy@gmail.com552709d2005-09-21 14:18:04 -05001467 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001468 slave_ops->ndo_do_ioctl == NULL) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001469 pr_warning("%s: Warning: no link monitoring support for %s\n",
1470 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
1472
1473 /* bond must be initialized by bond_open() before enslaving */
1474 if (!(bond_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001475 pr_warning("%s: master_dev is not up in bond_enslave\n",
1476 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
1478
1479 /* already enslaved */
1480 if (slave_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001481 pr_debug("Error, Device was already enslaved\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return -EBUSY;
1483 }
1484
1485 /* vlan challenged mutual exclusion */
1486 /* no need to lock since we're protected by rtnl_lock */
1487 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001488 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Jay Vosburghf35188f2010-07-21 12:14:47 +00001489 if (bond->vlgrp) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001490 pr_err("%s: Error: cannot enslave VLAN challenged slave %s on VLAN enabled bond %s\n",
1491 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return -EPERM;
1493 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001494 pr_warning("%s: Warning: enslaved VLAN challenged slave %s. Adding VLANs will be blocked as long as %s is part of bond %s\n",
1495 bond_dev->name, slave_dev->name,
1496 slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1498 }
1499 } else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001500 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (bond->slave_cnt == 0) {
1502 /* First slave, and it is not VLAN challenged,
1503 * so remove the block of adding VLANs over the bond.
1504 */
1505 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1506 }
1507 }
1508
Jay Vosburgh217df672005-09-26 16:11:50 -07001509 /*
1510 * Old ifenslave binaries are no longer supported. These can
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001511 * be identified with moderate accuracy by the state of the slave:
Jay Vosburgh217df672005-09-26 16:11:50 -07001512 * the current ifenslave will set the interface down prior to
1513 * enslaving it; the old ifenslave will not.
1514 */
1515 if ((slave_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001516 pr_err("%s is up. This may be due to an out of date ifenslave.\n",
Jay Vosburgh217df672005-09-26 16:11:50 -07001517 slave_dev->name);
1518 res = -EPERM;
1519 goto err_undo_flags;
1520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Moni Shoua872254d2007-10-09 19:43:38 -07001522 /* set bonding device ether type by slave - bonding netdevices are
1523 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1524 * there is a need to override some of the type dependent attribs/funcs.
1525 *
1526 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1527 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1528 */
1529 if (bond->slave_cnt == 0) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001530 if (bond_dev->type != slave_dev->type) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001531 pr_debug("%s: change device type from %d to %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001532 bond_dev->name,
1533 bond_dev->type, slave_dev->type);
Moni Shoua75c78502009-09-15 02:37:40 -07001534
Jiri Pirko3ca5b402010-03-10 10:29:35 +00001535 res = netdev_bonding_change(bond_dev,
1536 NETDEV_PRE_TYPE_CHANGE);
1537 res = notifier_to_errno(res);
1538 if (res) {
1539 pr_err("%s: refused to change device type\n",
1540 bond_dev->name);
1541 res = -EBUSY;
1542 goto err_undo_flags;
1543 }
Moni Shoua75c78502009-09-15 02:37:40 -07001544
Jiri Pirko32a806c2010-03-19 04:00:23 +00001545 /* Flush unicast and multicast addresses */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001546 dev_uc_flush(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001547 dev_mc_flush(bond_dev);
Jiri Pirko32a806c2010-03-19 04:00:23 +00001548
Moni Shouae36b9d12009-07-15 04:56:31 +00001549 if (slave_dev->type != ARPHRD_ETHER)
1550 bond_setup_by_slave(bond_dev, slave_dev);
1551 else
1552 ether_setup(bond_dev);
Moni Shoua75c78502009-09-15 02:37:40 -07001553
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001554 netdev_bonding_change(bond_dev,
1555 NETDEV_POST_TYPE_CHANGE);
Moni Shouae36b9d12009-07-15 04:56:31 +00001556 }
Moni Shoua872254d2007-10-09 19:43:38 -07001557 } else if (bond_dev->type != slave_dev->type) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001558 pr_err("%s ether type (%d) is different from other slaves (%d), can not enslave it.\n",
1559 slave_dev->name,
1560 slave_dev->type, bond_dev->type);
1561 res = -EINVAL;
1562 goto err_undo_flags;
Moni Shoua872254d2007-10-09 19:43:38 -07001563 }
1564
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001565 if (slave_ops->ndo_set_mac_address == NULL) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001566 if (bond->slave_cnt == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001567 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.",
1568 bond_dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001569 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1570 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001571 pr_err("%s: Error: The slave device specified does not support setting the MAC address, but fail_over_mac is not set to active.\n",
1572 bond_dev->name);
Moni Shoua2ab82852007-10-09 19:43:39 -07001573 res = -EOPNOTSUPP;
1574 goto err_undo_flags;
1575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001578 /* If this is the first slave, then we need to set the master's hardware
1579 * address to be the same as the slave's. */
1580 if (bond->slave_cnt == 0)
1581 memcpy(bond->dev->dev_addr, slave_dev->dev_addr,
1582 slave_dev->addr_len);
1583
1584
Joe Jin243cb4e2007-02-06 14:16:40 -08001585 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (!new_slave) {
1587 res = -ENOMEM;
1588 goto err_undo_flags;
1589 }
1590
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001591 /*
1592 * Set the new_slave's queue_id to be zero. Queue ID mapping
1593 * is set via sysfs or module option if desired.
1594 */
1595 new_slave->queue_id = 0;
1596
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001597 /* Save slave's original mtu and then set it to match the bond */
1598 new_slave->original_mtu = slave_dev->mtu;
1599 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1600 if (res) {
1601 pr_debug("Error %d calling dev_set_mtu\n", res);
1602 goto err_free;
1603 }
1604
Jay Vosburgh217df672005-09-26 16:11:50 -07001605 /*
1606 * Save slave's original ("permanent") mac address for modes
1607 * that need it, and for restoring it upon release, and then
1608 * set it to the master's address
1609 */
1610 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Jay Vosburghdd957c52007-10-09 19:57:24 -07001612 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001613 /*
1614 * Set slave to master's mac address. The application already
1615 * set the master's mac address to that of the first slave
1616 */
1617 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1618 addr.sa_family = slave_dev->type;
1619 res = dev_set_mac_address(slave_dev, &addr);
1620 if (res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001621 pr_debug("Error %d calling set_mac_address\n", res);
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001622 goto err_restore_mtu;
Moni Shoua2ab82852007-10-09 19:43:39 -07001623 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001626 res = netdev_set_master(slave_dev, bond_dev);
1627 if (res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001628 pr_debug("Error %d calling netdev_set_master\n", res);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001629 goto err_restore_mac;
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001630 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001631 /* open the slave since the application closed it */
1632 res = dev_open(slave_dev);
1633 if (res) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001634 pr_debug("Opening slave %s failed\n", slave_dev->name);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001635 goto err_unset_master;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 new_slave->dev = slave_dev;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001639 slave_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Holger Eitzenberger58402052008-12-09 23:07:13 -08001641 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 /* bond_alb_init_slave() must be called before all other stages since
1643 * it might fail and we do not want to have to undo everything
1644 */
1645 res = bond_alb_init_slave(bond, new_slave);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001646 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001647 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
1649
1650 /* If the mode USES_PRIMARY, then the new slave gets the
1651 * master's promisc (and mc) settings only if it becomes the
1652 * curr_active_slave, and that is taken care of later when calling
1653 * bond_change_active()
1654 */
1655 if (!USES_PRIMARY(bond->params.mode)) {
1656 /* set promiscuity level to new slave */
1657 if (bond_dev->flags & IFF_PROMISC) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001658 res = dev_set_promiscuity(slave_dev, 1);
1659 if (res)
1660 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
1662
1663 /* set allmulti level to new slave */
1664 if (bond_dev->flags & IFF_ALLMULTI) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001665 res = dev_set_allmulti(slave_dev, 1);
1666 if (res)
1667 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
1669
David S. Millerb9e40852008-07-15 00:15:08 -07001670 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 /* upload master's mc_list to new slave */
Jiri Pirko22bedad32010-04-01 21:22:57 +00001672 netdev_for_each_mc_addr(ha, bond_dev)
1673 dev_mc_add(slave_dev, ha->addr);
David S. Millerb9e40852008-07-15 00:15:08 -07001674 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676
1677 if (bond->params.mode == BOND_MODE_8023AD) {
1678 /* add lacpdu mc addr to mc list */
1679 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1680
Jiri Pirko22bedad32010-04-01 21:22:57 +00001681 dev_mc_add(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683
1684 bond_add_vlans_on_slave(bond, slave_dev);
1685
1686 write_lock_bh(&bond->lock);
1687
1688 bond_attach_slave(bond, new_slave);
1689
1690 new_slave->delay = 0;
1691 new_slave->link_failure_count = 0;
1692
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001693 bond_compute_features(bond);
1694
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001695 write_unlock_bh(&bond->lock);
1696
1697 read_lock(&bond->lock);
1698
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001699 new_slave->last_arp_rx = jiffies;
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (bond->params.miimon && !bond->params.use_carrier) {
1702 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1703
1704 if ((link_reporting == -1) && !bond->params.arp_interval) {
1705 /*
1706 * miimon is set but a bonded network driver
1707 * does not support ETHTOOL/MII and
1708 * arp_interval is not set. Note: if
1709 * use_carrier is enabled, we will never go
1710 * here (because netif_carrier is always
1711 * supported); thus, we don't need to change
1712 * the messages for netif_carrier.
1713 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001714 pr_warning("%s: Warning: MII and ETHTOOL support not available for interface %s, and arp_interval/arp_ip_target module parameters not specified, thus bonding will not detect link failures! see bonding.txt for details.\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001715 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 } else if (link_reporting == -1) {
1717 /* unable get link status using mii/ethtool */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001718 pr_warning("%s: Warning: can't get link status from interface %s; the network driver associated with this interface does not support MII or ETHTOOL link status reporting, thus miimon has no effect on this interface.\n",
1719 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
1721 }
1722
1723 /* check for initial state */
1724 if (!bond->params.miimon ||
1725 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1726 if (bond->params.updelay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001727 pr_debug("Initial state of slave_dev is BOND_LINK_BACK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 new_slave->link = BOND_LINK_BACK;
1729 new_slave->delay = bond->params.updelay;
1730 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001731 pr_debug("Initial state of slave_dev is BOND_LINK_UP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 new_slave->link = BOND_LINK_UP;
1733 }
1734 new_slave->jiffies = jiffies;
1735 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001736 pr_debug("Initial state of slave_dev is BOND_LINK_DOWN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 new_slave->link = BOND_LINK_DOWN;
1738 }
1739
1740 if (bond_update_speed_duplex(new_slave) &&
1741 (new_slave->link != BOND_LINK_DOWN)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001742 pr_warning("%s: Warning: failed to get speed and duplex from %s, assumed to be 100Mb/sec and Full.\n",
1743 bond_dev->name, new_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 if (bond->params.mode == BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001746 pr_warning("%s: Warning: Operation of 802.3ad mode requires ETHTOOL support in base driver for proper aggregator selection.\n",
1747 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
1749 }
1750
1751 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1752 /* if there is a primary slave, remember it */
Jiri Pirkoa5499522009-09-25 03:28:09 +00001753 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 bond->primary_slave = new_slave;
Jiri Pirkoa5499522009-09-25 03:28:09 +00001755 bond->force_primary = true;
1756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
1758
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001759 write_lock_bh(&bond->curr_slave_lock);
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 switch (bond->params.mode) {
1762 case BOND_MODE_ACTIVEBACKUP:
Jay Vosburgh8a8e4472006-09-22 21:56:15 -07001763 bond_set_slave_inactive_flags(new_slave);
1764 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 break;
1766 case BOND_MODE_8023AD:
1767 /* in 802.3ad mode, the internal mechanism
1768 * will activate the slaves in the selected
1769 * aggregator
1770 */
1771 bond_set_slave_inactive_flags(new_slave);
1772 /* if this is the first slave */
1773 if (bond->slave_cnt == 1) {
1774 SLAVE_AD_INFO(new_slave).id = 1;
1775 /* Initialize AD with the number of times that the AD timer is called in 1 second
1776 * can be called only after the mac address of the bond is set
1777 */
1778 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1779 bond->params.lacp_fast);
1780 } else {
1781 SLAVE_AD_INFO(new_slave).id =
1782 SLAVE_AD_INFO(new_slave->prev).id + 1;
1783 }
1784
1785 bond_3ad_bind_slave(new_slave);
1786 break;
1787 case BOND_MODE_TLB:
1788 case BOND_MODE_ALB:
1789 new_slave->state = BOND_STATE_ACTIVE;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001790 bond_set_slave_inactive_flags(new_slave);
Jiri Pirko5a29f782009-03-25 17:23:38 -07001791 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 break;
1793 default:
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001794 pr_debug("This slave is always active in trunk mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 /* always active in trunk mode */
1797 new_slave->state = BOND_STATE_ACTIVE;
1798
1799 /* In trunking mode there is little meaning to curr_active_slave
1800 * anyway (it holds no special properties of the bond device),
1801 * so we can change it without calling change_active_interface()
1802 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001803 if (!bond->curr_active_slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 bond->curr_active_slave = new_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 break;
1807 } /* switch(bond_mode) */
1808
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001809 write_unlock_bh(&bond->curr_slave_lock);
1810
Jay Vosburghff59c452006-03-27 13:27:43 -08001811 bond_set_carrier(bond);
1812
WANG Congf6dc31a2010-05-06 00:48:51 -07001813#ifdef CONFIG_NET_POLL_CONTROLLER
Andy Gospodarekc22d7ac2010-06-25 09:50:44 +00001814 if (disable_netpoll) {
WANG Congf6dc31a2010-05-06 00:48:51 -07001815 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
Andy Gospodarekc22d7ac2010-06-25 09:50:44 +00001816 } else {
1817 if (slaves_support_netpoll(bond_dev)) {
1818 bond_dev->priv_flags &= ~IFF_DISABLE_NETPOLL;
1819 if (bond_dev->npinfo)
1820 slave_dev->npinfo = bond_dev->npinfo;
1821 } else if (!(bond_dev->priv_flags & IFF_DISABLE_NETPOLL)) {
1822 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
1823 pr_info("New slave device %s does not support netpoll\n",
1824 slave_dev->name);
1825 pr_info("Disabling netpoll support for %s\n", bond_dev->name);
1826 }
WANG Congf6dc31a2010-05-06 00:48:51 -07001827 }
1828#endif
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001829 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001831 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1832 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001833 goto err_close;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001834
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001835 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
1836 bond_dev->name, slave_dev->name,
1837 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1838 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 /* enslave is successful */
1841 return 0;
1842
1843/* Undo stages on error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844err_close:
1845 dev_close(slave_dev);
1846
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001847err_unset_master:
1848 netdev_set_master(slave_dev, NULL);
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850err_restore_mac:
Jay Vosburghdd957c52007-10-09 19:57:24 -07001851 if (!bond->params.fail_over_mac) {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001852 /* XXX TODO - fom follow mode needs to change master's
1853 * MAC if this slave's MAC is in use by the bond, or at
1854 * least print a warning.
1855 */
Moni Shoua2ab82852007-10-09 19:43:39 -07001856 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1857 addr.sa_family = slave_dev->type;
1858 dev_set_mac_address(slave_dev, &addr);
1859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001861err_restore_mtu:
1862 dev_set_mtu(slave_dev, new_slave->original_mtu);
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864err_free:
1865 kfree(new_slave);
1866
1867err_undo_flags:
1868 bond_dev->features = old_features;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 return res;
1871}
1872
1873/*
1874 * Try to release the slave device <slave> from the bond device <master>
1875 * It is legal to access curr_active_slave without a lock because all the function
1876 * is write-locked.
1877 *
1878 * The rules for slave state should be:
1879 * for Active/Backup:
1880 * Active stays on all backups go down
1881 * for Bonded connections:
1882 * The first up interface should be left on and all others downed.
1883 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001884int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
Wang Chen454d7c92008-11-12 23:37:49 -08001886 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 struct slave *slave, *oldcurrent;
1888 struct sockaddr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 /* slave is not a slave or master is not master of this slave */
1891 if (!(slave_dev->flags & IFF_SLAVE) ||
1892 (slave_dev->master != bond_dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001893 pr_err("%s: Error: cannot release %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 bond_dev->name, slave_dev->name);
1895 return -EINVAL;
1896 }
1897
Neil Hormane843fa52010-10-13 16:01:50 +00001898 block_netpoll_tx();
WANG Congf6dc31a2010-05-06 00:48:51 -07001899 netdev_bonding_change(bond_dev, NETDEV_BONDING_DESLAVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 write_lock_bh(&bond->lock);
1901
1902 slave = bond_get_slave_by_dev(bond, slave_dev);
1903 if (!slave) {
1904 /* not a slave of this bond */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001905 pr_info("%s: %s not enslaved\n",
1906 bond_dev->name, slave_dev->name);
Jay Vosburghf5e2a7b2006-02-07 21:17:22 -08001907 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001908 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return -EINVAL;
1910 }
1911
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001912 if (!bond->params.fail_over_mac) {
Joe Perches8e95a202009-12-03 07:58:21 +00001913 if (!compare_ether_addr(bond_dev->dev_addr, slave->perm_hwaddr) &&
1914 bond->slave_cnt > 1)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001915 pr_warning("%s: Warning: the permanent HWaddr of %s - %pM - is still in use by %s. Set the HWaddr of %s to a different address to avoid conflicts.\n",
1916 bond_dev->name, slave_dev->name,
1917 slave->perm_hwaddr,
1918 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
1920
1921 /* Inform AD package of unbinding of slave. */
1922 if (bond->params.mode == BOND_MODE_8023AD) {
1923 /* must be called before the slave is
1924 * detached from the list
1925 */
1926 bond_3ad_unbind_slave(slave);
1927 }
1928
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001929 pr_info("%s: releasing %s interface %s\n",
1930 bond_dev->name,
1931 (slave->state == BOND_STATE_ACTIVE) ? "active" : "backup",
1932 slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 oldcurrent = bond->curr_active_slave;
1935
1936 bond->current_arp_slave = NULL;
1937
1938 /* release the slave from its bond */
1939 bond_detach_slave(bond, slave);
1940
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001941 bond_compute_features(bond);
1942
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001943 if (bond->primary_slave == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 bond->primary_slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001946 if (oldcurrent == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 bond_change_active_slave(bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Holger Eitzenberger58402052008-12-09 23:07:13 -08001949 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 /* Must be called only after the slave has been
1951 * detached from the list and the curr_active_slave
1952 * has been cleared (if our_slave == old_current),
1953 * but before a new active slave is selected.
1954 */
Jay Vosburgh25433312008-01-17 16:24:59 -08001955 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 bond_alb_deinit_slave(bond, slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08001957 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001960 if (oldcurrent == slave) {
1961 /*
1962 * Note that we hold RTNL over this sequence, so there
1963 * is no concern that another slave add/remove event
1964 * will interfere.
1965 */
1966 write_unlock_bh(&bond->lock);
1967 read_lock(&bond->lock);
1968 write_lock_bh(&bond->curr_slave_lock);
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 bond_select_active_slave(bond);
1971
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001972 write_unlock_bh(&bond->curr_slave_lock);
1973 read_unlock(&bond->lock);
1974 write_lock_bh(&bond->lock);
1975 }
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 if (bond->slave_cnt == 0) {
Jay Vosburghff59c452006-03-27 13:27:43 -08001978 bond_set_carrier(bond);
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 /* if the last slave was removed, zero the mac address
1981 * of the master so it will be set by the application
1982 * to the mac address of the first slave
1983 */
1984 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1985
Jay Vosburghf35188f2010-07-21 12:14:47 +00001986 if (!bond->vlgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1988 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001989 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
1990 bond_dev->name, bond_dev->name);
1991 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
1992 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
1994 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
1995 !bond_has_challenged_slaves(bond)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001996 pr_info("%s: last VLAN challenged slave %s left bond %s. VLAN blocking is removed\n",
1997 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1999 }
2000
2001 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002002 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002004 /* must do this from outside any spinlocks */
2005 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 bond_del_vlans_from_slave(bond, slave_dev);
2008
2009 /* If the mode USES_PRIMARY, then we should only remove its
2010 * promisc and mc settings if it was the curr_active_slave, but that was
2011 * already taken care of above when we detached the slave
2012 */
2013 if (!USES_PRIMARY(bond->params.mode)) {
2014 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002015 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002019 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002023 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002025 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027
2028 netdev_set_master(slave_dev, NULL);
2029
WANG Congf6dc31a2010-05-06 00:48:51 -07002030#ifdef CONFIG_NET_POLL_CONTROLLER
2031 read_lock_bh(&bond->lock);
Andy Gospodarekc22d7ac2010-06-25 09:50:44 +00002032
2033 /* Make sure netpoll over stays disabled until fixed. */
2034 if (!disable_netpoll)
2035 if (slaves_support_netpoll(bond_dev))
2036 bond_dev->priv_flags &= ~IFF_DISABLE_NETPOLL;
WANG Congf6dc31a2010-05-06 00:48:51 -07002037 read_unlock_bh(&bond->lock);
2038 if (slave_dev->netdev_ops->ndo_netpoll_cleanup)
2039 slave_dev->netdev_ops->ndo_netpoll_cleanup(slave_dev);
2040 else
2041 slave_dev->npinfo = NULL;
2042#endif
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 /* close slave before restoring its mac address */
2045 dev_close(slave_dev);
2046
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07002047 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002048 /* restore original ("permanent") mac address */
2049 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2050 addr.sa_family = slave_dev->type;
2051 dev_set_mac_address(slave_dev, &addr);
2052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00002054 dev_set_mtu(slave_dev, slave->original_mtu);
2055
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002056 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002057 IFF_SLAVE_INACTIVE | IFF_BONDING |
2058 IFF_SLAVE_NEEDARP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 kfree(slave);
2061
2062 return 0; /* deletion OK */
2063}
2064
2065/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002066* First release a slave and than destroy the bond if no more slaves are left.
Moni Shouad90a1622007-10-09 19:43:43 -07002067* Must be under rtnl_lock when this function is called.
2068*/
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002069int bond_release_and_destroy(struct net_device *bond_dev,
2070 struct net_device *slave_dev)
Moni Shouad90a1622007-10-09 19:43:43 -07002071{
Wang Chen454d7c92008-11-12 23:37:49 -08002072 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002073 int ret;
2074
2075 ret = bond_release(bond_dev, slave_dev);
2076 if ((ret == 0) && (bond->slave_cnt == 0)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002077 pr_info("%s: destroying bond %s.\n",
2078 bond_dev->name, bond_dev->name);
Stephen Hemminger9e716262009-06-12 19:02:47 +00002079 unregister_netdevice(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002080 }
2081 return ret;
2082}
2083
2084/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 * This function releases all slaves.
2086 */
2087static int bond_release_all(struct net_device *bond_dev)
2088{
Wang Chen454d7c92008-11-12 23:37:49 -08002089 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 struct slave *slave;
2091 struct net_device *slave_dev;
2092 struct sockaddr addr;
2093
Neil Hormane843fa52010-10-13 16:01:50 +00002094 block_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 write_lock_bh(&bond->lock);
2096
Jay Vosburghff59c452006-03-27 13:27:43 -08002097 netif_carrier_off(bond_dev);
2098
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002099 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102 bond->current_arp_slave = NULL;
2103 bond->primary_slave = NULL;
2104 bond_change_active_slave(bond, NULL);
2105
2106 while ((slave = bond->first_slave) != NULL) {
2107 /* Inform AD package of unbinding of slave
2108 * before slave is detached from the list.
2109 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002110 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 bond_3ad_unbind_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 slave_dev = slave->dev;
2114 bond_detach_slave(bond, slave);
2115
Jay Vosburgh25433312008-01-17 16:24:59 -08002116 /* now that the slave is detached, unlock and perform
2117 * all the undo steps that should not be called from
2118 * within a lock.
2119 */
2120 write_unlock_bh(&bond->lock);
2121
Holger Eitzenberger58402052008-12-09 23:07:13 -08002122 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 /* must be called only after the slave
2124 * has been detached from the list
2125 */
2126 bond_alb_deinit_slave(bond, slave);
2127 }
2128
Arthur Kepner8531c5f2005-08-23 01:34:53 -04002129 bond_compute_features(bond);
2130
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002131 bond_destroy_slave_symlinks(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 bond_del_vlans_from_slave(bond, slave_dev);
2133
2134 /* If the mode USES_PRIMARY, then we should only remove its
2135 * promisc and mc settings if it was the curr_active_slave, but that was
2136 * already taken care of above when we detached the slave
2137 */
2138 if (!USES_PRIMARY(bond->params.mode)) {
2139 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002140 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002144 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002148 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002150 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 }
2152
2153 netdev_set_master(slave_dev, NULL);
2154
2155 /* close slave before restoring its mac address */
2156 dev_close(slave_dev);
2157
Jay Vosburghdd957c52007-10-09 19:57:24 -07002158 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002159 /* restore original ("permanent") mac address*/
2160 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2161 addr.sa_family = slave_dev->type;
2162 dev_set_mac_address(slave_dev, &addr);
2163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002165 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
2166 IFF_SLAVE_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 kfree(slave);
2169
2170 /* re-acquire the lock before getting the next slave */
2171 write_lock_bh(&bond->lock);
2172 }
2173
2174 /* zero the mac address of the master so it will be
2175 * set by the application to the mac address of the
2176 * first slave
2177 */
2178 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2179
Jay Vosburghf35188f2010-07-21 12:14:47 +00002180 if (!bond->vlgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
Jay Vosburghf35188f2010-07-21 12:14:47 +00002182 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002183 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2184 bond_dev->name, bond_dev->name);
2185 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2186 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 }
2188
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002189 pr_info("%s: released all slaves\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191out:
2192 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002193 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 return 0;
2196}
2197
2198/*
2199 * This function changes the active slave to slave <slave_dev>.
2200 * It returns -EINVAL in the following cases.
2201 * - <slave_dev> is not found in the list.
2202 * - There is not active slave now.
2203 * - <slave_dev> is already active.
2204 * - The link state of <slave_dev> is not BOND_LINK_UP.
2205 * - <slave_dev> is not running.
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002206 * In these cases, this function does nothing.
2207 * In the other cases, current_slave pointer is changed and 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 */
2209static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2210{
Wang Chen454d7c92008-11-12 23:37:49 -08002211 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 struct slave *old_active = NULL;
2213 struct slave *new_active = NULL;
2214 int res = 0;
2215
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002216 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 /* Verify that master_dev is indeed the master of slave_dev */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002220 if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002223 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002225 read_lock(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 old_active = bond->curr_active_slave;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002227 read_unlock(&bond->curr_slave_lock);
2228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 new_active = bond_get_slave_by_dev(bond, slave_dev);
2230
2231 /*
2232 * Changing to the current active: do nothing; return success.
2233 */
2234 if (new_active && (new_active == old_active)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002235 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 return 0;
2237 }
2238
2239 if ((new_active) &&
2240 (old_active) &&
2241 (new_active->link == BOND_LINK_UP) &&
2242 IS_UP(new_active->dev)) {
Neil Hormane843fa52010-10-13 16:01:50 +00002243 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002244 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 bond_change_active_slave(bond, new_active);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002246 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002247 unblock_netpoll_tx();
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002248 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002251 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253 return res;
2254}
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2257{
Wang Chen454d7c92008-11-12 23:37:49 -08002258 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 info->bond_mode = bond->params.mode;
2261 info->miimon = bond->params.miimon;
2262
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002263 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 info->num_slaves = bond->slave_cnt;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002265 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
2267 return 0;
2268}
2269
2270static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2271{
Wang Chen454d7c92008-11-12 23:37:49 -08002272 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 struct slave *slave;
Eric Dumazet689c96c2009-04-23 03:39:04 +00002274 int i, res = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002276 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 bond_for_each_slave(bond, slave, i) {
2279 if (i == (int)info->slave_id) {
Eric Dumazet689c96c2009-04-23 03:39:04 +00002280 res = 0;
2281 strcpy(info->slave_name, slave->dev->name);
2282 info->link = slave->link;
2283 info->state = slave->state;
2284 info->link_failure_count = slave->link_failure_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 break;
2286 }
2287 }
2288
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002289 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Eric Dumazet689c96c2009-04-23 03:39:04 +00002291 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292}
2293
2294/*-------------------------------- Monitoring -------------------------------*/
2295
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002296
2297static int bond_miimon_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002299 struct slave *slave;
2300 int i, link_state, commit = 0;
Jiri Pirko41f89102009-04-24 03:57:29 +00002301 bool ignore_updelay;
2302
2303 ignore_updelay = !bond->curr_active_slave ? true : false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305 bond_for_each_slave(bond, slave, i) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002306 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002308 link_state = bond_check_dev_link(bond, slave->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
2310 switch (slave->link) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002311 case BOND_LINK_UP:
2312 if (link_state)
2313 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002315 slave->link = BOND_LINK_FAIL;
2316 slave->delay = bond->params.downdelay;
2317 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002318 pr_info("%s: link status down for %sinterface %s, disabling it in %d ms.\n",
2319 bond->dev->name,
2320 (bond->params.mode ==
2321 BOND_MODE_ACTIVEBACKUP) ?
2322 ((slave->state == BOND_STATE_ACTIVE) ?
2323 "active " : "backup ") : "",
2324 slave->dev->name,
2325 bond->params.downdelay * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002327 /*FALLTHRU*/
2328 case BOND_LINK_FAIL:
2329 if (link_state) {
2330 /*
2331 * recovered before downdelay expired
2332 */
2333 slave->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 slave->jiffies = jiffies;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002335 pr_info("%s: link status up again after %d ms for interface %s.\n",
2336 bond->dev->name,
2337 (bond->params.downdelay - slave->delay) *
2338 bond->params.miimon,
2339 slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002340 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002342
2343 if (slave->delay <= 0) {
2344 slave->new_link = BOND_LINK_DOWN;
2345 commit++;
2346 continue;
2347 }
2348
2349 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002352 case BOND_LINK_DOWN:
2353 if (!link_state)
2354 continue;
2355
2356 slave->link = BOND_LINK_BACK;
2357 slave->delay = bond->params.updelay;
2358
2359 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002360 pr_info("%s: link status up for interface %s, enabling it in %d ms.\n",
2361 bond->dev->name, slave->dev->name,
2362 ignore_updelay ? 0 :
2363 bond->params.updelay *
2364 bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002366 /*FALLTHRU*/
2367 case BOND_LINK_BACK:
2368 if (!link_state) {
2369 slave->link = BOND_LINK_DOWN;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002370 pr_info("%s: link status down again after %d ms for interface %s.\n",
2371 bond->dev->name,
2372 (bond->params.updelay - slave->delay) *
2373 bond->params.miimon,
2374 slave->dev->name);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002375
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002376 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002378
Jiri Pirko41f89102009-04-24 03:57:29 +00002379 if (ignore_updelay)
2380 slave->delay = 0;
2381
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002382 if (slave->delay <= 0) {
2383 slave->new_link = BOND_LINK_UP;
2384 commit++;
Jiri Pirko41f89102009-04-24 03:57:29 +00002385 ignore_updelay = false;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002386 continue;
2387 }
2388
2389 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 break;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002391 }
2392 }
2393
2394 return commit;
2395}
2396
2397static void bond_miimon_commit(struct bonding *bond)
2398{
2399 struct slave *slave;
2400 int i;
2401
2402 bond_for_each_slave(bond, slave, i) {
2403 switch (slave->new_link) {
2404 case BOND_LINK_NOCHANGE:
2405 continue;
2406
2407 case BOND_LINK_UP:
2408 slave->link = BOND_LINK_UP;
2409 slave->jiffies = jiffies;
2410
2411 if (bond->params.mode == BOND_MODE_8023AD) {
2412 /* prevent it from being the active one */
2413 slave->state = BOND_STATE_BACKUP;
2414 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2415 /* make it immediately active */
2416 slave->state = BOND_STATE_ACTIVE;
2417 } else if (slave != bond->primary_slave) {
2418 /* prevent it from being the active one */
2419 slave->state = BOND_STATE_BACKUP;
2420 }
2421
Krzysztof Piotr Oledzki546add72010-10-06 14:28:22 -07002422 bond_update_speed_duplex(slave);
2423
Krzysztof Piotr Oledzki700c2a72010-10-06 14:25:06 -07002424 pr_info("%s: link status definitely up for interface %s, %d Mbps %s duplex.\n",
2425 bond->dev->name, slave->dev->name,
2426 slave->speed, slave->duplex ? "full" : "half");
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002427
2428 /* notify ad that the link status has changed */
2429 if (bond->params.mode == BOND_MODE_8023AD)
2430 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2431
Holger Eitzenberger58402052008-12-09 23:07:13 -08002432 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002433 bond_alb_handle_link_change(bond, slave,
2434 BOND_LINK_UP);
2435
2436 if (!bond->curr_active_slave ||
2437 (slave == bond->primary_slave))
2438 goto do_failover;
2439
2440 continue;
2441
2442 case BOND_LINK_DOWN:
Jay Vosburghfba4acd2008-10-30 17:41:14 -07002443 if (slave->link_failure_count < UINT_MAX)
2444 slave->link_failure_count++;
2445
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002446 slave->link = BOND_LINK_DOWN;
2447
2448 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2449 bond->params.mode == BOND_MODE_8023AD)
2450 bond_set_slave_inactive_flags(slave);
2451
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002452 pr_info("%s: link status definitely down for interface %s, disabling it\n",
2453 bond->dev->name, slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002454
2455 if (bond->params.mode == BOND_MODE_8023AD)
2456 bond_3ad_handle_link_change(slave,
2457 BOND_LINK_DOWN);
2458
Jiri Pirkoae63e802009-05-27 05:42:36 +00002459 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002460 bond_alb_handle_link_change(bond, slave,
2461 BOND_LINK_DOWN);
2462
2463 if (slave == bond->curr_active_slave)
2464 goto do_failover;
2465
2466 continue;
2467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002469 pr_err("%s: invalid new link %d on slave %s\n",
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002470 bond->dev->name, slave->new_link,
2471 slave->dev->name);
2472 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002474 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002477do_failover:
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002478 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00002479 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002480 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 bond_select_active_slave(bond);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002482 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002483 unblock_netpoll_tx();
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002484 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002485
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002486 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002489/*
2490 * bond_mii_monitor
2491 *
2492 * Really a wrapper that splits the mii monitor into two phases: an
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002493 * inspection, then (if inspection indicates something needs to be done)
2494 * an acquisition of appropriate locks followed by a commit phase to
2495 * implement whatever link state changes are indicated.
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002496 */
2497void bond_mii_monitor(struct work_struct *work)
2498{
2499 struct bonding *bond = container_of(work, struct bonding,
2500 mii_work.work);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002501
2502 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002503 if (bond->kill_timers)
2504 goto out;
2505
2506 if (bond->slave_cnt == 0)
2507 goto re_arm;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002508
2509 if (bond->send_grat_arp) {
2510 read_lock(&bond->curr_slave_lock);
2511 bond_send_gratuitous_arp(bond);
2512 read_unlock(&bond->curr_slave_lock);
2513 }
2514
Brian Haley305d5522008-11-04 17:51:14 -08002515 if (bond->send_unsol_na) {
2516 read_lock(&bond->curr_slave_lock);
2517 bond_send_unsolicited_na(bond);
2518 read_unlock(&bond->curr_slave_lock);
2519 }
2520
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002521 if (bond_miimon_inspect(bond)) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002522 read_unlock(&bond->lock);
2523 rtnl_lock();
2524 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002525
2526 bond_miimon_commit(bond);
2527
Jay Vosburgh56556622008-01-17 16:25:03 -08002528 read_unlock(&bond->lock);
2529 rtnl_unlock(); /* might sleep, hold no other locks */
2530 read_lock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002531 }
2532
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002533re_arm:
2534 if (bond->params.miimon)
2535 queue_delayed_work(bond->wq, &bond->mii_work,
2536 msecs_to_jiffies(bond->params.miimon));
2537out:
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002538 read_unlock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002539}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002540
Al Virod3bb52b2007-08-22 20:06:58 -04002541static __be32 bond_glean_dev_ip(struct net_device *dev)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002542{
2543 struct in_device *idev;
2544 struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07002545 __be32 addr = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002546
2547 if (!dev)
2548 return 0;
2549
2550 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002551 idev = __in_dev_get_rcu(dev);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002552 if (!idev)
2553 goto out;
2554
2555 ifa = idev->ifa_list;
2556 if (!ifa)
2557 goto out;
2558
2559 addr = ifa->ifa_local;
2560out:
2561 rcu_read_unlock();
2562 return addr;
2563}
2564
Al Virod3bb52b2007-08-22 20:06:58 -04002565static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002566{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002567 struct vlan_entry *vlan;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002568
2569 if (ip == bond->master_ip)
2570 return 1;
2571
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002572 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002573 if (ip == vlan->vlan_ip)
2574 return 1;
2575 }
2576
2577 return 0;
2578}
2579
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002580/*
2581 * We go to the (large) trouble of VLAN tagging ARP frames because
2582 * switches in VLAN mode (especially if ports are configured as
2583 * "native" to a VLAN) might not pass non-tagged frames.
2584 */
Al Virod3bb52b2007-08-22 20:06:58 -04002585static 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 -04002586{
2587 struct sk_buff *skb;
2588
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002589 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002590 slave_dev->name, dest_ip, src_ip, vlan_id);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002591
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002592 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2593 NULL, slave_dev->dev_addr, NULL);
2594
2595 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002596 pr_err("ARP packet allocation failed\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002597 return;
2598 }
2599 if (vlan_id) {
2600 skb = vlan_put_tag(skb, vlan_id);
2601 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002602 pr_err("failed to insert VLAN tag\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002603 return;
2604 }
2605 }
2606 arp_xmit(skb);
2607}
2608
2609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2611{
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002612 int i, vlan_id, rv;
Al Virod3bb52b2007-08-22 20:06:58 -04002613 __be32 *targets = bond->params.arp_targets;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002614 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002615 struct net_device *vlan_dev;
2616 struct flowi fl;
2617 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Mitch Williams6b780562005-11-09 10:36:19 -08002619 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2620 if (!targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07002621 break;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002622 pr_debug("basa: target %x\n", targets[i]);
Jay Vosburghf35188f2010-07-21 12:14:47 +00002623 if (!bond->vlgrp) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002624 pr_debug("basa: empty vlan: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002625 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2626 bond->master_ip, 0);
2627 continue;
2628 }
2629
2630 /*
2631 * If VLANs are configured, we do a route lookup to
2632 * determine which VLAN interface would be used, so we
2633 * can tag the ARP with the proper VLAN tag.
2634 */
2635 memset(&fl, 0, sizeof(fl));
2636 fl.fl4_dst = targets[i];
2637 fl.fl4_tos = RTO_ONLINK;
2638
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00002639 rv = ip_route_output_key(dev_net(bond->dev), &rt, &fl);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002640 if (rv) {
2641 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002642 pr_warning("%s: no route to arp_ip_target %pI4\n",
2643 bond->dev->name, &fl.fl4_dst);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002644 }
2645 continue;
2646 }
2647
2648 /*
2649 * This target is not on a VLAN
2650 */
Changli Gaod8d1f302010-06-10 23:31:35 -07002651 if (rt->dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002652 ip_rt_put(rt);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002653 pr_debug("basa: rtdev == bond->dev: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002654 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2655 bond->master_ip, 0);
2656 continue;
2657 }
2658
2659 vlan_id = 0;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002660 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002661 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Changli Gaod8d1f302010-06-10 23:31:35 -07002662 if (vlan_dev == rt->dst.dev) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002663 vlan_id = vlan->vlan_id;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002664 pr_debug("basa: vlan match on %s %d\n",
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002665 vlan_dev->name, vlan_id);
2666 break;
2667 }
2668 }
2669
2670 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002671 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002672 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2673 vlan->vlan_ip, vlan_id);
2674 continue;
2675 }
2676
2677 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002678 pr_warning("%s: no path to arp_ip_target %pI4 via rt.dev %s\n",
2679 bond->dev->name, &fl.fl4_dst,
Changli Gaod8d1f302010-06-10 23:31:35 -07002680 rt->dst.dev ? rt->dst.dev->name : "NULL");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002681 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002682 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002683 }
2684}
2685
2686/*
2687 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2688 * for each VLAN above us.
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002689 *
2690 * Caller must hold curr_slave_lock for read or better
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002691 */
2692static void bond_send_gratuitous_arp(struct bonding *bond)
2693{
2694 struct slave *slave = bond->curr_active_slave;
2695 struct vlan_entry *vlan;
2696 struct net_device *vlan_dev;
2697
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002698 pr_debug("bond_send_grat_arp: bond %s slave %s\n",
2699 bond->dev->name, slave ? slave->dev->name : "NULL");
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002700
2701 if (!slave || !bond->send_grat_arp ||
2702 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002703 return;
2704
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002705 bond->send_grat_arp--;
2706
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002707 if (bond->master_ip) {
2708 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
Moni Shoua1053f622007-10-09 19:43:42 -07002709 bond->master_ip, 0);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002710 }
2711
Jay Vosburghf35188f2010-07-21 12:14:47 +00002712 if (!bond->vlgrp)
2713 return;
2714
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002715 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002716 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002717 if (vlan->vlan_ip) {
2718 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2719 vlan->vlan_ip, vlan->vlan_id);
2720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 }
2722}
2723
Al Virod3bb52b2007-08-22 20:06:58 -04002724static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002725{
2726 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002727 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002728
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002729 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002730 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002731 &sip, &tip, i, &targets[i],
2732 bond_has_this_ip(bond, tip));
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002733 if (sip == targets[i]) {
2734 if (bond_has_this_ip(bond, tip))
2735 slave->last_arp_rx = jiffies;
2736 return;
2737 }
2738 }
2739}
2740
2741static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2742{
2743 struct arphdr *arp;
2744 struct slave *slave;
2745 struct bonding *bond;
2746 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002747 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002748
Andy Gospodarek1f3c8802009-12-14 10:48:58 +00002749 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2750 /*
2751 * When using VLANS and bonding, dev and oriv_dev may be
2752 * incorrect if the physical interface supports VLAN
2753 * acceleration. With this change ARP validation now
2754 * works for hosts only reachable on the VLAN interface.
2755 */
2756 dev = vlan_dev_real_dev(dev);
2757 orig_dev = dev_get_by_index_rcu(dev_net(skb->dev),skb->skb_iif);
2758 }
2759
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002760 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2761 goto out;
2762
Wang Chen454d7c92008-11-12 23:37:49 -08002763 bond = netdev_priv(dev);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002764 read_lock(&bond->lock);
2765
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002766 pr_debug("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002767 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2768 orig_dev ? orig_dev->name : "NULL");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002769
2770 slave = bond_get_slave_by_dev(bond, orig_dev);
2771 if (!slave || !slave_do_arp_validate(bond, slave))
2772 goto out_unlock;
2773
Pavel Emelyanov988b7052008-03-03 12:20:57 -08002774 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002775 goto out_unlock;
2776
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002777 arp = arp_hdr(skb);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002778 if (arp->ar_hln != dev->addr_len ||
2779 skb->pkt_type == PACKET_OTHERHOST ||
2780 skb->pkt_type == PACKET_LOOPBACK ||
2781 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2782 arp->ar_pro != htons(ETH_P_IP) ||
2783 arp->ar_pln != 4)
2784 goto out_unlock;
2785
2786 arp_ptr = (unsigned char *)(arp + 1);
2787 arp_ptr += dev->addr_len;
2788 memcpy(&sip, arp_ptr, 4);
2789 arp_ptr += 4 + dev->addr_len;
2790 memcpy(&tip, arp_ptr, 4);
2791
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002792 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002793 bond->dev->name, slave->dev->name, slave->state,
2794 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2795 &sip, &tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002796
2797 /*
2798 * Backup slaves won't see the ARP reply, but do come through
2799 * here for each ARP probe (so we swap the sip/tip to validate
2800 * the probe). In a "redundant switch, common router" type of
2801 * configuration, the ARP probe will (hopefully) travel from
2802 * the active, through one switch, the router, then the other
2803 * switch before reaching the backup.
2804 */
2805 if (slave->state == BOND_STATE_ACTIVE)
2806 bond_validate_arp(bond, slave, sip, tip);
2807 else
2808 bond_validate_arp(bond, slave, tip, sip);
2809
2810out_unlock:
2811 read_unlock(&bond->lock);
2812out:
2813 dev_kfree_skb(skb);
2814 return NET_RX_SUCCESS;
2815}
2816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817/*
2818 * this function is called regularly to monitor each slave's link
2819 * ensuring that traffic is being sent and received when arp monitoring
2820 * is used in load-balancing mode. if the adapter has been dormant, then an
2821 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2822 * arp monitoring in active backup mode.
2823 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002824void bond_loadbalance_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002826 struct bonding *bond = container_of(work, struct bonding,
2827 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 struct slave *slave, *oldcurrent;
2829 int do_failover = 0;
2830 int delta_in_ticks;
2831 int i;
2832
2833 read_lock(&bond->lock);
2834
Jay Vosburgh5ce0da82008-05-17 21:10:07 -07002835 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002837 if (bond->kill_timers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002840 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843 read_lock(&bond->curr_slave_lock);
2844 oldcurrent = bond->curr_active_slave;
2845 read_unlock(&bond->curr_slave_lock);
2846
2847 /* see if any of the previous devices are up now (i.e. they have
2848 * xmt and rcv traffic). the curr_active_slave does not come into
2849 * the picture unless it is null. also, slave->jiffies is not needed
2850 * here because we send an arp on each slave and give a slave as
2851 * long as it needs to get the tx/rx within the delta.
2852 * TODO: what about up/down delay in arp mode? it wasn't here before
2853 * so it can wait
2854 */
2855 bond_for_each_slave(bond, slave, i) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002856 unsigned long trans_start = dev_trans_start(slave->dev);
2857
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002859 if (time_in_range(jiffies,
2860 trans_start - delta_in_ticks,
2861 trans_start + delta_in_ticks) &&
2862 time_in_range(jiffies,
2863 slave->dev->last_rx - delta_in_ticks,
2864 slave->dev->last_rx + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
2866 slave->link = BOND_LINK_UP;
2867 slave->state = BOND_STATE_ACTIVE;
2868
2869 /* primary_slave has no meaning in round-robin
2870 * mode. the window of a slave being up and
2871 * curr_active_slave being null after enslaving
2872 * is closed.
2873 */
2874 if (!oldcurrent) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002875 pr_info("%s: link status definitely up for interface %s, ",
2876 bond->dev->name,
2877 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 do_failover = 1;
2879 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002880 pr_info("%s: interface %s is now up\n",
2881 bond->dev->name,
2882 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 }
2884 }
2885 } else {
2886 /* slave->link == BOND_LINK_UP */
2887
2888 /* not all switches will respond to an arp request
2889 * when the source ip is 0, so don't take the link down
2890 * if we don't know our ip yet
2891 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002892 if (!time_in_range(jiffies,
2893 trans_start - delta_in_ticks,
2894 trans_start + 2 * delta_in_ticks) ||
2895 !time_in_range(jiffies,
2896 slave->dev->last_rx - delta_in_ticks,
2897 slave->dev->last_rx + 2 * delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
2899 slave->link = BOND_LINK_DOWN;
2900 slave->state = BOND_STATE_BACKUP;
2901
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002902 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002905 pr_info("%s: interface %s is now down.\n",
2906 bond->dev->name,
2907 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002909 if (slave == oldcurrent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 do_failover = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 }
2912 }
2913
2914 /* note: if switch is in round-robin mode, all links
2915 * must tx arp to ensure all links rx an arp - otherwise
2916 * links may oscillate or not come up at all; if switch is
2917 * in something like xor mode, there is nothing we can
2918 * do - all replies will be rx'ed on same link causing slaves
2919 * to be unstable during low/no traffic periods
2920 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002921 if (IS_UP(slave->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 bond_arp_send_all(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 }
2924
2925 if (do_failover) {
Neil Hormane843fa52010-10-13 16:01:50 +00002926 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002927 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
2929 bond_select_active_slave(bond);
2930
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002931 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002932 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 }
2934
2935re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002936 if (bond->params.arp_interval)
2937 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938out:
2939 read_unlock(&bond->lock);
2940}
2941
2942/*
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002943 * Called to inspect slaves for active-backup mode ARP monitor link state
2944 * changes. Sets new_link in slaves to specify what action should take
2945 * place for the slave. Returns 0 if no changes are found, >0 if changes
2946 * to link states must be committed.
2947 *
2948 * Called with bond->lock held for read.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002950static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 struct slave *slave;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002953 int i, commit = 0;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002954 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 bond_for_each_slave(bond, slave, i) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002957 slave->new_link = BOND_LINK_NOCHANGE;
2958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002960 if (time_in_range(jiffies,
2961 slave_last_rx(bond, slave) - delta_in_ticks,
2962 slave_last_rx(bond, slave) + delta_in_ticks)) {
2963
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002964 slave->new_link = BOND_LINK_UP;
2965 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002968 continue;
2969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002971 /*
2972 * Give slaves 2*delta after being enslaved or made
2973 * active. This avoids bouncing, as the last receive
2974 * times need a full ARP monitor cycle to be updated.
2975 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002976 if (time_in_range(jiffies,
2977 slave->jiffies - delta_in_ticks,
2978 slave->jiffies + 2 * delta_in_ticks))
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002979 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002981 /*
2982 * Backup slave is down if:
2983 * - No current_arp_slave AND
2984 * - more than 3*delta since last receive AND
2985 * - the bond has an IP address
2986 *
2987 * Note: a non-null current_arp_slave indicates
2988 * the curr_active_slave went down and we are
2989 * searching for a new one; under this condition
2990 * we only take the curr_active_slave down - this
2991 * gives each slave a chance to tx/rx traffic
2992 * before being taken out
2993 */
2994 if (slave->state == BOND_STATE_BACKUP &&
2995 !bond->current_arp_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002996 !time_in_range(jiffies,
2997 slave_last_rx(bond, slave) - delta_in_ticks,
2998 slave_last_rx(bond, slave) + 3 * delta_in_ticks)) {
2999
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003000 slave->new_link = BOND_LINK_DOWN;
3001 commit++;
3002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003004 /*
3005 * Active slave is down if:
3006 * - more than 2*delta since transmitting OR
3007 * - (more than 2*delta since receive AND
3008 * the bond has an IP address)
3009 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003010 trans_start = dev_trans_start(slave->dev);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003011 if ((slave->state == BOND_STATE_ACTIVE) &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003012 (!time_in_range(jiffies,
3013 trans_start - delta_in_ticks,
3014 trans_start + 2 * delta_in_ticks) ||
3015 !time_in_range(jiffies,
3016 slave_last_rx(bond, slave) - delta_in_ticks,
3017 slave_last_rx(bond, slave) + 2 * delta_in_ticks))) {
3018
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003019 slave->new_link = BOND_LINK_DOWN;
3020 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 }
3022 }
3023
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003024 return commit;
3025}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003027/*
3028 * Called to commit link state changes noted by inspection step of
3029 * active-backup mode ARP monitor.
3030 *
3031 * Called with RTNL and bond->lock for read.
3032 */
3033static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
3034{
3035 struct slave *slave;
3036 int i;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003037 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003039 bond_for_each_slave(bond, slave, i) {
3040 switch (slave->new_link) {
3041 case BOND_LINK_NOCHANGE:
3042 continue;
3043
3044 case BOND_LINK_UP:
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003045 trans_start = dev_trans_start(slave->dev);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003046 if ((!bond->curr_active_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003047 time_in_range(jiffies,
3048 trans_start - delta_in_ticks,
3049 trans_start + delta_in_ticks)) ||
Jiri Pirkob9f60252009-08-31 11:09:38 +00003050 bond->curr_active_slave != slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003051 slave->link = BOND_LINK_UP;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003052 bond->current_arp_slave = NULL;
3053
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003054 pr_info("%s: link status definitely up for interface %s.\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003055 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003056
Jiri Pirkob9f60252009-08-31 11:09:38 +00003057 if (!bond->curr_active_slave ||
3058 (slave == bond->primary_slave))
3059 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003060
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003061 }
3062
Jiri Pirkob9f60252009-08-31 11:09:38 +00003063 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003064
3065 case BOND_LINK_DOWN:
3066 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003069 slave->link = BOND_LINK_DOWN;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003070 bond_set_slave_inactive_flags(slave);
3071
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003072 pr_info("%s: link status definitely down for interface %s, disabling it\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003073 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003074
3075 if (slave == bond->curr_active_slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003076 bond->current_arp_slave = NULL;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003077 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003078 }
Jiri Pirkob9f60252009-08-31 11:09:38 +00003079
3080 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003081
3082 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003083 pr_err("%s: impossible: new_link %d on slave %s\n",
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003084 bond->dev->name, slave->new_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 slave->dev->name);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003086 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Jiri Pirkob9f60252009-08-31 11:09:38 +00003089do_failover:
3090 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00003091 block_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003092 write_lock_bh(&bond->curr_slave_lock);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003093 bond_select_active_slave(bond);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003094 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00003095 unblock_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003096 }
3097
3098 bond_set_carrier(bond);
3099}
3100
3101/*
3102 * Send ARP probes for active-backup mode ARP monitor.
3103 *
3104 * Called with bond->lock held for read.
3105 */
3106static void bond_ab_arp_probe(struct bonding *bond)
3107{
3108 struct slave *slave;
3109 int i;
3110
3111 read_lock(&bond->curr_slave_lock);
3112
3113 if (bond->current_arp_slave && bond->curr_active_slave)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003114 pr_info("PROBE: c_arp %s && cas %s BAD\n",
3115 bond->current_arp_slave->dev->name,
3116 bond->curr_active_slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003117
3118 if (bond->curr_active_slave) {
3119 bond_arp_send_all(bond, bond->curr_active_slave);
3120 read_unlock(&bond->curr_slave_lock);
3121 return;
3122 }
3123
3124 read_unlock(&bond->curr_slave_lock);
3125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 /* if we don't have a curr_active_slave, search for the next available
3127 * backup slave from the current_arp_slave and make it the candidate
3128 * for becoming the curr_active_slave
3129 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003130
3131 if (!bond->current_arp_slave) {
3132 bond->current_arp_slave = bond->first_slave;
3133 if (!bond->current_arp_slave)
3134 return;
3135 }
3136
3137 bond_set_slave_inactive_flags(bond->current_arp_slave);
3138
3139 /* search for next candidate */
3140 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3141 if (IS_UP(slave->dev)) {
3142 slave->link = BOND_LINK_BACK;
3143 bond_set_slave_active_flags(slave);
3144 bond_arp_send_all(bond, slave);
3145 slave->jiffies = jiffies;
3146 bond->current_arp_slave = slave;
3147 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 }
3149
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003150 /* if the link state is up at this point, we
3151 * mark it down - this can happen if we have
3152 * simultaneous link failures and
3153 * reselect_active_interface doesn't make this
3154 * one the current slave so it is still marked
3155 * up when it is actually down
3156 */
3157 if (slave->link == BOND_LINK_UP) {
3158 slave->link = BOND_LINK_DOWN;
3159 if (slave->link_failure_count < UINT_MAX)
3160 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003162 bond_set_slave_inactive_flags(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003164 pr_info("%s: backup interface %s is now down.\n",
3165 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 }
3167 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003168}
3169
3170void bond_activebackup_arp_mon(struct work_struct *work)
3171{
3172 struct bonding *bond = container_of(work, struct bonding,
3173 arp_work.work);
3174 int delta_in_ticks;
3175
3176 read_lock(&bond->lock);
3177
3178 if (bond->kill_timers)
3179 goto out;
3180
3181 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3182
3183 if (bond->slave_cnt == 0)
3184 goto re_arm;
3185
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003186 if (bond->send_grat_arp) {
3187 read_lock(&bond->curr_slave_lock);
3188 bond_send_gratuitous_arp(bond);
3189 read_unlock(&bond->curr_slave_lock);
3190 }
3191
Brian Haley305d5522008-11-04 17:51:14 -08003192 if (bond->send_unsol_na) {
3193 read_lock(&bond->curr_slave_lock);
3194 bond_send_unsolicited_na(bond);
3195 read_unlock(&bond->curr_slave_lock);
3196 }
3197
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003198 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3199 read_unlock(&bond->lock);
3200 rtnl_lock();
3201 read_lock(&bond->lock);
3202
3203 bond_ab_arp_commit(bond, delta_in_ticks);
3204
3205 read_unlock(&bond->lock);
3206 rtnl_unlock();
3207 read_lock(&bond->lock);
3208 }
3209
3210 bond_ab_arp_probe(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
3212re_arm:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003213 if (bond->params.arp_interval)
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003214 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215out:
3216 read_unlock(&bond->lock);
3217}
3218
3219/*------------------------------ proc/seq_file-------------------------------*/
3220
3221#ifdef CONFIG_PROC_FS
3222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00003224 __acquires(&dev_base_lock)
3225 __acquires(&bond->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
3227 struct bonding *bond = seq->private;
3228 loff_t off = 0;
3229 struct slave *slave;
3230 int i;
3231
3232 /* make sure the bond won't be taken away */
3233 read_lock(&dev_base_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003234 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003236 if (*pos == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 return SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
3239 bond_for_each_slave(bond, slave, i) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003240 if (++off == *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 return slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 }
3243
3244 return NULL;
3245}
3246
3247static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3248{
3249 struct bonding *bond = seq->private;
3250 struct slave *slave = v;
3251
3252 ++*pos;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003253 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 return bond->first_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
3256 slave = slave->next;
3257
3258 return (slave == bond->first_slave) ? NULL : slave;
3259}
3260
3261static void bond_info_seq_stop(struct seq_file *seq, void *v)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00003262 __releases(&bond->lock)
3263 __releases(&dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
3265 struct bonding *bond = seq->private;
3266
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003267 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 read_unlock(&dev_base_lock);
3269}
3270
3271static void bond_info_show_master(struct seq_file *seq)
3272{
3273 struct bonding *bond = seq->private;
3274 struct slave *curr;
Mitch Williams4756b022005-11-09 10:36:25 -08003275 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
3277 read_lock(&bond->curr_slave_lock);
3278 curr = bond->curr_active_slave;
3279 read_unlock(&bond->curr_slave_lock);
3280
Jay Vosburghdd957c52007-10-09 19:57:24 -07003281 seq_printf(seq, "Bonding Mode: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 bond_mode_name(bond->params.mode));
3283
Jay Vosburghdd957c52007-10-09 19:57:24 -07003284 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
3285 bond->params.fail_over_mac)
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07003286 seq_printf(seq, " (fail_over_mac %s)",
3287 fail_over_mac_tbl[bond->params.fail_over_mac].modename);
Jay Vosburghdd957c52007-10-09 19:57:24 -07003288
3289 seq_printf(seq, "\n");
3290
Mitch Williamsc61b75a2005-11-09 10:35:13 -08003291 if (bond->params.mode == BOND_MODE_XOR ||
3292 bond->params.mode == BOND_MODE_8023AD) {
3293 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3294 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3295 bond->params.xmit_policy);
3296 }
3297
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 if (USES_PRIMARY(bond->params.mode)) {
Jiri Pirkoa5499522009-09-25 03:28:09 +00003299 seq_printf(seq, "Primary Slave: %s",
Mitch Williams0f418b22005-11-09 10:35:21 -08003300 (bond->primary_slave) ?
3301 bond->primary_slave->dev->name : "None");
Jiri Pirkoa5499522009-09-25 03:28:09 +00003302 if (bond->primary_slave)
3303 seq_printf(seq, " (primary_reselect %s)",
3304 pri_reselect_tbl[bond->params.primary_reselect].modename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Jiri Pirkoa5499522009-09-25 03:28:09 +00003306 seq_printf(seq, "\nCurrently Active Slave: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 (curr) ? curr->dev->name : "None");
3308 }
3309
Jay Vosburghff59c452006-03-27 13:27:43 -08003310 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3311 "up" : "down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3313 seq_printf(seq, "Up Delay (ms): %d\n",
3314 bond->params.updelay * bond->params.miimon);
3315 seq_printf(seq, "Down Delay (ms): %d\n",
3316 bond->params.downdelay * bond->params.miimon);
3317
Mitch Williams4756b022005-11-09 10:36:25 -08003318
3319 /* ARP information */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003320 if (bond->params.arp_interval > 0) {
3321 int printed = 0;
Mitch Williams4756b022005-11-09 10:36:25 -08003322 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3323 bond->params.arp_interval);
3324
3325 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3326
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003327 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
Mitch Williams4756b022005-11-09 10:36:25 -08003328 if (!bond->params.arp_targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07003329 break;
Mitch Williams4756b022005-11-09 10:36:25 -08003330 if (printed)
3331 seq_printf(seq, ",");
Harvey Harrison8cf14e32008-10-29 22:43:33 -07003332 seq_printf(seq, " %pI4", &bond->params.arp_targets[i]);
Mitch Williams4756b022005-11-09 10:36:25 -08003333 printed = 1;
3334 }
3335 seq_printf(seq, "\n");
3336 }
3337
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 if (bond->params.mode == BOND_MODE_8023AD) {
3339 struct ad_info ad_info;
3340
3341 seq_puts(seq, "\n802.3ad info\n");
3342 seq_printf(seq, "LACP rate: %s\n",
3343 (bond->params.lacp_fast) ? "fast" : "slow");
Jay Vosburghfd989c82008-11-04 17:51:16 -08003344 seq_printf(seq, "Aggregator selection policy (ad_select): %s\n",
3345 ad_select_tbl[bond->params.ad_select].modename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
3347 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3348 seq_printf(seq, "bond %s has no active aggregator\n",
3349 bond->dev->name);
3350 } else {
3351 seq_printf(seq, "Active Aggregator Info:\n");
3352
3353 seq_printf(seq, "\tAggregator ID: %d\n",
3354 ad_info.aggregator_id);
3355 seq_printf(seq, "\tNumber of ports: %d\n",
3356 ad_info.ports);
3357 seq_printf(seq, "\tActor Key: %d\n",
3358 ad_info.actor_key);
3359 seq_printf(seq, "\tPartner Key: %d\n",
3360 ad_info.partner_key);
Johannes Berge1749612008-10-27 15:59:26 -07003361 seq_printf(seq, "\tPartner Mac Address: %pM\n",
3362 ad_info.partner_system);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 }
3364 }
3365}
3366
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003367static void bond_info_show_slave(struct seq_file *seq,
3368 const struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369{
3370 struct bonding *bond = seq->private;
3371
3372 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3373 seq_printf(seq, "MII Status: %s\n",
3374 (slave->link == BOND_LINK_UP) ? "up" : "down");
Krzysztof Oledzkidd53df22010-09-30 06:19:04 +00003375 seq_printf(seq, "Speed: %d Mbps\n", slave->speed);
3376 seq_printf(seq, "Duplex: %s\n", slave->duplex ? "full" : "half");
Kenzo Iwami655096452006-09-22 21:53:08 -07003377 seq_printf(seq, "Link Failure Count: %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 slave->link_failure_count);
3379
Johannes Berge1749612008-10-27 15:59:26 -07003380 seq_printf(seq, "Permanent HW addr: %pM\n", slave->perm_hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
3382 if (bond->params.mode == BOND_MODE_8023AD) {
3383 const struct aggregator *agg
3384 = SLAVE_AD_INFO(slave).port.aggregator;
3385
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003386 if (agg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 seq_printf(seq, "Aggregator ID: %d\n",
3388 agg->aggregator_identifier);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003389 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 seq_puts(seq, "Aggregator ID: N/A\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 }
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00003392 seq_printf(seq, "Slave queue ID: %d\n", slave->queue_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393}
3394
3395static int bond_info_seq_show(struct seq_file *seq, void *v)
3396{
3397 if (v == SEQ_START_TOKEN) {
3398 seq_printf(seq, "%s\n", version);
3399 bond_info_show_master(seq);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003400 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 bond_info_show_slave(seq, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
3403 return 0;
3404}
3405
Jan Engelhardt4101dec2009-01-14 13:52:18 -08003406static const struct seq_operations bond_info_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 .start = bond_info_seq_start,
3408 .next = bond_info_seq_next,
3409 .stop = bond_info_seq_stop,
3410 .show = bond_info_seq_show,
3411};
3412
3413static int bond_info_open(struct inode *inode, struct file *file)
3414{
3415 struct seq_file *seq;
3416 struct proc_dir_entry *proc;
3417 int res;
3418
3419 res = seq_open(file, &bond_info_seq_ops);
3420 if (!res) {
3421 /* recover the pointer buried in proc_dir_entry data */
3422 seq = file->private_data;
3423 proc = PDE(inode);
3424 seq->private = proc->data;
3425 }
3426
3427 return res;
3428}
3429
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08003430static const struct file_operations bond_info_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 .owner = THIS_MODULE,
3432 .open = bond_info_open,
3433 .read = seq_read,
3434 .llseek = seq_lseek,
3435 .release = seq_release,
3436};
3437
Nicolas de Pesloüan38fc0022009-10-13 00:45:06 -07003438static void bond_create_proc_entry(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439{
3440 struct net_device *bond_dev = bond->dev;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003441 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003443 if (bn->proc_dir) {
Denis V. Luneva95609c2008-04-29 01:02:29 -07003444 bond->proc_entry = proc_create_data(bond_dev->name,
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003445 S_IRUGO, bn->proc_dir,
Denis V. Luneva95609c2008-04-29 01:02:29 -07003446 &bond_info_fops, bond);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003447 if (bond->proc_entry == NULL)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003448 pr_warning("Warning: Cannot create /proc/net/%s/%s\n",
3449 DRV_NAME, bond_dev->name);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003450 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453}
3454
3455static void bond_remove_proc_entry(struct bonding *bond)
3456{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003457 struct net_device *bond_dev = bond->dev;
3458 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
3459
3460 if (bn->proc_dir && bond->proc_entry) {
3461 remove_proc_entry(bond->proc_file_name, bn->proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 memset(bond->proc_file_name, 0, IFNAMSIZ);
3463 bond->proc_entry = NULL;
3464 }
3465}
3466
3467/* Create the bonding directory under /proc/net, if doesn't exist yet.
3468 * Caller must hold rtnl_lock.
3469 */
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003470static void __net_init bond_create_proc_dir(struct bond_net *bn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003472 if (!bn->proc_dir) {
3473 bn->proc_dir = proc_mkdir(DRV_NAME, bn->net->proc_net);
3474 if (!bn->proc_dir)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003475 pr_warning("Warning: cannot create /proc/net/%s\n",
3476 DRV_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 }
3478}
3479
3480/* Destroy the bonding directory under /proc/net, if empty.
3481 * Caller must hold rtnl_lock.
3482 */
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003483static void __net_exit bond_destroy_proc_dir(struct bond_net *bn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003485 if (bn->proc_dir) {
3486 remove_proc_entry(DRV_NAME, bn->net->proc_net);
3487 bn->proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 }
3489}
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003490
3491#else /* !CONFIG_PROC_FS */
3492
Nicolas de Pesloüan38fc0022009-10-13 00:45:06 -07003493static void bond_create_proc_entry(struct bonding *bond)
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003494{
3495}
3496
3497static void bond_remove_proc_entry(struct bonding *bond)
3498{
3499}
3500
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003501static inline void bond_create_proc_dir(struct bond_net *bn)
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003502{
3503}
3504
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003505static inline void bond_destroy_proc_dir(struct bond_net *bn)
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003506{
3507}
3508
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509#endif /* CONFIG_PROC_FS */
3510
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003511
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512/*-------------------------- netdev event handling --------------------------*/
3513
3514/*
3515 * Change device name
3516 */
3517static int bond_event_changename(struct bonding *bond)
3518{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 bond_remove_proc_entry(bond);
3520 bond_create_proc_entry(bond);
Stephen Hemminger7e083842009-06-12 19:02:46 +00003521
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 return NOTIFY_DONE;
3523}
3524
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003525static int bond_master_netdev_event(unsigned long event,
3526 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527{
Wang Chen454d7c92008-11-12 23:37:49 -08003528 struct bonding *event_bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
3530 switch (event) {
3531 case NETDEV_CHANGENAME:
3532 return bond_event_changename(event_bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 default:
3534 break;
3535 }
3536
3537 return NOTIFY_DONE;
3538}
3539
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003540static int bond_slave_netdev_event(unsigned long event,
3541 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542{
3543 struct net_device *bond_dev = slave_dev->master;
Wang Chen454d7c92008-11-12 23:37:49 -08003544 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
3546 switch (event) {
3547 case NETDEV_UNREGISTER:
3548 if (bond_dev) {
Jay Vosburgh1284cd32007-10-15 16:44:27 -07003549 if (bond->setup_by_slave)
3550 bond_release_and_destroy(bond_dev, slave_dev);
3551 else
3552 bond_release(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 }
3554 break;
3555 case NETDEV_CHANGE:
Jay Vosburgh17d04502009-03-18 18:38:25 -07003556 if (bond->params.mode == BOND_MODE_8023AD || bond_is_lb(bond)) {
3557 struct slave *slave;
3558
3559 slave = bond_get_slave_by_dev(bond, slave_dev);
3560 if (slave) {
3561 u16 old_speed = slave->speed;
3562 u16 old_duplex = slave->duplex;
3563
3564 bond_update_speed_duplex(slave);
3565
3566 if (bond_is_lb(bond))
3567 break;
3568
3569 if (old_speed != slave->speed)
3570 bond_3ad_adapter_speed_changed(slave);
3571 if (old_duplex != slave->duplex)
3572 bond_3ad_adapter_duplex_changed(slave);
3573 }
3574 }
3575
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 break;
3577 case NETDEV_DOWN:
3578 /*
3579 * ... Or is it this?
3580 */
3581 break;
3582 case NETDEV_CHANGEMTU:
3583 /*
3584 * TODO: Should slaves be allowed to
3585 * independently alter their MTU? For
3586 * an active-backup bond, slaves need
3587 * not be the same type of device, so
3588 * MTUs may vary. For other modes,
3589 * slaves arguably should have the
3590 * same MTUs. To do this, we'd need to
3591 * take over the slave's change_mtu
3592 * function for the duration of their
3593 * servitude.
3594 */
3595 break;
3596 case NETDEV_CHANGENAME:
3597 /*
3598 * TODO: handle changing the primary's name
3599 */
3600 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003601 case NETDEV_FEAT_CHANGE:
3602 bond_compute_features(bond);
3603 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 default:
3605 break;
3606 }
3607
3608 return NOTIFY_DONE;
3609}
3610
3611/*
3612 * bond_netdev_event: handle netdev notifier chain events.
3613 *
3614 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003615 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 * locks for us to safely manipulate the slave devices (RTNL lock,
3617 * dev_probe_lock).
3618 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003619static int bond_netdev_event(struct notifier_block *this,
3620 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621{
3622 struct net_device *event_dev = (struct net_device *)ptr;
3623
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003624 pr_debug("event_dev: %s, event: %lx\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003625 event_dev ? event_dev->name : "None",
3626 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003628 if (!(event_dev->priv_flags & IFF_BONDING))
3629 return NOTIFY_DONE;
3630
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 if (event_dev->flags & IFF_MASTER) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003632 pr_debug("IFF_MASTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 return bond_master_netdev_event(event, event_dev);
3634 }
3635
3636 if (event_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003637 pr_debug("IFF_SLAVE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 return bond_slave_netdev_event(event, event_dev);
3639 }
3640
3641 return NOTIFY_DONE;
3642}
3643
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003644/*
3645 * bond_inetaddr_event: handle inetaddr notifier chain events.
3646 *
3647 * We keep track of device IPs primarily to use as source addresses in
3648 * ARP monitor probes (rather than spewing out broadcasts all the time).
3649 *
3650 * We track one IP for the main device (if it has one), plus one per VLAN.
3651 */
3652static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3653{
3654 struct in_ifaddr *ifa = ptr;
3655 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003656 struct bond_net *bn = net_generic(dev_net(event_dev), bond_net_id);
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003657 struct bonding *bond;
3658 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003659
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003660 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003661 if (bond->dev == event_dev) {
3662 switch (event) {
3663 case NETDEV_UP:
3664 bond->master_ip = ifa->ifa_local;
3665 return NOTIFY_OK;
3666 case NETDEV_DOWN:
3667 bond->master_ip = bond_glean_dev_ip(bond->dev);
3668 return NOTIFY_OK;
3669 default:
3670 return NOTIFY_DONE;
3671 }
3672 }
3673
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003674 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +00003675 if (!bond->vlgrp)
3676 continue;
Dan Aloni5c15bde2007-03-02 20:44:51 -08003677 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003678 if (vlan_dev == event_dev) {
3679 switch (event) {
3680 case NETDEV_UP:
3681 vlan->vlan_ip = ifa->ifa_local;
3682 return NOTIFY_OK;
3683 case NETDEV_DOWN:
3684 vlan->vlan_ip =
3685 bond_glean_dev_ip(vlan_dev);
3686 return NOTIFY_OK;
3687 default:
3688 return NOTIFY_DONE;
3689 }
3690 }
3691 }
3692 }
3693 return NOTIFY_DONE;
3694}
3695
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696static struct notifier_block bond_netdev_notifier = {
3697 .notifier_call = bond_netdev_event,
3698};
3699
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003700static struct notifier_block bond_inetaddr_notifier = {
3701 .notifier_call = bond_inetaddr_event,
3702};
3703
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704/*-------------------------- Packet type handling ---------------------------*/
3705
3706/* register to receive lacpdus on a bond */
3707static void bond_register_lacpdu(struct bonding *bond)
3708{
3709 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3710
3711 /* initialize packet type */
3712 pk_type->type = PKT_TYPE_LACPDU;
3713 pk_type->dev = bond->dev;
3714 pk_type->func = bond_3ad_lacpdu_recv;
3715
3716 dev_add_pack(pk_type);
3717}
3718
3719/* unregister to receive lacpdus on a bond */
3720static void bond_unregister_lacpdu(struct bonding *bond)
3721{
3722 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3723}
3724
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003725void bond_register_arp(struct bonding *bond)
3726{
3727 struct packet_type *pt = &bond->arp_mon_pt;
3728
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003729 if (pt->type)
3730 return;
3731
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003732 pt->type = htons(ETH_P_ARP);
Jay Vosburghe245cb72007-02-28 17:03:27 -08003733 pt->dev = bond->dev;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003734 pt->func = bond_arp_rcv;
3735 dev_add_pack(pt);
3736}
3737
3738void bond_unregister_arp(struct bonding *bond)
3739{
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003740 struct packet_type *pt = &bond->arp_mon_pt;
3741
3742 dev_remove_pack(pt);
3743 pt->type = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003744}
3745
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003746/*---------------------------- Hashing Policies -----------------------------*/
3747
3748/*
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003749 * Hash for the output device based upon layer 2 and layer 3 data. If
3750 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3751 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003752static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003753{
3754 struct ethhdr *data = (struct ethhdr *)skb->data;
3755 struct iphdr *iph = ip_hdr(skb);
3756
Brian Haleyf14c4e42008-09-02 10:08:08 -04003757 if (skb->protocol == htons(ETH_P_IP)) {
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003758 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
Jasper Spaansd3da6832009-10-23 04:08:46 +00003759 (data->h_dest[5] ^ data->h_source[5])) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003760 }
3761
Jasper Spaansd3da6832009-10-23 04:08:46 +00003762 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003763}
3764
3765/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003766 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003767 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3768 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3769 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003770static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003771{
3772 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003773 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003774 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003775 int layer4_xor = 0;
3776
Brian Haleyf14c4e42008-09-02 10:08:08 -04003777 if (skb->protocol == htons(ETH_P_IP)) {
3778 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003779 (iph->protocol == IPPROTO_TCP ||
3780 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003781 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003782 }
3783 return (layer4_xor ^
3784 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3785
3786 }
3787
Jasper Spaansd3da6832009-10-23 04:08:46 +00003788 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003789}
3790
3791/*
3792 * Hash for the output device based upon layer 2 data
3793 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003794static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003795{
3796 struct ethhdr *data = (struct ethhdr *)skb->data;
3797
Jasper Spaansd3da6832009-10-23 04:08:46 +00003798 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003799}
3800
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801/*-------------------------- Device entry points ----------------------------*/
3802
3803static int bond_open(struct net_device *bond_dev)
3804{
Wang Chen454d7c92008-11-12 23:37:49 -08003805 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
3807 bond->kill_timers = 0;
3808
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003809 INIT_DELAYED_WORK(&bond->mcast_work, bond_resend_igmp_join_requests_delayed);
3810
Holger Eitzenberger58402052008-12-09 23:07:13 -08003811 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 /* bond_alb_initialize must be called before the timer
3813 * is started.
3814 */
3815 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3816 /* something went wrong - fail the open operation */
stephen hemmingerb4739462010-01-25 23:34:15 +00003817 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 }
3819
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003820 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3821 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 }
3823
3824 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003825 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3826 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 }
3828
3829 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003830 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3831 INIT_DELAYED_WORK(&bond->arp_work,
3832 bond_activebackup_arp_mon);
3833 else
3834 INIT_DELAYED_WORK(&bond->arp_work,
3835 bond_loadbalance_arp_mon);
3836
3837 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003838 if (bond->params.arp_validate)
3839 bond_register_arp(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 }
3841
3842 if (bond->params.mode == BOND_MODE_8023AD) {
Adrian Bunka40745f2007-10-24 18:27:43 +02003843 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003844 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 /* register to receive LACPDUs */
3846 bond_register_lacpdu(bond);
Jay Vosburghfd989c82008-11-04 17:51:16 -08003847 bond_3ad_initiate_agg_selection(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 }
3849
3850 return 0;
3851}
3852
3853static int bond_close(struct net_device *bond_dev)
3854{
Wang Chen454d7c92008-11-12 23:37:49 -08003855 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
3857 if (bond->params.mode == BOND_MODE_8023AD) {
3858 /* Unregister the receive of LACPDUs */
3859 bond_unregister_lacpdu(bond);
3860 }
3861
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003862 if (bond->params.arp_validate)
3863 bond_unregister_arp(bond);
3864
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 write_lock_bh(&bond->lock);
3866
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003867 bond->send_grat_arp = 0;
Brian Haley305d5522008-11-04 17:51:14 -08003868 bond->send_unsol_na = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869
3870 /* signal timers not to re-arm */
3871 bond->kill_timers = 1;
3872
3873 write_unlock_bh(&bond->lock);
3874
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003876 cancel_delayed_work(&bond->mii_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 }
3878
3879 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003880 cancel_delayed_work(&bond->arp_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 }
3882
3883 switch (bond->params.mode) {
3884 case BOND_MODE_8023AD:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003885 cancel_delayed_work(&bond->ad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 break;
3887 case BOND_MODE_TLB:
3888 case BOND_MODE_ALB:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003889 cancel_delayed_work(&bond->alb_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 break;
3891 default:
3892 break;
3893 }
3894
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003895 if (delayed_work_pending(&bond->mcast_work))
3896 cancel_delayed_work(&bond->mcast_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
Holger Eitzenberger58402052008-12-09 23:07:13 -08003898 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 /* Must be called only after all
3900 * slaves have been released
3901 */
3902 bond_alb_deinitialize(bond);
3903 }
3904
3905 return 0;
3906}
3907
Eric Dumazet28172732010-07-07 14:58:56 -07003908static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3909 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910{
Wang Chen454d7c92008-11-12 23:37:49 -08003911 struct bonding *bond = netdev_priv(bond_dev);
Eric Dumazet28172732010-07-07 14:58:56 -07003912 struct rtnl_link_stats64 temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 struct slave *slave;
3914 int i;
3915
Eric Dumazet28172732010-07-07 14:58:56 -07003916 memset(stats, 0, sizeof(*stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
3918 read_lock_bh(&bond->lock);
3919
3920 bond_for_each_slave(bond, slave, i) {
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00003921 const struct rtnl_link_stats64 *sstats =
Eric Dumazet28172732010-07-07 14:58:56 -07003922 dev_get_stats(slave->dev, &temp);
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08003923
Eric Dumazet28172732010-07-07 14:58:56 -07003924 stats->rx_packets += sstats->rx_packets;
3925 stats->rx_bytes += sstats->rx_bytes;
3926 stats->rx_errors += sstats->rx_errors;
3927 stats->rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928
Eric Dumazet28172732010-07-07 14:58:56 -07003929 stats->tx_packets += sstats->tx_packets;
3930 stats->tx_bytes += sstats->tx_bytes;
3931 stats->tx_errors += sstats->tx_errors;
3932 stats->tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933
Eric Dumazet28172732010-07-07 14:58:56 -07003934 stats->multicast += sstats->multicast;
3935 stats->collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
Eric Dumazet28172732010-07-07 14:58:56 -07003937 stats->rx_length_errors += sstats->rx_length_errors;
3938 stats->rx_over_errors += sstats->rx_over_errors;
3939 stats->rx_crc_errors += sstats->rx_crc_errors;
3940 stats->rx_frame_errors += sstats->rx_frame_errors;
3941 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3942 stats->rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943
Eric Dumazet28172732010-07-07 14:58:56 -07003944 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3945 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3946 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3947 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3948 stats->tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 }
3950
3951 read_unlock_bh(&bond->lock);
3952
3953 return stats;
3954}
3955
3956static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3957{
3958 struct net_device *slave_dev = NULL;
3959 struct ifbond k_binfo;
3960 struct ifbond __user *u_binfo = NULL;
3961 struct ifslave k_sinfo;
3962 struct ifslave __user *u_sinfo = NULL;
3963 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 int res = 0;
3965
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003966 pr_debug("bond_ioctl: master=%s, cmd=%d\n", bond_dev->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967
3968 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 case SIOCGMIIPHY:
3970 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003971 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003973
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 mii->phy_id = 0;
3975 /* Fall Through */
3976 case SIOCGMIIREG:
3977 /*
3978 * We do this again just in case we were called by SIOCGMIIREG
3979 * instead of SIOCGMIIPHY.
3980 */
3981 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003982 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003984
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
3986 if (mii->reg_num == 1) {
Wang Chen454d7c92008-11-12 23:37:49 -08003987 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 mii->val_out = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003989 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 read_lock(&bond->curr_slave_lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003991 if (netif_carrier_ok(bond->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 mii->val_out = BMSR_LSTATUS;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003993
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 read_unlock(&bond->curr_slave_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003995 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 }
3997
3998 return 0;
3999 case BOND_INFO_QUERY_OLD:
4000 case SIOCBONDINFOQUERY:
4001 u_binfo = (struct ifbond __user *)ifr->ifr_data;
4002
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004003 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
4006 res = bond_info_query(bond_dev, &k_binfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004007 if (res == 0 &&
4008 copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
4009 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
4011 return res;
4012 case BOND_SLAVE_INFO_QUERY_OLD:
4013 case SIOCBONDSLAVEINFOQUERY:
4014 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
4015
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004016 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018
4019 res = bond_slave_info_query(bond_dev, &k_sinfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004020 if (res == 0 &&
4021 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
4022 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
4024 return res;
4025 default:
4026 /* Go on */
4027 break;
4028 }
4029
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004030 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004033 slave_dev = dev_get_by_name(dev_net(bond_dev), ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004035 pr_debug("slave_dev=%p:\n", slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004037 if (!slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 res = -ENODEV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004039 else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004040 pr_debug("slave_dev->name=%s:\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 switch (cmd) {
4042 case BOND_ENSLAVE_OLD:
4043 case SIOCBONDENSLAVE:
4044 res = bond_enslave(bond_dev, slave_dev);
4045 break;
4046 case BOND_RELEASE_OLD:
4047 case SIOCBONDRELEASE:
4048 res = bond_release(bond_dev, slave_dev);
4049 break;
4050 case BOND_SETHWADDR_OLD:
4051 case SIOCBONDSETHWADDR:
4052 res = bond_sethwaddr(bond_dev, slave_dev);
4053 break;
4054 case BOND_CHANGE_ACTIVE_OLD:
4055 case SIOCBONDCHANGEACTIVE:
4056 res = bond_ioctl_change_active(bond_dev, slave_dev);
4057 break;
4058 default:
4059 res = -EOPNOTSUPP;
4060 }
4061
4062 dev_put(slave_dev);
4063 }
4064
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 return res;
4066}
4067
Jiri Pirko22bedad32010-04-01 21:22:57 +00004068static bool bond_addr_in_mc_list(unsigned char *addr,
4069 struct netdev_hw_addr_list *list,
4070 int addrlen)
4071{
4072 struct netdev_hw_addr *ha;
4073
4074 netdev_hw_addr_list_for_each(ha, list)
4075 if (!memcmp(ha->addr, addr, addrlen))
4076 return true;
4077
4078 return false;
4079}
4080
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081static void bond_set_multicast_list(struct net_device *bond_dev)
4082{
Wang Chen454d7c92008-11-12 23:37:49 -08004083 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00004084 struct netdev_hw_addr *ha;
4085 bool found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 /*
4088 * Do promisc before checking multicast_mode
4089 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004090 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07004091 /*
4092 * FIXME: Need to handle the error when one of the multi-slaves
4093 * encounters error.
4094 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 bond_set_promiscuity(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004097
4098 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 bond_set_promiscuity(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004100
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
4102 /* set allmulti flag to slaves */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004103 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07004104 /*
4105 * FIXME: Need to handle the error when one of the multi-slaves
4106 * encounters error.
4107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 bond_set_allmulti(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004110
4111 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 bond_set_allmulti(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004113
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004115 read_lock(&bond->lock);
4116
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 bond->flags = bond_dev->flags;
4118
4119 /* looking for addresses to add to slaves' mc list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004120 netdev_for_each_mc_addr(ha, bond_dev) {
4121 found = bond_addr_in_mc_list(ha->addr, &bond->mc_list,
4122 bond_dev->addr_len);
4123 if (!found)
4124 bond_mc_add(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 }
4126
4127 /* looking for addresses to delete from slaves' list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004128 netdev_hw_addr_list_for_each(ha, &bond->mc_list) {
4129 found = bond_addr_in_mc_list(ha->addr, &bond_dev->mc,
4130 bond_dev->addr_len);
4131 if (!found)
4132 bond_mc_del(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 }
4134
4135 /* save master's multicast list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004136 __hw_addr_flush(&bond->mc_list);
4137 __hw_addr_add_multiple(&bond->mc_list, &bond_dev->mc,
4138 bond_dev->addr_len, NETDEV_HW_ADDR_T_MULTICAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004140 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141}
4142
Stephen Hemminger00829822008-11-20 20:14:53 -08004143static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
4144{
4145 struct bonding *bond = netdev_priv(dev);
4146 struct slave *slave = bond->first_slave;
4147
4148 if (slave) {
4149 const struct net_device_ops *slave_ops
4150 = slave->dev->netdev_ops;
4151 if (slave_ops->ndo_neigh_setup)
Patrick McHardy72e22402009-03-05 01:57:44 -08004152 return slave_ops->ndo_neigh_setup(slave->dev, parms);
Stephen Hemminger00829822008-11-20 20:14:53 -08004153 }
4154 return 0;
4155}
4156
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157/*
4158 * Change the MTU of all of a master's slaves to match the master
4159 */
4160static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
4161{
Wang Chen454d7c92008-11-12 23:37:49 -08004162 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 struct slave *slave, *stop_at;
4164 int res = 0;
4165 int i;
4166
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004167 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004168 (bond_dev ? bond_dev->name : "None"), new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
4170 /* Can't hold bond->lock with bh disabled here since
4171 * some base drivers panic. On the other hand we can't
4172 * hold bond->lock without bh disabled because we'll
4173 * deadlock. The only solution is to rely on the fact
4174 * that we're under rtnl_lock here, and the slaves
4175 * list won't change. This doesn't solve the problem
4176 * of setting the slave's MTU while it is
4177 * transmitting, but the assumption is that the base
4178 * driver can handle that.
4179 *
4180 * TODO: figure out a way to safely iterate the slaves
4181 * list, but without holding a lock around the actual
4182 * call to the base driver.
4183 */
4184
4185 bond_for_each_slave(bond, slave, i) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004186 pr_debug("s %p s->p %p c_m %p\n",
4187 slave,
4188 slave->prev,
4189 slave->dev->netdev_ops->ndo_change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08004190
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 res = dev_set_mtu(slave->dev, new_mtu);
4192
4193 if (res) {
4194 /* If we failed to set the slave's mtu to the new value
4195 * we must abort the operation even in ACTIVE_BACKUP
4196 * mode, because if we allow the backup slaves to have
4197 * different mtu values than the active slave we'll
4198 * need to change their mtu when doing a failover. That
4199 * means changing their mtu from timer context, which
4200 * is probably not a good idea.
4201 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004202 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 goto unwind;
4204 }
4205 }
4206
4207 bond_dev->mtu = new_mtu;
4208
4209 return 0;
4210
4211unwind:
4212 /* unwind from head to the slave that failed */
4213 stop_at = slave;
4214 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4215 int tmp_res;
4216
4217 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4218 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004219 pr_debug("unwind err %d dev %s\n",
4220 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 }
4222 }
4223
4224 return res;
4225}
4226
4227/*
4228 * Change HW address
4229 *
4230 * Note that many devices must be down to change the HW address, and
4231 * downing the master releases all slaves. We can make bonds full of
4232 * bonding devices to test this, however.
4233 */
4234static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4235{
Wang Chen454d7c92008-11-12 23:37:49 -08004236 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 struct sockaddr *sa = addr, tmp_sa;
4238 struct slave *slave, *stop_at;
4239 int res = 0;
4240 int i;
4241
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004242 if (bond->params.mode == BOND_MODE_ALB)
4243 return bond_alb_set_mac_address(bond_dev, addr);
4244
4245
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004246 pr_debug("bond=%p, name=%s\n",
4247 bond, bond_dev ? bond_dev->name : "None");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Jay Vosburghdd957c52007-10-09 19:57:24 -07004249 /*
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004250 * If fail_over_mac is set to active, do nothing and return
4251 * success. Returning an error causes ifenslave to fail.
Jay Vosburghdd957c52007-10-09 19:57:24 -07004252 */
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004253 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
Jay Vosburghdd957c52007-10-09 19:57:24 -07004254 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07004255
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004256 if (!is_valid_ether_addr(sa->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258
4259 /* Can't hold bond->lock with bh disabled here since
4260 * some base drivers panic. On the other hand we can't
4261 * hold bond->lock without bh disabled because we'll
4262 * deadlock. The only solution is to rely on the fact
4263 * that we're under rtnl_lock here, and the slaves
4264 * list won't change. This doesn't solve the problem
4265 * of setting the slave's hw address while it is
4266 * transmitting, but the assumption is that the base
4267 * driver can handle that.
4268 *
4269 * TODO: figure out a way to safely iterate the slaves
4270 * list, but without holding a lock around the actual
4271 * call to the base driver.
4272 */
4273
4274 bond_for_each_slave(bond, slave, i) {
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004275 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004276 pr_debug("slave %p %s\n", slave, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004278 if (slave_ops->ndo_set_mac_address == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 res = -EOPNOTSUPP;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004280 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 goto unwind;
4282 }
4283
4284 res = dev_set_mac_address(slave->dev, addr);
4285 if (res) {
4286 /* TODO: consider downing the slave
4287 * and retry ?
4288 * User should expect communications
4289 * breakage anyway until ARP finish
4290 * updating, so...
4291 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004292 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 goto unwind;
4294 }
4295 }
4296
4297 /* success */
4298 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4299 return 0;
4300
4301unwind:
4302 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4303 tmp_sa.sa_family = bond_dev->type;
4304
4305 /* unwind from head to the slave that failed */
4306 stop_at = slave;
4307 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4308 int tmp_res;
4309
4310 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4311 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004312 pr_debug("unwind err %d dev %s\n",
4313 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 }
4315 }
4316
4317 return res;
4318}
4319
4320static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4321{
Wang Chen454d7c92008-11-12 23:37:49 -08004322 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 struct slave *slave, *start_at;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004324 int i, slave_no, res = 1;
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004325 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326
4327 read_lock(&bond->lock);
4328
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004329 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 goto out;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004331 /*
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004332 * Start with the curr_active_slave that joined the bond as the
4333 * default for sending IGMP traffic. For failover purposes one
4334 * needs to maintain some consistency for the interface that will
4335 * send the join/membership reports. The curr_active_slave found
4336 * will send all of this type of traffic.
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004337 */
Eric Dumazet00ae7022010-03-30 23:08:37 +00004338 if ((iph->protocol == IPPROTO_IGMP) &&
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004339 (skb->protocol == htons(ETH_P_IP))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004341 read_lock(&bond->curr_slave_lock);
4342 slave = bond->curr_active_slave;
4343 read_unlock(&bond->curr_slave_lock);
4344
4345 if (!slave)
4346 goto out;
4347 } else {
4348 /*
4349 * Concurrent TX may collide on rr_tx_counter; we accept
4350 * that as being rare enough not to justify using an
4351 * atomic op here.
4352 */
4353 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
4354
4355 bond_for_each_slave(bond, slave, i) {
4356 slave_no--;
4357 if (slave_no < 0)
4358 break;
4359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 }
4361
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004362 start_at = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 bond_for_each_slave_from(bond, slave, i, start_at) {
4364 if (IS_UP(slave->dev) &&
4365 (slave->link == BOND_LINK_UP) &&
4366 (slave->state == BOND_STATE_ACTIVE)) {
4367 res = bond_dev_queue_xmit(bond, skb, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 break;
4369 }
4370 }
4371
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372out:
4373 if (res) {
4374 /* no suitable interface, frame not sent */
4375 dev_kfree_skb(skb);
4376 }
4377 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004378 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379}
4380
John W. Linville075897c2005-09-28 17:50:53 -04004381
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382/*
4383 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4384 * the bond has a usable interface.
4385 */
4386static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4387{
Wang Chen454d7c92008-11-12 23:37:49 -08004388 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 int res = 1;
4390
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 read_lock(&bond->lock);
4392 read_lock(&bond->curr_slave_lock);
4393
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004394 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
John W. Linville075897c2005-09-28 17:50:53 -04004397 if (!bond->curr_active_slave)
4398 goto out;
4399
John W. Linville075897c2005-09-28 17:50:53 -04004400 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4401
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004403 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 /* no suitable interface, frame not sent */
4405 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004406
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 read_unlock(&bond->curr_slave_lock);
4408 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004409 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410}
4411
4412/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004413 * In bond_xmit_xor() , we determine the output device by using a pre-
4414 * determined xmit_hash_policy(), If the selected device is not enabled,
4415 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 */
4417static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4418{
Wang Chen454d7c92008-11-12 23:37:49 -08004419 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 struct slave *slave, *start_at;
4421 int slave_no;
4422 int i;
4423 int res = 1;
4424
4425 read_lock(&bond->lock);
4426
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004427 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
Jasper Spaansa361c832009-10-23 04:09:24 +00004430 slave_no = bond->xmit_hash_policy(skb, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
4432 bond_for_each_slave(bond, slave, i) {
4433 slave_no--;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004434 if (slave_no < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 }
4437
4438 start_at = slave;
4439
4440 bond_for_each_slave_from(bond, slave, i, start_at) {
4441 if (IS_UP(slave->dev) &&
4442 (slave->link == BOND_LINK_UP) &&
4443 (slave->state == BOND_STATE_ACTIVE)) {
4444 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4445 break;
4446 }
4447 }
4448
4449out:
4450 if (res) {
4451 /* no suitable interface, frame not sent */
4452 dev_kfree_skb(skb);
4453 }
4454 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004455 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456}
4457
4458/*
4459 * in broadcast mode, we send everything to all usable interfaces.
4460 */
4461static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4462{
Wang Chen454d7c92008-11-12 23:37:49 -08004463 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 struct slave *slave, *start_at;
4465 struct net_device *tx_dev = NULL;
4466 int i;
4467 int res = 1;
4468
4469 read_lock(&bond->lock);
4470
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004471 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
4474 read_lock(&bond->curr_slave_lock);
4475 start_at = bond->curr_active_slave;
4476 read_unlock(&bond->curr_slave_lock);
4477
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004478 if (!start_at)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
4481 bond_for_each_slave_from(bond, slave, i, start_at) {
4482 if (IS_UP(slave->dev) &&
4483 (slave->link == BOND_LINK_UP) &&
4484 (slave->state == BOND_STATE_ACTIVE)) {
4485 if (tx_dev) {
4486 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4487 if (!skb2) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004488 pr_err("%s: Error: bond_xmit_broadcast(): skb_clone() failed\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08004489 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 continue;
4491 }
4492
4493 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4494 if (res) {
4495 dev_kfree_skb(skb2);
4496 continue;
4497 }
4498 }
4499 tx_dev = slave->dev;
4500 }
4501 }
4502
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004503 if (tx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 res = bond_dev_queue_xmit(bond, skb, tx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
4506out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004507 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 /* no suitable interface, frame not sent */
4509 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004510
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 /* frame sent to all suitable interfaces */
4512 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004513 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514}
4515
4516/*------------------------- Device initialization ---------------------------*/
4517
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004518static void bond_set_xmit_hash_policy(struct bonding *bond)
4519{
4520 switch (bond->params.xmit_policy) {
4521 case BOND_XMIT_POLICY_LAYER23:
4522 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4523 break;
4524 case BOND_XMIT_POLICY_LAYER34:
4525 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4526 break;
4527 case BOND_XMIT_POLICY_LAYER2:
4528 default:
4529 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4530 break;
4531 }
4532}
4533
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004534/*
4535 * Lookup the slave that corresponds to a qid
4536 */
4537static inline int bond_slave_override(struct bonding *bond,
4538 struct sk_buff *skb)
4539{
4540 int i, res = 1;
4541 struct slave *slave = NULL;
4542 struct slave *check_slave;
4543
4544 read_lock(&bond->lock);
4545
4546 if (!BOND_IS_OK(bond) || !skb->queue_mapping)
4547 goto out;
4548
4549 /* Find out if any slaves have the same mapping as this skb. */
4550 bond_for_each_slave(bond, check_slave, i) {
4551 if (check_slave->queue_id == skb->queue_mapping) {
4552 slave = check_slave;
4553 break;
4554 }
4555 }
4556
4557 /* If the slave isn't UP, use default transmit policy. */
4558 if (slave && slave->queue_id && IS_UP(slave->dev) &&
4559 (slave->link == BOND_LINK_UP)) {
4560 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4561 }
4562
4563out:
4564 read_unlock(&bond->lock);
4565 return res;
4566}
4567
4568static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb)
4569{
4570 /*
4571 * This helper function exists to help dev_pick_tx get the correct
4572 * destination queue. Using a helper function skips the a call to
4573 * skb_tx_hash and will put the skbs in the queue we expect on their
4574 * way down to the bonding driver.
4575 */
4576 return skb->queue_mapping;
4577}
4578
Stephen Hemminger424efe92009-08-31 19:50:51 +00004579static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
Stephen Hemminger00829822008-11-20 20:14:53 -08004580{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004581 struct bonding *bond = netdev_priv(dev);
4582
Neil Hormane843fa52010-10-13 16:01:50 +00004583 /*
4584 * If we risk deadlock from transmitting this in the
4585 * netpoll path, tell netpoll to queue the frame for later tx
4586 */
4587 if (is_netpoll_tx_blocked(dev))
4588 return NETDEV_TX_BUSY;
4589
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004590 if (TX_QUEUE_OVERRIDE(bond->params.mode)) {
4591 if (!bond_slave_override(bond, skb))
4592 return NETDEV_TX_OK;
4593 }
Stephen Hemminger00829822008-11-20 20:14:53 -08004594
4595 switch (bond->params.mode) {
4596 case BOND_MODE_ROUNDROBIN:
4597 return bond_xmit_roundrobin(skb, dev);
4598 case BOND_MODE_ACTIVEBACKUP:
4599 return bond_xmit_activebackup(skb, dev);
4600 case BOND_MODE_XOR:
4601 return bond_xmit_xor(skb, dev);
4602 case BOND_MODE_BROADCAST:
4603 return bond_xmit_broadcast(skb, dev);
4604 case BOND_MODE_8023AD:
4605 return bond_3ad_xmit_xor(skb, dev);
4606 case BOND_MODE_ALB:
4607 case BOND_MODE_TLB:
4608 return bond_alb_xmit(skb, dev);
4609 default:
4610 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004611 pr_err("%s: Error: Unknown bonding mode %d\n",
4612 dev->name, bond->params.mode);
Stephen Hemminger00829822008-11-20 20:14:53 -08004613 WARN_ON_ONCE(1);
4614 dev_kfree_skb(skb);
4615 return NETDEV_TX_OK;
4616 }
4617}
4618
4619
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620/*
4621 * set bond mode specific net device operations
4622 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004623void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004625 struct net_device *bond_dev = bond->dev;
4626
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 switch (mode) {
4628 case BOND_MODE_ROUNDROBIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 break;
4630 case BOND_MODE_ACTIVEBACKUP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 break;
4632 case BOND_MODE_XOR:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004633 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 break;
4635 case BOND_MODE_BROADCAST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 break;
4637 case BOND_MODE_8023AD:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004638 bond_set_master_3ad_flags(bond);
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004639 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004642 bond_set_master_alb_flags(bond);
4643 /* FALLTHRU */
4644 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 break;
4646 default:
4647 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004648 pr_err("%s: Error: Unknown bonding mode %d\n",
4649 bond_dev->name, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 break;
4651 }
4652}
4653
Jay Vosburgh217df672005-09-26 16:11:50 -07004654static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4655 struct ethtool_drvinfo *drvinfo)
4656{
4657 strncpy(drvinfo->driver, DRV_NAME, 32);
4658 strncpy(drvinfo->version, DRV_VERSION, 32);
4659 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4660}
4661
Jeff Garzik7282d492006-09-13 14:30:00 -04004662static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004663 .get_drvinfo = bond_ethtool_get_drvinfo,
Stephen Hemmingerfa53eba2008-09-13 21:17:09 -04004664 .get_link = ethtool_op_get_link,
4665 .get_tx_csum = ethtool_op_get_tx_csum,
4666 .get_sg = ethtool_op_get_sg,
4667 .get_tso = ethtool_op_get_tso,
4668 .get_ufo = ethtool_op_get_ufo,
4669 .get_flags = ethtool_op_get_flags,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004670};
4671
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004672static const struct net_device_ops bond_netdev_ops = {
Stephen Hemminger181470f2009-06-12 19:02:52 +00004673 .ndo_init = bond_init,
Stephen Hemminger9e716262009-06-12 19:02:47 +00004674 .ndo_uninit = bond_uninit,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004675 .ndo_open = bond_open,
4676 .ndo_stop = bond_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08004677 .ndo_start_xmit = bond_start_xmit,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004678 .ndo_select_queue = bond_select_queue,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00004679 .ndo_get_stats64 = bond_get_stats,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004680 .ndo_do_ioctl = bond_do_ioctl,
4681 .ndo_set_multicast_list = bond_set_multicast_list,
4682 .ndo_change_mtu = bond_change_mtu,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004683 .ndo_set_mac_address = bond_set_mac_address,
Stephen Hemminger00829822008-11-20 20:14:53 -08004684 .ndo_neigh_setup = bond_neigh_setup,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004685 .ndo_vlan_rx_register = bond_vlan_rx_register,
4686 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4687 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
WANG Congf6dc31a2010-05-06 00:48:51 -07004688#ifdef CONFIG_NET_POLL_CONTROLLER
4689 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
4690 .ndo_poll_controller = bond_poll_controller,
4691#endif
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004692};
4693
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004694static void bond_destructor(struct net_device *bond_dev)
4695{
4696 struct bonding *bond = netdev_priv(bond_dev);
4697 if (bond->wq)
4698 destroy_workqueue(bond->wq);
4699 free_netdev(bond_dev);
4700}
4701
Stephen Hemminger181470f2009-06-12 19:02:52 +00004702static void bond_setup(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703{
Wang Chen454d7c92008-11-12 23:37:49 -08004704 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 /* initialize rwlocks */
4707 rwlock_init(&bond->lock);
4708 rwlock_init(&bond->curr_slave_lock);
4709
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00004710 bond->params = bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
4712 /* Initialize pointers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 bond->dev = bond_dev;
4714 INIT_LIST_HEAD(&bond->vlan_list);
4715
4716 /* Initialize the device entry points */
Stephen Hemminger181470f2009-06-12 19:02:52 +00004717 ether_setup(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004718 bond_dev->netdev_ops = &bond_netdev_ops;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004719 bond_dev->ethtool_ops = &bond_ethtool_ops;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004720 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004722 bond_dev->destructor = bond_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723
4724 /* Initialize the device options */
4725 bond_dev->tx_queue_len = 0;
4726 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004727 bond_dev->priv_flags |= IFF_BONDING;
Stephen Hemminger181470f2009-06-12 19:02:52 +00004728 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
4729
Jay Vosburgh6cf3f412008-11-03 18:16:50 -08004730 if (bond->params.arp_interval)
4731 bond_dev->priv_flags |= IFF_MASTER_ARPMON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732
4733 /* At first, we block adding VLANs. That's the only way to
4734 * prevent problems that occur when adding VLANs over an
4735 * empty bond. The block will be removed once non-challenged
4736 * slaves are enslaved.
4737 */
4738 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4739
Herbert Xu932ff272006-06-09 12:20:56 -07004740 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 * transmitting */
4742 bond_dev->features |= NETIF_F_LLTX;
4743
4744 /* By default, we declare the bond to be fully
4745 * VLAN hardware accelerated capable. Special
4746 * care is taken in the various xmit functions
4747 * when there are slaves that are not hw accel
4748 * capable
4749 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4751 NETIF_F_HW_VLAN_RX |
4752 NETIF_F_HW_VLAN_FILTER);
4753
Eric Dumazete6599c22010-09-17 09:25:07 +00004754 /* By default, we enable GRO on bonding devices.
4755 * Actual support requires lowlevel drivers are GRO ready.
4756 */
4757 bond_dev->features |= NETIF_F_GRO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758}
4759
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004760static void bond_work_cancel_all(struct bonding *bond)
4761{
4762 write_lock_bh(&bond->lock);
4763 bond->kill_timers = 1;
4764 write_unlock_bh(&bond->lock);
4765
4766 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4767 cancel_delayed_work(&bond->mii_work);
4768
4769 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4770 cancel_delayed_work(&bond->arp_work);
4771
4772 if (bond->params.mode == BOND_MODE_ALB &&
4773 delayed_work_pending(&bond->alb_work))
4774 cancel_delayed_work(&bond->alb_work);
4775
4776 if (bond->params.mode == BOND_MODE_8023AD &&
4777 delayed_work_pending(&bond->ad_work))
4778 cancel_delayed_work(&bond->ad_work);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00004779
4780 if (delayed_work_pending(&bond->mcast_work))
4781 cancel_delayed_work(&bond->mcast_work);
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004782}
4783
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004784/*
4785* Destroy a bonding device.
4786* Must be under rtnl_lock when this function is called.
4787*/
4788static void bond_uninit(struct net_device *bond_dev)
Jay Vosburgha434e432008-10-30 17:41:15 -07004789{
Wang Chen454d7c92008-11-12 23:37:49 -08004790 struct bonding *bond = netdev_priv(bond_dev);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004791 struct vlan_entry *vlan, *tmp;
Jay Vosburgha434e432008-10-30 17:41:15 -07004792
WANG Congf6dc31a2010-05-06 00:48:51 -07004793 bond_netpoll_cleanup(bond_dev);
4794
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004795 /* Release the bonded slaves */
4796 bond_release_all(bond_dev);
4797
Jay Vosburgha434e432008-10-30 17:41:15 -07004798 list_del(&bond->bond_list);
4799
4800 bond_work_cancel_all(bond);
4801
Jay Vosburgha434e432008-10-30 17:41:15 -07004802 bond_remove_proc_entry(bond);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004803
Jiri Pirko22bedad32010-04-01 21:22:57 +00004804 __hw_addr_flush(&bond->mc_list);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004805
4806 list_for_each_entry_safe(vlan, tmp, &bond->vlan_list, vlan_list) {
4807 list_del(&vlan->vlan_list);
4808 kfree(vlan);
4809 }
Jay Vosburgha434e432008-10-30 17:41:15 -07004810}
4811
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812/*------------------------- Module initialization ---------------------------*/
4813
4814/*
4815 * Convert string input module parms. Accept either the
Jay Vosburghece95f72008-01-17 16:25:01 -08004816 * number of the mode or its string name. A bit complicated because
4817 * some mode names are substrings of other names, and calls from sysfs
4818 * may have whitespace in the name (trailing newlines, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 */
Holger Eitzenberger325dcf72008-12-09 23:10:17 -08004820int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821{
Hannes Eder54b87322009-02-14 11:15:49 +00004822 int modeint = -1, i, rv;
Jay Vosburgha42e5342008-01-29 18:07:43 -08004823 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
Jay Vosburghece95f72008-01-17 16:25:01 -08004824
Jay Vosburgha42e5342008-01-29 18:07:43 -08004825 for (p = (char *)buf; *p; p++)
4826 if (!(isdigit(*p) || isspace(*p)))
4827 break;
4828
4829 if (*p)
Jay Vosburghece95f72008-01-17 16:25:01 -08004830 rv = sscanf(buf, "%20s", modestr);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004831 else
Hannes Eder54b87322009-02-14 11:15:49 +00004832 rv = sscanf(buf, "%d", &modeint);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004833
4834 if (!rv)
4835 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
4837 for (i = 0; tbl[i].modename; i++) {
Hannes Eder54b87322009-02-14 11:15:49 +00004838 if (modeint == tbl[i].mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 return tbl[i].mode;
Jay Vosburghece95f72008-01-17 16:25:01 -08004840 if (strcmp(modestr, tbl[i].modename) == 0)
4841 return tbl[i].mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 }
4843
4844 return -1;
4845}
4846
4847static int bond_check_params(struct bond_params *params)
4848{
Jiri Pirkoa5499522009-09-25 03:28:09 +00004849 int arp_validate_value, fail_over_mac_value, primary_reselect_value;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004850
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 /*
4852 * Convert string parameters.
4853 */
4854 if (mode) {
4855 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4856 if (bond_mode == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004857 pr_err("Error: Invalid bonding mode \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 mode == NULL ? "NULL" : mode);
4859 return -EINVAL;
4860 }
4861 }
4862
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004863 if (xmit_hash_policy) {
4864 if ((bond_mode != BOND_MODE_XOR) &&
4865 (bond_mode != BOND_MODE_8023AD)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004866 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004867 bond_mode_name(bond_mode));
4868 } else {
4869 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4870 xmit_hashtype_tbl);
4871 if (xmit_hashtype == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004872 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004873 xmit_hash_policy == NULL ? "NULL" :
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004874 xmit_hash_policy);
4875 return -EINVAL;
4876 }
4877 }
4878 }
4879
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 if (lacp_rate) {
4881 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004882 pr_info("lacp_rate param is irrelevant in mode %s\n",
4883 bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 } else {
4885 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4886 if (lacp_fast == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004887 pr_err("Error: Invalid lacp rate \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 lacp_rate == NULL ? "NULL" : lacp_rate);
4889 return -EINVAL;
4890 }
4891 }
4892 }
4893
Jay Vosburghfd989c82008-11-04 17:51:16 -08004894 if (ad_select) {
4895 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4896 if (params->ad_select == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004897 pr_err("Error: Invalid ad_select \"%s\"\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -08004898 ad_select == NULL ? "NULL" : ad_select);
4899 return -EINVAL;
4900 }
4901
4902 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004903 pr_warning("ad_select param only affects 802.3ad mode\n");
Jay Vosburghfd989c82008-11-04 17:51:16 -08004904 }
4905 } else {
4906 params->ad_select = BOND_AD_STABLE;
4907 }
4908
Nicolas de Pesloüanf5841302009-08-28 13:18:34 +00004909 if (max_bonds < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004910 pr_warning("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4911 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 max_bonds = BOND_DEFAULT_MAX_BONDS;
4913 }
4914
4915 if (miimon < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004916 pr_warning("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to %d\n",
4917 miimon, INT_MAX, BOND_LINK_MON_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 miimon = BOND_LINK_MON_INTERV;
4919 }
4920
4921 if (updelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004922 pr_warning("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4923 updelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 updelay = 0;
4925 }
4926
4927 if (downdelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004928 pr_warning("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4929 downdelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 downdelay = 0;
4931 }
4932
4933 if ((use_carrier != 0) && (use_carrier != 1)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004934 pr_warning("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
4935 use_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 use_carrier = 1;
4937 }
4938
Moni Shoua7893b242008-05-17 21:10:12 -07004939 if (num_grat_arp < 0 || num_grat_arp > 255) {
Frans Pop2381a552010-03-24 07:57:36 +00004940 pr_warning("Warning: num_grat_arp (%d) not in range 0-255 so it was reset to 1\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004941 num_grat_arp);
Moni Shoua7893b242008-05-17 21:10:12 -07004942 num_grat_arp = 1;
4943 }
4944
Brian Haley305d5522008-11-04 17:51:14 -08004945 if (num_unsol_na < 0 || num_unsol_na > 255) {
Frans Pop2381a552010-03-24 07:57:36 +00004946 pr_warning("Warning: num_unsol_na (%d) not in range 0-255 so it was reset to 1\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004947 num_unsol_na);
Brian Haley305d5522008-11-04 17:51:14 -08004948 num_unsol_na = 1;
4949 }
4950
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 /* reset values for 802.3ad */
4952 if (bond_mode == BOND_MODE_8023AD) {
4953 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004954 pr_warning("Warning: miimon must be specified, otherwise bonding will not detect link failure, speed and duplex which are essential for 802.3ad operation\n");
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004955 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 miimon = 100;
4957 }
4958 }
4959
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004960 if (tx_queues < 1 || tx_queues > 255) {
4961 pr_warning("Warning: tx_queues (%d) should be between "
4962 "1 and 255, resetting to %d\n",
4963 tx_queues, BOND_DEFAULT_TX_QUEUES);
4964 tx_queues = BOND_DEFAULT_TX_QUEUES;
4965 }
4966
Andy Gospodarekebd8e492010-06-02 08:39:21 +00004967 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
4968 pr_warning("Warning: all_slaves_active module parameter (%d), "
4969 "not of valid value (0/1), so it was set to "
4970 "0\n", all_slaves_active);
4971 all_slaves_active = 0;
4972 }
4973
Flavio Leitnerc2952c32010-10-05 14:23:59 +00004974 if (resend_igmp < 0 || resend_igmp > 255) {
4975 pr_warning("Warning: resend_igmp (%d) should be between "
4976 "0 and 255, resetting to %d\n",
4977 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
4978 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
4979 }
4980
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 /* reset values for TLB/ALB */
4982 if ((bond_mode == BOND_MODE_TLB) ||
4983 (bond_mode == BOND_MODE_ALB)) {
4984 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004985 pr_warning("Warning: miimon must be specified, otherwise bonding will not detect link failure and link speed which are essential for TLB/ALB load balancing\n");
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004986 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 miimon = 100;
4988 }
4989 }
4990
4991 if (bond_mode == BOND_MODE_ALB) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004992 pr_notice("In ALB mode you might experience client disconnections upon reconnection of a link if the bonding module updelay parameter (%d msec) is incompatible with the forwarding delay time of the switch\n",
4993 updelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 }
4995
4996 if (!miimon) {
4997 if (updelay || downdelay) {
4998 /* just warn the user the up/down delay will have
4999 * no effect since miimon is zero...
5000 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005001 pr_warning("Warning: miimon module parameter not set and updelay (%d) or downdelay (%d) module parameter is set; updelay and downdelay have no effect unless miimon is set\n",
5002 updelay, downdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 }
5004 } else {
5005 /* don't allow arp monitoring */
5006 if (arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005007 pr_warning("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
5008 miimon, arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 arp_interval = 0;
5010 }
5011
5012 if ((updelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005013 pr_warning("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
5014 updelay, miimon,
5015 (updelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 }
5017
5018 updelay /= miimon;
5019
5020 if ((downdelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005021 pr_warning("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
5022 downdelay, miimon,
5023 (downdelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 }
5025
5026 downdelay /= miimon;
5027 }
5028
5029 if (arp_interval < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005030 pr_warning("Warning: arp_interval module parameter (%d) , not in range 0-%d, so it was reset to %d\n",
5031 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 arp_interval = BOND_LINK_ARP_INTERV;
5033 }
5034
5035 for (arp_ip_count = 0;
5036 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
5037 arp_ip_count++) {
5038 /* not complete check, but should be good enough to
5039 catch mistakes */
5040 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005041 pr_warning("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
5042 arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 arp_interval = 0;
5044 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04005045 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 arp_target[arp_ip_count] = ip;
5047 }
5048 }
5049
5050 if (arp_interval && !arp_ip_count) {
5051 /* don't allow arping if no arp_ip_target given... */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005052 pr_warning("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
5053 arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 arp_interval = 0;
5055 }
5056
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005057 if (arp_validate) {
5058 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005059 pr_err("arp_validate only supported in active-backup mode\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005060 return -EINVAL;
5061 }
5062 if (!arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005063 pr_err("arp_validate requires arp_interval\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005064 return -EINVAL;
5065 }
5066
5067 arp_validate_value = bond_parse_parm(arp_validate,
5068 arp_validate_tbl);
5069 if (arp_validate_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005070 pr_err("Error: invalid arp_validate \"%s\"\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005071 arp_validate == NULL ? "NULL" : arp_validate);
5072 return -EINVAL;
5073 }
5074 } else
5075 arp_validate_value = 0;
5076
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 if (miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005078 pr_info("MII link monitoring set to %d ms\n", miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 } else if (arp_interval) {
5080 int i;
5081
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005082 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
5083 arp_interval,
5084 arp_validate_tbl[arp_validate_value].modename,
5085 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086
5087 for (i = 0; i < arp_ip_count; i++)
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00005088 pr_info(" %s", arp_ip_target[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00005090 pr_info("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091
Jay Vosburghb8a97872008-06-13 18:12:04 -07005092 } else if (max_bonds) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 /* miimon and arp_interval not set, we need one so things
5094 * work as expected, see bonding.txt for details
5095 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005096 pr_warning("Warning: either miimon or arp_interval and arp_ip_target module parameters must be specified, otherwise bonding will not detect link failures! see bonding.txt for details.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 }
5098
5099 if (primary && !USES_PRIMARY(bond_mode)) {
5100 /* currently, using a primary only makes sense
5101 * in active backup, TLB or ALB modes
5102 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005103 pr_warning("Warning: %s primary device specified but has no effect in %s mode\n",
5104 primary, bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 primary = NULL;
5106 }
5107
Jiri Pirkoa5499522009-09-25 03:28:09 +00005108 if (primary && primary_reselect) {
5109 primary_reselect_value = bond_parse_parm(primary_reselect,
5110 pri_reselect_tbl);
5111 if (primary_reselect_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005112 pr_err("Error: Invalid primary_reselect \"%s\"\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00005113 primary_reselect ==
5114 NULL ? "NULL" : primary_reselect);
5115 return -EINVAL;
5116 }
5117 } else {
5118 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
5119 }
5120
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005121 if (fail_over_mac) {
5122 fail_over_mac_value = bond_parse_parm(fail_over_mac,
5123 fail_over_mac_tbl);
5124 if (fail_over_mac_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005125 pr_err("Error: invalid fail_over_mac \"%s\"\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005126 arp_validate == NULL ? "NULL" : arp_validate);
5127 return -EINVAL;
5128 }
5129
5130 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005131 pr_warning("Warning: fail_over_mac only affects active-backup mode.\n");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005132 } else {
5133 fail_over_mac_value = BOND_FOM_NONE;
5134 }
Jay Vosburghdd957c52007-10-09 19:57:24 -07005135
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 /* fill params struct with the proper values */
5137 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04005138 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 params->miimon = miimon;
Moni Shoua7893b242008-05-17 21:10:12 -07005140 params->num_grat_arp = num_grat_arp;
Brian Haley305d5522008-11-04 17:51:14 -08005141 params->num_unsol_na = num_unsol_na;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005143 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 params->updelay = updelay;
5145 params->downdelay = downdelay;
5146 params->use_carrier = use_carrier;
5147 params->lacp_fast = lacp_fast;
5148 params->primary[0] = 0;
Jiri Pirkoa5499522009-09-25 03:28:09 +00005149 params->primary_reselect = primary_reselect_value;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005150 params->fail_over_mac = fail_over_mac_value;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005151 params->tx_queues = tx_queues;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00005152 params->all_slaves_active = all_slaves_active;
Flavio Leitnerc2952c32010-10-05 14:23:59 +00005153 params->resend_igmp = resend_igmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154
5155 if (primary) {
5156 strncpy(params->primary, primary, IFNAMSIZ);
5157 params->primary[IFNAMSIZ - 1] = 0;
5158 }
5159
5160 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
5161
5162 return 0;
5163}
5164
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005165static struct lock_class_key bonding_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -07005166static struct lock_class_key bonding_netdev_addr_lock_key;
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005167
David S. Millere8a04642008-07-17 00:34:19 -07005168static void bond_set_lockdep_class_one(struct net_device *dev,
5169 struct netdev_queue *txq,
5170 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -07005171{
5172 lockdep_set_class(&txq->_xmit_lock,
5173 &bonding_netdev_xmit_lock_key);
5174}
5175
5176static void bond_set_lockdep_class(struct net_device *dev)
5177{
David S. Millercf508b12008-07-22 14:16:42 -07005178 lockdep_set_class(&dev->addr_list_lock,
5179 &bonding_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -07005180 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -07005181}
5182
Stephen Hemminger181470f2009-06-12 19:02:52 +00005183/*
5184 * Called from registration process
5185 */
5186static int bond_init(struct net_device *bond_dev)
5187{
5188 struct bonding *bond = netdev_priv(bond_dev);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005189 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005190
5191 pr_debug("Begin bond_init for %s\n", bond_dev->name);
5192
5193 bond->wq = create_singlethread_workqueue(bond_dev->name);
5194 if (!bond->wq)
5195 return -ENOMEM;
5196
5197 bond_set_lockdep_class(bond_dev);
5198
5199 netif_carrier_off(bond_dev);
5200
5201 bond_create_proc_entry(bond);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005202 list_add_tail(&bond->bond_list, &bn->dev_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005203
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00005204 bond_prepare_sysfs_group(bond);
Jiri Pirko22bedad32010-04-01 21:22:57 +00005205
5206 __hw_addr_init(&bond->mc_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005207 return 0;
5208}
5209
Eric W. Biederman88ead972009-10-29 14:18:25 +00005210static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
5211{
5212 if (tb[IFLA_ADDRESS]) {
5213 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
5214 return -EINVAL;
5215 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
5216 return -EADDRNOTAVAIL;
5217 }
5218 return 0;
5219}
5220
5221static struct rtnl_link_ops bond_link_ops __read_mostly = {
5222 .kind = "bond",
Eric W. Biederman66391042009-10-29 23:58:54 +00005223 .priv_size = sizeof(struct bonding),
Eric W. Biederman88ead972009-10-29 14:18:25 +00005224 .setup = bond_setup,
5225 .validate = bond_validate,
5226};
5227
Mitch Williamsdfe60392005-11-09 10:36:04 -08005228/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005229 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08005230 * Caller must NOT hold rtnl_lock; we need to release it here before we
5231 * set up our sysfs entries.
5232 */
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005233int bond_create(struct net *net, const char *name)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005234{
5235 struct net_device *bond_dev;
5236 int res;
5237
5238 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08005239
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005240 bond_dev = alloc_netdev_mq(sizeof(struct bonding), name ? name : "",
5241 bond_setup, tx_queues);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005242 if (!bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005243 pr_err("%s: eek! can't alloc netdev!\n", name);
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005244 rtnl_unlock();
5245 return -ENOMEM;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005246 }
5247
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005248 dev_net_set(bond_dev, net);
Eric W. Biederman88ead972009-10-29 14:18:25 +00005249 bond_dev->rtnl_link_ops = &bond_link_ops;
5250
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005251 if (!name) {
5252 res = dev_alloc_name(bond_dev, "bond%d");
5253 if (res < 0)
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005254 goto out;
Neil Horman27e6f062010-10-05 03:39:21 +00005255 } else {
5256 /*
5257 * If we're given a name to register
5258 * we need to ensure that its not already
5259 * registered
5260 */
5261 res = -EEXIST;
5262 if (__dev_get_by_name(net, name) != NULL)
5263 goto out;
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005264 }
5265
Mitch Williamsdfe60392005-11-09 10:36:04 -08005266 res = register_netdevice(bond_dev);
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005267
Eric W. Biederman30c15ba2009-10-29 14:18:23 +00005268out:
Mitch Williamsdfe60392005-11-09 10:36:04 -08005269 rtnl_unlock();
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005270 if (res < 0)
5271 bond_destructor(bond_dev);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005272 return res;
5273}
5274
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005275static int __net_init bond_net_init(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005276{
Eric W. Biederman15449742009-11-29 15:46:04 +00005277 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005278
5279 bn->net = net;
5280 INIT_LIST_HEAD(&bn->dev_list);
5281
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005282 bond_create_proc_dir(bn);
Eric W. Biederman15449742009-11-29 15:46:04 +00005283
5284 return 0;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005285}
5286
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005287static void __net_exit bond_net_exit(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005288{
Eric W. Biederman15449742009-11-29 15:46:04 +00005289 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005290
5291 bond_destroy_proc_dir(bn);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005292}
5293
5294static struct pernet_operations bond_net_ops = {
5295 .init = bond_net_init,
5296 .exit = bond_net_exit,
Eric W. Biederman15449742009-11-29 15:46:04 +00005297 .id = &bond_net_id,
5298 .size = sizeof(struct bond_net),
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005299};
5300
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301static int __init bonding_init(void)
5302{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 int i;
5304 int res;
5305
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005306 pr_info("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307
Mitch Williamsdfe60392005-11-09 10:36:04 -08005308 res = bond_check_params(&bonding_defaults);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005309 if (res)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005310 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311
Neil Hormane843fa52010-10-13 16:01:50 +00005312#ifdef CONFIG_NET_POLL_CONTROLLER
5313 if (!alloc_cpumask_var(&netpoll_block_tx, GFP_KERNEL)) {
5314 res = -ENOMEM;
5315 goto out;
5316 }
5317#endif
5318
Eric W. Biederman15449742009-11-29 15:46:04 +00005319 res = register_pernet_subsys(&bond_net_ops);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005320 if (res)
5321 goto out;
Jay Vosburgh027ea042008-01-17 16:25:02 -08005322
Eric W. Biederman88ead972009-10-29 14:18:25 +00005323 res = rtnl_link_register(&bond_link_ops);
5324 if (res)
Eric W. Biederman66391042009-10-29 23:58:54 +00005325 goto err_link;
Eric W. Biederman88ead972009-10-29 14:18:25 +00005326
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 for (i = 0; i < max_bonds; i++) {
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005328 res = bond_create(&init_net, NULL);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005329 if (res)
5330 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 }
5332
Mitch Williamsb76cdba2005-11-09 10:36:41 -08005333 res = bond_create_sysfs();
5334 if (res)
5335 goto err;
5336
Neil Hormane843fa52010-10-13 16:01:50 +00005337
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005339 register_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005340 bond_register_ipv6_notifier();
Mitch Williamsdfe60392005-11-09 10:36:04 -08005341out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 return res;
Eric W. Biederman88ead972009-10-29 14:18:25 +00005343err:
5344 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman66391042009-10-29 23:58:54 +00005345err_link:
Eric W. Biederman15449742009-11-29 15:46:04 +00005346 unregister_pernet_subsys(&bond_net_ops);
Neil Hormane843fa52010-10-13 16:01:50 +00005347#ifdef CONFIG_NET_POLL_CONTROLLER
5348 free_cpumask_var(netpoll_block_tx);
5349#endif
Eric W. Biederman88ead972009-10-29 14:18:25 +00005350 goto out;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005351
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352}
5353
5354static void __exit bonding_exit(void)
5355{
5356 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005357 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005358 bond_unregister_ipv6_notifier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005360 bond_destroy_sysfs();
5361
Eric W. Biederman88ead972009-10-29 14:18:25 +00005362 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman15449742009-11-29 15:46:04 +00005363 unregister_pernet_subsys(&bond_net_ops);
Neil Hormane843fa52010-10-13 16:01:50 +00005364
5365#ifdef CONFIG_NET_POLL_CONTROLLER
5366 free_cpumask_var(netpoll_block_tx);
5367#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368}
5369
5370module_init(bonding_init);
5371module_exit(bonding_exit);
5372MODULE_LICENSE("GPL");
5373MODULE_VERSION(DRV_VERSION);
5374MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5375MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
Eric W. Biederman88ead972009-10-29 14:18:25 +00005376MODULE_ALIAS_RTNL_LINK("bond");