blob: 912b416b757338896c2731fa7fb03f1a8b87a0a0 [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) {
1514 u16 *dest = (u16 *) eth_hdr(skb)->h_dest;
1515
1516 memcpy(dest, bond_dev->dev_addr, ETH_ALEN);
1517 }
1518
1519 return skb;
1520}
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522/* enslave device <slave> to bond device <master> */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001523int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Wang Chen454d7c92008-11-12 23:37:49 -08001525 struct bonding *bond = netdev_priv(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001526 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 struct slave *new_slave = NULL;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001528 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 struct sockaddr addr;
1530 int link_reporting;
1531 int old_features = bond_dev->features;
1532 int res = 0;
1533
nsxfreddy@gmail.com552709d2005-09-21 14:18:04 -05001534 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001535 slave_ops->ndo_do_ioctl == NULL) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001536 pr_warning("%s: Warning: no link monitoring support for %s\n",
1537 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539
1540 /* bond must be initialized by bond_open() before enslaving */
1541 if (!(bond_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001542 pr_warning("%s: master_dev is not up in bond_enslave\n",
1543 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545
1546 /* already enslaved */
1547 if (slave_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001548 pr_debug("Error, Device was already enslaved\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return -EBUSY;
1550 }
1551
1552 /* vlan challenged mutual exclusion */
1553 /* no need to lock since we're protected by rtnl_lock */
1554 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001555 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Jay Vosburghf35188f2010-07-21 12:14:47 +00001556 if (bond->vlgrp) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001557 pr_err("%s: Error: cannot enslave VLAN challenged slave %s on VLAN enabled bond %s\n",
1558 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return -EPERM;
1560 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001561 pr_warning("%s: Warning: enslaved VLAN challenged slave %s. Adding VLANs will be blocked as long as %s is part of bond %s\n",
1562 bond_dev->name, slave_dev->name,
1563 slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1565 }
1566 } else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001567 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (bond->slave_cnt == 0) {
1569 /* First slave, and it is not VLAN challenged,
1570 * so remove the block of adding VLANs over the bond.
1571 */
1572 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1573 }
1574 }
1575
Jay Vosburgh217df672005-09-26 16:11:50 -07001576 /*
1577 * Old ifenslave binaries are no longer supported. These can
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001578 * be identified with moderate accuracy by the state of the slave:
Jay Vosburgh217df672005-09-26 16:11:50 -07001579 * the current ifenslave will set the interface down prior to
1580 * enslaving it; the old ifenslave will not.
1581 */
1582 if ((slave_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001583 pr_err("%s is up. This may be due to an out of date ifenslave.\n",
Jay Vosburgh217df672005-09-26 16:11:50 -07001584 slave_dev->name);
1585 res = -EPERM;
1586 goto err_undo_flags;
1587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Moni Shoua872254d2007-10-09 19:43:38 -07001589 /* set bonding device ether type by slave - bonding netdevices are
1590 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1591 * there is a need to override some of the type dependent attribs/funcs.
1592 *
1593 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1594 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1595 */
1596 if (bond->slave_cnt == 0) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001597 if (bond_dev->type != slave_dev->type) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001598 pr_debug("%s: change device type from %d to %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001599 bond_dev->name,
1600 bond_dev->type, slave_dev->type);
Moni Shoua75c78502009-09-15 02:37:40 -07001601
Jiri Pirko3ca5b402010-03-10 10:29:35 +00001602 res = netdev_bonding_change(bond_dev,
1603 NETDEV_PRE_TYPE_CHANGE);
1604 res = notifier_to_errno(res);
1605 if (res) {
1606 pr_err("%s: refused to change device type\n",
1607 bond_dev->name);
1608 res = -EBUSY;
1609 goto err_undo_flags;
1610 }
Moni Shoua75c78502009-09-15 02:37:40 -07001611
Jiri Pirko32a806c2010-03-19 04:00:23 +00001612 /* Flush unicast and multicast addresses */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001613 dev_uc_flush(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001614 dev_mc_flush(bond_dev);
Jiri Pirko32a806c2010-03-19 04:00:23 +00001615
Moni Shouae36b9d12009-07-15 04:56:31 +00001616 if (slave_dev->type != ARPHRD_ETHER)
1617 bond_setup_by_slave(bond_dev, slave_dev);
1618 else
1619 ether_setup(bond_dev);
Moni Shoua75c78502009-09-15 02:37:40 -07001620
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001621 netdev_bonding_change(bond_dev,
1622 NETDEV_POST_TYPE_CHANGE);
Moni Shouae36b9d12009-07-15 04:56:31 +00001623 }
Moni Shoua872254d2007-10-09 19:43:38 -07001624 } else if (bond_dev->type != slave_dev->type) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001625 pr_err("%s ether type (%d) is different from other slaves (%d), can not enslave it.\n",
1626 slave_dev->name,
1627 slave_dev->type, bond_dev->type);
1628 res = -EINVAL;
1629 goto err_undo_flags;
Moni Shoua872254d2007-10-09 19:43:38 -07001630 }
1631
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001632 if (slave_ops->ndo_set_mac_address == NULL) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001633 if (bond->slave_cnt == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001634 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.",
1635 bond_dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001636 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1637 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001638 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",
1639 bond_dev->name);
Moni Shoua2ab82852007-10-09 19:43:39 -07001640 res = -EOPNOTSUPP;
1641 goto err_undo_flags;
1642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001645 /* If this is the first slave, then we need to set the master's hardware
1646 * address to be the same as the slave's. */
David Strandd13a2cb2010-12-01 11:43:08 -08001647 if (is_zero_ether_addr(bond->dev->dev_addr))
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001648 memcpy(bond->dev->dev_addr, slave_dev->dev_addr,
1649 slave_dev->addr_len);
1650
1651
Joe Jin243cb4e2007-02-06 14:16:40 -08001652 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 if (!new_slave) {
1654 res = -ENOMEM;
1655 goto err_undo_flags;
1656 }
1657
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001658 /*
1659 * Set the new_slave's queue_id to be zero. Queue ID mapping
1660 * is set via sysfs or module option if desired.
1661 */
1662 new_slave->queue_id = 0;
1663
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001664 /* Save slave's original mtu and then set it to match the bond */
1665 new_slave->original_mtu = slave_dev->mtu;
1666 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1667 if (res) {
1668 pr_debug("Error %d calling dev_set_mtu\n", res);
1669 goto err_free;
1670 }
1671
Jay Vosburgh217df672005-09-26 16:11:50 -07001672 /*
1673 * Save slave's original ("permanent") mac address for modes
1674 * that need it, and for restoring it upon release, and then
1675 * set it to the master's address
1676 */
1677 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Jay Vosburghdd957c52007-10-09 19:57:24 -07001679 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001680 /*
1681 * Set slave to master's mac address. The application already
1682 * set the master's mac address to that of the first slave
1683 */
1684 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1685 addr.sa_family = slave_dev->type;
1686 res = dev_set_mac_address(slave_dev, &addr);
1687 if (res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001688 pr_debug("Error %d calling set_mac_address\n", res);
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001689 goto err_restore_mtu;
Moni Shoua2ab82852007-10-09 19:43:39 -07001690 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Jiri Pirko1765a572011-02-12 06:48:36 +00001693 res = netdev_set_bond_master(slave_dev, bond_dev);
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001694 if (res) {
Jiri Pirko1765a572011-02-12 06:48:36 +00001695 pr_debug("Error %d calling netdev_set_bond_master\n", res);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001696 goto err_restore_mac;
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001697 }
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001698 res = netdev_rx_handler_register(slave_dev, bond_handle_frame, NULL);
1699 if (res) {
1700 pr_debug("Error %d calling netdev_rx_handler_register\n", res);
1701 goto err_unset_master;
1702 }
1703
Jay Vosburgh217df672005-09-26 16:11:50 -07001704 /* open the slave since the application closed it */
1705 res = dev_open(slave_dev);
1706 if (res) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001707 pr_debug("Opening slave %s failed\n", slave_dev->name);
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001708 goto err_unreg_rxhandler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 new_slave->dev = slave_dev;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001712 slave_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Holger Eitzenberger58402052008-12-09 23:07:13 -08001714 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 /* bond_alb_init_slave() must be called before all other stages since
1716 * it might fail and we do not want to have to undo everything
1717 */
1718 res = bond_alb_init_slave(bond, new_slave);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001719 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001720 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722
1723 /* If the mode USES_PRIMARY, then the new slave gets the
1724 * master's promisc (and mc) settings only if it becomes the
1725 * curr_active_slave, and that is taken care of later when calling
1726 * bond_change_active()
1727 */
1728 if (!USES_PRIMARY(bond->params.mode)) {
1729 /* set promiscuity level to new slave */
1730 if (bond_dev->flags & IFF_PROMISC) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001731 res = dev_set_promiscuity(slave_dev, 1);
1732 if (res)
1733 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 }
1735
1736 /* set allmulti level to new slave */
1737 if (bond_dev->flags & IFF_ALLMULTI) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001738 res = dev_set_allmulti(slave_dev, 1);
1739 if (res)
1740 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
1742
David S. Millerb9e40852008-07-15 00:15:08 -07001743 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 /* upload master's mc_list to new slave */
Jiri Pirko22bedad32010-04-01 21:22:57 +00001745 netdev_for_each_mc_addr(ha, bond_dev)
1746 dev_mc_add(slave_dev, ha->addr);
David S. Millerb9e40852008-07-15 00:15:08 -07001747 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
1749
1750 if (bond->params.mode == BOND_MODE_8023AD) {
1751 /* add lacpdu mc addr to mc list */
1752 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1753
Jiri Pirko22bedad32010-04-01 21:22:57 +00001754 dev_mc_add(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756
1757 bond_add_vlans_on_slave(bond, slave_dev);
1758
1759 write_lock_bh(&bond->lock);
1760
1761 bond_attach_slave(bond, new_slave);
1762
1763 new_slave->delay = 0;
1764 new_slave->link_failure_count = 0;
1765
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001766 bond_compute_features(bond);
1767
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001768 write_unlock_bh(&bond->lock);
1769
1770 read_lock(&bond->lock);
1771
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001772 new_slave->last_arp_rx = jiffies;
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 if (bond->params.miimon && !bond->params.use_carrier) {
1775 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1776
1777 if ((link_reporting == -1) && !bond->params.arp_interval) {
1778 /*
1779 * miimon is set but a bonded network driver
1780 * does not support ETHTOOL/MII and
1781 * arp_interval is not set. Note: if
1782 * use_carrier is enabled, we will never go
1783 * here (because netif_carrier is always
1784 * supported); thus, we don't need to change
1785 * the messages for netif_carrier.
1786 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001787 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 -08001788 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 } else if (link_reporting == -1) {
1790 /* unable get link status using mii/ethtool */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001791 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",
1792 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794 }
1795
1796 /* check for initial state */
1797 if (!bond->params.miimon ||
1798 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1799 if (bond->params.updelay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001800 pr_debug("Initial state of slave_dev is BOND_LINK_BACK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 new_slave->link = BOND_LINK_BACK;
1802 new_slave->delay = bond->params.updelay;
1803 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001804 pr_debug("Initial state of slave_dev is BOND_LINK_UP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 new_slave->link = BOND_LINK_UP;
1806 }
1807 new_slave->jiffies = jiffies;
1808 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001809 pr_debug("Initial state of slave_dev is BOND_LINK_DOWN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 new_slave->link = BOND_LINK_DOWN;
1811 }
1812
1813 if (bond_update_speed_duplex(new_slave) &&
1814 (new_slave->link != BOND_LINK_DOWN)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001815 pr_warning("%s: Warning: failed to get speed and duplex from %s, assumed to be 100Mb/sec and Full.\n",
1816 bond_dev->name, new_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 if (bond->params.mode == BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001819 pr_warning("%s: Warning: Operation of 802.3ad mode requires ETHTOOL support in base driver for proper aggregator selection.\n",
1820 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 }
1822 }
1823
1824 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1825 /* if there is a primary slave, remember it */
Jiri Pirkoa5499522009-09-25 03:28:09 +00001826 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 bond->primary_slave = new_slave;
Jiri Pirkoa5499522009-09-25 03:28:09 +00001828 bond->force_primary = true;
1829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 }
1831
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001832 write_lock_bh(&bond->curr_slave_lock);
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 switch (bond->params.mode) {
1835 case BOND_MODE_ACTIVEBACKUP:
Jay Vosburgh8a8e4472006-09-22 21:56:15 -07001836 bond_set_slave_inactive_flags(new_slave);
1837 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 break;
1839 case BOND_MODE_8023AD:
1840 /* in 802.3ad mode, the internal mechanism
1841 * will activate the slaves in the selected
1842 * aggregator
1843 */
1844 bond_set_slave_inactive_flags(new_slave);
1845 /* if this is the first slave */
1846 if (bond->slave_cnt == 1) {
1847 SLAVE_AD_INFO(new_slave).id = 1;
1848 /* Initialize AD with the number of times that the AD timer is called in 1 second
1849 * can be called only after the mac address of the bond is set
1850 */
1851 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1852 bond->params.lacp_fast);
1853 } else {
1854 SLAVE_AD_INFO(new_slave).id =
1855 SLAVE_AD_INFO(new_slave->prev).id + 1;
1856 }
1857
1858 bond_3ad_bind_slave(new_slave);
1859 break;
1860 case BOND_MODE_TLB:
1861 case BOND_MODE_ALB:
1862 new_slave->state = BOND_STATE_ACTIVE;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001863 bond_set_slave_inactive_flags(new_slave);
Jiri Pirko5a29f782009-03-25 17:23:38 -07001864 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 break;
1866 default:
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001867 pr_debug("This slave is always active in trunk mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 /* always active in trunk mode */
1870 new_slave->state = BOND_STATE_ACTIVE;
1871
1872 /* In trunking mode there is little meaning to curr_active_slave
1873 * anyway (it holds no special properties of the bond device),
1874 * so we can change it without calling change_active_interface()
1875 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001876 if (!bond->curr_active_slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 bond->curr_active_slave = new_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 break;
1880 } /* switch(bond_mode) */
1881
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001882 write_unlock_bh(&bond->curr_slave_lock);
1883
Jay Vosburghff59c452006-03-27 13:27:43 -08001884 bond_set_carrier(bond);
1885
WANG Congf6dc31a2010-05-06 00:48:51 -07001886#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001887 slave_dev->npinfo = bond_netpoll_info(bond);
1888 if (slave_dev->npinfo) {
1889 if (slave_enable_netpoll(new_slave)) {
1890 read_unlock(&bond->lock);
1891 pr_info("Error, %s: master_dev is using netpoll, "
1892 "but new slave device does not support netpoll.\n",
1893 bond_dev->name);
1894 res = -EBUSY;
1895 goto err_close;
1896 }
WANG Congf6dc31a2010-05-06 00:48:51 -07001897 }
1898#endif
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001899
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001900 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001902 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1903 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001904 goto err_close;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001905
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001906 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
1907 bond_dev->name, slave_dev->name,
1908 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1909 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 /* enslave is successful */
1912 return 0;
1913
1914/* Undo stages on error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915err_close:
1916 dev_close(slave_dev);
1917
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001918err_unreg_rxhandler:
1919 netdev_rx_handler_unregister(slave_dev);
1920
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001921err_unset_master:
Jiri Pirko1765a572011-02-12 06:48:36 +00001922 netdev_set_bond_master(slave_dev, NULL);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924err_restore_mac:
Jay Vosburghdd957c52007-10-09 19:57:24 -07001925 if (!bond->params.fail_over_mac) {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001926 /* XXX TODO - fom follow mode needs to change master's
1927 * MAC if this slave's MAC is in use by the bond, or at
1928 * least print a warning.
1929 */
Moni Shoua2ab82852007-10-09 19:43:39 -07001930 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1931 addr.sa_family = slave_dev->type;
1932 dev_set_mac_address(slave_dev, &addr);
1933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001935err_restore_mtu:
1936 dev_set_mtu(slave_dev, new_slave->original_mtu);
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938err_free:
1939 kfree(new_slave);
1940
1941err_undo_flags:
1942 bond_dev->features = old_features;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 return res;
1945}
1946
1947/*
1948 * Try to release the slave device <slave> from the bond device <master>
1949 * It is legal to access curr_active_slave without a lock because all the function
1950 * is write-locked.
1951 *
1952 * The rules for slave state should be:
1953 * for Active/Backup:
1954 * Active stays on all backups go down
1955 * for Bonded connections:
1956 * The first up interface should be left on and all others downed.
1957 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001958int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Wang Chen454d7c92008-11-12 23:37:49 -08001960 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 struct slave *slave, *oldcurrent;
1962 struct sockaddr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 /* slave is not a slave or master is not master of this slave */
1965 if (!(slave_dev->flags & IFF_SLAVE) ||
1966 (slave_dev->master != bond_dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001967 pr_err("%s: Error: cannot release %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 bond_dev->name, slave_dev->name);
1969 return -EINVAL;
1970 }
1971
Neil Hormane843fa52010-10-13 16:01:50 +00001972 block_netpoll_tx();
WANG Congf6dc31a2010-05-06 00:48:51 -07001973 netdev_bonding_change(bond_dev, NETDEV_BONDING_DESLAVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 write_lock_bh(&bond->lock);
1975
1976 slave = bond_get_slave_by_dev(bond, slave_dev);
1977 if (!slave) {
1978 /* not a slave of this bond */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001979 pr_info("%s: %s not enslaved\n",
1980 bond_dev->name, slave_dev->name);
Jay Vosburghf5e2a7b2006-02-07 21:17:22 -08001981 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001982 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return -EINVAL;
1984 }
1985
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001986 if (!bond->params.fail_over_mac) {
Joe Perches8e95a202009-12-03 07:58:21 +00001987 if (!compare_ether_addr(bond_dev->dev_addr, slave->perm_hwaddr) &&
1988 bond->slave_cnt > 1)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001989 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",
1990 bond_dev->name, slave_dev->name,
1991 slave->perm_hwaddr,
1992 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
1994
1995 /* Inform AD package of unbinding of slave. */
1996 if (bond->params.mode == BOND_MODE_8023AD) {
1997 /* must be called before the slave is
1998 * detached from the list
1999 */
2000 bond_3ad_unbind_slave(slave);
2001 }
2002
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002003 pr_info("%s: releasing %s interface %s\n",
2004 bond_dev->name,
2005 (slave->state == BOND_STATE_ACTIVE) ? "active" : "backup",
2006 slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 oldcurrent = bond->curr_active_slave;
2009
2010 bond->current_arp_slave = NULL;
2011
2012 /* release the slave from its bond */
2013 bond_detach_slave(bond, slave);
2014
Arthur Kepner8531c5f2005-08-23 01:34:53 -04002015 bond_compute_features(bond);
2016
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002017 if (bond->primary_slave == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 bond->primary_slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002020 if (oldcurrent == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 bond_change_active_slave(bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Holger Eitzenberger58402052008-12-09 23:07:13 -08002023 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 /* Must be called only after the slave has been
2025 * detached from the list and the curr_active_slave
2026 * has been cleared (if our_slave == old_current),
2027 * but before a new active slave is selected.
2028 */
Jay Vosburgh25433312008-01-17 16:24:59 -08002029 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 bond_alb_deinit_slave(bond, slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08002031 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 }
2033
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002034 if (oldcurrent == slave) {
2035 /*
2036 * Note that we hold RTNL over this sequence, so there
2037 * is no concern that another slave add/remove event
2038 * will interfere.
2039 */
2040 write_unlock_bh(&bond->lock);
2041 read_lock(&bond->lock);
2042 write_lock_bh(&bond->curr_slave_lock);
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 bond_select_active_slave(bond);
2045
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002046 write_unlock_bh(&bond->curr_slave_lock);
2047 read_unlock(&bond->lock);
2048 write_lock_bh(&bond->lock);
2049 }
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (bond->slave_cnt == 0) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002052 bond_set_carrier(bond);
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 /* if the last slave was removed, zero the mac address
2055 * of the master so it will be set by the application
2056 * to the mac address of the first slave
2057 */
2058 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2059
Jay Vosburghf35188f2010-07-21 12:14:47 +00002060 if (!bond->vlgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2062 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002063 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2064 bond_dev->name, bond_dev->name);
2065 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2066 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
2068 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2069 !bond_has_challenged_slaves(bond)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002070 pr_info("%s: last VLAN challenged slave %s left bond %s. VLAN blocking is removed\n",
2071 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
2073 }
2074
2075 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002076 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002078 /* must do this from outside any spinlocks */
2079 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 bond_del_vlans_from_slave(bond, slave_dev);
2082
2083 /* If the mode USES_PRIMARY, then we should only remove its
2084 * promisc and mc settings if it was the curr_active_slave, but that was
2085 * already taken care of above when we detached the slave
2086 */
2087 if (!USES_PRIMARY(bond->params.mode)) {
2088 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002089 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
2092 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002093 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002097 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002099 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 }
2101
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00002102 netdev_rx_handler_unregister(slave_dev);
Jiri Pirko1765a572011-02-12 06:48:36 +00002103 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002105 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07002106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 /* close slave before restoring its mac address */
2108 dev_close(slave_dev);
2109
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07002110 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002111 /* restore original ("permanent") mac address */
2112 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2113 addr.sa_family = slave_dev->type;
2114 dev_set_mac_address(slave_dev, &addr);
2115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00002117 dev_set_mtu(slave_dev, slave->original_mtu);
2118
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002119 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002120 IFF_SLAVE_INACTIVE | IFF_BONDING |
2121 IFF_SLAVE_NEEDARP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
2123 kfree(slave);
2124
2125 return 0; /* deletion OK */
2126}
2127
2128/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002129* First release a slave and than destroy the bond if no more slaves are left.
Moni Shouad90a1622007-10-09 19:43:43 -07002130* Must be under rtnl_lock when this function is called.
2131*/
stephen hemminger26d8ee72010-10-15 05:09:34 +00002132static int bond_release_and_destroy(struct net_device *bond_dev,
2133 struct net_device *slave_dev)
Moni Shouad90a1622007-10-09 19:43:43 -07002134{
Wang Chen454d7c92008-11-12 23:37:49 -08002135 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002136 int ret;
2137
2138 ret = bond_release(bond_dev, slave_dev);
2139 if ((ret == 0) && (bond->slave_cnt == 0)) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002140 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002141 pr_info("%s: destroying bond %s.\n",
2142 bond_dev->name, bond_dev->name);
Stephen Hemminger9e716262009-06-12 19:02:47 +00002143 unregister_netdevice(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002144 }
2145 return ret;
2146}
2147
2148/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 * This function releases all slaves.
2150 */
2151static int bond_release_all(struct net_device *bond_dev)
2152{
Wang Chen454d7c92008-11-12 23:37:49 -08002153 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 struct slave *slave;
2155 struct net_device *slave_dev;
2156 struct sockaddr addr;
2157
2158 write_lock_bh(&bond->lock);
2159
Jay Vosburghff59c452006-03-27 13:27:43 -08002160 netif_carrier_off(bond_dev);
2161
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002162 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 bond->current_arp_slave = NULL;
2166 bond->primary_slave = NULL;
2167 bond_change_active_slave(bond, NULL);
2168
2169 while ((slave = bond->first_slave) != NULL) {
2170 /* Inform AD package of unbinding of slave
2171 * before slave is detached from the list.
2172 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002173 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 bond_3ad_unbind_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 slave_dev = slave->dev;
2177 bond_detach_slave(bond, slave);
2178
Jay Vosburgh25433312008-01-17 16:24:59 -08002179 /* now that the slave is detached, unlock and perform
2180 * all the undo steps that should not be called from
2181 * within a lock.
2182 */
2183 write_unlock_bh(&bond->lock);
2184
Holger Eitzenberger58402052008-12-09 23:07:13 -08002185 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 /* must be called only after the slave
2187 * has been detached from the list
2188 */
2189 bond_alb_deinit_slave(bond, slave);
2190 }
2191
Arthur Kepner8531c5f2005-08-23 01:34:53 -04002192 bond_compute_features(bond);
2193
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002194 bond_destroy_slave_symlinks(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 bond_del_vlans_from_slave(bond, slave_dev);
2196
2197 /* If the mode USES_PRIMARY, then we should only remove its
2198 * promisc and mc settings if it was the curr_active_slave, but that was
2199 * already taken care of above when we detached the slave
2200 */
2201 if (!USES_PRIMARY(bond->params.mode)) {
2202 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002203 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
2206 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002207 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002211 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002213 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
2215
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00002216 netdev_rx_handler_unregister(slave_dev);
Jiri Pirko1765a572011-02-12 06:48:36 +00002217 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002219 slave_disable_netpoll(slave);
2220
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 /* close slave before restoring its mac address */
2222 dev_close(slave_dev);
2223
Jay Vosburghdd957c52007-10-09 19:57:24 -07002224 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002225 /* restore original ("permanent") mac address*/
2226 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2227 addr.sa_family = slave_dev->type;
2228 dev_set_mac_address(slave_dev, &addr);
2229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002231 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
2232 IFF_SLAVE_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
2234 kfree(slave);
2235
2236 /* re-acquire the lock before getting the next slave */
2237 write_lock_bh(&bond->lock);
2238 }
2239
2240 /* zero the mac address of the master so it will be
2241 * set by the application to the mac address of the
2242 * first slave
2243 */
2244 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2245
Jay Vosburghf35188f2010-07-21 12:14:47 +00002246 if (!bond->vlgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
Jay Vosburghf35188f2010-07-21 12:14:47 +00002248 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002249 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2250 bond_dev->name, bond_dev->name);
2251 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2252 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 }
2254
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002255 pr_info("%s: released all slaves\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257out:
2258 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 return 0;
2260}
2261
2262/*
2263 * This function changes the active slave to slave <slave_dev>.
2264 * It returns -EINVAL in the following cases.
2265 * - <slave_dev> is not found in the list.
2266 * - There is not active slave now.
2267 * - <slave_dev> is already active.
2268 * - The link state of <slave_dev> is not BOND_LINK_UP.
2269 * - <slave_dev> is not running.
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002270 * In these cases, this function does nothing.
2271 * In the other cases, current_slave pointer is changed and 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 */
2273static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2274{
Wang Chen454d7c92008-11-12 23:37:49 -08002275 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 struct slave *old_active = NULL;
2277 struct slave *new_active = NULL;
2278 int res = 0;
2279
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002280 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283 /* Verify that master_dev is indeed the master of slave_dev */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002284 if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002287 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002289 read_lock(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 old_active = bond->curr_active_slave;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002291 read_unlock(&bond->curr_slave_lock);
2292
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 new_active = bond_get_slave_by_dev(bond, slave_dev);
2294
2295 /*
2296 * Changing to the current active: do nothing; return success.
2297 */
2298 if (new_active && (new_active == old_active)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002299 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return 0;
2301 }
2302
2303 if ((new_active) &&
2304 (old_active) &&
2305 (new_active->link == BOND_LINK_UP) &&
2306 IS_UP(new_active->dev)) {
Neil Hormane843fa52010-10-13 16:01:50 +00002307 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002308 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 bond_change_active_slave(bond, new_active);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002310 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002311 unblock_netpoll_tx();
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002312 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002315 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 return res;
2318}
2319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2321{
Wang Chen454d7c92008-11-12 23:37:49 -08002322 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
2324 info->bond_mode = bond->params.mode;
2325 info->miimon = bond->params.miimon;
2326
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002327 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 info->num_slaves = bond->slave_cnt;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002329 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
2331 return 0;
2332}
2333
2334static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2335{
Wang Chen454d7c92008-11-12 23:37:49 -08002336 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 struct slave *slave;
Eric Dumazet689c96c2009-04-23 03:39:04 +00002338 int i, res = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002340 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342 bond_for_each_slave(bond, slave, i) {
2343 if (i == (int)info->slave_id) {
Eric Dumazet689c96c2009-04-23 03:39:04 +00002344 res = 0;
2345 strcpy(info->slave_name, slave->dev->name);
2346 info->link = slave->link;
2347 info->state = slave->state;
2348 info->link_failure_count = slave->link_failure_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 break;
2350 }
2351 }
2352
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002353 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Eric Dumazet689c96c2009-04-23 03:39:04 +00002355 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356}
2357
2358/*-------------------------------- Monitoring -------------------------------*/
2359
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002360
2361static int bond_miimon_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002363 struct slave *slave;
2364 int i, link_state, commit = 0;
Jiri Pirko41f89102009-04-24 03:57:29 +00002365 bool ignore_updelay;
2366
2367 ignore_updelay = !bond->curr_active_slave ? true : false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
2369 bond_for_each_slave(bond, slave, i) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002370 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002372 link_state = bond_check_dev_link(bond, slave->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
2374 switch (slave->link) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002375 case BOND_LINK_UP:
2376 if (link_state)
2377 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002379 slave->link = BOND_LINK_FAIL;
2380 slave->delay = bond->params.downdelay;
2381 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002382 pr_info("%s: link status down for %sinterface %s, disabling it in %d ms.\n",
2383 bond->dev->name,
2384 (bond->params.mode ==
2385 BOND_MODE_ACTIVEBACKUP) ?
2386 ((slave->state == BOND_STATE_ACTIVE) ?
2387 "active " : "backup ") : "",
2388 slave->dev->name,
2389 bond->params.downdelay * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002391 /*FALLTHRU*/
2392 case BOND_LINK_FAIL:
2393 if (link_state) {
2394 /*
2395 * recovered before downdelay expired
2396 */
2397 slave->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 slave->jiffies = jiffies;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002399 pr_info("%s: link status up again after %d ms for interface %s.\n",
2400 bond->dev->name,
2401 (bond->params.downdelay - slave->delay) *
2402 bond->params.miimon,
2403 slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002404 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002406
2407 if (slave->delay <= 0) {
2408 slave->new_link = BOND_LINK_DOWN;
2409 commit++;
2410 continue;
2411 }
2412
2413 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002416 case BOND_LINK_DOWN:
2417 if (!link_state)
2418 continue;
2419
2420 slave->link = BOND_LINK_BACK;
2421 slave->delay = bond->params.updelay;
2422
2423 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002424 pr_info("%s: link status up for interface %s, enabling it in %d ms.\n",
2425 bond->dev->name, slave->dev->name,
2426 ignore_updelay ? 0 :
2427 bond->params.updelay *
2428 bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002430 /*FALLTHRU*/
2431 case BOND_LINK_BACK:
2432 if (!link_state) {
2433 slave->link = BOND_LINK_DOWN;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002434 pr_info("%s: link status down again after %d ms for interface %s.\n",
2435 bond->dev->name,
2436 (bond->params.updelay - slave->delay) *
2437 bond->params.miimon,
2438 slave->dev->name);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002439
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002440 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002442
Jiri Pirko41f89102009-04-24 03:57:29 +00002443 if (ignore_updelay)
2444 slave->delay = 0;
2445
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002446 if (slave->delay <= 0) {
2447 slave->new_link = BOND_LINK_UP;
2448 commit++;
Jiri Pirko41f89102009-04-24 03:57:29 +00002449 ignore_updelay = false;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002450 continue;
2451 }
2452
2453 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 break;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002455 }
2456 }
2457
2458 return commit;
2459}
2460
2461static void bond_miimon_commit(struct bonding *bond)
2462{
2463 struct slave *slave;
2464 int i;
2465
2466 bond_for_each_slave(bond, slave, i) {
2467 switch (slave->new_link) {
2468 case BOND_LINK_NOCHANGE:
2469 continue;
2470
2471 case BOND_LINK_UP:
2472 slave->link = BOND_LINK_UP;
2473 slave->jiffies = jiffies;
2474
2475 if (bond->params.mode == BOND_MODE_8023AD) {
2476 /* prevent it from being the active one */
2477 slave->state = BOND_STATE_BACKUP;
2478 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2479 /* make it immediately active */
2480 slave->state = BOND_STATE_ACTIVE;
2481 } else if (slave != bond->primary_slave) {
2482 /* prevent it from being the active one */
2483 slave->state = BOND_STATE_BACKUP;
2484 }
2485
Krzysztof Piotr Oledzki546add72010-10-06 14:28:22 -07002486 bond_update_speed_duplex(slave);
2487
Krzysztof Piotr Oledzki700c2a72010-10-06 14:25:06 -07002488 pr_info("%s: link status definitely up for interface %s, %d Mbps %s duplex.\n",
2489 bond->dev->name, slave->dev->name,
2490 slave->speed, slave->duplex ? "full" : "half");
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002491
2492 /* notify ad that the link status has changed */
2493 if (bond->params.mode == BOND_MODE_8023AD)
2494 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2495
Holger Eitzenberger58402052008-12-09 23:07:13 -08002496 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002497 bond_alb_handle_link_change(bond, slave,
2498 BOND_LINK_UP);
2499
2500 if (!bond->curr_active_slave ||
2501 (slave == bond->primary_slave))
2502 goto do_failover;
2503
2504 continue;
2505
2506 case BOND_LINK_DOWN:
Jay Vosburghfba4acd2008-10-30 17:41:14 -07002507 if (slave->link_failure_count < UINT_MAX)
2508 slave->link_failure_count++;
2509
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002510 slave->link = BOND_LINK_DOWN;
2511
2512 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2513 bond->params.mode == BOND_MODE_8023AD)
2514 bond_set_slave_inactive_flags(slave);
2515
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002516 pr_info("%s: link status definitely down for interface %s, disabling it\n",
2517 bond->dev->name, slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002518
2519 if (bond->params.mode == BOND_MODE_8023AD)
2520 bond_3ad_handle_link_change(slave,
2521 BOND_LINK_DOWN);
2522
Jiri Pirkoae63e802009-05-27 05:42:36 +00002523 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002524 bond_alb_handle_link_change(bond, slave,
2525 BOND_LINK_DOWN);
2526
2527 if (slave == bond->curr_active_slave)
2528 goto do_failover;
2529
2530 continue;
2531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002533 pr_err("%s: invalid new link %d on slave %s\n",
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002534 bond->dev->name, slave->new_link,
2535 slave->dev->name);
2536 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002538 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 }
2540
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002541do_failover:
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002542 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00002543 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002544 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 bond_select_active_slave(bond);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002546 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002547 unblock_netpoll_tx();
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002548 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002549
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002550 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551}
2552
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002553/*
2554 * bond_mii_monitor
2555 *
2556 * Really a wrapper that splits the mii monitor into two phases: an
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002557 * inspection, then (if inspection indicates something needs to be done)
2558 * an acquisition of appropriate locks followed by a commit phase to
2559 * implement whatever link state changes are indicated.
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002560 */
2561void bond_mii_monitor(struct work_struct *work)
2562{
2563 struct bonding *bond = container_of(work, struct bonding,
2564 mii_work.work);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002565
2566 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002567 if (bond->kill_timers)
2568 goto out;
2569
2570 if (bond->slave_cnt == 0)
2571 goto re_arm;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002572
2573 if (bond->send_grat_arp) {
2574 read_lock(&bond->curr_slave_lock);
2575 bond_send_gratuitous_arp(bond);
2576 read_unlock(&bond->curr_slave_lock);
2577 }
2578
Brian Haley305d5522008-11-04 17:51:14 -08002579 if (bond->send_unsol_na) {
2580 read_lock(&bond->curr_slave_lock);
2581 bond_send_unsolicited_na(bond);
2582 read_unlock(&bond->curr_slave_lock);
2583 }
2584
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002585 if (bond_miimon_inspect(bond)) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002586 read_unlock(&bond->lock);
2587 rtnl_lock();
2588 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002589
2590 bond_miimon_commit(bond);
2591
Jay Vosburgh56556622008-01-17 16:25:03 -08002592 read_unlock(&bond->lock);
2593 rtnl_unlock(); /* might sleep, hold no other locks */
2594 read_lock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002595 }
2596
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002597re_arm:
2598 if (bond->params.miimon)
2599 queue_delayed_work(bond->wq, &bond->mii_work,
2600 msecs_to_jiffies(bond->params.miimon));
2601out:
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002602 read_unlock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002603}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002604
Al Virod3bb52b2007-08-22 20:06:58 -04002605static __be32 bond_glean_dev_ip(struct net_device *dev)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002606{
2607 struct in_device *idev;
2608 struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07002609 __be32 addr = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002610
2611 if (!dev)
2612 return 0;
2613
2614 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002615 idev = __in_dev_get_rcu(dev);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002616 if (!idev)
2617 goto out;
2618
2619 ifa = idev->ifa_list;
2620 if (!ifa)
2621 goto out;
2622
2623 addr = ifa->ifa_local;
2624out:
2625 rcu_read_unlock();
2626 return addr;
2627}
2628
Al Virod3bb52b2007-08-22 20:06:58 -04002629static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002630{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002631 struct vlan_entry *vlan;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002632
2633 if (ip == bond->master_ip)
2634 return 1;
2635
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002636 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002637 if (ip == vlan->vlan_ip)
2638 return 1;
2639 }
2640
2641 return 0;
2642}
2643
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002644/*
2645 * We go to the (large) trouble of VLAN tagging ARP frames because
2646 * switches in VLAN mode (especially if ports are configured as
2647 * "native" to a VLAN) might not pass non-tagged frames.
2648 */
Al Virod3bb52b2007-08-22 20:06:58 -04002649static 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 -04002650{
2651 struct sk_buff *skb;
2652
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002653 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002654 slave_dev->name, dest_ip, src_ip, vlan_id);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002655
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002656 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2657 NULL, slave_dev->dev_addr, NULL);
2658
2659 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002660 pr_err("ARP packet allocation failed\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002661 return;
2662 }
2663 if (vlan_id) {
2664 skb = vlan_put_tag(skb, vlan_id);
2665 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002666 pr_err("failed to insert VLAN tag\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002667 return;
2668 }
2669 }
2670 arp_xmit(skb);
2671}
2672
2673
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2675{
David S. Millerb23dd4f2011-03-02 14:31:35 -08002676 int i, vlan_id;
Al Virod3bb52b2007-08-22 20:06:58 -04002677 __be32 *targets = bond->params.arp_targets;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002678 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002679 struct net_device *vlan_dev;
2680 struct flowi fl;
2681 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Mitch Williams6b780562005-11-09 10:36:19 -08002683 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2684 if (!targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07002685 break;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002686 pr_debug("basa: target %x\n", targets[i]);
Jay Vosburghf35188f2010-07-21 12:14:47 +00002687 if (!bond->vlgrp) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002688 pr_debug("basa: empty vlan: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002689 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2690 bond->master_ip, 0);
2691 continue;
2692 }
2693
2694 /*
2695 * If VLANs are configured, we do a route lookup to
2696 * determine which VLAN interface would be used, so we
2697 * can tag the ARP with the proper VLAN tag.
2698 */
2699 memset(&fl, 0, sizeof(fl));
2700 fl.fl4_dst = targets[i];
2701 fl.fl4_tos = RTO_ONLINK;
2702
David S. Millerb23dd4f2011-03-02 14:31:35 -08002703 rt = ip_route_output_key(dev_net(bond->dev), &fl);
2704 if (IS_ERR(rt)) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002705 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002706 pr_warning("%s: no route to arp_ip_target %pI4\n",
2707 bond->dev->name, &fl.fl4_dst);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002708 }
2709 continue;
2710 }
2711
2712 /*
2713 * This target is not on a VLAN
2714 */
Changli Gaod8d1f302010-06-10 23:31:35 -07002715 if (rt->dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002716 ip_rt_put(rt);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002717 pr_debug("basa: rtdev == bond->dev: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002718 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2719 bond->master_ip, 0);
2720 continue;
2721 }
2722
2723 vlan_id = 0;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002724 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002725 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Changli Gaod8d1f302010-06-10 23:31:35 -07002726 if (vlan_dev == rt->dst.dev) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002727 vlan_id = vlan->vlan_id;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002728 pr_debug("basa: vlan match on %s %d\n",
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002729 vlan_dev->name, vlan_id);
2730 break;
2731 }
2732 }
2733
2734 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002735 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002736 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2737 vlan->vlan_ip, vlan_id);
2738 continue;
2739 }
2740
2741 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002742 pr_warning("%s: no path to arp_ip_target %pI4 via rt.dev %s\n",
2743 bond->dev->name, &fl.fl4_dst,
Changli Gaod8d1f302010-06-10 23:31:35 -07002744 rt->dst.dev ? rt->dst.dev->name : "NULL");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002745 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002746 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002747 }
2748}
2749
2750/*
2751 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2752 * for each VLAN above us.
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002753 *
2754 * Caller must hold curr_slave_lock for read or better
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002755 */
2756static void bond_send_gratuitous_arp(struct bonding *bond)
2757{
2758 struct slave *slave = bond->curr_active_slave;
2759 struct vlan_entry *vlan;
2760 struct net_device *vlan_dev;
2761
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002762 pr_debug("bond_send_grat_arp: bond %s slave %s\n",
2763 bond->dev->name, slave ? slave->dev->name : "NULL");
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002764
2765 if (!slave || !bond->send_grat_arp ||
2766 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002767 return;
2768
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002769 bond->send_grat_arp--;
2770
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002771 if (bond->master_ip) {
2772 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
Moni Shoua1053f622007-10-09 19:43:42 -07002773 bond->master_ip, 0);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002774 }
2775
Jay Vosburghf35188f2010-07-21 12:14:47 +00002776 if (!bond->vlgrp)
2777 return;
2778
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002779 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002780 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002781 if (vlan->vlan_ip) {
2782 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2783 vlan->vlan_ip, vlan->vlan_id);
2784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 }
2786}
2787
Al Virod3bb52b2007-08-22 20:06:58 -04002788static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002789{
2790 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002791 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002792
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002793 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002794 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002795 &sip, &tip, i, &targets[i],
2796 bond_has_this_ip(bond, tip));
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002797 if (sip == targets[i]) {
2798 if (bond_has_this_ip(bond, tip))
2799 slave->last_arp_rx = jiffies;
2800 return;
2801 }
2802 }
2803}
2804
2805static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2806{
2807 struct arphdr *arp;
2808 struct slave *slave;
2809 struct bonding *bond;
2810 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002811 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002812
Andy Gospodarek1f3c8802009-12-14 10:48:58 +00002813 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2814 /*
2815 * When using VLANS and bonding, dev and oriv_dev may be
2816 * incorrect if the physical interface supports VLAN
2817 * acceleration. With this change ARP validation now
2818 * works for hosts only reachable on the VLAN interface.
2819 */
2820 dev = vlan_dev_real_dev(dev);
2821 orig_dev = dev_get_by_index_rcu(dev_net(skb->dev),skb->skb_iif);
2822 }
2823
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002824 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2825 goto out;
2826
Wang Chen454d7c92008-11-12 23:37:49 -08002827 bond = netdev_priv(dev);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002828 read_lock(&bond->lock);
2829
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002830 pr_debug("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002831 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2832 orig_dev ? orig_dev->name : "NULL");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002833
2834 slave = bond_get_slave_by_dev(bond, orig_dev);
2835 if (!slave || !slave_do_arp_validate(bond, slave))
2836 goto out_unlock;
2837
Neil Hormanb3053252011-01-20 09:02:31 +00002838 skb = skb_share_check(skb, GFP_ATOMIC);
2839 if (!skb)
2840 goto out_unlock;
2841
Pavel Emelyanov988b7052008-03-03 12:20:57 -08002842 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002843 goto out_unlock;
2844
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002845 arp = arp_hdr(skb);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002846 if (arp->ar_hln != dev->addr_len ||
2847 skb->pkt_type == PACKET_OTHERHOST ||
2848 skb->pkt_type == PACKET_LOOPBACK ||
2849 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2850 arp->ar_pro != htons(ETH_P_IP) ||
2851 arp->ar_pln != 4)
2852 goto out_unlock;
2853
2854 arp_ptr = (unsigned char *)(arp + 1);
2855 arp_ptr += dev->addr_len;
2856 memcpy(&sip, arp_ptr, 4);
2857 arp_ptr += 4 + dev->addr_len;
2858 memcpy(&tip, arp_ptr, 4);
2859
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002860 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002861 bond->dev->name, slave->dev->name, slave->state,
2862 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2863 &sip, &tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002864
2865 /*
2866 * Backup slaves won't see the ARP reply, but do come through
2867 * here for each ARP probe (so we swap the sip/tip to validate
2868 * the probe). In a "redundant switch, common router" type of
2869 * configuration, the ARP probe will (hopefully) travel from
2870 * the active, through one switch, the router, then the other
2871 * switch before reaching the backup.
2872 */
2873 if (slave->state == BOND_STATE_ACTIVE)
2874 bond_validate_arp(bond, slave, sip, tip);
2875 else
2876 bond_validate_arp(bond, slave, tip, sip);
2877
2878out_unlock:
2879 read_unlock(&bond->lock);
2880out:
2881 dev_kfree_skb(skb);
2882 return NET_RX_SUCCESS;
2883}
2884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885/*
2886 * this function is called regularly to monitor each slave's link
2887 * ensuring that traffic is being sent and received when arp monitoring
2888 * is used in load-balancing mode. if the adapter has been dormant, then an
2889 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2890 * arp monitoring in active backup mode.
2891 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002892void bond_loadbalance_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002894 struct bonding *bond = container_of(work, struct bonding,
2895 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 struct slave *slave, *oldcurrent;
2897 int do_failover = 0;
2898 int delta_in_ticks;
2899 int i;
2900
2901 read_lock(&bond->lock);
2902
Jay Vosburgh5ce0da82008-05-17 21:10:07 -07002903 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002905 if (bond->kill_timers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002908 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
2911 read_lock(&bond->curr_slave_lock);
2912 oldcurrent = bond->curr_active_slave;
2913 read_unlock(&bond->curr_slave_lock);
2914
2915 /* see if any of the previous devices are up now (i.e. they have
2916 * xmt and rcv traffic). the curr_active_slave does not come into
2917 * the picture unless it is null. also, slave->jiffies is not needed
2918 * here because we send an arp on each slave and give a slave as
2919 * long as it needs to get the tx/rx within the delta.
2920 * TODO: what about up/down delay in arp mode? it wasn't here before
2921 * so it can wait
2922 */
2923 bond_for_each_slave(bond, slave, i) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002924 unsigned long trans_start = dev_trans_start(slave->dev);
2925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002927 if (time_in_range(jiffies,
2928 trans_start - delta_in_ticks,
2929 trans_start + delta_in_ticks) &&
2930 time_in_range(jiffies,
2931 slave->dev->last_rx - delta_in_ticks,
2932 slave->dev->last_rx + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
2934 slave->link = BOND_LINK_UP;
2935 slave->state = BOND_STATE_ACTIVE;
2936
2937 /* primary_slave has no meaning in round-robin
2938 * mode. the window of a slave being up and
2939 * curr_active_slave being null after enslaving
2940 * is closed.
2941 */
2942 if (!oldcurrent) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002943 pr_info("%s: link status definitely up for interface %s, ",
2944 bond->dev->name,
2945 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 do_failover = 1;
2947 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002948 pr_info("%s: interface %s is now up\n",
2949 bond->dev->name,
2950 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 }
2952 }
2953 } else {
2954 /* slave->link == BOND_LINK_UP */
2955
2956 /* not all switches will respond to an arp request
2957 * when the source ip is 0, so don't take the link down
2958 * if we don't know our ip yet
2959 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002960 if (!time_in_range(jiffies,
2961 trans_start - delta_in_ticks,
2962 trans_start + 2 * delta_in_ticks) ||
2963 !time_in_range(jiffies,
2964 slave->dev->last_rx - delta_in_ticks,
2965 slave->dev->last_rx + 2 * delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
2967 slave->link = BOND_LINK_DOWN;
2968 slave->state = BOND_STATE_BACKUP;
2969
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002970 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002973 pr_info("%s: interface %s is now down.\n",
2974 bond->dev->name,
2975 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002977 if (slave == oldcurrent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 do_failover = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 }
2980 }
2981
2982 /* note: if switch is in round-robin mode, all links
2983 * must tx arp to ensure all links rx an arp - otherwise
2984 * links may oscillate or not come up at all; if switch is
2985 * in something like xor mode, there is nothing we can
2986 * do - all replies will be rx'ed on same link causing slaves
2987 * to be unstable during low/no traffic periods
2988 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002989 if (IS_UP(slave->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 bond_arp_send_all(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 }
2992
2993 if (do_failover) {
Neil Hormane843fa52010-10-13 16:01:50 +00002994 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002995 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
2997 bond_select_active_slave(bond);
2998
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002999 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00003000 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 }
3002
3003re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003004 if (bond->params.arp_interval)
3005 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006out:
3007 read_unlock(&bond->lock);
3008}
3009
3010/*
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003011 * Called to inspect slaves for active-backup mode ARP monitor link state
3012 * changes. Sets new_link in slaves to specify what action should take
3013 * place for the slave. Returns 0 if no changes are found, >0 if changes
3014 * to link states must be committed.
3015 *
3016 * Called with bond->lock held for read.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003018static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 struct slave *slave;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003021 int i, commit = 0;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003022 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 bond_for_each_slave(bond, slave, i) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003025 slave->new_link = BOND_LINK_NOCHANGE;
3026
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003028 if (time_in_range(jiffies,
3029 slave_last_rx(bond, slave) - delta_in_ticks,
3030 slave_last_rx(bond, slave) + delta_in_ticks)) {
3031
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003032 slave->new_link = BOND_LINK_UP;
3033 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003036 continue;
3037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003039 /*
3040 * Give slaves 2*delta after being enslaved or made
3041 * active. This avoids bouncing, as the last receive
3042 * times need a full ARP monitor cycle to be updated.
3043 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003044 if (time_in_range(jiffies,
3045 slave->jiffies - delta_in_ticks,
3046 slave->jiffies + 2 * delta_in_ticks))
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003047 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003049 /*
3050 * Backup slave is down if:
3051 * - No current_arp_slave AND
3052 * - more than 3*delta since last receive AND
3053 * - the bond has an IP address
3054 *
3055 * Note: a non-null current_arp_slave indicates
3056 * the curr_active_slave went down and we are
3057 * searching for a new one; under this condition
3058 * we only take the curr_active_slave down - this
3059 * gives each slave a chance to tx/rx traffic
3060 * before being taken out
3061 */
3062 if (slave->state == BOND_STATE_BACKUP &&
3063 !bond->current_arp_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003064 !time_in_range(jiffies,
3065 slave_last_rx(bond, slave) - delta_in_ticks,
3066 slave_last_rx(bond, slave) + 3 * delta_in_ticks)) {
3067
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003068 slave->new_link = BOND_LINK_DOWN;
3069 commit++;
3070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003072 /*
3073 * Active slave is down if:
3074 * - more than 2*delta since transmitting OR
3075 * - (more than 2*delta since receive AND
3076 * the bond has an IP address)
3077 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003078 trans_start = dev_trans_start(slave->dev);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003079 if ((slave->state == BOND_STATE_ACTIVE) &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003080 (!time_in_range(jiffies,
3081 trans_start - delta_in_ticks,
3082 trans_start + 2 * delta_in_ticks) ||
3083 !time_in_range(jiffies,
3084 slave_last_rx(bond, slave) - delta_in_ticks,
3085 slave_last_rx(bond, slave) + 2 * delta_in_ticks))) {
3086
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003087 slave->new_link = BOND_LINK_DOWN;
3088 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 }
3090 }
3091
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003092 return commit;
3093}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003095/*
3096 * Called to commit link state changes noted by inspection step of
3097 * active-backup mode ARP monitor.
3098 *
3099 * Called with RTNL and bond->lock for read.
3100 */
3101static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
3102{
3103 struct slave *slave;
3104 int i;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003105 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003107 bond_for_each_slave(bond, slave, i) {
3108 switch (slave->new_link) {
3109 case BOND_LINK_NOCHANGE:
3110 continue;
3111
3112 case BOND_LINK_UP:
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003113 trans_start = dev_trans_start(slave->dev);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003114 if ((!bond->curr_active_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003115 time_in_range(jiffies,
3116 trans_start - delta_in_ticks,
3117 trans_start + delta_in_ticks)) ||
Jiri Pirkob9f60252009-08-31 11:09:38 +00003118 bond->curr_active_slave != slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003119 slave->link = BOND_LINK_UP;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003120 bond->current_arp_slave = NULL;
3121
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003122 pr_info("%s: link status definitely up for interface %s.\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003123 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003124
Jiri Pirkob9f60252009-08-31 11:09:38 +00003125 if (!bond->curr_active_slave ||
3126 (slave == bond->primary_slave))
3127 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003128
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003129 }
3130
Jiri Pirkob9f60252009-08-31 11:09:38 +00003131 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003132
3133 case BOND_LINK_DOWN:
3134 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003137 slave->link = BOND_LINK_DOWN;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003138 bond_set_slave_inactive_flags(slave);
3139
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003140 pr_info("%s: link status definitely down for interface %s, disabling it\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003141 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003142
3143 if (slave == bond->curr_active_slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003144 bond->current_arp_slave = NULL;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003145 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003146 }
Jiri Pirkob9f60252009-08-31 11:09:38 +00003147
3148 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003149
3150 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003151 pr_err("%s: impossible: new_link %d on slave %s\n",
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003152 bond->dev->name, slave->new_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 slave->dev->name);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003154 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Jiri Pirkob9f60252009-08-31 11:09:38 +00003157do_failover:
3158 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00003159 block_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003160 write_lock_bh(&bond->curr_slave_lock);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003161 bond_select_active_slave(bond);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003162 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00003163 unblock_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003164 }
3165
3166 bond_set_carrier(bond);
3167}
3168
3169/*
3170 * Send ARP probes for active-backup mode ARP monitor.
3171 *
3172 * Called with bond->lock held for read.
3173 */
3174static void bond_ab_arp_probe(struct bonding *bond)
3175{
3176 struct slave *slave;
3177 int i;
3178
3179 read_lock(&bond->curr_slave_lock);
3180
3181 if (bond->current_arp_slave && bond->curr_active_slave)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003182 pr_info("PROBE: c_arp %s && cas %s BAD\n",
3183 bond->current_arp_slave->dev->name,
3184 bond->curr_active_slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003185
3186 if (bond->curr_active_slave) {
3187 bond_arp_send_all(bond, bond->curr_active_slave);
3188 read_unlock(&bond->curr_slave_lock);
3189 return;
3190 }
3191
3192 read_unlock(&bond->curr_slave_lock);
3193
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 /* if we don't have a curr_active_slave, search for the next available
3195 * backup slave from the current_arp_slave and make it the candidate
3196 * for becoming the curr_active_slave
3197 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003198
3199 if (!bond->current_arp_slave) {
3200 bond->current_arp_slave = bond->first_slave;
3201 if (!bond->current_arp_slave)
3202 return;
3203 }
3204
3205 bond_set_slave_inactive_flags(bond->current_arp_slave);
3206
3207 /* search for next candidate */
3208 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3209 if (IS_UP(slave->dev)) {
3210 slave->link = BOND_LINK_BACK;
3211 bond_set_slave_active_flags(slave);
3212 bond_arp_send_all(bond, slave);
3213 slave->jiffies = jiffies;
3214 bond->current_arp_slave = slave;
3215 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 }
3217
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003218 /* if the link state is up at this point, we
3219 * mark it down - this can happen if we have
3220 * simultaneous link failures and
3221 * reselect_active_interface doesn't make this
3222 * one the current slave so it is still marked
3223 * up when it is actually down
3224 */
3225 if (slave->link == BOND_LINK_UP) {
3226 slave->link = BOND_LINK_DOWN;
3227 if (slave->link_failure_count < UINT_MAX)
3228 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003230 bond_set_slave_inactive_flags(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003232 pr_info("%s: backup interface %s is now down.\n",
3233 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 }
3235 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003236}
3237
3238void bond_activebackup_arp_mon(struct work_struct *work)
3239{
3240 struct bonding *bond = container_of(work, struct bonding,
3241 arp_work.work);
3242 int delta_in_ticks;
3243
3244 read_lock(&bond->lock);
3245
3246 if (bond->kill_timers)
3247 goto out;
3248
3249 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3250
3251 if (bond->slave_cnt == 0)
3252 goto re_arm;
3253
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003254 if (bond->send_grat_arp) {
3255 read_lock(&bond->curr_slave_lock);
3256 bond_send_gratuitous_arp(bond);
3257 read_unlock(&bond->curr_slave_lock);
3258 }
3259
Brian Haley305d5522008-11-04 17:51:14 -08003260 if (bond->send_unsol_na) {
3261 read_lock(&bond->curr_slave_lock);
3262 bond_send_unsolicited_na(bond);
3263 read_unlock(&bond->curr_slave_lock);
3264 }
3265
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003266 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3267 read_unlock(&bond->lock);
3268 rtnl_lock();
3269 read_lock(&bond->lock);
3270
3271 bond_ab_arp_commit(bond, delta_in_ticks);
3272
3273 read_unlock(&bond->lock);
3274 rtnl_unlock();
3275 read_lock(&bond->lock);
3276 }
3277
3278 bond_ab_arp_probe(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
3280re_arm:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003281 if (bond->params.arp_interval)
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003282 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283out:
3284 read_unlock(&bond->lock);
3285}
3286
3287/*------------------------------ proc/seq_file-------------------------------*/
3288
3289#ifdef CONFIG_PROC_FS
3290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazete4a7b932010-10-29 01:52:46 +00003292 __acquires(RCU)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00003293 __acquires(&bond->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294{
3295 struct bonding *bond = seq->private;
3296 loff_t off = 0;
3297 struct slave *slave;
3298 int i;
3299
3300 /* make sure the bond won't be taken away */
Eric Dumazete4a7b932010-10-29 01:52:46 +00003301 rcu_read_lock();
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003302 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003304 if (*pos == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 return SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
3307 bond_for_each_slave(bond, slave, i) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003308 if (++off == *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 return slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 }
3311
3312 return NULL;
3313}
3314
3315static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3316{
3317 struct bonding *bond = seq->private;
3318 struct slave *slave = v;
3319
3320 ++*pos;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003321 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 return bond->first_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
3324 slave = slave->next;
3325
3326 return (slave == bond->first_slave) ? NULL : slave;
3327}
3328
3329static void bond_info_seq_stop(struct seq_file *seq, void *v)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00003330 __releases(&bond->lock)
Eric Dumazete4a7b932010-10-29 01:52:46 +00003331 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332{
3333 struct bonding *bond = seq->private;
3334
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003335 read_unlock(&bond->lock);
Eric Dumazete4a7b932010-10-29 01:52:46 +00003336 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337}
3338
3339static void bond_info_show_master(struct seq_file *seq)
3340{
3341 struct bonding *bond = seq->private;
3342 struct slave *curr;
Mitch Williams4756b022005-11-09 10:36:25 -08003343 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
3345 read_lock(&bond->curr_slave_lock);
3346 curr = bond->curr_active_slave;
3347 read_unlock(&bond->curr_slave_lock);
3348
Jay Vosburghdd957c52007-10-09 19:57:24 -07003349 seq_printf(seq, "Bonding Mode: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 bond_mode_name(bond->params.mode));
3351
Jay Vosburghdd957c52007-10-09 19:57:24 -07003352 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP &&
3353 bond->params.fail_over_mac)
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07003354 seq_printf(seq, " (fail_over_mac %s)",
3355 fail_over_mac_tbl[bond->params.fail_over_mac].modename);
Jay Vosburghdd957c52007-10-09 19:57:24 -07003356
3357 seq_printf(seq, "\n");
3358
Mitch Williamsc61b75a2005-11-09 10:35:13 -08003359 if (bond->params.mode == BOND_MODE_XOR ||
3360 bond->params.mode == BOND_MODE_8023AD) {
3361 seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
3362 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
3363 bond->params.xmit_policy);
3364 }
3365
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 if (USES_PRIMARY(bond->params.mode)) {
Jiri Pirkoa5499522009-09-25 03:28:09 +00003367 seq_printf(seq, "Primary Slave: %s",
Mitch Williams0f418b22005-11-09 10:35:21 -08003368 (bond->primary_slave) ?
3369 bond->primary_slave->dev->name : "None");
Jiri Pirkoa5499522009-09-25 03:28:09 +00003370 if (bond->primary_slave)
3371 seq_printf(seq, " (primary_reselect %s)",
3372 pri_reselect_tbl[bond->params.primary_reselect].modename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
Jiri Pirkoa5499522009-09-25 03:28:09 +00003374 seq_printf(seq, "\nCurrently Active Slave: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 (curr) ? curr->dev->name : "None");
3376 }
3377
Jay Vosburghff59c452006-03-27 13:27:43 -08003378 seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
3379 "up" : "down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
3381 seq_printf(seq, "Up Delay (ms): %d\n",
3382 bond->params.updelay * bond->params.miimon);
3383 seq_printf(seq, "Down Delay (ms): %d\n",
3384 bond->params.downdelay * bond->params.miimon);
3385
Mitch Williams4756b022005-11-09 10:36:25 -08003386
3387 /* ARP information */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003388 if (bond->params.arp_interval > 0) {
3389 int printed = 0;
Mitch Williams4756b022005-11-09 10:36:25 -08003390 seq_printf(seq, "ARP Polling Interval (ms): %d\n",
3391 bond->params.arp_interval);
3392
3393 seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
3394
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003395 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
Mitch Williams4756b022005-11-09 10:36:25 -08003396 if (!bond->params.arp_targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07003397 break;
Mitch Williams4756b022005-11-09 10:36:25 -08003398 if (printed)
3399 seq_printf(seq, ",");
Harvey Harrison8cf14e32008-10-29 22:43:33 -07003400 seq_printf(seq, " %pI4", &bond->params.arp_targets[i]);
Mitch Williams4756b022005-11-09 10:36:25 -08003401 printed = 1;
3402 }
3403 seq_printf(seq, "\n");
3404 }
3405
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 if (bond->params.mode == BOND_MODE_8023AD) {
3407 struct ad_info ad_info;
3408
3409 seq_puts(seq, "\n802.3ad info\n");
3410 seq_printf(seq, "LACP rate: %s\n",
3411 (bond->params.lacp_fast) ? "fast" : "slow");
Jay Vosburghfd989c82008-11-04 17:51:16 -08003412 seq_printf(seq, "Aggregator selection policy (ad_select): %s\n",
3413 ad_select_tbl[bond->params.ad_select].modename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
3415 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3416 seq_printf(seq, "bond %s has no active aggregator\n",
3417 bond->dev->name);
3418 } else {
3419 seq_printf(seq, "Active Aggregator Info:\n");
3420
3421 seq_printf(seq, "\tAggregator ID: %d\n",
3422 ad_info.aggregator_id);
3423 seq_printf(seq, "\tNumber of ports: %d\n",
3424 ad_info.ports);
3425 seq_printf(seq, "\tActor Key: %d\n",
3426 ad_info.actor_key);
3427 seq_printf(seq, "\tPartner Key: %d\n",
3428 ad_info.partner_key);
Johannes Berge1749612008-10-27 15:59:26 -07003429 seq_printf(seq, "\tPartner Mac Address: %pM\n",
3430 ad_info.partner_system);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 }
3432 }
3433}
3434
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003435static void bond_info_show_slave(struct seq_file *seq,
3436 const struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437{
3438 struct bonding *bond = seq->private;
3439
3440 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3441 seq_printf(seq, "MII Status: %s\n",
3442 (slave->link == BOND_LINK_UP) ? "up" : "down");
Krzysztof Oledzkidd53df22010-09-30 06:19:04 +00003443 seq_printf(seq, "Speed: %d Mbps\n", slave->speed);
3444 seq_printf(seq, "Duplex: %s\n", slave->duplex ? "full" : "half");
Kenzo Iwami655096452006-09-22 21:53:08 -07003445 seq_printf(seq, "Link Failure Count: %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 slave->link_failure_count);
3447
Johannes Berge1749612008-10-27 15:59:26 -07003448 seq_printf(seq, "Permanent HW addr: %pM\n", slave->perm_hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
3450 if (bond->params.mode == BOND_MODE_8023AD) {
3451 const struct aggregator *agg
3452 = SLAVE_AD_INFO(slave).port.aggregator;
3453
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003454 if (agg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 seq_printf(seq, "Aggregator ID: %d\n",
3456 agg->aggregator_identifier);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003457 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 seq_puts(seq, "Aggregator ID: N/A\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 }
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00003460 seq_printf(seq, "Slave queue ID: %d\n", slave->queue_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461}
3462
3463static int bond_info_seq_show(struct seq_file *seq, void *v)
3464{
3465 if (v == SEQ_START_TOKEN) {
3466 seq_printf(seq, "%s\n", version);
3467 bond_info_show_master(seq);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003468 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 bond_info_show_slave(seq, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
3471 return 0;
3472}
3473
Jan Engelhardt4101dec2009-01-14 13:52:18 -08003474static const struct seq_operations bond_info_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 .start = bond_info_seq_start,
3476 .next = bond_info_seq_next,
3477 .stop = bond_info_seq_stop,
3478 .show = bond_info_seq_show,
3479};
3480
3481static int bond_info_open(struct inode *inode, struct file *file)
3482{
3483 struct seq_file *seq;
3484 struct proc_dir_entry *proc;
3485 int res;
3486
3487 res = seq_open(file, &bond_info_seq_ops);
3488 if (!res) {
3489 /* recover the pointer buried in proc_dir_entry data */
3490 seq = file->private_data;
3491 proc = PDE(inode);
3492 seq->private = proc->data;
3493 }
3494
3495 return res;
3496}
3497
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08003498static const struct file_operations bond_info_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 .owner = THIS_MODULE,
3500 .open = bond_info_open,
3501 .read = seq_read,
3502 .llseek = seq_lseek,
3503 .release = seq_release,
3504};
3505
Nicolas de Pesloüan38fc0022009-10-13 00:45:06 -07003506static void bond_create_proc_entry(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507{
3508 struct net_device *bond_dev = bond->dev;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003509 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003511 if (bn->proc_dir) {
Denis V. Luneva95609c2008-04-29 01:02:29 -07003512 bond->proc_entry = proc_create_data(bond_dev->name,
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003513 S_IRUGO, bn->proc_dir,
Denis V. Luneva95609c2008-04-29 01:02:29 -07003514 &bond_info_fops, bond);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003515 if (bond->proc_entry == NULL)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003516 pr_warning("Warning: Cannot create /proc/net/%s/%s\n",
3517 DRV_NAME, bond_dev->name);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003518 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521}
3522
3523static void bond_remove_proc_entry(struct bonding *bond)
3524{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003525 struct net_device *bond_dev = bond->dev;
3526 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
3527
3528 if (bn->proc_dir && bond->proc_entry) {
3529 remove_proc_entry(bond->proc_file_name, bn->proc_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 memset(bond->proc_file_name, 0, IFNAMSIZ);
3531 bond->proc_entry = NULL;
3532 }
3533}
3534
3535/* Create the bonding directory under /proc/net, if doesn't exist yet.
3536 * Caller must hold rtnl_lock.
3537 */
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003538static void __net_init bond_create_proc_dir(struct bond_net *bn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003540 if (!bn->proc_dir) {
3541 bn->proc_dir = proc_mkdir(DRV_NAME, bn->net->proc_net);
3542 if (!bn->proc_dir)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003543 pr_warning("Warning: cannot create /proc/net/%s\n",
3544 DRV_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 }
3546}
3547
3548/* Destroy the bonding directory under /proc/net, if empty.
3549 * Caller must hold rtnl_lock.
3550 */
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003551static void __net_exit bond_destroy_proc_dir(struct bond_net *bn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003553 if (bn->proc_dir) {
3554 remove_proc_entry(DRV_NAME, bn->net->proc_net);
3555 bn->proc_dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 }
3557}
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003558
3559#else /* !CONFIG_PROC_FS */
3560
Nicolas de Pesloüan38fc0022009-10-13 00:45:06 -07003561static void bond_create_proc_entry(struct bonding *bond)
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003562{
3563}
3564
3565static void bond_remove_proc_entry(struct bonding *bond)
3566{
3567}
3568
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003569static inline void bond_create_proc_dir(struct bond_net *bn)
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003570{
3571}
3572
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003573static inline void bond_destroy_proc_dir(struct bond_net *bn)
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003574{
3575}
3576
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577#endif /* CONFIG_PROC_FS */
3578
Jiri Pirkoaee64fa2009-05-05 06:20:51 +00003579
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580/*-------------------------- netdev event handling --------------------------*/
3581
3582/*
3583 * Change device name
3584 */
3585static int bond_event_changename(struct bonding *bond)
3586{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 bond_remove_proc_entry(bond);
3588 bond_create_proc_entry(bond);
Stephen Hemminger7e083842009-06-12 19:02:46 +00003589
Taku Izumif073c7c2010-12-09 15:17:13 +00003590 bond_debug_reregister(bond);
3591
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 return NOTIFY_DONE;
3593}
3594
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003595static int bond_master_netdev_event(unsigned long event,
3596 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597{
Wang Chen454d7c92008-11-12 23:37:49 -08003598 struct bonding *event_bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
3600 switch (event) {
3601 case NETDEV_CHANGENAME:
3602 return bond_event_changename(event_bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 default:
3604 break;
3605 }
3606
3607 return NOTIFY_DONE;
3608}
3609
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003610static int bond_slave_netdev_event(unsigned long event,
3611 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612{
3613 struct net_device *bond_dev = slave_dev->master;
Wang Chen454d7c92008-11-12 23:37:49 -08003614 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
3616 switch (event) {
3617 case NETDEV_UNREGISTER:
3618 if (bond_dev) {
Jay Vosburgh1284cd32007-10-15 16:44:27 -07003619 if (bond->setup_by_slave)
3620 bond_release_and_destroy(bond_dev, slave_dev);
3621 else
3622 bond_release(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 }
3624 break;
3625 case NETDEV_CHANGE:
Jay Vosburgh17d04502009-03-18 18:38:25 -07003626 if (bond->params.mode == BOND_MODE_8023AD || bond_is_lb(bond)) {
3627 struct slave *slave;
3628
3629 slave = bond_get_slave_by_dev(bond, slave_dev);
3630 if (slave) {
3631 u16 old_speed = slave->speed;
3632 u16 old_duplex = slave->duplex;
3633
3634 bond_update_speed_duplex(slave);
3635
3636 if (bond_is_lb(bond))
3637 break;
3638
3639 if (old_speed != slave->speed)
3640 bond_3ad_adapter_speed_changed(slave);
3641 if (old_duplex != slave->duplex)
3642 bond_3ad_adapter_duplex_changed(slave);
3643 }
3644 }
3645
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 break;
3647 case NETDEV_DOWN:
3648 /*
3649 * ... Or is it this?
3650 */
3651 break;
3652 case NETDEV_CHANGEMTU:
3653 /*
3654 * TODO: Should slaves be allowed to
3655 * independently alter their MTU? For
3656 * an active-backup bond, slaves need
3657 * not be the same type of device, so
3658 * MTUs may vary. For other modes,
3659 * slaves arguably should have the
3660 * same MTUs. To do this, we'd need to
3661 * take over the slave's change_mtu
3662 * function for the duration of their
3663 * servitude.
3664 */
3665 break;
3666 case NETDEV_CHANGENAME:
3667 /*
3668 * TODO: handle changing the primary's name
3669 */
3670 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003671 case NETDEV_FEAT_CHANGE:
3672 bond_compute_features(bond);
3673 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 default:
3675 break;
3676 }
3677
3678 return NOTIFY_DONE;
3679}
3680
3681/*
3682 * bond_netdev_event: handle netdev notifier chain events.
3683 *
3684 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003685 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 * locks for us to safely manipulate the slave devices (RTNL lock,
3687 * dev_probe_lock).
3688 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003689static int bond_netdev_event(struct notifier_block *this,
3690 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691{
3692 struct net_device *event_dev = (struct net_device *)ptr;
3693
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003694 pr_debug("event_dev: %s, event: %lx\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003695 event_dev ? event_dev->name : "None",
3696 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003698 if (!(event_dev->priv_flags & IFF_BONDING))
3699 return NOTIFY_DONE;
3700
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 if (event_dev->flags & IFF_MASTER) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003702 pr_debug("IFF_MASTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 return bond_master_netdev_event(event, event_dev);
3704 }
3705
3706 if (event_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003707 pr_debug("IFF_SLAVE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 return bond_slave_netdev_event(event, event_dev);
3709 }
3710
3711 return NOTIFY_DONE;
3712}
3713
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003714/*
3715 * bond_inetaddr_event: handle inetaddr notifier chain events.
3716 *
3717 * We keep track of device IPs primarily to use as source addresses in
3718 * ARP monitor probes (rather than spewing out broadcasts all the time).
3719 *
3720 * We track one IP for the main device (if it has one), plus one per VLAN.
3721 */
3722static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3723{
3724 struct in_ifaddr *ifa = ptr;
3725 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003726 struct bond_net *bn = net_generic(dev_net(event_dev), bond_net_id);
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003727 struct bonding *bond;
3728 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003729
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003730 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003731 if (bond->dev == event_dev) {
3732 switch (event) {
3733 case NETDEV_UP:
3734 bond->master_ip = ifa->ifa_local;
3735 return NOTIFY_OK;
3736 case NETDEV_DOWN:
3737 bond->master_ip = bond_glean_dev_ip(bond->dev);
3738 return NOTIFY_OK;
3739 default:
3740 return NOTIFY_DONE;
3741 }
3742 }
3743
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003744 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +00003745 if (!bond->vlgrp)
3746 continue;
Dan Aloni5c15bde2007-03-02 20:44:51 -08003747 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003748 if (vlan_dev == event_dev) {
3749 switch (event) {
3750 case NETDEV_UP:
3751 vlan->vlan_ip = ifa->ifa_local;
3752 return NOTIFY_OK;
3753 case NETDEV_DOWN:
3754 vlan->vlan_ip =
3755 bond_glean_dev_ip(vlan_dev);
3756 return NOTIFY_OK;
3757 default:
3758 return NOTIFY_DONE;
3759 }
3760 }
3761 }
3762 }
3763 return NOTIFY_DONE;
3764}
3765
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766static struct notifier_block bond_netdev_notifier = {
3767 .notifier_call = bond_netdev_event,
3768};
3769
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003770static struct notifier_block bond_inetaddr_notifier = {
3771 .notifier_call = bond_inetaddr_event,
3772};
3773
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774/*-------------------------- Packet type handling ---------------------------*/
3775
3776/* register to receive lacpdus on a bond */
3777static void bond_register_lacpdu(struct bonding *bond)
3778{
3779 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3780
3781 /* initialize packet type */
3782 pk_type->type = PKT_TYPE_LACPDU;
3783 pk_type->dev = bond->dev;
3784 pk_type->func = bond_3ad_lacpdu_recv;
3785
3786 dev_add_pack(pk_type);
3787}
3788
3789/* unregister to receive lacpdus on a bond */
3790static void bond_unregister_lacpdu(struct bonding *bond)
3791{
3792 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3793}
3794
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003795void bond_register_arp(struct bonding *bond)
3796{
3797 struct packet_type *pt = &bond->arp_mon_pt;
3798
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003799 if (pt->type)
3800 return;
3801
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003802 pt->type = htons(ETH_P_ARP);
Jay Vosburghe245cb72007-02-28 17:03:27 -08003803 pt->dev = bond->dev;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003804 pt->func = bond_arp_rcv;
3805 dev_add_pack(pt);
3806}
3807
3808void bond_unregister_arp(struct bonding *bond)
3809{
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003810 struct packet_type *pt = &bond->arp_mon_pt;
3811
3812 dev_remove_pack(pt);
3813 pt->type = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003814}
3815
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003816/*---------------------------- Hashing Policies -----------------------------*/
3817
3818/*
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003819 * Hash for the output device based upon layer 2 and layer 3 data. If
3820 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3821 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003822static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003823{
3824 struct ethhdr *data = (struct ethhdr *)skb->data;
3825 struct iphdr *iph = ip_hdr(skb);
3826
Brian Haleyf14c4e42008-09-02 10:08:08 -04003827 if (skb->protocol == htons(ETH_P_IP)) {
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003828 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
Jasper Spaansd3da6832009-10-23 04:08:46 +00003829 (data->h_dest[5] ^ data->h_source[5])) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003830 }
3831
Jasper Spaansd3da6832009-10-23 04:08:46 +00003832 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003833}
3834
3835/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003836 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003837 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3838 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3839 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003840static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003841{
3842 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003843 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003844 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003845 int layer4_xor = 0;
3846
Brian Haleyf14c4e42008-09-02 10:08:08 -04003847 if (skb->protocol == htons(ETH_P_IP)) {
3848 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003849 (iph->protocol == IPPROTO_TCP ||
3850 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003851 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003852 }
3853 return (layer4_xor ^
3854 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3855
3856 }
3857
Jasper Spaansd3da6832009-10-23 04:08:46 +00003858 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003859}
3860
3861/*
3862 * Hash for the output device based upon layer 2 data
3863 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003864static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003865{
3866 struct ethhdr *data = (struct ethhdr *)skb->data;
3867
Jasper Spaansd3da6832009-10-23 04:08:46 +00003868 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003869}
3870
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871/*-------------------------- Device entry points ----------------------------*/
3872
3873static int bond_open(struct net_device *bond_dev)
3874{
Wang Chen454d7c92008-11-12 23:37:49 -08003875 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876
3877 bond->kill_timers = 0;
3878
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003879 INIT_DELAYED_WORK(&bond->mcast_work, bond_resend_igmp_join_requests_delayed);
3880
Holger Eitzenberger58402052008-12-09 23:07:13 -08003881 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 /* bond_alb_initialize must be called before the timer
3883 * is started.
3884 */
3885 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3886 /* something went wrong - fail the open operation */
stephen hemmingerb4739462010-01-25 23:34:15 +00003887 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 }
3889
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003890 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3891 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 }
3893
3894 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003895 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3896 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 }
3898
3899 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003900 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3901 INIT_DELAYED_WORK(&bond->arp_work,
3902 bond_activebackup_arp_mon);
3903 else
3904 INIT_DELAYED_WORK(&bond->arp_work,
3905 bond_loadbalance_arp_mon);
3906
3907 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003908 if (bond->params.arp_validate)
3909 bond_register_arp(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 }
3911
3912 if (bond->params.mode == BOND_MODE_8023AD) {
Adrian Bunka40745f2007-10-24 18:27:43 +02003913 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003914 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 /* register to receive LACPDUs */
3916 bond_register_lacpdu(bond);
Jay Vosburghfd989c82008-11-04 17:51:16 -08003917 bond_3ad_initiate_agg_selection(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 }
3919
3920 return 0;
3921}
3922
3923static int bond_close(struct net_device *bond_dev)
3924{
Wang Chen454d7c92008-11-12 23:37:49 -08003925 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
3927 if (bond->params.mode == BOND_MODE_8023AD) {
3928 /* Unregister the receive of LACPDUs */
3929 bond_unregister_lacpdu(bond);
3930 }
3931
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003932 if (bond->params.arp_validate)
3933 bond_unregister_arp(bond);
3934
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 write_lock_bh(&bond->lock);
3936
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003937 bond->send_grat_arp = 0;
Brian Haley305d5522008-11-04 17:51:14 -08003938 bond->send_unsol_na = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
3940 /* signal timers not to re-arm */
3941 bond->kill_timers = 1;
3942
3943 write_unlock_bh(&bond->lock);
3944
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003946 cancel_delayed_work(&bond->mii_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 }
3948
3949 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003950 cancel_delayed_work(&bond->arp_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 }
3952
3953 switch (bond->params.mode) {
3954 case BOND_MODE_8023AD:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003955 cancel_delayed_work(&bond->ad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 break;
3957 case BOND_MODE_TLB:
3958 case BOND_MODE_ALB:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003959 cancel_delayed_work(&bond->alb_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 break;
3961 default:
3962 break;
3963 }
3964
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003965 if (delayed_work_pending(&bond->mcast_work))
3966 cancel_delayed_work(&bond->mcast_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967
Holger Eitzenberger58402052008-12-09 23:07:13 -08003968 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 /* Must be called only after all
3970 * slaves have been released
3971 */
3972 bond_alb_deinitialize(bond);
3973 }
3974
3975 return 0;
3976}
3977
Eric Dumazet28172732010-07-07 14:58:56 -07003978static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3979 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980{
Wang Chen454d7c92008-11-12 23:37:49 -08003981 struct bonding *bond = netdev_priv(bond_dev);
Eric Dumazet28172732010-07-07 14:58:56 -07003982 struct rtnl_link_stats64 temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 struct slave *slave;
3984 int i;
3985
Eric Dumazet28172732010-07-07 14:58:56 -07003986 memset(stats, 0, sizeof(*stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
3988 read_lock_bh(&bond->lock);
3989
3990 bond_for_each_slave(bond, slave, i) {
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00003991 const struct rtnl_link_stats64 *sstats =
Eric Dumazet28172732010-07-07 14:58:56 -07003992 dev_get_stats(slave->dev, &temp);
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08003993
Eric Dumazet28172732010-07-07 14:58:56 -07003994 stats->rx_packets += sstats->rx_packets;
3995 stats->rx_bytes += sstats->rx_bytes;
3996 stats->rx_errors += sstats->rx_errors;
3997 stats->rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
Eric Dumazet28172732010-07-07 14:58:56 -07003999 stats->tx_packets += sstats->tx_packets;
4000 stats->tx_bytes += sstats->tx_bytes;
4001 stats->tx_errors += sstats->tx_errors;
4002 stats->tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
Eric Dumazet28172732010-07-07 14:58:56 -07004004 stats->multicast += sstats->multicast;
4005 stats->collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
Eric Dumazet28172732010-07-07 14:58:56 -07004007 stats->rx_length_errors += sstats->rx_length_errors;
4008 stats->rx_over_errors += sstats->rx_over_errors;
4009 stats->rx_crc_errors += sstats->rx_crc_errors;
4010 stats->rx_frame_errors += sstats->rx_frame_errors;
4011 stats->rx_fifo_errors += sstats->rx_fifo_errors;
4012 stats->rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
Eric Dumazet28172732010-07-07 14:58:56 -07004014 stats->tx_aborted_errors += sstats->tx_aborted_errors;
4015 stats->tx_carrier_errors += sstats->tx_carrier_errors;
4016 stats->tx_fifo_errors += sstats->tx_fifo_errors;
4017 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
4018 stats->tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 }
4020
4021 read_unlock_bh(&bond->lock);
4022
4023 return stats;
4024}
4025
4026static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
4027{
4028 struct net_device *slave_dev = NULL;
4029 struct ifbond k_binfo;
4030 struct ifbond __user *u_binfo = NULL;
4031 struct ifslave k_sinfo;
4032 struct ifslave __user *u_sinfo = NULL;
4033 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 int res = 0;
4035
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004036 pr_debug("bond_ioctl: master=%s, cmd=%d\n", bond_dev->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
4038 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 case SIOCGMIIPHY:
4040 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004041 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004043
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 mii->phy_id = 0;
4045 /* Fall Through */
4046 case SIOCGMIIREG:
4047 /*
4048 * We do this again just in case we were called by SIOCGMIIREG
4049 * instead of SIOCGMIIPHY.
4050 */
4051 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004052 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004054
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
4056 if (mii->reg_num == 1) {
Wang Chen454d7c92008-11-12 23:37:49 -08004057 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 mii->val_out = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07004059 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 read_lock(&bond->curr_slave_lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004061 if (netif_carrier_ok(bond->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 mii->val_out = BMSR_LSTATUS;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004063
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 read_unlock(&bond->curr_slave_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07004065 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 }
4067
4068 return 0;
4069 case BOND_INFO_QUERY_OLD:
4070 case SIOCBONDINFOQUERY:
4071 u_binfo = (struct ifbond __user *)ifr->ifr_data;
4072
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004073 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
4076 res = bond_info_query(bond_dev, &k_binfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004077 if (res == 0 &&
4078 copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
4079 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
4081 return res;
4082 case BOND_SLAVE_INFO_QUERY_OLD:
4083 case SIOCBONDSLAVEINFOQUERY:
4084 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
4085
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004086 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
4089 res = bond_slave_info_query(bond_dev, &k_sinfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004090 if (res == 0 &&
4091 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
4092 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
4094 return res;
4095 default:
4096 /* Go on */
4097 break;
4098 }
4099
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004100 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004103 slave_dev = dev_get_by_name(dev_net(bond_dev), ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004105 pr_debug("slave_dev=%p:\n", slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004107 if (!slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 res = -ENODEV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004109 else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004110 pr_debug("slave_dev->name=%s:\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 switch (cmd) {
4112 case BOND_ENSLAVE_OLD:
4113 case SIOCBONDENSLAVE:
4114 res = bond_enslave(bond_dev, slave_dev);
4115 break;
4116 case BOND_RELEASE_OLD:
4117 case SIOCBONDRELEASE:
4118 res = bond_release(bond_dev, slave_dev);
4119 break;
4120 case BOND_SETHWADDR_OLD:
4121 case SIOCBONDSETHWADDR:
4122 res = bond_sethwaddr(bond_dev, slave_dev);
4123 break;
4124 case BOND_CHANGE_ACTIVE_OLD:
4125 case SIOCBONDCHANGEACTIVE:
4126 res = bond_ioctl_change_active(bond_dev, slave_dev);
4127 break;
4128 default:
4129 res = -EOPNOTSUPP;
4130 }
4131
4132 dev_put(slave_dev);
4133 }
4134
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 return res;
4136}
4137
Jiri Pirko22bedad32010-04-01 21:22:57 +00004138static bool bond_addr_in_mc_list(unsigned char *addr,
4139 struct netdev_hw_addr_list *list,
4140 int addrlen)
4141{
4142 struct netdev_hw_addr *ha;
4143
4144 netdev_hw_addr_list_for_each(ha, list)
4145 if (!memcmp(ha->addr, addr, addrlen))
4146 return true;
4147
4148 return false;
4149}
4150
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151static void bond_set_multicast_list(struct net_device *bond_dev)
4152{
Wang Chen454d7c92008-11-12 23:37:49 -08004153 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00004154 struct netdev_hw_addr *ha;
4155 bool found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 /*
4158 * Do promisc before checking multicast_mode
4159 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004160 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07004161 /*
4162 * FIXME: Need to handle the error when one of the multi-slaves
4163 * encounters error.
4164 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 bond_set_promiscuity(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004167
4168 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 bond_set_promiscuity(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004170
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
4172 /* set allmulti flag to slaves */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004173 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07004174 /*
4175 * FIXME: Need to handle the error when one of the multi-slaves
4176 * encounters error.
4177 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 bond_set_allmulti(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004180
4181 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 bond_set_allmulti(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004183
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004185 read_lock(&bond->lock);
4186
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 bond->flags = bond_dev->flags;
4188
4189 /* looking for addresses to add to slaves' mc list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004190 netdev_for_each_mc_addr(ha, bond_dev) {
4191 found = bond_addr_in_mc_list(ha->addr, &bond->mc_list,
4192 bond_dev->addr_len);
4193 if (!found)
4194 bond_mc_add(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 }
4196
4197 /* looking for addresses to delete from slaves' list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004198 netdev_hw_addr_list_for_each(ha, &bond->mc_list) {
4199 found = bond_addr_in_mc_list(ha->addr, &bond_dev->mc,
4200 bond_dev->addr_len);
4201 if (!found)
4202 bond_mc_del(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 }
4204
4205 /* save master's multicast list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004206 __hw_addr_flush(&bond->mc_list);
4207 __hw_addr_add_multiple(&bond->mc_list, &bond_dev->mc,
4208 bond_dev->addr_len, NETDEV_HW_ADDR_T_MULTICAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08004210 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211}
4212
Stephen Hemminger00829822008-11-20 20:14:53 -08004213static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
4214{
4215 struct bonding *bond = netdev_priv(dev);
4216 struct slave *slave = bond->first_slave;
4217
4218 if (slave) {
4219 const struct net_device_ops *slave_ops
4220 = slave->dev->netdev_ops;
4221 if (slave_ops->ndo_neigh_setup)
Patrick McHardy72e22402009-03-05 01:57:44 -08004222 return slave_ops->ndo_neigh_setup(slave->dev, parms);
Stephen Hemminger00829822008-11-20 20:14:53 -08004223 }
4224 return 0;
4225}
4226
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227/*
4228 * Change the MTU of all of a master's slaves to match the master
4229 */
4230static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
4231{
Wang Chen454d7c92008-11-12 23:37:49 -08004232 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 struct slave *slave, *stop_at;
4234 int res = 0;
4235 int i;
4236
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004237 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004238 (bond_dev ? bond_dev->name : "None"), new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
4240 /* Can't hold bond->lock with bh disabled here since
4241 * some base drivers panic. On the other hand we can't
4242 * hold bond->lock without bh disabled because we'll
4243 * deadlock. The only solution is to rely on the fact
4244 * that we're under rtnl_lock here, and the slaves
4245 * list won't change. This doesn't solve the problem
4246 * of setting the slave's MTU while it is
4247 * transmitting, but the assumption is that the base
4248 * driver can handle that.
4249 *
4250 * TODO: figure out a way to safely iterate the slaves
4251 * list, but without holding a lock around the actual
4252 * call to the base driver.
4253 */
4254
4255 bond_for_each_slave(bond, slave, i) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004256 pr_debug("s %p s->p %p c_m %p\n",
4257 slave,
4258 slave->prev,
4259 slave->dev->netdev_ops->ndo_change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08004260
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 res = dev_set_mtu(slave->dev, new_mtu);
4262
4263 if (res) {
4264 /* If we failed to set the slave's mtu to the new value
4265 * we must abort the operation even in ACTIVE_BACKUP
4266 * mode, because if we allow the backup slaves to have
4267 * different mtu values than the active slave we'll
4268 * need to change their mtu when doing a failover. That
4269 * means changing their mtu from timer context, which
4270 * is probably not a good idea.
4271 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004272 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 goto unwind;
4274 }
4275 }
4276
4277 bond_dev->mtu = new_mtu;
4278
4279 return 0;
4280
4281unwind:
4282 /* unwind from head to the slave that failed */
4283 stop_at = slave;
4284 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4285 int tmp_res;
4286
4287 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4288 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004289 pr_debug("unwind err %d dev %s\n",
4290 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 }
4292 }
4293
4294 return res;
4295}
4296
4297/*
4298 * Change HW address
4299 *
4300 * Note that many devices must be down to change the HW address, and
4301 * downing the master releases all slaves. We can make bonds full of
4302 * bonding devices to test this, however.
4303 */
4304static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4305{
Wang Chen454d7c92008-11-12 23:37:49 -08004306 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 struct sockaddr *sa = addr, tmp_sa;
4308 struct slave *slave, *stop_at;
4309 int res = 0;
4310 int i;
4311
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004312 if (bond->params.mode == BOND_MODE_ALB)
4313 return bond_alb_set_mac_address(bond_dev, addr);
4314
4315
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004316 pr_debug("bond=%p, name=%s\n",
4317 bond, bond_dev ? bond_dev->name : "None");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
Jay Vosburghdd957c52007-10-09 19:57:24 -07004319 /*
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004320 * If fail_over_mac is set to active, do nothing and return
4321 * success. Returning an error causes ifenslave to fail.
Jay Vosburghdd957c52007-10-09 19:57:24 -07004322 */
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004323 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
Jay Vosburghdd957c52007-10-09 19:57:24 -07004324 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07004325
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004326 if (!is_valid_ether_addr(sa->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
4329 /* Can't hold bond->lock with bh disabled here since
4330 * some base drivers panic. On the other hand we can't
4331 * hold bond->lock without bh disabled because we'll
4332 * deadlock. The only solution is to rely on the fact
4333 * that we're under rtnl_lock here, and the slaves
4334 * list won't change. This doesn't solve the problem
4335 * of setting the slave's hw address while it is
4336 * transmitting, but the assumption is that the base
4337 * driver can handle that.
4338 *
4339 * TODO: figure out a way to safely iterate the slaves
4340 * list, but without holding a lock around the actual
4341 * call to the base driver.
4342 */
4343
4344 bond_for_each_slave(bond, slave, i) {
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004345 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004346 pr_debug("slave %p %s\n", slave, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004348 if (slave_ops->ndo_set_mac_address == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 res = -EOPNOTSUPP;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004350 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 goto unwind;
4352 }
4353
4354 res = dev_set_mac_address(slave->dev, addr);
4355 if (res) {
4356 /* TODO: consider downing the slave
4357 * and retry ?
4358 * User should expect communications
4359 * breakage anyway until ARP finish
4360 * updating, so...
4361 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004362 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 goto unwind;
4364 }
4365 }
4366
4367 /* success */
4368 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4369 return 0;
4370
4371unwind:
4372 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4373 tmp_sa.sa_family = bond_dev->type;
4374
4375 /* unwind from head to the slave that failed */
4376 stop_at = slave;
4377 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4378 int tmp_res;
4379
4380 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4381 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004382 pr_debug("unwind err %d dev %s\n",
4383 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 }
4385 }
4386
4387 return res;
4388}
4389
4390static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4391{
Wang Chen454d7c92008-11-12 23:37:49 -08004392 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 struct slave *slave, *start_at;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004394 int i, slave_no, res = 1;
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004395 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
4397 read_lock(&bond->lock);
4398
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004399 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 goto out;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004401 /*
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004402 * Start with the curr_active_slave that joined the bond as the
4403 * default for sending IGMP traffic. For failover purposes one
4404 * needs to maintain some consistency for the interface that will
4405 * send the join/membership reports. The curr_active_slave found
4406 * will send all of this type of traffic.
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004407 */
Eric Dumazet00ae7022010-03-30 23:08:37 +00004408 if ((iph->protocol == IPPROTO_IGMP) &&
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004409 (skb->protocol == htons(ETH_P_IP))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004411 read_lock(&bond->curr_slave_lock);
4412 slave = bond->curr_active_slave;
4413 read_unlock(&bond->curr_slave_lock);
4414
4415 if (!slave)
4416 goto out;
4417 } else {
4418 /*
4419 * Concurrent TX may collide on rr_tx_counter; we accept
4420 * that as being rare enough not to justify using an
4421 * atomic op here.
4422 */
4423 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
4424
4425 bond_for_each_slave(bond, slave, i) {
4426 slave_no--;
4427 if (slave_no < 0)
4428 break;
4429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 }
4431
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004432 start_at = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 bond_for_each_slave_from(bond, slave, i, start_at) {
4434 if (IS_UP(slave->dev) &&
4435 (slave->link == BOND_LINK_UP) &&
4436 (slave->state == BOND_STATE_ACTIVE)) {
4437 res = bond_dev_queue_xmit(bond, skb, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 break;
4439 }
4440 }
4441
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442out:
4443 if (res) {
4444 /* no suitable interface, frame not sent */
4445 dev_kfree_skb(skb);
4446 }
4447 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004448 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449}
4450
John W. Linville075897c2005-09-28 17:50:53 -04004451
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452/*
4453 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4454 * the bond has a usable interface.
4455 */
4456static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4457{
Wang Chen454d7c92008-11-12 23:37:49 -08004458 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 int res = 1;
4460
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 read_lock(&bond->lock);
4462 read_lock(&bond->curr_slave_lock);
4463
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004464 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
John W. Linville075897c2005-09-28 17:50:53 -04004467 if (!bond->curr_active_slave)
4468 goto out;
4469
John W. Linville075897c2005-09-28 17:50:53 -04004470 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4471
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004473 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 /* no suitable interface, frame not sent */
4475 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004476
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 read_unlock(&bond->curr_slave_lock);
4478 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004479 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480}
4481
4482/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004483 * In bond_xmit_xor() , we determine the output device by using a pre-
4484 * determined xmit_hash_policy(), If the selected device is not enabled,
4485 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 */
4487static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4488{
Wang Chen454d7c92008-11-12 23:37:49 -08004489 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 struct slave *slave, *start_at;
4491 int slave_no;
4492 int i;
4493 int res = 1;
4494
4495 read_lock(&bond->lock);
4496
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004497 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499
Jasper Spaansa361c832009-10-23 04:09:24 +00004500 slave_no = bond->xmit_hash_policy(skb, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
4502 bond_for_each_slave(bond, slave, i) {
4503 slave_no--;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004504 if (slave_no < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 }
4507
4508 start_at = slave;
4509
4510 bond_for_each_slave_from(bond, slave, i, start_at) {
4511 if (IS_UP(slave->dev) &&
4512 (slave->link == BOND_LINK_UP) &&
4513 (slave->state == BOND_STATE_ACTIVE)) {
4514 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4515 break;
4516 }
4517 }
4518
4519out:
4520 if (res) {
4521 /* no suitable interface, frame not sent */
4522 dev_kfree_skb(skb);
4523 }
4524 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004525 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526}
4527
4528/*
4529 * in broadcast mode, we send everything to all usable interfaces.
4530 */
4531static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4532{
Wang Chen454d7c92008-11-12 23:37:49 -08004533 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 struct slave *slave, *start_at;
4535 struct net_device *tx_dev = NULL;
4536 int i;
4537 int res = 1;
4538
4539 read_lock(&bond->lock);
4540
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004541 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543
4544 read_lock(&bond->curr_slave_lock);
4545 start_at = bond->curr_active_slave;
4546 read_unlock(&bond->curr_slave_lock);
4547
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004548 if (!start_at)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550
4551 bond_for_each_slave_from(bond, slave, i, start_at) {
4552 if (IS_UP(slave->dev) &&
4553 (slave->link == BOND_LINK_UP) &&
4554 (slave->state == BOND_STATE_ACTIVE)) {
4555 if (tx_dev) {
4556 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4557 if (!skb2) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004558 pr_err("%s: Error: bond_xmit_broadcast(): skb_clone() failed\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08004559 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 continue;
4561 }
4562
4563 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4564 if (res) {
4565 dev_kfree_skb(skb2);
4566 continue;
4567 }
4568 }
4569 tx_dev = slave->dev;
4570 }
4571 }
4572
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004573 if (tx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 res = bond_dev_queue_xmit(bond, skb, tx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575
4576out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004577 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 /* no suitable interface, frame not sent */
4579 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004580
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 /* frame sent to all suitable interfaces */
4582 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004583 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584}
4585
4586/*------------------------- Device initialization ---------------------------*/
4587
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004588static void bond_set_xmit_hash_policy(struct bonding *bond)
4589{
4590 switch (bond->params.xmit_policy) {
4591 case BOND_XMIT_POLICY_LAYER23:
4592 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4593 break;
4594 case BOND_XMIT_POLICY_LAYER34:
4595 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4596 break;
4597 case BOND_XMIT_POLICY_LAYER2:
4598 default:
4599 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4600 break;
4601 }
4602}
4603
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004604/*
4605 * Lookup the slave that corresponds to a qid
4606 */
4607static inline int bond_slave_override(struct bonding *bond,
4608 struct sk_buff *skb)
4609{
4610 int i, res = 1;
4611 struct slave *slave = NULL;
4612 struct slave *check_slave;
4613
4614 read_lock(&bond->lock);
4615
4616 if (!BOND_IS_OK(bond) || !skb->queue_mapping)
4617 goto out;
4618
4619 /* Find out if any slaves have the same mapping as this skb. */
4620 bond_for_each_slave(bond, check_slave, i) {
4621 if (check_slave->queue_id == skb->queue_mapping) {
4622 slave = check_slave;
4623 break;
4624 }
4625 }
4626
4627 /* If the slave isn't UP, use default transmit policy. */
4628 if (slave && slave->queue_id && IS_UP(slave->dev) &&
4629 (slave->link == BOND_LINK_UP)) {
4630 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4631 }
4632
4633out:
4634 read_unlock(&bond->lock);
4635 return res;
4636}
4637
4638static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb)
4639{
4640 /*
4641 * This helper function exists to help dev_pick_tx get the correct
4642 * destination queue. Using a helper function skips the a call to
4643 * skb_tx_hash and will put the skbs in the queue we expect on their
4644 * way down to the bonding driver.
4645 */
4646 return skb->queue_mapping;
4647}
4648
Stephen Hemminger424efe92009-08-31 19:50:51 +00004649static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
Stephen Hemminger00829822008-11-20 20:14:53 -08004650{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004651 struct bonding *bond = netdev_priv(dev);
4652
Neil Hormane843fa52010-10-13 16:01:50 +00004653 /*
4654 * If we risk deadlock from transmitting this in the
4655 * netpoll path, tell netpoll to queue the frame for later tx
4656 */
4657 if (is_netpoll_tx_blocked(dev))
4658 return NETDEV_TX_BUSY;
4659
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004660 if (TX_QUEUE_OVERRIDE(bond->params.mode)) {
4661 if (!bond_slave_override(bond, skb))
4662 return NETDEV_TX_OK;
4663 }
Stephen Hemminger00829822008-11-20 20:14:53 -08004664
4665 switch (bond->params.mode) {
4666 case BOND_MODE_ROUNDROBIN:
4667 return bond_xmit_roundrobin(skb, dev);
4668 case BOND_MODE_ACTIVEBACKUP:
4669 return bond_xmit_activebackup(skb, dev);
4670 case BOND_MODE_XOR:
4671 return bond_xmit_xor(skb, dev);
4672 case BOND_MODE_BROADCAST:
4673 return bond_xmit_broadcast(skb, dev);
4674 case BOND_MODE_8023AD:
4675 return bond_3ad_xmit_xor(skb, dev);
4676 case BOND_MODE_ALB:
4677 case BOND_MODE_TLB:
4678 return bond_alb_xmit(skb, dev);
4679 default:
4680 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004681 pr_err("%s: Error: Unknown bonding mode %d\n",
4682 dev->name, bond->params.mode);
Stephen Hemminger00829822008-11-20 20:14:53 -08004683 WARN_ON_ONCE(1);
4684 dev_kfree_skb(skb);
4685 return NETDEV_TX_OK;
4686 }
4687}
4688
4689
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690/*
4691 * set bond mode specific net device operations
4692 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004693void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004695 struct net_device *bond_dev = bond->dev;
4696
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 switch (mode) {
4698 case BOND_MODE_ROUNDROBIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 break;
4700 case BOND_MODE_ACTIVEBACKUP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 break;
4702 case BOND_MODE_XOR:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004703 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 break;
4705 case BOND_MODE_BROADCAST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 break;
4707 case BOND_MODE_8023AD:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004708 bond_set_master_3ad_flags(bond);
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004709 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004712 bond_set_master_alb_flags(bond);
4713 /* FALLTHRU */
4714 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 break;
4716 default:
4717 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004718 pr_err("%s: Error: Unknown bonding mode %d\n",
4719 bond_dev->name, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 break;
4721 }
4722}
4723
Jay Vosburgh217df672005-09-26 16:11:50 -07004724static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4725 struct ethtool_drvinfo *drvinfo)
4726{
4727 strncpy(drvinfo->driver, DRV_NAME, 32);
4728 strncpy(drvinfo->version, DRV_VERSION, 32);
4729 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4730}
4731
Jeff Garzik7282d492006-09-13 14:30:00 -04004732static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004733 .get_drvinfo = bond_ethtool_get_drvinfo,
Stephen Hemmingerfa53eba2008-09-13 21:17:09 -04004734 .get_link = ethtool_op_get_link,
4735 .get_tx_csum = ethtool_op_get_tx_csum,
4736 .get_sg = ethtool_op_get_sg,
4737 .get_tso = ethtool_op_get_tso,
4738 .get_ufo = ethtool_op_get_ufo,
4739 .get_flags = ethtool_op_get_flags,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004740};
4741
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004742static const struct net_device_ops bond_netdev_ops = {
Stephen Hemminger181470f2009-06-12 19:02:52 +00004743 .ndo_init = bond_init,
Stephen Hemminger9e716262009-06-12 19:02:47 +00004744 .ndo_uninit = bond_uninit,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004745 .ndo_open = bond_open,
4746 .ndo_stop = bond_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08004747 .ndo_start_xmit = bond_start_xmit,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004748 .ndo_select_queue = bond_select_queue,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00004749 .ndo_get_stats64 = bond_get_stats,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004750 .ndo_do_ioctl = bond_do_ioctl,
4751 .ndo_set_multicast_list = bond_set_multicast_list,
4752 .ndo_change_mtu = bond_change_mtu,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004753 .ndo_set_mac_address = bond_set_mac_address,
Stephen Hemminger00829822008-11-20 20:14:53 -08004754 .ndo_neigh_setup = bond_neigh_setup,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004755 .ndo_vlan_rx_register = bond_vlan_rx_register,
4756 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4757 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
WANG Congf6dc31a2010-05-06 00:48:51 -07004758#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00004759 .ndo_netpoll_setup = bond_netpoll_setup,
WANG Congf6dc31a2010-05-06 00:48:51 -07004760 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
4761 .ndo_poll_controller = bond_poll_controller,
4762#endif
Jiri Pirko9232ecc2011-02-13 09:33:01 +00004763 .ndo_add_slave = bond_enslave,
4764 .ndo_del_slave = bond_release,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004765};
4766
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004767static void bond_destructor(struct net_device *bond_dev)
4768{
4769 struct bonding *bond = netdev_priv(bond_dev);
4770 if (bond->wq)
4771 destroy_workqueue(bond->wq);
4772 free_netdev(bond_dev);
4773}
4774
Stephen Hemminger181470f2009-06-12 19:02:52 +00004775static void bond_setup(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776{
Wang Chen454d7c92008-11-12 23:37:49 -08004777 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 /* initialize rwlocks */
4780 rwlock_init(&bond->lock);
4781 rwlock_init(&bond->curr_slave_lock);
4782
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00004783 bond->params = bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784
4785 /* Initialize pointers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 bond->dev = bond_dev;
4787 INIT_LIST_HEAD(&bond->vlan_list);
4788
4789 /* Initialize the device entry points */
Stephen Hemminger181470f2009-06-12 19:02:52 +00004790 ether_setup(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004791 bond_dev->netdev_ops = &bond_netdev_ops;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004792 bond_dev->ethtool_ops = &bond_ethtool_ops;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004793 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004795 bond_dev->destructor = bond_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796
4797 /* Initialize the device options */
4798 bond_dev->tx_queue_len = 0;
4799 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004800 bond_dev->priv_flags |= IFF_BONDING;
Stephen Hemminger181470f2009-06-12 19:02:52 +00004801 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
4802
Jay Vosburgh6cf3f412008-11-03 18:16:50 -08004803 if (bond->params.arp_interval)
4804 bond_dev->priv_flags |= IFF_MASTER_ARPMON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805
4806 /* At first, we block adding VLANs. That's the only way to
4807 * prevent problems that occur when adding VLANs over an
4808 * empty bond. The block will be removed once non-challenged
4809 * slaves are enslaved.
4810 */
4811 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4812
Herbert Xu932ff272006-06-09 12:20:56 -07004813 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 * transmitting */
4815 bond_dev->features |= NETIF_F_LLTX;
4816
4817 /* By default, we declare the bond to be fully
4818 * VLAN hardware accelerated capable. Special
4819 * care is taken in the various xmit functions
4820 * when there are slaves that are not hw accel
4821 * capable
4822 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4824 NETIF_F_HW_VLAN_RX |
4825 NETIF_F_HW_VLAN_FILTER);
4826
Eric Dumazete6599c22010-09-17 09:25:07 +00004827 /* By default, we enable GRO on bonding devices.
4828 * Actual support requires lowlevel drivers are GRO ready.
4829 */
4830 bond_dev->features |= NETIF_F_GRO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831}
4832
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004833static void bond_work_cancel_all(struct bonding *bond)
4834{
4835 write_lock_bh(&bond->lock);
4836 bond->kill_timers = 1;
4837 write_unlock_bh(&bond->lock);
4838
4839 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4840 cancel_delayed_work(&bond->mii_work);
4841
4842 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4843 cancel_delayed_work(&bond->arp_work);
4844
4845 if (bond->params.mode == BOND_MODE_ALB &&
4846 delayed_work_pending(&bond->alb_work))
4847 cancel_delayed_work(&bond->alb_work);
4848
4849 if (bond->params.mode == BOND_MODE_8023AD &&
4850 delayed_work_pending(&bond->ad_work))
4851 cancel_delayed_work(&bond->ad_work);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00004852
4853 if (delayed_work_pending(&bond->mcast_work))
4854 cancel_delayed_work(&bond->mcast_work);
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004855}
4856
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004857/*
4858* Destroy a bonding device.
4859* Must be under rtnl_lock when this function is called.
4860*/
4861static void bond_uninit(struct net_device *bond_dev)
Jay Vosburgha434e432008-10-30 17:41:15 -07004862{
Wang Chen454d7c92008-11-12 23:37:49 -08004863 struct bonding *bond = netdev_priv(bond_dev);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004864 struct vlan_entry *vlan, *tmp;
Jay Vosburgha434e432008-10-30 17:41:15 -07004865
WANG Congf6dc31a2010-05-06 00:48:51 -07004866 bond_netpoll_cleanup(bond_dev);
4867
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004868 /* Release the bonded slaves */
4869 bond_release_all(bond_dev);
4870
Jay Vosburgha434e432008-10-30 17:41:15 -07004871 list_del(&bond->bond_list);
4872
4873 bond_work_cancel_all(bond);
4874
Jay Vosburgha434e432008-10-30 17:41:15 -07004875 bond_remove_proc_entry(bond);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004876
Taku Izumif073c7c2010-12-09 15:17:13 +00004877 bond_debug_unregister(bond);
4878
Jiri Pirko22bedad32010-04-01 21:22:57 +00004879 __hw_addr_flush(&bond->mc_list);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004880
4881 list_for_each_entry_safe(vlan, tmp, &bond->vlan_list, vlan_list) {
4882 list_del(&vlan->vlan_list);
4883 kfree(vlan);
4884 }
Jay Vosburgha434e432008-10-30 17:41:15 -07004885}
4886
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887/*------------------------- Module initialization ---------------------------*/
4888
4889/*
4890 * Convert string input module parms. Accept either the
Jay Vosburghece95f72008-01-17 16:25:01 -08004891 * number of the mode or its string name. A bit complicated because
4892 * some mode names are substrings of other names, and calls from sysfs
4893 * may have whitespace in the name (trailing newlines, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 */
Holger Eitzenberger325dcf72008-12-09 23:10:17 -08004895int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896{
Hannes Eder54b87322009-02-14 11:15:49 +00004897 int modeint = -1, i, rv;
Jay Vosburgha42e5342008-01-29 18:07:43 -08004898 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
Jay Vosburghece95f72008-01-17 16:25:01 -08004899
Jay Vosburgha42e5342008-01-29 18:07:43 -08004900 for (p = (char *)buf; *p; p++)
4901 if (!(isdigit(*p) || isspace(*p)))
4902 break;
4903
4904 if (*p)
Jay Vosburghece95f72008-01-17 16:25:01 -08004905 rv = sscanf(buf, "%20s", modestr);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004906 else
Hannes Eder54b87322009-02-14 11:15:49 +00004907 rv = sscanf(buf, "%d", &modeint);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004908
4909 if (!rv)
4910 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911
4912 for (i = 0; tbl[i].modename; i++) {
Hannes Eder54b87322009-02-14 11:15:49 +00004913 if (modeint == tbl[i].mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 return tbl[i].mode;
Jay Vosburghece95f72008-01-17 16:25:01 -08004915 if (strcmp(modestr, tbl[i].modename) == 0)
4916 return tbl[i].mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 }
4918
4919 return -1;
4920}
4921
4922static int bond_check_params(struct bond_params *params)
4923{
Jiri Pirkoa5499522009-09-25 03:28:09 +00004924 int arp_validate_value, fail_over_mac_value, primary_reselect_value;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004925
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 /*
4927 * Convert string parameters.
4928 */
4929 if (mode) {
4930 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4931 if (bond_mode == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004932 pr_err("Error: Invalid bonding mode \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 mode == NULL ? "NULL" : mode);
4934 return -EINVAL;
4935 }
4936 }
4937
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004938 if (xmit_hash_policy) {
4939 if ((bond_mode != BOND_MODE_XOR) &&
4940 (bond_mode != BOND_MODE_8023AD)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004941 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004942 bond_mode_name(bond_mode));
4943 } else {
4944 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4945 xmit_hashtype_tbl);
4946 if (xmit_hashtype == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004947 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004948 xmit_hash_policy == NULL ? "NULL" :
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004949 xmit_hash_policy);
4950 return -EINVAL;
4951 }
4952 }
4953 }
4954
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 if (lacp_rate) {
4956 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004957 pr_info("lacp_rate param is irrelevant in mode %s\n",
4958 bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 } else {
4960 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4961 if (lacp_fast == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004962 pr_err("Error: Invalid lacp rate \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 lacp_rate == NULL ? "NULL" : lacp_rate);
4964 return -EINVAL;
4965 }
4966 }
4967 }
4968
Jay Vosburghfd989c82008-11-04 17:51:16 -08004969 if (ad_select) {
4970 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4971 if (params->ad_select == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004972 pr_err("Error: Invalid ad_select \"%s\"\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -08004973 ad_select == NULL ? "NULL" : ad_select);
4974 return -EINVAL;
4975 }
4976
4977 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004978 pr_warning("ad_select param only affects 802.3ad mode\n");
Jay Vosburghfd989c82008-11-04 17:51:16 -08004979 }
4980 } else {
4981 params->ad_select = BOND_AD_STABLE;
4982 }
4983
Nicolas de Pesloüanf5841302009-08-28 13:18:34 +00004984 if (max_bonds < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004985 pr_warning("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4986 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 max_bonds = BOND_DEFAULT_MAX_BONDS;
4988 }
4989
4990 if (miimon < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004991 pr_warning("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to %d\n",
4992 miimon, INT_MAX, BOND_LINK_MON_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 miimon = BOND_LINK_MON_INTERV;
4994 }
4995
4996 if (updelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004997 pr_warning("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4998 updelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 updelay = 0;
5000 }
5001
5002 if (downdelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005003 pr_warning("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
5004 downdelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 downdelay = 0;
5006 }
5007
5008 if ((use_carrier != 0) && (use_carrier != 1)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005009 pr_warning("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
5010 use_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 use_carrier = 1;
5012 }
5013
Moni Shoua7893b242008-05-17 21:10:12 -07005014 if (num_grat_arp < 0 || num_grat_arp > 255) {
Frans Pop2381a552010-03-24 07:57:36 +00005015 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 -08005016 num_grat_arp);
Moni Shoua7893b242008-05-17 21:10:12 -07005017 num_grat_arp = 1;
5018 }
5019
Brian Haley305d5522008-11-04 17:51:14 -08005020 if (num_unsol_na < 0 || num_unsol_na > 255) {
Frans Pop2381a552010-03-24 07:57:36 +00005021 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 -08005022 num_unsol_na);
Brian Haley305d5522008-11-04 17:51:14 -08005023 num_unsol_na = 1;
5024 }
5025
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 /* reset values for 802.3ad */
5027 if (bond_mode == BOND_MODE_8023AD) {
5028 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005029 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 +00005030 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 miimon = 100;
5032 }
5033 }
5034
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005035 if (tx_queues < 1 || tx_queues > 255) {
5036 pr_warning("Warning: tx_queues (%d) should be between "
5037 "1 and 255, resetting to %d\n",
5038 tx_queues, BOND_DEFAULT_TX_QUEUES);
5039 tx_queues = BOND_DEFAULT_TX_QUEUES;
5040 }
5041
Andy Gospodarekebd8e492010-06-02 08:39:21 +00005042 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
5043 pr_warning("Warning: all_slaves_active module parameter (%d), "
5044 "not of valid value (0/1), so it was set to "
5045 "0\n", all_slaves_active);
5046 all_slaves_active = 0;
5047 }
5048
Flavio Leitnerc2952c32010-10-05 14:23:59 +00005049 if (resend_igmp < 0 || resend_igmp > 255) {
5050 pr_warning("Warning: resend_igmp (%d) should be between "
5051 "0 and 255, resetting to %d\n",
5052 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
5053 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
5054 }
5055
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 /* reset values for TLB/ALB */
5057 if ((bond_mode == BOND_MODE_TLB) ||
5058 (bond_mode == BOND_MODE_ALB)) {
5059 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005060 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 +00005061 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 miimon = 100;
5063 }
5064 }
5065
5066 if (bond_mode == BOND_MODE_ALB) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005067 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",
5068 updelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 }
5070
5071 if (!miimon) {
5072 if (updelay || downdelay) {
5073 /* just warn the user the up/down delay will have
5074 * no effect since miimon is zero...
5075 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005076 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",
5077 updelay, downdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 }
5079 } else {
5080 /* don't allow arp monitoring */
5081 if (arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005082 pr_warning("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
5083 miimon, arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 arp_interval = 0;
5085 }
5086
5087 if ((updelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005088 pr_warning("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
5089 updelay, miimon,
5090 (updelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 }
5092
5093 updelay /= miimon;
5094
5095 if ((downdelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005096 pr_warning("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
5097 downdelay, miimon,
5098 (downdelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 }
5100
5101 downdelay /= miimon;
5102 }
5103
5104 if (arp_interval < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005105 pr_warning("Warning: arp_interval module parameter (%d) , not in range 0-%d, so it was reset to %d\n",
5106 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 arp_interval = BOND_LINK_ARP_INTERV;
5108 }
5109
5110 for (arp_ip_count = 0;
5111 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
5112 arp_ip_count++) {
5113 /* not complete check, but should be good enough to
5114 catch mistakes */
5115 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005116 pr_warning("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
5117 arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 arp_interval = 0;
5119 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04005120 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 arp_target[arp_ip_count] = ip;
5122 }
5123 }
5124
5125 if (arp_interval && !arp_ip_count) {
5126 /* don't allow arping if no arp_ip_target given... */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005127 pr_warning("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
5128 arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 arp_interval = 0;
5130 }
5131
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005132 if (arp_validate) {
5133 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005134 pr_err("arp_validate only supported in active-backup mode\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005135 return -EINVAL;
5136 }
5137 if (!arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005138 pr_err("arp_validate requires arp_interval\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005139 return -EINVAL;
5140 }
5141
5142 arp_validate_value = bond_parse_parm(arp_validate,
5143 arp_validate_tbl);
5144 if (arp_validate_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005145 pr_err("Error: invalid arp_validate \"%s\"\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005146 arp_validate == NULL ? "NULL" : arp_validate);
5147 return -EINVAL;
5148 }
5149 } else
5150 arp_validate_value = 0;
5151
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 if (miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005153 pr_info("MII link monitoring set to %d ms\n", miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 } else if (arp_interval) {
5155 int i;
5156
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005157 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
5158 arp_interval,
5159 arp_validate_tbl[arp_validate_value].modename,
5160 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161
5162 for (i = 0; i < arp_ip_count; i++)
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00005163 pr_info(" %s", arp_ip_target[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00005165 pr_info("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166
Jay Vosburghb8a97872008-06-13 18:12:04 -07005167 } else if (max_bonds) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 /* miimon and arp_interval not set, we need one so things
5169 * work as expected, see bonding.txt for details
5170 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005171 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 -07005172 }
5173
5174 if (primary && !USES_PRIMARY(bond_mode)) {
5175 /* currently, using a primary only makes sense
5176 * in active backup, TLB or ALB modes
5177 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005178 pr_warning("Warning: %s primary device specified but has no effect in %s mode\n",
5179 primary, bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 primary = NULL;
5181 }
5182
Jiri Pirkoa5499522009-09-25 03:28:09 +00005183 if (primary && primary_reselect) {
5184 primary_reselect_value = bond_parse_parm(primary_reselect,
5185 pri_reselect_tbl);
5186 if (primary_reselect_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005187 pr_err("Error: Invalid primary_reselect \"%s\"\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00005188 primary_reselect ==
5189 NULL ? "NULL" : primary_reselect);
5190 return -EINVAL;
5191 }
5192 } else {
5193 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
5194 }
5195
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005196 if (fail_over_mac) {
5197 fail_over_mac_value = bond_parse_parm(fail_over_mac,
5198 fail_over_mac_tbl);
5199 if (fail_over_mac_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005200 pr_err("Error: invalid fail_over_mac \"%s\"\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005201 arp_validate == NULL ? "NULL" : arp_validate);
5202 return -EINVAL;
5203 }
5204
5205 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005206 pr_warning("Warning: fail_over_mac only affects active-backup mode.\n");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005207 } else {
5208 fail_over_mac_value = BOND_FOM_NONE;
5209 }
Jay Vosburghdd957c52007-10-09 19:57:24 -07005210
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 /* fill params struct with the proper values */
5212 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04005213 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 params->miimon = miimon;
Moni Shoua7893b242008-05-17 21:10:12 -07005215 params->num_grat_arp = num_grat_arp;
Brian Haley305d5522008-11-04 17:51:14 -08005216 params->num_unsol_na = num_unsol_na;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005218 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 params->updelay = updelay;
5220 params->downdelay = downdelay;
5221 params->use_carrier = use_carrier;
5222 params->lacp_fast = lacp_fast;
5223 params->primary[0] = 0;
Jiri Pirkoa5499522009-09-25 03:28:09 +00005224 params->primary_reselect = primary_reselect_value;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005225 params->fail_over_mac = fail_over_mac_value;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005226 params->tx_queues = tx_queues;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00005227 params->all_slaves_active = all_slaves_active;
Flavio Leitnerc2952c32010-10-05 14:23:59 +00005228 params->resend_igmp = resend_igmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229
5230 if (primary) {
5231 strncpy(params->primary, primary, IFNAMSIZ);
5232 params->primary[IFNAMSIZ - 1] = 0;
5233 }
5234
5235 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
5236
5237 return 0;
5238}
5239
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005240static struct lock_class_key bonding_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -07005241static struct lock_class_key bonding_netdev_addr_lock_key;
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005242
David S. Millere8a04642008-07-17 00:34:19 -07005243static void bond_set_lockdep_class_one(struct net_device *dev,
5244 struct netdev_queue *txq,
5245 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -07005246{
5247 lockdep_set_class(&txq->_xmit_lock,
5248 &bonding_netdev_xmit_lock_key);
5249}
5250
5251static void bond_set_lockdep_class(struct net_device *dev)
5252{
David S. Millercf508b12008-07-22 14:16:42 -07005253 lockdep_set_class(&dev->addr_list_lock,
5254 &bonding_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -07005255 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -07005256}
5257
Stephen Hemminger181470f2009-06-12 19:02:52 +00005258/*
5259 * Called from registration process
5260 */
5261static int bond_init(struct net_device *bond_dev)
5262{
5263 struct bonding *bond = netdev_priv(bond_dev);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005264 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005265
5266 pr_debug("Begin bond_init for %s\n", bond_dev->name);
5267
5268 bond->wq = create_singlethread_workqueue(bond_dev->name);
5269 if (!bond->wq)
5270 return -ENOMEM;
5271
5272 bond_set_lockdep_class(bond_dev);
5273
5274 netif_carrier_off(bond_dev);
5275
5276 bond_create_proc_entry(bond);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005277 list_add_tail(&bond->bond_list, &bn->dev_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005278
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00005279 bond_prepare_sysfs_group(bond);
Jiri Pirko22bedad32010-04-01 21:22:57 +00005280
Taku Izumif073c7c2010-12-09 15:17:13 +00005281 bond_debug_register(bond);
5282
Jiri Pirko22bedad32010-04-01 21:22:57 +00005283 __hw_addr_init(&bond->mc_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005284 return 0;
5285}
5286
Eric W. Biederman88ead972009-10-29 14:18:25 +00005287static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
5288{
5289 if (tb[IFLA_ADDRESS]) {
5290 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
5291 return -EINVAL;
5292 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
5293 return -EADDRNOTAVAIL;
5294 }
5295 return 0;
5296}
5297
5298static struct rtnl_link_ops bond_link_ops __read_mostly = {
5299 .kind = "bond",
Eric W. Biederman66391042009-10-29 23:58:54 +00005300 .priv_size = sizeof(struct bonding),
Eric W. Biederman88ead972009-10-29 14:18:25 +00005301 .setup = bond_setup,
5302 .validate = bond_validate,
5303};
5304
Mitch Williamsdfe60392005-11-09 10:36:04 -08005305/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005306 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08005307 * Caller must NOT hold rtnl_lock; we need to release it here before we
5308 * set up our sysfs entries.
5309 */
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005310int bond_create(struct net *net, const char *name)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005311{
5312 struct net_device *bond_dev;
5313 int res;
5314
5315 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08005316
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005317 bond_dev = alloc_netdev_mq(sizeof(struct bonding), name ? name : "",
5318 bond_setup, tx_queues);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005319 if (!bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005320 pr_err("%s: eek! can't alloc netdev!\n", name);
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005321 rtnl_unlock();
5322 return -ENOMEM;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005323 }
5324
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005325 dev_net_set(bond_dev, net);
Eric W. Biederman88ead972009-10-29 14:18:25 +00005326 bond_dev->rtnl_link_ops = &bond_link_ops;
5327
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005328 if (!name) {
5329 res = dev_alloc_name(bond_dev, "bond%d");
5330 if (res < 0)
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005331 goto out;
Neil Horman27e6f062010-10-05 03:39:21 +00005332 } else {
5333 /*
5334 * If we're given a name to register
5335 * we need to ensure that its not already
5336 * registered
5337 */
5338 res = -EEXIST;
5339 if (__dev_get_by_name(net, name) != NULL)
5340 goto out;
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005341 }
5342
Mitch Williamsdfe60392005-11-09 10:36:04 -08005343 res = register_netdevice(bond_dev);
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005344
Eric W. Biederman30c15ba2009-10-29 14:18:23 +00005345out:
Mitch Williamsdfe60392005-11-09 10:36:04 -08005346 rtnl_unlock();
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005347 if (res < 0)
5348 bond_destructor(bond_dev);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005349 return res;
5350}
5351
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005352static int __net_init bond_net_init(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005353{
Eric W. Biederman15449742009-11-29 15:46:04 +00005354 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005355
5356 bn->net = net;
5357 INIT_LIST_HEAD(&bn->dev_list);
5358
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005359 bond_create_proc_dir(bn);
Eric W. Biederman15449742009-11-29 15:46:04 +00005360
5361 return 0;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005362}
5363
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005364static void __net_exit bond_net_exit(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005365{
Eric W. Biederman15449742009-11-29 15:46:04 +00005366 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005367
5368 bond_destroy_proc_dir(bn);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005369}
5370
5371static struct pernet_operations bond_net_ops = {
5372 .init = bond_net_init,
5373 .exit = bond_net_exit,
Eric W. Biederman15449742009-11-29 15:46:04 +00005374 .id = &bond_net_id,
5375 .size = sizeof(struct bond_net),
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005376};
5377
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378static int __init bonding_init(void)
5379{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 int i;
5381 int res;
5382
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005383 pr_info("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384
Mitch Williamsdfe60392005-11-09 10:36:04 -08005385 res = bond_check_params(&bonding_defaults);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005386 if (res)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005387 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388
Eric W. Biederman15449742009-11-29 15:46:04 +00005389 res = register_pernet_subsys(&bond_net_ops);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005390 if (res)
5391 goto out;
Jay Vosburgh027ea042008-01-17 16:25:02 -08005392
Eric W. Biederman88ead972009-10-29 14:18:25 +00005393 res = rtnl_link_register(&bond_link_ops);
5394 if (res)
Eric W. Biederman66391042009-10-29 23:58:54 +00005395 goto err_link;
Eric W. Biederman88ead972009-10-29 14:18:25 +00005396
Taku Izumif073c7c2010-12-09 15:17:13 +00005397 bond_create_debugfs();
5398
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 for (i = 0; i < max_bonds; i++) {
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005400 res = bond_create(&init_net, NULL);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005401 if (res)
5402 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 }
5404
Mitch Williamsb76cdba2005-11-09 10:36:41 -08005405 res = bond_create_sysfs();
5406 if (res)
5407 goto err;
5408
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005410 register_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005411 bond_register_ipv6_notifier();
Mitch Williamsdfe60392005-11-09 10:36:04 -08005412out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 return res;
Eric W. Biederman88ead972009-10-29 14:18:25 +00005414err:
5415 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman66391042009-10-29 23:58:54 +00005416err_link:
Eric W. Biederman15449742009-11-29 15:46:04 +00005417 unregister_pernet_subsys(&bond_net_ops);
Eric W. Biederman88ead972009-10-29 14:18:25 +00005418 goto out;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005419
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420}
5421
5422static void __exit bonding_exit(void)
5423{
5424 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005425 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005426 bond_unregister_ipv6_notifier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005428 bond_destroy_sysfs();
Taku Izumif073c7c2010-12-09 15:17:13 +00005429 bond_destroy_debugfs();
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005430
Eric W. Biederman88ead972009-10-29 14:18:25 +00005431 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman15449742009-11-29 15:46:04 +00005432 unregister_pernet_subsys(&bond_net_ops);
Neil Hormane843fa52010-10-13 16:01:50 +00005433
5434#ifdef CONFIG_NET_POLL_CONTROLLER
Neil Hormanfb4fa762010-12-06 09:05:50 +00005435 /*
5436 * Make sure we don't have an imbalance on our netpoll blocking
5437 */
5438 WARN_ON(atomic_read(&netpoll_block_tx));
Neil Hormane843fa52010-10-13 16:01:50 +00005439#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440}
5441
5442module_init(bonding_init);
5443module_exit(bonding_exit);
5444MODULE_LICENSE("GPL");
5445MODULE_VERSION(DRV_VERSION);
5446MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5447MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
Eric W. Biederman88ead972009-10-29 14:18:25 +00005448MODULE_ALIAS_RTNL_LINK("bond");