blob: 584f97b7306015c2993e2126bc81c5e775b1fa1b [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>
62#include <linux/inetdevice.h>
Jay Vosburgha816c7c2007-02-28 17:03:37 -080063#include <linux/igmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/etherdevice.h>
65#include <linux/skbuff.h>
66#include <net/sock.h>
67#include <linux/rtnetlink.h>
68#include <linux/proc_fs.h>
69#include <linux/seq_file.h>
70#include <linux/smp.h>
71#include <linux/if_ether.h>
72#include <net/arp.h>
73#include <linux/mii.h>
74#include <linux/ethtool.h>
75#include <linux/if_vlan.h>
76#include <linux/if_bonding.h>
David Sterbab63bb732007-12-06 23:40:33 -080077#include <linux/jiffies.h>
Neil Hormane843fa52010-10-13 16:01:50 +000078#include <linux/preempt.h>
Jay Vosburghc3ade5c2005-06-26 17:52:20 -040079#include <net/route.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020080#include <net/net_namespace.h>
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000081#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include "bonding.h"
83#include "bond_3ad.h"
84#include "bond_alb.h"
85
86/*---------------------------- Module parameters ----------------------------*/
87
88/* monitor all links that often (in milliseconds). <=0 disables monitoring */
89#define BOND_LINK_MON_INTERV 0
90#define BOND_LINK_ARP_INTERV 0
91
92static int max_bonds = BOND_DEFAULT_MAX_BONDS;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +000093static int tx_queues = BOND_DEFAULT_TX_QUEUES;
Moni Shoua7893b242008-05-17 21:10:12 -070094static int num_grat_arp = 1;
Brian Haley305d5522008-11-04 17:51:14 -080095static int num_unsol_na = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static int miimon = BOND_LINK_MON_INTERV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +000097static int updelay;
98static int downdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static int use_carrier = 1;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000100static char *mode;
101static char *primary;
Jiri Pirkoa5499522009-09-25 03:28:09 +0000102static char *primary_reselect;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000103static char *lacp_rate;
104static char *ad_select;
105static char *xmit_hash_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static int arp_interval = BOND_LINK_ARP_INTERV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000107static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
108static char *arp_validate;
109static char *fail_over_mac;
Andy Gospodarekebd8e492010-06-02 08:39:21 +0000110static int all_slaves_active = 0;
Stephen Hemmingerd2991f72009-06-12 19:02:44 +0000111static struct bond_params bonding_defaults;
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000112static int resend_igmp = BOND_DEFAULT_RESEND_IGMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114module_param(max_bonds, int, 0);
115MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
Andy Gospodarekbb1d9122010-06-02 08:40:18 +0000116module_param(tx_queues, int, 0);
117MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)");
Moni Shoua7893b242008-05-17 21:10:12 -0700118module_param(num_grat_arp, int, 0644);
119MODULE_PARM_DESC(num_grat_arp, "Number of gratuitous ARP packets to send on failover event");
Brian Haley305d5522008-11-04 17:51:14 -0800120module_param(num_unsol_na, int, 0644);
121MODULE_PARM_DESC(num_unsol_na, "Number of unsolicited IPv6 Neighbor Advertisements packets to send on failover event");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122module_param(miimon, int, 0);
123MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
124module_param(updelay, int, 0);
125MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
126module_param(downdelay, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800127MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
128 "in milliseconds");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129module_param(use_carrier, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800130MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
131 "0 for off, 1 for on (default)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132module_param(mode, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800133MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
134 "1 for active-backup, 2 for balance-xor, "
135 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
136 "6 for balance-alb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137module_param(primary, charp, 0);
138MODULE_PARM_DESC(primary, "Primary network device to use");
Jiri Pirkoa5499522009-09-25 03:28:09 +0000139module_param(primary_reselect, charp, 0);
140MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
141 "once it comes up; "
142 "0 for always (default), "
143 "1 for only if speed of primary is "
144 "better, "
145 "2 for only on active slave "
146 "failure");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147module_param(lacp_rate, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800148MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
149 "(slow/fast)");
Jay Vosburghfd989c82008-11-04 17:51:16 -0800150module_param(ad_select, charp, 0);
151MODULE_PARM_DESC(ad_select, "803.ad aggregation selection logic: stable (0, default), bandwidth (1), count (2)");
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400152module_param(xmit_hash_policy, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800153MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
154 ", 1 for layer 3+4");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155module_param(arp_interval, int, 0);
156MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
157module_param_array(arp_ip_target, charp, NULL, 0);
158MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700159module_param(arp_validate, charp, 0);
160MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700161module_param(fail_over_mac, charp, 0);
162MODULE_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 +0000163module_param(all_slaves_active, int, 0);
164MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface"
165 "by setting active flag for all slaves. "
166 "0 for never (default), 1 for always.");
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000167module_param(resend_igmp, int, 0);
168MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on link failure");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170/*----------------------------- Global variables ----------------------------*/
171
Neil Hormane843fa52010-10-13 16:01:50 +0000172#ifdef CONFIG_NET_POLL_CONTROLLER
Neil Hormanfb4fa762010-12-06 09:05:50 +0000173atomic_t netpoll_block_tx = ATOMIC_INIT(0);
Neil Hormane843fa52010-10-13 16:01:50 +0000174#endif
175
Arjan van de Venf71e1302006-03-03 21:33:57 -0500176static const char * const version =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
178
Eric Dumazetf99189b2009-11-17 10:42:49 +0000179int bond_net_id __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000181static __be32 arp_target[BOND_MAX_ARP_TARGETS];
182static int arp_ip_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183static int bond_mode = BOND_MODE_ROUNDROBIN;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000184static int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
185static int lacp_fast;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800187const struct bond_parm_tbl bond_lacp_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{ "slow", AD_LACP_SLOW},
189{ "fast", AD_LACP_FAST},
190{ NULL, -1},
191};
192
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800193const struct bond_parm_tbl bond_mode_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{ "balance-rr", BOND_MODE_ROUNDROBIN},
195{ "active-backup", BOND_MODE_ACTIVEBACKUP},
196{ "balance-xor", BOND_MODE_XOR},
197{ "broadcast", BOND_MODE_BROADCAST},
198{ "802.3ad", BOND_MODE_8023AD},
199{ "balance-tlb", BOND_MODE_TLB},
200{ "balance-alb", BOND_MODE_ALB},
201{ NULL, -1},
202};
203
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800204const struct bond_parm_tbl xmit_hashtype_tbl[] = {
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400205{ "layer2", BOND_XMIT_POLICY_LAYER2},
206{ "layer3+4", BOND_XMIT_POLICY_LAYER34},
Jay Vosburgh6f6652b2007-12-06 23:40:34 -0800207{ "layer2+3", BOND_XMIT_POLICY_LAYER23},
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400208{ NULL, -1},
209};
210
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800211const struct bond_parm_tbl arp_validate_tbl[] = {
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700212{ "none", BOND_ARP_VALIDATE_NONE},
213{ "active", BOND_ARP_VALIDATE_ACTIVE},
214{ "backup", BOND_ARP_VALIDATE_BACKUP},
215{ "all", BOND_ARP_VALIDATE_ALL},
216{ NULL, -1},
217};
218
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800219const struct bond_parm_tbl fail_over_mac_tbl[] = {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700220{ "none", BOND_FOM_NONE},
221{ "active", BOND_FOM_ACTIVE},
222{ "follow", BOND_FOM_FOLLOW},
223{ NULL, -1},
224};
225
Jiri Pirkoa5499522009-09-25 03:28:09 +0000226const struct bond_parm_tbl pri_reselect_tbl[] = {
227{ "always", BOND_PRI_RESELECT_ALWAYS},
228{ "better", BOND_PRI_RESELECT_BETTER},
229{ "failure", BOND_PRI_RESELECT_FAILURE},
230{ NULL, -1},
231};
232
Jay Vosburghfd989c82008-11-04 17:51:16 -0800233struct bond_parm_tbl ad_select_tbl[] = {
234{ "stable", BOND_AD_STABLE},
235{ "bandwidth", BOND_AD_BANDWIDTH},
236{ "count", BOND_AD_COUNT},
237{ NULL, -1},
238};
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*-------------------------- Forward declarations ---------------------------*/
241
Jay Vosburghc3ade5c2005-06-26 17:52:20 -0400242static void bond_send_gratuitous_arp(struct bonding *bond);
Stephen Hemminger181470f2009-06-12 19:02:52 +0000243static int bond_init(struct net_device *bond_dev);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +0000244static void bond_uninit(struct net_device *bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246/*---------------------------- General routines -----------------------------*/
247
Adrian Bunk4ad072c2007-07-09 11:51:12 -0700248static const char *bond_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800250 static const char *names[] = {
251 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
252 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
253 [BOND_MODE_XOR] = "load balancing (xor)",
254 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000255 [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800256 [BOND_MODE_TLB] = "transmit load balancing",
257 [BOND_MODE_ALB] = "adaptive load balancing",
258 };
259
260 if (mode < 0 || mode > BOND_MODE_ALB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return "unknown";
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800262
263 return names[mode];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/*---------------------------------- VLAN -----------------------------------*/
267
268/**
269 * bond_add_vlan - add a new vlan id on bond
270 * @bond: bond that got the notification
271 * @vlan_id: the vlan id to add
272 *
273 * Returns -ENOMEM if allocation failed.
274 */
275static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
276{
277 struct vlan_entry *vlan;
278
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800279 pr_debug("bond: %s, vlan id %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800280 (bond ? bond->dev->name : "None"), vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Brian Haley305d5522008-11-04 17:51:14 -0800282 vlan = kzalloc(sizeof(struct vlan_entry), GFP_KERNEL);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000283 if (!vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 INIT_LIST_HEAD(&vlan->vlan_list);
287 vlan->vlan_id = vlan_id;
288
289 write_lock_bh(&bond->lock);
290
291 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
292
293 write_unlock_bh(&bond->lock);
294
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800295 pr_debug("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 return 0;
298}
299
300/**
301 * bond_del_vlan - delete a vlan id from bond
302 * @bond: bond that got the notification
303 * @vlan_id: the vlan id to delete
304 *
305 * returns -ENODEV if @vlan_id was not found in @bond.
306 */
307static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
308{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700309 struct vlan_entry *vlan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 int res = -ENODEV;
311
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800312 pr_debug("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Neil Hormane843fa52010-10-13 16:01:50 +0000314 block_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 write_lock_bh(&bond->lock);
316
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700317 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (vlan->vlan_id == vlan_id) {
319 list_del(&vlan->vlan_list);
320
Holger Eitzenberger58402052008-12-09 23:07:13 -0800321 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 bond_alb_clear_vlan(bond, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800324 pr_debug("removed VLAN ID %d from bond %s\n",
325 vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 kfree(vlan);
328
329 if (list_empty(&bond->vlan_list) &&
330 (bond->slave_cnt == 0)) {
331 /* Last VLAN removed and no slaves, so
332 * restore block on adding VLANs. This will
333 * be removed once new slaves that are not
334 * VLAN challenged will be added.
335 */
336 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
337 }
338
339 res = 0;
340 goto out;
341 }
342 }
343
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800344 pr_debug("couldn't find VLAN ID %d in bond %s\n",
345 vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347out:
348 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +0000349 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return res;
351}
352
353/**
354 * bond_has_challenged_slaves
355 * @bond: the bond we're working on
356 *
357 * Searches the slave list. Returns 1 if a vlan challenged slave
358 * was found, 0 otherwise.
359 *
360 * Assumes bond->lock is held.
361 */
362static int bond_has_challenged_slaves(struct bonding *bond)
363{
364 struct slave *slave;
365 int i;
366
367 bond_for_each_slave(bond, slave, i) {
368 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800369 pr_debug("found VLAN challenged slave - %s\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800370 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return 1;
372 }
373 }
374
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800375 pr_debug("no VLAN challenged slaves found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return 0;
377}
378
379/**
380 * bond_next_vlan - safely skip to the next item in the vlans list.
381 * @bond: the bond we're working on
382 * @curr: item we're advancing from
383 *
384 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
385 * or @curr->next otherwise (even if it is @curr itself again).
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000386 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 * Caller must hold bond->lock
388 */
389struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
390{
391 struct vlan_entry *next, *last;
392
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000393 if (list_empty(&bond->vlan_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 if (!curr) {
397 next = list_entry(bond->vlan_list.next,
398 struct vlan_entry, vlan_list);
399 } else {
400 last = list_entry(bond->vlan_list.prev,
401 struct vlan_entry, vlan_list);
402 if (last == curr) {
403 next = list_entry(bond->vlan_list.next,
404 struct vlan_entry, vlan_list);
405 } else {
406 next = list_entry(curr->vlan_list.next,
407 struct vlan_entry, vlan_list);
408 }
409 }
410
411 return next;
412}
413
414/**
415 * bond_dev_queue_xmit - Prepare skb for xmit.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000416 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 * @bond: bond device that got this skb for tx.
418 * @skb: hw accel VLAN tagged skb to transmit
419 * @slave_dev: slave that is supposed to xmit this skbuff
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000421int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
422 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Ben Hutchings83874512010-12-13 08:19:28 +0000424 skb->dev = slave_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 skb->priority = 1;
Amerigo Wang080e4132011-02-17 23:43:33 +0000426 if (unlikely(netpoll_tx_running(slave_dev)))
Amerigo Wang8a8efa22011-02-17 23:43:32 +0000427 bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
Amerigo Wang080e4132011-02-17 23:43:33 +0000428 else
WANG Congf6dc31a2010-05-06 00:48:51 -0700429 dev_queue_xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 return 0;
432}
433
434/*
435 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
436 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
437 * lock because:
438 * a. This operation is performed in IOCTL context,
439 * b. The operation is protected by the RTNL semaphore in the 8021q code,
440 * c. Holding a lock with BH disabled while directly calling a base driver
441 * entry point is generally a BAD idea.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000442 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 * The design of synchronization/protection for this operation in the 8021q
444 * module is good for one or more VLAN devices over a single physical device
445 * and cannot be extended for a teaming solution like bonding, so there is a
446 * potential race condition here where a net device from the vlan group might
447 * be referenced (either by a base driver or the 8021q code) while it is being
448 * removed from the system. However, it turns out we're not making matters
449 * worse, and if it works for regular VLAN usage it will work here too.
450*/
451
452/**
453 * bond_vlan_rx_register - Propagates registration to slaves
454 * @bond_dev: bonding net device that got called
455 * @grp: vlan group being registered
456 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000457static void bond_vlan_rx_register(struct net_device *bond_dev,
458 struct vlan_group *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Wang Chen454d7c92008-11-12 23:37:49 -0800460 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct slave *slave;
462 int i;
463
Jarek Poplawskia71fb882010-10-27 07:08:22 +0000464 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 bond->vlgrp = grp;
Jarek Poplawskia71fb882010-10-27 07:08:22 +0000466 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 bond_for_each_slave(bond, slave, i) {
469 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800470 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800473 slave_ops->ndo_vlan_rx_register) {
474 slave_ops->ndo_vlan_rx_register(slave_dev, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476 }
477}
478
479/**
480 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
481 * @bond_dev: bonding net device that got called
482 * @vid: vlan id being added
483 */
484static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
485{
Wang Chen454d7c92008-11-12 23:37:49 -0800486 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 struct slave *slave;
488 int i, res;
489
490 bond_for_each_slave(bond, slave, i) {
491 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800492 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800495 slave_ops->ndo_vlan_rx_add_vid) {
496 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498 }
499
500 res = bond_add_vlan(bond, vid);
501 if (res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800502 pr_err("%s: Error: Failed to add vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 bond_dev->name, vid);
504 }
505}
506
507/**
508 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
509 * @bond_dev: bonding net device that got called
510 * @vid: vlan id being removed
511 */
512static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
513{
Wang Chen454d7c92008-11-12 23:37:49 -0800514 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 struct slave *slave;
516 struct net_device *vlan_dev;
517 int i, res;
518
519 bond_for_each_slave(bond, slave, i) {
520 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800521 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800524 slave_ops->ndo_vlan_rx_kill_vid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* Save and then restore vlan_dev in the grp array,
526 * since the slave's driver might clear it.
527 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800528 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800529 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vid);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800530 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 }
533
534 res = bond_del_vlan(bond, vid);
535 if (res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800536 pr_err("%s: Error: Failed to remove vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 bond_dev->name, vid);
538 }
539}
540
541static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
542{
543 struct vlan_entry *vlan;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800544 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Jay Vosburghf35188f2010-07-21 12:14:47 +0000546 if (!bond->vlgrp)
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000547 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800550 slave_ops->ndo_vlan_rx_register)
551 slave_ops->ndo_vlan_rx_register(slave_dev, bond->vlgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800554 !(slave_ops->ndo_vlan_rx_add_vid))
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000555 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800557 list_for_each_entry(vlan, &bond->vlan_list, vlan_list)
558 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vlan->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000561static void bond_del_vlans_from_slave(struct bonding *bond,
562 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800564 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct vlan_entry *vlan;
566 struct net_device *vlan_dev;
567
Jay Vosburghf35188f2010-07-21 12:14:47 +0000568 if (!bond->vlgrp)
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000569 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800572 !(slave_ops->ndo_vlan_rx_kill_vid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 goto unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +0000576 if (!vlan->vlan_id)
577 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /* Save and then restore vlan_dev in the grp array,
579 * since the slave's driver might clear it.
580 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800581 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800582 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800583 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
586unreg:
587 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800588 slave_ops->ndo_vlan_rx_register)
589 slave_ops->ndo_vlan_rx_register(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592/*------------------------------- Link status -------------------------------*/
593
594/*
Jay Vosburghff59c452006-03-27 13:27:43 -0800595 * Set the carrier state for the master according to the state of its
596 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
597 * do special 802.3ad magic.
598 *
599 * Returns zero if carrier state does not change, nonzero if it does.
600 */
601static int bond_set_carrier(struct bonding *bond)
602{
603 struct slave *slave;
604 int i;
605
606 if (bond->slave_cnt == 0)
607 goto down;
608
609 if (bond->params.mode == BOND_MODE_8023AD)
610 return bond_3ad_set_carrier(bond);
611
612 bond_for_each_slave(bond, slave, i) {
613 if (slave->link == BOND_LINK_UP) {
614 if (!netif_carrier_ok(bond->dev)) {
615 netif_carrier_on(bond->dev);
616 return 1;
617 }
618 return 0;
619 }
620 }
621
622down:
623 if (netif_carrier_ok(bond->dev)) {
624 netif_carrier_off(bond->dev);
625 return 1;
626 }
627 return 0;
628}
629
630/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * Get link speed and duplex from the slave's base driver
632 * using ethtool. If for some reason the call fails or the
633 * values are invalid, fake speed and duplex to 100/Full
634 * and return error.
635 */
636static int bond_update_speed_duplex(struct slave *slave)
637{
638 struct net_device *slave_dev = slave->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 struct ethtool_cmd etool;
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700640 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 /* Fake speed and duplex */
643 slave->speed = SPEED_100;
644 slave->duplex = DUPLEX_FULL;
645
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700646 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700649 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
650 if (res < 0)
651 return -1;
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 switch (etool.speed) {
654 case SPEED_10:
655 case SPEED_100:
656 case SPEED_1000:
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700657 case SPEED_10000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 break;
659 default:
660 return -1;
661 }
662
663 switch (etool.duplex) {
664 case DUPLEX_FULL:
665 case DUPLEX_HALF:
666 break;
667 default:
668 return -1;
669 }
670
671 slave->speed = etool.speed;
672 slave->duplex = etool.duplex;
673
674 return 0;
675}
676
677/*
678 * if <dev> supports MII link status reporting, check its link status.
679 *
680 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000681 * depending upon the setting of the use_carrier parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 *
683 * Return either BMSR_LSTATUS, meaning that the link is up (or we
684 * can't tell and just pretend it is), or 0, meaning that the link is
685 * down.
686 *
687 * If reporting is non-zero, instead of faking link up, return -1 if
688 * both ETHTOOL and MII ioctls fail (meaning the device does not
689 * support them). If use_carrier is set, return whatever it says.
690 * It'd be nice if there was a good way to tell if a driver supports
691 * netif_carrier, but there really isn't.
692 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000693static int bond_check_dev_link(struct bonding *bond,
694 struct net_device *slave_dev, int reporting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800696 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Jiri Bohacd9d52832009-10-28 22:23:54 -0700697 int (*ioctl)(struct net_device *, struct ifreq *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct ifreq ifr;
699 struct mii_ioctl_data *mii;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Petri Gynther6c988852009-08-28 12:05:15 +0000701 if (!reporting && !netif_running(slave_dev))
702 return 0;
703
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800704 if (bond->params.use_carrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Jiri Pirko29112f42009-04-24 01:58:23 +0000707 /* Try to get link status using Ethtool first. */
708 if (slave_dev->ethtool_ops) {
709 if (slave_dev->ethtool_ops->get_link) {
710 u32 link;
711
712 link = slave_dev->ethtool_ops->get_link(slave_dev);
713
714 return link ? BMSR_LSTATUS : 0;
715 }
716 }
717
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000718 /* Ethtool can't be used, fallback to MII ioctls. */
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800719 ioctl = slave_ops->ndo_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (ioctl) {
721 /* TODO: set pointer to correct ioctl on a per team member */
722 /* bases to make this more efficient. that is, once */
723 /* we determine the correct ioctl, we will always */
724 /* call it and not the others for that team */
725 /* member. */
726
727 /*
728 * We cannot assume that SIOCGMIIPHY will also read a
729 * register; not all network drivers (e.g., e100)
730 * support that.
731 */
732
733 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
734 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
735 mii = if_mii(&ifr);
736 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
737 mii->reg_num = MII_BMSR;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000738 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0)
739 return mii->val_out & BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741 }
742
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700743 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 * If reporting, report that either there's no dev->do_ioctl,
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700745 * or both SIOCGMIIREG and get_link failed (meaning that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 * cannot report link status). If not reporting, pretend
747 * we're ok.
748 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000749 return reporting ? -1 : BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
752/*----------------------------- Multicast list ------------------------------*/
753
754/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 * Push the promiscuity flag down to appropriate slaves
756 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700757static int bond_set_promiscuity(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700759 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (USES_PRIMARY(bond->params.mode)) {
761 /* write lock already acquired */
762 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700763 err = dev_set_promiscuity(bond->curr_active_slave->dev,
764 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766 } else {
767 struct slave *slave;
768 int i;
769 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700770 err = dev_set_promiscuity(slave->dev, inc);
771 if (err)
772 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700775 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
778/*
779 * Push the allmulti flag down to all slaves
780 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700781static int bond_set_allmulti(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700783 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (USES_PRIMARY(bond->params.mode)) {
785 /* write lock already acquired */
786 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700787 err = dev_set_allmulti(bond->curr_active_slave->dev,
788 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790 } else {
791 struct slave *slave;
792 int i;
793 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700794 err = dev_set_allmulti(slave->dev, inc);
795 if (err)
796 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700799 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
802/*
803 * Add a Multicast address to slaves
804 * according to mode
805 */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000806static void bond_mc_add(struct bonding *bond, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 if (USES_PRIMARY(bond->params.mode)) {
809 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000810 if (bond->curr_active_slave)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000811 dev_mc_add(bond->curr_active_slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 } else {
813 struct slave *slave;
814 int i;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000815
816 bond_for_each_slave(bond, slave, i)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000817 dev_mc_add(slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819}
820
821/*
822 * Remove a multicast address from slave
823 * according to mode
824 */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000825static void bond_mc_del(struct bonding *bond, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 if (USES_PRIMARY(bond->params.mode)) {
828 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000829 if (bond->curr_active_slave)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000830 dev_mc_del(bond->curr_active_slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 } else {
832 struct slave *slave;
833 int i;
834 bond_for_each_slave(bond, slave, i) {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000835 dev_mc_del(slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837 }
838}
839
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800840
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000841static void __bond_resend_igmp_join_requests(struct net_device *dev)
842{
843 struct in_device *in_dev;
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000844
845 rcu_read_lock();
846 in_dev = __in_dev_get_rcu(dev);
Eric Dumazet866f3b22010-11-18 09:33:19 -0800847 if (in_dev)
David S. Miller24912422010-11-19 13:13:47 -0800848 ip_mc_rejoin_groups(in_dev);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000849 rcu_read_unlock();
850}
851
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800852/*
853 * Retrieve the list of registered multicast addresses for the bonding
854 * device and retransmit an IGMP JOIN request to the current active
855 * slave.
856 */
857static void bond_resend_igmp_join_requests(struct bonding *bond)
858{
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000859 struct net_device *vlan_dev;
860 struct vlan_entry *vlan;
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800861
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000862 read_lock(&bond->lock);
863
864 /* rejoin all groups on bond device */
865 __bond_resend_igmp_join_requests(bond->dev);
866
867 /* rejoin all groups on vlan devices */
868 if (bond->vlgrp) {
869 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
870 vlan_dev = vlan_group_get_device(bond->vlgrp,
871 vlan->vlan_id);
872 if (vlan_dev)
873 __bond_resend_igmp_join_requests(vlan_dev);
874 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800875 }
876
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000877 if (--bond->igmp_retrans > 0)
878 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
879
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000880 read_unlock(&bond->lock);
881}
882
stephen hemminger379b7382010-10-15 11:02:56 +0000883static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000884{
885 struct bonding *bond = container_of(work, struct bonding,
886 mcast_work.work);
887 bond_resend_igmp_join_requests(bond);
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800888}
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 * flush all members of flush->mc_list from device dev->mc_list
892 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000893static void bond_mc_list_flush(struct net_device *bond_dev,
894 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Wang Chen454d7c92008-11-12 23:37:49 -0800896 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000897 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Jiri Pirko22bedad32010-04-01 21:22:57 +0000899 netdev_for_each_mc_addr(ha, bond_dev)
900 dev_mc_del(slave_dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 if (bond->params.mode == BOND_MODE_8023AD) {
903 /* del lacpdu mc addr from mc list */
904 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
905
Jiri Pirko22bedad32010-04-01 21:22:57 +0000906 dev_mc_del(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908}
909
910/*--------------------------- Active slave change ---------------------------*/
911
912/*
913 * Update the mc list and multicast-related flags for the new and
914 * old active slaves (if any) according to the multicast mode, and
915 * promiscuous flags unconditionally.
916 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000917static void bond_mc_swap(struct bonding *bond, struct slave *new_active,
918 struct slave *old_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Jiri Pirko22bedad32010-04-01 21:22:57 +0000920 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000922 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 /* nothing to do - mc list is already up-to-date on
924 * all slaves
925 */
926 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (old_active) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000929 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 dev_set_promiscuity(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000932 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 dev_set_allmulti(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Jiri Pirko22bedad32010-04-01 21:22:57 +0000935 netdev_for_each_mc_addr(ha, bond->dev)
936 dev_mc_del(old_active->dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
939 if (new_active) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700940 /* FIXME: Signal errors upstream. */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000941 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 dev_set_promiscuity(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000944 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 dev_set_allmulti(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Jiri Pirko22bedad32010-04-01 21:22:57 +0000947 netdev_for_each_mc_addr(ha, bond->dev)
948 dev_mc_add(new_active->dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950}
951
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700952/*
953 * bond_do_fail_over_mac
954 *
955 * Perform special MAC address swapping for fail_over_mac settings
956 *
957 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
958 */
959static void bond_do_fail_over_mac(struct bonding *bond,
960 struct slave *new_active,
961 struct slave *old_active)
Hannes Eder1f78d9f2009-02-14 11:15:33 +0000962 __releases(&bond->curr_slave_lock)
963 __releases(&bond->lock)
964 __acquires(&bond->lock)
965 __acquires(&bond->curr_slave_lock)
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700966{
967 u8 tmp_mac[ETH_ALEN];
968 struct sockaddr saddr;
969 int rv;
970
971 switch (bond->params.fail_over_mac) {
972 case BOND_FOM_ACTIVE:
973 if (new_active)
974 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
975 new_active->dev->addr_len);
976 break;
977 case BOND_FOM_FOLLOW:
978 /*
979 * if new_active && old_active, swap them
980 * if just old_active, do nothing (going to no active slave)
981 * if just new_active, set new_active to bond's MAC
982 */
983 if (!new_active)
984 return;
985
986 write_unlock_bh(&bond->curr_slave_lock);
987 read_unlock(&bond->lock);
988
989 if (old_active) {
990 memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
991 memcpy(saddr.sa_data, old_active->dev->dev_addr,
992 ETH_ALEN);
993 saddr.sa_family = new_active->dev->type;
994 } else {
995 memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
996 saddr.sa_family = bond->dev->type;
997 }
998
999 rv = dev_set_mac_address(new_active->dev, &saddr);
1000 if (rv) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001001 pr_err("%s: Error %d setting MAC of slave %s\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001002 bond->dev->name, -rv, new_active->dev->name);
1003 goto out;
1004 }
1005
1006 if (!old_active)
1007 goto out;
1008
1009 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
1010 saddr.sa_family = old_active->dev->type;
1011
1012 rv = dev_set_mac_address(old_active->dev, &saddr);
1013 if (rv)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001014 pr_err("%s: Error %d setting MAC of slave %s\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001015 bond->dev->name, -rv, new_active->dev->name);
1016out:
1017 read_lock(&bond->lock);
1018 write_lock_bh(&bond->curr_slave_lock);
1019 break;
1020 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001021 pr_err("%s: bond_do_fail_over_mac impossible: bad policy %d\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001022 bond->dev->name, bond->params.fail_over_mac);
1023 break;
1024 }
1025
1026}
1027
Jiri Pirkoa5499522009-09-25 03:28:09 +00001028static bool bond_should_change_active(struct bonding *bond)
1029{
1030 struct slave *prim = bond->primary_slave;
1031 struct slave *curr = bond->curr_active_slave;
1032
1033 if (!prim || !curr || curr->link != BOND_LINK_UP)
1034 return true;
1035 if (bond->force_primary) {
1036 bond->force_primary = false;
1037 return true;
1038 }
1039 if (bond->params.primary_reselect == BOND_PRI_RESELECT_BETTER &&
1040 (prim->speed < curr->speed ||
1041 (prim->speed == curr->speed && prim->duplex <= curr->duplex)))
1042 return false;
1043 if (bond->params.primary_reselect == BOND_PRI_RESELECT_FAILURE)
1044 return false;
1045 return true;
1046}
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048/**
1049 * find_best_interface - select the best available slave to be the active one
1050 * @bond: our bonding struct
1051 *
1052 * Warning: Caller must hold curr_slave_lock for writing.
1053 */
1054static struct slave *bond_find_best_slave(struct bonding *bond)
1055{
1056 struct slave *new_active, *old_active;
1057 struct slave *bestslave = NULL;
1058 int mintime = bond->params.updelay;
1059 int i;
1060
Nicolas de Pesloüan49b4ad92009-10-07 14:11:00 -07001061 new_active = bond->curr_active_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 if (!new_active) { /* there were no active slaves left */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001064 if (bond->slave_cnt > 0) /* found one slave */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 new_active = bond->first_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001066 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return NULL; /* still no slave, return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if ((bond->primary_slave) &&
Jiri Pirkoa5499522009-09-25 03:28:09 +00001071 bond->primary_slave->link == BOND_LINK_UP &&
1072 bond_should_change_active(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 new_active = bond->primary_slave;
1074 }
1075
1076 /* remember where to stop iterating over the slaves */
1077 old_active = new_active;
1078
1079 bond_for_each_slave_from(bond, new_active, i, old_active) {
Jiri Pirkob9f60252009-08-31 11:09:38 +00001080 if (new_active->link == BOND_LINK_UP) {
1081 return new_active;
1082 } else if (new_active->link == BOND_LINK_BACK &&
1083 IS_UP(new_active->dev)) {
1084 /* link up, but waiting for stabilization */
1085 if (new_active->delay < mintime) {
1086 mintime = new_active->delay;
1087 bestslave = new_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089 }
1090 }
1091
1092 return bestslave;
1093}
1094
1095/**
1096 * change_active_interface - change the active slave into the specified one
1097 * @bond: our bonding struct
1098 * @new: the new slave to make the active one
1099 *
1100 * Set the new slave to the bond's settings and unset them on the old
1101 * curr_active_slave.
1102 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1103 *
1104 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1105 * because it is apparently the best available slave we have, even though its
1106 * updelay hasn't timed out yet.
1107 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001108 * If new_active is not NULL, caller must hold bond->lock for read and
1109 * curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001111void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
1113 struct slave *old_active = bond->curr_active_slave;
1114
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001115 if (old_active == new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 if (new_active) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07001119 new_active->jiffies = jiffies;
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (new_active->link == BOND_LINK_BACK) {
1122 if (USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001123 pr_info("%s: making interface %s the new active one %d ms earlier.\n",
1124 bond->dev->name, new_active->dev->name,
1125 (bond->params.updelay - new_active->delay) * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
1128 new_active->delay = 0;
1129 new_active->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001131 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Holger Eitzenberger58402052008-12-09 23:07:13 -08001134 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 } else {
1137 if (USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001138 pr_info("%s: making interface %s the new active one.\n",
1139 bond->dev->name, new_active->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141 }
1142 }
1143
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001144 if (USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 bond_mc_swap(bond, new_active, old_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Holger Eitzenberger58402052008-12-09 23:07:13 -08001147 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 bond_alb_handle_active_change(bond, new_active);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001149 if (old_active)
1150 bond_set_slave_inactive_flags(old_active);
1151 if (new_active)
1152 bond_set_slave_active_flags(new_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 } else {
1154 bond->curr_active_slave = new_active;
1155 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001156
1157 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001158 if (old_active)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001159 bond_set_slave_inactive_flags(old_active);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001160
1161 if (new_active) {
1162 bond_set_slave_active_flags(new_active);
Moni Shoua2ab82852007-10-09 19:43:39 -07001163
Or Gerlitz709f8a42008-06-13 18:12:01 -07001164 if (bond->params.fail_over_mac)
1165 bond_do_fail_over_mac(bond, new_active,
1166 old_active);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001167
Ben Hutchingsffa95ed2010-12-13 08:19:56 +00001168 if (netif_running(bond->dev)) {
1169 bond->send_grat_arp = bond->params.num_grat_arp;
1170 bond_send_gratuitous_arp(bond);
Or Gerlitz01f31092008-06-13 18:12:02 -07001171
Ben Hutchingsffa95ed2010-12-13 08:19:56 +00001172 bond->send_unsol_na = bond->params.num_unsol_na;
1173 bond_send_unsolicited_na(bond);
1174 }
Brian Haley305d5522008-11-04 17:51:14 -08001175
Or Gerlitz01f31092008-06-13 18:12:02 -07001176 write_unlock_bh(&bond->curr_slave_lock);
1177 read_unlock(&bond->lock);
1178
Moni Shoua75c78502009-09-15 02:37:40 -07001179 netdev_bonding_change(bond->dev, NETDEV_BONDING_FAILOVER);
Or Gerlitz01f31092008-06-13 18:12:02 -07001180
1181 read_lock(&bond->lock);
1182 write_lock_bh(&bond->curr_slave_lock);
Moni Shoua7893b242008-05-17 21:10:12 -07001183 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001184 }
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001185
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001186 /* resend IGMP joins since active slave has changed or
1187 * all were sent on curr_active_slave */
Ben Hutchingsffa95ed2010-12-13 08:19:56 +00001188 if (((USES_PRIMARY(bond->params.mode) && new_active) ||
1189 bond->params.mode == BOND_MODE_ROUNDROBIN) &&
1190 netif_running(bond->dev)) {
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001191 bond->igmp_retrans = bond->params.resend_igmp;
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001192 queue_delayed_work(bond->wq, &bond->mcast_work, 0);
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
1196/**
1197 * bond_select_active_slave - select a new active slave, if needed
1198 * @bond: our bonding struct
1199 *
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001200 * This functions should be called when one of the following occurs:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 * - The old curr_active_slave has been released or lost its link.
1202 * - The primary_slave has got its link back.
1203 * - A slave has got its link back and there's no old curr_active_slave.
1204 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001205 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001207void bond_select_active_slave(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 struct slave *best_slave;
Jay Vosburghff59c452006-03-27 13:27:43 -08001210 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 best_slave = bond_find_best_slave(bond);
1213 if (best_slave != bond->curr_active_slave) {
1214 bond_change_active_slave(bond, best_slave);
Jay Vosburghff59c452006-03-27 13:27:43 -08001215 rv = bond_set_carrier(bond);
1216 if (!rv)
1217 return;
1218
1219 if (netif_carrier_ok(bond->dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001220 pr_info("%s: first active interface up!\n",
1221 bond->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08001222 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001223 pr_info("%s: now running without any active interface !\n",
1224 bond->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08001225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227}
1228
1229/*--------------------------- slave list handling ---------------------------*/
1230
1231/*
1232 * This function attaches the slave to the end of list.
1233 *
1234 * bond->lock held for writing by caller.
1235 */
1236static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1237{
1238 if (bond->first_slave == NULL) { /* attaching the first slave */
1239 new_slave->next = new_slave;
1240 new_slave->prev = new_slave;
1241 bond->first_slave = new_slave;
1242 } else {
1243 new_slave->next = bond->first_slave;
1244 new_slave->prev = bond->first_slave->prev;
1245 new_slave->next->prev = new_slave;
1246 new_slave->prev->next = new_slave;
1247 }
1248
1249 bond->slave_cnt++;
1250}
1251
1252/*
1253 * This function detaches the slave from the list.
1254 * WARNING: no check is made to verify if the slave effectively
1255 * belongs to <bond>.
1256 * Nothing is freed on return, structures are just unchained.
1257 * If any slave pointer in bond was pointing to <slave>,
1258 * it should be changed by the calling function.
1259 *
1260 * bond->lock held for writing by caller.
1261 */
1262static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1263{
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001264 if (slave->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 slave->next->prev = slave->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001267 if (slave->prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 slave->prev->next = slave->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 if (bond->first_slave == slave) { /* slave is the first slave */
1271 if (bond->slave_cnt > 1) { /* there are more slave */
1272 bond->first_slave = slave->next;
1273 } else {
1274 bond->first_slave = NULL; /* slave was the last one */
1275 }
1276 }
1277
1278 slave->next = NULL;
1279 slave->prev = NULL;
1280 bond->slave_cnt--;
1281}
1282
WANG Congf6dc31a2010-05-06 00:48:51 -07001283#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001284static inline int slave_enable_netpoll(struct slave *slave)
WANG Congf6dc31a2010-05-06 00:48:51 -07001285{
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001286 struct netpoll *np;
1287 int err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001288
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001289 np = kzalloc(sizeof(*np), GFP_KERNEL);
1290 err = -ENOMEM;
1291 if (!np)
1292 goto out;
1293
1294 np->dev = slave->dev;
1295 err = __netpoll_setup(np);
1296 if (err) {
1297 kfree(np);
1298 goto out;
WANG Congf6dc31a2010-05-06 00:48:51 -07001299 }
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001300 slave->np = np;
1301out:
1302 return err;
1303}
1304static inline void slave_disable_netpoll(struct slave *slave)
1305{
1306 struct netpoll *np = slave->np;
1307
1308 if (!np)
1309 return;
1310
1311 slave->np = NULL;
1312 synchronize_rcu_bh();
1313 __netpoll_cleanup(np);
1314 kfree(np);
1315}
1316static inline bool slave_dev_support_netpoll(struct net_device *slave_dev)
1317{
1318 if (slave_dev->priv_flags & IFF_DISABLE_NETPOLL)
1319 return false;
1320 if (!slave_dev->netdev_ops->ndo_poll_controller)
1321 return false;
1322 return true;
WANG Congf6dc31a2010-05-06 00:48:51 -07001323}
1324
1325static void bond_poll_controller(struct net_device *bond_dev)
1326{
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001327}
1328
1329static void __bond_netpoll_cleanup(struct bonding *bond)
1330{
Neil Hormanc2355e12010-10-13 16:01:49 +00001331 struct slave *slave;
1332 int i;
1333
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001334 bond_for_each_slave(bond, slave, i)
1335 if (IS_UP(slave->dev))
1336 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07001337}
WANG Congf6dc31a2010-05-06 00:48:51 -07001338static void bond_netpoll_cleanup(struct net_device *bond_dev)
1339{
1340 struct bonding *bond = netdev_priv(bond_dev);
WANG Congf6dc31a2010-05-06 00:48:51 -07001341
1342 read_lock(&bond->lock);
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001343 __bond_netpoll_cleanup(bond);
WANG Congf6dc31a2010-05-06 00:48:51 -07001344 read_unlock(&bond->lock);
1345}
1346
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001347static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
1348{
1349 struct bonding *bond = netdev_priv(dev);
1350 struct slave *slave;
1351 int i, err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001352
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001353 read_lock(&bond->lock);
1354 bond_for_each_slave(bond, slave, i) {
1355 if (!IS_UP(slave->dev))
1356 continue;
1357 err = slave_enable_netpoll(slave);
1358 if (err) {
1359 __bond_netpoll_cleanup(bond);
1360 break;
1361 }
1362 }
1363 read_unlock(&bond->lock);
1364 return err;
1365}
1366
1367static struct netpoll_info *bond_netpoll_info(struct bonding *bond)
1368{
1369 return bond->dev->npinfo;
1370}
1371
1372#else
1373static inline int slave_enable_netpoll(struct slave *slave)
1374{
1375 return 0;
1376}
1377static inline void slave_disable_netpoll(struct slave *slave)
1378{
1379}
WANG Congf6dc31a2010-05-06 00:48:51 -07001380static void bond_netpoll_cleanup(struct net_device *bond_dev)
1381{
1382}
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001383static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
1384{
1385 return 0;
1386}
1387static struct netpoll_info *bond_netpoll_info(struct bonding *bond)
1388{
1389 return NULL;
1390}
WANG Congf6dc31a2010-05-06 00:48:51 -07001391#endif
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393/*---------------------------------- IOCTL ----------------------------------*/
1394
Adrian Bunk4ad072c2007-07-09 11:51:12 -07001395static int bond_sethwaddr(struct net_device *bond_dev,
1396 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001398 pr_debug("bond_dev=%p\n", bond_dev);
1399 pr_debug("slave_dev=%p\n", slave_dev);
1400 pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1402 return 0;
1403}
1404
Herbert Xu7f353bf2007-08-10 15:47:58 -07001405#define BOND_VLAN_FEATURES \
1406 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1407 NETIF_F_HW_VLAN_FILTER)
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001408
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001409/*
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001410 * Compute the common dev->feature set available to all slaves. Some
Herbert Xu7f353bf2007-08-10 15:47:58 -07001411 * feature bits are managed elsewhere, so preserve those feature bits
1412 * on the master device.
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001413 */
1414static int bond_compute_features(struct bonding *bond)
1415{
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001416 struct slave *slave;
1417 struct net_device *bond_dev = bond->dev;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08001418 u32 features = bond_dev->features;
1419 u32 vlan_features = 0;
Moni Shoua3158bf72007-10-09 19:43:41 -07001420 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1421 bond_dev->hard_header_len);
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001422 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001423
Herbert Xu7f353bf2007-08-10 15:47:58 -07001424 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
Herbert Xub63365a2008-10-23 01:11:29 -07001425 features |= NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1426
1427 if (!bond->first_slave)
1428 goto done;
1429
1430 features &= ~NETIF_F_ONE_FOR_ALL;
Herbert Xu7f353bf2007-08-10 15:47:58 -07001431
Jay Vosburgh278339a2009-08-28 12:05:12 +00001432 vlan_features = bond->first_slave->dev->vlan_features;
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001433 bond_for_each_slave(bond, slave, i) {
Herbert Xub63365a2008-10-23 01:11:29 -07001434 features = netdev_increment_features(features,
1435 slave->dev->features,
1436 NETIF_F_ONE_FOR_ALL);
Jay Vosburgh278339a2009-08-28 12:05:12 +00001437 vlan_features = netdev_increment_features(vlan_features,
1438 slave->dev->vlan_features,
1439 NETIF_F_ONE_FOR_ALL);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001440 if (slave->dev->hard_header_len > max_hard_header_len)
1441 max_hard_header_len = slave->dev->hard_header_len;
1442 }
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001443
Herbert Xub63365a2008-10-23 01:11:29 -07001444done:
Herbert Xu7f353bf2007-08-10 15:47:58 -07001445 features |= (bond_dev->features & BOND_VLAN_FEATURES);
Michał Mirosławacd11302011-01-24 15:45:15 -08001446 bond_dev->features = netdev_fix_features(bond_dev, features);
1447 bond_dev->vlan_features = netdev_fix_features(bond_dev, vlan_features);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001448 bond_dev->hard_header_len = max_hard_header_len;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001449
1450 return 0;
1451}
1452
Moni Shoua872254d2007-10-09 19:43:38 -07001453static void bond_setup_by_slave(struct net_device *bond_dev,
1454 struct net_device *slave_dev)
1455{
Wang Chen454d7c92008-11-12 23:37:49 -08001456 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07001457
Stephen Hemminger00829822008-11-20 20:14:53 -08001458 bond_dev->header_ops = slave_dev->header_ops;
Moni Shoua872254d2007-10-09 19:43:38 -07001459
1460 bond_dev->type = slave_dev->type;
1461 bond_dev->hard_header_len = slave_dev->hard_header_len;
1462 bond_dev->addr_len = slave_dev->addr_len;
1463
1464 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1465 slave_dev->addr_len);
Moni Shouad90a1622007-10-09 19:43:43 -07001466 bond->setup_by_slave = 1;
Moni Shoua872254d2007-10-09 19:43:38 -07001467}
1468
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001469/* On bonding slaves other than the currently active slave, suppress
1470 * duplicates except for 802.3ad ETH_P_SLOW, alb non-mcast/bcast, and
1471 * ARP on active-backup slaves with arp_validate enabled.
1472 */
1473static bool bond_should_deliver_exact_match(struct sk_buff *skb,
1474 struct net_device *slave_dev,
1475 struct net_device *bond_dev)
1476{
1477 if (slave_dev->priv_flags & IFF_SLAVE_INACTIVE) {
1478 if (slave_dev->priv_flags & IFF_SLAVE_NEEDARP &&
1479 skb->protocol == __cpu_to_be16(ETH_P_ARP))
1480 return false;
1481
1482 if (bond_dev->priv_flags & IFF_MASTER_ALB &&
1483 skb->pkt_type != PACKET_BROADCAST &&
1484 skb->pkt_type != PACKET_MULTICAST)
1485 return false;
1486
1487 if (bond_dev->priv_flags & IFF_MASTER_8023AD &&
1488 skb->protocol == __cpu_to_be16(ETH_P_SLOW))
1489 return false;
1490
1491 return true;
1492 }
1493 return false;
1494}
1495
1496static struct sk_buff *bond_handle_frame(struct sk_buff *skb)
1497{
1498 struct net_device *slave_dev;
1499 struct net_device *bond_dev;
1500
1501 skb = skb_share_check(skb, GFP_ATOMIC);
1502 if (unlikely(!skb))
1503 return NULL;
1504 slave_dev = skb->dev;
1505 bond_dev = ACCESS_ONCE(slave_dev->master);
1506 if (unlikely(!bond_dev))
1507 return skb;
1508
1509 if (bond_dev->priv_flags & IFF_MASTER_ARPMON)
1510 slave_dev->last_rx = jiffies;
1511
1512 if (bond_should_deliver_exact_match(skb, slave_dev, bond_dev)) {
1513 skb->deliver_no_wcard = 1;
1514 return skb;
1515 }
1516
1517 skb->dev = bond_dev;
1518
1519 if (bond_dev->priv_flags & IFF_MASTER_ALB &&
1520 bond_dev->priv_flags & IFF_BRIDGE_PORT &&
1521 skb->pkt_type == PACKET_HOST) {
1522 u16 *dest = (u16 *) eth_hdr(skb)->h_dest;
1523
1524 memcpy(dest, bond_dev->dev_addr, ETH_ALEN);
1525 }
1526
1527 return skb;
1528}
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530/* enslave device <slave> to bond device <master> */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001531int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Wang Chen454d7c92008-11-12 23:37:49 -08001533 struct bonding *bond = netdev_priv(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001534 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 struct slave *new_slave = NULL;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001536 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 struct sockaddr addr;
1538 int link_reporting;
1539 int old_features = bond_dev->features;
1540 int res = 0;
1541
nsxfreddy@gmail.com552709d2005-09-21 14:18:04 -05001542 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001543 slave_ops->ndo_do_ioctl == NULL) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001544 pr_warning("%s: Warning: no link monitoring support for %s\n",
1545 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
1547
1548 /* bond must be initialized by bond_open() before enslaving */
1549 if (!(bond_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001550 pr_warning("%s: master_dev is not up in bond_enslave\n",
1551 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
1553
1554 /* already enslaved */
1555 if (slave_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001556 pr_debug("Error, Device was already enslaved\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 return -EBUSY;
1558 }
1559
1560 /* vlan challenged mutual exclusion */
1561 /* no need to lock since we're protected by rtnl_lock */
1562 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001563 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Jay Vosburghf35188f2010-07-21 12:14:47 +00001564 if (bond->vlgrp) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001565 pr_err("%s: Error: cannot enslave VLAN challenged slave %s on VLAN enabled bond %s\n",
1566 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 return -EPERM;
1568 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001569 pr_warning("%s: Warning: enslaved VLAN challenged slave %s. Adding VLANs will be blocked as long as %s is part of bond %s\n",
1570 bond_dev->name, slave_dev->name,
1571 slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1573 }
1574 } else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001575 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 if (bond->slave_cnt == 0) {
1577 /* First slave, and it is not VLAN challenged,
1578 * so remove the block of adding VLANs over the bond.
1579 */
1580 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1581 }
1582 }
1583
Jay Vosburgh217df672005-09-26 16:11:50 -07001584 /*
1585 * Old ifenslave binaries are no longer supported. These can
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001586 * be identified with moderate accuracy by the state of the slave:
Jay Vosburgh217df672005-09-26 16:11:50 -07001587 * the current ifenslave will set the interface down prior to
1588 * enslaving it; the old ifenslave will not.
1589 */
1590 if ((slave_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001591 pr_err("%s is up. This may be due to an out of date ifenslave.\n",
Jay Vosburgh217df672005-09-26 16:11:50 -07001592 slave_dev->name);
1593 res = -EPERM;
1594 goto err_undo_flags;
1595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Moni Shoua872254d2007-10-09 19:43:38 -07001597 /* set bonding device ether type by slave - bonding netdevices are
1598 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1599 * there is a need to override some of the type dependent attribs/funcs.
1600 *
1601 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1602 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1603 */
1604 if (bond->slave_cnt == 0) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001605 if (bond_dev->type != slave_dev->type) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001606 pr_debug("%s: change device type from %d to %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001607 bond_dev->name,
1608 bond_dev->type, slave_dev->type);
Moni Shoua75c78502009-09-15 02:37:40 -07001609
Jiri Pirko3ca5b402010-03-10 10:29:35 +00001610 res = netdev_bonding_change(bond_dev,
1611 NETDEV_PRE_TYPE_CHANGE);
1612 res = notifier_to_errno(res);
1613 if (res) {
1614 pr_err("%s: refused to change device type\n",
1615 bond_dev->name);
1616 res = -EBUSY;
1617 goto err_undo_flags;
1618 }
Moni Shoua75c78502009-09-15 02:37:40 -07001619
Jiri Pirko32a806c2010-03-19 04:00:23 +00001620 /* Flush unicast and multicast addresses */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001621 dev_uc_flush(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001622 dev_mc_flush(bond_dev);
Jiri Pirko32a806c2010-03-19 04:00:23 +00001623
Moni Shouae36b9d12009-07-15 04:56:31 +00001624 if (slave_dev->type != ARPHRD_ETHER)
1625 bond_setup_by_slave(bond_dev, slave_dev);
1626 else
1627 ether_setup(bond_dev);
Moni Shoua75c78502009-09-15 02:37:40 -07001628
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001629 netdev_bonding_change(bond_dev,
1630 NETDEV_POST_TYPE_CHANGE);
Moni Shouae36b9d12009-07-15 04:56:31 +00001631 }
Moni Shoua872254d2007-10-09 19:43:38 -07001632 } else if (bond_dev->type != slave_dev->type) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001633 pr_err("%s ether type (%d) is different from other slaves (%d), can not enslave it.\n",
1634 slave_dev->name,
1635 slave_dev->type, bond_dev->type);
1636 res = -EINVAL;
1637 goto err_undo_flags;
Moni Shoua872254d2007-10-09 19:43:38 -07001638 }
1639
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001640 if (slave_ops->ndo_set_mac_address == NULL) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001641 if (bond->slave_cnt == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001642 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.",
1643 bond_dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001644 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1645 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001646 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",
1647 bond_dev->name);
Moni Shoua2ab82852007-10-09 19:43:39 -07001648 res = -EOPNOTSUPP;
1649 goto err_undo_flags;
1650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001653 /* If this is the first slave, then we need to set the master's hardware
1654 * address to be the same as the slave's. */
David Strandd13a2cb2010-12-01 11:43:08 -08001655 if (is_zero_ether_addr(bond->dev->dev_addr))
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001656 memcpy(bond->dev->dev_addr, slave_dev->dev_addr,
1657 slave_dev->addr_len);
1658
1659
Joe Jin243cb4e2007-02-06 14:16:40 -08001660 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 if (!new_slave) {
1662 res = -ENOMEM;
1663 goto err_undo_flags;
1664 }
1665
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001666 /*
1667 * Set the new_slave's queue_id to be zero. Queue ID mapping
1668 * is set via sysfs or module option if desired.
1669 */
1670 new_slave->queue_id = 0;
1671
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001672 /* Save slave's original mtu and then set it to match the bond */
1673 new_slave->original_mtu = slave_dev->mtu;
1674 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1675 if (res) {
1676 pr_debug("Error %d calling dev_set_mtu\n", res);
1677 goto err_free;
1678 }
1679
Jay Vosburgh217df672005-09-26 16:11:50 -07001680 /*
1681 * Save slave's original ("permanent") mac address for modes
1682 * that need it, and for restoring it upon release, and then
1683 * set it to the master's address
1684 */
1685 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Jay Vosburghdd957c52007-10-09 19:57:24 -07001687 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001688 /*
1689 * Set slave to master's mac address. The application already
1690 * set the master's mac address to that of the first slave
1691 */
1692 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1693 addr.sa_family = slave_dev->type;
1694 res = dev_set_mac_address(slave_dev, &addr);
1695 if (res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001696 pr_debug("Error %d calling set_mac_address\n", res);
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001697 goto err_restore_mtu;
Moni Shoua2ab82852007-10-09 19:43:39 -07001698 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Jiri Pirko1765a572011-02-12 06:48:36 +00001701 res = netdev_set_bond_master(slave_dev, bond_dev);
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001702 if (res) {
Jiri Pirko1765a572011-02-12 06:48:36 +00001703 pr_debug("Error %d calling netdev_set_bond_master\n", res);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001704 goto err_restore_mac;
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001705 }
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001706 res = netdev_rx_handler_register(slave_dev, bond_handle_frame, NULL);
1707 if (res) {
1708 pr_debug("Error %d calling netdev_rx_handler_register\n", res);
1709 goto err_unset_master;
1710 }
1711
Jay Vosburgh217df672005-09-26 16:11:50 -07001712 /* open the slave since the application closed it */
1713 res = dev_open(slave_dev);
1714 if (res) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001715 pr_debug("Opening slave %s failed\n", slave_dev->name);
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001716 goto err_unreg_rxhandler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 }
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 new_slave->dev = slave_dev;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001720 slave_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Holger Eitzenberger58402052008-12-09 23:07:13 -08001722 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 /* bond_alb_init_slave() must be called before all other stages since
1724 * it might fail and we do not want to have to undo everything
1725 */
1726 res = bond_alb_init_slave(bond, new_slave);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001727 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001728 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
1730
1731 /* If the mode USES_PRIMARY, then the new slave gets the
1732 * master's promisc (and mc) settings only if it becomes the
1733 * curr_active_slave, and that is taken care of later when calling
1734 * bond_change_active()
1735 */
1736 if (!USES_PRIMARY(bond->params.mode)) {
1737 /* set promiscuity level to new slave */
1738 if (bond_dev->flags & IFF_PROMISC) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001739 res = dev_set_promiscuity(slave_dev, 1);
1740 if (res)
1741 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
1743
1744 /* set allmulti level to new slave */
1745 if (bond_dev->flags & IFF_ALLMULTI) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001746 res = dev_set_allmulti(slave_dev, 1);
1747 if (res)
1748 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
1750
David S. Millerb9e40852008-07-15 00:15:08 -07001751 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 /* upload master's mc_list to new slave */
Jiri Pirko22bedad32010-04-01 21:22:57 +00001753 netdev_for_each_mc_addr(ha, bond_dev)
1754 dev_mc_add(slave_dev, ha->addr);
David S. Millerb9e40852008-07-15 00:15:08 -07001755 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
1757
1758 if (bond->params.mode == BOND_MODE_8023AD) {
1759 /* add lacpdu mc addr to mc list */
1760 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1761
Jiri Pirko22bedad32010-04-01 21:22:57 +00001762 dev_mc_add(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
1764
1765 bond_add_vlans_on_slave(bond, slave_dev);
1766
1767 write_lock_bh(&bond->lock);
1768
1769 bond_attach_slave(bond, new_slave);
1770
1771 new_slave->delay = 0;
1772 new_slave->link_failure_count = 0;
1773
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001774 bond_compute_features(bond);
1775
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001776 write_unlock_bh(&bond->lock);
1777
1778 read_lock(&bond->lock);
1779
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001780 new_slave->last_arp_rx = jiffies;
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (bond->params.miimon && !bond->params.use_carrier) {
1783 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1784
1785 if ((link_reporting == -1) && !bond->params.arp_interval) {
1786 /*
1787 * miimon is set but a bonded network driver
1788 * does not support ETHTOOL/MII and
1789 * arp_interval is not set. Note: if
1790 * use_carrier is enabled, we will never go
1791 * here (because netif_carrier is always
1792 * supported); thus, we don't need to change
1793 * the messages for netif_carrier.
1794 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001795 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 -08001796 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 } else if (link_reporting == -1) {
1798 /* unable get link status using mii/ethtool */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001799 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",
1800 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
1802 }
1803
1804 /* check for initial state */
1805 if (!bond->params.miimon ||
1806 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1807 if (bond->params.updelay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001808 pr_debug("Initial state of slave_dev is BOND_LINK_BACK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 new_slave->link = BOND_LINK_BACK;
1810 new_slave->delay = bond->params.updelay;
1811 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001812 pr_debug("Initial state of slave_dev is BOND_LINK_UP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 new_slave->link = BOND_LINK_UP;
1814 }
1815 new_slave->jiffies = jiffies;
1816 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001817 pr_debug("Initial state of slave_dev is BOND_LINK_DOWN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 new_slave->link = BOND_LINK_DOWN;
1819 }
1820
1821 if (bond_update_speed_duplex(new_slave) &&
1822 (new_slave->link != BOND_LINK_DOWN)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001823 pr_warning("%s: Warning: failed to get speed and duplex from %s, assumed to be 100Mb/sec and Full.\n",
1824 bond_dev->name, new_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 if (bond->params.mode == BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001827 pr_warning("%s: Warning: Operation of 802.3ad mode requires ETHTOOL support in base driver for proper aggregator selection.\n",
1828 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
1830 }
1831
1832 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1833 /* if there is a primary slave, remember it */
Jiri Pirkoa5499522009-09-25 03:28:09 +00001834 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 bond->primary_slave = new_slave;
Jiri Pirkoa5499522009-09-25 03:28:09 +00001836 bond->force_primary = true;
1837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001840 write_lock_bh(&bond->curr_slave_lock);
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 switch (bond->params.mode) {
1843 case BOND_MODE_ACTIVEBACKUP:
Jay Vosburgh8a8e4472006-09-22 21:56:15 -07001844 bond_set_slave_inactive_flags(new_slave);
1845 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 break;
1847 case BOND_MODE_8023AD:
1848 /* in 802.3ad mode, the internal mechanism
1849 * will activate the slaves in the selected
1850 * aggregator
1851 */
1852 bond_set_slave_inactive_flags(new_slave);
1853 /* if this is the first slave */
1854 if (bond->slave_cnt == 1) {
1855 SLAVE_AD_INFO(new_slave).id = 1;
1856 /* Initialize AD with the number of times that the AD timer is called in 1 second
1857 * can be called only after the mac address of the bond is set
1858 */
1859 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1860 bond->params.lacp_fast);
1861 } else {
1862 SLAVE_AD_INFO(new_slave).id =
1863 SLAVE_AD_INFO(new_slave->prev).id + 1;
1864 }
1865
1866 bond_3ad_bind_slave(new_slave);
1867 break;
1868 case BOND_MODE_TLB:
1869 case BOND_MODE_ALB:
1870 new_slave->state = BOND_STATE_ACTIVE;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001871 bond_set_slave_inactive_flags(new_slave);
Jiri Pirko5a29f782009-03-25 17:23:38 -07001872 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 break;
1874 default:
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001875 pr_debug("This slave is always active in trunk mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 /* always active in trunk mode */
1878 new_slave->state = BOND_STATE_ACTIVE;
1879
1880 /* In trunking mode there is little meaning to curr_active_slave
1881 * anyway (it holds no special properties of the bond device),
1882 * so we can change it without calling change_active_interface()
1883 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001884 if (!bond->curr_active_slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 bond->curr_active_slave = new_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 break;
1888 } /* switch(bond_mode) */
1889
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001890 write_unlock_bh(&bond->curr_slave_lock);
1891
Jay Vosburghff59c452006-03-27 13:27:43 -08001892 bond_set_carrier(bond);
1893
WANG Congf6dc31a2010-05-06 00:48:51 -07001894#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001895 slave_dev->npinfo = bond_netpoll_info(bond);
1896 if (slave_dev->npinfo) {
1897 if (slave_enable_netpoll(new_slave)) {
1898 read_unlock(&bond->lock);
1899 pr_info("Error, %s: master_dev is using netpoll, "
1900 "but new slave device does not support netpoll.\n",
1901 bond_dev->name);
1902 res = -EBUSY;
1903 goto err_close;
1904 }
WANG Congf6dc31a2010-05-06 00:48:51 -07001905 }
1906#endif
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001907
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001908 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001910 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1911 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001912 goto err_close;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001913
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001914 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
1915 bond_dev->name, slave_dev->name,
1916 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1917 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 /* enslave is successful */
1920 return 0;
1921
1922/* Undo stages on error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923err_close:
1924 dev_close(slave_dev);
1925
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001926err_unreg_rxhandler:
1927 netdev_rx_handler_unregister(slave_dev);
1928
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001929err_unset_master:
Jiri Pirko1765a572011-02-12 06:48:36 +00001930 netdev_set_bond_master(slave_dev, NULL);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932err_restore_mac:
Jay Vosburghdd957c52007-10-09 19:57:24 -07001933 if (!bond->params.fail_over_mac) {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001934 /* XXX TODO - fom follow mode needs to change master's
1935 * MAC if this slave's MAC is in use by the bond, or at
1936 * least print a warning.
1937 */
Moni Shoua2ab82852007-10-09 19:43:39 -07001938 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1939 addr.sa_family = slave_dev->type;
1940 dev_set_mac_address(slave_dev, &addr);
1941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001943err_restore_mtu:
1944 dev_set_mtu(slave_dev, new_slave->original_mtu);
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946err_free:
1947 kfree(new_slave);
1948
1949err_undo_flags:
1950 bond_dev->features = old_features;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 return res;
1953}
1954
1955/*
1956 * Try to release the slave device <slave> from the bond device <master>
1957 * It is legal to access curr_active_slave without a lock because all the function
1958 * is write-locked.
1959 *
1960 * The rules for slave state should be:
1961 * for Active/Backup:
1962 * Active stays on all backups go down
1963 * for Bonded connections:
1964 * The first up interface should be left on and all others downed.
1965 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001966int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
Wang Chen454d7c92008-11-12 23:37:49 -08001968 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 struct slave *slave, *oldcurrent;
1970 struct sockaddr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 /* slave is not a slave or master is not master of this slave */
1973 if (!(slave_dev->flags & IFF_SLAVE) ||
1974 (slave_dev->master != bond_dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001975 pr_err("%s: Error: cannot release %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 bond_dev->name, slave_dev->name);
1977 return -EINVAL;
1978 }
1979
Neil Hormane843fa52010-10-13 16:01:50 +00001980 block_netpoll_tx();
WANG Congf6dc31a2010-05-06 00:48:51 -07001981 netdev_bonding_change(bond_dev, NETDEV_BONDING_DESLAVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 write_lock_bh(&bond->lock);
1983
1984 slave = bond_get_slave_by_dev(bond, slave_dev);
1985 if (!slave) {
1986 /* not a slave of this bond */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001987 pr_info("%s: %s not enslaved\n",
1988 bond_dev->name, slave_dev->name);
Jay Vosburghf5e2a7b2006-02-07 21:17:22 -08001989 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001990 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return -EINVAL;
1992 }
1993
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001994 if (!bond->params.fail_over_mac) {
Joe Perches8e95a202009-12-03 07:58:21 +00001995 if (!compare_ether_addr(bond_dev->dev_addr, slave->perm_hwaddr) &&
1996 bond->slave_cnt > 1)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001997 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",
1998 bond_dev->name, slave_dev->name,
1999 slave->perm_hwaddr,
2000 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 }
2002
2003 /* Inform AD package of unbinding of slave. */
2004 if (bond->params.mode == BOND_MODE_8023AD) {
2005 /* must be called before the slave is
2006 * detached from the list
2007 */
2008 bond_3ad_unbind_slave(slave);
2009 }
2010
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002011 pr_info("%s: releasing %s interface %s\n",
2012 bond_dev->name,
2013 (slave->state == BOND_STATE_ACTIVE) ? "active" : "backup",
2014 slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 oldcurrent = bond->curr_active_slave;
2017
2018 bond->current_arp_slave = NULL;
2019
2020 /* release the slave from its bond */
2021 bond_detach_slave(bond, slave);
2022
Arthur Kepner8531c5f2005-08-23 01:34:53 -04002023 bond_compute_features(bond);
2024
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002025 if (bond->primary_slave == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 bond->primary_slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002028 if (oldcurrent == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 bond_change_active_slave(bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Holger Eitzenberger58402052008-12-09 23:07:13 -08002031 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 /* Must be called only after the slave has been
2033 * detached from the list and the curr_active_slave
2034 * has been cleared (if our_slave == old_current),
2035 * but before a new active slave is selected.
2036 */
Jay Vosburgh25433312008-01-17 16:24:59 -08002037 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 bond_alb_deinit_slave(bond, slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08002039 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 }
2041
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002042 if (oldcurrent == slave) {
2043 /*
2044 * Note that we hold RTNL over this sequence, so there
2045 * is no concern that another slave add/remove event
2046 * will interfere.
2047 */
2048 write_unlock_bh(&bond->lock);
2049 read_lock(&bond->lock);
2050 write_lock_bh(&bond->curr_slave_lock);
2051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 bond_select_active_slave(bond);
2053
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002054 write_unlock_bh(&bond->curr_slave_lock);
2055 read_unlock(&bond->lock);
2056 write_lock_bh(&bond->lock);
2057 }
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (bond->slave_cnt == 0) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002060 bond_set_carrier(bond);
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 /* if the last slave was removed, zero the mac address
2063 * of the master so it will be set by the application
2064 * to the mac address of the first slave
2065 */
2066 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2067
Jay Vosburghf35188f2010-07-21 12:14:47 +00002068 if (!bond->vlgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2070 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002071 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2072 bond_dev->name, bond_dev->name);
2073 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2074 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 }
2076 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2077 !bond_has_challenged_slaves(bond)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002078 pr_info("%s: last VLAN challenged slave %s left bond %s. VLAN blocking is removed\n",
2079 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
2081 }
2082
2083 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002084 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002086 /* must do this from outside any spinlocks */
2087 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 bond_del_vlans_from_slave(bond, slave_dev);
2090
2091 /* If the mode USES_PRIMARY, then we should only remove its
2092 * promisc and mc settings if it was the curr_active_slave, but that was
2093 * already taken care of above when we detached the slave
2094 */
2095 if (!USES_PRIMARY(bond->params.mode)) {
2096 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002097 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002101 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002105 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002107 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
2109
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00002110 netdev_rx_handler_unregister(slave_dev);
Jiri Pirko1765a572011-02-12 06:48:36 +00002111 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002113 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07002114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 /* close slave before restoring its mac address */
2116 dev_close(slave_dev);
2117
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07002118 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002119 /* restore original ("permanent") mac address */
2120 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2121 addr.sa_family = slave_dev->type;
2122 dev_set_mac_address(slave_dev, &addr);
2123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00002125 dev_set_mtu(slave_dev, slave->original_mtu);
2126
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002127 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002128 IFF_SLAVE_INACTIVE | IFF_BONDING |
2129 IFF_SLAVE_NEEDARP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 kfree(slave);
2132
2133 return 0; /* deletion OK */
2134}
2135
2136/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002137* First release a slave and than destroy the bond if no more slaves are left.
Moni Shouad90a1622007-10-09 19:43:43 -07002138* Must be under rtnl_lock when this function is called.
2139*/
stephen hemminger26d8ee72010-10-15 05:09:34 +00002140static int bond_release_and_destroy(struct net_device *bond_dev,
2141 struct net_device *slave_dev)
Moni Shouad90a1622007-10-09 19:43:43 -07002142{
Wang Chen454d7c92008-11-12 23:37:49 -08002143 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002144 int ret;
2145
2146 ret = bond_release(bond_dev, slave_dev);
2147 if ((ret == 0) && (bond->slave_cnt == 0)) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002148 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002149 pr_info("%s: destroying bond %s.\n",
2150 bond_dev->name, bond_dev->name);
Stephen Hemminger9e716262009-06-12 19:02:47 +00002151 unregister_netdevice(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002152 }
2153 return ret;
2154}
2155
2156/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 * This function releases all slaves.
2158 */
2159static int bond_release_all(struct net_device *bond_dev)
2160{
Wang Chen454d7c92008-11-12 23:37:49 -08002161 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 struct slave *slave;
2163 struct net_device *slave_dev;
2164 struct sockaddr addr;
2165
2166 write_lock_bh(&bond->lock);
2167
Jay Vosburghff59c452006-03-27 13:27:43 -08002168 netif_carrier_off(bond_dev);
2169
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002170 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 bond->current_arp_slave = NULL;
2174 bond->primary_slave = NULL;
2175 bond_change_active_slave(bond, NULL);
2176
2177 while ((slave = bond->first_slave) != NULL) {
2178 /* Inform AD package of unbinding of slave
2179 * before slave is detached from the list.
2180 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002181 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 bond_3ad_unbind_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184 slave_dev = slave->dev;
2185 bond_detach_slave(bond, slave);
2186
Jay Vosburgh25433312008-01-17 16:24:59 -08002187 /* now that the slave is detached, unlock and perform
2188 * all the undo steps that should not be called from
2189 * within a lock.
2190 */
2191 write_unlock_bh(&bond->lock);
2192
Holger Eitzenberger58402052008-12-09 23:07:13 -08002193 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 /* must be called only after the slave
2195 * has been detached from the list
2196 */
2197 bond_alb_deinit_slave(bond, slave);
2198 }
2199
Arthur Kepner8531c5f2005-08-23 01:34:53 -04002200 bond_compute_features(bond);
2201
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002202 bond_destroy_slave_symlinks(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 bond_del_vlans_from_slave(bond, slave_dev);
2204
2205 /* If the mode USES_PRIMARY, then we should only remove its
2206 * promisc and mc settings if it was the curr_active_slave, but that was
2207 * already taken care of above when we detached the slave
2208 */
2209 if (!USES_PRIMARY(bond->params.mode)) {
2210 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002211 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002215 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002219 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002221 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
2223
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00002224 netdev_rx_handler_unregister(slave_dev);
Jiri Pirko1765a572011-02-12 06:48:36 +00002225 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002227 slave_disable_netpoll(slave);
2228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 /* close slave before restoring its mac address */
2230 dev_close(slave_dev);
2231
Jay Vosburghdd957c52007-10-09 19:57:24 -07002232 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002233 /* restore original ("permanent") mac address*/
2234 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2235 addr.sa_family = slave_dev->type;
2236 dev_set_mac_address(slave_dev, &addr);
2237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002239 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
2240 IFF_SLAVE_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242 kfree(slave);
2243
2244 /* re-acquire the lock before getting the next slave */
2245 write_lock_bh(&bond->lock);
2246 }
2247
2248 /* zero the mac address of the master so it will be
2249 * set by the application to the mac address of the
2250 * first slave
2251 */
2252 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2253
Jay Vosburghf35188f2010-07-21 12:14:47 +00002254 if (!bond->vlgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
Jay Vosburghf35188f2010-07-21 12:14:47 +00002256 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002257 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2258 bond_dev->name, bond_dev->name);
2259 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2260 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
2262
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002263 pr_info("%s: released all slaves\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265out:
2266 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return 0;
2268}
2269
2270/*
2271 * This function changes the active slave to slave <slave_dev>.
2272 * It returns -EINVAL in the following cases.
2273 * - <slave_dev> is not found in the list.
2274 * - There is not active slave now.
2275 * - <slave_dev> is already active.
2276 * - The link state of <slave_dev> is not BOND_LINK_UP.
2277 * - <slave_dev> is not running.
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002278 * In these cases, this function does nothing.
2279 * In the other cases, current_slave pointer is changed and 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 */
2281static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2282{
Wang Chen454d7c92008-11-12 23:37:49 -08002283 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 struct slave *old_active = NULL;
2285 struct slave *new_active = NULL;
2286 int res = 0;
2287
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002288 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 /* Verify that master_dev is indeed the master of slave_dev */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002292 if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002295 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002297 read_lock(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 old_active = bond->curr_active_slave;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002299 read_unlock(&bond->curr_slave_lock);
2300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 new_active = bond_get_slave_by_dev(bond, slave_dev);
2302
2303 /*
2304 * Changing to the current active: do nothing; return success.
2305 */
2306 if (new_active && (new_active == old_active)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002307 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return 0;
2309 }
2310
2311 if ((new_active) &&
2312 (old_active) &&
2313 (new_active->link == BOND_LINK_UP) &&
2314 IS_UP(new_active->dev)) {
Neil Hormane843fa52010-10-13 16:01:50 +00002315 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002316 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 bond_change_active_slave(bond, new_active);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002318 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002319 unblock_netpoll_tx();
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002320 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002323 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 return res;
2326}
2327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2329{
Wang Chen454d7c92008-11-12 23:37:49 -08002330 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 info->bond_mode = bond->params.mode;
2333 info->miimon = bond->params.miimon;
2334
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002335 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 info->num_slaves = bond->slave_cnt;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002337 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339 return 0;
2340}
2341
2342static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2343{
Wang Chen454d7c92008-11-12 23:37:49 -08002344 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 struct slave *slave;
Eric Dumazet689c96c2009-04-23 03:39:04 +00002346 int i, res = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002348 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 bond_for_each_slave(bond, slave, i) {
2351 if (i == (int)info->slave_id) {
Eric Dumazet689c96c2009-04-23 03:39:04 +00002352 res = 0;
2353 strcpy(info->slave_name, slave->dev->name);
2354 info->link = slave->link;
2355 info->state = slave->state;
2356 info->link_failure_count = slave->link_failure_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 break;
2358 }
2359 }
2360
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002361 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Eric Dumazet689c96c2009-04-23 03:39:04 +00002363 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
2366/*-------------------------------- Monitoring -------------------------------*/
2367
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002368
2369static int bond_miimon_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002371 struct slave *slave;
2372 int i, link_state, commit = 0;
Jiri Pirko41f89102009-04-24 03:57:29 +00002373 bool ignore_updelay;
2374
2375 ignore_updelay = !bond->curr_active_slave ? true : false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
2377 bond_for_each_slave(bond, slave, i) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002378 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002380 link_state = bond_check_dev_link(bond, slave->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 switch (slave->link) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002383 case BOND_LINK_UP:
2384 if (link_state)
2385 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002387 slave->link = BOND_LINK_FAIL;
2388 slave->delay = bond->params.downdelay;
2389 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002390 pr_info("%s: link status down for %sinterface %s, disabling it in %d ms.\n",
2391 bond->dev->name,
2392 (bond->params.mode ==
2393 BOND_MODE_ACTIVEBACKUP) ?
2394 ((slave->state == BOND_STATE_ACTIVE) ?
2395 "active " : "backup ") : "",
2396 slave->dev->name,
2397 bond->params.downdelay * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002399 /*FALLTHRU*/
2400 case BOND_LINK_FAIL:
2401 if (link_state) {
2402 /*
2403 * recovered before downdelay expired
2404 */
2405 slave->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 slave->jiffies = jiffies;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002407 pr_info("%s: link status up again after %d ms for interface %s.\n",
2408 bond->dev->name,
2409 (bond->params.downdelay - slave->delay) *
2410 bond->params.miimon,
2411 slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002412 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002414
2415 if (slave->delay <= 0) {
2416 slave->new_link = BOND_LINK_DOWN;
2417 commit++;
2418 continue;
2419 }
2420
2421 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002424 case BOND_LINK_DOWN:
2425 if (!link_state)
2426 continue;
2427
2428 slave->link = BOND_LINK_BACK;
2429 slave->delay = bond->params.updelay;
2430
2431 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002432 pr_info("%s: link status up for interface %s, enabling it in %d ms.\n",
2433 bond->dev->name, slave->dev->name,
2434 ignore_updelay ? 0 :
2435 bond->params.updelay *
2436 bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002438 /*FALLTHRU*/
2439 case BOND_LINK_BACK:
2440 if (!link_state) {
2441 slave->link = BOND_LINK_DOWN;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002442 pr_info("%s: link status down again after %d ms for interface %s.\n",
2443 bond->dev->name,
2444 (bond->params.updelay - slave->delay) *
2445 bond->params.miimon,
2446 slave->dev->name);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002447
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002448 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002450
Jiri Pirko41f89102009-04-24 03:57:29 +00002451 if (ignore_updelay)
2452 slave->delay = 0;
2453
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002454 if (slave->delay <= 0) {
2455 slave->new_link = BOND_LINK_UP;
2456 commit++;
Jiri Pirko41f89102009-04-24 03:57:29 +00002457 ignore_updelay = false;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002458 continue;
2459 }
2460
2461 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 break;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002463 }
2464 }
2465
2466 return commit;
2467}
2468
2469static void bond_miimon_commit(struct bonding *bond)
2470{
2471 struct slave *slave;
2472 int i;
2473
2474 bond_for_each_slave(bond, slave, i) {
2475 switch (slave->new_link) {
2476 case BOND_LINK_NOCHANGE:
2477 continue;
2478
2479 case BOND_LINK_UP:
2480 slave->link = BOND_LINK_UP;
2481 slave->jiffies = jiffies;
2482
2483 if (bond->params.mode == BOND_MODE_8023AD) {
2484 /* prevent it from being the active one */
2485 slave->state = BOND_STATE_BACKUP;
2486 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2487 /* make it immediately active */
2488 slave->state = BOND_STATE_ACTIVE;
2489 } else if (slave != bond->primary_slave) {
2490 /* prevent it from being the active one */
2491 slave->state = BOND_STATE_BACKUP;
2492 }
2493
Krzysztof Piotr Oledzki546add72010-10-06 14:28:22 -07002494 bond_update_speed_duplex(slave);
2495
Krzysztof Piotr Oledzki700c2a72010-10-06 14:25:06 -07002496 pr_info("%s: link status definitely up for interface %s, %d Mbps %s duplex.\n",
2497 bond->dev->name, slave->dev->name,
2498 slave->speed, slave->duplex ? "full" : "half");
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002499
2500 /* notify ad that the link status has changed */
2501 if (bond->params.mode == BOND_MODE_8023AD)
2502 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2503
Holger Eitzenberger58402052008-12-09 23:07:13 -08002504 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002505 bond_alb_handle_link_change(bond, slave,
2506 BOND_LINK_UP);
2507
2508 if (!bond->curr_active_slave ||
2509 (slave == bond->primary_slave))
2510 goto do_failover;
2511
2512 continue;
2513
2514 case BOND_LINK_DOWN:
Jay Vosburghfba4acd2008-10-30 17:41:14 -07002515 if (slave->link_failure_count < UINT_MAX)
2516 slave->link_failure_count++;
2517
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002518 slave->link = BOND_LINK_DOWN;
2519
2520 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2521 bond->params.mode == BOND_MODE_8023AD)
2522 bond_set_slave_inactive_flags(slave);
2523
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002524 pr_info("%s: link status definitely down for interface %s, disabling it\n",
2525 bond->dev->name, slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002526
2527 if (bond->params.mode == BOND_MODE_8023AD)
2528 bond_3ad_handle_link_change(slave,
2529 BOND_LINK_DOWN);
2530
Jiri Pirkoae63e802009-05-27 05:42:36 +00002531 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002532 bond_alb_handle_link_change(bond, slave,
2533 BOND_LINK_DOWN);
2534
2535 if (slave == bond->curr_active_slave)
2536 goto do_failover;
2537
2538 continue;
2539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002541 pr_err("%s: invalid new link %d on slave %s\n",
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002542 bond->dev->name, slave->new_link,
2543 slave->dev->name);
2544 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002546 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 }
2548
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002549do_failover:
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002550 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00002551 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002552 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 bond_select_active_slave(bond);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002554 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002555 unblock_netpoll_tx();
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002556 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002557
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002558 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559}
2560
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002561/*
2562 * bond_mii_monitor
2563 *
2564 * Really a wrapper that splits the mii monitor into two phases: an
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002565 * inspection, then (if inspection indicates something needs to be done)
2566 * an acquisition of appropriate locks followed by a commit phase to
2567 * implement whatever link state changes are indicated.
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002568 */
2569void bond_mii_monitor(struct work_struct *work)
2570{
2571 struct bonding *bond = container_of(work, struct bonding,
2572 mii_work.work);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002573
2574 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002575 if (bond->kill_timers)
2576 goto out;
2577
2578 if (bond->slave_cnt == 0)
2579 goto re_arm;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002580
2581 if (bond->send_grat_arp) {
2582 read_lock(&bond->curr_slave_lock);
2583 bond_send_gratuitous_arp(bond);
2584 read_unlock(&bond->curr_slave_lock);
2585 }
2586
Brian Haley305d5522008-11-04 17:51:14 -08002587 if (bond->send_unsol_na) {
2588 read_lock(&bond->curr_slave_lock);
2589 bond_send_unsolicited_na(bond);
2590 read_unlock(&bond->curr_slave_lock);
2591 }
2592
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002593 if (bond_miimon_inspect(bond)) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002594 read_unlock(&bond->lock);
2595 rtnl_lock();
2596 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002597
2598 bond_miimon_commit(bond);
2599
Jay Vosburgh56556622008-01-17 16:25:03 -08002600 read_unlock(&bond->lock);
2601 rtnl_unlock(); /* might sleep, hold no other locks */
2602 read_lock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002603 }
2604
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002605re_arm:
2606 if (bond->params.miimon)
2607 queue_delayed_work(bond->wq, &bond->mii_work,
2608 msecs_to_jiffies(bond->params.miimon));
2609out:
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002610 read_unlock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002611}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002612
Al Virod3bb52b2007-08-22 20:06:58 -04002613static __be32 bond_glean_dev_ip(struct net_device *dev)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002614{
2615 struct in_device *idev;
2616 struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07002617 __be32 addr = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002618
2619 if (!dev)
2620 return 0;
2621
2622 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002623 idev = __in_dev_get_rcu(dev);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002624 if (!idev)
2625 goto out;
2626
2627 ifa = idev->ifa_list;
2628 if (!ifa)
2629 goto out;
2630
2631 addr = ifa->ifa_local;
2632out:
2633 rcu_read_unlock();
2634 return addr;
2635}
2636
Al Virod3bb52b2007-08-22 20:06:58 -04002637static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002638{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002639 struct vlan_entry *vlan;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002640
2641 if (ip == bond->master_ip)
2642 return 1;
2643
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002644 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002645 if (ip == vlan->vlan_ip)
2646 return 1;
2647 }
2648
2649 return 0;
2650}
2651
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002652/*
2653 * We go to the (large) trouble of VLAN tagging ARP frames because
2654 * switches in VLAN mode (especially if ports are configured as
2655 * "native" to a VLAN) might not pass non-tagged frames.
2656 */
Al Virod3bb52b2007-08-22 20:06:58 -04002657static 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 -04002658{
2659 struct sk_buff *skb;
2660
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002661 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002662 slave_dev->name, dest_ip, src_ip, vlan_id);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002663
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002664 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2665 NULL, slave_dev->dev_addr, NULL);
2666
2667 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002668 pr_err("ARP packet allocation failed\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002669 return;
2670 }
2671 if (vlan_id) {
2672 skb = vlan_put_tag(skb, vlan_id);
2673 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002674 pr_err("failed to insert VLAN tag\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002675 return;
2676 }
2677 }
2678 arp_xmit(skb);
2679}
2680
2681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2683{
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002684 int i, vlan_id, rv;
Al Virod3bb52b2007-08-22 20:06:58 -04002685 __be32 *targets = bond->params.arp_targets;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002686 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002687 struct net_device *vlan_dev;
2688 struct flowi fl;
2689 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Mitch Williams6b780562005-11-09 10:36:19 -08002691 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2692 if (!targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07002693 break;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002694 pr_debug("basa: target %x\n", targets[i]);
Jay Vosburghf35188f2010-07-21 12:14:47 +00002695 if (!bond->vlgrp) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002696 pr_debug("basa: empty vlan: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002697 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2698 bond->master_ip, 0);
2699 continue;
2700 }
2701
2702 /*
2703 * If VLANs are configured, we do a route lookup to
2704 * determine which VLAN interface would be used, so we
2705 * can tag the ARP with the proper VLAN tag.
2706 */
2707 memset(&fl, 0, sizeof(fl));
2708 fl.fl4_dst = targets[i];
2709 fl.fl4_tos = RTO_ONLINK;
2710
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00002711 rv = ip_route_output_key(dev_net(bond->dev), &rt, &fl);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002712 if (rv) {
2713 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002714 pr_warning("%s: no route to arp_ip_target %pI4\n",
2715 bond->dev->name, &fl.fl4_dst);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002716 }
2717 continue;
2718 }
2719
2720 /*
2721 * This target is not on a VLAN
2722 */
Changli Gaod8d1f302010-06-10 23:31:35 -07002723 if (rt->dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002724 ip_rt_put(rt);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002725 pr_debug("basa: rtdev == bond->dev: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002726 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2727 bond->master_ip, 0);
2728 continue;
2729 }
2730
2731 vlan_id = 0;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002732 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002733 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Changli Gaod8d1f302010-06-10 23:31:35 -07002734 if (vlan_dev == rt->dst.dev) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002735 vlan_id = vlan->vlan_id;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002736 pr_debug("basa: vlan match on %s %d\n",
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002737 vlan_dev->name, vlan_id);
2738 break;
2739 }
2740 }
2741
2742 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002743 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002744 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2745 vlan->vlan_ip, vlan_id);
2746 continue;
2747 }
2748
2749 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002750 pr_warning("%s: no path to arp_ip_target %pI4 via rt.dev %s\n",
2751 bond->dev->name, &fl.fl4_dst,
Changli Gaod8d1f302010-06-10 23:31:35 -07002752 rt->dst.dev ? rt->dst.dev->name : "NULL");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002753 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002754 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002755 }
2756}
2757
2758/*
2759 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2760 * for each VLAN above us.
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002761 *
2762 * Caller must hold curr_slave_lock for read or better
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002763 */
2764static void bond_send_gratuitous_arp(struct bonding *bond)
2765{
2766 struct slave *slave = bond->curr_active_slave;
2767 struct vlan_entry *vlan;
2768 struct net_device *vlan_dev;
2769
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002770 pr_debug("bond_send_grat_arp: bond %s slave %s\n",
2771 bond->dev->name, slave ? slave->dev->name : "NULL");
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002772
2773 if (!slave || !bond->send_grat_arp ||
2774 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002775 return;
2776
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002777 bond->send_grat_arp--;
2778
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002779 if (bond->master_ip) {
2780 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
Moni Shoua1053f622007-10-09 19:43:42 -07002781 bond->master_ip, 0);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002782 }
2783
Jay Vosburghf35188f2010-07-21 12:14:47 +00002784 if (!bond->vlgrp)
2785 return;
2786
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002787 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002788 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002789 if (vlan->vlan_ip) {
2790 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2791 vlan->vlan_ip, vlan->vlan_id);
2792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 }
2794}
2795
Al Virod3bb52b2007-08-22 20:06:58 -04002796static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002797{
2798 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002799 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002800
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002801 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002802 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002803 &sip, &tip, i, &targets[i],
2804 bond_has_this_ip(bond, tip));
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002805 if (sip == targets[i]) {
2806 if (bond_has_this_ip(bond, tip))
2807 slave->last_arp_rx = jiffies;
2808 return;
2809 }
2810 }
2811}
2812
2813static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2814{
2815 struct arphdr *arp;
2816 struct slave *slave;
2817 struct bonding *bond;
2818 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002819 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002820
Andy Gospodarek1f3c8802009-12-14 10:48:58 +00002821 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2822 /*
2823 * When using VLANS and bonding, dev and oriv_dev may be
2824 * incorrect if the physical interface supports VLAN
2825 * acceleration. With this change ARP validation now
2826 * works for hosts only reachable on the VLAN interface.
2827 */
2828 dev = vlan_dev_real_dev(dev);
2829 orig_dev = dev_get_by_index_rcu(dev_net(skb->dev),skb->skb_iif);
2830 }
2831
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002832 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2833 goto out;
2834
Wang Chen454d7c92008-11-12 23:37:49 -08002835 bond = netdev_priv(dev);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002836 read_lock(&bond->lock);
2837
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002838 pr_debug("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002839 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2840 orig_dev ? orig_dev->name : "NULL");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002841
2842 slave = bond_get_slave_by_dev(bond, orig_dev);
2843 if (!slave || !slave_do_arp_validate(bond, slave))
2844 goto out_unlock;
2845
Neil Hormanb3053252011-01-20 09:02:31 +00002846 skb = skb_share_check(skb, GFP_ATOMIC);
2847 if (!skb)
2848 goto out_unlock;
2849
Pavel Emelyanov988b7052008-03-03 12:20:57 -08002850 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002851 goto out_unlock;
2852
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002853 arp = arp_hdr(skb);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002854 if (arp->ar_hln != dev->addr_len ||
2855 skb->pkt_type == PACKET_OTHERHOST ||
2856 skb->pkt_type == PACKET_LOOPBACK ||
2857 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2858 arp->ar_pro != htons(ETH_P_IP) ||
2859 arp->ar_pln != 4)
2860 goto out_unlock;
2861
2862 arp_ptr = (unsigned char *)(arp + 1);
2863 arp_ptr += dev->addr_len;
2864 memcpy(&sip, arp_ptr, 4);
2865 arp_ptr += 4 + dev->addr_len;
2866 memcpy(&tip, arp_ptr, 4);
2867
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002868 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002869 bond->dev->name, slave->dev->name, slave->state,
2870 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2871 &sip, &tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002872
2873 /*
2874 * Backup slaves won't see the ARP reply, but do come through
2875 * here for each ARP probe (so we swap the sip/tip to validate
2876 * the probe). In a "redundant switch, common router" type of
2877 * configuration, the ARP probe will (hopefully) travel from
2878 * the active, through one switch, the router, then the other
2879 * switch before reaching the backup.
2880 */
2881 if (slave->state == BOND_STATE_ACTIVE)
2882 bond_validate_arp(bond, slave, sip, tip);
2883 else
2884 bond_validate_arp(bond, slave, tip, sip);
2885
2886out_unlock:
2887 read_unlock(&bond->lock);
2888out:
2889 dev_kfree_skb(skb);
2890 return NET_RX_SUCCESS;
2891}
2892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893/*
2894 * this function is called regularly to monitor each slave's link
2895 * ensuring that traffic is being sent and received when arp monitoring
2896 * is used in load-balancing mode. if the adapter has been dormant, then an
2897 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2898 * arp monitoring in active backup mode.
2899 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002900void bond_loadbalance_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002902 struct bonding *bond = container_of(work, struct bonding,
2903 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 struct slave *slave, *oldcurrent;
2905 int do_failover = 0;
2906 int delta_in_ticks;
2907 int i;
2908
2909 read_lock(&bond->lock);
2910
Jay Vosburgh5ce0da82008-05-17 21:10:07 -07002911 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002913 if (bond->kill_timers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002916 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
2919 read_lock(&bond->curr_slave_lock);
2920 oldcurrent = bond->curr_active_slave;
2921 read_unlock(&bond->curr_slave_lock);
2922
2923 /* see if any of the previous devices are up now (i.e. they have
2924 * xmt and rcv traffic). the curr_active_slave does not come into
2925 * the picture unless it is null. also, slave->jiffies is not needed
2926 * here because we send an arp on each slave and give a slave as
2927 * long as it needs to get the tx/rx within the delta.
2928 * TODO: what about up/down delay in arp mode? it wasn't here before
2929 * so it can wait
2930 */
2931 bond_for_each_slave(bond, slave, i) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002932 unsigned long trans_start = dev_trans_start(slave->dev);
2933
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002935 if (time_in_range(jiffies,
2936 trans_start - delta_in_ticks,
2937 trans_start + delta_in_ticks) &&
2938 time_in_range(jiffies,
2939 slave->dev->last_rx - delta_in_ticks,
2940 slave->dev->last_rx + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
2942 slave->link = BOND_LINK_UP;
2943 slave->state = BOND_STATE_ACTIVE;
2944
2945 /* primary_slave has no meaning in round-robin
2946 * mode. the window of a slave being up and
2947 * curr_active_slave being null after enslaving
2948 * is closed.
2949 */
2950 if (!oldcurrent) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002951 pr_info("%s: link status definitely up for interface %s, ",
2952 bond->dev->name,
2953 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 do_failover = 1;
2955 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002956 pr_info("%s: interface %s is now up\n",
2957 bond->dev->name,
2958 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 }
2960 }
2961 } else {
2962 /* slave->link == BOND_LINK_UP */
2963
2964 /* not all switches will respond to an arp request
2965 * when the source ip is 0, so don't take the link down
2966 * if we don't know our ip yet
2967 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002968 if (!time_in_range(jiffies,
2969 trans_start - delta_in_ticks,
2970 trans_start + 2 * delta_in_ticks) ||
2971 !time_in_range(jiffies,
2972 slave->dev->last_rx - delta_in_ticks,
2973 slave->dev->last_rx + 2 * delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
2975 slave->link = BOND_LINK_DOWN;
2976 slave->state = BOND_STATE_BACKUP;
2977
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002978 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002981 pr_info("%s: interface %s is now down.\n",
2982 bond->dev->name,
2983 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002985 if (slave == oldcurrent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 do_failover = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 }
2988 }
2989
2990 /* note: if switch is in round-robin mode, all links
2991 * must tx arp to ensure all links rx an arp - otherwise
2992 * links may oscillate or not come up at all; if switch is
2993 * in something like xor mode, there is nothing we can
2994 * do - all replies will be rx'ed on same link causing slaves
2995 * to be unstable during low/no traffic periods
2996 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002997 if (IS_UP(slave->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 bond_arp_send_all(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 }
3000
3001 if (do_failover) {
Neil Hormane843fa52010-10-13 16:01:50 +00003002 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07003003 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
3005 bond_select_active_slave(bond);
3006
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07003007 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00003008 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 }
3010
3011re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003012 if (bond->params.arp_interval)
3013 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014out:
3015 read_unlock(&bond->lock);
3016}
3017
3018/*
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003019 * Called to inspect slaves for active-backup mode ARP monitor link state
3020 * changes. Sets new_link in slaves to specify what action should take
3021 * place for the slave. Returns 0 if no changes are found, >0 if changes
3022 * to link states must be committed.
3023 *
3024 * Called with bond->lock held for read.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003026static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 struct slave *slave;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003029 int i, commit = 0;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003030 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 bond_for_each_slave(bond, slave, i) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003033 slave->new_link = BOND_LINK_NOCHANGE;
3034
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003036 if (time_in_range(jiffies,
3037 slave_last_rx(bond, slave) - delta_in_ticks,
3038 slave_last_rx(bond, slave) + delta_in_ticks)) {
3039
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003040 slave->new_link = BOND_LINK_UP;
3041 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003044 continue;
3045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003047 /*
3048 * Give slaves 2*delta after being enslaved or made
3049 * active. This avoids bouncing, as the last receive
3050 * times need a full ARP monitor cycle to be updated.
3051 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003052 if (time_in_range(jiffies,
3053 slave->jiffies - delta_in_ticks,
3054 slave->jiffies + 2 * delta_in_ticks))
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003055 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003057 /*
3058 * Backup slave is down if:
3059 * - No current_arp_slave AND
3060 * - more than 3*delta since last receive AND
3061 * - the bond has an IP address
3062 *
3063 * Note: a non-null current_arp_slave indicates
3064 * the curr_active_slave went down and we are
3065 * searching for a new one; under this condition
3066 * we only take the curr_active_slave down - this
3067 * gives each slave a chance to tx/rx traffic
3068 * before being taken out
3069 */
3070 if (slave->state == BOND_STATE_BACKUP &&
3071 !bond->current_arp_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003072 !time_in_range(jiffies,
3073 slave_last_rx(bond, slave) - delta_in_ticks,
3074 slave_last_rx(bond, slave) + 3 * delta_in_ticks)) {
3075
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003076 slave->new_link = BOND_LINK_DOWN;
3077 commit++;
3078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003080 /*
3081 * Active slave is down if:
3082 * - more than 2*delta since transmitting OR
3083 * - (more than 2*delta since receive AND
3084 * the bond has an IP address)
3085 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003086 trans_start = dev_trans_start(slave->dev);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003087 if ((slave->state == BOND_STATE_ACTIVE) &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003088 (!time_in_range(jiffies,
3089 trans_start - delta_in_ticks,
3090 trans_start + 2 * delta_in_ticks) ||
3091 !time_in_range(jiffies,
3092 slave_last_rx(bond, slave) - delta_in_ticks,
3093 slave_last_rx(bond, slave) + 2 * delta_in_ticks))) {
3094
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003095 slave->new_link = BOND_LINK_DOWN;
3096 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 }
3098 }
3099
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003100 return commit;
3101}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003103/*
3104 * Called to commit link state changes noted by inspection step of
3105 * active-backup mode ARP monitor.
3106 *
3107 * Called with RTNL and bond->lock for read.
3108 */
3109static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
3110{
3111 struct slave *slave;
3112 int i;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003113 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003115 bond_for_each_slave(bond, slave, i) {
3116 switch (slave->new_link) {
3117 case BOND_LINK_NOCHANGE:
3118 continue;
3119
3120 case BOND_LINK_UP:
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003121 trans_start = dev_trans_start(slave->dev);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003122 if ((!bond->curr_active_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003123 time_in_range(jiffies,
3124 trans_start - delta_in_ticks,
3125 trans_start + delta_in_ticks)) ||
Jiri Pirkob9f60252009-08-31 11:09:38 +00003126 bond->curr_active_slave != slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003127 slave->link = BOND_LINK_UP;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003128 bond->current_arp_slave = NULL;
3129
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003130 pr_info("%s: link status definitely up for interface %s.\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003131 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003132
Jiri Pirkob9f60252009-08-31 11:09:38 +00003133 if (!bond->curr_active_slave ||
3134 (slave == bond->primary_slave))
3135 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003136
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003137 }
3138
Jiri Pirkob9f60252009-08-31 11:09:38 +00003139 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003140
3141 case BOND_LINK_DOWN:
3142 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003145 slave->link = BOND_LINK_DOWN;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003146 bond_set_slave_inactive_flags(slave);
3147
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003148 pr_info("%s: link status definitely down for interface %s, disabling it\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003149 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003150
3151 if (slave == bond->curr_active_slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003152 bond->current_arp_slave = NULL;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003153 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003154 }
Jiri Pirkob9f60252009-08-31 11:09:38 +00003155
3156 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003157
3158 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003159 pr_err("%s: impossible: new_link %d on slave %s\n",
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003160 bond->dev->name, slave->new_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 slave->dev->name);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003162 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
Jiri Pirkob9f60252009-08-31 11:09:38 +00003165do_failover:
3166 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00003167 block_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003168 write_lock_bh(&bond->curr_slave_lock);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003169 bond_select_active_slave(bond);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003170 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00003171 unblock_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003172 }
3173
3174 bond_set_carrier(bond);
3175}
3176
3177/*
3178 * Send ARP probes for active-backup mode ARP monitor.
3179 *
3180 * Called with bond->lock held for read.
3181 */
3182static void bond_ab_arp_probe(struct bonding *bond)
3183{
3184 struct slave *slave;
3185 int i;
3186
3187 read_lock(&bond->curr_slave_lock);
3188
3189 if (bond->current_arp_slave && bond->curr_active_slave)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003190 pr_info("PROBE: c_arp %s && cas %s BAD\n",
3191 bond->current_arp_slave->dev->name,
3192 bond->curr_active_slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003193
3194 if (bond->curr_active_slave) {
3195 bond_arp_send_all(bond, bond->curr_active_slave);
3196 read_unlock(&bond->curr_slave_lock);
3197 return;
3198 }
3199
3200 read_unlock(&bond->curr_slave_lock);
3201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 /* if we don't have a curr_active_slave, search for the next available
3203 * backup slave from the current_arp_slave and make it the candidate
3204 * for becoming the curr_active_slave
3205 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003206
3207 if (!bond->current_arp_slave) {
3208 bond->current_arp_slave = bond->first_slave;
3209 if (!bond->current_arp_slave)
3210 return;
3211 }
3212
3213 bond_set_slave_inactive_flags(bond->current_arp_slave);
3214
3215 /* search for next candidate */
3216 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3217 if (IS_UP(slave->dev)) {
3218 slave->link = BOND_LINK_BACK;
3219 bond_set_slave_active_flags(slave);
3220 bond_arp_send_all(bond, slave);
3221 slave->jiffies = jiffies;
3222 bond->current_arp_slave = slave;
3223 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 }
3225
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003226 /* if the link state is up at this point, we
3227 * mark it down - this can happen if we have
3228 * simultaneous link failures and
3229 * reselect_active_interface doesn't make this
3230 * one the current slave so it is still marked
3231 * up when it is actually down
3232 */
3233 if (slave->link == BOND_LINK_UP) {
3234 slave->link = BOND_LINK_DOWN;
3235 if (slave->link_failure_count < UINT_MAX)
3236 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003238 bond_set_slave_inactive_flags(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003240 pr_info("%s: backup interface %s is now down.\n",
3241 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 }
3243 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003244}
3245
3246void bond_activebackup_arp_mon(struct work_struct *work)
3247{
3248 struct bonding *bond = container_of(work, struct bonding,
3249 arp_work.work);
3250 int delta_in_ticks;
3251
3252 read_lock(&bond->lock);
3253
3254 if (bond->kill_timers)
3255 goto out;
3256
3257 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3258
3259 if (bond->slave_cnt == 0)
3260 goto re_arm;
3261
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003262 if (bond->send_grat_arp) {
3263 read_lock(&bond->curr_slave_lock);
3264 bond_send_gratuitous_arp(bond);
3265 read_unlock(&bond->curr_slave_lock);
3266 }
3267
Brian Haley305d5522008-11-04 17:51:14 -08003268 if (bond->send_unsol_na) {
3269 read_lock(&bond->curr_slave_lock);
3270 bond_send_unsolicited_na(bond);
3271 read_unlock(&bond->curr_slave_lock);
3272 }
3273
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003274 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3275 read_unlock(&bond->lock);
3276 rtnl_lock();
3277 read_lock(&bond->lock);
3278
3279 bond_ab_arp_commit(bond, delta_in_ticks);
3280
3281 read_unlock(&bond->lock);
3282 rtnl_unlock();
3283 read_lock(&bond->lock);
3284 }
3285
3286 bond_ab_arp_probe(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
3288re_arm:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003289 if (bond->params.arp_interval)
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003290 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291out:
3292 read_unlock(&bond->lock);
3293}
3294
3295/*------------------------------ proc/seq_file-------------------------------*/
3296
3297#ifdef CONFIG_PROC_FS
3298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazete4a7b932010-10-29 01:52:46 +00003300 __acquires(RCU)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00003301 __acquires(&bond->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302{
3303 struct bonding *bond = seq->private;
3304 loff_t off = 0;
3305 struct slave *slave;
3306 int i;
3307
3308 /* make sure the bond won't be taken away */
Eric Dumazete4a7b932010-10-29 01:52:46 +00003309 rcu_read_lock();
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003310 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003312 if (*pos == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 return SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
3315 bond_for_each_slave(bond, slave, i) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003316 if (++off == *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 return slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 }
3319
3320 return NULL;
3321}
3322
3323static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3324{
3325 struct bonding *bond = seq->private;
3326 struct slave *slave = v;
3327
3328 ++*pos;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003329 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 return bond->first_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
3332 slave = slave->next;
3333
3334 return (slave == bond->first_slave) ? NULL : slave;
3335}
3336
3337static void bond_info_seq_stop(struct seq_file *seq, void *v)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00003338 __releases(&bond->lock)
Eric Dumazete4a7b932010-10-29 01:52:46 +00003339 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340{
3341 struct bonding *bond = seq->private;
3342
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003343 read_unlock(&bond->lock);
Eric Dumazete4a7b932010-10-29 01:52:46 +00003344 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345}
3346
3347static void bond_info_show_master(struct seq_file *seq)
3348{
3349 struct bonding *bond = seq->private;
3350 struct slave *curr;
Mitch Williams4756b022005-11-09 10:36:25 -08003351 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
3353 read_lock(&bond->curr_slave_lock);
3354 curr = bond->curr_active_slave;
3355 read_unlock(&bond->curr_slave_lock);
3356
Jay Vosburghdd957c52007-10-09 19:57:24 -07003357 seq_printf(seq, "Bonding Mode: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 bond_mode_name(bond->params.mode));
3359
Jay Vosburghdd957c52007-10-09 19:57:24 -07003360 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
3361 bond->params.fail_over_mac)
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07003362 seq_printf(seq, " (fail_over_mac %s)",
3363 fail_over_mac_tbl[bond->params.fail_over_mac].modename);
Jay Vosburghdd957c52007-10-09 19:57:24 -07003364
3365 seq_printf(seq, "\n");
3366
Mitch Williamsc61b75a2005-11-09 10:35:13 -08003367 if (bond->params.mode == BOND_MODE_XOR ||
3368 bond->params.mode == BOND_MODE_8023AD) {
3369 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3370 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3371 bond->params.xmit_policy);
3372 }
3373
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 if (USES_PRIMARY(bond->params.mode)) {
Jiri Pirkoa5499522009-09-25 03:28:09 +00003375 seq_printf(seq, "Primary Slave: %s",
Mitch Williams0f418b22005-11-09 10:35:21 -08003376 (bond->primary_slave) ?
3377 bond->primary_slave->dev->name : "None");
Jiri Pirkoa5499522009-09-25 03:28:09 +00003378 if (bond->primary_slave)
3379 seq_printf(seq, " (primary_reselect %s)",
3380 pri_reselect_tbl[bond->params.primary_reselect].modename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
Jiri Pirkoa5499522009-09-25 03:28:09 +00003382 seq_printf(seq, "\nCurrently Active Slave: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 (curr) ? curr->dev->name : "None");
3384 }
3385
Jay Vosburghff59c452006-03-27 13:27:43 -08003386 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3387 "up" : "down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3389 seq_printf(seq, "Up Delay (ms): %d\n",
3390 bond->params.updelay * bond->params.miimon);
3391 seq_printf(seq, "Down Delay (ms): %d\n",
3392 bond->params.downdelay * bond->params.miimon);
3393
Mitch Williams4756b022005-11-09 10:36:25 -08003394
3395 /* ARP information */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003396 if (bond->params.arp_interval > 0) {
3397 int printed = 0;
Mitch Williams4756b022005-11-09 10:36:25 -08003398 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3399 bond->params.arp_interval);
3400
3401 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3402
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003403 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
Mitch Williams4756b022005-11-09 10:36:25 -08003404 if (!bond->params.arp_targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07003405 break;
Mitch Williams4756b022005-11-09 10:36:25 -08003406 if (printed)
3407 seq_printf(seq, ",");
Harvey Harrison8cf14e32008-10-29 22:43:33 -07003408 seq_printf(seq, " %pI4", &bond->params.arp_targets[i]);
Mitch Williams4756b022005-11-09 10:36:25 -08003409 printed = 1;
3410 }
3411 seq_printf(seq, "\n");
3412 }
3413
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 if (bond->params.mode == BOND_MODE_8023AD) {
3415 struct ad_info ad_info;
3416
3417 seq_puts(seq, "\n802.3ad info\n");
3418 seq_printf(seq, "LACP rate: %s\n",
3419 (bond->params.lacp_fast) ? "fast" : "slow");
Jay Vosburghfd989c82008-11-04 17:51:16 -08003420 seq_printf(seq, "Aggregator selection policy (ad_select): %s\n",
3421 ad_select_tbl[bond->params.ad_select].modename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
3423 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3424 seq_printf(seq, "bond %s has no active aggregator\n",
3425 bond->dev->name);
3426 } else {
3427 seq_printf(seq, "Active Aggregator Info:\n");
3428
3429 seq_printf(seq, "\tAggregator ID: %d\n",
3430 ad_info.aggregator_id);
3431 seq_printf(seq, "\tNumber of ports: %d\n",
3432 ad_info.ports);
3433 seq_printf(seq, "\tActor Key: %d\n",
3434 ad_info.actor_key);
3435 seq_printf(seq, "\tPartner Key: %d\n",
3436 ad_info.partner_key);
Johannes Berge1749612008-10-27 15:59:26 -07003437 seq_printf(seq, "\tPartner Mac Address: %pM\n",
3438 ad_info.partner_system);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 }
3440 }
3441}
3442
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003443static void bond_info_show_slave(struct seq_file *seq,
3444 const struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445{
3446 struct bonding *bond = seq->private;
3447
3448 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3449 seq_printf(seq, "MII Status: %s\n",
3450 (slave->link == BOND_LINK_UP) ? "up" : "down");
Krzysztof Oledzkidd53df22010-09-30 06:19:04 +00003451 seq_printf(seq, "Speed: %d Mbps\n", slave->speed);
3452 seq_printf(seq, "Duplex: %s\n", slave->duplex ? "full" : "half");
Kenzo Iwami655096452006-09-22 21:53:08 -07003453 seq_printf(seq, "Link Failure Count: %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 slave->link_failure_count);
3455
Johannes Berge1749612008-10-27 15:59:26 -07003456 seq_printf(seq, "Permanent HW addr: %pM\n", slave->perm_hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
3458 if (bond->params.mode == BOND_MODE_8023AD) {
3459 const struct aggregator *agg
3460 = SLAVE_AD_INFO(slave).port.aggregator;
3461
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003462 if (agg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 seq_printf(seq, "Aggregator ID: %d\n",
3464 agg->aggregator_identifier);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003465 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 seq_puts(seq, "Aggregator ID: N/A\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 }
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00003468 seq_printf(seq, "Slave queue ID: %d\n", slave->queue_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469}
3470
3471static int bond_info_seq_show(struct seq_file *seq, void *v)
3472{
3473 if (v == SEQ_START_TOKEN) {
3474 seq_printf(seq, "%s\n", version);
3475 bond_info_show_master(seq);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003476 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 bond_info_show_slave(seq, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
3479 return 0;
3480}
3481
Jan Engelhardt4101dec2009-01-14 13:52:18 -08003482static const struct seq_operations bond_info_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 .start = bond_info_seq_start,
3484 .next = bond_info_seq_next,
3485 .stop = bond_info_seq_stop,
3486 .show = bond_info_seq_show,
3487};
3488
3489static int bond_info_open(struct inode *inode, struct file *file)
3490{
3491 struct seq_file *seq;
3492 struct proc_dir_entry *proc;
3493 int res;
3494
3495 res = seq_open(file, &bond_info_seq_ops);
3496 if (!res) {
3497 /* recover the pointer buried in proc_dir_entry data */
3498 seq = file->private_data;
3499 proc = PDE(inode);
3500 seq->private = proc->data;
3501 }
3502
3503 return res;
3504}
3505
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08003506static const struct file_operations bond_info_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 .owner = THIS_MODULE,
3508 .open = bond_info_open,
3509 .read = seq_read,
3510 .llseek = seq_lseek,
3511 .release = seq_release,
3512};
3513
Nicolas de Pesloüan38fc0022009-10-13 00:45:06 -07003514static void bond_create_proc_entry(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515{
3516 struct net_device *bond_dev = bond->dev;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003517 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003519 if (bn->proc_dir) {
Denis V. Luneva95609c2008-04-29 01:02:29 -07003520 bond->proc_entry = proc_create_data(bond_dev->name,
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003521 S_IRUGO, bn->proc_dir,
Denis V. Luneva95609c2008-04-29 01:02:29 -07003522 &bond_info_fops, bond);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003523 if (bond->proc_entry == NULL)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003524 pr_warning("Warning: Cannot create /proc/net/%s/%s\n",
3525 DRV_NAME, bond_dev->name);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003526 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529}
3530
3531static void bond_remove_proc_entry(struct bonding *bond)
3532{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003533 struct net_device *bond_dev = bond->dev;
3534 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
3535
3536 if (bn->proc_dir && bond->proc_entry) {
3537 remove_proc_entry(bond->proc_file_name, bn->proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 memset(bond->proc_file_name, 0, IFNAMSIZ);
3539 bond->proc_entry = NULL;
3540 }
3541}
3542
3543/* Create the bonding directory under /proc/net, if doesn't exist yet.
3544 * Caller must hold rtnl_lock.
3545 */
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003546static void __net_init bond_create_proc_dir(struct bond_net *bn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003548 if (!bn->proc_dir) {
3549 bn->proc_dir = proc_mkdir(DRV_NAME, bn->net->proc_net);
3550 if (!bn->proc_dir)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003551 pr_warning("Warning: cannot create /proc/net/%s\n",
3552 DRV_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 }
3554}
3555
3556/* Destroy the bonding directory under /proc/net, if empty.
3557 * Caller must hold rtnl_lock.
3558 */
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003559static void __net_exit bond_destroy_proc_dir(struct bond_net *bn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003561 if (bn->proc_dir) {
3562 remove_proc_entry(DRV_NAME, bn->net->proc_net);
3563 bn->proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 }
3565}
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003566
3567#else /* !CONFIG_PROC_FS */
3568
Nicolas de Pesloüan38fc0022009-10-13 00:45:06 -07003569static void bond_create_proc_entry(struct bonding *bond)
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003570{
3571}
3572
3573static void bond_remove_proc_entry(struct bonding *bond)
3574{
3575}
3576
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003577static inline void bond_create_proc_dir(struct bond_net *bn)
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003578{
3579}
3580
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003581static inline void bond_destroy_proc_dir(struct bond_net *bn)
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003582{
3583}
3584
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585#endif /* CONFIG_PROC_FS */
3586
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003587
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588/*-------------------------- netdev event handling --------------------------*/
3589
3590/*
3591 * Change device name
3592 */
3593static int bond_event_changename(struct bonding *bond)
3594{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 bond_remove_proc_entry(bond);
3596 bond_create_proc_entry(bond);
Stephen Hemminger7e083842009-06-12 19:02:46 +00003597
Taku Izumif073c7c2010-12-09 15:17:13 +00003598 bond_debug_reregister(bond);
3599
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 return NOTIFY_DONE;
3601}
3602
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003603static int bond_master_netdev_event(unsigned long event,
3604 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
Wang Chen454d7c92008-11-12 23:37:49 -08003606 struct bonding *event_bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607
3608 switch (event) {
3609 case NETDEV_CHANGENAME:
3610 return bond_event_changename(event_bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 default:
3612 break;
3613 }
3614
3615 return NOTIFY_DONE;
3616}
3617
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003618static int bond_slave_netdev_event(unsigned long event,
3619 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
3621 struct net_device *bond_dev = slave_dev->master;
Wang Chen454d7c92008-11-12 23:37:49 -08003622 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623
3624 switch (event) {
3625 case NETDEV_UNREGISTER:
3626 if (bond_dev) {
Jay Vosburgh1284cd32007-10-15 16:44:27 -07003627 if (bond->setup_by_slave)
3628 bond_release_and_destroy(bond_dev, slave_dev);
3629 else
3630 bond_release(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 }
3632 break;
3633 case NETDEV_CHANGE:
Jay Vosburgh17d04502009-03-18 18:38:25 -07003634 if (bond->params.mode == BOND_MODE_8023AD || bond_is_lb(bond)) {
3635 struct slave *slave;
3636
3637 slave = bond_get_slave_by_dev(bond, slave_dev);
3638 if (slave) {
3639 u16 old_speed = slave->speed;
3640 u16 old_duplex = slave->duplex;
3641
3642 bond_update_speed_duplex(slave);
3643
3644 if (bond_is_lb(bond))
3645 break;
3646
3647 if (old_speed != slave->speed)
3648 bond_3ad_adapter_speed_changed(slave);
3649 if (old_duplex != slave->duplex)
3650 bond_3ad_adapter_duplex_changed(slave);
3651 }
3652 }
3653
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 break;
3655 case NETDEV_DOWN:
3656 /*
3657 * ... Or is it this?
3658 */
3659 break;
3660 case NETDEV_CHANGEMTU:
3661 /*
3662 * TODO: Should slaves be allowed to
3663 * independently alter their MTU? For
3664 * an active-backup bond, slaves need
3665 * not be the same type of device, so
3666 * MTUs may vary. For other modes,
3667 * slaves arguably should have the
3668 * same MTUs. To do this, we'd need to
3669 * take over the slave's change_mtu
3670 * function for the duration of their
3671 * servitude.
3672 */
3673 break;
3674 case NETDEV_CHANGENAME:
3675 /*
3676 * TODO: handle changing the primary's name
3677 */
3678 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003679 case NETDEV_FEAT_CHANGE:
3680 bond_compute_features(bond);
3681 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 default:
3683 break;
3684 }
3685
3686 return NOTIFY_DONE;
3687}
3688
3689/*
3690 * bond_netdev_event: handle netdev notifier chain events.
3691 *
3692 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003693 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 * locks for us to safely manipulate the slave devices (RTNL lock,
3695 * dev_probe_lock).
3696 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003697static int bond_netdev_event(struct notifier_block *this,
3698 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699{
3700 struct net_device *event_dev = (struct net_device *)ptr;
3701
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003702 pr_debug("event_dev: %s, event: %lx\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003703 event_dev ? event_dev->name : "None",
3704 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003706 if (!(event_dev->priv_flags & IFF_BONDING))
3707 return NOTIFY_DONE;
3708
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 if (event_dev->flags & IFF_MASTER) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003710 pr_debug("IFF_MASTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 return bond_master_netdev_event(event, event_dev);
3712 }
3713
3714 if (event_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003715 pr_debug("IFF_SLAVE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 return bond_slave_netdev_event(event, event_dev);
3717 }
3718
3719 return NOTIFY_DONE;
3720}
3721
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003722/*
3723 * bond_inetaddr_event: handle inetaddr notifier chain events.
3724 *
3725 * We keep track of device IPs primarily to use as source addresses in
3726 * ARP monitor probes (rather than spewing out broadcasts all the time).
3727 *
3728 * We track one IP for the main device (if it has one), plus one per VLAN.
3729 */
3730static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3731{
3732 struct in_ifaddr *ifa = ptr;
3733 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003734 struct bond_net *bn = net_generic(dev_net(event_dev), bond_net_id);
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003735 struct bonding *bond;
3736 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003737
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003738 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003739 if (bond->dev == event_dev) {
3740 switch (event) {
3741 case NETDEV_UP:
3742 bond->master_ip = ifa->ifa_local;
3743 return NOTIFY_OK;
3744 case NETDEV_DOWN:
3745 bond->master_ip = bond_glean_dev_ip(bond->dev);
3746 return NOTIFY_OK;
3747 default:
3748 return NOTIFY_DONE;
3749 }
3750 }
3751
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003752 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +00003753 if (!bond->vlgrp)
3754 continue;
Dan Aloni5c15bde2007-03-02 20:44:51 -08003755 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003756 if (vlan_dev == event_dev) {
3757 switch (event) {
3758 case NETDEV_UP:
3759 vlan->vlan_ip = ifa->ifa_local;
3760 return NOTIFY_OK;
3761 case NETDEV_DOWN:
3762 vlan->vlan_ip =
3763 bond_glean_dev_ip(vlan_dev);
3764 return NOTIFY_OK;
3765 default:
3766 return NOTIFY_DONE;
3767 }
3768 }
3769 }
3770 }
3771 return NOTIFY_DONE;
3772}
3773
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774static struct notifier_block bond_netdev_notifier = {
3775 .notifier_call = bond_netdev_event,
3776};
3777
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003778static struct notifier_block bond_inetaddr_notifier = {
3779 .notifier_call = bond_inetaddr_event,
3780};
3781
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782/*-------------------------- Packet type handling ---------------------------*/
3783
3784/* register to receive lacpdus on a bond */
3785static void bond_register_lacpdu(struct bonding *bond)
3786{
3787 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3788
3789 /* initialize packet type */
3790 pk_type->type = PKT_TYPE_LACPDU;
3791 pk_type->dev = bond->dev;
3792 pk_type->func = bond_3ad_lacpdu_recv;
3793
3794 dev_add_pack(pk_type);
3795}
3796
3797/* unregister to receive lacpdus on a bond */
3798static void bond_unregister_lacpdu(struct bonding *bond)
3799{
3800 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3801}
3802
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003803void bond_register_arp(struct bonding *bond)
3804{
3805 struct packet_type *pt = &bond->arp_mon_pt;
3806
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003807 if (pt->type)
3808 return;
3809
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003810 pt->type = htons(ETH_P_ARP);
Jay Vosburghe245cb72007-02-28 17:03:27 -08003811 pt->dev = bond->dev;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003812 pt->func = bond_arp_rcv;
3813 dev_add_pack(pt);
3814}
3815
3816void bond_unregister_arp(struct bonding *bond)
3817{
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003818 struct packet_type *pt = &bond->arp_mon_pt;
3819
3820 dev_remove_pack(pt);
3821 pt->type = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003822}
3823
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003824/*---------------------------- Hashing Policies -----------------------------*/
3825
3826/*
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003827 * Hash for the output device based upon layer 2 and layer 3 data. If
3828 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3829 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003830static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003831{
3832 struct ethhdr *data = (struct ethhdr *)skb->data;
3833 struct iphdr *iph = ip_hdr(skb);
3834
Brian Haleyf14c4e42008-09-02 10:08:08 -04003835 if (skb->protocol == htons(ETH_P_IP)) {
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003836 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
Jasper Spaansd3da6832009-10-23 04:08:46 +00003837 (data->h_dest[5] ^ data->h_source[5])) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003838 }
3839
Jasper Spaansd3da6832009-10-23 04:08:46 +00003840 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003841}
3842
3843/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003844 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003845 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3846 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3847 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003848static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003849{
3850 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003851 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003852 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003853 int layer4_xor = 0;
3854
Brian Haleyf14c4e42008-09-02 10:08:08 -04003855 if (skb->protocol == htons(ETH_P_IP)) {
3856 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003857 (iph->protocol == IPPROTO_TCP ||
3858 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003859 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003860 }
3861 return (layer4_xor ^
3862 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3863
3864 }
3865
Jasper Spaansd3da6832009-10-23 04:08:46 +00003866 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003867}
3868
3869/*
3870 * Hash for the output device based upon layer 2 data
3871 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003872static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003873{
3874 struct ethhdr *data = (struct ethhdr *)skb->data;
3875
Jasper Spaansd3da6832009-10-23 04:08:46 +00003876 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003877}
3878
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879/*-------------------------- Device entry points ----------------------------*/
3880
3881static int bond_open(struct net_device *bond_dev)
3882{
Wang Chen454d7c92008-11-12 23:37:49 -08003883 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
3885 bond->kill_timers = 0;
3886
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003887 INIT_DELAYED_WORK(&bond->mcast_work, bond_resend_igmp_join_requests_delayed);
3888
Holger Eitzenberger58402052008-12-09 23:07:13 -08003889 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 /* bond_alb_initialize must be called before the timer
3891 * is started.
3892 */
3893 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3894 /* something went wrong - fail the open operation */
stephen hemmingerb4739462010-01-25 23:34:15 +00003895 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 }
3897
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003898 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3899 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 }
3901
3902 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003903 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3904 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 }
3906
3907 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003908 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3909 INIT_DELAYED_WORK(&bond->arp_work,
3910 bond_activebackup_arp_mon);
3911 else
3912 INIT_DELAYED_WORK(&bond->arp_work,
3913 bond_loadbalance_arp_mon);
3914
3915 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003916 if (bond->params.arp_validate)
3917 bond_register_arp(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 }
3919
3920 if (bond->params.mode == BOND_MODE_8023AD) {
Adrian Bunka40745f2007-10-24 18:27:43 +02003921 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003922 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 /* register to receive LACPDUs */
3924 bond_register_lacpdu(bond);
Jay Vosburghfd989c82008-11-04 17:51:16 -08003925 bond_3ad_initiate_agg_selection(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 }
3927
3928 return 0;
3929}
3930
3931static int bond_close(struct net_device *bond_dev)
3932{
Wang Chen454d7c92008-11-12 23:37:49 -08003933 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934
3935 if (bond->params.mode == BOND_MODE_8023AD) {
3936 /* Unregister the receive of LACPDUs */
3937 bond_unregister_lacpdu(bond);
3938 }
3939
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003940 if (bond->params.arp_validate)
3941 bond_unregister_arp(bond);
3942
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 write_lock_bh(&bond->lock);
3944
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003945 bond->send_grat_arp = 0;
Brian Haley305d5522008-11-04 17:51:14 -08003946 bond->send_unsol_na = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
3948 /* signal timers not to re-arm */
3949 bond->kill_timers = 1;
3950
3951 write_unlock_bh(&bond->lock);
3952
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003954 cancel_delayed_work(&bond->mii_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 }
3956
3957 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003958 cancel_delayed_work(&bond->arp_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 }
3960
3961 switch (bond->params.mode) {
3962 case BOND_MODE_8023AD:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003963 cancel_delayed_work(&bond->ad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 break;
3965 case BOND_MODE_TLB:
3966 case BOND_MODE_ALB:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003967 cancel_delayed_work(&bond->alb_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 break;
3969 default:
3970 break;
3971 }
3972
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003973 if (delayed_work_pending(&bond->mcast_work))
3974 cancel_delayed_work(&bond->mcast_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975
Holger Eitzenberger58402052008-12-09 23:07:13 -08003976 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 /* Must be called only after all
3978 * slaves have been released
3979 */
3980 bond_alb_deinitialize(bond);
3981 }
3982
3983 return 0;
3984}
3985
Eric Dumazet28172732010-07-07 14:58:56 -07003986static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3987 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988{
Wang Chen454d7c92008-11-12 23:37:49 -08003989 struct bonding *bond = netdev_priv(bond_dev);
Eric Dumazet28172732010-07-07 14:58:56 -07003990 struct rtnl_link_stats64 temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 struct slave *slave;
3992 int i;
3993
Eric Dumazet28172732010-07-07 14:58:56 -07003994 memset(stats, 0, sizeof(*stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
3996 read_lock_bh(&bond->lock);
3997
3998 bond_for_each_slave(bond, slave, i) {
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00003999 const struct rtnl_link_stats64 *sstats =
Eric Dumazet28172732010-07-07 14:58:56 -07004000 dev_get_stats(slave->dev, &temp);
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08004001
Eric Dumazet28172732010-07-07 14:58:56 -07004002 stats->rx_packets += sstats->rx_packets;
4003 stats->rx_bytes += sstats->rx_bytes;
4004 stats->rx_errors += sstats->rx_errors;
4005 stats->rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
Eric Dumazet28172732010-07-07 14:58:56 -07004007 stats->tx_packets += sstats->tx_packets;
4008 stats->tx_bytes += sstats->tx_bytes;
4009 stats->tx_errors += sstats->tx_errors;
4010 stats->tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011
Eric Dumazet28172732010-07-07 14:58:56 -07004012 stats->multicast += sstats->multicast;
4013 stats->collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
Eric Dumazet28172732010-07-07 14:58:56 -07004015 stats->rx_length_errors += sstats->rx_length_errors;
4016 stats->rx_over_errors += sstats->rx_over_errors;
4017 stats->rx_crc_errors += sstats->rx_crc_errors;
4018 stats->rx_frame_errors += sstats->rx_frame_errors;
4019 stats->rx_fifo_errors += sstats->rx_fifo_errors;
4020 stats->rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
Eric Dumazet28172732010-07-07 14:58:56 -07004022 stats->tx_aborted_errors += sstats->tx_aborted_errors;
4023 stats->tx_carrier_errors += sstats->tx_carrier_errors;
4024 stats->tx_fifo_errors += sstats->tx_fifo_errors;
4025 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
4026 stats->tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 }
4028
4029 read_unlock_bh(&bond->lock);
4030
4031 return stats;
4032}
4033
4034static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
4035{
4036 struct net_device *slave_dev = NULL;
4037 struct ifbond k_binfo;
4038 struct ifbond __user *u_binfo = NULL;
4039 struct ifslave k_sinfo;
4040 struct ifslave __user *u_sinfo = NULL;
4041 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 int res = 0;
4043
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004044 pr_debug("bond_ioctl: master=%s, cmd=%d\n", bond_dev->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
4046 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 case SIOCGMIIPHY:
4048 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004049 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004051
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 mii->phy_id = 0;
4053 /* Fall Through */
4054 case SIOCGMIIREG:
4055 /*
4056 * We do this again just in case we were called by SIOCGMIIREG
4057 * instead of SIOCGMIIPHY.
4058 */
4059 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004060 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004062
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
4064 if (mii->reg_num == 1) {
Wang Chen454d7c92008-11-12 23:37:49 -08004065 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 mii->val_out = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07004067 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 read_lock(&bond->curr_slave_lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004069 if (netif_carrier_ok(bond->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 mii->val_out = BMSR_LSTATUS;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004071
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 read_unlock(&bond->curr_slave_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07004073 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 }
4075
4076 return 0;
4077 case BOND_INFO_QUERY_OLD:
4078 case SIOCBONDINFOQUERY:
4079 u_binfo = (struct ifbond __user *)ifr->ifr_data;
4080
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004081 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
4084 res = bond_info_query(bond_dev, &k_binfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004085 if (res == 0 &&
4086 copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
4087 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
4089 return res;
4090 case BOND_SLAVE_INFO_QUERY_OLD:
4091 case SIOCBONDSLAVEINFOQUERY:
4092 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
4093
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004094 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096
4097 res = bond_slave_info_query(bond_dev, &k_sinfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004098 if (res == 0 &&
4099 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
4100 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
4102 return res;
4103 default:
4104 /* Go on */
4105 break;
4106 }
4107
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004108 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004111 slave_dev = dev_get_by_name(dev_net(bond_dev), ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004113 pr_debug("slave_dev=%p:\n", slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004115 if (!slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 res = -ENODEV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004117 else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004118 pr_debug("slave_dev->name=%s:\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 switch (cmd) {
4120 case BOND_ENSLAVE_OLD:
4121 case SIOCBONDENSLAVE:
4122 res = bond_enslave(bond_dev, slave_dev);
4123 break;
4124 case BOND_RELEASE_OLD:
4125 case SIOCBONDRELEASE:
4126 res = bond_release(bond_dev, slave_dev);
4127 break;
4128 case BOND_SETHWADDR_OLD:
4129 case SIOCBONDSETHWADDR:
4130 res = bond_sethwaddr(bond_dev, slave_dev);
4131 break;
4132 case BOND_CHANGE_ACTIVE_OLD:
4133 case SIOCBONDCHANGEACTIVE:
4134 res = bond_ioctl_change_active(bond_dev, slave_dev);
4135 break;
4136 default:
4137 res = -EOPNOTSUPP;
4138 }
4139
4140 dev_put(slave_dev);
4141 }
4142
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 return res;
4144}
4145
Jiri Pirko22bedad32010-04-01 21:22:57 +00004146static bool bond_addr_in_mc_list(unsigned char *addr,
4147 struct netdev_hw_addr_list *list,
4148 int addrlen)
4149{
4150 struct netdev_hw_addr *ha;
4151
4152 netdev_hw_addr_list_for_each(ha, list)
4153 if (!memcmp(ha->addr, addr, addrlen))
4154 return true;
4155
4156 return false;
4157}
4158
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159static void bond_set_multicast_list(struct net_device *bond_dev)
4160{
Wang Chen454d7c92008-11-12 23:37:49 -08004161 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00004162 struct netdev_hw_addr *ha;
4163 bool found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 /*
4166 * Do promisc before checking multicast_mode
4167 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004168 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07004169 /*
4170 * FIXME: Need to handle the error when one of the multi-slaves
4171 * encounters error.
4172 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 bond_set_promiscuity(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004175
4176 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 bond_set_promiscuity(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004178
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
4180 /* set allmulti flag to slaves */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004181 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07004182 /*
4183 * FIXME: Need to handle the error when one of the multi-slaves
4184 * encounters error.
4185 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 bond_set_allmulti(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004188
4189 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 bond_set_allmulti(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004191
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004193 read_lock(&bond->lock);
4194
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 bond->flags = bond_dev->flags;
4196
4197 /* looking for addresses to add to slaves' mc list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004198 netdev_for_each_mc_addr(ha, bond_dev) {
4199 found = bond_addr_in_mc_list(ha->addr, &bond->mc_list,
4200 bond_dev->addr_len);
4201 if (!found)
4202 bond_mc_add(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 }
4204
4205 /* looking for addresses to delete from slaves' list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004206 netdev_hw_addr_list_for_each(ha, &bond->mc_list) {
4207 found = bond_addr_in_mc_list(ha->addr, &bond_dev->mc,
4208 bond_dev->addr_len);
4209 if (!found)
4210 bond_mc_del(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 }
4212
4213 /* save master's multicast list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004214 __hw_addr_flush(&bond->mc_list);
4215 __hw_addr_add_multiple(&bond->mc_list, &bond_dev->mc,
4216 bond_dev->addr_len, NETDEV_HW_ADDR_T_MULTICAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004218 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219}
4220
Stephen Hemminger00829822008-11-20 20:14:53 -08004221static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
4222{
4223 struct bonding *bond = netdev_priv(dev);
4224 struct slave *slave = bond->first_slave;
4225
4226 if (slave) {
4227 const struct net_device_ops *slave_ops
4228 = slave->dev->netdev_ops;
4229 if (slave_ops->ndo_neigh_setup)
Patrick McHardy72e22402009-03-05 01:57:44 -08004230 return slave_ops->ndo_neigh_setup(slave->dev, parms);
Stephen Hemminger00829822008-11-20 20:14:53 -08004231 }
4232 return 0;
4233}
4234
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235/*
4236 * Change the MTU of all of a master's slaves to match the master
4237 */
4238static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
4239{
Wang Chen454d7c92008-11-12 23:37:49 -08004240 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 struct slave *slave, *stop_at;
4242 int res = 0;
4243 int i;
4244
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004245 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004246 (bond_dev ? bond_dev->name : "None"), new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
4248 /* Can't hold bond->lock with bh disabled here since
4249 * some base drivers panic. On the other hand we can't
4250 * hold bond->lock without bh disabled because we'll
4251 * deadlock. The only solution is to rely on the fact
4252 * that we're under rtnl_lock here, and the slaves
4253 * list won't change. This doesn't solve the problem
4254 * of setting the slave's MTU while it is
4255 * transmitting, but the assumption is that the base
4256 * driver can handle that.
4257 *
4258 * TODO: figure out a way to safely iterate the slaves
4259 * list, but without holding a lock around the actual
4260 * call to the base driver.
4261 */
4262
4263 bond_for_each_slave(bond, slave, i) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004264 pr_debug("s %p s->p %p c_m %p\n",
4265 slave,
4266 slave->prev,
4267 slave->dev->netdev_ops->ndo_change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08004268
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 res = dev_set_mtu(slave->dev, new_mtu);
4270
4271 if (res) {
4272 /* If we failed to set the slave's mtu to the new value
4273 * we must abort the operation even in ACTIVE_BACKUP
4274 * mode, because if we allow the backup slaves to have
4275 * different mtu values than the active slave we'll
4276 * need to change their mtu when doing a failover. That
4277 * means changing their mtu from timer context, which
4278 * is probably not a good idea.
4279 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004280 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 goto unwind;
4282 }
4283 }
4284
4285 bond_dev->mtu = new_mtu;
4286
4287 return 0;
4288
4289unwind:
4290 /* unwind from head to the slave that failed */
4291 stop_at = slave;
4292 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4293 int tmp_res;
4294
4295 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4296 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004297 pr_debug("unwind err %d dev %s\n",
4298 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 }
4300 }
4301
4302 return res;
4303}
4304
4305/*
4306 * Change HW address
4307 *
4308 * Note that many devices must be down to change the HW address, and
4309 * downing the master releases all slaves. We can make bonds full of
4310 * bonding devices to test this, however.
4311 */
4312static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4313{
Wang Chen454d7c92008-11-12 23:37:49 -08004314 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 struct sockaddr *sa = addr, tmp_sa;
4316 struct slave *slave, *stop_at;
4317 int res = 0;
4318 int i;
4319
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004320 if (bond->params.mode == BOND_MODE_ALB)
4321 return bond_alb_set_mac_address(bond_dev, addr);
4322
4323
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004324 pr_debug("bond=%p, name=%s\n",
4325 bond, bond_dev ? bond_dev->name : "None");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326
Jay Vosburghdd957c52007-10-09 19:57:24 -07004327 /*
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004328 * If fail_over_mac is set to active, do nothing and return
4329 * success. Returning an error causes ifenslave to fail.
Jay Vosburghdd957c52007-10-09 19:57:24 -07004330 */
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004331 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
Jay Vosburghdd957c52007-10-09 19:57:24 -07004332 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07004333
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004334 if (!is_valid_ether_addr(sa->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
4337 /* Can't hold bond->lock with bh disabled here since
4338 * some base drivers panic. On the other hand we can't
4339 * hold bond->lock without bh disabled because we'll
4340 * deadlock. The only solution is to rely on the fact
4341 * that we're under rtnl_lock here, and the slaves
4342 * list won't change. This doesn't solve the problem
4343 * of setting the slave's hw address while it is
4344 * transmitting, but the assumption is that the base
4345 * driver can handle that.
4346 *
4347 * TODO: figure out a way to safely iterate the slaves
4348 * list, but without holding a lock around the actual
4349 * call to the base driver.
4350 */
4351
4352 bond_for_each_slave(bond, slave, i) {
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004353 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004354 pr_debug("slave %p %s\n", slave, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004356 if (slave_ops->ndo_set_mac_address == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 res = -EOPNOTSUPP;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004358 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 goto unwind;
4360 }
4361
4362 res = dev_set_mac_address(slave->dev, addr);
4363 if (res) {
4364 /* TODO: consider downing the slave
4365 * and retry ?
4366 * User should expect communications
4367 * breakage anyway until ARP finish
4368 * updating, so...
4369 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004370 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 goto unwind;
4372 }
4373 }
4374
4375 /* success */
4376 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4377 return 0;
4378
4379unwind:
4380 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4381 tmp_sa.sa_family = bond_dev->type;
4382
4383 /* unwind from head to the slave that failed */
4384 stop_at = slave;
4385 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4386 int tmp_res;
4387
4388 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4389 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004390 pr_debug("unwind err %d dev %s\n",
4391 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 }
4393 }
4394
4395 return res;
4396}
4397
4398static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4399{
Wang Chen454d7c92008-11-12 23:37:49 -08004400 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 struct slave *slave, *start_at;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004402 int i, slave_no, res = 1;
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004403 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
4405 read_lock(&bond->lock);
4406
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004407 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 goto out;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004409 /*
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004410 * Start with the curr_active_slave that joined the bond as the
4411 * default for sending IGMP traffic. For failover purposes one
4412 * needs to maintain some consistency for the interface that will
4413 * send the join/membership reports. The curr_active_slave found
4414 * will send all of this type of traffic.
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004415 */
Eric Dumazet00ae7022010-03-30 23:08:37 +00004416 if ((iph->protocol == IPPROTO_IGMP) &&
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004417 (skb->protocol == htons(ETH_P_IP))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004419 read_lock(&bond->curr_slave_lock);
4420 slave = bond->curr_active_slave;
4421 read_unlock(&bond->curr_slave_lock);
4422
4423 if (!slave)
4424 goto out;
4425 } else {
4426 /*
4427 * Concurrent TX may collide on rr_tx_counter; we accept
4428 * that as being rare enough not to justify using an
4429 * atomic op here.
4430 */
4431 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
4432
4433 bond_for_each_slave(bond, slave, i) {
4434 slave_no--;
4435 if (slave_no < 0)
4436 break;
4437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 }
4439
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004440 start_at = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 bond_for_each_slave_from(bond, slave, i, start_at) {
4442 if (IS_UP(slave->dev) &&
4443 (slave->link == BOND_LINK_UP) &&
4444 (slave->state == BOND_STATE_ACTIVE)) {
4445 res = bond_dev_queue_xmit(bond, skb, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 break;
4447 }
4448 }
4449
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450out:
4451 if (res) {
4452 /* no suitable interface, frame not sent */
4453 dev_kfree_skb(skb);
4454 }
4455 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004456 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457}
4458
John W. Linville075897c2005-09-28 17:50:53 -04004459
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460/*
4461 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4462 * the bond has a usable interface.
4463 */
4464static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4465{
Wang Chen454d7c92008-11-12 23:37:49 -08004466 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 int res = 1;
4468
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 read_lock(&bond->lock);
4470 read_lock(&bond->curr_slave_lock);
4471
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004472 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474
John W. Linville075897c2005-09-28 17:50:53 -04004475 if (!bond->curr_active_slave)
4476 goto out;
4477
John W. Linville075897c2005-09-28 17:50:53 -04004478 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4479
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004481 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 /* no suitable interface, frame not sent */
4483 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004484
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 read_unlock(&bond->curr_slave_lock);
4486 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004487 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488}
4489
4490/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004491 * In bond_xmit_xor() , we determine the output device by using a pre-
4492 * determined xmit_hash_policy(), If the selected device is not enabled,
4493 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 */
4495static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4496{
Wang Chen454d7c92008-11-12 23:37:49 -08004497 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 struct slave *slave, *start_at;
4499 int slave_no;
4500 int i;
4501 int res = 1;
4502
4503 read_lock(&bond->lock);
4504
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004505 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
Jasper Spaansa361c832009-10-23 04:09:24 +00004508 slave_no = bond->xmit_hash_policy(skb, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
4510 bond_for_each_slave(bond, slave, i) {
4511 slave_no--;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004512 if (slave_no < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 }
4515
4516 start_at = slave;
4517
4518 bond_for_each_slave_from(bond, slave, i, start_at) {
4519 if (IS_UP(slave->dev) &&
4520 (slave->link == BOND_LINK_UP) &&
4521 (slave->state == BOND_STATE_ACTIVE)) {
4522 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4523 break;
4524 }
4525 }
4526
4527out:
4528 if (res) {
4529 /* no suitable interface, frame not sent */
4530 dev_kfree_skb(skb);
4531 }
4532 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004533 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534}
4535
4536/*
4537 * in broadcast mode, we send everything to all usable interfaces.
4538 */
4539static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4540{
Wang Chen454d7c92008-11-12 23:37:49 -08004541 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 struct slave *slave, *start_at;
4543 struct net_device *tx_dev = NULL;
4544 int i;
4545 int res = 1;
4546
4547 read_lock(&bond->lock);
4548
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004549 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
4552 read_lock(&bond->curr_slave_lock);
4553 start_at = bond->curr_active_slave;
4554 read_unlock(&bond->curr_slave_lock);
4555
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004556 if (!start_at)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558
4559 bond_for_each_slave_from(bond, slave, i, start_at) {
4560 if (IS_UP(slave->dev) &&
4561 (slave->link == BOND_LINK_UP) &&
4562 (slave->state == BOND_STATE_ACTIVE)) {
4563 if (tx_dev) {
4564 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4565 if (!skb2) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004566 pr_err("%s: Error: bond_xmit_broadcast(): skb_clone() failed\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08004567 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 continue;
4569 }
4570
4571 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4572 if (res) {
4573 dev_kfree_skb(skb2);
4574 continue;
4575 }
4576 }
4577 tx_dev = slave->dev;
4578 }
4579 }
4580
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004581 if (tx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 res = bond_dev_queue_xmit(bond, skb, tx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
4584out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004585 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 /* no suitable interface, frame not sent */
4587 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004588
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 /* frame sent to all suitable interfaces */
4590 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004591 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592}
4593
4594/*------------------------- Device initialization ---------------------------*/
4595
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004596static void bond_set_xmit_hash_policy(struct bonding *bond)
4597{
4598 switch (bond->params.xmit_policy) {
4599 case BOND_XMIT_POLICY_LAYER23:
4600 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4601 break;
4602 case BOND_XMIT_POLICY_LAYER34:
4603 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4604 break;
4605 case BOND_XMIT_POLICY_LAYER2:
4606 default:
4607 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4608 break;
4609 }
4610}
4611
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004612/*
4613 * Lookup the slave that corresponds to a qid
4614 */
4615static inline int bond_slave_override(struct bonding *bond,
4616 struct sk_buff *skb)
4617{
4618 int i, res = 1;
4619 struct slave *slave = NULL;
4620 struct slave *check_slave;
4621
4622 read_lock(&bond->lock);
4623
4624 if (!BOND_IS_OK(bond) || !skb->queue_mapping)
4625 goto out;
4626
4627 /* Find out if any slaves have the same mapping as this skb. */
4628 bond_for_each_slave(bond, check_slave, i) {
4629 if (check_slave->queue_id == skb->queue_mapping) {
4630 slave = check_slave;
4631 break;
4632 }
4633 }
4634
4635 /* If the slave isn't UP, use default transmit policy. */
4636 if (slave && slave->queue_id && IS_UP(slave->dev) &&
4637 (slave->link == BOND_LINK_UP)) {
4638 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4639 }
4640
4641out:
4642 read_unlock(&bond->lock);
4643 return res;
4644}
4645
4646static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb)
4647{
4648 /*
4649 * This helper function exists to help dev_pick_tx get the correct
4650 * destination queue. Using a helper function skips the a call to
4651 * skb_tx_hash and will put the skbs in the queue we expect on their
4652 * way down to the bonding driver.
4653 */
4654 return skb->queue_mapping;
4655}
4656
Stephen Hemminger424efe92009-08-31 19:50:51 +00004657static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
Stephen Hemminger00829822008-11-20 20:14:53 -08004658{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004659 struct bonding *bond = netdev_priv(dev);
4660
Neil Hormane843fa52010-10-13 16:01:50 +00004661 /*
4662 * If we risk deadlock from transmitting this in the
4663 * netpoll path, tell netpoll to queue the frame for later tx
4664 */
4665 if (is_netpoll_tx_blocked(dev))
4666 return NETDEV_TX_BUSY;
4667
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004668 if (TX_QUEUE_OVERRIDE(bond->params.mode)) {
4669 if (!bond_slave_override(bond, skb))
4670 return NETDEV_TX_OK;
4671 }
Stephen Hemminger00829822008-11-20 20:14:53 -08004672
4673 switch (bond->params.mode) {
4674 case BOND_MODE_ROUNDROBIN:
4675 return bond_xmit_roundrobin(skb, dev);
4676 case BOND_MODE_ACTIVEBACKUP:
4677 return bond_xmit_activebackup(skb, dev);
4678 case BOND_MODE_XOR:
4679 return bond_xmit_xor(skb, dev);
4680 case BOND_MODE_BROADCAST:
4681 return bond_xmit_broadcast(skb, dev);
4682 case BOND_MODE_8023AD:
4683 return bond_3ad_xmit_xor(skb, dev);
4684 case BOND_MODE_ALB:
4685 case BOND_MODE_TLB:
4686 return bond_alb_xmit(skb, dev);
4687 default:
4688 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004689 pr_err("%s: Error: Unknown bonding mode %d\n",
4690 dev->name, bond->params.mode);
Stephen Hemminger00829822008-11-20 20:14:53 -08004691 WARN_ON_ONCE(1);
4692 dev_kfree_skb(skb);
4693 return NETDEV_TX_OK;
4694 }
4695}
4696
4697
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698/*
4699 * set bond mode specific net device operations
4700 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004701void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004703 struct net_device *bond_dev = bond->dev;
4704
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 switch (mode) {
4706 case BOND_MODE_ROUNDROBIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 break;
4708 case BOND_MODE_ACTIVEBACKUP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 break;
4710 case BOND_MODE_XOR:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004711 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 break;
4713 case BOND_MODE_BROADCAST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 break;
4715 case BOND_MODE_8023AD:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004716 bond_set_master_3ad_flags(bond);
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004717 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004720 bond_set_master_alb_flags(bond);
4721 /* FALLTHRU */
4722 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 break;
4724 default:
4725 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004726 pr_err("%s: Error: Unknown bonding mode %d\n",
4727 bond_dev->name, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 break;
4729 }
4730}
4731
Jay Vosburgh217df672005-09-26 16:11:50 -07004732static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4733 struct ethtool_drvinfo *drvinfo)
4734{
4735 strncpy(drvinfo->driver, DRV_NAME, 32);
4736 strncpy(drvinfo->version, DRV_VERSION, 32);
4737 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4738}
4739
Jeff Garzik7282d492006-09-13 14:30:00 -04004740static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004741 .get_drvinfo = bond_ethtool_get_drvinfo,
Stephen Hemmingerfa53eba2008-09-13 21:17:09 -04004742 .get_link = ethtool_op_get_link,
4743 .get_tx_csum = ethtool_op_get_tx_csum,
4744 .get_sg = ethtool_op_get_sg,
4745 .get_tso = ethtool_op_get_tso,
4746 .get_ufo = ethtool_op_get_ufo,
4747 .get_flags = ethtool_op_get_flags,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004748};
4749
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004750static const struct net_device_ops bond_netdev_ops = {
Stephen Hemminger181470f2009-06-12 19:02:52 +00004751 .ndo_init = bond_init,
Stephen Hemminger9e716262009-06-12 19:02:47 +00004752 .ndo_uninit = bond_uninit,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004753 .ndo_open = bond_open,
4754 .ndo_stop = bond_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08004755 .ndo_start_xmit = bond_start_xmit,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004756 .ndo_select_queue = bond_select_queue,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00004757 .ndo_get_stats64 = bond_get_stats,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004758 .ndo_do_ioctl = bond_do_ioctl,
4759 .ndo_set_multicast_list = bond_set_multicast_list,
4760 .ndo_change_mtu = bond_change_mtu,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004761 .ndo_set_mac_address = bond_set_mac_address,
Stephen Hemminger00829822008-11-20 20:14:53 -08004762 .ndo_neigh_setup = bond_neigh_setup,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004763 .ndo_vlan_rx_register = bond_vlan_rx_register,
4764 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4765 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
WANG Congf6dc31a2010-05-06 00:48:51 -07004766#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00004767 .ndo_netpoll_setup = bond_netpoll_setup,
WANG Congf6dc31a2010-05-06 00:48:51 -07004768 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
4769 .ndo_poll_controller = bond_poll_controller,
4770#endif
Jiri Pirko9232ecc2011-02-13 09:33:01 +00004771 .ndo_add_slave = bond_enslave,
4772 .ndo_del_slave = bond_release,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004773};
4774
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004775static void bond_destructor(struct net_device *bond_dev)
4776{
4777 struct bonding *bond = netdev_priv(bond_dev);
4778 if (bond->wq)
4779 destroy_workqueue(bond->wq);
4780 free_netdev(bond_dev);
4781}
4782
Stephen Hemminger181470f2009-06-12 19:02:52 +00004783static void bond_setup(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784{
Wang Chen454d7c92008-11-12 23:37:49 -08004785 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 /* initialize rwlocks */
4788 rwlock_init(&bond->lock);
4789 rwlock_init(&bond->curr_slave_lock);
4790
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00004791 bond->params = bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
4793 /* Initialize pointers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 bond->dev = bond_dev;
4795 INIT_LIST_HEAD(&bond->vlan_list);
4796
4797 /* Initialize the device entry points */
Stephen Hemminger181470f2009-06-12 19:02:52 +00004798 ether_setup(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004799 bond_dev->netdev_ops = &bond_netdev_ops;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004800 bond_dev->ethtool_ops = &bond_ethtool_ops;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004801 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004803 bond_dev->destructor = bond_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
4805 /* Initialize the device options */
4806 bond_dev->tx_queue_len = 0;
4807 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004808 bond_dev->priv_flags |= IFF_BONDING;
Stephen Hemminger181470f2009-06-12 19:02:52 +00004809 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
4810
Jay Vosburgh6cf3f412008-11-03 18:16:50 -08004811 if (bond->params.arp_interval)
4812 bond_dev->priv_flags |= IFF_MASTER_ARPMON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
4814 /* At first, we block adding VLANs. That's the only way to
4815 * prevent problems that occur when adding VLANs over an
4816 * empty bond. The block will be removed once non-challenged
4817 * slaves are enslaved.
4818 */
4819 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4820
Herbert Xu932ff272006-06-09 12:20:56 -07004821 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 * transmitting */
4823 bond_dev->features |= NETIF_F_LLTX;
4824
4825 /* By default, we declare the bond to be fully
4826 * VLAN hardware accelerated capable. Special
4827 * care is taken in the various xmit functions
4828 * when there are slaves that are not hw accel
4829 * capable
4830 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4832 NETIF_F_HW_VLAN_RX |
4833 NETIF_F_HW_VLAN_FILTER);
4834
Eric Dumazete6599c22010-09-17 09:25:07 +00004835 /* By default, we enable GRO on bonding devices.
4836 * Actual support requires lowlevel drivers are GRO ready.
4837 */
4838 bond_dev->features |= NETIF_F_GRO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839}
4840
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004841static void bond_work_cancel_all(struct bonding *bond)
4842{
4843 write_lock_bh(&bond->lock);
4844 bond->kill_timers = 1;
4845 write_unlock_bh(&bond->lock);
4846
4847 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4848 cancel_delayed_work(&bond->mii_work);
4849
4850 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4851 cancel_delayed_work(&bond->arp_work);
4852
4853 if (bond->params.mode == BOND_MODE_ALB &&
4854 delayed_work_pending(&bond->alb_work))
4855 cancel_delayed_work(&bond->alb_work);
4856
4857 if (bond->params.mode == BOND_MODE_8023AD &&
4858 delayed_work_pending(&bond->ad_work))
4859 cancel_delayed_work(&bond->ad_work);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00004860
4861 if (delayed_work_pending(&bond->mcast_work))
4862 cancel_delayed_work(&bond->mcast_work);
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004863}
4864
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004865/*
4866* Destroy a bonding device.
4867* Must be under rtnl_lock when this function is called.
4868*/
4869static void bond_uninit(struct net_device *bond_dev)
Jay Vosburgha434e432008-10-30 17:41:15 -07004870{
Wang Chen454d7c92008-11-12 23:37:49 -08004871 struct bonding *bond = netdev_priv(bond_dev);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004872 struct vlan_entry *vlan, *tmp;
Jay Vosburgha434e432008-10-30 17:41:15 -07004873
WANG Congf6dc31a2010-05-06 00:48:51 -07004874 bond_netpoll_cleanup(bond_dev);
4875
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004876 /* Release the bonded slaves */
4877 bond_release_all(bond_dev);
4878
Jay Vosburgha434e432008-10-30 17:41:15 -07004879 list_del(&bond->bond_list);
4880
4881 bond_work_cancel_all(bond);
4882
Jay Vosburgha434e432008-10-30 17:41:15 -07004883 bond_remove_proc_entry(bond);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004884
Taku Izumif073c7c2010-12-09 15:17:13 +00004885 bond_debug_unregister(bond);
4886
Jiri Pirko22bedad32010-04-01 21:22:57 +00004887 __hw_addr_flush(&bond->mc_list);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004888
4889 list_for_each_entry_safe(vlan, tmp, &bond->vlan_list, vlan_list) {
4890 list_del(&vlan->vlan_list);
4891 kfree(vlan);
4892 }
Jay Vosburgha434e432008-10-30 17:41:15 -07004893}
4894
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895/*------------------------- Module initialization ---------------------------*/
4896
4897/*
4898 * Convert string input module parms. Accept either the
Jay Vosburghece95f72008-01-17 16:25:01 -08004899 * number of the mode or its string name. A bit complicated because
4900 * some mode names are substrings of other names, and calls from sysfs
4901 * may have whitespace in the name (trailing newlines, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 */
Holger Eitzenberger325dcf72008-12-09 23:10:17 -08004903int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904{
Hannes Eder54b87322009-02-14 11:15:49 +00004905 int modeint = -1, i, rv;
Jay Vosburgha42e5342008-01-29 18:07:43 -08004906 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
Jay Vosburghece95f72008-01-17 16:25:01 -08004907
Jay Vosburgha42e5342008-01-29 18:07:43 -08004908 for (p = (char *)buf; *p; p++)
4909 if (!(isdigit(*p) || isspace(*p)))
4910 break;
4911
4912 if (*p)
Jay Vosburghece95f72008-01-17 16:25:01 -08004913 rv = sscanf(buf, "%20s", modestr);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004914 else
Hannes Eder54b87322009-02-14 11:15:49 +00004915 rv = sscanf(buf, "%d", &modeint);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004916
4917 if (!rv)
4918 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919
4920 for (i = 0; tbl[i].modename; i++) {
Hannes Eder54b87322009-02-14 11:15:49 +00004921 if (modeint == tbl[i].mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 return tbl[i].mode;
Jay Vosburghece95f72008-01-17 16:25:01 -08004923 if (strcmp(modestr, tbl[i].modename) == 0)
4924 return tbl[i].mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 }
4926
4927 return -1;
4928}
4929
4930static int bond_check_params(struct bond_params *params)
4931{
Jiri Pirkoa5499522009-09-25 03:28:09 +00004932 int arp_validate_value, fail_over_mac_value, primary_reselect_value;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004933
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 /*
4935 * Convert string parameters.
4936 */
4937 if (mode) {
4938 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4939 if (bond_mode == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004940 pr_err("Error: Invalid bonding mode \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 mode == NULL ? "NULL" : mode);
4942 return -EINVAL;
4943 }
4944 }
4945
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004946 if (xmit_hash_policy) {
4947 if ((bond_mode != BOND_MODE_XOR) &&
4948 (bond_mode != BOND_MODE_8023AD)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004949 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004950 bond_mode_name(bond_mode));
4951 } else {
4952 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4953 xmit_hashtype_tbl);
4954 if (xmit_hashtype == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004955 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004956 xmit_hash_policy == NULL ? "NULL" :
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004957 xmit_hash_policy);
4958 return -EINVAL;
4959 }
4960 }
4961 }
4962
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 if (lacp_rate) {
4964 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004965 pr_info("lacp_rate param is irrelevant in mode %s\n",
4966 bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 } else {
4968 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4969 if (lacp_fast == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004970 pr_err("Error: Invalid lacp rate \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 lacp_rate == NULL ? "NULL" : lacp_rate);
4972 return -EINVAL;
4973 }
4974 }
4975 }
4976
Jay Vosburghfd989c82008-11-04 17:51:16 -08004977 if (ad_select) {
4978 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4979 if (params->ad_select == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004980 pr_err("Error: Invalid ad_select \"%s\"\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -08004981 ad_select == NULL ? "NULL" : ad_select);
4982 return -EINVAL;
4983 }
4984
4985 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004986 pr_warning("ad_select param only affects 802.3ad mode\n");
Jay Vosburghfd989c82008-11-04 17:51:16 -08004987 }
4988 } else {
4989 params->ad_select = BOND_AD_STABLE;
4990 }
4991
Nicolas de Pesloüanf5841302009-08-28 13:18:34 +00004992 if (max_bonds < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004993 pr_warning("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4994 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 max_bonds = BOND_DEFAULT_MAX_BONDS;
4996 }
4997
4998 if (miimon < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004999 pr_warning("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to %d\n",
5000 miimon, INT_MAX, BOND_LINK_MON_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 miimon = BOND_LINK_MON_INTERV;
5002 }
5003
5004 if (updelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005005 pr_warning("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
5006 updelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 updelay = 0;
5008 }
5009
5010 if (downdelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005011 pr_warning("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
5012 downdelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 downdelay = 0;
5014 }
5015
5016 if ((use_carrier != 0) && (use_carrier != 1)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005017 pr_warning("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
5018 use_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 use_carrier = 1;
5020 }
5021
Moni Shoua7893b242008-05-17 21:10:12 -07005022 if (num_grat_arp < 0 || num_grat_arp > 255) {
Frans Pop2381a552010-03-24 07:57:36 +00005023 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 -08005024 num_grat_arp);
Moni Shoua7893b242008-05-17 21:10:12 -07005025 num_grat_arp = 1;
5026 }
5027
Brian Haley305d5522008-11-04 17:51:14 -08005028 if (num_unsol_na < 0 || num_unsol_na > 255) {
Frans Pop2381a552010-03-24 07:57:36 +00005029 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 -08005030 num_unsol_na);
Brian Haley305d5522008-11-04 17:51:14 -08005031 num_unsol_na = 1;
5032 }
5033
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 /* reset values for 802.3ad */
5035 if (bond_mode == BOND_MODE_8023AD) {
5036 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005037 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 +00005038 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 miimon = 100;
5040 }
5041 }
5042
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005043 if (tx_queues < 1 || tx_queues > 255) {
5044 pr_warning("Warning: tx_queues (%d) should be between "
5045 "1 and 255, resetting to %d\n",
5046 tx_queues, BOND_DEFAULT_TX_QUEUES);
5047 tx_queues = BOND_DEFAULT_TX_QUEUES;
5048 }
5049
Andy Gospodarekebd8e492010-06-02 08:39:21 +00005050 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
5051 pr_warning("Warning: all_slaves_active module parameter (%d), "
5052 "not of valid value (0/1), so it was set to "
5053 "0\n", all_slaves_active);
5054 all_slaves_active = 0;
5055 }
5056
Flavio Leitnerc2952c32010-10-05 14:23:59 +00005057 if (resend_igmp < 0 || resend_igmp > 255) {
5058 pr_warning("Warning: resend_igmp (%d) should be between "
5059 "0 and 255, resetting to %d\n",
5060 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
5061 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
5062 }
5063
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 /* reset values for TLB/ALB */
5065 if ((bond_mode == BOND_MODE_TLB) ||
5066 (bond_mode == BOND_MODE_ALB)) {
5067 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005068 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 +00005069 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 miimon = 100;
5071 }
5072 }
5073
5074 if (bond_mode == BOND_MODE_ALB) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005075 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",
5076 updelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 }
5078
5079 if (!miimon) {
5080 if (updelay || downdelay) {
5081 /* just warn the user the up/down delay will have
5082 * no effect since miimon is zero...
5083 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005084 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",
5085 updelay, downdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 }
5087 } else {
5088 /* don't allow arp monitoring */
5089 if (arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005090 pr_warning("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
5091 miimon, arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 arp_interval = 0;
5093 }
5094
5095 if ((updelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005096 pr_warning("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
5097 updelay, miimon,
5098 (updelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 }
5100
5101 updelay /= miimon;
5102
5103 if ((downdelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005104 pr_warning("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
5105 downdelay, miimon,
5106 (downdelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 }
5108
5109 downdelay /= miimon;
5110 }
5111
5112 if (arp_interval < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005113 pr_warning("Warning: arp_interval module parameter (%d) , not in range 0-%d, so it was reset to %d\n",
5114 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 arp_interval = BOND_LINK_ARP_INTERV;
5116 }
5117
5118 for (arp_ip_count = 0;
5119 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
5120 arp_ip_count++) {
5121 /* not complete check, but should be good enough to
5122 catch mistakes */
5123 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005124 pr_warning("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
5125 arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 arp_interval = 0;
5127 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04005128 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 arp_target[arp_ip_count] = ip;
5130 }
5131 }
5132
5133 if (arp_interval && !arp_ip_count) {
5134 /* don't allow arping if no arp_ip_target given... */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005135 pr_warning("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
5136 arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 arp_interval = 0;
5138 }
5139
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005140 if (arp_validate) {
5141 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005142 pr_err("arp_validate only supported in active-backup mode\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005143 return -EINVAL;
5144 }
5145 if (!arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005146 pr_err("arp_validate requires arp_interval\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005147 return -EINVAL;
5148 }
5149
5150 arp_validate_value = bond_parse_parm(arp_validate,
5151 arp_validate_tbl);
5152 if (arp_validate_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005153 pr_err("Error: invalid arp_validate \"%s\"\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005154 arp_validate == NULL ? "NULL" : arp_validate);
5155 return -EINVAL;
5156 }
5157 } else
5158 arp_validate_value = 0;
5159
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 if (miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005161 pr_info("MII link monitoring set to %d ms\n", miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 } else if (arp_interval) {
5163 int i;
5164
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005165 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
5166 arp_interval,
5167 arp_validate_tbl[arp_validate_value].modename,
5168 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169
5170 for (i = 0; i < arp_ip_count; i++)
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00005171 pr_info(" %s", arp_ip_target[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00005173 pr_info("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174
Jay Vosburghb8a97872008-06-13 18:12:04 -07005175 } else if (max_bonds) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 /* miimon and arp_interval not set, we need one so things
5177 * work as expected, see bonding.txt for details
5178 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005179 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 -07005180 }
5181
5182 if (primary && !USES_PRIMARY(bond_mode)) {
5183 /* currently, using a primary only makes sense
5184 * in active backup, TLB or ALB modes
5185 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005186 pr_warning("Warning: %s primary device specified but has no effect in %s mode\n",
5187 primary, bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 primary = NULL;
5189 }
5190
Jiri Pirkoa5499522009-09-25 03:28:09 +00005191 if (primary && primary_reselect) {
5192 primary_reselect_value = bond_parse_parm(primary_reselect,
5193 pri_reselect_tbl);
5194 if (primary_reselect_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005195 pr_err("Error: Invalid primary_reselect \"%s\"\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00005196 primary_reselect ==
5197 NULL ? "NULL" : primary_reselect);
5198 return -EINVAL;
5199 }
5200 } else {
5201 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
5202 }
5203
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005204 if (fail_over_mac) {
5205 fail_over_mac_value = bond_parse_parm(fail_over_mac,
5206 fail_over_mac_tbl);
5207 if (fail_over_mac_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005208 pr_err("Error: invalid fail_over_mac \"%s\"\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005209 arp_validate == NULL ? "NULL" : arp_validate);
5210 return -EINVAL;
5211 }
5212
5213 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005214 pr_warning("Warning: fail_over_mac only affects active-backup mode.\n");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005215 } else {
5216 fail_over_mac_value = BOND_FOM_NONE;
5217 }
Jay Vosburghdd957c52007-10-09 19:57:24 -07005218
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 /* fill params struct with the proper values */
5220 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04005221 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 params->miimon = miimon;
Moni Shoua7893b242008-05-17 21:10:12 -07005223 params->num_grat_arp = num_grat_arp;
Brian Haley305d5522008-11-04 17:51:14 -08005224 params->num_unsol_na = num_unsol_na;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005226 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 params->updelay = updelay;
5228 params->downdelay = downdelay;
5229 params->use_carrier = use_carrier;
5230 params->lacp_fast = lacp_fast;
5231 params->primary[0] = 0;
Jiri Pirkoa5499522009-09-25 03:28:09 +00005232 params->primary_reselect = primary_reselect_value;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005233 params->fail_over_mac = fail_over_mac_value;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005234 params->tx_queues = tx_queues;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00005235 params->all_slaves_active = all_slaves_active;
Flavio Leitnerc2952c32010-10-05 14:23:59 +00005236 params->resend_igmp = resend_igmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237
5238 if (primary) {
5239 strncpy(params->primary, primary, IFNAMSIZ);
5240 params->primary[IFNAMSIZ - 1] = 0;
5241 }
5242
5243 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
5244
5245 return 0;
5246}
5247
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005248static struct lock_class_key bonding_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -07005249static struct lock_class_key bonding_netdev_addr_lock_key;
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005250
David S. Millere8a04642008-07-17 00:34:19 -07005251static void bond_set_lockdep_class_one(struct net_device *dev,
5252 struct netdev_queue *txq,
5253 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -07005254{
5255 lockdep_set_class(&txq->_xmit_lock,
5256 &bonding_netdev_xmit_lock_key);
5257}
5258
5259static void bond_set_lockdep_class(struct net_device *dev)
5260{
David S. Millercf508b12008-07-22 14:16:42 -07005261 lockdep_set_class(&dev->addr_list_lock,
5262 &bonding_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -07005263 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -07005264}
5265
Stephen Hemminger181470f2009-06-12 19:02:52 +00005266/*
5267 * Called from registration process
5268 */
5269static int bond_init(struct net_device *bond_dev)
5270{
5271 struct bonding *bond = netdev_priv(bond_dev);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005272 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005273
5274 pr_debug("Begin bond_init for %s\n", bond_dev->name);
5275
5276 bond->wq = create_singlethread_workqueue(bond_dev->name);
5277 if (!bond->wq)
5278 return -ENOMEM;
5279
5280 bond_set_lockdep_class(bond_dev);
5281
5282 netif_carrier_off(bond_dev);
5283
5284 bond_create_proc_entry(bond);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005285 list_add_tail(&bond->bond_list, &bn->dev_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005286
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00005287 bond_prepare_sysfs_group(bond);
Jiri Pirko22bedad32010-04-01 21:22:57 +00005288
Taku Izumif073c7c2010-12-09 15:17:13 +00005289 bond_debug_register(bond);
5290
Jiri Pirko22bedad32010-04-01 21:22:57 +00005291 __hw_addr_init(&bond->mc_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005292 return 0;
5293}
5294
Eric W. Biederman88ead972009-10-29 14:18:25 +00005295static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
5296{
5297 if (tb[IFLA_ADDRESS]) {
5298 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
5299 return -EINVAL;
5300 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
5301 return -EADDRNOTAVAIL;
5302 }
5303 return 0;
5304}
5305
5306static struct rtnl_link_ops bond_link_ops __read_mostly = {
5307 .kind = "bond",
Eric W. Biederman66391042009-10-29 23:58:54 +00005308 .priv_size = sizeof(struct bonding),
Eric W. Biederman88ead972009-10-29 14:18:25 +00005309 .setup = bond_setup,
5310 .validate = bond_validate,
5311};
5312
Mitch Williamsdfe60392005-11-09 10:36:04 -08005313/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005314 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08005315 * Caller must NOT hold rtnl_lock; we need to release it here before we
5316 * set up our sysfs entries.
5317 */
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005318int bond_create(struct net *net, const char *name)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005319{
5320 struct net_device *bond_dev;
5321 int res;
5322
5323 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08005324
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005325 bond_dev = alloc_netdev_mq(sizeof(struct bonding), name ? name : "",
5326 bond_setup, tx_queues);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005327 if (!bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005328 pr_err("%s: eek! can't alloc netdev!\n", name);
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005329 rtnl_unlock();
5330 return -ENOMEM;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005331 }
5332
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005333 dev_net_set(bond_dev, net);
Eric W. Biederman88ead972009-10-29 14:18:25 +00005334 bond_dev->rtnl_link_ops = &bond_link_ops;
5335
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005336 if (!name) {
5337 res = dev_alloc_name(bond_dev, "bond%d");
5338 if (res < 0)
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005339 goto out;
Neil Horman27e6f062010-10-05 03:39:21 +00005340 } else {
5341 /*
5342 * If we're given a name to register
5343 * we need to ensure that its not already
5344 * registered
5345 */
5346 res = -EEXIST;
5347 if (__dev_get_by_name(net, name) != NULL)
5348 goto out;
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005349 }
5350
Mitch Williamsdfe60392005-11-09 10:36:04 -08005351 res = register_netdevice(bond_dev);
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005352
Eric W. Biederman30c15ba2009-10-29 14:18:23 +00005353out:
Mitch Williamsdfe60392005-11-09 10:36:04 -08005354 rtnl_unlock();
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005355 if (res < 0)
5356 bond_destructor(bond_dev);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005357 return res;
5358}
5359
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005360static int __net_init bond_net_init(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005361{
Eric W. Biederman15449742009-11-29 15:46:04 +00005362 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005363
5364 bn->net = net;
5365 INIT_LIST_HEAD(&bn->dev_list);
5366
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005367 bond_create_proc_dir(bn);
Eric W. Biederman15449742009-11-29 15:46:04 +00005368
5369 return 0;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005370}
5371
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005372static void __net_exit bond_net_exit(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005373{
Eric W. Biederman15449742009-11-29 15:46:04 +00005374 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005375
5376 bond_destroy_proc_dir(bn);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005377}
5378
5379static struct pernet_operations bond_net_ops = {
5380 .init = bond_net_init,
5381 .exit = bond_net_exit,
Eric W. Biederman15449742009-11-29 15:46:04 +00005382 .id = &bond_net_id,
5383 .size = sizeof(struct bond_net),
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005384};
5385
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386static int __init bonding_init(void)
5387{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 int i;
5389 int res;
5390
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005391 pr_info("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392
Mitch Williamsdfe60392005-11-09 10:36:04 -08005393 res = bond_check_params(&bonding_defaults);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005394 if (res)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005395 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396
Eric W. Biederman15449742009-11-29 15:46:04 +00005397 res = register_pernet_subsys(&bond_net_ops);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005398 if (res)
5399 goto out;
Jay Vosburgh027ea042008-01-17 16:25:02 -08005400
Eric W. Biederman88ead972009-10-29 14:18:25 +00005401 res = rtnl_link_register(&bond_link_ops);
5402 if (res)
Eric W. Biederman66391042009-10-29 23:58:54 +00005403 goto err_link;
Eric W. Biederman88ead972009-10-29 14:18:25 +00005404
Taku Izumif073c7c2010-12-09 15:17:13 +00005405 bond_create_debugfs();
5406
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 for (i = 0; i < max_bonds; i++) {
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005408 res = bond_create(&init_net, NULL);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005409 if (res)
5410 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 }
5412
Mitch Williamsb76cdba2005-11-09 10:36:41 -08005413 res = bond_create_sysfs();
5414 if (res)
5415 goto err;
5416
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005418 register_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005419 bond_register_ipv6_notifier();
Mitch Williamsdfe60392005-11-09 10:36:04 -08005420out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 return res;
Eric W. Biederman88ead972009-10-29 14:18:25 +00005422err:
5423 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman66391042009-10-29 23:58:54 +00005424err_link:
Eric W. Biederman15449742009-11-29 15:46:04 +00005425 unregister_pernet_subsys(&bond_net_ops);
Eric W. Biederman88ead972009-10-29 14:18:25 +00005426 goto out;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005427
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428}
5429
5430static void __exit bonding_exit(void)
5431{
5432 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005433 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005434 bond_unregister_ipv6_notifier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005436 bond_destroy_sysfs();
Taku Izumif073c7c2010-12-09 15:17:13 +00005437 bond_destroy_debugfs();
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005438
Eric W. Biederman88ead972009-10-29 14:18:25 +00005439 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman15449742009-11-29 15:46:04 +00005440 unregister_pernet_subsys(&bond_net_ops);
Neil Hormane843fa52010-10-13 16:01:50 +00005441
5442#ifdef CONFIG_NET_POLL_CONTROLLER
Neil Hormanfb4fa762010-12-06 09:05:50 +00005443 /*
5444 * Make sure we don't have an imbalance on our netpoll blocking
5445 */
5446 WARN_ON(atomic_read(&netpoll_block_tx));
Neil Hormane843fa52010-10-13 16:01:50 +00005447#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448}
5449
5450module_init(bonding_init);
5451module_exit(bonding_exit);
5452MODULE_LICENSE("GPL");
5453MODULE_VERSION(DRV_VERSION);
5454MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5455MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
Eric W. Biederman88ead972009-10-29 14:18:25 +00005456MODULE_ALIAS_RTNL_LINK("bond");