blob: 2ed662464cacd59fbc0ad243e8a90e2c36c8f9a6 [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 Wang8a8efa22011-02-17 23:43:32 +0000426 if (unlikely(netpoll_tx_running(slave_dev))) {
WANG Congf6dc31a2010-05-06 00:48:51 -0700427 slave_dev->priv_flags |= IFF_IN_NETPOLL;
Amerigo Wang8a8efa22011-02-17 23:43:32 +0000428 bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
WANG Congf6dc31a2010-05-06 00:48:51 -0700429 slave_dev->priv_flags &= ~IFF_IN_NETPOLL;
WANG Congf6dc31a2010-05-06 00:48:51 -0700430 } else
WANG Congf6dc31a2010-05-06 00:48:51 -0700431 dev_queue_xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 return 0;
434}
435
436/*
437 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
438 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
439 * lock because:
440 * a. This operation is performed in IOCTL context,
441 * b. The operation is protected by the RTNL semaphore in the 8021q code,
442 * c. Holding a lock with BH disabled while directly calling a base driver
443 * entry point is generally a BAD idea.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000444 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 * The design of synchronization/protection for this operation in the 8021q
446 * module is good for one or more VLAN devices over a single physical device
447 * and cannot be extended for a teaming solution like bonding, so there is a
448 * potential race condition here where a net device from the vlan group might
449 * be referenced (either by a base driver or the 8021q code) while it is being
450 * removed from the system. However, it turns out we're not making matters
451 * worse, and if it works for regular VLAN usage it will work here too.
452*/
453
454/**
455 * bond_vlan_rx_register - Propagates registration to slaves
456 * @bond_dev: bonding net device that got called
457 * @grp: vlan group being registered
458 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000459static void bond_vlan_rx_register(struct net_device *bond_dev,
460 struct vlan_group *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Wang Chen454d7c92008-11-12 23:37:49 -0800462 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct slave *slave;
464 int i;
465
Jarek Poplawskia71fb882010-10-27 07:08:22 +0000466 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 bond->vlgrp = grp;
Jarek Poplawskia71fb882010-10-27 07:08:22 +0000468 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 bond_for_each_slave(bond, slave, i) {
471 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800472 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800475 slave_ops->ndo_vlan_rx_register) {
476 slave_ops->ndo_vlan_rx_register(slave_dev, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478 }
479}
480
481/**
482 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
483 * @bond_dev: bonding net device that got called
484 * @vid: vlan id being added
485 */
486static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
487{
Wang Chen454d7c92008-11-12 23:37:49 -0800488 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 struct slave *slave;
490 int i, res;
491
492 bond_for_each_slave(bond, slave, i) {
493 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800494 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800497 slave_ops->ndo_vlan_rx_add_vid) {
498 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500 }
501
502 res = bond_add_vlan(bond, vid);
503 if (res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800504 pr_err("%s: Error: Failed to add vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 bond_dev->name, vid);
506 }
507}
508
509/**
510 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
511 * @bond_dev: bonding net device that got called
512 * @vid: vlan id being removed
513 */
514static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
515{
Wang Chen454d7c92008-11-12 23:37:49 -0800516 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 struct slave *slave;
518 struct net_device *vlan_dev;
519 int i, res;
520
521 bond_for_each_slave(bond, slave, i) {
522 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800523 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800526 slave_ops->ndo_vlan_rx_kill_vid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /* Save and then restore vlan_dev in the grp array,
528 * since the slave's driver might clear it.
529 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800530 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800531 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vid);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800532 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534 }
535
536 res = bond_del_vlan(bond, vid);
537 if (res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800538 pr_err("%s: Error: Failed to remove vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 bond_dev->name, vid);
540 }
541}
542
543static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
544{
545 struct vlan_entry *vlan;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800546 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Jay Vosburghf35188f2010-07-21 12:14:47 +0000548 if (!bond->vlgrp)
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000549 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800552 slave_ops->ndo_vlan_rx_register)
553 slave_ops->ndo_vlan_rx_register(slave_dev, bond->vlgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800556 !(slave_ops->ndo_vlan_rx_add_vid))
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000557 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800559 list_for_each_entry(vlan, &bond->vlan_list, vlan_list)
560 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vlan->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000563static void bond_del_vlans_from_slave(struct bonding *bond,
564 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800566 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 struct vlan_entry *vlan;
568 struct net_device *vlan_dev;
569
Jay Vosburghf35188f2010-07-21 12:14:47 +0000570 if (!bond->vlgrp)
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000571 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800574 !(slave_ops->ndo_vlan_rx_kill_vid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 goto unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +0000578 if (!vlan->vlan_id)
579 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /* Save and then restore vlan_dev in the grp array,
581 * since the slave's driver might clear it.
582 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800583 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800584 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800585 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587
588unreg:
589 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800590 slave_ops->ndo_vlan_rx_register)
591 slave_ops->ndo_vlan_rx_register(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594/*------------------------------- Link status -------------------------------*/
595
596/*
Jay Vosburghff59c452006-03-27 13:27:43 -0800597 * Set the carrier state for the master according to the state of its
598 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
599 * do special 802.3ad magic.
600 *
601 * Returns zero if carrier state does not change, nonzero if it does.
602 */
603static int bond_set_carrier(struct bonding *bond)
604{
605 struct slave *slave;
606 int i;
607
608 if (bond->slave_cnt == 0)
609 goto down;
610
611 if (bond->params.mode == BOND_MODE_8023AD)
612 return bond_3ad_set_carrier(bond);
613
614 bond_for_each_slave(bond, slave, i) {
615 if (slave->link == BOND_LINK_UP) {
616 if (!netif_carrier_ok(bond->dev)) {
617 netif_carrier_on(bond->dev);
618 return 1;
619 }
620 return 0;
621 }
622 }
623
624down:
625 if (netif_carrier_ok(bond->dev)) {
626 netif_carrier_off(bond->dev);
627 return 1;
628 }
629 return 0;
630}
631
632/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 * Get link speed and duplex from the slave's base driver
634 * using ethtool. If for some reason the call fails or the
635 * values are invalid, fake speed and duplex to 100/Full
636 * and return error.
637 */
638static int bond_update_speed_duplex(struct slave *slave)
639{
640 struct net_device *slave_dev = slave->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct ethtool_cmd etool;
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700642 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 /* Fake speed and duplex */
645 slave->speed = SPEED_100;
646 slave->duplex = DUPLEX_FULL;
647
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700648 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700651 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
652 if (res < 0)
653 return -1;
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 switch (etool.speed) {
656 case SPEED_10:
657 case SPEED_100:
658 case SPEED_1000:
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700659 case SPEED_10000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 break;
661 default:
662 return -1;
663 }
664
665 switch (etool.duplex) {
666 case DUPLEX_FULL:
667 case DUPLEX_HALF:
668 break;
669 default:
670 return -1;
671 }
672
673 slave->speed = etool.speed;
674 slave->duplex = etool.duplex;
675
676 return 0;
677}
678
679/*
680 * if <dev> supports MII link status reporting, check its link status.
681 *
682 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000683 * depending upon the setting of the use_carrier parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 *
685 * Return either BMSR_LSTATUS, meaning that the link is up (or we
686 * can't tell and just pretend it is), or 0, meaning that the link is
687 * down.
688 *
689 * If reporting is non-zero, instead of faking link up, return -1 if
690 * both ETHTOOL and MII ioctls fail (meaning the device does not
691 * support them). If use_carrier is set, return whatever it says.
692 * It'd be nice if there was a good way to tell if a driver supports
693 * netif_carrier, but there really isn't.
694 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000695static int bond_check_dev_link(struct bonding *bond,
696 struct net_device *slave_dev, int reporting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800698 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Jiri Bohacd9d52832009-10-28 22:23:54 -0700699 int (*ioctl)(struct net_device *, struct ifreq *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct ifreq ifr;
701 struct mii_ioctl_data *mii;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Petri Gynther6c988852009-08-28 12:05:15 +0000703 if (!reporting && !netif_running(slave_dev))
704 return 0;
705
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800706 if (bond->params.use_carrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Jiri Pirko29112f42009-04-24 01:58:23 +0000709 /* Try to get link status using Ethtool first. */
710 if (slave_dev->ethtool_ops) {
711 if (slave_dev->ethtool_ops->get_link) {
712 u32 link;
713
714 link = slave_dev->ethtool_ops->get_link(slave_dev);
715
716 return link ? BMSR_LSTATUS : 0;
717 }
718 }
719
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000720 /* Ethtool can't be used, fallback to MII ioctls. */
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800721 ioctl = slave_ops->ndo_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (ioctl) {
723 /* TODO: set pointer to correct ioctl on a per team member */
724 /* bases to make this more efficient. that is, once */
725 /* we determine the correct ioctl, we will always */
726 /* call it and not the others for that team */
727 /* member. */
728
729 /*
730 * We cannot assume that SIOCGMIIPHY will also read a
731 * register; not all network drivers (e.g., e100)
732 * support that.
733 */
734
735 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
736 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
737 mii = if_mii(&ifr);
738 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
739 mii->reg_num = MII_BMSR;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000740 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0)
741 return mii->val_out & BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743 }
744
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700745 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 * If reporting, report that either there's no dev->do_ioctl,
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700747 * or both SIOCGMIIREG and get_link failed (meaning that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 * cannot report link status). If not reporting, pretend
749 * we're ok.
750 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000751 return reporting ? -1 : BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754/*----------------------------- Multicast list ------------------------------*/
755
756/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 * Push the promiscuity flag down to appropriate slaves
758 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700759static int bond_set_promiscuity(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700761 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (USES_PRIMARY(bond->params.mode)) {
763 /* write lock already acquired */
764 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700765 err = dev_set_promiscuity(bond->curr_active_slave->dev,
766 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768 } else {
769 struct slave *slave;
770 int i;
771 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700772 err = dev_set_promiscuity(slave->dev, inc);
773 if (err)
774 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700777 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
780/*
781 * Push the allmulti flag down to all slaves
782 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700783static int bond_set_allmulti(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700785 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (USES_PRIMARY(bond->params.mode)) {
787 /* write lock already acquired */
788 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700789 err = dev_set_allmulti(bond->curr_active_slave->dev,
790 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792 } else {
793 struct slave *slave;
794 int i;
795 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700796 err = dev_set_allmulti(slave->dev, inc);
797 if (err)
798 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700801 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
804/*
805 * Add a Multicast address to slaves
806 * according to mode
807 */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000808static void bond_mc_add(struct bonding *bond, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 if (USES_PRIMARY(bond->params.mode)) {
811 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000812 if (bond->curr_active_slave)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000813 dev_mc_add(bond->curr_active_slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 } else {
815 struct slave *slave;
816 int i;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000817
818 bond_for_each_slave(bond, slave, i)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000819 dev_mc_add(slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821}
822
823/*
824 * Remove a multicast address from slave
825 * according to mode
826 */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000827static void bond_mc_del(struct bonding *bond, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 if (USES_PRIMARY(bond->params.mode)) {
830 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000831 if (bond->curr_active_slave)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000832 dev_mc_del(bond->curr_active_slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 } else {
834 struct slave *slave;
835 int i;
836 bond_for_each_slave(bond, slave, i) {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000837 dev_mc_del(slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839 }
840}
841
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800842
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000843static void __bond_resend_igmp_join_requests(struct net_device *dev)
844{
845 struct in_device *in_dev;
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000846
847 rcu_read_lock();
848 in_dev = __in_dev_get_rcu(dev);
Eric Dumazet866f3b22010-11-18 09:33:19 -0800849 if (in_dev)
David S. Miller24912422010-11-19 13:13:47 -0800850 ip_mc_rejoin_groups(in_dev);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000851 rcu_read_unlock();
852}
853
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800854/*
855 * Retrieve the list of registered multicast addresses for the bonding
856 * device and retransmit an IGMP JOIN request to the current active
857 * slave.
858 */
859static void bond_resend_igmp_join_requests(struct bonding *bond)
860{
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000861 struct net_device *vlan_dev;
862 struct vlan_entry *vlan;
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800863
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000864 read_lock(&bond->lock);
865
866 /* rejoin all groups on bond device */
867 __bond_resend_igmp_join_requests(bond->dev);
868
869 /* rejoin all groups on vlan devices */
870 if (bond->vlgrp) {
871 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
872 vlan_dev = vlan_group_get_device(bond->vlgrp,
873 vlan->vlan_id);
874 if (vlan_dev)
875 __bond_resend_igmp_join_requests(vlan_dev);
876 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800877 }
878
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000879 if (--bond->igmp_retrans > 0)
880 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
881
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000882 read_unlock(&bond->lock);
883}
884
stephen hemminger379b7382010-10-15 11:02:56 +0000885static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000886{
887 struct bonding *bond = container_of(work, struct bonding,
888 mcast_work.work);
889 bond_resend_igmp_join_requests(bond);
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800890}
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 * flush all members of flush->mc_list from device dev->mc_list
894 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000895static void bond_mc_list_flush(struct net_device *bond_dev,
896 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Wang Chen454d7c92008-11-12 23:37:49 -0800898 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000899 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Jiri Pirko22bedad32010-04-01 21:22:57 +0000901 netdev_for_each_mc_addr(ha, bond_dev)
902 dev_mc_del(slave_dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 if (bond->params.mode == BOND_MODE_8023AD) {
905 /* del lacpdu mc addr from mc list */
906 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
907
Jiri Pirko22bedad32010-04-01 21:22:57 +0000908 dev_mc_del(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910}
911
912/*--------------------------- Active slave change ---------------------------*/
913
914/*
915 * Update the mc list and multicast-related flags for the new and
916 * old active slaves (if any) according to the multicast mode, and
917 * promiscuous flags unconditionally.
918 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000919static void bond_mc_swap(struct bonding *bond, struct slave *new_active,
920 struct slave *old_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Jiri Pirko22bedad32010-04-01 21:22:57 +0000922 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000924 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 /* nothing to do - mc list is already up-to-date on
926 * all slaves
927 */
928 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 if (old_active) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000931 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 dev_set_promiscuity(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000934 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 dev_set_allmulti(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Jiri Pirko22bedad32010-04-01 21:22:57 +0000937 netdev_for_each_mc_addr(ha, bond->dev)
938 dev_mc_del(old_active->dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940
941 if (new_active) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700942 /* FIXME: Signal errors upstream. */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000943 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 dev_set_promiscuity(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000946 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 dev_set_allmulti(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Jiri Pirko22bedad32010-04-01 21:22:57 +0000949 netdev_for_each_mc_addr(ha, bond->dev)
950 dev_mc_add(new_active->dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
952}
953
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700954/*
955 * bond_do_fail_over_mac
956 *
957 * Perform special MAC address swapping for fail_over_mac settings
958 *
959 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
960 */
961static void bond_do_fail_over_mac(struct bonding *bond,
962 struct slave *new_active,
963 struct slave *old_active)
Hannes Eder1f78d9f2009-02-14 11:15:33 +0000964 __releases(&bond->curr_slave_lock)
965 __releases(&bond->lock)
966 __acquires(&bond->lock)
967 __acquires(&bond->curr_slave_lock)
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700968{
969 u8 tmp_mac[ETH_ALEN];
970 struct sockaddr saddr;
971 int rv;
972
973 switch (bond->params.fail_over_mac) {
974 case BOND_FOM_ACTIVE:
975 if (new_active)
976 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
977 new_active->dev->addr_len);
978 break;
979 case BOND_FOM_FOLLOW:
980 /*
981 * if new_active && old_active, swap them
982 * if just old_active, do nothing (going to no active slave)
983 * if just new_active, set new_active to bond's MAC
984 */
985 if (!new_active)
986 return;
987
988 write_unlock_bh(&bond->curr_slave_lock);
989 read_unlock(&bond->lock);
990
991 if (old_active) {
992 memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
993 memcpy(saddr.sa_data, old_active->dev->dev_addr,
994 ETH_ALEN);
995 saddr.sa_family = new_active->dev->type;
996 } else {
997 memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
998 saddr.sa_family = bond->dev->type;
999 }
1000
1001 rv = dev_set_mac_address(new_active->dev, &saddr);
1002 if (rv) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001003 pr_err("%s: Error %d setting MAC of slave %s\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001004 bond->dev->name, -rv, new_active->dev->name);
1005 goto out;
1006 }
1007
1008 if (!old_active)
1009 goto out;
1010
1011 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
1012 saddr.sa_family = old_active->dev->type;
1013
1014 rv = dev_set_mac_address(old_active->dev, &saddr);
1015 if (rv)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001016 pr_err("%s: Error %d setting MAC of slave %s\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001017 bond->dev->name, -rv, new_active->dev->name);
1018out:
1019 read_lock(&bond->lock);
1020 write_lock_bh(&bond->curr_slave_lock);
1021 break;
1022 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001023 pr_err("%s: bond_do_fail_over_mac impossible: bad policy %d\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001024 bond->dev->name, bond->params.fail_over_mac);
1025 break;
1026 }
1027
1028}
1029
Jiri Pirkoa5499522009-09-25 03:28:09 +00001030static bool bond_should_change_active(struct bonding *bond)
1031{
1032 struct slave *prim = bond->primary_slave;
1033 struct slave *curr = bond->curr_active_slave;
1034
1035 if (!prim || !curr || curr->link != BOND_LINK_UP)
1036 return true;
1037 if (bond->force_primary) {
1038 bond->force_primary = false;
1039 return true;
1040 }
1041 if (bond->params.primary_reselect == BOND_PRI_RESELECT_BETTER &&
1042 (prim->speed < curr->speed ||
1043 (prim->speed == curr->speed && prim->duplex <= curr->duplex)))
1044 return false;
1045 if (bond->params.primary_reselect == BOND_PRI_RESELECT_FAILURE)
1046 return false;
1047 return true;
1048}
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050/**
1051 * find_best_interface - select the best available slave to be the active one
1052 * @bond: our bonding struct
1053 *
1054 * Warning: Caller must hold curr_slave_lock for writing.
1055 */
1056static struct slave *bond_find_best_slave(struct bonding *bond)
1057{
1058 struct slave *new_active, *old_active;
1059 struct slave *bestslave = NULL;
1060 int mintime = bond->params.updelay;
1061 int i;
1062
Nicolas de Pesloüan49b4ad92009-10-07 14:11:00 -07001063 new_active = bond->curr_active_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 if (!new_active) { /* there were no active slaves left */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001066 if (bond->slave_cnt > 0) /* found one slave */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 new_active = bond->first_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001068 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return NULL; /* still no slave, return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if ((bond->primary_slave) &&
Jiri Pirkoa5499522009-09-25 03:28:09 +00001073 bond->primary_slave->link == BOND_LINK_UP &&
1074 bond_should_change_active(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 new_active = bond->primary_slave;
1076 }
1077
1078 /* remember where to stop iterating over the slaves */
1079 old_active = new_active;
1080
1081 bond_for_each_slave_from(bond, new_active, i, old_active) {
Jiri Pirkob9f60252009-08-31 11:09:38 +00001082 if (new_active->link == BOND_LINK_UP) {
1083 return new_active;
1084 } else if (new_active->link == BOND_LINK_BACK &&
1085 IS_UP(new_active->dev)) {
1086 /* link up, but waiting for stabilization */
1087 if (new_active->delay < mintime) {
1088 mintime = new_active->delay;
1089 bestslave = new_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091 }
1092 }
1093
1094 return bestslave;
1095}
1096
1097/**
1098 * change_active_interface - change the active slave into the specified one
1099 * @bond: our bonding struct
1100 * @new: the new slave to make the active one
1101 *
1102 * Set the new slave to the bond's settings and unset them on the old
1103 * curr_active_slave.
1104 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1105 *
1106 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1107 * because it is apparently the best available slave we have, even though its
1108 * updelay hasn't timed out yet.
1109 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001110 * If new_active is not NULL, caller must hold bond->lock for read and
1111 * curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001113void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 struct slave *old_active = bond->curr_active_slave;
1116
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001117 if (old_active == new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 if (new_active) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07001121 new_active->jiffies = jiffies;
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (new_active->link == BOND_LINK_BACK) {
1124 if (USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001125 pr_info("%s: making interface %s the new active one %d ms earlier.\n",
1126 bond->dev->name, new_active->dev->name,
1127 (bond->params.updelay - new_active->delay) * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
1129
1130 new_active->delay = 0;
1131 new_active->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001133 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Holger Eitzenberger58402052008-12-09 23:07:13 -08001136 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 } else {
1139 if (USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001140 pr_info("%s: making interface %s the new active one.\n",
1141 bond->dev->name, new_active->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143 }
1144 }
1145
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001146 if (USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 bond_mc_swap(bond, new_active, old_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Holger Eitzenberger58402052008-12-09 23:07:13 -08001149 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 bond_alb_handle_active_change(bond, new_active);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001151 if (old_active)
1152 bond_set_slave_inactive_flags(old_active);
1153 if (new_active)
1154 bond_set_slave_active_flags(new_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 } else {
1156 bond->curr_active_slave = new_active;
1157 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001158
1159 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001160 if (old_active)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001161 bond_set_slave_inactive_flags(old_active);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001162
1163 if (new_active) {
1164 bond_set_slave_active_flags(new_active);
Moni Shoua2ab82852007-10-09 19:43:39 -07001165
Or Gerlitz709f8a42008-06-13 18:12:01 -07001166 if (bond->params.fail_over_mac)
1167 bond_do_fail_over_mac(bond, new_active,
1168 old_active);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001169
Ben Hutchingsffa95ed2010-12-13 08:19:56 +00001170 if (netif_running(bond->dev)) {
1171 bond->send_grat_arp = bond->params.num_grat_arp;
1172 bond_send_gratuitous_arp(bond);
Or Gerlitz01f31092008-06-13 18:12:02 -07001173
Ben Hutchingsffa95ed2010-12-13 08:19:56 +00001174 bond->send_unsol_na = bond->params.num_unsol_na;
1175 bond_send_unsolicited_na(bond);
1176 }
Brian Haley305d5522008-11-04 17:51:14 -08001177
Or Gerlitz01f31092008-06-13 18:12:02 -07001178 write_unlock_bh(&bond->curr_slave_lock);
1179 read_unlock(&bond->lock);
1180
Moni Shoua75c78502009-09-15 02:37:40 -07001181 netdev_bonding_change(bond->dev, NETDEV_BONDING_FAILOVER);
Or Gerlitz01f31092008-06-13 18:12:02 -07001182
1183 read_lock(&bond->lock);
1184 write_lock_bh(&bond->curr_slave_lock);
Moni Shoua7893b242008-05-17 21:10:12 -07001185 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001186 }
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001187
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001188 /* resend IGMP joins since active slave has changed or
1189 * all were sent on curr_active_slave */
Ben Hutchingsffa95ed2010-12-13 08:19:56 +00001190 if (((USES_PRIMARY(bond->params.mode) && new_active) ||
1191 bond->params.mode == BOND_MODE_ROUNDROBIN) &&
1192 netif_running(bond->dev)) {
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001193 bond->igmp_retrans = bond->params.resend_igmp;
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001194 queue_delayed_work(bond->wq, &bond->mcast_work, 0);
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
1198/**
1199 * bond_select_active_slave - select a new active slave, if needed
1200 * @bond: our bonding struct
1201 *
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001202 * This functions should be called when one of the following occurs:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 * - The old curr_active_slave has been released or lost its link.
1204 * - The primary_slave has got its link back.
1205 * - A slave has got its link back and there's no old curr_active_slave.
1206 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001207 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001209void bond_select_active_slave(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
1211 struct slave *best_slave;
Jay Vosburghff59c452006-03-27 13:27:43 -08001212 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 best_slave = bond_find_best_slave(bond);
1215 if (best_slave != bond->curr_active_slave) {
1216 bond_change_active_slave(bond, best_slave);
Jay Vosburghff59c452006-03-27 13:27:43 -08001217 rv = bond_set_carrier(bond);
1218 if (!rv)
1219 return;
1220
1221 if (netif_carrier_ok(bond->dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001222 pr_info("%s: first active interface up!\n",
1223 bond->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08001224 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001225 pr_info("%s: now running without any active interface !\n",
1226 bond->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08001227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
1229}
1230
1231/*--------------------------- slave list handling ---------------------------*/
1232
1233/*
1234 * This function attaches the slave to the end of list.
1235 *
1236 * bond->lock held for writing by caller.
1237 */
1238static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1239{
1240 if (bond->first_slave == NULL) { /* attaching the first slave */
1241 new_slave->next = new_slave;
1242 new_slave->prev = new_slave;
1243 bond->first_slave = new_slave;
1244 } else {
1245 new_slave->next = bond->first_slave;
1246 new_slave->prev = bond->first_slave->prev;
1247 new_slave->next->prev = new_slave;
1248 new_slave->prev->next = new_slave;
1249 }
1250
1251 bond->slave_cnt++;
1252}
1253
1254/*
1255 * This function detaches the slave from the list.
1256 * WARNING: no check is made to verify if the slave effectively
1257 * belongs to <bond>.
1258 * Nothing is freed on return, structures are just unchained.
1259 * If any slave pointer in bond was pointing to <slave>,
1260 * it should be changed by the calling function.
1261 *
1262 * bond->lock held for writing by caller.
1263 */
1264static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1265{
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001266 if (slave->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 slave->next->prev = slave->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001269 if (slave->prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 slave->prev->next = slave->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 if (bond->first_slave == slave) { /* slave is the first slave */
1273 if (bond->slave_cnt > 1) { /* there are more slave */
1274 bond->first_slave = slave->next;
1275 } else {
1276 bond->first_slave = NULL; /* slave was the last one */
1277 }
1278 }
1279
1280 slave->next = NULL;
1281 slave->prev = NULL;
1282 bond->slave_cnt--;
1283}
1284
WANG Congf6dc31a2010-05-06 00:48:51 -07001285#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001286static inline int slave_enable_netpoll(struct slave *slave)
WANG Congf6dc31a2010-05-06 00:48:51 -07001287{
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001288 struct netpoll *np;
1289 int err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001290
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001291 np = kzalloc(sizeof(*np), GFP_KERNEL);
1292 err = -ENOMEM;
1293 if (!np)
1294 goto out;
1295
1296 np->dev = slave->dev;
1297 err = __netpoll_setup(np);
1298 if (err) {
1299 kfree(np);
1300 goto out;
WANG Congf6dc31a2010-05-06 00:48:51 -07001301 }
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001302 slave->np = np;
1303out:
1304 return err;
1305}
1306static inline void slave_disable_netpoll(struct slave *slave)
1307{
1308 struct netpoll *np = slave->np;
1309
1310 if (!np)
1311 return;
1312
1313 slave->np = NULL;
1314 synchronize_rcu_bh();
1315 __netpoll_cleanup(np);
1316 kfree(np);
1317}
1318static inline bool slave_dev_support_netpoll(struct net_device *slave_dev)
1319{
1320 if (slave_dev->priv_flags & IFF_DISABLE_NETPOLL)
1321 return false;
1322 if (!slave_dev->netdev_ops->ndo_poll_controller)
1323 return false;
1324 return true;
WANG Congf6dc31a2010-05-06 00:48:51 -07001325}
1326
1327static void bond_poll_controller(struct net_device *bond_dev)
1328{
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001329}
1330
1331static void __bond_netpoll_cleanup(struct bonding *bond)
1332{
Neil Hormanc2355e12010-10-13 16:01:49 +00001333 struct slave *slave;
1334 int i;
1335
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001336 bond_for_each_slave(bond, slave, i)
1337 if (IS_UP(slave->dev))
1338 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07001339}
WANG Congf6dc31a2010-05-06 00:48:51 -07001340static void bond_netpoll_cleanup(struct net_device *bond_dev)
1341{
1342 struct bonding *bond = netdev_priv(bond_dev);
WANG Congf6dc31a2010-05-06 00:48:51 -07001343
1344 read_lock(&bond->lock);
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001345 __bond_netpoll_cleanup(bond);
WANG Congf6dc31a2010-05-06 00:48:51 -07001346 read_unlock(&bond->lock);
1347}
1348
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001349static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
1350{
1351 struct bonding *bond = netdev_priv(dev);
1352 struct slave *slave;
1353 int i, err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001354
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001355 read_lock(&bond->lock);
1356 bond_for_each_slave(bond, slave, i) {
1357 if (!IS_UP(slave->dev))
1358 continue;
1359 err = slave_enable_netpoll(slave);
1360 if (err) {
1361 __bond_netpoll_cleanup(bond);
1362 break;
1363 }
1364 }
1365 read_unlock(&bond->lock);
1366 return err;
1367}
1368
1369static struct netpoll_info *bond_netpoll_info(struct bonding *bond)
1370{
1371 return bond->dev->npinfo;
1372}
1373
1374#else
1375static inline int slave_enable_netpoll(struct slave *slave)
1376{
1377 return 0;
1378}
1379static inline void slave_disable_netpoll(struct slave *slave)
1380{
1381}
WANG Congf6dc31a2010-05-06 00:48:51 -07001382static void bond_netpoll_cleanup(struct net_device *bond_dev)
1383{
1384}
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001385static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
1386{
1387 return 0;
1388}
1389static struct netpoll_info *bond_netpoll_info(struct bonding *bond)
1390{
1391 return NULL;
1392}
WANG Congf6dc31a2010-05-06 00:48:51 -07001393#endif
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395/*---------------------------------- IOCTL ----------------------------------*/
1396
Adrian Bunk4ad072c2007-07-09 11:51:12 -07001397static int bond_sethwaddr(struct net_device *bond_dev,
1398 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001400 pr_debug("bond_dev=%p\n", bond_dev);
1401 pr_debug("slave_dev=%p\n", slave_dev);
1402 pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1404 return 0;
1405}
1406
Herbert Xu7f353bf2007-08-10 15:47:58 -07001407#define BOND_VLAN_FEATURES \
1408 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1409 NETIF_F_HW_VLAN_FILTER)
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001410
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001411/*
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001412 * Compute the common dev->feature set available to all slaves. Some
Herbert Xu7f353bf2007-08-10 15:47:58 -07001413 * feature bits are managed elsewhere, so preserve those feature bits
1414 * on the master device.
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001415 */
1416static int bond_compute_features(struct bonding *bond)
1417{
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001418 struct slave *slave;
1419 struct net_device *bond_dev = bond->dev;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08001420 u32 features = bond_dev->features;
1421 u32 vlan_features = 0;
Moni Shoua3158bf72007-10-09 19:43:41 -07001422 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1423 bond_dev->hard_header_len);
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001424 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001425
Herbert Xu7f353bf2007-08-10 15:47:58 -07001426 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
Herbert Xub63365a2008-10-23 01:11:29 -07001427 features |= NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1428
1429 if (!bond->first_slave)
1430 goto done;
1431
1432 features &= ~NETIF_F_ONE_FOR_ALL;
Herbert Xu7f353bf2007-08-10 15:47:58 -07001433
Jay Vosburgh278339a2009-08-28 12:05:12 +00001434 vlan_features = bond->first_slave->dev->vlan_features;
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001435 bond_for_each_slave(bond, slave, i) {
Herbert Xub63365a2008-10-23 01:11:29 -07001436 features = netdev_increment_features(features,
1437 slave->dev->features,
1438 NETIF_F_ONE_FOR_ALL);
Jay Vosburgh278339a2009-08-28 12:05:12 +00001439 vlan_features = netdev_increment_features(vlan_features,
1440 slave->dev->vlan_features,
1441 NETIF_F_ONE_FOR_ALL);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001442 if (slave->dev->hard_header_len > max_hard_header_len)
1443 max_hard_header_len = slave->dev->hard_header_len;
1444 }
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001445
Herbert Xub63365a2008-10-23 01:11:29 -07001446done:
Herbert Xu7f353bf2007-08-10 15:47:58 -07001447 features |= (bond_dev->features & BOND_VLAN_FEATURES);
Michał Mirosławacd11302011-01-24 15:45:15 -08001448 bond_dev->features = netdev_fix_features(bond_dev, features);
1449 bond_dev->vlan_features = netdev_fix_features(bond_dev, vlan_features);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001450 bond_dev->hard_header_len = max_hard_header_len;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001451
1452 return 0;
1453}
1454
Moni Shoua872254d2007-10-09 19:43:38 -07001455static void bond_setup_by_slave(struct net_device *bond_dev,
1456 struct net_device *slave_dev)
1457{
Wang Chen454d7c92008-11-12 23:37:49 -08001458 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07001459
Stephen Hemminger00829822008-11-20 20:14:53 -08001460 bond_dev->header_ops = slave_dev->header_ops;
Moni Shoua872254d2007-10-09 19:43:38 -07001461
1462 bond_dev->type = slave_dev->type;
1463 bond_dev->hard_header_len = slave_dev->hard_header_len;
1464 bond_dev->addr_len = slave_dev->addr_len;
1465
1466 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1467 slave_dev->addr_len);
Moni Shouad90a1622007-10-09 19:43:43 -07001468 bond->setup_by_slave = 1;
Moni Shoua872254d2007-10-09 19:43:38 -07001469}
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471/* enslave device <slave> to bond device <master> */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001472int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Wang Chen454d7c92008-11-12 23:37:49 -08001474 struct bonding *bond = netdev_priv(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001475 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 struct slave *new_slave = NULL;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001477 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 struct sockaddr addr;
1479 int link_reporting;
1480 int old_features = bond_dev->features;
1481 int res = 0;
1482
nsxfreddy@gmail.com552709d2005-09-21 14:18:04 -05001483 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001484 slave_ops->ndo_do_ioctl == NULL) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001485 pr_warning("%s: Warning: no link monitoring support for %s\n",
1486 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488
1489 /* bond must be initialized by bond_open() before enslaving */
1490 if (!(bond_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001491 pr_warning("%s: master_dev is not up in bond_enslave\n",
1492 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
1494
1495 /* already enslaved */
1496 if (slave_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001497 pr_debug("Error, Device was already enslaved\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return -EBUSY;
1499 }
1500
1501 /* vlan challenged mutual exclusion */
1502 /* no need to lock since we're protected by rtnl_lock */
1503 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001504 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Jay Vosburghf35188f2010-07-21 12:14:47 +00001505 if (bond->vlgrp) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001506 pr_err("%s: Error: cannot enslave VLAN challenged slave %s on VLAN enabled bond %s\n",
1507 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return -EPERM;
1509 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001510 pr_warning("%s: Warning: enslaved VLAN challenged slave %s. Adding VLANs will be blocked as long as %s is part of bond %s\n",
1511 bond_dev->name, slave_dev->name,
1512 slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1514 }
1515 } else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001516 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 if (bond->slave_cnt == 0) {
1518 /* First slave, and it is not VLAN challenged,
1519 * so remove the block of adding VLANs over the bond.
1520 */
1521 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1522 }
1523 }
1524
Jay Vosburgh217df672005-09-26 16:11:50 -07001525 /*
1526 * Old ifenslave binaries are no longer supported. These can
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001527 * be identified with moderate accuracy by the state of the slave:
Jay Vosburgh217df672005-09-26 16:11:50 -07001528 * the current ifenslave will set the interface down prior to
1529 * enslaving it; the old ifenslave will not.
1530 */
1531 if ((slave_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001532 pr_err("%s is up. This may be due to an out of date ifenslave.\n",
Jay Vosburgh217df672005-09-26 16:11:50 -07001533 slave_dev->name);
1534 res = -EPERM;
1535 goto err_undo_flags;
1536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Moni Shoua872254d2007-10-09 19:43:38 -07001538 /* set bonding device ether type by slave - bonding netdevices are
1539 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1540 * there is a need to override some of the type dependent attribs/funcs.
1541 *
1542 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1543 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1544 */
1545 if (bond->slave_cnt == 0) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001546 if (bond_dev->type != slave_dev->type) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001547 pr_debug("%s: change device type from %d to %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001548 bond_dev->name,
1549 bond_dev->type, slave_dev->type);
Moni Shoua75c78502009-09-15 02:37:40 -07001550
Jiri Pirko3ca5b402010-03-10 10:29:35 +00001551 res = netdev_bonding_change(bond_dev,
1552 NETDEV_PRE_TYPE_CHANGE);
1553 res = notifier_to_errno(res);
1554 if (res) {
1555 pr_err("%s: refused to change device type\n",
1556 bond_dev->name);
1557 res = -EBUSY;
1558 goto err_undo_flags;
1559 }
Moni Shoua75c78502009-09-15 02:37:40 -07001560
Jiri Pirko32a806c2010-03-19 04:00:23 +00001561 /* Flush unicast and multicast addresses */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001562 dev_uc_flush(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001563 dev_mc_flush(bond_dev);
Jiri Pirko32a806c2010-03-19 04:00:23 +00001564
Moni Shouae36b9d12009-07-15 04:56:31 +00001565 if (slave_dev->type != ARPHRD_ETHER)
1566 bond_setup_by_slave(bond_dev, slave_dev);
1567 else
1568 ether_setup(bond_dev);
Moni Shoua75c78502009-09-15 02:37:40 -07001569
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001570 netdev_bonding_change(bond_dev,
1571 NETDEV_POST_TYPE_CHANGE);
Moni Shouae36b9d12009-07-15 04:56:31 +00001572 }
Moni Shoua872254d2007-10-09 19:43:38 -07001573 } else if (bond_dev->type != slave_dev->type) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001574 pr_err("%s ether type (%d) is different from other slaves (%d), can not enslave it.\n",
1575 slave_dev->name,
1576 slave_dev->type, bond_dev->type);
1577 res = -EINVAL;
1578 goto err_undo_flags;
Moni Shoua872254d2007-10-09 19:43:38 -07001579 }
1580
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001581 if (slave_ops->ndo_set_mac_address == NULL) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001582 if (bond->slave_cnt == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001583 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.",
1584 bond_dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001585 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1586 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001587 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",
1588 bond_dev->name);
Moni Shoua2ab82852007-10-09 19:43:39 -07001589 res = -EOPNOTSUPP;
1590 goto err_undo_flags;
1591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001594 /* If this is the first slave, then we need to set the master's hardware
1595 * address to be the same as the slave's. */
David Strandd13a2cb2010-12-01 11:43:08 -08001596 if (is_zero_ether_addr(bond->dev->dev_addr))
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001597 memcpy(bond->dev->dev_addr, slave_dev->dev_addr,
1598 slave_dev->addr_len);
1599
1600
Joe Jin243cb4e2007-02-06 14:16:40 -08001601 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (!new_slave) {
1603 res = -ENOMEM;
1604 goto err_undo_flags;
1605 }
1606
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001607 /*
1608 * Set the new_slave's queue_id to be zero. Queue ID mapping
1609 * is set via sysfs or module option if desired.
1610 */
1611 new_slave->queue_id = 0;
1612
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001613 /* Save slave's original mtu and then set it to match the bond */
1614 new_slave->original_mtu = slave_dev->mtu;
1615 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1616 if (res) {
1617 pr_debug("Error %d calling dev_set_mtu\n", res);
1618 goto err_free;
1619 }
1620
Jay Vosburgh217df672005-09-26 16:11:50 -07001621 /*
1622 * Save slave's original ("permanent") mac address for modes
1623 * that need it, and for restoring it upon release, and then
1624 * set it to the master's address
1625 */
1626 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Jay Vosburghdd957c52007-10-09 19:57:24 -07001628 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001629 /*
1630 * Set slave to master's mac address. The application already
1631 * set the master's mac address to that of the first slave
1632 */
1633 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1634 addr.sa_family = slave_dev->type;
1635 res = dev_set_mac_address(slave_dev, &addr);
1636 if (res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001637 pr_debug("Error %d calling set_mac_address\n", res);
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001638 goto err_restore_mtu;
Moni Shoua2ab82852007-10-09 19:43:39 -07001639 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Jiri Pirko1765a572011-02-12 06:48:36 +00001642 res = netdev_set_bond_master(slave_dev, bond_dev);
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001643 if (res) {
Jiri Pirko1765a572011-02-12 06:48:36 +00001644 pr_debug("Error %d calling netdev_set_bond_master\n", res);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001645 goto err_restore_mac;
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001646 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001647 /* open the slave since the application closed it */
1648 res = dev_open(slave_dev);
1649 if (res) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001650 pr_debug("Opening slave %s failed\n", slave_dev->name);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001651 goto err_unset_master;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 new_slave->dev = slave_dev;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001655 slave_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Holger Eitzenberger58402052008-12-09 23:07:13 -08001657 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 /* bond_alb_init_slave() must be called before all other stages since
1659 * it might fail and we do not want to have to undo everything
1660 */
1661 res = bond_alb_init_slave(bond, new_slave);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001662 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001663 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665
1666 /* If the mode USES_PRIMARY, then the new slave gets the
1667 * master's promisc (and mc) settings only if it becomes the
1668 * curr_active_slave, and that is taken care of later when calling
1669 * bond_change_active()
1670 */
1671 if (!USES_PRIMARY(bond->params.mode)) {
1672 /* set promiscuity level to new slave */
1673 if (bond_dev->flags & IFF_PROMISC) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001674 res = dev_set_promiscuity(slave_dev, 1);
1675 if (res)
1676 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
1678
1679 /* set allmulti level to new slave */
1680 if (bond_dev->flags & IFF_ALLMULTI) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001681 res = dev_set_allmulti(slave_dev, 1);
1682 if (res)
1683 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 }
1685
David S. Millerb9e40852008-07-15 00:15:08 -07001686 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 /* upload master's mc_list to new slave */
Jiri Pirko22bedad32010-04-01 21:22:57 +00001688 netdev_for_each_mc_addr(ha, bond_dev)
1689 dev_mc_add(slave_dev, ha->addr);
David S. Millerb9e40852008-07-15 00:15:08 -07001690 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
1692
1693 if (bond->params.mode == BOND_MODE_8023AD) {
1694 /* add lacpdu mc addr to mc list */
1695 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1696
Jiri Pirko22bedad32010-04-01 21:22:57 +00001697 dev_mc_add(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
1699
1700 bond_add_vlans_on_slave(bond, slave_dev);
1701
1702 write_lock_bh(&bond->lock);
1703
1704 bond_attach_slave(bond, new_slave);
1705
1706 new_slave->delay = 0;
1707 new_slave->link_failure_count = 0;
1708
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001709 bond_compute_features(bond);
1710
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001711 write_unlock_bh(&bond->lock);
1712
1713 read_lock(&bond->lock);
1714
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001715 new_slave->last_arp_rx = jiffies;
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 if (bond->params.miimon && !bond->params.use_carrier) {
1718 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1719
1720 if ((link_reporting == -1) && !bond->params.arp_interval) {
1721 /*
1722 * miimon is set but a bonded network driver
1723 * does not support ETHTOOL/MII and
1724 * arp_interval is not set. Note: if
1725 * use_carrier is enabled, we will never go
1726 * here (because netif_carrier is always
1727 * supported); thus, we don't need to change
1728 * the messages for netif_carrier.
1729 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001730 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 -08001731 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 } else if (link_reporting == -1) {
1733 /* unable get link status using mii/ethtool */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001734 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",
1735 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
1737 }
1738
1739 /* check for initial state */
1740 if (!bond->params.miimon ||
1741 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1742 if (bond->params.updelay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001743 pr_debug("Initial state of slave_dev is BOND_LINK_BACK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 new_slave->link = BOND_LINK_BACK;
1745 new_slave->delay = bond->params.updelay;
1746 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001747 pr_debug("Initial state of slave_dev is BOND_LINK_UP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 new_slave->link = BOND_LINK_UP;
1749 }
1750 new_slave->jiffies = jiffies;
1751 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001752 pr_debug("Initial state of slave_dev is BOND_LINK_DOWN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 new_slave->link = BOND_LINK_DOWN;
1754 }
1755
1756 if (bond_update_speed_duplex(new_slave) &&
1757 (new_slave->link != BOND_LINK_DOWN)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001758 pr_warning("%s: Warning: failed to get speed and duplex from %s, assumed to be 100Mb/sec and Full.\n",
1759 bond_dev->name, new_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 if (bond->params.mode == BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001762 pr_warning("%s: Warning: Operation of 802.3ad mode requires ETHTOOL support in base driver for proper aggregator selection.\n",
1763 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
1765 }
1766
1767 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1768 /* if there is a primary slave, remember it */
Jiri Pirkoa5499522009-09-25 03:28:09 +00001769 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 bond->primary_slave = new_slave;
Jiri Pirkoa5499522009-09-25 03:28:09 +00001771 bond->force_primary = true;
1772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001775 write_lock_bh(&bond->curr_slave_lock);
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 switch (bond->params.mode) {
1778 case BOND_MODE_ACTIVEBACKUP:
Jay Vosburgh8a8e4472006-09-22 21:56:15 -07001779 bond_set_slave_inactive_flags(new_slave);
1780 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 break;
1782 case BOND_MODE_8023AD:
1783 /* in 802.3ad mode, the internal mechanism
1784 * will activate the slaves in the selected
1785 * aggregator
1786 */
1787 bond_set_slave_inactive_flags(new_slave);
1788 /* if this is the first slave */
1789 if (bond->slave_cnt == 1) {
1790 SLAVE_AD_INFO(new_slave).id = 1;
1791 /* Initialize AD with the number of times that the AD timer is called in 1 second
1792 * can be called only after the mac address of the bond is set
1793 */
1794 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1795 bond->params.lacp_fast);
1796 } else {
1797 SLAVE_AD_INFO(new_slave).id =
1798 SLAVE_AD_INFO(new_slave->prev).id + 1;
1799 }
1800
1801 bond_3ad_bind_slave(new_slave);
1802 break;
1803 case BOND_MODE_TLB:
1804 case BOND_MODE_ALB:
1805 new_slave->state = BOND_STATE_ACTIVE;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001806 bond_set_slave_inactive_flags(new_slave);
Jiri Pirko5a29f782009-03-25 17:23:38 -07001807 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 break;
1809 default:
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001810 pr_debug("This slave is always active in trunk mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 /* always active in trunk mode */
1813 new_slave->state = BOND_STATE_ACTIVE;
1814
1815 /* In trunking mode there is little meaning to curr_active_slave
1816 * anyway (it holds no special properties of the bond device),
1817 * so we can change it without calling change_active_interface()
1818 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001819 if (!bond->curr_active_slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 bond->curr_active_slave = new_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 break;
1823 } /* switch(bond_mode) */
1824
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001825 write_unlock_bh(&bond->curr_slave_lock);
1826
Jay Vosburghff59c452006-03-27 13:27:43 -08001827 bond_set_carrier(bond);
1828
WANG Congf6dc31a2010-05-06 00:48:51 -07001829#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001830 slave_dev->npinfo = bond_netpoll_info(bond);
1831 if (slave_dev->npinfo) {
1832 if (slave_enable_netpoll(new_slave)) {
1833 read_unlock(&bond->lock);
1834 pr_info("Error, %s: master_dev is using netpoll, "
1835 "but new slave device does not support netpoll.\n",
1836 bond_dev->name);
1837 res = -EBUSY;
1838 goto err_close;
1839 }
WANG Congf6dc31a2010-05-06 00:48:51 -07001840 }
1841#endif
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001842
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001843 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001845 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1846 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001847 goto err_close;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001848
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001849 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
1850 bond_dev->name, slave_dev->name,
1851 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1852 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854 /* enslave is successful */
1855 return 0;
1856
1857/* Undo stages on error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858err_close:
1859 dev_close(slave_dev);
1860
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001861err_unset_master:
Jiri Pirko1765a572011-02-12 06:48:36 +00001862 netdev_set_bond_master(slave_dev, NULL);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864err_restore_mac:
Jay Vosburghdd957c52007-10-09 19:57:24 -07001865 if (!bond->params.fail_over_mac) {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001866 /* XXX TODO - fom follow mode needs to change master's
1867 * MAC if this slave's MAC is in use by the bond, or at
1868 * least print a warning.
1869 */
Moni Shoua2ab82852007-10-09 19:43:39 -07001870 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1871 addr.sa_family = slave_dev->type;
1872 dev_set_mac_address(slave_dev, &addr);
1873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001875err_restore_mtu:
1876 dev_set_mtu(slave_dev, new_slave->original_mtu);
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878err_free:
1879 kfree(new_slave);
1880
1881err_undo_flags:
1882 bond_dev->features = old_features;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return res;
1885}
1886
1887/*
1888 * Try to release the slave device <slave> from the bond device <master>
1889 * It is legal to access curr_active_slave without a lock because all the function
1890 * is write-locked.
1891 *
1892 * The rules for slave state should be:
1893 * for Active/Backup:
1894 * Active stays on all backups go down
1895 * for Bonded connections:
1896 * The first up interface should be left on and all others downed.
1897 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001898int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Wang Chen454d7c92008-11-12 23:37:49 -08001900 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 struct slave *slave, *oldcurrent;
1902 struct sockaddr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 /* slave is not a slave or master is not master of this slave */
1905 if (!(slave_dev->flags & IFF_SLAVE) ||
1906 (slave_dev->master != bond_dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001907 pr_err("%s: Error: cannot release %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 bond_dev->name, slave_dev->name);
1909 return -EINVAL;
1910 }
1911
Neil Hormane843fa52010-10-13 16:01:50 +00001912 block_netpoll_tx();
WANG Congf6dc31a2010-05-06 00:48:51 -07001913 netdev_bonding_change(bond_dev, NETDEV_BONDING_DESLAVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 write_lock_bh(&bond->lock);
1915
1916 slave = bond_get_slave_by_dev(bond, slave_dev);
1917 if (!slave) {
1918 /* not a slave of this bond */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001919 pr_info("%s: %s not enslaved\n",
1920 bond_dev->name, slave_dev->name);
Jay Vosburghf5e2a7b2006-02-07 21:17:22 -08001921 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001922 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 return -EINVAL;
1924 }
1925
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001926 if (!bond->params.fail_over_mac) {
Joe Perches8e95a202009-12-03 07:58:21 +00001927 if (!compare_ether_addr(bond_dev->dev_addr, slave->perm_hwaddr) &&
1928 bond->slave_cnt > 1)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001929 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",
1930 bond_dev->name, slave_dev->name,
1931 slave->perm_hwaddr,
1932 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
1934
1935 /* Inform AD package of unbinding of slave. */
1936 if (bond->params.mode == BOND_MODE_8023AD) {
1937 /* must be called before the slave is
1938 * detached from the list
1939 */
1940 bond_3ad_unbind_slave(slave);
1941 }
1942
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001943 pr_info("%s: releasing %s interface %s\n",
1944 bond_dev->name,
1945 (slave->state == BOND_STATE_ACTIVE) ? "active" : "backup",
1946 slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948 oldcurrent = bond->curr_active_slave;
1949
1950 bond->current_arp_slave = NULL;
1951
1952 /* release the slave from its bond */
1953 bond_detach_slave(bond, slave);
1954
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001955 bond_compute_features(bond);
1956
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001957 if (bond->primary_slave == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 bond->primary_slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001960 if (oldcurrent == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 bond_change_active_slave(bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Holger Eitzenberger58402052008-12-09 23:07:13 -08001963 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 /* Must be called only after the slave has been
1965 * detached from the list and the curr_active_slave
1966 * has been cleared (if our_slave == old_current),
1967 * but before a new active slave is selected.
1968 */
Jay Vosburgh25433312008-01-17 16:24:59 -08001969 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 bond_alb_deinit_slave(bond, slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08001971 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 }
1973
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001974 if (oldcurrent == slave) {
1975 /*
1976 * Note that we hold RTNL over this sequence, so there
1977 * is no concern that another slave add/remove event
1978 * will interfere.
1979 */
1980 write_unlock_bh(&bond->lock);
1981 read_lock(&bond->lock);
1982 write_lock_bh(&bond->curr_slave_lock);
1983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 bond_select_active_slave(bond);
1985
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001986 write_unlock_bh(&bond->curr_slave_lock);
1987 read_unlock(&bond->lock);
1988 write_lock_bh(&bond->lock);
1989 }
1990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if (bond->slave_cnt == 0) {
Jay Vosburghff59c452006-03-27 13:27:43 -08001992 bond_set_carrier(bond);
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 /* if the last slave was removed, zero the mac address
1995 * of the master so it will be set by the application
1996 * to the mac address of the first slave
1997 */
1998 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1999
Jay Vosburghf35188f2010-07-21 12:14:47 +00002000 if (!bond->vlgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2002 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002003 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2004 bond_dev->name, bond_dev->name);
2005 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2006 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 }
2008 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2009 !bond_has_challenged_slaves(bond)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002010 pr_info("%s: last VLAN challenged slave %s left bond %s. VLAN blocking is removed\n",
2011 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
2013 }
2014
2015 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002016 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002018 /* must do this from outside any spinlocks */
2019 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 bond_del_vlans_from_slave(bond, slave_dev);
2022
2023 /* If the mode USES_PRIMARY, then we should only remove its
2024 * promisc and mc settings if it was the curr_active_slave, but that was
2025 * already taken care of above when we detached the slave
2026 */
2027 if (!USES_PRIMARY(bond->params.mode)) {
2028 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002029 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002033 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002037 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002039 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 }
2041
Jiri Pirko1765a572011-02-12 06:48:36 +00002042 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002044 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07002045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 /* close slave before restoring its mac address */
2047 dev_close(slave_dev);
2048
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07002049 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002050 /* restore original ("permanent") mac address */
2051 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2052 addr.sa_family = slave_dev->type;
2053 dev_set_mac_address(slave_dev, &addr);
2054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00002056 dev_set_mtu(slave_dev, slave->original_mtu);
2057
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002058 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002059 IFF_SLAVE_INACTIVE | IFF_BONDING |
2060 IFF_SLAVE_NEEDARP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 kfree(slave);
2063
2064 return 0; /* deletion OK */
2065}
2066
2067/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002068* First release a slave and than destroy the bond if no more slaves are left.
Moni Shouad90a1622007-10-09 19:43:43 -07002069* Must be under rtnl_lock when this function is called.
2070*/
stephen hemminger26d8ee72010-10-15 05:09:34 +00002071static int bond_release_and_destroy(struct net_device *bond_dev,
2072 struct net_device *slave_dev)
Moni Shouad90a1622007-10-09 19:43:43 -07002073{
Wang Chen454d7c92008-11-12 23:37:49 -08002074 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002075 int ret;
2076
2077 ret = bond_release(bond_dev, slave_dev);
2078 if ((ret == 0) && (bond->slave_cnt == 0)) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002079 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002080 pr_info("%s: destroying bond %s.\n",
2081 bond_dev->name, bond_dev->name);
Stephen Hemminger9e716262009-06-12 19:02:47 +00002082 unregister_netdevice(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002083 }
2084 return ret;
2085}
2086
2087/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 * This function releases all slaves.
2089 */
2090static int bond_release_all(struct net_device *bond_dev)
2091{
Wang Chen454d7c92008-11-12 23:37:49 -08002092 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 struct slave *slave;
2094 struct net_device *slave_dev;
2095 struct sockaddr addr;
2096
2097 write_lock_bh(&bond->lock);
2098
Jay Vosburghff59c452006-03-27 13:27:43 -08002099 netif_carrier_off(bond_dev);
2100
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002101 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 bond->current_arp_slave = NULL;
2105 bond->primary_slave = NULL;
2106 bond_change_active_slave(bond, NULL);
2107
2108 while ((slave = bond->first_slave) != NULL) {
2109 /* Inform AD package of unbinding of slave
2110 * before slave is detached from the list.
2111 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002112 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 bond_3ad_unbind_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 slave_dev = slave->dev;
2116 bond_detach_slave(bond, slave);
2117
Jay Vosburgh25433312008-01-17 16:24:59 -08002118 /* now that the slave is detached, unlock and perform
2119 * all the undo steps that should not be called from
2120 * within a lock.
2121 */
2122 write_unlock_bh(&bond->lock);
2123
Holger Eitzenberger58402052008-12-09 23:07:13 -08002124 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 /* must be called only after the slave
2126 * has been detached from the list
2127 */
2128 bond_alb_deinit_slave(bond, slave);
2129 }
2130
Arthur Kepner8531c5f2005-08-23 01:34:53 -04002131 bond_compute_features(bond);
2132
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002133 bond_destroy_slave_symlinks(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 bond_del_vlans_from_slave(bond, slave_dev);
2135
2136 /* If the mode USES_PRIMARY, then we should only remove its
2137 * promisc and mc settings if it was the curr_active_slave, but that was
2138 * already taken care of above when we detached the slave
2139 */
2140 if (!USES_PRIMARY(bond->params.mode)) {
2141 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002142 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002146 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002150 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002152 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
2154
Jiri Pirko1765a572011-02-12 06:48:36 +00002155 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002157 slave_disable_netpoll(slave);
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 /* close slave before restoring its mac address */
2160 dev_close(slave_dev);
2161
Jay Vosburghdd957c52007-10-09 19:57:24 -07002162 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002163 /* restore original ("permanent") mac address*/
2164 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2165 addr.sa_family = slave_dev->type;
2166 dev_set_mac_address(slave_dev, &addr);
2167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002169 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
2170 IFF_SLAVE_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 kfree(slave);
2173
2174 /* re-acquire the lock before getting the next slave */
2175 write_lock_bh(&bond->lock);
2176 }
2177
2178 /* zero the mac address of the master so it will be
2179 * set by the application to the mac address of the
2180 * first slave
2181 */
2182 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2183
Jay Vosburghf35188f2010-07-21 12:14:47 +00002184 if (!bond->vlgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
Jay Vosburghf35188f2010-07-21 12:14:47 +00002186 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002187 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2188 bond_dev->name, bond_dev->name);
2189 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2190 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 }
2192
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002193 pr_info("%s: released all slaves\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195out:
2196 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return 0;
2198}
2199
2200/*
2201 * This function changes the active slave to slave <slave_dev>.
2202 * It returns -EINVAL in the following cases.
2203 * - <slave_dev> is not found in the list.
2204 * - There is not active slave now.
2205 * - <slave_dev> is already active.
2206 * - The link state of <slave_dev> is not BOND_LINK_UP.
2207 * - <slave_dev> is not running.
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002208 * In these cases, this function does nothing.
2209 * In the other cases, current_slave pointer is changed and 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 */
2211static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2212{
Wang Chen454d7c92008-11-12 23:37:49 -08002213 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 struct slave *old_active = NULL;
2215 struct slave *new_active = NULL;
2216 int res = 0;
2217
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002218 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
2221 /* Verify that master_dev is indeed the master of slave_dev */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002222 if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002225 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002227 read_lock(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 old_active = bond->curr_active_slave;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002229 read_unlock(&bond->curr_slave_lock);
2230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 new_active = bond_get_slave_by_dev(bond, slave_dev);
2232
2233 /*
2234 * Changing to the current active: do nothing; return success.
2235 */
2236 if (new_active && (new_active == old_active)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002237 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return 0;
2239 }
2240
2241 if ((new_active) &&
2242 (old_active) &&
2243 (new_active->link == BOND_LINK_UP) &&
2244 IS_UP(new_active->dev)) {
Neil Hormane843fa52010-10-13 16:01:50 +00002245 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002246 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 bond_change_active_slave(bond, new_active);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002248 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002249 unblock_netpoll_tx();
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002250 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002253 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
2255 return res;
2256}
2257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2259{
Wang Chen454d7c92008-11-12 23:37:49 -08002260 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
2262 info->bond_mode = bond->params.mode;
2263 info->miimon = bond->params.miimon;
2264
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002265 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 info->num_slaves = bond->slave_cnt;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002267 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 return 0;
2270}
2271
2272static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2273{
Wang Chen454d7c92008-11-12 23:37:49 -08002274 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 struct slave *slave;
Eric Dumazet689c96c2009-04-23 03:39:04 +00002276 int i, res = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002278 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280 bond_for_each_slave(bond, slave, i) {
2281 if (i == (int)info->slave_id) {
Eric Dumazet689c96c2009-04-23 03:39:04 +00002282 res = 0;
2283 strcpy(info->slave_name, slave->dev->name);
2284 info->link = slave->link;
2285 info->state = slave->state;
2286 info->link_failure_count = slave->link_failure_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 break;
2288 }
2289 }
2290
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002291 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Eric Dumazet689c96c2009-04-23 03:39:04 +00002293 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
2296/*-------------------------------- Monitoring -------------------------------*/
2297
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002298
2299static int bond_miimon_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002301 struct slave *slave;
2302 int i, link_state, commit = 0;
Jiri Pirko41f89102009-04-24 03:57:29 +00002303 bool ignore_updelay;
2304
2305 ignore_updelay = !bond->curr_active_slave ? true : false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307 bond_for_each_slave(bond, slave, i) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002308 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002310 link_state = bond_check_dev_link(bond, slave->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312 switch (slave->link) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002313 case BOND_LINK_UP:
2314 if (link_state)
2315 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002317 slave->link = BOND_LINK_FAIL;
2318 slave->delay = bond->params.downdelay;
2319 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002320 pr_info("%s: link status down for %sinterface %s, disabling it in %d ms.\n",
2321 bond->dev->name,
2322 (bond->params.mode ==
2323 BOND_MODE_ACTIVEBACKUP) ?
2324 ((slave->state == BOND_STATE_ACTIVE) ?
2325 "active " : "backup ") : "",
2326 slave->dev->name,
2327 bond->params.downdelay * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002329 /*FALLTHRU*/
2330 case BOND_LINK_FAIL:
2331 if (link_state) {
2332 /*
2333 * recovered before downdelay expired
2334 */
2335 slave->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 slave->jiffies = jiffies;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002337 pr_info("%s: link status up again after %d ms for interface %s.\n",
2338 bond->dev->name,
2339 (bond->params.downdelay - slave->delay) *
2340 bond->params.miimon,
2341 slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002342 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002344
2345 if (slave->delay <= 0) {
2346 slave->new_link = BOND_LINK_DOWN;
2347 commit++;
2348 continue;
2349 }
2350
2351 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002354 case BOND_LINK_DOWN:
2355 if (!link_state)
2356 continue;
2357
2358 slave->link = BOND_LINK_BACK;
2359 slave->delay = bond->params.updelay;
2360
2361 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002362 pr_info("%s: link status up for interface %s, enabling it in %d ms.\n",
2363 bond->dev->name, slave->dev->name,
2364 ignore_updelay ? 0 :
2365 bond->params.updelay *
2366 bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002368 /*FALLTHRU*/
2369 case BOND_LINK_BACK:
2370 if (!link_state) {
2371 slave->link = BOND_LINK_DOWN;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002372 pr_info("%s: link status down again after %d ms for interface %s.\n",
2373 bond->dev->name,
2374 (bond->params.updelay - slave->delay) *
2375 bond->params.miimon,
2376 slave->dev->name);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002377
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002378 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002380
Jiri Pirko41f89102009-04-24 03:57:29 +00002381 if (ignore_updelay)
2382 slave->delay = 0;
2383
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002384 if (slave->delay <= 0) {
2385 slave->new_link = BOND_LINK_UP;
2386 commit++;
Jiri Pirko41f89102009-04-24 03:57:29 +00002387 ignore_updelay = false;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002388 continue;
2389 }
2390
2391 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 break;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002393 }
2394 }
2395
2396 return commit;
2397}
2398
2399static void bond_miimon_commit(struct bonding *bond)
2400{
2401 struct slave *slave;
2402 int i;
2403
2404 bond_for_each_slave(bond, slave, i) {
2405 switch (slave->new_link) {
2406 case BOND_LINK_NOCHANGE:
2407 continue;
2408
2409 case BOND_LINK_UP:
2410 slave->link = BOND_LINK_UP;
2411 slave->jiffies = jiffies;
2412
2413 if (bond->params.mode == BOND_MODE_8023AD) {
2414 /* prevent it from being the active one */
2415 slave->state = BOND_STATE_BACKUP;
2416 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2417 /* make it immediately active */
2418 slave->state = BOND_STATE_ACTIVE;
2419 } else if (slave != bond->primary_slave) {
2420 /* prevent it from being the active one */
2421 slave->state = BOND_STATE_BACKUP;
2422 }
2423
Krzysztof Piotr Oledzki546add72010-10-06 14:28:22 -07002424 bond_update_speed_duplex(slave);
2425
Krzysztof Piotr Oledzki700c2a72010-10-06 14:25:06 -07002426 pr_info("%s: link status definitely up for interface %s, %d Mbps %s duplex.\n",
2427 bond->dev->name, slave->dev->name,
2428 slave->speed, slave->duplex ? "full" : "half");
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002429
2430 /* notify ad that the link status has changed */
2431 if (bond->params.mode == BOND_MODE_8023AD)
2432 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2433
Holger Eitzenberger58402052008-12-09 23:07:13 -08002434 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002435 bond_alb_handle_link_change(bond, slave,
2436 BOND_LINK_UP);
2437
2438 if (!bond->curr_active_slave ||
2439 (slave == bond->primary_slave))
2440 goto do_failover;
2441
2442 continue;
2443
2444 case BOND_LINK_DOWN:
Jay Vosburghfba4acd2008-10-30 17:41:14 -07002445 if (slave->link_failure_count < UINT_MAX)
2446 slave->link_failure_count++;
2447
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002448 slave->link = BOND_LINK_DOWN;
2449
2450 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2451 bond->params.mode == BOND_MODE_8023AD)
2452 bond_set_slave_inactive_flags(slave);
2453
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002454 pr_info("%s: link status definitely down for interface %s, disabling it\n",
2455 bond->dev->name, slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002456
2457 if (bond->params.mode == BOND_MODE_8023AD)
2458 bond_3ad_handle_link_change(slave,
2459 BOND_LINK_DOWN);
2460
Jiri Pirkoae63e802009-05-27 05:42:36 +00002461 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002462 bond_alb_handle_link_change(bond, slave,
2463 BOND_LINK_DOWN);
2464
2465 if (slave == bond->curr_active_slave)
2466 goto do_failover;
2467
2468 continue;
2469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002471 pr_err("%s: invalid new link %d on slave %s\n",
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002472 bond->dev->name, slave->new_link,
2473 slave->dev->name);
2474 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002476 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
2478
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002479do_failover:
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002480 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00002481 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002482 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 bond_select_active_slave(bond);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002484 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002485 unblock_netpoll_tx();
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002486 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002487
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002488 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489}
2490
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002491/*
2492 * bond_mii_monitor
2493 *
2494 * Really a wrapper that splits the mii monitor into two phases: an
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002495 * inspection, then (if inspection indicates something needs to be done)
2496 * an acquisition of appropriate locks followed by a commit phase to
2497 * implement whatever link state changes are indicated.
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002498 */
2499void bond_mii_monitor(struct work_struct *work)
2500{
2501 struct bonding *bond = container_of(work, struct bonding,
2502 mii_work.work);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002503
2504 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002505 if (bond->kill_timers)
2506 goto out;
2507
2508 if (bond->slave_cnt == 0)
2509 goto re_arm;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002510
2511 if (bond->send_grat_arp) {
2512 read_lock(&bond->curr_slave_lock);
2513 bond_send_gratuitous_arp(bond);
2514 read_unlock(&bond->curr_slave_lock);
2515 }
2516
Brian Haley305d5522008-11-04 17:51:14 -08002517 if (bond->send_unsol_na) {
2518 read_lock(&bond->curr_slave_lock);
2519 bond_send_unsolicited_na(bond);
2520 read_unlock(&bond->curr_slave_lock);
2521 }
2522
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002523 if (bond_miimon_inspect(bond)) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002524 read_unlock(&bond->lock);
2525 rtnl_lock();
2526 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002527
2528 bond_miimon_commit(bond);
2529
Jay Vosburgh56556622008-01-17 16:25:03 -08002530 read_unlock(&bond->lock);
2531 rtnl_unlock(); /* might sleep, hold no other locks */
2532 read_lock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002533 }
2534
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002535re_arm:
2536 if (bond->params.miimon)
2537 queue_delayed_work(bond->wq, &bond->mii_work,
2538 msecs_to_jiffies(bond->params.miimon));
2539out:
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002540 read_unlock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002541}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002542
Al Virod3bb52b2007-08-22 20:06:58 -04002543static __be32 bond_glean_dev_ip(struct net_device *dev)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002544{
2545 struct in_device *idev;
2546 struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07002547 __be32 addr = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002548
2549 if (!dev)
2550 return 0;
2551
2552 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002553 idev = __in_dev_get_rcu(dev);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002554 if (!idev)
2555 goto out;
2556
2557 ifa = idev->ifa_list;
2558 if (!ifa)
2559 goto out;
2560
2561 addr = ifa->ifa_local;
2562out:
2563 rcu_read_unlock();
2564 return addr;
2565}
2566
Al Virod3bb52b2007-08-22 20:06:58 -04002567static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002568{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002569 struct vlan_entry *vlan;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002570
2571 if (ip == bond->master_ip)
2572 return 1;
2573
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002574 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002575 if (ip == vlan->vlan_ip)
2576 return 1;
2577 }
2578
2579 return 0;
2580}
2581
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002582/*
2583 * We go to the (large) trouble of VLAN tagging ARP frames because
2584 * switches in VLAN mode (especially if ports are configured as
2585 * "native" to a VLAN) might not pass non-tagged frames.
2586 */
Al Virod3bb52b2007-08-22 20:06:58 -04002587static 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 -04002588{
2589 struct sk_buff *skb;
2590
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002591 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002592 slave_dev->name, dest_ip, src_ip, vlan_id);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002593
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002594 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2595 NULL, slave_dev->dev_addr, NULL);
2596
2597 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002598 pr_err("ARP packet allocation failed\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002599 return;
2600 }
2601 if (vlan_id) {
2602 skb = vlan_put_tag(skb, vlan_id);
2603 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002604 pr_err("failed to insert VLAN tag\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002605 return;
2606 }
2607 }
2608 arp_xmit(skb);
2609}
2610
2611
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2613{
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002614 int i, vlan_id, rv;
Al Virod3bb52b2007-08-22 20:06:58 -04002615 __be32 *targets = bond->params.arp_targets;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002616 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002617 struct net_device *vlan_dev;
2618 struct flowi fl;
2619 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Mitch Williams6b780562005-11-09 10:36:19 -08002621 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2622 if (!targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07002623 break;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002624 pr_debug("basa: target %x\n", targets[i]);
Jay Vosburghf35188f2010-07-21 12:14:47 +00002625 if (!bond->vlgrp) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002626 pr_debug("basa: empty vlan: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002627 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2628 bond->master_ip, 0);
2629 continue;
2630 }
2631
2632 /*
2633 * If VLANs are configured, we do a route lookup to
2634 * determine which VLAN interface would be used, so we
2635 * can tag the ARP with the proper VLAN tag.
2636 */
2637 memset(&fl, 0, sizeof(fl));
2638 fl.fl4_dst = targets[i];
2639 fl.fl4_tos = RTO_ONLINK;
2640
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00002641 rv = ip_route_output_key(dev_net(bond->dev), &rt, &fl);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002642 if (rv) {
2643 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002644 pr_warning("%s: no route to arp_ip_target %pI4\n",
2645 bond->dev->name, &fl.fl4_dst);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002646 }
2647 continue;
2648 }
2649
2650 /*
2651 * This target is not on a VLAN
2652 */
Changli Gaod8d1f302010-06-10 23:31:35 -07002653 if (rt->dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002654 ip_rt_put(rt);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002655 pr_debug("basa: rtdev == bond->dev: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002656 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2657 bond->master_ip, 0);
2658 continue;
2659 }
2660
2661 vlan_id = 0;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002662 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002663 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Changli Gaod8d1f302010-06-10 23:31:35 -07002664 if (vlan_dev == rt->dst.dev) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002665 vlan_id = vlan->vlan_id;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002666 pr_debug("basa: vlan match on %s %d\n",
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002667 vlan_dev->name, vlan_id);
2668 break;
2669 }
2670 }
2671
2672 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002673 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002674 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2675 vlan->vlan_ip, vlan_id);
2676 continue;
2677 }
2678
2679 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002680 pr_warning("%s: no path to arp_ip_target %pI4 via rt.dev %s\n",
2681 bond->dev->name, &fl.fl4_dst,
Changli Gaod8d1f302010-06-10 23:31:35 -07002682 rt->dst.dev ? rt->dst.dev->name : "NULL");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002683 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002684 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002685 }
2686}
2687
2688/*
2689 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2690 * for each VLAN above us.
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002691 *
2692 * Caller must hold curr_slave_lock for read or better
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002693 */
2694static void bond_send_gratuitous_arp(struct bonding *bond)
2695{
2696 struct slave *slave = bond->curr_active_slave;
2697 struct vlan_entry *vlan;
2698 struct net_device *vlan_dev;
2699
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002700 pr_debug("bond_send_grat_arp: bond %s slave %s\n",
2701 bond->dev->name, slave ? slave->dev->name : "NULL");
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002702
2703 if (!slave || !bond->send_grat_arp ||
2704 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002705 return;
2706
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002707 bond->send_grat_arp--;
2708
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002709 if (bond->master_ip) {
2710 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
Moni Shoua1053f622007-10-09 19:43:42 -07002711 bond->master_ip, 0);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002712 }
2713
Jay Vosburghf35188f2010-07-21 12:14:47 +00002714 if (!bond->vlgrp)
2715 return;
2716
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002717 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002718 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002719 if (vlan->vlan_ip) {
2720 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2721 vlan->vlan_ip, vlan->vlan_id);
2722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
2724}
2725
Al Virod3bb52b2007-08-22 20:06:58 -04002726static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002727{
2728 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002729 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002730
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002731 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002732 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002733 &sip, &tip, i, &targets[i],
2734 bond_has_this_ip(bond, tip));
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002735 if (sip == targets[i]) {
2736 if (bond_has_this_ip(bond, tip))
2737 slave->last_arp_rx = jiffies;
2738 return;
2739 }
2740 }
2741}
2742
2743static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2744{
2745 struct arphdr *arp;
2746 struct slave *slave;
2747 struct bonding *bond;
2748 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002749 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002750
Andy Gospodarek1f3c8802009-12-14 10:48:58 +00002751 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2752 /*
2753 * When using VLANS and bonding, dev and oriv_dev may be
2754 * incorrect if the physical interface supports VLAN
2755 * acceleration. With this change ARP validation now
2756 * works for hosts only reachable on the VLAN interface.
2757 */
2758 dev = vlan_dev_real_dev(dev);
2759 orig_dev = dev_get_by_index_rcu(dev_net(skb->dev),skb->skb_iif);
2760 }
2761
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002762 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2763 goto out;
2764
Wang Chen454d7c92008-11-12 23:37:49 -08002765 bond = netdev_priv(dev);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002766 read_lock(&bond->lock);
2767
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002768 pr_debug("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002769 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2770 orig_dev ? orig_dev->name : "NULL");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002771
2772 slave = bond_get_slave_by_dev(bond, orig_dev);
2773 if (!slave || !slave_do_arp_validate(bond, slave))
2774 goto out_unlock;
2775
Neil Hormanb3053252011-01-20 09:02:31 +00002776 skb = skb_share_check(skb, GFP_ATOMIC);
2777 if (!skb)
2778 goto out_unlock;
2779
Pavel Emelyanov988b7052008-03-03 12:20:57 -08002780 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002781 goto out_unlock;
2782
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002783 arp = arp_hdr(skb);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002784 if (arp->ar_hln != dev->addr_len ||
2785 skb->pkt_type == PACKET_OTHERHOST ||
2786 skb->pkt_type == PACKET_LOOPBACK ||
2787 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2788 arp->ar_pro != htons(ETH_P_IP) ||
2789 arp->ar_pln != 4)
2790 goto out_unlock;
2791
2792 arp_ptr = (unsigned char *)(arp + 1);
2793 arp_ptr += dev->addr_len;
2794 memcpy(&sip, arp_ptr, 4);
2795 arp_ptr += 4 + dev->addr_len;
2796 memcpy(&tip, arp_ptr, 4);
2797
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002798 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002799 bond->dev->name, slave->dev->name, slave->state,
2800 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2801 &sip, &tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002802
2803 /*
2804 * Backup slaves won't see the ARP reply, but do come through
2805 * here for each ARP probe (so we swap the sip/tip to validate
2806 * the probe). In a "redundant switch, common router" type of
2807 * configuration, the ARP probe will (hopefully) travel from
2808 * the active, through one switch, the router, then the other
2809 * switch before reaching the backup.
2810 */
2811 if (slave->state == BOND_STATE_ACTIVE)
2812 bond_validate_arp(bond, slave, sip, tip);
2813 else
2814 bond_validate_arp(bond, slave, tip, sip);
2815
2816out_unlock:
2817 read_unlock(&bond->lock);
2818out:
2819 dev_kfree_skb(skb);
2820 return NET_RX_SUCCESS;
2821}
2822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823/*
2824 * this function is called regularly to monitor each slave's link
2825 * ensuring that traffic is being sent and received when arp monitoring
2826 * is used in load-balancing mode. if the adapter has been dormant, then an
2827 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2828 * arp monitoring in active backup mode.
2829 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002830void bond_loadbalance_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002832 struct bonding *bond = container_of(work, struct bonding,
2833 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 struct slave *slave, *oldcurrent;
2835 int do_failover = 0;
2836 int delta_in_ticks;
2837 int i;
2838
2839 read_lock(&bond->lock);
2840
Jay Vosburgh5ce0da82008-05-17 21:10:07 -07002841 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002843 if (bond->kill_timers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002846 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 read_lock(&bond->curr_slave_lock);
2850 oldcurrent = bond->curr_active_slave;
2851 read_unlock(&bond->curr_slave_lock);
2852
2853 /* see if any of the previous devices are up now (i.e. they have
2854 * xmt and rcv traffic). the curr_active_slave does not come into
2855 * the picture unless it is null. also, slave->jiffies is not needed
2856 * here because we send an arp on each slave and give a slave as
2857 * long as it needs to get the tx/rx within the delta.
2858 * TODO: what about up/down delay in arp mode? it wasn't here before
2859 * so it can wait
2860 */
2861 bond_for_each_slave(bond, slave, i) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002862 unsigned long trans_start = dev_trans_start(slave->dev);
2863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002865 if (time_in_range(jiffies,
2866 trans_start - delta_in_ticks,
2867 trans_start + delta_in_ticks) &&
2868 time_in_range(jiffies,
2869 slave->dev->last_rx - delta_in_ticks,
2870 slave->dev->last_rx + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
2872 slave->link = BOND_LINK_UP;
2873 slave->state = BOND_STATE_ACTIVE;
2874
2875 /* primary_slave has no meaning in round-robin
2876 * mode. the window of a slave being up and
2877 * curr_active_slave being null after enslaving
2878 * is closed.
2879 */
2880 if (!oldcurrent) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002881 pr_info("%s: link status definitely up for interface %s, ",
2882 bond->dev->name,
2883 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 do_failover = 1;
2885 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002886 pr_info("%s: interface %s is now up\n",
2887 bond->dev->name,
2888 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 }
2890 }
2891 } else {
2892 /* slave->link == BOND_LINK_UP */
2893
2894 /* not all switches will respond to an arp request
2895 * when the source ip is 0, so don't take the link down
2896 * if we don't know our ip yet
2897 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002898 if (!time_in_range(jiffies,
2899 trans_start - delta_in_ticks,
2900 trans_start + 2 * delta_in_ticks) ||
2901 !time_in_range(jiffies,
2902 slave->dev->last_rx - delta_in_ticks,
2903 slave->dev->last_rx + 2 * delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
2905 slave->link = BOND_LINK_DOWN;
2906 slave->state = BOND_STATE_BACKUP;
2907
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002908 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002911 pr_info("%s: interface %s is now down.\n",
2912 bond->dev->name,
2913 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002915 if (slave == oldcurrent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 do_failover = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 }
2918 }
2919
2920 /* note: if switch is in round-robin mode, all links
2921 * must tx arp to ensure all links rx an arp - otherwise
2922 * links may oscillate or not come up at all; if switch is
2923 * in something like xor mode, there is nothing we can
2924 * do - all replies will be rx'ed on same link causing slaves
2925 * to be unstable during low/no traffic periods
2926 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002927 if (IS_UP(slave->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 bond_arp_send_all(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 }
2930
2931 if (do_failover) {
Neil Hormane843fa52010-10-13 16:01:50 +00002932 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002933 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
2935 bond_select_active_slave(bond);
2936
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002937 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002938 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 }
2940
2941re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002942 if (bond->params.arp_interval)
2943 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944out:
2945 read_unlock(&bond->lock);
2946}
2947
2948/*
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002949 * Called to inspect slaves for active-backup mode ARP monitor link state
2950 * changes. Sets new_link in slaves to specify what action should take
2951 * place for the slave. Returns 0 if no changes are found, >0 if changes
2952 * to link states must be committed.
2953 *
2954 * Called with bond->lock held for read.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002956static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 struct slave *slave;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002959 int i, commit = 0;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002960 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 bond_for_each_slave(bond, slave, i) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002963 slave->new_link = BOND_LINK_NOCHANGE;
2964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002966 if (time_in_range(jiffies,
2967 slave_last_rx(bond, slave) - delta_in_ticks,
2968 slave_last_rx(bond, slave) + delta_in_ticks)) {
2969
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002970 slave->new_link = BOND_LINK_UP;
2971 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002974 continue;
2975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002977 /*
2978 * Give slaves 2*delta after being enslaved or made
2979 * active. This avoids bouncing, as the last receive
2980 * times need a full ARP monitor cycle to be updated.
2981 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002982 if (time_in_range(jiffies,
2983 slave->jiffies - delta_in_ticks,
2984 slave->jiffies + 2 * delta_in_ticks))
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002985 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002987 /*
2988 * Backup slave is down if:
2989 * - No current_arp_slave AND
2990 * - more than 3*delta since last receive AND
2991 * - the bond has an IP address
2992 *
2993 * Note: a non-null current_arp_slave indicates
2994 * the curr_active_slave went down and we are
2995 * searching for a new one; under this condition
2996 * we only take the curr_active_slave down - this
2997 * gives each slave a chance to tx/rx traffic
2998 * before being taken out
2999 */
3000 if (slave->state == BOND_STATE_BACKUP &&
3001 !bond->current_arp_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003002 !time_in_range(jiffies,
3003 slave_last_rx(bond, slave) - delta_in_ticks,
3004 slave_last_rx(bond, slave) + 3 * delta_in_ticks)) {
3005
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003006 slave->new_link = BOND_LINK_DOWN;
3007 commit++;
3008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003010 /*
3011 * Active slave is down if:
3012 * - more than 2*delta since transmitting OR
3013 * - (more than 2*delta since receive AND
3014 * the bond has an IP address)
3015 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003016 trans_start = dev_trans_start(slave->dev);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003017 if ((slave->state == BOND_STATE_ACTIVE) &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003018 (!time_in_range(jiffies,
3019 trans_start - delta_in_ticks,
3020 trans_start + 2 * delta_in_ticks) ||
3021 !time_in_range(jiffies,
3022 slave_last_rx(bond, slave) - delta_in_ticks,
3023 slave_last_rx(bond, slave) + 2 * delta_in_ticks))) {
3024
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003025 slave->new_link = BOND_LINK_DOWN;
3026 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 }
3028 }
3029
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003030 return commit;
3031}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003033/*
3034 * Called to commit link state changes noted by inspection step of
3035 * active-backup mode ARP monitor.
3036 *
3037 * Called with RTNL and bond->lock for read.
3038 */
3039static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
3040{
3041 struct slave *slave;
3042 int i;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003043 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003045 bond_for_each_slave(bond, slave, i) {
3046 switch (slave->new_link) {
3047 case BOND_LINK_NOCHANGE:
3048 continue;
3049
3050 case BOND_LINK_UP:
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003051 trans_start = dev_trans_start(slave->dev);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003052 if ((!bond->curr_active_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003053 time_in_range(jiffies,
3054 trans_start - delta_in_ticks,
3055 trans_start + delta_in_ticks)) ||
Jiri Pirkob9f60252009-08-31 11:09:38 +00003056 bond->curr_active_slave != slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003057 slave->link = BOND_LINK_UP;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003058 bond->current_arp_slave = NULL;
3059
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003060 pr_info("%s: link status definitely up for interface %s.\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003061 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003062
Jiri Pirkob9f60252009-08-31 11:09:38 +00003063 if (!bond->curr_active_slave ||
3064 (slave == bond->primary_slave))
3065 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003066
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003067 }
3068
Jiri Pirkob9f60252009-08-31 11:09:38 +00003069 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003070
3071 case BOND_LINK_DOWN:
3072 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003075 slave->link = BOND_LINK_DOWN;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003076 bond_set_slave_inactive_flags(slave);
3077
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003078 pr_info("%s: link status definitely down for interface %s, disabling it\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003079 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003080
3081 if (slave == bond->curr_active_slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003082 bond->current_arp_slave = NULL;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003083 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003084 }
Jiri Pirkob9f60252009-08-31 11:09:38 +00003085
3086 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003087
3088 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003089 pr_err("%s: impossible: new_link %d on slave %s\n",
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003090 bond->dev->name, slave->new_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 slave->dev->name);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003092 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
Jiri Pirkob9f60252009-08-31 11:09:38 +00003095do_failover:
3096 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00003097 block_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003098 write_lock_bh(&bond->curr_slave_lock);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003099 bond_select_active_slave(bond);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003100 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00003101 unblock_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003102 }
3103
3104 bond_set_carrier(bond);
3105}
3106
3107/*
3108 * Send ARP probes for active-backup mode ARP monitor.
3109 *
3110 * Called with bond->lock held for read.
3111 */
3112static void bond_ab_arp_probe(struct bonding *bond)
3113{
3114 struct slave *slave;
3115 int i;
3116
3117 read_lock(&bond->curr_slave_lock);
3118
3119 if (bond->current_arp_slave && bond->curr_active_slave)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003120 pr_info("PROBE: c_arp %s && cas %s BAD\n",
3121 bond->current_arp_slave->dev->name,
3122 bond->curr_active_slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003123
3124 if (bond->curr_active_slave) {
3125 bond_arp_send_all(bond, bond->curr_active_slave);
3126 read_unlock(&bond->curr_slave_lock);
3127 return;
3128 }
3129
3130 read_unlock(&bond->curr_slave_lock);
3131
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 /* if we don't have a curr_active_slave, search for the next available
3133 * backup slave from the current_arp_slave and make it the candidate
3134 * for becoming the curr_active_slave
3135 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003136
3137 if (!bond->current_arp_slave) {
3138 bond->current_arp_slave = bond->first_slave;
3139 if (!bond->current_arp_slave)
3140 return;
3141 }
3142
3143 bond_set_slave_inactive_flags(bond->current_arp_slave);
3144
3145 /* search for next candidate */
3146 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3147 if (IS_UP(slave->dev)) {
3148 slave->link = BOND_LINK_BACK;
3149 bond_set_slave_active_flags(slave);
3150 bond_arp_send_all(bond, slave);
3151 slave->jiffies = jiffies;
3152 bond->current_arp_slave = slave;
3153 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 }
3155
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003156 /* if the link state is up at this point, we
3157 * mark it down - this can happen if we have
3158 * simultaneous link failures and
3159 * reselect_active_interface doesn't make this
3160 * one the current slave so it is still marked
3161 * up when it is actually down
3162 */
3163 if (slave->link == BOND_LINK_UP) {
3164 slave->link = BOND_LINK_DOWN;
3165 if (slave->link_failure_count < UINT_MAX)
3166 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003168 bond_set_slave_inactive_flags(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003170 pr_info("%s: backup interface %s is now down.\n",
3171 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 }
3173 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003174}
3175
3176void bond_activebackup_arp_mon(struct work_struct *work)
3177{
3178 struct bonding *bond = container_of(work, struct bonding,
3179 arp_work.work);
3180 int delta_in_ticks;
3181
3182 read_lock(&bond->lock);
3183
3184 if (bond->kill_timers)
3185 goto out;
3186
3187 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3188
3189 if (bond->slave_cnt == 0)
3190 goto re_arm;
3191
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003192 if (bond->send_grat_arp) {
3193 read_lock(&bond->curr_slave_lock);
3194 bond_send_gratuitous_arp(bond);
3195 read_unlock(&bond->curr_slave_lock);
3196 }
3197
Brian Haley305d5522008-11-04 17:51:14 -08003198 if (bond->send_unsol_na) {
3199 read_lock(&bond->curr_slave_lock);
3200 bond_send_unsolicited_na(bond);
3201 read_unlock(&bond->curr_slave_lock);
3202 }
3203
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003204 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3205 read_unlock(&bond->lock);
3206 rtnl_lock();
3207 read_lock(&bond->lock);
3208
3209 bond_ab_arp_commit(bond, delta_in_ticks);
3210
3211 read_unlock(&bond->lock);
3212 rtnl_unlock();
3213 read_lock(&bond->lock);
3214 }
3215
3216 bond_ab_arp_probe(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
3218re_arm:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003219 if (bond->params.arp_interval)
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003220 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221out:
3222 read_unlock(&bond->lock);
3223}
3224
3225/*------------------------------ proc/seq_file-------------------------------*/
3226
3227#ifdef CONFIG_PROC_FS
3228
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazete4a7b932010-10-29 01:52:46 +00003230 __acquires(RCU)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00003231 __acquires(&bond->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232{
3233 struct bonding *bond = seq->private;
3234 loff_t off = 0;
3235 struct slave *slave;
3236 int i;
3237
3238 /* make sure the bond won't be taken away */
Eric Dumazete4a7b932010-10-29 01:52:46 +00003239 rcu_read_lock();
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003240 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003242 if (*pos == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 return SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
3245 bond_for_each_slave(bond, slave, i) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003246 if (++off == *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 return slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 }
3249
3250 return NULL;
3251}
3252
3253static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3254{
3255 struct bonding *bond = seq->private;
3256 struct slave *slave = v;
3257
3258 ++*pos;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003259 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 return bond->first_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
3262 slave = slave->next;
3263
3264 return (slave == bond->first_slave) ? NULL : slave;
3265}
3266
3267static void bond_info_seq_stop(struct seq_file *seq, void *v)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00003268 __releases(&bond->lock)
Eric Dumazete4a7b932010-10-29 01:52:46 +00003269 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270{
3271 struct bonding *bond = seq->private;
3272
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003273 read_unlock(&bond->lock);
Eric Dumazete4a7b932010-10-29 01:52:46 +00003274 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275}
3276
3277static void bond_info_show_master(struct seq_file *seq)
3278{
3279 struct bonding *bond = seq->private;
3280 struct slave *curr;
Mitch Williams4756b022005-11-09 10:36:25 -08003281 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
3283 read_lock(&bond->curr_slave_lock);
3284 curr = bond->curr_active_slave;
3285 read_unlock(&bond->curr_slave_lock);
3286
Jay Vosburghdd957c52007-10-09 19:57:24 -07003287 seq_printf(seq, "Bonding Mode: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 bond_mode_name(bond->params.mode));
3289
Jay Vosburghdd957c52007-10-09 19:57:24 -07003290 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
3291 bond->params.fail_over_mac)
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07003292 seq_printf(seq, " (fail_over_mac %s)",
3293 fail_over_mac_tbl[bond->params.fail_over_mac].modename);
Jay Vosburghdd957c52007-10-09 19:57:24 -07003294
3295 seq_printf(seq, "\n");
3296
Mitch Williamsc61b75a2005-11-09 10:35:13 -08003297 if (bond->params.mode == BOND_MODE_XOR ||
3298 bond->params.mode == BOND_MODE_8023AD) {
3299 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3300 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3301 bond->params.xmit_policy);
3302 }
3303
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 if (USES_PRIMARY(bond->params.mode)) {
Jiri Pirkoa5499522009-09-25 03:28:09 +00003305 seq_printf(seq, "Primary Slave: %s",
Mitch Williams0f418b22005-11-09 10:35:21 -08003306 (bond->primary_slave) ?
3307 bond->primary_slave->dev->name : "None");
Jiri Pirkoa5499522009-09-25 03:28:09 +00003308 if (bond->primary_slave)
3309 seq_printf(seq, " (primary_reselect %s)",
3310 pri_reselect_tbl[bond->params.primary_reselect].modename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
Jiri Pirkoa5499522009-09-25 03:28:09 +00003312 seq_printf(seq, "\nCurrently Active Slave: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 (curr) ? curr->dev->name : "None");
3314 }
3315
Jay Vosburghff59c452006-03-27 13:27:43 -08003316 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3317 "up" : "down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3319 seq_printf(seq, "Up Delay (ms): %d\n",
3320 bond->params.updelay * bond->params.miimon);
3321 seq_printf(seq, "Down Delay (ms): %d\n",
3322 bond->params.downdelay * bond->params.miimon);
3323
Mitch Williams4756b022005-11-09 10:36:25 -08003324
3325 /* ARP information */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003326 if (bond->params.arp_interval > 0) {
3327 int printed = 0;
Mitch Williams4756b022005-11-09 10:36:25 -08003328 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3329 bond->params.arp_interval);
3330
3331 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3332
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003333 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
Mitch Williams4756b022005-11-09 10:36:25 -08003334 if (!bond->params.arp_targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07003335 break;
Mitch Williams4756b022005-11-09 10:36:25 -08003336 if (printed)
3337 seq_printf(seq, ",");
Harvey Harrison8cf14e32008-10-29 22:43:33 -07003338 seq_printf(seq, " %pI4", &bond->params.arp_targets[i]);
Mitch Williams4756b022005-11-09 10:36:25 -08003339 printed = 1;
3340 }
3341 seq_printf(seq, "\n");
3342 }
3343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 if (bond->params.mode == BOND_MODE_8023AD) {
3345 struct ad_info ad_info;
3346
3347 seq_puts(seq, "\n802.3ad info\n");
3348 seq_printf(seq, "LACP rate: %s\n",
3349 (bond->params.lacp_fast) ? "fast" : "slow");
Jay Vosburghfd989c82008-11-04 17:51:16 -08003350 seq_printf(seq, "Aggregator selection policy (ad_select): %s\n",
3351 ad_select_tbl[bond->params.ad_select].modename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
3353 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3354 seq_printf(seq, "bond %s has no active aggregator\n",
3355 bond->dev->name);
3356 } else {
3357 seq_printf(seq, "Active Aggregator Info:\n");
3358
3359 seq_printf(seq, "\tAggregator ID: %d\n",
3360 ad_info.aggregator_id);
3361 seq_printf(seq, "\tNumber of ports: %d\n",
3362 ad_info.ports);
3363 seq_printf(seq, "\tActor Key: %d\n",
3364 ad_info.actor_key);
3365 seq_printf(seq, "\tPartner Key: %d\n",
3366 ad_info.partner_key);
Johannes Berge1749612008-10-27 15:59:26 -07003367 seq_printf(seq, "\tPartner Mac Address: %pM\n",
3368 ad_info.partner_system);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 }
3370 }
3371}
3372
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003373static void bond_info_show_slave(struct seq_file *seq,
3374 const struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375{
3376 struct bonding *bond = seq->private;
3377
3378 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3379 seq_printf(seq, "MII Status: %s\n",
3380 (slave->link == BOND_LINK_UP) ? "up" : "down");
Krzysztof Oledzkidd53df22010-09-30 06:19:04 +00003381 seq_printf(seq, "Speed: %d Mbps\n", slave->speed);
3382 seq_printf(seq, "Duplex: %s\n", slave->duplex ? "full" : "half");
Kenzo Iwami655096452006-09-22 21:53:08 -07003383 seq_printf(seq, "Link Failure Count: %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 slave->link_failure_count);
3385
Johannes Berge1749612008-10-27 15:59:26 -07003386 seq_printf(seq, "Permanent HW addr: %pM\n", slave->perm_hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
3388 if (bond->params.mode == BOND_MODE_8023AD) {
3389 const struct aggregator *agg
3390 = SLAVE_AD_INFO(slave).port.aggregator;
3391
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003392 if (agg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 seq_printf(seq, "Aggregator ID: %d\n",
3394 agg->aggregator_identifier);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003395 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 seq_puts(seq, "Aggregator ID: N/A\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 }
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00003398 seq_printf(seq, "Slave queue ID: %d\n", slave->queue_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399}
3400
3401static int bond_info_seq_show(struct seq_file *seq, void *v)
3402{
3403 if (v == SEQ_START_TOKEN) {
3404 seq_printf(seq, "%s\n", version);
3405 bond_info_show_master(seq);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003406 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 bond_info_show_slave(seq, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
3409 return 0;
3410}
3411
Jan Engelhardt4101dec2009-01-14 13:52:18 -08003412static const struct seq_operations bond_info_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 .start = bond_info_seq_start,
3414 .next = bond_info_seq_next,
3415 .stop = bond_info_seq_stop,
3416 .show = bond_info_seq_show,
3417};
3418
3419static int bond_info_open(struct inode *inode, struct file *file)
3420{
3421 struct seq_file *seq;
3422 struct proc_dir_entry *proc;
3423 int res;
3424
3425 res = seq_open(file, &bond_info_seq_ops);
3426 if (!res) {
3427 /* recover the pointer buried in proc_dir_entry data */
3428 seq = file->private_data;
3429 proc = PDE(inode);
3430 seq->private = proc->data;
3431 }
3432
3433 return res;
3434}
3435
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08003436static const struct file_operations bond_info_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 .owner = THIS_MODULE,
3438 .open = bond_info_open,
3439 .read = seq_read,
3440 .llseek = seq_lseek,
3441 .release = seq_release,
3442};
3443
Nicolas de Pesloüan38fc0022009-10-13 00:45:06 -07003444static void bond_create_proc_entry(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445{
3446 struct net_device *bond_dev = bond->dev;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003447 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003449 if (bn->proc_dir) {
Denis V. Luneva95609c2008-04-29 01:02:29 -07003450 bond->proc_entry = proc_create_data(bond_dev->name,
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003451 S_IRUGO, bn->proc_dir,
Denis V. Luneva95609c2008-04-29 01:02:29 -07003452 &bond_info_fops, bond);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003453 if (bond->proc_entry == NULL)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003454 pr_warning("Warning: Cannot create /proc/net/%s/%s\n",
3455 DRV_NAME, bond_dev->name);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003456 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459}
3460
3461static void bond_remove_proc_entry(struct bonding *bond)
3462{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003463 struct net_device *bond_dev = bond->dev;
3464 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
3465
3466 if (bn->proc_dir && bond->proc_entry) {
3467 remove_proc_entry(bond->proc_file_name, bn->proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 memset(bond->proc_file_name, 0, IFNAMSIZ);
3469 bond->proc_entry = NULL;
3470 }
3471}
3472
3473/* Create the bonding directory under /proc/net, if doesn't exist yet.
3474 * Caller must hold rtnl_lock.
3475 */
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003476static void __net_init bond_create_proc_dir(struct bond_net *bn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003478 if (!bn->proc_dir) {
3479 bn->proc_dir = proc_mkdir(DRV_NAME, bn->net->proc_net);
3480 if (!bn->proc_dir)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003481 pr_warning("Warning: cannot create /proc/net/%s\n",
3482 DRV_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 }
3484}
3485
3486/* Destroy the bonding directory under /proc/net, if empty.
3487 * Caller must hold rtnl_lock.
3488 */
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003489static void __net_exit bond_destroy_proc_dir(struct bond_net *bn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003491 if (bn->proc_dir) {
3492 remove_proc_entry(DRV_NAME, bn->net->proc_net);
3493 bn->proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 }
3495}
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003496
3497#else /* !CONFIG_PROC_FS */
3498
Nicolas de Pesloüan38fc0022009-10-13 00:45:06 -07003499static void bond_create_proc_entry(struct bonding *bond)
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003500{
3501}
3502
3503static void bond_remove_proc_entry(struct bonding *bond)
3504{
3505}
3506
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003507static inline void bond_create_proc_dir(struct bond_net *bn)
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003508{
3509}
3510
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003511static inline void bond_destroy_proc_dir(struct bond_net *bn)
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003512{
3513}
3514
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515#endif /* CONFIG_PROC_FS */
3516
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003517
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518/*-------------------------- netdev event handling --------------------------*/
3519
3520/*
3521 * Change device name
3522 */
3523static int bond_event_changename(struct bonding *bond)
3524{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 bond_remove_proc_entry(bond);
3526 bond_create_proc_entry(bond);
Stephen Hemminger7e083842009-06-12 19:02:46 +00003527
Taku Izumif073c7c2010-12-09 15:17:13 +00003528 bond_debug_reregister(bond);
3529
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 return NOTIFY_DONE;
3531}
3532
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003533static int bond_master_netdev_event(unsigned long event,
3534 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535{
Wang Chen454d7c92008-11-12 23:37:49 -08003536 struct bonding *event_bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
3538 switch (event) {
3539 case NETDEV_CHANGENAME:
3540 return bond_event_changename(event_bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 default:
3542 break;
3543 }
3544
3545 return NOTIFY_DONE;
3546}
3547
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003548static int bond_slave_netdev_event(unsigned long event,
3549 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550{
3551 struct net_device *bond_dev = slave_dev->master;
Wang Chen454d7c92008-11-12 23:37:49 -08003552 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
3554 switch (event) {
3555 case NETDEV_UNREGISTER:
3556 if (bond_dev) {
Jay Vosburgh1284cd32007-10-15 16:44:27 -07003557 if (bond->setup_by_slave)
3558 bond_release_and_destroy(bond_dev, slave_dev);
3559 else
3560 bond_release(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 }
3562 break;
3563 case NETDEV_CHANGE:
Jay Vosburgh17d04502009-03-18 18:38:25 -07003564 if (bond->params.mode == BOND_MODE_8023AD || bond_is_lb(bond)) {
3565 struct slave *slave;
3566
3567 slave = bond_get_slave_by_dev(bond, slave_dev);
3568 if (slave) {
3569 u16 old_speed = slave->speed;
3570 u16 old_duplex = slave->duplex;
3571
3572 bond_update_speed_duplex(slave);
3573
3574 if (bond_is_lb(bond))
3575 break;
3576
3577 if (old_speed != slave->speed)
3578 bond_3ad_adapter_speed_changed(slave);
3579 if (old_duplex != slave->duplex)
3580 bond_3ad_adapter_duplex_changed(slave);
3581 }
3582 }
3583
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 break;
3585 case NETDEV_DOWN:
3586 /*
3587 * ... Or is it this?
3588 */
3589 break;
3590 case NETDEV_CHANGEMTU:
3591 /*
3592 * TODO: Should slaves be allowed to
3593 * independently alter their MTU? For
3594 * an active-backup bond, slaves need
3595 * not be the same type of device, so
3596 * MTUs may vary. For other modes,
3597 * slaves arguably should have the
3598 * same MTUs. To do this, we'd need to
3599 * take over the slave's change_mtu
3600 * function for the duration of their
3601 * servitude.
3602 */
3603 break;
3604 case NETDEV_CHANGENAME:
3605 /*
3606 * TODO: handle changing the primary's name
3607 */
3608 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003609 case NETDEV_FEAT_CHANGE:
3610 bond_compute_features(bond);
3611 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 default:
3613 break;
3614 }
3615
3616 return NOTIFY_DONE;
3617}
3618
3619/*
3620 * bond_netdev_event: handle netdev notifier chain events.
3621 *
3622 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003623 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 * locks for us to safely manipulate the slave devices (RTNL lock,
3625 * dev_probe_lock).
3626 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003627static int bond_netdev_event(struct notifier_block *this,
3628 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{
3630 struct net_device *event_dev = (struct net_device *)ptr;
3631
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003632 pr_debug("event_dev: %s, event: %lx\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003633 event_dev ? event_dev->name : "None",
3634 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003636 if (!(event_dev->priv_flags & IFF_BONDING))
3637 return NOTIFY_DONE;
3638
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 if (event_dev->flags & IFF_MASTER) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003640 pr_debug("IFF_MASTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 return bond_master_netdev_event(event, event_dev);
3642 }
3643
3644 if (event_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003645 pr_debug("IFF_SLAVE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 return bond_slave_netdev_event(event, event_dev);
3647 }
3648
3649 return NOTIFY_DONE;
3650}
3651
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003652/*
3653 * bond_inetaddr_event: handle inetaddr notifier chain events.
3654 *
3655 * We keep track of device IPs primarily to use as source addresses in
3656 * ARP monitor probes (rather than spewing out broadcasts all the time).
3657 *
3658 * We track one IP for the main device (if it has one), plus one per VLAN.
3659 */
3660static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3661{
3662 struct in_ifaddr *ifa = ptr;
3663 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003664 struct bond_net *bn = net_generic(dev_net(event_dev), bond_net_id);
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003665 struct bonding *bond;
3666 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003667
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003668 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003669 if (bond->dev == event_dev) {
3670 switch (event) {
3671 case NETDEV_UP:
3672 bond->master_ip = ifa->ifa_local;
3673 return NOTIFY_OK;
3674 case NETDEV_DOWN:
3675 bond->master_ip = bond_glean_dev_ip(bond->dev);
3676 return NOTIFY_OK;
3677 default:
3678 return NOTIFY_DONE;
3679 }
3680 }
3681
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003682 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +00003683 if (!bond->vlgrp)
3684 continue;
Dan Aloni5c15bde2007-03-02 20:44:51 -08003685 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003686 if (vlan_dev == event_dev) {
3687 switch (event) {
3688 case NETDEV_UP:
3689 vlan->vlan_ip = ifa->ifa_local;
3690 return NOTIFY_OK;
3691 case NETDEV_DOWN:
3692 vlan->vlan_ip =
3693 bond_glean_dev_ip(vlan_dev);
3694 return NOTIFY_OK;
3695 default:
3696 return NOTIFY_DONE;
3697 }
3698 }
3699 }
3700 }
3701 return NOTIFY_DONE;
3702}
3703
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704static struct notifier_block bond_netdev_notifier = {
3705 .notifier_call = bond_netdev_event,
3706};
3707
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003708static struct notifier_block bond_inetaddr_notifier = {
3709 .notifier_call = bond_inetaddr_event,
3710};
3711
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712/*-------------------------- Packet type handling ---------------------------*/
3713
3714/* register to receive lacpdus on a bond */
3715static void bond_register_lacpdu(struct bonding *bond)
3716{
3717 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3718
3719 /* initialize packet type */
3720 pk_type->type = PKT_TYPE_LACPDU;
3721 pk_type->dev = bond->dev;
3722 pk_type->func = bond_3ad_lacpdu_recv;
3723
3724 dev_add_pack(pk_type);
3725}
3726
3727/* unregister to receive lacpdus on a bond */
3728static void bond_unregister_lacpdu(struct bonding *bond)
3729{
3730 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3731}
3732
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003733void bond_register_arp(struct bonding *bond)
3734{
3735 struct packet_type *pt = &bond->arp_mon_pt;
3736
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003737 if (pt->type)
3738 return;
3739
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003740 pt->type = htons(ETH_P_ARP);
Jay Vosburghe245cb72007-02-28 17:03:27 -08003741 pt->dev = bond->dev;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003742 pt->func = bond_arp_rcv;
3743 dev_add_pack(pt);
3744}
3745
3746void bond_unregister_arp(struct bonding *bond)
3747{
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003748 struct packet_type *pt = &bond->arp_mon_pt;
3749
3750 dev_remove_pack(pt);
3751 pt->type = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003752}
3753
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003754/*---------------------------- Hashing Policies -----------------------------*/
3755
3756/*
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003757 * Hash for the output device based upon layer 2 and layer 3 data. If
3758 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3759 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003760static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003761{
3762 struct ethhdr *data = (struct ethhdr *)skb->data;
3763 struct iphdr *iph = ip_hdr(skb);
3764
Brian Haleyf14c4e42008-09-02 10:08:08 -04003765 if (skb->protocol == htons(ETH_P_IP)) {
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003766 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
Jasper Spaansd3da6832009-10-23 04:08:46 +00003767 (data->h_dest[5] ^ data->h_source[5])) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003768 }
3769
Jasper Spaansd3da6832009-10-23 04:08:46 +00003770 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003771}
3772
3773/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003774 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003775 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3776 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3777 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003778static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003779{
3780 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003781 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003782 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003783 int layer4_xor = 0;
3784
Brian Haleyf14c4e42008-09-02 10:08:08 -04003785 if (skb->protocol == htons(ETH_P_IP)) {
3786 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003787 (iph->protocol == IPPROTO_TCP ||
3788 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003789 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003790 }
3791 return (layer4_xor ^
3792 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3793
3794 }
3795
Jasper Spaansd3da6832009-10-23 04:08:46 +00003796 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003797}
3798
3799/*
3800 * Hash for the output device based upon layer 2 data
3801 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003802static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003803{
3804 struct ethhdr *data = (struct ethhdr *)skb->data;
3805
Jasper Spaansd3da6832009-10-23 04:08:46 +00003806 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003807}
3808
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809/*-------------------------- Device entry points ----------------------------*/
3810
3811static int bond_open(struct net_device *bond_dev)
3812{
Wang Chen454d7c92008-11-12 23:37:49 -08003813 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
3815 bond->kill_timers = 0;
3816
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003817 INIT_DELAYED_WORK(&bond->mcast_work, bond_resend_igmp_join_requests_delayed);
3818
Holger Eitzenberger58402052008-12-09 23:07:13 -08003819 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 /* bond_alb_initialize must be called before the timer
3821 * is started.
3822 */
3823 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3824 /* something went wrong - fail the open operation */
stephen hemmingerb4739462010-01-25 23:34:15 +00003825 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 }
3827
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003828 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3829 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 }
3831
3832 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003833 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3834 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 }
3836
3837 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003838 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3839 INIT_DELAYED_WORK(&bond->arp_work,
3840 bond_activebackup_arp_mon);
3841 else
3842 INIT_DELAYED_WORK(&bond->arp_work,
3843 bond_loadbalance_arp_mon);
3844
3845 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003846 if (bond->params.arp_validate)
3847 bond_register_arp(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 }
3849
3850 if (bond->params.mode == BOND_MODE_8023AD) {
Adrian Bunka40745f2007-10-24 18:27:43 +02003851 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003852 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 /* register to receive LACPDUs */
3854 bond_register_lacpdu(bond);
Jay Vosburghfd989c82008-11-04 17:51:16 -08003855 bond_3ad_initiate_agg_selection(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 }
3857
3858 return 0;
3859}
3860
3861static int bond_close(struct net_device *bond_dev)
3862{
Wang Chen454d7c92008-11-12 23:37:49 -08003863 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864
3865 if (bond->params.mode == BOND_MODE_8023AD) {
3866 /* Unregister the receive of LACPDUs */
3867 bond_unregister_lacpdu(bond);
3868 }
3869
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003870 if (bond->params.arp_validate)
3871 bond_unregister_arp(bond);
3872
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 write_lock_bh(&bond->lock);
3874
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003875 bond->send_grat_arp = 0;
Brian Haley305d5522008-11-04 17:51:14 -08003876 bond->send_unsol_na = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
3878 /* signal timers not to re-arm */
3879 bond->kill_timers = 1;
3880
3881 write_unlock_bh(&bond->lock);
3882
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003884 cancel_delayed_work(&bond->mii_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 }
3886
3887 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003888 cancel_delayed_work(&bond->arp_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 }
3890
3891 switch (bond->params.mode) {
3892 case BOND_MODE_8023AD:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003893 cancel_delayed_work(&bond->ad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 break;
3895 case BOND_MODE_TLB:
3896 case BOND_MODE_ALB:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003897 cancel_delayed_work(&bond->alb_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 break;
3899 default:
3900 break;
3901 }
3902
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003903 if (delayed_work_pending(&bond->mcast_work))
3904 cancel_delayed_work(&bond->mcast_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
Holger Eitzenberger58402052008-12-09 23:07:13 -08003906 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 /* Must be called only after all
3908 * slaves have been released
3909 */
3910 bond_alb_deinitialize(bond);
3911 }
3912
3913 return 0;
3914}
3915
Eric Dumazet28172732010-07-07 14:58:56 -07003916static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3917 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918{
Wang Chen454d7c92008-11-12 23:37:49 -08003919 struct bonding *bond = netdev_priv(bond_dev);
Eric Dumazet28172732010-07-07 14:58:56 -07003920 struct rtnl_link_stats64 temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 struct slave *slave;
3922 int i;
3923
Eric Dumazet28172732010-07-07 14:58:56 -07003924 memset(stats, 0, sizeof(*stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
3926 read_lock_bh(&bond->lock);
3927
3928 bond_for_each_slave(bond, slave, i) {
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00003929 const struct rtnl_link_stats64 *sstats =
Eric Dumazet28172732010-07-07 14:58:56 -07003930 dev_get_stats(slave->dev, &temp);
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08003931
Eric Dumazet28172732010-07-07 14:58:56 -07003932 stats->rx_packets += sstats->rx_packets;
3933 stats->rx_bytes += sstats->rx_bytes;
3934 stats->rx_errors += sstats->rx_errors;
3935 stats->rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
Eric Dumazet28172732010-07-07 14:58:56 -07003937 stats->tx_packets += sstats->tx_packets;
3938 stats->tx_bytes += sstats->tx_bytes;
3939 stats->tx_errors += sstats->tx_errors;
3940 stats->tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
Eric Dumazet28172732010-07-07 14:58:56 -07003942 stats->multicast += sstats->multicast;
3943 stats->collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
Eric Dumazet28172732010-07-07 14:58:56 -07003945 stats->rx_length_errors += sstats->rx_length_errors;
3946 stats->rx_over_errors += sstats->rx_over_errors;
3947 stats->rx_crc_errors += sstats->rx_crc_errors;
3948 stats->rx_frame_errors += sstats->rx_frame_errors;
3949 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3950 stats->rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
Eric Dumazet28172732010-07-07 14:58:56 -07003952 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3953 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3954 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3955 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3956 stats->tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 }
3958
3959 read_unlock_bh(&bond->lock);
3960
3961 return stats;
3962}
3963
3964static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3965{
3966 struct net_device *slave_dev = NULL;
3967 struct ifbond k_binfo;
3968 struct ifbond __user *u_binfo = NULL;
3969 struct ifslave k_sinfo;
3970 struct ifslave __user *u_sinfo = NULL;
3971 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 int res = 0;
3973
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003974 pr_debug("bond_ioctl: master=%s, cmd=%d\n", bond_dev->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975
3976 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 case SIOCGMIIPHY:
3978 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003979 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003981
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 mii->phy_id = 0;
3983 /* Fall Through */
3984 case SIOCGMIIREG:
3985 /*
3986 * We do this again just in case we were called by SIOCGMIIREG
3987 * instead of SIOCGMIIPHY.
3988 */
3989 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003990 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003992
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
3994 if (mii->reg_num == 1) {
Wang Chen454d7c92008-11-12 23:37:49 -08003995 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 mii->val_out = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003997 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 read_lock(&bond->curr_slave_lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003999 if (netif_carrier_ok(bond->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 mii->val_out = BMSR_LSTATUS;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004001
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 read_unlock(&bond->curr_slave_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07004003 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 }
4005
4006 return 0;
4007 case BOND_INFO_QUERY_OLD:
4008 case SIOCBONDINFOQUERY:
4009 u_binfo = (struct ifbond __user *)ifr->ifr_data;
4010
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004011 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
4014 res = bond_info_query(bond_dev, &k_binfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004015 if (res == 0 &&
4016 copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
4017 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018
4019 return res;
4020 case BOND_SLAVE_INFO_QUERY_OLD:
4021 case SIOCBONDSLAVEINFOQUERY:
4022 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
4023
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004024 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026
4027 res = bond_slave_info_query(bond_dev, &k_sinfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004028 if (res == 0 &&
4029 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
4030 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
4032 return res;
4033 default:
4034 /* Go on */
4035 break;
4036 }
4037
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004038 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004041 slave_dev = dev_get_by_name(dev_net(bond_dev), ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004043 pr_debug("slave_dev=%p:\n", slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004045 if (!slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 res = -ENODEV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004047 else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004048 pr_debug("slave_dev->name=%s:\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 switch (cmd) {
4050 case BOND_ENSLAVE_OLD:
4051 case SIOCBONDENSLAVE:
4052 res = bond_enslave(bond_dev, slave_dev);
4053 break;
4054 case BOND_RELEASE_OLD:
4055 case SIOCBONDRELEASE:
4056 res = bond_release(bond_dev, slave_dev);
4057 break;
4058 case BOND_SETHWADDR_OLD:
4059 case SIOCBONDSETHWADDR:
4060 res = bond_sethwaddr(bond_dev, slave_dev);
4061 break;
4062 case BOND_CHANGE_ACTIVE_OLD:
4063 case SIOCBONDCHANGEACTIVE:
4064 res = bond_ioctl_change_active(bond_dev, slave_dev);
4065 break;
4066 default:
4067 res = -EOPNOTSUPP;
4068 }
4069
4070 dev_put(slave_dev);
4071 }
4072
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 return res;
4074}
4075
Jiri Pirko22bedad32010-04-01 21:22:57 +00004076static bool bond_addr_in_mc_list(unsigned char *addr,
4077 struct netdev_hw_addr_list *list,
4078 int addrlen)
4079{
4080 struct netdev_hw_addr *ha;
4081
4082 netdev_hw_addr_list_for_each(ha, list)
4083 if (!memcmp(ha->addr, addr, addrlen))
4084 return true;
4085
4086 return false;
4087}
4088
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089static void bond_set_multicast_list(struct net_device *bond_dev)
4090{
Wang Chen454d7c92008-11-12 23:37:49 -08004091 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00004092 struct netdev_hw_addr *ha;
4093 bool found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 /*
4096 * Do promisc before checking multicast_mode
4097 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004098 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07004099 /*
4100 * FIXME: Need to handle the error when one of the multi-slaves
4101 * encounters error.
4102 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 bond_set_promiscuity(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004105
4106 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 bond_set_promiscuity(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004108
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
4110 /* set allmulti flag to slaves */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004111 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07004112 /*
4113 * FIXME: Need to handle the error when one of the multi-slaves
4114 * encounters error.
4115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 bond_set_allmulti(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004118
4119 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 bond_set_allmulti(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004121
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004123 read_lock(&bond->lock);
4124
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 bond->flags = bond_dev->flags;
4126
4127 /* looking for addresses to add to slaves' mc list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004128 netdev_for_each_mc_addr(ha, bond_dev) {
4129 found = bond_addr_in_mc_list(ha->addr, &bond->mc_list,
4130 bond_dev->addr_len);
4131 if (!found)
4132 bond_mc_add(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 }
4134
4135 /* looking for addresses to delete from slaves' list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004136 netdev_hw_addr_list_for_each(ha, &bond->mc_list) {
4137 found = bond_addr_in_mc_list(ha->addr, &bond_dev->mc,
4138 bond_dev->addr_len);
4139 if (!found)
4140 bond_mc_del(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 }
4142
4143 /* save master's multicast list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004144 __hw_addr_flush(&bond->mc_list);
4145 __hw_addr_add_multiple(&bond->mc_list, &bond_dev->mc,
4146 bond_dev->addr_len, NETDEV_HW_ADDR_T_MULTICAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004148 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149}
4150
Stephen Hemminger00829822008-11-20 20:14:53 -08004151static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
4152{
4153 struct bonding *bond = netdev_priv(dev);
4154 struct slave *slave = bond->first_slave;
4155
4156 if (slave) {
4157 const struct net_device_ops *slave_ops
4158 = slave->dev->netdev_ops;
4159 if (slave_ops->ndo_neigh_setup)
Patrick McHardy72e22402009-03-05 01:57:44 -08004160 return slave_ops->ndo_neigh_setup(slave->dev, parms);
Stephen Hemminger00829822008-11-20 20:14:53 -08004161 }
4162 return 0;
4163}
4164
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165/*
4166 * Change the MTU of all of a master's slaves to match the master
4167 */
4168static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
4169{
Wang Chen454d7c92008-11-12 23:37:49 -08004170 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 struct slave *slave, *stop_at;
4172 int res = 0;
4173 int i;
4174
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004175 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004176 (bond_dev ? bond_dev->name : "None"), new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
4178 /* Can't hold bond->lock with bh disabled here since
4179 * some base drivers panic. On the other hand we can't
4180 * hold bond->lock without bh disabled because we'll
4181 * deadlock. The only solution is to rely on the fact
4182 * that we're under rtnl_lock here, and the slaves
4183 * list won't change. This doesn't solve the problem
4184 * of setting the slave's MTU while it is
4185 * transmitting, but the assumption is that the base
4186 * driver can handle that.
4187 *
4188 * TODO: figure out a way to safely iterate the slaves
4189 * list, but without holding a lock around the actual
4190 * call to the base driver.
4191 */
4192
4193 bond_for_each_slave(bond, slave, i) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004194 pr_debug("s %p s->p %p c_m %p\n",
4195 slave,
4196 slave->prev,
4197 slave->dev->netdev_ops->ndo_change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08004198
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 res = dev_set_mtu(slave->dev, new_mtu);
4200
4201 if (res) {
4202 /* If we failed to set the slave's mtu to the new value
4203 * we must abort the operation even in ACTIVE_BACKUP
4204 * mode, because if we allow the backup slaves to have
4205 * different mtu values than the active slave we'll
4206 * need to change their mtu when doing a failover. That
4207 * means changing their mtu from timer context, which
4208 * is probably not a good idea.
4209 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004210 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 goto unwind;
4212 }
4213 }
4214
4215 bond_dev->mtu = new_mtu;
4216
4217 return 0;
4218
4219unwind:
4220 /* unwind from head to the slave that failed */
4221 stop_at = slave;
4222 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4223 int tmp_res;
4224
4225 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4226 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004227 pr_debug("unwind err %d dev %s\n",
4228 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 }
4230 }
4231
4232 return res;
4233}
4234
4235/*
4236 * Change HW address
4237 *
4238 * Note that many devices must be down to change the HW address, and
4239 * downing the master releases all slaves. We can make bonds full of
4240 * bonding devices to test this, however.
4241 */
4242static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4243{
Wang Chen454d7c92008-11-12 23:37:49 -08004244 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 struct sockaddr *sa = addr, tmp_sa;
4246 struct slave *slave, *stop_at;
4247 int res = 0;
4248 int i;
4249
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004250 if (bond->params.mode == BOND_MODE_ALB)
4251 return bond_alb_set_mac_address(bond_dev, addr);
4252
4253
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004254 pr_debug("bond=%p, name=%s\n",
4255 bond, bond_dev ? bond_dev->name : "None");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
Jay Vosburghdd957c52007-10-09 19:57:24 -07004257 /*
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004258 * If fail_over_mac is set to active, do nothing and return
4259 * success. Returning an error causes ifenslave to fail.
Jay Vosburghdd957c52007-10-09 19:57:24 -07004260 */
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004261 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
Jay Vosburghdd957c52007-10-09 19:57:24 -07004262 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07004263
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004264 if (!is_valid_ether_addr(sa->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
4267 /* Can't hold bond->lock with bh disabled here since
4268 * some base drivers panic. On the other hand we can't
4269 * hold bond->lock without bh disabled because we'll
4270 * deadlock. The only solution is to rely on the fact
4271 * that we're under rtnl_lock here, and the slaves
4272 * list won't change. This doesn't solve the problem
4273 * of setting the slave's hw address while it is
4274 * transmitting, but the assumption is that the base
4275 * driver can handle that.
4276 *
4277 * TODO: figure out a way to safely iterate the slaves
4278 * list, but without holding a lock around the actual
4279 * call to the base driver.
4280 */
4281
4282 bond_for_each_slave(bond, slave, i) {
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004283 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004284 pr_debug("slave %p %s\n", slave, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004286 if (slave_ops->ndo_set_mac_address == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 res = -EOPNOTSUPP;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004288 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 goto unwind;
4290 }
4291
4292 res = dev_set_mac_address(slave->dev, addr);
4293 if (res) {
4294 /* TODO: consider downing the slave
4295 * and retry ?
4296 * User should expect communications
4297 * breakage anyway until ARP finish
4298 * updating, so...
4299 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004300 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 goto unwind;
4302 }
4303 }
4304
4305 /* success */
4306 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4307 return 0;
4308
4309unwind:
4310 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4311 tmp_sa.sa_family = bond_dev->type;
4312
4313 /* unwind from head to the slave that failed */
4314 stop_at = slave;
4315 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4316 int tmp_res;
4317
4318 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4319 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004320 pr_debug("unwind err %d dev %s\n",
4321 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 }
4323 }
4324
4325 return res;
4326}
4327
4328static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4329{
Wang Chen454d7c92008-11-12 23:37:49 -08004330 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 struct slave *slave, *start_at;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004332 int i, slave_no, res = 1;
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004333 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
4335 read_lock(&bond->lock);
4336
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004337 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 goto out;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004339 /*
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004340 * Start with the curr_active_slave that joined the bond as the
4341 * default for sending IGMP traffic. For failover purposes one
4342 * needs to maintain some consistency for the interface that will
4343 * send the join/membership reports. The curr_active_slave found
4344 * will send all of this type of traffic.
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004345 */
Eric Dumazet00ae7022010-03-30 23:08:37 +00004346 if ((iph->protocol == IPPROTO_IGMP) &&
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004347 (skb->protocol == htons(ETH_P_IP))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004349 read_lock(&bond->curr_slave_lock);
4350 slave = bond->curr_active_slave;
4351 read_unlock(&bond->curr_slave_lock);
4352
4353 if (!slave)
4354 goto out;
4355 } else {
4356 /*
4357 * Concurrent TX may collide on rr_tx_counter; we accept
4358 * that as being rare enough not to justify using an
4359 * atomic op here.
4360 */
4361 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
4362
4363 bond_for_each_slave(bond, slave, i) {
4364 slave_no--;
4365 if (slave_no < 0)
4366 break;
4367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 }
4369
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004370 start_at = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 bond_for_each_slave_from(bond, slave, i, start_at) {
4372 if (IS_UP(slave->dev) &&
4373 (slave->link == BOND_LINK_UP) &&
4374 (slave->state == BOND_STATE_ACTIVE)) {
4375 res = bond_dev_queue_xmit(bond, skb, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 break;
4377 }
4378 }
4379
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380out:
4381 if (res) {
4382 /* no suitable interface, frame not sent */
4383 dev_kfree_skb(skb);
4384 }
4385 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004386 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387}
4388
John W. Linville075897c2005-09-28 17:50:53 -04004389
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390/*
4391 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4392 * the bond has a usable interface.
4393 */
4394static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4395{
Wang Chen454d7c92008-11-12 23:37:49 -08004396 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 int res = 1;
4398
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 read_lock(&bond->lock);
4400 read_lock(&bond->curr_slave_lock);
4401
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004402 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
John W. Linville075897c2005-09-28 17:50:53 -04004405 if (!bond->curr_active_slave)
4406 goto out;
4407
John W. Linville075897c2005-09-28 17:50:53 -04004408 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4409
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004411 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 /* no suitable interface, frame not sent */
4413 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004414
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 read_unlock(&bond->curr_slave_lock);
4416 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004417 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418}
4419
4420/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004421 * In bond_xmit_xor() , we determine the output device by using a pre-
4422 * determined xmit_hash_policy(), If the selected device is not enabled,
4423 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 */
4425static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4426{
Wang Chen454d7c92008-11-12 23:37:49 -08004427 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 struct slave *slave, *start_at;
4429 int slave_no;
4430 int i;
4431 int res = 1;
4432
4433 read_lock(&bond->lock);
4434
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004435 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
Jasper Spaansa361c832009-10-23 04:09:24 +00004438 slave_no = bond->xmit_hash_policy(skb, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
4440 bond_for_each_slave(bond, slave, i) {
4441 slave_no--;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004442 if (slave_no < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 }
4445
4446 start_at = slave;
4447
4448 bond_for_each_slave_from(bond, slave, i, start_at) {
4449 if (IS_UP(slave->dev) &&
4450 (slave->link == BOND_LINK_UP) &&
4451 (slave->state == BOND_STATE_ACTIVE)) {
4452 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4453 break;
4454 }
4455 }
4456
4457out:
4458 if (res) {
4459 /* no suitable interface, frame not sent */
4460 dev_kfree_skb(skb);
4461 }
4462 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004463 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464}
4465
4466/*
4467 * in broadcast mode, we send everything to all usable interfaces.
4468 */
4469static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4470{
Wang Chen454d7c92008-11-12 23:37:49 -08004471 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 struct slave *slave, *start_at;
4473 struct net_device *tx_dev = NULL;
4474 int i;
4475 int res = 1;
4476
4477 read_lock(&bond->lock);
4478
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004479 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
4482 read_lock(&bond->curr_slave_lock);
4483 start_at = bond->curr_active_slave;
4484 read_unlock(&bond->curr_slave_lock);
4485
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004486 if (!start_at)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
4489 bond_for_each_slave_from(bond, slave, i, start_at) {
4490 if (IS_UP(slave->dev) &&
4491 (slave->link == BOND_LINK_UP) &&
4492 (slave->state == BOND_STATE_ACTIVE)) {
4493 if (tx_dev) {
4494 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4495 if (!skb2) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004496 pr_err("%s: Error: bond_xmit_broadcast(): skb_clone() failed\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08004497 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 continue;
4499 }
4500
4501 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4502 if (res) {
4503 dev_kfree_skb(skb2);
4504 continue;
4505 }
4506 }
4507 tx_dev = slave->dev;
4508 }
4509 }
4510
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004511 if (tx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 res = bond_dev_queue_xmit(bond, skb, tx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
4514out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004515 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 /* no suitable interface, frame not sent */
4517 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004518
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 /* frame sent to all suitable interfaces */
4520 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004521 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522}
4523
4524/*------------------------- Device initialization ---------------------------*/
4525
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004526static void bond_set_xmit_hash_policy(struct bonding *bond)
4527{
4528 switch (bond->params.xmit_policy) {
4529 case BOND_XMIT_POLICY_LAYER23:
4530 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4531 break;
4532 case BOND_XMIT_POLICY_LAYER34:
4533 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4534 break;
4535 case BOND_XMIT_POLICY_LAYER2:
4536 default:
4537 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4538 break;
4539 }
4540}
4541
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004542/*
4543 * Lookup the slave that corresponds to a qid
4544 */
4545static inline int bond_slave_override(struct bonding *bond,
4546 struct sk_buff *skb)
4547{
4548 int i, res = 1;
4549 struct slave *slave = NULL;
4550 struct slave *check_slave;
4551
4552 read_lock(&bond->lock);
4553
4554 if (!BOND_IS_OK(bond) || !skb->queue_mapping)
4555 goto out;
4556
4557 /* Find out if any slaves have the same mapping as this skb. */
4558 bond_for_each_slave(bond, check_slave, i) {
4559 if (check_slave->queue_id == skb->queue_mapping) {
4560 slave = check_slave;
4561 break;
4562 }
4563 }
4564
4565 /* If the slave isn't UP, use default transmit policy. */
4566 if (slave && slave->queue_id && IS_UP(slave->dev) &&
4567 (slave->link == BOND_LINK_UP)) {
4568 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4569 }
4570
4571out:
4572 read_unlock(&bond->lock);
4573 return res;
4574}
4575
4576static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb)
4577{
4578 /*
4579 * This helper function exists to help dev_pick_tx get the correct
4580 * destination queue. Using a helper function skips the a call to
4581 * skb_tx_hash and will put the skbs in the queue we expect on their
4582 * way down to the bonding driver.
4583 */
4584 return skb->queue_mapping;
4585}
4586
Stephen Hemminger424efe92009-08-31 19:50:51 +00004587static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
Stephen Hemminger00829822008-11-20 20:14:53 -08004588{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004589 struct bonding *bond = netdev_priv(dev);
4590
Neil Hormane843fa52010-10-13 16:01:50 +00004591 /*
4592 * If we risk deadlock from transmitting this in the
4593 * netpoll path, tell netpoll to queue the frame for later tx
4594 */
4595 if (is_netpoll_tx_blocked(dev))
4596 return NETDEV_TX_BUSY;
4597
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004598 if (TX_QUEUE_OVERRIDE(bond->params.mode)) {
4599 if (!bond_slave_override(bond, skb))
4600 return NETDEV_TX_OK;
4601 }
Stephen Hemminger00829822008-11-20 20:14:53 -08004602
4603 switch (bond->params.mode) {
4604 case BOND_MODE_ROUNDROBIN:
4605 return bond_xmit_roundrobin(skb, dev);
4606 case BOND_MODE_ACTIVEBACKUP:
4607 return bond_xmit_activebackup(skb, dev);
4608 case BOND_MODE_XOR:
4609 return bond_xmit_xor(skb, dev);
4610 case BOND_MODE_BROADCAST:
4611 return bond_xmit_broadcast(skb, dev);
4612 case BOND_MODE_8023AD:
4613 return bond_3ad_xmit_xor(skb, dev);
4614 case BOND_MODE_ALB:
4615 case BOND_MODE_TLB:
4616 return bond_alb_xmit(skb, dev);
4617 default:
4618 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004619 pr_err("%s: Error: Unknown bonding mode %d\n",
4620 dev->name, bond->params.mode);
Stephen Hemminger00829822008-11-20 20:14:53 -08004621 WARN_ON_ONCE(1);
4622 dev_kfree_skb(skb);
4623 return NETDEV_TX_OK;
4624 }
4625}
4626
4627
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628/*
4629 * set bond mode specific net device operations
4630 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004631void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004633 struct net_device *bond_dev = bond->dev;
4634
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 switch (mode) {
4636 case BOND_MODE_ROUNDROBIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 break;
4638 case BOND_MODE_ACTIVEBACKUP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 break;
4640 case BOND_MODE_XOR:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004641 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 break;
4643 case BOND_MODE_BROADCAST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 break;
4645 case BOND_MODE_8023AD:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004646 bond_set_master_3ad_flags(bond);
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004647 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004650 bond_set_master_alb_flags(bond);
4651 /* FALLTHRU */
4652 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 break;
4654 default:
4655 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004656 pr_err("%s: Error: Unknown bonding mode %d\n",
4657 bond_dev->name, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 break;
4659 }
4660}
4661
Jay Vosburgh217df672005-09-26 16:11:50 -07004662static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4663 struct ethtool_drvinfo *drvinfo)
4664{
4665 strncpy(drvinfo->driver, DRV_NAME, 32);
4666 strncpy(drvinfo->version, DRV_VERSION, 32);
4667 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4668}
4669
Jeff Garzik7282d492006-09-13 14:30:00 -04004670static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004671 .get_drvinfo = bond_ethtool_get_drvinfo,
Stephen Hemmingerfa53eba2008-09-13 21:17:09 -04004672 .get_link = ethtool_op_get_link,
4673 .get_tx_csum = ethtool_op_get_tx_csum,
4674 .get_sg = ethtool_op_get_sg,
4675 .get_tso = ethtool_op_get_tso,
4676 .get_ufo = ethtool_op_get_ufo,
4677 .get_flags = ethtool_op_get_flags,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004678};
4679
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004680static const struct net_device_ops bond_netdev_ops = {
Stephen Hemminger181470f2009-06-12 19:02:52 +00004681 .ndo_init = bond_init,
Stephen Hemminger9e716262009-06-12 19:02:47 +00004682 .ndo_uninit = bond_uninit,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004683 .ndo_open = bond_open,
4684 .ndo_stop = bond_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08004685 .ndo_start_xmit = bond_start_xmit,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004686 .ndo_select_queue = bond_select_queue,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00004687 .ndo_get_stats64 = bond_get_stats,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004688 .ndo_do_ioctl = bond_do_ioctl,
4689 .ndo_set_multicast_list = bond_set_multicast_list,
4690 .ndo_change_mtu = bond_change_mtu,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004691 .ndo_set_mac_address = bond_set_mac_address,
Stephen Hemminger00829822008-11-20 20:14:53 -08004692 .ndo_neigh_setup = bond_neigh_setup,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004693 .ndo_vlan_rx_register = bond_vlan_rx_register,
4694 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4695 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
WANG Congf6dc31a2010-05-06 00:48:51 -07004696#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00004697 .ndo_netpoll_setup = bond_netpoll_setup,
WANG Congf6dc31a2010-05-06 00:48:51 -07004698 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
4699 .ndo_poll_controller = bond_poll_controller,
4700#endif
Jiri Pirko9232ecc2011-02-13 09:33:01 +00004701 .ndo_add_slave = bond_enslave,
4702 .ndo_del_slave = bond_release,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004703};
4704
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004705static void bond_destructor(struct net_device *bond_dev)
4706{
4707 struct bonding *bond = netdev_priv(bond_dev);
4708 if (bond->wq)
4709 destroy_workqueue(bond->wq);
4710 free_netdev(bond_dev);
4711}
4712
Stephen Hemminger181470f2009-06-12 19:02:52 +00004713static void bond_setup(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714{
Wang Chen454d7c92008-11-12 23:37:49 -08004715 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 /* initialize rwlocks */
4718 rwlock_init(&bond->lock);
4719 rwlock_init(&bond->curr_slave_lock);
4720
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00004721 bond->params = bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722
4723 /* Initialize pointers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 bond->dev = bond_dev;
4725 INIT_LIST_HEAD(&bond->vlan_list);
4726
4727 /* Initialize the device entry points */
Stephen Hemminger181470f2009-06-12 19:02:52 +00004728 ether_setup(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004729 bond_dev->netdev_ops = &bond_netdev_ops;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004730 bond_dev->ethtool_ops = &bond_ethtool_ops;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004731 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004733 bond_dev->destructor = bond_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734
4735 /* Initialize the device options */
4736 bond_dev->tx_queue_len = 0;
4737 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004738 bond_dev->priv_flags |= IFF_BONDING;
Stephen Hemminger181470f2009-06-12 19:02:52 +00004739 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
4740
Jay Vosburgh6cf3f412008-11-03 18:16:50 -08004741 if (bond->params.arp_interval)
4742 bond_dev->priv_flags |= IFF_MASTER_ARPMON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743
4744 /* At first, we block adding VLANs. That's the only way to
4745 * prevent problems that occur when adding VLANs over an
4746 * empty bond. The block will be removed once non-challenged
4747 * slaves are enslaved.
4748 */
4749 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4750
Herbert Xu932ff272006-06-09 12:20:56 -07004751 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 * transmitting */
4753 bond_dev->features |= NETIF_F_LLTX;
4754
4755 /* By default, we declare the bond to be fully
4756 * VLAN hardware accelerated capable. Special
4757 * care is taken in the various xmit functions
4758 * when there are slaves that are not hw accel
4759 * capable
4760 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4762 NETIF_F_HW_VLAN_RX |
4763 NETIF_F_HW_VLAN_FILTER);
4764
Eric Dumazete6599c22010-09-17 09:25:07 +00004765 /* By default, we enable GRO on bonding devices.
4766 * Actual support requires lowlevel drivers are GRO ready.
4767 */
4768 bond_dev->features |= NETIF_F_GRO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769}
4770
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004771static void bond_work_cancel_all(struct bonding *bond)
4772{
4773 write_lock_bh(&bond->lock);
4774 bond->kill_timers = 1;
4775 write_unlock_bh(&bond->lock);
4776
4777 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4778 cancel_delayed_work(&bond->mii_work);
4779
4780 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4781 cancel_delayed_work(&bond->arp_work);
4782
4783 if (bond->params.mode == BOND_MODE_ALB &&
4784 delayed_work_pending(&bond->alb_work))
4785 cancel_delayed_work(&bond->alb_work);
4786
4787 if (bond->params.mode == BOND_MODE_8023AD &&
4788 delayed_work_pending(&bond->ad_work))
4789 cancel_delayed_work(&bond->ad_work);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00004790
4791 if (delayed_work_pending(&bond->mcast_work))
4792 cancel_delayed_work(&bond->mcast_work);
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004793}
4794
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004795/*
4796* Destroy a bonding device.
4797* Must be under rtnl_lock when this function is called.
4798*/
4799static void bond_uninit(struct net_device *bond_dev)
Jay Vosburgha434e432008-10-30 17:41:15 -07004800{
Wang Chen454d7c92008-11-12 23:37:49 -08004801 struct bonding *bond = netdev_priv(bond_dev);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004802 struct vlan_entry *vlan, *tmp;
Jay Vosburgha434e432008-10-30 17:41:15 -07004803
WANG Congf6dc31a2010-05-06 00:48:51 -07004804 bond_netpoll_cleanup(bond_dev);
4805
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004806 /* Release the bonded slaves */
4807 bond_release_all(bond_dev);
4808
Jay Vosburgha434e432008-10-30 17:41:15 -07004809 list_del(&bond->bond_list);
4810
4811 bond_work_cancel_all(bond);
4812
Jay Vosburgha434e432008-10-30 17:41:15 -07004813 bond_remove_proc_entry(bond);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004814
Taku Izumif073c7c2010-12-09 15:17:13 +00004815 bond_debug_unregister(bond);
4816
Jiri Pirko22bedad32010-04-01 21:22:57 +00004817 __hw_addr_flush(&bond->mc_list);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004818
4819 list_for_each_entry_safe(vlan, tmp, &bond->vlan_list, vlan_list) {
4820 list_del(&vlan->vlan_list);
4821 kfree(vlan);
4822 }
Jay Vosburgha434e432008-10-30 17:41:15 -07004823}
4824
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825/*------------------------- Module initialization ---------------------------*/
4826
4827/*
4828 * Convert string input module parms. Accept either the
Jay Vosburghece95f72008-01-17 16:25:01 -08004829 * number of the mode or its string name. A bit complicated because
4830 * some mode names are substrings of other names, and calls from sysfs
4831 * may have whitespace in the name (trailing newlines, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 */
Holger Eitzenberger325dcf72008-12-09 23:10:17 -08004833int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834{
Hannes Eder54b87322009-02-14 11:15:49 +00004835 int modeint = -1, i, rv;
Jay Vosburgha42e5342008-01-29 18:07:43 -08004836 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
Jay Vosburghece95f72008-01-17 16:25:01 -08004837
Jay Vosburgha42e5342008-01-29 18:07:43 -08004838 for (p = (char *)buf; *p; p++)
4839 if (!(isdigit(*p) || isspace(*p)))
4840 break;
4841
4842 if (*p)
Jay Vosburghece95f72008-01-17 16:25:01 -08004843 rv = sscanf(buf, "%20s", modestr);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004844 else
Hannes Eder54b87322009-02-14 11:15:49 +00004845 rv = sscanf(buf, "%d", &modeint);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004846
4847 if (!rv)
4848 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849
4850 for (i = 0; tbl[i].modename; i++) {
Hannes Eder54b87322009-02-14 11:15:49 +00004851 if (modeint == tbl[i].mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 return tbl[i].mode;
Jay Vosburghece95f72008-01-17 16:25:01 -08004853 if (strcmp(modestr, tbl[i].modename) == 0)
4854 return tbl[i].mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 }
4856
4857 return -1;
4858}
4859
4860static int bond_check_params(struct bond_params *params)
4861{
Jiri Pirkoa5499522009-09-25 03:28:09 +00004862 int arp_validate_value, fail_over_mac_value, primary_reselect_value;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004863
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 /*
4865 * Convert string parameters.
4866 */
4867 if (mode) {
4868 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4869 if (bond_mode == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004870 pr_err("Error: Invalid bonding mode \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 mode == NULL ? "NULL" : mode);
4872 return -EINVAL;
4873 }
4874 }
4875
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004876 if (xmit_hash_policy) {
4877 if ((bond_mode != BOND_MODE_XOR) &&
4878 (bond_mode != BOND_MODE_8023AD)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004879 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004880 bond_mode_name(bond_mode));
4881 } else {
4882 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4883 xmit_hashtype_tbl);
4884 if (xmit_hashtype == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004885 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004886 xmit_hash_policy == NULL ? "NULL" :
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004887 xmit_hash_policy);
4888 return -EINVAL;
4889 }
4890 }
4891 }
4892
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 if (lacp_rate) {
4894 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004895 pr_info("lacp_rate param is irrelevant in mode %s\n",
4896 bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 } else {
4898 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4899 if (lacp_fast == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004900 pr_err("Error: Invalid lacp rate \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 lacp_rate == NULL ? "NULL" : lacp_rate);
4902 return -EINVAL;
4903 }
4904 }
4905 }
4906
Jay Vosburghfd989c82008-11-04 17:51:16 -08004907 if (ad_select) {
4908 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4909 if (params->ad_select == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004910 pr_err("Error: Invalid ad_select \"%s\"\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -08004911 ad_select == NULL ? "NULL" : ad_select);
4912 return -EINVAL;
4913 }
4914
4915 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004916 pr_warning("ad_select param only affects 802.3ad mode\n");
Jay Vosburghfd989c82008-11-04 17:51:16 -08004917 }
4918 } else {
4919 params->ad_select = BOND_AD_STABLE;
4920 }
4921
Nicolas de Pesloüanf5841302009-08-28 13:18:34 +00004922 if (max_bonds < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004923 pr_warning("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4924 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 max_bonds = BOND_DEFAULT_MAX_BONDS;
4926 }
4927
4928 if (miimon < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004929 pr_warning("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to %d\n",
4930 miimon, INT_MAX, BOND_LINK_MON_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 miimon = BOND_LINK_MON_INTERV;
4932 }
4933
4934 if (updelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004935 pr_warning("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4936 updelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 updelay = 0;
4938 }
4939
4940 if (downdelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004941 pr_warning("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4942 downdelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 downdelay = 0;
4944 }
4945
4946 if ((use_carrier != 0) && (use_carrier != 1)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004947 pr_warning("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
4948 use_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 use_carrier = 1;
4950 }
4951
Moni Shoua7893b242008-05-17 21:10:12 -07004952 if (num_grat_arp < 0 || num_grat_arp > 255) {
Frans Pop2381a552010-03-24 07:57:36 +00004953 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 -08004954 num_grat_arp);
Moni Shoua7893b242008-05-17 21:10:12 -07004955 num_grat_arp = 1;
4956 }
4957
Brian Haley305d5522008-11-04 17:51:14 -08004958 if (num_unsol_na < 0 || num_unsol_na > 255) {
Frans Pop2381a552010-03-24 07:57:36 +00004959 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 -08004960 num_unsol_na);
Brian Haley305d5522008-11-04 17:51:14 -08004961 num_unsol_na = 1;
4962 }
4963
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 /* reset values for 802.3ad */
4965 if (bond_mode == BOND_MODE_8023AD) {
4966 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004967 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 +00004968 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 miimon = 100;
4970 }
4971 }
4972
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004973 if (tx_queues < 1 || tx_queues > 255) {
4974 pr_warning("Warning: tx_queues (%d) should be between "
4975 "1 and 255, resetting to %d\n",
4976 tx_queues, BOND_DEFAULT_TX_QUEUES);
4977 tx_queues = BOND_DEFAULT_TX_QUEUES;
4978 }
4979
Andy Gospodarekebd8e492010-06-02 08:39:21 +00004980 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
4981 pr_warning("Warning: all_slaves_active module parameter (%d), "
4982 "not of valid value (0/1), so it was set to "
4983 "0\n", all_slaves_active);
4984 all_slaves_active = 0;
4985 }
4986
Flavio Leitnerc2952c32010-10-05 14:23:59 +00004987 if (resend_igmp < 0 || resend_igmp > 255) {
4988 pr_warning("Warning: resend_igmp (%d) should be between "
4989 "0 and 255, resetting to %d\n",
4990 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
4991 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
4992 }
4993
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 /* reset values for TLB/ALB */
4995 if ((bond_mode == BOND_MODE_TLB) ||
4996 (bond_mode == BOND_MODE_ALB)) {
4997 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004998 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 +00004999 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 miimon = 100;
5001 }
5002 }
5003
5004 if (bond_mode == BOND_MODE_ALB) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005005 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",
5006 updelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 }
5008
5009 if (!miimon) {
5010 if (updelay || downdelay) {
5011 /* just warn the user the up/down delay will have
5012 * no effect since miimon is zero...
5013 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005014 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",
5015 updelay, downdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 }
5017 } else {
5018 /* don't allow arp monitoring */
5019 if (arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005020 pr_warning("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
5021 miimon, arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 arp_interval = 0;
5023 }
5024
5025 if ((updelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005026 pr_warning("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
5027 updelay, miimon,
5028 (updelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 }
5030
5031 updelay /= miimon;
5032
5033 if ((downdelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005034 pr_warning("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
5035 downdelay, miimon,
5036 (downdelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 }
5038
5039 downdelay /= miimon;
5040 }
5041
5042 if (arp_interval < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005043 pr_warning("Warning: arp_interval module parameter (%d) , not in range 0-%d, so it was reset to %d\n",
5044 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 arp_interval = BOND_LINK_ARP_INTERV;
5046 }
5047
5048 for (arp_ip_count = 0;
5049 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
5050 arp_ip_count++) {
5051 /* not complete check, but should be good enough to
5052 catch mistakes */
5053 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005054 pr_warning("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
5055 arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 arp_interval = 0;
5057 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04005058 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 arp_target[arp_ip_count] = ip;
5060 }
5061 }
5062
5063 if (arp_interval && !arp_ip_count) {
5064 /* don't allow arping if no arp_ip_target given... */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005065 pr_warning("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
5066 arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 arp_interval = 0;
5068 }
5069
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005070 if (arp_validate) {
5071 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005072 pr_err("arp_validate only supported in active-backup mode\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005073 return -EINVAL;
5074 }
5075 if (!arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005076 pr_err("arp_validate requires arp_interval\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005077 return -EINVAL;
5078 }
5079
5080 arp_validate_value = bond_parse_parm(arp_validate,
5081 arp_validate_tbl);
5082 if (arp_validate_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005083 pr_err("Error: invalid arp_validate \"%s\"\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005084 arp_validate == NULL ? "NULL" : arp_validate);
5085 return -EINVAL;
5086 }
5087 } else
5088 arp_validate_value = 0;
5089
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 if (miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005091 pr_info("MII link monitoring set to %d ms\n", miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 } else if (arp_interval) {
5093 int i;
5094
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005095 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
5096 arp_interval,
5097 arp_validate_tbl[arp_validate_value].modename,
5098 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099
5100 for (i = 0; i < arp_ip_count; i++)
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00005101 pr_info(" %s", arp_ip_target[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00005103 pr_info("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104
Jay Vosburghb8a97872008-06-13 18:12:04 -07005105 } else if (max_bonds) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 /* miimon and arp_interval not set, we need one so things
5107 * work as expected, see bonding.txt for details
5108 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005109 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 -07005110 }
5111
5112 if (primary && !USES_PRIMARY(bond_mode)) {
5113 /* currently, using a primary only makes sense
5114 * in active backup, TLB or ALB modes
5115 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005116 pr_warning("Warning: %s primary device specified but has no effect in %s mode\n",
5117 primary, bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 primary = NULL;
5119 }
5120
Jiri Pirkoa5499522009-09-25 03:28:09 +00005121 if (primary && primary_reselect) {
5122 primary_reselect_value = bond_parse_parm(primary_reselect,
5123 pri_reselect_tbl);
5124 if (primary_reselect_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005125 pr_err("Error: Invalid primary_reselect \"%s\"\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00005126 primary_reselect ==
5127 NULL ? "NULL" : primary_reselect);
5128 return -EINVAL;
5129 }
5130 } else {
5131 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
5132 }
5133
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005134 if (fail_over_mac) {
5135 fail_over_mac_value = bond_parse_parm(fail_over_mac,
5136 fail_over_mac_tbl);
5137 if (fail_over_mac_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005138 pr_err("Error: invalid fail_over_mac \"%s\"\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005139 arp_validate == NULL ? "NULL" : arp_validate);
5140 return -EINVAL;
5141 }
5142
5143 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005144 pr_warning("Warning: fail_over_mac only affects active-backup mode.\n");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005145 } else {
5146 fail_over_mac_value = BOND_FOM_NONE;
5147 }
Jay Vosburghdd957c52007-10-09 19:57:24 -07005148
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 /* fill params struct with the proper values */
5150 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04005151 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 params->miimon = miimon;
Moni Shoua7893b242008-05-17 21:10:12 -07005153 params->num_grat_arp = num_grat_arp;
Brian Haley305d5522008-11-04 17:51:14 -08005154 params->num_unsol_na = num_unsol_na;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005156 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 params->updelay = updelay;
5158 params->downdelay = downdelay;
5159 params->use_carrier = use_carrier;
5160 params->lacp_fast = lacp_fast;
5161 params->primary[0] = 0;
Jiri Pirkoa5499522009-09-25 03:28:09 +00005162 params->primary_reselect = primary_reselect_value;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005163 params->fail_over_mac = fail_over_mac_value;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005164 params->tx_queues = tx_queues;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00005165 params->all_slaves_active = all_slaves_active;
Flavio Leitnerc2952c32010-10-05 14:23:59 +00005166 params->resend_igmp = resend_igmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167
5168 if (primary) {
5169 strncpy(params->primary, primary, IFNAMSIZ);
5170 params->primary[IFNAMSIZ - 1] = 0;
5171 }
5172
5173 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
5174
5175 return 0;
5176}
5177
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005178static struct lock_class_key bonding_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -07005179static struct lock_class_key bonding_netdev_addr_lock_key;
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005180
David S. Millere8a04642008-07-17 00:34:19 -07005181static void bond_set_lockdep_class_one(struct net_device *dev,
5182 struct netdev_queue *txq,
5183 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -07005184{
5185 lockdep_set_class(&txq->_xmit_lock,
5186 &bonding_netdev_xmit_lock_key);
5187}
5188
5189static void bond_set_lockdep_class(struct net_device *dev)
5190{
David S. Millercf508b12008-07-22 14:16:42 -07005191 lockdep_set_class(&dev->addr_list_lock,
5192 &bonding_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -07005193 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -07005194}
5195
Stephen Hemminger181470f2009-06-12 19:02:52 +00005196/*
5197 * Called from registration process
5198 */
5199static int bond_init(struct net_device *bond_dev)
5200{
5201 struct bonding *bond = netdev_priv(bond_dev);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005202 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005203
5204 pr_debug("Begin bond_init for %s\n", bond_dev->name);
5205
5206 bond->wq = create_singlethread_workqueue(bond_dev->name);
5207 if (!bond->wq)
5208 return -ENOMEM;
5209
5210 bond_set_lockdep_class(bond_dev);
5211
5212 netif_carrier_off(bond_dev);
5213
5214 bond_create_proc_entry(bond);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005215 list_add_tail(&bond->bond_list, &bn->dev_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005216
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00005217 bond_prepare_sysfs_group(bond);
Jiri Pirko22bedad32010-04-01 21:22:57 +00005218
Taku Izumif073c7c2010-12-09 15:17:13 +00005219 bond_debug_register(bond);
5220
Jiri Pirko22bedad32010-04-01 21:22:57 +00005221 __hw_addr_init(&bond->mc_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005222 return 0;
5223}
5224
Eric W. Biederman88ead972009-10-29 14:18:25 +00005225static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
5226{
5227 if (tb[IFLA_ADDRESS]) {
5228 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
5229 return -EINVAL;
5230 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
5231 return -EADDRNOTAVAIL;
5232 }
5233 return 0;
5234}
5235
5236static struct rtnl_link_ops bond_link_ops __read_mostly = {
5237 .kind = "bond",
Eric W. Biederman66391042009-10-29 23:58:54 +00005238 .priv_size = sizeof(struct bonding),
Eric W. Biederman88ead972009-10-29 14:18:25 +00005239 .setup = bond_setup,
5240 .validate = bond_validate,
5241};
5242
Mitch Williamsdfe60392005-11-09 10:36:04 -08005243/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005244 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08005245 * Caller must NOT hold rtnl_lock; we need to release it here before we
5246 * set up our sysfs entries.
5247 */
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005248int bond_create(struct net *net, const char *name)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005249{
5250 struct net_device *bond_dev;
5251 int res;
5252
5253 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08005254
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005255 bond_dev = alloc_netdev_mq(sizeof(struct bonding), name ? name : "",
5256 bond_setup, tx_queues);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005257 if (!bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005258 pr_err("%s: eek! can't alloc netdev!\n", name);
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005259 rtnl_unlock();
5260 return -ENOMEM;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005261 }
5262
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005263 dev_net_set(bond_dev, net);
Eric W. Biederman88ead972009-10-29 14:18:25 +00005264 bond_dev->rtnl_link_ops = &bond_link_ops;
5265
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005266 if (!name) {
5267 res = dev_alloc_name(bond_dev, "bond%d");
5268 if (res < 0)
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005269 goto out;
Neil Horman27e6f062010-10-05 03:39:21 +00005270 } else {
5271 /*
5272 * If we're given a name to register
5273 * we need to ensure that its not already
5274 * registered
5275 */
5276 res = -EEXIST;
5277 if (__dev_get_by_name(net, name) != NULL)
5278 goto out;
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005279 }
5280
Mitch Williamsdfe60392005-11-09 10:36:04 -08005281 res = register_netdevice(bond_dev);
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005282
Eric W. Biederman30c15ba2009-10-29 14:18:23 +00005283out:
Mitch Williamsdfe60392005-11-09 10:36:04 -08005284 rtnl_unlock();
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005285 if (res < 0)
5286 bond_destructor(bond_dev);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005287 return res;
5288}
5289
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005290static int __net_init bond_net_init(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005291{
Eric W. Biederman15449742009-11-29 15:46:04 +00005292 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005293
5294 bn->net = net;
5295 INIT_LIST_HEAD(&bn->dev_list);
5296
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005297 bond_create_proc_dir(bn);
Eric W. Biederman15449742009-11-29 15:46:04 +00005298
5299 return 0;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005300}
5301
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005302static void __net_exit bond_net_exit(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005303{
Eric W. Biederman15449742009-11-29 15:46:04 +00005304 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005305
5306 bond_destroy_proc_dir(bn);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005307}
5308
5309static struct pernet_operations bond_net_ops = {
5310 .init = bond_net_init,
5311 .exit = bond_net_exit,
Eric W. Biederman15449742009-11-29 15:46:04 +00005312 .id = &bond_net_id,
5313 .size = sizeof(struct bond_net),
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005314};
5315
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316static int __init bonding_init(void)
5317{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 int i;
5319 int res;
5320
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005321 pr_info("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322
Mitch Williamsdfe60392005-11-09 10:36:04 -08005323 res = bond_check_params(&bonding_defaults);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005324 if (res)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005325 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326
Eric W. Biederman15449742009-11-29 15:46:04 +00005327 res = register_pernet_subsys(&bond_net_ops);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005328 if (res)
5329 goto out;
Jay Vosburgh027ea042008-01-17 16:25:02 -08005330
Eric W. Biederman88ead972009-10-29 14:18:25 +00005331 res = rtnl_link_register(&bond_link_ops);
5332 if (res)
Eric W. Biederman66391042009-10-29 23:58:54 +00005333 goto err_link;
Eric W. Biederman88ead972009-10-29 14:18:25 +00005334
Taku Izumif073c7c2010-12-09 15:17:13 +00005335 bond_create_debugfs();
5336
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 for (i = 0; i < max_bonds; i++) {
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005338 res = bond_create(&init_net, NULL);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005339 if (res)
5340 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 }
5342
Mitch Williamsb76cdba2005-11-09 10:36:41 -08005343 res = bond_create_sysfs();
5344 if (res)
5345 goto err;
5346
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005348 register_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005349 bond_register_ipv6_notifier();
Mitch Williamsdfe60392005-11-09 10:36:04 -08005350out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 return res;
Eric W. Biederman88ead972009-10-29 14:18:25 +00005352err:
5353 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman66391042009-10-29 23:58:54 +00005354err_link:
Eric W. Biederman15449742009-11-29 15:46:04 +00005355 unregister_pernet_subsys(&bond_net_ops);
Eric W. Biederman88ead972009-10-29 14:18:25 +00005356 goto out;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005357
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358}
5359
5360static void __exit bonding_exit(void)
5361{
5362 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005363 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005364 bond_unregister_ipv6_notifier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005366 bond_destroy_sysfs();
Taku Izumif073c7c2010-12-09 15:17:13 +00005367 bond_destroy_debugfs();
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005368
Eric W. Biederman88ead972009-10-29 14:18:25 +00005369 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman15449742009-11-29 15:46:04 +00005370 unregister_pernet_subsys(&bond_net_ops);
Neil Hormane843fa52010-10-13 16:01:50 +00005371
5372#ifdef CONFIG_NET_POLL_CONTROLLER
Neil Hormanfb4fa762010-12-06 09:05:50 +00005373 /*
5374 * Make sure we don't have an imbalance on our netpoll blocking
5375 */
5376 WARN_ON(atomic_read(&netpoll_block_tx));
Neil Hormane843fa52010-10-13 16:01:50 +00005377#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378}
5379
5380module_init(bonding_init);
5381module_exit(bonding_exit);
5382MODULE_LICENSE("GPL");
5383MODULE_VERSION(DRV_VERSION);
5384MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5385MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
Eric W. Biederman88ead972009-10-29 14:18:25 +00005386MODULE_ALIAS_RTNL_LINK("bond");