blob: 2065cb4002b51b9ea87eaa4ec6d3e1b6f194d694 [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;
WANG Congcefa9992011-06-19 16:13:01 -07001300 strlcpy(np->dev_name, slave->dev->name, IFNAMSIZ);
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001301 err = __netpoll_setup(np);
1302 if (err) {
1303 kfree(np);
1304 goto out;
WANG Congf6dc31a2010-05-06 00:48:51 -07001305 }
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001306 slave->np = np;
1307out:
1308 return err;
1309}
1310static inline void slave_disable_netpoll(struct slave *slave)
1311{
1312 struct netpoll *np = slave->np;
1313
1314 if (!np)
1315 return;
1316
1317 slave->np = NULL;
1318 synchronize_rcu_bh();
1319 __netpoll_cleanup(np);
1320 kfree(np);
1321}
1322static inline bool slave_dev_support_netpoll(struct net_device *slave_dev)
1323{
1324 if (slave_dev->priv_flags & IFF_DISABLE_NETPOLL)
1325 return false;
1326 if (!slave_dev->netdev_ops->ndo_poll_controller)
1327 return false;
1328 return true;
WANG Congf6dc31a2010-05-06 00:48:51 -07001329}
1330
1331static void bond_poll_controller(struct net_device *bond_dev)
1332{
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001333}
1334
1335static void __bond_netpoll_cleanup(struct bonding *bond)
1336{
Neil Hormanc2355e12010-10-13 16:01:49 +00001337 struct slave *slave;
1338 int i;
1339
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001340 bond_for_each_slave(bond, slave, i)
1341 if (IS_UP(slave->dev))
1342 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07001343}
WANG Congf6dc31a2010-05-06 00:48:51 -07001344static void bond_netpoll_cleanup(struct net_device *bond_dev)
1345{
1346 struct bonding *bond = netdev_priv(bond_dev);
WANG Congf6dc31a2010-05-06 00:48:51 -07001347
1348 read_lock(&bond->lock);
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001349 __bond_netpoll_cleanup(bond);
WANG Congf6dc31a2010-05-06 00:48:51 -07001350 read_unlock(&bond->lock);
1351}
1352
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001353static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
1354{
1355 struct bonding *bond = netdev_priv(dev);
1356 struct slave *slave;
1357 int i, err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001358
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001359 read_lock(&bond->lock);
1360 bond_for_each_slave(bond, slave, i) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001361 err = slave_enable_netpoll(slave);
1362 if (err) {
1363 __bond_netpoll_cleanup(bond);
1364 break;
1365 }
1366 }
1367 read_unlock(&bond->lock);
1368 return err;
1369}
1370
1371static struct netpoll_info *bond_netpoll_info(struct bonding *bond)
1372{
1373 return bond->dev->npinfo;
1374}
1375
1376#else
1377static inline int slave_enable_netpoll(struct slave *slave)
1378{
1379 return 0;
1380}
1381static inline void slave_disable_netpoll(struct slave *slave)
1382{
1383}
WANG Congf6dc31a2010-05-06 00:48:51 -07001384static void bond_netpoll_cleanup(struct net_device *bond_dev)
1385{
1386}
WANG Congf6dc31a2010-05-06 00:48:51 -07001387#endif
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389/*---------------------------------- IOCTL ----------------------------------*/
1390
Adrian Bunk4ad072c2007-07-09 11:51:12 -07001391static int bond_sethwaddr(struct net_device *bond_dev,
1392 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001394 pr_debug("bond_dev=%p\n", bond_dev);
1395 pr_debug("slave_dev=%p\n", slave_dev);
1396 pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1398 return 0;
1399}
1400
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001401static u32 bond_fix_features(struct net_device *dev, u32 features)
1402{
1403 struct slave *slave;
1404 struct bonding *bond = netdev_priv(dev);
1405 u32 mask;
1406 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001407
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001408 read_lock(&bond->lock);
1409
1410 if (!bond->first_slave) {
1411 /* Disable adding VLANs to empty bond. But why? --mq */
1412 features |= NETIF_F_VLAN_CHALLENGED;
1413 goto out;
1414 }
1415
1416 mask = features;
1417 features &= ~NETIF_F_ONE_FOR_ALL;
1418 features |= NETIF_F_ALL_FOR_ALL;
1419
1420 bond_for_each_slave(bond, slave, i) {
1421 features = netdev_increment_features(features,
1422 slave->dev->features,
1423 mask);
1424 }
1425
1426out:
1427 read_unlock(&bond->lock);
1428 return features;
1429}
1430
Michał Mirosław62f2a3a2011-07-13 14:10:29 +00001431#define BOND_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
1432 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
1433 NETIF_F_HIGHDMA | NETIF_F_LRO)
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001434
1435static void bond_compute_features(struct bonding *bond)
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001436{
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001437 struct slave *slave;
1438 struct net_device *bond_dev = bond->dev;
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001439 u32 vlan_features = BOND_VLAN_FEATURES;
1440 unsigned short max_hard_header_len = ETH_HLEN;
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001441 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001442
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001443 read_lock(&bond->lock);
Herbert Xub63365a2008-10-23 01:11:29 -07001444
1445 if (!bond->first_slave)
1446 goto done;
1447
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001448 bond_for_each_slave(bond, slave, i) {
Jay Vosburgh278339a2009-08-28 12:05:12 +00001449 vlan_features = netdev_increment_features(vlan_features,
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001450 slave->dev->vlan_features, BOND_VLAN_FEATURES);
1451
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001452 if (slave->dev->hard_header_len > max_hard_header_len)
1453 max_hard_header_len = slave->dev->hard_header_len;
1454 }
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001455
Herbert Xub63365a2008-10-23 01:11:29 -07001456done:
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001457 bond_dev->vlan_features = vlan_features;
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001458 bond_dev->hard_header_len = max_hard_header_len;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001459
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001460 read_unlock(&bond->lock);
1461
1462 netdev_change_features(bond_dev);
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001463}
1464
Moni Shoua872254d2007-10-09 19:43:38 -07001465static void bond_setup_by_slave(struct net_device *bond_dev,
1466 struct net_device *slave_dev)
1467{
Wang Chen454d7c92008-11-12 23:37:49 -08001468 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07001469
Stephen Hemminger00829822008-11-20 20:14:53 -08001470 bond_dev->header_ops = slave_dev->header_ops;
Moni Shoua872254d2007-10-09 19:43:38 -07001471
1472 bond_dev->type = slave_dev->type;
1473 bond_dev->hard_header_len = slave_dev->hard_header_len;
1474 bond_dev->addr_len = slave_dev->addr_len;
1475
1476 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1477 slave_dev->addr_len);
Moni Shouad90a1622007-10-09 19:43:43 -07001478 bond->setup_by_slave = 1;
Moni Shoua872254d2007-10-09 19:43:38 -07001479}
1480
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001481/* On bonding slaves other than the currently active slave, suppress
Jiri Pirko3aba8912011-04-19 03:48:16 +00001482 * duplicates except for alb non-mcast/bcast.
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001483 */
1484static bool bond_should_deliver_exact_match(struct sk_buff *skb,
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001485 struct slave *slave,
1486 struct bonding *bond)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001487{
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001488 if (bond_is_slave_inactive(slave)) {
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001489 if (bond->params.mode == BOND_MODE_ALB &&
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001490 skb->pkt_type != PACKET_BROADCAST &&
1491 skb->pkt_type != PACKET_MULTICAST)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001492 return false;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001493 return true;
1494 }
1495 return false;
1496}
1497
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001498static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001499{
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001500 struct sk_buff *skb = *pskb;
Jiri Pirkof1c17752011-03-12 03:14:35 +00001501 struct slave *slave;
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001502 struct bonding *bond;
Mitsuo Hayasakae8c492b2011-10-12 16:04:29 +00001503 void (*recv_probe)(struct sk_buff *, struct bonding *,
1504 struct slave *);
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001505
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001506 skb = skb_share_check(skb, GFP_ATOMIC);
1507 if (unlikely(!skb))
1508 return RX_HANDLER_CONSUMED;
1509
1510 *pskb = skb;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001511
Jiri Pirko35d48902011-03-22 02:38:12 +00001512 slave = bond_slave_get_rcu(skb->dev);
1513 bond = slave->bond;
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001514
1515 if (bond->params.arp_interval)
Jiri Pirkof1c17752011-03-12 03:14:35 +00001516 slave->dev->last_rx = jiffies;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001517
Mitsuo Hayasakae8c492b2011-10-12 16:04:29 +00001518 recv_probe = ACCESS_ONCE(bond->recv_probe);
1519 if (recv_probe) {
Jiri Pirko3aba8912011-04-19 03:48:16 +00001520 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
1521
1522 if (likely(nskb)) {
Mitsuo Hayasakae8c492b2011-10-12 16:04:29 +00001523 recv_probe(nskb, bond, slave);
Jiri Pirko3aba8912011-04-19 03:48:16 +00001524 dev_kfree_skb(nskb);
1525 }
1526 }
1527
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001528 if (bond_should_deliver_exact_match(skb, slave, bond)) {
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001529 return RX_HANDLER_EXACT;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001530 }
1531
Jiri Pirko35d48902011-03-22 02:38:12 +00001532 skb->dev = bond->dev;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001533
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001534 if (bond->params.mode == BOND_MODE_ALB &&
Jiri Pirko35d48902011-03-22 02:38:12 +00001535 bond->dev->priv_flags & IFF_BRIDGE_PORT &&
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001536 skb->pkt_type == PACKET_HOST) {
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001537
Changli Gao541ac7c2011-03-02 21:07:14 +00001538 if (unlikely(skb_cow_head(skb,
1539 skb->data - skb_mac_header(skb)))) {
1540 kfree_skb(skb);
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001541 return RX_HANDLER_CONSUMED;
Changli Gao541ac7c2011-03-02 21:07:14 +00001542 }
Jiri Pirko35d48902011-03-22 02:38:12 +00001543 memcpy(eth_hdr(skb)->h_dest, bond->dev->dev_addr, ETH_ALEN);
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001544 }
1545
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001546 return RX_HANDLER_ANOTHER;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001547}
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549/* enslave device <slave> to bond device <master> */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001550int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Wang Chen454d7c92008-11-12 23:37:49 -08001552 struct bonding *bond = netdev_priv(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001553 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 struct slave *new_slave = NULL;
Jiri Pirko22bedad2010-04-01 21:22:57 +00001555 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 struct sockaddr addr;
1557 int link_reporting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 int res = 0;
1559
nsxfreddy@gmail.com552709d2005-09-21 14:18:04 -05001560 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001561 slave_ops->ndo_do_ioctl == NULL) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001562 pr_warning("%s: Warning: no link monitoring support for %s\n",
1563 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 /* already enslaved */
1567 if (slave_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001568 pr_debug("Error, Device was already enslaved\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return -EBUSY;
1570 }
1571
1572 /* vlan challenged mutual exclusion */
1573 /* no need to lock since we're protected by rtnl_lock */
1574 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001575 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Jay Vosburghf35188f2010-07-21 12:14:47 +00001576 if (bond->vlgrp) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001577 pr_err("%s: Error: cannot enslave VLAN challenged slave %s on VLAN enabled bond %s\n",
1578 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 return -EPERM;
1580 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001581 pr_warning("%s: Warning: enslaved VLAN challenged slave %s. Adding VLANs will be blocked as long as %s is part of bond %s\n",
1582 bond_dev->name, slave_dev->name,
1583 slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
1585 } else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001586 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
1588
Jay Vosburgh217df672005-09-26 16:11:50 -07001589 /*
1590 * Old ifenslave binaries are no longer supported. These can
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001591 * be identified with moderate accuracy by the state of the slave:
Jay Vosburgh217df672005-09-26 16:11:50 -07001592 * the current ifenslave will set the interface down prior to
1593 * enslaving it; the old ifenslave will not.
1594 */
1595 if ((slave_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001596 pr_err("%s is up. This may be due to an out of date ifenslave.\n",
Jay Vosburgh217df672005-09-26 16:11:50 -07001597 slave_dev->name);
1598 res = -EPERM;
1599 goto err_undo_flags;
1600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Moni Shoua872254d2007-10-09 19:43:38 -07001602 /* set bonding device ether type by slave - bonding netdevices are
1603 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1604 * there is a need to override some of the type dependent attribs/funcs.
1605 *
1606 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1607 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1608 */
1609 if (bond->slave_cnt == 0) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001610 if (bond_dev->type != slave_dev->type) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001611 pr_debug("%s: change device type from %d to %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001612 bond_dev->name,
1613 bond_dev->type, slave_dev->type);
Moni Shoua75c78502009-09-15 02:37:40 -07001614
Jiri Pirko3ca5b402010-03-10 10:29:35 +00001615 res = netdev_bonding_change(bond_dev,
1616 NETDEV_PRE_TYPE_CHANGE);
1617 res = notifier_to_errno(res);
1618 if (res) {
1619 pr_err("%s: refused to change device type\n",
1620 bond_dev->name);
1621 res = -EBUSY;
1622 goto err_undo_flags;
1623 }
Moni Shoua75c78502009-09-15 02:37:40 -07001624
Jiri Pirko32a806c2010-03-19 04:00:23 +00001625 /* Flush unicast and multicast addresses */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001626 dev_uc_flush(bond_dev);
Jiri Pirko22bedad2010-04-01 21:22:57 +00001627 dev_mc_flush(bond_dev);
Jiri Pirko32a806c2010-03-19 04:00:23 +00001628
Moni Shouae36b9d12009-07-15 04:56:31 +00001629 if (slave_dev->type != ARPHRD_ETHER)
1630 bond_setup_by_slave(bond_dev, slave_dev);
Neil Horman9cf81e72011-07-26 06:05:38 +00001631 else {
Moni Shouae36b9d12009-07-15 04:56:31 +00001632 ether_setup(bond_dev);
Neil Horman9cf81e72011-07-26 06:05:38 +00001633 bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1634 }
Moni Shoua75c78502009-09-15 02:37:40 -07001635
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001636 netdev_bonding_change(bond_dev,
1637 NETDEV_POST_TYPE_CHANGE);
Moni Shouae36b9d12009-07-15 04:56:31 +00001638 }
Moni Shoua872254d2007-10-09 19:43:38 -07001639 } else if (bond_dev->type != slave_dev->type) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001640 pr_err("%s ether type (%d) is different from other slaves (%d), can not enslave it.\n",
1641 slave_dev->name,
1642 slave_dev->type, bond_dev->type);
1643 res = -EINVAL;
1644 goto err_undo_flags;
Moni Shoua872254d2007-10-09 19:43:38 -07001645 }
1646
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001647 if (slave_ops->ndo_set_mac_address == NULL) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001648 if (bond->slave_cnt == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001649 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.",
1650 bond_dev->name);
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001651 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1652 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001653 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",
1654 bond_dev->name);
Moni Shoua2ab82852007-10-09 19:43:39 -07001655 res = -EOPNOTSUPP;
1656 goto err_undo_flags;
1657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
1659
Amerigo Wang8d8fc292011-05-19 21:39:10 +00001660 call_netdevice_notifiers(NETDEV_JOIN, slave_dev);
1661
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001662 /* If this is the first slave, then we need to set the master's hardware
1663 * address to be the same as the slave's. */
David Strandd13a2cb2010-12-01 11:43:08 -08001664 if (is_zero_ether_addr(bond->dev->dev_addr))
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001665 memcpy(bond->dev->dev_addr, slave_dev->dev_addr,
1666 slave_dev->addr_len);
1667
1668
Joe Jin243cb4e2007-02-06 14:16:40 -08001669 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (!new_slave) {
1671 res = -ENOMEM;
1672 goto err_undo_flags;
1673 }
1674
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001675 /*
1676 * Set the new_slave's queue_id to be zero. Queue ID mapping
1677 * is set via sysfs or module option if desired.
1678 */
1679 new_slave->queue_id = 0;
1680
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001681 /* Save slave's original mtu and then set it to match the bond */
1682 new_slave->original_mtu = slave_dev->mtu;
1683 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1684 if (res) {
1685 pr_debug("Error %d calling dev_set_mtu\n", res);
1686 goto err_free;
1687 }
1688
Jay Vosburgh217df672005-09-26 16:11:50 -07001689 /*
1690 * Save slave's original ("permanent") mac address for modes
1691 * that need it, and for restoring it upon release, and then
1692 * set it to the master's address
1693 */
1694 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Jay Vosburghdd957c52007-10-09 19:57:24 -07001696 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001697 /*
1698 * Set slave to master's mac address. The application already
1699 * set the master's mac address to that of the first slave
1700 */
1701 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1702 addr.sa_family = slave_dev->type;
1703 res = dev_set_mac_address(slave_dev, &addr);
1704 if (res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001705 pr_debug("Error %d calling set_mac_address\n", res);
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001706 goto err_restore_mtu;
Moni Shoua2ab82852007-10-09 19:43:39 -07001707 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Jiri Pirko1765a572011-02-12 06:48:36 +00001710 res = netdev_set_bond_master(slave_dev, bond_dev);
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001711 if (res) {
Jiri Pirko1765a572011-02-12 06:48:36 +00001712 pr_debug("Error %d calling netdev_set_bond_master\n", res);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001713 goto err_restore_mac;
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001714 }
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001715
Jay Vosburgh217df672005-09-26 16:11:50 -07001716 /* open the slave since the application closed it */
1717 res = dev_open(slave_dev);
1718 if (res) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001719 pr_debug("Opening slave %s failed\n", slave_dev->name);
Jiri Pirko35d48902011-03-22 02:38:12 +00001720 goto err_unset_master;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722
Jiri Pirko35d48902011-03-22 02:38:12 +00001723 new_slave->bond = bond;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 new_slave->dev = slave_dev;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001725 slave_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Holger Eitzenberger58402052008-12-09 23:07:13 -08001727 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 /* bond_alb_init_slave() must be called before all other stages since
1729 * it might fail and we do not want to have to undo everything
1730 */
1731 res = bond_alb_init_slave(bond, new_slave);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001732 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001733 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 }
1735
1736 /* If the mode USES_PRIMARY, then the new slave gets the
1737 * master's promisc (and mc) settings only if it becomes the
1738 * curr_active_slave, and that is taken care of later when calling
1739 * bond_change_active()
1740 */
1741 if (!USES_PRIMARY(bond->params.mode)) {
1742 /* set promiscuity level to new slave */
1743 if (bond_dev->flags & IFF_PROMISC) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001744 res = dev_set_promiscuity(slave_dev, 1);
1745 if (res)
1746 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
1748
1749 /* set allmulti level to new slave */
1750 if (bond_dev->flags & IFF_ALLMULTI) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001751 res = dev_set_allmulti(slave_dev, 1);
1752 if (res)
1753 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755
David S. Millerb9e40852008-07-15 00:15:08 -07001756 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 /* upload master's mc_list to new slave */
Jiri Pirko22bedad2010-04-01 21:22:57 +00001758 netdev_for_each_mc_addr(ha, bond_dev)
1759 dev_mc_add(slave_dev, ha->addr);
David S. Millerb9e40852008-07-15 00:15:08 -07001760 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
1762
1763 if (bond->params.mode == BOND_MODE_8023AD) {
1764 /* add lacpdu mc addr to mc list */
1765 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1766
Jiri Pirko22bedad2010-04-01 21:22:57 +00001767 dev_mc_add(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
1769
1770 bond_add_vlans_on_slave(bond, slave_dev);
1771
1772 write_lock_bh(&bond->lock);
1773
1774 bond_attach_slave(bond, new_slave);
1775
1776 new_slave->delay = 0;
1777 new_slave->link_failure_count = 0;
1778
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001779 write_unlock_bh(&bond->lock);
1780
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001781 bond_compute_features(bond);
1782
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001783 read_lock(&bond->lock);
1784
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001785 new_slave->last_arp_rx = jiffies;
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (bond->params.miimon && !bond->params.use_carrier) {
1788 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1789
1790 if ((link_reporting == -1) && !bond->params.arp_interval) {
1791 /*
1792 * miimon is set but a bonded network driver
1793 * does not support ETHTOOL/MII and
1794 * arp_interval is not set. Note: if
1795 * use_carrier is enabled, we will never go
1796 * here (because netif_carrier is always
1797 * supported); thus, we don't need to change
1798 * the messages for netif_carrier.
1799 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001800 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 -08001801 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 } else if (link_reporting == -1) {
1803 /* unable get link status using mii/ethtool */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001804 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",
1805 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
1807 }
1808
1809 /* check for initial state */
1810 if (!bond->params.miimon ||
1811 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1812 if (bond->params.updelay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001813 pr_debug("Initial state of slave_dev is BOND_LINK_BACK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 new_slave->link = BOND_LINK_BACK;
1815 new_slave->delay = bond->params.updelay;
1816 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001817 pr_debug("Initial state of slave_dev is BOND_LINK_UP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 new_slave->link = BOND_LINK_UP;
1819 }
1820 new_slave->jiffies = jiffies;
1821 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001822 pr_debug("Initial state of slave_dev is BOND_LINK_DOWN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 new_slave->link = BOND_LINK_DOWN;
1824 }
1825
1826 if (bond_update_speed_duplex(new_slave) &&
1827 (new_slave->link != BOND_LINK_DOWN)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001828 pr_warning("%s: Warning: failed to get speed and duplex from %s, assumed to be 100Mb/sec and Full.\n",
1829 bond_dev->name, new_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 if (bond->params.mode == BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001832 pr_warning("%s: Warning: Operation of 802.3ad mode requires ETHTOOL support in base driver for proper aggregator selection.\n",
1833 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
1835 }
1836
1837 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1838 /* if there is a primary slave, remember it */
Jiri Pirkoa5499522009-09-25 03:28:09 +00001839 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 bond->primary_slave = new_slave;
Jiri Pirkoa5499522009-09-25 03:28:09 +00001841 bond->force_primary = true;
1842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
1844
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001845 write_lock_bh(&bond->curr_slave_lock);
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 switch (bond->params.mode) {
1848 case BOND_MODE_ACTIVEBACKUP:
Jay Vosburgh8a8e4472006-09-22 21:56:15 -07001849 bond_set_slave_inactive_flags(new_slave);
1850 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 break;
1852 case BOND_MODE_8023AD:
1853 /* in 802.3ad mode, the internal mechanism
1854 * will activate the slaves in the selected
1855 * aggregator
1856 */
1857 bond_set_slave_inactive_flags(new_slave);
1858 /* if this is the first slave */
1859 if (bond->slave_cnt == 1) {
1860 SLAVE_AD_INFO(new_slave).id = 1;
1861 /* Initialize AD with the number of times that the AD timer is called in 1 second
1862 * can be called only after the mac address of the bond is set
1863 */
1864 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1865 bond->params.lacp_fast);
1866 } else {
1867 SLAVE_AD_INFO(new_slave).id =
1868 SLAVE_AD_INFO(new_slave->prev).id + 1;
1869 }
1870
1871 bond_3ad_bind_slave(new_slave);
1872 break;
1873 case BOND_MODE_TLB:
1874 case BOND_MODE_ALB:
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001875 bond_set_active_slave(new_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001876 bond_set_slave_inactive_flags(new_slave);
Jiri Pirko5a29f782009-03-25 17:23:38 -07001877 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 break;
1879 default:
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001880 pr_debug("This slave is always active in trunk mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 /* always active in trunk mode */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001883 bond_set_active_slave(new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 /* In trunking mode there is little meaning to curr_active_slave
1886 * anyway (it holds no special properties of the bond device),
1887 * so we can change it without calling change_active_interface()
1888 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001889 if (!bond->curr_active_slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 bond->curr_active_slave = new_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 break;
1893 } /* switch(bond_mode) */
1894
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001895 write_unlock_bh(&bond->curr_slave_lock);
1896
Jay Vosburghff59c452006-03-27 13:27:43 -08001897 bond_set_carrier(bond);
1898
WANG Congf6dc31a2010-05-06 00:48:51 -07001899#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001900 slave_dev->npinfo = bond_netpoll_info(bond);
1901 if (slave_dev->npinfo) {
1902 if (slave_enable_netpoll(new_slave)) {
1903 read_unlock(&bond->lock);
1904 pr_info("Error, %s: master_dev is using netpoll, "
1905 "but new slave device does not support netpoll.\n",
1906 bond_dev->name);
1907 res = -EBUSY;
1908 goto err_close;
1909 }
WANG Congf6dc31a2010-05-06 00:48:51 -07001910 }
1911#endif
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001912
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001913 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001915 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1916 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001917 goto err_close;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001918
Jiri Pirko35d48902011-03-22 02:38:12 +00001919 res = netdev_rx_handler_register(slave_dev, bond_handle_frame,
1920 new_slave);
1921 if (res) {
1922 pr_debug("Error %d calling netdev_rx_handler_register\n", res);
1923 goto err_dest_symlinks;
1924 }
1925
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001926 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
1927 bond_dev->name, slave_dev->name,
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001928 bond_is_active_slave(new_slave) ? "n active" : " backup",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001929 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 /* enslave is successful */
1932 return 0;
1933
1934/* Undo stages on error */
Jiri Pirko35d48902011-03-22 02:38:12 +00001935err_dest_symlinks:
1936 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938err_close:
1939 dev_close(slave_dev);
1940
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001941err_unset_master:
Jiri Pirko1765a572011-02-12 06:48:36 +00001942 netdev_set_bond_master(slave_dev, NULL);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944err_restore_mac:
Jay Vosburghdd957c52007-10-09 19:57:24 -07001945 if (!bond->params.fail_over_mac) {
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001946 /* XXX TODO - fom follow mode needs to change master's
1947 * MAC if this slave's MAC is in use by the bond, or at
1948 * least print a warning.
1949 */
Moni Shoua2ab82852007-10-09 19:43:39 -07001950 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1951 addr.sa_family = slave_dev->type;
1952 dev_set_mac_address(slave_dev, &addr);
1953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001955err_restore_mtu:
1956 dev_set_mtu(slave_dev, new_slave->original_mtu);
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958err_free:
1959 kfree(new_slave);
1960
1961err_undo_flags:
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001962 bond_compute_features(bond);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 return res;
1965}
1966
1967/*
1968 * Try to release the slave device <slave> from the bond device <master>
1969 * It is legal to access curr_active_slave without a lock because all the function
1970 * is write-locked.
1971 *
1972 * The rules for slave state should be:
1973 * for Active/Backup:
1974 * Active stays on all backups go down
1975 * for Bonded connections:
1976 * The first up interface should be left on and all others downed.
1977 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001978int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
Wang Chen454d7c92008-11-12 23:37:49 -08001980 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 struct slave *slave, *oldcurrent;
1982 struct sockaddr addr;
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001983 u32 old_features = bond_dev->features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 /* slave is not a slave or master is not master of this slave */
1986 if (!(slave_dev->flags & IFF_SLAVE) ||
1987 (slave_dev->master != bond_dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001988 pr_err("%s: Error: cannot release %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 bond_dev->name, slave_dev->name);
1990 return -EINVAL;
1991 }
1992
Neil Hormane843fa52010-10-13 16:01:50 +00001993 block_netpoll_tx();
Amerigo Wangdaf92092011-05-19 21:39:12 +00001994 netdev_bonding_change(bond_dev, NETDEV_RELEASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 write_lock_bh(&bond->lock);
1996
1997 slave = bond_get_slave_by_dev(bond, slave_dev);
1998 if (!slave) {
1999 /* not a slave of this bond */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002000 pr_info("%s: %s not enslaved\n",
2001 bond_dev->name, slave_dev->name);
Jay Vosburghf5e2a7b2006-02-07 21:17:22 -08002002 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002003 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return -EINVAL;
2005 }
2006
Jiri Pirko35d48902011-03-22 02:38:12 +00002007 /* unregister rx_handler early so bond_handle_frame wouldn't be called
2008 * for this slave anymore.
2009 */
2010 netdev_rx_handler_unregister(slave_dev);
2011 write_unlock_bh(&bond->lock);
2012 synchronize_net();
2013 write_lock_bh(&bond->lock);
2014
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07002015 if (!bond->params.fail_over_mac) {
Joe Perches8e95a202009-12-03 07:58:21 +00002016 if (!compare_ether_addr(bond_dev->dev_addr, slave->perm_hwaddr) &&
2017 bond->slave_cnt > 1)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002018 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",
2019 bond_dev->name, slave_dev->name,
2020 slave->perm_hwaddr,
2021 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
2023
2024 /* Inform AD package of unbinding of slave. */
2025 if (bond->params.mode == BOND_MODE_8023AD) {
2026 /* must be called before the slave is
2027 * detached from the list
2028 */
2029 bond_3ad_unbind_slave(slave);
2030 }
2031
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002032 pr_info("%s: releasing %s interface %s\n",
2033 bond_dev->name,
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002034 bond_is_active_slave(slave) ? "active" : "backup",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002035 slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 oldcurrent = bond->curr_active_slave;
2038
2039 bond->current_arp_slave = NULL;
2040
2041 /* release the slave from its bond */
2042 bond_detach_slave(bond, slave);
2043
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002044 if (bond->primary_slave == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 bond->primary_slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002047 if (oldcurrent == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 bond_change_active_slave(bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Holger Eitzenberger58402052008-12-09 23:07:13 -08002050 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 /* Must be called only after the slave has been
2052 * detached from the list and the curr_active_slave
2053 * has been cleared (if our_slave == old_current),
2054 * but before a new active slave is selected.
2055 */
Jay Vosburgh25433312008-01-17 16:24:59 -08002056 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 bond_alb_deinit_slave(bond, slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08002058 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
2060
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002061 if (oldcurrent == slave) {
2062 /*
2063 * Note that we hold RTNL over this sequence, so there
2064 * is no concern that another slave add/remove event
2065 * will interfere.
2066 */
2067 write_unlock_bh(&bond->lock);
2068 read_lock(&bond->lock);
2069 write_lock_bh(&bond->curr_slave_lock);
2070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 bond_select_active_slave(bond);
2072
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002073 write_unlock_bh(&bond->curr_slave_lock);
2074 read_unlock(&bond->lock);
2075 write_lock_bh(&bond->lock);
2076 }
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (bond->slave_cnt == 0) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002079 bond_set_carrier(bond);
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 /* if the last slave was removed, zero the mac address
2082 * of the master so it will be set by the application
2083 * to the mac address of the first slave
2084 */
2085 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2086
Michał Mirosławb2a103e2011-05-07 03:22:17 +00002087 if (bond->vlgrp) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002088 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2089 bond_dev->name, bond_dev->name);
2090 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2091 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
2094
2095 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002096 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Michał Mirosławb2a103e2011-05-07 03:22:17 +00002098 bond_compute_features(bond);
2099 if (!(bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2100 (old_features & NETIF_F_VLAN_CHALLENGED))
2101 pr_info("%s: last VLAN challenged slave %s left bond %s. VLAN blocking is removed\n",
2102 bond_dev->name, slave_dev->name, bond_dev->name);
2103
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002104 /* must do this from outside any spinlocks */
2105 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 bond_del_vlans_from_slave(bond, slave_dev);
2108
2109 /* If the mode USES_PRIMARY, then we should only remove its
2110 * promisc and mc settings if it was the curr_active_slave, but that was
2111 * already taken care of above when we detached the slave
2112 */
2113 if (!USES_PRIMARY(bond->params.mode)) {
2114 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002115 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002119 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002123 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002125 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 }
2127
Jiri Pirko1765a572011-02-12 06:48:36 +00002128 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002130 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07002131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 /* close slave before restoring its mac address */
2133 dev_close(slave_dev);
2134
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07002135 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002136 /* restore original ("permanent") mac address */
2137 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2138 addr.sa_family = slave_dev->type;
2139 dev_set_mac_address(slave_dev, &addr);
2140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00002142 dev_set_mtu(slave_dev, slave->original_mtu);
2143
Jiri Pirko2d7011c2011-03-16 08:46:43 +00002144 slave_dev->priv_flags &= ~IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 kfree(slave);
2147
2148 return 0; /* deletion OK */
2149}
2150
2151/*
Nicolas de Pesloüandadaa102011-03-19 13:36:18 -07002152* First release a slave and then destroy the bond if no more slaves are left.
Moni Shouad90a1622007-10-09 19:43:43 -07002153* Must be under rtnl_lock when this function is called.
2154*/
stephen hemminger26d8ee72010-10-15 05:09:34 +00002155static int bond_release_and_destroy(struct net_device *bond_dev,
2156 struct net_device *slave_dev)
Moni Shouad90a1622007-10-09 19:43:43 -07002157{
Wang Chen454d7c92008-11-12 23:37:49 -08002158 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002159 int ret;
2160
2161 ret = bond_release(bond_dev, slave_dev);
2162 if ((ret == 0) && (bond->slave_cnt == 0)) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002163 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002164 pr_info("%s: destroying bond %s.\n",
2165 bond_dev->name, bond_dev->name);
Stephen Hemminger9e716262009-06-12 19:02:47 +00002166 unregister_netdevice(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002167 }
2168 return ret;
2169}
2170
2171/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 * This function releases all slaves.
2173 */
2174static int bond_release_all(struct net_device *bond_dev)
2175{
Wang Chen454d7c92008-11-12 23:37:49 -08002176 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 struct slave *slave;
2178 struct net_device *slave_dev;
2179 struct sockaddr addr;
2180
2181 write_lock_bh(&bond->lock);
2182
Jay Vosburghff59c452006-03-27 13:27:43 -08002183 netif_carrier_off(bond_dev);
2184
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002185 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 bond->current_arp_slave = NULL;
2189 bond->primary_slave = NULL;
2190 bond_change_active_slave(bond, NULL);
2191
2192 while ((slave = bond->first_slave) != NULL) {
2193 /* Inform AD package of unbinding of slave
2194 * before slave is detached from the list.
2195 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002196 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 bond_3ad_unbind_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 slave_dev = slave->dev;
2200 bond_detach_slave(bond, slave);
2201
Jay Vosburgh25433312008-01-17 16:24:59 -08002202 /* now that the slave is detached, unlock and perform
2203 * all the undo steps that should not be called from
2204 * within a lock.
2205 */
2206 write_unlock_bh(&bond->lock);
2207
Jiri Pirko35d48902011-03-22 02:38:12 +00002208 /* unregister rx_handler early so bond_handle_frame wouldn't
2209 * be called for this slave anymore.
2210 */
2211 netdev_rx_handler_unregister(slave_dev);
2212 synchronize_net();
2213
Holger Eitzenberger58402052008-12-09 23:07:13 -08002214 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 /* must be called only after the slave
2216 * has been detached from the list
2217 */
2218 bond_alb_deinit_slave(bond, slave);
2219 }
2220
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002221 bond_destroy_slave_symlinks(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 bond_del_vlans_from_slave(bond, slave_dev);
2223
2224 /* If the mode USES_PRIMARY, then we should only remove its
2225 * promisc and mc settings if it was the curr_active_slave, but that was
2226 * already taken care of above when we detached the slave
2227 */
2228 if (!USES_PRIMARY(bond->params.mode)) {
2229 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002230 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002234 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002238 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002240 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
2242
Jiri Pirko1765a572011-02-12 06:48:36 +00002243 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002245 slave_disable_netpoll(slave);
2246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 /* close slave before restoring its mac address */
2248 dev_close(slave_dev);
2249
Jay Vosburghdd957c52007-10-09 19:57:24 -07002250 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002251 /* restore original ("permanent") mac address*/
2252 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2253 addr.sa_family = slave_dev->type;
2254 dev_set_mac_address(slave_dev, &addr);
2255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 kfree(slave);
2258
2259 /* re-acquire the lock before getting the next slave */
2260 write_lock_bh(&bond->lock);
2261 }
2262
2263 /* zero the mac address of the master so it will be
2264 * set by the application to the mac address of the
2265 * first slave
2266 */
2267 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2268
Michał Mirosławb2a103e2011-05-07 03:22:17 +00002269 if (bond->vlgrp) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002270 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2271 bond_dev->name, bond_dev->name);
2272 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2273 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
2275
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002276 pr_info("%s: released all slaves\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278out:
2279 write_unlock_bh(&bond->lock);
Michał Mirosławb2a103e2011-05-07 03:22:17 +00002280
2281 bond_compute_features(bond);
2282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 return 0;
2284}
2285
2286/*
2287 * This function changes the active slave to slave <slave_dev>.
2288 * It returns -EINVAL in the following cases.
2289 * - <slave_dev> is not found in the list.
2290 * - There is not active slave now.
2291 * - <slave_dev> is already active.
2292 * - The link state of <slave_dev> is not BOND_LINK_UP.
2293 * - <slave_dev> is not running.
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002294 * In these cases, this function does nothing.
2295 * In the other cases, current_slave pointer is changed and 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 */
2297static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2298{
Wang Chen454d7c92008-11-12 23:37:49 -08002299 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 struct slave *old_active = NULL;
2301 struct slave *new_active = NULL;
2302 int res = 0;
2303
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002304 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307 /* Verify that master_dev is indeed the master of slave_dev */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002308 if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002311 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002313 read_lock(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 old_active = bond->curr_active_slave;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002315 read_unlock(&bond->curr_slave_lock);
2316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 new_active = bond_get_slave_by_dev(bond, slave_dev);
2318
2319 /*
2320 * Changing to the current active: do nothing; return success.
2321 */
2322 if (new_active && (new_active == old_active)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002323 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 return 0;
2325 }
2326
2327 if ((new_active) &&
2328 (old_active) &&
2329 (new_active->link == BOND_LINK_UP) &&
2330 IS_UP(new_active->dev)) {
Neil Hormane843fa52010-10-13 16:01:50 +00002331 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002332 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 bond_change_active_slave(bond, new_active);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002334 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002335 unblock_netpoll_tx();
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002336 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002339 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341 return res;
2342}
2343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2345{
Wang Chen454d7c92008-11-12 23:37:49 -08002346 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
2348 info->bond_mode = bond->params.mode;
2349 info->miimon = bond->params.miimon;
2350
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002351 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 info->num_slaves = bond->slave_cnt;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002353 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
2355 return 0;
2356}
2357
2358static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2359{
Wang Chen454d7c92008-11-12 23:37:49 -08002360 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 struct slave *slave;
Eric Dumazet689c96c2009-04-23 03:39:04 +00002362 int i, res = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002364 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366 bond_for_each_slave(bond, slave, i) {
2367 if (i == (int)info->slave_id) {
Eric Dumazet689c96c2009-04-23 03:39:04 +00002368 res = 0;
2369 strcpy(info->slave_name, slave->dev->name);
2370 info->link = slave->link;
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002371 info->state = bond_slave_state(slave);
Eric Dumazet689c96c2009-04-23 03:39:04 +00002372 info->link_failure_count = slave->link_failure_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 break;
2374 }
2375 }
2376
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002377 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Eric Dumazet689c96c2009-04-23 03:39:04 +00002379 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380}
2381
2382/*-------------------------------- Monitoring -------------------------------*/
2383
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002384
2385static int bond_miimon_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386{
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002387 struct slave *slave;
2388 int i, link_state, commit = 0;
Jiri Pirko41f89102009-04-24 03:57:29 +00002389 bool ignore_updelay;
2390
2391 ignore_updelay = !bond->curr_active_slave ? true : false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393 bond_for_each_slave(bond, slave, i) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002394 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002396 link_state = bond_check_dev_link(bond, slave->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
2398 switch (slave->link) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002399 case BOND_LINK_UP:
2400 if (link_state)
2401 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002403 slave->link = BOND_LINK_FAIL;
2404 slave->delay = bond->params.downdelay;
2405 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002406 pr_info("%s: link status down for %sinterface %s, disabling it in %d ms.\n",
2407 bond->dev->name,
2408 (bond->params.mode ==
2409 BOND_MODE_ACTIVEBACKUP) ?
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002410 (bond_is_active_slave(slave) ?
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002411 "active " : "backup ") : "",
2412 slave->dev->name,
2413 bond->params.downdelay * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002415 /*FALLTHRU*/
2416 case BOND_LINK_FAIL:
2417 if (link_state) {
2418 /*
2419 * recovered before downdelay expired
2420 */
2421 slave->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 slave->jiffies = jiffies;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002423 pr_info("%s: link status up again after %d ms for interface %s.\n",
2424 bond->dev->name,
2425 (bond->params.downdelay - slave->delay) *
2426 bond->params.miimon,
2427 slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002428 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002430
2431 if (slave->delay <= 0) {
2432 slave->new_link = BOND_LINK_DOWN;
2433 commit++;
2434 continue;
2435 }
2436
2437 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002440 case BOND_LINK_DOWN:
2441 if (!link_state)
2442 continue;
2443
2444 slave->link = BOND_LINK_BACK;
2445 slave->delay = bond->params.updelay;
2446
2447 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002448 pr_info("%s: link status up for interface %s, enabling it in %d ms.\n",
2449 bond->dev->name, slave->dev->name,
2450 ignore_updelay ? 0 :
2451 bond->params.updelay *
2452 bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002454 /*FALLTHRU*/
2455 case BOND_LINK_BACK:
2456 if (!link_state) {
2457 slave->link = BOND_LINK_DOWN;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002458 pr_info("%s: link status down again after %d ms for interface %s.\n",
2459 bond->dev->name,
2460 (bond->params.updelay - slave->delay) *
2461 bond->params.miimon,
2462 slave->dev->name);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002463
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002464 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002466
Jiri Pirko41f89102009-04-24 03:57:29 +00002467 if (ignore_updelay)
2468 slave->delay = 0;
2469
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002470 if (slave->delay <= 0) {
2471 slave->new_link = BOND_LINK_UP;
2472 commit++;
Jiri Pirko41f89102009-04-24 03:57:29 +00002473 ignore_updelay = false;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002474 continue;
2475 }
2476
2477 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 break;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002479 }
2480 }
2481
2482 return commit;
2483}
2484
2485static void bond_miimon_commit(struct bonding *bond)
2486{
2487 struct slave *slave;
2488 int i;
2489
2490 bond_for_each_slave(bond, slave, i) {
2491 switch (slave->new_link) {
2492 case BOND_LINK_NOCHANGE:
2493 continue;
2494
2495 case BOND_LINK_UP:
2496 slave->link = BOND_LINK_UP;
2497 slave->jiffies = jiffies;
2498
2499 if (bond->params.mode == BOND_MODE_8023AD) {
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 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2503 /* make it immediately active */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002504 bond_set_active_slave(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002505 } else if (slave != bond->primary_slave) {
2506 /* prevent it from being the active one */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002507 bond_set_backup_slave(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002508 }
2509
Krzysztof Piotr Oledzki546add72010-10-06 14:28:22 -07002510 bond_update_speed_duplex(slave);
2511
David Decotigny5d305302011-04-13 15:22:31 +00002512 pr_info("%s: link status definitely up for interface %s, %u Mbps %s duplex.\n",
Krzysztof Piotr Oledzki700c2a72010-10-06 14:25:06 -07002513 bond->dev->name, slave->dev->name,
2514 slave->speed, slave->duplex ? "full" : "half");
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002515
2516 /* notify ad that the link status has changed */
2517 if (bond->params.mode == BOND_MODE_8023AD)
2518 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2519
Holger Eitzenberger58402052008-12-09 23:07:13 -08002520 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002521 bond_alb_handle_link_change(bond, slave,
2522 BOND_LINK_UP);
2523
2524 if (!bond->curr_active_slave ||
2525 (slave == bond->primary_slave))
2526 goto do_failover;
2527
2528 continue;
2529
2530 case BOND_LINK_DOWN:
Jay Vosburghfba4acd2008-10-30 17:41:14 -07002531 if (slave->link_failure_count < UINT_MAX)
2532 slave->link_failure_count++;
2533
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002534 slave->link = BOND_LINK_DOWN;
2535
2536 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2537 bond->params.mode == BOND_MODE_8023AD)
2538 bond_set_slave_inactive_flags(slave);
2539
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002540 pr_info("%s: link status definitely down for interface %s, disabling it\n",
2541 bond->dev->name, slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002542
2543 if (bond->params.mode == BOND_MODE_8023AD)
2544 bond_3ad_handle_link_change(slave,
2545 BOND_LINK_DOWN);
2546
Jiri Pirkoae63e802009-05-27 05:42:36 +00002547 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002548 bond_alb_handle_link_change(bond, slave,
2549 BOND_LINK_DOWN);
2550
2551 if (slave == bond->curr_active_slave)
2552 goto do_failover;
2553
2554 continue;
2555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002557 pr_err("%s: invalid new link %d on slave %s\n",
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002558 bond->dev->name, slave->new_link,
2559 slave->dev->name);
2560 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002562 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 }
2564
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002565do_failover:
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002566 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00002567 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002568 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 bond_select_active_slave(bond);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002570 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002571 unblock_netpoll_tx();
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002572 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002573
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002574 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
2576
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002577/*
2578 * bond_mii_monitor
2579 *
2580 * Really a wrapper that splits the mii monitor into two phases: an
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002581 * inspection, then (if inspection indicates something needs to be done)
2582 * an acquisition of appropriate locks followed by a commit phase to
2583 * implement whatever link state changes are indicated.
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002584 */
2585void bond_mii_monitor(struct work_struct *work)
2586{
2587 struct bonding *bond = container_of(work, struct bonding,
2588 mii_work.work);
Ben Hutchingsad246c92011-04-26 15:25:52 +00002589 bool should_notify_peers = false;
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002590
2591 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002592 if (bond->kill_timers)
2593 goto out;
2594
2595 if (bond->slave_cnt == 0)
2596 goto re_arm;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002597
Ben Hutchingsad246c92011-04-26 15:25:52 +00002598 should_notify_peers = bond_should_notify_peers(bond);
2599
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002600 if (bond_miimon_inspect(bond)) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002601 read_unlock(&bond->lock);
2602 rtnl_lock();
2603 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002604
2605 bond_miimon_commit(bond);
2606
Jay Vosburgh56556622008-01-17 16:25:03 -08002607 read_unlock(&bond->lock);
2608 rtnl_unlock(); /* might sleep, hold no other locks */
2609 read_lock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002610 }
2611
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002612re_arm:
2613 if (bond->params.miimon)
2614 queue_delayed_work(bond->wq, &bond->mii_work,
2615 msecs_to_jiffies(bond->params.miimon));
2616out:
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002617 read_unlock(&bond->lock);
Ben Hutchingsad246c92011-04-26 15:25:52 +00002618
2619 if (should_notify_peers) {
2620 rtnl_lock();
2621 netdev_bonding_change(bond->dev, NETDEV_NOTIFY_PEERS);
2622 rtnl_unlock();
2623 }
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002624}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002625
Al Virod3bb52b2007-08-22 20:06:58 -04002626static __be32 bond_glean_dev_ip(struct net_device *dev)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002627{
2628 struct in_device *idev;
2629 struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07002630 __be32 addr = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002631
2632 if (!dev)
2633 return 0;
2634
2635 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002636 idev = __in_dev_get_rcu(dev);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002637 if (!idev)
2638 goto out;
2639
2640 ifa = idev->ifa_list;
2641 if (!ifa)
2642 goto out;
2643
2644 addr = ifa->ifa_local;
2645out:
2646 rcu_read_unlock();
2647 return addr;
2648}
2649
Al Virod3bb52b2007-08-22 20:06:58 -04002650static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002651{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002652 struct vlan_entry *vlan;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002653
2654 if (ip == bond->master_ip)
2655 return 1;
2656
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002657 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002658 if (ip == vlan->vlan_ip)
2659 return 1;
2660 }
2661
2662 return 0;
2663}
2664
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002665/*
2666 * We go to the (large) trouble of VLAN tagging ARP frames because
2667 * switches in VLAN mode (especially if ports are configured as
2668 * "native" to a VLAN) might not pass non-tagged frames.
2669 */
Al Virod3bb52b2007-08-22 20:06:58 -04002670static 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 -04002671{
2672 struct sk_buff *skb;
2673
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002674 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002675 slave_dev->name, dest_ip, src_ip, vlan_id);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002676
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002677 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2678 NULL, slave_dev->dev_addr, NULL);
2679
2680 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002681 pr_err("ARP packet allocation failed\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002682 return;
2683 }
2684 if (vlan_id) {
2685 skb = vlan_put_tag(skb, vlan_id);
2686 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002687 pr_err("failed to insert VLAN tag\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002688 return;
2689 }
2690 }
2691 arp_xmit(skb);
2692}
2693
2694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2696{
David S. Millerb23dd4f2011-03-02 14:31:35 -08002697 int i, vlan_id;
Al Virod3bb52b2007-08-22 20:06:58 -04002698 __be32 *targets = bond->params.arp_targets;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002699 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002700 struct net_device *vlan_dev;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002701 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
Mitch Williams6b780562005-11-09 10:36:19 -08002703 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2704 if (!targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07002705 break;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002706 pr_debug("basa: target %x\n", targets[i]);
Jay Vosburghf35188f2010-07-21 12:14:47 +00002707 if (!bond->vlgrp) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002708 pr_debug("basa: empty vlan: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002709 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2710 bond->master_ip, 0);
2711 continue;
2712 }
2713
2714 /*
2715 * If VLANs are configured, we do a route lookup to
2716 * determine which VLAN interface would be used, so we
2717 * can tag the ARP with the proper VLAN tag.
2718 */
David S. Miller78fbfd82011-03-12 00:00:52 -05002719 rt = ip_route_output(dev_net(bond->dev), targets[i], 0,
2720 RTO_ONLINK, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08002721 if (IS_ERR(rt)) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002722 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002723 pr_warning("%s: no route to arp_ip_target %pI4\n",
David S. Miller78fbfd82011-03-12 00:00:52 -05002724 bond->dev->name, &targets[i]);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002725 }
2726 continue;
2727 }
2728
2729 /*
2730 * This target is not on a VLAN
2731 */
Changli Gaod8d1f302010-06-10 23:31:35 -07002732 if (rt->dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002733 ip_rt_put(rt);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002734 pr_debug("basa: rtdev == bond->dev: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002735 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2736 bond->master_ip, 0);
2737 continue;
2738 }
2739
2740 vlan_id = 0;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002741 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002742 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Changli Gaod8d1f302010-06-10 23:31:35 -07002743 if (vlan_dev == rt->dst.dev) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002744 vlan_id = vlan->vlan_id;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002745 pr_debug("basa: vlan match on %s %d\n",
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002746 vlan_dev->name, vlan_id);
2747 break;
2748 }
2749 }
2750
2751 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002752 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002753 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2754 vlan->vlan_ip, vlan_id);
2755 continue;
2756 }
2757
2758 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002759 pr_warning("%s: no path to arp_ip_target %pI4 via rt.dev %s\n",
David S. Miller78fbfd82011-03-12 00:00:52 -05002760 bond->dev->name, &targets[i],
Changli Gaod8d1f302010-06-10 23:31:35 -07002761 rt->dst.dev ? rt->dst.dev->name : "NULL");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002762 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002763 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002764 }
2765}
2766
Al Virod3bb52b2007-08-22 20:06:58 -04002767static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002768{
2769 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002770 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002771
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002772 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002773 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002774 &sip, &tip, i, &targets[i],
2775 bond_has_this_ip(bond, tip));
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002776 if (sip == targets[i]) {
2777 if (bond_has_this_ip(bond, tip))
2778 slave->last_arp_rx = jiffies;
2779 return;
2780 }
2781 }
2782}
2783
Jiri Pirko3aba8912011-04-19 03:48:16 +00002784static void bond_arp_rcv(struct sk_buff *skb, struct bonding *bond,
2785 struct slave *slave)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002786{
2787 struct arphdr *arp;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002788 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002789 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002790
Jiri Pirko3aba8912011-04-19 03:48:16 +00002791 if (skb->protocol != __cpu_to_be16(ETH_P_ARP))
2792 return;
Andy Gospodarek1f3c8802009-12-14 10:48:58 +00002793
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002794 read_lock(&bond->lock);
2795
Jiri Pirko3aba8912011-04-19 03:48:16 +00002796 pr_debug("bond_arp_rcv: bond %s skb->dev %s\n",
2797 bond->dev->name, skb->dev->name);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002798
Jiri Pirko3aba8912011-04-19 03:48:16 +00002799 if (!pskb_may_pull(skb, arp_hdr_len(bond->dev)))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002800 goto out_unlock;
2801
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002802 arp = arp_hdr(skb);
Jiri Pirko3aba8912011-04-19 03:48:16 +00002803 if (arp->ar_hln != bond->dev->addr_len ||
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002804 skb->pkt_type == PACKET_OTHERHOST ||
2805 skb->pkt_type == PACKET_LOOPBACK ||
2806 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2807 arp->ar_pro != htons(ETH_P_IP) ||
2808 arp->ar_pln != 4)
2809 goto out_unlock;
2810
2811 arp_ptr = (unsigned char *)(arp + 1);
Jiri Pirko3aba8912011-04-19 03:48:16 +00002812 arp_ptr += bond->dev->addr_len;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002813 memcpy(&sip, arp_ptr, 4);
Jiri Pirko3aba8912011-04-19 03:48:16 +00002814 arp_ptr += 4 + bond->dev->addr_len;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002815 memcpy(&tip, arp_ptr, 4);
2816
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002817 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002818 bond->dev->name, slave->dev->name, bond_slave_state(slave),
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002819 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2820 &sip, &tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002821
2822 /*
2823 * Backup slaves won't see the ARP reply, but do come through
2824 * here for each ARP probe (so we swap the sip/tip to validate
2825 * the probe). In a "redundant switch, common router" type of
2826 * configuration, the ARP probe will (hopefully) travel from
2827 * the active, through one switch, the router, then the other
2828 * switch before reaching the backup.
2829 */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002830 if (bond_is_active_slave(slave))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002831 bond_validate_arp(bond, slave, sip, tip);
2832 else
2833 bond_validate_arp(bond, slave, tip, sip);
2834
2835out_unlock:
2836 read_unlock(&bond->lock);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002837}
2838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839/*
2840 * this function is called regularly to monitor each slave's link
2841 * ensuring that traffic is being sent and received when arp monitoring
2842 * is used in load-balancing mode. if the adapter has been dormant, then an
2843 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2844 * arp monitoring in active backup mode.
2845 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002846void bond_loadbalance_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002848 struct bonding *bond = container_of(work, struct bonding,
2849 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 struct slave *slave, *oldcurrent;
2851 int do_failover = 0;
2852 int delta_in_ticks;
2853 int i;
2854
2855 read_lock(&bond->lock);
2856
Jay Vosburgh5ce0da82008-05-17 21:10:07 -07002857 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002859 if (bond->kill_timers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002862 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
2865 read_lock(&bond->curr_slave_lock);
2866 oldcurrent = bond->curr_active_slave;
2867 read_unlock(&bond->curr_slave_lock);
2868
2869 /* see if any of the previous devices are up now (i.e. they have
2870 * xmt and rcv traffic). the curr_active_slave does not come into
2871 * the picture unless it is null. also, slave->jiffies is not needed
2872 * here because we send an arp on each slave and give a slave as
2873 * long as it needs to get the tx/rx within the delta.
2874 * TODO: what about up/down delay in arp mode? it wasn't here before
2875 * so it can wait
2876 */
2877 bond_for_each_slave(bond, slave, i) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002878 unsigned long trans_start = dev_trans_start(slave->dev);
2879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002881 if (time_in_range(jiffies,
2882 trans_start - delta_in_ticks,
2883 trans_start + delta_in_ticks) &&
2884 time_in_range(jiffies,
2885 slave->dev->last_rx - delta_in_ticks,
2886 slave->dev->last_rx + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
2888 slave->link = BOND_LINK_UP;
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002889 bond_set_active_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
2891 /* primary_slave has no meaning in round-robin
2892 * mode. the window of a slave being up and
2893 * curr_active_slave being null after enslaving
2894 * is closed.
2895 */
2896 if (!oldcurrent) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002897 pr_info("%s: link status definitely up for interface %s, ",
2898 bond->dev->name,
2899 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 do_failover = 1;
2901 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002902 pr_info("%s: interface %s is now up\n",
2903 bond->dev->name,
2904 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 }
2906 }
2907 } else {
2908 /* slave->link == BOND_LINK_UP */
2909
2910 /* not all switches will respond to an arp request
2911 * when the source ip is 0, so don't take the link down
2912 * if we don't know our ip yet
2913 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002914 if (!time_in_range(jiffies,
2915 trans_start - delta_in_ticks,
2916 trans_start + 2 * delta_in_ticks) ||
2917 !time_in_range(jiffies,
2918 slave->dev->last_rx - delta_in_ticks,
2919 slave->dev->last_rx + 2 * delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
2921 slave->link = BOND_LINK_DOWN;
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002922 bond_set_backup_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002924 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002927 pr_info("%s: interface %s is now down.\n",
2928 bond->dev->name,
2929 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002931 if (slave == oldcurrent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 do_failover = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 }
2934 }
2935
2936 /* note: if switch is in round-robin mode, all links
2937 * must tx arp to ensure all links rx an arp - otherwise
2938 * links may oscillate or not come up at all; if switch is
2939 * in something like xor mode, there is nothing we can
2940 * do - all replies will be rx'ed on same link causing slaves
2941 * to be unstable during low/no traffic periods
2942 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002943 if (IS_UP(slave->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 bond_arp_send_all(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 }
2946
2947 if (do_failover) {
Neil Hormane843fa52010-10-13 16:01:50 +00002948 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002949 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
2951 bond_select_active_slave(bond);
2952
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002953 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002954 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 }
2956
2957re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002958 if (bond->params.arp_interval)
2959 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960out:
2961 read_unlock(&bond->lock);
2962}
2963
2964/*
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002965 * Called to inspect slaves for active-backup mode ARP monitor link state
2966 * changes. Sets new_link in slaves to specify what action should take
2967 * place for the slave. Returns 0 if no changes are found, >0 if changes
2968 * to link states must be committed.
2969 *
2970 * Called with bond->lock held for read.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002972static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 struct slave *slave;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002975 int i, commit = 0;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002976 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 bond_for_each_slave(bond, slave, i) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002979 slave->new_link = BOND_LINK_NOCHANGE;
2980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002982 if (time_in_range(jiffies,
2983 slave_last_rx(bond, slave) - delta_in_ticks,
2984 slave_last_rx(bond, slave) + delta_in_ticks)) {
2985
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002986 slave->new_link = BOND_LINK_UP;
2987 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002990 continue;
2991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002993 /*
2994 * Give slaves 2*delta after being enslaved or made
2995 * active. This avoids bouncing, as the last receive
2996 * times need a full ARP monitor cycle to be updated.
2997 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002998 if (time_in_range(jiffies,
2999 slave->jiffies - delta_in_ticks,
3000 slave->jiffies + 2 * delta_in_ticks))
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003001 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003003 /*
3004 * Backup slave is down if:
3005 * - No current_arp_slave AND
3006 * - more than 3*delta since last receive AND
3007 * - the bond has an IP address
3008 *
3009 * Note: a non-null current_arp_slave indicates
3010 * the curr_active_slave went down and we are
3011 * searching for a new one; under this condition
3012 * we only take the curr_active_slave down - this
3013 * gives each slave a chance to tx/rx traffic
3014 * before being taken out
3015 */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00003016 if (!bond_is_active_slave(slave) &&
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003017 !bond->current_arp_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003018 !time_in_range(jiffies,
3019 slave_last_rx(bond, slave) - delta_in_ticks,
3020 slave_last_rx(bond, slave) + 3 * delta_in_ticks)) {
3021
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003022 slave->new_link = BOND_LINK_DOWN;
3023 commit++;
3024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003026 /*
3027 * Active slave is down if:
3028 * - more than 2*delta since transmitting OR
3029 * - (more than 2*delta since receive AND
3030 * the bond has an IP address)
3031 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003032 trans_start = dev_trans_start(slave->dev);
Jiri Pirkoe30bc062011-03-12 03:14:37 +00003033 if (bond_is_active_slave(slave) &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003034 (!time_in_range(jiffies,
3035 trans_start - delta_in_ticks,
3036 trans_start + 2 * delta_in_ticks) ||
3037 !time_in_range(jiffies,
3038 slave_last_rx(bond, slave) - delta_in_ticks,
3039 slave_last_rx(bond, slave) + 2 * delta_in_ticks))) {
3040
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003041 slave->new_link = BOND_LINK_DOWN;
3042 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 }
3044 }
3045
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003046 return commit;
3047}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003049/*
3050 * Called to commit link state changes noted by inspection step of
3051 * active-backup mode ARP monitor.
3052 *
3053 * Called with RTNL and bond->lock for read.
3054 */
3055static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
3056{
3057 struct slave *slave;
3058 int i;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003059 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003061 bond_for_each_slave(bond, slave, i) {
3062 switch (slave->new_link) {
3063 case BOND_LINK_NOCHANGE:
3064 continue;
3065
3066 case BOND_LINK_UP:
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003067 trans_start = dev_trans_start(slave->dev);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003068 if ((!bond->curr_active_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003069 time_in_range(jiffies,
3070 trans_start - delta_in_ticks,
3071 trans_start + delta_in_ticks)) ||
Jiri Pirkob9f60252009-08-31 11:09:38 +00003072 bond->curr_active_slave != slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003073 slave->link = BOND_LINK_UP;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003074 bond->current_arp_slave = NULL;
3075
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003076 pr_info("%s: link status definitely up for interface %s.\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003077 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003078
Jiri Pirkob9f60252009-08-31 11:09:38 +00003079 if (!bond->curr_active_slave ||
3080 (slave == bond->primary_slave))
3081 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003082
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003083 }
3084
Jiri Pirkob9f60252009-08-31 11:09:38 +00003085 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003086
3087 case BOND_LINK_DOWN:
3088 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003091 slave->link = BOND_LINK_DOWN;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003092 bond_set_slave_inactive_flags(slave);
3093
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003094 pr_info("%s: link status definitely down for interface %s, disabling it\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003095 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003096
3097 if (slave == bond->curr_active_slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003098 bond->current_arp_slave = NULL;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003099 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003100 }
Jiri Pirkob9f60252009-08-31 11:09:38 +00003101
3102 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003103
3104 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003105 pr_err("%s: impossible: new_link %d on slave %s\n",
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003106 bond->dev->name, slave->new_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 slave->dev->name);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003108 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
Jiri Pirkob9f60252009-08-31 11:09:38 +00003111do_failover:
3112 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00003113 block_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003114 write_lock_bh(&bond->curr_slave_lock);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003115 bond_select_active_slave(bond);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003116 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00003117 unblock_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003118 }
3119
3120 bond_set_carrier(bond);
3121}
3122
3123/*
3124 * Send ARP probes for active-backup mode ARP monitor.
3125 *
3126 * Called with bond->lock held for read.
3127 */
3128static void bond_ab_arp_probe(struct bonding *bond)
3129{
3130 struct slave *slave;
3131 int i;
3132
3133 read_lock(&bond->curr_slave_lock);
3134
3135 if (bond->current_arp_slave && bond->curr_active_slave)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003136 pr_info("PROBE: c_arp %s && cas %s BAD\n",
3137 bond->current_arp_slave->dev->name,
3138 bond->curr_active_slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003139
3140 if (bond->curr_active_slave) {
3141 bond_arp_send_all(bond, bond->curr_active_slave);
3142 read_unlock(&bond->curr_slave_lock);
3143 return;
3144 }
3145
3146 read_unlock(&bond->curr_slave_lock);
3147
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 /* if we don't have a curr_active_slave, search for the next available
3149 * backup slave from the current_arp_slave and make it the candidate
3150 * for becoming the curr_active_slave
3151 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003152
3153 if (!bond->current_arp_slave) {
3154 bond->current_arp_slave = bond->first_slave;
3155 if (!bond->current_arp_slave)
3156 return;
3157 }
3158
3159 bond_set_slave_inactive_flags(bond->current_arp_slave);
3160
3161 /* search for next candidate */
3162 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3163 if (IS_UP(slave->dev)) {
3164 slave->link = BOND_LINK_BACK;
3165 bond_set_slave_active_flags(slave);
3166 bond_arp_send_all(bond, slave);
3167 slave->jiffies = jiffies;
3168 bond->current_arp_slave = slave;
3169 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 }
3171
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003172 /* if the link state is up at this point, we
3173 * mark it down - this can happen if we have
3174 * simultaneous link failures and
3175 * reselect_active_interface doesn't make this
3176 * one the current slave so it is still marked
3177 * up when it is actually down
3178 */
3179 if (slave->link == BOND_LINK_UP) {
3180 slave->link = BOND_LINK_DOWN;
3181 if (slave->link_failure_count < UINT_MAX)
3182 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003184 bond_set_slave_inactive_flags(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003186 pr_info("%s: backup interface %s is now down.\n",
3187 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 }
3189 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003190}
3191
3192void bond_activebackup_arp_mon(struct work_struct *work)
3193{
3194 struct bonding *bond = container_of(work, struct bonding,
3195 arp_work.work);
Ben Hutchingsad246c92011-04-26 15:25:52 +00003196 bool should_notify_peers = false;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003197 int delta_in_ticks;
3198
3199 read_lock(&bond->lock);
3200
3201 if (bond->kill_timers)
3202 goto out;
3203
3204 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3205
3206 if (bond->slave_cnt == 0)
3207 goto re_arm;
3208
Ben Hutchingsad246c92011-04-26 15:25:52 +00003209 should_notify_peers = bond_should_notify_peers(bond);
3210
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003211 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3212 read_unlock(&bond->lock);
3213 rtnl_lock();
3214 read_lock(&bond->lock);
3215
3216 bond_ab_arp_commit(bond, delta_in_ticks);
3217
3218 read_unlock(&bond->lock);
3219 rtnl_unlock();
3220 read_lock(&bond->lock);
3221 }
3222
3223 bond_ab_arp_probe(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
3225re_arm:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003226 if (bond->params.arp_interval)
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003227 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228out:
3229 read_unlock(&bond->lock);
Ben Hutchingsad246c92011-04-26 15:25:52 +00003230
3231 if (should_notify_peers) {
3232 rtnl_lock();
3233 netdev_bonding_change(bond->dev, NETDEV_NOTIFY_PEERS);
3234 rtnl_unlock();
3235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236}
3237
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238/*-------------------------- netdev event handling --------------------------*/
3239
3240/*
3241 * Change device name
3242 */
3243static int bond_event_changename(struct bonding *bond)
3244{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 bond_remove_proc_entry(bond);
3246 bond_create_proc_entry(bond);
Stephen Hemminger7e083842009-06-12 19:02:46 +00003247
Taku Izumif073c7c2010-12-09 15:17:13 +00003248 bond_debug_reregister(bond);
3249
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 return NOTIFY_DONE;
3251}
3252
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003253static int bond_master_netdev_event(unsigned long event,
3254 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255{
Wang Chen454d7c92008-11-12 23:37:49 -08003256 struct bonding *event_bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
3258 switch (event) {
3259 case NETDEV_CHANGENAME:
3260 return bond_event_changename(event_bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 default:
3262 break;
3263 }
3264
3265 return NOTIFY_DONE;
3266}
3267
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003268static int bond_slave_netdev_event(unsigned long event,
3269 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270{
3271 struct net_device *bond_dev = slave_dev->master;
Wang Chen454d7c92008-11-12 23:37:49 -08003272 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
3274 switch (event) {
3275 case NETDEV_UNREGISTER:
3276 if (bond_dev) {
Jay Vosburgh1284cd32007-10-15 16:44:27 -07003277 if (bond->setup_by_slave)
3278 bond_release_and_destroy(bond_dev, slave_dev);
3279 else
3280 bond_release(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 }
3282 break;
3283 case NETDEV_CHANGE:
Jay Vosburgh17d04502009-03-18 18:38:25 -07003284 if (bond->params.mode == BOND_MODE_8023AD || bond_is_lb(bond)) {
3285 struct slave *slave;
3286
3287 slave = bond_get_slave_by_dev(bond, slave_dev);
3288 if (slave) {
David Decotigny5d305302011-04-13 15:22:31 +00003289 u32 old_speed = slave->speed;
David Decotigny65cce192011-04-13 15:22:30 +00003290 u8 old_duplex = slave->duplex;
Jay Vosburgh17d04502009-03-18 18:38:25 -07003291
3292 bond_update_speed_duplex(slave);
3293
3294 if (bond_is_lb(bond))
3295 break;
3296
3297 if (old_speed != slave->speed)
3298 bond_3ad_adapter_speed_changed(slave);
3299 if (old_duplex != slave->duplex)
3300 bond_3ad_adapter_duplex_changed(slave);
3301 }
3302 }
3303
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 break;
3305 case NETDEV_DOWN:
3306 /*
3307 * ... Or is it this?
3308 */
3309 break;
3310 case NETDEV_CHANGEMTU:
3311 /*
3312 * TODO: Should slaves be allowed to
3313 * independently alter their MTU? For
3314 * an active-backup bond, slaves need
3315 * not be the same type of device, so
3316 * MTUs may vary. For other modes,
3317 * slaves arguably should have the
3318 * same MTUs. To do this, we'd need to
3319 * take over the slave's change_mtu
3320 * function for the duration of their
3321 * servitude.
3322 */
3323 break;
3324 case NETDEV_CHANGENAME:
3325 /*
3326 * TODO: handle changing the primary's name
3327 */
3328 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003329 case NETDEV_FEAT_CHANGE:
3330 bond_compute_features(bond);
3331 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 default:
3333 break;
3334 }
3335
3336 return NOTIFY_DONE;
3337}
3338
3339/*
3340 * bond_netdev_event: handle netdev notifier chain events.
3341 *
3342 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003343 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 * locks for us to safely manipulate the slave devices (RTNL lock,
3345 * dev_probe_lock).
3346 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003347static int bond_netdev_event(struct notifier_block *this,
3348 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349{
3350 struct net_device *event_dev = (struct net_device *)ptr;
3351
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003352 pr_debug("event_dev: %s, event: %lx\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003353 event_dev ? event_dev->name : "None",
3354 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003356 if (!(event_dev->priv_flags & IFF_BONDING))
3357 return NOTIFY_DONE;
3358
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 if (event_dev->flags & IFF_MASTER) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003360 pr_debug("IFF_MASTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 return bond_master_netdev_event(event, event_dev);
3362 }
3363
3364 if (event_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003365 pr_debug("IFF_SLAVE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 return bond_slave_netdev_event(event, event_dev);
3367 }
3368
3369 return NOTIFY_DONE;
3370}
3371
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003372/*
3373 * bond_inetaddr_event: handle inetaddr notifier chain events.
3374 *
3375 * We keep track of device IPs primarily to use as source addresses in
3376 * ARP monitor probes (rather than spewing out broadcasts all the time).
3377 *
3378 * We track one IP for the main device (if it has one), plus one per VLAN.
3379 */
3380static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3381{
3382 struct in_ifaddr *ifa = ptr;
3383 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003384 struct bond_net *bn = net_generic(dev_net(event_dev), bond_net_id);
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003385 struct bonding *bond;
3386 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003387
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003388 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003389 if (bond->dev == event_dev) {
3390 switch (event) {
3391 case NETDEV_UP:
3392 bond->master_ip = ifa->ifa_local;
3393 return NOTIFY_OK;
3394 case NETDEV_DOWN:
3395 bond->master_ip = bond_glean_dev_ip(bond->dev);
3396 return NOTIFY_OK;
3397 default:
3398 return NOTIFY_DONE;
3399 }
3400 }
3401
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003402 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +00003403 if (!bond->vlgrp)
3404 continue;
Dan Aloni5c15bde2007-03-02 20:44:51 -08003405 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003406 if (vlan_dev == event_dev) {
3407 switch (event) {
3408 case NETDEV_UP:
3409 vlan->vlan_ip = ifa->ifa_local;
3410 return NOTIFY_OK;
3411 case NETDEV_DOWN:
3412 vlan->vlan_ip =
3413 bond_glean_dev_ip(vlan_dev);
3414 return NOTIFY_OK;
3415 default:
3416 return NOTIFY_DONE;
3417 }
3418 }
3419 }
3420 }
3421 return NOTIFY_DONE;
3422}
3423
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424static struct notifier_block bond_netdev_notifier = {
3425 .notifier_call = bond_netdev_event,
3426};
3427
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003428static struct notifier_block bond_inetaddr_notifier = {
3429 .notifier_call = bond_inetaddr_event,
3430};
3431
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003432/*---------------------------- Hashing Policies -----------------------------*/
3433
3434/*
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003435 * Hash for the output device based upon layer 2 and layer 3 data. If
3436 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3437 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003438static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003439{
3440 struct ethhdr *data = (struct ethhdr *)skb->data;
3441 struct iphdr *iph = ip_hdr(skb);
3442
Brian Haleyf14c4e42008-09-02 10:08:08 -04003443 if (skb->protocol == htons(ETH_P_IP)) {
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003444 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
Jasper Spaansd3da6832009-10-23 04:08:46 +00003445 (data->h_dest[5] ^ data->h_source[5])) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003446 }
3447
Jasper Spaansd3da6832009-10-23 04:08:46 +00003448 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003449}
3450
3451/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003452 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003453 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3454 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3455 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003456static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003457{
3458 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003459 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003460 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003461 int layer4_xor = 0;
3462
Brian Haleyf14c4e42008-09-02 10:08:08 -04003463 if (skb->protocol == htons(ETH_P_IP)) {
3464 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003465 (iph->protocol == IPPROTO_TCP ||
3466 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003467 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003468 }
3469 return (layer4_xor ^
3470 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3471
3472 }
3473
Jasper Spaansd3da6832009-10-23 04:08:46 +00003474 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003475}
3476
3477/*
3478 * Hash for the output device based upon layer 2 data
3479 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003480static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003481{
3482 struct ethhdr *data = (struct ethhdr *)skb->data;
3483
Jasper Spaansd3da6832009-10-23 04:08:46 +00003484 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003485}
3486
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487/*-------------------------- Device entry points ----------------------------*/
3488
3489static int bond_open(struct net_device *bond_dev)
3490{
Wang Chen454d7c92008-11-12 23:37:49 -08003491 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
3493 bond->kill_timers = 0;
3494
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003495 INIT_DELAYED_WORK(&bond->mcast_work, bond_resend_igmp_join_requests_delayed);
3496
Holger Eitzenberger58402052008-12-09 23:07:13 -08003497 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 /* bond_alb_initialize must be called before the timer
3499 * is started.
3500 */
3501 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3502 /* something went wrong - fail the open operation */
stephen hemmingerb4739462010-01-25 23:34:15 +00003503 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 }
3505
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003506 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3507 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 }
3509
3510 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003511 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3512 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 }
3514
3515 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003516 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3517 INIT_DELAYED_WORK(&bond->arp_work,
3518 bond_activebackup_arp_mon);
3519 else
3520 INIT_DELAYED_WORK(&bond->arp_work,
3521 bond_loadbalance_arp_mon);
3522
3523 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003524 if (bond->params.arp_validate)
Jiri Pirko3aba8912011-04-19 03:48:16 +00003525 bond->recv_probe = bond_arp_rcv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 }
3527
3528 if (bond->params.mode == BOND_MODE_8023AD) {
Adrian Bunka40745f2007-10-24 18:27:43 +02003529 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003530 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 /* register to receive LACPDUs */
Jiri Pirko3aba8912011-04-19 03:48:16 +00003532 bond->recv_probe = bond_3ad_lacpdu_recv;
Jay Vosburghfd989c82008-11-04 17:51:16 -08003533 bond_3ad_initiate_agg_selection(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 }
3535
3536 return 0;
3537}
3538
3539static int bond_close(struct net_device *bond_dev)
3540{
Wang Chen454d7c92008-11-12 23:37:49 -08003541 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 write_lock_bh(&bond->lock);
3544
Ben Hutchingsad246c92011-04-26 15:25:52 +00003545 bond->send_peer_notif = 0;
3546
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 /* signal timers not to re-arm */
3548 bond->kill_timers = 1;
3549
3550 write_unlock_bh(&bond->lock);
3551
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003553 cancel_delayed_work(&bond->mii_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 }
3555
3556 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003557 cancel_delayed_work(&bond->arp_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 }
3559
3560 switch (bond->params.mode) {
3561 case BOND_MODE_8023AD:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003562 cancel_delayed_work(&bond->ad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 break;
3564 case BOND_MODE_TLB:
3565 case BOND_MODE_ALB:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003566 cancel_delayed_work(&bond->alb_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 break;
3568 default:
3569 break;
3570 }
3571
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003572 if (delayed_work_pending(&bond->mcast_work))
3573 cancel_delayed_work(&bond->mcast_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
Holger Eitzenberger58402052008-12-09 23:07:13 -08003575 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 /* Must be called only after all
3577 * slaves have been released
3578 */
3579 bond_alb_deinitialize(bond);
3580 }
Jiri Pirko3aba8912011-04-19 03:48:16 +00003581 bond->recv_probe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
3583 return 0;
3584}
3585
Eric Dumazet28172732010-07-07 14:58:56 -07003586static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3587 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588{
Wang Chen454d7c92008-11-12 23:37:49 -08003589 struct bonding *bond = netdev_priv(bond_dev);
Eric Dumazet28172732010-07-07 14:58:56 -07003590 struct rtnl_link_stats64 temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 struct slave *slave;
3592 int i;
3593
Eric Dumazet28172732010-07-07 14:58:56 -07003594 memset(stats, 0, sizeof(*stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
3596 read_lock_bh(&bond->lock);
3597
3598 bond_for_each_slave(bond, slave, i) {
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00003599 const struct rtnl_link_stats64 *sstats =
Eric Dumazet28172732010-07-07 14:58:56 -07003600 dev_get_stats(slave->dev, &temp);
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08003601
Eric Dumazet28172732010-07-07 14:58:56 -07003602 stats->rx_packets += sstats->rx_packets;
3603 stats->rx_bytes += sstats->rx_bytes;
3604 stats->rx_errors += sstats->rx_errors;
3605 stats->rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Eric Dumazet28172732010-07-07 14:58:56 -07003607 stats->tx_packets += sstats->tx_packets;
3608 stats->tx_bytes += sstats->tx_bytes;
3609 stats->tx_errors += sstats->tx_errors;
3610 stats->tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
Eric Dumazet28172732010-07-07 14:58:56 -07003612 stats->multicast += sstats->multicast;
3613 stats->collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
Eric Dumazet28172732010-07-07 14:58:56 -07003615 stats->rx_length_errors += sstats->rx_length_errors;
3616 stats->rx_over_errors += sstats->rx_over_errors;
3617 stats->rx_crc_errors += sstats->rx_crc_errors;
3618 stats->rx_frame_errors += sstats->rx_frame_errors;
3619 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3620 stats->rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621
Eric Dumazet28172732010-07-07 14:58:56 -07003622 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3623 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3624 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3625 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3626 stats->tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 }
3628
3629 read_unlock_bh(&bond->lock);
3630
3631 return stats;
3632}
3633
3634static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3635{
3636 struct net_device *slave_dev = NULL;
3637 struct ifbond k_binfo;
3638 struct ifbond __user *u_binfo = NULL;
3639 struct ifslave k_sinfo;
3640 struct ifslave __user *u_sinfo = NULL;
3641 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 int res = 0;
3643
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003644 pr_debug("bond_ioctl: master=%s, cmd=%d\n", bond_dev->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
3646 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 case SIOCGMIIPHY:
3648 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003649 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003651
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 mii->phy_id = 0;
3653 /* Fall Through */
3654 case SIOCGMIIREG:
3655 /*
3656 * We do this again just in case we were called by SIOCGMIIREG
3657 * instead of SIOCGMIIPHY.
3658 */
3659 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003660 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
3664 if (mii->reg_num == 1) {
Wang Chen454d7c92008-11-12 23:37:49 -08003665 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 mii->val_out = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003667 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 read_lock(&bond->curr_slave_lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003669 if (netif_carrier_ok(bond->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 mii->val_out = BMSR_LSTATUS;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003671
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 read_unlock(&bond->curr_slave_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003673 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 }
3675
3676 return 0;
3677 case BOND_INFO_QUERY_OLD:
3678 case SIOCBONDINFOQUERY:
3679 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3680
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003681 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
3684 res = bond_info_query(bond_dev, &k_binfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003685 if (res == 0 &&
3686 copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
3687 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
3689 return res;
3690 case BOND_SLAVE_INFO_QUERY_OLD:
3691 case SIOCBONDSLAVEINFOQUERY:
3692 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3693
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003694 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696
3697 res = bond_slave_info_query(bond_dev, &k_sinfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003698 if (res == 0 &&
3699 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
3700 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
3702 return res;
3703 default:
3704 /* Go on */
3705 break;
3706 }
3707
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003708 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003711 slave_dev = dev_get_by_name(dev_net(bond_dev), ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003713 pr_debug("slave_dev=%p:\n", slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003715 if (!slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 res = -ENODEV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003717 else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003718 pr_debug("slave_dev->name=%s:\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 switch (cmd) {
3720 case BOND_ENSLAVE_OLD:
3721 case SIOCBONDENSLAVE:
3722 res = bond_enslave(bond_dev, slave_dev);
3723 break;
3724 case BOND_RELEASE_OLD:
3725 case SIOCBONDRELEASE:
3726 res = bond_release(bond_dev, slave_dev);
3727 break;
3728 case BOND_SETHWADDR_OLD:
3729 case SIOCBONDSETHWADDR:
3730 res = bond_sethwaddr(bond_dev, slave_dev);
3731 break;
3732 case BOND_CHANGE_ACTIVE_OLD:
3733 case SIOCBONDCHANGEACTIVE:
3734 res = bond_ioctl_change_active(bond_dev, slave_dev);
3735 break;
3736 default:
3737 res = -EOPNOTSUPP;
3738 }
3739
3740 dev_put(slave_dev);
3741 }
3742
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 return res;
3744}
3745
Jiri Pirko22bedad2010-04-01 21:22:57 +00003746static bool bond_addr_in_mc_list(unsigned char *addr,
3747 struct netdev_hw_addr_list *list,
3748 int addrlen)
3749{
3750 struct netdev_hw_addr *ha;
3751
3752 netdev_hw_addr_list_for_each(ha, list)
3753 if (!memcmp(ha->addr, addr, addrlen))
3754 return true;
3755
3756 return false;
3757}
3758
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759static void bond_set_multicast_list(struct net_device *bond_dev)
3760{
Wang Chen454d7c92008-11-12 23:37:49 -08003761 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad2010-04-01 21:22:57 +00003762 struct netdev_hw_addr *ha;
3763 bool found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 /*
3766 * Do promisc before checking multicast_mode
3767 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003768 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07003769 /*
3770 * FIXME: Need to handle the error when one of the multi-slaves
3771 * encounters error.
3772 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 bond_set_promiscuity(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003775
3776 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 bond_set_promiscuity(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003778
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
3780 /* set allmulti flag to slaves */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003781 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07003782 /*
3783 * FIXME: Need to handle the error when one of the multi-slaves
3784 * encounters error.
3785 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 bond_set_allmulti(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003788
3789 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 bond_set_allmulti(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003791
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08003793 read_lock(&bond->lock);
3794
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 bond->flags = bond_dev->flags;
3796
3797 /* looking for addresses to add to slaves' mc list */
Jiri Pirko22bedad2010-04-01 21:22:57 +00003798 netdev_for_each_mc_addr(ha, bond_dev) {
3799 found = bond_addr_in_mc_list(ha->addr, &bond->mc_list,
3800 bond_dev->addr_len);
3801 if (!found)
3802 bond_mc_add(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 }
3804
3805 /* looking for addresses to delete from slaves' list */
Jiri Pirko22bedad2010-04-01 21:22:57 +00003806 netdev_hw_addr_list_for_each(ha, &bond->mc_list) {
3807 found = bond_addr_in_mc_list(ha->addr, &bond_dev->mc,
3808 bond_dev->addr_len);
3809 if (!found)
3810 bond_mc_del(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 }
3812
3813 /* save master's multicast list */
Jiri Pirko22bedad2010-04-01 21:22:57 +00003814 __hw_addr_flush(&bond->mc_list);
3815 __hw_addr_add_multiple(&bond->mc_list, &bond_dev->mc,
3816 bond_dev->addr_len, NETDEV_HW_ADDR_T_MULTICAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08003818 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819}
3820
Stephen Hemminger00829822008-11-20 20:14:53 -08003821static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
3822{
3823 struct bonding *bond = netdev_priv(dev);
3824 struct slave *slave = bond->first_slave;
3825
3826 if (slave) {
3827 const struct net_device_ops *slave_ops
3828 = slave->dev->netdev_ops;
3829 if (slave_ops->ndo_neigh_setup)
Patrick McHardy72e22402009-03-05 01:57:44 -08003830 return slave_ops->ndo_neigh_setup(slave->dev, parms);
Stephen Hemminger00829822008-11-20 20:14:53 -08003831 }
3832 return 0;
3833}
3834
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835/*
3836 * Change the MTU of all of a master's slaves to match the master
3837 */
3838static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3839{
Wang Chen454d7c92008-11-12 23:37:49 -08003840 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 struct slave *slave, *stop_at;
3842 int res = 0;
3843 int i;
3844
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003845 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003846 (bond_dev ? bond_dev->name : "None"), new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847
3848 /* Can't hold bond->lock with bh disabled here since
3849 * some base drivers panic. On the other hand we can't
3850 * hold bond->lock without bh disabled because we'll
3851 * deadlock. The only solution is to rely on the fact
3852 * that we're under rtnl_lock here, and the slaves
3853 * list won't change. This doesn't solve the problem
3854 * of setting the slave's MTU while it is
3855 * transmitting, but the assumption is that the base
3856 * driver can handle that.
3857 *
3858 * TODO: figure out a way to safely iterate the slaves
3859 * list, but without holding a lock around the actual
3860 * call to the base driver.
3861 */
3862
3863 bond_for_each_slave(bond, slave, i) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003864 pr_debug("s %p s->p %p c_m %p\n",
3865 slave,
3866 slave->prev,
3867 slave->dev->netdev_ops->ndo_change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08003868
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 res = dev_set_mtu(slave->dev, new_mtu);
3870
3871 if (res) {
3872 /* If we failed to set the slave's mtu to the new value
3873 * we must abort the operation even in ACTIVE_BACKUP
3874 * mode, because if we allow the backup slaves to have
3875 * different mtu values than the active slave we'll
3876 * need to change their mtu when doing a failover. That
3877 * means changing their mtu from timer context, which
3878 * is probably not a good idea.
3879 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003880 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 goto unwind;
3882 }
3883 }
3884
3885 bond_dev->mtu = new_mtu;
3886
3887 return 0;
3888
3889unwind:
3890 /* unwind from head to the slave that failed */
3891 stop_at = slave;
3892 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3893 int tmp_res;
3894
3895 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
3896 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003897 pr_debug("unwind err %d dev %s\n",
3898 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 }
3900 }
3901
3902 return res;
3903}
3904
3905/*
3906 * Change HW address
3907 *
3908 * Note that many devices must be down to change the HW address, and
3909 * downing the master releases all slaves. We can make bonds full of
3910 * bonding devices to test this, however.
3911 */
3912static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
3913{
Wang Chen454d7c92008-11-12 23:37:49 -08003914 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 struct sockaddr *sa = addr, tmp_sa;
3916 struct slave *slave, *stop_at;
3917 int res = 0;
3918 int i;
3919
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08003920 if (bond->params.mode == BOND_MODE_ALB)
3921 return bond_alb_set_mac_address(bond_dev, addr);
3922
3923
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003924 pr_debug("bond=%p, name=%s\n",
3925 bond, bond_dev ? bond_dev->name : "None");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
Jay Vosburghdd957c52007-10-09 19:57:24 -07003927 /*
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07003928 * If fail_over_mac is set to active, do nothing and return
3929 * success. Returning an error causes ifenslave to fail.
Jay Vosburghdd957c52007-10-09 19:57:24 -07003930 */
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07003931 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
Jay Vosburghdd957c52007-10-09 19:57:24 -07003932 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07003933
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003934 if (!is_valid_ether_addr(sa->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
3937 /* Can't hold bond->lock with bh disabled here since
3938 * some base drivers panic. On the other hand we can't
3939 * hold bond->lock without bh disabled because we'll
3940 * deadlock. The only solution is to rely on the fact
3941 * that we're under rtnl_lock here, and the slaves
3942 * list won't change. This doesn't solve the problem
3943 * of setting the slave's hw address while it is
3944 * transmitting, but the assumption is that the base
3945 * driver can handle that.
3946 *
3947 * TODO: figure out a way to safely iterate the slaves
3948 * list, but without holding a lock around the actual
3949 * call to the base driver.
3950 */
3951
3952 bond_for_each_slave(bond, slave, i) {
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08003953 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003954 pr_debug("slave %p %s\n", slave, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08003956 if (slave_ops->ndo_set_mac_address == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 res = -EOPNOTSUPP;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003958 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 goto unwind;
3960 }
3961
3962 res = dev_set_mac_address(slave->dev, addr);
3963 if (res) {
3964 /* TODO: consider downing the slave
3965 * and retry ?
3966 * User should expect communications
3967 * breakage anyway until ARP finish
3968 * updating, so...
3969 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003970 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 goto unwind;
3972 }
3973 }
3974
3975 /* success */
3976 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
3977 return 0;
3978
3979unwind:
3980 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
3981 tmp_sa.sa_family = bond_dev->type;
3982
3983 /* unwind from head to the slave that failed */
3984 stop_at = slave;
3985 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3986 int tmp_res;
3987
3988 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
3989 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003990 pr_debug("unwind err %d dev %s\n",
3991 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 }
3993 }
3994
3995 return res;
3996}
3997
3998static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
3999{
Wang Chen454d7c92008-11-12 23:37:49 -08004000 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 struct slave *slave, *start_at;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004002 int i, slave_no, res = 1;
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004003 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004005 /*
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004006 * Start with the curr_active_slave that joined the bond as the
4007 * default for sending IGMP traffic. For failover purposes one
4008 * needs to maintain some consistency for the interface that will
4009 * send the join/membership reports. The curr_active_slave found
4010 * will send all of this type of traffic.
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004011 */
Eric Dumazet00ae7022010-03-30 23:08:37 +00004012 if ((iph->protocol == IPPROTO_IGMP) &&
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004013 (skb->protocol == htons(ETH_P_IP))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004015 read_lock(&bond->curr_slave_lock);
4016 slave = bond->curr_active_slave;
4017 read_unlock(&bond->curr_slave_lock);
4018
4019 if (!slave)
4020 goto out;
4021 } else {
4022 /*
4023 * Concurrent TX may collide on rr_tx_counter; we accept
4024 * that as being rare enough not to justify using an
4025 * atomic op here.
4026 */
4027 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
4028
4029 bond_for_each_slave(bond, slave, i) {
4030 slave_no--;
4031 if (slave_no < 0)
4032 break;
4033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 }
4035
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004036 start_at = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 bond_for_each_slave_from(bond, slave, i, start_at) {
4038 if (IS_UP(slave->dev) &&
4039 (slave->link == BOND_LINK_UP) &&
Jiri Pirkoe30bc062011-03-12 03:14:37 +00004040 bond_is_active_slave(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 res = bond_dev_queue_xmit(bond, skb, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 break;
4043 }
4044 }
4045
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046out:
4047 if (res) {
4048 /* no suitable interface, frame not sent */
4049 dev_kfree_skb(skb);
4050 }
Michał Mirosław0693e882011-05-07 01:48:02 +00004051
Patrick McHardyec634fe2009-07-05 19:23:38 -07004052 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053}
4054
John W. Linville075897c2005-09-28 17:50:53 -04004055
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056/*
4057 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4058 * the bond has a usable interface.
4059 */
4060static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4061{
Wang Chen454d7c92008-11-12 23:37:49 -08004062 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 int res = 1;
4064
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 read_lock(&bond->curr_slave_lock);
4066
Michał Mirosław0693e882011-05-07 01:48:02 +00004067 if (bond->curr_active_slave)
4068 res = bond_dev_queue_xmit(bond, skb,
4069 bond->curr_active_slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004071 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 /* no suitable interface, frame not sent */
4073 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004074
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 read_unlock(&bond->curr_slave_lock);
Michał Mirosław0693e882011-05-07 01:48:02 +00004076
Patrick McHardyec634fe2009-07-05 19:23:38 -07004077 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078}
4079
4080/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004081 * In bond_xmit_xor() , we determine the output device by using a pre-
4082 * determined xmit_hash_policy(), If the selected device is not enabled,
4083 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 */
4085static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4086{
Wang Chen454d7c92008-11-12 23:37:49 -08004087 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 struct slave *slave, *start_at;
4089 int slave_no;
4090 int i;
4091 int res = 1;
4092
Jasper Spaansa361c832009-10-23 04:09:24 +00004093 slave_no = bond->xmit_hash_policy(skb, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
4095 bond_for_each_slave(bond, slave, i) {
4096 slave_no--;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004097 if (slave_no < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 }
4100
4101 start_at = slave;
4102
4103 bond_for_each_slave_from(bond, slave, i, start_at) {
4104 if (IS_UP(slave->dev) &&
4105 (slave->link == BOND_LINK_UP) &&
Jiri Pirkoe30bc062011-03-12 03:14:37 +00004106 bond_is_active_slave(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4108 break;
4109 }
4110 }
4111
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 if (res) {
4113 /* no suitable interface, frame not sent */
4114 dev_kfree_skb(skb);
4115 }
Michał Mirosław0693e882011-05-07 01:48:02 +00004116
Patrick McHardyec634fe2009-07-05 19:23:38 -07004117 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118}
4119
4120/*
4121 * in broadcast mode, we send everything to all usable interfaces.
4122 */
4123static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4124{
Wang Chen454d7c92008-11-12 23:37:49 -08004125 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 struct slave *slave, *start_at;
4127 struct net_device *tx_dev = NULL;
4128 int i;
4129 int res = 1;
4130
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 read_lock(&bond->curr_slave_lock);
4132 start_at = bond->curr_active_slave;
4133 read_unlock(&bond->curr_slave_lock);
4134
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004135 if (!start_at)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137
4138 bond_for_each_slave_from(bond, slave, i, start_at) {
4139 if (IS_UP(slave->dev) &&
4140 (slave->link == BOND_LINK_UP) &&
Jiri Pirkoe30bc062011-03-12 03:14:37 +00004141 bond_is_active_slave(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 if (tx_dev) {
4143 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4144 if (!skb2) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004145 pr_err("%s: Error: bond_xmit_broadcast(): skb_clone() failed\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08004146 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 continue;
4148 }
4149
4150 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4151 if (res) {
4152 dev_kfree_skb(skb2);
4153 continue;
4154 }
4155 }
4156 tx_dev = slave->dev;
4157 }
4158 }
4159
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004160 if (tx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 res = bond_dev_queue_xmit(bond, skb, tx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162
4163out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004164 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 /* no suitable interface, frame not sent */
4166 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004167
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 /* frame sent to all suitable interfaces */
Patrick McHardyec634fe2009-07-05 19:23:38 -07004169 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170}
4171
4172/*------------------------- Device initialization ---------------------------*/
4173
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004174static void bond_set_xmit_hash_policy(struct bonding *bond)
4175{
4176 switch (bond->params.xmit_policy) {
4177 case BOND_XMIT_POLICY_LAYER23:
4178 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4179 break;
4180 case BOND_XMIT_POLICY_LAYER34:
4181 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4182 break;
4183 case BOND_XMIT_POLICY_LAYER2:
4184 default:
4185 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4186 break;
4187 }
4188}
4189
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004190/*
4191 * Lookup the slave that corresponds to a qid
4192 */
4193static inline int bond_slave_override(struct bonding *bond,
4194 struct sk_buff *skb)
4195{
4196 int i, res = 1;
4197 struct slave *slave = NULL;
4198 struct slave *check_slave;
4199
Michał Mirosław0693e882011-05-07 01:48:02 +00004200 if (!skb->queue_mapping)
4201 return 1;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004202
4203 /* Find out if any slaves have the same mapping as this skb. */
4204 bond_for_each_slave(bond, check_slave, i) {
4205 if (check_slave->queue_id == skb->queue_mapping) {
4206 slave = check_slave;
4207 break;
4208 }
4209 }
4210
4211 /* If the slave isn't UP, use default transmit policy. */
4212 if (slave && slave->queue_id && IS_UP(slave->dev) &&
4213 (slave->link == BOND_LINK_UP)) {
4214 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4215 }
4216
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004217 return res;
4218}
4219
Neil Horman374eeb52011-06-03 10:35:52 +00004220
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004221static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb)
4222{
4223 /*
4224 * This helper function exists to help dev_pick_tx get the correct
Phil Oesterfd0e4352011-03-14 06:22:04 +00004225 * destination queue. Using a helper function skips a call to
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004226 * skb_tx_hash and will put the skbs in the queue we expect on their
4227 * way down to the bonding driver.
4228 */
Phil Oesterfd0e4352011-03-14 06:22:04 +00004229 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
4230
Neil Horman374eeb52011-06-03 10:35:52 +00004231 /*
4232 * Save the original txq to restore before passing to the driver
4233 */
4234 bond_queue_mapping(skb) = skb->queue_mapping;
4235
Phil Oesterfd0e4352011-03-14 06:22:04 +00004236 if (unlikely(txq >= dev->real_num_tx_queues)) {
David Decotignyd30ee672011-04-13 15:22:29 +00004237 do {
Phil Oesterfd0e4352011-03-14 06:22:04 +00004238 txq -= dev->real_num_tx_queues;
David Decotignyd30ee672011-04-13 15:22:29 +00004239 } while (txq >= dev->real_num_tx_queues);
Phil Oesterfd0e4352011-03-14 06:22:04 +00004240 }
4241 return txq;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004242}
4243
Michał Mirosław0693e882011-05-07 01:48:02 +00004244static netdev_tx_t __bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
Stephen Hemminger00829822008-11-20 20:14:53 -08004245{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004246 struct bonding *bond = netdev_priv(dev);
4247
4248 if (TX_QUEUE_OVERRIDE(bond->params.mode)) {
4249 if (!bond_slave_override(bond, skb))
4250 return NETDEV_TX_OK;
4251 }
Stephen Hemminger00829822008-11-20 20:14:53 -08004252
4253 switch (bond->params.mode) {
4254 case BOND_MODE_ROUNDROBIN:
4255 return bond_xmit_roundrobin(skb, dev);
4256 case BOND_MODE_ACTIVEBACKUP:
4257 return bond_xmit_activebackup(skb, dev);
4258 case BOND_MODE_XOR:
4259 return bond_xmit_xor(skb, dev);
4260 case BOND_MODE_BROADCAST:
4261 return bond_xmit_broadcast(skb, dev);
4262 case BOND_MODE_8023AD:
4263 return bond_3ad_xmit_xor(skb, dev);
4264 case BOND_MODE_ALB:
4265 case BOND_MODE_TLB:
4266 return bond_alb_xmit(skb, dev);
4267 default:
4268 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004269 pr_err("%s: Error: Unknown bonding mode %d\n",
4270 dev->name, bond->params.mode);
Stephen Hemminger00829822008-11-20 20:14:53 -08004271 WARN_ON_ONCE(1);
4272 dev_kfree_skb(skb);
4273 return NETDEV_TX_OK;
4274 }
4275}
4276
Michał Mirosław0693e882011-05-07 01:48:02 +00004277static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
4278{
4279 struct bonding *bond = netdev_priv(dev);
4280 netdev_tx_t ret = NETDEV_TX_OK;
4281
4282 /*
4283 * If we risk deadlock from transmitting this in the
4284 * netpoll path, tell netpoll to queue the frame for later tx
4285 */
4286 if (is_netpoll_tx_blocked(dev))
4287 return NETDEV_TX_BUSY;
4288
4289 read_lock(&bond->lock);
4290
4291 if (bond->slave_cnt)
4292 ret = __bond_start_xmit(skb, dev);
4293 else
4294 dev_kfree_skb(skb);
4295
4296 read_unlock(&bond->lock);
4297
4298 return ret;
4299}
Stephen Hemminger00829822008-11-20 20:14:53 -08004300
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301/*
4302 * set bond mode specific net device operations
4303 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004304void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004306 struct net_device *bond_dev = bond->dev;
4307
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 switch (mode) {
4309 case BOND_MODE_ROUNDROBIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 break;
4311 case BOND_MODE_ACTIVEBACKUP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 break;
4313 case BOND_MODE_XOR:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004314 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 break;
4316 case BOND_MODE_BROADCAST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 break;
4318 case BOND_MODE_8023AD:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004319 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004322 /* FALLTHRU */
4323 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 break;
4325 default:
4326 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004327 pr_err("%s: Error: Unknown bonding mode %d\n",
4328 bond_dev->name, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 break;
4330 }
4331}
4332
Jay Vosburgh217df672005-09-26 16:11:50 -07004333static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4334 struct ethtool_drvinfo *drvinfo)
4335{
4336 strncpy(drvinfo->driver, DRV_NAME, 32);
4337 strncpy(drvinfo->version, DRV_VERSION, 32);
4338 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4339}
4340
Jeff Garzik7282d492006-09-13 14:30:00 -04004341static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004342 .get_drvinfo = bond_ethtool_get_drvinfo,
Stephen Hemmingerfa53eba2008-09-13 21:17:09 -04004343 .get_link = ethtool_op_get_link,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004344};
4345
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004346static const struct net_device_ops bond_netdev_ops = {
Stephen Hemminger181470f2009-06-12 19:02:52 +00004347 .ndo_init = bond_init,
Stephen Hemminger9e716262009-06-12 19:02:47 +00004348 .ndo_uninit = bond_uninit,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004349 .ndo_open = bond_open,
4350 .ndo_stop = bond_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08004351 .ndo_start_xmit = bond_start_xmit,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004352 .ndo_select_queue = bond_select_queue,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00004353 .ndo_get_stats64 = bond_get_stats,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004354 .ndo_do_ioctl = bond_do_ioctl,
4355 .ndo_set_multicast_list = bond_set_multicast_list,
4356 .ndo_change_mtu = bond_change_mtu,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004357 .ndo_set_mac_address = bond_set_mac_address,
Stephen Hemminger00829822008-11-20 20:14:53 -08004358 .ndo_neigh_setup = bond_neigh_setup,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004359 .ndo_vlan_rx_register = bond_vlan_rx_register,
4360 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4361 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
WANG Congf6dc31a2010-05-06 00:48:51 -07004362#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00004363 .ndo_netpoll_setup = bond_netpoll_setup,
WANG Congf6dc31a2010-05-06 00:48:51 -07004364 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
4365 .ndo_poll_controller = bond_poll_controller,
4366#endif
Jiri Pirko9232ecc2011-02-13 09:33:01 +00004367 .ndo_add_slave = bond_enslave,
4368 .ndo_del_slave = bond_release,
Michał Mirosławb2a103e2011-05-07 03:22:17 +00004369 .ndo_fix_features = bond_fix_features,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004370};
4371
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004372static void bond_destructor(struct net_device *bond_dev)
4373{
4374 struct bonding *bond = netdev_priv(bond_dev);
4375 if (bond->wq)
4376 destroy_workqueue(bond->wq);
4377 free_netdev(bond_dev);
4378}
4379
Stephen Hemminger181470f2009-06-12 19:02:52 +00004380static void bond_setup(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
Wang Chen454d7c92008-11-12 23:37:49 -08004382 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 /* initialize rwlocks */
4385 rwlock_init(&bond->lock);
4386 rwlock_init(&bond->curr_slave_lock);
4387
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00004388 bond->params = bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
4390 /* Initialize pointers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 bond->dev = bond_dev;
4392 INIT_LIST_HEAD(&bond->vlan_list);
4393
4394 /* Initialize the device entry points */
Stephen Hemminger181470f2009-06-12 19:02:52 +00004395 ether_setup(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004396 bond_dev->netdev_ops = &bond_netdev_ops;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004397 bond_dev->ethtool_ops = &bond_ethtool_ops;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004398 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004400 bond_dev->destructor = bond_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401
4402 /* Initialize the device options */
4403 bond_dev->tx_queue_len = 0;
4404 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004405 bond_dev->priv_flags |= IFF_BONDING;
Neil Horman9cf81e72011-07-26 06:05:38 +00004406 bond_dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
Stephen Hemminger181470f2009-06-12 19:02:52 +00004407
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 /* At first, we block adding VLANs. That's the only way to
4409 * prevent problems that occur when adding VLANs over an
4410 * empty bond. The block will be removed once non-challenged
4411 * slaves are enslaved.
4412 */
4413 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4414
Herbert Xu932ff272006-06-09 12:20:56 -07004415 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 * transmitting */
4417 bond_dev->features |= NETIF_F_LLTX;
4418
4419 /* By default, we declare the bond to be fully
4420 * VLAN hardware accelerated capable. Special
4421 * care is taken in the various xmit functions
4422 * when there are slaves that are not hw accel
4423 * capable
4424 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
Michał Mirosławb2a103e2011-05-07 03:22:17 +00004426 bond_dev->hw_features = BOND_VLAN_FEATURES |
4427 NETIF_F_HW_VLAN_TX |
4428 NETIF_F_HW_VLAN_RX |
4429 NETIF_F_HW_VLAN_FILTER;
4430
4431 bond_dev->hw_features &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_NO_CSUM);
4432 bond_dev->features |= bond_dev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433}
4434
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004435static void bond_work_cancel_all(struct bonding *bond)
4436{
4437 write_lock_bh(&bond->lock);
4438 bond->kill_timers = 1;
4439 write_unlock_bh(&bond->lock);
4440
4441 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4442 cancel_delayed_work(&bond->mii_work);
4443
4444 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4445 cancel_delayed_work(&bond->arp_work);
4446
4447 if (bond->params.mode == BOND_MODE_ALB &&
4448 delayed_work_pending(&bond->alb_work))
4449 cancel_delayed_work(&bond->alb_work);
4450
4451 if (bond->params.mode == BOND_MODE_8023AD &&
4452 delayed_work_pending(&bond->ad_work))
4453 cancel_delayed_work(&bond->ad_work);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00004454
4455 if (delayed_work_pending(&bond->mcast_work))
4456 cancel_delayed_work(&bond->mcast_work);
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004457}
4458
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004459/*
4460* Destroy a bonding device.
4461* Must be under rtnl_lock when this function is called.
4462*/
4463static void bond_uninit(struct net_device *bond_dev)
Jay Vosburgha434e432008-10-30 17:41:15 -07004464{
Wang Chen454d7c92008-11-12 23:37:49 -08004465 struct bonding *bond = netdev_priv(bond_dev);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004466 struct vlan_entry *vlan, *tmp;
Jay Vosburgha434e432008-10-30 17:41:15 -07004467
WANG Congf6dc31a2010-05-06 00:48:51 -07004468 bond_netpoll_cleanup(bond_dev);
4469
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004470 /* Release the bonded slaves */
4471 bond_release_all(bond_dev);
4472
Jay Vosburgha434e432008-10-30 17:41:15 -07004473 list_del(&bond->bond_list);
4474
4475 bond_work_cancel_all(bond);
4476
Jay Vosburgha434e432008-10-30 17:41:15 -07004477 bond_remove_proc_entry(bond);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004478
Taku Izumif073c7c2010-12-09 15:17:13 +00004479 bond_debug_unregister(bond);
4480
Jiri Pirko22bedad2010-04-01 21:22:57 +00004481 __hw_addr_flush(&bond->mc_list);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004482
4483 list_for_each_entry_safe(vlan, tmp, &bond->vlan_list, vlan_list) {
4484 list_del(&vlan->vlan_list);
4485 kfree(vlan);
4486 }
Jay Vosburgha434e432008-10-30 17:41:15 -07004487}
4488
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489/*------------------------- Module initialization ---------------------------*/
4490
4491/*
4492 * Convert string input module parms. Accept either the
Jay Vosburghece95f72008-01-17 16:25:01 -08004493 * number of the mode or its string name. A bit complicated because
4494 * some mode names are substrings of other names, and calls from sysfs
4495 * may have whitespace in the name (trailing newlines, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 */
Holger Eitzenberger325dcf72008-12-09 23:10:17 -08004497int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498{
Hannes Eder54b87322009-02-14 11:15:49 +00004499 int modeint = -1, i, rv;
Jay Vosburgha42e5342008-01-29 18:07:43 -08004500 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
Jay Vosburghece95f72008-01-17 16:25:01 -08004501
Jay Vosburgha42e5342008-01-29 18:07:43 -08004502 for (p = (char *)buf; *p; p++)
4503 if (!(isdigit(*p) || isspace(*p)))
4504 break;
4505
4506 if (*p)
Jay Vosburghece95f72008-01-17 16:25:01 -08004507 rv = sscanf(buf, "%20s", modestr);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004508 else
Hannes Eder54b87322009-02-14 11:15:49 +00004509 rv = sscanf(buf, "%d", &modeint);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004510
4511 if (!rv)
4512 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
4514 for (i = 0; tbl[i].modename; i++) {
Hannes Eder54b87322009-02-14 11:15:49 +00004515 if (modeint == tbl[i].mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 return tbl[i].mode;
Jay Vosburghece95f72008-01-17 16:25:01 -08004517 if (strcmp(modestr, tbl[i].modename) == 0)
4518 return tbl[i].mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 }
4520
4521 return -1;
4522}
4523
4524static int bond_check_params(struct bond_params *params)
4525{
Jiri Pirkoa5499522009-09-25 03:28:09 +00004526 int arp_validate_value, fail_over_mac_value, primary_reselect_value;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004527
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 /*
4529 * Convert string parameters.
4530 */
4531 if (mode) {
4532 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4533 if (bond_mode == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004534 pr_err("Error: Invalid bonding mode \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 mode == NULL ? "NULL" : mode);
4536 return -EINVAL;
4537 }
4538 }
4539
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004540 if (xmit_hash_policy) {
4541 if ((bond_mode != BOND_MODE_XOR) &&
4542 (bond_mode != BOND_MODE_8023AD)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004543 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004544 bond_mode_name(bond_mode));
4545 } else {
4546 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4547 xmit_hashtype_tbl);
4548 if (xmit_hashtype == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004549 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004550 xmit_hash_policy == NULL ? "NULL" :
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004551 xmit_hash_policy);
4552 return -EINVAL;
4553 }
4554 }
4555 }
4556
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 if (lacp_rate) {
4558 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004559 pr_info("lacp_rate param is irrelevant in mode %s\n",
4560 bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 } else {
4562 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4563 if (lacp_fast == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004564 pr_err("Error: Invalid lacp rate \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 lacp_rate == NULL ? "NULL" : lacp_rate);
4566 return -EINVAL;
4567 }
4568 }
4569 }
4570
Jay Vosburghfd989c82008-11-04 17:51:16 -08004571 if (ad_select) {
4572 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4573 if (params->ad_select == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004574 pr_err("Error: Invalid ad_select \"%s\"\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -08004575 ad_select == NULL ? "NULL" : ad_select);
4576 return -EINVAL;
4577 }
4578
4579 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004580 pr_warning("ad_select param only affects 802.3ad mode\n");
Jay Vosburghfd989c82008-11-04 17:51:16 -08004581 }
4582 } else {
4583 params->ad_select = BOND_AD_STABLE;
4584 }
4585
Nicolas de Pesloüanf5841302009-08-28 13:18:34 +00004586 if (max_bonds < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004587 pr_warning("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4588 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 max_bonds = BOND_DEFAULT_MAX_BONDS;
4590 }
4591
4592 if (miimon < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004593 pr_warning("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to %d\n",
4594 miimon, INT_MAX, BOND_LINK_MON_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 miimon = BOND_LINK_MON_INTERV;
4596 }
4597
4598 if (updelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004599 pr_warning("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4600 updelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 updelay = 0;
4602 }
4603
4604 if (downdelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004605 pr_warning("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4606 downdelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 downdelay = 0;
4608 }
4609
4610 if ((use_carrier != 0) && (use_carrier != 1)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004611 pr_warning("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
4612 use_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 use_carrier = 1;
4614 }
4615
Ben Hutchingsad246c92011-04-26 15:25:52 +00004616 if (num_peer_notif < 0 || num_peer_notif > 255) {
4617 pr_warning("Warning: num_grat_arp/num_unsol_na (%d) not in range 0-255 so it was reset to 1\n",
4618 num_peer_notif);
4619 num_peer_notif = 1;
4620 }
4621
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 /* reset values for 802.3ad */
4623 if (bond_mode == BOND_MODE_8023AD) {
4624 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004625 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 +00004626 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 miimon = 100;
4628 }
4629 }
4630
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004631 if (tx_queues < 1 || tx_queues > 255) {
4632 pr_warning("Warning: tx_queues (%d) should be between "
4633 "1 and 255, resetting to %d\n",
4634 tx_queues, BOND_DEFAULT_TX_QUEUES);
4635 tx_queues = BOND_DEFAULT_TX_QUEUES;
4636 }
4637
Andy Gospodarekebd8e492010-06-02 08:39:21 +00004638 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
4639 pr_warning("Warning: all_slaves_active module parameter (%d), "
4640 "not of valid value (0/1), so it was set to "
4641 "0\n", all_slaves_active);
4642 all_slaves_active = 0;
4643 }
4644
Flavio Leitnerc2952c32010-10-05 14:23:59 +00004645 if (resend_igmp < 0 || resend_igmp > 255) {
4646 pr_warning("Warning: resend_igmp (%d) should be between "
4647 "0 and 255, resetting to %d\n",
4648 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
4649 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
4650 }
4651
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 /* reset values for TLB/ALB */
4653 if ((bond_mode == BOND_MODE_TLB) ||
4654 (bond_mode == BOND_MODE_ALB)) {
4655 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004656 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 +00004657 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 miimon = 100;
4659 }
4660 }
4661
4662 if (bond_mode == BOND_MODE_ALB) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004663 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",
4664 updelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 }
4666
4667 if (!miimon) {
4668 if (updelay || downdelay) {
4669 /* just warn the user the up/down delay will have
4670 * no effect since miimon is zero...
4671 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004672 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",
4673 updelay, downdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 }
4675 } else {
4676 /* don't allow arp monitoring */
4677 if (arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004678 pr_warning("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
4679 miimon, arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 arp_interval = 0;
4681 }
4682
4683 if ((updelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004684 pr_warning("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
4685 updelay, miimon,
4686 (updelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 }
4688
4689 updelay /= miimon;
4690
4691 if ((downdelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004692 pr_warning("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
4693 downdelay, miimon,
4694 (downdelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 }
4696
4697 downdelay /= miimon;
4698 }
4699
4700 if (arp_interval < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004701 pr_warning("Warning: arp_interval module parameter (%d) , not in range 0-%d, so it was reset to %d\n",
4702 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 arp_interval = BOND_LINK_ARP_INTERV;
4704 }
4705
4706 for (arp_ip_count = 0;
4707 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4708 arp_ip_count++) {
4709 /* not complete check, but should be good enough to
4710 catch mistakes */
4711 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004712 pr_warning("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
4713 arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 arp_interval = 0;
4715 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04004716 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 arp_target[arp_ip_count] = ip;
4718 }
4719 }
4720
4721 if (arp_interval && !arp_ip_count) {
4722 /* don't allow arping if no arp_ip_target given... */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004723 pr_warning("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
4724 arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 arp_interval = 0;
4726 }
4727
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004728 if (arp_validate) {
4729 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004730 pr_err("arp_validate only supported in active-backup mode\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004731 return -EINVAL;
4732 }
4733 if (!arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004734 pr_err("arp_validate requires arp_interval\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004735 return -EINVAL;
4736 }
4737
4738 arp_validate_value = bond_parse_parm(arp_validate,
4739 arp_validate_tbl);
4740 if (arp_validate_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004741 pr_err("Error: invalid arp_validate \"%s\"\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004742 arp_validate == NULL ? "NULL" : arp_validate);
4743 return -EINVAL;
4744 }
4745 } else
4746 arp_validate_value = 0;
4747
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 if (miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004749 pr_info("MII link monitoring set to %d ms\n", miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 } else if (arp_interval) {
4751 int i;
4752
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004753 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
4754 arp_interval,
4755 arp_validate_tbl[arp_validate_value].modename,
4756 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
4758 for (i = 0; i < arp_ip_count; i++)
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00004759 pr_info(" %s", arp_ip_target[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00004761 pr_info("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
Jay Vosburghb8a97872008-06-13 18:12:04 -07004763 } else if (max_bonds) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 /* miimon and arp_interval not set, we need one so things
4765 * work as expected, see bonding.txt for details
4766 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004767 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 -07004768 }
4769
4770 if (primary && !USES_PRIMARY(bond_mode)) {
4771 /* currently, using a primary only makes sense
4772 * in active backup, TLB or ALB modes
4773 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004774 pr_warning("Warning: %s primary device specified but has no effect in %s mode\n",
4775 primary, bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 primary = NULL;
4777 }
4778
Jiri Pirkoa5499522009-09-25 03:28:09 +00004779 if (primary && primary_reselect) {
4780 primary_reselect_value = bond_parse_parm(primary_reselect,
4781 pri_reselect_tbl);
4782 if (primary_reselect_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004783 pr_err("Error: Invalid primary_reselect \"%s\"\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00004784 primary_reselect ==
4785 NULL ? "NULL" : primary_reselect);
4786 return -EINVAL;
4787 }
4788 } else {
4789 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
4790 }
4791
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07004792 if (fail_over_mac) {
4793 fail_over_mac_value = bond_parse_parm(fail_over_mac,
4794 fail_over_mac_tbl);
4795 if (fail_over_mac_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004796 pr_err("Error: invalid fail_over_mac \"%s\"\n",
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07004797 arp_validate == NULL ? "NULL" : arp_validate);
4798 return -EINVAL;
4799 }
4800
4801 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004802 pr_warning("Warning: fail_over_mac only affects active-backup mode.\n");
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07004803 } else {
4804 fail_over_mac_value = BOND_FOM_NONE;
4805 }
Jay Vosburghdd957c52007-10-09 19:57:24 -07004806
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 /* fill params struct with the proper values */
4808 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004809 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 params->miimon = miimon;
Ben Hutchingsad246c92011-04-26 15:25:52 +00004811 params->num_peer_notif = num_peer_notif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004813 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 params->updelay = updelay;
4815 params->downdelay = downdelay;
4816 params->use_carrier = use_carrier;
4817 params->lacp_fast = lacp_fast;
4818 params->primary[0] = 0;
Jiri Pirkoa5499522009-09-25 03:28:09 +00004819 params->primary_reselect = primary_reselect_value;
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07004820 params->fail_over_mac = fail_over_mac_value;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004821 params->tx_queues = tx_queues;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00004822 params->all_slaves_active = all_slaves_active;
Flavio Leitnerc2952c32010-10-05 14:23:59 +00004823 params->resend_igmp = resend_igmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824
4825 if (primary) {
4826 strncpy(params->primary, primary, IFNAMSIZ);
4827 params->primary[IFNAMSIZ - 1] = 0;
4828 }
4829
4830 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4831
4832 return 0;
4833}
4834
Peter Zijlstra0daa2302006-11-08 19:51:01 -08004835static struct lock_class_key bonding_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -07004836static struct lock_class_key bonding_netdev_addr_lock_key;
Peter Zijlstra0daa2302006-11-08 19:51:01 -08004837
David S. Millere8a04642008-07-17 00:34:19 -07004838static void bond_set_lockdep_class_one(struct net_device *dev,
4839 struct netdev_queue *txq,
4840 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -07004841{
4842 lockdep_set_class(&txq->_xmit_lock,
4843 &bonding_netdev_xmit_lock_key);
4844}
4845
4846static void bond_set_lockdep_class(struct net_device *dev)
4847{
David S. Millercf508b12008-07-22 14:16:42 -07004848 lockdep_set_class(&dev->addr_list_lock,
4849 &bonding_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -07004850 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -07004851}
4852
Stephen Hemminger181470f2009-06-12 19:02:52 +00004853/*
4854 * Called from registration process
4855 */
4856static int bond_init(struct net_device *bond_dev)
4857{
4858 struct bonding *bond = netdev_priv(bond_dev);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004859 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Neil Horman9fe06172011-05-25 08:13:01 +00004860 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Stephen Hemminger181470f2009-06-12 19:02:52 +00004861
4862 pr_debug("Begin bond_init for %s\n", bond_dev->name);
4863
Neil Horman9fe06172011-05-25 08:13:01 +00004864 /*
4865 * Initialize locks that may be required during
4866 * en/deslave operations. All of the bond_open work
4867 * (of which this is part) should really be moved to
4868 * a phase prior to dev_open
4869 */
4870 spin_lock_init(&(bond_info->tx_hashtbl_lock));
4871 spin_lock_init(&(bond_info->rx_hashtbl_lock));
4872
Stephen Hemminger181470f2009-06-12 19:02:52 +00004873 bond->wq = create_singlethread_workqueue(bond_dev->name);
4874 if (!bond->wq)
4875 return -ENOMEM;
4876
4877 bond_set_lockdep_class(bond_dev);
4878
Stephen Hemminger181470f2009-06-12 19:02:52 +00004879 bond_create_proc_entry(bond);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004880 list_add_tail(&bond->bond_list, &bn->dev_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00004881
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00004882 bond_prepare_sysfs_group(bond);
Jiri Pirko22bedad2010-04-01 21:22:57 +00004883
Taku Izumif073c7c2010-12-09 15:17:13 +00004884 bond_debug_register(bond);
4885
Jiri Pirko22bedad2010-04-01 21:22:57 +00004886 __hw_addr_init(&bond->mc_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00004887 return 0;
4888}
4889
Eric W. Biederman88ead972009-10-29 14:18:25 +00004890static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
4891{
4892 if (tb[IFLA_ADDRESS]) {
4893 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
4894 return -EINVAL;
4895 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
4896 return -EADDRNOTAVAIL;
4897 }
4898 return 0;
4899}
4900
4901static struct rtnl_link_ops bond_link_ops __read_mostly = {
4902 .kind = "bond",
Eric W. Biederman66391042009-10-29 23:58:54 +00004903 .priv_size = sizeof(struct bonding),
Eric W. Biederman88ead972009-10-29 14:18:25 +00004904 .setup = bond_setup,
4905 .validate = bond_validate,
4906};
4907
Mitch Williamsdfe60392005-11-09 10:36:04 -08004908/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004909 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08004910 * Caller must NOT hold rtnl_lock; we need to release it here before we
4911 * set up our sysfs entries.
4912 */
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004913int bond_create(struct net *net, const char *name)
Mitch Williamsdfe60392005-11-09 10:36:04 -08004914{
4915 struct net_device *bond_dev;
4916 int res;
4917
4918 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08004919
Jiri Pirko1c5cae82011-04-30 01:21:32 +00004920 bond_dev = alloc_netdev_mq(sizeof(struct bonding),
4921 name ? name : "bond%d",
4922 bond_setup, tx_queues);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004923 if (!bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004924 pr_err("%s: eek! can't alloc netdev!\n", name);
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004925 rtnl_unlock();
4926 return -ENOMEM;
Mitch Williamsdfe60392005-11-09 10:36:04 -08004927 }
4928
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004929 dev_net_set(bond_dev, net);
Eric W. Biederman88ead972009-10-29 14:18:25 +00004930 bond_dev->rtnl_link_ops = &bond_link_ops;
4931
Mitch Williamsdfe60392005-11-09 10:36:04 -08004932 res = register_netdevice(bond_dev);
Peter Zijlstra0daa2302006-11-08 19:51:01 -08004933
Phil Oestere826eaf2011-03-14 06:22:05 +00004934 netif_carrier_off(bond_dev);
4935
Mitch Williamsdfe60392005-11-09 10:36:04 -08004936 rtnl_unlock();
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004937 if (res < 0)
4938 bond_destructor(bond_dev);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004939 return res;
4940}
4941
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004942static int __net_init bond_net_init(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004943{
Eric W. Biederman15449742009-11-29 15:46:04 +00004944 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004945
4946 bn->net = net;
4947 INIT_LIST_HEAD(&bn->dev_list);
4948
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004949 bond_create_proc_dir(bn);
Eric W. Biederman15449742009-11-29 15:46:04 +00004950
4951 return 0;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004952}
4953
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004954static void __net_exit bond_net_exit(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004955{
Eric W. Biederman15449742009-11-29 15:46:04 +00004956 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004957
4958 bond_destroy_proc_dir(bn);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004959}
4960
4961static struct pernet_operations bond_net_ops = {
4962 .init = bond_net_init,
4963 .exit = bond_net_exit,
Eric W. Biederman15449742009-11-29 15:46:04 +00004964 .id = &bond_net_id,
4965 .size = sizeof(struct bond_net),
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004966};
4967
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968static int __init bonding_init(void)
4969{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 int i;
4971 int res;
4972
Amerigo Wangbd33acc2011-03-06 21:58:46 +00004973 pr_info("%s", bond_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974
Mitch Williamsdfe60392005-11-09 10:36:04 -08004975 res = bond_check_params(&bonding_defaults);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004976 if (res)
Mitch Williamsdfe60392005-11-09 10:36:04 -08004977 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
Eric W. Biederman15449742009-11-29 15:46:04 +00004979 res = register_pernet_subsys(&bond_net_ops);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004980 if (res)
4981 goto out;
Jay Vosburgh027ea042008-01-17 16:25:02 -08004982
Eric W. Biederman88ead972009-10-29 14:18:25 +00004983 res = rtnl_link_register(&bond_link_ops);
4984 if (res)
Eric W. Biederman66391042009-10-29 23:58:54 +00004985 goto err_link;
Eric W. Biederman88ead972009-10-29 14:18:25 +00004986
Taku Izumif073c7c2010-12-09 15:17:13 +00004987 bond_create_debugfs();
4988
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 for (i = 0; i < max_bonds; i++) {
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004990 res = bond_create(&init_net, NULL);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004991 if (res)
4992 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 }
4994
Mitch Williamsb76cdba2005-11-09 10:36:41 -08004995 res = bond_create_sysfs();
4996 if (res)
4997 goto err;
4998
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005000 register_inetaddr_notifier(&bond_inetaddr_notifier);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005001out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 return res;
Eric W. Biederman88ead972009-10-29 14:18:25 +00005003err:
5004 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman66391042009-10-29 23:58:54 +00005005err_link:
Eric W. Biederman15449742009-11-29 15:46:04 +00005006 unregister_pernet_subsys(&bond_net_ops);
Eric W. Biederman88ead972009-10-29 14:18:25 +00005007 goto out;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009}
5010
5011static void __exit bonding_exit(void)
5012{
5013 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005014 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005016 bond_destroy_sysfs();
Taku Izumif073c7c2010-12-09 15:17:13 +00005017 bond_destroy_debugfs();
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005018
Eric W. Biederman88ead972009-10-29 14:18:25 +00005019 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman15449742009-11-29 15:46:04 +00005020 unregister_pernet_subsys(&bond_net_ops);
Neil Hormane843fa52010-10-13 16:01:50 +00005021
5022#ifdef CONFIG_NET_POLL_CONTROLLER
Neil Hormanfb4fa762010-12-06 09:05:50 +00005023 /*
5024 * Make sure we don't have an imbalance on our netpoll blocking
5025 */
5026 WARN_ON(atomic_read(&netpoll_block_tx));
Neil Hormane843fa52010-10-13 16:01:50 +00005027#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028}
5029
5030module_init(bonding_init);
5031module_exit(bonding_exit);
5032MODULE_LICENSE("GPL");
5033MODULE_VERSION(DRV_VERSION);
5034MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5035MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
Eric W. Biederman88ead972009-10-29 14:18:25 +00005036MODULE_ALIAS_RTNL_LINK("bond");