blob: 652b30e525d01d8c4b547465bb66f943929fd4ea [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 Vosburgh3915c1e2008-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 Vosburgh3915c1e2008-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
Neil Horman374eeb52011-06-03 10:35:52 +0000391#define bond_queue_mapping(skb) (*(u16 *)((skb)->cb))
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/**
394 * bond_dev_queue_xmit - Prepare skb for xmit.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000395 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 * @bond: bond device that got this skb for tx.
397 * @skb: hw accel VLAN tagged skb to transmit
398 * @slave_dev: slave that is supposed to xmit this skbuff
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000400int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
401 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Ben Hutchings83874512010-12-13 08:19:28 +0000403 skb->dev = slave_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 skb->priority = 1;
Neil Horman374eeb52011-06-03 10:35:52 +0000405
406 skb->queue_mapping = bond_queue_mapping(skb);
407
Amerigo Wang080e4132011-02-17 23:43:33 +0000408 if (unlikely(netpoll_tx_running(slave_dev)))
Amerigo Wang8a8efa22011-02-17 23:43:32 +0000409 bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
Amerigo Wang080e4132011-02-17 23:43:33 +0000410 else
WANG Congf6dc31a2010-05-06 00:48:51 -0700411 dev_queue_xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 return 0;
414}
415
416/*
417 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
418 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
419 * lock because:
420 * a. This operation is performed in IOCTL context,
421 * b. The operation is protected by the RTNL semaphore in the 8021q code,
422 * c. Holding a lock with BH disabled while directly calling a base driver
423 * entry point is generally a BAD idea.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000424 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * The design of synchronization/protection for this operation in the 8021q
426 * module is good for one or more VLAN devices over a single physical device
427 * and cannot be extended for a teaming solution like bonding, so there is a
428 * potential race condition here where a net device from the vlan group might
429 * be referenced (either by a base driver or the 8021q code) while it is being
430 * removed from the system. However, it turns out we're not making matters
431 * worse, and if it works for regular VLAN usage it will work here too.
432*/
433
434/**
435 * bond_vlan_rx_register - Propagates registration to slaves
436 * @bond_dev: bonding net device that got called
437 * @grp: vlan group being registered
438 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000439static void bond_vlan_rx_register(struct net_device *bond_dev,
440 struct vlan_group *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Wang Chen454d7c92008-11-12 23:37:49 -0800442 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 struct slave *slave;
444 int i;
445
Jarek Poplawskia71fb882010-10-27 07:08:22 +0000446 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 bond->vlgrp = grp;
Jarek Poplawskia71fb882010-10-27 07:08:22 +0000448 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 bond_for_each_slave(bond, slave, i) {
451 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800452 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800455 slave_ops->ndo_vlan_rx_register) {
456 slave_ops->ndo_vlan_rx_register(slave_dev, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458 }
459}
460
461/**
462 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
463 * @bond_dev: bonding net device that got called
464 * @vid: vlan id being added
465 */
466static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
467{
Wang Chen454d7c92008-11-12 23:37:49 -0800468 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 struct slave *slave;
470 int i, res;
471
472 bond_for_each_slave(bond, slave, i) {
473 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800474 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800477 slave_ops->ndo_vlan_rx_add_vid) {
478 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480 }
481
482 res = bond_add_vlan(bond, vid);
483 if (res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800484 pr_err("%s: Error: Failed to add vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 bond_dev->name, vid);
486 }
487}
488
489/**
490 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
491 * @bond_dev: bonding net device that got called
492 * @vid: vlan id being removed
493 */
494static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
495{
Wang Chen454d7c92008-11-12 23:37:49 -0800496 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 struct slave *slave;
498 struct net_device *vlan_dev;
499 int i, res;
500
501 bond_for_each_slave(bond, slave, i) {
502 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800503 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800506 slave_ops->ndo_vlan_rx_kill_vid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /* Save and then restore vlan_dev in the grp array,
508 * since the slave's driver might clear it.
509 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800510 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800511 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vid);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800512 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514 }
515
516 res = bond_del_vlan(bond, vid);
517 if (res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800518 pr_err("%s: Error: Failed to remove vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 bond_dev->name, vid);
520 }
521}
522
523static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
524{
525 struct vlan_entry *vlan;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800526 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Jay Vosburghf35188f2010-07-21 12:14:47 +0000528 if (!bond->vlgrp)
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000529 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800532 slave_ops->ndo_vlan_rx_register)
533 slave_ops->ndo_vlan_rx_register(slave_dev, bond->vlgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800536 !(slave_ops->ndo_vlan_rx_add_vid))
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000537 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800539 list_for_each_entry(vlan, &bond->vlan_list, vlan_list)
540 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vlan->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000543static void bond_del_vlans_from_slave(struct bonding *bond,
544 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800546 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 struct vlan_entry *vlan;
548 struct net_device *vlan_dev;
549
Jay Vosburghf35188f2010-07-21 12:14:47 +0000550 if (!bond->vlgrp)
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000551 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800554 !(slave_ops->ndo_vlan_rx_kill_vid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 goto unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +0000558 if (!vlan->vlan_id)
559 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /* Save and then restore vlan_dev in the grp array,
561 * since the slave's driver might clear it.
562 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800563 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800564 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800565 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567
568unreg:
569 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800570 slave_ops->ndo_vlan_rx_register)
571 slave_ops->ndo_vlan_rx_register(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574/*------------------------------- Link status -------------------------------*/
575
576/*
Jay Vosburghff59c452006-03-27 13:27:43 -0800577 * Set the carrier state for the master according to the state of its
578 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
579 * do special 802.3ad magic.
580 *
581 * Returns zero if carrier state does not change, nonzero if it does.
582 */
583static int bond_set_carrier(struct bonding *bond)
584{
585 struct slave *slave;
586 int i;
587
588 if (bond->slave_cnt == 0)
589 goto down;
590
591 if (bond->params.mode == BOND_MODE_8023AD)
592 return bond_3ad_set_carrier(bond);
593
594 bond_for_each_slave(bond, slave, i) {
595 if (slave->link == BOND_LINK_UP) {
596 if (!netif_carrier_ok(bond->dev)) {
597 netif_carrier_on(bond->dev);
598 return 1;
599 }
600 return 0;
601 }
602 }
603
604down:
605 if (netif_carrier_ok(bond->dev)) {
606 netif_carrier_off(bond->dev);
607 return 1;
608 }
609 return 0;
610}
611
612/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 * Get link speed and duplex from the slave's base driver
614 * using ethtool. If for some reason the call fails or the
615 * values are invalid, fake speed and duplex to 100/Full
616 * and return error.
617 */
618static int bond_update_speed_duplex(struct slave *slave)
619{
620 struct net_device *slave_dev = slave->dev;
David Decotigny5d305302011-04-13 15:22:31 +0000621 struct ethtool_cmd etool = { .cmd = ETHTOOL_GSET };
622 u32 slave_speed;
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700623 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 /* Fake speed and duplex */
626 slave->speed = SPEED_100;
627 slave->duplex = DUPLEX_FULL;
628
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700629 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700632 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
633 if (res < 0)
634 return -1;
635
David Decotigny5d305302011-04-13 15:22:31 +0000636 slave_speed = ethtool_cmd_speed(&etool);
637 switch (slave_speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 case SPEED_10:
639 case SPEED_100:
640 case SPEED_1000:
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700641 case SPEED_10000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 break;
643 default:
644 return -1;
645 }
646
647 switch (etool.duplex) {
648 case DUPLEX_FULL:
649 case DUPLEX_HALF:
650 break;
651 default:
652 return -1;
653 }
654
David Decotigny5d305302011-04-13 15:22:31 +0000655 slave->speed = slave_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 slave->duplex = etool.duplex;
657
658 return 0;
659}
660
661/*
662 * if <dev> supports MII link status reporting, check its link status.
663 *
664 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000665 * depending upon the setting of the use_carrier parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 *
667 * Return either BMSR_LSTATUS, meaning that the link is up (or we
668 * can't tell and just pretend it is), or 0, meaning that the link is
669 * down.
670 *
671 * If reporting is non-zero, instead of faking link up, return -1 if
672 * both ETHTOOL and MII ioctls fail (meaning the device does not
673 * support them). If use_carrier is set, return whatever it says.
674 * It'd be nice if there was a good way to tell if a driver supports
675 * netif_carrier, but there really isn't.
676 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000677static int bond_check_dev_link(struct bonding *bond,
678 struct net_device *slave_dev, int reporting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800680 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Jiri Bohacd9d52832009-10-28 22:23:54 -0700681 int (*ioctl)(struct net_device *, struct ifreq *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 struct ifreq ifr;
683 struct mii_ioctl_data *mii;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Petri Gynther6c988852009-08-28 12:05:15 +0000685 if (!reporting && !netif_running(slave_dev))
686 return 0;
687
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800688 if (bond->params.use_carrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Jiri Pirko29112f42009-04-24 01:58:23 +0000691 /* Try to get link status using Ethtool first. */
692 if (slave_dev->ethtool_ops) {
693 if (slave_dev->ethtool_ops->get_link) {
694 u32 link;
695
696 link = slave_dev->ethtool_ops->get_link(slave_dev);
697
698 return link ? BMSR_LSTATUS : 0;
699 }
700 }
701
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000702 /* Ethtool can't be used, fallback to MII ioctls. */
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800703 ioctl = slave_ops->ndo_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (ioctl) {
705 /* TODO: set pointer to correct ioctl on a per team member */
706 /* bases to make this more efficient. that is, once */
707 /* we determine the correct ioctl, we will always */
708 /* call it and not the others for that team */
709 /* member. */
710
711 /*
712 * We cannot assume that SIOCGMIIPHY will also read a
713 * register; not all network drivers (e.g., e100)
714 * support that.
715 */
716
717 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
718 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
719 mii = if_mii(&ifr);
720 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
721 mii->reg_num = MII_BMSR;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000722 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0)
723 return mii->val_out & BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725 }
726
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700727 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 * If reporting, report that either there's no dev->do_ioctl,
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700729 * or both SIOCGMIIREG and get_link failed (meaning that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 * cannot report link status). If not reporting, pretend
731 * we're ok.
732 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000733 return reporting ? -1 : BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
736/*----------------------------- Multicast list ------------------------------*/
737
738/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 * Push the promiscuity flag down to appropriate slaves
740 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700741static int bond_set_promiscuity(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700743 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (USES_PRIMARY(bond->params.mode)) {
745 /* write lock already acquired */
746 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700747 err = dev_set_promiscuity(bond->curr_active_slave->dev,
748 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750 } else {
751 struct slave *slave;
752 int i;
753 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700754 err = dev_set_promiscuity(slave->dev, inc);
755 if (err)
756 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700759 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
762/*
763 * Push the allmulti flag down to all slaves
764 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700765static int bond_set_allmulti(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700767 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (USES_PRIMARY(bond->params.mode)) {
769 /* write lock already acquired */
770 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700771 err = dev_set_allmulti(bond->curr_active_slave->dev,
772 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774 } else {
775 struct slave *slave;
776 int i;
777 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700778 err = dev_set_allmulti(slave->dev, inc);
779 if (err)
780 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700783 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
786/*
787 * Add a Multicast address to slaves
788 * according to mode
789 */
Jiri Pirko22bedad2010-04-01 21:22:57 +0000790static void bond_mc_add(struct bonding *bond, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 if (USES_PRIMARY(bond->params.mode)) {
793 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000794 if (bond->curr_active_slave)
Jiri Pirko22bedad2010-04-01 21:22:57 +0000795 dev_mc_add(bond->curr_active_slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 } else {
797 struct slave *slave;
798 int i;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000799
800 bond_for_each_slave(bond, slave, i)
Jiri Pirko22bedad2010-04-01 21:22:57 +0000801 dev_mc_add(slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803}
804
805/*
806 * Remove a multicast address from slave
807 * according to mode
808 */
Jiri Pirko22bedad2010-04-01 21:22:57 +0000809static void bond_mc_del(struct bonding *bond, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 if (USES_PRIMARY(bond->params.mode)) {
812 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000813 if (bond->curr_active_slave)
Jiri Pirko22bedad2010-04-01 21:22:57 +0000814 dev_mc_del(bond->curr_active_slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 } else {
816 struct slave *slave;
817 int i;
818 bond_for_each_slave(bond, slave, i) {
Jiri Pirko22bedad2010-04-01 21:22:57 +0000819 dev_mc_del(slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821 }
822}
823
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800824
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000825static void __bond_resend_igmp_join_requests(struct net_device *dev)
826{
827 struct in_device *in_dev;
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000828
829 rcu_read_lock();
830 in_dev = __in_dev_get_rcu(dev);
Eric Dumazet866f3b22010-11-18 09:33:19 -0800831 if (in_dev)
David S. Miller24912422010-11-19 13:13:47 -0800832 ip_mc_rejoin_groups(in_dev);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000833 rcu_read_unlock();
834}
835
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800836/*
837 * Retrieve the list of registered multicast addresses for the bonding
838 * device and retransmit an IGMP JOIN request to the current active
839 * slave.
840 */
841static void bond_resend_igmp_join_requests(struct bonding *bond)
842{
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000843 struct net_device *vlan_dev;
844 struct vlan_entry *vlan;
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800845
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000846 read_lock(&bond->lock);
847
848 /* rejoin all groups on bond device */
849 __bond_resend_igmp_join_requests(bond->dev);
850
851 /* rejoin all groups on vlan devices */
852 if (bond->vlgrp) {
853 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
854 vlan_dev = vlan_group_get_device(bond->vlgrp,
855 vlan->vlan_id);
856 if (vlan_dev)
857 __bond_resend_igmp_join_requests(vlan_dev);
858 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800859 }
860
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000861 if (--bond->igmp_retrans > 0)
862 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
863
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000864 read_unlock(&bond->lock);
865}
866
stephen hemminger379b7382010-10-15 11:02:56 +0000867static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000868{
869 struct bonding *bond = container_of(work, struct bonding,
Flavio Leitner94265cf2011-05-25 08:38:58 +0000870 mcast_work.work);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000871 bond_resend_igmp_join_requests(bond);
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800872}
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 * flush all members of flush->mc_list from device dev->mc_list
876 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000877static void bond_mc_list_flush(struct net_device *bond_dev,
878 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Wang Chen454d7c92008-11-12 23:37:49 -0800880 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad2010-04-01 21:22:57 +0000881 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Jiri Pirko22bedad2010-04-01 21:22:57 +0000883 netdev_for_each_mc_addr(ha, bond_dev)
884 dev_mc_del(slave_dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 if (bond->params.mode == BOND_MODE_8023AD) {
887 /* del lacpdu mc addr from mc list */
888 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
889
Jiri Pirko22bedad2010-04-01 21:22:57 +0000890 dev_mc_del(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892}
893
894/*--------------------------- Active slave change ---------------------------*/
895
896/*
897 * Update the mc list and multicast-related flags for the new and
898 * old active slaves (if any) according to the multicast mode, and
899 * promiscuous flags unconditionally.
900 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000901static void bond_mc_swap(struct bonding *bond, struct slave *new_active,
902 struct slave *old_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Jiri Pirko22bedad2010-04-01 21:22:57 +0000904 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000906 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 /* nothing to do - mc list is already up-to-date on
908 * all slaves
909 */
910 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 if (old_active) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000913 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 dev_set_promiscuity(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000916 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 dev_set_allmulti(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Jiri Pirko22bedad2010-04-01 21:22:57 +0000919 netdev_for_each_mc_addr(ha, bond->dev)
920 dev_mc_del(old_active->dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922
923 if (new_active) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700924 /* FIXME: Signal errors upstream. */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000925 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 dev_set_promiscuity(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000928 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 dev_set_allmulti(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Jiri Pirko22bedad2010-04-01 21:22:57 +0000931 netdev_for_each_mc_addr(ha, bond->dev)
932 dev_mc_add(new_active->dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934}
935
Jay Vosburgh3915c1e2008-05-17 21:10:14 -0700936/*
937 * bond_do_fail_over_mac
938 *
939 * Perform special MAC address swapping for fail_over_mac settings
940 *
941 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
942 */
943static void bond_do_fail_over_mac(struct bonding *bond,
944 struct slave *new_active,
945 struct slave *old_active)
Hannes Eder1f78d9f2009-02-14 11:15:33 +0000946 __releases(&bond->curr_slave_lock)
947 __releases(&bond->lock)
948 __acquires(&bond->lock)
949 __acquires(&bond->curr_slave_lock)
Jay Vosburgh3915c1e2008-05-17 21:10:14 -0700950{
951 u8 tmp_mac[ETH_ALEN];
952 struct sockaddr saddr;
953 int rv;
954
955 switch (bond->params.fail_over_mac) {
956 case BOND_FOM_ACTIVE:
957 if (new_active)
958 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
959 new_active->dev->addr_len);
960 break;
961 case BOND_FOM_FOLLOW:
962 /*
963 * if new_active && old_active, swap them
964 * if just old_active, do nothing (going to no active slave)
965 * if just new_active, set new_active to bond's MAC
966 */
967 if (!new_active)
968 return;
969
970 write_unlock_bh(&bond->curr_slave_lock);
971 read_unlock(&bond->lock);
972
973 if (old_active) {
974 memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
975 memcpy(saddr.sa_data, old_active->dev->dev_addr,
976 ETH_ALEN);
977 saddr.sa_family = new_active->dev->type;
978 } else {
979 memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
980 saddr.sa_family = bond->dev->type;
981 }
982
983 rv = dev_set_mac_address(new_active->dev, &saddr);
984 if (rv) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800985 pr_err("%s: Error %d setting MAC of slave %s\n",
Jay Vosburgh3915c1e2008-05-17 21:10:14 -0700986 bond->dev->name, -rv, new_active->dev->name);
987 goto out;
988 }
989
990 if (!old_active)
991 goto out;
992
993 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
994 saddr.sa_family = old_active->dev->type;
995
996 rv = dev_set_mac_address(old_active->dev, &saddr);
997 if (rv)
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800998 pr_err("%s: Error %d setting MAC of slave %s\n",
Jay Vosburgh3915c1e2008-05-17 21:10:14 -0700999 bond->dev->name, -rv, new_active->dev->name);
1000out:
1001 read_lock(&bond->lock);
1002 write_lock_bh(&bond->curr_slave_lock);
1003 break;
1004 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001005 pr_err("%s: bond_do_fail_over_mac impossible: bad policy %d\n",
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001006 bond->dev->name, bond->params.fail_over_mac);
1007 break;
1008 }
1009
1010}
1011
Jiri Pirkoa5499522009-09-25 03:28:09 +00001012static bool bond_should_change_active(struct bonding *bond)
1013{
1014 struct slave *prim = bond->primary_slave;
1015 struct slave *curr = bond->curr_active_slave;
1016
1017 if (!prim || !curr || curr->link != BOND_LINK_UP)
1018 return true;
1019 if (bond->force_primary) {
1020 bond->force_primary = false;
1021 return true;
1022 }
1023 if (bond->params.primary_reselect == BOND_PRI_RESELECT_BETTER &&
1024 (prim->speed < curr->speed ||
1025 (prim->speed == curr->speed && prim->duplex <= curr->duplex)))
1026 return false;
1027 if (bond->params.primary_reselect == BOND_PRI_RESELECT_FAILURE)
1028 return false;
1029 return true;
1030}
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032/**
1033 * find_best_interface - select the best available slave to be the active one
1034 * @bond: our bonding struct
1035 *
1036 * Warning: Caller must hold curr_slave_lock for writing.
1037 */
1038static struct slave *bond_find_best_slave(struct bonding *bond)
1039{
1040 struct slave *new_active, *old_active;
1041 struct slave *bestslave = NULL;
1042 int mintime = bond->params.updelay;
1043 int i;
1044
Nicolas de Pesloüan49b4ad92009-10-07 14:11:00 -07001045 new_active = bond->curr_active_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 if (!new_active) { /* there were no active slaves left */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001048 if (bond->slave_cnt > 0) /* found one slave */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 new_active = bond->first_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001050 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return NULL; /* still no slave, return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if ((bond->primary_slave) &&
Jiri Pirkoa5499522009-09-25 03:28:09 +00001055 bond->primary_slave->link == BOND_LINK_UP &&
1056 bond_should_change_active(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 new_active = bond->primary_slave;
1058 }
1059
1060 /* remember where to stop iterating over the slaves */
1061 old_active = new_active;
1062
1063 bond_for_each_slave_from(bond, new_active, i, old_active) {
Jiri Pirkob9f60252009-08-31 11:09:38 +00001064 if (new_active->link == BOND_LINK_UP) {
1065 return new_active;
1066 } else if (new_active->link == BOND_LINK_BACK &&
1067 IS_UP(new_active->dev)) {
1068 /* link up, but waiting for stabilization */
1069 if (new_active->delay < mintime) {
1070 mintime = new_active->delay;
1071 bestslave = new_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073 }
1074 }
1075
1076 return bestslave;
1077}
1078
Ben Hutchingsad246c92011-04-26 15:25:52 +00001079static bool bond_should_notify_peers(struct bonding *bond)
1080{
1081 struct slave *slave = bond->curr_active_slave;
1082
1083 pr_debug("bond_should_notify_peers: bond %s slave %s\n",
1084 bond->dev->name, slave ? slave->dev->name : "NULL");
1085
1086 if (!slave || !bond->send_peer_notif ||
1087 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
1088 return false;
1089
1090 bond->send_peer_notif--;
1091 return true;
1092}
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094/**
1095 * change_active_interface - change the active slave into the specified one
1096 * @bond: our bonding struct
1097 * @new: the new slave to make the active one
1098 *
1099 * Set the new slave to the bond's settings and unset them on the old
1100 * curr_active_slave.
1101 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1102 *
1103 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1104 * because it is apparently the best available slave we have, even though its
1105 * updelay hasn't timed out yet.
1106 *
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001107 * If new_active is not NULL, caller must hold bond->lock for read and
1108 * curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001110void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 struct slave *old_active = bond->curr_active_slave;
1113
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001114 if (old_active == new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 if (new_active) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07001118 new_active->jiffies = jiffies;
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (new_active->link == BOND_LINK_BACK) {
1121 if (USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001122 pr_info("%s: making interface %s the new active one %d ms earlier.\n",
1123 bond->dev->name, new_active->dev->name,
1124 (bond->params.updelay - new_active->delay) * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126
1127 new_active->delay = 0;
1128 new_active->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001130 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Holger Eitzenberger58402052008-12-09 23:07:13 -08001133 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 } else {
1136 if (USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001137 pr_info("%s: making interface %s the new active one.\n",
1138 bond->dev->name, new_active->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140 }
1141 }
1142
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001143 if (USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 bond_mc_swap(bond, new_active, old_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Holger Eitzenberger58402052008-12-09 23:07:13 -08001146 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 bond_alb_handle_active_change(bond, new_active);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001148 if (old_active)
1149 bond_set_slave_inactive_flags(old_active);
1150 if (new_active)
1151 bond_set_slave_active_flags(new_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 } else {
1153 bond->curr_active_slave = new_active;
1154 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001155
1156 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001157 if (old_active)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001158 bond_set_slave_inactive_flags(old_active);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001159
1160 if (new_active) {
Ben Hutchingsad246c92011-04-26 15:25:52 +00001161 bool should_notify_peers = false;
1162
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001163 bond_set_slave_active_flags(new_active);
Moni Shoua2ab82852007-10-09 19:43:39 -07001164
Or Gerlitz709f8a42008-06-13 18:12:01 -07001165 if (bond->params.fail_over_mac)
1166 bond_do_fail_over_mac(bond, new_active,
1167 old_active);
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001168
Ben Hutchingsad246c92011-04-26 15:25:52 +00001169 if (netif_running(bond->dev)) {
1170 bond->send_peer_notif =
1171 bond->params.num_peer_notif;
1172 should_notify_peers =
1173 bond_should_notify_peers(bond);
1174 }
1175
Or Gerlitz01f31092008-06-13 18:12:02 -07001176 write_unlock_bh(&bond->curr_slave_lock);
1177 read_unlock(&bond->lock);
1178
Moni Shoua75c78502009-09-15 02:37:40 -07001179 netdev_bonding_change(bond->dev, NETDEV_BONDING_FAILOVER);
Ben Hutchingsad246c92011-04-26 15:25:52 +00001180 if (should_notify_peers)
1181 netdev_bonding_change(bond->dev,
1182 NETDEV_NOTIFY_PEERS);
Or Gerlitz01f31092008-06-13 18:12:02 -07001183
1184 read_lock(&bond->lock);
1185 write_lock_bh(&bond->curr_slave_lock);
Moni Shoua7893b242008-05-17 21:10:12 -07001186 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001187 }
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001188
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001189 /* resend IGMP joins since active slave has changed or
Flavio Leitner94265cf2011-05-25 08:38:58 +00001190 * all were sent on curr_active_slave.
1191 * resend only if bond is brought up with the affected
1192 * bonding modes and the retransmission is enabled */
1193 if (netif_running(bond->dev) && (bond->params.resend_igmp > 0) &&
1194 ((USES_PRIMARY(bond->params.mode) && new_active) ||
1195 bond->params.mode == BOND_MODE_ROUNDROBIN)) {
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001196 bond->igmp_retrans = bond->params.resend_igmp;
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001197 queue_delayed_work(bond->wq, &bond->mcast_work, 0);
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
1201/**
1202 * bond_select_active_slave - select a new active slave, if needed
1203 * @bond: our bonding struct
1204 *
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001205 * This functions should be called when one of the following occurs:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 * - The old curr_active_slave has been released or lost its link.
1207 * - The primary_slave has got its link back.
1208 * - A slave has got its link back and there's no old curr_active_slave.
1209 *
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001210 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001212void bond_select_active_slave(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 struct slave *best_slave;
Jay Vosburghff59c452006-03-27 13:27:43 -08001215 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 best_slave = bond_find_best_slave(bond);
1218 if (best_slave != bond->curr_active_slave) {
1219 bond_change_active_slave(bond, best_slave);
Jay Vosburghff59c452006-03-27 13:27:43 -08001220 rv = bond_set_carrier(bond);
1221 if (!rv)
1222 return;
1223
1224 if (netif_carrier_ok(bond->dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001225 pr_info("%s: first active interface up!\n",
1226 bond->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08001227 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001228 pr_info("%s: now running without any active interface !\n",
1229 bond->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08001230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232}
1233
1234/*--------------------------- slave list handling ---------------------------*/
1235
1236/*
1237 * This function attaches the slave to the end of list.
1238 *
1239 * bond->lock held for writing by caller.
1240 */
1241static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1242{
1243 if (bond->first_slave == NULL) { /* attaching the first slave */
1244 new_slave->next = new_slave;
1245 new_slave->prev = new_slave;
1246 bond->first_slave = new_slave;
1247 } else {
1248 new_slave->next = bond->first_slave;
1249 new_slave->prev = bond->first_slave->prev;
1250 new_slave->next->prev = new_slave;
1251 new_slave->prev->next = new_slave;
1252 }
1253
1254 bond->slave_cnt++;
1255}
1256
1257/*
1258 * This function detaches the slave from the list.
1259 * WARNING: no check is made to verify if the slave effectively
1260 * belongs to <bond>.
1261 * Nothing is freed on return, structures are just unchained.
1262 * If any slave pointer in bond was pointing to <slave>,
1263 * it should be changed by the calling function.
1264 *
1265 * bond->lock held for writing by caller.
1266 */
1267static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1268{
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001269 if (slave->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 slave->next->prev = slave->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001272 if (slave->prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 slave->prev->next = slave->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 if (bond->first_slave == slave) { /* slave is the first slave */
1276 if (bond->slave_cnt > 1) { /* there are more slave */
1277 bond->first_slave = slave->next;
1278 } else {
1279 bond->first_slave = NULL; /* slave was the last one */
1280 }
1281 }
1282
1283 slave->next = NULL;
1284 slave->prev = NULL;
1285 bond->slave_cnt--;
1286}
1287
WANG Congf6dc31a2010-05-06 00:48:51 -07001288#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001289static inline int slave_enable_netpoll(struct slave *slave)
WANG Congf6dc31a2010-05-06 00:48:51 -07001290{
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001291 struct netpoll *np;
1292 int err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001293
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001294 np = kzalloc(sizeof(*np), GFP_KERNEL);
1295 err = -ENOMEM;
1296 if (!np)
1297 goto out;
1298
1299 np->dev = slave->dev;
1300 err = __netpoll_setup(np);
1301 if (err) {
1302 kfree(np);
1303 goto out;
WANG Congf6dc31a2010-05-06 00:48:51 -07001304 }
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001305 slave->np = np;
1306out:
1307 return err;
1308}
1309static inline void slave_disable_netpoll(struct slave *slave)
1310{
1311 struct netpoll *np = slave->np;
1312
1313 if (!np)
1314 return;
1315
1316 slave->np = NULL;
1317 synchronize_rcu_bh();
1318 __netpoll_cleanup(np);
1319 kfree(np);
1320}
1321static inline bool slave_dev_support_netpoll(struct net_device *slave_dev)
1322{
1323 if (slave_dev->priv_flags & IFF_DISABLE_NETPOLL)
1324 return false;
1325 if (!slave_dev->netdev_ops->ndo_poll_controller)
1326 return false;
1327 return true;
WANG Congf6dc31a2010-05-06 00:48:51 -07001328}
1329
1330static void bond_poll_controller(struct net_device *bond_dev)
1331{
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001332}
1333
1334static void __bond_netpoll_cleanup(struct bonding *bond)
1335{
Neil Hormanc2355e12010-10-13 16:01:49 +00001336 struct slave *slave;
1337 int i;
1338
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001339 bond_for_each_slave(bond, slave, i)
1340 if (IS_UP(slave->dev))
1341 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07001342}
WANG Congf6dc31a2010-05-06 00:48:51 -07001343static void bond_netpoll_cleanup(struct net_device *bond_dev)
1344{
1345 struct bonding *bond = netdev_priv(bond_dev);
WANG Congf6dc31a2010-05-06 00:48:51 -07001346
1347 read_lock(&bond->lock);
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001348 __bond_netpoll_cleanup(bond);
WANG Congf6dc31a2010-05-06 00:48:51 -07001349 read_unlock(&bond->lock);
1350}
1351
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001352static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
1353{
1354 struct bonding *bond = netdev_priv(dev);
1355 struct slave *slave;
1356 int i, err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001357
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001358 read_lock(&bond->lock);
1359 bond_for_each_slave(bond, slave, i) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001360 err = slave_enable_netpoll(slave);
1361 if (err) {
1362 __bond_netpoll_cleanup(bond);
1363 break;
1364 }
1365 }
1366 read_unlock(&bond->lock);
1367 return err;
1368}
1369
1370static struct netpoll_info *bond_netpoll_info(struct bonding *bond)
1371{
1372 return bond->dev->npinfo;
1373}
1374
1375#else
1376static inline int slave_enable_netpoll(struct slave *slave)
1377{
1378 return 0;
1379}
1380static inline void slave_disable_netpoll(struct slave *slave)
1381{
1382}
WANG Congf6dc31a2010-05-06 00:48:51 -07001383static void bond_netpoll_cleanup(struct net_device *bond_dev)
1384{
1385}
WANG Congf6dc31a2010-05-06 00:48:51 -07001386#endif
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388/*---------------------------------- IOCTL ----------------------------------*/
1389
Adrian Bunk4ad072c2007-07-09 11:51:12 -07001390static int bond_sethwaddr(struct net_device *bond_dev,
1391 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001393 pr_debug("bond_dev=%p\n", bond_dev);
1394 pr_debug("slave_dev=%p\n", slave_dev);
1395 pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1397 return 0;
1398}
1399
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001400static u32 bond_fix_features(struct net_device *dev, u32 features)
1401{
1402 struct slave *slave;
1403 struct bonding *bond = netdev_priv(dev);
1404 u32 mask;
1405 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001406
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001407 read_lock(&bond->lock);
1408
1409 if (!bond->first_slave) {
1410 /* Disable adding VLANs to empty bond. But why? --mq */
1411 features |= NETIF_F_VLAN_CHALLENGED;
1412 goto out;
1413 }
1414
1415 mask = features;
1416 features &= ~NETIF_F_ONE_FOR_ALL;
1417 features |= NETIF_F_ALL_FOR_ALL;
1418
1419 bond_for_each_slave(bond, slave, i) {
1420 features = netdev_increment_features(features,
1421 slave->dev->features,
1422 mask);
1423 }
1424
1425out:
1426 read_unlock(&bond->lock);
1427 return features;
1428}
1429
1430#define BOND_VLAN_FEATURES (NETIF_F_ALL_TX_OFFLOADS | \
1431 NETIF_F_SOFT_FEATURES | \
1432 NETIF_F_LRO)
1433
1434static void bond_compute_features(struct bonding *bond)
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001435{
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001436 struct slave *slave;
1437 struct net_device *bond_dev = bond->dev;
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001438 u32 vlan_features = BOND_VLAN_FEATURES;
1439 unsigned short max_hard_header_len = ETH_HLEN;
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001440 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001441
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001442 read_lock(&bond->lock);
Herbert Xub63365a2008-10-23 01:11:29 -07001443
1444 if (!bond->first_slave)
1445 goto done;
1446
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001447 bond_for_each_slave(bond, slave, i) {
Jay Vosburgh278339a2009-08-28 12:05:12 +00001448 vlan_features = netdev_increment_features(vlan_features,
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001449 slave->dev->vlan_features, BOND_VLAN_FEATURES);
1450
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001451 if (slave->dev->hard_header_len > max_hard_header_len)
1452 max_hard_header_len = slave->dev->hard_header_len;
1453 }
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001454
Herbert Xub63365a2008-10-23 01:11:29 -07001455done:
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001456 bond_dev->vlan_features = vlan_features;
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001457 bond_dev->hard_header_len = max_hard_header_len;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001458
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001459 read_unlock(&bond->lock);
1460
1461 netdev_change_features(bond_dev);
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001462}
1463
Moni Shoua872254d2007-10-09 19:43:38 -07001464static void bond_setup_by_slave(struct net_device *bond_dev,
1465 struct net_device *slave_dev)
1466{
Wang Chen454d7c92008-11-12 23:37:49 -08001467 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07001468
Stephen Hemminger00829822008-11-20 20:14:53 -08001469 bond_dev->header_ops = slave_dev->header_ops;
Moni Shoua872254d2007-10-09 19:43:38 -07001470
1471 bond_dev->type = slave_dev->type;
1472 bond_dev->hard_header_len = slave_dev->hard_header_len;
1473 bond_dev->addr_len = slave_dev->addr_len;
1474
1475 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1476 slave_dev->addr_len);
Moni Shouad90a1622007-10-09 19:43:43 -07001477 bond->setup_by_slave = 1;
Moni Shoua872254d2007-10-09 19:43:38 -07001478}
1479
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001480/* On bonding slaves other than the currently active slave, suppress
Jiri Pirko3aba8912011-04-19 03:48:16 +00001481 * duplicates except for alb non-mcast/bcast.
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001482 */
1483static bool bond_should_deliver_exact_match(struct sk_buff *skb,
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001484 struct slave *slave,
1485 struct bonding *bond)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001486{
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001487 if (bond_is_slave_inactive(slave)) {
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001488 if (bond->params.mode == BOND_MODE_ALB &&
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001489 skb->pkt_type != PACKET_BROADCAST &&
1490 skb->pkt_type != PACKET_MULTICAST)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001491 return false;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001492 return true;
1493 }
1494 return false;
1495}
1496
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001497static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001498{
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001499 struct sk_buff *skb = *pskb;
Jiri Pirkof1c17752011-03-12 03:14:35 +00001500 struct slave *slave;
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001501 struct bonding *bond;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001502
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001503 skb = skb_share_check(skb, GFP_ATOMIC);
1504 if (unlikely(!skb))
1505 return RX_HANDLER_CONSUMED;
1506
1507 *pskb = skb;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001508
Jiri Pirko35d48902011-03-22 02:38:12 +00001509 slave = bond_slave_get_rcu(skb->dev);
1510 bond = slave->bond;
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001511
1512 if (bond->params.arp_interval)
Jiri Pirkof1c17752011-03-12 03:14:35 +00001513 slave->dev->last_rx = jiffies;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001514
Jiri Pirko3aba8912011-04-19 03:48:16 +00001515 if (bond->recv_probe) {
1516 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
1517
1518 if (likely(nskb)) {
1519 bond->recv_probe(nskb, bond, slave);
1520 dev_kfree_skb(nskb);
1521 }
1522 }
1523
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001524 if (bond_should_deliver_exact_match(skb, slave, bond)) {
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001525 return RX_HANDLER_EXACT;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001526 }
1527
Jiri Pirko35d48902011-03-22 02:38:12 +00001528 skb->dev = bond->dev;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001529
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001530 if (bond->params.mode == BOND_MODE_ALB &&
Jiri Pirko35d48902011-03-22 02:38:12 +00001531 bond->dev->priv_flags & IFF_BRIDGE_PORT &&
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001532 skb->pkt_type == PACKET_HOST) {
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001533
Changli Gao541ac7c2011-03-02 21:07:14 +00001534 if (unlikely(skb_cow_head(skb,
1535 skb->data - skb_mac_header(skb)))) {
1536 kfree_skb(skb);
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001537 return RX_HANDLER_CONSUMED;
Changli Gao541ac7c2011-03-02 21:07:14 +00001538 }
Jiri Pirko35d48902011-03-22 02:38:12 +00001539 memcpy(eth_hdr(skb)->h_dest, bond->dev->dev_addr, ETH_ALEN);
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001540 }
1541
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001542 return RX_HANDLER_ANOTHER;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001543}
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545/* enslave device <slave> to bond device <master> */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001546int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
Wang Chen454d7c92008-11-12 23:37:49 -08001548 struct bonding *bond = netdev_priv(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001549 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 struct slave *new_slave = NULL;
Jiri Pirko22bedad2010-04-01 21:22:57 +00001551 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 struct sockaddr addr;
1553 int link_reporting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 int res = 0;
1555
nsxfreddy@gmail.com552709d2005-09-21 14:18:04 -05001556 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001557 slave_ops->ndo_do_ioctl == NULL) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001558 pr_warning("%s: Warning: no link monitoring support for %s\n",
1559 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 /* already enslaved */
1563 if (slave_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001564 pr_debug("Error, Device was already enslaved\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return -EBUSY;
1566 }
1567
1568 /* vlan challenged mutual exclusion */
1569 /* no need to lock since we're protected by rtnl_lock */
1570 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001571 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Jay Vosburghf35188f2010-07-21 12:14:47 +00001572 if (bond->vlgrp) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001573 pr_err("%s: Error: cannot enslave VLAN challenged slave %s on VLAN enabled bond %s\n",
1574 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return -EPERM;
1576 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001577 pr_warning("%s: Warning: enslaved VLAN challenged slave %s. Adding VLANs will be blocked as long as %s is part of bond %s\n",
1578 bond_dev->name, slave_dev->name,
1579 slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
1581 } else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001582 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
1584
Jay Vosburgh217df672005-09-26 16:11:50 -07001585 /*
1586 * Old ifenslave binaries are no longer supported. These can
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001587 * be identified with moderate accuracy by the state of the slave:
Jay Vosburgh217df672005-09-26 16:11:50 -07001588 * the current ifenslave will set the interface down prior to
1589 * enslaving it; the old ifenslave will not.
1590 */
1591 if ((slave_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001592 pr_err("%s is up. This may be due to an out of date ifenslave.\n",
Jay Vosburgh217df672005-09-26 16:11:50 -07001593 slave_dev->name);
1594 res = -EPERM;
1595 goto err_undo_flags;
1596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Moni Shoua872254d2007-10-09 19:43:38 -07001598 /* set bonding device ether type by slave - bonding netdevices are
1599 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1600 * there is a need to override some of the type dependent attribs/funcs.
1601 *
1602 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1603 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1604 */
1605 if (bond->slave_cnt == 0) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001606 if (bond_dev->type != slave_dev->type) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001607 pr_debug("%s: change device type from %d to %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001608 bond_dev->name,
1609 bond_dev->type, slave_dev->type);
Moni Shoua75c78502009-09-15 02:37:40 -07001610
Jiri Pirko3ca5b402010-03-10 10:29:35 +00001611 res = netdev_bonding_change(bond_dev,
1612 NETDEV_PRE_TYPE_CHANGE);
1613 res = notifier_to_errno(res);
1614 if (res) {
1615 pr_err("%s: refused to change device type\n",
1616 bond_dev->name);
1617 res = -EBUSY;
1618 goto err_undo_flags;
1619 }
Moni Shoua75c78502009-09-15 02:37:40 -07001620
Jiri Pirko32a806c2010-03-19 04:00:23 +00001621 /* Flush unicast and multicast addresses */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001622 dev_uc_flush(bond_dev);
Jiri Pirko22bedad2010-04-01 21:22:57 +00001623 dev_mc_flush(bond_dev);
Jiri Pirko32a806c2010-03-19 04:00:23 +00001624
Moni Shouae36b9d12009-07-15 04:56:31 +00001625 if (slave_dev->type != ARPHRD_ETHER)
1626 bond_setup_by_slave(bond_dev, slave_dev);
1627 else
1628 ether_setup(bond_dev);
Moni Shoua75c78502009-09-15 02:37:40 -07001629
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001630 netdev_bonding_change(bond_dev,
1631 NETDEV_POST_TYPE_CHANGE);
Moni Shouae36b9d12009-07-15 04:56:31 +00001632 }
Moni Shoua872254d2007-10-09 19:43:38 -07001633 } else if (bond_dev->type != slave_dev->type) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001634 pr_err("%s ether type (%d) is different from other slaves (%d), can not enslave it.\n",
1635 slave_dev->name,
1636 slave_dev->type, bond_dev->type);
1637 res = -EINVAL;
1638 goto err_undo_flags;
Moni Shoua872254d2007-10-09 19:43:38 -07001639 }
1640
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001641 if (slave_ops->ndo_set_mac_address == NULL) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001642 if (bond->slave_cnt == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001643 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.",
1644 bond_dev->name);
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001645 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1646 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001647 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",
1648 bond_dev->name);
Moni Shoua2ab82852007-10-09 19:43:39 -07001649 res = -EOPNOTSUPP;
1650 goto err_undo_flags;
1651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
1653
Amerigo Wang8d8fc292011-05-19 21:39:10 +00001654 call_netdevice_notifiers(NETDEV_JOIN, slave_dev);
1655
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001656 /* If this is the first slave, then we need to set the master's hardware
1657 * address to be the same as the slave's. */
David Strandd13a2cb2010-12-01 11:43:08 -08001658 if (is_zero_ether_addr(bond->dev->dev_addr))
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001659 memcpy(bond->dev->dev_addr, slave_dev->dev_addr,
1660 slave_dev->addr_len);
1661
1662
Joe Jin243cb4e2007-02-06 14:16:40 -08001663 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (!new_slave) {
1665 res = -ENOMEM;
1666 goto err_undo_flags;
1667 }
1668
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001669 /*
1670 * Set the new_slave's queue_id to be zero. Queue ID mapping
1671 * is set via sysfs or module option if desired.
1672 */
1673 new_slave->queue_id = 0;
1674
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001675 /* Save slave's original mtu and then set it to match the bond */
1676 new_slave->original_mtu = slave_dev->mtu;
1677 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1678 if (res) {
1679 pr_debug("Error %d calling dev_set_mtu\n", res);
1680 goto err_free;
1681 }
1682
Jay Vosburgh217df672005-09-26 16:11:50 -07001683 /*
1684 * Save slave's original ("permanent") mac address for modes
1685 * that need it, and for restoring it upon release, and then
1686 * set it to the master's address
1687 */
1688 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Jay Vosburghdd957c52007-10-09 19:57:24 -07001690 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001691 /*
1692 * Set slave to master's mac address. The application already
1693 * set the master's mac address to that of the first slave
1694 */
1695 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1696 addr.sa_family = slave_dev->type;
1697 res = dev_set_mac_address(slave_dev, &addr);
1698 if (res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001699 pr_debug("Error %d calling set_mac_address\n", res);
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001700 goto err_restore_mtu;
Moni Shoua2ab82852007-10-09 19:43:39 -07001701 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Jiri Pirko1765a572011-02-12 06:48:36 +00001704 res = netdev_set_bond_master(slave_dev, bond_dev);
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001705 if (res) {
Jiri Pirko1765a572011-02-12 06:48:36 +00001706 pr_debug("Error %d calling netdev_set_bond_master\n", res);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001707 goto err_restore_mac;
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001708 }
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001709
Jay Vosburgh217df672005-09-26 16:11:50 -07001710 /* open the slave since the application closed it */
1711 res = dev_open(slave_dev);
1712 if (res) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001713 pr_debug("Opening slave %s failed\n", slave_dev->name);
Jiri Pirko35d48902011-03-22 02:38:12 +00001714 goto err_unset_master;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716
Jiri Pirko35d48902011-03-22 02:38:12 +00001717 new_slave->bond = bond;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 new_slave->dev = slave_dev;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001719 slave_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Holger Eitzenberger58402052008-12-09 23:07:13 -08001721 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 /* bond_alb_init_slave() must be called before all other stages since
1723 * it might fail and we do not want to have to undo everything
1724 */
1725 res = bond_alb_init_slave(bond, new_slave);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001726 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001727 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729
1730 /* If the mode USES_PRIMARY, then the new slave gets the
1731 * master's promisc (and mc) settings only if it becomes the
1732 * curr_active_slave, and that is taken care of later when calling
1733 * bond_change_active()
1734 */
1735 if (!USES_PRIMARY(bond->params.mode)) {
1736 /* set promiscuity level to new slave */
1737 if (bond_dev->flags & IFF_PROMISC) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001738 res = dev_set_promiscuity(slave_dev, 1);
1739 if (res)
1740 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
1742
1743 /* set allmulti level to new slave */
1744 if (bond_dev->flags & IFF_ALLMULTI) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001745 res = dev_set_allmulti(slave_dev, 1);
1746 if (res)
1747 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
1749
David S. Millerb9e40852008-07-15 00:15:08 -07001750 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /* upload master's mc_list to new slave */
Jiri Pirko22bedad2010-04-01 21:22:57 +00001752 netdev_for_each_mc_addr(ha, bond_dev)
1753 dev_mc_add(slave_dev, ha->addr);
David S. Millerb9e40852008-07-15 00:15:08 -07001754 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756
1757 if (bond->params.mode == BOND_MODE_8023AD) {
1758 /* add lacpdu mc addr to mc list */
1759 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1760
Jiri Pirko22bedad2010-04-01 21:22:57 +00001761 dev_mc_add(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
1763
1764 bond_add_vlans_on_slave(bond, slave_dev);
1765
1766 write_lock_bh(&bond->lock);
1767
1768 bond_attach_slave(bond, new_slave);
1769
1770 new_slave->delay = 0;
1771 new_slave->link_failure_count = 0;
1772
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001773 write_unlock_bh(&bond->lock);
1774
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001775 bond_compute_features(bond);
1776
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001777 read_lock(&bond->lock);
1778
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001779 new_slave->last_arp_rx = jiffies;
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 if (bond->params.miimon && !bond->params.use_carrier) {
1782 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1783
1784 if ((link_reporting == -1) && !bond->params.arp_interval) {
1785 /*
1786 * miimon is set but a bonded network driver
1787 * does not support ETHTOOL/MII and
1788 * arp_interval is not set. Note: if
1789 * use_carrier is enabled, we will never go
1790 * here (because netif_carrier is always
1791 * supported); thus, we don't need to change
1792 * the messages for netif_carrier.
1793 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001794 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 -08001795 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 } else if (link_reporting == -1) {
1797 /* unable get link status using mii/ethtool */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001798 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",
1799 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
1801 }
1802
1803 /* check for initial state */
1804 if (!bond->params.miimon ||
1805 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1806 if (bond->params.updelay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001807 pr_debug("Initial state of slave_dev is BOND_LINK_BACK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 new_slave->link = BOND_LINK_BACK;
1809 new_slave->delay = bond->params.updelay;
1810 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001811 pr_debug("Initial state of slave_dev is BOND_LINK_UP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 new_slave->link = BOND_LINK_UP;
1813 }
1814 new_slave->jiffies = jiffies;
1815 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001816 pr_debug("Initial state of slave_dev is BOND_LINK_DOWN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 new_slave->link = BOND_LINK_DOWN;
1818 }
1819
1820 if (bond_update_speed_duplex(new_slave) &&
1821 (new_slave->link != BOND_LINK_DOWN)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001822 pr_warning("%s: Warning: failed to get speed and duplex from %s, assumed to be 100Mb/sec and Full.\n",
1823 bond_dev->name, new_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 if (bond->params.mode == BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001826 pr_warning("%s: Warning: Operation of 802.3ad mode requires ETHTOOL support in base driver for proper aggregator selection.\n",
1827 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
1829 }
1830
1831 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1832 /* if there is a primary slave, remember it */
Jiri Pirkoa5499522009-09-25 03:28:09 +00001833 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 bond->primary_slave = new_slave;
Jiri Pirkoa5499522009-09-25 03:28:09 +00001835 bond->force_primary = true;
1836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
1838
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001839 write_lock_bh(&bond->curr_slave_lock);
1840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 switch (bond->params.mode) {
1842 case BOND_MODE_ACTIVEBACKUP:
Jay Vosburgh8a8e4472006-09-22 21:56:15 -07001843 bond_set_slave_inactive_flags(new_slave);
1844 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 break;
1846 case BOND_MODE_8023AD:
1847 /* in 802.3ad mode, the internal mechanism
1848 * will activate the slaves in the selected
1849 * aggregator
1850 */
1851 bond_set_slave_inactive_flags(new_slave);
1852 /* if this is the first slave */
1853 if (bond->slave_cnt == 1) {
1854 SLAVE_AD_INFO(new_slave).id = 1;
1855 /* Initialize AD with the number of times that the AD timer is called in 1 second
1856 * can be called only after the mac address of the bond is set
1857 */
1858 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1859 bond->params.lacp_fast);
1860 } else {
1861 SLAVE_AD_INFO(new_slave).id =
1862 SLAVE_AD_INFO(new_slave->prev).id + 1;
1863 }
1864
1865 bond_3ad_bind_slave(new_slave);
1866 break;
1867 case BOND_MODE_TLB:
1868 case BOND_MODE_ALB:
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001869 bond_set_active_slave(new_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001870 bond_set_slave_inactive_flags(new_slave);
Jiri Pirko5a29f782009-03-25 17:23:38 -07001871 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 break;
1873 default:
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001874 pr_debug("This slave is always active in trunk mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 /* always active in trunk mode */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001877 bond_set_active_slave(new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879 /* In trunking mode there is little meaning to curr_active_slave
1880 * anyway (it holds no special properties of the bond device),
1881 * so we can change it without calling change_active_interface()
1882 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001883 if (!bond->curr_active_slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 bond->curr_active_slave = new_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 break;
1887 } /* switch(bond_mode) */
1888
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001889 write_unlock_bh(&bond->curr_slave_lock);
1890
Jay Vosburghff59c452006-03-27 13:27:43 -08001891 bond_set_carrier(bond);
1892
WANG Congf6dc31a2010-05-06 00:48:51 -07001893#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001894 slave_dev->npinfo = bond_netpoll_info(bond);
1895 if (slave_dev->npinfo) {
1896 if (slave_enable_netpoll(new_slave)) {
1897 read_unlock(&bond->lock);
1898 pr_info("Error, %s: master_dev is using netpoll, "
1899 "but new slave device does not support netpoll.\n",
1900 bond_dev->name);
1901 res = -EBUSY;
1902 goto err_close;
1903 }
WANG Congf6dc31a2010-05-06 00:48:51 -07001904 }
1905#endif
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001906
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001907 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001909 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1910 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001911 goto err_close;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001912
Jiri Pirko35d48902011-03-22 02:38:12 +00001913 res = netdev_rx_handler_register(slave_dev, bond_handle_frame,
1914 new_slave);
1915 if (res) {
1916 pr_debug("Error %d calling netdev_rx_handler_register\n", res);
1917 goto err_dest_symlinks;
1918 }
1919
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001920 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
1921 bond_dev->name, slave_dev->name,
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001922 bond_is_active_slave(new_slave) ? "n active" : " backup",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001923 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 /* enslave is successful */
1926 return 0;
1927
1928/* Undo stages on error */
Jiri Pirko35d48902011-03-22 02:38:12 +00001929err_dest_symlinks:
1930 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932err_close:
1933 dev_close(slave_dev);
1934
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001935err_unset_master:
Jiri Pirko1765a572011-02-12 06:48:36 +00001936 netdev_set_bond_master(slave_dev, NULL);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938err_restore_mac:
Jay Vosburghdd957c52007-10-09 19:57:24 -07001939 if (!bond->params.fail_over_mac) {
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001940 /* XXX TODO - fom follow mode needs to change master's
1941 * MAC if this slave's MAC is in use by the bond, or at
1942 * least print a warning.
1943 */
Moni Shoua2ab82852007-10-09 19:43:39 -07001944 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1945 addr.sa_family = slave_dev->type;
1946 dev_set_mac_address(slave_dev, &addr);
1947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001949err_restore_mtu:
1950 dev_set_mtu(slave_dev, new_slave->original_mtu);
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952err_free:
1953 kfree(new_slave);
1954
1955err_undo_flags:
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001956 bond_compute_features(bond);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return res;
1959}
1960
1961/*
1962 * Try to release the slave device <slave> from the bond device <master>
1963 * It is legal to access curr_active_slave without a lock because all the function
1964 * is write-locked.
1965 *
1966 * The rules for slave state should be:
1967 * for Active/Backup:
1968 * Active stays on all backups go down
1969 * for Bonded connections:
1970 * The first up interface should be left on and all others downed.
1971 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001972int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Wang Chen454d7c92008-11-12 23:37:49 -08001974 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 struct slave *slave, *oldcurrent;
1976 struct sockaddr addr;
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001977 u32 old_features = bond_dev->features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 /* slave is not a slave or master is not master of this slave */
1980 if (!(slave_dev->flags & IFF_SLAVE) ||
1981 (slave_dev->master != bond_dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001982 pr_err("%s: Error: cannot release %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 bond_dev->name, slave_dev->name);
1984 return -EINVAL;
1985 }
1986
Neil Hormane843fa52010-10-13 16:01:50 +00001987 block_netpoll_tx();
Amerigo Wangdaf92092011-05-19 21:39:12 +00001988 netdev_bonding_change(bond_dev, NETDEV_RELEASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 write_lock_bh(&bond->lock);
1990
1991 slave = bond_get_slave_by_dev(bond, slave_dev);
1992 if (!slave) {
1993 /* not a slave of this bond */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001994 pr_info("%s: %s not enslaved\n",
1995 bond_dev->name, slave_dev->name);
Jay Vosburghf5e2a7b2006-02-07 21:17:22 -08001996 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001997 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 return -EINVAL;
1999 }
2000
Jiri Pirko35d48902011-03-22 02:38:12 +00002001 /* unregister rx_handler early so bond_handle_frame wouldn't be called
2002 * for this slave anymore.
2003 */
2004 netdev_rx_handler_unregister(slave_dev);
2005 write_unlock_bh(&bond->lock);
2006 synchronize_net();
2007 write_lock_bh(&bond->lock);
2008
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07002009 if (!bond->params.fail_over_mac) {
Joe Perches8e95a202009-12-03 07:58:21 +00002010 if (!compare_ether_addr(bond_dev->dev_addr, slave->perm_hwaddr) &&
2011 bond->slave_cnt > 1)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002012 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",
2013 bond_dev->name, slave_dev->name,
2014 slave->perm_hwaddr,
2015 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
2017
2018 /* Inform AD package of unbinding of slave. */
2019 if (bond->params.mode == BOND_MODE_8023AD) {
2020 /* must be called before the slave is
2021 * detached from the list
2022 */
2023 bond_3ad_unbind_slave(slave);
2024 }
2025
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002026 pr_info("%s: releasing %s interface %s\n",
2027 bond_dev->name,
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002028 bond_is_active_slave(slave) ? "active" : "backup",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002029 slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 oldcurrent = bond->curr_active_slave;
2032
2033 bond->current_arp_slave = NULL;
2034
2035 /* release the slave from its bond */
2036 bond_detach_slave(bond, slave);
2037
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002038 if (bond->primary_slave == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 bond->primary_slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002041 if (oldcurrent == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 bond_change_active_slave(bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Holger Eitzenberger58402052008-12-09 23:07:13 -08002044 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 /* Must be called only after the slave has been
2046 * detached from the list and the curr_active_slave
2047 * has been cleared (if our_slave == old_current),
2048 * but before a new active slave is selected.
2049 */
Jay Vosburgh25433312008-01-17 16:24:59 -08002050 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 bond_alb_deinit_slave(bond, slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08002052 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
2054
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002055 if (oldcurrent == slave) {
2056 /*
2057 * Note that we hold RTNL over this sequence, so there
2058 * is no concern that another slave add/remove event
2059 * will interfere.
2060 */
2061 write_unlock_bh(&bond->lock);
2062 read_lock(&bond->lock);
2063 write_lock_bh(&bond->curr_slave_lock);
2064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 bond_select_active_slave(bond);
2066
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002067 write_unlock_bh(&bond->curr_slave_lock);
2068 read_unlock(&bond->lock);
2069 write_lock_bh(&bond->lock);
2070 }
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 if (bond->slave_cnt == 0) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002073 bond_set_carrier(bond);
2074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 /* if the last slave was removed, zero the mac address
2076 * of the master so it will be set by the application
2077 * to the mac address of the first slave
2078 */
2079 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2080
Michał Mirosławb2a103e2011-05-07 03:22:17 +00002081 if (bond->vlgrp) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002082 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2083 bond_dev->name, bond_dev->name);
2084 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2085 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
2088
2089 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002090 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Michał Mirosławb2a103e2011-05-07 03:22:17 +00002092 bond_compute_features(bond);
2093 if (!(bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2094 (old_features & NETIF_F_VLAN_CHALLENGED))
2095 pr_info("%s: last VLAN challenged slave %s left bond %s. VLAN blocking is removed\n",
2096 bond_dev->name, slave_dev->name, bond_dev->name);
2097
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002098 /* must do this from outside any spinlocks */
2099 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 bond_del_vlans_from_slave(bond, slave_dev);
2102
2103 /* If the mode USES_PRIMARY, then we should only remove its
2104 * promisc and mc settings if it was the curr_active_slave, but that was
2105 * already taken care of above when we detached the slave
2106 */
2107 if (!USES_PRIMARY(bond->params.mode)) {
2108 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002109 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002113 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002117 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002119 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
2121
Jiri Pirko1765a572011-02-12 06:48:36 +00002122 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002124 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07002125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 /* close slave before restoring its mac address */
2127 dev_close(slave_dev);
2128
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07002129 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002130 /* restore original ("permanent") mac address */
2131 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2132 addr.sa_family = slave_dev->type;
2133 dev_set_mac_address(slave_dev, &addr);
2134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00002136 dev_set_mtu(slave_dev, slave->original_mtu);
2137
Jiri Pirko2d7011c2011-03-16 08:46:43 +00002138 slave_dev->priv_flags &= ~IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 kfree(slave);
2141
2142 return 0; /* deletion OK */
2143}
2144
2145/*
Nicolas de Pesloüandadaa102011-03-19 13:36:18 -07002146* First release a slave and then destroy the bond if no more slaves are left.
Moni Shouad90a1622007-10-09 19:43:43 -07002147* Must be under rtnl_lock when this function is called.
2148*/
stephen hemminger26d8ee72010-10-15 05:09:34 +00002149static int bond_release_and_destroy(struct net_device *bond_dev,
2150 struct net_device *slave_dev)
Moni Shouad90a1622007-10-09 19:43:43 -07002151{
Wang Chen454d7c92008-11-12 23:37:49 -08002152 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002153 int ret;
2154
2155 ret = bond_release(bond_dev, slave_dev);
2156 if ((ret == 0) && (bond->slave_cnt == 0)) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002157 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002158 pr_info("%s: destroying bond %s.\n",
2159 bond_dev->name, bond_dev->name);
Stephen Hemminger9e716262009-06-12 19:02:47 +00002160 unregister_netdevice(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002161 }
2162 return ret;
2163}
2164
2165/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 * This function releases all slaves.
2167 */
2168static int bond_release_all(struct net_device *bond_dev)
2169{
Wang Chen454d7c92008-11-12 23:37:49 -08002170 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 struct slave *slave;
2172 struct net_device *slave_dev;
2173 struct sockaddr addr;
2174
2175 write_lock_bh(&bond->lock);
2176
Jay Vosburghff59c452006-03-27 13:27:43 -08002177 netif_carrier_off(bond_dev);
2178
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002179 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 bond->current_arp_slave = NULL;
2183 bond->primary_slave = NULL;
2184 bond_change_active_slave(bond, NULL);
2185
2186 while ((slave = bond->first_slave) != NULL) {
2187 /* Inform AD package of unbinding of slave
2188 * before slave is detached from the list.
2189 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002190 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 bond_3ad_unbind_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
2193 slave_dev = slave->dev;
2194 bond_detach_slave(bond, slave);
2195
Jay Vosburgh25433312008-01-17 16:24:59 -08002196 /* now that the slave is detached, unlock and perform
2197 * all the undo steps that should not be called from
2198 * within a lock.
2199 */
2200 write_unlock_bh(&bond->lock);
2201
Jiri Pirko35d48902011-03-22 02:38:12 +00002202 /* unregister rx_handler early so bond_handle_frame wouldn't
2203 * be called for this slave anymore.
2204 */
2205 netdev_rx_handler_unregister(slave_dev);
2206 synchronize_net();
2207
Holger Eitzenberger58402052008-12-09 23:07:13 -08002208 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 /* must be called only after the slave
2210 * has been detached from the list
2211 */
2212 bond_alb_deinit_slave(bond, slave);
2213 }
2214
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002215 bond_destroy_slave_symlinks(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 bond_del_vlans_from_slave(bond, slave_dev);
2217
2218 /* If the mode USES_PRIMARY, then we should only remove its
2219 * promisc and mc settings if it was the curr_active_slave, but that was
2220 * already taken care of above when we detached the slave
2221 */
2222 if (!USES_PRIMARY(bond->params.mode)) {
2223 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002224 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002228 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002232 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002234 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
2236
Jiri Pirko1765a572011-02-12 06:48:36 +00002237 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002239 slave_disable_netpoll(slave);
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 /* close slave before restoring its mac address */
2242 dev_close(slave_dev);
2243
Jay Vosburghdd957c52007-10-09 19:57:24 -07002244 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002245 /* restore original ("permanent") mac address*/
2246 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2247 addr.sa_family = slave_dev->type;
2248 dev_set_mac_address(slave_dev, &addr);
2249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 kfree(slave);
2252
2253 /* re-acquire the lock before getting the next slave */
2254 write_lock_bh(&bond->lock);
2255 }
2256
2257 /* zero the mac address of the master so it will be
2258 * set by the application to the mac address of the
2259 * first slave
2260 */
2261 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2262
Michał Mirosławb2a103e2011-05-07 03:22:17 +00002263 if (bond->vlgrp) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002264 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2265 bond_dev->name, bond_dev->name);
2266 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2267 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 }
2269
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002270 pr_info("%s: released all slaves\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272out:
2273 write_unlock_bh(&bond->lock);
Michał Mirosławb2a103e2011-05-07 03:22:17 +00002274
2275 bond_compute_features(bond);
2276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 return 0;
2278}
2279
2280/*
2281 * This function changes the active slave to slave <slave_dev>.
2282 * It returns -EINVAL in the following cases.
2283 * - <slave_dev> is not found in the list.
2284 * - There is not active slave now.
2285 * - <slave_dev> is already active.
2286 * - The link state of <slave_dev> is not BOND_LINK_UP.
2287 * - <slave_dev> is not running.
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002288 * In these cases, this function does nothing.
2289 * In the other cases, current_slave pointer is changed and 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 */
2291static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2292{
Wang Chen454d7c92008-11-12 23:37:49 -08002293 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 struct slave *old_active = NULL;
2295 struct slave *new_active = NULL;
2296 int res = 0;
2297
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002298 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
2301 /* Verify that master_dev is indeed the master of slave_dev */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002302 if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002305 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002307 read_lock(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 old_active = bond->curr_active_slave;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002309 read_unlock(&bond->curr_slave_lock);
2310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 new_active = bond_get_slave_by_dev(bond, slave_dev);
2312
2313 /*
2314 * Changing to the current active: do nothing; return success.
2315 */
2316 if (new_active && (new_active == old_active)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002317 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 return 0;
2319 }
2320
2321 if ((new_active) &&
2322 (old_active) &&
2323 (new_active->link == BOND_LINK_UP) &&
2324 IS_UP(new_active->dev)) {
Neil Hormane843fa52010-10-13 16:01:50 +00002325 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002326 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 bond_change_active_slave(bond, new_active);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002328 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002329 unblock_netpoll_tx();
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002330 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002333 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 return res;
2336}
2337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2339{
Wang Chen454d7c92008-11-12 23:37:49 -08002340 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342 info->bond_mode = bond->params.mode;
2343 info->miimon = bond->params.miimon;
2344
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002345 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 info->num_slaves = bond->slave_cnt;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002347 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
2349 return 0;
2350}
2351
2352static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2353{
Wang Chen454d7c92008-11-12 23:37:49 -08002354 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 struct slave *slave;
Eric Dumazet689c96c2009-04-23 03:39:04 +00002356 int i, res = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002358 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
2360 bond_for_each_slave(bond, slave, i) {
2361 if (i == (int)info->slave_id) {
Eric Dumazet689c96c2009-04-23 03:39:04 +00002362 res = 0;
2363 strcpy(info->slave_name, slave->dev->name);
2364 info->link = slave->link;
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002365 info->state = bond_slave_state(slave);
Eric Dumazet689c96c2009-04-23 03:39:04 +00002366 info->link_failure_count = slave->link_failure_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 break;
2368 }
2369 }
2370
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002371 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Eric Dumazet689c96c2009-04-23 03:39:04 +00002373 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374}
2375
2376/*-------------------------------- Monitoring -------------------------------*/
2377
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002378
2379static int bond_miimon_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380{
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002381 struct slave *slave;
2382 int i, link_state, commit = 0;
Jiri Pirko41f89102009-04-24 03:57:29 +00002383 bool ignore_updelay;
2384
2385 ignore_updelay = !bond->curr_active_slave ? true : false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 bond_for_each_slave(bond, slave, i) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002388 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002390 link_state = bond_check_dev_link(bond, slave->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
2392 switch (slave->link) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002393 case BOND_LINK_UP:
2394 if (link_state)
2395 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002397 slave->link = BOND_LINK_FAIL;
2398 slave->delay = bond->params.downdelay;
2399 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002400 pr_info("%s: link status down for %sinterface %s, disabling it in %d ms.\n",
2401 bond->dev->name,
2402 (bond->params.mode ==
2403 BOND_MODE_ACTIVEBACKUP) ?
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002404 (bond_is_active_slave(slave) ?
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002405 "active " : "backup ") : "",
2406 slave->dev->name,
2407 bond->params.downdelay * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002409 /*FALLTHRU*/
2410 case BOND_LINK_FAIL:
2411 if (link_state) {
2412 /*
2413 * recovered before downdelay expired
2414 */
2415 slave->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 slave->jiffies = jiffies;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002417 pr_info("%s: link status up again after %d ms for interface %s.\n",
2418 bond->dev->name,
2419 (bond->params.downdelay - slave->delay) *
2420 bond->params.miimon,
2421 slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002422 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002424
2425 if (slave->delay <= 0) {
2426 slave->new_link = BOND_LINK_DOWN;
2427 commit++;
2428 continue;
2429 }
2430
2431 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002434 case BOND_LINK_DOWN:
2435 if (!link_state)
2436 continue;
2437
2438 slave->link = BOND_LINK_BACK;
2439 slave->delay = bond->params.updelay;
2440
2441 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002442 pr_info("%s: link status up for interface %s, enabling it in %d ms.\n",
2443 bond->dev->name, slave->dev->name,
2444 ignore_updelay ? 0 :
2445 bond->params.updelay *
2446 bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002448 /*FALLTHRU*/
2449 case BOND_LINK_BACK:
2450 if (!link_state) {
2451 slave->link = BOND_LINK_DOWN;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002452 pr_info("%s: link status down again after %d ms for interface %s.\n",
2453 bond->dev->name,
2454 (bond->params.updelay - slave->delay) *
2455 bond->params.miimon,
2456 slave->dev->name);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002457
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002458 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002460
Jiri Pirko41f89102009-04-24 03:57:29 +00002461 if (ignore_updelay)
2462 slave->delay = 0;
2463
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002464 if (slave->delay <= 0) {
2465 slave->new_link = BOND_LINK_UP;
2466 commit++;
Jiri Pirko41f89102009-04-24 03:57:29 +00002467 ignore_updelay = false;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002468 continue;
2469 }
2470
2471 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 break;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002473 }
2474 }
2475
2476 return commit;
2477}
2478
2479static void bond_miimon_commit(struct bonding *bond)
2480{
2481 struct slave *slave;
2482 int i;
2483
2484 bond_for_each_slave(bond, slave, i) {
2485 switch (slave->new_link) {
2486 case BOND_LINK_NOCHANGE:
2487 continue;
2488
2489 case BOND_LINK_UP:
2490 slave->link = BOND_LINK_UP;
2491 slave->jiffies = jiffies;
2492
2493 if (bond->params.mode == BOND_MODE_8023AD) {
2494 /* prevent it from being the active one */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002495 bond_set_backup_slave(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002496 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2497 /* make it immediately active */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002498 bond_set_active_slave(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002499 } else if (slave != bond->primary_slave) {
2500 /* prevent it from being the active one */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002501 bond_set_backup_slave(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002502 }
2503
Krzysztof Piotr Oledzki546add72010-10-06 14:28:22 -07002504 bond_update_speed_duplex(slave);
2505
David Decotigny5d305302011-04-13 15:22:31 +00002506 pr_info("%s: link status definitely up for interface %s, %u Mbps %s duplex.\n",
Krzysztof Piotr Oledzki700c2a72010-10-06 14:25:06 -07002507 bond->dev->name, slave->dev->name,
2508 slave->speed, slave->duplex ? "full" : "half");
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002509
2510 /* notify ad that the link status has changed */
2511 if (bond->params.mode == BOND_MODE_8023AD)
2512 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2513
Holger Eitzenberger58402052008-12-09 23:07:13 -08002514 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002515 bond_alb_handle_link_change(bond, slave,
2516 BOND_LINK_UP);
2517
2518 if (!bond->curr_active_slave ||
2519 (slave == bond->primary_slave))
2520 goto do_failover;
2521
2522 continue;
2523
2524 case BOND_LINK_DOWN:
Jay Vosburghfba4acd2008-10-30 17:41:14 -07002525 if (slave->link_failure_count < UINT_MAX)
2526 slave->link_failure_count++;
2527
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002528 slave->link = BOND_LINK_DOWN;
2529
2530 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2531 bond->params.mode == BOND_MODE_8023AD)
2532 bond_set_slave_inactive_flags(slave);
2533
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002534 pr_info("%s: link status definitely down for interface %s, disabling it\n",
2535 bond->dev->name, slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002536
2537 if (bond->params.mode == BOND_MODE_8023AD)
2538 bond_3ad_handle_link_change(slave,
2539 BOND_LINK_DOWN);
2540
Jiri Pirkoae63e802009-05-27 05:42:36 +00002541 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002542 bond_alb_handle_link_change(bond, slave,
2543 BOND_LINK_DOWN);
2544
2545 if (slave == bond->curr_active_slave)
2546 goto do_failover;
2547
2548 continue;
2549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002551 pr_err("%s: invalid new link %d on slave %s\n",
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002552 bond->dev->name, slave->new_link,
2553 slave->dev->name);
2554 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002556 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 }
2558
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002559do_failover:
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002560 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00002561 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002562 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 bond_select_active_slave(bond);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002564 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002565 unblock_netpoll_tx();
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002566 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002567
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002568 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569}
2570
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002571/*
2572 * bond_mii_monitor
2573 *
2574 * Really a wrapper that splits the mii monitor into two phases: an
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002575 * inspection, then (if inspection indicates something needs to be done)
2576 * an acquisition of appropriate locks followed by a commit phase to
2577 * implement whatever link state changes are indicated.
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002578 */
2579void bond_mii_monitor(struct work_struct *work)
2580{
2581 struct bonding *bond = container_of(work, struct bonding,
2582 mii_work.work);
Ben Hutchingsad246c92011-04-26 15:25:52 +00002583 bool should_notify_peers = false;
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002584
2585 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002586 if (bond->kill_timers)
2587 goto out;
2588
2589 if (bond->slave_cnt == 0)
2590 goto re_arm;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002591
Ben Hutchingsad246c92011-04-26 15:25:52 +00002592 should_notify_peers = bond_should_notify_peers(bond);
2593
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002594 if (bond_miimon_inspect(bond)) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002595 read_unlock(&bond->lock);
2596 rtnl_lock();
2597 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002598
2599 bond_miimon_commit(bond);
2600
Jay Vosburgh56556622008-01-17 16:25:03 -08002601 read_unlock(&bond->lock);
2602 rtnl_unlock(); /* might sleep, hold no other locks */
2603 read_lock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002604 }
2605
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002606re_arm:
2607 if (bond->params.miimon)
2608 queue_delayed_work(bond->wq, &bond->mii_work,
2609 msecs_to_jiffies(bond->params.miimon));
2610out:
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002611 read_unlock(&bond->lock);
Ben Hutchingsad246c92011-04-26 15:25:52 +00002612
2613 if (should_notify_peers) {
2614 rtnl_lock();
2615 netdev_bonding_change(bond->dev, NETDEV_NOTIFY_PEERS);
2616 rtnl_unlock();
2617 }
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002618}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002619
Al Virod3bb52b2007-08-22 20:06:58 -04002620static __be32 bond_glean_dev_ip(struct net_device *dev)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002621{
2622 struct in_device *idev;
2623 struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07002624 __be32 addr = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002625
2626 if (!dev)
2627 return 0;
2628
2629 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002630 idev = __in_dev_get_rcu(dev);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002631 if (!idev)
2632 goto out;
2633
2634 ifa = idev->ifa_list;
2635 if (!ifa)
2636 goto out;
2637
2638 addr = ifa->ifa_local;
2639out:
2640 rcu_read_unlock();
2641 return addr;
2642}
2643
Al Virod3bb52b2007-08-22 20:06:58 -04002644static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002645{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002646 struct vlan_entry *vlan;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002647
2648 if (ip == bond->master_ip)
2649 return 1;
2650
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002651 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002652 if (ip == vlan->vlan_ip)
2653 return 1;
2654 }
2655
2656 return 0;
2657}
2658
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002659/*
2660 * We go to the (large) trouble of VLAN tagging ARP frames because
2661 * switches in VLAN mode (especially if ports are configured as
2662 * "native" to a VLAN) might not pass non-tagged frames.
2663 */
Al Virod3bb52b2007-08-22 20:06:58 -04002664static 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 -04002665{
2666 struct sk_buff *skb;
2667
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002668 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002669 slave_dev->name, dest_ip, src_ip, vlan_id);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002670
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002671 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2672 NULL, slave_dev->dev_addr, NULL);
2673
2674 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002675 pr_err("ARP packet allocation failed\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002676 return;
2677 }
2678 if (vlan_id) {
2679 skb = vlan_put_tag(skb, vlan_id);
2680 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002681 pr_err("failed to insert VLAN tag\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002682 return;
2683 }
2684 }
2685 arp_xmit(skb);
2686}
2687
2688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2690{
David S. Millerb23dd4f2011-03-02 14:31:35 -08002691 int i, vlan_id;
Al Virod3bb52b2007-08-22 20:06:58 -04002692 __be32 *targets = bond->params.arp_targets;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002693 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002694 struct net_device *vlan_dev;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002695 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Mitch Williams6b780562005-11-09 10:36:19 -08002697 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2698 if (!targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07002699 break;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002700 pr_debug("basa: target %x\n", targets[i]);
Jay Vosburghf35188f2010-07-21 12:14:47 +00002701 if (!bond->vlgrp) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002702 pr_debug("basa: empty vlan: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002703 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2704 bond->master_ip, 0);
2705 continue;
2706 }
2707
2708 /*
2709 * If VLANs are configured, we do a route lookup to
2710 * determine which VLAN interface would be used, so we
2711 * can tag the ARP with the proper VLAN tag.
2712 */
David S. Miller78fbfd82011-03-12 00:00:52 -05002713 rt = ip_route_output(dev_net(bond->dev), targets[i], 0,
2714 RTO_ONLINK, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08002715 if (IS_ERR(rt)) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002716 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002717 pr_warning("%s: no route to arp_ip_target %pI4\n",
David S. Miller78fbfd82011-03-12 00:00:52 -05002718 bond->dev->name, &targets[i]);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002719 }
2720 continue;
2721 }
2722
2723 /*
2724 * This target is not on a VLAN
2725 */
Changli Gaod8d1f302010-06-10 23:31:35 -07002726 if (rt->dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002727 ip_rt_put(rt);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002728 pr_debug("basa: rtdev == bond->dev: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002729 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2730 bond->master_ip, 0);
2731 continue;
2732 }
2733
2734 vlan_id = 0;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002735 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002736 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Changli Gaod8d1f302010-06-10 23:31:35 -07002737 if (vlan_dev == rt->dst.dev) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002738 vlan_id = vlan->vlan_id;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002739 pr_debug("basa: vlan match on %s %d\n",
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002740 vlan_dev->name, vlan_id);
2741 break;
2742 }
2743 }
2744
2745 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002746 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002747 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2748 vlan->vlan_ip, vlan_id);
2749 continue;
2750 }
2751
2752 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002753 pr_warning("%s: no path to arp_ip_target %pI4 via rt.dev %s\n",
David S. Miller78fbfd82011-03-12 00:00:52 -05002754 bond->dev->name, &targets[i],
Changli Gaod8d1f302010-06-10 23:31:35 -07002755 rt->dst.dev ? rt->dst.dev->name : "NULL");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002756 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002757 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002758 }
2759}
2760
Al Virod3bb52b2007-08-22 20:06:58 -04002761static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002762{
2763 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002764 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002765
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002766 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002767 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002768 &sip, &tip, i, &targets[i],
2769 bond_has_this_ip(bond, tip));
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002770 if (sip == targets[i]) {
2771 if (bond_has_this_ip(bond, tip))
2772 slave->last_arp_rx = jiffies;
2773 return;
2774 }
2775 }
2776}
2777
Jiri Pirko3aba8912011-04-19 03:48:16 +00002778static void bond_arp_rcv(struct sk_buff *skb, struct bonding *bond,
2779 struct slave *slave)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002780{
2781 struct arphdr *arp;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002782 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002783 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002784
Jiri Pirko3aba8912011-04-19 03:48:16 +00002785 if (skb->protocol != __cpu_to_be16(ETH_P_ARP))
2786 return;
Andy Gospodarek1f3c8802009-12-14 10:48:58 +00002787
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002788 read_lock(&bond->lock);
2789
Jiri Pirko3aba8912011-04-19 03:48:16 +00002790 pr_debug("bond_arp_rcv: bond %s skb->dev %s\n",
2791 bond->dev->name, skb->dev->name);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002792
Jiri Pirko3aba8912011-04-19 03:48:16 +00002793 if (!pskb_may_pull(skb, arp_hdr_len(bond->dev)))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002794 goto out_unlock;
2795
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002796 arp = arp_hdr(skb);
Jiri Pirko3aba8912011-04-19 03:48:16 +00002797 if (arp->ar_hln != bond->dev->addr_len ||
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002798 skb->pkt_type == PACKET_OTHERHOST ||
2799 skb->pkt_type == PACKET_LOOPBACK ||
2800 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2801 arp->ar_pro != htons(ETH_P_IP) ||
2802 arp->ar_pln != 4)
2803 goto out_unlock;
2804
2805 arp_ptr = (unsigned char *)(arp + 1);
Jiri Pirko3aba8912011-04-19 03:48:16 +00002806 arp_ptr += bond->dev->addr_len;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002807 memcpy(&sip, arp_ptr, 4);
Jiri Pirko3aba8912011-04-19 03:48:16 +00002808 arp_ptr += 4 + bond->dev->addr_len;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002809 memcpy(&tip, arp_ptr, 4);
2810
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002811 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002812 bond->dev->name, slave->dev->name, bond_slave_state(slave),
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002813 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2814 &sip, &tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002815
2816 /*
2817 * Backup slaves won't see the ARP reply, but do come through
2818 * here for each ARP probe (so we swap the sip/tip to validate
2819 * the probe). In a "redundant switch, common router" type of
2820 * configuration, the ARP probe will (hopefully) travel from
2821 * the active, through one switch, the router, then the other
2822 * switch before reaching the backup.
2823 */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002824 if (bond_is_active_slave(slave))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002825 bond_validate_arp(bond, slave, sip, tip);
2826 else
2827 bond_validate_arp(bond, slave, tip, sip);
2828
2829out_unlock:
2830 read_unlock(&bond->lock);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002831}
2832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833/*
2834 * this function is called regularly to monitor each slave's link
2835 * ensuring that traffic is being sent and received when arp monitoring
2836 * is used in load-balancing mode. if the adapter has been dormant, then an
2837 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2838 * arp monitoring in active backup mode.
2839 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002840void bond_loadbalance_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002842 struct bonding *bond = container_of(work, struct bonding,
2843 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 struct slave *slave, *oldcurrent;
2845 int do_failover = 0;
2846 int delta_in_ticks;
2847 int i;
2848
2849 read_lock(&bond->lock);
2850
Jay Vosburgh5ce0da82008-05-17 21:10:07 -07002851 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002853 if (bond->kill_timers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002856 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
2859 read_lock(&bond->curr_slave_lock);
2860 oldcurrent = bond->curr_active_slave;
2861 read_unlock(&bond->curr_slave_lock);
2862
2863 /* see if any of the previous devices are up now (i.e. they have
2864 * xmt and rcv traffic). the curr_active_slave does not come into
2865 * the picture unless it is null. also, slave->jiffies is not needed
2866 * here because we send an arp on each slave and give a slave as
2867 * long as it needs to get the tx/rx within the delta.
2868 * TODO: what about up/down delay in arp mode? it wasn't here before
2869 * so it can wait
2870 */
2871 bond_for_each_slave(bond, slave, i) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002872 unsigned long trans_start = dev_trans_start(slave->dev);
2873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002875 if (time_in_range(jiffies,
2876 trans_start - delta_in_ticks,
2877 trans_start + delta_in_ticks) &&
2878 time_in_range(jiffies,
2879 slave->dev->last_rx - delta_in_ticks,
2880 slave->dev->last_rx + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
2882 slave->link = BOND_LINK_UP;
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002883 bond_set_active_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
2885 /* primary_slave has no meaning in round-robin
2886 * mode. the window of a slave being up and
2887 * curr_active_slave being null after enslaving
2888 * is closed.
2889 */
2890 if (!oldcurrent) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002891 pr_info("%s: link status definitely up for interface %s, ",
2892 bond->dev->name,
2893 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 do_failover = 1;
2895 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002896 pr_info("%s: interface %s is now up\n",
2897 bond->dev->name,
2898 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 }
2900 }
2901 } else {
2902 /* slave->link == BOND_LINK_UP */
2903
2904 /* not all switches will respond to an arp request
2905 * when the source ip is 0, so don't take the link down
2906 * if we don't know our ip yet
2907 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002908 if (!time_in_range(jiffies,
2909 trans_start - delta_in_ticks,
2910 trans_start + 2 * delta_in_ticks) ||
2911 !time_in_range(jiffies,
2912 slave->dev->last_rx - delta_in_ticks,
2913 slave->dev->last_rx + 2 * delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
2915 slave->link = BOND_LINK_DOWN;
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002916 bond_set_backup_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002918 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002921 pr_info("%s: interface %s is now down.\n",
2922 bond->dev->name,
2923 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002925 if (slave == oldcurrent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 do_failover = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 }
2928 }
2929
2930 /* note: if switch is in round-robin mode, all links
2931 * must tx arp to ensure all links rx an arp - otherwise
2932 * links may oscillate or not come up at all; if switch is
2933 * in something like xor mode, there is nothing we can
2934 * do - all replies will be rx'ed on same link causing slaves
2935 * to be unstable during low/no traffic periods
2936 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002937 if (IS_UP(slave->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 bond_arp_send_all(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 }
2940
2941 if (do_failover) {
Neil Hormane843fa52010-10-13 16:01:50 +00002942 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002943 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
2945 bond_select_active_slave(bond);
2946
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002947 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002948 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 }
2950
2951re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002952 if (bond->params.arp_interval)
2953 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954out:
2955 read_unlock(&bond->lock);
2956}
2957
2958/*
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002959 * Called to inspect slaves for active-backup mode ARP monitor link state
2960 * changes. Sets new_link in slaves to specify what action should take
2961 * place for the slave. Returns 0 if no changes are found, >0 if changes
2962 * to link states must be committed.
2963 *
2964 * Called with bond->lock held for read.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002966static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 struct slave *slave;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002969 int i, commit = 0;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002970 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 bond_for_each_slave(bond, slave, i) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002973 slave->new_link = BOND_LINK_NOCHANGE;
2974
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002976 if (time_in_range(jiffies,
2977 slave_last_rx(bond, slave) - delta_in_ticks,
2978 slave_last_rx(bond, slave) + delta_in_ticks)) {
2979
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002980 slave->new_link = BOND_LINK_UP;
2981 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002984 continue;
2985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002987 /*
2988 * Give slaves 2*delta after being enslaved or made
2989 * active. This avoids bouncing, as the last receive
2990 * times need a full ARP monitor cycle to be updated.
2991 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002992 if (time_in_range(jiffies,
2993 slave->jiffies - delta_in_ticks,
2994 slave->jiffies + 2 * delta_in_ticks))
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002995 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002997 /*
2998 * Backup slave is down if:
2999 * - No current_arp_slave AND
3000 * - more than 3*delta since last receive AND
3001 * - the bond has an IP address
3002 *
3003 * Note: a non-null current_arp_slave indicates
3004 * the curr_active_slave went down and we are
3005 * searching for a new one; under this condition
3006 * we only take the curr_active_slave down - this
3007 * gives each slave a chance to tx/rx traffic
3008 * before being taken out
3009 */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00003010 if (!bond_is_active_slave(slave) &&
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003011 !bond->current_arp_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003012 !time_in_range(jiffies,
3013 slave_last_rx(bond, slave) - delta_in_ticks,
3014 slave_last_rx(bond, slave) + 3 * delta_in_ticks)) {
3015
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003016 slave->new_link = BOND_LINK_DOWN;
3017 commit++;
3018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003020 /*
3021 * Active slave is down if:
3022 * - more than 2*delta since transmitting OR
3023 * - (more than 2*delta since receive AND
3024 * the bond has an IP address)
3025 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003026 trans_start = dev_trans_start(slave->dev);
Jiri Pirkoe30bc062011-03-12 03:14:37 +00003027 if (bond_is_active_slave(slave) &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003028 (!time_in_range(jiffies,
3029 trans_start - delta_in_ticks,
3030 trans_start + 2 * delta_in_ticks) ||
3031 !time_in_range(jiffies,
3032 slave_last_rx(bond, slave) - delta_in_ticks,
3033 slave_last_rx(bond, slave) + 2 * delta_in_ticks))) {
3034
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003035 slave->new_link = BOND_LINK_DOWN;
3036 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 }
3038 }
3039
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003040 return commit;
3041}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003043/*
3044 * Called to commit link state changes noted by inspection step of
3045 * active-backup mode ARP monitor.
3046 *
3047 * Called with RTNL and bond->lock for read.
3048 */
3049static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
3050{
3051 struct slave *slave;
3052 int i;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003053 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003055 bond_for_each_slave(bond, slave, i) {
3056 switch (slave->new_link) {
3057 case BOND_LINK_NOCHANGE:
3058 continue;
3059
3060 case BOND_LINK_UP:
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003061 trans_start = dev_trans_start(slave->dev);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003062 if ((!bond->curr_active_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003063 time_in_range(jiffies,
3064 trans_start - delta_in_ticks,
3065 trans_start + delta_in_ticks)) ||
Jiri Pirkob9f60252009-08-31 11:09:38 +00003066 bond->curr_active_slave != slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003067 slave->link = BOND_LINK_UP;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003068 bond->current_arp_slave = NULL;
3069
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003070 pr_info("%s: link status definitely up for interface %s.\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003071 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003072
Jiri Pirkob9f60252009-08-31 11:09:38 +00003073 if (!bond->curr_active_slave ||
3074 (slave == bond->primary_slave))
3075 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003076
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003077 }
3078
Jiri Pirkob9f60252009-08-31 11:09:38 +00003079 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003080
3081 case BOND_LINK_DOWN:
3082 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003085 slave->link = BOND_LINK_DOWN;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003086 bond_set_slave_inactive_flags(slave);
3087
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003088 pr_info("%s: link status definitely down for interface %s, disabling it\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003089 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003090
3091 if (slave == bond->curr_active_slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003092 bond->current_arp_slave = NULL;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003093 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003094 }
Jiri Pirkob9f60252009-08-31 11:09:38 +00003095
3096 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003097
3098 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003099 pr_err("%s: impossible: new_link %d on slave %s\n",
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003100 bond->dev->name, slave->new_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 slave->dev->name);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003102 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Jiri Pirkob9f60252009-08-31 11:09:38 +00003105do_failover:
3106 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00003107 block_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003108 write_lock_bh(&bond->curr_slave_lock);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003109 bond_select_active_slave(bond);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003110 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00003111 unblock_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003112 }
3113
3114 bond_set_carrier(bond);
3115}
3116
3117/*
3118 * Send ARP probes for active-backup mode ARP monitor.
3119 *
3120 * Called with bond->lock held for read.
3121 */
3122static void bond_ab_arp_probe(struct bonding *bond)
3123{
3124 struct slave *slave;
3125 int i;
3126
3127 read_lock(&bond->curr_slave_lock);
3128
3129 if (bond->current_arp_slave && bond->curr_active_slave)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003130 pr_info("PROBE: c_arp %s && cas %s BAD\n",
3131 bond->current_arp_slave->dev->name,
3132 bond->curr_active_slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003133
3134 if (bond->curr_active_slave) {
3135 bond_arp_send_all(bond, bond->curr_active_slave);
3136 read_unlock(&bond->curr_slave_lock);
3137 return;
3138 }
3139
3140 read_unlock(&bond->curr_slave_lock);
3141
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 /* if we don't have a curr_active_slave, search for the next available
3143 * backup slave from the current_arp_slave and make it the candidate
3144 * for becoming the curr_active_slave
3145 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003146
3147 if (!bond->current_arp_slave) {
3148 bond->current_arp_slave = bond->first_slave;
3149 if (!bond->current_arp_slave)
3150 return;
3151 }
3152
3153 bond_set_slave_inactive_flags(bond->current_arp_slave);
3154
3155 /* search for next candidate */
3156 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3157 if (IS_UP(slave->dev)) {
3158 slave->link = BOND_LINK_BACK;
3159 bond_set_slave_active_flags(slave);
3160 bond_arp_send_all(bond, slave);
3161 slave->jiffies = jiffies;
3162 bond->current_arp_slave = slave;
3163 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 }
3165
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003166 /* if the link state is up at this point, we
3167 * mark it down - this can happen if we have
3168 * simultaneous link failures and
3169 * reselect_active_interface doesn't make this
3170 * one the current slave so it is still marked
3171 * up when it is actually down
3172 */
3173 if (slave->link == BOND_LINK_UP) {
3174 slave->link = BOND_LINK_DOWN;
3175 if (slave->link_failure_count < UINT_MAX)
3176 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003178 bond_set_slave_inactive_flags(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003180 pr_info("%s: backup interface %s is now down.\n",
3181 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 }
3183 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003184}
3185
3186void bond_activebackup_arp_mon(struct work_struct *work)
3187{
3188 struct bonding *bond = container_of(work, struct bonding,
3189 arp_work.work);
Ben Hutchingsad246c92011-04-26 15:25:52 +00003190 bool should_notify_peers = false;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003191 int delta_in_ticks;
3192
3193 read_lock(&bond->lock);
3194
3195 if (bond->kill_timers)
3196 goto out;
3197
3198 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3199
3200 if (bond->slave_cnt == 0)
3201 goto re_arm;
3202
Ben Hutchingsad246c92011-04-26 15:25:52 +00003203 should_notify_peers = bond_should_notify_peers(bond);
3204
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003205 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3206 read_unlock(&bond->lock);
3207 rtnl_lock();
3208 read_lock(&bond->lock);
3209
3210 bond_ab_arp_commit(bond, delta_in_ticks);
3211
3212 read_unlock(&bond->lock);
3213 rtnl_unlock();
3214 read_lock(&bond->lock);
3215 }
3216
3217 bond_ab_arp_probe(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
3219re_arm:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003220 if (bond->params.arp_interval)
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003221 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222out:
3223 read_unlock(&bond->lock);
Ben Hutchingsad246c92011-04-26 15:25:52 +00003224
3225 if (should_notify_peers) {
3226 rtnl_lock();
3227 netdev_bonding_change(bond->dev, NETDEV_NOTIFY_PEERS);
3228 rtnl_unlock();
3229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230}
3231
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232/*-------------------------- netdev event handling --------------------------*/
3233
3234/*
3235 * Change device name
3236 */
3237static int bond_event_changename(struct bonding *bond)
3238{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 bond_remove_proc_entry(bond);
3240 bond_create_proc_entry(bond);
Stephen Hemminger7e083842009-06-12 19:02:46 +00003241
Taku Izumif073c7c2010-12-09 15:17:13 +00003242 bond_debug_reregister(bond);
3243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 return NOTIFY_DONE;
3245}
3246
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003247static int bond_master_netdev_event(unsigned long event,
3248 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249{
Wang Chen454d7c92008-11-12 23:37:49 -08003250 struct bonding *event_bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
3252 switch (event) {
3253 case NETDEV_CHANGENAME:
3254 return bond_event_changename(event_bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 default:
3256 break;
3257 }
3258
3259 return NOTIFY_DONE;
3260}
3261
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003262static int bond_slave_netdev_event(unsigned long event,
3263 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
3265 struct net_device *bond_dev = slave_dev->master;
Wang Chen454d7c92008-11-12 23:37:49 -08003266 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
3268 switch (event) {
3269 case NETDEV_UNREGISTER:
3270 if (bond_dev) {
Jay Vosburgh1284cd32007-10-15 16:44:27 -07003271 if (bond->setup_by_slave)
3272 bond_release_and_destroy(bond_dev, slave_dev);
3273 else
3274 bond_release(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 }
3276 break;
3277 case NETDEV_CHANGE:
Jay Vosburgh17d04502009-03-18 18:38:25 -07003278 if (bond->params.mode == BOND_MODE_8023AD || bond_is_lb(bond)) {
3279 struct slave *slave;
3280
3281 slave = bond_get_slave_by_dev(bond, slave_dev);
3282 if (slave) {
David Decotigny5d305302011-04-13 15:22:31 +00003283 u32 old_speed = slave->speed;
David Decotigny65cce192011-04-13 15:22:30 +00003284 u8 old_duplex = slave->duplex;
Jay Vosburgh17d04502009-03-18 18:38:25 -07003285
3286 bond_update_speed_duplex(slave);
3287
3288 if (bond_is_lb(bond))
3289 break;
3290
3291 if (old_speed != slave->speed)
3292 bond_3ad_adapter_speed_changed(slave);
3293 if (old_duplex != slave->duplex)
3294 bond_3ad_adapter_duplex_changed(slave);
3295 }
3296 }
3297
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 break;
3299 case NETDEV_DOWN:
3300 /*
3301 * ... Or is it this?
3302 */
3303 break;
3304 case NETDEV_CHANGEMTU:
3305 /*
3306 * TODO: Should slaves be allowed to
3307 * independently alter their MTU? For
3308 * an active-backup bond, slaves need
3309 * not be the same type of device, so
3310 * MTUs may vary. For other modes,
3311 * slaves arguably should have the
3312 * same MTUs. To do this, we'd need to
3313 * take over the slave's change_mtu
3314 * function for the duration of their
3315 * servitude.
3316 */
3317 break;
3318 case NETDEV_CHANGENAME:
3319 /*
3320 * TODO: handle changing the primary's name
3321 */
3322 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003323 case NETDEV_FEAT_CHANGE:
3324 bond_compute_features(bond);
3325 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 default:
3327 break;
3328 }
3329
3330 return NOTIFY_DONE;
3331}
3332
3333/*
3334 * bond_netdev_event: handle netdev notifier chain events.
3335 *
3336 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003337 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 * locks for us to safely manipulate the slave devices (RTNL lock,
3339 * dev_probe_lock).
3340 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003341static int bond_netdev_event(struct notifier_block *this,
3342 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343{
3344 struct net_device *event_dev = (struct net_device *)ptr;
3345
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003346 pr_debug("event_dev: %s, event: %lx\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003347 event_dev ? event_dev->name : "None",
3348 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003350 if (!(event_dev->priv_flags & IFF_BONDING))
3351 return NOTIFY_DONE;
3352
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 if (event_dev->flags & IFF_MASTER) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003354 pr_debug("IFF_MASTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 return bond_master_netdev_event(event, event_dev);
3356 }
3357
3358 if (event_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003359 pr_debug("IFF_SLAVE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 return bond_slave_netdev_event(event, event_dev);
3361 }
3362
3363 return NOTIFY_DONE;
3364}
3365
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003366/*
3367 * bond_inetaddr_event: handle inetaddr notifier chain events.
3368 *
3369 * We keep track of device IPs primarily to use as source addresses in
3370 * ARP monitor probes (rather than spewing out broadcasts all the time).
3371 *
3372 * We track one IP for the main device (if it has one), plus one per VLAN.
3373 */
3374static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3375{
3376 struct in_ifaddr *ifa = ptr;
3377 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003378 struct bond_net *bn = net_generic(dev_net(event_dev), bond_net_id);
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003379 struct bonding *bond;
3380 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003381
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003382 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003383 if (bond->dev == event_dev) {
3384 switch (event) {
3385 case NETDEV_UP:
3386 bond->master_ip = ifa->ifa_local;
3387 return NOTIFY_OK;
3388 case NETDEV_DOWN:
3389 bond->master_ip = bond_glean_dev_ip(bond->dev);
3390 return NOTIFY_OK;
3391 default:
3392 return NOTIFY_DONE;
3393 }
3394 }
3395
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003396 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +00003397 if (!bond->vlgrp)
3398 continue;
Dan Aloni5c15bde2007-03-02 20:44:51 -08003399 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003400 if (vlan_dev == event_dev) {
3401 switch (event) {
3402 case NETDEV_UP:
3403 vlan->vlan_ip = ifa->ifa_local;
3404 return NOTIFY_OK;
3405 case NETDEV_DOWN:
3406 vlan->vlan_ip =
3407 bond_glean_dev_ip(vlan_dev);
3408 return NOTIFY_OK;
3409 default:
3410 return NOTIFY_DONE;
3411 }
3412 }
3413 }
3414 }
3415 return NOTIFY_DONE;
3416}
3417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418static struct notifier_block bond_netdev_notifier = {
3419 .notifier_call = bond_netdev_event,
3420};
3421
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003422static struct notifier_block bond_inetaddr_notifier = {
3423 .notifier_call = bond_inetaddr_event,
3424};
3425
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003426/*---------------------------- Hashing Policies -----------------------------*/
3427
3428/*
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003429 * Hash for the output device based upon layer 2 and layer 3 data. If
3430 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3431 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003432static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003433{
3434 struct ethhdr *data = (struct ethhdr *)skb->data;
3435 struct iphdr *iph = ip_hdr(skb);
3436
Brian Haleyf14c4e42008-09-02 10:08:08 -04003437 if (skb->protocol == htons(ETH_P_IP)) {
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003438 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
Jasper Spaansd3da6832009-10-23 04:08:46 +00003439 (data->h_dest[5] ^ data->h_source[5])) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003440 }
3441
Jasper Spaansd3da6832009-10-23 04:08:46 +00003442 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003443}
3444
3445/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003446 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003447 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3448 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3449 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003450static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003451{
3452 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003453 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003454 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003455 int layer4_xor = 0;
3456
Brian Haleyf14c4e42008-09-02 10:08:08 -04003457 if (skb->protocol == htons(ETH_P_IP)) {
3458 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003459 (iph->protocol == IPPROTO_TCP ||
3460 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003461 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003462 }
3463 return (layer4_xor ^
3464 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3465
3466 }
3467
Jasper Spaansd3da6832009-10-23 04:08:46 +00003468 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003469}
3470
3471/*
3472 * Hash for the output device based upon layer 2 data
3473 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003474static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003475{
3476 struct ethhdr *data = (struct ethhdr *)skb->data;
3477
Jasper Spaansd3da6832009-10-23 04:08:46 +00003478 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003479}
3480
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481/*-------------------------- Device entry points ----------------------------*/
3482
3483static int bond_open(struct net_device *bond_dev)
3484{
Wang Chen454d7c92008-11-12 23:37:49 -08003485 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
3487 bond->kill_timers = 0;
3488
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003489 INIT_DELAYED_WORK(&bond->mcast_work, bond_resend_igmp_join_requests_delayed);
3490
Holger Eitzenberger58402052008-12-09 23:07:13 -08003491 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 /* bond_alb_initialize must be called before the timer
3493 * is started.
3494 */
3495 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3496 /* something went wrong - fail the open operation */
stephen hemmingerb4739462010-01-25 23:34:15 +00003497 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 }
3499
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003500 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3501 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 }
3503
3504 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003505 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3506 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 }
3508
3509 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003510 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3511 INIT_DELAYED_WORK(&bond->arp_work,
3512 bond_activebackup_arp_mon);
3513 else
3514 INIT_DELAYED_WORK(&bond->arp_work,
3515 bond_loadbalance_arp_mon);
3516
3517 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003518 if (bond->params.arp_validate)
Jiri Pirko3aba8912011-04-19 03:48:16 +00003519 bond->recv_probe = bond_arp_rcv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 }
3521
3522 if (bond->params.mode == BOND_MODE_8023AD) {
Adrian Bunka40745f2007-10-24 18:27:43 +02003523 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003524 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 /* register to receive LACPDUs */
Jiri Pirko3aba8912011-04-19 03:48:16 +00003526 bond->recv_probe = bond_3ad_lacpdu_recv;
Jay Vosburghfd989c82008-11-04 17:51:16 -08003527 bond_3ad_initiate_agg_selection(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 }
3529
3530 return 0;
3531}
3532
3533static int bond_close(struct net_device *bond_dev)
3534{
Wang Chen454d7c92008-11-12 23:37:49 -08003535 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 write_lock_bh(&bond->lock);
3538
Ben Hutchingsad246c92011-04-26 15:25:52 +00003539 bond->send_peer_notif = 0;
3540
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 /* signal timers not to re-arm */
3542 bond->kill_timers = 1;
3543
3544 write_unlock_bh(&bond->lock);
3545
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003547 cancel_delayed_work(&bond->mii_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 }
3549
3550 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003551 cancel_delayed_work(&bond->arp_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 }
3553
3554 switch (bond->params.mode) {
3555 case BOND_MODE_8023AD:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003556 cancel_delayed_work(&bond->ad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 break;
3558 case BOND_MODE_TLB:
3559 case BOND_MODE_ALB:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003560 cancel_delayed_work(&bond->alb_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 break;
3562 default:
3563 break;
3564 }
3565
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003566 if (delayed_work_pending(&bond->mcast_work))
3567 cancel_delayed_work(&bond->mcast_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Holger Eitzenberger58402052008-12-09 23:07:13 -08003569 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 /* Must be called only after all
3571 * slaves have been released
3572 */
3573 bond_alb_deinitialize(bond);
3574 }
Jiri Pirko3aba8912011-04-19 03:48:16 +00003575 bond->recv_probe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
3577 return 0;
3578}
3579
Eric Dumazet28172732010-07-07 14:58:56 -07003580static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3581 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582{
Wang Chen454d7c92008-11-12 23:37:49 -08003583 struct bonding *bond = netdev_priv(bond_dev);
Eric Dumazet28172732010-07-07 14:58:56 -07003584 struct rtnl_link_stats64 temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 struct slave *slave;
3586 int i;
3587
Eric Dumazet28172732010-07-07 14:58:56 -07003588 memset(stats, 0, sizeof(*stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
3590 read_lock_bh(&bond->lock);
3591
3592 bond_for_each_slave(bond, slave, i) {
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00003593 const struct rtnl_link_stats64 *sstats =
Eric Dumazet28172732010-07-07 14:58:56 -07003594 dev_get_stats(slave->dev, &temp);
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08003595
Eric Dumazet28172732010-07-07 14:58:56 -07003596 stats->rx_packets += sstats->rx_packets;
3597 stats->rx_bytes += sstats->rx_bytes;
3598 stats->rx_errors += sstats->rx_errors;
3599 stats->rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
Eric Dumazet28172732010-07-07 14:58:56 -07003601 stats->tx_packets += sstats->tx_packets;
3602 stats->tx_bytes += sstats->tx_bytes;
3603 stats->tx_errors += sstats->tx_errors;
3604 stats->tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
Eric Dumazet28172732010-07-07 14:58:56 -07003606 stats->multicast += sstats->multicast;
3607 stats->collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608
Eric Dumazet28172732010-07-07 14:58:56 -07003609 stats->rx_length_errors += sstats->rx_length_errors;
3610 stats->rx_over_errors += sstats->rx_over_errors;
3611 stats->rx_crc_errors += sstats->rx_crc_errors;
3612 stats->rx_frame_errors += sstats->rx_frame_errors;
3613 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3614 stats->rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
Eric Dumazet28172732010-07-07 14:58:56 -07003616 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3617 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3618 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3619 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3620 stats->tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 }
3622
3623 read_unlock_bh(&bond->lock);
3624
3625 return stats;
3626}
3627
3628static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3629{
3630 struct net_device *slave_dev = NULL;
3631 struct ifbond k_binfo;
3632 struct ifbond __user *u_binfo = NULL;
3633 struct ifslave k_sinfo;
3634 struct ifslave __user *u_sinfo = NULL;
3635 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 int res = 0;
3637
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003638 pr_debug("bond_ioctl: master=%s, cmd=%d\n", bond_dev->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
3640 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 case SIOCGMIIPHY:
3642 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003643 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003645
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 mii->phy_id = 0;
3647 /* Fall Through */
3648 case SIOCGMIIREG:
3649 /*
3650 * We do this again just in case we were called by SIOCGMIIREG
3651 * instead of SIOCGMIIPHY.
3652 */
3653 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003654 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003656
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
3658 if (mii->reg_num == 1) {
Wang Chen454d7c92008-11-12 23:37:49 -08003659 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 mii->val_out = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003661 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 read_lock(&bond->curr_slave_lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003663 if (netif_carrier_ok(bond->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 mii->val_out = BMSR_LSTATUS;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003665
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 read_unlock(&bond->curr_slave_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003667 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 }
3669
3670 return 0;
3671 case BOND_INFO_QUERY_OLD:
3672 case SIOCBONDINFOQUERY:
3673 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3674
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003675 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
3678 res = bond_info_query(bond_dev, &k_binfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003679 if (res == 0 &&
3680 copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
3681 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
3683 return res;
3684 case BOND_SLAVE_INFO_QUERY_OLD:
3685 case SIOCBONDSLAVEINFOQUERY:
3686 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3687
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003688 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690
3691 res = bond_slave_info_query(bond_dev, &k_sinfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003692 if (res == 0 &&
3693 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
3694 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
3696 return res;
3697 default:
3698 /* Go on */
3699 break;
3700 }
3701
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003702 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003705 slave_dev = dev_get_by_name(dev_net(bond_dev), ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003707 pr_debug("slave_dev=%p:\n", slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003709 if (!slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 res = -ENODEV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003711 else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003712 pr_debug("slave_dev->name=%s:\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 switch (cmd) {
3714 case BOND_ENSLAVE_OLD:
3715 case SIOCBONDENSLAVE:
3716 res = bond_enslave(bond_dev, slave_dev);
3717 break;
3718 case BOND_RELEASE_OLD:
3719 case SIOCBONDRELEASE:
3720 res = bond_release(bond_dev, slave_dev);
3721 break;
3722 case BOND_SETHWADDR_OLD:
3723 case SIOCBONDSETHWADDR:
3724 res = bond_sethwaddr(bond_dev, slave_dev);
3725 break;
3726 case BOND_CHANGE_ACTIVE_OLD:
3727 case SIOCBONDCHANGEACTIVE:
3728 res = bond_ioctl_change_active(bond_dev, slave_dev);
3729 break;
3730 default:
3731 res = -EOPNOTSUPP;
3732 }
3733
3734 dev_put(slave_dev);
3735 }
3736
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 return res;
3738}
3739
Jiri Pirko22bedad2010-04-01 21:22:57 +00003740static bool bond_addr_in_mc_list(unsigned char *addr,
3741 struct netdev_hw_addr_list *list,
3742 int addrlen)
3743{
3744 struct netdev_hw_addr *ha;
3745
3746 netdev_hw_addr_list_for_each(ha, list)
3747 if (!memcmp(ha->addr, addr, addrlen))
3748 return true;
3749
3750 return false;
3751}
3752
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753static void bond_set_multicast_list(struct net_device *bond_dev)
3754{
Wang Chen454d7c92008-11-12 23:37:49 -08003755 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad2010-04-01 21:22:57 +00003756 struct netdev_hw_addr *ha;
3757 bool found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 /*
3760 * Do promisc before checking multicast_mode
3761 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003762 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07003763 /*
3764 * FIXME: Need to handle the error when one of the multi-slaves
3765 * encounters error.
3766 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 bond_set_promiscuity(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003769
3770 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 bond_set_promiscuity(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003772
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
3774 /* set allmulti flag to slaves */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003775 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07003776 /*
3777 * FIXME: Need to handle the error when one of the multi-slaves
3778 * encounters error.
3779 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 bond_set_allmulti(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003782
3783 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 bond_set_allmulti(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003785
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08003787 read_lock(&bond->lock);
3788
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 bond->flags = bond_dev->flags;
3790
3791 /* looking for addresses to add to slaves' mc list */
Jiri Pirko22bedad2010-04-01 21:22:57 +00003792 netdev_for_each_mc_addr(ha, bond_dev) {
3793 found = bond_addr_in_mc_list(ha->addr, &bond->mc_list,
3794 bond_dev->addr_len);
3795 if (!found)
3796 bond_mc_add(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 }
3798
3799 /* looking for addresses to delete from slaves' list */
Jiri Pirko22bedad2010-04-01 21:22:57 +00003800 netdev_hw_addr_list_for_each(ha, &bond->mc_list) {
3801 found = bond_addr_in_mc_list(ha->addr, &bond_dev->mc,
3802 bond_dev->addr_len);
3803 if (!found)
3804 bond_mc_del(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 }
3806
3807 /* save master's multicast list */
Jiri Pirko22bedad2010-04-01 21:22:57 +00003808 __hw_addr_flush(&bond->mc_list);
3809 __hw_addr_add_multiple(&bond->mc_list, &bond_dev->mc,
3810 bond_dev->addr_len, NETDEV_HW_ADDR_T_MULTICAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08003812 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813}
3814
Stephen Hemminger00829822008-11-20 20:14:53 -08003815static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
3816{
3817 struct bonding *bond = netdev_priv(dev);
3818 struct slave *slave = bond->first_slave;
3819
3820 if (slave) {
3821 const struct net_device_ops *slave_ops
3822 = slave->dev->netdev_ops;
3823 if (slave_ops->ndo_neigh_setup)
Patrick McHardy72e22402009-03-05 01:57:44 -08003824 return slave_ops->ndo_neigh_setup(slave->dev, parms);
Stephen Hemminger00829822008-11-20 20:14:53 -08003825 }
3826 return 0;
3827}
3828
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829/*
3830 * Change the MTU of all of a master's slaves to match the master
3831 */
3832static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3833{
Wang Chen454d7c92008-11-12 23:37:49 -08003834 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 struct slave *slave, *stop_at;
3836 int res = 0;
3837 int i;
3838
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003839 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003840 (bond_dev ? bond_dev->name : "None"), new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
3842 /* Can't hold bond->lock with bh disabled here since
3843 * some base drivers panic. On the other hand we can't
3844 * hold bond->lock without bh disabled because we'll
3845 * deadlock. The only solution is to rely on the fact
3846 * that we're under rtnl_lock here, and the slaves
3847 * list won't change. This doesn't solve the problem
3848 * of setting the slave's MTU while it is
3849 * transmitting, but the assumption is that the base
3850 * driver can handle that.
3851 *
3852 * TODO: figure out a way to safely iterate the slaves
3853 * list, but without holding a lock around the actual
3854 * call to the base driver.
3855 */
3856
3857 bond_for_each_slave(bond, slave, i) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003858 pr_debug("s %p s->p %p c_m %p\n",
3859 slave,
3860 slave->prev,
3861 slave->dev->netdev_ops->ndo_change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08003862
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 res = dev_set_mtu(slave->dev, new_mtu);
3864
3865 if (res) {
3866 /* If we failed to set the slave's mtu to the new value
3867 * we must abort the operation even in ACTIVE_BACKUP
3868 * mode, because if we allow the backup slaves to have
3869 * different mtu values than the active slave we'll
3870 * need to change their mtu when doing a failover. That
3871 * means changing their mtu from timer context, which
3872 * is probably not a good idea.
3873 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003874 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 goto unwind;
3876 }
3877 }
3878
3879 bond_dev->mtu = new_mtu;
3880
3881 return 0;
3882
3883unwind:
3884 /* unwind from head to the slave that failed */
3885 stop_at = slave;
3886 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3887 int tmp_res;
3888
3889 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
3890 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003891 pr_debug("unwind err %d dev %s\n",
3892 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 }
3894 }
3895
3896 return res;
3897}
3898
3899/*
3900 * Change HW address
3901 *
3902 * Note that many devices must be down to change the HW address, and
3903 * downing the master releases all slaves. We can make bonds full of
3904 * bonding devices to test this, however.
3905 */
3906static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
3907{
Wang Chen454d7c92008-11-12 23:37:49 -08003908 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 struct sockaddr *sa = addr, tmp_sa;
3910 struct slave *slave, *stop_at;
3911 int res = 0;
3912 int i;
3913
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08003914 if (bond->params.mode == BOND_MODE_ALB)
3915 return bond_alb_set_mac_address(bond_dev, addr);
3916
3917
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003918 pr_debug("bond=%p, name=%s\n",
3919 bond, bond_dev ? bond_dev->name : "None");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920
Jay Vosburghdd957c52007-10-09 19:57:24 -07003921 /*
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07003922 * If fail_over_mac is set to active, do nothing and return
3923 * success. Returning an error causes ifenslave to fail.
Jay Vosburghdd957c52007-10-09 19:57:24 -07003924 */
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07003925 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
Jay Vosburghdd957c52007-10-09 19:57:24 -07003926 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07003927
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003928 if (!is_valid_ether_addr(sa->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
3931 /* Can't hold bond->lock with bh disabled here since
3932 * some base drivers panic. On the other hand we can't
3933 * hold bond->lock without bh disabled because we'll
3934 * deadlock. The only solution is to rely on the fact
3935 * that we're under rtnl_lock here, and the slaves
3936 * list won't change. This doesn't solve the problem
3937 * of setting the slave's hw address while it is
3938 * transmitting, but the assumption is that the base
3939 * driver can handle that.
3940 *
3941 * TODO: figure out a way to safely iterate the slaves
3942 * list, but without holding a lock around the actual
3943 * call to the base driver.
3944 */
3945
3946 bond_for_each_slave(bond, slave, i) {
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08003947 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003948 pr_debug("slave %p %s\n", slave, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08003950 if (slave_ops->ndo_set_mac_address == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 res = -EOPNOTSUPP;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003952 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 goto unwind;
3954 }
3955
3956 res = dev_set_mac_address(slave->dev, addr);
3957 if (res) {
3958 /* TODO: consider downing the slave
3959 * and retry ?
3960 * User should expect communications
3961 * breakage anyway until ARP finish
3962 * updating, so...
3963 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003964 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 goto unwind;
3966 }
3967 }
3968
3969 /* success */
3970 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
3971 return 0;
3972
3973unwind:
3974 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
3975 tmp_sa.sa_family = bond_dev->type;
3976
3977 /* unwind from head to the slave that failed */
3978 stop_at = slave;
3979 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3980 int tmp_res;
3981
3982 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
3983 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003984 pr_debug("unwind err %d dev %s\n",
3985 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 }
3987 }
3988
3989 return res;
3990}
3991
3992static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
3993{
Wang Chen454d7c92008-11-12 23:37:49 -08003994 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 struct slave *slave, *start_at;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07003996 int i, slave_no, res = 1;
Andy Gospodareka2fd9402010-03-25 14:49:05 +00003997 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
Jay Vosburghcf5f9042007-10-17 17:37:47 -07003999 /*
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004000 * Start with the curr_active_slave that joined the bond as the
4001 * default for sending IGMP traffic. For failover purposes one
4002 * needs to maintain some consistency for the interface that will
4003 * send the join/membership reports. The curr_active_slave found
4004 * will send all of this type of traffic.
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004005 */
Eric Dumazet00ae7022010-03-30 23:08:37 +00004006 if ((iph->protocol == IPPROTO_IGMP) &&
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004007 (skb->protocol == htons(ETH_P_IP))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004009 read_lock(&bond->curr_slave_lock);
4010 slave = bond->curr_active_slave;
4011 read_unlock(&bond->curr_slave_lock);
4012
4013 if (!slave)
4014 goto out;
4015 } else {
4016 /*
4017 * Concurrent TX may collide on rr_tx_counter; we accept
4018 * that as being rare enough not to justify using an
4019 * atomic op here.
4020 */
4021 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
4022
4023 bond_for_each_slave(bond, slave, i) {
4024 slave_no--;
4025 if (slave_no < 0)
4026 break;
4027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 }
4029
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004030 start_at = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 bond_for_each_slave_from(bond, slave, i, start_at) {
4032 if (IS_UP(slave->dev) &&
4033 (slave->link == BOND_LINK_UP) &&
Jiri Pirkoe30bc062011-03-12 03:14:37 +00004034 bond_is_active_slave(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 res = bond_dev_queue_xmit(bond, skb, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 break;
4037 }
4038 }
4039
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040out:
4041 if (res) {
4042 /* no suitable interface, frame not sent */
4043 dev_kfree_skb(skb);
4044 }
Michał Mirosław0693e882011-05-07 01:48:02 +00004045
Patrick McHardyec634fe2009-07-05 19:23:38 -07004046 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047}
4048
John W. Linville075897c2005-09-28 17:50:53 -04004049
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050/*
4051 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4052 * the bond has a usable interface.
4053 */
4054static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4055{
Wang Chen454d7c92008-11-12 23:37:49 -08004056 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 int res = 1;
4058
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 read_lock(&bond->curr_slave_lock);
4060
Michał Mirosław0693e882011-05-07 01:48:02 +00004061 if (bond->curr_active_slave)
4062 res = bond_dev_queue_xmit(bond, skb,
4063 bond->curr_active_slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004065 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 /* no suitable interface, frame not sent */
4067 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004068
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 read_unlock(&bond->curr_slave_lock);
Michał Mirosław0693e882011-05-07 01:48:02 +00004070
Patrick McHardyec634fe2009-07-05 19:23:38 -07004071 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072}
4073
4074/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004075 * In bond_xmit_xor() , we determine the output device by using a pre-
4076 * determined xmit_hash_policy(), If the selected device is not enabled,
4077 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 */
4079static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4080{
Wang Chen454d7c92008-11-12 23:37:49 -08004081 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 struct slave *slave, *start_at;
4083 int slave_no;
4084 int i;
4085 int res = 1;
4086
Jasper Spaansa361c832009-10-23 04:09:24 +00004087 slave_no = bond->xmit_hash_policy(skb, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
4089 bond_for_each_slave(bond, slave, i) {
4090 slave_no--;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004091 if (slave_no < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 }
4094
4095 start_at = slave;
4096
4097 bond_for_each_slave_from(bond, slave, i, start_at) {
4098 if (IS_UP(slave->dev) &&
4099 (slave->link == BOND_LINK_UP) &&
Jiri Pirkoe30bc062011-03-12 03:14:37 +00004100 bond_is_active_slave(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4102 break;
4103 }
4104 }
4105
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 if (res) {
4107 /* no suitable interface, frame not sent */
4108 dev_kfree_skb(skb);
4109 }
Michał Mirosław0693e882011-05-07 01:48:02 +00004110
Patrick McHardyec634fe2009-07-05 19:23:38 -07004111 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112}
4113
4114/*
4115 * in broadcast mode, we send everything to all usable interfaces.
4116 */
4117static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4118{
Wang Chen454d7c92008-11-12 23:37:49 -08004119 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 struct slave *slave, *start_at;
4121 struct net_device *tx_dev = NULL;
4122 int i;
4123 int res = 1;
4124
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 read_lock(&bond->curr_slave_lock);
4126 start_at = bond->curr_active_slave;
4127 read_unlock(&bond->curr_slave_lock);
4128
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004129 if (!start_at)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
4132 bond_for_each_slave_from(bond, slave, i, start_at) {
4133 if (IS_UP(slave->dev) &&
4134 (slave->link == BOND_LINK_UP) &&
Jiri Pirkoe30bc062011-03-12 03:14:37 +00004135 bond_is_active_slave(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 if (tx_dev) {
4137 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4138 if (!skb2) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004139 pr_err("%s: Error: bond_xmit_broadcast(): skb_clone() failed\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08004140 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 continue;
4142 }
4143
4144 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4145 if (res) {
4146 dev_kfree_skb(skb2);
4147 continue;
4148 }
4149 }
4150 tx_dev = slave->dev;
4151 }
4152 }
4153
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004154 if (tx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 res = bond_dev_queue_xmit(bond, skb, tx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
4157out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004158 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 /* no suitable interface, frame not sent */
4160 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004161
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 /* frame sent to all suitable interfaces */
Patrick McHardyec634fe2009-07-05 19:23:38 -07004163 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164}
4165
4166/*------------------------- Device initialization ---------------------------*/
4167
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004168static void bond_set_xmit_hash_policy(struct bonding *bond)
4169{
4170 switch (bond->params.xmit_policy) {
4171 case BOND_XMIT_POLICY_LAYER23:
4172 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4173 break;
4174 case BOND_XMIT_POLICY_LAYER34:
4175 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4176 break;
4177 case BOND_XMIT_POLICY_LAYER2:
4178 default:
4179 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4180 break;
4181 }
4182}
4183
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004184/*
4185 * Lookup the slave that corresponds to a qid
4186 */
4187static inline int bond_slave_override(struct bonding *bond,
4188 struct sk_buff *skb)
4189{
4190 int i, res = 1;
4191 struct slave *slave = NULL;
4192 struct slave *check_slave;
4193
Michał Mirosław0693e882011-05-07 01:48:02 +00004194 if (!skb->queue_mapping)
4195 return 1;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004196
4197 /* Find out if any slaves have the same mapping as this skb. */
4198 bond_for_each_slave(bond, check_slave, i) {
4199 if (check_slave->queue_id == skb->queue_mapping) {
4200 slave = check_slave;
4201 break;
4202 }
4203 }
4204
4205 /* If the slave isn't UP, use default transmit policy. */
4206 if (slave && slave->queue_id && IS_UP(slave->dev) &&
4207 (slave->link == BOND_LINK_UP)) {
4208 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4209 }
4210
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004211 return res;
4212}
4213
Neil Horman374eeb52011-06-03 10:35:52 +00004214
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004215static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb)
4216{
4217 /*
4218 * This helper function exists to help dev_pick_tx get the correct
Phil Oesterfd0e4352011-03-14 06:22:04 +00004219 * destination queue. Using a helper function skips a call to
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004220 * skb_tx_hash and will put the skbs in the queue we expect on their
4221 * way down to the bonding driver.
4222 */
Phil Oesterfd0e4352011-03-14 06:22:04 +00004223 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
4224
Neil Horman374eeb52011-06-03 10:35:52 +00004225 /*
4226 * Save the original txq to restore before passing to the driver
4227 */
4228 bond_queue_mapping(skb) = skb->queue_mapping;
4229
Phil Oesterfd0e4352011-03-14 06:22:04 +00004230 if (unlikely(txq >= dev->real_num_tx_queues)) {
David Decotignyd30ee672011-04-13 15:22:29 +00004231 do {
Phil Oesterfd0e4352011-03-14 06:22:04 +00004232 txq -= dev->real_num_tx_queues;
David Decotignyd30ee672011-04-13 15:22:29 +00004233 } while (txq >= dev->real_num_tx_queues);
Phil Oesterfd0e4352011-03-14 06:22:04 +00004234 }
4235 return txq;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004236}
4237
Michał Mirosław0693e882011-05-07 01:48:02 +00004238static netdev_tx_t __bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
Stephen Hemminger00829822008-11-20 20:14:53 -08004239{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004240 struct bonding *bond = netdev_priv(dev);
4241
4242 if (TX_QUEUE_OVERRIDE(bond->params.mode)) {
4243 if (!bond_slave_override(bond, skb))
4244 return NETDEV_TX_OK;
4245 }
Stephen Hemminger00829822008-11-20 20:14:53 -08004246
4247 switch (bond->params.mode) {
4248 case BOND_MODE_ROUNDROBIN:
4249 return bond_xmit_roundrobin(skb, dev);
4250 case BOND_MODE_ACTIVEBACKUP:
4251 return bond_xmit_activebackup(skb, dev);
4252 case BOND_MODE_XOR:
4253 return bond_xmit_xor(skb, dev);
4254 case BOND_MODE_BROADCAST:
4255 return bond_xmit_broadcast(skb, dev);
4256 case BOND_MODE_8023AD:
4257 return bond_3ad_xmit_xor(skb, dev);
4258 case BOND_MODE_ALB:
4259 case BOND_MODE_TLB:
4260 return bond_alb_xmit(skb, dev);
4261 default:
4262 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004263 pr_err("%s: Error: Unknown bonding mode %d\n",
4264 dev->name, bond->params.mode);
Stephen Hemminger00829822008-11-20 20:14:53 -08004265 WARN_ON_ONCE(1);
4266 dev_kfree_skb(skb);
4267 return NETDEV_TX_OK;
4268 }
4269}
4270
Michał Mirosław0693e882011-05-07 01:48:02 +00004271static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
4272{
4273 struct bonding *bond = netdev_priv(dev);
4274 netdev_tx_t ret = NETDEV_TX_OK;
4275
4276 /*
4277 * If we risk deadlock from transmitting this in the
4278 * netpoll path, tell netpoll to queue the frame for later tx
4279 */
4280 if (is_netpoll_tx_blocked(dev))
4281 return NETDEV_TX_BUSY;
4282
4283 read_lock(&bond->lock);
4284
4285 if (bond->slave_cnt)
4286 ret = __bond_start_xmit(skb, dev);
4287 else
4288 dev_kfree_skb(skb);
4289
4290 read_unlock(&bond->lock);
4291
4292 return ret;
4293}
Stephen Hemminger00829822008-11-20 20:14:53 -08004294
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295/*
4296 * set bond mode specific net device operations
4297 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004298void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004300 struct net_device *bond_dev = bond->dev;
4301
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 switch (mode) {
4303 case BOND_MODE_ROUNDROBIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 break;
4305 case BOND_MODE_ACTIVEBACKUP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 break;
4307 case BOND_MODE_XOR:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004308 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 break;
4310 case BOND_MODE_BROADCAST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 break;
4312 case BOND_MODE_8023AD:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004313 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004316 /* FALLTHRU */
4317 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 break;
4319 default:
4320 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004321 pr_err("%s: Error: Unknown bonding mode %d\n",
4322 bond_dev->name, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 break;
4324 }
4325}
4326
Jay Vosburgh217df672005-09-26 16:11:50 -07004327static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4328 struct ethtool_drvinfo *drvinfo)
4329{
4330 strncpy(drvinfo->driver, DRV_NAME, 32);
4331 strncpy(drvinfo->version, DRV_VERSION, 32);
4332 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4333}
4334
Jeff Garzik7282d492006-09-13 14:30:00 -04004335static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004336 .get_drvinfo = bond_ethtool_get_drvinfo,
Stephen Hemmingerfa53eba2008-09-13 21:17:09 -04004337 .get_link = ethtool_op_get_link,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004338};
4339
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004340static const struct net_device_ops bond_netdev_ops = {
Stephen Hemminger181470f2009-06-12 19:02:52 +00004341 .ndo_init = bond_init,
Stephen Hemminger9e716262009-06-12 19:02:47 +00004342 .ndo_uninit = bond_uninit,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004343 .ndo_open = bond_open,
4344 .ndo_stop = bond_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08004345 .ndo_start_xmit = bond_start_xmit,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004346 .ndo_select_queue = bond_select_queue,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00004347 .ndo_get_stats64 = bond_get_stats,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004348 .ndo_do_ioctl = bond_do_ioctl,
4349 .ndo_set_multicast_list = bond_set_multicast_list,
4350 .ndo_change_mtu = bond_change_mtu,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004351 .ndo_set_mac_address = bond_set_mac_address,
Stephen Hemminger00829822008-11-20 20:14:53 -08004352 .ndo_neigh_setup = bond_neigh_setup,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004353 .ndo_vlan_rx_register = bond_vlan_rx_register,
4354 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4355 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
WANG Congf6dc31a2010-05-06 00:48:51 -07004356#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00004357 .ndo_netpoll_setup = bond_netpoll_setup,
WANG Congf6dc31a2010-05-06 00:48:51 -07004358 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
4359 .ndo_poll_controller = bond_poll_controller,
4360#endif
Jiri Pirko9232ecc2011-02-13 09:33:01 +00004361 .ndo_add_slave = bond_enslave,
4362 .ndo_del_slave = bond_release,
Michał Mirosławb2a103e2011-05-07 03:22:17 +00004363 .ndo_fix_features = bond_fix_features,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004364};
4365
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004366static void bond_destructor(struct net_device *bond_dev)
4367{
4368 struct bonding *bond = netdev_priv(bond_dev);
4369 if (bond->wq)
4370 destroy_workqueue(bond->wq);
4371 free_netdev(bond_dev);
4372}
4373
Stephen Hemminger181470f2009-06-12 19:02:52 +00004374static void bond_setup(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375{
Wang Chen454d7c92008-11-12 23:37:49 -08004376 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 /* initialize rwlocks */
4379 rwlock_init(&bond->lock);
4380 rwlock_init(&bond->curr_slave_lock);
4381
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00004382 bond->params = bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
4384 /* Initialize pointers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 bond->dev = bond_dev;
4386 INIT_LIST_HEAD(&bond->vlan_list);
4387
4388 /* Initialize the device entry points */
Stephen Hemminger181470f2009-06-12 19:02:52 +00004389 ether_setup(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004390 bond_dev->netdev_ops = &bond_netdev_ops;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004391 bond_dev->ethtool_ops = &bond_ethtool_ops;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004392 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004394 bond_dev->destructor = bond_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
4396 /* Initialize the device options */
4397 bond_dev->tx_queue_len = 0;
4398 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004399 bond_dev->priv_flags |= IFF_BONDING;
Stephen Hemminger181470f2009-06-12 19:02:52 +00004400 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
4401
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 /* At first, we block adding VLANs. That's the only way to
4403 * prevent problems that occur when adding VLANs over an
4404 * empty bond. The block will be removed once non-challenged
4405 * slaves are enslaved.
4406 */
4407 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4408
Herbert Xu932ff272006-06-09 12:20:56 -07004409 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 * transmitting */
4411 bond_dev->features |= NETIF_F_LLTX;
4412
4413 /* By default, we declare the bond to be fully
4414 * VLAN hardware accelerated capable. Special
4415 * care is taken in the various xmit functions
4416 * when there are slaves that are not hw accel
4417 * capable
4418 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
Michał Mirosławb2a103e2011-05-07 03:22:17 +00004420 bond_dev->hw_features = BOND_VLAN_FEATURES |
4421 NETIF_F_HW_VLAN_TX |
4422 NETIF_F_HW_VLAN_RX |
4423 NETIF_F_HW_VLAN_FILTER;
4424
4425 bond_dev->hw_features &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_NO_CSUM);
4426 bond_dev->features |= bond_dev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427}
4428
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004429static void bond_work_cancel_all(struct bonding *bond)
4430{
4431 write_lock_bh(&bond->lock);
4432 bond->kill_timers = 1;
4433 write_unlock_bh(&bond->lock);
4434
4435 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4436 cancel_delayed_work(&bond->mii_work);
4437
4438 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4439 cancel_delayed_work(&bond->arp_work);
4440
4441 if (bond->params.mode == BOND_MODE_ALB &&
4442 delayed_work_pending(&bond->alb_work))
4443 cancel_delayed_work(&bond->alb_work);
4444
4445 if (bond->params.mode == BOND_MODE_8023AD &&
4446 delayed_work_pending(&bond->ad_work))
4447 cancel_delayed_work(&bond->ad_work);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00004448
4449 if (delayed_work_pending(&bond->mcast_work))
4450 cancel_delayed_work(&bond->mcast_work);
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004451}
4452
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004453/*
4454* Destroy a bonding device.
4455* Must be under rtnl_lock when this function is called.
4456*/
4457static void bond_uninit(struct net_device *bond_dev)
Jay Vosburgha434e432008-10-30 17:41:15 -07004458{
Wang Chen454d7c92008-11-12 23:37:49 -08004459 struct bonding *bond = netdev_priv(bond_dev);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004460 struct vlan_entry *vlan, *tmp;
Jay Vosburgha434e432008-10-30 17:41:15 -07004461
WANG Congf6dc31a2010-05-06 00:48:51 -07004462 bond_netpoll_cleanup(bond_dev);
4463
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004464 /* Release the bonded slaves */
4465 bond_release_all(bond_dev);
4466
Jay Vosburgha434e432008-10-30 17:41:15 -07004467 list_del(&bond->bond_list);
4468
4469 bond_work_cancel_all(bond);
4470
Jay Vosburgha434e432008-10-30 17:41:15 -07004471 bond_remove_proc_entry(bond);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004472
Taku Izumif073c7c2010-12-09 15:17:13 +00004473 bond_debug_unregister(bond);
4474
Jiri Pirko22bedad2010-04-01 21:22:57 +00004475 __hw_addr_flush(&bond->mc_list);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004476
4477 list_for_each_entry_safe(vlan, tmp, &bond->vlan_list, vlan_list) {
4478 list_del(&vlan->vlan_list);
4479 kfree(vlan);
4480 }
Jay Vosburgha434e432008-10-30 17:41:15 -07004481}
4482
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483/*------------------------- Module initialization ---------------------------*/
4484
4485/*
4486 * Convert string input module parms. Accept either the
Jay Vosburghece95f72008-01-17 16:25:01 -08004487 * number of the mode or its string name. A bit complicated because
4488 * some mode names are substrings of other names, and calls from sysfs
4489 * may have whitespace in the name (trailing newlines, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 */
Holger Eitzenberger325dcf72008-12-09 23:10:17 -08004491int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492{
Hannes Eder54b87322009-02-14 11:15:49 +00004493 int modeint = -1, i, rv;
Jay Vosburgha42e5342008-01-29 18:07:43 -08004494 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
Jay Vosburghece95f72008-01-17 16:25:01 -08004495
Jay Vosburgha42e5342008-01-29 18:07:43 -08004496 for (p = (char *)buf; *p; p++)
4497 if (!(isdigit(*p) || isspace(*p)))
4498 break;
4499
4500 if (*p)
Jay Vosburghece95f72008-01-17 16:25:01 -08004501 rv = sscanf(buf, "%20s", modestr);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004502 else
Hannes Eder54b87322009-02-14 11:15:49 +00004503 rv = sscanf(buf, "%d", &modeint);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004504
4505 if (!rv)
4506 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
4508 for (i = 0; tbl[i].modename; i++) {
Hannes Eder54b87322009-02-14 11:15:49 +00004509 if (modeint == tbl[i].mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 return tbl[i].mode;
Jay Vosburghece95f72008-01-17 16:25:01 -08004511 if (strcmp(modestr, tbl[i].modename) == 0)
4512 return tbl[i].mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 }
4514
4515 return -1;
4516}
4517
4518static int bond_check_params(struct bond_params *params)
4519{
Jiri Pirkoa5499522009-09-25 03:28:09 +00004520 int arp_validate_value, fail_over_mac_value, primary_reselect_value;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004521
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 /*
4523 * Convert string parameters.
4524 */
4525 if (mode) {
4526 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4527 if (bond_mode == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004528 pr_err("Error: Invalid bonding mode \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 mode == NULL ? "NULL" : mode);
4530 return -EINVAL;
4531 }
4532 }
4533
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004534 if (xmit_hash_policy) {
4535 if ((bond_mode != BOND_MODE_XOR) &&
4536 (bond_mode != BOND_MODE_8023AD)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004537 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004538 bond_mode_name(bond_mode));
4539 } else {
4540 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4541 xmit_hashtype_tbl);
4542 if (xmit_hashtype == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004543 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004544 xmit_hash_policy == NULL ? "NULL" :
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004545 xmit_hash_policy);
4546 return -EINVAL;
4547 }
4548 }
4549 }
4550
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 if (lacp_rate) {
4552 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004553 pr_info("lacp_rate param is irrelevant in mode %s\n",
4554 bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 } else {
4556 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4557 if (lacp_fast == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004558 pr_err("Error: Invalid lacp rate \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 lacp_rate == NULL ? "NULL" : lacp_rate);
4560 return -EINVAL;
4561 }
4562 }
4563 }
4564
Jay Vosburghfd989c82008-11-04 17:51:16 -08004565 if (ad_select) {
4566 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4567 if (params->ad_select == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004568 pr_err("Error: Invalid ad_select \"%s\"\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -08004569 ad_select == NULL ? "NULL" : ad_select);
4570 return -EINVAL;
4571 }
4572
4573 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004574 pr_warning("ad_select param only affects 802.3ad mode\n");
Jay Vosburghfd989c82008-11-04 17:51:16 -08004575 }
4576 } else {
4577 params->ad_select = BOND_AD_STABLE;
4578 }
4579
Nicolas de Pesloüanf5841302009-08-28 13:18:34 +00004580 if (max_bonds < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004581 pr_warning("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4582 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 max_bonds = BOND_DEFAULT_MAX_BONDS;
4584 }
4585
4586 if (miimon < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004587 pr_warning("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to %d\n",
4588 miimon, INT_MAX, BOND_LINK_MON_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 miimon = BOND_LINK_MON_INTERV;
4590 }
4591
4592 if (updelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004593 pr_warning("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4594 updelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 updelay = 0;
4596 }
4597
4598 if (downdelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004599 pr_warning("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4600 downdelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 downdelay = 0;
4602 }
4603
4604 if ((use_carrier != 0) && (use_carrier != 1)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004605 pr_warning("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
4606 use_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 use_carrier = 1;
4608 }
4609
Ben Hutchingsad246c92011-04-26 15:25:52 +00004610 if (num_peer_notif < 0 || num_peer_notif > 255) {
4611 pr_warning("Warning: num_grat_arp/num_unsol_na (%d) not in range 0-255 so it was reset to 1\n",
4612 num_peer_notif);
4613 num_peer_notif = 1;
4614 }
4615
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 /* reset values for 802.3ad */
4617 if (bond_mode == BOND_MODE_8023AD) {
4618 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004619 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 +00004620 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 miimon = 100;
4622 }
4623 }
4624
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004625 if (tx_queues < 1 || tx_queues > 255) {
4626 pr_warning("Warning: tx_queues (%d) should be between "
4627 "1 and 255, resetting to %d\n",
4628 tx_queues, BOND_DEFAULT_TX_QUEUES);
4629 tx_queues = BOND_DEFAULT_TX_QUEUES;
4630 }
4631
Andy Gospodarekebd8e492010-06-02 08:39:21 +00004632 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
4633 pr_warning("Warning: all_slaves_active module parameter (%d), "
4634 "not of valid value (0/1), so it was set to "
4635 "0\n", all_slaves_active);
4636 all_slaves_active = 0;
4637 }
4638
Flavio Leitnerc2952c32010-10-05 14:23:59 +00004639 if (resend_igmp < 0 || resend_igmp > 255) {
4640 pr_warning("Warning: resend_igmp (%d) should be between "
4641 "0 and 255, resetting to %d\n",
4642 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
4643 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
4644 }
4645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 /* reset values for TLB/ALB */
4647 if ((bond_mode == BOND_MODE_TLB) ||
4648 (bond_mode == BOND_MODE_ALB)) {
4649 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004650 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 +00004651 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 miimon = 100;
4653 }
4654 }
4655
4656 if (bond_mode == BOND_MODE_ALB) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004657 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",
4658 updelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 }
4660
4661 if (!miimon) {
4662 if (updelay || downdelay) {
4663 /* just warn the user the up/down delay will have
4664 * no effect since miimon is zero...
4665 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004666 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",
4667 updelay, downdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 }
4669 } else {
4670 /* don't allow arp monitoring */
4671 if (arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004672 pr_warning("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
4673 miimon, arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 arp_interval = 0;
4675 }
4676
4677 if ((updelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004678 pr_warning("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
4679 updelay, miimon,
4680 (updelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 }
4682
4683 updelay /= miimon;
4684
4685 if ((downdelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004686 pr_warning("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
4687 downdelay, miimon,
4688 (downdelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 }
4690
4691 downdelay /= miimon;
4692 }
4693
4694 if (arp_interval < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004695 pr_warning("Warning: arp_interval module parameter (%d) , not in range 0-%d, so it was reset to %d\n",
4696 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 arp_interval = BOND_LINK_ARP_INTERV;
4698 }
4699
4700 for (arp_ip_count = 0;
4701 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4702 arp_ip_count++) {
4703 /* not complete check, but should be good enough to
4704 catch mistakes */
4705 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004706 pr_warning("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
4707 arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 arp_interval = 0;
4709 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04004710 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 arp_target[arp_ip_count] = ip;
4712 }
4713 }
4714
4715 if (arp_interval && !arp_ip_count) {
4716 /* don't allow arping if no arp_ip_target given... */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004717 pr_warning("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
4718 arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 arp_interval = 0;
4720 }
4721
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004722 if (arp_validate) {
4723 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004724 pr_err("arp_validate only supported in active-backup mode\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004725 return -EINVAL;
4726 }
4727 if (!arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004728 pr_err("arp_validate requires arp_interval\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004729 return -EINVAL;
4730 }
4731
4732 arp_validate_value = bond_parse_parm(arp_validate,
4733 arp_validate_tbl);
4734 if (arp_validate_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004735 pr_err("Error: invalid arp_validate \"%s\"\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004736 arp_validate == NULL ? "NULL" : arp_validate);
4737 return -EINVAL;
4738 }
4739 } else
4740 arp_validate_value = 0;
4741
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 if (miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004743 pr_info("MII link monitoring set to %d ms\n", miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 } else if (arp_interval) {
4745 int i;
4746
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004747 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
4748 arp_interval,
4749 arp_validate_tbl[arp_validate_value].modename,
4750 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
4752 for (i = 0; i < arp_ip_count; i++)
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00004753 pr_info(" %s", arp_ip_target[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00004755 pr_info("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756
Jay Vosburghb8a97872008-06-13 18:12:04 -07004757 } else if (max_bonds) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 /* miimon and arp_interval not set, we need one so things
4759 * work as expected, see bonding.txt for details
4760 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004761 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 -07004762 }
4763
4764 if (primary && !USES_PRIMARY(bond_mode)) {
4765 /* currently, using a primary only makes sense
4766 * in active backup, TLB or ALB modes
4767 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004768 pr_warning("Warning: %s primary device specified but has no effect in %s mode\n",
4769 primary, bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 primary = NULL;
4771 }
4772
Jiri Pirkoa5499522009-09-25 03:28:09 +00004773 if (primary && primary_reselect) {
4774 primary_reselect_value = bond_parse_parm(primary_reselect,
4775 pri_reselect_tbl);
4776 if (primary_reselect_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004777 pr_err("Error: Invalid primary_reselect \"%s\"\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00004778 primary_reselect ==
4779 NULL ? "NULL" : primary_reselect);
4780 return -EINVAL;
4781 }
4782 } else {
4783 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
4784 }
4785
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07004786 if (fail_over_mac) {
4787 fail_over_mac_value = bond_parse_parm(fail_over_mac,
4788 fail_over_mac_tbl);
4789 if (fail_over_mac_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004790 pr_err("Error: invalid fail_over_mac \"%s\"\n",
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07004791 arp_validate == NULL ? "NULL" : arp_validate);
4792 return -EINVAL;
4793 }
4794
4795 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004796 pr_warning("Warning: fail_over_mac only affects active-backup mode.\n");
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07004797 } else {
4798 fail_over_mac_value = BOND_FOM_NONE;
4799 }
Jay Vosburghdd957c52007-10-09 19:57:24 -07004800
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 /* fill params struct with the proper values */
4802 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004803 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 params->miimon = miimon;
Ben Hutchingsad246c92011-04-26 15:25:52 +00004805 params->num_peer_notif = num_peer_notif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004807 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 params->updelay = updelay;
4809 params->downdelay = downdelay;
4810 params->use_carrier = use_carrier;
4811 params->lacp_fast = lacp_fast;
4812 params->primary[0] = 0;
Jiri Pirkoa5499522009-09-25 03:28:09 +00004813 params->primary_reselect = primary_reselect_value;
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07004814 params->fail_over_mac = fail_over_mac_value;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004815 params->tx_queues = tx_queues;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00004816 params->all_slaves_active = all_slaves_active;
Flavio Leitnerc2952c32010-10-05 14:23:59 +00004817 params->resend_igmp = resend_igmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818
4819 if (primary) {
4820 strncpy(params->primary, primary, IFNAMSIZ);
4821 params->primary[IFNAMSIZ - 1] = 0;
4822 }
4823
4824 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4825
4826 return 0;
4827}
4828
Peter Zijlstra0daa2302006-11-08 19:51:01 -08004829static struct lock_class_key bonding_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -07004830static struct lock_class_key bonding_netdev_addr_lock_key;
Peter Zijlstra0daa2302006-11-08 19:51:01 -08004831
David S. Millere8a04642008-07-17 00:34:19 -07004832static void bond_set_lockdep_class_one(struct net_device *dev,
4833 struct netdev_queue *txq,
4834 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -07004835{
4836 lockdep_set_class(&txq->_xmit_lock,
4837 &bonding_netdev_xmit_lock_key);
4838}
4839
4840static void bond_set_lockdep_class(struct net_device *dev)
4841{
David S. Millercf508b12008-07-22 14:16:42 -07004842 lockdep_set_class(&dev->addr_list_lock,
4843 &bonding_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -07004844 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -07004845}
4846
Stephen Hemminger181470f2009-06-12 19:02:52 +00004847/*
4848 * Called from registration process
4849 */
4850static int bond_init(struct net_device *bond_dev)
4851{
4852 struct bonding *bond = netdev_priv(bond_dev);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004853 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Neil Horman9fe06172011-05-25 08:13:01 +00004854 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Stephen Hemminger181470f2009-06-12 19:02:52 +00004855
4856 pr_debug("Begin bond_init for %s\n", bond_dev->name);
4857
Neil Horman9fe06172011-05-25 08:13:01 +00004858 /*
4859 * Initialize locks that may be required during
4860 * en/deslave operations. All of the bond_open work
4861 * (of which this is part) should really be moved to
4862 * a phase prior to dev_open
4863 */
4864 spin_lock_init(&(bond_info->tx_hashtbl_lock));
4865 spin_lock_init(&(bond_info->rx_hashtbl_lock));
4866
Stephen Hemminger181470f2009-06-12 19:02:52 +00004867 bond->wq = create_singlethread_workqueue(bond_dev->name);
4868 if (!bond->wq)
4869 return -ENOMEM;
4870
4871 bond_set_lockdep_class(bond_dev);
4872
Stephen Hemminger181470f2009-06-12 19:02:52 +00004873 bond_create_proc_entry(bond);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004874 list_add_tail(&bond->bond_list, &bn->dev_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00004875
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00004876 bond_prepare_sysfs_group(bond);
Jiri Pirko22bedad2010-04-01 21:22:57 +00004877
Taku Izumif073c7c2010-12-09 15:17:13 +00004878 bond_debug_register(bond);
4879
Jiri Pirko22bedad2010-04-01 21:22:57 +00004880 __hw_addr_init(&bond->mc_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00004881 return 0;
4882}
4883
Eric W. Biederman88ead972009-10-29 14:18:25 +00004884static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
4885{
4886 if (tb[IFLA_ADDRESS]) {
4887 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
4888 return -EINVAL;
4889 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
4890 return -EADDRNOTAVAIL;
4891 }
4892 return 0;
4893}
4894
4895static struct rtnl_link_ops bond_link_ops __read_mostly = {
4896 .kind = "bond",
Eric W. Biederman66391042009-10-29 23:58:54 +00004897 .priv_size = sizeof(struct bonding),
Eric W. Biederman88ead972009-10-29 14:18:25 +00004898 .setup = bond_setup,
4899 .validate = bond_validate,
4900};
4901
Mitch Williamsdfe60392005-11-09 10:36:04 -08004902/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004903 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08004904 * Caller must NOT hold rtnl_lock; we need to release it here before we
4905 * set up our sysfs entries.
4906 */
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004907int bond_create(struct net *net, const char *name)
Mitch Williamsdfe60392005-11-09 10:36:04 -08004908{
4909 struct net_device *bond_dev;
4910 int res;
4911
4912 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08004913
Jiri Pirko1c5cae82011-04-30 01:21:32 +00004914 bond_dev = alloc_netdev_mq(sizeof(struct bonding),
4915 name ? name : "bond%d",
4916 bond_setup, tx_queues);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004917 if (!bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004918 pr_err("%s: eek! can't alloc netdev!\n", name);
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004919 rtnl_unlock();
4920 return -ENOMEM;
Mitch Williamsdfe60392005-11-09 10:36:04 -08004921 }
4922
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004923 dev_net_set(bond_dev, net);
Eric W. Biederman88ead972009-10-29 14:18:25 +00004924 bond_dev->rtnl_link_ops = &bond_link_ops;
4925
Mitch Williamsdfe60392005-11-09 10:36:04 -08004926 res = register_netdevice(bond_dev);
Peter Zijlstra0daa2302006-11-08 19:51:01 -08004927
Phil Oestere826eaf2011-03-14 06:22:05 +00004928 netif_carrier_off(bond_dev);
4929
Mitch Williamsdfe60392005-11-09 10:36:04 -08004930 rtnl_unlock();
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004931 if (res < 0)
4932 bond_destructor(bond_dev);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004933 return res;
4934}
4935
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004936static int __net_init bond_net_init(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004937{
Eric W. Biederman15449742009-11-29 15:46:04 +00004938 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004939
4940 bn->net = net;
4941 INIT_LIST_HEAD(&bn->dev_list);
4942
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004943 bond_create_proc_dir(bn);
Eric W. Biederman15449742009-11-29 15:46:04 +00004944
4945 return 0;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004946}
4947
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004948static void __net_exit bond_net_exit(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004949{
Eric W. Biederman15449742009-11-29 15:46:04 +00004950 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004951
4952 bond_destroy_proc_dir(bn);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004953}
4954
4955static struct pernet_operations bond_net_ops = {
4956 .init = bond_net_init,
4957 .exit = bond_net_exit,
Eric W. Biederman15449742009-11-29 15:46:04 +00004958 .id = &bond_net_id,
4959 .size = sizeof(struct bond_net),
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004960};
4961
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962static int __init bonding_init(void)
4963{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 int i;
4965 int res;
4966
Amerigo Wangbd33acc2011-03-06 21:58:46 +00004967 pr_info("%s", bond_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968
Mitch Williamsdfe60392005-11-09 10:36:04 -08004969 res = bond_check_params(&bonding_defaults);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004970 if (res)
Mitch Williamsdfe60392005-11-09 10:36:04 -08004971 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
Eric W. Biederman15449742009-11-29 15:46:04 +00004973 res = register_pernet_subsys(&bond_net_ops);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004974 if (res)
4975 goto out;
Jay Vosburgh027ea042008-01-17 16:25:02 -08004976
Eric W. Biederman88ead972009-10-29 14:18:25 +00004977 res = rtnl_link_register(&bond_link_ops);
4978 if (res)
Eric W. Biederman66391042009-10-29 23:58:54 +00004979 goto err_link;
Eric W. Biederman88ead972009-10-29 14:18:25 +00004980
Taku Izumif073c7c2010-12-09 15:17:13 +00004981 bond_create_debugfs();
4982
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 for (i = 0; i < max_bonds; i++) {
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004984 res = bond_create(&init_net, NULL);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004985 if (res)
4986 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 }
4988
Mitch Williamsb76cdba2005-11-09 10:36:41 -08004989 res = bond_create_sysfs();
4990 if (res)
4991 goto err;
4992
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04004994 register_inetaddr_notifier(&bond_inetaddr_notifier);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004995out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 return res;
Eric W. Biederman88ead972009-10-29 14:18:25 +00004997err:
4998 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman66391042009-10-29 23:58:54 +00004999err_link:
Eric W. Biederman15449742009-11-29 15:46:04 +00005000 unregister_pernet_subsys(&bond_net_ops);
Eric W. Biederman88ead972009-10-29 14:18:25 +00005001 goto out;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005002
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003}
5004
5005static void __exit bonding_exit(void)
5006{
5007 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005008 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005010 bond_destroy_sysfs();
Taku Izumif073c7c2010-12-09 15:17:13 +00005011 bond_destroy_debugfs();
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005012
Eric W. Biederman88ead972009-10-29 14:18:25 +00005013 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman15449742009-11-29 15:46:04 +00005014 unregister_pernet_subsys(&bond_net_ops);
Neil Hormane843fa52010-10-13 16:01:50 +00005015
5016#ifdef CONFIG_NET_POLL_CONTROLLER
Neil Hormanfb4fa762010-12-06 09:05:50 +00005017 /*
5018 * Make sure we don't have an imbalance on our netpoll blocking
5019 */
5020 WARN_ON(atomic_read(&netpoll_block_tx));
Neil Hormane843fa52010-10-13 16:01:50 +00005021#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022}
5023
5024module_init(bonding_init);
5025module_exit(bonding_exit);
5026MODULE_LICENSE("GPL");
5027MODULE_VERSION(DRV_VERSION);
5028MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5029MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
Eric W. Biederman88ead972009-10-29 14:18:25 +00005030MODULE_ALIAS_RTNL_LINK("bond");