blob: 17b4dd94da907f388e66e9e5bd6ec6d5daf50148 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/smp.h>
69#include <linux/if_ether.h>
70#include <net/arp.h>
71#include <linux/mii.h>
72#include <linux/ethtool.h>
73#include <linux/if_vlan.h>
74#include <linux/if_bonding.h>
David Sterbab63bb732007-12-06 23:40:33 -080075#include <linux/jiffies.h>
Neil Hormane843fa52010-10-13 16:01:50 +000076#include <linux/preempt.h>
Jay Vosburghc3ade5c2005-06-26 17:52:20 -040077#include <net/route.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020078#include <net/net_namespace.h>
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000079#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include "bonding.h"
81#include "bond_3ad.h"
82#include "bond_alb.h"
83
84/*---------------------------- Module parameters ----------------------------*/
85
86/* monitor all links that often (in milliseconds). <=0 disables monitoring */
87#define BOND_LINK_MON_INTERV 0
88#define BOND_LINK_ARP_INTERV 0
89
90static int max_bonds = BOND_DEFAULT_MAX_BONDS;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +000091static int tx_queues = BOND_DEFAULT_TX_QUEUES;
Ben Hutchingsad246c92011-04-26 15:25:52 +000092static int num_peer_notif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static int miimon = BOND_LINK_MON_INTERV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +000094static int updelay;
95static int downdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static int use_carrier = 1;
Stephen Hemminger3d632c32009-06-12 19:02:48 +000097static char *mode;
98static char *primary;
Jiri Pirkoa5499522009-09-25 03:28:09 +000099static char *primary_reselect;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000100static char *lacp_rate;
101static char *ad_select;
102static char *xmit_hash_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static int arp_interval = BOND_LINK_ARP_INTERV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000104static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
105static char *arp_validate;
106static char *fail_over_mac;
Andy Gospodarekebd8e492010-06-02 08:39:21 +0000107static int all_slaves_active = 0;
Stephen Hemmingerd2991f72009-06-12 19:02:44 +0000108static struct bond_params bonding_defaults;
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000109static int resend_igmp = BOND_DEFAULT_RESEND_IGMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111module_param(max_bonds, int, 0);
112MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
Andy Gospodarekbb1d9122010-06-02 08:40:18 +0000113module_param(tx_queues, int, 0);
114MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)");
Ben Hutchingsad246c92011-04-26 15:25:52 +0000115module_param_named(num_grat_arp, num_peer_notif, int, 0644);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000116MODULE_PARM_DESC(num_grat_arp, "Number of peer notifications to send on "
117 "failover event (alias of num_unsol_na)");
Ben Hutchingsad246c92011-04-26 15:25:52 +0000118module_param_named(num_unsol_na, num_peer_notif, int, 0644);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000119MODULE_PARM_DESC(num_unsol_na, "Number of peer notifications to send on "
120 "failover event (alias of num_grat_arp)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121module_param(miimon, int, 0);
122MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
123module_param(updelay, int, 0);
124MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
125module_param(downdelay, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800126MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
127 "in milliseconds");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128module_param(use_carrier, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800129MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
130 "0 for off, 1 for on (default)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131module_param(mode, charp, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000132MODULE_PARM_DESC(mode, "Mode of operation; 0 for balance-rr, "
Mitch Williams2ac47662005-11-09 10:35:03 -0800133 "1 for active-backup, 2 for balance-xor, "
134 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
135 "6 for balance-alb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136module_param(primary, charp, 0);
137MODULE_PARM_DESC(primary, "Primary network device to use");
Jiri Pirkoa5499522009-09-25 03:28:09 +0000138module_param(primary_reselect, charp, 0);
139MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
140 "once it comes up; "
141 "0 for always (default), "
142 "1 for only if speed of primary is "
143 "better, "
144 "2 for only on active slave "
145 "failure");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146module_param(lacp_rate, charp, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000147MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner; "
148 "0 for slow, 1 for fast");
Jay Vosburghfd989c82008-11-04 17:51:16 -0800149module_param(ad_select, charp, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000150MODULE_PARM_DESC(ad_select, "803.ad aggregation selection logic; "
151 "0 for stable (default), 1 for bandwidth, "
152 "2 for count");
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400153module_param(xmit_hash_policy, charp, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000154MODULE_PARM_DESC(xmit_hash_policy, "balance-xor and 802.3ad hashing method; "
155 "0 for layer 2 (default), 1 for layer 3+4, "
156 "2 for layer 2+3");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157module_param(arp_interval, int, 0);
158MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
159module_param_array(arp_ip_target, charp, NULL, 0);
160MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700161module_param(arp_validate, charp, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000162MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes; "
163 "0 for none (default), 1 for active, "
164 "2 for backup, 3 for all");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700165module_param(fail_over_mac, charp, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000166MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to "
167 "the same MAC; 0 for none (default), "
168 "1 for active, 2 for follow");
Andy Gospodarekebd8e492010-06-02 08:39:21 +0000169module_param(all_slaves_active, int, 0);
170MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface"
Andy Gospodarek90e62472011-05-25 04:41:59 +0000171 "by setting active flag for all slaves; "
Andy Gospodarekebd8e492010-06-02 08:39:21 +0000172 "0 for never (default), 1 for always.");
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000173module_param(resend_igmp, int, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000174MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on "
175 "link failure");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177/*----------------------------- Global variables ----------------------------*/
178
Neil Hormane843fa52010-10-13 16:01:50 +0000179#ifdef CONFIG_NET_POLL_CONTROLLER
Neil Hormanfb4fa762010-12-06 09:05:50 +0000180atomic_t netpoll_block_tx = ATOMIC_INIT(0);
Neil Hormane843fa52010-10-13 16:01:50 +0000181#endif
182
Eric Dumazetf99189b2009-11-17 10:42:49 +0000183int bond_net_id __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000185static __be32 arp_target[BOND_MAX_ARP_TARGETS];
186static int arp_ip_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static int bond_mode = BOND_MODE_ROUNDROBIN;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000188static int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
189static int lacp_fast;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800191const struct bond_parm_tbl bond_lacp_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{ "slow", AD_LACP_SLOW},
193{ "fast", AD_LACP_FAST},
194{ NULL, -1},
195};
196
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800197const struct bond_parm_tbl bond_mode_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{ "balance-rr", BOND_MODE_ROUNDROBIN},
199{ "active-backup", BOND_MODE_ACTIVEBACKUP},
200{ "balance-xor", BOND_MODE_XOR},
201{ "broadcast", BOND_MODE_BROADCAST},
202{ "802.3ad", BOND_MODE_8023AD},
203{ "balance-tlb", BOND_MODE_TLB},
204{ "balance-alb", BOND_MODE_ALB},
205{ NULL, -1},
206};
207
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800208const struct bond_parm_tbl xmit_hashtype_tbl[] = {
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400209{ "layer2", BOND_XMIT_POLICY_LAYER2},
210{ "layer3+4", BOND_XMIT_POLICY_LAYER34},
Jay Vosburgh6f6652b2007-12-06 23:40:34 -0800211{ "layer2+3", BOND_XMIT_POLICY_LAYER23},
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400212{ NULL, -1},
213};
214
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800215const struct bond_parm_tbl arp_validate_tbl[] = {
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700216{ "none", BOND_ARP_VALIDATE_NONE},
217{ "active", BOND_ARP_VALIDATE_ACTIVE},
218{ "backup", BOND_ARP_VALIDATE_BACKUP},
219{ "all", BOND_ARP_VALIDATE_ALL},
220{ NULL, -1},
221};
222
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800223const struct bond_parm_tbl fail_over_mac_tbl[] = {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700224{ "none", BOND_FOM_NONE},
225{ "active", BOND_FOM_ACTIVE},
226{ "follow", BOND_FOM_FOLLOW},
227{ NULL, -1},
228};
229
Jiri Pirkoa5499522009-09-25 03:28:09 +0000230const struct bond_parm_tbl pri_reselect_tbl[] = {
231{ "always", BOND_PRI_RESELECT_ALWAYS},
232{ "better", BOND_PRI_RESELECT_BETTER},
233{ "failure", BOND_PRI_RESELECT_FAILURE},
234{ NULL, -1},
235};
236
Jay Vosburghfd989c82008-11-04 17:51:16 -0800237struct bond_parm_tbl ad_select_tbl[] = {
238{ "stable", BOND_AD_STABLE},
239{ "bandwidth", BOND_AD_BANDWIDTH},
240{ "count", BOND_AD_COUNT},
241{ NULL, -1},
242};
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/*-------------------------- Forward declarations ---------------------------*/
245
Stephen Hemminger181470f2009-06-12 19:02:52 +0000246static int bond_init(struct net_device *bond_dev);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +0000247static void bond_uninit(struct net_device *bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249/*---------------------------- General routines -----------------------------*/
250
Amerigo Wangbd33acc2011-03-06 21:58:46 +0000251const char *bond_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800253 static const char *names[] = {
254 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
255 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
256 [BOND_MODE_XOR] = "load balancing (xor)",
257 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000258 [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800259 [BOND_MODE_TLB] = "transmit load balancing",
260 [BOND_MODE_ALB] = "adaptive load balancing",
261 };
262
263 if (mode < 0 || mode > BOND_MODE_ALB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return "unknown";
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800265
266 return names[mode];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269/*---------------------------------- VLAN -----------------------------------*/
270
271/**
272 * bond_add_vlan - add a new vlan id on bond
273 * @bond: bond that got the notification
274 * @vlan_id: the vlan id to add
275 *
276 * Returns -ENOMEM if allocation failed.
277 */
278static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
279{
280 struct vlan_entry *vlan;
281
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800282 pr_debug("bond: %s, vlan id %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800283 (bond ? bond->dev->name : "None"), vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Brian Haley305d5522008-11-04 17:51:14 -0800285 vlan = kzalloc(sizeof(struct vlan_entry), GFP_KERNEL);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000286 if (!vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 INIT_LIST_HEAD(&vlan->vlan_list);
290 vlan->vlan_id = vlan_id;
291
292 write_lock_bh(&bond->lock);
293
294 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
295
296 write_unlock_bh(&bond->lock);
297
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800298 pr_debug("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 return 0;
301}
302
303/**
304 * bond_del_vlan - delete a vlan id from bond
305 * @bond: bond that got the notification
306 * @vlan_id: the vlan id to delete
307 *
308 * returns -ENODEV if @vlan_id was not found in @bond.
309 */
310static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
311{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700312 struct vlan_entry *vlan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 int res = -ENODEV;
314
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800315 pr_debug("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Neil Hormane843fa52010-10-13 16:01:50 +0000317 block_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 write_lock_bh(&bond->lock);
319
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700320 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (vlan->vlan_id == vlan_id) {
322 list_del(&vlan->vlan_list);
323
Holger Eitzenberger58402052008-12-09 23:07:13 -0800324 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 bond_alb_clear_vlan(bond, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800327 pr_debug("removed VLAN ID %d from bond %s\n",
328 vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 kfree(vlan);
331
332 if (list_empty(&bond->vlan_list) &&
333 (bond->slave_cnt == 0)) {
334 /* Last VLAN removed and no slaves, so
335 * restore block on adding VLANs. This will
336 * be removed once new slaves that are not
337 * VLAN challenged will be added.
338 */
339 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
340 }
341
342 res = 0;
343 goto out;
344 }
345 }
346
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800347 pr_debug("couldn't find VLAN ID %d in bond %s\n",
348 vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350out:
351 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +0000352 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return res;
354}
355
356/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 * bond_next_vlan - safely skip to the next item in the vlans list.
358 * @bond: the bond we're working on
359 * @curr: item we're advancing from
360 *
361 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
362 * or @curr->next otherwise (even if it is @curr itself again).
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000363 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 * Caller must hold bond->lock
365 */
366struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
367{
368 struct vlan_entry *next, *last;
369
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000370 if (list_empty(&bond->vlan_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 if (!curr) {
374 next = list_entry(bond->vlan_list.next,
375 struct vlan_entry, vlan_list);
376 } else {
377 last = list_entry(bond->vlan_list.prev,
378 struct vlan_entry, vlan_list);
379 if (last == curr) {
380 next = list_entry(bond->vlan_list.next,
381 struct vlan_entry, vlan_list);
382 } else {
383 next = list_entry(curr->vlan_list.next,
384 struct vlan_entry, vlan_list);
385 }
386 }
387
388 return next;
389}
390
391/**
392 * bond_dev_queue_xmit - Prepare skb for xmit.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000393 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 * @bond: bond device that got this skb for tx.
395 * @skb: hw accel VLAN tagged skb to transmit
396 * @slave_dev: slave that is supposed to xmit this skbuff
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000398int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
399 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Ben Hutchings83874512010-12-13 08:19:28 +0000401 skb->dev = slave_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 skb->priority = 1;
Amerigo Wang080e4132011-02-17 23:43:33 +0000403 if (unlikely(netpoll_tx_running(slave_dev)))
Amerigo Wang8a8efa22011-02-17 23:43:32 +0000404 bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
Amerigo Wang080e4132011-02-17 23:43:33 +0000405 else
WANG Congf6dc31a2010-05-06 00:48:51 -0700406 dev_queue_xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 return 0;
409}
410
411/*
412 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
413 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
414 * lock because:
415 * a. This operation is performed in IOCTL context,
416 * b. The operation is protected by the RTNL semaphore in the 8021q code,
417 * c. Holding a lock with BH disabled while directly calling a base driver
418 * entry point is generally a BAD idea.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000419 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 * The design of synchronization/protection for this operation in the 8021q
421 * module is good for one or more VLAN devices over a single physical device
422 * and cannot be extended for a teaming solution like bonding, so there is a
423 * potential race condition here where a net device from the vlan group might
424 * be referenced (either by a base driver or the 8021q code) while it is being
425 * removed from the system. However, it turns out we're not making matters
426 * worse, and if it works for regular VLAN usage it will work here too.
427*/
428
429/**
430 * bond_vlan_rx_register - Propagates registration to slaves
431 * @bond_dev: bonding net device that got called
432 * @grp: vlan group being registered
433 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000434static void bond_vlan_rx_register(struct net_device *bond_dev,
435 struct vlan_group *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Wang Chen454d7c92008-11-12 23:37:49 -0800437 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 struct slave *slave;
439 int i;
440
Jarek Poplawskia71fb882010-10-27 07:08:22 +0000441 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 bond->vlgrp = grp;
Jarek Poplawskia71fb882010-10-27 07:08:22 +0000443 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 bond_for_each_slave(bond, slave, i) {
446 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800447 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800450 slave_ops->ndo_vlan_rx_register) {
451 slave_ops->ndo_vlan_rx_register(slave_dev, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453 }
454}
455
456/**
457 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
458 * @bond_dev: bonding net device that got called
459 * @vid: vlan id being added
460 */
461static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
462{
Wang Chen454d7c92008-11-12 23:37:49 -0800463 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 struct slave *slave;
465 int i, res;
466
467 bond_for_each_slave(bond, slave, i) {
468 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800469 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800472 slave_ops->ndo_vlan_rx_add_vid) {
473 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475 }
476
477 res = bond_add_vlan(bond, vid);
478 if (res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800479 pr_err("%s: Error: Failed to add vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 bond_dev->name, vid);
481 }
482}
483
484/**
485 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
486 * @bond_dev: bonding net device that got called
487 * @vid: vlan id being removed
488 */
489static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
490{
Wang Chen454d7c92008-11-12 23:37:49 -0800491 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 struct slave *slave;
493 struct net_device *vlan_dev;
494 int i, res;
495
496 bond_for_each_slave(bond, slave, i) {
497 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800498 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800501 slave_ops->ndo_vlan_rx_kill_vid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /* Save and then restore vlan_dev in the grp array,
503 * since the slave's driver might clear it.
504 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800505 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800506 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vid);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800507 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509 }
510
511 res = bond_del_vlan(bond, vid);
512 if (res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800513 pr_err("%s: Error: Failed to remove vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 bond_dev->name, vid);
515 }
516}
517
518static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
519{
520 struct vlan_entry *vlan;
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
Jay Vosburghf35188f2010-07-21 12:14:47 +0000523 if (!bond->vlgrp)
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000524 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800527 slave_ops->ndo_vlan_rx_register)
528 slave_ops->ndo_vlan_rx_register(slave_dev, bond->vlgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800531 !(slave_ops->ndo_vlan_rx_add_vid))
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000532 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800534 list_for_each_entry(vlan, &bond->vlan_list, vlan_list)
535 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vlan->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000538static void bond_del_vlans_from_slave(struct bonding *bond,
539 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800541 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct vlan_entry *vlan;
543 struct net_device *vlan_dev;
544
Jay Vosburghf35188f2010-07-21 12:14:47 +0000545 if (!bond->vlgrp)
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000546 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800549 !(slave_ops->ndo_vlan_rx_kill_vid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 goto unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +0000553 if (!vlan->vlan_id)
554 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* Save and then restore vlan_dev in the grp array,
556 * since the slave's driver might clear it.
557 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800558 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800559 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800560 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562
563unreg:
564 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800565 slave_ops->ndo_vlan_rx_register)
566 slave_ops->ndo_vlan_rx_register(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
569/*------------------------------- Link status -------------------------------*/
570
571/*
Jay Vosburghff59c452006-03-27 13:27:43 -0800572 * Set the carrier state for the master according to the state of its
573 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
574 * do special 802.3ad magic.
575 *
576 * Returns zero if carrier state does not change, nonzero if it does.
577 */
578static int bond_set_carrier(struct bonding *bond)
579{
580 struct slave *slave;
581 int i;
582
583 if (bond->slave_cnt == 0)
584 goto down;
585
586 if (bond->params.mode == BOND_MODE_8023AD)
587 return bond_3ad_set_carrier(bond);
588
589 bond_for_each_slave(bond, slave, i) {
590 if (slave->link == BOND_LINK_UP) {
591 if (!netif_carrier_ok(bond->dev)) {
592 netif_carrier_on(bond->dev);
593 return 1;
594 }
595 return 0;
596 }
597 }
598
599down:
600 if (netif_carrier_ok(bond->dev)) {
601 netif_carrier_off(bond->dev);
602 return 1;
603 }
604 return 0;
605}
606
607/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * Get link speed and duplex from the slave's base driver
609 * using ethtool. If for some reason the call fails or the
610 * values are invalid, fake speed and duplex to 100/Full
611 * and return error.
612 */
613static int bond_update_speed_duplex(struct slave *slave)
614{
615 struct net_device *slave_dev = slave->dev;
David Decotigny5d305302011-04-13 15:22:31 +0000616 struct ethtool_cmd etool = { .cmd = ETHTOOL_GSET };
617 u32 slave_speed;
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700618 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 /* Fake speed and duplex */
621 slave->speed = SPEED_100;
622 slave->duplex = DUPLEX_FULL;
623
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700624 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700627 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
628 if (res < 0)
629 return -1;
630
David Decotigny5d305302011-04-13 15:22:31 +0000631 slave_speed = ethtool_cmd_speed(&etool);
632 switch (slave_speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 case SPEED_10:
634 case SPEED_100:
635 case SPEED_1000:
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700636 case SPEED_10000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 break;
638 default:
639 return -1;
640 }
641
642 switch (etool.duplex) {
643 case DUPLEX_FULL:
644 case DUPLEX_HALF:
645 break;
646 default:
647 return -1;
648 }
649
David Decotigny5d305302011-04-13 15:22:31 +0000650 slave->speed = slave_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 slave->duplex = etool.duplex;
652
653 return 0;
654}
655
656/*
657 * if <dev> supports MII link status reporting, check its link status.
658 *
659 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000660 * depending upon the setting of the use_carrier parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 *
662 * Return either BMSR_LSTATUS, meaning that the link is up (or we
663 * can't tell and just pretend it is), or 0, meaning that the link is
664 * down.
665 *
666 * If reporting is non-zero, instead of faking link up, return -1 if
667 * both ETHTOOL and MII ioctls fail (meaning the device does not
668 * support them). If use_carrier is set, return whatever it says.
669 * It'd be nice if there was a good way to tell if a driver supports
670 * netif_carrier, but there really isn't.
671 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000672static int bond_check_dev_link(struct bonding *bond,
673 struct net_device *slave_dev, int reporting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800675 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Jiri Bohacd9d52832009-10-28 22:23:54 -0700676 int (*ioctl)(struct net_device *, struct ifreq *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 struct ifreq ifr;
678 struct mii_ioctl_data *mii;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Petri Gynther6c988852009-08-28 12:05:15 +0000680 if (!reporting && !netif_running(slave_dev))
681 return 0;
682
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800683 if (bond->params.use_carrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Jiri Pirko29112f42009-04-24 01:58:23 +0000686 /* Try to get link status using Ethtool first. */
687 if (slave_dev->ethtool_ops) {
688 if (slave_dev->ethtool_ops->get_link) {
689 u32 link;
690
691 link = slave_dev->ethtool_ops->get_link(slave_dev);
692
693 return link ? BMSR_LSTATUS : 0;
694 }
695 }
696
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000697 /* Ethtool can't be used, fallback to MII ioctls. */
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800698 ioctl = slave_ops->ndo_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (ioctl) {
700 /* TODO: set pointer to correct ioctl on a per team member */
701 /* bases to make this more efficient. that is, once */
702 /* we determine the correct ioctl, we will always */
703 /* call it and not the others for that team */
704 /* member. */
705
706 /*
707 * We cannot assume that SIOCGMIIPHY will also read a
708 * register; not all network drivers (e.g., e100)
709 * support that.
710 */
711
712 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
713 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
714 mii = if_mii(&ifr);
715 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
716 mii->reg_num = MII_BMSR;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000717 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0)
718 return mii->val_out & BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720 }
721
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700722 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 * If reporting, report that either there's no dev->do_ioctl,
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700724 * or both SIOCGMIIREG and get_link failed (meaning that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 * cannot report link status). If not reporting, pretend
726 * we're ok.
727 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000728 return reporting ? -1 : BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
731/*----------------------------- Multicast list ------------------------------*/
732
733/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 * Push the promiscuity flag down to appropriate slaves
735 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700736static int bond_set_promiscuity(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700738 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (USES_PRIMARY(bond->params.mode)) {
740 /* write lock already acquired */
741 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700742 err = dev_set_promiscuity(bond->curr_active_slave->dev,
743 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745 } else {
746 struct slave *slave;
747 int i;
748 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700749 err = dev_set_promiscuity(slave->dev, inc);
750 if (err)
751 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700754 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
757/*
758 * Push the allmulti flag down to all slaves
759 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700760static int bond_set_allmulti(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700762 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (USES_PRIMARY(bond->params.mode)) {
764 /* write lock already acquired */
765 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700766 err = dev_set_allmulti(bond->curr_active_slave->dev,
767 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769 } else {
770 struct slave *slave;
771 int i;
772 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700773 err = dev_set_allmulti(slave->dev, inc);
774 if (err)
775 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700778 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
781/*
782 * Add a Multicast address to slaves
783 * according to mode
784 */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000785static void bond_mc_add(struct bonding *bond, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 if (USES_PRIMARY(bond->params.mode)) {
788 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000789 if (bond->curr_active_slave)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000790 dev_mc_add(bond->curr_active_slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 } else {
792 struct slave *slave;
793 int i;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000794
795 bond_for_each_slave(bond, slave, i)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000796 dev_mc_add(slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798}
799
800/*
801 * Remove a multicast address from slave
802 * according to mode
803 */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000804static void bond_mc_del(struct bonding *bond, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 if (USES_PRIMARY(bond->params.mode)) {
807 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000808 if (bond->curr_active_slave)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000809 dev_mc_del(bond->curr_active_slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 } else {
811 struct slave *slave;
812 int i;
813 bond_for_each_slave(bond, slave, i) {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000814 dev_mc_del(slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816 }
817}
818
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800819
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000820static void __bond_resend_igmp_join_requests(struct net_device *dev)
821{
822 struct in_device *in_dev;
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000823
824 rcu_read_lock();
825 in_dev = __in_dev_get_rcu(dev);
Eric Dumazet866f3b22010-11-18 09:33:19 -0800826 if (in_dev)
David S. Miller24912422010-11-19 13:13:47 -0800827 ip_mc_rejoin_groups(in_dev);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000828 rcu_read_unlock();
829}
830
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800831/*
832 * Retrieve the list of registered multicast addresses for the bonding
833 * device and retransmit an IGMP JOIN request to the current active
834 * slave.
835 */
836static void bond_resend_igmp_join_requests(struct bonding *bond)
837{
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000838 struct net_device *vlan_dev;
839 struct vlan_entry *vlan;
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800840
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000841 read_lock(&bond->lock);
842
843 /* rejoin all groups on bond device */
844 __bond_resend_igmp_join_requests(bond->dev);
845
846 /* rejoin all groups on vlan devices */
847 if (bond->vlgrp) {
848 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
849 vlan_dev = vlan_group_get_device(bond->vlgrp,
850 vlan->vlan_id);
851 if (vlan_dev)
852 __bond_resend_igmp_join_requests(vlan_dev);
853 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800854 }
855
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000856 if (--bond->igmp_retrans > 0)
857 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
858
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000859 read_unlock(&bond->lock);
860}
861
stephen hemminger379b7382010-10-15 11:02:56 +0000862static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000863{
864 struct bonding *bond = container_of(work, struct bonding,
Flavio Leitner94265cf2011-05-25 08:38:58 +0000865 mcast_work.work);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000866 bond_resend_igmp_join_requests(bond);
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800867}
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 * flush all members of flush->mc_list from device dev->mc_list
871 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000872static void bond_mc_list_flush(struct net_device *bond_dev,
873 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Wang Chen454d7c92008-11-12 23:37:49 -0800875 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000876 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Jiri Pirko22bedad32010-04-01 21:22:57 +0000878 netdev_for_each_mc_addr(ha, bond_dev)
879 dev_mc_del(slave_dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 if (bond->params.mode == BOND_MODE_8023AD) {
882 /* del lacpdu mc addr from mc list */
883 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
884
Jiri Pirko22bedad32010-04-01 21:22:57 +0000885 dev_mc_del(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887}
888
889/*--------------------------- Active slave change ---------------------------*/
890
891/*
892 * Update the mc list and multicast-related flags for the new and
893 * old active slaves (if any) according to the multicast mode, and
894 * promiscuous flags unconditionally.
895 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000896static void bond_mc_swap(struct bonding *bond, struct slave *new_active,
897 struct slave *old_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Jiri Pirko22bedad32010-04-01 21:22:57 +0000899 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000901 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /* nothing to do - mc list is already up-to-date on
903 * all slaves
904 */
905 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 if (old_active) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000908 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 dev_set_promiscuity(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000911 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 dev_set_allmulti(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Jiri Pirko22bedad32010-04-01 21:22:57 +0000914 netdev_for_each_mc_addr(ha, bond->dev)
915 dev_mc_del(old_active->dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917
918 if (new_active) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700919 /* FIXME: Signal errors upstream. */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000920 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 dev_set_promiscuity(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000923 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 dev_set_allmulti(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Jiri Pirko22bedad32010-04-01 21:22:57 +0000926 netdev_for_each_mc_addr(ha, bond->dev)
927 dev_mc_add(new_active->dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929}
930
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700931/*
932 * bond_do_fail_over_mac
933 *
934 * Perform special MAC address swapping for fail_over_mac settings
935 *
936 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
937 */
938static void bond_do_fail_over_mac(struct bonding *bond,
939 struct slave *new_active,
940 struct slave *old_active)
Hannes Eder1f78d9f2009-02-14 11:15:33 +0000941 __releases(&bond->curr_slave_lock)
942 __releases(&bond->lock)
943 __acquires(&bond->lock)
944 __acquires(&bond->curr_slave_lock)
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700945{
946 u8 tmp_mac[ETH_ALEN];
947 struct sockaddr saddr;
948 int rv;
949
950 switch (bond->params.fail_over_mac) {
951 case BOND_FOM_ACTIVE:
952 if (new_active)
953 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
954 new_active->dev->addr_len);
955 break;
956 case BOND_FOM_FOLLOW:
957 /*
958 * if new_active && old_active, swap them
959 * if just old_active, do nothing (going to no active slave)
960 * if just new_active, set new_active to bond's MAC
961 */
962 if (!new_active)
963 return;
964
965 write_unlock_bh(&bond->curr_slave_lock);
966 read_unlock(&bond->lock);
967
968 if (old_active) {
969 memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
970 memcpy(saddr.sa_data, old_active->dev->dev_addr,
971 ETH_ALEN);
972 saddr.sa_family = new_active->dev->type;
973 } else {
974 memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
975 saddr.sa_family = bond->dev->type;
976 }
977
978 rv = dev_set_mac_address(new_active->dev, &saddr);
979 if (rv) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800980 pr_err("%s: Error %d setting MAC of slave %s\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700981 bond->dev->name, -rv, new_active->dev->name);
982 goto out;
983 }
984
985 if (!old_active)
986 goto out;
987
988 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
989 saddr.sa_family = old_active->dev->type;
990
991 rv = dev_set_mac_address(old_active->dev, &saddr);
992 if (rv)
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800993 pr_err("%s: Error %d setting MAC of slave %s\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700994 bond->dev->name, -rv, new_active->dev->name);
995out:
996 read_lock(&bond->lock);
997 write_lock_bh(&bond->curr_slave_lock);
998 break;
999 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001000 pr_err("%s: bond_do_fail_over_mac impossible: bad policy %d\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001001 bond->dev->name, bond->params.fail_over_mac);
1002 break;
1003 }
1004
1005}
1006
Jiri Pirkoa5499522009-09-25 03:28:09 +00001007static bool bond_should_change_active(struct bonding *bond)
1008{
1009 struct slave *prim = bond->primary_slave;
1010 struct slave *curr = bond->curr_active_slave;
1011
1012 if (!prim || !curr || curr->link != BOND_LINK_UP)
1013 return true;
1014 if (bond->force_primary) {
1015 bond->force_primary = false;
1016 return true;
1017 }
1018 if (bond->params.primary_reselect == BOND_PRI_RESELECT_BETTER &&
1019 (prim->speed < curr->speed ||
1020 (prim->speed == curr->speed && prim->duplex <= curr->duplex)))
1021 return false;
1022 if (bond->params.primary_reselect == BOND_PRI_RESELECT_FAILURE)
1023 return false;
1024 return true;
1025}
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027/**
1028 * find_best_interface - select the best available slave to be the active one
1029 * @bond: our bonding struct
1030 *
1031 * Warning: Caller must hold curr_slave_lock for writing.
1032 */
1033static struct slave *bond_find_best_slave(struct bonding *bond)
1034{
1035 struct slave *new_active, *old_active;
1036 struct slave *bestslave = NULL;
1037 int mintime = bond->params.updelay;
1038 int i;
1039
Nicolas de Pesloüan49b4ad92009-10-07 14:11:00 -07001040 new_active = bond->curr_active_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 if (!new_active) { /* there were no active slaves left */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001043 if (bond->slave_cnt > 0) /* found one slave */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 new_active = bond->first_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001045 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return NULL; /* still no slave, return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if ((bond->primary_slave) &&
Jiri Pirkoa5499522009-09-25 03:28:09 +00001050 bond->primary_slave->link == BOND_LINK_UP &&
1051 bond_should_change_active(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 new_active = bond->primary_slave;
1053 }
1054
1055 /* remember where to stop iterating over the slaves */
1056 old_active = new_active;
1057
1058 bond_for_each_slave_from(bond, new_active, i, old_active) {
Jiri Pirkob9f60252009-08-31 11:09:38 +00001059 if (new_active->link == BOND_LINK_UP) {
1060 return new_active;
1061 } else if (new_active->link == BOND_LINK_BACK &&
1062 IS_UP(new_active->dev)) {
1063 /* link up, but waiting for stabilization */
1064 if (new_active->delay < mintime) {
1065 mintime = new_active->delay;
1066 bestslave = new_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068 }
1069 }
1070
1071 return bestslave;
1072}
1073
Ben Hutchingsad246c92011-04-26 15:25:52 +00001074static bool bond_should_notify_peers(struct bonding *bond)
1075{
1076 struct slave *slave = bond->curr_active_slave;
1077
1078 pr_debug("bond_should_notify_peers: bond %s slave %s\n",
1079 bond->dev->name, slave ? slave->dev->name : "NULL");
1080
1081 if (!slave || !bond->send_peer_notif ||
1082 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
1083 return false;
1084
1085 bond->send_peer_notif--;
1086 return true;
1087}
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089/**
1090 * change_active_interface - change the active slave into the specified one
1091 * @bond: our bonding struct
1092 * @new: the new slave to make the active one
1093 *
1094 * Set the new slave to the bond's settings and unset them on the old
1095 * curr_active_slave.
1096 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1097 *
1098 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1099 * because it is apparently the best available slave we have, even though its
1100 * updelay hasn't timed out yet.
1101 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001102 * If new_active is not NULL, caller must hold bond->lock for read and
1103 * curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001105void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
1107 struct slave *old_active = bond->curr_active_slave;
1108
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001109 if (old_active == new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 if (new_active) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07001113 new_active->jiffies = jiffies;
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (new_active->link == BOND_LINK_BACK) {
1116 if (USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001117 pr_info("%s: making interface %s the new active one %d ms earlier.\n",
1118 bond->dev->name, new_active->dev->name,
1119 (bond->params.updelay - new_active->delay) * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121
1122 new_active->delay = 0;
1123 new_active->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001125 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Holger Eitzenberger58402052008-12-09 23:07:13 -08001128 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 } else {
1131 if (USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001132 pr_info("%s: making interface %s the new active one.\n",
1133 bond->dev->name, new_active->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135 }
1136 }
1137
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001138 if (USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 bond_mc_swap(bond, new_active, old_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Holger Eitzenberger58402052008-12-09 23:07:13 -08001141 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 bond_alb_handle_active_change(bond, new_active);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001143 if (old_active)
1144 bond_set_slave_inactive_flags(old_active);
1145 if (new_active)
1146 bond_set_slave_active_flags(new_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 } else {
1148 bond->curr_active_slave = new_active;
1149 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001150
1151 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001152 if (old_active)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001153 bond_set_slave_inactive_flags(old_active);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001154
1155 if (new_active) {
Ben Hutchingsad246c92011-04-26 15:25:52 +00001156 bool should_notify_peers = false;
1157
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001158 bond_set_slave_active_flags(new_active);
Moni Shoua2ab82852007-10-09 19:43:39 -07001159
Or Gerlitz709f8a42008-06-13 18:12:01 -07001160 if (bond->params.fail_over_mac)
1161 bond_do_fail_over_mac(bond, new_active,
1162 old_active);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001163
Ben Hutchingsad246c92011-04-26 15:25:52 +00001164 if (netif_running(bond->dev)) {
1165 bond->send_peer_notif =
1166 bond->params.num_peer_notif;
1167 should_notify_peers =
1168 bond_should_notify_peers(bond);
1169 }
1170
Or Gerlitz01f31092008-06-13 18:12:02 -07001171 write_unlock_bh(&bond->curr_slave_lock);
1172 read_unlock(&bond->lock);
1173
Moni Shoua75c78502009-09-15 02:37:40 -07001174 netdev_bonding_change(bond->dev, NETDEV_BONDING_FAILOVER);
Ben Hutchingsad246c92011-04-26 15:25:52 +00001175 if (should_notify_peers)
1176 netdev_bonding_change(bond->dev,
1177 NETDEV_NOTIFY_PEERS);
Or Gerlitz01f31092008-06-13 18:12:02 -07001178
1179 read_lock(&bond->lock);
1180 write_lock_bh(&bond->curr_slave_lock);
Moni Shoua7893b242008-05-17 21:10:12 -07001181 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001182 }
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001183
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001184 /* resend IGMP joins since active slave has changed or
Flavio Leitner94265cf2011-05-25 08:38:58 +00001185 * all were sent on curr_active_slave.
1186 * resend only if bond is brought up with the affected
1187 * bonding modes and the retransmission is enabled */
1188 if (netif_running(bond->dev) && (bond->params.resend_igmp > 0) &&
1189 ((USES_PRIMARY(bond->params.mode) && new_active) ||
1190 bond->params.mode == BOND_MODE_ROUNDROBIN)) {
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) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001355 err = slave_enable_netpoll(slave);
1356 if (err) {
1357 __bond_netpoll_cleanup(bond);
1358 break;
1359 }
1360 }
1361 read_unlock(&bond->lock);
1362 return err;
1363}
1364
1365static struct netpoll_info *bond_netpoll_info(struct bonding *bond)
1366{
1367 return bond->dev->npinfo;
1368}
1369
1370#else
1371static inline int slave_enable_netpoll(struct slave *slave)
1372{
1373 return 0;
1374}
1375static inline void slave_disable_netpoll(struct slave *slave)
1376{
1377}
WANG Congf6dc31a2010-05-06 00:48:51 -07001378static void bond_netpoll_cleanup(struct net_device *bond_dev)
1379{
1380}
WANG Congf6dc31a2010-05-06 00:48:51 -07001381#endif
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383/*---------------------------------- IOCTL ----------------------------------*/
1384
Adrian Bunk4ad072c2007-07-09 11:51:12 -07001385static int bond_sethwaddr(struct net_device *bond_dev,
1386 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001388 pr_debug("bond_dev=%p\n", bond_dev);
1389 pr_debug("slave_dev=%p\n", slave_dev);
1390 pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1392 return 0;
1393}
1394
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001395static u32 bond_fix_features(struct net_device *dev, u32 features)
1396{
1397 struct slave *slave;
1398 struct bonding *bond = netdev_priv(dev);
1399 u32 mask;
1400 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001401
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001402 read_lock(&bond->lock);
1403
1404 if (!bond->first_slave) {
1405 /* Disable adding VLANs to empty bond. But why? --mq */
1406 features |= NETIF_F_VLAN_CHALLENGED;
1407 goto out;
1408 }
1409
1410 mask = features;
1411 features &= ~NETIF_F_ONE_FOR_ALL;
1412 features |= NETIF_F_ALL_FOR_ALL;
1413
1414 bond_for_each_slave(bond, slave, i) {
1415 features = netdev_increment_features(features,
1416 slave->dev->features,
1417 mask);
1418 }
1419
1420out:
1421 read_unlock(&bond->lock);
1422 return features;
1423}
1424
1425#define BOND_VLAN_FEATURES (NETIF_F_ALL_TX_OFFLOADS | \
1426 NETIF_F_SOFT_FEATURES | \
1427 NETIF_F_LRO)
1428
1429static void bond_compute_features(struct bonding *bond)
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001430{
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001431 struct slave *slave;
1432 struct net_device *bond_dev = bond->dev;
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001433 u32 vlan_features = BOND_VLAN_FEATURES;
1434 unsigned short max_hard_header_len = ETH_HLEN;
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001435 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001436
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001437 read_lock(&bond->lock);
Herbert Xub63365a2008-10-23 01:11:29 -07001438
1439 if (!bond->first_slave)
1440 goto done;
1441
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001442 bond_for_each_slave(bond, slave, i) {
Jay Vosburgh278339a2009-08-28 12:05:12 +00001443 vlan_features = netdev_increment_features(vlan_features,
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001444 slave->dev->vlan_features, BOND_VLAN_FEATURES);
1445
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001446 if (slave->dev->hard_header_len > max_hard_header_len)
1447 max_hard_header_len = slave->dev->hard_header_len;
1448 }
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001449
Herbert Xub63365a2008-10-23 01:11:29 -07001450done:
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001451 bond_dev->vlan_features = vlan_features;
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001452 bond_dev->hard_header_len = max_hard_header_len;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001453
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001454 read_unlock(&bond->lock);
1455
1456 netdev_change_features(bond_dev);
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001457}
1458
Moni Shoua872254d2007-10-09 19:43:38 -07001459static void bond_setup_by_slave(struct net_device *bond_dev,
1460 struct net_device *slave_dev)
1461{
Wang Chen454d7c92008-11-12 23:37:49 -08001462 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07001463
Stephen Hemminger00829822008-11-20 20:14:53 -08001464 bond_dev->header_ops = slave_dev->header_ops;
Moni Shoua872254d2007-10-09 19:43:38 -07001465
1466 bond_dev->type = slave_dev->type;
1467 bond_dev->hard_header_len = slave_dev->hard_header_len;
1468 bond_dev->addr_len = slave_dev->addr_len;
1469
1470 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1471 slave_dev->addr_len);
Moni Shouad90a1622007-10-09 19:43:43 -07001472 bond->setup_by_slave = 1;
Moni Shoua872254d2007-10-09 19:43:38 -07001473}
1474
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001475/* On bonding slaves other than the currently active slave, suppress
Jiri Pirko3aba8912011-04-19 03:48:16 +00001476 * duplicates except for alb non-mcast/bcast.
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001477 */
1478static bool bond_should_deliver_exact_match(struct sk_buff *skb,
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001479 struct slave *slave,
1480 struct bonding *bond)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001481{
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001482 if (bond_is_slave_inactive(slave)) {
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001483 if (bond->params.mode == BOND_MODE_ALB &&
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001484 skb->pkt_type != PACKET_BROADCAST &&
1485 skb->pkt_type != PACKET_MULTICAST)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001486 return false;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001487 return true;
1488 }
1489 return false;
1490}
1491
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001492static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001493{
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001494 struct sk_buff *skb = *pskb;
Jiri Pirkof1c17752011-03-12 03:14:35 +00001495 struct slave *slave;
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001496 struct bonding *bond;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001497
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001498 skb = skb_share_check(skb, GFP_ATOMIC);
1499 if (unlikely(!skb))
1500 return RX_HANDLER_CONSUMED;
1501
1502 *pskb = skb;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001503
Jiri Pirko35d48902011-03-22 02:38:12 +00001504 slave = bond_slave_get_rcu(skb->dev);
1505 bond = slave->bond;
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001506
1507 if (bond->params.arp_interval)
Jiri Pirkof1c17752011-03-12 03:14:35 +00001508 slave->dev->last_rx = jiffies;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001509
Jiri Pirko3aba8912011-04-19 03:48:16 +00001510 if (bond->recv_probe) {
1511 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
1512
1513 if (likely(nskb)) {
1514 bond->recv_probe(nskb, bond, slave);
1515 dev_kfree_skb(nskb);
1516 }
1517 }
1518
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001519 if (bond_should_deliver_exact_match(skb, slave, bond)) {
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001520 return RX_HANDLER_EXACT;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001521 }
1522
Jiri Pirko35d48902011-03-22 02:38:12 +00001523 skb->dev = bond->dev;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001524
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001525 if (bond->params.mode == BOND_MODE_ALB &&
Jiri Pirko35d48902011-03-22 02:38:12 +00001526 bond->dev->priv_flags & IFF_BRIDGE_PORT &&
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001527 skb->pkt_type == PACKET_HOST) {
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001528
Changli Gao541ac7c2011-03-02 21:07:14 +00001529 if (unlikely(skb_cow_head(skb,
1530 skb->data - skb_mac_header(skb)))) {
1531 kfree_skb(skb);
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001532 return RX_HANDLER_CONSUMED;
Changli Gao541ac7c2011-03-02 21:07:14 +00001533 }
Jiri Pirko35d48902011-03-22 02:38:12 +00001534 memcpy(eth_hdr(skb)->h_dest, bond->dev->dev_addr, ETH_ALEN);
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001535 }
1536
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001537 return RX_HANDLER_ANOTHER;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001538}
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540/* enslave device <slave> to bond device <master> */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001541int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Wang Chen454d7c92008-11-12 23:37:49 -08001543 struct bonding *bond = netdev_priv(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001544 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 struct slave *new_slave = NULL;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001546 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 struct sockaddr addr;
1548 int link_reporting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 int res = 0;
1550
nsxfreddy@gmail.com552709d2005-09-21 14:18:04 -05001551 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001552 slave_ops->ndo_do_ioctl == NULL) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001553 pr_warning("%s: Warning: no link monitoring support for %s\n",
1554 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 /* already enslaved */
1558 if (slave_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001559 pr_debug("Error, Device was already enslaved\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return -EBUSY;
1561 }
1562
1563 /* vlan challenged mutual exclusion */
1564 /* no need to lock since we're protected by rtnl_lock */
1565 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001566 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Jay Vosburghf35188f2010-07-21 12:14:47 +00001567 if (bond->vlgrp) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001568 pr_err("%s: Error: cannot enslave VLAN challenged slave %s on VLAN enabled bond %s\n",
1569 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return -EPERM;
1571 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001572 pr_warning("%s: Warning: enslaved VLAN challenged slave %s. Adding VLANs will be blocked as long as %s is part of bond %s\n",
1573 bond_dev->name, slave_dev->name,
1574 slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
1576 } else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001577 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
1579
Jay Vosburgh217df672005-09-26 16:11:50 -07001580 /*
1581 * Old ifenslave binaries are no longer supported. These can
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001582 * be identified with moderate accuracy by the state of the slave:
Jay Vosburgh217df672005-09-26 16:11:50 -07001583 * the current ifenslave will set the interface down prior to
1584 * enslaving it; the old ifenslave will not.
1585 */
1586 if ((slave_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001587 pr_err("%s is up. This may be due to an out of date ifenslave.\n",
Jay Vosburgh217df672005-09-26 16:11:50 -07001588 slave_dev->name);
1589 res = -EPERM;
1590 goto err_undo_flags;
1591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Moni Shoua872254d2007-10-09 19:43:38 -07001593 /* set bonding device ether type by slave - bonding netdevices are
1594 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1595 * there is a need to override some of the type dependent attribs/funcs.
1596 *
1597 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1598 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1599 */
1600 if (bond->slave_cnt == 0) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001601 if (bond_dev->type != slave_dev->type) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001602 pr_debug("%s: change device type from %d to %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001603 bond_dev->name,
1604 bond_dev->type, slave_dev->type);
Moni Shoua75c78502009-09-15 02:37:40 -07001605
Jiri Pirko3ca5b402010-03-10 10:29:35 +00001606 res = netdev_bonding_change(bond_dev,
1607 NETDEV_PRE_TYPE_CHANGE);
1608 res = notifier_to_errno(res);
1609 if (res) {
1610 pr_err("%s: refused to change device type\n",
1611 bond_dev->name);
1612 res = -EBUSY;
1613 goto err_undo_flags;
1614 }
Moni Shoua75c78502009-09-15 02:37:40 -07001615
Jiri Pirko32a806c2010-03-19 04:00:23 +00001616 /* Flush unicast and multicast addresses */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001617 dev_uc_flush(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001618 dev_mc_flush(bond_dev);
Jiri Pirko32a806c2010-03-19 04:00:23 +00001619
Moni Shouae36b9d12009-07-15 04:56:31 +00001620 if (slave_dev->type != ARPHRD_ETHER)
1621 bond_setup_by_slave(bond_dev, slave_dev);
1622 else
1623 ether_setup(bond_dev);
Moni Shoua75c78502009-09-15 02:37:40 -07001624
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001625 netdev_bonding_change(bond_dev,
1626 NETDEV_POST_TYPE_CHANGE);
Moni Shouae36b9d12009-07-15 04:56:31 +00001627 }
Moni Shoua872254d2007-10-09 19:43:38 -07001628 } else if (bond_dev->type != slave_dev->type) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001629 pr_err("%s ether type (%d) is different from other slaves (%d), can not enslave it.\n",
1630 slave_dev->name,
1631 slave_dev->type, bond_dev->type);
1632 res = -EINVAL;
1633 goto err_undo_flags;
Moni Shoua872254d2007-10-09 19:43:38 -07001634 }
1635
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001636 if (slave_ops->ndo_set_mac_address == NULL) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001637 if (bond->slave_cnt == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001638 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.",
1639 bond_dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001640 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1641 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001642 pr_err("%s: Error: The slave device specified does not support setting the MAC address, but fail_over_mac is not set to active.\n",
1643 bond_dev->name);
Moni Shoua2ab82852007-10-09 19:43:39 -07001644 res = -EOPNOTSUPP;
1645 goto err_undo_flags;
1646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
1648
Amerigo Wang8d8fc292011-05-19 21:39:10 +00001649 call_netdevice_notifiers(NETDEV_JOIN, slave_dev);
1650
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001651 /* If this is the first slave, then we need to set the master's hardware
1652 * address to be the same as the slave's. */
David Strandd13a2cb2010-12-01 11:43:08 -08001653 if (is_zero_ether_addr(bond->dev->dev_addr))
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001654 memcpy(bond->dev->dev_addr, slave_dev->dev_addr,
1655 slave_dev->addr_len);
1656
1657
Joe Jin243cb4e2007-02-06 14:16:40 -08001658 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (!new_slave) {
1660 res = -ENOMEM;
1661 goto err_undo_flags;
1662 }
1663
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001664 /*
1665 * Set the new_slave's queue_id to be zero. Queue ID mapping
1666 * is set via sysfs or module option if desired.
1667 */
1668 new_slave->queue_id = 0;
1669
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001670 /* Save slave's original mtu and then set it to match the bond */
1671 new_slave->original_mtu = slave_dev->mtu;
1672 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1673 if (res) {
1674 pr_debug("Error %d calling dev_set_mtu\n", res);
1675 goto err_free;
1676 }
1677
Jay Vosburgh217df672005-09-26 16:11:50 -07001678 /*
1679 * Save slave's original ("permanent") mac address for modes
1680 * that need it, and for restoring it upon release, and then
1681 * set it to the master's address
1682 */
1683 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Jay Vosburghdd957c52007-10-09 19:57:24 -07001685 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001686 /*
1687 * Set slave to master's mac address. The application already
1688 * set the master's mac address to that of the first slave
1689 */
1690 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1691 addr.sa_family = slave_dev->type;
1692 res = dev_set_mac_address(slave_dev, &addr);
1693 if (res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001694 pr_debug("Error %d calling set_mac_address\n", res);
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001695 goto err_restore_mtu;
Moni Shoua2ab82852007-10-09 19:43:39 -07001696 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Jiri Pirko1765a572011-02-12 06:48:36 +00001699 res = netdev_set_bond_master(slave_dev, bond_dev);
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001700 if (res) {
Jiri Pirko1765a572011-02-12 06:48:36 +00001701 pr_debug("Error %d calling netdev_set_bond_master\n", res);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001702 goto err_restore_mac;
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001703 }
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001704
Jay Vosburgh217df672005-09-26 16:11:50 -07001705 /* open the slave since the application closed it */
1706 res = dev_open(slave_dev);
1707 if (res) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001708 pr_debug("Opening slave %s failed\n", slave_dev->name);
Jiri Pirko35d48902011-03-22 02:38:12 +00001709 goto err_unset_master;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711
Jiri Pirko35d48902011-03-22 02:38:12 +00001712 new_slave->bond = bond;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 new_slave->dev = slave_dev;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001714 slave_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Holger Eitzenberger58402052008-12-09 23:07:13 -08001716 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 /* bond_alb_init_slave() must be called before all other stages since
1718 * it might fail and we do not want to have to undo everything
1719 */
1720 res = bond_alb_init_slave(bond, new_slave);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001721 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001722 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
1724
1725 /* If the mode USES_PRIMARY, then the new slave gets the
1726 * master's promisc (and mc) settings only if it becomes the
1727 * curr_active_slave, and that is taken care of later when calling
1728 * bond_change_active()
1729 */
1730 if (!USES_PRIMARY(bond->params.mode)) {
1731 /* set promiscuity level to new slave */
1732 if (bond_dev->flags & IFF_PROMISC) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001733 res = dev_set_promiscuity(slave_dev, 1);
1734 if (res)
1735 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
1737
1738 /* set allmulti level to new slave */
1739 if (bond_dev->flags & IFF_ALLMULTI) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001740 res = dev_set_allmulti(slave_dev, 1);
1741 if (res)
1742 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
1744
David S. Millerb9e40852008-07-15 00:15:08 -07001745 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 /* upload master's mc_list to new slave */
Jiri Pirko22bedad32010-04-01 21:22:57 +00001747 netdev_for_each_mc_addr(ha, bond_dev)
1748 dev_mc_add(slave_dev, ha->addr);
David S. Millerb9e40852008-07-15 00:15:08 -07001749 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
1751
1752 if (bond->params.mode == BOND_MODE_8023AD) {
1753 /* add lacpdu mc addr to mc list */
1754 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1755
Jiri Pirko22bedad32010-04-01 21:22:57 +00001756 dev_mc_add(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
1758
1759 bond_add_vlans_on_slave(bond, slave_dev);
1760
1761 write_lock_bh(&bond->lock);
1762
1763 bond_attach_slave(bond, new_slave);
1764
1765 new_slave->delay = 0;
1766 new_slave->link_failure_count = 0;
1767
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001768 write_unlock_bh(&bond->lock);
1769
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001770 bond_compute_features(bond);
1771
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001772 read_lock(&bond->lock);
1773
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001774 new_slave->last_arp_rx = jiffies;
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (bond->params.miimon && !bond->params.use_carrier) {
1777 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1778
1779 if ((link_reporting == -1) && !bond->params.arp_interval) {
1780 /*
1781 * miimon is set but a bonded network driver
1782 * does not support ETHTOOL/MII and
1783 * arp_interval is not set. Note: if
1784 * use_carrier is enabled, we will never go
1785 * here (because netif_carrier is always
1786 * supported); thus, we don't need to change
1787 * the messages for netif_carrier.
1788 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001789 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 -08001790 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 } else if (link_reporting == -1) {
1792 /* unable get link status using mii/ethtool */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001793 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",
1794 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
1796 }
1797
1798 /* check for initial state */
1799 if (!bond->params.miimon ||
1800 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1801 if (bond->params.updelay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001802 pr_debug("Initial state of slave_dev is BOND_LINK_BACK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 new_slave->link = BOND_LINK_BACK;
1804 new_slave->delay = bond->params.updelay;
1805 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001806 pr_debug("Initial state of slave_dev is BOND_LINK_UP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 new_slave->link = BOND_LINK_UP;
1808 }
1809 new_slave->jiffies = jiffies;
1810 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001811 pr_debug("Initial state of slave_dev is BOND_LINK_DOWN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 new_slave->link = BOND_LINK_DOWN;
1813 }
1814
1815 if (bond_update_speed_duplex(new_slave) &&
1816 (new_slave->link != BOND_LINK_DOWN)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001817 pr_warning("%s: Warning: failed to get speed and duplex from %s, assumed to be 100Mb/sec and Full.\n",
1818 bond_dev->name, new_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820 if (bond->params.mode == BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001821 pr_warning("%s: Warning: Operation of 802.3ad mode requires ETHTOOL support in base driver for proper aggregator selection.\n",
1822 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824 }
1825
1826 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1827 /* if there is a primary slave, remember it */
Jiri Pirkoa5499522009-09-25 03:28:09 +00001828 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 bond->primary_slave = new_slave;
Jiri Pirkoa5499522009-09-25 03:28:09 +00001830 bond->force_primary = true;
1831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
1833
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001834 write_lock_bh(&bond->curr_slave_lock);
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 switch (bond->params.mode) {
1837 case BOND_MODE_ACTIVEBACKUP:
Jay Vosburgh8a8e4472006-09-22 21:56:15 -07001838 bond_set_slave_inactive_flags(new_slave);
1839 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 break;
1841 case BOND_MODE_8023AD:
1842 /* in 802.3ad mode, the internal mechanism
1843 * will activate the slaves in the selected
1844 * aggregator
1845 */
1846 bond_set_slave_inactive_flags(new_slave);
1847 /* if this is the first slave */
1848 if (bond->slave_cnt == 1) {
1849 SLAVE_AD_INFO(new_slave).id = 1;
1850 /* Initialize AD with the number of times that the AD timer is called in 1 second
1851 * can be called only after the mac address of the bond is set
1852 */
1853 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1854 bond->params.lacp_fast);
1855 } else {
1856 SLAVE_AD_INFO(new_slave).id =
1857 SLAVE_AD_INFO(new_slave->prev).id + 1;
1858 }
1859
1860 bond_3ad_bind_slave(new_slave);
1861 break;
1862 case BOND_MODE_TLB:
1863 case BOND_MODE_ALB:
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001864 bond_set_active_slave(new_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001865 bond_set_slave_inactive_flags(new_slave);
Jiri Pirko5a29f782009-03-25 17:23:38 -07001866 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 break;
1868 default:
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001869 pr_debug("This slave is always active in trunk mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 /* always active in trunk mode */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001872 bond_set_active_slave(new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 /* In trunking mode there is little meaning to curr_active_slave
1875 * anyway (it holds no special properties of the bond device),
1876 * so we can change it without calling change_active_interface()
1877 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001878 if (!bond->curr_active_slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 bond->curr_active_slave = new_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 break;
1882 } /* switch(bond_mode) */
1883
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001884 write_unlock_bh(&bond->curr_slave_lock);
1885
Jay Vosburghff59c452006-03-27 13:27:43 -08001886 bond_set_carrier(bond);
1887
WANG Congf6dc31a2010-05-06 00:48:51 -07001888#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001889 slave_dev->npinfo = bond_netpoll_info(bond);
1890 if (slave_dev->npinfo) {
1891 if (slave_enable_netpoll(new_slave)) {
1892 read_unlock(&bond->lock);
1893 pr_info("Error, %s: master_dev is using netpoll, "
1894 "but new slave device does not support netpoll.\n",
1895 bond_dev->name);
1896 res = -EBUSY;
1897 goto err_close;
1898 }
WANG Congf6dc31a2010-05-06 00:48:51 -07001899 }
1900#endif
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001901
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001902 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001904 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1905 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001906 goto err_close;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001907
Jiri Pirko35d48902011-03-22 02:38:12 +00001908 res = netdev_rx_handler_register(slave_dev, bond_handle_frame,
1909 new_slave);
1910 if (res) {
1911 pr_debug("Error %d calling netdev_rx_handler_register\n", res);
1912 goto err_dest_symlinks;
1913 }
1914
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001915 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
1916 bond_dev->name, slave_dev->name,
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001917 bond_is_active_slave(new_slave) ? "n active" : " backup",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001918 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 /* enslave is successful */
1921 return 0;
1922
1923/* Undo stages on error */
Jiri Pirko35d48902011-03-22 02:38:12 +00001924err_dest_symlinks:
1925 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927err_close:
1928 dev_close(slave_dev);
1929
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001930err_unset_master:
Jiri Pirko1765a572011-02-12 06:48:36 +00001931 netdev_set_bond_master(slave_dev, NULL);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933err_restore_mac:
Jay Vosburghdd957c52007-10-09 19:57:24 -07001934 if (!bond->params.fail_over_mac) {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001935 /* XXX TODO - fom follow mode needs to change master's
1936 * MAC if this slave's MAC is in use by the bond, or at
1937 * least print a warning.
1938 */
Moni Shoua2ab82852007-10-09 19:43:39 -07001939 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1940 addr.sa_family = slave_dev->type;
1941 dev_set_mac_address(slave_dev, &addr);
1942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001944err_restore_mtu:
1945 dev_set_mtu(slave_dev, new_slave->original_mtu);
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947err_free:
1948 kfree(new_slave);
1949
1950err_undo_flags:
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001951 bond_compute_features(bond);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return res;
1954}
1955
1956/*
1957 * Try to release the slave device <slave> from the bond device <master>
1958 * It is legal to access curr_active_slave without a lock because all the function
1959 * is write-locked.
1960 *
1961 * The rules for slave state should be:
1962 * for Active/Backup:
1963 * Active stays on all backups go down
1964 * for Bonded connections:
1965 * The first up interface should be left on and all others downed.
1966 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001967int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
Wang Chen454d7c92008-11-12 23:37:49 -08001969 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 struct slave *slave, *oldcurrent;
1971 struct sockaddr addr;
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001972 u32 old_features = bond_dev->features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974 /* slave is not a slave or master is not master of this slave */
1975 if (!(slave_dev->flags & IFF_SLAVE) ||
1976 (slave_dev->master != bond_dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001977 pr_err("%s: Error: cannot release %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 bond_dev->name, slave_dev->name);
1979 return -EINVAL;
1980 }
1981
Neil Hormane843fa52010-10-13 16:01:50 +00001982 block_netpoll_tx();
Amerigo Wangdaf92092011-05-19 21:39:12 +00001983 netdev_bonding_change(bond_dev, NETDEV_RELEASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 write_lock_bh(&bond->lock);
1985
1986 slave = bond_get_slave_by_dev(bond, slave_dev);
1987 if (!slave) {
1988 /* not a slave of this bond */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001989 pr_info("%s: %s not enslaved\n",
1990 bond_dev->name, slave_dev->name);
Jay Vosburghf5e2a7b2006-02-07 21:17:22 -08001991 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001992 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return -EINVAL;
1994 }
1995
Jiri Pirko35d48902011-03-22 02:38:12 +00001996 /* unregister rx_handler early so bond_handle_frame wouldn't be called
1997 * for this slave anymore.
1998 */
1999 netdev_rx_handler_unregister(slave_dev);
2000 write_unlock_bh(&bond->lock);
2001 synchronize_net();
2002 write_lock_bh(&bond->lock);
2003
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07002004 if (!bond->params.fail_over_mac) {
Joe Perches8e95a202009-12-03 07:58:21 +00002005 if (!compare_ether_addr(bond_dev->dev_addr, slave->perm_hwaddr) &&
2006 bond->slave_cnt > 1)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002007 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",
2008 bond_dev->name, slave_dev->name,
2009 slave->perm_hwaddr,
2010 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
2012
2013 /* Inform AD package of unbinding of slave. */
2014 if (bond->params.mode == BOND_MODE_8023AD) {
2015 /* must be called before the slave is
2016 * detached from the list
2017 */
2018 bond_3ad_unbind_slave(slave);
2019 }
2020
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002021 pr_info("%s: releasing %s interface %s\n",
2022 bond_dev->name,
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002023 bond_is_active_slave(slave) ? "active" : "backup",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002024 slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 oldcurrent = bond->curr_active_slave;
2027
2028 bond->current_arp_slave = NULL;
2029
2030 /* release the slave from its bond */
2031 bond_detach_slave(bond, slave);
2032
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002033 if (bond->primary_slave == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 bond->primary_slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002036 if (oldcurrent == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 bond_change_active_slave(bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Holger Eitzenberger58402052008-12-09 23:07:13 -08002039 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 /* Must be called only after the slave has been
2041 * detached from the list and the curr_active_slave
2042 * has been cleared (if our_slave == old_current),
2043 * but before a new active slave is selected.
2044 */
Jay Vosburgh25433312008-01-17 16:24:59 -08002045 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 bond_alb_deinit_slave(bond, slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08002047 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 }
2049
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002050 if (oldcurrent == slave) {
2051 /*
2052 * Note that we hold RTNL over this sequence, so there
2053 * is no concern that another slave add/remove event
2054 * will interfere.
2055 */
2056 write_unlock_bh(&bond->lock);
2057 read_lock(&bond->lock);
2058 write_lock_bh(&bond->curr_slave_lock);
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 bond_select_active_slave(bond);
2061
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002062 write_unlock_bh(&bond->curr_slave_lock);
2063 read_unlock(&bond->lock);
2064 write_lock_bh(&bond->lock);
2065 }
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (bond->slave_cnt == 0) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002068 bond_set_carrier(bond);
2069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 /* if the last slave was removed, zero the mac address
2071 * of the master so it will be set by the application
2072 * to the mac address of the first slave
2073 */
2074 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2075
Michał Mirosławb2a103e2011-05-07 03:22:17 +00002076 if (bond->vlgrp) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002077 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2078 bond_dev->name, bond_dev->name);
2079 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2080 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
2083
2084 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002085 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Michał Mirosławb2a103e2011-05-07 03:22:17 +00002087 bond_compute_features(bond);
2088 if (!(bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2089 (old_features & NETIF_F_VLAN_CHALLENGED))
2090 pr_info("%s: last VLAN challenged slave %s left bond %s. VLAN blocking is removed\n",
2091 bond_dev->name, slave_dev->name, bond_dev->name);
2092
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002093 /* must do this from outside any spinlocks */
2094 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 bond_del_vlans_from_slave(bond, slave_dev);
2097
2098 /* If the mode USES_PRIMARY, then we should only remove its
2099 * promisc and mc settings if it was the curr_active_slave, but that was
2100 * already taken care of above when we detached the slave
2101 */
2102 if (!USES_PRIMARY(bond->params.mode)) {
2103 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002104 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002108 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
2111 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002112 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002114 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
2116
Jiri Pirko1765a572011-02-12 06:48:36 +00002117 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002119 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07002120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 /* close slave before restoring its mac address */
2122 dev_close(slave_dev);
2123
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07002124 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002125 /* restore original ("permanent") mac address */
2126 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2127 addr.sa_family = slave_dev->type;
2128 dev_set_mac_address(slave_dev, &addr);
2129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00002131 dev_set_mtu(slave_dev, slave->original_mtu);
2132
Jiri Pirko2d7011c2011-03-16 08:46:43 +00002133 slave_dev->priv_flags &= ~IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135 kfree(slave);
2136
2137 return 0; /* deletion OK */
2138}
2139
2140/*
Nicolas de Pesloüandadaa102011-03-19 13:36:18 -07002141* First release a slave and then destroy the bond if no more slaves are left.
Moni Shouad90a1622007-10-09 19:43:43 -07002142* Must be under rtnl_lock when this function is called.
2143*/
stephen hemminger26d8ee72010-10-15 05:09:34 +00002144static int bond_release_and_destroy(struct net_device *bond_dev,
2145 struct net_device *slave_dev)
Moni Shouad90a1622007-10-09 19:43:43 -07002146{
Wang Chen454d7c92008-11-12 23:37:49 -08002147 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002148 int ret;
2149
2150 ret = bond_release(bond_dev, slave_dev);
2151 if ((ret == 0) && (bond->slave_cnt == 0)) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002152 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002153 pr_info("%s: destroying bond %s.\n",
2154 bond_dev->name, bond_dev->name);
Stephen Hemminger9e716262009-06-12 19:02:47 +00002155 unregister_netdevice(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002156 }
2157 return ret;
2158}
2159
2160/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 * This function releases all slaves.
2162 */
2163static int bond_release_all(struct net_device *bond_dev)
2164{
Wang Chen454d7c92008-11-12 23:37:49 -08002165 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 struct slave *slave;
2167 struct net_device *slave_dev;
2168 struct sockaddr addr;
2169
2170 write_lock_bh(&bond->lock);
2171
Jay Vosburghff59c452006-03-27 13:27:43 -08002172 netif_carrier_off(bond_dev);
2173
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002174 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177 bond->current_arp_slave = NULL;
2178 bond->primary_slave = NULL;
2179 bond_change_active_slave(bond, NULL);
2180
2181 while ((slave = bond->first_slave) != NULL) {
2182 /* Inform AD package of unbinding of slave
2183 * before slave is detached from the list.
2184 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002185 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 bond_3ad_unbind_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 slave_dev = slave->dev;
2189 bond_detach_slave(bond, slave);
2190
Jay Vosburgh25433312008-01-17 16:24:59 -08002191 /* now that the slave is detached, unlock and perform
2192 * all the undo steps that should not be called from
2193 * within a lock.
2194 */
2195 write_unlock_bh(&bond->lock);
2196
Jiri Pirko35d48902011-03-22 02:38:12 +00002197 /* unregister rx_handler early so bond_handle_frame wouldn't
2198 * be called for this slave anymore.
2199 */
2200 netdev_rx_handler_unregister(slave_dev);
2201 synchronize_net();
2202
Holger Eitzenberger58402052008-12-09 23:07:13 -08002203 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 /* must be called only after the slave
2205 * has been detached from the list
2206 */
2207 bond_alb_deinit_slave(bond, slave);
2208 }
2209
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002210 bond_destroy_slave_symlinks(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 bond_del_vlans_from_slave(bond, slave_dev);
2212
2213 /* If the mode USES_PRIMARY, then we should only remove its
2214 * promisc and mc settings if it was the curr_active_slave, but that was
2215 * already taken care of above when we detached the slave
2216 */
2217 if (!USES_PRIMARY(bond->params.mode)) {
2218 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002219 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002223 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002227 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002229 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
2231
Jiri Pirko1765a572011-02-12 06:48:36 +00002232 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002234 slave_disable_netpoll(slave);
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 /* close slave before restoring its mac address */
2237 dev_close(slave_dev);
2238
Jay Vosburghdd957c52007-10-09 19:57:24 -07002239 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002240 /* restore original ("permanent") mac address*/
2241 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2242 addr.sa_family = slave_dev->type;
2243 dev_set_mac_address(slave_dev, &addr);
2244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 kfree(slave);
2247
2248 /* re-acquire the lock before getting the next slave */
2249 write_lock_bh(&bond->lock);
2250 }
2251
2252 /* zero the mac address of the master so it will be
2253 * set by the application to the mac address of the
2254 * first slave
2255 */
2256 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2257
Michał Mirosławb2a103e2011-05-07 03:22:17 +00002258 if (bond->vlgrp) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002259 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2260 bond_dev->name, bond_dev->name);
2261 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2262 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 }
2264
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002265 pr_info("%s: released all slaves\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
2267out:
2268 write_unlock_bh(&bond->lock);
Michał Mirosławb2a103e2011-05-07 03:22:17 +00002269
2270 bond_compute_features(bond);
2271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 return 0;
2273}
2274
2275/*
2276 * This function changes the active slave to slave <slave_dev>.
2277 * It returns -EINVAL in the following cases.
2278 * - <slave_dev> is not found in the list.
2279 * - There is not active slave now.
2280 * - <slave_dev> is already active.
2281 * - The link state of <slave_dev> is not BOND_LINK_UP.
2282 * - <slave_dev> is not running.
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002283 * In these cases, this function does nothing.
2284 * In the other cases, current_slave pointer is changed and 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 */
2286static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2287{
Wang Chen454d7c92008-11-12 23:37:49 -08002288 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 struct slave *old_active = NULL;
2290 struct slave *new_active = NULL;
2291 int res = 0;
2292
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002293 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296 /* Verify that master_dev is indeed the master of slave_dev */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002297 if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002300 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002302 read_lock(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 old_active = bond->curr_active_slave;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002304 read_unlock(&bond->curr_slave_lock);
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 new_active = bond_get_slave_by_dev(bond, slave_dev);
2307
2308 /*
2309 * Changing to the current active: do nothing; return success.
2310 */
2311 if (new_active && (new_active == old_active)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002312 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return 0;
2314 }
2315
2316 if ((new_active) &&
2317 (old_active) &&
2318 (new_active->link == BOND_LINK_UP) &&
2319 IS_UP(new_active->dev)) {
Neil Hormane843fa52010-10-13 16:01:50 +00002320 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002321 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 bond_change_active_slave(bond, new_active);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002323 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002324 unblock_netpoll_tx();
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002325 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002328 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 return res;
2331}
2332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2334{
Wang Chen454d7c92008-11-12 23:37:49 -08002335 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 info->bond_mode = bond->params.mode;
2338 info->miimon = bond->params.miimon;
2339
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002340 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 info->num_slaves = bond->slave_cnt;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002342 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344 return 0;
2345}
2346
2347static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2348{
Wang Chen454d7c92008-11-12 23:37:49 -08002349 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 struct slave *slave;
Eric Dumazet689c96c2009-04-23 03:39:04 +00002351 int i, res = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002353 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
2355 bond_for_each_slave(bond, slave, i) {
2356 if (i == (int)info->slave_id) {
Eric Dumazet689c96c2009-04-23 03:39:04 +00002357 res = 0;
2358 strcpy(info->slave_name, slave->dev->name);
2359 info->link = slave->link;
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002360 info->state = bond_slave_state(slave);
Eric Dumazet689c96c2009-04-23 03:39:04 +00002361 info->link_failure_count = slave->link_failure_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 break;
2363 }
2364 }
2365
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002366 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Eric Dumazet689c96c2009-04-23 03:39:04 +00002368 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
2370
2371/*-------------------------------- Monitoring -------------------------------*/
2372
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002373
2374static int bond_miimon_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002376 struct slave *slave;
2377 int i, link_state, commit = 0;
Jiri Pirko41f89102009-04-24 03:57:29 +00002378 bool ignore_updelay;
2379
2380 ignore_updelay = !bond->curr_active_slave ? true : false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 bond_for_each_slave(bond, slave, i) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002383 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002385 link_state = bond_check_dev_link(bond, slave->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 switch (slave->link) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002388 case BOND_LINK_UP:
2389 if (link_state)
2390 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002392 slave->link = BOND_LINK_FAIL;
2393 slave->delay = bond->params.downdelay;
2394 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002395 pr_info("%s: link status down for %sinterface %s, disabling it in %d ms.\n",
2396 bond->dev->name,
2397 (bond->params.mode ==
2398 BOND_MODE_ACTIVEBACKUP) ?
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002399 (bond_is_active_slave(slave) ?
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002400 "active " : "backup ") : "",
2401 slave->dev->name,
2402 bond->params.downdelay * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002404 /*FALLTHRU*/
2405 case BOND_LINK_FAIL:
2406 if (link_state) {
2407 /*
2408 * recovered before downdelay expired
2409 */
2410 slave->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 slave->jiffies = jiffies;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002412 pr_info("%s: link status up again after %d ms for interface %s.\n",
2413 bond->dev->name,
2414 (bond->params.downdelay - slave->delay) *
2415 bond->params.miimon,
2416 slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002417 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002419
2420 if (slave->delay <= 0) {
2421 slave->new_link = BOND_LINK_DOWN;
2422 commit++;
2423 continue;
2424 }
2425
2426 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002429 case BOND_LINK_DOWN:
2430 if (!link_state)
2431 continue;
2432
2433 slave->link = BOND_LINK_BACK;
2434 slave->delay = bond->params.updelay;
2435
2436 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002437 pr_info("%s: link status up for interface %s, enabling it in %d ms.\n",
2438 bond->dev->name, slave->dev->name,
2439 ignore_updelay ? 0 :
2440 bond->params.updelay *
2441 bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002443 /*FALLTHRU*/
2444 case BOND_LINK_BACK:
2445 if (!link_state) {
2446 slave->link = BOND_LINK_DOWN;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002447 pr_info("%s: link status down again after %d ms for interface %s.\n",
2448 bond->dev->name,
2449 (bond->params.updelay - slave->delay) *
2450 bond->params.miimon,
2451 slave->dev->name);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002452
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002453 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002455
Jiri Pirko41f89102009-04-24 03:57:29 +00002456 if (ignore_updelay)
2457 slave->delay = 0;
2458
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002459 if (slave->delay <= 0) {
2460 slave->new_link = BOND_LINK_UP;
2461 commit++;
Jiri Pirko41f89102009-04-24 03:57:29 +00002462 ignore_updelay = false;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002463 continue;
2464 }
2465
2466 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 break;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002468 }
2469 }
2470
2471 return commit;
2472}
2473
2474static void bond_miimon_commit(struct bonding *bond)
2475{
2476 struct slave *slave;
2477 int i;
2478
2479 bond_for_each_slave(bond, slave, i) {
2480 switch (slave->new_link) {
2481 case BOND_LINK_NOCHANGE:
2482 continue;
2483
2484 case BOND_LINK_UP:
2485 slave->link = BOND_LINK_UP;
2486 slave->jiffies = jiffies;
2487
2488 if (bond->params.mode == BOND_MODE_8023AD) {
2489 /* prevent it from being the active one */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002490 bond_set_backup_slave(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002491 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2492 /* make it immediately active */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002493 bond_set_active_slave(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002494 } else if (slave != bond->primary_slave) {
2495 /* prevent it from being the active one */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002496 bond_set_backup_slave(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002497 }
2498
Krzysztof Piotr Oledzki546add72010-10-06 14:28:22 -07002499 bond_update_speed_duplex(slave);
2500
David Decotigny5d305302011-04-13 15:22:31 +00002501 pr_info("%s: link status definitely up for interface %s, %u Mbps %s duplex.\n",
Krzysztof Piotr Oledzki700c2a72010-10-06 14:25:06 -07002502 bond->dev->name, slave->dev->name,
2503 slave->speed, slave->duplex ? "full" : "half");
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002504
2505 /* notify ad that the link status has changed */
2506 if (bond->params.mode == BOND_MODE_8023AD)
2507 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2508
Holger Eitzenberger58402052008-12-09 23:07:13 -08002509 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002510 bond_alb_handle_link_change(bond, slave,
2511 BOND_LINK_UP);
2512
2513 if (!bond->curr_active_slave ||
2514 (slave == bond->primary_slave))
2515 goto do_failover;
2516
2517 continue;
2518
2519 case BOND_LINK_DOWN:
Jay Vosburghfba4acd2008-10-30 17:41:14 -07002520 if (slave->link_failure_count < UINT_MAX)
2521 slave->link_failure_count++;
2522
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002523 slave->link = BOND_LINK_DOWN;
2524
2525 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2526 bond->params.mode == BOND_MODE_8023AD)
2527 bond_set_slave_inactive_flags(slave);
2528
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002529 pr_info("%s: link status definitely down for interface %s, disabling it\n",
2530 bond->dev->name, slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002531
2532 if (bond->params.mode == BOND_MODE_8023AD)
2533 bond_3ad_handle_link_change(slave,
2534 BOND_LINK_DOWN);
2535
Jiri Pirkoae63e802009-05-27 05:42:36 +00002536 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002537 bond_alb_handle_link_change(bond, slave,
2538 BOND_LINK_DOWN);
2539
2540 if (slave == bond->curr_active_slave)
2541 goto do_failover;
2542
2543 continue;
2544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002546 pr_err("%s: invalid new link %d on slave %s\n",
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002547 bond->dev->name, slave->new_link,
2548 slave->dev->name);
2549 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002551 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 }
2553
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002554do_failover:
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002555 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00002556 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002557 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 bond_select_active_slave(bond);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002559 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002560 unblock_netpoll_tx();
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002561 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002562
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002563 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564}
2565
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002566/*
2567 * bond_mii_monitor
2568 *
2569 * Really a wrapper that splits the mii monitor into two phases: an
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002570 * inspection, then (if inspection indicates something needs to be done)
2571 * an acquisition of appropriate locks followed by a commit phase to
2572 * implement whatever link state changes are indicated.
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002573 */
2574void bond_mii_monitor(struct work_struct *work)
2575{
2576 struct bonding *bond = container_of(work, struct bonding,
2577 mii_work.work);
Ben Hutchingsad246c92011-04-26 15:25:52 +00002578 bool should_notify_peers = false;
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002579
2580 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002581 if (bond->kill_timers)
2582 goto out;
2583
2584 if (bond->slave_cnt == 0)
2585 goto re_arm;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002586
Ben Hutchingsad246c92011-04-26 15:25:52 +00002587 should_notify_peers = bond_should_notify_peers(bond);
2588
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002589 if (bond_miimon_inspect(bond)) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002590 read_unlock(&bond->lock);
2591 rtnl_lock();
2592 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002593
2594 bond_miimon_commit(bond);
2595
Jay Vosburgh56556622008-01-17 16:25:03 -08002596 read_unlock(&bond->lock);
2597 rtnl_unlock(); /* might sleep, hold no other locks */
2598 read_lock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002599 }
2600
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002601re_arm:
2602 if (bond->params.miimon)
2603 queue_delayed_work(bond->wq, &bond->mii_work,
2604 msecs_to_jiffies(bond->params.miimon));
2605out:
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002606 read_unlock(&bond->lock);
Ben Hutchingsad246c92011-04-26 15:25:52 +00002607
2608 if (should_notify_peers) {
2609 rtnl_lock();
2610 netdev_bonding_change(bond->dev, NETDEV_NOTIFY_PEERS);
2611 rtnl_unlock();
2612 }
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002613}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002614
Al Virod3bb52b2007-08-22 20:06:58 -04002615static __be32 bond_glean_dev_ip(struct net_device *dev)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002616{
2617 struct in_device *idev;
2618 struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07002619 __be32 addr = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002620
2621 if (!dev)
2622 return 0;
2623
2624 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002625 idev = __in_dev_get_rcu(dev);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002626 if (!idev)
2627 goto out;
2628
2629 ifa = idev->ifa_list;
2630 if (!ifa)
2631 goto out;
2632
2633 addr = ifa->ifa_local;
2634out:
2635 rcu_read_unlock();
2636 return addr;
2637}
2638
Al Virod3bb52b2007-08-22 20:06:58 -04002639static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002640{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002641 struct vlan_entry *vlan;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002642
2643 if (ip == bond->master_ip)
2644 return 1;
2645
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002646 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002647 if (ip == vlan->vlan_ip)
2648 return 1;
2649 }
2650
2651 return 0;
2652}
2653
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002654/*
2655 * We go to the (large) trouble of VLAN tagging ARP frames because
2656 * switches in VLAN mode (especially if ports are configured as
2657 * "native" to a VLAN) might not pass non-tagged frames.
2658 */
Al Virod3bb52b2007-08-22 20:06:58 -04002659static 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 -04002660{
2661 struct sk_buff *skb;
2662
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002663 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002664 slave_dev->name, dest_ip, src_ip, vlan_id);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002665
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002666 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2667 NULL, slave_dev->dev_addr, NULL);
2668
2669 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002670 pr_err("ARP packet allocation failed\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002671 return;
2672 }
2673 if (vlan_id) {
2674 skb = vlan_put_tag(skb, vlan_id);
2675 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002676 pr_err("failed to insert VLAN tag\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002677 return;
2678 }
2679 }
2680 arp_xmit(skb);
2681}
2682
2683
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2685{
David S. Millerb23dd4f2011-03-02 14:31:35 -08002686 int i, vlan_id;
Al Virod3bb52b2007-08-22 20:06:58 -04002687 __be32 *targets = bond->params.arp_targets;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002688 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002689 struct net_device *vlan_dev;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002690 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Mitch Williams6b780562005-11-09 10:36:19 -08002692 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2693 if (!targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07002694 break;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002695 pr_debug("basa: target %x\n", targets[i]);
Jay Vosburghf35188f2010-07-21 12:14:47 +00002696 if (!bond->vlgrp) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002697 pr_debug("basa: empty vlan: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002698 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2699 bond->master_ip, 0);
2700 continue;
2701 }
2702
2703 /*
2704 * If VLANs are configured, we do a route lookup to
2705 * determine which VLAN interface would be used, so we
2706 * can tag the ARP with the proper VLAN tag.
2707 */
David S. Miller78fbfd82011-03-12 00:00:52 -05002708 rt = ip_route_output(dev_net(bond->dev), targets[i], 0,
2709 RTO_ONLINK, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08002710 if (IS_ERR(rt)) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002711 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002712 pr_warning("%s: no route to arp_ip_target %pI4\n",
David S. Miller78fbfd82011-03-12 00:00:52 -05002713 bond->dev->name, &targets[i]);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002714 }
2715 continue;
2716 }
2717
2718 /*
2719 * This target is not on a VLAN
2720 */
Changli Gaod8d1f302010-06-10 23:31:35 -07002721 if (rt->dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002722 ip_rt_put(rt);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002723 pr_debug("basa: rtdev == bond->dev: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002724 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2725 bond->master_ip, 0);
2726 continue;
2727 }
2728
2729 vlan_id = 0;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002730 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002731 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Changli Gaod8d1f302010-06-10 23:31:35 -07002732 if (vlan_dev == rt->dst.dev) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002733 vlan_id = vlan->vlan_id;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002734 pr_debug("basa: vlan match on %s %d\n",
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002735 vlan_dev->name, vlan_id);
2736 break;
2737 }
2738 }
2739
2740 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002741 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002742 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2743 vlan->vlan_ip, vlan_id);
2744 continue;
2745 }
2746
2747 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002748 pr_warning("%s: no path to arp_ip_target %pI4 via rt.dev %s\n",
David S. Miller78fbfd82011-03-12 00:00:52 -05002749 bond->dev->name, &targets[i],
Changli Gaod8d1f302010-06-10 23:31:35 -07002750 rt->dst.dev ? rt->dst.dev->name : "NULL");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002751 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002752 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002753 }
2754}
2755
Al Virod3bb52b2007-08-22 20:06:58 -04002756static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002757{
2758 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002759 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002760
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002761 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002762 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002763 &sip, &tip, i, &targets[i],
2764 bond_has_this_ip(bond, tip));
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002765 if (sip == targets[i]) {
2766 if (bond_has_this_ip(bond, tip))
2767 slave->last_arp_rx = jiffies;
2768 return;
2769 }
2770 }
2771}
2772
Jiri Pirko3aba8912011-04-19 03:48:16 +00002773static void bond_arp_rcv(struct sk_buff *skb, struct bonding *bond,
2774 struct slave *slave)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002775{
2776 struct arphdr *arp;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002777 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002778 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002779
Jiri Pirko3aba8912011-04-19 03:48:16 +00002780 if (skb->protocol != __cpu_to_be16(ETH_P_ARP))
2781 return;
Andy Gospodarek1f3c8802009-12-14 10:48:58 +00002782
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002783 read_lock(&bond->lock);
2784
Jiri Pirko3aba8912011-04-19 03:48:16 +00002785 pr_debug("bond_arp_rcv: bond %s skb->dev %s\n",
2786 bond->dev->name, skb->dev->name);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002787
Jiri Pirko3aba8912011-04-19 03:48:16 +00002788 if (!pskb_may_pull(skb, arp_hdr_len(bond->dev)))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002789 goto out_unlock;
2790
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002791 arp = arp_hdr(skb);
Jiri Pirko3aba8912011-04-19 03:48:16 +00002792 if (arp->ar_hln != bond->dev->addr_len ||
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002793 skb->pkt_type == PACKET_OTHERHOST ||
2794 skb->pkt_type == PACKET_LOOPBACK ||
2795 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2796 arp->ar_pro != htons(ETH_P_IP) ||
2797 arp->ar_pln != 4)
2798 goto out_unlock;
2799
2800 arp_ptr = (unsigned char *)(arp + 1);
Jiri Pirko3aba8912011-04-19 03:48:16 +00002801 arp_ptr += bond->dev->addr_len;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002802 memcpy(&sip, arp_ptr, 4);
Jiri Pirko3aba8912011-04-19 03:48:16 +00002803 arp_ptr += 4 + bond->dev->addr_len;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002804 memcpy(&tip, arp_ptr, 4);
2805
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002806 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002807 bond->dev->name, slave->dev->name, bond_slave_state(slave),
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002808 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2809 &sip, &tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002810
2811 /*
2812 * Backup slaves won't see the ARP reply, but do come through
2813 * here for each ARP probe (so we swap the sip/tip to validate
2814 * the probe). In a "redundant switch, common router" type of
2815 * configuration, the ARP probe will (hopefully) travel from
2816 * the active, through one switch, the router, then the other
2817 * switch before reaching the backup.
2818 */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002819 if (bond_is_active_slave(slave))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002820 bond_validate_arp(bond, slave, sip, tip);
2821 else
2822 bond_validate_arp(bond, slave, tip, sip);
2823
2824out_unlock:
2825 read_unlock(&bond->lock);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002826}
2827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828/*
2829 * this function is called regularly to monitor each slave's link
2830 * ensuring that traffic is being sent and received when arp monitoring
2831 * is used in load-balancing mode. if the adapter has been dormant, then an
2832 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2833 * arp monitoring in active backup mode.
2834 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002835void bond_loadbalance_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002837 struct bonding *bond = container_of(work, struct bonding,
2838 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 struct slave *slave, *oldcurrent;
2840 int do_failover = 0;
2841 int delta_in_ticks;
2842 int i;
2843
2844 read_lock(&bond->lock);
2845
Jay Vosburgh5ce0da82008-05-17 21:10:07 -07002846 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002848 if (bond->kill_timers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002851 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
2854 read_lock(&bond->curr_slave_lock);
2855 oldcurrent = bond->curr_active_slave;
2856 read_unlock(&bond->curr_slave_lock);
2857
2858 /* see if any of the previous devices are up now (i.e. they have
2859 * xmt and rcv traffic). the curr_active_slave does not come into
2860 * the picture unless it is null. also, slave->jiffies is not needed
2861 * here because we send an arp on each slave and give a slave as
2862 * long as it needs to get the tx/rx within the delta.
2863 * TODO: what about up/down delay in arp mode? it wasn't here before
2864 * so it can wait
2865 */
2866 bond_for_each_slave(bond, slave, i) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002867 unsigned long trans_start = dev_trans_start(slave->dev);
2868
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002870 if (time_in_range(jiffies,
2871 trans_start - delta_in_ticks,
2872 trans_start + delta_in_ticks) &&
2873 time_in_range(jiffies,
2874 slave->dev->last_rx - delta_in_ticks,
2875 slave->dev->last_rx + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
2877 slave->link = BOND_LINK_UP;
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002878 bond_set_active_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880 /* primary_slave has no meaning in round-robin
2881 * mode. the window of a slave being up and
2882 * curr_active_slave being null after enslaving
2883 * is closed.
2884 */
2885 if (!oldcurrent) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002886 pr_info("%s: link status definitely up for interface %s, ",
2887 bond->dev->name,
2888 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 do_failover = 1;
2890 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002891 pr_info("%s: interface %s is now up\n",
2892 bond->dev->name,
2893 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
2895 }
2896 } else {
2897 /* slave->link == BOND_LINK_UP */
2898
2899 /* not all switches will respond to an arp request
2900 * when the source ip is 0, so don't take the link down
2901 * if we don't know our ip yet
2902 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002903 if (!time_in_range(jiffies,
2904 trans_start - delta_in_ticks,
2905 trans_start + 2 * delta_in_ticks) ||
2906 !time_in_range(jiffies,
2907 slave->dev->last_rx - delta_in_ticks,
2908 slave->dev->last_rx + 2 * delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
2910 slave->link = BOND_LINK_DOWN;
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002911 bond_set_backup_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002913 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002916 pr_info("%s: interface %s is now down.\n",
2917 bond->dev->name,
2918 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002920 if (slave == oldcurrent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 do_failover = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 }
2923 }
2924
2925 /* note: if switch is in round-robin mode, all links
2926 * must tx arp to ensure all links rx an arp - otherwise
2927 * links may oscillate or not come up at all; if switch is
2928 * in something like xor mode, there is nothing we can
2929 * do - all replies will be rx'ed on same link causing slaves
2930 * to be unstable during low/no traffic periods
2931 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002932 if (IS_UP(slave->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 bond_arp_send_all(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 }
2935
2936 if (do_failover) {
Neil Hormane843fa52010-10-13 16:01:50 +00002937 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002938 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
2940 bond_select_active_slave(bond);
2941
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002942 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002943 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 }
2945
2946re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002947 if (bond->params.arp_interval)
2948 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949out:
2950 read_unlock(&bond->lock);
2951}
2952
2953/*
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002954 * Called to inspect slaves for active-backup mode ARP monitor link state
2955 * changes. Sets new_link in slaves to specify what action should take
2956 * place for the slave. Returns 0 if no changes are found, >0 if changes
2957 * to link states must be committed.
2958 *
2959 * Called with bond->lock held for read.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002961static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 struct slave *slave;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002964 int i, commit = 0;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002965 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 bond_for_each_slave(bond, slave, i) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002968 slave->new_link = BOND_LINK_NOCHANGE;
2969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002971 if (time_in_range(jiffies,
2972 slave_last_rx(bond, slave) - delta_in_ticks,
2973 slave_last_rx(bond, slave) + delta_in_ticks)) {
2974
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002975 slave->new_link = BOND_LINK_UP;
2976 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002979 continue;
2980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002982 /*
2983 * Give slaves 2*delta after being enslaved or made
2984 * active. This avoids bouncing, as the last receive
2985 * times need a full ARP monitor cycle to be updated.
2986 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002987 if (time_in_range(jiffies,
2988 slave->jiffies - delta_in_ticks,
2989 slave->jiffies + 2 * delta_in_ticks))
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002990 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002992 /*
2993 * Backup slave is down if:
2994 * - No current_arp_slave AND
2995 * - more than 3*delta since last receive AND
2996 * - the bond has an IP address
2997 *
2998 * Note: a non-null current_arp_slave indicates
2999 * the curr_active_slave went down and we are
3000 * searching for a new one; under this condition
3001 * we only take the curr_active_slave down - this
3002 * gives each slave a chance to tx/rx traffic
3003 * before being taken out
3004 */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00003005 if (!bond_is_active_slave(slave) &&
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003006 !bond->current_arp_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003007 !time_in_range(jiffies,
3008 slave_last_rx(bond, slave) - delta_in_ticks,
3009 slave_last_rx(bond, slave) + 3 * delta_in_ticks)) {
3010
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003011 slave->new_link = BOND_LINK_DOWN;
3012 commit++;
3013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003015 /*
3016 * Active slave is down if:
3017 * - more than 2*delta since transmitting OR
3018 * - (more than 2*delta since receive AND
3019 * the bond has an IP address)
3020 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003021 trans_start = dev_trans_start(slave->dev);
Jiri Pirkoe30bc062011-03-12 03:14:37 +00003022 if (bond_is_active_slave(slave) &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003023 (!time_in_range(jiffies,
3024 trans_start - delta_in_ticks,
3025 trans_start + 2 * delta_in_ticks) ||
3026 !time_in_range(jiffies,
3027 slave_last_rx(bond, slave) - delta_in_ticks,
3028 slave_last_rx(bond, slave) + 2 * delta_in_ticks))) {
3029
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003030 slave->new_link = BOND_LINK_DOWN;
3031 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 }
3033 }
3034
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003035 return commit;
3036}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003038/*
3039 * Called to commit link state changes noted by inspection step of
3040 * active-backup mode ARP monitor.
3041 *
3042 * Called with RTNL and bond->lock for read.
3043 */
3044static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
3045{
3046 struct slave *slave;
3047 int i;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003048 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003050 bond_for_each_slave(bond, slave, i) {
3051 switch (slave->new_link) {
3052 case BOND_LINK_NOCHANGE:
3053 continue;
3054
3055 case BOND_LINK_UP:
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003056 trans_start = dev_trans_start(slave->dev);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003057 if ((!bond->curr_active_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003058 time_in_range(jiffies,
3059 trans_start - delta_in_ticks,
3060 trans_start + delta_in_ticks)) ||
Jiri Pirkob9f60252009-08-31 11:09:38 +00003061 bond->curr_active_slave != slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003062 slave->link = BOND_LINK_UP;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003063 bond->current_arp_slave = NULL;
3064
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003065 pr_info("%s: link status definitely up for interface %s.\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003066 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003067
Jiri Pirkob9f60252009-08-31 11:09:38 +00003068 if (!bond->curr_active_slave ||
3069 (slave == bond->primary_slave))
3070 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003071
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003072 }
3073
Jiri Pirkob9f60252009-08-31 11:09:38 +00003074 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003075
3076 case BOND_LINK_DOWN:
3077 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003080 slave->link = BOND_LINK_DOWN;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003081 bond_set_slave_inactive_flags(slave);
3082
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003083 pr_info("%s: link status definitely down for interface %s, disabling it\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003084 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003085
3086 if (slave == bond->curr_active_slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003087 bond->current_arp_slave = NULL;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003088 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003089 }
Jiri Pirkob9f60252009-08-31 11:09:38 +00003090
3091 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003092
3093 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003094 pr_err("%s: impossible: new_link %d on slave %s\n",
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003095 bond->dev->name, slave->new_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 slave->dev->name);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003097 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Jiri Pirkob9f60252009-08-31 11:09:38 +00003100do_failover:
3101 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00003102 block_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003103 write_lock_bh(&bond->curr_slave_lock);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003104 bond_select_active_slave(bond);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003105 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00003106 unblock_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003107 }
3108
3109 bond_set_carrier(bond);
3110}
3111
3112/*
3113 * Send ARP probes for active-backup mode ARP monitor.
3114 *
3115 * Called with bond->lock held for read.
3116 */
3117static void bond_ab_arp_probe(struct bonding *bond)
3118{
3119 struct slave *slave;
3120 int i;
3121
3122 read_lock(&bond->curr_slave_lock);
3123
3124 if (bond->current_arp_slave && bond->curr_active_slave)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003125 pr_info("PROBE: c_arp %s && cas %s BAD\n",
3126 bond->current_arp_slave->dev->name,
3127 bond->curr_active_slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003128
3129 if (bond->curr_active_slave) {
3130 bond_arp_send_all(bond, bond->curr_active_slave);
3131 read_unlock(&bond->curr_slave_lock);
3132 return;
3133 }
3134
3135 read_unlock(&bond->curr_slave_lock);
3136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 /* if we don't have a curr_active_slave, search for the next available
3138 * backup slave from the current_arp_slave and make it the candidate
3139 * for becoming the curr_active_slave
3140 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003141
3142 if (!bond->current_arp_slave) {
3143 bond->current_arp_slave = bond->first_slave;
3144 if (!bond->current_arp_slave)
3145 return;
3146 }
3147
3148 bond_set_slave_inactive_flags(bond->current_arp_slave);
3149
3150 /* search for next candidate */
3151 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3152 if (IS_UP(slave->dev)) {
3153 slave->link = BOND_LINK_BACK;
3154 bond_set_slave_active_flags(slave);
3155 bond_arp_send_all(bond, slave);
3156 slave->jiffies = jiffies;
3157 bond->current_arp_slave = slave;
3158 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 }
3160
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003161 /* if the link state is up at this point, we
3162 * mark it down - this can happen if we have
3163 * simultaneous link failures and
3164 * reselect_active_interface doesn't make this
3165 * one the current slave so it is still marked
3166 * up when it is actually down
3167 */
3168 if (slave->link == BOND_LINK_UP) {
3169 slave->link = BOND_LINK_DOWN;
3170 if (slave->link_failure_count < UINT_MAX)
3171 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003173 bond_set_slave_inactive_flags(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003175 pr_info("%s: backup interface %s is now down.\n",
3176 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 }
3178 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003179}
3180
3181void bond_activebackup_arp_mon(struct work_struct *work)
3182{
3183 struct bonding *bond = container_of(work, struct bonding,
3184 arp_work.work);
Ben Hutchingsad246c92011-04-26 15:25:52 +00003185 bool should_notify_peers = false;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003186 int delta_in_ticks;
3187
3188 read_lock(&bond->lock);
3189
3190 if (bond->kill_timers)
3191 goto out;
3192
3193 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3194
3195 if (bond->slave_cnt == 0)
3196 goto re_arm;
3197
Ben Hutchingsad246c92011-04-26 15:25:52 +00003198 should_notify_peers = bond_should_notify_peers(bond);
3199
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003200 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3201 read_unlock(&bond->lock);
3202 rtnl_lock();
3203 read_lock(&bond->lock);
3204
3205 bond_ab_arp_commit(bond, delta_in_ticks);
3206
3207 read_unlock(&bond->lock);
3208 rtnl_unlock();
3209 read_lock(&bond->lock);
3210 }
3211
3212 bond_ab_arp_probe(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
3214re_arm:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003215 if (bond->params.arp_interval)
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003216 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217out:
3218 read_unlock(&bond->lock);
Ben Hutchingsad246c92011-04-26 15:25:52 +00003219
3220 if (should_notify_peers) {
3221 rtnl_lock();
3222 netdev_bonding_change(bond->dev, NETDEV_NOTIFY_PEERS);
3223 rtnl_unlock();
3224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225}
3226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227/*-------------------------- netdev event handling --------------------------*/
3228
3229/*
3230 * Change device name
3231 */
3232static int bond_event_changename(struct bonding *bond)
3233{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 bond_remove_proc_entry(bond);
3235 bond_create_proc_entry(bond);
Stephen Hemminger7e083842009-06-12 19:02:46 +00003236
Taku Izumif073c7c2010-12-09 15:17:13 +00003237 bond_debug_reregister(bond);
3238
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 return NOTIFY_DONE;
3240}
3241
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003242static int bond_master_netdev_event(unsigned long event,
3243 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244{
Wang Chen454d7c92008-11-12 23:37:49 -08003245 struct bonding *event_bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
3247 switch (event) {
3248 case NETDEV_CHANGENAME:
3249 return bond_event_changename(event_bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 default:
3251 break;
3252 }
3253
3254 return NOTIFY_DONE;
3255}
3256
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003257static int bond_slave_netdev_event(unsigned long event,
3258 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259{
3260 struct net_device *bond_dev = slave_dev->master;
Wang Chen454d7c92008-11-12 23:37:49 -08003261 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
3263 switch (event) {
3264 case NETDEV_UNREGISTER:
3265 if (bond_dev) {
Jay Vosburgh1284cd32007-10-15 16:44:27 -07003266 if (bond->setup_by_slave)
3267 bond_release_and_destroy(bond_dev, slave_dev);
3268 else
3269 bond_release(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 }
3271 break;
3272 case NETDEV_CHANGE:
Jay Vosburgh17d04502009-03-18 18:38:25 -07003273 if (bond->params.mode == BOND_MODE_8023AD || bond_is_lb(bond)) {
3274 struct slave *slave;
3275
3276 slave = bond_get_slave_by_dev(bond, slave_dev);
3277 if (slave) {
David Decotigny5d305302011-04-13 15:22:31 +00003278 u32 old_speed = slave->speed;
David Decotigny65cce192011-04-13 15:22:30 +00003279 u8 old_duplex = slave->duplex;
Jay Vosburgh17d04502009-03-18 18:38:25 -07003280
3281 bond_update_speed_duplex(slave);
3282
3283 if (bond_is_lb(bond))
3284 break;
3285
3286 if (old_speed != slave->speed)
3287 bond_3ad_adapter_speed_changed(slave);
3288 if (old_duplex != slave->duplex)
3289 bond_3ad_adapter_duplex_changed(slave);
3290 }
3291 }
3292
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 break;
3294 case NETDEV_DOWN:
3295 /*
3296 * ... Or is it this?
3297 */
3298 break;
3299 case NETDEV_CHANGEMTU:
3300 /*
3301 * TODO: Should slaves be allowed to
3302 * independently alter their MTU? For
3303 * an active-backup bond, slaves need
3304 * not be the same type of device, so
3305 * MTUs may vary. For other modes,
3306 * slaves arguably should have the
3307 * same MTUs. To do this, we'd need to
3308 * take over the slave's change_mtu
3309 * function for the duration of their
3310 * servitude.
3311 */
3312 break;
3313 case NETDEV_CHANGENAME:
3314 /*
3315 * TODO: handle changing the primary's name
3316 */
3317 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003318 case NETDEV_FEAT_CHANGE:
3319 bond_compute_features(bond);
3320 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 default:
3322 break;
3323 }
3324
3325 return NOTIFY_DONE;
3326}
3327
3328/*
3329 * bond_netdev_event: handle netdev notifier chain events.
3330 *
3331 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003332 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 * locks for us to safely manipulate the slave devices (RTNL lock,
3334 * dev_probe_lock).
3335 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003336static int bond_netdev_event(struct notifier_block *this,
3337 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
3339 struct net_device *event_dev = (struct net_device *)ptr;
3340
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003341 pr_debug("event_dev: %s, event: %lx\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003342 event_dev ? event_dev->name : "None",
3343 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003345 if (!(event_dev->priv_flags & IFF_BONDING))
3346 return NOTIFY_DONE;
3347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 if (event_dev->flags & IFF_MASTER) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003349 pr_debug("IFF_MASTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 return bond_master_netdev_event(event, event_dev);
3351 }
3352
3353 if (event_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003354 pr_debug("IFF_SLAVE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 return bond_slave_netdev_event(event, event_dev);
3356 }
3357
3358 return NOTIFY_DONE;
3359}
3360
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003361/*
3362 * bond_inetaddr_event: handle inetaddr notifier chain events.
3363 *
3364 * We keep track of device IPs primarily to use as source addresses in
3365 * ARP monitor probes (rather than spewing out broadcasts all the time).
3366 *
3367 * We track one IP for the main device (if it has one), plus one per VLAN.
3368 */
3369static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3370{
3371 struct in_ifaddr *ifa = ptr;
3372 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003373 struct bond_net *bn = net_generic(dev_net(event_dev), bond_net_id);
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003374 struct bonding *bond;
3375 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003376
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003377 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003378 if (bond->dev == event_dev) {
3379 switch (event) {
3380 case NETDEV_UP:
3381 bond->master_ip = ifa->ifa_local;
3382 return NOTIFY_OK;
3383 case NETDEV_DOWN:
3384 bond->master_ip = bond_glean_dev_ip(bond->dev);
3385 return NOTIFY_OK;
3386 default:
3387 return NOTIFY_DONE;
3388 }
3389 }
3390
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003391 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +00003392 if (!bond->vlgrp)
3393 continue;
Dan Aloni5c15bde2007-03-02 20:44:51 -08003394 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003395 if (vlan_dev == event_dev) {
3396 switch (event) {
3397 case NETDEV_UP:
3398 vlan->vlan_ip = ifa->ifa_local;
3399 return NOTIFY_OK;
3400 case NETDEV_DOWN:
3401 vlan->vlan_ip =
3402 bond_glean_dev_ip(vlan_dev);
3403 return NOTIFY_OK;
3404 default:
3405 return NOTIFY_DONE;
3406 }
3407 }
3408 }
3409 }
3410 return NOTIFY_DONE;
3411}
3412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413static struct notifier_block bond_netdev_notifier = {
3414 .notifier_call = bond_netdev_event,
3415};
3416
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003417static struct notifier_block bond_inetaddr_notifier = {
3418 .notifier_call = bond_inetaddr_event,
3419};
3420
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003421/*---------------------------- Hashing Policies -----------------------------*/
3422
3423/*
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003424 * Hash for the output device based upon layer 2 and layer 3 data. If
3425 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3426 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003427static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003428{
3429 struct ethhdr *data = (struct ethhdr *)skb->data;
3430 struct iphdr *iph = ip_hdr(skb);
3431
Brian Haleyf14c4e42008-09-02 10:08:08 -04003432 if (skb->protocol == htons(ETH_P_IP)) {
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003433 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
Jasper Spaansd3da6832009-10-23 04:08:46 +00003434 (data->h_dest[5] ^ data->h_source[5])) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003435 }
3436
Jasper Spaansd3da6832009-10-23 04:08:46 +00003437 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003438}
3439
3440/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003441 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003442 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3443 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3444 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003445static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003446{
3447 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003448 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003449 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003450 int layer4_xor = 0;
3451
Brian Haleyf14c4e42008-09-02 10:08:08 -04003452 if (skb->protocol == htons(ETH_P_IP)) {
3453 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003454 (iph->protocol == IPPROTO_TCP ||
3455 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003456 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003457 }
3458 return (layer4_xor ^
3459 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3460
3461 }
3462
Jasper Spaansd3da6832009-10-23 04:08:46 +00003463 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003464}
3465
3466/*
3467 * Hash for the output device based upon layer 2 data
3468 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003469static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003470{
3471 struct ethhdr *data = (struct ethhdr *)skb->data;
3472
Jasper Spaansd3da6832009-10-23 04:08:46 +00003473 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003474}
3475
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476/*-------------------------- Device entry points ----------------------------*/
3477
3478static int bond_open(struct net_device *bond_dev)
3479{
Wang Chen454d7c92008-11-12 23:37:49 -08003480 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
3482 bond->kill_timers = 0;
3483
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003484 INIT_DELAYED_WORK(&bond->mcast_work, bond_resend_igmp_join_requests_delayed);
3485
Holger Eitzenberger58402052008-12-09 23:07:13 -08003486 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 /* bond_alb_initialize must be called before the timer
3488 * is started.
3489 */
3490 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3491 /* something went wrong - fail the open operation */
stephen hemmingerb4739462010-01-25 23:34:15 +00003492 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 }
3494
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003495 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3496 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 }
3498
3499 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003500 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3501 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 }
3503
3504 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003505 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3506 INIT_DELAYED_WORK(&bond->arp_work,
3507 bond_activebackup_arp_mon);
3508 else
3509 INIT_DELAYED_WORK(&bond->arp_work,
3510 bond_loadbalance_arp_mon);
3511
3512 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003513 if (bond->params.arp_validate)
Jiri Pirko3aba8912011-04-19 03:48:16 +00003514 bond->recv_probe = bond_arp_rcv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 }
3516
3517 if (bond->params.mode == BOND_MODE_8023AD) {
Adrian Bunka40745f2007-10-24 18:27:43 +02003518 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003519 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 /* register to receive LACPDUs */
Jiri Pirko3aba8912011-04-19 03:48:16 +00003521 bond->recv_probe = bond_3ad_lacpdu_recv;
Jay Vosburghfd989c82008-11-04 17:51:16 -08003522 bond_3ad_initiate_agg_selection(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 }
3524
3525 return 0;
3526}
3527
3528static int bond_close(struct net_device *bond_dev)
3529{
Wang Chen454d7c92008-11-12 23:37:49 -08003530 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 write_lock_bh(&bond->lock);
3533
Ben Hutchingsad246c92011-04-26 15:25:52 +00003534 bond->send_peer_notif = 0;
3535
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 /* signal timers not to re-arm */
3537 bond->kill_timers = 1;
3538
3539 write_unlock_bh(&bond->lock);
3540
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003542 cancel_delayed_work(&bond->mii_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 }
3544
3545 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003546 cancel_delayed_work(&bond->arp_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 }
3548
3549 switch (bond->params.mode) {
3550 case BOND_MODE_8023AD:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003551 cancel_delayed_work(&bond->ad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 break;
3553 case BOND_MODE_TLB:
3554 case BOND_MODE_ALB:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003555 cancel_delayed_work(&bond->alb_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 break;
3557 default:
3558 break;
3559 }
3560
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003561 if (delayed_work_pending(&bond->mcast_work))
3562 cancel_delayed_work(&bond->mcast_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
Holger Eitzenberger58402052008-12-09 23:07:13 -08003564 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 /* Must be called only after all
3566 * slaves have been released
3567 */
3568 bond_alb_deinitialize(bond);
3569 }
Jiri Pirko3aba8912011-04-19 03:48:16 +00003570 bond->recv_probe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
3572 return 0;
3573}
3574
Eric Dumazet28172732010-07-07 14:58:56 -07003575static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3576 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577{
Wang Chen454d7c92008-11-12 23:37:49 -08003578 struct bonding *bond = netdev_priv(bond_dev);
Eric Dumazet28172732010-07-07 14:58:56 -07003579 struct rtnl_link_stats64 temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 struct slave *slave;
3581 int i;
3582
Eric Dumazet28172732010-07-07 14:58:56 -07003583 memset(stats, 0, sizeof(*stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
3585 read_lock_bh(&bond->lock);
3586
3587 bond_for_each_slave(bond, slave, i) {
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00003588 const struct rtnl_link_stats64 *sstats =
Eric Dumazet28172732010-07-07 14:58:56 -07003589 dev_get_stats(slave->dev, &temp);
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08003590
Eric Dumazet28172732010-07-07 14:58:56 -07003591 stats->rx_packets += sstats->rx_packets;
3592 stats->rx_bytes += sstats->rx_bytes;
3593 stats->rx_errors += sstats->rx_errors;
3594 stats->rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
Eric Dumazet28172732010-07-07 14:58:56 -07003596 stats->tx_packets += sstats->tx_packets;
3597 stats->tx_bytes += sstats->tx_bytes;
3598 stats->tx_errors += sstats->tx_errors;
3599 stats->tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
Eric Dumazet28172732010-07-07 14:58:56 -07003601 stats->multicast += sstats->multicast;
3602 stats->collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603
Eric Dumazet28172732010-07-07 14:58:56 -07003604 stats->rx_length_errors += sstats->rx_length_errors;
3605 stats->rx_over_errors += sstats->rx_over_errors;
3606 stats->rx_crc_errors += sstats->rx_crc_errors;
3607 stats->rx_frame_errors += sstats->rx_frame_errors;
3608 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3609 stats->rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610
Eric Dumazet28172732010-07-07 14:58:56 -07003611 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3612 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3613 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3614 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3615 stats->tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 }
3617
3618 read_unlock_bh(&bond->lock);
3619
3620 return stats;
3621}
3622
3623static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3624{
3625 struct net_device *slave_dev = NULL;
3626 struct ifbond k_binfo;
3627 struct ifbond __user *u_binfo = NULL;
3628 struct ifslave k_sinfo;
3629 struct ifslave __user *u_sinfo = NULL;
3630 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 int res = 0;
3632
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003633 pr_debug("bond_ioctl: master=%s, cmd=%d\n", bond_dev->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
3635 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 case SIOCGMIIPHY:
3637 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003638 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003640
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 mii->phy_id = 0;
3642 /* Fall Through */
3643 case SIOCGMIIREG:
3644 /*
3645 * We do this again just in case we were called by SIOCGMIIREG
3646 * instead of SIOCGMIIPHY.
3647 */
3648 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003649 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003651
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
3653 if (mii->reg_num == 1) {
Wang Chen454d7c92008-11-12 23:37:49 -08003654 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 mii->val_out = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003656 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 read_lock(&bond->curr_slave_lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003658 if (netif_carrier_ok(bond->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 mii->val_out = BMSR_LSTATUS;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003660
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 read_unlock(&bond->curr_slave_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003662 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 }
3664
3665 return 0;
3666 case BOND_INFO_QUERY_OLD:
3667 case SIOCBONDINFOQUERY:
3668 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3669
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003670 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
3673 res = bond_info_query(bond_dev, &k_binfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003674 if (res == 0 &&
3675 copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
3676 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
3678 return res;
3679 case BOND_SLAVE_INFO_QUERY_OLD:
3680 case SIOCBONDSLAVEINFOQUERY:
3681 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3682
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003683 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
3686 res = bond_slave_info_query(bond_dev, &k_sinfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003687 if (res == 0 &&
3688 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
3689 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690
3691 return res;
3692 default:
3693 /* Go on */
3694 break;
3695 }
3696
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003697 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003700 slave_dev = dev_get_by_name(dev_net(bond_dev), ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003702 pr_debug("slave_dev=%p:\n", slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003704 if (!slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 res = -ENODEV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003706 else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003707 pr_debug("slave_dev->name=%s:\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 switch (cmd) {
3709 case BOND_ENSLAVE_OLD:
3710 case SIOCBONDENSLAVE:
3711 res = bond_enslave(bond_dev, slave_dev);
3712 break;
3713 case BOND_RELEASE_OLD:
3714 case SIOCBONDRELEASE:
3715 res = bond_release(bond_dev, slave_dev);
3716 break;
3717 case BOND_SETHWADDR_OLD:
3718 case SIOCBONDSETHWADDR:
3719 res = bond_sethwaddr(bond_dev, slave_dev);
3720 break;
3721 case BOND_CHANGE_ACTIVE_OLD:
3722 case SIOCBONDCHANGEACTIVE:
3723 res = bond_ioctl_change_active(bond_dev, slave_dev);
3724 break;
3725 default:
3726 res = -EOPNOTSUPP;
3727 }
3728
3729 dev_put(slave_dev);
3730 }
3731
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 return res;
3733}
3734
Jiri Pirko22bedad32010-04-01 21:22:57 +00003735static bool bond_addr_in_mc_list(unsigned char *addr,
3736 struct netdev_hw_addr_list *list,
3737 int addrlen)
3738{
3739 struct netdev_hw_addr *ha;
3740
3741 netdev_hw_addr_list_for_each(ha, list)
3742 if (!memcmp(ha->addr, addr, addrlen))
3743 return true;
3744
3745 return false;
3746}
3747
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748static void bond_set_multicast_list(struct net_device *bond_dev)
3749{
Wang Chen454d7c92008-11-12 23:37:49 -08003750 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00003751 struct netdev_hw_addr *ha;
3752 bool found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 /*
3755 * Do promisc before checking multicast_mode
3756 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003757 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07003758 /*
3759 * FIXME: Need to handle the error when one of the multi-slaves
3760 * encounters error.
3761 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 bond_set_promiscuity(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003764
3765 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 bond_set_promiscuity(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003767
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
3769 /* set allmulti flag to slaves */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003770 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07003771 /*
3772 * FIXME: Need to handle the error when one of the multi-slaves
3773 * encounters error.
3774 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 bond_set_allmulti(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003777
3778 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 bond_set_allmulti(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003780
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08003782 read_lock(&bond->lock);
3783
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 bond->flags = bond_dev->flags;
3785
3786 /* looking for addresses to add to slaves' mc list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00003787 netdev_for_each_mc_addr(ha, bond_dev) {
3788 found = bond_addr_in_mc_list(ha->addr, &bond->mc_list,
3789 bond_dev->addr_len);
3790 if (!found)
3791 bond_mc_add(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 }
3793
3794 /* looking for addresses to delete from slaves' list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00003795 netdev_hw_addr_list_for_each(ha, &bond->mc_list) {
3796 found = bond_addr_in_mc_list(ha->addr, &bond_dev->mc,
3797 bond_dev->addr_len);
3798 if (!found)
3799 bond_mc_del(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 }
3801
3802 /* save master's multicast list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00003803 __hw_addr_flush(&bond->mc_list);
3804 __hw_addr_add_multiple(&bond->mc_list, &bond_dev->mc,
3805 bond_dev->addr_len, NETDEV_HW_ADDR_T_MULTICAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08003807 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808}
3809
Stephen Hemminger00829822008-11-20 20:14:53 -08003810static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
3811{
3812 struct bonding *bond = netdev_priv(dev);
3813 struct slave *slave = bond->first_slave;
3814
3815 if (slave) {
3816 const struct net_device_ops *slave_ops
3817 = slave->dev->netdev_ops;
3818 if (slave_ops->ndo_neigh_setup)
Patrick McHardy72e22402009-03-05 01:57:44 -08003819 return slave_ops->ndo_neigh_setup(slave->dev, parms);
Stephen Hemminger00829822008-11-20 20:14:53 -08003820 }
3821 return 0;
3822}
3823
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824/*
3825 * Change the MTU of all of a master's slaves to match the master
3826 */
3827static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3828{
Wang Chen454d7c92008-11-12 23:37:49 -08003829 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 struct slave *slave, *stop_at;
3831 int res = 0;
3832 int i;
3833
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003834 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003835 (bond_dev ? bond_dev->name : "None"), new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
3837 /* Can't hold bond->lock with bh disabled here since
3838 * some base drivers panic. On the other hand we can't
3839 * hold bond->lock without bh disabled because we'll
3840 * deadlock. The only solution is to rely on the fact
3841 * that we're under rtnl_lock here, and the slaves
3842 * list won't change. This doesn't solve the problem
3843 * of setting the slave's MTU while it is
3844 * transmitting, but the assumption is that the base
3845 * driver can handle that.
3846 *
3847 * TODO: figure out a way to safely iterate the slaves
3848 * list, but without holding a lock around the actual
3849 * call to the base driver.
3850 */
3851
3852 bond_for_each_slave(bond, slave, i) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003853 pr_debug("s %p s->p %p c_m %p\n",
3854 slave,
3855 slave->prev,
3856 slave->dev->netdev_ops->ndo_change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08003857
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 res = dev_set_mtu(slave->dev, new_mtu);
3859
3860 if (res) {
3861 /* If we failed to set the slave's mtu to the new value
3862 * we must abort the operation even in ACTIVE_BACKUP
3863 * mode, because if we allow the backup slaves to have
3864 * different mtu values than the active slave we'll
3865 * need to change their mtu when doing a failover. That
3866 * means changing their mtu from timer context, which
3867 * is probably not a good idea.
3868 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003869 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 goto unwind;
3871 }
3872 }
3873
3874 bond_dev->mtu = new_mtu;
3875
3876 return 0;
3877
3878unwind:
3879 /* unwind from head to the slave that failed */
3880 stop_at = slave;
3881 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3882 int tmp_res;
3883
3884 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
3885 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003886 pr_debug("unwind err %d dev %s\n",
3887 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 }
3889 }
3890
3891 return res;
3892}
3893
3894/*
3895 * Change HW address
3896 *
3897 * Note that many devices must be down to change the HW address, and
3898 * downing the master releases all slaves. We can make bonds full of
3899 * bonding devices to test this, however.
3900 */
3901static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
3902{
Wang Chen454d7c92008-11-12 23:37:49 -08003903 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 struct sockaddr *sa = addr, tmp_sa;
3905 struct slave *slave, *stop_at;
3906 int res = 0;
3907 int i;
3908
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08003909 if (bond->params.mode == BOND_MODE_ALB)
3910 return bond_alb_set_mac_address(bond_dev, addr);
3911
3912
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003913 pr_debug("bond=%p, name=%s\n",
3914 bond, bond_dev ? bond_dev->name : "None");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
Jay Vosburghdd957c52007-10-09 19:57:24 -07003916 /*
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07003917 * If fail_over_mac is set to active, do nothing and return
3918 * success. Returning an error causes ifenslave to fail.
Jay Vosburghdd957c52007-10-09 19:57:24 -07003919 */
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07003920 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
Jay Vosburghdd957c52007-10-09 19:57:24 -07003921 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07003922
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003923 if (!is_valid_ether_addr(sa->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
3926 /* Can't hold bond->lock with bh disabled here since
3927 * some base drivers panic. On the other hand we can't
3928 * hold bond->lock without bh disabled because we'll
3929 * deadlock. The only solution is to rely on the fact
3930 * that we're under rtnl_lock here, and the slaves
3931 * list won't change. This doesn't solve the problem
3932 * of setting the slave's hw address while it is
3933 * transmitting, but the assumption is that the base
3934 * driver can handle that.
3935 *
3936 * TODO: figure out a way to safely iterate the slaves
3937 * list, but without holding a lock around the actual
3938 * call to the base driver.
3939 */
3940
3941 bond_for_each_slave(bond, slave, i) {
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08003942 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003943 pr_debug("slave %p %s\n", slave, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08003945 if (slave_ops->ndo_set_mac_address == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 res = -EOPNOTSUPP;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003947 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 goto unwind;
3949 }
3950
3951 res = dev_set_mac_address(slave->dev, addr);
3952 if (res) {
3953 /* TODO: consider downing the slave
3954 * and retry ?
3955 * User should expect communications
3956 * breakage anyway until ARP finish
3957 * updating, so...
3958 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003959 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 goto unwind;
3961 }
3962 }
3963
3964 /* success */
3965 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
3966 return 0;
3967
3968unwind:
3969 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
3970 tmp_sa.sa_family = bond_dev->type;
3971
3972 /* unwind from head to the slave that failed */
3973 stop_at = slave;
3974 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3975 int tmp_res;
3976
3977 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
3978 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003979 pr_debug("unwind err %d dev %s\n",
3980 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 }
3982 }
3983
3984 return res;
3985}
3986
3987static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
3988{
Wang Chen454d7c92008-11-12 23:37:49 -08003989 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 struct slave *slave, *start_at;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07003991 int i, slave_no, res = 1;
Andy Gospodareka2fd9402010-03-25 14:49:05 +00003992 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
Jay Vosburghcf5f9042007-10-17 17:37:47 -07003994 /*
Andy Gospodareka2fd9402010-03-25 14:49:05 +00003995 * Start with the curr_active_slave that joined the bond as the
3996 * default for sending IGMP traffic. For failover purposes one
3997 * needs to maintain some consistency for the interface that will
3998 * send the join/membership reports. The curr_active_slave found
3999 * will send all of this type of traffic.
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004000 */
Eric Dumazet00ae7022010-03-30 23:08:37 +00004001 if ((iph->protocol == IPPROTO_IGMP) &&
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004002 (skb->protocol == htons(ETH_P_IP))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004004 read_lock(&bond->curr_slave_lock);
4005 slave = bond->curr_active_slave;
4006 read_unlock(&bond->curr_slave_lock);
4007
4008 if (!slave)
4009 goto out;
4010 } else {
4011 /*
4012 * Concurrent TX may collide on rr_tx_counter; we accept
4013 * that as being rare enough not to justify using an
4014 * atomic op here.
4015 */
4016 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
4017
4018 bond_for_each_slave(bond, slave, i) {
4019 slave_no--;
4020 if (slave_no < 0)
4021 break;
4022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 }
4024
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004025 start_at = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 bond_for_each_slave_from(bond, slave, i, start_at) {
4027 if (IS_UP(slave->dev) &&
4028 (slave->link == BOND_LINK_UP) &&
Jiri Pirkoe30bc062011-03-12 03:14:37 +00004029 bond_is_active_slave(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 res = bond_dev_queue_xmit(bond, skb, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 break;
4032 }
4033 }
4034
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035out:
4036 if (res) {
4037 /* no suitable interface, frame not sent */
4038 dev_kfree_skb(skb);
4039 }
Michał Mirosław0693e882011-05-07 01:48:02 +00004040
Patrick McHardyec634fe2009-07-05 19:23:38 -07004041 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042}
4043
John W. Linville075897c2005-09-28 17:50:53 -04004044
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045/*
4046 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4047 * the bond has a usable interface.
4048 */
4049static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4050{
Wang Chen454d7c92008-11-12 23:37:49 -08004051 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 int res = 1;
4053
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 read_lock(&bond->curr_slave_lock);
4055
Michał Mirosław0693e882011-05-07 01:48:02 +00004056 if (bond->curr_active_slave)
4057 res = bond_dev_queue_xmit(bond, skb,
4058 bond->curr_active_slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004060 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 /* no suitable interface, frame not sent */
4062 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004063
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 read_unlock(&bond->curr_slave_lock);
Michał Mirosław0693e882011-05-07 01:48:02 +00004065
Patrick McHardyec634fe2009-07-05 19:23:38 -07004066 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067}
4068
4069/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004070 * In bond_xmit_xor() , we determine the output device by using a pre-
4071 * determined xmit_hash_policy(), If the selected device is not enabled,
4072 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 */
4074static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4075{
Wang Chen454d7c92008-11-12 23:37:49 -08004076 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 struct slave *slave, *start_at;
4078 int slave_no;
4079 int i;
4080 int res = 1;
4081
Jasper Spaansa361c832009-10-23 04:09:24 +00004082 slave_no = bond->xmit_hash_policy(skb, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
4084 bond_for_each_slave(bond, slave, i) {
4085 slave_no--;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004086 if (slave_no < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 }
4089
4090 start_at = slave;
4091
4092 bond_for_each_slave_from(bond, slave, i, start_at) {
4093 if (IS_UP(slave->dev) &&
4094 (slave->link == BOND_LINK_UP) &&
Jiri Pirkoe30bc062011-03-12 03:14:37 +00004095 bond_is_active_slave(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4097 break;
4098 }
4099 }
4100
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 if (res) {
4102 /* no suitable interface, frame not sent */
4103 dev_kfree_skb(skb);
4104 }
Michał Mirosław0693e882011-05-07 01:48:02 +00004105
Patrick McHardyec634fe2009-07-05 19:23:38 -07004106 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107}
4108
4109/*
4110 * in broadcast mode, we send everything to all usable interfaces.
4111 */
4112static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4113{
Wang Chen454d7c92008-11-12 23:37:49 -08004114 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 struct slave *slave, *start_at;
4116 struct net_device *tx_dev = NULL;
4117 int i;
4118 int res = 1;
4119
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 read_lock(&bond->curr_slave_lock);
4121 start_at = bond->curr_active_slave;
4122 read_unlock(&bond->curr_slave_lock);
4123
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004124 if (!start_at)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
4127 bond_for_each_slave_from(bond, slave, i, start_at) {
4128 if (IS_UP(slave->dev) &&
4129 (slave->link == BOND_LINK_UP) &&
Jiri Pirkoe30bc062011-03-12 03:14:37 +00004130 bond_is_active_slave(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 if (tx_dev) {
4132 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4133 if (!skb2) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004134 pr_err("%s: Error: bond_xmit_broadcast(): skb_clone() failed\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08004135 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 continue;
4137 }
4138
4139 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4140 if (res) {
4141 dev_kfree_skb(skb2);
4142 continue;
4143 }
4144 }
4145 tx_dev = slave->dev;
4146 }
4147 }
4148
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004149 if (tx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 res = bond_dev_queue_xmit(bond, skb, tx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
4152out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004153 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 /* no suitable interface, frame not sent */
4155 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004156
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 /* frame sent to all suitable interfaces */
Patrick McHardyec634fe2009-07-05 19:23:38 -07004158 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159}
4160
4161/*------------------------- Device initialization ---------------------------*/
4162
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004163static void bond_set_xmit_hash_policy(struct bonding *bond)
4164{
4165 switch (bond->params.xmit_policy) {
4166 case BOND_XMIT_POLICY_LAYER23:
4167 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4168 break;
4169 case BOND_XMIT_POLICY_LAYER34:
4170 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4171 break;
4172 case BOND_XMIT_POLICY_LAYER2:
4173 default:
4174 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4175 break;
4176 }
4177}
4178
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004179/*
4180 * Lookup the slave that corresponds to a qid
4181 */
4182static inline int bond_slave_override(struct bonding *bond,
4183 struct sk_buff *skb)
4184{
4185 int i, res = 1;
4186 struct slave *slave = NULL;
4187 struct slave *check_slave;
4188
Michał Mirosław0693e882011-05-07 01:48:02 +00004189 if (!skb->queue_mapping)
4190 return 1;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004191
4192 /* Find out if any slaves have the same mapping as this skb. */
4193 bond_for_each_slave(bond, check_slave, i) {
4194 if (check_slave->queue_id == skb->queue_mapping) {
4195 slave = check_slave;
4196 break;
4197 }
4198 }
4199
4200 /* If the slave isn't UP, use default transmit policy. */
4201 if (slave && slave->queue_id && IS_UP(slave->dev) &&
4202 (slave->link == BOND_LINK_UP)) {
4203 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4204 }
4205
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004206 return res;
4207}
4208
4209static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb)
4210{
4211 /*
4212 * This helper function exists to help dev_pick_tx get the correct
Phil Oesterfd0e4352011-03-14 06:22:04 +00004213 * destination queue. Using a helper function skips a call to
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004214 * skb_tx_hash and will put the skbs in the queue we expect on their
4215 * way down to the bonding driver.
4216 */
Phil Oesterfd0e4352011-03-14 06:22:04 +00004217 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
4218
4219 if (unlikely(txq >= dev->real_num_tx_queues)) {
David Decotignyd30ee672011-04-13 15:22:29 +00004220 do {
Phil Oesterfd0e4352011-03-14 06:22:04 +00004221 txq -= dev->real_num_tx_queues;
David Decotignyd30ee672011-04-13 15:22:29 +00004222 } while (txq >= dev->real_num_tx_queues);
Phil Oesterfd0e4352011-03-14 06:22:04 +00004223 }
4224 return txq;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004225}
4226
Michał Mirosław0693e882011-05-07 01:48:02 +00004227static netdev_tx_t __bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
Stephen Hemminger00829822008-11-20 20:14:53 -08004228{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004229 struct bonding *bond = netdev_priv(dev);
4230
4231 if (TX_QUEUE_OVERRIDE(bond->params.mode)) {
4232 if (!bond_slave_override(bond, skb))
4233 return NETDEV_TX_OK;
4234 }
Stephen Hemminger00829822008-11-20 20:14:53 -08004235
4236 switch (bond->params.mode) {
4237 case BOND_MODE_ROUNDROBIN:
4238 return bond_xmit_roundrobin(skb, dev);
4239 case BOND_MODE_ACTIVEBACKUP:
4240 return bond_xmit_activebackup(skb, dev);
4241 case BOND_MODE_XOR:
4242 return bond_xmit_xor(skb, dev);
4243 case BOND_MODE_BROADCAST:
4244 return bond_xmit_broadcast(skb, dev);
4245 case BOND_MODE_8023AD:
4246 return bond_3ad_xmit_xor(skb, dev);
4247 case BOND_MODE_ALB:
4248 case BOND_MODE_TLB:
4249 return bond_alb_xmit(skb, dev);
4250 default:
4251 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004252 pr_err("%s: Error: Unknown bonding mode %d\n",
4253 dev->name, bond->params.mode);
Stephen Hemminger00829822008-11-20 20:14:53 -08004254 WARN_ON_ONCE(1);
4255 dev_kfree_skb(skb);
4256 return NETDEV_TX_OK;
4257 }
4258}
4259
Michał Mirosław0693e882011-05-07 01:48:02 +00004260static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
4261{
4262 struct bonding *bond = netdev_priv(dev);
4263 netdev_tx_t ret = NETDEV_TX_OK;
4264
4265 /*
4266 * If we risk deadlock from transmitting this in the
4267 * netpoll path, tell netpoll to queue the frame for later tx
4268 */
4269 if (is_netpoll_tx_blocked(dev))
4270 return NETDEV_TX_BUSY;
4271
4272 read_lock(&bond->lock);
4273
4274 if (bond->slave_cnt)
4275 ret = __bond_start_xmit(skb, dev);
4276 else
4277 dev_kfree_skb(skb);
4278
4279 read_unlock(&bond->lock);
4280
4281 return ret;
4282}
Stephen Hemminger00829822008-11-20 20:14:53 -08004283
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284/*
4285 * set bond mode specific net device operations
4286 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004287void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004289 struct net_device *bond_dev = bond->dev;
4290
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 switch (mode) {
4292 case BOND_MODE_ROUNDROBIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 break;
4294 case BOND_MODE_ACTIVEBACKUP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 break;
4296 case BOND_MODE_XOR:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004297 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 break;
4299 case BOND_MODE_BROADCAST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 break;
4301 case BOND_MODE_8023AD:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004302 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004305 /* FALLTHRU */
4306 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 break;
4308 default:
4309 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004310 pr_err("%s: Error: Unknown bonding mode %d\n",
4311 bond_dev->name, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 break;
4313 }
4314}
4315
Jay Vosburgh217df672005-09-26 16:11:50 -07004316static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4317 struct ethtool_drvinfo *drvinfo)
4318{
4319 strncpy(drvinfo->driver, DRV_NAME, 32);
4320 strncpy(drvinfo->version, DRV_VERSION, 32);
4321 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4322}
4323
Jeff Garzik7282d492006-09-13 14:30:00 -04004324static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004325 .get_drvinfo = bond_ethtool_get_drvinfo,
Stephen Hemmingerfa53eba2008-09-13 21:17:09 -04004326 .get_link = ethtool_op_get_link,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004327};
4328
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004329static const struct net_device_ops bond_netdev_ops = {
Stephen Hemminger181470f2009-06-12 19:02:52 +00004330 .ndo_init = bond_init,
Stephen Hemminger9e716262009-06-12 19:02:47 +00004331 .ndo_uninit = bond_uninit,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004332 .ndo_open = bond_open,
4333 .ndo_stop = bond_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08004334 .ndo_start_xmit = bond_start_xmit,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004335 .ndo_select_queue = bond_select_queue,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00004336 .ndo_get_stats64 = bond_get_stats,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004337 .ndo_do_ioctl = bond_do_ioctl,
4338 .ndo_set_multicast_list = bond_set_multicast_list,
4339 .ndo_change_mtu = bond_change_mtu,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004340 .ndo_set_mac_address = bond_set_mac_address,
Stephen Hemminger00829822008-11-20 20:14:53 -08004341 .ndo_neigh_setup = bond_neigh_setup,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004342 .ndo_vlan_rx_register = bond_vlan_rx_register,
4343 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4344 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
WANG Congf6dc31a2010-05-06 00:48:51 -07004345#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00004346 .ndo_netpoll_setup = bond_netpoll_setup,
WANG Congf6dc31a2010-05-06 00:48:51 -07004347 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
4348 .ndo_poll_controller = bond_poll_controller,
4349#endif
Jiri Pirko9232ecc2011-02-13 09:33:01 +00004350 .ndo_add_slave = bond_enslave,
4351 .ndo_del_slave = bond_release,
Michał Mirosławb2a103e2011-05-07 03:22:17 +00004352 .ndo_fix_features = bond_fix_features,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004353};
4354
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004355static void bond_destructor(struct net_device *bond_dev)
4356{
4357 struct bonding *bond = netdev_priv(bond_dev);
4358 if (bond->wq)
4359 destroy_workqueue(bond->wq);
4360 free_netdev(bond_dev);
4361}
4362
Stephen Hemminger181470f2009-06-12 19:02:52 +00004363static void bond_setup(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364{
Wang Chen454d7c92008-11-12 23:37:49 -08004365 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 /* initialize rwlocks */
4368 rwlock_init(&bond->lock);
4369 rwlock_init(&bond->curr_slave_lock);
4370
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00004371 bond->params = bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
4373 /* Initialize pointers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 bond->dev = bond_dev;
4375 INIT_LIST_HEAD(&bond->vlan_list);
4376
4377 /* Initialize the device entry points */
Stephen Hemminger181470f2009-06-12 19:02:52 +00004378 ether_setup(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004379 bond_dev->netdev_ops = &bond_netdev_ops;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004380 bond_dev->ethtool_ops = &bond_ethtool_ops;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004381 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004383 bond_dev->destructor = bond_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
4385 /* Initialize the device options */
4386 bond_dev->tx_queue_len = 0;
4387 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004388 bond_dev->priv_flags |= IFF_BONDING;
Stephen Hemminger181470f2009-06-12 19:02:52 +00004389 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
4390
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 /* At first, we block adding VLANs. That's the only way to
4392 * prevent problems that occur when adding VLANs over an
4393 * empty bond. The block will be removed once non-challenged
4394 * slaves are enslaved.
4395 */
4396 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4397
Herbert Xu932ff272006-06-09 12:20:56 -07004398 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 * transmitting */
4400 bond_dev->features |= NETIF_F_LLTX;
4401
4402 /* By default, we declare the bond to be fully
4403 * VLAN hardware accelerated capable. Special
4404 * care is taken in the various xmit functions
4405 * when there are slaves that are not hw accel
4406 * capable
4407 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408
Michał Mirosławb2a103e2011-05-07 03:22:17 +00004409 bond_dev->hw_features = BOND_VLAN_FEATURES |
4410 NETIF_F_HW_VLAN_TX |
4411 NETIF_F_HW_VLAN_RX |
4412 NETIF_F_HW_VLAN_FILTER;
4413
4414 bond_dev->hw_features &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_NO_CSUM);
4415 bond_dev->features |= bond_dev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416}
4417
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004418static void bond_work_cancel_all(struct bonding *bond)
4419{
4420 write_lock_bh(&bond->lock);
4421 bond->kill_timers = 1;
4422 write_unlock_bh(&bond->lock);
4423
4424 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4425 cancel_delayed_work(&bond->mii_work);
4426
4427 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4428 cancel_delayed_work(&bond->arp_work);
4429
4430 if (bond->params.mode == BOND_MODE_ALB &&
4431 delayed_work_pending(&bond->alb_work))
4432 cancel_delayed_work(&bond->alb_work);
4433
4434 if (bond->params.mode == BOND_MODE_8023AD &&
4435 delayed_work_pending(&bond->ad_work))
4436 cancel_delayed_work(&bond->ad_work);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00004437
4438 if (delayed_work_pending(&bond->mcast_work))
4439 cancel_delayed_work(&bond->mcast_work);
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004440}
4441
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004442/*
4443* Destroy a bonding device.
4444* Must be under rtnl_lock when this function is called.
4445*/
4446static void bond_uninit(struct net_device *bond_dev)
Jay Vosburgha434e432008-10-30 17:41:15 -07004447{
Wang Chen454d7c92008-11-12 23:37:49 -08004448 struct bonding *bond = netdev_priv(bond_dev);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004449 struct vlan_entry *vlan, *tmp;
Jay Vosburgha434e432008-10-30 17:41:15 -07004450
WANG Congf6dc31a2010-05-06 00:48:51 -07004451 bond_netpoll_cleanup(bond_dev);
4452
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004453 /* Release the bonded slaves */
4454 bond_release_all(bond_dev);
4455
Jay Vosburgha434e432008-10-30 17:41:15 -07004456 list_del(&bond->bond_list);
4457
4458 bond_work_cancel_all(bond);
4459
Jay Vosburgha434e432008-10-30 17:41:15 -07004460 bond_remove_proc_entry(bond);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004461
Taku Izumif073c7c2010-12-09 15:17:13 +00004462 bond_debug_unregister(bond);
4463
Jiri Pirko22bedad32010-04-01 21:22:57 +00004464 __hw_addr_flush(&bond->mc_list);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004465
4466 list_for_each_entry_safe(vlan, tmp, &bond->vlan_list, vlan_list) {
4467 list_del(&vlan->vlan_list);
4468 kfree(vlan);
4469 }
Jay Vosburgha434e432008-10-30 17:41:15 -07004470}
4471
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472/*------------------------- Module initialization ---------------------------*/
4473
4474/*
4475 * Convert string input module parms. Accept either the
Jay Vosburghece95f72008-01-17 16:25:01 -08004476 * number of the mode or its string name. A bit complicated because
4477 * some mode names are substrings of other names, and calls from sysfs
4478 * may have whitespace in the name (trailing newlines, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 */
Holger Eitzenberger325dcf72008-12-09 23:10:17 -08004480int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481{
Hannes Eder54b87322009-02-14 11:15:49 +00004482 int modeint = -1, i, rv;
Jay Vosburgha42e5342008-01-29 18:07:43 -08004483 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
Jay Vosburghece95f72008-01-17 16:25:01 -08004484
Jay Vosburgha42e5342008-01-29 18:07:43 -08004485 for (p = (char *)buf; *p; p++)
4486 if (!(isdigit(*p) || isspace(*p)))
4487 break;
4488
4489 if (*p)
Jay Vosburghece95f72008-01-17 16:25:01 -08004490 rv = sscanf(buf, "%20s", modestr);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004491 else
Hannes Eder54b87322009-02-14 11:15:49 +00004492 rv = sscanf(buf, "%d", &modeint);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004493
4494 if (!rv)
4495 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
4497 for (i = 0; tbl[i].modename; i++) {
Hannes Eder54b87322009-02-14 11:15:49 +00004498 if (modeint == tbl[i].mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 return tbl[i].mode;
Jay Vosburghece95f72008-01-17 16:25:01 -08004500 if (strcmp(modestr, tbl[i].modename) == 0)
4501 return tbl[i].mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 }
4503
4504 return -1;
4505}
4506
4507static int bond_check_params(struct bond_params *params)
4508{
Jiri Pirkoa5499522009-09-25 03:28:09 +00004509 int arp_validate_value, fail_over_mac_value, primary_reselect_value;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004510
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 /*
4512 * Convert string parameters.
4513 */
4514 if (mode) {
4515 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4516 if (bond_mode == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004517 pr_err("Error: Invalid bonding mode \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 mode == NULL ? "NULL" : mode);
4519 return -EINVAL;
4520 }
4521 }
4522
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004523 if (xmit_hash_policy) {
4524 if ((bond_mode != BOND_MODE_XOR) &&
4525 (bond_mode != BOND_MODE_8023AD)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004526 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004527 bond_mode_name(bond_mode));
4528 } else {
4529 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4530 xmit_hashtype_tbl);
4531 if (xmit_hashtype == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004532 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004533 xmit_hash_policy == NULL ? "NULL" :
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004534 xmit_hash_policy);
4535 return -EINVAL;
4536 }
4537 }
4538 }
4539
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 if (lacp_rate) {
4541 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004542 pr_info("lacp_rate param is irrelevant in mode %s\n",
4543 bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 } else {
4545 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4546 if (lacp_fast == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004547 pr_err("Error: Invalid lacp rate \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 lacp_rate == NULL ? "NULL" : lacp_rate);
4549 return -EINVAL;
4550 }
4551 }
4552 }
4553
Jay Vosburghfd989c82008-11-04 17:51:16 -08004554 if (ad_select) {
4555 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4556 if (params->ad_select == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004557 pr_err("Error: Invalid ad_select \"%s\"\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -08004558 ad_select == NULL ? "NULL" : ad_select);
4559 return -EINVAL;
4560 }
4561
4562 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004563 pr_warning("ad_select param only affects 802.3ad mode\n");
Jay Vosburghfd989c82008-11-04 17:51:16 -08004564 }
4565 } else {
4566 params->ad_select = BOND_AD_STABLE;
4567 }
4568
Nicolas de Pesloüanf5841302009-08-28 13:18:34 +00004569 if (max_bonds < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004570 pr_warning("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4571 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 max_bonds = BOND_DEFAULT_MAX_BONDS;
4573 }
4574
4575 if (miimon < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004576 pr_warning("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to %d\n",
4577 miimon, INT_MAX, BOND_LINK_MON_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 miimon = BOND_LINK_MON_INTERV;
4579 }
4580
4581 if (updelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004582 pr_warning("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4583 updelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 updelay = 0;
4585 }
4586
4587 if (downdelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004588 pr_warning("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4589 downdelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 downdelay = 0;
4591 }
4592
4593 if ((use_carrier != 0) && (use_carrier != 1)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004594 pr_warning("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
4595 use_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 use_carrier = 1;
4597 }
4598
Ben Hutchingsad246c92011-04-26 15:25:52 +00004599 if (num_peer_notif < 0 || num_peer_notif > 255) {
4600 pr_warning("Warning: num_grat_arp/num_unsol_na (%d) not in range 0-255 so it was reset to 1\n",
4601 num_peer_notif);
4602 num_peer_notif = 1;
4603 }
4604
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 /* reset values for 802.3ad */
4606 if (bond_mode == BOND_MODE_8023AD) {
4607 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004608 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 +00004609 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 miimon = 100;
4611 }
4612 }
4613
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004614 if (tx_queues < 1 || tx_queues > 255) {
4615 pr_warning("Warning: tx_queues (%d) should be between "
4616 "1 and 255, resetting to %d\n",
4617 tx_queues, BOND_DEFAULT_TX_QUEUES);
4618 tx_queues = BOND_DEFAULT_TX_QUEUES;
4619 }
4620
Andy Gospodarekebd8e492010-06-02 08:39:21 +00004621 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
4622 pr_warning("Warning: all_slaves_active module parameter (%d), "
4623 "not of valid value (0/1), so it was set to "
4624 "0\n", all_slaves_active);
4625 all_slaves_active = 0;
4626 }
4627
Flavio Leitnerc2952c32010-10-05 14:23:59 +00004628 if (resend_igmp < 0 || resend_igmp > 255) {
4629 pr_warning("Warning: resend_igmp (%d) should be between "
4630 "0 and 255, resetting to %d\n",
4631 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
4632 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
4633 }
4634
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 /* reset values for TLB/ALB */
4636 if ((bond_mode == BOND_MODE_TLB) ||
4637 (bond_mode == BOND_MODE_ALB)) {
4638 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004639 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 +00004640 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 miimon = 100;
4642 }
4643 }
4644
4645 if (bond_mode == BOND_MODE_ALB) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004646 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",
4647 updelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 }
4649
4650 if (!miimon) {
4651 if (updelay || downdelay) {
4652 /* just warn the user the up/down delay will have
4653 * no effect since miimon is zero...
4654 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004655 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",
4656 updelay, downdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 }
4658 } else {
4659 /* don't allow arp monitoring */
4660 if (arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004661 pr_warning("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
4662 miimon, arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 arp_interval = 0;
4664 }
4665
4666 if ((updelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004667 pr_warning("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
4668 updelay, miimon,
4669 (updelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 }
4671
4672 updelay /= miimon;
4673
4674 if ((downdelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004675 pr_warning("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
4676 downdelay, miimon,
4677 (downdelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 }
4679
4680 downdelay /= miimon;
4681 }
4682
4683 if (arp_interval < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004684 pr_warning("Warning: arp_interval module parameter (%d) , not in range 0-%d, so it was reset to %d\n",
4685 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 arp_interval = BOND_LINK_ARP_INTERV;
4687 }
4688
4689 for (arp_ip_count = 0;
4690 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4691 arp_ip_count++) {
4692 /* not complete check, but should be good enough to
4693 catch mistakes */
4694 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004695 pr_warning("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
4696 arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 arp_interval = 0;
4698 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04004699 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 arp_target[arp_ip_count] = ip;
4701 }
4702 }
4703
4704 if (arp_interval && !arp_ip_count) {
4705 /* don't allow arping if no arp_ip_target given... */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004706 pr_warning("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
4707 arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 arp_interval = 0;
4709 }
4710
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004711 if (arp_validate) {
4712 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004713 pr_err("arp_validate only supported in active-backup mode\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004714 return -EINVAL;
4715 }
4716 if (!arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004717 pr_err("arp_validate requires arp_interval\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004718 return -EINVAL;
4719 }
4720
4721 arp_validate_value = bond_parse_parm(arp_validate,
4722 arp_validate_tbl);
4723 if (arp_validate_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004724 pr_err("Error: invalid arp_validate \"%s\"\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004725 arp_validate == NULL ? "NULL" : arp_validate);
4726 return -EINVAL;
4727 }
4728 } else
4729 arp_validate_value = 0;
4730
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 if (miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004732 pr_info("MII link monitoring set to %d ms\n", miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 } else if (arp_interval) {
4734 int i;
4735
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004736 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
4737 arp_interval,
4738 arp_validate_tbl[arp_validate_value].modename,
4739 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740
4741 for (i = 0; i < arp_ip_count; i++)
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00004742 pr_info(" %s", arp_ip_target[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00004744 pr_info("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745
Jay Vosburghb8a97872008-06-13 18:12:04 -07004746 } else if (max_bonds) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 /* miimon and arp_interval not set, we need one so things
4748 * work as expected, see bonding.txt for details
4749 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004750 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 -07004751 }
4752
4753 if (primary && !USES_PRIMARY(bond_mode)) {
4754 /* currently, using a primary only makes sense
4755 * in active backup, TLB or ALB modes
4756 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004757 pr_warning("Warning: %s primary device specified but has no effect in %s mode\n",
4758 primary, bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 primary = NULL;
4760 }
4761
Jiri Pirkoa5499522009-09-25 03:28:09 +00004762 if (primary && primary_reselect) {
4763 primary_reselect_value = bond_parse_parm(primary_reselect,
4764 pri_reselect_tbl);
4765 if (primary_reselect_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004766 pr_err("Error: Invalid primary_reselect \"%s\"\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00004767 primary_reselect ==
4768 NULL ? "NULL" : primary_reselect);
4769 return -EINVAL;
4770 }
4771 } else {
4772 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
4773 }
4774
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004775 if (fail_over_mac) {
4776 fail_over_mac_value = bond_parse_parm(fail_over_mac,
4777 fail_over_mac_tbl);
4778 if (fail_over_mac_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004779 pr_err("Error: invalid fail_over_mac \"%s\"\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004780 arp_validate == NULL ? "NULL" : arp_validate);
4781 return -EINVAL;
4782 }
4783
4784 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004785 pr_warning("Warning: fail_over_mac only affects active-backup mode.\n");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004786 } else {
4787 fail_over_mac_value = BOND_FOM_NONE;
4788 }
Jay Vosburghdd957c52007-10-09 19:57:24 -07004789
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 /* fill params struct with the proper values */
4791 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004792 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 params->miimon = miimon;
Ben Hutchingsad246c92011-04-26 15:25:52 +00004794 params->num_peer_notif = num_peer_notif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004796 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 params->updelay = updelay;
4798 params->downdelay = downdelay;
4799 params->use_carrier = use_carrier;
4800 params->lacp_fast = lacp_fast;
4801 params->primary[0] = 0;
Jiri Pirkoa5499522009-09-25 03:28:09 +00004802 params->primary_reselect = primary_reselect_value;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004803 params->fail_over_mac = fail_over_mac_value;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004804 params->tx_queues = tx_queues;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00004805 params->all_slaves_active = all_slaves_active;
Flavio Leitnerc2952c32010-10-05 14:23:59 +00004806 params->resend_igmp = resend_igmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
4808 if (primary) {
4809 strncpy(params->primary, primary, IFNAMSIZ);
4810 params->primary[IFNAMSIZ - 1] = 0;
4811 }
4812
4813 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4814
4815 return 0;
4816}
4817
Peter Zijlstra0daa23032006-11-08 19:51:01 -08004818static struct lock_class_key bonding_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -07004819static struct lock_class_key bonding_netdev_addr_lock_key;
Peter Zijlstra0daa23032006-11-08 19:51:01 -08004820
David S. Millere8a04642008-07-17 00:34:19 -07004821static void bond_set_lockdep_class_one(struct net_device *dev,
4822 struct netdev_queue *txq,
4823 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -07004824{
4825 lockdep_set_class(&txq->_xmit_lock,
4826 &bonding_netdev_xmit_lock_key);
4827}
4828
4829static void bond_set_lockdep_class(struct net_device *dev)
4830{
David S. Millercf508b12008-07-22 14:16:42 -07004831 lockdep_set_class(&dev->addr_list_lock,
4832 &bonding_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -07004833 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -07004834}
4835
Stephen Hemminger181470f2009-06-12 19:02:52 +00004836/*
4837 * Called from registration process
4838 */
4839static int bond_init(struct net_device *bond_dev)
4840{
4841 struct bonding *bond = netdev_priv(bond_dev);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004842 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Neil Horman9fe06172011-05-25 08:13:01 +00004843 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Stephen Hemminger181470f2009-06-12 19:02:52 +00004844
4845 pr_debug("Begin bond_init for %s\n", bond_dev->name);
4846
Neil Horman9fe06172011-05-25 08:13:01 +00004847 /*
4848 * Initialize locks that may be required during
4849 * en/deslave operations. All of the bond_open work
4850 * (of which this is part) should really be moved to
4851 * a phase prior to dev_open
4852 */
4853 spin_lock_init(&(bond_info->tx_hashtbl_lock));
4854 spin_lock_init(&(bond_info->rx_hashtbl_lock));
4855
Stephen Hemminger181470f2009-06-12 19:02:52 +00004856 bond->wq = create_singlethread_workqueue(bond_dev->name);
4857 if (!bond->wq)
4858 return -ENOMEM;
4859
4860 bond_set_lockdep_class(bond_dev);
4861
Stephen Hemminger181470f2009-06-12 19:02:52 +00004862 bond_create_proc_entry(bond);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004863 list_add_tail(&bond->bond_list, &bn->dev_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00004864
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00004865 bond_prepare_sysfs_group(bond);
Jiri Pirko22bedad32010-04-01 21:22:57 +00004866
Taku Izumif073c7c2010-12-09 15:17:13 +00004867 bond_debug_register(bond);
4868
Jiri Pirko22bedad32010-04-01 21:22:57 +00004869 __hw_addr_init(&bond->mc_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00004870 return 0;
4871}
4872
Eric W. Biederman88ead972009-10-29 14:18:25 +00004873static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
4874{
4875 if (tb[IFLA_ADDRESS]) {
4876 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
4877 return -EINVAL;
4878 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
4879 return -EADDRNOTAVAIL;
4880 }
4881 return 0;
4882}
4883
4884static struct rtnl_link_ops bond_link_ops __read_mostly = {
4885 .kind = "bond",
Eric W. Biederman66391042009-10-29 23:58:54 +00004886 .priv_size = sizeof(struct bonding),
Eric W. Biederman88ead972009-10-29 14:18:25 +00004887 .setup = bond_setup,
4888 .validate = bond_validate,
4889};
4890
Mitch Williamsdfe60392005-11-09 10:36:04 -08004891/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004892 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08004893 * Caller must NOT hold rtnl_lock; we need to release it here before we
4894 * set up our sysfs entries.
4895 */
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004896int bond_create(struct net *net, const char *name)
Mitch Williamsdfe60392005-11-09 10:36:04 -08004897{
4898 struct net_device *bond_dev;
4899 int res;
4900
4901 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08004902
Jiri Pirko1c5cae82011-04-30 01:21:32 +00004903 bond_dev = alloc_netdev_mq(sizeof(struct bonding),
4904 name ? name : "bond%d",
4905 bond_setup, tx_queues);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004906 if (!bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004907 pr_err("%s: eek! can't alloc netdev!\n", name);
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004908 rtnl_unlock();
4909 return -ENOMEM;
Mitch Williamsdfe60392005-11-09 10:36:04 -08004910 }
4911
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004912 dev_net_set(bond_dev, net);
Eric W. Biederman88ead972009-10-29 14:18:25 +00004913 bond_dev->rtnl_link_ops = &bond_link_ops;
4914
Mitch Williamsdfe60392005-11-09 10:36:04 -08004915 res = register_netdevice(bond_dev);
Peter Zijlstra0daa23032006-11-08 19:51:01 -08004916
Phil Oestere826eaf2011-03-14 06:22:05 +00004917 netif_carrier_off(bond_dev);
4918
Mitch Williamsdfe60392005-11-09 10:36:04 -08004919 rtnl_unlock();
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004920 if (res < 0)
4921 bond_destructor(bond_dev);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004922 return res;
4923}
4924
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004925static int __net_init bond_net_init(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004926{
Eric W. Biederman15449742009-11-29 15:46:04 +00004927 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004928
4929 bn->net = net;
4930 INIT_LIST_HEAD(&bn->dev_list);
4931
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004932 bond_create_proc_dir(bn);
Eric W. Biederman15449742009-11-29 15:46:04 +00004933
4934 return 0;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004935}
4936
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004937static void __net_exit bond_net_exit(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004938{
Eric W. Biederman15449742009-11-29 15:46:04 +00004939 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004940
4941 bond_destroy_proc_dir(bn);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004942}
4943
4944static struct pernet_operations bond_net_ops = {
4945 .init = bond_net_init,
4946 .exit = bond_net_exit,
Eric W. Biederman15449742009-11-29 15:46:04 +00004947 .id = &bond_net_id,
4948 .size = sizeof(struct bond_net),
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004949};
4950
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951static int __init bonding_init(void)
4952{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 int i;
4954 int res;
4955
Amerigo Wangbd33acc2011-03-06 21:58:46 +00004956 pr_info("%s", bond_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
Mitch Williamsdfe60392005-11-09 10:36:04 -08004958 res = bond_check_params(&bonding_defaults);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004959 if (res)
Mitch Williamsdfe60392005-11-09 10:36:04 -08004960 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961
Eric W. Biederman15449742009-11-29 15:46:04 +00004962 res = register_pernet_subsys(&bond_net_ops);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004963 if (res)
4964 goto out;
Jay Vosburgh027ea042008-01-17 16:25:02 -08004965
Eric W. Biederman88ead972009-10-29 14:18:25 +00004966 res = rtnl_link_register(&bond_link_ops);
4967 if (res)
Eric W. Biederman66391042009-10-29 23:58:54 +00004968 goto err_link;
Eric W. Biederman88ead972009-10-29 14:18:25 +00004969
Taku Izumif073c7c2010-12-09 15:17:13 +00004970 bond_create_debugfs();
4971
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 for (i = 0; i < max_bonds; i++) {
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004973 res = bond_create(&init_net, NULL);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004974 if (res)
4975 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 }
4977
Mitch Williamsb76cdba2005-11-09 10:36:41 -08004978 res = bond_create_sysfs();
4979 if (res)
4980 goto err;
4981
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04004983 register_inetaddr_notifier(&bond_inetaddr_notifier);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004984out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 return res;
Eric W. Biederman88ead972009-10-29 14:18:25 +00004986err:
4987 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman66391042009-10-29 23:58:54 +00004988err_link:
Eric W. Biederman15449742009-11-29 15:46:04 +00004989 unregister_pernet_subsys(&bond_net_ops);
Eric W. Biederman88ead972009-10-29 14:18:25 +00004990 goto out;
Mitch Williamsdfe60392005-11-09 10:36:04 -08004991
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992}
4993
4994static void __exit bonding_exit(void)
4995{
4996 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04004997 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998
Pavel Emelyanovae68c392008-05-02 17:49:39 -07004999 bond_destroy_sysfs();
Taku Izumif073c7c2010-12-09 15:17:13 +00005000 bond_destroy_debugfs();
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005001
Eric W. Biederman88ead972009-10-29 14:18:25 +00005002 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman15449742009-11-29 15:46:04 +00005003 unregister_pernet_subsys(&bond_net_ops);
Neil Hormane843fa52010-10-13 16:01:50 +00005004
5005#ifdef CONFIG_NET_POLL_CONTROLLER
Neil Hormanfb4fa762010-12-06 09:05:50 +00005006 /*
5007 * Make sure we don't have an imbalance on our netpoll blocking
5008 */
5009 WARN_ON(atomic_read(&netpoll_block_tx));
Neil Hormane843fa52010-10-13 16:01:50 +00005010#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011}
5012
5013module_init(bonding_init);
5014module_exit(bonding_exit);
5015MODULE_LICENSE("GPL");
5016MODULE_VERSION(DRV_VERSION);
5017MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5018MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
Eric W. Biederman88ead972009-10-29 14:18:25 +00005019MODULE_ALIAS_RTNL_LINK("bond");