blob: c75126ddc646323be172753b97c279a9d1e560ef [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * originally based on the dummy device.
3 *
4 * Copyright 1999, Thomas Davis, tadavis@lbl.gov.
5 * Licensed under the GPL. Based on dummy.c, and eql.c devices.
6 *
7 * bonding.c: an Ethernet Bonding driver
8 *
9 * This is useful to talk to a Cisco EtherChannel compatible equipment:
10 * Cisco 5500
11 * Sun Trunking (Solaris)
12 * Alteon AceDirector Trunks
13 * Linux Bonding
14 * and probably many L2 switches ...
15 *
16 * How it works:
17 * ifconfig bond0 ipaddress netmask up
18 * will setup a network device, with an ip address. No mac address
19 * will be assigned at this time. The hw mac address will come from
20 * the first slave bonded to the channel. All slaves will then use
21 * this hw mac address.
22 *
23 * ifconfig bond0 down
24 * will release all slaves, marking them as down.
25 *
26 * ifenslave bond0 eth0
27 * will attach eth0 to bond0 as a slave. eth0 hw mac address will either
28 * a: be used as initial mac address
29 * b: if a hw mac address already is there, eth0's hw mac address
30 * will then be set from bond0.
31 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 */
33
Joe Perchesa4aee5c2009-12-13 20:06:07 -080034#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/kernel.h>
37#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/types.h>
39#include <linux/fcntl.h>
40#include <linux/interrupt.h>
41#include <linux/ptrace.h>
42#include <linux/ioport.h>
43#include <linux/in.h>
Jay Vosburgh169a3e62005-06-26 17:54:11 -040044#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/ip.h>
Jay Vosburgh169a3e62005-06-26 17:54:11 -040046#include <linux/tcp.h>
47#include <linux/udp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/slab.h>
49#include <linux/string.h>
50#include <linux/init.h>
51#include <linux/timer.h>
52#include <linux/socket.h>
53#include <linux/ctype.h>
54#include <linux/inet.h>
55#include <linux/bitops.h>
Stephen Hemminger3d632c32009-06-12 19:02:48 +000056#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <asm/dma.h>
Stephen Hemminger3d632c32009-06-12 19:02:48 +000059#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/errno.h>
61#include <linux/netdevice.h>
62#include <linux/inetdevice.h>
Jay Vosburgha816c7c2007-02-28 17:03:37 -080063#include <linux/igmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/etherdevice.h>
65#include <linux/skbuff.h>
66#include <net/sock.h>
67#include <linux/rtnetlink.h>
68#include <linux/proc_fs.h>
69#include <linux/seq_file.h>
70#include <linux/smp.h>
71#include <linux/if_ether.h>
72#include <net/arp.h>
73#include <linux/mii.h>
74#include <linux/ethtool.h>
75#include <linux/if_vlan.h>
76#include <linux/if_bonding.h>
David Sterbab63bb732007-12-06 23:40:33 -080077#include <linux/jiffies.h>
Neil Hormane843fa52010-10-13 16:01:50 +000078#include <linux/preempt.h>
Jay Vosburghc3ade5c2005-06-26 17:52:20 -040079#include <net/route.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020080#include <net/net_namespace.h>
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000081#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include "bonding.h"
83#include "bond_3ad.h"
84#include "bond_alb.h"
85
86/*---------------------------- Module parameters ----------------------------*/
87
88/* monitor all links that often (in milliseconds). <=0 disables monitoring */
89#define BOND_LINK_MON_INTERV 0
90#define BOND_LINK_ARP_INTERV 0
91
92static int max_bonds = BOND_DEFAULT_MAX_BONDS;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +000093static int tx_queues = BOND_DEFAULT_TX_QUEUES;
Moni Shoua7893b242008-05-17 21:10:12 -070094static int num_grat_arp = 1;
Brian Haley305d5522008-11-04 17:51:14 -080095static int num_unsol_na = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static int miimon = BOND_LINK_MON_INTERV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +000097static int updelay;
98static int downdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static int use_carrier = 1;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000100static char *mode;
101static char *primary;
Jiri Pirkoa5499522009-09-25 03:28:09 +0000102static char *primary_reselect;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000103static char *lacp_rate;
104static char *ad_select;
105static char *xmit_hash_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static int arp_interval = BOND_LINK_ARP_INTERV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000107static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
108static char *arp_validate;
109static char *fail_over_mac;
Andy Gospodarekebd8e492010-06-02 08:39:21 +0000110static int all_slaves_active = 0;
Stephen Hemmingerd2991f72009-06-12 19:02:44 +0000111static struct bond_params bonding_defaults;
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000112static int resend_igmp = BOND_DEFAULT_RESEND_IGMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114module_param(max_bonds, int, 0);
115MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
Andy Gospodarekbb1d9122010-06-02 08:40:18 +0000116module_param(tx_queues, int, 0);
117MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)");
Moni Shoua7893b242008-05-17 21:10:12 -0700118module_param(num_grat_arp, int, 0644);
119MODULE_PARM_DESC(num_grat_arp, "Number of gratuitous ARP packets to send on failover event");
Brian Haley305d5522008-11-04 17:51:14 -0800120module_param(num_unsol_na, int, 0644);
121MODULE_PARM_DESC(num_unsol_na, "Number of unsolicited IPv6 Neighbor Advertisements packets to send on failover event");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122module_param(miimon, int, 0);
123MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
124module_param(updelay, int, 0);
125MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
126module_param(downdelay, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800127MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
128 "in milliseconds");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129module_param(use_carrier, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800130MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
131 "0 for off, 1 for on (default)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132module_param(mode, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800133MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
134 "1 for active-backup, 2 for balance-xor, "
135 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
136 "6 for balance-alb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137module_param(primary, charp, 0);
138MODULE_PARM_DESC(primary, "Primary network device to use");
Jiri Pirkoa5499522009-09-25 03:28:09 +0000139module_param(primary_reselect, charp, 0);
140MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
141 "once it comes up; "
142 "0 for always (default), "
143 "1 for only if speed of primary is "
144 "better, "
145 "2 for only on active slave "
146 "failure");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147module_param(lacp_rate, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800148MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
149 "(slow/fast)");
Jay Vosburghfd989c82008-11-04 17:51:16 -0800150module_param(ad_select, charp, 0);
151MODULE_PARM_DESC(ad_select, "803.ad aggregation selection logic: stable (0, default), bandwidth (1), count (2)");
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400152module_param(xmit_hash_policy, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800153MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
154 ", 1 for layer 3+4");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155module_param(arp_interval, int, 0);
156MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
157module_param_array(arp_ip_target, charp, NULL, 0);
158MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700159module_param(arp_validate, charp, 0);
160MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700161module_param(fail_over_mac, charp, 0);
162MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to the same MAC. none (default), active or follow");
Andy Gospodarekebd8e492010-06-02 08:39:21 +0000163module_param(all_slaves_active, int, 0);
164MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface"
165 "by setting active flag for all slaves. "
166 "0 for never (default), 1 for always.");
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000167module_param(resend_igmp, int, 0);
168MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on link failure");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170/*----------------------------- Global variables ----------------------------*/
171
Neil Hormane843fa52010-10-13 16:01:50 +0000172#ifdef CONFIG_NET_POLL_CONTROLLER
Neil Hormanfb4fa762010-12-06 09:05:50 +0000173atomic_t netpoll_block_tx = ATOMIC_INIT(0);
Neil Hormane843fa52010-10-13 16:01:50 +0000174#endif
175
Arjan van de Venf71e1302006-03-03 21:33:57 -0500176static const char * const version =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n";
178
Eric Dumazetf99189b2009-11-17 10:42:49 +0000179int bond_net_id __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000181static __be32 arp_target[BOND_MAX_ARP_TARGETS];
182static int arp_ip_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183static int bond_mode = BOND_MODE_ROUNDROBIN;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000184static int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
185static int lacp_fast;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800187const struct bond_parm_tbl bond_lacp_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{ "slow", AD_LACP_SLOW},
189{ "fast", AD_LACP_FAST},
190{ NULL, -1},
191};
192
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800193const struct bond_parm_tbl bond_mode_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{ "balance-rr", BOND_MODE_ROUNDROBIN},
195{ "active-backup", BOND_MODE_ACTIVEBACKUP},
196{ "balance-xor", BOND_MODE_XOR},
197{ "broadcast", BOND_MODE_BROADCAST},
198{ "802.3ad", BOND_MODE_8023AD},
199{ "balance-tlb", BOND_MODE_TLB},
200{ "balance-alb", BOND_MODE_ALB},
201{ NULL, -1},
202};
203
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800204const struct bond_parm_tbl xmit_hashtype_tbl[] = {
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400205{ "layer2", BOND_XMIT_POLICY_LAYER2},
206{ "layer3+4", BOND_XMIT_POLICY_LAYER34},
Jay Vosburgh6f6652b2007-12-06 23:40:34 -0800207{ "layer2+3", BOND_XMIT_POLICY_LAYER23},
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400208{ NULL, -1},
209};
210
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800211const struct bond_parm_tbl arp_validate_tbl[] = {
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700212{ "none", BOND_ARP_VALIDATE_NONE},
213{ "active", BOND_ARP_VALIDATE_ACTIVE},
214{ "backup", BOND_ARP_VALIDATE_BACKUP},
215{ "all", BOND_ARP_VALIDATE_ALL},
216{ NULL, -1},
217};
218
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800219const struct bond_parm_tbl fail_over_mac_tbl[] = {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700220{ "none", BOND_FOM_NONE},
221{ "active", BOND_FOM_ACTIVE},
222{ "follow", BOND_FOM_FOLLOW},
223{ NULL, -1},
224};
225
Jiri Pirkoa5499522009-09-25 03:28:09 +0000226const struct bond_parm_tbl pri_reselect_tbl[] = {
227{ "always", BOND_PRI_RESELECT_ALWAYS},
228{ "better", BOND_PRI_RESELECT_BETTER},
229{ "failure", BOND_PRI_RESELECT_FAILURE},
230{ NULL, -1},
231};
232
Jay Vosburghfd989c82008-11-04 17:51:16 -0800233struct bond_parm_tbl ad_select_tbl[] = {
234{ "stable", BOND_AD_STABLE},
235{ "bandwidth", BOND_AD_BANDWIDTH},
236{ "count", BOND_AD_COUNT},
237{ NULL, -1},
238};
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*-------------------------- Forward declarations ---------------------------*/
241
Jay Vosburghc3ade5c2005-06-26 17:52:20 -0400242static void bond_send_gratuitous_arp(struct bonding *bond);
Stephen Hemminger181470f2009-06-12 19:02:52 +0000243static int bond_init(struct net_device *bond_dev);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +0000244static void bond_uninit(struct net_device *bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246/*---------------------------- General routines -----------------------------*/
247
Adrian Bunk4ad072c2007-07-09 11:51:12 -0700248static const char *bond_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800250 static const char *names[] = {
251 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
252 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
253 [BOND_MODE_XOR] = "load balancing (xor)",
254 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000255 [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800256 [BOND_MODE_TLB] = "transmit load balancing",
257 [BOND_MODE_ALB] = "adaptive load balancing",
258 };
259
260 if (mode < 0 || mode > BOND_MODE_ALB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return "unknown";
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800262
263 return names[mode];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/*---------------------------------- VLAN -----------------------------------*/
267
268/**
269 * bond_add_vlan - add a new vlan id on bond
270 * @bond: bond that got the notification
271 * @vlan_id: the vlan id to add
272 *
273 * Returns -ENOMEM if allocation failed.
274 */
275static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
276{
277 struct vlan_entry *vlan;
278
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800279 pr_debug("bond: %s, vlan id %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800280 (bond ? bond->dev->name : "None"), vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Brian Haley305d5522008-11-04 17:51:14 -0800282 vlan = kzalloc(sizeof(struct vlan_entry), GFP_KERNEL);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000283 if (!vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 INIT_LIST_HEAD(&vlan->vlan_list);
287 vlan->vlan_id = vlan_id;
288
289 write_lock_bh(&bond->lock);
290
291 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
292
293 write_unlock_bh(&bond->lock);
294
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800295 pr_debug("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 return 0;
298}
299
300/**
301 * bond_del_vlan - delete a vlan id from bond
302 * @bond: bond that got the notification
303 * @vlan_id: the vlan id to delete
304 *
305 * returns -ENODEV if @vlan_id was not found in @bond.
306 */
307static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
308{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700309 struct vlan_entry *vlan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 int res = -ENODEV;
311
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800312 pr_debug("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Neil Hormane843fa52010-10-13 16:01:50 +0000314 block_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 write_lock_bh(&bond->lock);
316
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700317 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (vlan->vlan_id == vlan_id) {
319 list_del(&vlan->vlan_list);
320
Holger Eitzenberger58402052008-12-09 23:07:13 -0800321 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 bond_alb_clear_vlan(bond, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800324 pr_debug("removed VLAN ID %d from bond %s\n",
325 vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 kfree(vlan);
328
329 if (list_empty(&bond->vlan_list) &&
330 (bond->slave_cnt == 0)) {
331 /* Last VLAN removed and no slaves, so
332 * restore block on adding VLANs. This will
333 * be removed once new slaves that are not
334 * VLAN challenged will be added.
335 */
336 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
337 }
338
339 res = 0;
340 goto out;
341 }
342 }
343
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800344 pr_debug("couldn't find VLAN ID %d in bond %s\n",
345 vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347out:
348 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +0000349 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return res;
351}
352
353/**
354 * bond_has_challenged_slaves
355 * @bond: the bond we're working on
356 *
357 * Searches the slave list. Returns 1 if a vlan challenged slave
358 * was found, 0 otherwise.
359 *
360 * Assumes bond->lock is held.
361 */
362static int bond_has_challenged_slaves(struct bonding *bond)
363{
364 struct slave *slave;
365 int i;
366
367 bond_for_each_slave(bond, slave, i) {
368 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800369 pr_debug("found VLAN challenged slave - %s\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800370 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return 1;
372 }
373 }
374
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800375 pr_debug("no VLAN challenged slaves found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return 0;
377}
378
379/**
380 * bond_next_vlan - safely skip to the next item in the vlans list.
381 * @bond: the bond we're working on
382 * @curr: item we're advancing from
383 *
384 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
385 * or @curr->next otherwise (even if it is @curr itself again).
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000386 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 * Caller must hold bond->lock
388 */
389struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
390{
391 struct vlan_entry *next, *last;
392
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000393 if (list_empty(&bond->vlan_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 if (!curr) {
397 next = list_entry(bond->vlan_list.next,
398 struct vlan_entry, vlan_list);
399 } else {
400 last = list_entry(bond->vlan_list.prev,
401 struct vlan_entry, vlan_list);
402 if (last == curr) {
403 next = list_entry(bond->vlan_list.next,
404 struct vlan_entry, vlan_list);
405 } else {
406 next = list_entry(curr->vlan_list.next,
407 struct vlan_entry, vlan_list);
408 }
409 }
410
411 return next;
412}
413
414/**
415 * bond_dev_queue_xmit - Prepare skb for xmit.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000416 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 * @bond: bond device that got this skb for tx.
418 * @skb: hw accel VLAN tagged skb to transmit
419 * @slave_dev: slave that is supposed to xmit this skbuff
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000421int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
422 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Ben Hutchings83874512010-12-13 08:19:28 +0000424 skb->dev = slave_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 skb->priority = 1;
Amerigo Wang080e4132011-02-17 23:43:33 +0000426 if (unlikely(netpoll_tx_running(slave_dev)))
Amerigo Wang8a8efa22011-02-17 23:43:32 +0000427 bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
Amerigo Wang080e4132011-02-17 23:43:33 +0000428 else
WANG Congf6dc31a2010-05-06 00:48:51 -0700429 dev_queue_xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 return 0;
432}
433
434/*
435 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
436 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
437 * lock because:
438 * a. This operation is performed in IOCTL context,
439 * b. The operation is protected by the RTNL semaphore in the 8021q code,
440 * c. Holding a lock with BH disabled while directly calling a base driver
441 * entry point is generally a BAD idea.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000442 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 * The design of synchronization/protection for this operation in the 8021q
444 * module is good for one or more VLAN devices over a single physical device
445 * and cannot be extended for a teaming solution like bonding, so there is a
446 * potential race condition here where a net device from the vlan group might
447 * be referenced (either by a base driver or the 8021q code) while it is being
448 * removed from the system. However, it turns out we're not making matters
449 * worse, and if it works for regular VLAN usage it will work here too.
450*/
451
452/**
453 * bond_vlan_rx_register - Propagates registration to slaves
454 * @bond_dev: bonding net device that got called
455 * @grp: vlan group being registered
456 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000457static void bond_vlan_rx_register(struct net_device *bond_dev,
458 struct vlan_group *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Wang Chen454d7c92008-11-12 23:37:49 -0800460 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct slave *slave;
462 int i;
463
Jarek Poplawskia71fb882010-10-27 07:08:22 +0000464 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 bond->vlgrp = grp;
Jarek Poplawskia71fb882010-10-27 07:08:22 +0000466 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 bond_for_each_slave(bond, slave, i) {
469 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800470 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800473 slave_ops->ndo_vlan_rx_register) {
474 slave_ops->ndo_vlan_rx_register(slave_dev, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476 }
477}
478
479/**
480 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
481 * @bond_dev: bonding net device that got called
482 * @vid: vlan id being added
483 */
484static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
485{
Wang Chen454d7c92008-11-12 23:37:49 -0800486 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 struct slave *slave;
488 int i, res;
489
490 bond_for_each_slave(bond, slave, i) {
491 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800492 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800495 slave_ops->ndo_vlan_rx_add_vid) {
496 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498 }
499
500 res = bond_add_vlan(bond, vid);
501 if (res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800502 pr_err("%s: Error: Failed to add vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 bond_dev->name, vid);
504 }
505}
506
507/**
508 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
509 * @bond_dev: bonding net device that got called
510 * @vid: vlan id being removed
511 */
512static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
513{
Wang Chen454d7c92008-11-12 23:37:49 -0800514 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 struct slave *slave;
516 struct net_device *vlan_dev;
517 int i, res;
518
519 bond_for_each_slave(bond, slave, i) {
520 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800521 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800524 slave_ops->ndo_vlan_rx_kill_vid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* Save and then restore vlan_dev in the grp array,
526 * since the slave's driver might clear it.
527 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800528 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800529 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vid);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800530 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 }
533
534 res = bond_del_vlan(bond, vid);
535 if (res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800536 pr_err("%s: Error: Failed to remove vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 bond_dev->name, vid);
538 }
539}
540
541static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
542{
543 struct vlan_entry *vlan;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800544 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Jay Vosburghf35188f2010-07-21 12:14:47 +0000546 if (!bond->vlgrp)
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000547 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800550 slave_ops->ndo_vlan_rx_register)
551 slave_ops->ndo_vlan_rx_register(slave_dev, bond->vlgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800554 !(slave_ops->ndo_vlan_rx_add_vid))
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000555 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800557 list_for_each_entry(vlan, &bond->vlan_list, vlan_list)
558 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vlan->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000561static void bond_del_vlans_from_slave(struct bonding *bond,
562 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800564 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct vlan_entry *vlan;
566 struct net_device *vlan_dev;
567
Jay Vosburghf35188f2010-07-21 12:14:47 +0000568 if (!bond->vlgrp)
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000569 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800572 !(slave_ops->ndo_vlan_rx_kill_vid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 goto unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +0000576 if (!vlan->vlan_id)
577 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /* Save and then restore vlan_dev in the grp array,
579 * since the slave's driver might clear it.
580 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800581 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800582 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800583 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
586unreg:
587 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800588 slave_ops->ndo_vlan_rx_register)
589 slave_ops->ndo_vlan_rx_register(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592/*------------------------------- Link status -------------------------------*/
593
594/*
Jay Vosburghff59c452006-03-27 13:27:43 -0800595 * Set the carrier state for the master according to the state of its
596 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
597 * do special 802.3ad magic.
598 *
599 * Returns zero if carrier state does not change, nonzero if it does.
600 */
601static int bond_set_carrier(struct bonding *bond)
602{
603 struct slave *slave;
604 int i;
605
606 if (bond->slave_cnt == 0)
607 goto down;
608
609 if (bond->params.mode == BOND_MODE_8023AD)
610 return bond_3ad_set_carrier(bond);
611
612 bond_for_each_slave(bond, slave, i) {
613 if (slave->link == BOND_LINK_UP) {
614 if (!netif_carrier_ok(bond->dev)) {
615 netif_carrier_on(bond->dev);
616 return 1;
617 }
618 return 0;
619 }
620 }
621
622down:
623 if (netif_carrier_ok(bond->dev)) {
624 netif_carrier_off(bond->dev);
625 return 1;
626 }
627 return 0;
628}
629
630/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * Get link speed and duplex from the slave's base driver
632 * using ethtool. If for some reason the call fails or the
633 * values are invalid, fake speed and duplex to 100/Full
634 * and return error.
635 */
636static int bond_update_speed_duplex(struct slave *slave)
637{
638 struct net_device *slave_dev = slave->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 struct ethtool_cmd etool;
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700640 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 /* Fake speed and duplex */
643 slave->speed = SPEED_100;
644 slave->duplex = DUPLEX_FULL;
645
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700646 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700649 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
650 if (res < 0)
651 return -1;
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 switch (etool.speed) {
654 case SPEED_10:
655 case SPEED_100:
656 case SPEED_1000:
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700657 case SPEED_10000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 break;
659 default:
660 return -1;
661 }
662
663 switch (etool.duplex) {
664 case DUPLEX_FULL:
665 case DUPLEX_HALF:
666 break;
667 default:
668 return -1;
669 }
670
671 slave->speed = etool.speed;
672 slave->duplex = etool.duplex;
673
674 return 0;
675}
676
677/*
678 * if <dev> supports MII link status reporting, check its link status.
679 *
680 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000681 * depending upon the setting of the use_carrier parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 *
683 * Return either BMSR_LSTATUS, meaning that the link is up (or we
684 * can't tell and just pretend it is), or 0, meaning that the link is
685 * down.
686 *
687 * If reporting is non-zero, instead of faking link up, return -1 if
688 * both ETHTOOL and MII ioctls fail (meaning the device does not
689 * support them). If use_carrier is set, return whatever it says.
690 * It'd be nice if there was a good way to tell if a driver supports
691 * netif_carrier, but there really isn't.
692 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000693static int bond_check_dev_link(struct bonding *bond,
694 struct net_device *slave_dev, int reporting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800696 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Jiri Bohacd9d52832009-10-28 22:23:54 -0700697 int (*ioctl)(struct net_device *, struct ifreq *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct ifreq ifr;
699 struct mii_ioctl_data *mii;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Petri Gynther6c988852009-08-28 12:05:15 +0000701 if (!reporting && !netif_running(slave_dev))
702 return 0;
703
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800704 if (bond->params.use_carrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Jiri Pirko29112f42009-04-24 01:58:23 +0000707 /* Try to get link status using Ethtool first. */
708 if (slave_dev->ethtool_ops) {
709 if (slave_dev->ethtool_ops->get_link) {
710 u32 link;
711
712 link = slave_dev->ethtool_ops->get_link(slave_dev);
713
714 return link ? BMSR_LSTATUS : 0;
715 }
716 }
717
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000718 /* Ethtool can't be used, fallback to MII ioctls. */
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800719 ioctl = slave_ops->ndo_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (ioctl) {
721 /* TODO: set pointer to correct ioctl on a per team member */
722 /* bases to make this more efficient. that is, once */
723 /* we determine the correct ioctl, we will always */
724 /* call it and not the others for that team */
725 /* member. */
726
727 /*
728 * We cannot assume that SIOCGMIIPHY will also read a
729 * register; not all network drivers (e.g., e100)
730 * support that.
731 */
732
733 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
734 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
735 mii = if_mii(&ifr);
736 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
737 mii->reg_num = MII_BMSR;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000738 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0)
739 return mii->val_out & BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741 }
742
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700743 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 * If reporting, report that either there's no dev->do_ioctl,
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700745 * or both SIOCGMIIREG and get_link failed (meaning that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 * cannot report link status). If not reporting, pretend
747 * we're ok.
748 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000749 return reporting ? -1 : BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
752/*----------------------------- Multicast list ------------------------------*/
753
754/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 * Push the promiscuity flag down to appropriate slaves
756 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700757static int bond_set_promiscuity(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700759 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (USES_PRIMARY(bond->params.mode)) {
761 /* write lock already acquired */
762 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700763 err = dev_set_promiscuity(bond->curr_active_slave->dev,
764 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766 } else {
767 struct slave *slave;
768 int i;
769 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700770 err = dev_set_promiscuity(slave->dev, inc);
771 if (err)
772 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700775 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
778/*
779 * Push the allmulti flag down to all slaves
780 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700781static int bond_set_allmulti(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700783 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (USES_PRIMARY(bond->params.mode)) {
785 /* write lock already acquired */
786 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700787 err = dev_set_allmulti(bond->curr_active_slave->dev,
788 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790 } else {
791 struct slave *slave;
792 int i;
793 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700794 err = dev_set_allmulti(slave->dev, inc);
795 if (err)
796 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700799 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
802/*
803 * Add a Multicast address to slaves
804 * according to mode
805 */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000806static void bond_mc_add(struct bonding *bond, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 if (USES_PRIMARY(bond->params.mode)) {
809 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000810 if (bond->curr_active_slave)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000811 dev_mc_add(bond->curr_active_slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 } else {
813 struct slave *slave;
814 int i;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000815
816 bond_for_each_slave(bond, slave, i)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000817 dev_mc_add(slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819}
820
821/*
822 * Remove a multicast address from slave
823 * according to mode
824 */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000825static void bond_mc_del(struct bonding *bond, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 if (USES_PRIMARY(bond->params.mode)) {
828 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000829 if (bond->curr_active_slave)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000830 dev_mc_del(bond->curr_active_slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 } else {
832 struct slave *slave;
833 int i;
834 bond_for_each_slave(bond, slave, i) {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000835 dev_mc_del(slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837 }
838}
839
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800840
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000841static void __bond_resend_igmp_join_requests(struct net_device *dev)
842{
843 struct in_device *in_dev;
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000844
845 rcu_read_lock();
846 in_dev = __in_dev_get_rcu(dev);
Eric Dumazet866f3b22010-11-18 09:33:19 -0800847 if (in_dev)
David S. Miller24912422010-11-19 13:13:47 -0800848 ip_mc_rejoin_groups(in_dev);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000849 rcu_read_unlock();
850}
851
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800852/*
853 * Retrieve the list of registered multicast addresses for the bonding
854 * device and retransmit an IGMP JOIN request to the current active
855 * slave.
856 */
857static void bond_resend_igmp_join_requests(struct bonding *bond)
858{
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000859 struct net_device *vlan_dev;
860 struct vlan_entry *vlan;
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800861
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000862 read_lock(&bond->lock);
863
864 /* rejoin all groups on bond device */
865 __bond_resend_igmp_join_requests(bond->dev);
866
867 /* rejoin all groups on vlan devices */
868 if (bond->vlgrp) {
869 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
870 vlan_dev = vlan_group_get_device(bond->vlgrp,
871 vlan->vlan_id);
872 if (vlan_dev)
873 __bond_resend_igmp_join_requests(vlan_dev);
874 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800875 }
876
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000877 if (--bond->igmp_retrans > 0)
878 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
879
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000880 read_unlock(&bond->lock);
881}
882
stephen hemminger379b7382010-10-15 11:02:56 +0000883static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000884{
885 struct bonding *bond = container_of(work, struct bonding,
886 mcast_work.work);
887 bond_resend_igmp_join_requests(bond);
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800888}
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 * flush all members of flush->mc_list from device dev->mc_list
892 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000893static void bond_mc_list_flush(struct net_device *bond_dev,
894 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Wang Chen454d7c92008-11-12 23:37:49 -0800896 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000897 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Jiri Pirko22bedad32010-04-01 21:22:57 +0000899 netdev_for_each_mc_addr(ha, bond_dev)
900 dev_mc_del(slave_dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 if (bond->params.mode == BOND_MODE_8023AD) {
903 /* del lacpdu mc addr from mc list */
904 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
905
Jiri Pirko22bedad32010-04-01 21:22:57 +0000906 dev_mc_del(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908}
909
910/*--------------------------- Active slave change ---------------------------*/
911
912/*
913 * Update the mc list and multicast-related flags for the new and
914 * old active slaves (if any) according to the multicast mode, and
915 * promiscuous flags unconditionally.
916 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000917static void bond_mc_swap(struct bonding *bond, struct slave *new_active,
918 struct slave *old_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Jiri Pirko22bedad32010-04-01 21:22:57 +0000920 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000922 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 /* nothing to do - mc list is already up-to-date on
924 * all slaves
925 */
926 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (old_active) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000929 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 dev_set_promiscuity(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000932 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 dev_set_allmulti(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Jiri Pirko22bedad32010-04-01 21:22:57 +0000935 netdev_for_each_mc_addr(ha, bond->dev)
936 dev_mc_del(old_active->dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
939 if (new_active) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700940 /* FIXME: Signal errors upstream. */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000941 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 dev_set_promiscuity(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000944 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 dev_set_allmulti(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Jiri Pirko22bedad32010-04-01 21:22:57 +0000947 netdev_for_each_mc_addr(ha, bond->dev)
948 dev_mc_add(new_active->dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950}
951
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700952/*
953 * bond_do_fail_over_mac
954 *
955 * Perform special MAC address swapping for fail_over_mac settings
956 *
957 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
958 */
959static void bond_do_fail_over_mac(struct bonding *bond,
960 struct slave *new_active,
961 struct slave *old_active)
Hannes Eder1f78d9f2009-02-14 11:15:33 +0000962 __releases(&bond->curr_slave_lock)
963 __releases(&bond->lock)
964 __acquires(&bond->lock)
965 __acquires(&bond->curr_slave_lock)
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700966{
967 u8 tmp_mac[ETH_ALEN];
968 struct sockaddr saddr;
969 int rv;
970
971 switch (bond->params.fail_over_mac) {
972 case BOND_FOM_ACTIVE:
973 if (new_active)
974 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
975 new_active->dev->addr_len);
976 break;
977 case BOND_FOM_FOLLOW:
978 /*
979 * if new_active && old_active, swap them
980 * if just old_active, do nothing (going to no active slave)
981 * if just new_active, set new_active to bond's MAC
982 */
983 if (!new_active)
984 return;
985
986 write_unlock_bh(&bond->curr_slave_lock);
987 read_unlock(&bond->lock);
988
989 if (old_active) {
990 memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
991 memcpy(saddr.sa_data, old_active->dev->dev_addr,
992 ETH_ALEN);
993 saddr.sa_family = new_active->dev->type;
994 } else {
995 memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
996 saddr.sa_family = bond->dev->type;
997 }
998
999 rv = dev_set_mac_address(new_active->dev, &saddr);
1000 if (rv) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001001 pr_err("%s: Error %d setting MAC of slave %s\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001002 bond->dev->name, -rv, new_active->dev->name);
1003 goto out;
1004 }
1005
1006 if (!old_active)
1007 goto out;
1008
1009 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
1010 saddr.sa_family = old_active->dev->type;
1011
1012 rv = dev_set_mac_address(old_active->dev, &saddr);
1013 if (rv)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001014 pr_err("%s: Error %d setting MAC of slave %s\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001015 bond->dev->name, -rv, new_active->dev->name);
1016out:
1017 read_lock(&bond->lock);
1018 write_lock_bh(&bond->curr_slave_lock);
1019 break;
1020 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001021 pr_err("%s: bond_do_fail_over_mac impossible: bad policy %d\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001022 bond->dev->name, bond->params.fail_over_mac);
1023 break;
1024 }
1025
1026}
1027
Jiri Pirkoa5499522009-09-25 03:28:09 +00001028static bool bond_should_change_active(struct bonding *bond)
1029{
1030 struct slave *prim = bond->primary_slave;
1031 struct slave *curr = bond->curr_active_slave;
1032
1033 if (!prim || !curr || curr->link != BOND_LINK_UP)
1034 return true;
1035 if (bond->force_primary) {
1036 bond->force_primary = false;
1037 return true;
1038 }
1039 if (bond->params.primary_reselect == BOND_PRI_RESELECT_BETTER &&
1040 (prim->speed < curr->speed ||
1041 (prim->speed == curr->speed && prim->duplex <= curr->duplex)))
1042 return false;
1043 if (bond->params.primary_reselect == BOND_PRI_RESELECT_FAILURE)
1044 return false;
1045 return true;
1046}
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048/**
1049 * find_best_interface - select the best available slave to be the active one
1050 * @bond: our bonding struct
1051 *
1052 * Warning: Caller must hold curr_slave_lock for writing.
1053 */
1054static struct slave *bond_find_best_slave(struct bonding *bond)
1055{
1056 struct slave *new_active, *old_active;
1057 struct slave *bestslave = NULL;
1058 int mintime = bond->params.updelay;
1059 int i;
1060
Nicolas de Pesloüan49b4ad92009-10-07 14:11:00 -07001061 new_active = bond->curr_active_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 if (!new_active) { /* there were no active slaves left */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001064 if (bond->slave_cnt > 0) /* found one slave */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 new_active = bond->first_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001066 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return NULL; /* still no slave, return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if ((bond->primary_slave) &&
Jiri Pirkoa5499522009-09-25 03:28:09 +00001071 bond->primary_slave->link == BOND_LINK_UP &&
1072 bond_should_change_active(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 new_active = bond->primary_slave;
1074 }
1075
1076 /* remember where to stop iterating over the slaves */
1077 old_active = new_active;
1078
1079 bond_for_each_slave_from(bond, new_active, i, old_active) {
Jiri Pirkob9f60252009-08-31 11:09:38 +00001080 if (new_active->link == BOND_LINK_UP) {
1081 return new_active;
1082 } else if (new_active->link == BOND_LINK_BACK &&
1083 IS_UP(new_active->dev)) {
1084 /* link up, but waiting for stabilization */
1085 if (new_active->delay < mintime) {
1086 mintime = new_active->delay;
1087 bestslave = new_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089 }
1090 }
1091
1092 return bestslave;
1093}
1094
1095/**
1096 * change_active_interface - change the active slave into the specified one
1097 * @bond: our bonding struct
1098 * @new: the new slave to make the active one
1099 *
1100 * Set the new slave to the bond's settings and unset them on the old
1101 * curr_active_slave.
1102 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1103 *
1104 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1105 * because it is apparently the best available slave we have, even though its
1106 * updelay hasn't timed out yet.
1107 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001108 * If new_active is not NULL, caller must hold bond->lock for read and
1109 * curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001111void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
1113 struct slave *old_active = bond->curr_active_slave;
1114
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001115 if (old_active == new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 if (new_active) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07001119 new_active->jiffies = jiffies;
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (new_active->link == BOND_LINK_BACK) {
1122 if (USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001123 pr_info("%s: making interface %s the new active one %d ms earlier.\n",
1124 bond->dev->name, new_active->dev->name,
1125 (bond->params.updelay - new_active->delay) * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
1128 new_active->delay = 0;
1129 new_active->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001131 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Holger Eitzenberger58402052008-12-09 23:07:13 -08001134 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 } else {
1137 if (USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001138 pr_info("%s: making interface %s the new active one.\n",
1139 bond->dev->name, new_active->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141 }
1142 }
1143
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001144 if (USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 bond_mc_swap(bond, new_active, old_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Holger Eitzenberger58402052008-12-09 23:07:13 -08001147 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 bond_alb_handle_active_change(bond, new_active);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001149 if (old_active)
1150 bond_set_slave_inactive_flags(old_active);
1151 if (new_active)
1152 bond_set_slave_active_flags(new_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 } else {
1154 bond->curr_active_slave = new_active;
1155 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001156
1157 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001158 if (old_active)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001159 bond_set_slave_inactive_flags(old_active);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001160
1161 if (new_active) {
1162 bond_set_slave_active_flags(new_active);
Moni Shoua2ab82852007-10-09 19:43:39 -07001163
Or Gerlitz709f8a42008-06-13 18:12:01 -07001164 if (bond->params.fail_over_mac)
1165 bond_do_fail_over_mac(bond, new_active,
1166 old_active);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001167
Ben Hutchingsffa95ed2010-12-13 08:19:56 +00001168 if (netif_running(bond->dev)) {
1169 bond->send_grat_arp = bond->params.num_grat_arp;
1170 bond_send_gratuitous_arp(bond);
Or Gerlitz01f31092008-06-13 18:12:02 -07001171
Ben Hutchingsffa95ed2010-12-13 08:19:56 +00001172 bond->send_unsol_na = bond->params.num_unsol_na;
1173 bond_send_unsolicited_na(bond);
1174 }
Brian Haley305d5522008-11-04 17:51:14 -08001175
Or Gerlitz01f31092008-06-13 18:12:02 -07001176 write_unlock_bh(&bond->curr_slave_lock);
1177 read_unlock(&bond->lock);
1178
Moni Shoua75c78502009-09-15 02:37:40 -07001179 netdev_bonding_change(bond->dev, NETDEV_BONDING_FAILOVER);
Or Gerlitz01f31092008-06-13 18:12:02 -07001180
1181 read_lock(&bond->lock);
1182 write_lock_bh(&bond->curr_slave_lock);
Moni Shoua7893b242008-05-17 21:10:12 -07001183 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001184 }
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001185
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001186 /* resend IGMP joins since active slave has changed or
1187 * all were sent on curr_active_slave */
Ben Hutchingsffa95ed2010-12-13 08:19:56 +00001188 if (((USES_PRIMARY(bond->params.mode) && new_active) ||
1189 bond->params.mode == BOND_MODE_ROUNDROBIN) &&
1190 netif_running(bond->dev)) {
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001191 bond->igmp_retrans = bond->params.resend_igmp;
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001192 queue_delayed_work(bond->wq, &bond->mcast_work, 0);
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
1196/**
1197 * bond_select_active_slave - select a new active slave, if needed
1198 * @bond: our bonding struct
1199 *
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001200 * This functions should be called when one of the following occurs:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 * - The old curr_active_slave has been released or lost its link.
1202 * - The primary_slave has got its link back.
1203 * - A slave has got its link back and there's no old curr_active_slave.
1204 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001205 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001207void bond_select_active_slave(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 struct slave *best_slave;
Jay Vosburghff59c452006-03-27 13:27:43 -08001210 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 best_slave = bond_find_best_slave(bond);
1213 if (best_slave != bond->curr_active_slave) {
1214 bond_change_active_slave(bond, best_slave);
Jay Vosburghff59c452006-03-27 13:27:43 -08001215 rv = bond_set_carrier(bond);
1216 if (!rv)
1217 return;
1218
1219 if (netif_carrier_ok(bond->dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001220 pr_info("%s: first active interface up!\n",
1221 bond->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08001222 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001223 pr_info("%s: now running without any active interface !\n",
1224 bond->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08001225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227}
1228
1229/*--------------------------- slave list handling ---------------------------*/
1230
1231/*
1232 * This function attaches the slave to the end of list.
1233 *
1234 * bond->lock held for writing by caller.
1235 */
1236static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1237{
1238 if (bond->first_slave == NULL) { /* attaching the first slave */
1239 new_slave->next = new_slave;
1240 new_slave->prev = new_slave;
1241 bond->first_slave = new_slave;
1242 } else {
1243 new_slave->next = bond->first_slave;
1244 new_slave->prev = bond->first_slave->prev;
1245 new_slave->next->prev = new_slave;
1246 new_slave->prev->next = new_slave;
1247 }
1248
1249 bond->slave_cnt++;
1250}
1251
1252/*
1253 * This function detaches the slave from the list.
1254 * WARNING: no check is made to verify if the slave effectively
1255 * belongs to <bond>.
1256 * Nothing is freed on return, structures are just unchained.
1257 * If any slave pointer in bond was pointing to <slave>,
1258 * it should be changed by the calling function.
1259 *
1260 * bond->lock held for writing by caller.
1261 */
1262static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1263{
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001264 if (slave->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 slave->next->prev = slave->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001267 if (slave->prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 slave->prev->next = slave->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 if (bond->first_slave == slave) { /* slave is the first slave */
1271 if (bond->slave_cnt > 1) { /* there are more slave */
1272 bond->first_slave = slave->next;
1273 } else {
1274 bond->first_slave = NULL; /* slave was the last one */
1275 }
1276 }
1277
1278 slave->next = NULL;
1279 slave->prev = NULL;
1280 bond->slave_cnt--;
1281}
1282
WANG Congf6dc31a2010-05-06 00:48:51 -07001283#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001284static inline int slave_enable_netpoll(struct slave *slave)
WANG Congf6dc31a2010-05-06 00:48:51 -07001285{
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001286 struct netpoll *np;
1287 int err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001288
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001289 np = kzalloc(sizeof(*np), GFP_KERNEL);
1290 err = -ENOMEM;
1291 if (!np)
1292 goto out;
1293
1294 np->dev = slave->dev;
1295 err = __netpoll_setup(np);
1296 if (err) {
1297 kfree(np);
1298 goto out;
WANG Congf6dc31a2010-05-06 00:48:51 -07001299 }
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001300 slave->np = np;
1301out:
1302 return err;
1303}
1304static inline void slave_disable_netpoll(struct slave *slave)
1305{
1306 struct netpoll *np = slave->np;
1307
1308 if (!np)
1309 return;
1310
1311 slave->np = NULL;
1312 synchronize_rcu_bh();
1313 __netpoll_cleanup(np);
1314 kfree(np);
1315}
1316static inline bool slave_dev_support_netpoll(struct net_device *slave_dev)
1317{
1318 if (slave_dev->priv_flags & IFF_DISABLE_NETPOLL)
1319 return false;
1320 if (!slave_dev->netdev_ops->ndo_poll_controller)
1321 return false;
1322 return true;
WANG Congf6dc31a2010-05-06 00:48:51 -07001323}
1324
1325static void bond_poll_controller(struct net_device *bond_dev)
1326{
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001327}
1328
1329static void __bond_netpoll_cleanup(struct bonding *bond)
1330{
Neil Hormanc2355e12010-10-13 16:01:49 +00001331 struct slave *slave;
1332 int i;
1333
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001334 bond_for_each_slave(bond, slave, i)
1335 if (IS_UP(slave->dev))
1336 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07001337}
WANG Congf6dc31a2010-05-06 00:48:51 -07001338static void bond_netpoll_cleanup(struct net_device *bond_dev)
1339{
1340 struct bonding *bond = netdev_priv(bond_dev);
WANG Congf6dc31a2010-05-06 00:48:51 -07001341
1342 read_lock(&bond->lock);
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001343 __bond_netpoll_cleanup(bond);
WANG Congf6dc31a2010-05-06 00:48:51 -07001344 read_unlock(&bond->lock);
1345}
1346
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001347static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
1348{
1349 struct bonding *bond = netdev_priv(dev);
1350 struct slave *slave;
1351 int i, err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001352
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001353 read_lock(&bond->lock);
1354 bond_for_each_slave(bond, slave, i) {
1355 if (!IS_UP(slave->dev))
1356 continue;
1357 err = slave_enable_netpoll(slave);
1358 if (err) {
1359 __bond_netpoll_cleanup(bond);
1360 break;
1361 }
1362 }
1363 read_unlock(&bond->lock);
1364 return err;
1365}
1366
1367static struct netpoll_info *bond_netpoll_info(struct bonding *bond)
1368{
1369 return bond->dev->npinfo;
1370}
1371
1372#else
1373static inline int slave_enable_netpoll(struct slave *slave)
1374{
1375 return 0;
1376}
1377static inline void slave_disable_netpoll(struct slave *slave)
1378{
1379}
WANG Congf6dc31a2010-05-06 00:48:51 -07001380static void bond_netpoll_cleanup(struct net_device *bond_dev)
1381{
1382}
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001383static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
1384{
1385 return 0;
1386}
1387static struct netpoll_info *bond_netpoll_info(struct bonding *bond)
1388{
1389 return NULL;
1390}
WANG Congf6dc31a2010-05-06 00:48:51 -07001391#endif
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393/*---------------------------------- IOCTL ----------------------------------*/
1394
Adrian Bunk4ad072c2007-07-09 11:51:12 -07001395static int bond_sethwaddr(struct net_device *bond_dev,
1396 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001398 pr_debug("bond_dev=%p\n", bond_dev);
1399 pr_debug("slave_dev=%p\n", slave_dev);
1400 pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1402 return 0;
1403}
1404
Herbert Xu7f353bf2007-08-10 15:47:58 -07001405#define BOND_VLAN_FEATURES \
1406 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1407 NETIF_F_HW_VLAN_FILTER)
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001408
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001409/*
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001410 * Compute the common dev->feature set available to all slaves. Some
Herbert Xu7f353bf2007-08-10 15:47:58 -07001411 * feature bits are managed elsewhere, so preserve those feature bits
1412 * on the master device.
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001413 */
1414static int bond_compute_features(struct bonding *bond)
1415{
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001416 struct slave *slave;
1417 struct net_device *bond_dev = bond->dev;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08001418 u32 features = bond_dev->features;
1419 u32 vlan_features = 0;
Moni Shoua3158bf72007-10-09 19:43:41 -07001420 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1421 bond_dev->hard_header_len);
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001422 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001423
Herbert Xu7f353bf2007-08-10 15:47:58 -07001424 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
Herbert Xub63365a2008-10-23 01:11:29 -07001425 features |= NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1426
1427 if (!bond->first_slave)
1428 goto done;
1429
1430 features &= ~NETIF_F_ONE_FOR_ALL;
Herbert Xu7f353bf2007-08-10 15:47:58 -07001431
Jay Vosburgh278339a2009-08-28 12:05:12 +00001432 vlan_features = bond->first_slave->dev->vlan_features;
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001433 bond_for_each_slave(bond, slave, i) {
Herbert Xub63365a2008-10-23 01:11:29 -07001434 features = netdev_increment_features(features,
1435 slave->dev->features,
1436 NETIF_F_ONE_FOR_ALL);
Jay Vosburgh278339a2009-08-28 12:05:12 +00001437 vlan_features = netdev_increment_features(vlan_features,
1438 slave->dev->vlan_features,
1439 NETIF_F_ONE_FOR_ALL);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001440 if (slave->dev->hard_header_len > max_hard_header_len)
1441 max_hard_header_len = slave->dev->hard_header_len;
1442 }
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001443
Herbert Xub63365a2008-10-23 01:11:29 -07001444done:
Herbert Xu7f353bf2007-08-10 15:47:58 -07001445 features |= (bond_dev->features & BOND_VLAN_FEATURES);
Michał Mirosławacd11302011-01-24 15:45:15 -08001446 bond_dev->features = netdev_fix_features(bond_dev, features);
1447 bond_dev->vlan_features = netdev_fix_features(bond_dev, vlan_features);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001448 bond_dev->hard_header_len = max_hard_header_len;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001449
1450 return 0;
1451}
1452
Moni Shoua872254d2007-10-09 19:43:38 -07001453static void bond_setup_by_slave(struct net_device *bond_dev,
1454 struct net_device *slave_dev)
1455{
Wang Chen454d7c92008-11-12 23:37:49 -08001456 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07001457
Stephen Hemminger00829822008-11-20 20:14:53 -08001458 bond_dev->header_ops = slave_dev->header_ops;
Moni Shoua872254d2007-10-09 19:43:38 -07001459
1460 bond_dev->type = slave_dev->type;
1461 bond_dev->hard_header_len = slave_dev->hard_header_len;
1462 bond_dev->addr_len = slave_dev->addr_len;
1463
1464 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1465 slave_dev->addr_len);
Moni Shouad90a1622007-10-09 19:43:43 -07001466 bond->setup_by_slave = 1;
Moni Shoua872254d2007-10-09 19:43:38 -07001467}
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469/* enslave device <slave> to bond device <master> */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001470int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Wang Chen454d7c92008-11-12 23:37:49 -08001472 struct bonding *bond = netdev_priv(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001473 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 struct slave *new_slave = NULL;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001475 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 struct sockaddr addr;
1477 int link_reporting;
1478 int old_features = bond_dev->features;
1479 int res = 0;
1480
nsxfreddy@gmail.com552709d2005-09-21 14:18:04 -05001481 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001482 slave_ops->ndo_do_ioctl == NULL) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001483 pr_warning("%s: Warning: no link monitoring support for %s\n",
1484 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486
1487 /* bond must be initialized by bond_open() before enslaving */
1488 if (!(bond_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001489 pr_warning("%s: master_dev is not up in bond_enslave\n",
1490 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
1492
1493 /* already enslaved */
1494 if (slave_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001495 pr_debug("Error, Device was already enslaved\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return -EBUSY;
1497 }
1498
1499 /* vlan challenged mutual exclusion */
1500 /* no need to lock since we're protected by rtnl_lock */
1501 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001502 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Jay Vosburghf35188f2010-07-21 12:14:47 +00001503 if (bond->vlgrp) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001504 pr_err("%s: Error: cannot enslave VLAN challenged slave %s on VLAN enabled bond %s\n",
1505 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return -EPERM;
1507 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001508 pr_warning("%s: Warning: enslaved VLAN challenged slave %s. Adding VLANs will be blocked as long as %s is part of bond %s\n",
1509 bond_dev->name, slave_dev->name,
1510 slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1512 }
1513 } else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001514 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 if (bond->slave_cnt == 0) {
1516 /* First slave, and it is not VLAN challenged,
1517 * so remove the block of adding VLANs over the bond.
1518 */
1519 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1520 }
1521 }
1522
Jay Vosburgh217df672005-09-26 16:11:50 -07001523 /*
1524 * Old ifenslave binaries are no longer supported. These can
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001525 * be identified with moderate accuracy by the state of the slave:
Jay Vosburgh217df672005-09-26 16:11:50 -07001526 * the current ifenslave will set the interface down prior to
1527 * enslaving it; the old ifenslave will not.
1528 */
1529 if ((slave_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001530 pr_err("%s is up. This may be due to an out of date ifenslave.\n",
Jay Vosburgh217df672005-09-26 16:11:50 -07001531 slave_dev->name);
1532 res = -EPERM;
1533 goto err_undo_flags;
1534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Moni Shoua872254d2007-10-09 19:43:38 -07001536 /* set bonding device ether type by slave - bonding netdevices are
1537 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1538 * there is a need to override some of the type dependent attribs/funcs.
1539 *
1540 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1541 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1542 */
1543 if (bond->slave_cnt == 0) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001544 if (bond_dev->type != slave_dev->type) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001545 pr_debug("%s: change device type from %d to %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001546 bond_dev->name,
1547 bond_dev->type, slave_dev->type);
Moni Shoua75c78502009-09-15 02:37:40 -07001548
Jiri Pirko3ca5b402010-03-10 10:29:35 +00001549 res = netdev_bonding_change(bond_dev,
1550 NETDEV_PRE_TYPE_CHANGE);
1551 res = notifier_to_errno(res);
1552 if (res) {
1553 pr_err("%s: refused to change device type\n",
1554 bond_dev->name);
1555 res = -EBUSY;
1556 goto err_undo_flags;
1557 }
Moni Shoua75c78502009-09-15 02:37:40 -07001558
Jiri Pirko32a806c2010-03-19 04:00:23 +00001559 /* Flush unicast and multicast addresses */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001560 dev_uc_flush(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001561 dev_mc_flush(bond_dev);
Jiri Pirko32a806c2010-03-19 04:00:23 +00001562
Moni Shouae36b9d12009-07-15 04:56:31 +00001563 if (slave_dev->type != ARPHRD_ETHER)
1564 bond_setup_by_slave(bond_dev, slave_dev);
1565 else
1566 ether_setup(bond_dev);
Moni Shoua75c78502009-09-15 02:37:40 -07001567
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001568 netdev_bonding_change(bond_dev,
1569 NETDEV_POST_TYPE_CHANGE);
Moni Shouae36b9d12009-07-15 04:56:31 +00001570 }
Moni Shoua872254d2007-10-09 19:43:38 -07001571 } else if (bond_dev->type != slave_dev->type) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001572 pr_err("%s ether type (%d) is different from other slaves (%d), can not enslave it.\n",
1573 slave_dev->name,
1574 slave_dev->type, bond_dev->type);
1575 res = -EINVAL;
1576 goto err_undo_flags;
Moni Shoua872254d2007-10-09 19:43:38 -07001577 }
1578
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001579 if (slave_ops->ndo_set_mac_address == NULL) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001580 if (bond->slave_cnt == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001581 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.",
1582 bond_dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001583 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1584 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001585 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",
1586 bond_dev->name);
Moni Shoua2ab82852007-10-09 19:43:39 -07001587 res = -EOPNOTSUPP;
1588 goto err_undo_flags;
1589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
1591
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001592 /* If this is the first slave, then we need to set the master's hardware
1593 * address to be the same as the slave's. */
David Strandd13a2cb2010-12-01 11:43:08 -08001594 if (is_zero_ether_addr(bond->dev->dev_addr))
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001595 memcpy(bond->dev->dev_addr, slave_dev->dev_addr,
1596 slave_dev->addr_len);
1597
1598
Joe Jin243cb4e2007-02-06 14:16:40 -08001599 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (!new_slave) {
1601 res = -ENOMEM;
1602 goto err_undo_flags;
1603 }
1604
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001605 /*
1606 * Set the new_slave's queue_id to be zero. Queue ID mapping
1607 * is set via sysfs or module option if desired.
1608 */
1609 new_slave->queue_id = 0;
1610
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001611 /* Save slave's original mtu and then set it to match the bond */
1612 new_slave->original_mtu = slave_dev->mtu;
1613 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1614 if (res) {
1615 pr_debug("Error %d calling dev_set_mtu\n", res);
1616 goto err_free;
1617 }
1618
Jay Vosburgh217df672005-09-26 16:11:50 -07001619 /*
1620 * Save slave's original ("permanent") mac address for modes
1621 * that need it, and for restoring it upon release, and then
1622 * set it to the master's address
1623 */
1624 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Jay Vosburghdd957c52007-10-09 19:57:24 -07001626 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001627 /*
1628 * Set slave to master's mac address. The application already
1629 * set the master's mac address to that of the first slave
1630 */
1631 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1632 addr.sa_family = slave_dev->type;
1633 res = dev_set_mac_address(slave_dev, &addr);
1634 if (res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001635 pr_debug("Error %d calling set_mac_address\n", res);
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001636 goto err_restore_mtu;
Moni Shoua2ab82852007-10-09 19:43:39 -07001637 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Jiri Pirko1765a572011-02-12 06:48:36 +00001640 res = netdev_set_bond_master(slave_dev, bond_dev);
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001641 if (res) {
Jiri Pirko1765a572011-02-12 06:48:36 +00001642 pr_debug("Error %d calling netdev_set_bond_master\n", res);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001643 goto err_restore_mac;
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001644 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001645 /* open the slave since the application closed it */
1646 res = dev_open(slave_dev);
1647 if (res) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001648 pr_debug("Opening slave %s failed\n", slave_dev->name);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001649 goto err_unset_master;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 new_slave->dev = slave_dev;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001653 slave_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Holger Eitzenberger58402052008-12-09 23:07:13 -08001655 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 /* bond_alb_init_slave() must be called before all other stages since
1657 * it might fail and we do not want to have to undo everything
1658 */
1659 res = bond_alb_init_slave(bond, new_slave);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001660 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001661 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
1663
1664 /* If the mode USES_PRIMARY, then the new slave gets the
1665 * master's promisc (and mc) settings only if it becomes the
1666 * curr_active_slave, and that is taken care of later when calling
1667 * bond_change_active()
1668 */
1669 if (!USES_PRIMARY(bond->params.mode)) {
1670 /* set promiscuity level to new slave */
1671 if (bond_dev->flags & IFF_PROMISC) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001672 res = dev_set_promiscuity(slave_dev, 1);
1673 if (res)
1674 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676
1677 /* set allmulti level to new slave */
1678 if (bond_dev->flags & IFF_ALLMULTI) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001679 res = dev_set_allmulti(slave_dev, 1);
1680 if (res)
1681 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683
David S. Millerb9e40852008-07-15 00:15:08 -07001684 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 /* upload master's mc_list to new slave */
Jiri Pirko22bedad32010-04-01 21:22:57 +00001686 netdev_for_each_mc_addr(ha, bond_dev)
1687 dev_mc_add(slave_dev, ha->addr);
David S. Millerb9e40852008-07-15 00:15:08 -07001688 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 }
1690
1691 if (bond->params.mode == BOND_MODE_8023AD) {
1692 /* add lacpdu mc addr to mc list */
1693 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1694
Jiri Pirko22bedad32010-04-01 21:22:57 +00001695 dev_mc_add(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
1697
1698 bond_add_vlans_on_slave(bond, slave_dev);
1699
1700 write_lock_bh(&bond->lock);
1701
1702 bond_attach_slave(bond, new_slave);
1703
1704 new_slave->delay = 0;
1705 new_slave->link_failure_count = 0;
1706
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001707 bond_compute_features(bond);
1708
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001709 write_unlock_bh(&bond->lock);
1710
1711 read_lock(&bond->lock);
1712
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001713 new_slave->last_arp_rx = jiffies;
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if (bond->params.miimon && !bond->params.use_carrier) {
1716 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1717
1718 if ((link_reporting == -1) && !bond->params.arp_interval) {
1719 /*
1720 * miimon is set but a bonded network driver
1721 * does not support ETHTOOL/MII and
1722 * arp_interval is not set. Note: if
1723 * use_carrier is enabled, we will never go
1724 * here (because netif_carrier is always
1725 * supported); thus, we don't need to change
1726 * the messages for netif_carrier.
1727 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001728 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 -08001729 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 } else if (link_reporting == -1) {
1731 /* unable get link status using mii/ethtool */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001732 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",
1733 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 }
1735 }
1736
1737 /* check for initial state */
1738 if (!bond->params.miimon ||
1739 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1740 if (bond->params.updelay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001741 pr_debug("Initial state of slave_dev is BOND_LINK_BACK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 new_slave->link = BOND_LINK_BACK;
1743 new_slave->delay = bond->params.updelay;
1744 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001745 pr_debug("Initial state of slave_dev is BOND_LINK_UP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 new_slave->link = BOND_LINK_UP;
1747 }
1748 new_slave->jiffies = jiffies;
1749 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001750 pr_debug("Initial state of slave_dev is BOND_LINK_DOWN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 new_slave->link = BOND_LINK_DOWN;
1752 }
1753
1754 if (bond_update_speed_duplex(new_slave) &&
1755 (new_slave->link != BOND_LINK_DOWN)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001756 pr_warning("%s: Warning: failed to get speed and duplex from %s, assumed to be 100Mb/sec and Full.\n",
1757 bond_dev->name, new_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 if (bond->params.mode == BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001760 pr_warning("%s: Warning: Operation of 802.3ad mode requires ETHTOOL support in base driver for proper aggregator selection.\n",
1761 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
1763 }
1764
1765 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1766 /* if there is a primary slave, remember it */
Jiri Pirkoa5499522009-09-25 03:28:09 +00001767 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 bond->primary_slave = new_slave;
Jiri Pirkoa5499522009-09-25 03:28:09 +00001769 bond->force_primary = true;
1770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 }
1772
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001773 write_lock_bh(&bond->curr_slave_lock);
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 switch (bond->params.mode) {
1776 case BOND_MODE_ACTIVEBACKUP:
Jay Vosburgh8a8e4472006-09-22 21:56:15 -07001777 bond_set_slave_inactive_flags(new_slave);
1778 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 break;
1780 case BOND_MODE_8023AD:
1781 /* in 802.3ad mode, the internal mechanism
1782 * will activate the slaves in the selected
1783 * aggregator
1784 */
1785 bond_set_slave_inactive_flags(new_slave);
1786 /* if this is the first slave */
1787 if (bond->slave_cnt == 1) {
1788 SLAVE_AD_INFO(new_slave).id = 1;
1789 /* Initialize AD with the number of times that the AD timer is called in 1 second
1790 * can be called only after the mac address of the bond is set
1791 */
1792 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1793 bond->params.lacp_fast);
1794 } else {
1795 SLAVE_AD_INFO(new_slave).id =
1796 SLAVE_AD_INFO(new_slave->prev).id + 1;
1797 }
1798
1799 bond_3ad_bind_slave(new_slave);
1800 break;
1801 case BOND_MODE_TLB:
1802 case BOND_MODE_ALB:
1803 new_slave->state = BOND_STATE_ACTIVE;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001804 bond_set_slave_inactive_flags(new_slave);
Jiri Pirko5a29f782009-03-25 17:23:38 -07001805 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 break;
1807 default:
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001808 pr_debug("This slave is always active in trunk mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 /* always active in trunk mode */
1811 new_slave->state = BOND_STATE_ACTIVE;
1812
1813 /* In trunking mode there is little meaning to curr_active_slave
1814 * anyway (it holds no special properties of the bond device),
1815 * so we can change it without calling change_active_interface()
1816 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001817 if (!bond->curr_active_slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 bond->curr_active_slave = new_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 break;
1821 } /* switch(bond_mode) */
1822
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001823 write_unlock_bh(&bond->curr_slave_lock);
1824
Jay Vosburghff59c452006-03-27 13:27:43 -08001825 bond_set_carrier(bond);
1826
WANG Congf6dc31a2010-05-06 00:48:51 -07001827#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001828 slave_dev->npinfo = bond_netpoll_info(bond);
1829 if (slave_dev->npinfo) {
1830 if (slave_enable_netpoll(new_slave)) {
1831 read_unlock(&bond->lock);
1832 pr_info("Error, %s: master_dev is using netpoll, "
1833 "but new slave device does not support netpoll.\n",
1834 bond_dev->name);
1835 res = -EBUSY;
1836 goto err_close;
1837 }
WANG Congf6dc31a2010-05-06 00:48:51 -07001838 }
1839#endif
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001840
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001841 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001843 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1844 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001845 goto err_close;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001846
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001847 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
1848 bond_dev->name, slave_dev->name,
1849 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1850 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 /* enslave is successful */
1853 return 0;
1854
1855/* Undo stages on error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856err_close:
1857 dev_close(slave_dev);
1858
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001859err_unset_master:
Jiri Pirko1765a572011-02-12 06:48:36 +00001860 netdev_set_bond_master(slave_dev, NULL);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862err_restore_mac:
Jay Vosburghdd957c52007-10-09 19:57:24 -07001863 if (!bond->params.fail_over_mac) {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001864 /* XXX TODO - fom follow mode needs to change master's
1865 * MAC if this slave's MAC is in use by the bond, or at
1866 * least print a warning.
1867 */
Moni Shoua2ab82852007-10-09 19:43:39 -07001868 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1869 addr.sa_family = slave_dev->type;
1870 dev_set_mac_address(slave_dev, &addr);
1871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001873err_restore_mtu:
1874 dev_set_mtu(slave_dev, new_slave->original_mtu);
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876err_free:
1877 kfree(new_slave);
1878
1879err_undo_flags:
1880 bond_dev->features = old_features;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return res;
1883}
1884
1885/*
1886 * Try to release the slave device <slave> from the bond device <master>
1887 * It is legal to access curr_active_slave without a lock because all the function
1888 * is write-locked.
1889 *
1890 * The rules for slave state should be:
1891 * for Active/Backup:
1892 * Active stays on all backups go down
1893 * for Bonded connections:
1894 * The first up interface should be left on and all others downed.
1895 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001896int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Wang Chen454d7c92008-11-12 23:37:49 -08001898 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 struct slave *slave, *oldcurrent;
1900 struct sockaddr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902 /* slave is not a slave or master is not master of this slave */
1903 if (!(slave_dev->flags & IFF_SLAVE) ||
1904 (slave_dev->master != bond_dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001905 pr_err("%s: Error: cannot release %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 bond_dev->name, slave_dev->name);
1907 return -EINVAL;
1908 }
1909
Neil Hormane843fa52010-10-13 16:01:50 +00001910 block_netpoll_tx();
WANG Congf6dc31a2010-05-06 00:48:51 -07001911 netdev_bonding_change(bond_dev, NETDEV_BONDING_DESLAVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 write_lock_bh(&bond->lock);
1913
1914 slave = bond_get_slave_by_dev(bond, slave_dev);
1915 if (!slave) {
1916 /* not a slave of this bond */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001917 pr_info("%s: %s not enslaved\n",
1918 bond_dev->name, slave_dev->name);
Jay Vosburghf5e2a7b2006-02-07 21:17:22 -08001919 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001920 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 return -EINVAL;
1922 }
1923
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001924 if (!bond->params.fail_over_mac) {
Joe Perches8e95a202009-12-03 07:58:21 +00001925 if (!compare_ether_addr(bond_dev->dev_addr, slave->perm_hwaddr) &&
1926 bond->slave_cnt > 1)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001927 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",
1928 bond_dev->name, slave_dev->name,
1929 slave->perm_hwaddr,
1930 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
1932
1933 /* Inform AD package of unbinding of slave. */
1934 if (bond->params.mode == BOND_MODE_8023AD) {
1935 /* must be called before the slave is
1936 * detached from the list
1937 */
1938 bond_3ad_unbind_slave(slave);
1939 }
1940
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001941 pr_info("%s: releasing %s interface %s\n",
1942 bond_dev->name,
1943 (slave->state == BOND_STATE_ACTIVE) ? "active" : "backup",
1944 slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 oldcurrent = bond->curr_active_slave;
1947
1948 bond->current_arp_slave = NULL;
1949
1950 /* release the slave from its bond */
1951 bond_detach_slave(bond, slave);
1952
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001953 bond_compute_features(bond);
1954
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001955 if (bond->primary_slave == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 bond->primary_slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001958 if (oldcurrent == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 bond_change_active_slave(bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Holger Eitzenberger58402052008-12-09 23:07:13 -08001961 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 /* Must be called only after the slave has been
1963 * detached from the list and the curr_active_slave
1964 * has been cleared (if our_slave == old_current),
1965 * but before a new active slave is selected.
1966 */
Jay Vosburgh25433312008-01-17 16:24:59 -08001967 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 bond_alb_deinit_slave(bond, slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08001969 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
1971
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001972 if (oldcurrent == slave) {
1973 /*
1974 * Note that we hold RTNL over this sequence, so there
1975 * is no concern that another slave add/remove event
1976 * will interfere.
1977 */
1978 write_unlock_bh(&bond->lock);
1979 read_lock(&bond->lock);
1980 write_lock_bh(&bond->curr_slave_lock);
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 bond_select_active_slave(bond);
1983
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001984 write_unlock_bh(&bond->curr_slave_lock);
1985 read_unlock(&bond->lock);
1986 write_lock_bh(&bond->lock);
1987 }
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (bond->slave_cnt == 0) {
Jay Vosburghff59c452006-03-27 13:27:43 -08001990 bond_set_carrier(bond);
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 /* if the last slave was removed, zero the mac address
1993 * of the master so it will be set by the application
1994 * to the mac address of the first slave
1995 */
1996 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
1997
Jay Vosburghf35188f2010-07-21 12:14:47 +00001998 if (!bond->vlgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2000 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002001 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2002 bond_dev->name, bond_dev->name);
2003 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2004 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
2006 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2007 !bond_has_challenged_slaves(bond)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002008 pr_info("%s: last VLAN challenged slave %s left bond %s. VLAN blocking is removed\n",
2009 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
2011 }
2012
2013 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002014 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002016 /* must do this from outside any spinlocks */
2017 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 bond_del_vlans_from_slave(bond, slave_dev);
2020
2021 /* If the mode USES_PRIMARY, then we should only remove its
2022 * promisc and mc settings if it was the curr_active_slave, but that was
2023 * already taken care of above when we detached the slave
2024 */
2025 if (!USES_PRIMARY(bond->params.mode)) {
2026 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002027 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002031 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002035 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002037 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 }
2039
Jiri Pirko1765a572011-02-12 06:48:36 +00002040 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002042 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07002043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 /* close slave before restoring its mac address */
2045 dev_close(slave_dev);
2046
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07002047 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002048 /* restore original ("permanent") mac address */
2049 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2050 addr.sa_family = slave_dev->type;
2051 dev_set_mac_address(slave_dev, &addr);
2052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00002054 dev_set_mtu(slave_dev, slave->original_mtu);
2055
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002056 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002057 IFF_SLAVE_INACTIVE | IFF_BONDING |
2058 IFF_SLAVE_NEEDARP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 kfree(slave);
2061
2062 return 0; /* deletion OK */
2063}
2064
2065/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002066* First release a slave and than destroy the bond if no more slaves are left.
Moni Shouad90a1622007-10-09 19:43:43 -07002067* Must be under rtnl_lock when this function is called.
2068*/
stephen hemminger26d8ee72010-10-15 05:09:34 +00002069static int bond_release_and_destroy(struct net_device *bond_dev,
2070 struct net_device *slave_dev)
Moni Shouad90a1622007-10-09 19:43:43 -07002071{
Wang Chen454d7c92008-11-12 23:37:49 -08002072 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002073 int ret;
2074
2075 ret = bond_release(bond_dev, slave_dev);
2076 if ((ret == 0) && (bond->slave_cnt == 0)) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002077 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002078 pr_info("%s: destroying bond %s.\n",
2079 bond_dev->name, bond_dev->name);
Stephen Hemminger9e716262009-06-12 19:02:47 +00002080 unregister_netdevice(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002081 }
2082 return ret;
2083}
2084
2085/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 * This function releases all slaves.
2087 */
2088static int bond_release_all(struct net_device *bond_dev)
2089{
Wang Chen454d7c92008-11-12 23:37:49 -08002090 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 struct slave *slave;
2092 struct net_device *slave_dev;
2093 struct sockaddr addr;
2094
2095 write_lock_bh(&bond->lock);
2096
Jay Vosburghff59c452006-03-27 13:27:43 -08002097 netif_carrier_off(bond_dev);
2098
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002099 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102 bond->current_arp_slave = NULL;
2103 bond->primary_slave = NULL;
2104 bond_change_active_slave(bond, NULL);
2105
2106 while ((slave = bond->first_slave) != NULL) {
2107 /* Inform AD package of unbinding of slave
2108 * before slave is detached from the list.
2109 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002110 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 bond_3ad_unbind_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 slave_dev = slave->dev;
2114 bond_detach_slave(bond, slave);
2115
Jay Vosburgh25433312008-01-17 16:24:59 -08002116 /* now that the slave is detached, unlock and perform
2117 * all the undo steps that should not be called from
2118 * within a lock.
2119 */
2120 write_unlock_bh(&bond->lock);
2121
Holger Eitzenberger58402052008-12-09 23:07:13 -08002122 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 /* must be called only after the slave
2124 * has been detached from the list
2125 */
2126 bond_alb_deinit_slave(bond, slave);
2127 }
2128
Arthur Kepner8531c5f2005-08-23 01:34:53 -04002129 bond_compute_features(bond);
2130
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002131 bond_destroy_slave_symlinks(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 bond_del_vlans_from_slave(bond, slave_dev);
2133
2134 /* If the mode USES_PRIMARY, then we should only remove its
2135 * promisc and mc settings if it was the curr_active_slave, but that was
2136 * already taken care of above when we detached the slave
2137 */
2138 if (!USES_PRIMARY(bond->params.mode)) {
2139 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002140 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002144 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002148 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002150 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 }
2152
Jiri Pirko1765a572011-02-12 06:48:36 +00002153 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002155 slave_disable_netpoll(slave);
2156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 /* close slave before restoring its mac address */
2158 dev_close(slave_dev);
2159
Jay Vosburghdd957c52007-10-09 19:57:24 -07002160 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002161 /* restore original ("permanent") mac address*/
2162 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2163 addr.sa_family = slave_dev->type;
2164 dev_set_mac_address(slave_dev, &addr);
2165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002167 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
2168 IFF_SLAVE_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 kfree(slave);
2171
2172 /* re-acquire the lock before getting the next slave */
2173 write_lock_bh(&bond->lock);
2174 }
2175
2176 /* zero the mac address of the master so it will be
2177 * set by the application to the mac address of the
2178 * first slave
2179 */
2180 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2181
Jay Vosburghf35188f2010-07-21 12:14:47 +00002182 if (!bond->vlgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
Jay Vosburghf35188f2010-07-21 12:14:47 +00002184 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002185 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2186 bond_dev->name, bond_dev->name);
2187 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2188 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 }
2190
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002191 pr_info("%s: released all slaves\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
2193out:
2194 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 return 0;
2196}
2197
2198/*
2199 * This function changes the active slave to slave <slave_dev>.
2200 * It returns -EINVAL in the following cases.
2201 * - <slave_dev> is not found in the list.
2202 * - There is not active slave now.
2203 * - <slave_dev> is already active.
2204 * - The link state of <slave_dev> is not BOND_LINK_UP.
2205 * - <slave_dev> is not running.
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002206 * In these cases, this function does nothing.
2207 * In the other cases, current_slave pointer is changed and 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 */
2209static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2210{
Wang Chen454d7c92008-11-12 23:37:49 -08002211 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 struct slave *old_active = NULL;
2213 struct slave *new_active = NULL;
2214 int res = 0;
2215
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002216 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 /* Verify that master_dev is indeed the master of slave_dev */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002220 if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002223 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002225 read_lock(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 old_active = bond->curr_active_slave;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002227 read_unlock(&bond->curr_slave_lock);
2228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 new_active = bond_get_slave_by_dev(bond, slave_dev);
2230
2231 /*
2232 * Changing to the current active: do nothing; return success.
2233 */
2234 if (new_active && (new_active == old_active)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002235 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 return 0;
2237 }
2238
2239 if ((new_active) &&
2240 (old_active) &&
2241 (new_active->link == BOND_LINK_UP) &&
2242 IS_UP(new_active->dev)) {
Neil Hormane843fa52010-10-13 16:01:50 +00002243 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002244 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 bond_change_active_slave(bond, new_active);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002246 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002247 unblock_netpoll_tx();
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002248 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002251 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253 return res;
2254}
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2257{
Wang Chen454d7c92008-11-12 23:37:49 -08002258 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 info->bond_mode = bond->params.mode;
2261 info->miimon = bond->params.miimon;
2262
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002263 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 info->num_slaves = bond->slave_cnt;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002265 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
2267 return 0;
2268}
2269
2270static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2271{
Wang Chen454d7c92008-11-12 23:37:49 -08002272 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 struct slave *slave;
Eric Dumazet689c96c2009-04-23 03:39:04 +00002274 int i, res = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002276 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 bond_for_each_slave(bond, slave, i) {
2279 if (i == (int)info->slave_id) {
Eric Dumazet689c96c2009-04-23 03:39:04 +00002280 res = 0;
2281 strcpy(info->slave_name, slave->dev->name);
2282 info->link = slave->link;
2283 info->state = slave->state;
2284 info->link_failure_count = slave->link_failure_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 break;
2286 }
2287 }
2288
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002289 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Eric Dumazet689c96c2009-04-23 03:39:04 +00002291 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292}
2293
2294/*-------------------------------- Monitoring -------------------------------*/
2295
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002296
2297static int bond_miimon_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002299 struct slave *slave;
2300 int i, link_state, commit = 0;
Jiri Pirko41f89102009-04-24 03:57:29 +00002301 bool ignore_updelay;
2302
2303 ignore_updelay = !bond->curr_active_slave ? true : false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305 bond_for_each_slave(bond, slave, i) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002306 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002308 link_state = bond_check_dev_link(bond, slave->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
2310 switch (slave->link) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002311 case BOND_LINK_UP:
2312 if (link_state)
2313 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002315 slave->link = BOND_LINK_FAIL;
2316 slave->delay = bond->params.downdelay;
2317 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002318 pr_info("%s: link status down for %sinterface %s, disabling it in %d ms.\n",
2319 bond->dev->name,
2320 (bond->params.mode ==
2321 BOND_MODE_ACTIVEBACKUP) ?
2322 ((slave->state == BOND_STATE_ACTIVE) ?
2323 "active " : "backup ") : "",
2324 slave->dev->name,
2325 bond->params.downdelay * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002327 /*FALLTHRU*/
2328 case BOND_LINK_FAIL:
2329 if (link_state) {
2330 /*
2331 * recovered before downdelay expired
2332 */
2333 slave->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 slave->jiffies = jiffies;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002335 pr_info("%s: link status up again after %d ms for interface %s.\n",
2336 bond->dev->name,
2337 (bond->params.downdelay - slave->delay) *
2338 bond->params.miimon,
2339 slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002340 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002342
2343 if (slave->delay <= 0) {
2344 slave->new_link = BOND_LINK_DOWN;
2345 commit++;
2346 continue;
2347 }
2348
2349 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002352 case BOND_LINK_DOWN:
2353 if (!link_state)
2354 continue;
2355
2356 slave->link = BOND_LINK_BACK;
2357 slave->delay = bond->params.updelay;
2358
2359 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002360 pr_info("%s: link status up for interface %s, enabling it in %d ms.\n",
2361 bond->dev->name, slave->dev->name,
2362 ignore_updelay ? 0 :
2363 bond->params.updelay *
2364 bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002366 /*FALLTHRU*/
2367 case BOND_LINK_BACK:
2368 if (!link_state) {
2369 slave->link = BOND_LINK_DOWN;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002370 pr_info("%s: link status down again after %d ms for interface %s.\n",
2371 bond->dev->name,
2372 (bond->params.updelay - slave->delay) *
2373 bond->params.miimon,
2374 slave->dev->name);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002375
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002376 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002378
Jiri Pirko41f89102009-04-24 03:57:29 +00002379 if (ignore_updelay)
2380 slave->delay = 0;
2381
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002382 if (slave->delay <= 0) {
2383 slave->new_link = BOND_LINK_UP;
2384 commit++;
Jiri Pirko41f89102009-04-24 03:57:29 +00002385 ignore_updelay = false;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002386 continue;
2387 }
2388
2389 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 break;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002391 }
2392 }
2393
2394 return commit;
2395}
2396
2397static void bond_miimon_commit(struct bonding *bond)
2398{
2399 struct slave *slave;
2400 int i;
2401
2402 bond_for_each_slave(bond, slave, i) {
2403 switch (slave->new_link) {
2404 case BOND_LINK_NOCHANGE:
2405 continue;
2406
2407 case BOND_LINK_UP:
2408 slave->link = BOND_LINK_UP;
2409 slave->jiffies = jiffies;
2410
2411 if (bond->params.mode == BOND_MODE_8023AD) {
2412 /* prevent it from being the active one */
2413 slave->state = BOND_STATE_BACKUP;
2414 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2415 /* make it immediately active */
2416 slave->state = BOND_STATE_ACTIVE;
2417 } else if (slave != bond->primary_slave) {
2418 /* prevent it from being the active one */
2419 slave->state = BOND_STATE_BACKUP;
2420 }
2421
Krzysztof Piotr Oledzki546add72010-10-06 14:28:22 -07002422 bond_update_speed_duplex(slave);
2423
Krzysztof Piotr Oledzki700c2a72010-10-06 14:25:06 -07002424 pr_info("%s: link status definitely up for interface %s, %d Mbps %s duplex.\n",
2425 bond->dev->name, slave->dev->name,
2426 slave->speed, slave->duplex ? "full" : "half");
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002427
2428 /* notify ad that the link status has changed */
2429 if (bond->params.mode == BOND_MODE_8023AD)
2430 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2431
Holger Eitzenberger58402052008-12-09 23:07:13 -08002432 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002433 bond_alb_handle_link_change(bond, slave,
2434 BOND_LINK_UP);
2435
2436 if (!bond->curr_active_slave ||
2437 (slave == bond->primary_slave))
2438 goto do_failover;
2439
2440 continue;
2441
2442 case BOND_LINK_DOWN:
Jay Vosburghfba4acd2008-10-30 17:41:14 -07002443 if (slave->link_failure_count < UINT_MAX)
2444 slave->link_failure_count++;
2445
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002446 slave->link = BOND_LINK_DOWN;
2447
2448 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2449 bond->params.mode == BOND_MODE_8023AD)
2450 bond_set_slave_inactive_flags(slave);
2451
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002452 pr_info("%s: link status definitely down for interface %s, disabling it\n",
2453 bond->dev->name, slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002454
2455 if (bond->params.mode == BOND_MODE_8023AD)
2456 bond_3ad_handle_link_change(slave,
2457 BOND_LINK_DOWN);
2458
Jiri Pirkoae63e802009-05-27 05:42:36 +00002459 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002460 bond_alb_handle_link_change(bond, slave,
2461 BOND_LINK_DOWN);
2462
2463 if (slave == bond->curr_active_slave)
2464 goto do_failover;
2465
2466 continue;
2467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002469 pr_err("%s: invalid new link %d on slave %s\n",
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002470 bond->dev->name, slave->new_link,
2471 slave->dev->name);
2472 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002474 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002477do_failover:
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002478 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00002479 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002480 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 bond_select_active_slave(bond);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002482 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002483 unblock_netpoll_tx();
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002484 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002485
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002486 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002489/*
2490 * bond_mii_monitor
2491 *
2492 * Really a wrapper that splits the mii monitor into two phases: an
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002493 * inspection, then (if inspection indicates something needs to be done)
2494 * an acquisition of appropriate locks followed by a commit phase to
2495 * implement whatever link state changes are indicated.
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002496 */
2497void bond_mii_monitor(struct work_struct *work)
2498{
2499 struct bonding *bond = container_of(work, struct bonding,
2500 mii_work.work);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002501
2502 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002503 if (bond->kill_timers)
2504 goto out;
2505
2506 if (bond->slave_cnt == 0)
2507 goto re_arm;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002508
2509 if (bond->send_grat_arp) {
2510 read_lock(&bond->curr_slave_lock);
2511 bond_send_gratuitous_arp(bond);
2512 read_unlock(&bond->curr_slave_lock);
2513 }
2514
Brian Haley305d5522008-11-04 17:51:14 -08002515 if (bond->send_unsol_na) {
2516 read_lock(&bond->curr_slave_lock);
2517 bond_send_unsolicited_na(bond);
2518 read_unlock(&bond->curr_slave_lock);
2519 }
2520
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002521 if (bond_miimon_inspect(bond)) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002522 read_unlock(&bond->lock);
2523 rtnl_lock();
2524 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002525
2526 bond_miimon_commit(bond);
2527
Jay Vosburgh56556622008-01-17 16:25:03 -08002528 read_unlock(&bond->lock);
2529 rtnl_unlock(); /* might sleep, hold no other locks */
2530 read_lock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002531 }
2532
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002533re_arm:
2534 if (bond->params.miimon)
2535 queue_delayed_work(bond->wq, &bond->mii_work,
2536 msecs_to_jiffies(bond->params.miimon));
2537out:
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002538 read_unlock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002539}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002540
Al Virod3bb52b2007-08-22 20:06:58 -04002541static __be32 bond_glean_dev_ip(struct net_device *dev)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002542{
2543 struct in_device *idev;
2544 struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07002545 __be32 addr = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002546
2547 if (!dev)
2548 return 0;
2549
2550 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002551 idev = __in_dev_get_rcu(dev);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002552 if (!idev)
2553 goto out;
2554
2555 ifa = idev->ifa_list;
2556 if (!ifa)
2557 goto out;
2558
2559 addr = ifa->ifa_local;
2560out:
2561 rcu_read_unlock();
2562 return addr;
2563}
2564
Al Virod3bb52b2007-08-22 20:06:58 -04002565static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002566{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002567 struct vlan_entry *vlan;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002568
2569 if (ip == bond->master_ip)
2570 return 1;
2571
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002572 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002573 if (ip == vlan->vlan_ip)
2574 return 1;
2575 }
2576
2577 return 0;
2578}
2579
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002580/*
2581 * We go to the (large) trouble of VLAN tagging ARP frames because
2582 * switches in VLAN mode (especially if ports are configured as
2583 * "native" to a VLAN) might not pass non-tagged frames.
2584 */
Al Virod3bb52b2007-08-22 20:06:58 -04002585static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002586{
2587 struct sk_buff *skb;
2588
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002589 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002590 slave_dev->name, dest_ip, src_ip, vlan_id);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002591
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002592 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2593 NULL, slave_dev->dev_addr, NULL);
2594
2595 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002596 pr_err("ARP packet allocation failed\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002597 return;
2598 }
2599 if (vlan_id) {
2600 skb = vlan_put_tag(skb, vlan_id);
2601 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002602 pr_err("failed to insert VLAN tag\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002603 return;
2604 }
2605 }
2606 arp_xmit(skb);
2607}
2608
2609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2611{
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002612 int i, vlan_id, rv;
Al Virod3bb52b2007-08-22 20:06:58 -04002613 __be32 *targets = bond->params.arp_targets;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002614 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002615 struct net_device *vlan_dev;
2616 struct flowi fl;
2617 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Mitch Williams6b780562005-11-09 10:36:19 -08002619 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2620 if (!targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07002621 break;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002622 pr_debug("basa: target %x\n", targets[i]);
Jay Vosburghf35188f2010-07-21 12:14:47 +00002623 if (!bond->vlgrp) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002624 pr_debug("basa: empty vlan: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002625 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2626 bond->master_ip, 0);
2627 continue;
2628 }
2629
2630 /*
2631 * If VLANs are configured, we do a route lookup to
2632 * determine which VLAN interface would be used, so we
2633 * can tag the ARP with the proper VLAN tag.
2634 */
2635 memset(&fl, 0, sizeof(fl));
2636 fl.fl4_dst = targets[i];
2637 fl.fl4_tos = RTO_ONLINK;
2638
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00002639 rv = ip_route_output_key(dev_net(bond->dev), &rt, &fl);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002640 if (rv) {
2641 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002642 pr_warning("%s: no route to arp_ip_target %pI4\n",
2643 bond->dev->name, &fl.fl4_dst);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002644 }
2645 continue;
2646 }
2647
2648 /*
2649 * This target is not on a VLAN
2650 */
Changli Gaod8d1f302010-06-10 23:31:35 -07002651 if (rt->dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002652 ip_rt_put(rt);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002653 pr_debug("basa: rtdev == bond->dev: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002654 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2655 bond->master_ip, 0);
2656 continue;
2657 }
2658
2659 vlan_id = 0;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002660 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002661 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Changli Gaod8d1f302010-06-10 23:31:35 -07002662 if (vlan_dev == rt->dst.dev) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002663 vlan_id = vlan->vlan_id;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002664 pr_debug("basa: vlan match on %s %d\n",
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002665 vlan_dev->name, vlan_id);
2666 break;
2667 }
2668 }
2669
2670 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002671 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002672 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2673 vlan->vlan_ip, vlan_id);
2674 continue;
2675 }
2676
2677 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002678 pr_warning("%s: no path to arp_ip_target %pI4 via rt.dev %s\n",
2679 bond->dev->name, &fl.fl4_dst,
Changli Gaod8d1f302010-06-10 23:31:35 -07002680 rt->dst.dev ? rt->dst.dev->name : "NULL");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002681 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002682 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002683 }
2684}
2685
2686/*
2687 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2688 * for each VLAN above us.
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002689 *
2690 * Caller must hold curr_slave_lock for read or better
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002691 */
2692static void bond_send_gratuitous_arp(struct bonding *bond)
2693{
2694 struct slave *slave = bond->curr_active_slave;
2695 struct vlan_entry *vlan;
2696 struct net_device *vlan_dev;
2697
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002698 pr_debug("bond_send_grat_arp: bond %s slave %s\n",
2699 bond->dev->name, slave ? slave->dev->name : "NULL");
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002700
2701 if (!slave || !bond->send_grat_arp ||
2702 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002703 return;
2704
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002705 bond->send_grat_arp--;
2706
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002707 if (bond->master_ip) {
2708 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
Moni Shoua1053f622007-10-09 19:43:42 -07002709 bond->master_ip, 0);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002710 }
2711
Jay Vosburghf35188f2010-07-21 12:14:47 +00002712 if (!bond->vlgrp)
2713 return;
2714
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002715 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002716 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002717 if (vlan->vlan_ip) {
2718 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2719 vlan->vlan_ip, vlan->vlan_id);
2720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 }
2722}
2723
Al Virod3bb52b2007-08-22 20:06:58 -04002724static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002725{
2726 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002727 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002728
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002729 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002730 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002731 &sip, &tip, i, &targets[i],
2732 bond_has_this_ip(bond, tip));
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002733 if (sip == targets[i]) {
2734 if (bond_has_this_ip(bond, tip))
2735 slave->last_arp_rx = jiffies;
2736 return;
2737 }
2738 }
2739}
2740
2741static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2742{
2743 struct arphdr *arp;
2744 struct slave *slave;
2745 struct bonding *bond;
2746 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002747 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002748
Andy Gospodarek1f3c8802009-12-14 10:48:58 +00002749 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2750 /*
2751 * When using VLANS and bonding, dev and oriv_dev may be
2752 * incorrect if the physical interface supports VLAN
2753 * acceleration. With this change ARP validation now
2754 * works for hosts only reachable on the VLAN interface.
2755 */
2756 dev = vlan_dev_real_dev(dev);
2757 orig_dev = dev_get_by_index_rcu(dev_net(skb->dev),skb->skb_iif);
2758 }
2759
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002760 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2761 goto out;
2762
Wang Chen454d7c92008-11-12 23:37:49 -08002763 bond = netdev_priv(dev);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002764 read_lock(&bond->lock);
2765
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002766 pr_debug("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002767 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2768 orig_dev ? orig_dev->name : "NULL");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002769
2770 slave = bond_get_slave_by_dev(bond, orig_dev);
2771 if (!slave || !slave_do_arp_validate(bond, slave))
2772 goto out_unlock;
2773
Neil Hormanb3053252011-01-20 09:02:31 +00002774 skb = skb_share_check(skb, GFP_ATOMIC);
2775 if (!skb)
2776 goto out_unlock;
2777
Pavel Emelyanov988b7052008-03-03 12:20:57 -08002778 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002779 goto out_unlock;
2780
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002781 arp = arp_hdr(skb);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002782 if (arp->ar_hln != dev->addr_len ||
2783 skb->pkt_type == PACKET_OTHERHOST ||
2784 skb->pkt_type == PACKET_LOOPBACK ||
2785 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2786 arp->ar_pro != htons(ETH_P_IP) ||
2787 arp->ar_pln != 4)
2788 goto out_unlock;
2789
2790 arp_ptr = (unsigned char *)(arp + 1);
2791 arp_ptr += dev->addr_len;
2792 memcpy(&sip, arp_ptr, 4);
2793 arp_ptr += 4 + dev->addr_len;
2794 memcpy(&tip, arp_ptr, 4);
2795
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002796 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002797 bond->dev->name, slave->dev->name, slave->state,
2798 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2799 &sip, &tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002800
2801 /*
2802 * Backup slaves won't see the ARP reply, but do come through
2803 * here for each ARP probe (so we swap the sip/tip to validate
2804 * the probe). In a "redundant switch, common router" type of
2805 * configuration, the ARP probe will (hopefully) travel from
2806 * the active, through one switch, the router, then the other
2807 * switch before reaching the backup.
2808 */
2809 if (slave->state == BOND_STATE_ACTIVE)
2810 bond_validate_arp(bond, slave, sip, tip);
2811 else
2812 bond_validate_arp(bond, slave, tip, sip);
2813
2814out_unlock:
2815 read_unlock(&bond->lock);
2816out:
2817 dev_kfree_skb(skb);
2818 return NET_RX_SUCCESS;
2819}
2820
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821/*
2822 * this function is called regularly to monitor each slave's link
2823 * ensuring that traffic is being sent and received when arp monitoring
2824 * is used in load-balancing mode. if the adapter has been dormant, then an
2825 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2826 * arp monitoring in active backup mode.
2827 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002828void bond_loadbalance_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002830 struct bonding *bond = container_of(work, struct bonding,
2831 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 struct slave *slave, *oldcurrent;
2833 int do_failover = 0;
2834 int delta_in_ticks;
2835 int i;
2836
2837 read_lock(&bond->lock);
2838
Jay Vosburgh5ce0da82008-05-17 21:10:07 -07002839 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002841 if (bond->kill_timers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002844 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
2847 read_lock(&bond->curr_slave_lock);
2848 oldcurrent = bond->curr_active_slave;
2849 read_unlock(&bond->curr_slave_lock);
2850
2851 /* see if any of the previous devices are up now (i.e. they have
2852 * xmt and rcv traffic). the curr_active_slave does not come into
2853 * the picture unless it is null. also, slave->jiffies is not needed
2854 * here because we send an arp on each slave and give a slave as
2855 * long as it needs to get the tx/rx within the delta.
2856 * TODO: what about up/down delay in arp mode? it wasn't here before
2857 * so it can wait
2858 */
2859 bond_for_each_slave(bond, slave, i) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002860 unsigned long trans_start = dev_trans_start(slave->dev);
2861
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002863 if (time_in_range(jiffies,
2864 trans_start - delta_in_ticks,
2865 trans_start + delta_in_ticks) &&
2866 time_in_range(jiffies,
2867 slave->dev->last_rx - delta_in_ticks,
2868 slave->dev->last_rx + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870 slave->link = BOND_LINK_UP;
2871 slave->state = BOND_STATE_ACTIVE;
2872
2873 /* primary_slave has no meaning in round-robin
2874 * mode. the window of a slave being up and
2875 * curr_active_slave being null after enslaving
2876 * is closed.
2877 */
2878 if (!oldcurrent) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002879 pr_info("%s: link status definitely up for interface %s, ",
2880 bond->dev->name,
2881 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 do_failover = 1;
2883 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002884 pr_info("%s: interface %s is now up\n",
2885 bond->dev->name,
2886 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 }
2888 }
2889 } else {
2890 /* slave->link == BOND_LINK_UP */
2891
2892 /* not all switches will respond to an arp request
2893 * when the source ip is 0, so don't take the link down
2894 * if we don't know our ip yet
2895 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002896 if (!time_in_range(jiffies,
2897 trans_start - delta_in_ticks,
2898 trans_start + 2 * delta_in_ticks) ||
2899 !time_in_range(jiffies,
2900 slave->dev->last_rx - delta_in_ticks,
2901 slave->dev->last_rx + 2 * delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
2903 slave->link = BOND_LINK_DOWN;
2904 slave->state = BOND_STATE_BACKUP;
2905
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002906 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002909 pr_info("%s: interface %s is now down.\n",
2910 bond->dev->name,
2911 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002913 if (slave == oldcurrent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 do_failover = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 }
2916 }
2917
2918 /* note: if switch is in round-robin mode, all links
2919 * must tx arp to ensure all links rx an arp - otherwise
2920 * links may oscillate or not come up at all; if switch is
2921 * in something like xor mode, there is nothing we can
2922 * do - all replies will be rx'ed on same link causing slaves
2923 * to be unstable during low/no traffic periods
2924 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002925 if (IS_UP(slave->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 bond_arp_send_all(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 }
2928
2929 if (do_failover) {
Neil Hormane843fa52010-10-13 16:01:50 +00002930 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002931 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
2933 bond_select_active_slave(bond);
2934
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002935 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002936 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 }
2938
2939re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002940 if (bond->params.arp_interval)
2941 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942out:
2943 read_unlock(&bond->lock);
2944}
2945
2946/*
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002947 * Called to inspect slaves for active-backup mode ARP monitor link state
2948 * changes. Sets new_link in slaves to specify what action should take
2949 * place for the slave. Returns 0 if no changes are found, >0 if changes
2950 * to link states must be committed.
2951 *
2952 * Called with bond->lock held for read.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002954static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 struct slave *slave;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002957 int i, commit = 0;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002958 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 bond_for_each_slave(bond, slave, i) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002961 slave->new_link = BOND_LINK_NOCHANGE;
2962
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002964 if (time_in_range(jiffies,
2965 slave_last_rx(bond, slave) - delta_in_ticks,
2966 slave_last_rx(bond, slave) + delta_in_ticks)) {
2967
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002968 slave->new_link = BOND_LINK_UP;
2969 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002972 continue;
2973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002975 /*
2976 * Give slaves 2*delta after being enslaved or made
2977 * active. This avoids bouncing, as the last receive
2978 * times need a full ARP monitor cycle to be updated.
2979 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002980 if (time_in_range(jiffies,
2981 slave->jiffies - delta_in_ticks,
2982 slave->jiffies + 2 * delta_in_ticks))
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002983 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002985 /*
2986 * Backup slave is down if:
2987 * - No current_arp_slave AND
2988 * - more than 3*delta since last receive AND
2989 * - the bond has an IP address
2990 *
2991 * Note: a non-null current_arp_slave indicates
2992 * the curr_active_slave went down and we are
2993 * searching for a new one; under this condition
2994 * we only take the curr_active_slave down - this
2995 * gives each slave a chance to tx/rx traffic
2996 * before being taken out
2997 */
2998 if (slave->state == BOND_STATE_BACKUP &&
2999 !bond->current_arp_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003000 !time_in_range(jiffies,
3001 slave_last_rx(bond, slave) - delta_in_ticks,
3002 slave_last_rx(bond, slave) + 3 * delta_in_ticks)) {
3003
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003004 slave->new_link = BOND_LINK_DOWN;
3005 commit++;
3006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003008 /*
3009 * Active slave is down if:
3010 * - more than 2*delta since transmitting OR
3011 * - (more than 2*delta since receive AND
3012 * the bond has an IP address)
3013 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003014 trans_start = dev_trans_start(slave->dev);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003015 if ((slave->state == BOND_STATE_ACTIVE) &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003016 (!time_in_range(jiffies,
3017 trans_start - delta_in_ticks,
3018 trans_start + 2 * delta_in_ticks) ||
3019 !time_in_range(jiffies,
3020 slave_last_rx(bond, slave) - delta_in_ticks,
3021 slave_last_rx(bond, slave) + 2 * delta_in_ticks))) {
3022
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003023 slave->new_link = BOND_LINK_DOWN;
3024 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 }
3026 }
3027
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003028 return commit;
3029}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003031/*
3032 * Called to commit link state changes noted by inspection step of
3033 * active-backup mode ARP monitor.
3034 *
3035 * Called with RTNL and bond->lock for read.
3036 */
3037static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
3038{
3039 struct slave *slave;
3040 int i;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003041 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003043 bond_for_each_slave(bond, slave, i) {
3044 switch (slave->new_link) {
3045 case BOND_LINK_NOCHANGE:
3046 continue;
3047
3048 case BOND_LINK_UP:
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003049 trans_start = dev_trans_start(slave->dev);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003050 if ((!bond->curr_active_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003051 time_in_range(jiffies,
3052 trans_start - delta_in_ticks,
3053 trans_start + delta_in_ticks)) ||
Jiri Pirkob9f60252009-08-31 11:09:38 +00003054 bond->curr_active_slave != slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003055 slave->link = BOND_LINK_UP;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003056 bond->current_arp_slave = NULL;
3057
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003058 pr_info("%s: link status definitely up for interface %s.\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003059 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003060
Jiri Pirkob9f60252009-08-31 11:09:38 +00003061 if (!bond->curr_active_slave ||
3062 (slave == bond->primary_slave))
3063 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003064
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003065 }
3066
Jiri Pirkob9f60252009-08-31 11:09:38 +00003067 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003068
3069 case BOND_LINK_DOWN:
3070 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003073 slave->link = BOND_LINK_DOWN;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003074 bond_set_slave_inactive_flags(slave);
3075
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003076 pr_info("%s: link status definitely down for interface %s, disabling it\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003077 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003078
3079 if (slave == bond->curr_active_slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003080 bond->current_arp_slave = NULL;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003081 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003082 }
Jiri Pirkob9f60252009-08-31 11:09:38 +00003083
3084 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003085
3086 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003087 pr_err("%s: impossible: new_link %d on slave %s\n",
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003088 bond->dev->name, slave->new_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 slave->dev->name);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003090 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
Jiri Pirkob9f60252009-08-31 11:09:38 +00003093do_failover:
3094 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00003095 block_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003096 write_lock_bh(&bond->curr_slave_lock);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003097 bond_select_active_slave(bond);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003098 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00003099 unblock_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003100 }
3101
3102 bond_set_carrier(bond);
3103}
3104
3105/*
3106 * Send ARP probes for active-backup mode ARP monitor.
3107 *
3108 * Called with bond->lock held for read.
3109 */
3110static void bond_ab_arp_probe(struct bonding *bond)
3111{
3112 struct slave *slave;
3113 int i;
3114
3115 read_lock(&bond->curr_slave_lock);
3116
3117 if (bond->current_arp_slave && bond->curr_active_slave)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003118 pr_info("PROBE: c_arp %s && cas %s BAD\n",
3119 bond->current_arp_slave->dev->name,
3120 bond->curr_active_slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003121
3122 if (bond->curr_active_slave) {
3123 bond_arp_send_all(bond, bond->curr_active_slave);
3124 read_unlock(&bond->curr_slave_lock);
3125 return;
3126 }
3127
3128 read_unlock(&bond->curr_slave_lock);
3129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 /* if we don't have a curr_active_slave, search for the next available
3131 * backup slave from the current_arp_slave and make it the candidate
3132 * for becoming the curr_active_slave
3133 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003134
3135 if (!bond->current_arp_slave) {
3136 bond->current_arp_slave = bond->first_slave;
3137 if (!bond->current_arp_slave)
3138 return;
3139 }
3140
3141 bond_set_slave_inactive_flags(bond->current_arp_slave);
3142
3143 /* search for next candidate */
3144 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3145 if (IS_UP(slave->dev)) {
3146 slave->link = BOND_LINK_BACK;
3147 bond_set_slave_active_flags(slave);
3148 bond_arp_send_all(bond, slave);
3149 slave->jiffies = jiffies;
3150 bond->current_arp_slave = slave;
3151 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 }
3153
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003154 /* if the link state is up at this point, we
3155 * mark it down - this can happen if we have
3156 * simultaneous link failures and
3157 * reselect_active_interface doesn't make this
3158 * one the current slave so it is still marked
3159 * up when it is actually down
3160 */
3161 if (slave->link == BOND_LINK_UP) {
3162 slave->link = BOND_LINK_DOWN;
3163 if (slave->link_failure_count < UINT_MAX)
3164 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003166 bond_set_slave_inactive_flags(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003168 pr_info("%s: backup interface %s is now down.\n",
3169 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 }
3171 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003172}
3173
3174void bond_activebackup_arp_mon(struct work_struct *work)
3175{
3176 struct bonding *bond = container_of(work, struct bonding,
3177 arp_work.work);
3178 int delta_in_ticks;
3179
3180 read_lock(&bond->lock);
3181
3182 if (bond->kill_timers)
3183 goto out;
3184
3185 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3186
3187 if (bond->slave_cnt == 0)
3188 goto re_arm;
3189
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003190 if (bond->send_grat_arp) {
3191 read_lock(&bond->curr_slave_lock);
3192 bond_send_gratuitous_arp(bond);
3193 read_unlock(&bond->curr_slave_lock);
3194 }
3195
Brian Haley305d5522008-11-04 17:51:14 -08003196 if (bond->send_unsol_na) {
3197 read_lock(&bond->curr_slave_lock);
3198 bond_send_unsolicited_na(bond);
3199 read_unlock(&bond->curr_slave_lock);
3200 }
3201
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003202 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3203 read_unlock(&bond->lock);
3204 rtnl_lock();
3205 read_lock(&bond->lock);
3206
3207 bond_ab_arp_commit(bond, delta_in_ticks);
3208
3209 read_unlock(&bond->lock);
3210 rtnl_unlock();
3211 read_lock(&bond->lock);
3212 }
3213
3214 bond_ab_arp_probe(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
3216re_arm:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003217 if (bond->params.arp_interval)
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003218 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219out:
3220 read_unlock(&bond->lock);
3221}
3222
3223/*------------------------------ proc/seq_file-------------------------------*/
3224
3225#ifdef CONFIG_PROC_FS
3226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazete4a7b932010-10-29 01:52:46 +00003228 __acquires(RCU)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00003229 __acquires(&bond->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
3231 struct bonding *bond = seq->private;
3232 loff_t off = 0;
3233 struct slave *slave;
3234 int i;
3235
3236 /* make sure the bond won't be taken away */
Eric Dumazete4a7b932010-10-29 01:52:46 +00003237 rcu_read_lock();
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003238 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003240 if (*pos == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 return SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
3243 bond_for_each_slave(bond, slave, i) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003244 if (++off == *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 return slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 }
3247
3248 return NULL;
3249}
3250
3251static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3252{
3253 struct bonding *bond = seq->private;
3254 struct slave *slave = v;
3255
3256 ++*pos;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003257 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 return bond->first_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
3260 slave = slave->next;
3261
3262 return (slave == bond->first_slave) ? NULL : slave;
3263}
3264
3265static void bond_info_seq_stop(struct seq_file *seq, void *v)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00003266 __releases(&bond->lock)
Eric Dumazete4a7b932010-10-29 01:52:46 +00003267 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
3269 struct bonding *bond = seq->private;
3270
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003271 read_unlock(&bond->lock);
Eric Dumazete4a7b932010-10-29 01:52:46 +00003272 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273}
3274
3275static void bond_info_show_master(struct seq_file *seq)
3276{
3277 struct bonding *bond = seq->private;
3278 struct slave *curr;
Mitch Williams4756b022005-11-09 10:36:25 -08003279 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
3281 read_lock(&bond->curr_slave_lock);
3282 curr = bond->curr_active_slave;
3283 read_unlock(&bond->curr_slave_lock);
3284
Jay Vosburghdd957c52007-10-09 19:57:24 -07003285 seq_printf(seq, "Bonding Mode: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 bond_mode_name(bond->params.mode));
3287
Jay Vosburghdd957c52007-10-09 19:57:24 -07003288 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
3289 bond->params.fail_over_mac)
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07003290 seq_printf(seq, " (fail_over_mac %s)",
3291 fail_over_mac_tbl[bond->params.fail_over_mac].modename);
Jay Vosburghdd957c52007-10-09 19:57:24 -07003292
3293 seq_printf(seq, "\n");
3294
Mitch Williamsc61b75a2005-11-09 10:35:13 -08003295 if (bond->params.mode == BOND_MODE_XOR ||
3296 bond->params.mode == BOND_MODE_8023AD) {
3297 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3298 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3299 bond->params.xmit_policy);
3300 }
3301
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 if (USES_PRIMARY(bond->params.mode)) {
Jiri Pirkoa5499522009-09-25 03:28:09 +00003303 seq_printf(seq, "Primary Slave: %s",
Mitch Williams0f418b22005-11-09 10:35:21 -08003304 (bond->primary_slave) ?
3305 bond->primary_slave->dev->name : "None");
Jiri Pirkoa5499522009-09-25 03:28:09 +00003306 if (bond->primary_slave)
3307 seq_printf(seq, " (primary_reselect %s)",
3308 pri_reselect_tbl[bond->params.primary_reselect].modename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Jiri Pirkoa5499522009-09-25 03:28:09 +00003310 seq_printf(seq, "\nCurrently Active Slave: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 (curr) ? curr->dev->name : "None");
3312 }
3313
Jay Vosburghff59c452006-03-27 13:27:43 -08003314 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3315 "up" : "down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3317 seq_printf(seq, "Up Delay (ms): %d\n",
3318 bond->params.updelay * bond->params.miimon);
3319 seq_printf(seq, "Down Delay (ms): %d\n",
3320 bond->params.downdelay * bond->params.miimon);
3321
Mitch Williams4756b022005-11-09 10:36:25 -08003322
3323 /* ARP information */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003324 if (bond->params.arp_interval > 0) {
3325 int printed = 0;
Mitch Williams4756b022005-11-09 10:36:25 -08003326 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3327 bond->params.arp_interval);
3328
3329 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3330
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003331 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
Mitch Williams4756b022005-11-09 10:36:25 -08003332 if (!bond->params.arp_targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07003333 break;
Mitch Williams4756b022005-11-09 10:36:25 -08003334 if (printed)
3335 seq_printf(seq, ",");
Harvey Harrison8cf14e32008-10-29 22:43:33 -07003336 seq_printf(seq, " %pI4", &bond->params.arp_targets[i]);
Mitch Williams4756b022005-11-09 10:36:25 -08003337 printed = 1;
3338 }
3339 seq_printf(seq, "\n");
3340 }
3341
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 if (bond->params.mode == BOND_MODE_8023AD) {
3343 struct ad_info ad_info;
3344
3345 seq_puts(seq, "\n802.3ad info\n");
3346 seq_printf(seq, "LACP rate: %s\n",
3347 (bond->params.lacp_fast) ? "fast" : "slow");
Jay Vosburghfd989c82008-11-04 17:51:16 -08003348 seq_printf(seq, "Aggregator selection policy (ad_select): %s\n",
3349 ad_select_tbl[bond->params.ad_select].modename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
3351 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3352 seq_printf(seq, "bond %s has no active aggregator\n",
3353 bond->dev->name);
3354 } else {
3355 seq_printf(seq, "Active Aggregator Info:\n");
3356
3357 seq_printf(seq, "\tAggregator ID: %d\n",
3358 ad_info.aggregator_id);
3359 seq_printf(seq, "\tNumber of ports: %d\n",
3360 ad_info.ports);
3361 seq_printf(seq, "\tActor Key: %d\n",
3362 ad_info.actor_key);
3363 seq_printf(seq, "\tPartner Key: %d\n",
3364 ad_info.partner_key);
Johannes Berge1749612008-10-27 15:59:26 -07003365 seq_printf(seq, "\tPartner Mac Address: %pM\n",
3366 ad_info.partner_system);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 }
3368 }
3369}
3370
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003371static void bond_info_show_slave(struct seq_file *seq,
3372 const struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373{
3374 struct bonding *bond = seq->private;
3375
3376 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3377 seq_printf(seq, "MII Status: %s\n",
3378 (slave->link == BOND_LINK_UP) ? "up" : "down");
Krzysztof Oledzkidd53df22010-09-30 06:19:04 +00003379 seq_printf(seq, "Speed: %d Mbps\n", slave->speed);
3380 seq_printf(seq, "Duplex: %s\n", slave->duplex ? "full" : "half");
Kenzo Iwami655096452006-09-22 21:53:08 -07003381 seq_printf(seq, "Link Failure Count: %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 slave->link_failure_count);
3383
Johannes Berge1749612008-10-27 15:59:26 -07003384 seq_printf(seq, "Permanent HW addr: %pM\n", slave->perm_hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
3386 if (bond->params.mode == BOND_MODE_8023AD) {
3387 const struct aggregator *agg
3388 = SLAVE_AD_INFO(slave).port.aggregator;
3389
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003390 if (agg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 seq_printf(seq, "Aggregator ID: %d\n",
3392 agg->aggregator_identifier);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003393 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 seq_puts(seq, "Aggregator ID: N/A\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 }
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00003396 seq_printf(seq, "Slave queue ID: %d\n", slave->queue_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397}
3398
3399static int bond_info_seq_show(struct seq_file *seq, void *v)
3400{
3401 if (v == SEQ_START_TOKEN) {
3402 seq_printf(seq, "%s\n", version);
3403 bond_info_show_master(seq);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003404 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 bond_info_show_slave(seq, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
3407 return 0;
3408}
3409
Jan Engelhardt4101dec2009-01-14 13:52:18 -08003410static const struct seq_operations bond_info_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 .start = bond_info_seq_start,
3412 .next = bond_info_seq_next,
3413 .stop = bond_info_seq_stop,
3414 .show = bond_info_seq_show,
3415};
3416
3417static int bond_info_open(struct inode *inode, struct file *file)
3418{
3419 struct seq_file *seq;
3420 struct proc_dir_entry *proc;
3421 int res;
3422
3423 res = seq_open(file, &bond_info_seq_ops);
3424 if (!res) {
3425 /* recover the pointer buried in proc_dir_entry data */
3426 seq = file->private_data;
3427 proc = PDE(inode);
3428 seq->private = proc->data;
3429 }
3430
3431 return res;
3432}
3433
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08003434static const struct file_operations bond_info_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 .owner = THIS_MODULE,
3436 .open = bond_info_open,
3437 .read = seq_read,
3438 .llseek = seq_lseek,
3439 .release = seq_release,
3440};
3441
Nicolas de Pesloüan38fc0022009-10-13 00:45:06 -07003442static void bond_create_proc_entry(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443{
3444 struct net_device *bond_dev = bond->dev;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003445 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003447 if (bn->proc_dir) {
Denis V. Luneva95609c2008-04-29 01:02:29 -07003448 bond->proc_entry = proc_create_data(bond_dev->name,
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003449 S_IRUGO, bn->proc_dir,
Denis V. Luneva95609c2008-04-29 01:02:29 -07003450 &bond_info_fops, bond);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003451 if (bond->proc_entry == NULL)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003452 pr_warning("Warning: Cannot create /proc/net/%s/%s\n",
3453 DRV_NAME, bond_dev->name);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003454 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457}
3458
3459static void bond_remove_proc_entry(struct bonding *bond)
3460{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003461 struct net_device *bond_dev = bond->dev;
3462 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
3463
3464 if (bn->proc_dir && bond->proc_entry) {
3465 remove_proc_entry(bond->proc_file_name, bn->proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 memset(bond->proc_file_name, 0, IFNAMSIZ);
3467 bond->proc_entry = NULL;
3468 }
3469}
3470
3471/* Create the bonding directory under /proc/net, if doesn't exist yet.
3472 * Caller must hold rtnl_lock.
3473 */
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003474static void __net_init bond_create_proc_dir(struct bond_net *bn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003476 if (!bn->proc_dir) {
3477 bn->proc_dir = proc_mkdir(DRV_NAME, bn->net->proc_net);
3478 if (!bn->proc_dir)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003479 pr_warning("Warning: cannot create /proc/net/%s\n",
3480 DRV_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 }
3482}
3483
3484/* Destroy the bonding directory under /proc/net, if empty.
3485 * Caller must hold rtnl_lock.
3486 */
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003487static void __net_exit bond_destroy_proc_dir(struct bond_net *bn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003489 if (bn->proc_dir) {
3490 remove_proc_entry(DRV_NAME, bn->net->proc_net);
3491 bn->proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 }
3493}
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003494
3495#else /* !CONFIG_PROC_FS */
3496
Nicolas de Pesloüan38fc0022009-10-13 00:45:06 -07003497static void bond_create_proc_entry(struct bonding *bond)
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003498{
3499}
3500
3501static void bond_remove_proc_entry(struct bonding *bond)
3502{
3503}
3504
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003505static inline void bond_create_proc_dir(struct bond_net *bn)
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003506{
3507}
3508
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003509static inline void bond_destroy_proc_dir(struct bond_net *bn)
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003510{
3511}
3512
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513#endif /* CONFIG_PROC_FS */
3514
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003515
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516/*-------------------------- netdev event handling --------------------------*/
3517
3518/*
3519 * Change device name
3520 */
3521static int bond_event_changename(struct bonding *bond)
3522{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 bond_remove_proc_entry(bond);
3524 bond_create_proc_entry(bond);
Stephen Hemminger7e083842009-06-12 19:02:46 +00003525
Taku Izumif073c7c2010-12-09 15:17:13 +00003526 bond_debug_reregister(bond);
3527
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 return NOTIFY_DONE;
3529}
3530
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003531static int bond_master_netdev_event(unsigned long event,
3532 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533{
Wang Chen454d7c92008-11-12 23:37:49 -08003534 struct bonding *event_bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
3536 switch (event) {
3537 case NETDEV_CHANGENAME:
3538 return bond_event_changename(event_bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 default:
3540 break;
3541 }
3542
3543 return NOTIFY_DONE;
3544}
3545
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003546static int bond_slave_netdev_event(unsigned long event,
3547 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
3549 struct net_device *bond_dev = slave_dev->master;
Wang Chen454d7c92008-11-12 23:37:49 -08003550 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
3552 switch (event) {
3553 case NETDEV_UNREGISTER:
3554 if (bond_dev) {
Jay Vosburgh1284cd32007-10-15 16:44:27 -07003555 if (bond->setup_by_slave)
3556 bond_release_and_destroy(bond_dev, slave_dev);
3557 else
3558 bond_release(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 }
3560 break;
3561 case NETDEV_CHANGE:
Jay Vosburgh17d04502009-03-18 18:38:25 -07003562 if (bond->params.mode == BOND_MODE_8023AD || bond_is_lb(bond)) {
3563 struct slave *slave;
3564
3565 slave = bond_get_slave_by_dev(bond, slave_dev);
3566 if (slave) {
3567 u16 old_speed = slave->speed;
3568 u16 old_duplex = slave->duplex;
3569
3570 bond_update_speed_duplex(slave);
3571
3572 if (bond_is_lb(bond))
3573 break;
3574
3575 if (old_speed != slave->speed)
3576 bond_3ad_adapter_speed_changed(slave);
3577 if (old_duplex != slave->duplex)
3578 bond_3ad_adapter_duplex_changed(slave);
3579 }
3580 }
3581
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 break;
3583 case NETDEV_DOWN:
3584 /*
3585 * ... Or is it this?
3586 */
3587 break;
3588 case NETDEV_CHANGEMTU:
3589 /*
3590 * TODO: Should slaves be allowed to
3591 * independently alter their MTU? For
3592 * an active-backup bond, slaves need
3593 * not be the same type of device, so
3594 * MTUs may vary. For other modes,
3595 * slaves arguably should have the
3596 * same MTUs. To do this, we'd need to
3597 * take over the slave's change_mtu
3598 * function for the duration of their
3599 * servitude.
3600 */
3601 break;
3602 case NETDEV_CHANGENAME:
3603 /*
3604 * TODO: handle changing the primary's name
3605 */
3606 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003607 case NETDEV_FEAT_CHANGE:
3608 bond_compute_features(bond);
3609 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 default:
3611 break;
3612 }
3613
3614 return NOTIFY_DONE;
3615}
3616
3617/*
3618 * bond_netdev_event: handle netdev notifier chain events.
3619 *
3620 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003621 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 * locks for us to safely manipulate the slave devices (RTNL lock,
3623 * dev_probe_lock).
3624 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003625static int bond_netdev_event(struct notifier_block *this,
3626 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
3628 struct net_device *event_dev = (struct net_device *)ptr;
3629
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003630 pr_debug("event_dev: %s, event: %lx\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003631 event_dev ? event_dev->name : "None",
3632 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003634 if (!(event_dev->priv_flags & IFF_BONDING))
3635 return NOTIFY_DONE;
3636
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 if (event_dev->flags & IFF_MASTER) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003638 pr_debug("IFF_MASTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 return bond_master_netdev_event(event, event_dev);
3640 }
3641
3642 if (event_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003643 pr_debug("IFF_SLAVE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 return bond_slave_netdev_event(event, event_dev);
3645 }
3646
3647 return NOTIFY_DONE;
3648}
3649
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003650/*
3651 * bond_inetaddr_event: handle inetaddr notifier chain events.
3652 *
3653 * We keep track of device IPs primarily to use as source addresses in
3654 * ARP monitor probes (rather than spewing out broadcasts all the time).
3655 *
3656 * We track one IP for the main device (if it has one), plus one per VLAN.
3657 */
3658static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3659{
3660 struct in_ifaddr *ifa = ptr;
3661 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003662 struct bond_net *bn = net_generic(dev_net(event_dev), bond_net_id);
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003663 struct bonding *bond;
3664 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003665
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003666 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003667 if (bond->dev == event_dev) {
3668 switch (event) {
3669 case NETDEV_UP:
3670 bond->master_ip = ifa->ifa_local;
3671 return NOTIFY_OK;
3672 case NETDEV_DOWN:
3673 bond->master_ip = bond_glean_dev_ip(bond->dev);
3674 return NOTIFY_OK;
3675 default:
3676 return NOTIFY_DONE;
3677 }
3678 }
3679
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003680 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +00003681 if (!bond->vlgrp)
3682 continue;
Dan Aloni5c15bde2007-03-02 20:44:51 -08003683 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003684 if (vlan_dev == event_dev) {
3685 switch (event) {
3686 case NETDEV_UP:
3687 vlan->vlan_ip = ifa->ifa_local;
3688 return NOTIFY_OK;
3689 case NETDEV_DOWN:
3690 vlan->vlan_ip =
3691 bond_glean_dev_ip(vlan_dev);
3692 return NOTIFY_OK;
3693 default:
3694 return NOTIFY_DONE;
3695 }
3696 }
3697 }
3698 }
3699 return NOTIFY_DONE;
3700}
3701
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702static struct notifier_block bond_netdev_notifier = {
3703 .notifier_call = bond_netdev_event,
3704};
3705
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003706static struct notifier_block bond_inetaddr_notifier = {
3707 .notifier_call = bond_inetaddr_event,
3708};
3709
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710/*-------------------------- Packet type handling ---------------------------*/
3711
3712/* register to receive lacpdus on a bond */
3713static void bond_register_lacpdu(struct bonding *bond)
3714{
3715 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3716
3717 /* initialize packet type */
3718 pk_type->type = PKT_TYPE_LACPDU;
3719 pk_type->dev = bond->dev;
3720 pk_type->func = bond_3ad_lacpdu_recv;
3721
3722 dev_add_pack(pk_type);
3723}
3724
3725/* unregister to receive lacpdus on a bond */
3726static void bond_unregister_lacpdu(struct bonding *bond)
3727{
3728 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3729}
3730
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003731void bond_register_arp(struct bonding *bond)
3732{
3733 struct packet_type *pt = &bond->arp_mon_pt;
3734
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003735 if (pt->type)
3736 return;
3737
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003738 pt->type = htons(ETH_P_ARP);
Jay Vosburghe245cb72007-02-28 17:03:27 -08003739 pt->dev = bond->dev;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003740 pt->func = bond_arp_rcv;
3741 dev_add_pack(pt);
3742}
3743
3744void bond_unregister_arp(struct bonding *bond)
3745{
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003746 struct packet_type *pt = &bond->arp_mon_pt;
3747
3748 dev_remove_pack(pt);
3749 pt->type = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003750}
3751
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003752/*---------------------------- Hashing Policies -----------------------------*/
3753
3754/*
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003755 * Hash for the output device based upon layer 2 and layer 3 data. If
3756 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3757 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003758static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003759{
3760 struct ethhdr *data = (struct ethhdr *)skb->data;
3761 struct iphdr *iph = ip_hdr(skb);
3762
Brian Haleyf14c4e42008-09-02 10:08:08 -04003763 if (skb->protocol == htons(ETH_P_IP)) {
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003764 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
Jasper Spaansd3da6832009-10-23 04:08:46 +00003765 (data->h_dest[5] ^ data->h_source[5])) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003766 }
3767
Jasper Spaansd3da6832009-10-23 04:08:46 +00003768 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003769}
3770
3771/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003772 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003773 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3774 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3775 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003776static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003777{
3778 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003779 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003780 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003781 int layer4_xor = 0;
3782
Brian Haleyf14c4e42008-09-02 10:08:08 -04003783 if (skb->protocol == htons(ETH_P_IP)) {
3784 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003785 (iph->protocol == IPPROTO_TCP ||
3786 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003787 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003788 }
3789 return (layer4_xor ^
3790 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3791
3792 }
3793
Jasper Spaansd3da6832009-10-23 04:08:46 +00003794 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003795}
3796
3797/*
3798 * Hash for the output device based upon layer 2 data
3799 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003800static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003801{
3802 struct ethhdr *data = (struct ethhdr *)skb->data;
3803
Jasper Spaansd3da6832009-10-23 04:08:46 +00003804 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003805}
3806
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807/*-------------------------- Device entry points ----------------------------*/
3808
3809static int bond_open(struct net_device *bond_dev)
3810{
Wang Chen454d7c92008-11-12 23:37:49 -08003811 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
3813 bond->kill_timers = 0;
3814
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003815 INIT_DELAYED_WORK(&bond->mcast_work, bond_resend_igmp_join_requests_delayed);
3816
Holger Eitzenberger58402052008-12-09 23:07:13 -08003817 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 /* bond_alb_initialize must be called before the timer
3819 * is started.
3820 */
3821 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3822 /* something went wrong - fail the open operation */
stephen hemmingerb4739462010-01-25 23:34:15 +00003823 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 }
3825
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003826 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3827 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 }
3829
3830 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003831 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3832 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 }
3834
3835 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003836 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3837 INIT_DELAYED_WORK(&bond->arp_work,
3838 bond_activebackup_arp_mon);
3839 else
3840 INIT_DELAYED_WORK(&bond->arp_work,
3841 bond_loadbalance_arp_mon);
3842
3843 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003844 if (bond->params.arp_validate)
3845 bond_register_arp(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 }
3847
3848 if (bond->params.mode == BOND_MODE_8023AD) {
Adrian Bunka40745f2007-10-24 18:27:43 +02003849 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003850 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 /* register to receive LACPDUs */
3852 bond_register_lacpdu(bond);
Jay Vosburghfd989c82008-11-04 17:51:16 -08003853 bond_3ad_initiate_agg_selection(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 }
3855
3856 return 0;
3857}
3858
3859static int bond_close(struct net_device *bond_dev)
3860{
Wang Chen454d7c92008-11-12 23:37:49 -08003861 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862
3863 if (bond->params.mode == BOND_MODE_8023AD) {
3864 /* Unregister the receive of LACPDUs */
3865 bond_unregister_lacpdu(bond);
3866 }
3867
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003868 if (bond->params.arp_validate)
3869 bond_unregister_arp(bond);
3870
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 write_lock_bh(&bond->lock);
3872
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003873 bond->send_grat_arp = 0;
Brian Haley305d5522008-11-04 17:51:14 -08003874 bond->send_unsol_na = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
3876 /* signal timers not to re-arm */
3877 bond->kill_timers = 1;
3878
3879 write_unlock_bh(&bond->lock);
3880
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003882 cancel_delayed_work(&bond->mii_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 }
3884
3885 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003886 cancel_delayed_work(&bond->arp_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 }
3888
3889 switch (bond->params.mode) {
3890 case BOND_MODE_8023AD:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003891 cancel_delayed_work(&bond->ad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 break;
3893 case BOND_MODE_TLB:
3894 case BOND_MODE_ALB:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003895 cancel_delayed_work(&bond->alb_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 break;
3897 default:
3898 break;
3899 }
3900
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003901 if (delayed_work_pending(&bond->mcast_work))
3902 cancel_delayed_work(&bond->mcast_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
Holger Eitzenberger58402052008-12-09 23:07:13 -08003904 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 /* Must be called only after all
3906 * slaves have been released
3907 */
3908 bond_alb_deinitialize(bond);
3909 }
3910
3911 return 0;
3912}
3913
Eric Dumazet28172732010-07-07 14:58:56 -07003914static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3915 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916{
Wang Chen454d7c92008-11-12 23:37:49 -08003917 struct bonding *bond = netdev_priv(bond_dev);
Eric Dumazet28172732010-07-07 14:58:56 -07003918 struct rtnl_link_stats64 temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 struct slave *slave;
3920 int i;
3921
Eric Dumazet28172732010-07-07 14:58:56 -07003922 memset(stats, 0, sizeof(*stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
3924 read_lock_bh(&bond->lock);
3925
3926 bond_for_each_slave(bond, slave, i) {
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00003927 const struct rtnl_link_stats64 *sstats =
Eric Dumazet28172732010-07-07 14:58:56 -07003928 dev_get_stats(slave->dev, &temp);
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08003929
Eric Dumazet28172732010-07-07 14:58:56 -07003930 stats->rx_packets += sstats->rx_packets;
3931 stats->rx_bytes += sstats->rx_bytes;
3932 stats->rx_errors += sstats->rx_errors;
3933 stats->rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934
Eric Dumazet28172732010-07-07 14:58:56 -07003935 stats->tx_packets += sstats->tx_packets;
3936 stats->tx_bytes += sstats->tx_bytes;
3937 stats->tx_errors += sstats->tx_errors;
3938 stats->tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
Eric Dumazet28172732010-07-07 14:58:56 -07003940 stats->multicast += sstats->multicast;
3941 stats->collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
Eric Dumazet28172732010-07-07 14:58:56 -07003943 stats->rx_length_errors += sstats->rx_length_errors;
3944 stats->rx_over_errors += sstats->rx_over_errors;
3945 stats->rx_crc_errors += sstats->rx_crc_errors;
3946 stats->rx_frame_errors += sstats->rx_frame_errors;
3947 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3948 stats->rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
Eric Dumazet28172732010-07-07 14:58:56 -07003950 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3951 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3952 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3953 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3954 stats->tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 }
3956
3957 read_unlock_bh(&bond->lock);
3958
3959 return stats;
3960}
3961
3962static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3963{
3964 struct net_device *slave_dev = NULL;
3965 struct ifbond k_binfo;
3966 struct ifbond __user *u_binfo = NULL;
3967 struct ifslave k_sinfo;
3968 struct ifslave __user *u_sinfo = NULL;
3969 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 int res = 0;
3971
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003972 pr_debug("bond_ioctl: master=%s, cmd=%d\n", bond_dev->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
3974 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 case SIOCGMIIPHY:
3976 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003977 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003979
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 mii->phy_id = 0;
3981 /* Fall Through */
3982 case SIOCGMIIREG:
3983 /*
3984 * We do this again just in case we were called by SIOCGMIIREG
3985 * instead of SIOCGMIIPHY.
3986 */
3987 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003988 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003990
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
3992 if (mii->reg_num == 1) {
Wang Chen454d7c92008-11-12 23:37:49 -08003993 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 mii->val_out = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003995 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 read_lock(&bond->curr_slave_lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003997 if (netif_carrier_ok(bond->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 mii->val_out = BMSR_LSTATUS;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003999
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 read_unlock(&bond->curr_slave_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07004001 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 }
4003
4004 return 0;
4005 case BOND_INFO_QUERY_OLD:
4006 case SIOCBONDINFOQUERY:
4007 u_binfo = (struct ifbond __user *)ifr->ifr_data;
4008
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004009 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011
4012 res = bond_info_query(bond_dev, &k_binfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004013 if (res == 0 &&
4014 copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
4015 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016
4017 return res;
4018 case BOND_SLAVE_INFO_QUERY_OLD:
4019 case SIOCBONDSLAVEINFOQUERY:
4020 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
4021
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004022 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
4025 res = bond_slave_info_query(bond_dev, &k_sinfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004026 if (res == 0 &&
4027 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
4028 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
4030 return res;
4031 default:
4032 /* Go on */
4033 break;
4034 }
4035
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004036 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004039 slave_dev = dev_get_by_name(dev_net(bond_dev), ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004041 pr_debug("slave_dev=%p:\n", slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004043 if (!slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 res = -ENODEV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004045 else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004046 pr_debug("slave_dev->name=%s:\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 switch (cmd) {
4048 case BOND_ENSLAVE_OLD:
4049 case SIOCBONDENSLAVE:
4050 res = bond_enslave(bond_dev, slave_dev);
4051 break;
4052 case BOND_RELEASE_OLD:
4053 case SIOCBONDRELEASE:
4054 res = bond_release(bond_dev, slave_dev);
4055 break;
4056 case BOND_SETHWADDR_OLD:
4057 case SIOCBONDSETHWADDR:
4058 res = bond_sethwaddr(bond_dev, slave_dev);
4059 break;
4060 case BOND_CHANGE_ACTIVE_OLD:
4061 case SIOCBONDCHANGEACTIVE:
4062 res = bond_ioctl_change_active(bond_dev, slave_dev);
4063 break;
4064 default:
4065 res = -EOPNOTSUPP;
4066 }
4067
4068 dev_put(slave_dev);
4069 }
4070
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 return res;
4072}
4073
Jiri Pirko22bedad32010-04-01 21:22:57 +00004074static bool bond_addr_in_mc_list(unsigned char *addr,
4075 struct netdev_hw_addr_list *list,
4076 int addrlen)
4077{
4078 struct netdev_hw_addr *ha;
4079
4080 netdev_hw_addr_list_for_each(ha, list)
4081 if (!memcmp(ha->addr, addr, addrlen))
4082 return true;
4083
4084 return false;
4085}
4086
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087static void bond_set_multicast_list(struct net_device *bond_dev)
4088{
Wang Chen454d7c92008-11-12 23:37:49 -08004089 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00004090 struct netdev_hw_addr *ha;
4091 bool found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 /*
4094 * Do promisc before checking multicast_mode
4095 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004096 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07004097 /*
4098 * FIXME: Need to handle the error when one of the multi-slaves
4099 * encounters error.
4100 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 bond_set_promiscuity(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004103
4104 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 bond_set_promiscuity(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004106
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
4108 /* set allmulti flag to slaves */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004109 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07004110 /*
4111 * FIXME: Need to handle the error when one of the multi-slaves
4112 * encounters error.
4113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 bond_set_allmulti(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004116
4117 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 bond_set_allmulti(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004119
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004121 read_lock(&bond->lock);
4122
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 bond->flags = bond_dev->flags;
4124
4125 /* looking for addresses to add to slaves' mc list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004126 netdev_for_each_mc_addr(ha, bond_dev) {
4127 found = bond_addr_in_mc_list(ha->addr, &bond->mc_list,
4128 bond_dev->addr_len);
4129 if (!found)
4130 bond_mc_add(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 }
4132
4133 /* looking for addresses to delete from slaves' list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004134 netdev_hw_addr_list_for_each(ha, &bond->mc_list) {
4135 found = bond_addr_in_mc_list(ha->addr, &bond_dev->mc,
4136 bond_dev->addr_len);
4137 if (!found)
4138 bond_mc_del(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 }
4140
4141 /* save master's multicast list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004142 __hw_addr_flush(&bond->mc_list);
4143 __hw_addr_add_multiple(&bond->mc_list, &bond_dev->mc,
4144 bond_dev->addr_len, NETDEV_HW_ADDR_T_MULTICAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004146 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147}
4148
Stephen Hemminger00829822008-11-20 20:14:53 -08004149static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
4150{
4151 struct bonding *bond = netdev_priv(dev);
4152 struct slave *slave = bond->first_slave;
4153
4154 if (slave) {
4155 const struct net_device_ops *slave_ops
4156 = slave->dev->netdev_ops;
4157 if (slave_ops->ndo_neigh_setup)
Patrick McHardy72e22402009-03-05 01:57:44 -08004158 return slave_ops->ndo_neigh_setup(slave->dev, parms);
Stephen Hemminger00829822008-11-20 20:14:53 -08004159 }
4160 return 0;
4161}
4162
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163/*
4164 * Change the MTU of all of a master's slaves to match the master
4165 */
4166static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
4167{
Wang Chen454d7c92008-11-12 23:37:49 -08004168 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 struct slave *slave, *stop_at;
4170 int res = 0;
4171 int i;
4172
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004173 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004174 (bond_dev ? bond_dev->name : "None"), new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
4176 /* Can't hold bond->lock with bh disabled here since
4177 * some base drivers panic. On the other hand we can't
4178 * hold bond->lock without bh disabled because we'll
4179 * deadlock. The only solution is to rely on the fact
4180 * that we're under rtnl_lock here, and the slaves
4181 * list won't change. This doesn't solve the problem
4182 * of setting the slave's MTU while it is
4183 * transmitting, but the assumption is that the base
4184 * driver can handle that.
4185 *
4186 * TODO: figure out a way to safely iterate the slaves
4187 * list, but without holding a lock around the actual
4188 * call to the base driver.
4189 */
4190
4191 bond_for_each_slave(bond, slave, i) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004192 pr_debug("s %p s->p %p c_m %p\n",
4193 slave,
4194 slave->prev,
4195 slave->dev->netdev_ops->ndo_change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08004196
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 res = dev_set_mtu(slave->dev, new_mtu);
4198
4199 if (res) {
4200 /* If we failed to set the slave's mtu to the new value
4201 * we must abort the operation even in ACTIVE_BACKUP
4202 * mode, because if we allow the backup slaves to have
4203 * different mtu values than the active slave we'll
4204 * need to change their mtu when doing a failover. That
4205 * means changing their mtu from timer context, which
4206 * is probably not a good idea.
4207 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004208 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 goto unwind;
4210 }
4211 }
4212
4213 bond_dev->mtu = new_mtu;
4214
4215 return 0;
4216
4217unwind:
4218 /* unwind from head to the slave that failed */
4219 stop_at = slave;
4220 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4221 int tmp_res;
4222
4223 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4224 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004225 pr_debug("unwind err %d dev %s\n",
4226 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 }
4228 }
4229
4230 return res;
4231}
4232
4233/*
4234 * Change HW address
4235 *
4236 * Note that many devices must be down to change the HW address, and
4237 * downing the master releases all slaves. We can make bonds full of
4238 * bonding devices to test this, however.
4239 */
4240static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4241{
Wang Chen454d7c92008-11-12 23:37:49 -08004242 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 struct sockaddr *sa = addr, tmp_sa;
4244 struct slave *slave, *stop_at;
4245 int res = 0;
4246 int i;
4247
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004248 if (bond->params.mode == BOND_MODE_ALB)
4249 return bond_alb_set_mac_address(bond_dev, addr);
4250
4251
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004252 pr_debug("bond=%p, name=%s\n",
4253 bond, bond_dev ? bond_dev->name : "None");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
Jay Vosburghdd957c52007-10-09 19:57:24 -07004255 /*
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004256 * If fail_over_mac is set to active, do nothing and return
4257 * success. Returning an error causes ifenslave to fail.
Jay Vosburghdd957c52007-10-09 19:57:24 -07004258 */
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004259 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
Jay Vosburghdd957c52007-10-09 19:57:24 -07004260 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07004261
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004262 if (!is_valid_ether_addr(sa->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
4265 /* Can't hold bond->lock with bh disabled here since
4266 * some base drivers panic. On the other hand we can't
4267 * hold bond->lock without bh disabled because we'll
4268 * deadlock. The only solution is to rely on the fact
4269 * that we're under rtnl_lock here, and the slaves
4270 * list won't change. This doesn't solve the problem
4271 * of setting the slave's hw address while it is
4272 * transmitting, but the assumption is that the base
4273 * driver can handle that.
4274 *
4275 * TODO: figure out a way to safely iterate the slaves
4276 * list, but without holding a lock around the actual
4277 * call to the base driver.
4278 */
4279
4280 bond_for_each_slave(bond, slave, i) {
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004281 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004282 pr_debug("slave %p %s\n", slave, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004284 if (slave_ops->ndo_set_mac_address == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 res = -EOPNOTSUPP;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004286 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 goto unwind;
4288 }
4289
4290 res = dev_set_mac_address(slave->dev, addr);
4291 if (res) {
4292 /* TODO: consider downing the slave
4293 * and retry ?
4294 * User should expect communications
4295 * breakage anyway until ARP finish
4296 * updating, so...
4297 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004298 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 goto unwind;
4300 }
4301 }
4302
4303 /* success */
4304 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4305 return 0;
4306
4307unwind:
4308 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4309 tmp_sa.sa_family = bond_dev->type;
4310
4311 /* unwind from head to the slave that failed */
4312 stop_at = slave;
4313 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4314 int tmp_res;
4315
4316 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4317 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004318 pr_debug("unwind err %d dev %s\n",
4319 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 }
4321 }
4322
4323 return res;
4324}
4325
4326static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4327{
Wang Chen454d7c92008-11-12 23:37:49 -08004328 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 struct slave *slave, *start_at;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004330 int i, slave_no, res = 1;
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004331 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332
4333 read_lock(&bond->lock);
4334
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004335 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 goto out;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004337 /*
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004338 * Start with the curr_active_slave that joined the bond as the
4339 * default for sending IGMP traffic. For failover purposes one
4340 * needs to maintain some consistency for the interface that will
4341 * send the join/membership reports. The curr_active_slave found
4342 * will send all of this type of traffic.
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004343 */
Eric Dumazet00ae7022010-03-30 23:08:37 +00004344 if ((iph->protocol == IPPROTO_IGMP) &&
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004345 (skb->protocol == htons(ETH_P_IP))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004347 read_lock(&bond->curr_slave_lock);
4348 slave = bond->curr_active_slave;
4349 read_unlock(&bond->curr_slave_lock);
4350
4351 if (!slave)
4352 goto out;
4353 } else {
4354 /*
4355 * Concurrent TX may collide on rr_tx_counter; we accept
4356 * that as being rare enough not to justify using an
4357 * atomic op here.
4358 */
4359 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
4360
4361 bond_for_each_slave(bond, slave, i) {
4362 slave_no--;
4363 if (slave_no < 0)
4364 break;
4365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 }
4367
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004368 start_at = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 bond_for_each_slave_from(bond, slave, i, start_at) {
4370 if (IS_UP(slave->dev) &&
4371 (slave->link == BOND_LINK_UP) &&
4372 (slave->state == BOND_STATE_ACTIVE)) {
4373 res = bond_dev_queue_xmit(bond, skb, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 break;
4375 }
4376 }
4377
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378out:
4379 if (res) {
4380 /* no suitable interface, frame not sent */
4381 dev_kfree_skb(skb);
4382 }
4383 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004384 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385}
4386
John W. Linville075897c2005-09-28 17:50:53 -04004387
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388/*
4389 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4390 * the bond has a usable interface.
4391 */
4392static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4393{
Wang Chen454d7c92008-11-12 23:37:49 -08004394 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 int res = 1;
4396
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 read_lock(&bond->lock);
4398 read_lock(&bond->curr_slave_lock);
4399
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004400 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402
John W. Linville075897c2005-09-28 17:50:53 -04004403 if (!bond->curr_active_slave)
4404 goto out;
4405
John W. Linville075897c2005-09-28 17:50:53 -04004406 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4407
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004409 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 /* no suitable interface, frame not sent */
4411 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004412
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 read_unlock(&bond->curr_slave_lock);
4414 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004415 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416}
4417
4418/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004419 * In bond_xmit_xor() , we determine the output device by using a pre-
4420 * determined xmit_hash_policy(), If the selected device is not enabled,
4421 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 */
4423static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4424{
Wang Chen454d7c92008-11-12 23:37:49 -08004425 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 struct slave *slave, *start_at;
4427 int slave_no;
4428 int i;
4429 int res = 1;
4430
4431 read_lock(&bond->lock);
4432
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004433 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
Jasper Spaansa361c832009-10-23 04:09:24 +00004436 slave_no = bond->xmit_hash_policy(skb, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
4438 bond_for_each_slave(bond, slave, i) {
4439 slave_no--;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004440 if (slave_no < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 }
4443
4444 start_at = slave;
4445
4446 bond_for_each_slave_from(bond, slave, i, start_at) {
4447 if (IS_UP(slave->dev) &&
4448 (slave->link == BOND_LINK_UP) &&
4449 (slave->state == BOND_STATE_ACTIVE)) {
4450 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4451 break;
4452 }
4453 }
4454
4455out:
4456 if (res) {
4457 /* no suitable interface, frame not sent */
4458 dev_kfree_skb(skb);
4459 }
4460 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004461 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462}
4463
4464/*
4465 * in broadcast mode, we send everything to all usable interfaces.
4466 */
4467static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4468{
Wang Chen454d7c92008-11-12 23:37:49 -08004469 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 struct slave *slave, *start_at;
4471 struct net_device *tx_dev = NULL;
4472 int i;
4473 int res = 1;
4474
4475 read_lock(&bond->lock);
4476
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004477 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479
4480 read_lock(&bond->curr_slave_lock);
4481 start_at = bond->curr_active_slave;
4482 read_unlock(&bond->curr_slave_lock);
4483
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004484 if (!start_at)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
4487 bond_for_each_slave_from(bond, slave, i, start_at) {
4488 if (IS_UP(slave->dev) &&
4489 (slave->link == BOND_LINK_UP) &&
4490 (slave->state == BOND_STATE_ACTIVE)) {
4491 if (tx_dev) {
4492 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4493 if (!skb2) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004494 pr_err("%s: Error: bond_xmit_broadcast(): skb_clone() failed\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08004495 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 continue;
4497 }
4498
4499 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4500 if (res) {
4501 dev_kfree_skb(skb2);
4502 continue;
4503 }
4504 }
4505 tx_dev = slave->dev;
4506 }
4507 }
4508
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004509 if (tx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 res = bond_dev_queue_xmit(bond, skb, tx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
4512out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004513 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 /* no suitable interface, frame not sent */
4515 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004516
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 /* frame sent to all suitable interfaces */
4518 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004519 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520}
4521
4522/*------------------------- Device initialization ---------------------------*/
4523
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004524static void bond_set_xmit_hash_policy(struct bonding *bond)
4525{
4526 switch (bond->params.xmit_policy) {
4527 case BOND_XMIT_POLICY_LAYER23:
4528 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4529 break;
4530 case BOND_XMIT_POLICY_LAYER34:
4531 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4532 break;
4533 case BOND_XMIT_POLICY_LAYER2:
4534 default:
4535 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4536 break;
4537 }
4538}
4539
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004540/*
4541 * Lookup the slave that corresponds to a qid
4542 */
4543static inline int bond_slave_override(struct bonding *bond,
4544 struct sk_buff *skb)
4545{
4546 int i, res = 1;
4547 struct slave *slave = NULL;
4548 struct slave *check_slave;
4549
4550 read_lock(&bond->lock);
4551
4552 if (!BOND_IS_OK(bond) || !skb->queue_mapping)
4553 goto out;
4554
4555 /* Find out if any slaves have the same mapping as this skb. */
4556 bond_for_each_slave(bond, check_slave, i) {
4557 if (check_slave->queue_id == skb->queue_mapping) {
4558 slave = check_slave;
4559 break;
4560 }
4561 }
4562
4563 /* If the slave isn't UP, use default transmit policy. */
4564 if (slave && slave->queue_id && IS_UP(slave->dev) &&
4565 (slave->link == BOND_LINK_UP)) {
4566 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4567 }
4568
4569out:
4570 read_unlock(&bond->lock);
4571 return res;
4572}
4573
4574static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb)
4575{
4576 /*
4577 * This helper function exists to help dev_pick_tx get the correct
4578 * destination queue. Using a helper function skips the a call to
4579 * skb_tx_hash and will put the skbs in the queue we expect on their
4580 * way down to the bonding driver.
4581 */
4582 return skb->queue_mapping;
4583}
4584
Stephen Hemminger424efe92009-08-31 19:50:51 +00004585static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
Stephen Hemminger00829822008-11-20 20:14:53 -08004586{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004587 struct bonding *bond = netdev_priv(dev);
4588
Neil Hormane843fa52010-10-13 16:01:50 +00004589 /*
4590 * If we risk deadlock from transmitting this in the
4591 * netpoll path, tell netpoll to queue the frame for later tx
4592 */
4593 if (is_netpoll_tx_blocked(dev))
4594 return NETDEV_TX_BUSY;
4595
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004596 if (TX_QUEUE_OVERRIDE(bond->params.mode)) {
4597 if (!bond_slave_override(bond, skb))
4598 return NETDEV_TX_OK;
4599 }
Stephen Hemminger00829822008-11-20 20:14:53 -08004600
4601 switch (bond->params.mode) {
4602 case BOND_MODE_ROUNDROBIN:
4603 return bond_xmit_roundrobin(skb, dev);
4604 case BOND_MODE_ACTIVEBACKUP:
4605 return bond_xmit_activebackup(skb, dev);
4606 case BOND_MODE_XOR:
4607 return bond_xmit_xor(skb, dev);
4608 case BOND_MODE_BROADCAST:
4609 return bond_xmit_broadcast(skb, dev);
4610 case BOND_MODE_8023AD:
4611 return bond_3ad_xmit_xor(skb, dev);
4612 case BOND_MODE_ALB:
4613 case BOND_MODE_TLB:
4614 return bond_alb_xmit(skb, dev);
4615 default:
4616 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004617 pr_err("%s: Error: Unknown bonding mode %d\n",
4618 dev->name, bond->params.mode);
Stephen Hemminger00829822008-11-20 20:14:53 -08004619 WARN_ON_ONCE(1);
4620 dev_kfree_skb(skb);
4621 return NETDEV_TX_OK;
4622 }
4623}
4624
4625
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626/*
4627 * set bond mode specific net device operations
4628 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004629void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004631 struct net_device *bond_dev = bond->dev;
4632
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 switch (mode) {
4634 case BOND_MODE_ROUNDROBIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 break;
4636 case BOND_MODE_ACTIVEBACKUP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 break;
4638 case BOND_MODE_XOR:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004639 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 break;
4641 case BOND_MODE_BROADCAST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 break;
4643 case BOND_MODE_8023AD:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004644 bond_set_master_3ad_flags(bond);
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004645 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004648 bond_set_master_alb_flags(bond);
4649 /* FALLTHRU */
4650 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 break;
4652 default:
4653 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004654 pr_err("%s: Error: Unknown bonding mode %d\n",
4655 bond_dev->name, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 break;
4657 }
4658}
4659
Jay Vosburgh217df672005-09-26 16:11:50 -07004660static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4661 struct ethtool_drvinfo *drvinfo)
4662{
4663 strncpy(drvinfo->driver, DRV_NAME, 32);
4664 strncpy(drvinfo->version, DRV_VERSION, 32);
4665 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4666}
4667
Jeff Garzik7282d492006-09-13 14:30:00 -04004668static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004669 .get_drvinfo = bond_ethtool_get_drvinfo,
Stephen Hemmingerfa53eba2008-09-13 21:17:09 -04004670 .get_link = ethtool_op_get_link,
4671 .get_tx_csum = ethtool_op_get_tx_csum,
4672 .get_sg = ethtool_op_get_sg,
4673 .get_tso = ethtool_op_get_tso,
4674 .get_ufo = ethtool_op_get_ufo,
4675 .get_flags = ethtool_op_get_flags,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004676};
4677
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004678static const struct net_device_ops bond_netdev_ops = {
Stephen Hemminger181470f2009-06-12 19:02:52 +00004679 .ndo_init = bond_init,
Stephen Hemminger9e716262009-06-12 19:02:47 +00004680 .ndo_uninit = bond_uninit,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004681 .ndo_open = bond_open,
4682 .ndo_stop = bond_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08004683 .ndo_start_xmit = bond_start_xmit,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004684 .ndo_select_queue = bond_select_queue,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00004685 .ndo_get_stats64 = bond_get_stats,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004686 .ndo_do_ioctl = bond_do_ioctl,
4687 .ndo_set_multicast_list = bond_set_multicast_list,
4688 .ndo_change_mtu = bond_change_mtu,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004689 .ndo_set_mac_address = bond_set_mac_address,
Stephen Hemminger00829822008-11-20 20:14:53 -08004690 .ndo_neigh_setup = bond_neigh_setup,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004691 .ndo_vlan_rx_register = bond_vlan_rx_register,
4692 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4693 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
WANG Congf6dc31a2010-05-06 00:48:51 -07004694#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00004695 .ndo_netpoll_setup = bond_netpoll_setup,
WANG Congf6dc31a2010-05-06 00:48:51 -07004696 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
4697 .ndo_poll_controller = bond_poll_controller,
4698#endif
Jiri Pirko9232ecc2011-02-13 09:33:01 +00004699 .ndo_add_slave = bond_enslave,
4700 .ndo_del_slave = bond_release,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004701};
4702
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004703static void bond_destructor(struct net_device *bond_dev)
4704{
4705 struct bonding *bond = netdev_priv(bond_dev);
4706 if (bond->wq)
4707 destroy_workqueue(bond->wq);
4708 free_netdev(bond_dev);
4709}
4710
Stephen Hemminger181470f2009-06-12 19:02:52 +00004711static void bond_setup(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712{
Wang Chen454d7c92008-11-12 23:37:49 -08004713 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 /* initialize rwlocks */
4716 rwlock_init(&bond->lock);
4717 rwlock_init(&bond->curr_slave_lock);
4718
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00004719 bond->params = bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
4721 /* Initialize pointers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 bond->dev = bond_dev;
4723 INIT_LIST_HEAD(&bond->vlan_list);
4724
4725 /* Initialize the device entry points */
Stephen Hemminger181470f2009-06-12 19:02:52 +00004726 ether_setup(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004727 bond_dev->netdev_ops = &bond_netdev_ops;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004728 bond_dev->ethtool_ops = &bond_ethtool_ops;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004729 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004731 bond_dev->destructor = bond_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732
4733 /* Initialize the device options */
4734 bond_dev->tx_queue_len = 0;
4735 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004736 bond_dev->priv_flags |= IFF_BONDING;
Stephen Hemminger181470f2009-06-12 19:02:52 +00004737 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
4738
Jay Vosburgh6cf3f412008-11-03 18:16:50 -08004739 if (bond->params.arp_interval)
4740 bond_dev->priv_flags |= IFF_MASTER_ARPMON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
4742 /* At first, we block adding VLANs. That's the only way to
4743 * prevent problems that occur when adding VLANs over an
4744 * empty bond. The block will be removed once non-challenged
4745 * slaves are enslaved.
4746 */
4747 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4748
Herbert Xu932ff272006-06-09 12:20:56 -07004749 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 * transmitting */
4751 bond_dev->features |= NETIF_F_LLTX;
4752
4753 /* By default, we declare the bond to be fully
4754 * VLAN hardware accelerated capable. Special
4755 * care is taken in the various xmit functions
4756 * when there are slaves that are not hw accel
4757 * capable
4758 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4760 NETIF_F_HW_VLAN_RX |
4761 NETIF_F_HW_VLAN_FILTER);
4762
Eric Dumazete6599c22010-09-17 09:25:07 +00004763 /* By default, we enable GRO on bonding devices.
4764 * Actual support requires lowlevel drivers are GRO ready.
4765 */
4766 bond_dev->features |= NETIF_F_GRO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767}
4768
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004769static void bond_work_cancel_all(struct bonding *bond)
4770{
4771 write_lock_bh(&bond->lock);
4772 bond->kill_timers = 1;
4773 write_unlock_bh(&bond->lock);
4774
4775 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4776 cancel_delayed_work(&bond->mii_work);
4777
4778 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4779 cancel_delayed_work(&bond->arp_work);
4780
4781 if (bond->params.mode == BOND_MODE_ALB &&
4782 delayed_work_pending(&bond->alb_work))
4783 cancel_delayed_work(&bond->alb_work);
4784
4785 if (bond->params.mode == BOND_MODE_8023AD &&
4786 delayed_work_pending(&bond->ad_work))
4787 cancel_delayed_work(&bond->ad_work);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00004788
4789 if (delayed_work_pending(&bond->mcast_work))
4790 cancel_delayed_work(&bond->mcast_work);
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004791}
4792
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004793/*
4794* Destroy a bonding device.
4795* Must be under rtnl_lock when this function is called.
4796*/
4797static void bond_uninit(struct net_device *bond_dev)
Jay Vosburgha434e432008-10-30 17:41:15 -07004798{
Wang Chen454d7c92008-11-12 23:37:49 -08004799 struct bonding *bond = netdev_priv(bond_dev);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004800 struct vlan_entry *vlan, *tmp;
Jay Vosburgha434e432008-10-30 17:41:15 -07004801
WANG Congf6dc31a2010-05-06 00:48:51 -07004802 bond_netpoll_cleanup(bond_dev);
4803
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004804 /* Release the bonded slaves */
4805 bond_release_all(bond_dev);
4806
Jay Vosburgha434e432008-10-30 17:41:15 -07004807 list_del(&bond->bond_list);
4808
4809 bond_work_cancel_all(bond);
4810
Jay Vosburgha434e432008-10-30 17:41:15 -07004811 bond_remove_proc_entry(bond);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004812
Taku Izumif073c7c2010-12-09 15:17:13 +00004813 bond_debug_unregister(bond);
4814
Jiri Pirko22bedad32010-04-01 21:22:57 +00004815 __hw_addr_flush(&bond->mc_list);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004816
4817 list_for_each_entry_safe(vlan, tmp, &bond->vlan_list, vlan_list) {
4818 list_del(&vlan->vlan_list);
4819 kfree(vlan);
4820 }
Jay Vosburgha434e432008-10-30 17:41:15 -07004821}
4822
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823/*------------------------- Module initialization ---------------------------*/
4824
4825/*
4826 * Convert string input module parms. Accept either the
Jay Vosburghece95f72008-01-17 16:25:01 -08004827 * number of the mode or its string name. A bit complicated because
4828 * some mode names are substrings of other names, and calls from sysfs
4829 * may have whitespace in the name (trailing newlines, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 */
Holger Eitzenberger325dcf72008-12-09 23:10:17 -08004831int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832{
Hannes Eder54b87322009-02-14 11:15:49 +00004833 int modeint = -1, i, rv;
Jay Vosburgha42e5342008-01-29 18:07:43 -08004834 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
Jay Vosburghece95f72008-01-17 16:25:01 -08004835
Jay Vosburgha42e5342008-01-29 18:07:43 -08004836 for (p = (char *)buf; *p; p++)
4837 if (!(isdigit(*p) || isspace(*p)))
4838 break;
4839
4840 if (*p)
Jay Vosburghece95f72008-01-17 16:25:01 -08004841 rv = sscanf(buf, "%20s", modestr);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004842 else
Hannes Eder54b87322009-02-14 11:15:49 +00004843 rv = sscanf(buf, "%d", &modeint);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004844
4845 if (!rv)
4846 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847
4848 for (i = 0; tbl[i].modename; i++) {
Hannes Eder54b87322009-02-14 11:15:49 +00004849 if (modeint == tbl[i].mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 return tbl[i].mode;
Jay Vosburghece95f72008-01-17 16:25:01 -08004851 if (strcmp(modestr, tbl[i].modename) == 0)
4852 return tbl[i].mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 }
4854
4855 return -1;
4856}
4857
4858static int bond_check_params(struct bond_params *params)
4859{
Jiri Pirkoa5499522009-09-25 03:28:09 +00004860 int arp_validate_value, fail_over_mac_value, primary_reselect_value;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004861
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 /*
4863 * Convert string parameters.
4864 */
4865 if (mode) {
4866 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4867 if (bond_mode == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004868 pr_err("Error: Invalid bonding mode \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 mode == NULL ? "NULL" : mode);
4870 return -EINVAL;
4871 }
4872 }
4873
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004874 if (xmit_hash_policy) {
4875 if ((bond_mode != BOND_MODE_XOR) &&
4876 (bond_mode != BOND_MODE_8023AD)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004877 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004878 bond_mode_name(bond_mode));
4879 } else {
4880 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4881 xmit_hashtype_tbl);
4882 if (xmit_hashtype == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004883 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004884 xmit_hash_policy == NULL ? "NULL" :
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004885 xmit_hash_policy);
4886 return -EINVAL;
4887 }
4888 }
4889 }
4890
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 if (lacp_rate) {
4892 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004893 pr_info("lacp_rate param is irrelevant in mode %s\n",
4894 bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 } else {
4896 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4897 if (lacp_fast == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004898 pr_err("Error: Invalid lacp rate \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 lacp_rate == NULL ? "NULL" : lacp_rate);
4900 return -EINVAL;
4901 }
4902 }
4903 }
4904
Jay Vosburghfd989c82008-11-04 17:51:16 -08004905 if (ad_select) {
4906 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4907 if (params->ad_select == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004908 pr_err("Error: Invalid ad_select \"%s\"\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -08004909 ad_select == NULL ? "NULL" : ad_select);
4910 return -EINVAL;
4911 }
4912
4913 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004914 pr_warning("ad_select param only affects 802.3ad mode\n");
Jay Vosburghfd989c82008-11-04 17:51:16 -08004915 }
4916 } else {
4917 params->ad_select = BOND_AD_STABLE;
4918 }
4919
Nicolas de Pesloüanf5841302009-08-28 13:18:34 +00004920 if (max_bonds < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004921 pr_warning("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4922 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 max_bonds = BOND_DEFAULT_MAX_BONDS;
4924 }
4925
4926 if (miimon < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004927 pr_warning("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to %d\n",
4928 miimon, INT_MAX, BOND_LINK_MON_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 miimon = BOND_LINK_MON_INTERV;
4930 }
4931
4932 if (updelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004933 pr_warning("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4934 updelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 updelay = 0;
4936 }
4937
4938 if (downdelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004939 pr_warning("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4940 downdelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 downdelay = 0;
4942 }
4943
4944 if ((use_carrier != 0) && (use_carrier != 1)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004945 pr_warning("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
4946 use_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 use_carrier = 1;
4948 }
4949
Moni Shoua7893b242008-05-17 21:10:12 -07004950 if (num_grat_arp < 0 || num_grat_arp > 255) {
Frans Pop2381a552010-03-24 07:57:36 +00004951 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 -08004952 num_grat_arp);
Moni Shoua7893b242008-05-17 21:10:12 -07004953 num_grat_arp = 1;
4954 }
4955
Brian Haley305d5522008-11-04 17:51:14 -08004956 if (num_unsol_na < 0 || num_unsol_na > 255) {
Frans Pop2381a552010-03-24 07:57:36 +00004957 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 -08004958 num_unsol_na);
Brian Haley305d5522008-11-04 17:51:14 -08004959 num_unsol_na = 1;
4960 }
4961
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 /* reset values for 802.3ad */
4963 if (bond_mode == BOND_MODE_8023AD) {
4964 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004965 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 +00004966 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 miimon = 100;
4968 }
4969 }
4970
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004971 if (tx_queues < 1 || tx_queues > 255) {
4972 pr_warning("Warning: tx_queues (%d) should be between "
4973 "1 and 255, resetting to %d\n",
4974 tx_queues, BOND_DEFAULT_TX_QUEUES);
4975 tx_queues = BOND_DEFAULT_TX_QUEUES;
4976 }
4977
Andy Gospodarekebd8e492010-06-02 08:39:21 +00004978 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
4979 pr_warning("Warning: all_slaves_active module parameter (%d), "
4980 "not of valid value (0/1), so it was set to "
4981 "0\n", all_slaves_active);
4982 all_slaves_active = 0;
4983 }
4984
Flavio Leitnerc2952c32010-10-05 14:23:59 +00004985 if (resend_igmp < 0 || resend_igmp > 255) {
4986 pr_warning("Warning: resend_igmp (%d) should be between "
4987 "0 and 255, resetting to %d\n",
4988 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
4989 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
4990 }
4991
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 /* reset values for TLB/ALB */
4993 if ((bond_mode == BOND_MODE_TLB) ||
4994 (bond_mode == BOND_MODE_ALB)) {
4995 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004996 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 +00004997 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 miimon = 100;
4999 }
5000 }
5001
5002 if (bond_mode == BOND_MODE_ALB) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005003 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",
5004 updelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 }
5006
5007 if (!miimon) {
5008 if (updelay || downdelay) {
5009 /* just warn the user the up/down delay will have
5010 * no effect since miimon is zero...
5011 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005012 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",
5013 updelay, downdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 }
5015 } else {
5016 /* don't allow arp monitoring */
5017 if (arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005018 pr_warning("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
5019 miimon, arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 arp_interval = 0;
5021 }
5022
5023 if ((updelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005024 pr_warning("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
5025 updelay, miimon,
5026 (updelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 }
5028
5029 updelay /= miimon;
5030
5031 if ((downdelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005032 pr_warning("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
5033 downdelay, miimon,
5034 (downdelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 }
5036
5037 downdelay /= miimon;
5038 }
5039
5040 if (arp_interval < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005041 pr_warning("Warning: arp_interval module parameter (%d) , not in range 0-%d, so it was reset to %d\n",
5042 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 arp_interval = BOND_LINK_ARP_INTERV;
5044 }
5045
5046 for (arp_ip_count = 0;
5047 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
5048 arp_ip_count++) {
5049 /* not complete check, but should be good enough to
5050 catch mistakes */
5051 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005052 pr_warning("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
5053 arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 arp_interval = 0;
5055 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04005056 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 arp_target[arp_ip_count] = ip;
5058 }
5059 }
5060
5061 if (arp_interval && !arp_ip_count) {
5062 /* don't allow arping if no arp_ip_target given... */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005063 pr_warning("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
5064 arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 arp_interval = 0;
5066 }
5067
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005068 if (arp_validate) {
5069 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005070 pr_err("arp_validate only supported in active-backup mode\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005071 return -EINVAL;
5072 }
5073 if (!arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005074 pr_err("arp_validate requires arp_interval\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005075 return -EINVAL;
5076 }
5077
5078 arp_validate_value = bond_parse_parm(arp_validate,
5079 arp_validate_tbl);
5080 if (arp_validate_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005081 pr_err("Error: invalid arp_validate \"%s\"\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005082 arp_validate == NULL ? "NULL" : arp_validate);
5083 return -EINVAL;
5084 }
5085 } else
5086 arp_validate_value = 0;
5087
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 if (miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005089 pr_info("MII link monitoring set to %d ms\n", miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 } else if (arp_interval) {
5091 int i;
5092
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005093 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
5094 arp_interval,
5095 arp_validate_tbl[arp_validate_value].modename,
5096 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097
5098 for (i = 0; i < arp_ip_count; i++)
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00005099 pr_info(" %s", arp_ip_target[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00005101 pr_info("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102
Jay Vosburghb8a97872008-06-13 18:12:04 -07005103 } else if (max_bonds) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 /* miimon and arp_interval not set, we need one so things
5105 * work as expected, see bonding.txt for details
5106 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005107 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 -07005108 }
5109
5110 if (primary && !USES_PRIMARY(bond_mode)) {
5111 /* currently, using a primary only makes sense
5112 * in active backup, TLB or ALB modes
5113 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005114 pr_warning("Warning: %s primary device specified but has no effect in %s mode\n",
5115 primary, bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 primary = NULL;
5117 }
5118
Jiri Pirkoa5499522009-09-25 03:28:09 +00005119 if (primary && primary_reselect) {
5120 primary_reselect_value = bond_parse_parm(primary_reselect,
5121 pri_reselect_tbl);
5122 if (primary_reselect_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005123 pr_err("Error: Invalid primary_reselect \"%s\"\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00005124 primary_reselect ==
5125 NULL ? "NULL" : primary_reselect);
5126 return -EINVAL;
5127 }
5128 } else {
5129 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
5130 }
5131
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005132 if (fail_over_mac) {
5133 fail_over_mac_value = bond_parse_parm(fail_over_mac,
5134 fail_over_mac_tbl);
5135 if (fail_over_mac_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005136 pr_err("Error: invalid fail_over_mac \"%s\"\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005137 arp_validate == NULL ? "NULL" : arp_validate);
5138 return -EINVAL;
5139 }
5140
5141 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005142 pr_warning("Warning: fail_over_mac only affects active-backup mode.\n");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005143 } else {
5144 fail_over_mac_value = BOND_FOM_NONE;
5145 }
Jay Vosburghdd957c52007-10-09 19:57:24 -07005146
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 /* fill params struct with the proper values */
5148 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04005149 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 params->miimon = miimon;
Moni Shoua7893b242008-05-17 21:10:12 -07005151 params->num_grat_arp = num_grat_arp;
Brian Haley305d5522008-11-04 17:51:14 -08005152 params->num_unsol_na = num_unsol_na;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005154 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 params->updelay = updelay;
5156 params->downdelay = downdelay;
5157 params->use_carrier = use_carrier;
5158 params->lacp_fast = lacp_fast;
5159 params->primary[0] = 0;
Jiri Pirkoa5499522009-09-25 03:28:09 +00005160 params->primary_reselect = primary_reselect_value;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005161 params->fail_over_mac = fail_over_mac_value;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005162 params->tx_queues = tx_queues;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00005163 params->all_slaves_active = all_slaves_active;
Flavio Leitnerc2952c32010-10-05 14:23:59 +00005164 params->resend_igmp = resend_igmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
5166 if (primary) {
5167 strncpy(params->primary, primary, IFNAMSIZ);
5168 params->primary[IFNAMSIZ - 1] = 0;
5169 }
5170
5171 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
5172
5173 return 0;
5174}
5175
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005176static struct lock_class_key bonding_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -07005177static struct lock_class_key bonding_netdev_addr_lock_key;
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005178
David S. Millere8a04642008-07-17 00:34:19 -07005179static void bond_set_lockdep_class_one(struct net_device *dev,
5180 struct netdev_queue *txq,
5181 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -07005182{
5183 lockdep_set_class(&txq->_xmit_lock,
5184 &bonding_netdev_xmit_lock_key);
5185}
5186
5187static void bond_set_lockdep_class(struct net_device *dev)
5188{
David S. Millercf508b12008-07-22 14:16:42 -07005189 lockdep_set_class(&dev->addr_list_lock,
5190 &bonding_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -07005191 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -07005192}
5193
Stephen Hemminger181470f2009-06-12 19:02:52 +00005194/*
5195 * Called from registration process
5196 */
5197static int bond_init(struct net_device *bond_dev)
5198{
5199 struct bonding *bond = netdev_priv(bond_dev);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005200 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005201
5202 pr_debug("Begin bond_init for %s\n", bond_dev->name);
5203
5204 bond->wq = create_singlethread_workqueue(bond_dev->name);
5205 if (!bond->wq)
5206 return -ENOMEM;
5207
5208 bond_set_lockdep_class(bond_dev);
5209
5210 netif_carrier_off(bond_dev);
5211
5212 bond_create_proc_entry(bond);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005213 list_add_tail(&bond->bond_list, &bn->dev_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005214
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00005215 bond_prepare_sysfs_group(bond);
Jiri Pirko22bedad32010-04-01 21:22:57 +00005216
Taku Izumif073c7c2010-12-09 15:17:13 +00005217 bond_debug_register(bond);
5218
Jiri Pirko22bedad32010-04-01 21:22:57 +00005219 __hw_addr_init(&bond->mc_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005220 return 0;
5221}
5222
Eric W. Biederman88ead972009-10-29 14:18:25 +00005223static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
5224{
5225 if (tb[IFLA_ADDRESS]) {
5226 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
5227 return -EINVAL;
5228 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
5229 return -EADDRNOTAVAIL;
5230 }
5231 return 0;
5232}
5233
5234static struct rtnl_link_ops bond_link_ops __read_mostly = {
5235 .kind = "bond",
Eric W. Biederman66391042009-10-29 23:58:54 +00005236 .priv_size = sizeof(struct bonding),
Eric W. Biederman88ead972009-10-29 14:18:25 +00005237 .setup = bond_setup,
5238 .validate = bond_validate,
5239};
5240
Mitch Williamsdfe60392005-11-09 10:36:04 -08005241/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005242 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08005243 * Caller must NOT hold rtnl_lock; we need to release it here before we
5244 * set up our sysfs entries.
5245 */
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005246int bond_create(struct net *net, const char *name)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005247{
5248 struct net_device *bond_dev;
5249 int res;
5250
5251 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08005252
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005253 bond_dev = alloc_netdev_mq(sizeof(struct bonding), name ? name : "",
5254 bond_setup, tx_queues);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005255 if (!bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005256 pr_err("%s: eek! can't alloc netdev!\n", name);
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005257 rtnl_unlock();
5258 return -ENOMEM;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005259 }
5260
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005261 dev_net_set(bond_dev, net);
Eric W. Biederman88ead972009-10-29 14:18:25 +00005262 bond_dev->rtnl_link_ops = &bond_link_ops;
5263
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005264 if (!name) {
5265 res = dev_alloc_name(bond_dev, "bond%d");
5266 if (res < 0)
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005267 goto out;
Neil Horman27e6f062010-10-05 03:39:21 +00005268 } else {
5269 /*
5270 * If we're given a name to register
5271 * we need to ensure that its not already
5272 * registered
5273 */
5274 res = -EEXIST;
5275 if (__dev_get_by_name(net, name) != NULL)
5276 goto out;
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005277 }
5278
Mitch Williamsdfe60392005-11-09 10:36:04 -08005279 res = register_netdevice(bond_dev);
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005280
Eric W. Biederman30c15ba2009-10-29 14:18:23 +00005281out:
Mitch Williamsdfe60392005-11-09 10:36:04 -08005282 rtnl_unlock();
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005283 if (res < 0)
5284 bond_destructor(bond_dev);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005285 return res;
5286}
5287
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005288static int __net_init bond_net_init(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005289{
Eric W. Biederman15449742009-11-29 15:46:04 +00005290 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005291
5292 bn->net = net;
5293 INIT_LIST_HEAD(&bn->dev_list);
5294
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005295 bond_create_proc_dir(bn);
Eric W. Biederman15449742009-11-29 15:46:04 +00005296
5297 return 0;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005298}
5299
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005300static void __net_exit bond_net_exit(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005301{
Eric W. Biederman15449742009-11-29 15:46:04 +00005302 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005303
5304 bond_destroy_proc_dir(bn);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005305}
5306
5307static struct pernet_operations bond_net_ops = {
5308 .init = bond_net_init,
5309 .exit = bond_net_exit,
Eric W. Biederman15449742009-11-29 15:46:04 +00005310 .id = &bond_net_id,
5311 .size = sizeof(struct bond_net),
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005312};
5313
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314static int __init bonding_init(void)
5315{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 int i;
5317 int res;
5318
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005319 pr_info("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320
Mitch Williamsdfe60392005-11-09 10:36:04 -08005321 res = bond_check_params(&bonding_defaults);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005322 if (res)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005323 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324
Eric W. Biederman15449742009-11-29 15:46:04 +00005325 res = register_pernet_subsys(&bond_net_ops);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005326 if (res)
5327 goto out;
Jay Vosburgh027ea042008-01-17 16:25:02 -08005328
Eric W. Biederman88ead972009-10-29 14:18:25 +00005329 res = rtnl_link_register(&bond_link_ops);
5330 if (res)
Eric W. Biederman66391042009-10-29 23:58:54 +00005331 goto err_link;
Eric W. Biederman88ead972009-10-29 14:18:25 +00005332
Taku Izumif073c7c2010-12-09 15:17:13 +00005333 bond_create_debugfs();
5334
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 for (i = 0; i < max_bonds; i++) {
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005336 res = bond_create(&init_net, NULL);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005337 if (res)
5338 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 }
5340
Mitch Williamsb76cdba2005-11-09 10:36:41 -08005341 res = bond_create_sysfs();
5342 if (res)
5343 goto err;
5344
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005346 register_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005347 bond_register_ipv6_notifier();
Mitch Williamsdfe60392005-11-09 10:36:04 -08005348out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 return res;
Eric W. Biederman88ead972009-10-29 14:18:25 +00005350err:
5351 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman66391042009-10-29 23:58:54 +00005352err_link:
Eric W. Biederman15449742009-11-29 15:46:04 +00005353 unregister_pernet_subsys(&bond_net_ops);
Eric W. Biederman88ead972009-10-29 14:18:25 +00005354 goto out;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005355
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356}
5357
5358static void __exit bonding_exit(void)
5359{
5360 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005361 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005362 bond_unregister_ipv6_notifier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005364 bond_destroy_sysfs();
Taku Izumif073c7c2010-12-09 15:17:13 +00005365 bond_destroy_debugfs();
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005366
Eric W. Biederman88ead972009-10-29 14:18:25 +00005367 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman15449742009-11-29 15:46:04 +00005368 unregister_pernet_subsys(&bond_net_ops);
Neil Hormane843fa52010-10-13 16:01:50 +00005369
5370#ifdef CONFIG_NET_POLL_CONTROLLER
Neil Hormanfb4fa762010-12-06 09:05:50 +00005371 /*
5372 * Make sure we don't have an imbalance on our netpoll blocking
5373 */
5374 WARN_ON(atomic_read(&netpoll_block_tx));
Neil Hormane843fa52010-10-13 16:01:50 +00005375#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376}
5377
5378module_init(bonding_init);
5379module_exit(bonding_exit);
5380MODULE_LICENSE("GPL");
5381MODULE_VERSION(DRV_VERSION);
5382MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5383MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
Eric W. Biederman88ead972009-10-29 14:18:25 +00005384MODULE_ALIAS_RTNL_LINK("bond");