blob: 7b7ca971672ff259773a6270fddfbc810dcffff9 [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}
WANG Congf6dc31a2010-05-06 00:48:51 -07001383#endif
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385/*---------------------------------- IOCTL ----------------------------------*/
1386
Adrian Bunk4ad072c2007-07-09 11:51:12 -07001387static int bond_sethwaddr(struct net_device *bond_dev,
1388 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001390 pr_debug("bond_dev=%p\n", bond_dev);
1391 pr_debug("slave_dev=%p\n", slave_dev);
1392 pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1394 return 0;
1395}
1396
Herbert Xu7f353bf2007-08-10 15:47:58 -07001397#define BOND_VLAN_FEATURES \
1398 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1399 NETIF_F_HW_VLAN_FILTER)
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001400
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001401/*
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001402 * Compute the common dev->feature set available to all slaves. Some
Herbert Xu7f353bf2007-08-10 15:47:58 -07001403 * feature bits are managed elsewhere, so preserve those feature bits
1404 * on the master device.
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001405 */
1406static int bond_compute_features(struct bonding *bond)
1407{
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001408 struct slave *slave;
1409 struct net_device *bond_dev = bond->dev;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08001410 u32 features = bond_dev->features;
1411 u32 vlan_features = 0;
Moni Shoua3158bf72007-10-09 19:43:41 -07001412 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1413 bond_dev->hard_header_len);
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001414 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001415
Herbert Xu7f353bf2007-08-10 15:47:58 -07001416 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
Herbert Xub63365a2008-10-23 01:11:29 -07001417 features |= NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1418
1419 if (!bond->first_slave)
1420 goto done;
1421
1422 features &= ~NETIF_F_ONE_FOR_ALL;
Herbert Xu7f353bf2007-08-10 15:47:58 -07001423
Jay Vosburgh278339a2009-08-28 12:05:12 +00001424 vlan_features = bond->first_slave->dev->vlan_features;
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001425 bond_for_each_slave(bond, slave, i) {
Herbert Xub63365a2008-10-23 01:11:29 -07001426 features = netdev_increment_features(features,
1427 slave->dev->features,
1428 NETIF_F_ONE_FOR_ALL);
Jay Vosburgh278339a2009-08-28 12:05:12 +00001429 vlan_features = netdev_increment_features(vlan_features,
1430 slave->dev->vlan_features,
1431 NETIF_F_ONE_FOR_ALL);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001432 if (slave->dev->hard_header_len > max_hard_header_len)
1433 max_hard_header_len = slave->dev->hard_header_len;
1434 }
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001435
Herbert Xub63365a2008-10-23 01:11:29 -07001436done:
Herbert Xu7f353bf2007-08-10 15:47:58 -07001437 features |= (bond_dev->features & BOND_VLAN_FEATURES);
Michał Mirosławacd11302011-01-24 15:45:15 -08001438 bond_dev->features = netdev_fix_features(bond_dev, features);
1439 bond_dev->vlan_features = netdev_fix_features(bond_dev, vlan_features);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001440 bond_dev->hard_header_len = max_hard_header_len;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001441
1442 return 0;
1443}
1444
Moni Shoua872254d2007-10-09 19:43:38 -07001445static void bond_setup_by_slave(struct net_device *bond_dev,
1446 struct net_device *slave_dev)
1447{
Wang Chen454d7c92008-11-12 23:37:49 -08001448 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07001449
Stephen Hemminger00829822008-11-20 20:14:53 -08001450 bond_dev->header_ops = slave_dev->header_ops;
Moni Shoua872254d2007-10-09 19:43:38 -07001451
1452 bond_dev->type = slave_dev->type;
1453 bond_dev->hard_header_len = slave_dev->hard_header_len;
1454 bond_dev->addr_len = slave_dev->addr_len;
1455
1456 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1457 slave_dev->addr_len);
Moni Shouad90a1622007-10-09 19:43:43 -07001458 bond->setup_by_slave = 1;
Moni Shoua872254d2007-10-09 19:43:38 -07001459}
1460
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001461/* On bonding slaves other than the currently active slave, suppress
1462 * duplicates except for 802.3ad ETH_P_SLOW, alb non-mcast/bcast, and
1463 * ARP on active-backup slaves with arp_validate enabled.
1464 */
1465static bool bond_should_deliver_exact_match(struct sk_buff *skb,
1466 struct net_device *slave_dev,
1467 struct net_device *bond_dev)
1468{
1469 if (slave_dev->priv_flags & IFF_SLAVE_INACTIVE) {
1470 if (slave_dev->priv_flags & IFF_SLAVE_NEEDARP &&
1471 skb->protocol == __cpu_to_be16(ETH_P_ARP))
1472 return false;
1473
1474 if (bond_dev->priv_flags & IFF_MASTER_ALB &&
1475 skb->pkt_type != PACKET_BROADCAST &&
1476 skb->pkt_type != PACKET_MULTICAST)
1477 return false;
1478
1479 if (bond_dev->priv_flags & IFF_MASTER_8023AD &&
1480 skb->protocol == __cpu_to_be16(ETH_P_SLOW))
1481 return false;
1482
1483 return true;
1484 }
1485 return false;
1486}
1487
1488static struct sk_buff *bond_handle_frame(struct sk_buff *skb)
1489{
1490 struct net_device *slave_dev;
1491 struct net_device *bond_dev;
1492
1493 skb = skb_share_check(skb, GFP_ATOMIC);
1494 if (unlikely(!skb))
1495 return NULL;
1496 slave_dev = skb->dev;
1497 bond_dev = ACCESS_ONCE(slave_dev->master);
1498 if (unlikely(!bond_dev))
1499 return skb;
1500
1501 if (bond_dev->priv_flags & IFF_MASTER_ARPMON)
1502 slave_dev->last_rx = jiffies;
1503
1504 if (bond_should_deliver_exact_match(skb, slave_dev, bond_dev)) {
1505 skb->deliver_no_wcard = 1;
1506 return skb;
1507 }
1508
1509 skb->dev = bond_dev;
1510
1511 if (bond_dev->priv_flags & IFF_MASTER_ALB &&
1512 bond_dev->priv_flags & IFF_BRIDGE_PORT &&
1513 skb->pkt_type == PACKET_HOST) {
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001514
Changli Gao541ac7c2011-03-02 21:07:14 +00001515 if (unlikely(skb_cow_head(skb,
1516 skb->data - skb_mac_header(skb)))) {
1517 kfree_skb(skb);
1518 return NULL;
1519 }
1520 memcpy(eth_hdr(skb)->h_dest, bond_dev->dev_addr, ETH_ALEN);
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001521 }
1522
1523 return skb;
1524}
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526/* enslave device <slave> to bond device <master> */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001527int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
Wang Chen454d7c92008-11-12 23:37:49 -08001529 struct bonding *bond = netdev_priv(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001530 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 struct slave *new_slave = NULL;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001532 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 struct sockaddr addr;
1534 int link_reporting;
1535 int old_features = bond_dev->features;
1536 int res = 0;
1537
nsxfreddy@gmail.com552709d2005-09-21 14:18:04 -05001538 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001539 slave_ops->ndo_do_ioctl == NULL) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001540 pr_warning("%s: Warning: no link monitoring support for %s\n",
1541 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543
1544 /* bond must be initialized by bond_open() before enslaving */
1545 if (!(bond_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001546 pr_warning("%s: master_dev is not up in bond_enslave\n",
1547 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549
1550 /* already enslaved */
1551 if (slave_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001552 pr_debug("Error, Device was already enslaved\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return -EBUSY;
1554 }
1555
1556 /* vlan challenged mutual exclusion */
1557 /* no need to lock since we're protected by rtnl_lock */
1558 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001559 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Jay Vosburghf35188f2010-07-21 12:14:47 +00001560 if (bond->vlgrp) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001561 pr_err("%s: Error: cannot enslave VLAN challenged slave %s on VLAN enabled bond %s\n",
1562 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return -EPERM;
1564 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001565 pr_warning("%s: Warning: enslaved VLAN challenged slave %s. Adding VLANs will be blocked as long as %s is part of bond %s\n",
1566 bond_dev->name, slave_dev->name,
1567 slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1569 }
1570 } else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001571 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (bond->slave_cnt == 0) {
1573 /* First slave, and it is not VLAN challenged,
1574 * so remove the block of adding VLANs over the bond.
1575 */
1576 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1577 }
1578 }
1579
Jay Vosburgh217df672005-09-26 16:11:50 -07001580 /*
1581 * Old ifenslave binaries are no longer supported. These can
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001582 * be identified with moderate accuracy by the state of the slave:
Jay Vosburgh217df672005-09-26 16:11:50 -07001583 * the current ifenslave will set the interface down prior to
1584 * enslaving it; the old ifenslave will not.
1585 */
1586 if ((slave_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001587 pr_err("%s is up. This may be due to an out of date ifenslave.\n",
Jay Vosburgh217df672005-09-26 16:11:50 -07001588 slave_dev->name);
1589 res = -EPERM;
1590 goto err_undo_flags;
1591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Moni Shoua872254d2007-10-09 19:43:38 -07001593 /* set bonding device ether type by slave - bonding netdevices are
1594 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1595 * there is a need to override some of the type dependent attribs/funcs.
1596 *
1597 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1598 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1599 */
1600 if (bond->slave_cnt == 0) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001601 if (bond_dev->type != slave_dev->type) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001602 pr_debug("%s: change device type from %d to %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001603 bond_dev->name,
1604 bond_dev->type, slave_dev->type);
Moni Shoua75c78502009-09-15 02:37:40 -07001605
Jiri Pirko3ca5b402010-03-10 10:29:35 +00001606 res = netdev_bonding_change(bond_dev,
1607 NETDEV_PRE_TYPE_CHANGE);
1608 res = notifier_to_errno(res);
1609 if (res) {
1610 pr_err("%s: refused to change device type\n",
1611 bond_dev->name);
1612 res = -EBUSY;
1613 goto err_undo_flags;
1614 }
Moni Shoua75c78502009-09-15 02:37:40 -07001615
Jiri Pirko32a806c2010-03-19 04:00:23 +00001616 /* Flush unicast and multicast addresses */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001617 dev_uc_flush(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001618 dev_mc_flush(bond_dev);
Jiri Pirko32a806c2010-03-19 04:00:23 +00001619
Moni Shouae36b9d12009-07-15 04:56:31 +00001620 if (slave_dev->type != ARPHRD_ETHER)
1621 bond_setup_by_slave(bond_dev, slave_dev);
1622 else
1623 ether_setup(bond_dev);
Moni Shoua75c78502009-09-15 02:37:40 -07001624
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001625 netdev_bonding_change(bond_dev,
1626 NETDEV_POST_TYPE_CHANGE);
Moni Shouae36b9d12009-07-15 04:56:31 +00001627 }
Moni Shoua872254d2007-10-09 19:43:38 -07001628 } else if (bond_dev->type != slave_dev->type) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001629 pr_err("%s ether type (%d) is different from other slaves (%d), can not enslave it.\n",
1630 slave_dev->name,
1631 slave_dev->type, bond_dev->type);
1632 res = -EINVAL;
1633 goto err_undo_flags;
Moni Shoua872254d2007-10-09 19:43:38 -07001634 }
1635
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001636 if (slave_ops->ndo_set_mac_address == NULL) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001637 if (bond->slave_cnt == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001638 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.",
1639 bond_dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001640 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1641 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001642 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",
1643 bond_dev->name);
Moni Shoua2ab82852007-10-09 19:43:39 -07001644 res = -EOPNOTSUPP;
1645 goto err_undo_flags;
1646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
1648
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001649 /* If this is the first slave, then we need to set the master's hardware
1650 * address to be the same as the slave's. */
David Strandd13a2cb2010-12-01 11:43:08 -08001651 if (is_zero_ether_addr(bond->dev->dev_addr))
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001652 memcpy(bond->dev->dev_addr, slave_dev->dev_addr,
1653 slave_dev->addr_len);
1654
1655
Joe Jin243cb4e2007-02-06 14:16:40 -08001656 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (!new_slave) {
1658 res = -ENOMEM;
1659 goto err_undo_flags;
1660 }
1661
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001662 /*
1663 * Set the new_slave's queue_id to be zero. Queue ID mapping
1664 * is set via sysfs or module option if desired.
1665 */
1666 new_slave->queue_id = 0;
1667
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001668 /* Save slave's original mtu and then set it to match the bond */
1669 new_slave->original_mtu = slave_dev->mtu;
1670 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1671 if (res) {
1672 pr_debug("Error %d calling dev_set_mtu\n", res);
1673 goto err_free;
1674 }
1675
Jay Vosburgh217df672005-09-26 16:11:50 -07001676 /*
1677 * Save slave's original ("permanent") mac address for modes
1678 * that need it, and for restoring it upon release, and then
1679 * set it to the master's address
1680 */
1681 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Jay Vosburghdd957c52007-10-09 19:57:24 -07001683 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001684 /*
1685 * Set slave to master's mac address. The application already
1686 * set the master's mac address to that of the first slave
1687 */
1688 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1689 addr.sa_family = slave_dev->type;
1690 res = dev_set_mac_address(slave_dev, &addr);
1691 if (res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001692 pr_debug("Error %d calling set_mac_address\n", res);
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001693 goto err_restore_mtu;
Moni Shoua2ab82852007-10-09 19:43:39 -07001694 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Jiri Pirko1765a572011-02-12 06:48:36 +00001697 res = netdev_set_bond_master(slave_dev, bond_dev);
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001698 if (res) {
Jiri Pirko1765a572011-02-12 06:48:36 +00001699 pr_debug("Error %d calling netdev_set_bond_master\n", res);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001700 goto err_restore_mac;
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001701 }
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001702 res = netdev_rx_handler_register(slave_dev, bond_handle_frame, NULL);
1703 if (res) {
1704 pr_debug("Error %d calling netdev_rx_handler_register\n", res);
1705 goto err_unset_master;
1706 }
1707
Jay Vosburgh217df672005-09-26 16:11:50 -07001708 /* open the slave since the application closed it */
1709 res = dev_open(slave_dev);
1710 if (res) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001711 pr_debug("Opening slave %s failed\n", slave_dev->name);
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001712 goto err_unreg_rxhandler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 new_slave->dev = slave_dev;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001716 slave_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Holger Eitzenberger58402052008-12-09 23:07:13 -08001718 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 /* bond_alb_init_slave() must be called before all other stages since
1720 * it might fail and we do not want to have to undo everything
1721 */
1722 res = bond_alb_init_slave(bond, new_slave);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001723 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001724 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726
1727 /* If the mode USES_PRIMARY, then the new slave gets the
1728 * master's promisc (and mc) settings only if it becomes the
1729 * curr_active_slave, and that is taken care of later when calling
1730 * bond_change_active()
1731 */
1732 if (!USES_PRIMARY(bond->params.mode)) {
1733 /* set promiscuity level to new slave */
1734 if (bond_dev->flags & IFF_PROMISC) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001735 res = dev_set_promiscuity(slave_dev, 1);
1736 if (res)
1737 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
1739
1740 /* set allmulti level to new slave */
1741 if (bond_dev->flags & IFF_ALLMULTI) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001742 res = dev_set_allmulti(slave_dev, 1);
1743 if (res)
1744 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746
David S. Millerb9e40852008-07-15 00:15:08 -07001747 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 /* upload master's mc_list to new slave */
Jiri Pirko22bedad32010-04-01 21:22:57 +00001749 netdev_for_each_mc_addr(ha, bond_dev)
1750 dev_mc_add(slave_dev, ha->addr);
David S. Millerb9e40852008-07-15 00:15:08 -07001751 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
1753
1754 if (bond->params.mode == BOND_MODE_8023AD) {
1755 /* add lacpdu mc addr to mc list */
1756 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1757
Jiri Pirko22bedad32010-04-01 21:22:57 +00001758 dev_mc_add(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
1760
1761 bond_add_vlans_on_slave(bond, slave_dev);
1762
1763 write_lock_bh(&bond->lock);
1764
1765 bond_attach_slave(bond, new_slave);
1766
1767 new_slave->delay = 0;
1768 new_slave->link_failure_count = 0;
1769
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001770 bond_compute_features(bond);
1771
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001772 write_unlock_bh(&bond->lock);
1773
1774 read_lock(&bond->lock);
1775
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001776 new_slave->last_arp_rx = jiffies;
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (bond->params.miimon && !bond->params.use_carrier) {
1779 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1780
1781 if ((link_reporting == -1) && !bond->params.arp_interval) {
1782 /*
1783 * miimon is set but a bonded network driver
1784 * does not support ETHTOOL/MII and
1785 * arp_interval is not set. Note: if
1786 * use_carrier is enabled, we will never go
1787 * here (because netif_carrier is always
1788 * supported); thus, we don't need to change
1789 * the messages for netif_carrier.
1790 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001791 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 -08001792 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 } else if (link_reporting == -1) {
1794 /* unable get link status using mii/ethtool */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001795 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",
1796 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
1798 }
1799
1800 /* check for initial state */
1801 if (!bond->params.miimon ||
1802 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1803 if (bond->params.updelay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001804 pr_debug("Initial state of slave_dev is BOND_LINK_BACK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 new_slave->link = BOND_LINK_BACK;
1806 new_slave->delay = bond->params.updelay;
1807 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001808 pr_debug("Initial state of slave_dev is BOND_LINK_UP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 new_slave->link = BOND_LINK_UP;
1810 }
1811 new_slave->jiffies = jiffies;
1812 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001813 pr_debug("Initial state of slave_dev is BOND_LINK_DOWN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 new_slave->link = BOND_LINK_DOWN;
1815 }
1816
1817 if (bond_update_speed_duplex(new_slave) &&
1818 (new_slave->link != BOND_LINK_DOWN)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001819 pr_warning("%s: Warning: failed to get speed and duplex from %s, assumed to be 100Mb/sec and Full.\n",
1820 bond_dev->name, new_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 if (bond->params.mode == BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001823 pr_warning("%s: Warning: Operation of 802.3ad mode requires ETHTOOL support in base driver for proper aggregator selection.\n",
1824 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
1826 }
1827
1828 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1829 /* if there is a primary slave, remember it */
Jiri Pirkoa5499522009-09-25 03:28:09 +00001830 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 bond->primary_slave = new_slave;
Jiri Pirkoa5499522009-09-25 03:28:09 +00001832 bond->force_primary = true;
1833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
1835
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001836 write_lock_bh(&bond->curr_slave_lock);
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 switch (bond->params.mode) {
1839 case BOND_MODE_ACTIVEBACKUP:
Jay Vosburgh8a8e4472006-09-22 21:56:15 -07001840 bond_set_slave_inactive_flags(new_slave);
1841 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 break;
1843 case BOND_MODE_8023AD:
1844 /* in 802.3ad mode, the internal mechanism
1845 * will activate the slaves in the selected
1846 * aggregator
1847 */
1848 bond_set_slave_inactive_flags(new_slave);
1849 /* if this is the first slave */
1850 if (bond->slave_cnt == 1) {
1851 SLAVE_AD_INFO(new_slave).id = 1;
1852 /* Initialize AD with the number of times that the AD timer is called in 1 second
1853 * can be called only after the mac address of the bond is set
1854 */
1855 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1856 bond->params.lacp_fast);
1857 } else {
1858 SLAVE_AD_INFO(new_slave).id =
1859 SLAVE_AD_INFO(new_slave->prev).id + 1;
1860 }
1861
1862 bond_3ad_bind_slave(new_slave);
1863 break;
1864 case BOND_MODE_TLB:
1865 case BOND_MODE_ALB:
1866 new_slave->state = BOND_STATE_ACTIVE;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001867 bond_set_slave_inactive_flags(new_slave);
Jiri Pirko5a29f782009-03-25 17:23:38 -07001868 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 break;
1870 default:
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001871 pr_debug("This slave is always active in trunk mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 /* always active in trunk mode */
1874 new_slave->state = BOND_STATE_ACTIVE;
1875
1876 /* In trunking mode there is little meaning to curr_active_slave
1877 * anyway (it holds no special properties of the bond device),
1878 * so we can change it without calling change_active_interface()
1879 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001880 if (!bond->curr_active_slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 bond->curr_active_slave = new_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 break;
1884 } /* switch(bond_mode) */
1885
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001886 write_unlock_bh(&bond->curr_slave_lock);
1887
Jay Vosburghff59c452006-03-27 13:27:43 -08001888 bond_set_carrier(bond);
1889
WANG Congf6dc31a2010-05-06 00:48:51 -07001890#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001891 slave_dev->npinfo = bond_netpoll_info(bond);
1892 if (slave_dev->npinfo) {
1893 if (slave_enable_netpoll(new_slave)) {
1894 read_unlock(&bond->lock);
1895 pr_info("Error, %s: master_dev is using netpoll, "
1896 "but new slave device does not support netpoll.\n",
1897 bond_dev->name);
1898 res = -EBUSY;
1899 goto err_close;
1900 }
WANG Congf6dc31a2010-05-06 00:48:51 -07001901 }
1902#endif
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001903
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001904 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001906 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1907 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001908 goto err_close;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001909
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001910 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
1911 bond_dev->name, slave_dev->name,
1912 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1913 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 /* enslave is successful */
1916 return 0;
1917
1918/* Undo stages on error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919err_close:
1920 dev_close(slave_dev);
1921
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001922err_unreg_rxhandler:
1923 netdev_rx_handler_unregister(slave_dev);
1924
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001925err_unset_master:
Jiri Pirko1765a572011-02-12 06:48:36 +00001926 netdev_set_bond_master(slave_dev, NULL);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928err_restore_mac:
Jay Vosburghdd957c52007-10-09 19:57:24 -07001929 if (!bond->params.fail_over_mac) {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001930 /* XXX TODO - fom follow mode needs to change master's
1931 * MAC if this slave's MAC is in use by the bond, or at
1932 * least print a warning.
1933 */
Moni Shoua2ab82852007-10-09 19:43:39 -07001934 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1935 addr.sa_family = slave_dev->type;
1936 dev_set_mac_address(slave_dev, &addr);
1937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001939err_restore_mtu:
1940 dev_set_mtu(slave_dev, new_slave->original_mtu);
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942err_free:
1943 kfree(new_slave);
1944
1945err_undo_flags:
1946 bond_dev->features = old_features;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return res;
1949}
1950
1951/*
1952 * Try to release the slave device <slave> from the bond device <master>
1953 * It is legal to access curr_active_slave without a lock because all the function
1954 * is write-locked.
1955 *
1956 * The rules for slave state should be:
1957 * for Active/Backup:
1958 * Active stays on all backups go down
1959 * for Bonded connections:
1960 * The first up interface should be left on and all others downed.
1961 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001962int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
Wang Chen454d7c92008-11-12 23:37:49 -08001964 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 struct slave *slave, *oldcurrent;
1966 struct sockaddr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 /* slave is not a slave or master is not master of this slave */
1969 if (!(slave_dev->flags & IFF_SLAVE) ||
1970 (slave_dev->master != bond_dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001971 pr_err("%s: Error: cannot release %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 bond_dev->name, slave_dev->name);
1973 return -EINVAL;
1974 }
1975
Neil Hormane843fa52010-10-13 16:01:50 +00001976 block_netpoll_tx();
WANG Congf6dc31a2010-05-06 00:48:51 -07001977 netdev_bonding_change(bond_dev, NETDEV_BONDING_DESLAVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 write_lock_bh(&bond->lock);
1979
1980 slave = bond_get_slave_by_dev(bond, slave_dev);
1981 if (!slave) {
1982 /* not a slave of this bond */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001983 pr_info("%s: %s not enslaved\n",
1984 bond_dev->name, slave_dev->name);
Jay Vosburghf5e2a7b2006-02-07 21:17:22 -08001985 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001986 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 return -EINVAL;
1988 }
1989
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001990 if (!bond->params.fail_over_mac) {
Joe Perches8e95a202009-12-03 07:58:21 +00001991 if (!compare_ether_addr(bond_dev->dev_addr, slave->perm_hwaddr) &&
1992 bond->slave_cnt > 1)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001993 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",
1994 bond_dev->name, slave_dev->name,
1995 slave->perm_hwaddr,
1996 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
1998
1999 /* Inform AD package of unbinding of slave. */
2000 if (bond->params.mode == BOND_MODE_8023AD) {
2001 /* must be called before the slave is
2002 * detached from the list
2003 */
2004 bond_3ad_unbind_slave(slave);
2005 }
2006
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002007 pr_info("%s: releasing %s interface %s\n",
2008 bond_dev->name,
2009 (slave->state == BOND_STATE_ACTIVE) ? "active" : "backup",
2010 slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 oldcurrent = bond->curr_active_slave;
2013
2014 bond->current_arp_slave = NULL;
2015
2016 /* release the slave from its bond */
2017 bond_detach_slave(bond, slave);
2018
Arthur Kepner8531c5f2005-08-23 01:34:53 -04002019 bond_compute_features(bond);
2020
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002021 if (bond->primary_slave == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 bond->primary_slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002024 if (oldcurrent == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 bond_change_active_slave(bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Holger Eitzenberger58402052008-12-09 23:07:13 -08002027 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 /* Must be called only after the slave has been
2029 * detached from the list and the curr_active_slave
2030 * has been cleared (if our_slave == old_current),
2031 * but before a new active slave is selected.
2032 */
Jay Vosburgh25433312008-01-17 16:24:59 -08002033 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 bond_alb_deinit_slave(bond, slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08002035 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
2037
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002038 if (oldcurrent == slave) {
2039 /*
2040 * Note that we hold RTNL over this sequence, so there
2041 * is no concern that another slave add/remove event
2042 * will interfere.
2043 */
2044 write_unlock_bh(&bond->lock);
2045 read_lock(&bond->lock);
2046 write_lock_bh(&bond->curr_slave_lock);
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 bond_select_active_slave(bond);
2049
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002050 write_unlock_bh(&bond->curr_slave_lock);
2051 read_unlock(&bond->lock);
2052 write_lock_bh(&bond->lock);
2053 }
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (bond->slave_cnt == 0) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002056 bond_set_carrier(bond);
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 /* if the last slave was removed, zero the mac address
2059 * of the master so it will be set by the application
2060 * to the mac address of the first slave
2061 */
2062 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2063
Jay Vosburghf35188f2010-07-21 12:14:47 +00002064 if (!bond->vlgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2066 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002067 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2068 bond_dev->name, bond_dev->name);
2069 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2070 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
2072 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2073 !bond_has_challenged_slaves(bond)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002074 pr_info("%s: last VLAN challenged slave %s left bond %s. VLAN blocking is removed\n",
2075 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
2077 }
2078
2079 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002080 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002082 /* must do this from outside any spinlocks */
2083 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 bond_del_vlans_from_slave(bond, slave_dev);
2086
2087 /* If the mode USES_PRIMARY, then we should only remove its
2088 * promisc and mc settings if it was the curr_active_slave, but that was
2089 * already taken care of above when we detached the slave
2090 */
2091 if (!USES_PRIMARY(bond->params.mode)) {
2092 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002093 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002097 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002101 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002103 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
2105
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00002106 netdev_rx_handler_unregister(slave_dev);
Jiri Pirko1765a572011-02-12 06:48:36 +00002107 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002109 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07002110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 /* close slave before restoring its mac address */
2112 dev_close(slave_dev);
2113
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07002114 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002115 /* restore original ("permanent") mac address */
2116 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2117 addr.sa_family = slave_dev->type;
2118 dev_set_mac_address(slave_dev, &addr);
2119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00002121 dev_set_mtu(slave_dev, slave->original_mtu);
2122
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002123 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002124 IFF_SLAVE_INACTIVE | IFF_BONDING |
2125 IFF_SLAVE_NEEDARP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127 kfree(slave);
2128
2129 return 0; /* deletion OK */
2130}
2131
2132/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002133* First release a slave and than destroy the bond if no more slaves are left.
Moni Shouad90a1622007-10-09 19:43:43 -07002134* Must be under rtnl_lock when this function is called.
2135*/
stephen hemminger26d8ee72010-10-15 05:09:34 +00002136static int bond_release_and_destroy(struct net_device *bond_dev,
2137 struct net_device *slave_dev)
Moni Shouad90a1622007-10-09 19:43:43 -07002138{
Wang Chen454d7c92008-11-12 23:37:49 -08002139 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002140 int ret;
2141
2142 ret = bond_release(bond_dev, slave_dev);
2143 if ((ret == 0) && (bond->slave_cnt == 0)) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002144 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002145 pr_info("%s: destroying bond %s.\n",
2146 bond_dev->name, bond_dev->name);
Stephen Hemminger9e716262009-06-12 19:02:47 +00002147 unregister_netdevice(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002148 }
2149 return ret;
2150}
2151
2152/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 * This function releases all slaves.
2154 */
2155static int bond_release_all(struct net_device *bond_dev)
2156{
Wang Chen454d7c92008-11-12 23:37:49 -08002157 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 struct slave *slave;
2159 struct net_device *slave_dev;
2160 struct sockaddr addr;
2161
2162 write_lock_bh(&bond->lock);
2163
Jay Vosburghff59c452006-03-27 13:27:43 -08002164 netif_carrier_off(bond_dev);
2165
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002166 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169 bond->current_arp_slave = NULL;
2170 bond->primary_slave = NULL;
2171 bond_change_active_slave(bond, NULL);
2172
2173 while ((slave = bond->first_slave) != NULL) {
2174 /* Inform AD package of unbinding of slave
2175 * before slave is detached from the list.
2176 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002177 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 bond_3ad_unbind_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
2180 slave_dev = slave->dev;
2181 bond_detach_slave(bond, slave);
2182
Jay Vosburgh25433312008-01-17 16:24:59 -08002183 /* now that the slave is detached, unlock and perform
2184 * all the undo steps that should not be called from
2185 * within a lock.
2186 */
2187 write_unlock_bh(&bond->lock);
2188
Holger Eitzenberger58402052008-12-09 23:07:13 -08002189 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 /* must be called only after the slave
2191 * has been detached from the list
2192 */
2193 bond_alb_deinit_slave(bond, slave);
2194 }
2195
Arthur Kepner8531c5f2005-08-23 01:34:53 -04002196 bond_compute_features(bond);
2197
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002198 bond_destroy_slave_symlinks(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 bond_del_vlans_from_slave(bond, slave_dev);
2200
2201 /* If the mode USES_PRIMARY, then we should only remove its
2202 * promisc and mc settings if it was the curr_active_slave, but that was
2203 * already taken care of above when we detached the slave
2204 */
2205 if (!USES_PRIMARY(bond->params.mode)) {
2206 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002207 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002211 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002215 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002217 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 }
2219
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00002220 netdev_rx_handler_unregister(slave_dev);
Jiri Pirko1765a572011-02-12 06:48:36 +00002221 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002223 slave_disable_netpoll(slave);
2224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 /* close slave before restoring its mac address */
2226 dev_close(slave_dev);
2227
Jay Vosburghdd957c52007-10-09 19:57:24 -07002228 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002229 /* restore original ("permanent") mac address*/
2230 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2231 addr.sa_family = slave_dev->type;
2232 dev_set_mac_address(slave_dev, &addr);
2233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002235 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
2236 IFF_SLAVE_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 kfree(slave);
2239
2240 /* re-acquire the lock before getting the next slave */
2241 write_lock_bh(&bond->lock);
2242 }
2243
2244 /* zero the mac address of the master so it will be
2245 * set by the application to the mac address of the
2246 * first slave
2247 */
2248 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2249
Jay Vosburghf35188f2010-07-21 12:14:47 +00002250 if (!bond->vlgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
Jay Vosburghf35188f2010-07-21 12:14:47 +00002252 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002253 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2254 bond_dev->name, bond_dev->name);
2255 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2256 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
2258
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002259 pr_info("%s: released all slaves\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261out:
2262 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 return 0;
2264}
2265
2266/*
2267 * This function changes the active slave to slave <slave_dev>.
2268 * It returns -EINVAL in the following cases.
2269 * - <slave_dev> is not found in the list.
2270 * - There is not active slave now.
2271 * - <slave_dev> is already active.
2272 * - The link state of <slave_dev> is not BOND_LINK_UP.
2273 * - <slave_dev> is not running.
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002274 * In these cases, this function does nothing.
2275 * In the other cases, current_slave pointer is changed and 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 */
2277static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2278{
Wang Chen454d7c92008-11-12 23:37:49 -08002279 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 struct slave *old_active = NULL;
2281 struct slave *new_active = NULL;
2282 int res = 0;
2283
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002284 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287 /* Verify that master_dev is indeed the master of slave_dev */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002288 if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002291 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002293 read_lock(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 old_active = bond->curr_active_slave;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002295 read_unlock(&bond->curr_slave_lock);
2296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 new_active = bond_get_slave_by_dev(bond, slave_dev);
2298
2299 /*
2300 * Changing to the current active: do nothing; return success.
2301 */
2302 if (new_active && (new_active == old_active)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002303 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 return 0;
2305 }
2306
2307 if ((new_active) &&
2308 (old_active) &&
2309 (new_active->link == BOND_LINK_UP) &&
2310 IS_UP(new_active->dev)) {
Neil Hormane843fa52010-10-13 16:01:50 +00002311 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002312 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 bond_change_active_slave(bond, new_active);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002314 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002315 unblock_netpoll_tx();
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002316 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002319 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 return res;
2322}
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2325{
Wang Chen454d7c92008-11-12 23:37:49 -08002326 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 info->bond_mode = bond->params.mode;
2329 info->miimon = bond->params.miimon;
2330
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002331 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 info->num_slaves = bond->slave_cnt;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002333 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 return 0;
2336}
2337
2338static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2339{
Wang Chen454d7c92008-11-12 23:37:49 -08002340 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 struct slave *slave;
Eric Dumazet689c96c2009-04-23 03:39:04 +00002342 int i, res = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002344 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
2346 bond_for_each_slave(bond, slave, i) {
2347 if (i == (int)info->slave_id) {
Eric Dumazet689c96c2009-04-23 03:39:04 +00002348 res = 0;
2349 strcpy(info->slave_name, slave->dev->name);
2350 info->link = slave->link;
2351 info->state = slave->state;
2352 info->link_failure_count = slave->link_failure_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 break;
2354 }
2355 }
2356
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002357 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Eric Dumazet689c96c2009-04-23 03:39:04 +00002359 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360}
2361
2362/*-------------------------------- Monitoring -------------------------------*/
2363
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002364
2365static int bond_miimon_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002367 struct slave *slave;
2368 int i, link_state, commit = 0;
Jiri Pirko41f89102009-04-24 03:57:29 +00002369 bool ignore_updelay;
2370
2371 ignore_updelay = !bond->curr_active_slave ? true : false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373 bond_for_each_slave(bond, slave, i) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002374 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002376 link_state = bond_check_dev_link(bond, slave->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
2378 switch (slave->link) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002379 case BOND_LINK_UP:
2380 if (link_state)
2381 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002383 slave->link = BOND_LINK_FAIL;
2384 slave->delay = bond->params.downdelay;
2385 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002386 pr_info("%s: link status down for %sinterface %s, disabling it in %d ms.\n",
2387 bond->dev->name,
2388 (bond->params.mode ==
2389 BOND_MODE_ACTIVEBACKUP) ?
2390 ((slave->state == BOND_STATE_ACTIVE) ?
2391 "active " : "backup ") : "",
2392 slave->dev->name,
2393 bond->params.downdelay * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002395 /*FALLTHRU*/
2396 case BOND_LINK_FAIL:
2397 if (link_state) {
2398 /*
2399 * recovered before downdelay expired
2400 */
2401 slave->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 slave->jiffies = jiffies;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002403 pr_info("%s: link status up again after %d ms for interface %s.\n",
2404 bond->dev->name,
2405 (bond->params.downdelay - slave->delay) *
2406 bond->params.miimon,
2407 slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002408 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002410
2411 if (slave->delay <= 0) {
2412 slave->new_link = BOND_LINK_DOWN;
2413 commit++;
2414 continue;
2415 }
2416
2417 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002420 case BOND_LINK_DOWN:
2421 if (!link_state)
2422 continue;
2423
2424 slave->link = BOND_LINK_BACK;
2425 slave->delay = bond->params.updelay;
2426
2427 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002428 pr_info("%s: link status up for interface %s, enabling it in %d ms.\n",
2429 bond->dev->name, slave->dev->name,
2430 ignore_updelay ? 0 :
2431 bond->params.updelay *
2432 bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002434 /*FALLTHRU*/
2435 case BOND_LINK_BACK:
2436 if (!link_state) {
2437 slave->link = BOND_LINK_DOWN;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002438 pr_info("%s: link status down again after %d ms for interface %s.\n",
2439 bond->dev->name,
2440 (bond->params.updelay - slave->delay) *
2441 bond->params.miimon,
2442 slave->dev->name);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002443
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002444 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002446
Jiri Pirko41f89102009-04-24 03:57:29 +00002447 if (ignore_updelay)
2448 slave->delay = 0;
2449
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002450 if (slave->delay <= 0) {
2451 slave->new_link = BOND_LINK_UP;
2452 commit++;
Jiri Pirko41f89102009-04-24 03:57:29 +00002453 ignore_updelay = false;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002454 continue;
2455 }
2456
2457 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 break;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002459 }
2460 }
2461
2462 return commit;
2463}
2464
2465static void bond_miimon_commit(struct bonding *bond)
2466{
2467 struct slave *slave;
2468 int i;
2469
2470 bond_for_each_slave(bond, slave, i) {
2471 switch (slave->new_link) {
2472 case BOND_LINK_NOCHANGE:
2473 continue;
2474
2475 case BOND_LINK_UP:
2476 slave->link = BOND_LINK_UP;
2477 slave->jiffies = jiffies;
2478
2479 if (bond->params.mode == BOND_MODE_8023AD) {
2480 /* prevent it from being the active one */
2481 slave->state = BOND_STATE_BACKUP;
2482 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2483 /* make it immediately active */
2484 slave->state = BOND_STATE_ACTIVE;
2485 } else if (slave != bond->primary_slave) {
2486 /* prevent it from being the active one */
2487 slave->state = BOND_STATE_BACKUP;
2488 }
2489
Krzysztof Piotr Oledzki546add72010-10-06 14:28:22 -07002490 bond_update_speed_duplex(slave);
2491
Krzysztof Piotr Oledzki700c2a72010-10-06 14:25:06 -07002492 pr_info("%s: link status definitely up for interface %s, %d Mbps %s duplex.\n",
2493 bond->dev->name, slave->dev->name,
2494 slave->speed, slave->duplex ? "full" : "half");
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002495
2496 /* notify ad that the link status has changed */
2497 if (bond->params.mode == BOND_MODE_8023AD)
2498 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2499
Holger Eitzenberger58402052008-12-09 23:07:13 -08002500 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002501 bond_alb_handle_link_change(bond, slave,
2502 BOND_LINK_UP);
2503
2504 if (!bond->curr_active_slave ||
2505 (slave == bond->primary_slave))
2506 goto do_failover;
2507
2508 continue;
2509
2510 case BOND_LINK_DOWN:
Jay Vosburghfba4acd2008-10-30 17:41:14 -07002511 if (slave->link_failure_count < UINT_MAX)
2512 slave->link_failure_count++;
2513
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002514 slave->link = BOND_LINK_DOWN;
2515
2516 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2517 bond->params.mode == BOND_MODE_8023AD)
2518 bond_set_slave_inactive_flags(slave);
2519
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002520 pr_info("%s: link status definitely down for interface %s, disabling it\n",
2521 bond->dev->name, slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002522
2523 if (bond->params.mode == BOND_MODE_8023AD)
2524 bond_3ad_handle_link_change(slave,
2525 BOND_LINK_DOWN);
2526
Jiri Pirkoae63e802009-05-27 05:42:36 +00002527 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002528 bond_alb_handle_link_change(bond, slave,
2529 BOND_LINK_DOWN);
2530
2531 if (slave == bond->curr_active_slave)
2532 goto do_failover;
2533
2534 continue;
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002537 pr_err("%s: invalid new link %d on slave %s\n",
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002538 bond->dev->name, slave->new_link,
2539 slave->dev->name);
2540 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002542 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
2544
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002545do_failover:
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002546 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00002547 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002548 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 bond_select_active_slave(bond);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002550 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002551 unblock_netpoll_tx();
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002552 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002553
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002554 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555}
2556
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002557/*
2558 * bond_mii_monitor
2559 *
2560 * Really a wrapper that splits the mii monitor into two phases: an
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002561 * inspection, then (if inspection indicates something needs to be done)
2562 * an acquisition of appropriate locks followed by a commit phase to
2563 * implement whatever link state changes are indicated.
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002564 */
2565void bond_mii_monitor(struct work_struct *work)
2566{
2567 struct bonding *bond = container_of(work, struct bonding,
2568 mii_work.work);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002569
2570 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002571 if (bond->kill_timers)
2572 goto out;
2573
2574 if (bond->slave_cnt == 0)
2575 goto re_arm;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002576
2577 if (bond->send_grat_arp) {
2578 read_lock(&bond->curr_slave_lock);
2579 bond_send_gratuitous_arp(bond);
2580 read_unlock(&bond->curr_slave_lock);
2581 }
2582
Brian Haley305d5522008-11-04 17:51:14 -08002583 if (bond->send_unsol_na) {
2584 read_lock(&bond->curr_slave_lock);
2585 bond_send_unsolicited_na(bond);
2586 read_unlock(&bond->curr_slave_lock);
2587 }
2588
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002589 if (bond_miimon_inspect(bond)) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002590 read_unlock(&bond->lock);
2591 rtnl_lock();
2592 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002593
2594 bond_miimon_commit(bond);
2595
Jay Vosburgh56556622008-01-17 16:25:03 -08002596 read_unlock(&bond->lock);
2597 rtnl_unlock(); /* might sleep, hold no other locks */
2598 read_lock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002599 }
2600
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002601re_arm:
2602 if (bond->params.miimon)
2603 queue_delayed_work(bond->wq, &bond->mii_work,
2604 msecs_to_jiffies(bond->params.miimon));
2605out:
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002606 read_unlock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002607}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002608
Al Virod3bb52b2007-08-22 20:06:58 -04002609static __be32 bond_glean_dev_ip(struct net_device *dev)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002610{
2611 struct in_device *idev;
2612 struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07002613 __be32 addr = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002614
2615 if (!dev)
2616 return 0;
2617
2618 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002619 idev = __in_dev_get_rcu(dev);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002620 if (!idev)
2621 goto out;
2622
2623 ifa = idev->ifa_list;
2624 if (!ifa)
2625 goto out;
2626
2627 addr = ifa->ifa_local;
2628out:
2629 rcu_read_unlock();
2630 return addr;
2631}
2632
Al Virod3bb52b2007-08-22 20:06:58 -04002633static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002634{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002635 struct vlan_entry *vlan;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002636
2637 if (ip == bond->master_ip)
2638 return 1;
2639
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002640 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002641 if (ip == vlan->vlan_ip)
2642 return 1;
2643 }
2644
2645 return 0;
2646}
2647
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002648/*
2649 * We go to the (large) trouble of VLAN tagging ARP frames because
2650 * switches in VLAN mode (especially if ports are configured as
2651 * "native" to a VLAN) might not pass non-tagged frames.
2652 */
Al Virod3bb52b2007-08-22 20:06:58 -04002653static 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 -04002654{
2655 struct sk_buff *skb;
2656
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002657 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002658 slave_dev->name, dest_ip, src_ip, vlan_id);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002659
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002660 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2661 NULL, slave_dev->dev_addr, NULL);
2662
2663 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002664 pr_err("ARP packet allocation failed\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002665 return;
2666 }
2667 if (vlan_id) {
2668 skb = vlan_put_tag(skb, vlan_id);
2669 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002670 pr_err("failed to insert VLAN tag\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002671 return;
2672 }
2673 }
2674 arp_xmit(skb);
2675}
2676
2677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2679{
David S. Millerb23dd4f2011-03-02 14:31:35 -08002680 int i, vlan_id;
Al Virod3bb52b2007-08-22 20:06:58 -04002681 __be32 *targets = bond->params.arp_targets;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002682 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002683 struct net_device *vlan_dev;
2684 struct flowi fl;
2685 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Mitch Williams6b780562005-11-09 10:36:19 -08002687 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2688 if (!targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07002689 break;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002690 pr_debug("basa: target %x\n", targets[i]);
Jay Vosburghf35188f2010-07-21 12:14:47 +00002691 if (!bond->vlgrp) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002692 pr_debug("basa: empty vlan: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002693 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2694 bond->master_ip, 0);
2695 continue;
2696 }
2697
2698 /*
2699 * If VLANs are configured, we do a route lookup to
2700 * determine which VLAN interface would be used, so we
2701 * can tag the ARP with the proper VLAN tag.
2702 */
2703 memset(&fl, 0, sizeof(fl));
2704 fl.fl4_dst = targets[i];
2705 fl.fl4_tos = RTO_ONLINK;
2706
David S. Millerb23dd4f2011-03-02 14:31:35 -08002707 rt = ip_route_output_key(dev_net(bond->dev), &fl);
2708 if (IS_ERR(rt)) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002709 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002710 pr_warning("%s: no route to arp_ip_target %pI4\n",
2711 bond->dev->name, &fl.fl4_dst);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002712 }
2713 continue;
2714 }
2715
2716 /*
2717 * This target is not on a VLAN
2718 */
Changli Gaod8d1f302010-06-10 23:31:35 -07002719 if (rt->dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002720 ip_rt_put(rt);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002721 pr_debug("basa: rtdev == bond->dev: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002722 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2723 bond->master_ip, 0);
2724 continue;
2725 }
2726
2727 vlan_id = 0;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002728 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002729 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Changli Gaod8d1f302010-06-10 23:31:35 -07002730 if (vlan_dev == rt->dst.dev) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002731 vlan_id = vlan->vlan_id;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002732 pr_debug("basa: vlan match on %s %d\n",
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002733 vlan_dev->name, vlan_id);
2734 break;
2735 }
2736 }
2737
2738 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002739 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002740 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2741 vlan->vlan_ip, vlan_id);
2742 continue;
2743 }
2744
2745 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002746 pr_warning("%s: no path to arp_ip_target %pI4 via rt.dev %s\n",
2747 bond->dev->name, &fl.fl4_dst,
Changli Gaod8d1f302010-06-10 23:31:35 -07002748 rt->dst.dev ? rt->dst.dev->name : "NULL");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002749 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002750 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002751 }
2752}
2753
2754/*
2755 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2756 * for each VLAN above us.
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002757 *
2758 * Caller must hold curr_slave_lock for read or better
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002759 */
2760static void bond_send_gratuitous_arp(struct bonding *bond)
2761{
2762 struct slave *slave = bond->curr_active_slave;
2763 struct vlan_entry *vlan;
2764 struct net_device *vlan_dev;
2765
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002766 pr_debug("bond_send_grat_arp: bond %s slave %s\n",
2767 bond->dev->name, slave ? slave->dev->name : "NULL");
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002768
2769 if (!slave || !bond->send_grat_arp ||
2770 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002771 return;
2772
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002773 bond->send_grat_arp--;
2774
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002775 if (bond->master_ip) {
2776 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
Moni Shoua1053f622007-10-09 19:43:42 -07002777 bond->master_ip, 0);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002778 }
2779
Jay Vosburghf35188f2010-07-21 12:14:47 +00002780 if (!bond->vlgrp)
2781 return;
2782
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002783 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002784 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002785 if (vlan->vlan_ip) {
2786 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2787 vlan->vlan_ip, vlan->vlan_id);
2788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 }
2790}
2791
Al Virod3bb52b2007-08-22 20:06:58 -04002792static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002793{
2794 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002795 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002796
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002797 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002798 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002799 &sip, &tip, i, &targets[i],
2800 bond_has_this_ip(bond, tip));
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002801 if (sip == targets[i]) {
2802 if (bond_has_this_ip(bond, tip))
2803 slave->last_arp_rx = jiffies;
2804 return;
2805 }
2806 }
2807}
2808
2809static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2810{
2811 struct arphdr *arp;
2812 struct slave *slave;
2813 struct bonding *bond;
2814 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002815 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002816
Andy Gospodarek1f3c8802009-12-14 10:48:58 +00002817 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2818 /*
2819 * When using VLANS and bonding, dev and oriv_dev may be
2820 * incorrect if the physical interface supports VLAN
2821 * acceleration. With this change ARP validation now
2822 * works for hosts only reachable on the VLAN interface.
2823 */
2824 dev = vlan_dev_real_dev(dev);
2825 orig_dev = dev_get_by_index_rcu(dev_net(skb->dev),skb->skb_iif);
2826 }
2827
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002828 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2829 goto out;
2830
Wang Chen454d7c92008-11-12 23:37:49 -08002831 bond = netdev_priv(dev);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002832 read_lock(&bond->lock);
2833
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002834 pr_debug("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002835 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2836 orig_dev ? orig_dev->name : "NULL");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002837
2838 slave = bond_get_slave_by_dev(bond, orig_dev);
2839 if (!slave || !slave_do_arp_validate(bond, slave))
2840 goto out_unlock;
2841
Neil Hormanb3053252011-01-20 09:02:31 +00002842 skb = skb_share_check(skb, GFP_ATOMIC);
2843 if (!skb)
2844 goto out_unlock;
2845
Pavel Emelyanov988b7052008-03-03 12:20:57 -08002846 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002847 goto out_unlock;
2848
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002849 arp = arp_hdr(skb);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002850 if (arp->ar_hln != dev->addr_len ||
2851 skb->pkt_type == PACKET_OTHERHOST ||
2852 skb->pkt_type == PACKET_LOOPBACK ||
2853 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2854 arp->ar_pro != htons(ETH_P_IP) ||
2855 arp->ar_pln != 4)
2856 goto out_unlock;
2857
2858 arp_ptr = (unsigned char *)(arp + 1);
2859 arp_ptr += dev->addr_len;
2860 memcpy(&sip, arp_ptr, 4);
2861 arp_ptr += 4 + dev->addr_len;
2862 memcpy(&tip, arp_ptr, 4);
2863
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002864 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002865 bond->dev->name, slave->dev->name, slave->state,
2866 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2867 &sip, &tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002868
2869 /*
2870 * Backup slaves won't see the ARP reply, but do come through
2871 * here for each ARP probe (so we swap the sip/tip to validate
2872 * the probe). In a "redundant switch, common router" type of
2873 * configuration, the ARP probe will (hopefully) travel from
2874 * the active, through one switch, the router, then the other
2875 * switch before reaching the backup.
2876 */
2877 if (slave->state == BOND_STATE_ACTIVE)
2878 bond_validate_arp(bond, slave, sip, tip);
2879 else
2880 bond_validate_arp(bond, slave, tip, sip);
2881
2882out_unlock:
2883 read_unlock(&bond->lock);
2884out:
2885 dev_kfree_skb(skb);
2886 return NET_RX_SUCCESS;
2887}
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889/*
2890 * this function is called regularly to monitor each slave's link
2891 * ensuring that traffic is being sent and received when arp monitoring
2892 * is used in load-balancing mode. if the adapter has been dormant, then an
2893 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2894 * arp monitoring in active backup mode.
2895 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002896void bond_loadbalance_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002898 struct bonding *bond = container_of(work, struct bonding,
2899 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 struct slave *slave, *oldcurrent;
2901 int do_failover = 0;
2902 int delta_in_ticks;
2903 int i;
2904
2905 read_lock(&bond->lock);
2906
Jay Vosburgh5ce0da82008-05-17 21:10:07 -07002907 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002909 if (bond->kill_timers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002912 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
2915 read_lock(&bond->curr_slave_lock);
2916 oldcurrent = bond->curr_active_slave;
2917 read_unlock(&bond->curr_slave_lock);
2918
2919 /* see if any of the previous devices are up now (i.e. they have
2920 * xmt and rcv traffic). the curr_active_slave does not come into
2921 * the picture unless it is null. also, slave->jiffies is not needed
2922 * here because we send an arp on each slave and give a slave as
2923 * long as it needs to get the tx/rx within the delta.
2924 * TODO: what about up/down delay in arp mode? it wasn't here before
2925 * so it can wait
2926 */
2927 bond_for_each_slave(bond, slave, i) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002928 unsigned long trans_start = dev_trans_start(slave->dev);
2929
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002931 if (time_in_range(jiffies,
2932 trans_start - delta_in_ticks,
2933 trans_start + delta_in_ticks) &&
2934 time_in_range(jiffies,
2935 slave->dev->last_rx - delta_in_ticks,
2936 slave->dev->last_rx + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
2938 slave->link = BOND_LINK_UP;
2939 slave->state = BOND_STATE_ACTIVE;
2940
2941 /* primary_slave has no meaning in round-robin
2942 * mode. the window of a slave being up and
2943 * curr_active_slave being null after enslaving
2944 * is closed.
2945 */
2946 if (!oldcurrent) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002947 pr_info("%s: link status definitely up for interface %s, ",
2948 bond->dev->name,
2949 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 do_failover = 1;
2951 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002952 pr_info("%s: interface %s is now up\n",
2953 bond->dev->name,
2954 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 }
2956 }
2957 } else {
2958 /* slave->link == BOND_LINK_UP */
2959
2960 /* not all switches will respond to an arp request
2961 * when the source ip is 0, so don't take the link down
2962 * if we don't know our ip yet
2963 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002964 if (!time_in_range(jiffies,
2965 trans_start - delta_in_ticks,
2966 trans_start + 2 * delta_in_ticks) ||
2967 !time_in_range(jiffies,
2968 slave->dev->last_rx - delta_in_ticks,
2969 slave->dev->last_rx + 2 * delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
2971 slave->link = BOND_LINK_DOWN;
2972 slave->state = BOND_STATE_BACKUP;
2973
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002974 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002977 pr_info("%s: interface %s is now down.\n",
2978 bond->dev->name,
2979 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002981 if (slave == oldcurrent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 do_failover = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 }
2984 }
2985
2986 /* note: if switch is in round-robin mode, all links
2987 * must tx arp to ensure all links rx an arp - otherwise
2988 * links may oscillate or not come up at all; if switch is
2989 * in something like xor mode, there is nothing we can
2990 * do - all replies will be rx'ed on same link causing slaves
2991 * to be unstable during low/no traffic periods
2992 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002993 if (IS_UP(slave->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 bond_arp_send_all(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 }
2996
2997 if (do_failover) {
Neil Hormane843fa52010-10-13 16:01:50 +00002998 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002999 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
3001 bond_select_active_slave(bond);
3002
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07003003 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00003004 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 }
3006
3007re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003008 if (bond->params.arp_interval)
3009 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010out:
3011 read_unlock(&bond->lock);
3012}
3013
3014/*
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003015 * Called to inspect slaves for active-backup mode ARP monitor link state
3016 * changes. Sets new_link in slaves to specify what action should take
3017 * place for the slave. Returns 0 if no changes are found, >0 if changes
3018 * to link states must be committed.
3019 *
3020 * Called with bond->lock held for read.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003022static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 struct slave *slave;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003025 int i, commit = 0;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003026 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 bond_for_each_slave(bond, slave, i) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003029 slave->new_link = BOND_LINK_NOCHANGE;
3030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003032 if (time_in_range(jiffies,
3033 slave_last_rx(bond, slave) - delta_in_ticks,
3034 slave_last_rx(bond, slave) + delta_in_ticks)) {
3035
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003036 slave->new_link = BOND_LINK_UP;
3037 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003040 continue;
3041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003043 /*
3044 * Give slaves 2*delta after being enslaved or made
3045 * active. This avoids bouncing, as the last receive
3046 * times need a full ARP monitor cycle to be updated.
3047 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003048 if (time_in_range(jiffies,
3049 slave->jiffies - delta_in_ticks,
3050 slave->jiffies + 2 * delta_in_ticks))
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003051 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003053 /*
3054 * Backup slave is down if:
3055 * - No current_arp_slave AND
3056 * - more than 3*delta since last receive AND
3057 * - the bond has an IP address
3058 *
3059 * Note: a non-null current_arp_slave indicates
3060 * the curr_active_slave went down and we are
3061 * searching for a new one; under this condition
3062 * we only take the curr_active_slave down - this
3063 * gives each slave a chance to tx/rx traffic
3064 * before being taken out
3065 */
3066 if (slave->state == BOND_STATE_BACKUP &&
3067 !bond->current_arp_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003068 !time_in_range(jiffies,
3069 slave_last_rx(bond, slave) - delta_in_ticks,
3070 slave_last_rx(bond, slave) + 3 * delta_in_ticks)) {
3071
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003072 slave->new_link = BOND_LINK_DOWN;
3073 commit++;
3074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003076 /*
3077 * Active slave is down if:
3078 * - more than 2*delta since transmitting OR
3079 * - (more than 2*delta since receive AND
3080 * the bond has an IP address)
3081 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003082 trans_start = dev_trans_start(slave->dev);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003083 if ((slave->state == BOND_STATE_ACTIVE) &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003084 (!time_in_range(jiffies,
3085 trans_start - delta_in_ticks,
3086 trans_start + 2 * delta_in_ticks) ||
3087 !time_in_range(jiffies,
3088 slave_last_rx(bond, slave) - delta_in_ticks,
3089 slave_last_rx(bond, slave) + 2 * delta_in_ticks))) {
3090
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003091 slave->new_link = BOND_LINK_DOWN;
3092 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 }
3094 }
3095
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003096 return commit;
3097}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003099/*
3100 * Called to commit link state changes noted by inspection step of
3101 * active-backup mode ARP monitor.
3102 *
3103 * Called with RTNL and bond->lock for read.
3104 */
3105static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
3106{
3107 struct slave *slave;
3108 int i;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003109 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003111 bond_for_each_slave(bond, slave, i) {
3112 switch (slave->new_link) {
3113 case BOND_LINK_NOCHANGE:
3114 continue;
3115
3116 case BOND_LINK_UP:
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003117 trans_start = dev_trans_start(slave->dev);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003118 if ((!bond->curr_active_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003119 time_in_range(jiffies,
3120 trans_start - delta_in_ticks,
3121 trans_start + delta_in_ticks)) ||
Jiri Pirkob9f60252009-08-31 11:09:38 +00003122 bond->curr_active_slave != slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003123 slave->link = BOND_LINK_UP;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003124 bond->current_arp_slave = NULL;
3125
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003126 pr_info("%s: link status definitely up for interface %s.\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003127 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003128
Jiri Pirkob9f60252009-08-31 11:09:38 +00003129 if (!bond->curr_active_slave ||
3130 (slave == bond->primary_slave))
3131 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003132
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003133 }
3134
Jiri Pirkob9f60252009-08-31 11:09:38 +00003135 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003136
3137 case BOND_LINK_DOWN:
3138 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003141 slave->link = BOND_LINK_DOWN;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003142 bond_set_slave_inactive_flags(slave);
3143
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003144 pr_info("%s: link status definitely down for interface %s, disabling it\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003145 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003146
3147 if (slave == bond->curr_active_slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003148 bond->current_arp_slave = NULL;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003149 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003150 }
Jiri Pirkob9f60252009-08-31 11:09:38 +00003151
3152 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003153
3154 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003155 pr_err("%s: impossible: new_link %d on slave %s\n",
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003156 bond->dev->name, slave->new_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 slave->dev->name);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003158 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
Jiri Pirkob9f60252009-08-31 11:09:38 +00003161do_failover:
3162 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00003163 block_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003164 write_lock_bh(&bond->curr_slave_lock);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003165 bond_select_active_slave(bond);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003166 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00003167 unblock_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003168 }
3169
3170 bond_set_carrier(bond);
3171}
3172
3173/*
3174 * Send ARP probes for active-backup mode ARP monitor.
3175 *
3176 * Called with bond->lock held for read.
3177 */
3178static void bond_ab_arp_probe(struct bonding *bond)
3179{
3180 struct slave *slave;
3181 int i;
3182
3183 read_lock(&bond->curr_slave_lock);
3184
3185 if (bond->current_arp_slave && bond->curr_active_slave)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003186 pr_info("PROBE: c_arp %s && cas %s BAD\n",
3187 bond->current_arp_slave->dev->name,
3188 bond->curr_active_slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003189
3190 if (bond->curr_active_slave) {
3191 bond_arp_send_all(bond, bond->curr_active_slave);
3192 read_unlock(&bond->curr_slave_lock);
3193 return;
3194 }
3195
3196 read_unlock(&bond->curr_slave_lock);
3197
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 /* if we don't have a curr_active_slave, search for the next available
3199 * backup slave from the current_arp_slave and make it the candidate
3200 * for becoming the curr_active_slave
3201 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003202
3203 if (!bond->current_arp_slave) {
3204 bond->current_arp_slave = bond->first_slave;
3205 if (!bond->current_arp_slave)
3206 return;
3207 }
3208
3209 bond_set_slave_inactive_flags(bond->current_arp_slave);
3210
3211 /* search for next candidate */
3212 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3213 if (IS_UP(slave->dev)) {
3214 slave->link = BOND_LINK_BACK;
3215 bond_set_slave_active_flags(slave);
3216 bond_arp_send_all(bond, slave);
3217 slave->jiffies = jiffies;
3218 bond->current_arp_slave = slave;
3219 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 }
3221
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003222 /* if the link state is up at this point, we
3223 * mark it down - this can happen if we have
3224 * simultaneous link failures and
3225 * reselect_active_interface doesn't make this
3226 * one the current slave so it is still marked
3227 * up when it is actually down
3228 */
3229 if (slave->link == BOND_LINK_UP) {
3230 slave->link = BOND_LINK_DOWN;
3231 if (slave->link_failure_count < UINT_MAX)
3232 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003234 bond_set_slave_inactive_flags(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003236 pr_info("%s: backup interface %s is now down.\n",
3237 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 }
3239 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003240}
3241
3242void bond_activebackup_arp_mon(struct work_struct *work)
3243{
3244 struct bonding *bond = container_of(work, struct bonding,
3245 arp_work.work);
3246 int delta_in_ticks;
3247
3248 read_lock(&bond->lock);
3249
3250 if (bond->kill_timers)
3251 goto out;
3252
3253 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3254
3255 if (bond->slave_cnt == 0)
3256 goto re_arm;
3257
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003258 if (bond->send_grat_arp) {
3259 read_lock(&bond->curr_slave_lock);
3260 bond_send_gratuitous_arp(bond);
3261 read_unlock(&bond->curr_slave_lock);
3262 }
3263
Brian Haley305d5522008-11-04 17:51:14 -08003264 if (bond->send_unsol_na) {
3265 read_lock(&bond->curr_slave_lock);
3266 bond_send_unsolicited_na(bond);
3267 read_unlock(&bond->curr_slave_lock);
3268 }
3269
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003270 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3271 read_unlock(&bond->lock);
3272 rtnl_lock();
3273 read_lock(&bond->lock);
3274
3275 bond_ab_arp_commit(bond, delta_in_ticks);
3276
3277 read_unlock(&bond->lock);
3278 rtnl_unlock();
3279 read_lock(&bond->lock);
3280 }
3281
3282 bond_ab_arp_probe(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
3284re_arm:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003285 if (bond->params.arp_interval)
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003286 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287out:
3288 read_unlock(&bond->lock);
3289}
3290
3291/*------------------------------ proc/seq_file-------------------------------*/
3292
3293#ifdef CONFIG_PROC_FS
3294
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazete4a7b932010-10-29 01:52:46 +00003296 __acquires(RCU)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00003297 __acquires(&bond->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
3299 struct bonding *bond = seq->private;
3300 loff_t off = 0;
3301 struct slave *slave;
3302 int i;
3303
3304 /* make sure the bond won't be taken away */
Eric Dumazete4a7b932010-10-29 01:52:46 +00003305 rcu_read_lock();
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003306 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003308 if (*pos == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 return SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
3311 bond_for_each_slave(bond, slave, i) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003312 if (++off == *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 return slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 }
3315
3316 return NULL;
3317}
3318
3319static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3320{
3321 struct bonding *bond = seq->private;
3322 struct slave *slave = v;
3323
3324 ++*pos;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003325 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 return bond->first_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327
3328 slave = slave->next;
3329
3330 return (slave == bond->first_slave) ? NULL : slave;
3331}
3332
3333static void bond_info_seq_stop(struct seq_file *seq, void *v)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00003334 __releases(&bond->lock)
Eric Dumazete4a7b932010-10-29 01:52:46 +00003335 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
3337 struct bonding *bond = seq->private;
3338
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003339 read_unlock(&bond->lock);
Eric Dumazete4a7b932010-10-29 01:52:46 +00003340 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341}
3342
3343static void bond_info_show_master(struct seq_file *seq)
3344{
3345 struct bonding *bond = seq->private;
3346 struct slave *curr;
Mitch Williams4756b022005-11-09 10:36:25 -08003347 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
3349 read_lock(&bond->curr_slave_lock);
3350 curr = bond->curr_active_slave;
3351 read_unlock(&bond->curr_slave_lock);
3352
Jay Vosburghdd957c52007-10-09 19:57:24 -07003353 seq_printf(seq, "Bonding Mode: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 bond_mode_name(bond->params.mode));
3355
Jay Vosburghdd957c52007-10-09 19:57:24 -07003356 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
3357 bond->params.fail_over_mac)
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07003358 seq_printf(seq, " (fail_over_mac %s)",
3359 fail_over_mac_tbl[bond->params.fail_over_mac].modename);
Jay Vosburghdd957c52007-10-09 19:57:24 -07003360
3361 seq_printf(seq, "\n");
3362
Mitch Williamsc61b75a2005-11-09 10:35:13 -08003363 if (bond->params.mode == BOND_MODE_XOR ||
3364 bond->params.mode == BOND_MODE_8023AD) {
3365 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3366 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3367 bond->params.xmit_policy);
3368 }
3369
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 if (USES_PRIMARY(bond->params.mode)) {
Jiri Pirkoa5499522009-09-25 03:28:09 +00003371 seq_printf(seq, "Primary Slave: %s",
Mitch Williams0f418b22005-11-09 10:35:21 -08003372 (bond->primary_slave) ?
3373 bond->primary_slave->dev->name : "None");
Jiri Pirkoa5499522009-09-25 03:28:09 +00003374 if (bond->primary_slave)
3375 seq_printf(seq, " (primary_reselect %s)",
3376 pri_reselect_tbl[bond->params.primary_reselect].modename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377
Jiri Pirkoa5499522009-09-25 03:28:09 +00003378 seq_printf(seq, "\nCurrently Active Slave: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 (curr) ? curr->dev->name : "None");
3380 }
3381
Jay Vosburghff59c452006-03-27 13:27:43 -08003382 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3383 "up" : "down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3385 seq_printf(seq, "Up Delay (ms): %d\n",
3386 bond->params.updelay * bond->params.miimon);
3387 seq_printf(seq, "Down Delay (ms): %d\n",
3388 bond->params.downdelay * bond->params.miimon);
3389
Mitch Williams4756b022005-11-09 10:36:25 -08003390
3391 /* ARP information */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003392 if (bond->params.arp_interval > 0) {
3393 int printed = 0;
Mitch Williams4756b022005-11-09 10:36:25 -08003394 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3395 bond->params.arp_interval);
3396
3397 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3398
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003399 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
Mitch Williams4756b022005-11-09 10:36:25 -08003400 if (!bond->params.arp_targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07003401 break;
Mitch Williams4756b022005-11-09 10:36:25 -08003402 if (printed)
3403 seq_printf(seq, ",");
Harvey Harrison8cf14e32008-10-29 22:43:33 -07003404 seq_printf(seq, " %pI4", &bond->params.arp_targets[i]);
Mitch Williams4756b022005-11-09 10:36:25 -08003405 printed = 1;
3406 }
3407 seq_printf(seq, "\n");
3408 }
3409
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 if (bond->params.mode == BOND_MODE_8023AD) {
3411 struct ad_info ad_info;
3412
3413 seq_puts(seq, "\n802.3ad info\n");
3414 seq_printf(seq, "LACP rate: %s\n",
3415 (bond->params.lacp_fast) ? "fast" : "slow");
Jay Vosburghfd989c82008-11-04 17:51:16 -08003416 seq_printf(seq, "Aggregator selection policy (ad_select): %s\n",
3417 ad_select_tbl[bond->params.ad_select].modename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
3419 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3420 seq_printf(seq, "bond %s has no active aggregator\n",
3421 bond->dev->name);
3422 } else {
3423 seq_printf(seq, "Active Aggregator Info:\n");
3424
3425 seq_printf(seq, "\tAggregator ID: %d\n",
3426 ad_info.aggregator_id);
3427 seq_printf(seq, "\tNumber of ports: %d\n",
3428 ad_info.ports);
3429 seq_printf(seq, "\tActor Key: %d\n",
3430 ad_info.actor_key);
3431 seq_printf(seq, "\tPartner Key: %d\n",
3432 ad_info.partner_key);
Johannes Berge1749612008-10-27 15:59:26 -07003433 seq_printf(seq, "\tPartner Mac Address: %pM\n",
3434 ad_info.partner_system);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 }
3436 }
3437}
3438
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003439static void bond_info_show_slave(struct seq_file *seq,
3440 const struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441{
3442 struct bonding *bond = seq->private;
3443
3444 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3445 seq_printf(seq, "MII Status: %s\n",
3446 (slave->link == BOND_LINK_UP) ? "up" : "down");
Krzysztof Oledzkidd53df22010-09-30 06:19:04 +00003447 seq_printf(seq, "Speed: %d Mbps\n", slave->speed);
3448 seq_printf(seq, "Duplex: %s\n", slave->duplex ? "full" : "half");
Kenzo Iwami655096452006-09-22 21:53:08 -07003449 seq_printf(seq, "Link Failure Count: %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 slave->link_failure_count);
3451
Johannes Berge1749612008-10-27 15:59:26 -07003452 seq_printf(seq, "Permanent HW addr: %pM\n", slave->perm_hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
3454 if (bond->params.mode == BOND_MODE_8023AD) {
3455 const struct aggregator *agg
3456 = SLAVE_AD_INFO(slave).port.aggregator;
3457
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003458 if (agg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 seq_printf(seq, "Aggregator ID: %d\n",
3460 agg->aggregator_identifier);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003461 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 seq_puts(seq, "Aggregator ID: N/A\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 }
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00003464 seq_printf(seq, "Slave queue ID: %d\n", slave->queue_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465}
3466
3467static int bond_info_seq_show(struct seq_file *seq, void *v)
3468{
3469 if (v == SEQ_START_TOKEN) {
3470 seq_printf(seq, "%s\n", version);
3471 bond_info_show_master(seq);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003472 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 bond_info_show_slave(seq, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
3475 return 0;
3476}
3477
Jan Engelhardt4101dec2009-01-14 13:52:18 -08003478static const struct seq_operations bond_info_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 .start = bond_info_seq_start,
3480 .next = bond_info_seq_next,
3481 .stop = bond_info_seq_stop,
3482 .show = bond_info_seq_show,
3483};
3484
3485static int bond_info_open(struct inode *inode, struct file *file)
3486{
3487 struct seq_file *seq;
3488 struct proc_dir_entry *proc;
3489 int res;
3490
3491 res = seq_open(file, &bond_info_seq_ops);
3492 if (!res) {
3493 /* recover the pointer buried in proc_dir_entry data */
3494 seq = file->private_data;
3495 proc = PDE(inode);
3496 seq->private = proc->data;
3497 }
3498
3499 return res;
3500}
3501
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08003502static const struct file_operations bond_info_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 .owner = THIS_MODULE,
3504 .open = bond_info_open,
3505 .read = seq_read,
3506 .llseek = seq_lseek,
3507 .release = seq_release,
3508};
3509
Nicolas de Pesloüan38fc0022009-10-13 00:45:06 -07003510static void bond_create_proc_entry(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511{
3512 struct net_device *bond_dev = bond->dev;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003513 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003515 if (bn->proc_dir) {
Denis V. Luneva95609c2008-04-29 01:02:29 -07003516 bond->proc_entry = proc_create_data(bond_dev->name,
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003517 S_IRUGO, bn->proc_dir,
Denis V. Luneva95609c2008-04-29 01:02:29 -07003518 &bond_info_fops, bond);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003519 if (bond->proc_entry == NULL)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003520 pr_warning("Warning: Cannot create /proc/net/%s/%s\n",
3521 DRV_NAME, bond_dev->name);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003522 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525}
3526
3527static void bond_remove_proc_entry(struct bonding *bond)
3528{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003529 struct net_device *bond_dev = bond->dev;
3530 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
3531
3532 if (bn->proc_dir && bond->proc_entry) {
3533 remove_proc_entry(bond->proc_file_name, bn->proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 memset(bond->proc_file_name, 0, IFNAMSIZ);
3535 bond->proc_entry = NULL;
3536 }
3537}
3538
3539/* Create the bonding directory under /proc/net, if doesn't exist yet.
3540 * Caller must hold rtnl_lock.
3541 */
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003542static void __net_init bond_create_proc_dir(struct bond_net *bn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003544 if (!bn->proc_dir) {
3545 bn->proc_dir = proc_mkdir(DRV_NAME, bn->net->proc_net);
3546 if (!bn->proc_dir)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003547 pr_warning("Warning: cannot create /proc/net/%s\n",
3548 DRV_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 }
3550}
3551
3552/* Destroy the bonding directory under /proc/net, if empty.
3553 * Caller must hold rtnl_lock.
3554 */
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003555static void __net_exit bond_destroy_proc_dir(struct bond_net *bn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003557 if (bn->proc_dir) {
3558 remove_proc_entry(DRV_NAME, bn->net->proc_net);
3559 bn->proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 }
3561}
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003562
3563#else /* !CONFIG_PROC_FS */
3564
Nicolas de Pesloüan38fc0022009-10-13 00:45:06 -07003565static void bond_create_proc_entry(struct bonding *bond)
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003566{
3567}
3568
3569static void bond_remove_proc_entry(struct bonding *bond)
3570{
3571}
3572
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003573static inline void bond_create_proc_dir(struct bond_net *bn)
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003574{
3575}
3576
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003577static inline void bond_destroy_proc_dir(struct bond_net *bn)
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003578{
3579}
3580
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581#endif /* CONFIG_PROC_FS */
3582
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003583
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584/*-------------------------- netdev event handling --------------------------*/
3585
3586/*
3587 * Change device name
3588 */
3589static int bond_event_changename(struct bonding *bond)
3590{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 bond_remove_proc_entry(bond);
3592 bond_create_proc_entry(bond);
Stephen Hemminger7e083842009-06-12 19:02:46 +00003593
Taku Izumif073c7c2010-12-09 15:17:13 +00003594 bond_debug_reregister(bond);
3595
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 return NOTIFY_DONE;
3597}
3598
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003599static int bond_master_netdev_event(unsigned long event,
3600 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601{
Wang Chen454d7c92008-11-12 23:37:49 -08003602 struct bonding *event_bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603
3604 switch (event) {
3605 case NETDEV_CHANGENAME:
3606 return bond_event_changename(event_bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 default:
3608 break;
3609 }
3610
3611 return NOTIFY_DONE;
3612}
3613
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003614static int bond_slave_netdev_event(unsigned long event,
3615 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616{
3617 struct net_device *bond_dev = slave_dev->master;
Wang Chen454d7c92008-11-12 23:37:49 -08003618 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
3620 switch (event) {
3621 case NETDEV_UNREGISTER:
3622 if (bond_dev) {
Jay Vosburgh1284cd32007-10-15 16:44:27 -07003623 if (bond->setup_by_slave)
3624 bond_release_and_destroy(bond_dev, slave_dev);
3625 else
3626 bond_release(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 }
3628 break;
3629 case NETDEV_CHANGE:
Jay Vosburgh17d04502009-03-18 18:38:25 -07003630 if (bond->params.mode == BOND_MODE_8023AD || bond_is_lb(bond)) {
3631 struct slave *slave;
3632
3633 slave = bond_get_slave_by_dev(bond, slave_dev);
3634 if (slave) {
3635 u16 old_speed = slave->speed;
3636 u16 old_duplex = slave->duplex;
3637
3638 bond_update_speed_duplex(slave);
3639
3640 if (bond_is_lb(bond))
3641 break;
3642
3643 if (old_speed != slave->speed)
3644 bond_3ad_adapter_speed_changed(slave);
3645 if (old_duplex != slave->duplex)
3646 bond_3ad_adapter_duplex_changed(slave);
3647 }
3648 }
3649
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 break;
3651 case NETDEV_DOWN:
3652 /*
3653 * ... Or is it this?
3654 */
3655 break;
3656 case NETDEV_CHANGEMTU:
3657 /*
3658 * TODO: Should slaves be allowed to
3659 * independently alter their MTU? For
3660 * an active-backup bond, slaves need
3661 * not be the same type of device, so
3662 * MTUs may vary. For other modes,
3663 * slaves arguably should have the
3664 * same MTUs. To do this, we'd need to
3665 * take over the slave's change_mtu
3666 * function for the duration of their
3667 * servitude.
3668 */
3669 break;
3670 case NETDEV_CHANGENAME:
3671 /*
3672 * TODO: handle changing the primary's name
3673 */
3674 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003675 case NETDEV_FEAT_CHANGE:
3676 bond_compute_features(bond);
3677 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 default:
3679 break;
3680 }
3681
3682 return NOTIFY_DONE;
3683}
3684
3685/*
3686 * bond_netdev_event: handle netdev notifier chain events.
3687 *
3688 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003689 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 * locks for us to safely manipulate the slave devices (RTNL lock,
3691 * dev_probe_lock).
3692 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003693static int bond_netdev_event(struct notifier_block *this,
3694 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695{
3696 struct net_device *event_dev = (struct net_device *)ptr;
3697
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003698 pr_debug("event_dev: %s, event: %lx\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003699 event_dev ? event_dev->name : "None",
3700 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003702 if (!(event_dev->priv_flags & IFF_BONDING))
3703 return NOTIFY_DONE;
3704
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 if (event_dev->flags & IFF_MASTER) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003706 pr_debug("IFF_MASTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 return bond_master_netdev_event(event, event_dev);
3708 }
3709
3710 if (event_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003711 pr_debug("IFF_SLAVE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 return bond_slave_netdev_event(event, event_dev);
3713 }
3714
3715 return NOTIFY_DONE;
3716}
3717
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003718/*
3719 * bond_inetaddr_event: handle inetaddr notifier chain events.
3720 *
3721 * We keep track of device IPs primarily to use as source addresses in
3722 * ARP monitor probes (rather than spewing out broadcasts all the time).
3723 *
3724 * We track one IP for the main device (if it has one), plus one per VLAN.
3725 */
3726static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3727{
3728 struct in_ifaddr *ifa = ptr;
3729 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003730 struct bond_net *bn = net_generic(dev_net(event_dev), bond_net_id);
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003731 struct bonding *bond;
3732 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003733
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003734 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003735 if (bond->dev == event_dev) {
3736 switch (event) {
3737 case NETDEV_UP:
3738 bond->master_ip = ifa->ifa_local;
3739 return NOTIFY_OK;
3740 case NETDEV_DOWN:
3741 bond->master_ip = bond_glean_dev_ip(bond->dev);
3742 return NOTIFY_OK;
3743 default:
3744 return NOTIFY_DONE;
3745 }
3746 }
3747
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003748 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +00003749 if (!bond->vlgrp)
3750 continue;
Dan Aloni5c15bde2007-03-02 20:44:51 -08003751 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003752 if (vlan_dev == event_dev) {
3753 switch (event) {
3754 case NETDEV_UP:
3755 vlan->vlan_ip = ifa->ifa_local;
3756 return NOTIFY_OK;
3757 case NETDEV_DOWN:
3758 vlan->vlan_ip =
3759 bond_glean_dev_ip(vlan_dev);
3760 return NOTIFY_OK;
3761 default:
3762 return NOTIFY_DONE;
3763 }
3764 }
3765 }
3766 }
3767 return NOTIFY_DONE;
3768}
3769
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770static struct notifier_block bond_netdev_notifier = {
3771 .notifier_call = bond_netdev_event,
3772};
3773
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003774static struct notifier_block bond_inetaddr_notifier = {
3775 .notifier_call = bond_inetaddr_event,
3776};
3777
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778/*-------------------------- Packet type handling ---------------------------*/
3779
3780/* register to receive lacpdus on a bond */
3781static void bond_register_lacpdu(struct bonding *bond)
3782{
3783 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3784
3785 /* initialize packet type */
3786 pk_type->type = PKT_TYPE_LACPDU;
3787 pk_type->dev = bond->dev;
3788 pk_type->func = bond_3ad_lacpdu_recv;
3789
3790 dev_add_pack(pk_type);
3791}
3792
3793/* unregister to receive lacpdus on a bond */
3794static void bond_unregister_lacpdu(struct bonding *bond)
3795{
3796 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3797}
3798
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003799void bond_register_arp(struct bonding *bond)
3800{
3801 struct packet_type *pt = &bond->arp_mon_pt;
3802
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003803 if (pt->type)
3804 return;
3805
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003806 pt->type = htons(ETH_P_ARP);
Jay Vosburghe245cb72007-02-28 17:03:27 -08003807 pt->dev = bond->dev;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003808 pt->func = bond_arp_rcv;
3809 dev_add_pack(pt);
3810}
3811
3812void bond_unregister_arp(struct bonding *bond)
3813{
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003814 struct packet_type *pt = &bond->arp_mon_pt;
3815
3816 dev_remove_pack(pt);
3817 pt->type = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003818}
3819
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003820/*---------------------------- Hashing Policies -----------------------------*/
3821
3822/*
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003823 * Hash for the output device based upon layer 2 and layer 3 data. If
3824 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3825 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003826static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003827{
3828 struct ethhdr *data = (struct ethhdr *)skb->data;
3829 struct iphdr *iph = ip_hdr(skb);
3830
Brian Haleyf14c4e42008-09-02 10:08:08 -04003831 if (skb->protocol == htons(ETH_P_IP)) {
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003832 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
Jasper Spaansd3da6832009-10-23 04:08:46 +00003833 (data->h_dest[5] ^ data->h_source[5])) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003834 }
3835
Jasper Spaansd3da6832009-10-23 04:08:46 +00003836 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003837}
3838
3839/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003840 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003841 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3842 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3843 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003844static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003845{
3846 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003847 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003848 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003849 int layer4_xor = 0;
3850
Brian Haleyf14c4e42008-09-02 10:08:08 -04003851 if (skb->protocol == htons(ETH_P_IP)) {
3852 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003853 (iph->protocol == IPPROTO_TCP ||
3854 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003855 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003856 }
3857 return (layer4_xor ^
3858 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3859
3860 }
3861
Jasper Spaansd3da6832009-10-23 04:08:46 +00003862 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003863}
3864
3865/*
3866 * Hash for the output device based upon layer 2 data
3867 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003868static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003869{
3870 struct ethhdr *data = (struct ethhdr *)skb->data;
3871
Jasper Spaansd3da6832009-10-23 04:08:46 +00003872 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003873}
3874
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875/*-------------------------- Device entry points ----------------------------*/
3876
3877static int bond_open(struct net_device *bond_dev)
3878{
Wang Chen454d7c92008-11-12 23:37:49 -08003879 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880
3881 bond->kill_timers = 0;
3882
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003883 INIT_DELAYED_WORK(&bond->mcast_work, bond_resend_igmp_join_requests_delayed);
3884
Holger Eitzenberger58402052008-12-09 23:07:13 -08003885 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 /* bond_alb_initialize must be called before the timer
3887 * is started.
3888 */
3889 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3890 /* something went wrong - fail the open operation */
stephen hemmingerb4739462010-01-25 23:34:15 +00003891 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 }
3893
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003894 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3895 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 }
3897
3898 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003899 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3900 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 }
3902
3903 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003904 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3905 INIT_DELAYED_WORK(&bond->arp_work,
3906 bond_activebackup_arp_mon);
3907 else
3908 INIT_DELAYED_WORK(&bond->arp_work,
3909 bond_loadbalance_arp_mon);
3910
3911 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003912 if (bond->params.arp_validate)
3913 bond_register_arp(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 }
3915
3916 if (bond->params.mode == BOND_MODE_8023AD) {
Adrian Bunka40745f2007-10-24 18:27:43 +02003917 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003918 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 /* register to receive LACPDUs */
3920 bond_register_lacpdu(bond);
Jay Vosburghfd989c82008-11-04 17:51:16 -08003921 bond_3ad_initiate_agg_selection(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 }
3923
3924 return 0;
3925}
3926
3927static int bond_close(struct net_device *bond_dev)
3928{
Wang Chen454d7c92008-11-12 23:37:49 -08003929 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
3931 if (bond->params.mode == BOND_MODE_8023AD) {
3932 /* Unregister the receive of LACPDUs */
3933 bond_unregister_lacpdu(bond);
3934 }
3935
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003936 if (bond->params.arp_validate)
3937 bond_unregister_arp(bond);
3938
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 write_lock_bh(&bond->lock);
3940
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003941 bond->send_grat_arp = 0;
Brian Haley305d5522008-11-04 17:51:14 -08003942 bond->send_unsol_na = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943
3944 /* signal timers not to re-arm */
3945 bond->kill_timers = 1;
3946
3947 write_unlock_bh(&bond->lock);
3948
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003950 cancel_delayed_work(&bond->mii_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 }
3952
3953 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003954 cancel_delayed_work(&bond->arp_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 }
3956
3957 switch (bond->params.mode) {
3958 case BOND_MODE_8023AD:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003959 cancel_delayed_work(&bond->ad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 break;
3961 case BOND_MODE_TLB:
3962 case BOND_MODE_ALB:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003963 cancel_delayed_work(&bond->alb_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 break;
3965 default:
3966 break;
3967 }
3968
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003969 if (delayed_work_pending(&bond->mcast_work))
3970 cancel_delayed_work(&bond->mcast_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
Holger Eitzenberger58402052008-12-09 23:07:13 -08003972 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 /* Must be called only after all
3974 * slaves have been released
3975 */
3976 bond_alb_deinitialize(bond);
3977 }
3978
3979 return 0;
3980}
3981
Eric Dumazet28172732010-07-07 14:58:56 -07003982static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3983 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984{
Wang Chen454d7c92008-11-12 23:37:49 -08003985 struct bonding *bond = netdev_priv(bond_dev);
Eric Dumazet28172732010-07-07 14:58:56 -07003986 struct rtnl_link_stats64 temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 struct slave *slave;
3988 int i;
3989
Eric Dumazet28172732010-07-07 14:58:56 -07003990 memset(stats, 0, sizeof(*stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
3992 read_lock_bh(&bond->lock);
3993
3994 bond_for_each_slave(bond, slave, i) {
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00003995 const struct rtnl_link_stats64 *sstats =
Eric Dumazet28172732010-07-07 14:58:56 -07003996 dev_get_stats(slave->dev, &temp);
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08003997
Eric Dumazet28172732010-07-07 14:58:56 -07003998 stats->rx_packets += sstats->rx_packets;
3999 stats->rx_bytes += sstats->rx_bytes;
4000 stats->rx_errors += sstats->rx_errors;
4001 stats->rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
Eric Dumazet28172732010-07-07 14:58:56 -07004003 stats->tx_packets += sstats->tx_packets;
4004 stats->tx_bytes += sstats->tx_bytes;
4005 stats->tx_errors += sstats->tx_errors;
4006 stats->tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
Eric Dumazet28172732010-07-07 14:58:56 -07004008 stats->multicast += sstats->multicast;
4009 stats->collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
Eric Dumazet28172732010-07-07 14:58:56 -07004011 stats->rx_length_errors += sstats->rx_length_errors;
4012 stats->rx_over_errors += sstats->rx_over_errors;
4013 stats->rx_crc_errors += sstats->rx_crc_errors;
4014 stats->rx_frame_errors += sstats->rx_frame_errors;
4015 stats->rx_fifo_errors += sstats->rx_fifo_errors;
4016 stats->rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017
Eric Dumazet28172732010-07-07 14:58:56 -07004018 stats->tx_aborted_errors += sstats->tx_aborted_errors;
4019 stats->tx_carrier_errors += sstats->tx_carrier_errors;
4020 stats->tx_fifo_errors += sstats->tx_fifo_errors;
4021 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
4022 stats->tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 }
4024
4025 read_unlock_bh(&bond->lock);
4026
4027 return stats;
4028}
4029
4030static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
4031{
4032 struct net_device *slave_dev = NULL;
4033 struct ifbond k_binfo;
4034 struct ifbond __user *u_binfo = NULL;
4035 struct ifslave k_sinfo;
4036 struct ifslave __user *u_sinfo = NULL;
4037 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 int res = 0;
4039
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004040 pr_debug("bond_ioctl: master=%s, cmd=%d\n", bond_dev->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
4042 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 case SIOCGMIIPHY:
4044 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004045 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004047
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 mii->phy_id = 0;
4049 /* Fall Through */
4050 case SIOCGMIIREG:
4051 /*
4052 * We do this again just in case we were called by SIOCGMIIREG
4053 * instead of SIOCGMIIPHY.
4054 */
4055 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004056 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004058
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
4060 if (mii->reg_num == 1) {
Wang Chen454d7c92008-11-12 23:37:49 -08004061 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 mii->val_out = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07004063 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 read_lock(&bond->curr_slave_lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004065 if (netif_carrier_ok(bond->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 mii->val_out = BMSR_LSTATUS;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004067
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 read_unlock(&bond->curr_slave_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07004069 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 }
4071
4072 return 0;
4073 case BOND_INFO_QUERY_OLD:
4074 case SIOCBONDINFOQUERY:
4075 u_binfo = (struct ifbond __user *)ifr->ifr_data;
4076
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004077 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
4080 res = bond_info_query(bond_dev, &k_binfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004081 if (res == 0 &&
4082 copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
4083 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
4085 return res;
4086 case BOND_SLAVE_INFO_QUERY_OLD:
4087 case SIOCBONDSLAVEINFOQUERY:
4088 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
4089
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004090 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
4093 res = bond_slave_info_query(bond_dev, &k_sinfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004094 if (res == 0 &&
4095 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
4096 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
4098 return res;
4099 default:
4100 /* Go on */
4101 break;
4102 }
4103
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004104 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004107 slave_dev = dev_get_by_name(dev_net(bond_dev), ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004109 pr_debug("slave_dev=%p:\n", slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004111 if (!slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 res = -ENODEV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004113 else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004114 pr_debug("slave_dev->name=%s:\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 switch (cmd) {
4116 case BOND_ENSLAVE_OLD:
4117 case SIOCBONDENSLAVE:
4118 res = bond_enslave(bond_dev, slave_dev);
4119 break;
4120 case BOND_RELEASE_OLD:
4121 case SIOCBONDRELEASE:
4122 res = bond_release(bond_dev, slave_dev);
4123 break;
4124 case BOND_SETHWADDR_OLD:
4125 case SIOCBONDSETHWADDR:
4126 res = bond_sethwaddr(bond_dev, slave_dev);
4127 break;
4128 case BOND_CHANGE_ACTIVE_OLD:
4129 case SIOCBONDCHANGEACTIVE:
4130 res = bond_ioctl_change_active(bond_dev, slave_dev);
4131 break;
4132 default:
4133 res = -EOPNOTSUPP;
4134 }
4135
4136 dev_put(slave_dev);
4137 }
4138
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 return res;
4140}
4141
Jiri Pirko22bedad32010-04-01 21:22:57 +00004142static bool bond_addr_in_mc_list(unsigned char *addr,
4143 struct netdev_hw_addr_list *list,
4144 int addrlen)
4145{
4146 struct netdev_hw_addr *ha;
4147
4148 netdev_hw_addr_list_for_each(ha, list)
4149 if (!memcmp(ha->addr, addr, addrlen))
4150 return true;
4151
4152 return false;
4153}
4154
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155static void bond_set_multicast_list(struct net_device *bond_dev)
4156{
Wang Chen454d7c92008-11-12 23:37:49 -08004157 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00004158 struct netdev_hw_addr *ha;
4159 bool found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 /*
4162 * Do promisc before checking multicast_mode
4163 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004164 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07004165 /*
4166 * FIXME: Need to handle the error when one of the multi-slaves
4167 * encounters error.
4168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 bond_set_promiscuity(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004171
4172 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 bond_set_promiscuity(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004174
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
4176 /* set allmulti flag to slaves */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004177 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07004178 /*
4179 * FIXME: Need to handle the error when one of the multi-slaves
4180 * encounters error.
4181 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 bond_set_allmulti(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004184
4185 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 bond_set_allmulti(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004187
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004189 read_lock(&bond->lock);
4190
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 bond->flags = bond_dev->flags;
4192
4193 /* looking for addresses to add to slaves' mc list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004194 netdev_for_each_mc_addr(ha, bond_dev) {
4195 found = bond_addr_in_mc_list(ha->addr, &bond->mc_list,
4196 bond_dev->addr_len);
4197 if (!found)
4198 bond_mc_add(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 }
4200
4201 /* looking for addresses to delete from slaves' list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004202 netdev_hw_addr_list_for_each(ha, &bond->mc_list) {
4203 found = bond_addr_in_mc_list(ha->addr, &bond_dev->mc,
4204 bond_dev->addr_len);
4205 if (!found)
4206 bond_mc_del(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 }
4208
4209 /* save master's multicast list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004210 __hw_addr_flush(&bond->mc_list);
4211 __hw_addr_add_multiple(&bond->mc_list, &bond_dev->mc,
4212 bond_dev->addr_len, NETDEV_HW_ADDR_T_MULTICAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004214 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215}
4216
Stephen Hemminger00829822008-11-20 20:14:53 -08004217static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
4218{
4219 struct bonding *bond = netdev_priv(dev);
4220 struct slave *slave = bond->first_slave;
4221
4222 if (slave) {
4223 const struct net_device_ops *slave_ops
4224 = slave->dev->netdev_ops;
4225 if (slave_ops->ndo_neigh_setup)
Patrick McHardy72e22402009-03-05 01:57:44 -08004226 return slave_ops->ndo_neigh_setup(slave->dev, parms);
Stephen Hemminger00829822008-11-20 20:14:53 -08004227 }
4228 return 0;
4229}
4230
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231/*
4232 * Change the MTU of all of a master's slaves to match the master
4233 */
4234static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
4235{
Wang Chen454d7c92008-11-12 23:37:49 -08004236 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 struct slave *slave, *stop_at;
4238 int res = 0;
4239 int i;
4240
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004241 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004242 (bond_dev ? bond_dev->name : "None"), new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
4244 /* Can't hold bond->lock with bh disabled here since
4245 * some base drivers panic. On the other hand we can't
4246 * hold bond->lock without bh disabled because we'll
4247 * deadlock. The only solution is to rely on the fact
4248 * that we're under rtnl_lock here, and the slaves
4249 * list won't change. This doesn't solve the problem
4250 * of setting the slave's MTU while it is
4251 * transmitting, but the assumption is that the base
4252 * driver can handle that.
4253 *
4254 * TODO: figure out a way to safely iterate the slaves
4255 * list, but without holding a lock around the actual
4256 * call to the base driver.
4257 */
4258
4259 bond_for_each_slave(bond, slave, i) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004260 pr_debug("s %p s->p %p c_m %p\n",
4261 slave,
4262 slave->prev,
4263 slave->dev->netdev_ops->ndo_change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08004264
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 res = dev_set_mtu(slave->dev, new_mtu);
4266
4267 if (res) {
4268 /* If we failed to set the slave's mtu to the new value
4269 * we must abort the operation even in ACTIVE_BACKUP
4270 * mode, because if we allow the backup slaves to have
4271 * different mtu values than the active slave we'll
4272 * need to change their mtu when doing a failover. That
4273 * means changing their mtu from timer context, which
4274 * is probably not a good idea.
4275 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004276 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 goto unwind;
4278 }
4279 }
4280
4281 bond_dev->mtu = new_mtu;
4282
4283 return 0;
4284
4285unwind:
4286 /* unwind from head to the slave that failed */
4287 stop_at = slave;
4288 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4289 int tmp_res;
4290
4291 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4292 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004293 pr_debug("unwind err %d dev %s\n",
4294 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 }
4296 }
4297
4298 return res;
4299}
4300
4301/*
4302 * Change HW address
4303 *
4304 * Note that many devices must be down to change the HW address, and
4305 * downing the master releases all slaves. We can make bonds full of
4306 * bonding devices to test this, however.
4307 */
4308static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4309{
Wang Chen454d7c92008-11-12 23:37:49 -08004310 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 struct sockaddr *sa = addr, tmp_sa;
4312 struct slave *slave, *stop_at;
4313 int res = 0;
4314 int i;
4315
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004316 if (bond->params.mode == BOND_MODE_ALB)
4317 return bond_alb_set_mac_address(bond_dev, addr);
4318
4319
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004320 pr_debug("bond=%p, name=%s\n",
4321 bond, bond_dev ? bond_dev->name : "None");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322
Jay Vosburghdd957c52007-10-09 19:57:24 -07004323 /*
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004324 * If fail_over_mac is set to active, do nothing and return
4325 * success. Returning an error causes ifenslave to fail.
Jay Vosburghdd957c52007-10-09 19:57:24 -07004326 */
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004327 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
Jay Vosburghdd957c52007-10-09 19:57:24 -07004328 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07004329
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004330 if (!is_valid_ether_addr(sa->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332
4333 /* Can't hold bond->lock with bh disabled here since
4334 * some base drivers panic. On the other hand we can't
4335 * hold bond->lock without bh disabled because we'll
4336 * deadlock. The only solution is to rely on the fact
4337 * that we're under rtnl_lock here, and the slaves
4338 * list won't change. This doesn't solve the problem
4339 * of setting the slave's hw address while it is
4340 * transmitting, but the assumption is that the base
4341 * driver can handle that.
4342 *
4343 * TODO: figure out a way to safely iterate the slaves
4344 * list, but without holding a lock around the actual
4345 * call to the base driver.
4346 */
4347
4348 bond_for_each_slave(bond, slave, i) {
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004349 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004350 pr_debug("slave %p %s\n", slave, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004352 if (slave_ops->ndo_set_mac_address == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 res = -EOPNOTSUPP;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004354 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 goto unwind;
4356 }
4357
4358 res = dev_set_mac_address(slave->dev, addr);
4359 if (res) {
4360 /* TODO: consider downing the slave
4361 * and retry ?
4362 * User should expect communications
4363 * breakage anyway until ARP finish
4364 * updating, so...
4365 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004366 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 goto unwind;
4368 }
4369 }
4370
4371 /* success */
4372 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4373 return 0;
4374
4375unwind:
4376 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4377 tmp_sa.sa_family = bond_dev->type;
4378
4379 /* unwind from head to the slave that failed */
4380 stop_at = slave;
4381 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4382 int tmp_res;
4383
4384 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4385 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004386 pr_debug("unwind err %d dev %s\n",
4387 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 }
4389 }
4390
4391 return res;
4392}
4393
4394static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4395{
Wang Chen454d7c92008-11-12 23:37:49 -08004396 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 struct slave *slave, *start_at;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004398 int i, slave_no, res = 1;
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004399 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
4401 read_lock(&bond->lock);
4402
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004403 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 goto out;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004405 /*
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004406 * Start with the curr_active_slave that joined the bond as the
4407 * default for sending IGMP traffic. For failover purposes one
4408 * needs to maintain some consistency for the interface that will
4409 * send the join/membership reports. The curr_active_slave found
4410 * will send all of this type of traffic.
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004411 */
Eric Dumazet00ae7022010-03-30 23:08:37 +00004412 if ((iph->protocol == IPPROTO_IGMP) &&
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004413 (skb->protocol == htons(ETH_P_IP))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004415 read_lock(&bond->curr_slave_lock);
4416 slave = bond->curr_active_slave;
4417 read_unlock(&bond->curr_slave_lock);
4418
4419 if (!slave)
4420 goto out;
4421 } else {
4422 /*
4423 * Concurrent TX may collide on rr_tx_counter; we accept
4424 * that as being rare enough not to justify using an
4425 * atomic op here.
4426 */
4427 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
4428
4429 bond_for_each_slave(bond, slave, i) {
4430 slave_no--;
4431 if (slave_no < 0)
4432 break;
4433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 }
4435
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004436 start_at = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 bond_for_each_slave_from(bond, slave, i, start_at) {
4438 if (IS_UP(slave->dev) &&
4439 (slave->link == BOND_LINK_UP) &&
4440 (slave->state == BOND_STATE_ACTIVE)) {
4441 res = bond_dev_queue_xmit(bond, skb, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 break;
4443 }
4444 }
4445
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446out:
4447 if (res) {
4448 /* no suitable interface, frame not sent */
4449 dev_kfree_skb(skb);
4450 }
4451 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004452 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453}
4454
John W. Linville075897c2005-09-28 17:50:53 -04004455
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456/*
4457 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4458 * the bond has a usable interface.
4459 */
4460static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4461{
Wang Chen454d7c92008-11-12 23:37:49 -08004462 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 int res = 1;
4464
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 read_lock(&bond->lock);
4466 read_lock(&bond->curr_slave_lock);
4467
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004468 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
John W. Linville075897c2005-09-28 17:50:53 -04004471 if (!bond->curr_active_slave)
4472 goto out;
4473
John W. Linville075897c2005-09-28 17:50:53 -04004474 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4475
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004477 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 /* no suitable interface, frame not sent */
4479 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004480
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 read_unlock(&bond->curr_slave_lock);
4482 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004483 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484}
4485
4486/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004487 * In bond_xmit_xor() , we determine the output device by using a pre-
4488 * determined xmit_hash_policy(), If the selected device is not enabled,
4489 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 */
4491static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4492{
Wang Chen454d7c92008-11-12 23:37:49 -08004493 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 struct slave *slave, *start_at;
4495 int slave_no;
4496 int i;
4497 int res = 1;
4498
4499 read_lock(&bond->lock);
4500
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004501 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
Jasper Spaansa361c832009-10-23 04:09:24 +00004504 slave_no = bond->xmit_hash_policy(skb, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
4506 bond_for_each_slave(bond, slave, i) {
4507 slave_no--;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004508 if (slave_no < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 }
4511
4512 start_at = slave;
4513
4514 bond_for_each_slave_from(bond, slave, i, start_at) {
4515 if (IS_UP(slave->dev) &&
4516 (slave->link == BOND_LINK_UP) &&
4517 (slave->state == BOND_STATE_ACTIVE)) {
4518 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4519 break;
4520 }
4521 }
4522
4523out:
4524 if (res) {
4525 /* no suitable interface, frame not sent */
4526 dev_kfree_skb(skb);
4527 }
4528 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004529 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530}
4531
4532/*
4533 * in broadcast mode, we send everything to all usable interfaces.
4534 */
4535static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4536{
Wang Chen454d7c92008-11-12 23:37:49 -08004537 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 struct slave *slave, *start_at;
4539 struct net_device *tx_dev = NULL;
4540 int i;
4541 int res = 1;
4542
4543 read_lock(&bond->lock);
4544
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004545 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
4548 read_lock(&bond->curr_slave_lock);
4549 start_at = bond->curr_active_slave;
4550 read_unlock(&bond->curr_slave_lock);
4551
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004552 if (!start_at)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
4555 bond_for_each_slave_from(bond, slave, i, start_at) {
4556 if (IS_UP(slave->dev) &&
4557 (slave->link == BOND_LINK_UP) &&
4558 (slave->state == BOND_STATE_ACTIVE)) {
4559 if (tx_dev) {
4560 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4561 if (!skb2) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004562 pr_err("%s: Error: bond_xmit_broadcast(): skb_clone() failed\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08004563 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 continue;
4565 }
4566
4567 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4568 if (res) {
4569 dev_kfree_skb(skb2);
4570 continue;
4571 }
4572 }
4573 tx_dev = slave->dev;
4574 }
4575 }
4576
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004577 if (tx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 res = bond_dev_queue_xmit(bond, skb, tx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
4580out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004581 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 /* no suitable interface, frame not sent */
4583 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004584
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 /* frame sent to all suitable interfaces */
4586 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004587 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588}
4589
4590/*------------------------- Device initialization ---------------------------*/
4591
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004592static void bond_set_xmit_hash_policy(struct bonding *bond)
4593{
4594 switch (bond->params.xmit_policy) {
4595 case BOND_XMIT_POLICY_LAYER23:
4596 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4597 break;
4598 case BOND_XMIT_POLICY_LAYER34:
4599 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4600 break;
4601 case BOND_XMIT_POLICY_LAYER2:
4602 default:
4603 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4604 break;
4605 }
4606}
4607
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004608/*
4609 * Lookup the slave that corresponds to a qid
4610 */
4611static inline int bond_slave_override(struct bonding *bond,
4612 struct sk_buff *skb)
4613{
4614 int i, res = 1;
4615 struct slave *slave = NULL;
4616 struct slave *check_slave;
4617
4618 read_lock(&bond->lock);
4619
4620 if (!BOND_IS_OK(bond) || !skb->queue_mapping)
4621 goto out;
4622
4623 /* Find out if any slaves have the same mapping as this skb. */
4624 bond_for_each_slave(bond, check_slave, i) {
4625 if (check_slave->queue_id == skb->queue_mapping) {
4626 slave = check_slave;
4627 break;
4628 }
4629 }
4630
4631 /* If the slave isn't UP, use default transmit policy. */
4632 if (slave && slave->queue_id && IS_UP(slave->dev) &&
4633 (slave->link == BOND_LINK_UP)) {
4634 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4635 }
4636
4637out:
4638 read_unlock(&bond->lock);
4639 return res;
4640}
4641
4642static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb)
4643{
4644 /*
4645 * This helper function exists to help dev_pick_tx get the correct
4646 * destination queue. Using a helper function skips the a call to
4647 * skb_tx_hash and will put the skbs in the queue we expect on their
4648 * way down to the bonding driver.
4649 */
4650 return skb->queue_mapping;
4651}
4652
Stephen Hemminger424efe92009-08-31 19:50:51 +00004653static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
Stephen Hemminger00829822008-11-20 20:14:53 -08004654{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004655 struct bonding *bond = netdev_priv(dev);
4656
Neil Hormane843fa52010-10-13 16:01:50 +00004657 /*
4658 * If we risk deadlock from transmitting this in the
4659 * netpoll path, tell netpoll to queue the frame for later tx
4660 */
4661 if (is_netpoll_tx_blocked(dev))
4662 return NETDEV_TX_BUSY;
4663
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004664 if (TX_QUEUE_OVERRIDE(bond->params.mode)) {
4665 if (!bond_slave_override(bond, skb))
4666 return NETDEV_TX_OK;
4667 }
Stephen Hemminger00829822008-11-20 20:14:53 -08004668
4669 switch (bond->params.mode) {
4670 case BOND_MODE_ROUNDROBIN:
4671 return bond_xmit_roundrobin(skb, dev);
4672 case BOND_MODE_ACTIVEBACKUP:
4673 return bond_xmit_activebackup(skb, dev);
4674 case BOND_MODE_XOR:
4675 return bond_xmit_xor(skb, dev);
4676 case BOND_MODE_BROADCAST:
4677 return bond_xmit_broadcast(skb, dev);
4678 case BOND_MODE_8023AD:
4679 return bond_3ad_xmit_xor(skb, dev);
4680 case BOND_MODE_ALB:
4681 case BOND_MODE_TLB:
4682 return bond_alb_xmit(skb, dev);
4683 default:
4684 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004685 pr_err("%s: Error: Unknown bonding mode %d\n",
4686 dev->name, bond->params.mode);
Stephen Hemminger00829822008-11-20 20:14:53 -08004687 WARN_ON_ONCE(1);
4688 dev_kfree_skb(skb);
4689 return NETDEV_TX_OK;
4690 }
4691}
4692
4693
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694/*
4695 * set bond mode specific net device operations
4696 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004697void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004699 struct net_device *bond_dev = bond->dev;
4700
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 switch (mode) {
4702 case BOND_MODE_ROUNDROBIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 break;
4704 case BOND_MODE_ACTIVEBACKUP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 break;
4706 case BOND_MODE_XOR:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004707 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 break;
4709 case BOND_MODE_BROADCAST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 break;
4711 case BOND_MODE_8023AD:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004712 bond_set_master_3ad_flags(bond);
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004713 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004716 bond_set_master_alb_flags(bond);
4717 /* FALLTHRU */
4718 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 break;
4720 default:
4721 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004722 pr_err("%s: Error: Unknown bonding mode %d\n",
4723 bond_dev->name, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 break;
4725 }
4726}
4727
Jay Vosburgh217df672005-09-26 16:11:50 -07004728static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4729 struct ethtool_drvinfo *drvinfo)
4730{
4731 strncpy(drvinfo->driver, DRV_NAME, 32);
4732 strncpy(drvinfo->version, DRV_VERSION, 32);
4733 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4734}
4735
Jeff Garzik7282d492006-09-13 14:30:00 -04004736static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004737 .get_drvinfo = bond_ethtool_get_drvinfo,
Stephen Hemmingerfa53eba2008-09-13 21:17:09 -04004738 .get_link = ethtool_op_get_link,
4739 .get_tx_csum = ethtool_op_get_tx_csum,
4740 .get_sg = ethtool_op_get_sg,
4741 .get_tso = ethtool_op_get_tso,
4742 .get_ufo = ethtool_op_get_ufo,
4743 .get_flags = ethtool_op_get_flags,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004744};
4745
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004746static const struct net_device_ops bond_netdev_ops = {
Stephen Hemminger181470f2009-06-12 19:02:52 +00004747 .ndo_init = bond_init,
Stephen Hemminger9e716262009-06-12 19:02:47 +00004748 .ndo_uninit = bond_uninit,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004749 .ndo_open = bond_open,
4750 .ndo_stop = bond_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08004751 .ndo_start_xmit = bond_start_xmit,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004752 .ndo_select_queue = bond_select_queue,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00004753 .ndo_get_stats64 = bond_get_stats,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004754 .ndo_do_ioctl = bond_do_ioctl,
4755 .ndo_set_multicast_list = bond_set_multicast_list,
4756 .ndo_change_mtu = bond_change_mtu,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004757 .ndo_set_mac_address = bond_set_mac_address,
Stephen Hemminger00829822008-11-20 20:14:53 -08004758 .ndo_neigh_setup = bond_neigh_setup,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004759 .ndo_vlan_rx_register = bond_vlan_rx_register,
4760 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4761 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
WANG Congf6dc31a2010-05-06 00:48:51 -07004762#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00004763 .ndo_netpoll_setup = bond_netpoll_setup,
WANG Congf6dc31a2010-05-06 00:48:51 -07004764 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
4765 .ndo_poll_controller = bond_poll_controller,
4766#endif
Jiri Pirko9232ecc2011-02-13 09:33:01 +00004767 .ndo_add_slave = bond_enslave,
4768 .ndo_del_slave = bond_release,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004769};
4770
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004771static void bond_destructor(struct net_device *bond_dev)
4772{
4773 struct bonding *bond = netdev_priv(bond_dev);
4774 if (bond->wq)
4775 destroy_workqueue(bond->wq);
4776 free_netdev(bond_dev);
4777}
4778
Stephen Hemminger181470f2009-06-12 19:02:52 +00004779static void bond_setup(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780{
Wang Chen454d7c92008-11-12 23:37:49 -08004781 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 /* initialize rwlocks */
4784 rwlock_init(&bond->lock);
4785 rwlock_init(&bond->curr_slave_lock);
4786
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00004787 bond->params = bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
4789 /* Initialize pointers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 bond->dev = bond_dev;
4791 INIT_LIST_HEAD(&bond->vlan_list);
4792
4793 /* Initialize the device entry points */
Stephen Hemminger181470f2009-06-12 19:02:52 +00004794 ether_setup(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004795 bond_dev->netdev_ops = &bond_netdev_ops;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004796 bond_dev->ethtool_ops = &bond_ethtool_ops;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004797 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004799 bond_dev->destructor = bond_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800
4801 /* Initialize the device options */
4802 bond_dev->tx_queue_len = 0;
4803 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004804 bond_dev->priv_flags |= IFF_BONDING;
Stephen Hemminger181470f2009-06-12 19:02:52 +00004805 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
4806
Jay Vosburgh6cf3f412008-11-03 18:16:50 -08004807 if (bond->params.arp_interval)
4808 bond_dev->priv_flags |= IFF_MASTER_ARPMON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
4810 /* At first, we block adding VLANs. That's the only way to
4811 * prevent problems that occur when adding VLANs over an
4812 * empty bond. The block will be removed once non-challenged
4813 * slaves are enslaved.
4814 */
4815 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4816
Herbert Xu932ff272006-06-09 12:20:56 -07004817 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 * transmitting */
4819 bond_dev->features |= NETIF_F_LLTX;
4820
4821 /* By default, we declare the bond to be fully
4822 * VLAN hardware accelerated capable. Special
4823 * care is taken in the various xmit functions
4824 * when there are slaves that are not hw accel
4825 * capable
4826 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4828 NETIF_F_HW_VLAN_RX |
4829 NETIF_F_HW_VLAN_FILTER);
4830
Eric Dumazete6599c22010-09-17 09:25:07 +00004831 /* By default, we enable GRO on bonding devices.
4832 * Actual support requires lowlevel drivers are GRO ready.
4833 */
4834 bond_dev->features |= NETIF_F_GRO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835}
4836
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004837static void bond_work_cancel_all(struct bonding *bond)
4838{
4839 write_lock_bh(&bond->lock);
4840 bond->kill_timers = 1;
4841 write_unlock_bh(&bond->lock);
4842
4843 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4844 cancel_delayed_work(&bond->mii_work);
4845
4846 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4847 cancel_delayed_work(&bond->arp_work);
4848
4849 if (bond->params.mode == BOND_MODE_ALB &&
4850 delayed_work_pending(&bond->alb_work))
4851 cancel_delayed_work(&bond->alb_work);
4852
4853 if (bond->params.mode == BOND_MODE_8023AD &&
4854 delayed_work_pending(&bond->ad_work))
4855 cancel_delayed_work(&bond->ad_work);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00004856
4857 if (delayed_work_pending(&bond->mcast_work))
4858 cancel_delayed_work(&bond->mcast_work);
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004859}
4860
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004861/*
4862* Destroy a bonding device.
4863* Must be under rtnl_lock when this function is called.
4864*/
4865static void bond_uninit(struct net_device *bond_dev)
Jay Vosburgha434e432008-10-30 17:41:15 -07004866{
Wang Chen454d7c92008-11-12 23:37:49 -08004867 struct bonding *bond = netdev_priv(bond_dev);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004868 struct vlan_entry *vlan, *tmp;
Jay Vosburgha434e432008-10-30 17:41:15 -07004869
WANG Congf6dc31a2010-05-06 00:48:51 -07004870 bond_netpoll_cleanup(bond_dev);
4871
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004872 /* Release the bonded slaves */
4873 bond_release_all(bond_dev);
4874
Jay Vosburgha434e432008-10-30 17:41:15 -07004875 list_del(&bond->bond_list);
4876
4877 bond_work_cancel_all(bond);
4878
Jay Vosburgha434e432008-10-30 17:41:15 -07004879 bond_remove_proc_entry(bond);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004880
Taku Izumif073c7c2010-12-09 15:17:13 +00004881 bond_debug_unregister(bond);
4882
Jiri Pirko22bedad32010-04-01 21:22:57 +00004883 __hw_addr_flush(&bond->mc_list);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004884
4885 list_for_each_entry_safe(vlan, tmp, &bond->vlan_list, vlan_list) {
4886 list_del(&vlan->vlan_list);
4887 kfree(vlan);
4888 }
Jay Vosburgha434e432008-10-30 17:41:15 -07004889}
4890
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891/*------------------------- Module initialization ---------------------------*/
4892
4893/*
4894 * Convert string input module parms. Accept either the
Jay Vosburghece95f72008-01-17 16:25:01 -08004895 * number of the mode or its string name. A bit complicated because
4896 * some mode names are substrings of other names, and calls from sysfs
4897 * may have whitespace in the name (trailing newlines, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 */
Holger Eitzenberger325dcf72008-12-09 23:10:17 -08004899int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900{
Hannes Eder54b87322009-02-14 11:15:49 +00004901 int modeint = -1, i, rv;
Jay Vosburgha42e5342008-01-29 18:07:43 -08004902 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
Jay Vosburghece95f72008-01-17 16:25:01 -08004903
Jay Vosburgha42e5342008-01-29 18:07:43 -08004904 for (p = (char *)buf; *p; p++)
4905 if (!(isdigit(*p) || isspace(*p)))
4906 break;
4907
4908 if (*p)
Jay Vosburghece95f72008-01-17 16:25:01 -08004909 rv = sscanf(buf, "%20s", modestr);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004910 else
Hannes Eder54b87322009-02-14 11:15:49 +00004911 rv = sscanf(buf, "%d", &modeint);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004912
4913 if (!rv)
4914 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
4916 for (i = 0; tbl[i].modename; i++) {
Hannes Eder54b87322009-02-14 11:15:49 +00004917 if (modeint == tbl[i].mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 return tbl[i].mode;
Jay Vosburghece95f72008-01-17 16:25:01 -08004919 if (strcmp(modestr, tbl[i].modename) == 0)
4920 return tbl[i].mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 }
4922
4923 return -1;
4924}
4925
4926static int bond_check_params(struct bond_params *params)
4927{
Jiri Pirkoa5499522009-09-25 03:28:09 +00004928 int arp_validate_value, fail_over_mac_value, primary_reselect_value;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004929
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 /*
4931 * Convert string parameters.
4932 */
4933 if (mode) {
4934 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4935 if (bond_mode == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004936 pr_err("Error: Invalid bonding mode \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 mode == NULL ? "NULL" : mode);
4938 return -EINVAL;
4939 }
4940 }
4941
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004942 if (xmit_hash_policy) {
4943 if ((bond_mode != BOND_MODE_XOR) &&
4944 (bond_mode != BOND_MODE_8023AD)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004945 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004946 bond_mode_name(bond_mode));
4947 } else {
4948 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4949 xmit_hashtype_tbl);
4950 if (xmit_hashtype == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004951 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004952 xmit_hash_policy == NULL ? "NULL" :
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004953 xmit_hash_policy);
4954 return -EINVAL;
4955 }
4956 }
4957 }
4958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 if (lacp_rate) {
4960 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004961 pr_info("lacp_rate param is irrelevant in mode %s\n",
4962 bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 } else {
4964 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4965 if (lacp_fast == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004966 pr_err("Error: Invalid lacp rate \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 lacp_rate == NULL ? "NULL" : lacp_rate);
4968 return -EINVAL;
4969 }
4970 }
4971 }
4972
Jay Vosburghfd989c82008-11-04 17:51:16 -08004973 if (ad_select) {
4974 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4975 if (params->ad_select == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004976 pr_err("Error: Invalid ad_select \"%s\"\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -08004977 ad_select == NULL ? "NULL" : ad_select);
4978 return -EINVAL;
4979 }
4980
4981 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004982 pr_warning("ad_select param only affects 802.3ad mode\n");
Jay Vosburghfd989c82008-11-04 17:51:16 -08004983 }
4984 } else {
4985 params->ad_select = BOND_AD_STABLE;
4986 }
4987
Nicolas de Pesloüanf5841302009-08-28 13:18:34 +00004988 if (max_bonds < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004989 pr_warning("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4990 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 max_bonds = BOND_DEFAULT_MAX_BONDS;
4992 }
4993
4994 if (miimon < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004995 pr_warning("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to %d\n",
4996 miimon, INT_MAX, BOND_LINK_MON_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 miimon = BOND_LINK_MON_INTERV;
4998 }
4999
5000 if (updelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005001 pr_warning("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
5002 updelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 updelay = 0;
5004 }
5005
5006 if (downdelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005007 pr_warning("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
5008 downdelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 downdelay = 0;
5010 }
5011
5012 if ((use_carrier != 0) && (use_carrier != 1)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005013 pr_warning("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
5014 use_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 use_carrier = 1;
5016 }
5017
Moni Shoua7893b242008-05-17 21:10:12 -07005018 if (num_grat_arp < 0 || num_grat_arp > 255) {
Frans Pop2381a552010-03-24 07:57:36 +00005019 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 -08005020 num_grat_arp);
Moni Shoua7893b242008-05-17 21:10:12 -07005021 num_grat_arp = 1;
5022 }
5023
Brian Haley305d5522008-11-04 17:51:14 -08005024 if (num_unsol_na < 0 || num_unsol_na > 255) {
Frans Pop2381a552010-03-24 07:57:36 +00005025 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 -08005026 num_unsol_na);
Brian Haley305d5522008-11-04 17:51:14 -08005027 num_unsol_na = 1;
5028 }
5029
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 /* reset values for 802.3ad */
5031 if (bond_mode == BOND_MODE_8023AD) {
5032 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005033 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 +00005034 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 miimon = 100;
5036 }
5037 }
5038
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005039 if (tx_queues < 1 || tx_queues > 255) {
5040 pr_warning("Warning: tx_queues (%d) should be between "
5041 "1 and 255, resetting to %d\n",
5042 tx_queues, BOND_DEFAULT_TX_QUEUES);
5043 tx_queues = BOND_DEFAULT_TX_QUEUES;
5044 }
5045
Andy Gospodarekebd8e492010-06-02 08:39:21 +00005046 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
5047 pr_warning("Warning: all_slaves_active module parameter (%d), "
5048 "not of valid value (0/1), so it was set to "
5049 "0\n", all_slaves_active);
5050 all_slaves_active = 0;
5051 }
5052
Flavio Leitnerc2952c32010-10-05 14:23:59 +00005053 if (resend_igmp < 0 || resend_igmp > 255) {
5054 pr_warning("Warning: resend_igmp (%d) should be between "
5055 "0 and 255, resetting to %d\n",
5056 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
5057 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
5058 }
5059
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 /* reset values for TLB/ALB */
5061 if ((bond_mode == BOND_MODE_TLB) ||
5062 (bond_mode == BOND_MODE_ALB)) {
5063 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005064 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 +00005065 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 miimon = 100;
5067 }
5068 }
5069
5070 if (bond_mode == BOND_MODE_ALB) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005071 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",
5072 updelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 }
5074
5075 if (!miimon) {
5076 if (updelay || downdelay) {
5077 /* just warn the user the up/down delay will have
5078 * no effect since miimon is zero...
5079 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005080 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",
5081 updelay, downdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 }
5083 } else {
5084 /* don't allow arp monitoring */
5085 if (arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005086 pr_warning("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
5087 miimon, arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 arp_interval = 0;
5089 }
5090
5091 if ((updelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005092 pr_warning("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
5093 updelay, miimon,
5094 (updelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 }
5096
5097 updelay /= miimon;
5098
5099 if ((downdelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005100 pr_warning("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
5101 downdelay, miimon,
5102 (downdelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 }
5104
5105 downdelay /= miimon;
5106 }
5107
5108 if (arp_interval < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005109 pr_warning("Warning: arp_interval module parameter (%d) , not in range 0-%d, so it was reset to %d\n",
5110 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 arp_interval = BOND_LINK_ARP_INTERV;
5112 }
5113
5114 for (arp_ip_count = 0;
5115 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
5116 arp_ip_count++) {
5117 /* not complete check, but should be good enough to
5118 catch mistakes */
5119 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005120 pr_warning("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
5121 arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 arp_interval = 0;
5123 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04005124 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 arp_target[arp_ip_count] = ip;
5126 }
5127 }
5128
5129 if (arp_interval && !arp_ip_count) {
5130 /* don't allow arping if no arp_ip_target given... */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005131 pr_warning("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
5132 arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 arp_interval = 0;
5134 }
5135
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005136 if (arp_validate) {
5137 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005138 pr_err("arp_validate only supported in active-backup mode\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005139 return -EINVAL;
5140 }
5141 if (!arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005142 pr_err("arp_validate requires arp_interval\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005143 return -EINVAL;
5144 }
5145
5146 arp_validate_value = bond_parse_parm(arp_validate,
5147 arp_validate_tbl);
5148 if (arp_validate_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005149 pr_err("Error: invalid arp_validate \"%s\"\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005150 arp_validate == NULL ? "NULL" : arp_validate);
5151 return -EINVAL;
5152 }
5153 } else
5154 arp_validate_value = 0;
5155
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 if (miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005157 pr_info("MII link monitoring set to %d ms\n", miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 } else if (arp_interval) {
5159 int i;
5160
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005161 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
5162 arp_interval,
5163 arp_validate_tbl[arp_validate_value].modename,
5164 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
5166 for (i = 0; i < arp_ip_count; i++)
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00005167 pr_info(" %s", arp_ip_target[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00005169 pr_info("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170
Jay Vosburghb8a97872008-06-13 18:12:04 -07005171 } else if (max_bonds) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 /* miimon and arp_interval not set, we need one so things
5173 * work as expected, see bonding.txt for details
5174 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005175 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 -07005176 }
5177
5178 if (primary && !USES_PRIMARY(bond_mode)) {
5179 /* currently, using a primary only makes sense
5180 * in active backup, TLB or ALB modes
5181 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005182 pr_warning("Warning: %s primary device specified but has no effect in %s mode\n",
5183 primary, bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 primary = NULL;
5185 }
5186
Jiri Pirkoa5499522009-09-25 03:28:09 +00005187 if (primary && primary_reselect) {
5188 primary_reselect_value = bond_parse_parm(primary_reselect,
5189 pri_reselect_tbl);
5190 if (primary_reselect_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005191 pr_err("Error: Invalid primary_reselect \"%s\"\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00005192 primary_reselect ==
5193 NULL ? "NULL" : primary_reselect);
5194 return -EINVAL;
5195 }
5196 } else {
5197 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
5198 }
5199
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005200 if (fail_over_mac) {
5201 fail_over_mac_value = bond_parse_parm(fail_over_mac,
5202 fail_over_mac_tbl);
5203 if (fail_over_mac_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005204 pr_err("Error: invalid fail_over_mac \"%s\"\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005205 arp_validate == NULL ? "NULL" : arp_validate);
5206 return -EINVAL;
5207 }
5208
5209 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005210 pr_warning("Warning: fail_over_mac only affects active-backup mode.\n");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005211 } else {
5212 fail_over_mac_value = BOND_FOM_NONE;
5213 }
Jay Vosburghdd957c52007-10-09 19:57:24 -07005214
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 /* fill params struct with the proper values */
5216 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04005217 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 params->miimon = miimon;
Moni Shoua7893b242008-05-17 21:10:12 -07005219 params->num_grat_arp = num_grat_arp;
Brian Haley305d5522008-11-04 17:51:14 -08005220 params->num_unsol_na = num_unsol_na;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005222 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 params->updelay = updelay;
5224 params->downdelay = downdelay;
5225 params->use_carrier = use_carrier;
5226 params->lacp_fast = lacp_fast;
5227 params->primary[0] = 0;
Jiri Pirkoa5499522009-09-25 03:28:09 +00005228 params->primary_reselect = primary_reselect_value;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005229 params->fail_over_mac = fail_over_mac_value;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005230 params->tx_queues = tx_queues;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00005231 params->all_slaves_active = all_slaves_active;
Flavio Leitnerc2952c32010-10-05 14:23:59 +00005232 params->resend_igmp = resend_igmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233
5234 if (primary) {
5235 strncpy(params->primary, primary, IFNAMSIZ);
5236 params->primary[IFNAMSIZ - 1] = 0;
5237 }
5238
5239 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
5240
5241 return 0;
5242}
5243
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005244static struct lock_class_key bonding_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -07005245static struct lock_class_key bonding_netdev_addr_lock_key;
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005246
David S. Millere8a04642008-07-17 00:34:19 -07005247static void bond_set_lockdep_class_one(struct net_device *dev,
5248 struct netdev_queue *txq,
5249 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -07005250{
5251 lockdep_set_class(&txq->_xmit_lock,
5252 &bonding_netdev_xmit_lock_key);
5253}
5254
5255static void bond_set_lockdep_class(struct net_device *dev)
5256{
David S. Millercf508b12008-07-22 14:16:42 -07005257 lockdep_set_class(&dev->addr_list_lock,
5258 &bonding_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -07005259 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -07005260}
5261
Stephen Hemminger181470f2009-06-12 19:02:52 +00005262/*
5263 * Called from registration process
5264 */
5265static int bond_init(struct net_device *bond_dev)
5266{
5267 struct bonding *bond = netdev_priv(bond_dev);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005268 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005269
5270 pr_debug("Begin bond_init for %s\n", bond_dev->name);
5271
5272 bond->wq = create_singlethread_workqueue(bond_dev->name);
5273 if (!bond->wq)
5274 return -ENOMEM;
5275
5276 bond_set_lockdep_class(bond_dev);
5277
5278 netif_carrier_off(bond_dev);
5279
5280 bond_create_proc_entry(bond);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005281 list_add_tail(&bond->bond_list, &bn->dev_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005282
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00005283 bond_prepare_sysfs_group(bond);
Jiri Pirko22bedad32010-04-01 21:22:57 +00005284
Taku Izumif073c7c2010-12-09 15:17:13 +00005285 bond_debug_register(bond);
5286
Jiri Pirko22bedad32010-04-01 21:22:57 +00005287 __hw_addr_init(&bond->mc_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005288 return 0;
5289}
5290
Eric W. Biederman88ead972009-10-29 14:18:25 +00005291static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
5292{
5293 if (tb[IFLA_ADDRESS]) {
5294 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
5295 return -EINVAL;
5296 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
5297 return -EADDRNOTAVAIL;
5298 }
5299 return 0;
5300}
5301
5302static struct rtnl_link_ops bond_link_ops __read_mostly = {
5303 .kind = "bond",
Eric W. Biederman66391042009-10-29 23:58:54 +00005304 .priv_size = sizeof(struct bonding),
Eric W. Biederman88ead972009-10-29 14:18:25 +00005305 .setup = bond_setup,
5306 .validate = bond_validate,
5307};
5308
Mitch Williamsdfe60392005-11-09 10:36:04 -08005309/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005310 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08005311 * Caller must NOT hold rtnl_lock; we need to release it here before we
5312 * set up our sysfs entries.
5313 */
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005314int bond_create(struct net *net, const char *name)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005315{
5316 struct net_device *bond_dev;
5317 int res;
5318
5319 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08005320
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005321 bond_dev = alloc_netdev_mq(sizeof(struct bonding), name ? name : "",
5322 bond_setup, tx_queues);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005323 if (!bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005324 pr_err("%s: eek! can't alloc netdev!\n", name);
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005325 rtnl_unlock();
5326 return -ENOMEM;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005327 }
5328
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005329 dev_net_set(bond_dev, net);
Eric W. Biederman88ead972009-10-29 14:18:25 +00005330 bond_dev->rtnl_link_ops = &bond_link_ops;
5331
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005332 if (!name) {
5333 res = dev_alloc_name(bond_dev, "bond%d");
5334 if (res < 0)
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005335 goto out;
Neil Horman27e6f062010-10-05 03:39:21 +00005336 } else {
5337 /*
5338 * If we're given a name to register
5339 * we need to ensure that its not already
5340 * registered
5341 */
5342 res = -EEXIST;
5343 if (__dev_get_by_name(net, name) != NULL)
5344 goto out;
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005345 }
5346
Mitch Williamsdfe60392005-11-09 10:36:04 -08005347 res = register_netdevice(bond_dev);
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005348
Eric W. Biederman30c15ba2009-10-29 14:18:23 +00005349out:
Mitch Williamsdfe60392005-11-09 10:36:04 -08005350 rtnl_unlock();
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005351 if (res < 0)
5352 bond_destructor(bond_dev);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005353 return res;
5354}
5355
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005356static int __net_init bond_net_init(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005357{
Eric W. Biederman15449742009-11-29 15:46:04 +00005358 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005359
5360 bn->net = net;
5361 INIT_LIST_HEAD(&bn->dev_list);
5362
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005363 bond_create_proc_dir(bn);
Eric W. Biederman15449742009-11-29 15:46:04 +00005364
5365 return 0;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005366}
5367
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005368static void __net_exit bond_net_exit(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005369{
Eric W. Biederman15449742009-11-29 15:46:04 +00005370 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005371
5372 bond_destroy_proc_dir(bn);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005373}
5374
5375static struct pernet_operations bond_net_ops = {
5376 .init = bond_net_init,
5377 .exit = bond_net_exit,
Eric W. Biederman15449742009-11-29 15:46:04 +00005378 .id = &bond_net_id,
5379 .size = sizeof(struct bond_net),
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005380};
5381
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382static int __init bonding_init(void)
5383{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 int i;
5385 int res;
5386
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005387 pr_info("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388
Mitch Williamsdfe60392005-11-09 10:36:04 -08005389 res = bond_check_params(&bonding_defaults);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005390 if (res)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005391 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392
Eric W. Biederman15449742009-11-29 15:46:04 +00005393 res = register_pernet_subsys(&bond_net_ops);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005394 if (res)
5395 goto out;
Jay Vosburgh027ea042008-01-17 16:25:02 -08005396
Eric W. Biederman88ead972009-10-29 14:18:25 +00005397 res = rtnl_link_register(&bond_link_ops);
5398 if (res)
Eric W. Biederman66391042009-10-29 23:58:54 +00005399 goto err_link;
Eric W. Biederman88ead972009-10-29 14:18:25 +00005400
Taku Izumif073c7c2010-12-09 15:17:13 +00005401 bond_create_debugfs();
5402
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 for (i = 0; i < max_bonds; i++) {
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005404 res = bond_create(&init_net, NULL);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005405 if (res)
5406 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 }
5408
Mitch Williamsb76cdba2005-11-09 10:36:41 -08005409 res = bond_create_sysfs();
5410 if (res)
5411 goto err;
5412
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005414 register_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005415 bond_register_ipv6_notifier();
Mitch Williamsdfe60392005-11-09 10:36:04 -08005416out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 return res;
Eric W. Biederman88ead972009-10-29 14:18:25 +00005418err:
5419 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman66391042009-10-29 23:58:54 +00005420err_link:
Eric W. Biederman15449742009-11-29 15:46:04 +00005421 unregister_pernet_subsys(&bond_net_ops);
Eric W. Biederman88ead972009-10-29 14:18:25 +00005422 goto out;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005423
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424}
5425
5426static void __exit bonding_exit(void)
5427{
5428 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005429 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005430 bond_unregister_ipv6_notifier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005432 bond_destroy_sysfs();
Taku Izumif073c7c2010-12-09 15:17:13 +00005433 bond_destroy_debugfs();
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005434
Eric W. Biederman88ead972009-10-29 14:18:25 +00005435 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman15449742009-11-29 15:46:04 +00005436 unregister_pernet_subsys(&bond_net_ops);
Neil Hormane843fa52010-10-13 16:01:50 +00005437
5438#ifdef CONFIG_NET_POLL_CONTROLLER
Neil Hormanfb4fa762010-12-06 09:05:50 +00005439 /*
5440 * Make sure we don't have an imbalance on our netpoll blocking
5441 */
5442 WARN_ON(atomic_read(&netpoll_block_tx));
Neil Hormane843fa52010-10-13 16:01:50 +00005443#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444}
5445
5446module_init(bonding_init);
5447module_exit(bonding_exit);
5448MODULE_LICENSE("GPL");
5449MODULE_VERSION(DRV_VERSION);
5450MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5451MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
Eric W. Biederman88ead972009-10-29 14:18:25 +00005452MODULE_ALIAS_RTNL_LINK("bond");