blob: ca902ae3f2e56bf51c6a01efafc578004a24f827 [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;
Moni Shoua7893b242008-05-17 21:10:12 -070092static int num_grat_arp = 1;
Brian Haley305d5522008-11-04 17:51:14 -080093static int num_unsol_na = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static int miimon = BOND_LINK_MON_INTERV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +000095static int updelay;
96static int downdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static int use_carrier = 1;
Stephen Hemminger3d632c32009-06-12 19:02:48 +000098static char *mode;
99static char *primary;
Jiri Pirkoa5499522009-09-25 03:28:09 +0000100static char *primary_reselect;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000101static char *lacp_rate;
102static char *ad_select;
103static char *xmit_hash_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static int arp_interval = BOND_LINK_ARP_INTERV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000105static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
106static char *arp_validate;
107static char *fail_over_mac;
Andy Gospodarekebd8e492010-06-02 08:39:21 +0000108static int all_slaves_active = 0;
Stephen Hemmingerd2991f72009-06-12 19:02:44 +0000109static struct bond_params bonding_defaults;
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000110static int resend_igmp = BOND_DEFAULT_RESEND_IGMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112module_param(max_bonds, int, 0);
113MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
Andy Gospodarekbb1d9122010-06-02 08:40:18 +0000114module_param(tx_queues, int, 0);
115MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)");
Moni Shoua7893b242008-05-17 21:10:12 -0700116module_param(num_grat_arp, int, 0644);
117MODULE_PARM_DESC(num_grat_arp, "Number of gratuitous ARP packets to send on failover event");
Brian Haley305d5522008-11-04 17:51:14 -0800118module_param(num_unsol_na, int, 0644);
119MODULE_PARM_DESC(num_unsol_na, "Number of unsolicited IPv6 Neighbor Advertisements packets to send on failover event");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120module_param(miimon, int, 0);
121MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
122module_param(updelay, int, 0);
123MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
124module_param(downdelay, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800125MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
126 "in milliseconds");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127module_param(use_carrier, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800128MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
129 "0 for off, 1 for on (default)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130module_param(mode, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800131MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
132 "1 for active-backup, 2 for balance-xor, "
133 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
134 "6 for balance-alb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135module_param(primary, charp, 0);
136MODULE_PARM_DESC(primary, "Primary network device to use");
Jiri Pirkoa5499522009-09-25 03:28:09 +0000137module_param(primary_reselect, charp, 0);
138MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
139 "once it comes up; "
140 "0 for always (default), "
141 "1 for only if speed of primary is "
142 "better, "
143 "2 for only on active slave "
144 "failure");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145module_param(lacp_rate, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800146MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
147 "(slow/fast)");
Jay Vosburghfd989c82008-11-04 17:51:16 -0800148module_param(ad_select, charp, 0);
149MODULE_PARM_DESC(ad_select, "803.ad aggregation selection logic: stable (0, default), bandwidth (1), count (2)");
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400150module_param(xmit_hash_policy, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800151MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
152 ", 1 for layer 3+4");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153module_param(arp_interval, int, 0);
154MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
155module_param_array(arp_ip_target, charp, NULL, 0);
156MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700157module_param(arp_validate, charp, 0);
158MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700159module_param(fail_over_mac, charp, 0);
160MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to the same MAC. none (default), active or follow");
Andy Gospodarekebd8e492010-06-02 08:39:21 +0000161module_param(all_slaves_active, int, 0);
162MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface"
163 "by setting active flag for all slaves. "
164 "0 for never (default), 1 for always.");
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000165module_param(resend_igmp, int, 0);
166MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on link failure");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168/*----------------------------- Global variables ----------------------------*/
169
Neil Hormane843fa52010-10-13 16:01:50 +0000170#ifdef CONFIG_NET_POLL_CONTROLLER
Neil Hormanfb4fa762010-12-06 09:05:50 +0000171atomic_t netpoll_block_tx = ATOMIC_INIT(0);
Neil Hormane843fa52010-10-13 16:01:50 +0000172#endif
173
Eric Dumazetf99189b2009-11-17 10:42:49 +0000174int bond_net_id __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000176static __be32 arp_target[BOND_MAX_ARP_TARGETS];
177static int arp_ip_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178static int bond_mode = BOND_MODE_ROUNDROBIN;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000179static int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
180static int lacp_fast;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800182const struct bond_parm_tbl bond_lacp_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{ "slow", AD_LACP_SLOW},
184{ "fast", AD_LACP_FAST},
185{ NULL, -1},
186};
187
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800188const struct bond_parm_tbl bond_mode_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{ "balance-rr", BOND_MODE_ROUNDROBIN},
190{ "active-backup", BOND_MODE_ACTIVEBACKUP},
191{ "balance-xor", BOND_MODE_XOR},
192{ "broadcast", BOND_MODE_BROADCAST},
193{ "802.3ad", BOND_MODE_8023AD},
194{ "balance-tlb", BOND_MODE_TLB},
195{ "balance-alb", BOND_MODE_ALB},
196{ NULL, -1},
197};
198
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800199const struct bond_parm_tbl xmit_hashtype_tbl[] = {
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400200{ "layer2", BOND_XMIT_POLICY_LAYER2},
201{ "layer3+4", BOND_XMIT_POLICY_LAYER34},
Jay Vosburgh6f6652b2007-12-06 23:40:34 -0800202{ "layer2+3", BOND_XMIT_POLICY_LAYER23},
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400203{ NULL, -1},
204};
205
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800206const struct bond_parm_tbl arp_validate_tbl[] = {
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700207{ "none", BOND_ARP_VALIDATE_NONE},
208{ "active", BOND_ARP_VALIDATE_ACTIVE},
209{ "backup", BOND_ARP_VALIDATE_BACKUP},
210{ "all", BOND_ARP_VALIDATE_ALL},
211{ NULL, -1},
212};
213
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800214const struct bond_parm_tbl fail_over_mac_tbl[] = {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700215{ "none", BOND_FOM_NONE},
216{ "active", BOND_FOM_ACTIVE},
217{ "follow", BOND_FOM_FOLLOW},
218{ NULL, -1},
219};
220
Jiri Pirkoa5499522009-09-25 03:28:09 +0000221const struct bond_parm_tbl pri_reselect_tbl[] = {
222{ "always", BOND_PRI_RESELECT_ALWAYS},
223{ "better", BOND_PRI_RESELECT_BETTER},
224{ "failure", BOND_PRI_RESELECT_FAILURE},
225{ NULL, -1},
226};
227
Jay Vosburghfd989c82008-11-04 17:51:16 -0800228struct bond_parm_tbl ad_select_tbl[] = {
229{ "stable", BOND_AD_STABLE},
230{ "bandwidth", BOND_AD_BANDWIDTH},
231{ "count", BOND_AD_COUNT},
232{ NULL, -1},
233};
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235/*-------------------------- Forward declarations ---------------------------*/
236
Jay Vosburghc3ade5c2005-06-26 17:52:20 -0400237static void bond_send_gratuitous_arp(struct bonding *bond);
Stephen Hemminger181470f2009-06-12 19:02:52 +0000238static int bond_init(struct net_device *bond_dev);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +0000239static void bond_uninit(struct net_device *bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241/*---------------------------- General routines -----------------------------*/
242
Amerigo Wangbd33acc2011-03-06 21:58:46 +0000243const char *bond_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800245 static const char *names[] = {
246 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
247 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
248 [BOND_MODE_XOR] = "load balancing (xor)",
249 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000250 [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800251 [BOND_MODE_TLB] = "transmit load balancing",
252 [BOND_MODE_ALB] = "adaptive load balancing",
253 };
254
255 if (mode < 0 || mode > BOND_MODE_ALB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return "unknown";
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800257
258 return names[mode];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261/*---------------------------------- VLAN -----------------------------------*/
262
263/**
264 * bond_add_vlan - add a new vlan id on bond
265 * @bond: bond that got the notification
266 * @vlan_id: the vlan id to add
267 *
268 * Returns -ENOMEM if allocation failed.
269 */
270static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
271{
272 struct vlan_entry *vlan;
273
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800274 pr_debug("bond: %s, vlan id %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800275 (bond ? bond->dev->name : "None"), vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Brian Haley305d5522008-11-04 17:51:14 -0800277 vlan = kzalloc(sizeof(struct vlan_entry), GFP_KERNEL);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000278 if (!vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 INIT_LIST_HEAD(&vlan->vlan_list);
282 vlan->vlan_id = vlan_id;
283
284 write_lock_bh(&bond->lock);
285
286 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
287
288 write_unlock_bh(&bond->lock);
289
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800290 pr_debug("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 return 0;
293}
294
295/**
296 * bond_del_vlan - delete a vlan id from bond
297 * @bond: bond that got the notification
298 * @vlan_id: the vlan id to delete
299 *
300 * returns -ENODEV if @vlan_id was not found in @bond.
301 */
302static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
303{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700304 struct vlan_entry *vlan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 int res = -ENODEV;
306
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800307 pr_debug("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Neil Hormane843fa52010-10-13 16:01:50 +0000309 block_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 write_lock_bh(&bond->lock);
311
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700312 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (vlan->vlan_id == vlan_id) {
314 list_del(&vlan->vlan_list);
315
Holger Eitzenberger58402052008-12-09 23:07:13 -0800316 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 bond_alb_clear_vlan(bond, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800319 pr_debug("removed VLAN ID %d from bond %s\n",
320 vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 kfree(vlan);
323
324 if (list_empty(&bond->vlan_list) &&
325 (bond->slave_cnt == 0)) {
326 /* Last VLAN removed and no slaves, so
327 * restore block on adding VLANs. This will
328 * be removed once new slaves that are not
329 * VLAN challenged will be added.
330 */
331 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
332 }
333
334 res = 0;
335 goto out;
336 }
337 }
338
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800339 pr_debug("couldn't find VLAN ID %d in bond %s\n",
340 vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342out:
343 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +0000344 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return res;
346}
347
348/**
349 * bond_has_challenged_slaves
350 * @bond: the bond we're working on
351 *
352 * Searches the slave list. Returns 1 if a vlan challenged slave
353 * was found, 0 otherwise.
354 *
355 * Assumes bond->lock is held.
356 */
357static int bond_has_challenged_slaves(struct bonding *bond)
358{
359 struct slave *slave;
360 int i;
361
362 bond_for_each_slave(bond, slave, i) {
363 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800364 pr_debug("found VLAN challenged slave - %s\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800365 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return 1;
367 }
368 }
369
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800370 pr_debug("no VLAN challenged slaves found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return 0;
372}
373
374/**
375 * bond_next_vlan - safely skip to the next item in the vlans list.
376 * @bond: the bond we're working on
377 * @curr: item we're advancing from
378 *
379 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
380 * or @curr->next otherwise (even if it is @curr itself again).
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000381 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 * Caller must hold bond->lock
383 */
384struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
385{
386 struct vlan_entry *next, *last;
387
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000388 if (list_empty(&bond->vlan_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (!curr) {
392 next = list_entry(bond->vlan_list.next,
393 struct vlan_entry, vlan_list);
394 } else {
395 last = list_entry(bond->vlan_list.prev,
396 struct vlan_entry, vlan_list);
397 if (last == curr) {
398 next = list_entry(bond->vlan_list.next,
399 struct vlan_entry, vlan_list);
400 } else {
401 next = list_entry(curr->vlan_list.next,
402 struct vlan_entry, vlan_list);
403 }
404 }
405
406 return next;
407}
408
409/**
410 * bond_dev_queue_xmit - Prepare skb for xmit.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000411 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 * @bond: bond device that got this skb for tx.
413 * @skb: hw accel VLAN tagged skb to transmit
414 * @slave_dev: slave that is supposed to xmit this skbuff
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000416int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
417 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Ben Hutchings83874512010-12-13 08:19:28 +0000419 skb->dev = slave_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 skb->priority = 1;
Amerigo Wang080e4132011-02-17 23:43:33 +0000421 if (unlikely(netpoll_tx_running(slave_dev)))
Amerigo Wang8a8efa22011-02-17 23:43:32 +0000422 bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
Amerigo Wang080e4132011-02-17 23:43:33 +0000423 else
WANG Congf6dc31a2010-05-06 00:48:51 -0700424 dev_queue_xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 return 0;
427}
428
429/*
430 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
431 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
432 * lock because:
433 * a. This operation is performed in IOCTL context,
434 * b. The operation is protected by the RTNL semaphore in the 8021q code,
435 * c. Holding a lock with BH disabled while directly calling a base driver
436 * entry point is generally a BAD idea.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000437 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 * The design of synchronization/protection for this operation in the 8021q
439 * module is good for one or more VLAN devices over a single physical device
440 * and cannot be extended for a teaming solution like bonding, so there is a
441 * potential race condition here where a net device from the vlan group might
442 * be referenced (either by a base driver or the 8021q code) while it is being
443 * removed from the system. However, it turns out we're not making matters
444 * worse, and if it works for regular VLAN usage it will work here too.
445*/
446
447/**
448 * bond_vlan_rx_register - Propagates registration to slaves
449 * @bond_dev: bonding net device that got called
450 * @grp: vlan group being registered
451 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000452static void bond_vlan_rx_register(struct net_device *bond_dev,
453 struct vlan_group *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Wang Chen454d7c92008-11-12 23:37:49 -0800455 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct slave *slave;
457 int i;
458
Jarek Poplawskia71fb882010-10-27 07:08:22 +0000459 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 bond->vlgrp = grp;
Jarek Poplawskia71fb882010-10-27 07:08:22 +0000461 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 bond_for_each_slave(bond, slave, i) {
464 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800465 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800468 slave_ops->ndo_vlan_rx_register) {
469 slave_ops->ndo_vlan_rx_register(slave_dev, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471 }
472}
473
474/**
475 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
476 * @bond_dev: bonding net device that got called
477 * @vid: vlan id being added
478 */
479static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
480{
Wang Chen454d7c92008-11-12 23:37:49 -0800481 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 struct slave *slave;
483 int i, res;
484
485 bond_for_each_slave(bond, slave, i) {
486 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800487 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800490 slave_ops->ndo_vlan_rx_add_vid) {
491 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493 }
494
495 res = bond_add_vlan(bond, vid);
496 if (res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800497 pr_err("%s: Error: Failed to add vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 bond_dev->name, vid);
499 }
500}
501
502/**
503 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
504 * @bond_dev: bonding net device that got called
505 * @vid: vlan id being removed
506 */
507static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
508{
Wang Chen454d7c92008-11-12 23:37:49 -0800509 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 struct slave *slave;
511 struct net_device *vlan_dev;
512 int i, res;
513
514 bond_for_each_slave(bond, slave, i) {
515 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800516 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800519 slave_ops->ndo_vlan_rx_kill_vid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* Save and then restore vlan_dev in the grp array,
521 * since the slave's driver might clear it.
522 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800523 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800524 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vid);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800525 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527 }
528
529 res = bond_del_vlan(bond, vid);
530 if (res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800531 pr_err("%s: Error: Failed to remove vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 bond_dev->name, vid);
533 }
534}
535
536static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
537{
538 struct vlan_entry *vlan;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800539 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Jay Vosburghf35188f2010-07-21 12:14:47 +0000541 if (!bond->vlgrp)
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000542 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800545 slave_ops->ndo_vlan_rx_register)
546 slave_ops->ndo_vlan_rx_register(slave_dev, bond->vlgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800549 !(slave_ops->ndo_vlan_rx_add_vid))
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000550 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800552 list_for_each_entry(vlan, &bond->vlan_list, vlan_list)
553 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vlan->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000556static void bond_del_vlans_from_slave(struct bonding *bond,
557 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800559 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 struct vlan_entry *vlan;
561 struct net_device *vlan_dev;
562
Jay Vosburghf35188f2010-07-21 12:14:47 +0000563 if (!bond->vlgrp)
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000564 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800567 !(slave_ops->ndo_vlan_rx_kill_vid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 goto unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +0000571 if (!vlan->vlan_id)
572 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /* Save and then restore vlan_dev in the grp array,
574 * since the slave's driver might clear it.
575 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800576 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800577 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800578 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580
581unreg:
582 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800583 slave_ops->ndo_vlan_rx_register)
584 slave_ops->ndo_vlan_rx_register(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
587/*------------------------------- Link status -------------------------------*/
588
589/*
Jay Vosburghff59c452006-03-27 13:27:43 -0800590 * Set the carrier state for the master according to the state of its
591 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
592 * do special 802.3ad magic.
593 *
594 * Returns zero if carrier state does not change, nonzero if it does.
595 */
596static int bond_set_carrier(struct bonding *bond)
597{
598 struct slave *slave;
599 int i;
600
601 if (bond->slave_cnt == 0)
602 goto down;
603
604 if (bond->params.mode == BOND_MODE_8023AD)
605 return bond_3ad_set_carrier(bond);
606
607 bond_for_each_slave(bond, slave, i) {
608 if (slave->link == BOND_LINK_UP) {
609 if (!netif_carrier_ok(bond->dev)) {
610 netif_carrier_on(bond->dev);
611 return 1;
612 }
613 return 0;
614 }
615 }
616
617down:
618 if (netif_carrier_ok(bond->dev)) {
619 netif_carrier_off(bond->dev);
620 return 1;
621 }
622 return 0;
623}
624
625/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 * Get link speed and duplex from the slave's base driver
627 * using ethtool. If for some reason the call fails or the
628 * values are invalid, fake speed and duplex to 100/Full
629 * and return error.
630 */
631static int bond_update_speed_duplex(struct slave *slave)
632{
633 struct net_device *slave_dev = slave->dev;
David Decotigny5d305302011-04-13 15:22:31 +0000634 struct ethtool_cmd etool = { .cmd = ETHTOOL_GSET };
635 u32 slave_speed;
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700636 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 /* Fake speed and duplex */
639 slave->speed = SPEED_100;
640 slave->duplex = DUPLEX_FULL;
641
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700642 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700645 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
646 if (res < 0)
647 return -1;
648
David Decotigny5d305302011-04-13 15:22:31 +0000649 slave_speed = ethtool_cmd_speed(&etool);
650 switch (slave_speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 case SPEED_10:
652 case SPEED_100:
653 case SPEED_1000:
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700654 case SPEED_10000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 break;
656 default:
657 return -1;
658 }
659
660 switch (etool.duplex) {
661 case DUPLEX_FULL:
662 case DUPLEX_HALF:
663 break;
664 default:
665 return -1;
666 }
667
David Decotigny5d305302011-04-13 15:22:31 +0000668 slave->speed = slave_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 slave->duplex = etool.duplex;
670
671 return 0;
672}
673
674/*
675 * if <dev> supports MII link status reporting, check its link status.
676 *
677 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000678 * depending upon the setting of the use_carrier parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 *
680 * Return either BMSR_LSTATUS, meaning that the link is up (or we
681 * can't tell and just pretend it is), or 0, meaning that the link is
682 * down.
683 *
684 * If reporting is non-zero, instead of faking link up, return -1 if
685 * both ETHTOOL and MII ioctls fail (meaning the device does not
686 * support them). If use_carrier is set, return whatever it says.
687 * It'd be nice if there was a good way to tell if a driver supports
688 * netif_carrier, but there really isn't.
689 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000690static int bond_check_dev_link(struct bonding *bond,
691 struct net_device *slave_dev, int reporting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800693 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Jiri Bohacd9d52832009-10-28 22:23:54 -0700694 int (*ioctl)(struct net_device *, struct ifreq *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct ifreq ifr;
696 struct mii_ioctl_data *mii;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Petri Gynther6c988852009-08-28 12:05:15 +0000698 if (!reporting && !netif_running(slave_dev))
699 return 0;
700
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800701 if (bond->params.use_carrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Jiri Pirko29112f42009-04-24 01:58:23 +0000704 /* Try to get link status using Ethtool first. */
705 if (slave_dev->ethtool_ops) {
706 if (slave_dev->ethtool_ops->get_link) {
707 u32 link;
708
709 link = slave_dev->ethtool_ops->get_link(slave_dev);
710
711 return link ? BMSR_LSTATUS : 0;
712 }
713 }
714
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000715 /* Ethtool can't be used, fallback to MII ioctls. */
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800716 ioctl = slave_ops->ndo_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (ioctl) {
718 /* TODO: set pointer to correct ioctl on a per team member */
719 /* bases to make this more efficient. that is, once */
720 /* we determine the correct ioctl, we will always */
721 /* call it and not the others for that team */
722 /* member. */
723
724 /*
725 * We cannot assume that SIOCGMIIPHY will also read a
726 * register; not all network drivers (e.g., e100)
727 * support that.
728 */
729
730 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
731 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
732 mii = if_mii(&ifr);
733 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
734 mii->reg_num = MII_BMSR;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000735 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0)
736 return mii->val_out & BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738 }
739
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700740 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 * If reporting, report that either there's no dev->do_ioctl,
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700742 * or both SIOCGMIIREG and get_link failed (meaning that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 * cannot report link status). If not reporting, pretend
744 * we're ok.
745 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000746 return reporting ? -1 : BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
749/*----------------------------- Multicast list ------------------------------*/
750
751/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 * Push the promiscuity flag down to appropriate slaves
753 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700754static int bond_set_promiscuity(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700756 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (USES_PRIMARY(bond->params.mode)) {
758 /* write lock already acquired */
759 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700760 err = dev_set_promiscuity(bond->curr_active_slave->dev,
761 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763 } else {
764 struct slave *slave;
765 int i;
766 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700767 err = dev_set_promiscuity(slave->dev, inc);
768 if (err)
769 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700772 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
775/*
776 * Push the allmulti flag down to all slaves
777 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700778static int bond_set_allmulti(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700780 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (USES_PRIMARY(bond->params.mode)) {
782 /* write lock already acquired */
783 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700784 err = dev_set_allmulti(bond->curr_active_slave->dev,
785 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787 } else {
788 struct slave *slave;
789 int i;
790 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700791 err = dev_set_allmulti(slave->dev, inc);
792 if (err)
793 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700796 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
799/*
800 * Add a Multicast address to slaves
801 * according to mode
802 */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000803static void bond_mc_add(struct bonding *bond, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 if (USES_PRIMARY(bond->params.mode)) {
806 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000807 if (bond->curr_active_slave)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000808 dev_mc_add(bond->curr_active_slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 } else {
810 struct slave *slave;
811 int i;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000812
813 bond_for_each_slave(bond, slave, i)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000814 dev_mc_add(slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816}
817
818/*
819 * Remove a multicast address from slave
820 * according to mode
821 */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000822static void bond_mc_del(struct bonding *bond, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 if (USES_PRIMARY(bond->params.mode)) {
825 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000826 if (bond->curr_active_slave)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000827 dev_mc_del(bond->curr_active_slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 } else {
829 struct slave *slave;
830 int i;
831 bond_for_each_slave(bond, slave, i) {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000832 dev_mc_del(slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834 }
835}
836
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800837
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000838static void __bond_resend_igmp_join_requests(struct net_device *dev)
839{
840 struct in_device *in_dev;
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000841
842 rcu_read_lock();
843 in_dev = __in_dev_get_rcu(dev);
Eric Dumazet866f3b22010-11-18 09:33:19 -0800844 if (in_dev)
David S. Miller24912422010-11-19 13:13:47 -0800845 ip_mc_rejoin_groups(in_dev);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000846 rcu_read_unlock();
847}
848
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800849/*
850 * Retrieve the list of registered multicast addresses for the bonding
851 * device and retransmit an IGMP JOIN request to the current active
852 * slave.
853 */
854static void bond_resend_igmp_join_requests(struct bonding *bond)
855{
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000856 struct net_device *vlan_dev;
857 struct vlan_entry *vlan;
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800858
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000859 read_lock(&bond->lock);
860
861 /* rejoin all groups on bond device */
862 __bond_resend_igmp_join_requests(bond->dev);
863
864 /* rejoin all groups on vlan devices */
865 if (bond->vlgrp) {
866 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
867 vlan_dev = vlan_group_get_device(bond->vlgrp,
868 vlan->vlan_id);
869 if (vlan_dev)
870 __bond_resend_igmp_join_requests(vlan_dev);
871 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800872 }
873
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000874 if (--bond->igmp_retrans > 0)
875 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
876
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000877 read_unlock(&bond->lock);
878}
879
stephen hemminger379b7382010-10-15 11:02:56 +0000880static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000881{
882 struct bonding *bond = container_of(work, struct bonding,
883 mcast_work.work);
884 bond_resend_igmp_join_requests(bond);
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800885}
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 * flush all members of flush->mc_list from device dev->mc_list
889 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000890static void bond_mc_list_flush(struct net_device *bond_dev,
891 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Wang Chen454d7c92008-11-12 23:37:49 -0800893 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000894 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Jiri Pirko22bedad32010-04-01 21:22:57 +0000896 netdev_for_each_mc_addr(ha, bond_dev)
897 dev_mc_del(slave_dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 if (bond->params.mode == BOND_MODE_8023AD) {
900 /* del lacpdu mc addr from mc list */
901 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
902
Jiri Pirko22bedad32010-04-01 21:22:57 +0000903 dev_mc_del(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905}
906
907/*--------------------------- Active slave change ---------------------------*/
908
909/*
910 * Update the mc list and multicast-related flags for the new and
911 * old active slaves (if any) according to the multicast mode, and
912 * promiscuous flags unconditionally.
913 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000914static void bond_mc_swap(struct bonding *bond, struct slave *new_active,
915 struct slave *old_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Jiri Pirko22bedad32010-04-01 21:22:57 +0000917 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000919 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 /* nothing to do - mc list is already up-to-date on
921 * all slaves
922 */
923 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 if (old_active) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000926 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 dev_set_promiscuity(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000929 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 dev_set_allmulti(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Jiri Pirko22bedad32010-04-01 21:22:57 +0000932 netdev_for_each_mc_addr(ha, bond->dev)
933 dev_mc_del(old_active->dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
935
936 if (new_active) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700937 /* FIXME: Signal errors upstream. */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000938 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 dev_set_promiscuity(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000941 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 dev_set_allmulti(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Jiri Pirko22bedad32010-04-01 21:22:57 +0000944 netdev_for_each_mc_addr(ha, bond->dev)
945 dev_mc_add(new_active->dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947}
948
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700949/*
950 * bond_do_fail_over_mac
951 *
952 * Perform special MAC address swapping for fail_over_mac settings
953 *
954 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
955 */
956static void bond_do_fail_over_mac(struct bonding *bond,
957 struct slave *new_active,
958 struct slave *old_active)
Hannes Eder1f78d9f2009-02-14 11:15:33 +0000959 __releases(&bond->curr_slave_lock)
960 __releases(&bond->lock)
961 __acquires(&bond->lock)
962 __acquires(&bond->curr_slave_lock)
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700963{
964 u8 tmp_mac[ETH_ALEN];
965 struct sockaddr saddr;
966 int rv;
967
968 switch (bond->params.fail_over_mac) {
969 case BOND_FOM_ACTIVE:
970 if (new_active)
971 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
972 new_active->dev->addr_len);
973 break;
974 case BOND_FOM_FOLLOW:
975 /*
976 * if new_active && old_active, swap them
977 * if just old_active, do nothing (going to no active slave)
978 * if just new_active, set new_active to bond's MAC
979 */
980 if (!new_active)
981 return;
982
983 write_unlock_bh(&bond->curr_slave_lock);
984 read_unlock(&bond->lock);
985
986 if (old_active) {
987 memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
988 memcpy(saddr.sa_data, old_active->dev->dev_addr,
989 ETH_ALEN);
990 saddr.sa_family = new_active->dev->type;
991 } else {
992 memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
993 saddr.sa_family = bond->dev->type;
994 }
995
996 rv = dev_set_mac_address(new_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 Vosburgh3915c1e82008-05-17 21:10:14 -0700999 bond->dev->name, -rv, new_active->dev->name);
1000 goto out;
1001 }
1002
1003 if (!old_active)
1004 goto out;
1005
1006 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
1007 saddr.sa_family = old_active->dev->type;
1008
1009 rv = dev_set_mac_address(old_active->dev, &saddr);
1010 if (rv)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001011 pr_err("%s: Error %d setting MAC of slave %s\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001012 bond->dev->name, -rv, new_active->dev->name);
1013out:
1014 read_lock(&bond->lock);
1015 write_lock_bh(&bond->curr_slave_lock);
1016 break;
1017 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001018 pr_err("%s: bond_do_fail_over_mac impossible: bad policy %d\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001019 bond->dev->name, bond->params.fail_over_mac);
1020 break;
1021 }
1022
1023}
1024
Jiri Pirkoa5499522009-09-25 03:28:09 +00001025static bool bond_should_change_active(struct bonding *bond)
1026{
1027 struct slave *prim = bond->primary_slave;
1028 struct slave *curr = bond->curr_active_slave;
1029
1030 if (!prim || !curr || curr->link != BOND_LINK_UP)
1031 return true;
1032 if (bond->force_primary) {
1033 bond->force_primary = false;
1034 return true;
1035 }
1036 if (bond->params.primary_reselect == BOND_PRI_RESELECT_BETTER &&
1037 (prim->speed < curr->speed ||
1038 (prim->speed == curr->speed && prim->duplex <= curr->duplex)))
1039 return false;
1040 if (bond->params.primary_reselect == BOND_PRI_RESELECT_FAILURE)
1041 return false;
1042 return true;
1043}
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045/**
1046 * find_best_interface - select the best available slave to be the active one
1047 * @bond: our bonding struct
1048 *
1049 * Warning: Caller must hold curr_slave_lock for writing.
1050 */
1051static struct slave *bond_find_best_slave(struct bonding *bond)
1052{
1053 struct slave *new_active, *old_active;
1054 struct slave *bestslave = NULL;
1055 int mintime = bond->params.updelay;
1056 int i;
1057
Nicolas de Pesloüan49b4ad92009-10-07 14:11:00 -07001058 new_active = bond->curr_active_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 if (!new_active) { /* there were no active slaves left */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001061 if (bond->slave_cnt > 0) /* found one slave */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 new_active = bond->first_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001063 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return NULL; /* still no slave, return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if ((bond->primary_slave) &&
Jiri Pirkoa5499522009-09-25 03:28:09 +00001068 bond->primary_slave->link == BOND_LINK_UP &&
1069 bond_should_change_active(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 new_active = bond->primary_slave;
1071 }
1072
1073 /* remember where to stop iterating over the slaves */
1074 old_active = new_active;
1075
1076 bond_for_each_slave_from(bond, new_active, i, old_active) {
Jiri Pirkob9f60252009-08-31 11:09:38 +00001077 if (new_active->link == BOND_LINK_UP) {
1078 return new_active;
1079 } else if (new_active->link == BOND_LINK_BACK &&
1080 IS_UP(new_active->dev)) {
1081 /* link up, but waiting for stabilization */
1082 if (new_active->delay < mintime) {
1083 mintime = new_active->delay;
1084 bestslave = new_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086 }
1087 }
1088
1089 return bestslave;
1090}
1091
1092/**
1093 * change_active_interface - change the active slave into the specified one
1094 * @bond: our bonding struct
1095 * @new: the new slave to make the active one
1096 *
1097 * Set the new slave to the bond's settings and unset them on the old
1098 * curr_active_slave.
1099 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1100 *
1101 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1102 * because it is apparently the best available slave we have, even though its
1103 * updelay hasn't timed out yet.
1104 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001105 * If new_active is not NULL, caller must hold bond->lock for read and
1106 * curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001108void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
1110 struct slave *old_active = bond->curr_active_slave;
1111
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001112 if (old_active == new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 if (new_active) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07001116 new_active->jiffies = jiffies;
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (new_active->link == BOND_LINK_BACK) {
1119 if (USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001120 pr_info("%s: making interface %s the new active one %d ms earlier.\n",
1121 bond->dev->name, new_active->dev->name,
1122 (bond->params.updelay - new_active->delay) * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124
1125 new_active->delay = 0;
1126 new_active->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001128 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Holger Eitzenberger58402052008-12-09 23:07:13 -08001131 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 } else {
1134 if (USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001135 pr_info("%s: making interface %s the new active one.\n",
1136 bond->dev->name, new_active->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138 }
1139 }
1140
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001141 if (USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 bond_mc_swap(bond, new_active, old_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Holger Eitzenberger58402052008-12-09 23:07:13 -08001144 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 bond_alb_handle_active_change(bond, new_active);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001146 if (old_active)
1147 bond_set_slave_inactive_flags(old_active);
1148 if (new_active)
1149 bond_set_slave_active_flags(new_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 } else {
1151 bond->curr_active_slave = new_active;
1152 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001153
1154 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001155 if (old_active)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001156 bond_set_slave_inactive_flags(old_active);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001157
1158 if (new_active) {
1159 bond_set_slave_active_flags(new_active);
Moni Shoua2ab82852007-10-09 19:43:39 -07001160
Or Gerlitz709f8a42008-06-13 18:12:01 -07001161 if (bond->params.fail_over_mac)
1162 bond_do_fail_over_mac(bond, new_active,
1163 old_active);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001164
Ben Hutchingsffa95ed2010-12-13 08:19:56 +00001165 if (netif_running(bond->dev)) {
1166 bond->send_grat_arp = bond->params.num_grat_arp;
1167 bond_send_gratuitous_arp(bond);
Or Gerlitz01f31092008-06-13 18:12:02 -07001168
Ben Hutchingsffa95ed2010-12-13 08:19:56 +00001169 bond->send_unsol_na = bond->params.num_unsol_na;
1170 bond_send_unsolicited_na(bond);
1171 }
Brian Haley305d5522008-11-04 17:51:14 -08001172
Or Gerlitz01f31092008-06-13 18:12:02 -07001173 write_unlock_bh(&bond->curr_slave_lock);
1174 read_unlock(&bond->lock);
1175
Moni Shoua75c78502009-09-15 02:37:40 -07001176 netdev_bonding_change(bond->dev, NETDEV_BONDING_FAILOVER);
Or Gerlitz01f31092008-06-13 18:12:02 -07001177
1178 read_lock(&bond->lock);
1179 write_lock_bh(&bond->curr_slave_lock);
Moni Shoua7893b242008-05-17 21:10:12 -07001180 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001181 }
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001182
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001183 /* resend IGMP joins since active slave has changed or
1184 * all were sent on curr_active_slave */
Ben Hutchingsffa95ed2010-12-13 08:19:56 +00001185 if (((USES_PRIMARY(bond->params.mode) && new_active) ||
1186 bond->params.mode == BOND_MODE_ROUNDROBIN) &&
1187 netif_running(bond->dev)) {
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001188 bond->igmp_retrans = bond->params.resend_igmp;
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001189 queue_delayed_work(bond->wq, &bond->mcast_work, 0);
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
1193/**
1194 * bond_select_active_slave - select a new active slave, if needed
1195 * @bond: our bonding struct
1196 *
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001197 * This functions should be called when one of the following occurs:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 * - The old curr_active_slave has been released or lost its link.
1199 * - The primary_slave has got its link back.
1200 * - A slave has got its link back and there's no old curr_active_slave.
1201 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001202 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001204void bond_select_active_slave(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
1206 struct slave *best_slave;
Jay Vosburghff59c452006-03-27 13:27:43 -08001207 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 best_slave = bond_find_best_slave(bond);
1210 if (best_slave != bond->curr_active_slave) {
1211 bond_change_active_slave(bond, best_slave);
Jay Vosburghff59c452006-03-27 13:27:43 -08001212 rv = bond_set_carrier(bond);
1213 if (!rv)
1214 return;
1215
1216 if (netif_carrier_ok(bond->dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001217 pr_info("%s: first active interface up!\n",
1218 bond->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08001219 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001220 pr_info("%s: now running without any active interface !\n",
1221 bond->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08001222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224}
1225
1226/*--------------------------- slave list handling ---------------------------*/
1227
1228/*
1229 * This function attaches the slave to the end of list.
1230 *
1231 * bond->lock held for writing by caller.
1232 */
1233static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1234{
1235 if (bond->first_slave == NULL) { /* attaching the first slave */
1236 new_slave->next = new_slave;
1237 new_slave->prev = new_slave;
1238 bond->first_slave = new_slave;
1239 } else {
1240 new_slave->next = bond->first_slave;
1241 new_slave->prev = bond->first_slave->prev;
1242 new_slave->next->prev = new_slave;
1243 new_slave->prev->next = new_slave;
1244 }
1245
1246 bond->slave_cnt++;
1247}
1248
1249/*
1250 * This function detaches the slave from the list.
1251 * WARNING: no check is made to verify if the slave effectively
1252 * belongs to <bond>.
1253 * Nothing is freed on return, structures are just unchained.
1254 * If any slave pointer in bond was pointing to <slave>,
1255 * it should be changed by the calling function.
1256 *
1257 * bond->lock held for writing by caller.
1258 */
1259static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1260{
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001261 if (slave->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 slave->next->prev = slave->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001264 if (slave->prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 slave->prev->next = slave->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 if (bond->first_slave == slave) { /* slave is the first slave */
1268 if (bond->slave_cnt > 1) { /* there are more slave */
1269 bond->first_slave = slave->next;
1270 } else {
1271 bond->first_slave = NULL; /* slave was the last one */
1272 }
1273 }
1274
1275 slave->next = NULL;
1276 slave->prev = NULL;
1277 bond->slave_cnt--;
1278}
1279
WANG Congf6dc31a2010-05-06 00:48:51 -07001280#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001281static inline int slave_enable_netpoll(struct slave *slave)
WANG Congf6dc31a2010-05-06 00:48:51 -07001282{
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001283 struct netpoll *np;
1284 int err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001285
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001286 np = kzalloc(sizeof(*np), GFP_KERNEL);
1287 err = -ENOMEM;
1288 if (!np)
1289 goto out;
1290
1291 np->dev = slave->dev;
1292 err = __netpoll_setup(np);
1293 if (err) {
1294 kfree(np);
1295 goto out;
WANG Congf6dc31a2010-05-06 00:48:51 -07001296 }
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001297 slave->np = np;
1298out:
1299 return err;
1300}
1301static inline void slave_disable_netpoll(struct slave *slave)
1302{
1303 struct netpoll *np = slave->np;
1304
1305 if (!np)
1306 return;
1307
1308 slave->np = NULL;
1309 synchronize_rcu_bh();
1310 __netpoll_cleanup(np);
1311 kfree(np);
1312}
1313static inline bool slave_dev_support_netpoll(struct net_device *slave_dev)
1314{
1315 if (slave_dev->priv_flags & IFF_DISABLE_NETPOLL)
1316 return false;
1317 if (!slave_dev->netdev_ops->ndo_poll_controller)
1318 return false;
1319 return true;
WANG Congf6dc31a2010-05-06 00:48:51 -07001320}
1321
1322static void bond_poll_controller(struct net_device *bond_dev)
1323{
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001324}
1325
1326static void __bond_netpoll_cleanup(struct bonding *bond)
1327{
Neil Hormanc2355e12010-10-13 16:01:49 +00001328 struct slave *slave;
1329 int i;
1330
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001331 bond_for_each_slave(bond, slave, i)
1332 if (IS_UP(slave->dev))
1333 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07001334}
WANG Congf6dc31a2010-05-06 00:48:51 -07001335static void bond_netpoll_cleanup(struct net_device *bond_dev)
1336{
1337 struct bonding *bond = netdev_priv(bond_dev);
WANG Congf6dc31a2010-05-06 00:48:51 -07001338
1339 read_lock(&bond->lock);
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001340 __bond_netpoll_cleanup(bond);
WANG Congf6dc31a2010-05-06 00:48:51 -07001341 read_unlock(&bond->lock);
1342}
1343
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001344static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
1345{
1346 struct bonding *bond = netdev_priv(dev);
1347 struct slave *slave;
1348 int i, err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001349
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001350 read_lock(&bond->lock);
1351 bond_for_each_slave(bond, slave, i) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001352 err = slave_enable_netpoll(slave);
1353 if (err) {
1354 __bond_netpoll_cleanup(bond);
1355 break;
1356 }
1357 }
1358 read_unlock(&bond->lock);
1359 return err;
1360}
1361
1362static struct netpoll_info *bond_netpoll_info(struct bonding *bond)
1363{
1364 return bond->dev->npinfo;
1365}
1366
1367#else
1368static inline int slave_enable_netpoll(struct slave *slave)
1369{
1370 return 0;
1371}
1372static inline void slave_disable_netpoll(struct slave *slave)
1373{
1374}
WANG Congf6dc31a2010-05-06 00:48:51 -07001375static void bond_netpoll_cleanup(struct net_device *bond_dev)
1376{
1377}
WANG Congf6dc31a2010-05-06 00:48:51 -07001378#endif
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380/*---------------------------------- IOCTL ----------------------------------*/
1381
Adrian Bunk4ad072c2007-07-09 11:51:12 -07001382static int bond_sethwaddr(struct net_device *bond_dev,
1383 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001385 pr_debug("bond_dev=%p\n", bond_dev);
1386 pr_debug("slave_dev=%p\n", slave_dev);
1387 pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1389 return 0;
1390}
1391
Herbert Xu7f353bf2007-08-10 15:47:58 -07001392#define BOND_VLAN_FEATURES \
1393 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1394 NETIF_F_HW_VLAN_FILTER)
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001395
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001396/*
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001397 * Compute the common dev->feature set available to all slaves. Some
Herbert Xu7f353bf2007-08-10 15:47:58 -07001398 * feature bits are managed elsewhere, so preserve those feature bits
1399 * on the master device.
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001400 */
1401static int bond_compute_features(struct bonding *bond)
1402{
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001403 struct slave *slave;
1404 struct net_device *bond_dev = bond->dev;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08001405 u32 features = bond_dev->features;
1406 u32 vlan_features = 0;
Moni Shoua3158bf72007-10-09 19:43:41 -07001407 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1408 bond_dev->hard_header_len);
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001409 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001410
Herbert Xu7f353bf2007-08-10 15:47:58 -07001411 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
Tom Herbertc6e1a0d2011-04-04 22:30:30 -07001412 features |= NETIF_F_GSO_MASK | NETIF_F_NO_CSUM | NETIF_F_NOCACHE_COPY;
Herbert Xub63365a2008-10-23 01:11:29 -07001413
1414 if (!bond->first_slave)
1415 goto done;
1416
1417 features &= ~NETIF_F_ONE_FOR_ALL;
Herbert Xu7f353bf2007-08-10 15:47:58 -07001418
Jay Vosburgh278339a2009-08-28 12:05:12 +00001419 vlan_features = bond->first_slave->dev->vlan_features;
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001420 bond_for_each_slave(bond, slave, i) {
Herbert Xub63365a2008-10-23 01:11:29 -07001421 features = netdev_increment_features(features,
1422 slave->dev->features,
1423 NETIF_F_ONE_FOR_ALL);
Jay Vosburgh278339a2009-08-28 12:05:12 +00001424 vlan_features = netdev_increment_features(vlan_features,
1425 slave->dev->vlan_features,
1426 NETIF_F_ONE_FOR_ALL);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001427 if (slave->dev->hard_header_len > max_hard_header_len)
1428 max_hard_header_len = slave->dev->hard_header_len;
1429 }
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001430
Herbert Xub63365a2008-10-23 01:11:29 -07001431done:
Herbert Xu7f353bf2007-08-10 15:47:58 -07001432 features |= (bond_dev->features & BOND_VLAN_FEATURES);
Michał Mirosławacd11302011-01-24 15:45:15 -08001433 bond_dev->features = netdev_fix_features(bond_dev, features);
1434 bond_dev->vlan_features = netdev_fix_features(bond_dev, vlan_features);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001435 bond_dev->hard_header_len = max_hard_header_len;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001436
1437 return 0;
1438}
1439
Moni Shoua872254d2007-10-09 19:43:38 -07001440static void bond_setup_by_slave(struct net_device *bond_dev,
1441 struct net_device *slave_dev)
1442{
Wang Chen454d7c92008-11-12 23:37:49 -08001443 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07001444
Stephen Hemminger00829822008-11-20 20:14:53 -08001445 bond_dev->header_ops = slave_dev->header_ops;
Moni Shoua872254d2007-10-09 19:43:38 -07001446
1447 bond_dev->type = slave_dev->type;
1448 bond_dev->hard_header_len = slave_dev->hard_header_len;
1449 bond_dev->addr_len = slave_dev->addr_len;
1450
1451 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1452 slave_dev->addr_len);
Moni Shouad90a1622007-10-09 19:43:43 -07001453 bond->setup_by_slave = 1;
Moni Shoua872254d2007-10-09 19:43:38 -07001454}
1455
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001456/* On bonding slaves other than the currently active slave, suppress
1457 * duplicates except for 802.3ad ETH_P_SLOW, alb non-mcast/bcast, and
1458 * ARP on active-backup slaves with arp_validate enabled.
1459 */
1460static bool bond_should_deliver_exact_match(struct sk_buff *skb,
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001461 struct slave *slave,
1462 struct bonding *bond)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001463{
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001464 if (bond_is_slave_inactive(slave)) {
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001465 if (slave_do_arp_validate(bond, slave) &&
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001466 skb->protocol == __cpu_to_be16(ETH_P_ARP))
1467 return false;
1468
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001469 if (bond->params.mode == BOND_MODE_ALB &&
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001470 skb->pkt_type != PACKET_BROADCAST &&
1471 skb->pkt_type != PACKET_MULTICAST)
1472 return false;
1473
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001474 if (bond->params.mode == BOND_MODE_8023AD &&
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001475 skb->protocol == __cpu_to_be16(ETH_P_SLOW))
1476 return false;
1477
1478 return true;
1479 }
1480 return false;
1481}
1482
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001483static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001484{
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001485 struct sk_buff *skb = *pskb;
Jiri Pirkof1c17752011-03-12 03:14:35 +00001486 struct slave *slave;
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001487 struct bonding *bond;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001488
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001489 skb = skb_share_check(skb, GFP_ATOMIC);
1490 if (unlikely(!skb))
1491 return RX_HANDLER_CONSUMED;
1492
1493 *pskb = skb;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001494
Jiri Pirko35d48902011-03-22 02:38:12 +00001495 slave = bond_slave_get_rcu(skb->dev);
1496 bond = slave->bond;
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001497
1498 if (bond->params.arp_interval)
Jiri Pirkof1c17752011-03-12 03:14:35 +00001499 slave->dev->last_rx = jiffies;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001500
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001501 if (bond_should_deliver_exact_match(skb, slave, bond)) {
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001502 return RX_HANDLER_EXACT;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001503 }
1504
Jiri Pirko35d48902011-03-22 02:38:12 +00001505 skb->dev = bond->dev;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001506
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001507 if (bond->params.mode == BOND_MODE_ALB &&
Jiri Pirko35d48902011-03-22 02:38:12 +00001508 bond->dev->priv_flags & IFF_BRIDGE_PORT &&
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001509 skb->pkt_type == PACKET_HOST) {
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001510
Changli Gao541ac7c2011-03-02 21:07:14 +00001511 if (unlikely(skb_cow_head(skb,
1512 skb->data - skb_mac_header(skb)))) {
1513 kfree_skb(skb);
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001514 return RX_HANDLER_CONSUMED;
Changli Gao541ac7c2011-03-02 21:07:14 +00001515 }
Jiri Pirko35d48902011-03-22 02:38:12 +00001516 memcpy(eth_hdr(skb)->h_dest, bond->dev->dev_addr, ETH_ALEN);
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001517 }
1518
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001519 return RX_HANDLER_ANOTHER;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001520}
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522/* enslave device <slave> to bond device <master> */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001523int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Wang Chen454d7c92008-11-12 23:37:49 -08001525 struct bonding *bond = netdev_priv(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001526 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 struct slave *new_slave = NULL;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001528 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 struct sockaddr addr;
1530 int link_reporting;
1531 int old_features = bond_dev->features;
1532 int res = 0;
1533
nsxfreddy@gmail.com552709d2005-09-21 14:18:04 -05001534 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001535 slave_ops->ndo_do_ioctl == NULL) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001536 pr_warning("%s: Warning: no link monitoring support for %s\n",
1537 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539
1540 /* bond must be initialized by bond_open() before enslaving */
1541 if (!(bond_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001542 pr_warning("%s: master_dev is not up in bond_enslave\n",
1543 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545
1546 /* already enslaved */
1547 if (slave_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001548 pr_debug("Error, Device was already enslaved\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return -EBUSY;
1550 }
1551
1552 /* vlan challenged mutual exclusion */
1553 /* no need to lock since we're protected by rtnl_lock */
1554 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001555 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Jay Vosburghf35188f2010-07-21 12:14:47 +00001556 if (bond->vlgrp) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001557 pr_err("%s: Error: cannot enslave VLAN challenged slave %s on VLAN enabled bond %s\n",
1558 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return -EPERM;
1560 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001561 pr_warning("%s: Warning: enslaved VLAN challenged slave %s. Adding VLANs will be blocked as long as %s is part of bond %s\n",
1562 bond_dev->name, slave_dev->name,
1563 slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1565 }
1566 } else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001567 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (bond->slave_cnt == 0) {
1569 /* First slave, and it is not VLAN challenged,
1570 * so remove the block of adding VLANs over the bond.
1571 */
1572 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1573 }
1574 }
1575
Jay Vosburgh217df672005-09-26 16:11:50 -07001576 /*
1577 * Old ifenslave binaries are no longer supported. These can
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001578 * be identified with moderate accuracy by the state of the slave:
Jay Vosburgh217df672005-09-26 16:11:50 -07001579 * the current ifenslave will set the interface down prior to
1580 * enslaving it; the old ifenslave will not.
1581 */
1582 if ((slave_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001583 pr_err("%s is up. This may be due to an out of date ifenslave.\n",
Jay Vosburgh217df672005-09-26 16:11:50 -07001584 slave_dev->name);
1585 res = -EPERM;
1586 goto err_undo_flags;
1587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Moni Shoua872254d2007-10-09 19:43:38 -07001589 /* set bonding device ether type by slave - bonding netdevices are
1590 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1591 * there is a need to override some of the type dependent attribs/funcs.
1592 *
1593 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1594 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1595 */
1596 if (bond->slave_cnt == 0) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001597 if (bond_dev->type != slave_dev->type) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001598 pr_debug("%s: change device type from %d to %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001599 bond_dev->name,
1600 bond_dev->type, slave_dev->type);
Moni Shoua75c78502009-09-15 02:37:40 -07001601
Jiri Pirko3ca5b402010-03-10 10:29:35 +00001602 res = netdev_bonding_change(bond_dev,
1603 NETDEV_PRE_TYPE_CHANGE);
1604 res = notifier_to_errno(res);
1605 if (res) {
1606 pr_err("%s: refused to change device type\n",
1607 bond_dev->name);
1608 res = -EBUSY;
1609 goto err_undo_flags;
1610 }
Moni Shoua75c78502009-09-15 02:37:40 -07001611
Jiri Pirko32a806c2010-03-19 04:00:23 +00001612 /* Flush unicast and multicast addresses */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001613 dev_uc_flush(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001614 dev_mc_flush(bond_dev);
Jiri Pirko32a806c2010-03-19 04:00:23 +00001615
Moni Shouae36b9d12009-07-15 04:56:31 +00001616 if (slave_dev->type != ARPHRD_ETHER)
1617 bond_setup_by_slave(bond_dev, slave_dev);
1618 else
1619 ether_setup(bond_dev);
Moni Shoua75c78502009-09-15 02:37:40 -07001620
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001621 netdev_bonding_change(bond_dev,
1622 NETDEV_POST_TYPE_CHANGE);
Moni Shouae36b9d12009-07-15 04:56:31 +00001623 }
Moni Shoua872254d2007-10-09 19:43:38 -07001624 } else if (bond_dev->type != slave_dev->type) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001625 pr_err("%s ether type (%d) is different from other slaves (%d), can not enslave it.\n",
1626 slave_dev->name,
1627 slave_dev->type, bond_dev->type);
1628 res = -EINVAL;
1629 goto err_undo_flags;
Moni Shoua872254d2007-10-09 19:43:38 -07001630 }
1631
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001632 if (slave_ops->ndo_set_mac_address == NULL) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001633 if (bond->slave_cnt == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001634 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.",
1635 bond_dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001636 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1637 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001638 pr_err("%s: Error: The slave device specified does not support setting the MAC address, but fail_over_mac is not set to active.\n",
1639 bond_dev->name);
Moni Shoua2ab82852007-10-09 19:43:39 -07001640 res = -EOPNOTSUPP;
1641 goto err_undo_flags;
1642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001645 /* If this is the first slave, then we need to set the master's hardware
1646 * address to be the same as the slave's. */
David Strandd13a2cb2010-12-01 11:43:08 -08001647 if (is_zero_ether_addr(bond->dev->dev_addr))
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001648 memcpy(bond->dev->dev_addr, slave_dev->dev_addr,
1649 slave_dev->addr_len);
1650
1651
Joe Jin243cb4e2007-02-06 14:16:40 -08001652 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 if (!new_slave) {
1654 res = -ENOMEM;
1655 goto err_undo_flags;
1656 }
1657
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001658 /*
1659 * Set the new_slave's queue_id to be zero. Queue ID mapping
1660 * is set via sysfs or module option if desired.
1661 */
1662 new_slave->queue_id = 0;
1663
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001664 /* Save slave's original mtu and then set it to match the bond */
1665 new_slave->original_mtu = slave_dev->mtu;
1666 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1667 if (res) {
1668 pr_debug("Error %d calling dev_set_mtu\n", res);
1669 goto err_free;
1670 }
1671
Jay Vosburgh217df672005-09-26 16:11:50 -07001672 /*
1673 * Save slave's original ("permanent") mac address for modes
1674 * that need it, and for restoring it upon release, and then
1675 * set it to the master's address
1676 */
1677 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Jay Vosburghdd957c52007-10-09 19:57:24 -07001679 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001680 /*
1681 * Set slave to master's mac address. The application already
1682 * set the master's mac address to that of the first slave
1683 */
1684 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1685 addr.sa_family = slave_dev->type;
1686 res = dev_set_mac_address(slave_dev, &addr);
1687 if (res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001688 pr_debug("Error %d calling set_mac_address\n", res);
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001689 goto err_restore_mtu;
Moni Shoua2ab82852007-10-09 19:43:39 -07001690 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Jiri Pirko1765a572011-02-12 06:48:36 +00001693 res = netdev_set_bond_master(slave_dev, bond_dev);
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001694 if (res) {
Jiri Pirko1765a572011-02-12 06:48:36 +00001695 pr_debug("Error %d calling netdev_set_bond_master\n", res);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001696 goto err_restore_mac;
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001697 }
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001698
Jay Vosburgh217df672005-09-26 16:11:50 -07001699 /* open the slave since the application closed it */
1700 res = dev_open(slave_dev);
1701 if (res) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001702 pr_debug("Opening slave %s failed\n", slave_dev->name);
Jiri Pirko35d48902011-03-22 02:38:12 +00001703 goto err_unset_master;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
1705
Jiri Pirko35d48902011-03-22 02:38:12 +00001706 new_slave->bond = bond;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 new_slave->dev = slave_dev;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001708 slave_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Holger Eitzenberger58402052008-12-09 23:07:13 -08001710 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 /* bond_alb_init_slave() must be called before all other stages since
1712 * it might fail and we do not want to have to undo everything
1713 */
1714 res = bond_alb_init_slave(bond, new_slave);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001715 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001716 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 }
1718
1719 /* If the mode USES_PRIMARY, then the new slave gets the
1720 * master's promisc (and mc) settings only if it becomes the
1721 * curr_active_slave, and that is taken care of later when calling
1722 * bond_change_active()
1723 */
1724 if (!USES_PRIMARY(bond->params.mode)) {
1725 /* set promiscuity level to new slave */
1726 if (bond_dev->flags & IFF_PROMISC) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001727 res = dev_set_promiscuity(slave_dev, 1);
1728 if (res)
1729 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 }
1731
1732 /* set allmulti level to new slave */
1733 if (bond_dev->flags & IFF_ALLMULTI) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001734 res = dev_set_allmulti(slave_dev, 1);
1735 if (res)
1736 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
1738
David S. Millerb9e40852008-07-15 00:15:08 -07001739 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 /* upload master's mc_list to new slave */
Jiri Pirko22bedad32010-04-01 21:22:57 +00001741 netdev_for_each_mc_addr(ha, bond_dev)
1742 dev_mc_add(slave_dev, ha->addr);
David S. Millerb9e40852008-07-15 00:15:08 -07001743 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
1745
1746 if (bond->params.mode == BOND_MODE_8023AD) {
1747 /* add lacpdu mc addr to mc list */
1748 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1749
Jiri Pirko22bedad32010-04-01 21:22:57 +00001750 dev_mc_add(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
1752
1753 bond_add_vlans_on_slave(bond, slave_dev);
1754
1755 write_lock_bh(&bond->lock);
1756
1757 bond_attach_slave(bond, new_slave);
1758
1759 new_slave->delay = 0;
1760 new_slave->link_failure_count = 0;
1761
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001762 bond_compute_features(bond);
1763
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001764 write_unlock_bh(&bond->lock);
1765
1766 read_lock(&bond->lock);
1767
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001768 new_slave->last_arp_rx = jiffies;
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (bond->params.miimon && !bond->params.use_carrier) {
1771 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1772
1773 if ((link_reporting == -1) && !bond->params.arp_interval) {
1774 /*
1775 * miimon is set but a bonded network driver
1776 * does not support ETHTOOL/MII and
1777 * arp_interval is not set. Note: if
1778 * use_carrier is enabled, we will never go
1779 * here (because netif_carrier is always
1780 * supported); thus, we don't need to change
1781 * the messages for netif_carrier.
1782 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001783 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 -08001784 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 } else if (link_reporting == -1) {
1786 /* unable get link status using mii/ethtool */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001787 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",
1788 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
1790 }
1791
1792 /* check for initial state */
1793 if (!bond->params.miimon ||
1794 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1795 if (bond->params.updelay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001796 pr_debug("Initial state of slave_dev is BOND_LINK_BACK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 new_slave->link = BOND_LINK_BACK;
1798 new_slave->delay = bond->params.updelay;
1799 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001800 pr_debug("Initial state of slave_dev is BOND_LINK_UP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 new_slave->link = BOND_LINK_UP;
1802 }
1803 new_slave->jiffies = jiffies;
1804 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001805 pr_debug("Initial state of slave_dev is BOND_LINK_DOWN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 new_slave->link = BOND_LINK_DOWN;
1807 }
1808
1809 if (bond_update_speed_duplex(new_slave) &&
1810 (new_slave->link != BOND_LINK_DOWN)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001811 pr_warning("%s: Warning: failed to get speed and duplex from %s, assumed to be 100Mb/sec and Full.\n",
1812 bond_dev->name, new_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 if (bond->params.mode == BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001815 pr_warning("%s: Warning: Operation of 802.3ad mode requires ETHTOOL support in base driver for proper aggregator selection.\n",
1816 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 }
1818 }
1819
1820 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1821 /* if there is a primary slave, remember it */
Jiri Pirkoa5499522009-09-25 03:28:09 +00001822 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 bond->primary_slave = new_slave;
Jiri Pirkoa5499522009-09-25 03:28:09 +00001824 bond->force_primary = true;
1825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
1827
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001828 write_lock_bh(&bond->curr_slave_lock);
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 switch (bond->params.mode) {
1831 case BOND_MODE_ACTIVEBACKUP:
Jay Vosburgh8a8e4472006-09-22 21:56:15 -07001832 bond_set_slave_inactive_flags(new_slave);
1833 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 break;
1835 case BOND_MODE_8023AD:
1836 /* in 802.3ad mode, the internal mechanism
1837 * will activate the slaves in the selected
1838 * aggregator
1839 */
1840 bond_set_slave_inactive_flags(new_slave);
1841 /* if this is the first slave */
1842 if (bond->slave_cnt == 1) {
1843 SLAVE_AD_INFO(new_slave).id = 1;
1844 /* Initialize AD with the number of times that the AD timer is called in 1 second
1845 * can be called only after the mac address of the bond is set
1846 */
1847 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1848 bond->params.lacp_fast);
1849 } else {
1850 SLAVE_AD_INFO(new_slave).id =
1851 SLAVE_AD_INFO(new_slave->prev).id + 1;
1852 }
1853
1854 bond_3ad_bind_slave(new_slave);
1855 break;
1856 case BOND_MODE_TLB:
1857 case BOND_MODE_ALB:
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001858 bond_set_active_slave(new_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001859 bond_set_slave_inactive_flags(new_slave);
Jiri Pirko5a29f782009-03-25 17:23:38 -07001860 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 break;
1862 default:
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001863 pr_debug("This slave is always active in trunk mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 /* always active in trunk mode */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001866 bond_set_active_slave(new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 /* In trunking mode there is little meaning to curr_active_slave
1869 * anyway (it holds no special properties of the bond device),
1870 * so we can change it without calling change_active_interface()
1871 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001872 if (!bond->curr_active_slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 bond->curr_active_slave = new_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 break;
1876 } /* switch(bond_mode) */
1877
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001878 write_unlock_bh(&bond->curr_slave_lock);
1879
Jay Vosburghff59c452006-03-27 13:27:43 -08001880 bond_set_carrier(bond);
1881
WANG Congf6dc31a2010-05-06 00:48:51 -07001882#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001883 slave_dev->npinfo = bond_netpoll_info(bond);
1884 if (slave_dev->npinfo) {
1885 if (slave_enable_netpoll(new_slave)) {
1886 read_unlock(&bond->lock);
1887 pr_info("Error, %s: master_dev is using netpoll, "
1888 "but new slave device does not support netpoll.\n",
1889 bond_dev->name);
1890 res = -EBUSY;
1891 goto err_close;
1892 }
WANG Congf6dc31a2010-05-06 00:48:51 -07001893 }
1894#endif
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001895
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001896 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001898 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1899 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001900 goto err_close;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001901
Jiri Pirko35d48902011-03-22 02:38:12 +00001902 res = netdev_rx_handler_register(slave_dev, bond_handle_frame,
1903 new_slave);
1904 if (res) {
1905 pr_debug("Error %d calling netdev_rx_handler_register\n", res);
1906 goto err_dest_symlinks;
1907 }
1908
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001909 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
1910 bond_dev->name, slave_dev->name,
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001911 bond_is_active_slave(new_slave) ? "n active" : " backup",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001912 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 /* enslave is successful */
1915 return 0;
1916
1917/* Undo stages on error */
Jiri Pirko35d48902011-03-22 02:38:12 +00001918err_dest_symlinks:
1919 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921err_close:
1922 dev_close(slave_dev);
1923
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001924err_unset_master:
Jiri Pirko1765a572011-02-12 06:48:36 +00001925 netdev_set_bond_master(slave_dev, NULL);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927err_restore_mac:
Jay Vosburghdd957c52007-10-09 19:57:24 -07001928 if (!bond->params.fail_over_mac) {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001929 /* XXX TODO - fom follow mode needs to change master's
1930 * MAC if this slave's MAC is in use by the bond, or at
1931 * least print a warning.
1932 */
Moni Shoua2ab82852007-10-09 19:43:39 -07001933 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1934 addr.sa_family = slave_dev->type;
1935 dev_set_mac_address(slave_dev, &addr);
1936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001938err_restore_mtu:
1939 dev_set_mtu(slave_dev, new_slave->original_mtu);
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941err_free:
1942 kfree(new_slave);
1943
1944err_undo_flags:
1945 bond_dev->features = old_features;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return res;
1948}
1949
1950/*
1951 * Try to release the slave device <slave> from the bond device <master>
1952 * It is legal to access curr_active_slave without a lock because all the function
1953 * is write-locked.
1954 *
1955 * The rules for slave state should be:
1956 * for Active/Backup:
1957 * Active stays on all backups go down
1958 * for Bonded connections:
1959 * The first up interface should be left on and all others downed.
1960 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001961int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Wang Chen454d7c92008-11-12 23:37:49 -08001963 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 struct slave *slave, *oldcurrent;
1965 struct sockaddr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 /* slave is not a slave or master is not master of this slave */
1968 if (!(slave_dev->flags & IFF_SLAVE) ||
1969 (slave_dev->master != bond_dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001970 pr_err("%s: Error: cannot release %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 bond_dev->name, slave_dev->name);
1972 return -EINVAL;
1973 }
1974
Neil Hormane843fa52010-10-13 16:01:50 +00001975 block_netpoll_tx();
WANG Congf6dc31a2010-05-06 00:48:51 -07001976 netdev_bonding_change(bond_dev, NETDEV_BONDING_DESLAVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 write_lock_bh(&bond->lock);
1978
1979 slave = bond_get_slave_by_dev(bond, slave_dev);
1980 if (!slave) {
1981 /* not a slave of this bond */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001982 pr_info("%s: %s not enslaved\n",
1983 bond_dev->name, slave_dev->name);
Jay Vosburghf5e2a7b2006-02-07 21:17:22 -08001984 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001985 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return -EINVAL;
1987 }
1988
Jiri Pirko35d48902011-03-22 02:38:12 +00001989 /* unregister rx_handler early so bond_handle_frame wouldn't be called
1990 * for this slave anymore.
1991 */
1992 netdev_rx_handler_unregister(slave_dev);
1993 write_unlock_bh(&bond->lock);
1994 synchronize_net();
1995 write_lock_bh(&bond->lock);
1996
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001997 if (!bond->params.fail_over_mac) {
Joe Perches8e95a202009-12-03 07:58:21 +00001998 if (!compare_ether_addr(bond_dev->dev_addr, slave->perm_hwaddr) &&
1999 bond->slave_cnt > 1)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002000 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",
2001 bond_dev->name, slave_dev->name,
2002 slave->perm_hwaddr,
2003 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
2005
2006 /* Inform AD package of unbinding of slave. */
2007 if (bond->params.mode == BOND_MODE_8023AD) {
2008 /* must be called before the slave is
2009 * detached from the list
2010 */
2011 bond_3ad_unbind_slave(slave);
2012 }
2013
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002014 pr_info("%s: releasing %s interface %s\n",
2015 bond_dev->name,
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002016 bond_is_active_slave(slave) ? "active" : "backup",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002017 slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019 oldcurrent = bond->curr_active_slave;
2020
2021 bond->current_arp_slave = NULL;
2022
2023 /* release the slave from its bond */
2024 bond_detach_slave(bond, slave);
2025
Arthur Kepner8531c5f2005-08-23 01:34:53 -04002026 bond_compute_features(bond);
2027
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002028 if (bond->primary_slave == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 bond->primary_slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002031 if (oldcurrent == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 bond_change_active_slave(bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Holger Eitzenberger58402052008-12-09 23:07:13 -08002034 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 /* Must be called only after the slave has been
2036 * detached from the list and the curr_active_slave
2037 * has been cleared (if our_slave == old_current),
2038 * but before a new active slave is selected.
2039 */
Jay Vosburgh25433312008-01-17 16:24:59 -08002040 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 bond_alb_deinit_slave(bond, slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08002042 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
2044
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002045 if (oldcurrent == slave) {
2046 /*
2047 * Note that we hold RTNL over this sequence, so there
2048 * is no concern that another slave add/remove event
2049 * will interfere.
2050 */
2051 write_unlock_bh(&bond->lock);
2052 read_lock(&bond->lock);
2053 write_lock_bh(&bond->curr_slave_lock);
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 bond_select_active_slave(bond);
2056
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002057 write_unlock_bh(&bond->curr_slave_lock);
2058 read_unlock(&bond->lock);
2059 write_lock_bh(&bond->lock);
2060 }
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (bond->slave_cnt == 0) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002063 bond_set_carrier(bond);
2064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 /* if the last slave was removed, zero the mac address
2066 * of the master so it will be set by the application
2067 * to the mac address of the first slave
2068 */
2069 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2070
Jay Vosburghf35188f2010-07-21 12:14:47 +00002071 if (!bond->vlgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2073 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002074 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2075 bond_dev->name, bond_dev->name);
2076 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2077 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
2079 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2080 !bond_has_challenged_slaves(bond)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002081 pr_info("%s: last VLAN challenged slave %s left bond %s. VLAN blocking is removed\n",
2082 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
2084 }
2085
2086 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002087 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002089 /* must do this from outside any spinlocks */
2090 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 bond_del_vlans_from_slave(bond, slave_dev);
2093
2094 /* If the mode USES_PRIMARY, then we should only remove its
2095 * promisc and mc settings if it was the curr_active_slave, but that was
2096 * already taken care of above when we detached the slave
2097 */
2098 if (!USES_PRIMARY(bond->params.mode)) {
2099 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002100 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002104 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002108 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002110 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
2112
Jiri Pirko1765a572011-02-12 06:48:36 +00002113 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002115 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07002116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 /* close slave before restoring its mac address */
2118 dev_close(slave_dev);
2119
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07002120 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002121 /* restore original ("permanent") mac address */
2122 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2123 addr.sa_family = slave_dev->type;
2124 dev_set_mac_address(slave_dev, &addr);
2125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00002127 dev_set_mtu(slave_dev, slave->original_mtu);
2128
Jiri Pirko2d7011c2011-03-16 08:46:43 +00002129 slave_dev->priv_flags &= ~IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 kfree(slave);
2132
2133 return 0; /* deletion OK */
2134}
2135
2136/*
Nicolas de Pesloüandadaa102011-03-19 13:36:18 -07002137* First release a slave and then destroy the bond if no more slaves are left.
Moni Shouad90a1622007-10-09 19:43:43 -07002138* Must be under rtnl_lock when this function is called.
2139*/
stephen hemminger26d8ee72010-10-15 05:09:34 +00002140static int bond_release_and_destroy(struct net_device *bond_dev,
2141 struct net_device *slave_dev)
Moni Shouad90a1622007-10-09 19:43:43 -07002142{
Wang Chen454d7c92008-11-12 23:37:49 -08002143 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002144 int ret;
2145
2146 ret = bond_release(bond_dev, slave_dev);
2147 if ((ret == 0) && (bond->slave_cnt == 0)) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002148 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002149 pr_info("%s: destroying bond %s.\n",
2150 bond_dev->name, bond_dev->name);
Stephen Hemminger9e716262009-06-12 19:02:47 +00002151 unregister_netdevice(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002152 }
2153 return ret;
2154}
2155
2156/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 * This function releases all slaves.
2158 */
2159static int bond_release_all(struct net_device *bond_dev)
2160{
Wang Chen454d7c92008-11-12 23:37:49 -08002161 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 struct slave *slave;
2163 struct net_device *slave_dev;
2164 struct sockaddr addr;
2165
2166 write_lock_bh(&bond->lock);
2167
Jay Vosburghff59c452006-03-27 13:27:43 -08002168 netif_carrier_off(bond_dev);
2169
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002170 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 bond->current_arp_slave = NULL;
2174 bond->primary_slave = NULL;
2175 bond_change_active_slave(bond, NULL);
2176
2177 while ((slave = bond->first_slave) != NULL) {
2178 /* Inform AD package of unbinding of slave
2179 * before slave is detached from the list.
2180 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002181 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 bond_3ad_unbind_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184 slave_dev = slave->dev;
2185 bond_detach_slave(bond, slave);
2186
Jay Vosburgh25433312008-01-17 16:24:59 -08002187 /* now that the slave is detached, unlock and perform
2188 * all the undo steps that should not be called from
2189 * within a lock.
2190 */
2191 write_unlock_bh(&bond->lock);
2192
Jiri Pirko35d48902011-03-22 02:38:12 +00002193 /* unregister rx_handler early so bond_handle_frame wouldn't
2194 * be called for this slave anymore.
2195 */
2196 netdev_rx_handler_unregister(slave_dev);
2197 synchronize_net();
2198
Holger Eitzenberger58402052008-12-09 23:07:13 -08002199 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 /* must be called only after the slave
2201 * has been detached from the list
2202 */
2203 bond_alb_deinit_slave(bond, slave);
2204 }
2205
Arthur Kepner8531c5f2005-08-23 01:34:53 -04002206 bond_compute_features(bond);
2207
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002208 bond_destroy_slave_symlinks(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 bond_del_vlans_from_slave(bond, slave_dev);
2210
2211 /* If the mode USES_PRIMARY, then we should only remove its
2212 * promisc and mc settings if it was the curr_active_slave, but that was
2213 * already taken care of above when we detached the slave
2214 */
2215 if (!USES_PRIMARY(bond->params.mode)) {
2216 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002217 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
2220 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002221 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
2224 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002225 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002227 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 }
2229
Jiri Pirko1765a572011-02-12 06:48:36 +00002230 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002232 slave_disable_netpoll(slave);
2233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 /* close slave before restoring its mac address */
2235 dev_close(slave_dev);
2236
Jay Vosburghdd957c52007-10-09 19:57:24 -07002237 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002238 /* restore original ("permanent") mac address*/
2239 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2240 addr.sa_family = slave_dev->type;
2241 dev_set_mac_address(slave_dev, &addr);
2242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 kfree(slave);
2245
2246 /* re-acquire the lock before getting the next slave */
2247 write_lock_bh(&bond->lock);
2248 }
2249
2250 /* zero the mac address of the master so it will be
2251 * set by the application to the mac address of the
2252 * first slave
2253 */
2254 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2255
Jay Vosburghf35188f2010-07-21 12:14:47 +00002256 if (!bond->vlgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
Jay Vosburghf35188f2010-07-21 12:14:47 +00002258 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002259 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2260 bond_dev->name, bond_dev->name);
2261 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2262 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 }
2264
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002265 pr_info("%s: released all slaves\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
2267out:
2268 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 return 0;
2270}
2271
2272/*
2273 * This function changes the active slave to slave <slave_dev>.
2274 * It returns -EINVAL in the following cases.
2275 * - <slave_dev> is not found in the list.
2276 * - There is not active slave now.
2277 * - <slave_dev> is already active.
2278 * - The link state of <slave_dev> is not BOND_LINK_UP.
2279 * - <slave_dev> is not running.
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002280 * In these cases, this function does nothing.
2281 * In the other cases, current_slave pointer is changed and 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 */
2283static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2284{
Wang Chen454d7c92008-11-12 23:37:49 -08002285 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 struct slave *old_active = NULL;
2287 struct slave *new_active = NULL;
2288 int res = 0;
2289
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002290 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293 /* Verify that master_dev is indeed the master of slave_dev */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002294 if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002297 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002299 read_lock(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 old_active = bond->curr_active_slave;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002301 read_unlock(&bond->curr_slave_lock);
2302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 new_active = bond_get_slave_by_dev(bond, slave_dev);
2304
2305 /*
2306 * Changing to the current active: do nothing; return success.
2307 */
2308 if (new_active && (new_active == old_active)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002309 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return 0;
2311 }
2312
2313 if ((new_active) &&
2314 (old_active) &&
2315 (new_active->link == BOND_LINK_UP) &&
2316 IS_UP(new_active->dev)) {
Neil Hormane843fa52010-10-13 16:01:50 +00002317 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002318 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 bond_change_active_slave(bond, new_active);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002320 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002321 unblock_netpoll_tx();
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002322 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002325 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 return res;
2328}
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2331{
Wang Chen454d7c92008-11-12 23:37:49 -08002332 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334 info->bond_mode = bond->params.mode;
2335 info->miimon = bond->params.miimon;
2336
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002337 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 info->num_slaves = bond->slave_cnt;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002339 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341 return 0;
2342}
2343
2344static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *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 struct slave *slave;
Eric Dumazet689c96c2009-04-23 03:39:04 +00002348 int i, res = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002350 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
2352 bond_for_each_slave(bond, slave, i) {
2353 if (i == (int)info->slave_id) {
Eric Dumazet689c96c2009-04-23 03:39:04 +00002354 res = 0;
2355 strcpy(info->slave_name, slave->dev->name);
2356 info->link = slave->link;
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002357 info->state = bond_slave_state(slave);
Eric Dumazet689c96c2009-04-23 03:39:04 +00002358 info->link_failure_count = slave->link_failure_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 break;
2360 }
2361 }
2362
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002363 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Eric Dumazet689c96c2009-04-23 03:39:04 +00002365 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
2368/*-------------------------------- Monitoring -------------------------------*/
2369
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002370
2371static int bond_miimon_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002373 struct slave *slave;
2374 int i, link_state, commit = 0;
Jiri Pirko41f89102009-04-24 03:57:29 +00002375 bool ignore_updelay;
2376
2377 ignore_updelay = !bond->curr_active_slave ? true : false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379 bond_for_each_slave(bond, slave, i) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002380 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002382 link_state = bond_check_dev_link(bond, slave->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 switch (slave->link) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002385 case BOND_LINK_UP:
2386 if (link_state)
2387 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002389 slave->link = BOND_LINK_FAIL;
2390 slave->delay = bond->params.downdelay;
2391 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002392 pr_info("%s: link status down for %sinterface %s, disabling it in %d ms.\n",
2393 bond->dev->name,
2394 (bond->params.mode ==
2395 BOND_MODE_ACTIVEBACKUP) ?
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002396 (bond_is_active_slave(slave) ?
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002397 "active " : "backup ") : "",
2398 slave->dev->name,
2399 bond->params.downdelay * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002401 /*FALLTHRU*/
2402 case BOND_LINK_FAIL:
2403 if (link_state) {
2404 /*
2405 * recovered before downdelay expired
2406 */
2407 slave->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 slave->jiffies = jiffies;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002409 pr_info("%s: link status up again after %d ms for interface %s.\n",
2410 bond->dev->name,
2411 (bond->params.downdelay - slave->delay) *
2412 bond->params.miimon,
2413 slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002414 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002416
2417 if (slave->delay <= 0) {
2418 slave->new_link = BOND_LINK_DOWN;
2419 commit++;
2420 continue;
2421 }
2422
2423 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002426 case BOND_LINK_DOWN:
2427 if (!link_state)
2428 continue;
2429
2430 slave->link = BOND_LINK_BACK;
2431 slave->delay = bond->params.updelay;
2432
2433 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002434 pr_info("%s: link status up for interface %s, enabling it in %d ms.\n",
2435 bond->dev->name, slave->dev->name,
2436 ignore_updelay ? 0 :
2437 bond->params.updelay *
2438 bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002440 /*FALLTHRU*/
2441 case BOND_LINK_BACK:
2442 if (!link_state) {
2443 slave->link = BOND_LINK_DOWN;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002444 pr_info("%s: link status down again after %d ms for interface %s.\n",
2445 bond->dev->name,
2446 (bond->params.updelay - slave->delay) *
2447 bond->params.miimon,
2448 slave->dev->name);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002449
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002450 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002452
Jiri Pirko41f89102009-04-24 03:57:29 +00002453 if (ignore_updelay)
2454 slave->delay = 0;
2455
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002456 if (slave->delay <= 0) {
2457 slave->new_link = BOND_LINK_UP;
2458 commit++;
Jiri Pirko41f89102009-04-24 03:57:29 +00002459 ignore_updelay = false;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002460 continue;
2461 }
2462
2463 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 break;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002465 }
2466 }
2467
2468 return commit;
2469}
2470
2471static void bond_miimon_commit(struct bonding *bond)
2472{
2473 struct slave *slave;
2474 int i;
2475
2476 bond_for_each_slave(bond, slave, i) {
2477 switch (slave->new_link) {
2478 case BOND_LINK_NOCHANGE:
2479 continue;
2480
2481 case BOND_LINK_UP:
2482 slave->link = BOND_LINK_UP;
2483 slave->jiffies = jiffies;
2484
2485 if (bond->params.mode == BOND_MODE_8023AD) {
2486 /* prevent it from being the active one */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002487 bond_set_backup_slave(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002488 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2489 /* make it immediately active */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002490 bond_set_active_slave(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002491 } else if (slave != bond->primary_slave) {
2492 /* prevent it from being the active one */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002493 bond_set_backup_slave(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002494 }
2495
Krzysztof Piotr Oledzki546add72010-10-06 14:28:22 -07002496 bond_update_speed_duplex(slave);
2497
David Decotigny5d305302011-04-13 15:22:31 +00002498 pr_info("%s: link status definitely up for interface %s, %u Mbps %s duplex.\n",
Krzysztof Piotr Oledzki700c2a72010-10-06 14:25:06 -07002499 bond->dev->name, slave->dev->name,
2500 slave->speed, slave->duplex ? "full" : "half");
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002501
2502 /* notify ad that the link status has changed */
2503 if (bond->params.mode == BOND_MODE_8023AD)
2504 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2505
Holger Eitzenberger58402052008-12-09 23:07:13 -08002506 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002507 bond_alb_handle_link_change(bond, slave,
2508 BOND_LINK_UP);
2509
2510 if (!bond->curr_active_slave ||
2511 (slave == bond->primary_slave))
2512 goto do_failover;
2513
2514 continue;
2515
2516 case BOND_LINK_DOWN:
Jay Vosburghfba4acd2008-10-30 17:41:14 -07002517 if (slave->link_failure_count < UINT_MAX)
2518 slave->link_failure_count++;
2519
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002520 slave->link = BOND_LINK_DOWN;
2521
2522 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2523 bond->params.mode == BOND_MODE_8023AD)
2524 bond_set_slave_inactive_flags(slave);
2525
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002526 pr_info("%s: link status definitely down for interface %s, disabling it\n",
2527 bond->dev->name, slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002528
2529 if (bond->params.mode == BOND_MODE_8023AD)
2530 bond_3ad_handle_link_change(slave,
2531 BOND_LINK_DOWN);
2532
Jiri Pirkoae63e802009-05-27 05:42:36 +00002533 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002534 bond_alb_handle_link_change(bond, slave,
2535 BOND_LINK_DOWN);
2536
2537 if (slave == bond->curr_active_slave)
2538 goto do_failover;
2539
2540 continue;
2541
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002543 pr_err("%s: invalid new link %d on slave %s\n",
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002544 bond->dev->name, slave->new_link,
2545 slave->dev->name);
2546 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002548 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 }
2550
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002551do_failover:
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002552 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00002553 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002554 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 bond_select_active_slave(bond);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002556 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002557 unblock_netpoll_tx();
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002558 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002559
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002560 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561}
2562
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002563/*
2564 * bond_mii_monitor
2565 *
2566 * Really a wrapper that splits the mii monitor into two phases: an
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002567 * inspection, then (if inspection indicates something needs to be done)
2568 * an acquisition of appropriate locks followed by a commit phase to
2569 * implement whatever link state changes are indicated.
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002570 */
2571void bond_mii_monitor(struct work_struct *work)
2572{
2573 struct bonding *bond = container_of(work, struct bonding,
2574 mii_work.work);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002575
2576 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002577 if (bond->kill_timers)
2578 goto out;
2579
2580 if (bond->slave_cnt == 0)
2581 goto re_arm;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002582
2583 if (bond->send_grat_arp) {
2584 read_lock(&bond->curr_slave_lock);
2585 bond_send_gratuitous_arp(bond);
2586 read_unlock(&bond->curr_slave_lock);
2587 }
2588
Brian Haley305d5522008-11-04 17:51:14 -08002589 if (bond->send_unsol_na) {
2590 read_lock(&bond->curr_slave_lock);
2591 bond_send_unsolicited_na(bond);
2592 read_unlock(&bond->curr_slave_lock);
2593 }
2594
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002595 if (bond_miimon_inspect(bond)) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002596 read_unlock(&bond->lock);
2597 rtnl_lock();
2598 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002599
2600 bond_miimon_commit(bond);
2601
Jay Vosburgh56556622008-01-17 16:25:03 -08002602 read_unlock(&bond->lock);
2603 rtnl_unlock(); /* might sleep, hold no other locks */
2604 read_lock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002605 }
2606
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002607re_arm:
2608 if (bond->params.miimon)
2609 queue_delayed_work(bond->wq, &bond->mii_work,
2610 msecs_to_jiffies(bond->params.miimon));
2611out:
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002612 read_unlock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002613}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002614
Al Virod3bb52b2007-08-22 20:06:58 -04002615static __be32 bond_glean_dev_ip(struct net_device *dev)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002616{
2617 struct in_device *idev;
2618 struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07002619 __be32 addr = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002620
2621 if (!dev)
2622 return 0;
2623
2624 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002625 idev = __in_dev_get_rcu(dev);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002626 if (!idev)
2627 goto out;
2628
2629 ifa = idev->ifa_list;
2630 if (!ifa)
2631 goto out;
2632
2633 addr = ifa->ifa_local;
2634out:
2635 rcu_read_unlock();
2636 return addr;
2637}
2638
Al Virod3bb52b2007-08-22 20:06:58 -04002639static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002640{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002641 struct vlan_entry *vlan;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002642
2643 if (ip == bond->master_ip)
2644 return 1;
2645
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002646 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002647 if (ip == vlan->vlan_ip)
2648 return 1;
2649 }
2650
2651 return 0;
2652}
2653
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002654/*
2655 * We go to the (large) trouble of VLAN tagging ARP frames because
2656 * switches in VLAN mode (especially if ports are configured as
2657 * "native" to a VLAN) might not pass non-tagged frames.
2658 */
Al Virod3bb52b2007-08-22 20:06:58 -04002659static void bond_arp_send(struct net_device *slave_dev, int arp_op, __be32 dest_ip, __be32 src_ip, unsigned short vlan_id)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002660{
2661 struct sk_buff *skb;
2662
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002663 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002664 slave_dev->name, dest_ip, src_ip, vlan_id);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002665
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002666 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2667 NULL, slave_dev->dev_addr, NULL);
2668
2669 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002670 pr_err("ARP packet allocation failed\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002671 return;
2672 }
2673 if (vlan_id) {
2674 skb = vlan_put_tag(skb, vlan_id);
2675 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002676 pr_err("failed to insert VLAN tag\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002677 return;
2678 }
2679 }
2680 arp_xmit(skb);
2681}
2682
2683
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2685{
David S. Millerb23dd4f2011-03-02 14:31:35 -08002686 int i, vlan_id;
Al Virod3bb52b2007-08-22 20:06:58 -04002687 __be32 *targets = bond->params.arp_targets;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002688 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002689 struct net_device *vlan_dev;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002690 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Mitch Williams6b780562005-11-09 10:36:19 -08002692 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2693 if (!targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07002694 break;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002695 pr_debug("basa: target %x\n", targets[i]);
Jay Vosburghf35188f2010-07-21 12:14:47 +00002696 if (!bond->vlgrp) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002697 pr_debug("basa: empty vlan: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002698 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2699 bond->master_ip, 0);
2700 continue;
2701 }
2702
2703 /*
2704 * If VLANs are configured, we do a route lookup to
2705 * determine which VLAN interface would be used, so we
2706 * can tag the ARP with the proper VLAN tag.
2707 */
David S. Miller78fbfd82011-03-12 00:00:52 -05002708 rt = ip_route_output(dev_net(bond->dev), targets[i], 0,
2709 RTO_ONLINK, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08002710 if (IS_ERR(rt)) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002711 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002712 pr_warning("%s: no route to arp_ip_target %pI4\n",
David S. Miller78fbfd82011-03-12 00:00:52 -05002713 bond->dev->name, &targets[i]);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002714 }
2715 continue;
2716 }
2717
2718 /*
2719 * This target is not on a VLAN
2720 */
Changli Gaod8d1f302010-06-10 23:31:35 -07002721 if (rt->dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002722 ip_rt_put(rt);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002723 pr_debug("basa: rtdev == bond->dev: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002724 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2725 bond->master_ip, 0);
2726 continue;
2727 }
2728
2729 vlan_id = 0;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002730 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002731 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Changli Gaod8d1f302010-06-10 23:31:35 -07002732 if (vlan_dev == rt->dst.dev) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002733 vlan_id = vlan->vlan_id;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002734 pr_debug("basa: vlan match on %s %d\n",
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002735 vlan_dev->name, vlan_id);
2736 break;
2737 }
2738 }
2739
2740 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002741 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002742 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2743 vlan->vlan_ip, vlan_id);
2744 continue;
2745 }
2746
2747 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002748 pr_warning("%s: no path to arp_ip_target %pI4 via rt.dev %s\n",
David S. Miller78fbfd82011-03-12 00:00:52 -05002749 bond->dev->name, &targets[i],
Changli Gaod8d1f302010-06-10 23:31:35 -07002750 rt->dst.dev ? rt->dst.dev->name : "NULL");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002751 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002752 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002753 }
2754}
2755
2756/*
2757 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2758 * for each VLAN above us.
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002759 *
2760 * Caller must hold curr_slave_lock for read or better
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002761 */
2762static void bond_send_gratuitous_arp(struct bonding *bond)
2763{
2764 struct slave *slave = bond->curr_active_slave;
2765 struct vlan_entry *vlan;
2766 struct net_device *vlan_dev;
2767
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002768 pr_debug("bond_send_grat_arp: bond %s slave %s\n",
2769 bond->dev->name, slave ? slave->dev->name : "NULL");
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002770
2771 if (!slave || !bond->send_grat_arp ||
2772 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002773 return;
2774
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002775 bond->send_grat_arp--;
2776
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002777 if (bond->master_ip) {
2778 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
Moni Shoua1053f622007-10-09 19:43:42 -07002779 bond->master_ip, 0);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002780 }
2781
Jay Vosburghf35188f2010-07-21 12:14:47 +00002782 if (!bond->vlgrp)
2783 return;
2784
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002785 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002786 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002787 if (vlan->vlan_ip) {
2788 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2789 vlan->vlan_ip, vlan->vlan_id);
2790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 }
2792}
2793
Al Virod3bb52b2007-08-22 20:06:58 -04002794static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002795{
2796 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002797 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002798
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002799 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002800 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002801 &sip, &tip, i, &targets[i],
2802 bond_has_this_ip(bond, tip));
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002803 if (sip == targets[i]) {
2804 if (bond_has_this_ip(bond, tip))
2805 slave->last_arp_rx = jiffies;
2806 return;
2807 }
2808 }
2809}
2810
2811static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2812{
2813 struct arphdr *arp;
2814 struct slave *slave;
2815 struct bonding *bond;
2816 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002817 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002818
Andy Gospodarek1f3c8802009-12-14 10:48:58 +00002819 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2820 /*
2821 * When using VLANS and bonding, dev and oriv_dev may be
2822 * incorrect if the physical interface supports VLAN
2823 * acceleration. With this change ARP validation now
2824 * works for hosts only reachable on the VLAN interface.
2825 */
2826 dev = vlan_dev_real_dev(dev);
2827 orig_dev = dev_get_by_index_rcu(dev_net(skb->dev),skb->skb_iif);
2828 }
2829
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002830 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2831 goto out;
2832
Wang Chen454d7c92008-11-12 23:37:49 -08002833 bond = netdev_priv(dev);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002834 read_lock(&bond->lock);
2835
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002836 pr_debug("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002837 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2838 orig_dev ? orig_dev->name : "NULL");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002839
2840 slave = bond_get_slave_by_dev(bond, orig_dev);
2841 if (!slave || !slave_do_arp_validate(bond, slave))
2842 goto out_unlock;
2843
Neil Hormanb3053252011-01-20 09:02:31 +00002844 skb = skb_share_check(skb, GFP_ATOMIC);
2845 if (!skb)
2846 goto out_unlock;
2847
Pavel Emelyanov988b7052008-03-03 12:20:57 -08002848 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002849 goto out_unlock;
2850
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002851 arp = arp_hdr(skb);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002852 if (arp->ar_hln != dev->addr_len ||
2853 skb->pkt_type == PACKET_OTHERHOST ||
2854 skb->pkt_type == PACKET_LOOPBACK ||
2855 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2856 arp->ar_pro != htons(ETH_P_IP) ||
2857 arp->ar_pln != 4)
2858 goto out_unlock;
2859
2860 arp_ptr = (unsigned char *)(arp + 1);
2861 arp_ptr += dev->addr_len;
2862 memcpy(&sip, arp_ptr, 4);
2863 arp_ptr += 4 + dev->addr_len;
2864 memcpy(&tip, arp_ptr, 4);
2865
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002866 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002867 bond->dev->name, slave->dev->name, bond_slave_state(slave),
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002868 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2869 &sip, &tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002870
2871 /*
2872 * Backup slaves won't see the ARP reply, but do come through
2873 * here for each ARP probe (so we swap the sip/tip to validate
2874 * the probe). In a "redundant switch, common router" type of
2875 * configuration, the ARP probe will (hopefully) travel from
2876 * the active, through one switch, the router, then the other
2877 * switch before reaching the backup.
2878 */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002879 if (bond_is_active_slave(slave))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002880 bond_validate_arp(bond, slave, sip, tip);
2881 else
2882 bond_validate_arp(bond, slave, tip, sip);
2883
2884out_unlock:
2885 read_unlock(&bond->lock);
2886out:
2887 dev_kfree_skb(skb);
2888 return NET_RX_SUCCESS;
2889}
2890
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891/*
2892 * this function is called regularly to monitor each slave's link
2893 * ensuring that traffic is being sent and received when arp monitoring
2894 * is used in load-balancing mode. if the adapter has been dormant, then an
2895 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2896 * arp monitoring in active backup mode.
2897 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002898void bond_loadbalance_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002900 struct bonding *bond = container_of(work, struct bonding,
2901 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 struct slave *slave, *oldcurrent;
2903 int do_failover = 0;
2904 int delta_in_ticks;
2905 int i;
2906
2907 read_lock(&bond->lock);
2908
Jay Vosburgh5ce0da82008-05-17 21:10:07 -07002909 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002911 if (bond->kill_timers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002914 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
2917 read_lock(&bond->curr_slave_lock);
2918 oldcurrent = bond->curr_active_slave;
2919 read_unlock(&bond->curr_slave_lock);
2920
2921 /* see if any of the previous devices are up now (i.e. they have
2922 * xmt and rcv traffic). the curr_active_slave does not come into
2923 * the picture unless it is null. also, slave->jiffies is not needed
2924 * here because we send an arp on each slave and give a slave as
2925 * long as it needs to get the tx/rx within the delta.
2926 * TODO: what about up/down delay in arp mode? it wasn't here before
2927 * so it can wait
2928 */
2929 bond_for_each_slave(bond, slave, i) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002930 unsigned long trans_start = dev_trans_start(slave->dev);
2931
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002933 if (time_in_range(jiffies,
2934 trans_start - delta_in_ticks,
2935 trans_start + delta_in_ticks) &&
2936 time_in_range(jiffies,
2937 slave->dev->last_rx - delta_in_ticks,
2938 slave->dev->last_rx + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
2940 slave->link = BOND_LINK_UP;
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002941 bond_set_active_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
2943 /* primary_slave has no meaning in round-robin
2944 * mode. the window of a slave being up and
2945 * curr_active_slave being null after enslaving
2946 * is closed.
2947 */
2948 if (!oldcurrent) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002949 pr_info("%s: link status definitely up for interface %s, ",
2950 bond->dev->name,
2951 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 do_failover = 1;
2953 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002954 pr_info("%s: interface %s is now up\n",
2955 bond->dev->name,
2956 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 }
2958 }
2959 } else {
2960 /* slave->link == BOND_LINK_UP */
2961
2962 /* not all switches will respond to an arp request
2963 * when the source ip is 0, so don't take the link down
2964 * if we don't know our ip yet
2965 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002966 if (!time_in_range(jiffies,
2967 trans_start - delta_in_ticks,
2968 trans_start + 2 * delta_in_ticks) ||
2969 !time_in_range(jiffies,
2970 slave->dev->last_rx - delta_in_ticks,
2971 slave->dev->last_rx + 2 * delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973 slave->link = BOND_LINK_DOWN;
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002974 bond_set_backup_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002976 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002979 pr_info("%s: interface %s is now down.\n",
2980 bond->dev->name,
2981 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002983 if (slave == oldcurrent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 do_failover = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 }
2986 }
2987
2988 /* note: if switch is in round-robin mode, all links
2989 * must tx arp to ensure all links rx an arp - otherwise
2990 * links may oscillate or not come up at all; if switch is
2991 * in something like xor mode, there is nothing we can
2992 * do - all replies will be rx'ed on same link causing slaves
2993 * to be unstable during low/no traffic periods
2994 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002995 if (IS_UP(slave->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 bond_arp_send_all(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 }
2998
2999 if (do_failover) {
Neil Hormane843fa52010-10-13 16:01:50 +00003000 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07003001 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
3003 bond_select_active_slave(bond);
3004
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07003005 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00003006 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 }
3008
3009re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003010 if (bond->params.arp_interval)
3011 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012out:
3013 read_unlock(&bond->lock);
3014}
3015
3016/*
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003017 * Called to inspect slaves for active-backup mode ARP monitor link state
3018 * changes. Sets new_link in slaves to specify what action should take
3019 * place for the slave. Returns 0 if no changes are found, >0 if changes
3020 * to link states must be committed.
3021 *
3022 * Called with bond->lock held for read.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003024static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 struct slave *slave;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003027 int i, commit = 0;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003028 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 bond_for_each_slave(bond, slave, i) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003031 slave->new_link = BOND_LINK_NOCHANGE;
3032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003034 if (time_in_range(jiffies,
3035 slave_last_rx(bond, slave) - delta_in_ticks,
3036 slave_last_rx(bond, slave) + delta_in_ticks)) {
3037
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003038 slave->new_link = BOND_LINK_UP;
3039 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003042 continue;
3043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003045 /*
3046 * Give slaves 2*delta after being enslaved or made
3047 * active. This avoids bouncing, as the last receive
3048 * times need a full ARP monitor cycle to be updated.
3049 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003050 if (time_in_range(jiffies,
3051 slave->jiffies - delta_in_ticks,
3052 slave->jiffies + 2 * delta_in_ticks))
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003053 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003055 /*
3056 * Backup slave is down if:
3057 * - No current_arp_slave AND
3058 * - more than 3*delta since last receive AND
3059 * - the bond has an IP address
3060 *
3061 * Note: a non-null current_arp_slave indicates
3062 * the curr_active_slave went down and we are
3063 * searching for a new one; under this condition
3064 * we only take the curr_active_slave down - this
3065 * gives each slave a chance to tx/rx traffic
3066 * before being taken out
3067 */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00003068 if (!bond_is_active_slave(slave) &&
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003069 !bond->current_arp_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003070 !time_in_range(jiffies,
3071 slave_last_rx(bond, slave) - delta_in_ticks,
3072 slave_last_rx(bond, slave) + 3 * delta_in_ticks)) {
3073
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003074 slave->new_link = BOND_LINK_DOWN;
3075 commit++;
3076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003078 /*
3079 * Active slave is down if:
3080 * - more than 2*delta since transmitting OR
3081 * - (more than 2*delta since receive AND
3082 * the bond has an IP address)
3083 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003084 trans_start = dev_trans_start(slave->dev);
Jiri Pirkoe30bc062011-03-12 03:14:37 +00003085 if (bond_is_active_slave(slave) &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003086 (!time_in_range(jiffies,
3087 trans_start - delta_in_ticks,
3088 trans_start + 2 * delta_in_ticks) ||
3089 !time_in_range(jiffies,
3090 slave_last_rx(bond, slave) - delta_in_ticks,
3091 slave_last_rx(bond, slave) + 2 * delta_in_ticks))) {
3092
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003093 slave->new_link = BOND_LINK_DOWN;
3094 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 }
3096 }
3097
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003098 return commit;
3099}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003101/*
3102 * Called to commit link state changes noted by inspection step of
3103 * active-backup mode ARP monitor.
3104 *
3105 * Called with RTNL and bond->lock for read.
3106 */
3107static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
3108{
3109 struct slave *slave;
3110 int i;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003111 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003113 bond_for_each_slave(bond, slave, i) {
3114 switch (slave->new_link) {
3115 case BOND_LINK_NOCHANGE:
3116 continue;
3117
3118 case BOND_LINK_UP:
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003119 trans_start = dev_trans_start(slave->dev);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003120 if ((!bond->curr_active_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003121 time_in_range(jiffies,
3122 trans_start - delta_in_ticks,
3123 trans_start + delta_in_ticks)) ||
Jiri Pirkob9f60252009-08-31 11:09:38 +00003124 bond->curr_active_slave != slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003125 slave->link = BOND_LINK_UP;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003126 bond->current_arp_slave = NULL;
3127
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003128 pr_info("%s: link status definitely up for interface %s.\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003129 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003130
Jiri Pirkob9f60252009-08-31 11:09:38 +00003131 if (!bond->curr_active_slave ||
3132 (slave == bond->primary_slave))
3133 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003134
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003135 }
3136
Jiri Pirkob9f60252009-08-31 11:09:38 +00003137 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003138
3139 case BOND_LINK_DOWN:
3140 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003143 slave->link = BOND_LINK_DOWN;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003144 bond_set_slave_inactive_flags(slave);
3145
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003146 pr_info("%s: link status definitely down for interface %s, disabling it\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003147 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003148
3149 if (slave == bond->curr_active_slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003150 bond->current_arp_slave = NULL;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003151 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003152 }
Jiri Pirkob9f60252009-08-31 11:09:38 +00003153
3154 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003155
3156 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003157 pr_err("%s: impossible: new_link %d on slave %s\n",
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003158 bond->dev->name, slave->new_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 slave->dev->name);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003160 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Jiri Pirkob9f60252009-08-31 11:09:38 +00003163do_failover:
3164 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00003165 block_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003166 write_lock_bh(&bond->curr_slave_lock);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003167 bond_select_active_slave(bond);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003168 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00003169 unblock_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003170 }
3171
3172 bond_set_carrier(bond);
3173}
3174
3175/*
3176 * Send ARP probes for active-backup mode ARP monitor.
3177 *
3178 * Called with bond->lock held for read.
3179 */
3180static void bond_ab_arp_probe(struct bonding *bond)
3181{
3182 struct slave *slave;
3183 int i;
3184
3185 read_lock(&bond->curr_slave_lock);
3186
3187 if (bond->current_arp_slave && bond->curr_active_slave)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003188 pr_info("PROBE: c_arp %s && cas %s BAD\n",
3189 bond->current_arp_slave->dev->name,
3190 bond->curr_active_slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003191
3192 if (bond->curr_active_slave) {
3193 bond_arp_send_all(bond, bond->curr_active_slave);
3194 read_unlock(&bond->curr_slave_lock);
3195 return;
3196 }
3197
3198 read_unlock(&bond->curr_slave_lock);
3199
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 /* if we don't have a curr_active_slave, search for the next available
3201 * backup slave from the current_arp_slave and make it the candidate
3202 * for becoming the curr_active_slave
3203 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003204
3205 if (!bond->current_arp_slave) {
3206 bond->current_arp_slave = bond->first_slave;
3207 if (!bond->current_arp_slave)
3208 return;
3209 }
3210
3211 bond_set_slave_inactive_flags(bond->current_arp_slave);
3212
3213 /* search for next candidate */
3214 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3215 if (IS_UP(slave->dev)) {
3216 slave->link = BOND_LINK_BACK;
3217 bond_set_slave_active_flags(slave);
3218 bond_arp_send_all(bond, slave);
3219 slave->jiffies = jiffies;
3220 bond->current_arp_slave = slave;
3221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 }
3223
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003224 /* if the link state is up at this point, we
3225 * mark it down - this can happen if we have
3226 * simultaneous link failures and
3227 * reselect_active_interface doesn't make this
3228 * one the current slave so it is still marked
3229 * up when it is actually down
3230 */
3231 if (slave->link == BOND_LINK_UP) {
3232 slave->link = BOND_LINK_DOWN;
3233 if (slave->link_failure_count < UINT_MAX)
3234 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003236 bond_set_slave_inactive_flags(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003238 pr_info("%s: backup interface %s is now down.\n",
3239 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 }
3241 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003242}
3243
3244void bond_activebackup_arp_mon(struct work_struct *work)
3245{
3246 struct bonding *bond = container_of(work, struct bonding,
3247 arp_work.work);
3248 int delta_in_ticks;
3249
3250 read_lock(&bond->lock);
3251
3252 if (bond->kill_timers)
3253 goto out;
3254
3255 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3256
3257 if (bond->slave_cnt == 0)
3258 goto re_arm;
3259
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003260 if (bond->send_grat_arp) {
3261 read_lock(&bond->curr_slave_lock);
3262 bond_send_gratuitous_arp(bond);
3263 read_unlock(&bond->curr_slave_lock);
3264 }
3265
Brian Haley305d5522008-11-04 17:51:14 -08003266 if (bond->send_unsol_na) {
3267 read_lock(&bond->curr_slave_lock);
3268 bond_send_unsolicited_na(bond);
3269 read_unlock(&bond->curr_slave_lock);
3270 }
3271
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003272 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3273 read_unlock(&bond->lock);
3274 rtnl_lock();
3275 read_lock(&bond->lock);
3276
3277 bond_ab_arp_commit(bond, delta_in_ticks);
3278
3279 read_unlock(&bond->lock);
3280 rtnl_unlock();
3281 read_lock(&bond->lock);
3282 }
3283
3284 bond_ab_arp_probe(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
3286re_arm:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003287 if (bond->params.arp_interval)
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003288 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289out:
3290 read_unlock(&bond->lock);
3291}
3292
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293/*-------------------------- netdev event handling --------------------------*/
3294
3295/*
3296 * Change device name
3297 */
3298static int bond_event_changename(struct bonding *bond)
3299{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 bond_remove_proc_entry(bond);
3301 bond_create_proc_entry(bond);
Stephen Hemminger7e083842009-06-12 19:02:46 +00003302
Taku Izumif073c7c2010-12-09 15:17:13 +00003303 bond_debug_reregister(bond);
3304
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 return NOTIFY_DONE;
3306}
3307
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003308static int bond_master_netdev_event(unsigned long event,
3309 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310{
Wang Chen454d7c92008-11-12 23:37:49 -08003311 struct bonding *event_bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
3313 switch (event) {
3314 case NETDEV_CHANGENAME:
3315 return bond_event_changename(event_bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 default:
3317 break;
3318 }
3319
3320 return NOTIFY_DONE;
3321}
3322
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003323static int bond_slave_netdev_event(unsigned long event,
3324 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325{
3326 struct net_device *bond_dev = slave_dev->master;
Wang Chen454d7c92008-11-12 23:37:49 -08003327 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
3329 switch (event) {
3330 case NETDEV_UNREGISTER:
3331 if (bond_dev) {
Jay Vosburgh1284cd32007-10-15 16:44:27 -07003332 if (bond->setup_by_slave)
3333 bond_release_and_destroy(bond_dev, slave_dev);
3334 else
3335 bond_release(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 }
3337 break;
3338 case NETDEV_CHANGE:
Jay Vosburgh17d04502009-03-18 18:38:25 -07003339 if (bond->params.mode == BOND_MODE_8023AD || bond_is_lb(bond)) {
3340 struct slave *slave;
3341
3342 slave = bond_get_slave_by_dev(bond, slave_dev);
3343 if (slave) {
David Decotigny5d305302011-04-13 15:22:31 +00003344 u32 old_speed = slave->speed;
David Decotigny65cce192011-04-13 15:22:30 +00003345 u8 old_duplex = slave->duplex;
Jay Vosburgh17d04502009-03-18 18:38:25 -07003346
3347 bond_update_speed_duplex(slave);
3348
3349 if (bond_is_lb(bond))
3350 break;
3351
3352 if (old_speed != slave->speed)
3353 bond_3ad_adapter_speed_changed(slave);
3354 if (old_duplex != slave->duplex)
3355 bond_3ad_adapter_duplex_changed(slave);
3356 }
3357 }
3358
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 break;
3360 case NETDEV_DOWN:
3361 /*
3362 * ... Or is it this?
3363 */
3364 break;
3365 case NETDEV_CHANGEMTU:
3366 /*
3367 * TODO: Should slaves be allowed to
3368 * independently alter their MTU? For
3369 * an active-backup bond, slaves need
3370 * not be the same type of device, so
3371 * MTUs may vary. For other modes,
3372 * slaves arguably should have the
3373 * same MTUs. To do this, we'd need to
3374 * take over the slave's change_mtu
3375 * function for the duration of their
3376 * servitude.
3377 */
3378 break;
3379 case NETDEV_CHANGENAME:
3380 /*
3381 * TODO: handle changing the primary's name
3382 */
3383 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003384 case NETDEV_FEAT_CHANGE:
3385 bond_compute_features(bond);
3386 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 default:
3388 break;
3389 }
3390
3391 return NOTIFY_DONE;
3392}
3393
3394/*
3395 * bond_netdev_event: handle netdev notifier chain events.
3396 *
3397 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003398 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 * locks for us to safely manipulate the slave devices (RTNL lock,
3400 * dev_probe_lock).
3401 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003402static int bond_netdev_event(struct notifier_block *this,
3403 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404{
3405 struct net_device *event_dev = (struct net_device *)ptr;
3406
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003407 pr_debug("event_dev: %s, event: %lx\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003408 event_dev ? event_dev->name : "None",
3409 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003411 if (!(event_dev->priv_flags & IFF_BONDING))
3412 return NOTIFY_DONE;
3413
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 if (event_dev->flags & IFF_MASTER) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003415 pr_debug("IFF_MASTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 return bond_master_netdev_event(event, event_dev);
3417 }
3418
3419 if (event_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003420 pr_debug("IFF_SLAVE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 return bond_slave_netdev_event(event, event_dev);
3422 }
3423
3424 return NOTIFY_DONE;
3425}
3426
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003427/*
3428 * bond_inetaddr_event: handle inetaddr notifier chain events.
3429 *
3430 * We keep track of device IPs primarily to use as source addresses in
3431 * ARP monitor probes (rather than spewing out broadcasts all the time).
3432 *
3433 * We track one IP for the main device (if it has one), plus one per VLAN.
3434 */
3435static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3436{
3437 struct in_ifaddr *ifa = ptr;
3438 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003439 struct bond_net *bn = net_generic(dev_net(event_dev), bond_net_id);
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003440 struct bonding *bond;
3441 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003442
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003443 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003444 if (bond->dev == event_dev) {
3445 switch (event) {
3446 case NETDEV_UP:
3447 bond->master_ip = ifa->ifa_local;
3448 return NOTIFY_OK;
3449 case NETDEV_DOWN:
3450 bond->master_ip = bond_glean_dev_ip(bond->dev);
3451 return NOTIFY_OK;
3452 default:
3453 return NOTIFY_DONE;
3454 }
3455 }
3456
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003457 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +00003458 if (!bond->vlgrp)
3459 continue;
Dan Aloni5c15bde2007-03-02 20:44:51 -08003460 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003461 if (vlan_dev == event_dev) {
3462 switch (event) {
3463 case NETDEV_UP:
3464 vlan->vlan_ip = ifa->ifa_local;
3465 return NOTIFY_OK;
3466 case NETDEV_DOWN:
3467 vlan->vlan_ip =
3468 bond_glean_dev_ip(vlan_dev);
3469 return NOTIFY_OK;
3470 default:
3471 return NOTIFY_DONE;
3472 }
3473 }
3474 }
3475 }
3476 return NOTIFY_DONE;
3477}
3478
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479static struct notifier_block bond_netdev_notifier = {
3480 .notifier_call = bond_netdev_event,
3481};
3482
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003483static struct notifier_block bond_inetaddr_notifier = {
3484 .notifier_call = bond_inetaddr_event,
3485};
3486
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487/*-------------------------- Packet type handling ---------------------------*/
3488
3489/* register to receive lacpdus on a bond */
3490static void bond_register_lacpdu(struct bonding *bond)
3491{
3492 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3493
3494 /* initialize packet type */
3495 pk_type->type = PKT_TYPE_LACPDU;
3496 pk_type->dev = bond->dev;
3497 pk_type->func = bond_3ad_lacpdu_recv;
3498
3499 dev_add_pack(pk_type);
3500}
3501
3502/* unregister to receive lacpdus on a bond */
3503static void bond_unregister_lacpdu(struct bonding *bond)
3504{
3505 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3506}
3507
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003508void bond_register_arp(struct bonding *bond)
3509{
3510 struct packet_type *pt = &bond->arp_mon_pt;
3511
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003512 if (pt->type)
3513 return;
3514
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003515 pt->type = htons(ETH_P_ARP);
Jay Vosburghe245cb72007-02-28 17:03:27 -08003516 pt->dev = bond->dev;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003517 pt->func = bond_arp_rcv;
3518 dev_add_pack(pt);
3519}
3520
3521void bond_unregister_arp(struct bonding *bond)
3522{
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003523 struct packet_type *pt = &bond->arp_mon_pt;
3524
3525 dev_remove_pack(pt);
3526 pt->type = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003527}
3528
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003529/*---------------------------- Hashing Policies -----------------------------*/
3530
3531/*
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003532 * Hash for the output device based upon layer 2 and layer 3 data. If
3533 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3534 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003535static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003536{
3537 struct ethhdr *data = (struct ethhdr *)skb->data;
3538 struct iphdr *iph = ip_hdr(skb);
3539
Brian Haleyf14c4e42008-09-02 10:08:08 -04003540 if (skb->protocol == htons(ETH_P_IP)) {
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003541 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
Jasper Spaansd3da6832009-10-23 04:08:46 +00003542 (data->h_dest[5] ^ data->h_source[5])) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003543 }
3544
Jasper Spaansd3da6832009-10-23 04:08:46 +00003545 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003546}
3547
3548/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003549 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003550 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3551 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3552 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003553static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003554{
3555 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003556 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003557 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003558 int layer4_xor = 0;
3559
Brian Haleyf14c4e42008-09-02 10:08:08 -04003560 if (skb->protocol == htons(ETH_P_IP)) {
3561 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003562 (iph->protocol == IPPROTO_TCP ||
3563 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003564 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003565 }
3566 return (layer4_xor ^
3567 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3568
3569 }
3570
Jasper Spaansd3da6832009-10-23 04:08:46 +00003571 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003572}
3573
3574/*
3575 * Hash for the output device based upon layer 2 data
3576 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003577static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003578{
3579 struct ethhdr *data = (struct ethhdr *)skb->data;
3580
Jasper Spaansd3da6832009-10-23 04:08:46 +00003581 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003582}
3583
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584/*-------------------------- Device entry points ----------------------------*/
3585
3586static int bond_open(struct net_device *bond_dev)
3587{
Wang Chen454d7c92008-11-12 23:37:49 -08003588 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
3590 bond->kill_timers = 0;
3591
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003592 INIT_DELAYED_WORK(&bond->mcast_work, bond_resend_igmp_join_requests_delayed);
3593
Holger Eitzenberger58402052008-12-09 23:07:13 -08003594 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 /* bond_alb_initialize must be called before the timer
3596 * is started.
3597 */
3598 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3599 /* something went wrong - fail the open operation */
stephen hemmingerb4739462010-01-25 23:34:15 +00003600 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 }
3602
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003603 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3604 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 }
3606
3607 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003608 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3609 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 }
3611
3612 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003613 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3614 INIT_DELAYED_WORK(&bond->arp_work,
3615 bond_activebackup_arp_mon);
3616 else
3617 INIT_DELAYED_WORK(&bond->arp_work,
3618 bond_loadbalance_arp_mon);
3619
3620 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003621 if (bond->params.arp_validate)
3622 bond_register_arp(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 }
3624
3625 if (bond->params.mode == BOND_MODE_8023AD) {
Adrian Bunka40745f2007-10-24 18:27:43 +02003626 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003627 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 /* register to receive LACPDUs */
3629 bond_register_lacpdu(bond);
Jay Vosburghfd989c82008-11-04 17:51:16 -08003630 bond_3ad_initiate_agg_selection(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 }
3632
3633 return 0;
3634}
3635
3636static int bond_close(struct net_device *bond_dev)
3637{
Wang Chen454d7c92008-11-12 23:37:49 -08003638 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
3640 if (bond->params.mode == BOND_MODE_8023AD) {
3641 /* Unregister the receive of LACPDUs */
3642 bond_unregister_lacpdu(bond);
3643 }
3644
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003645 if (bond->params.arp_validate)
3646 bond_unregister_arp(bond);
3647
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 write_lock_bh(&bond->lock);
3649
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003650 bond->send_grat_arp = 0;
Brian Haley305d5522008-11-04 17:51:14 -08003651 bond->send_unsol_na = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
3653 /* signal timers not to re-arm */
3654 bond->kill_timers = 1;
3655
3656 write_unlock_bh(&bond->lock);
3657
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003659 cancel_delayed_work(&bond->mii_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 }
3661
3662 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003663 cancel_delayed_work(&bond->arp_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 }
3665
3666 switch (bond->params.mode) {
3667 case BOND_MODE_8023AD:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003668 cancel_delayed_work(&bond->ad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 break;
3670 case BOND_MODE_TLB:
3671 case BOND_MODE_ALB:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003672 cancel_delayed_work(&bond->alb_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 break;
3674 default:
3675 break;
3676 }
3677
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003678 if (delayed_work_pending(&bond->mcast_work))
3679 cancel_delayed_work(&bond->mcast_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Holger Eitzenberger58402052008-12-09 23:07:13 -08003681 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 /* Must be called only after all
3683 * slaves have been released
3684 */
3685 bond_alb_deinitialize(bond);
3686 }
3687
3688 return 0;
3689}
3690
Eric Dumazet28172732010-07-07 14:58:56 -07003691static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3692 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693{
Wang Chen454d7c92008-11-12 23:37:49 -08003694 struct bonding *bond = netdev_priv(bond_dev);
Eric Dumazet28172732010-07-07 14:58:56 -07003695 struct rtnl_link_stats64 temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 struct slave *slave;
3697 int i;
3698
Eric Dumazet28172732010-07-07 14:58:56 -07003699 memset(stats, 0, sizeof(*stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
3701 read_lock_bh(&bond->lock);
3702
3703 bond_for_each_slave(bond, slave, i) {
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00003704 const struct rtnl_link_stats64 *sstats =
Eric Dumazet28172732010-07-07 14:58:56 -07003705 dev_get_stats(slave->dev, &temp);
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08003706
Eric Dumazet28172732010-07-07 14:58:56 -07003707 stats->rx_packets += sstats->rx_packets;
3708 stats->rx_bytes += sstats->rx_bytes;
3709 stats->rx_errors += sstats->rx_errors;
3710 stats->rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711
Eric Dumazet28172732010-07-07 14:58:56 -07003712 stats->tx_packets += sstats->tx_packets;
3713 stats->tx_bytes += sstats->tx_bytes;
3714 stats->tx_errors += sstats->tx_errors;
3715 stats->tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
Eric Dumazet28172732010-07-07 14:58:56 -07003717 stats->multicast += sstats->multicast;
3718 stats->collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719
Eric Dumazet28172732010-07-07 14:58:56 -07003720 stats->rx_length_errors += sstats->rx_length_errors;
3721 stats->rx_over_errors += sstats->rx_over_errors;
3722 stats->rx_crc_errors += sstats->rx_crc_errors;
3723 stats->rx_frame_errors += sstats->rx_frame_errors;
3724 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3725 stats->rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726
Eric Dumazet28172732010-07-07 14:58:56 -07003727 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3728 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3729 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3730 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3731 stats->tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 }
3733
3734 read_unlock_bh(&bond->lock);
3735
3736 return stats;
3737}
3738
3739static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3740{
3741 struct net_device *slave_dev = NULL;
3742 struct ifbond k_binfo;
3743 struct ifbond __user *u_binfo = NULL;
3744 struct ifslave k_sinfo;
3745 struct ifslave __user *u_sinfo = NULL;
3746 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 int res = 0;
3748
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003749 pr_debug("bond_ioctl: master=%s, cmd=%d\n", bond_dev->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
3751 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 case SIOCGMIIPHY:
3753 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003754 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003756
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 mii->phy_id = 0;
3758 /* Fall Through */
3759 case SIOCGMIIREG:
3760 /*
3761 * We do this again just in case we were called by SIOCGMIIREG
3762 * instead of SIOCGMIIPHY.
3763 */
3764 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003765 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003767
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
3769 if (mii->reg_num == 1) {
Wang Chen454d7c92008-11-12 23:37:49 -08003770 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 mii->val_out = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003772 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 read_lock(&bond->curr_slave_lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003774 if (netif_carrier_ok(bond->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 mii->val_out = BMSR_LSTATUS;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003776
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 read_unlock(&bond->curr_slave_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003778 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 }
3780
3781 return 0;
3782 case BOND_INFO_QUERY_OLD:
3783 case SIOCBONDINFOQUERY:
3784 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3785
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003786 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
3789 res = bond_info_query(bond_dev, &k_binfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003790 if (res == 0 &&
3791 copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
3792 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793
3794 return res;
3795 case BOND_SLAVE_INFO_QUERY_OLD:
3796 case SIOCBONDSLAVEINFOQUERY:
3797 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3798
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003799 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
3802 res = bond_slave_info_query(bond_dev, &k_sinfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003803 if (res == 0 &&
3804 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
3805 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
3807 return res;
3808 default:
3809 /* Go on */
3810 break;
3811 }
3812
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003813 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003816 slave_dev = dev_get_by_name(dev_net(bond_dev), ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003818 pr_debug("slave_dev=%p:\n", slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003820 if (!slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 res = -ENODEV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003822 else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003823 pr_debug("slave_dev->name=%s:\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 switch (cmd) {
3825 case BOND_ENSLAVE_OLD:
3826 case SIOCBONDENSLAVE:
3827 res = bond_enslave(bond_dev, slave_dev);
3828 break;
3829 case BOND_RELEASE_OLD:
3830 case SIOCBONDRELEASE:
3831 res = bond_release(bond_dev, slave_dev);
3832 break;
3833 case BOND_SETHWADDR_OLD:
3834 case SIOCBONDSETHWADDR:
3835 res = bond_sethwaddr(bond_dev, slave_dev);
3836 break;
3837 case BOND_CHANGE_ACTIVE_OLD:
3838 case SIOCBONDCHANGEACTIVE:
3839 res = bond_ioctl_change_active(bond_dev, slave_dev);
3840 break;
3841 default:
3842 res = -EOPNOTSUPP;
3843 }
3844
3845 dev_put(slave_dev);
3846 }
3847
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 return res;
3849}
3850
Jiri Pirko22bedad32010-04-01 21:22:57 +00003851static bool bond_addr_in_mc_list(unsigned char *addr,
3852 struct netdev_hw_addr_list *list,
3853 int addrlen)
3854{
3855 struct netdev_hw_addr *ha;
3856
3857 netdev_hw_addr_list_for_each(ha, list)
3858 if (!memcmp(ha->addr, addr, addrlen))
3859 return true;
3860
3861 return false;
3862}
3863
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864static void bond_set_multicast_list(struct net_device *bond_dev)
3865{
Wang Chen454d7c92008-11-12 23:37:49 -08003866 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00003867 struct netdev_hw_addr *ha;
3868 bool found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 /*
3871 * Do promisc before checking multicast_mode
3872 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003873 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07003874 /*
3875 * FIXME: Need to handle the error when one of the multi-slaves
3876 * encounters error.
3877 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 bond_set_promiscuity(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003880
3881 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 bond_set_promiscuity(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003883
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
3885 /* set allmulti flag to slaves */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003886 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07003887 /*
3888 * FIXME: Need to handle the error when one of the multi-slaves
3889 * encounters error.
3890 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 bond_set_allmulti(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003893
3894 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 bond_set_allmulti(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003896
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08003898 read_lock(&bond->lock);
3899
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 bond->flags = bond_dev->flags;
3901
3902 /* looking for addresses to add to slaves' mc list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00003903 netdev_for_each_mc_addr(ha, bond_dev) {
3904 found = bond_addr_in_mc_list(ha->addr, &bond->mc_list,
3905 bond_dev->addr_len);
3906 if (!found)
3907 bond_mc_add(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 }
3909
3910 /* looking for addresses to delete from slaves' list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00003911 netdev_hw_addr_list_for_each(ha, &bond->mc_list) {
3912 found = bond_addr_in_mc_list(ha->addr, &bond_dev->mc,
3913 bond_dev->addr_len);
3914 if (!found)
3915 bond_mc_del(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 }
3917
3918 /* save master's multicast list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00003919 __hw_addr_flush(&bond->mc_list);
3920 __hw_addr_add_multiple(&bond->mc_list, &bond_dev->mc,
3921 bond_dev->addr_len, NETDEV_HW_ADDR_T_MULTICAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08003923 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924}
3925
Stephen Hemminger00829822008-11-20 20:14:53 -08003926static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
3927{
3928 struct bonding *bond = netdev_priv(dev);
3929 struct slave *slave = bond->first_slave;
3930
3931 if (slave) {
3932 const struct net_device_ops *slave_ops
3933 = slave->dev->netdev_ops;
3934 if (slave_ops->ndo_neigh_setup)
Patrick McHardy72e22402009-03-05 01:57:44 -08003935 return slave_ops->ndo_neigh_setup(slave->dev, parms);
Stephen Hemminger00829822008-11-20 20:14:53 -08003936 }
3937 return 0;
3938}
3939
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940/*
3941 * Change the MTU of all of a master's slaves to match the master
3942 */
3943static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3944{
Wang Chen454d7c92008-11-12 23:37:49 -08003945 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 struct slave *slave, *stop_at;
3947 int res = 0;
3948 int i;
3949
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003950 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003951 (bond_dev ? bond_dev->name : "None"), new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952
3953 /* Can't hold bond->lock with bh disabled here since
3954 * some base drivers panic. On the other hand we can't
3955 * hold bond->lock without bh disabled because we'll
3956 * deadlock. The only solution is to rely on the fact
3957 * that we're under rtnl_lock here, and the slaves
3958 * list won't change. This doesn't solve the problem
3959 * of setting the slave's MTU while it is
3960 * transmitting, but the assumption is that the base
3961 * driver can handle that.
3962 *
3963 * TODO: figure out a way to safely iterate the slaves
3964 * list, but without holding a lock around the actual
3965 * call to the base driver.
3966 */
3967
3968 bond_for_each_slave(bond, slave, i) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003969 pr_debug("s %p s->p %p c_m %p\n",
3970 slave,
3971 slave->prev,
3972 slave->dev->netdev_ops->ndo_change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08003973
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 res = dev_set_mtu(slave->dev, new_mtu);
3975
3976 if (res) {
3977 /* If we failed to set the slave's mtu to the new value
3978 * we must abort the operation even in ACTIVE_BACKUP
3979 * mode, because if we allow the backup slaves to have
3980 * different mtu values than the active slave we'll
3981 * need to change their mtu when doing a failover. That
3982 * means changing their mtu from timer context, which
3983 * is probably not a good idea.
3984 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003985 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 goto unwind;
3987 }
3988 }
3989
3990 bond_dev->mtu = new_mtu;
3991
3992 return 0;
3993
3994unwind:
3995 /* unwind from head to the slave that failed */
3996 stop_at = slave;
3997 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3998 int tmp_res;
3999
4000 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
4001 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004002 pr_debug("unwind err %d dev %s\n",
4003 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 }
4005 }
4006
4007 return res;
4008}
4009
4010/*
4011 * Change HW address
4012 *
4013 * Note that many devices must be down to change the HW address, and
4014 * downing the master releases all slaves. We can make bonds full of
4015 * bonding devices to test this, however.
4016 */
4017static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4018{
Wang Chen454d7c92008-11-12 23:37:49 -08004019 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 struct sockaddr *sa = addr, tmp_sa;
4021 struct slave *slave, *stop_at;
4022 int res = 0;
4023 int i;
4024
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004025 if (bond->params.mode == BOND_MODE_ALB)
4026 return bond_alb_set_mac_address(bond_dev, addr);
4027
4028
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004029 pr_debug("bond=%p, name=%s\n",
4030 bond, bond_dev ? bond_dev->name : "None");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
Jay Vosburghdd957c52007-10-09 19:57:24 -07004032 /*
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004033 * If fail_over_mac is set to active, do nothing and return
4034 * success. Returning an error causes ifenslave to fail.
Jay Vosburghdd957c52007-10-09 19:57:24 -07004035 */
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004036 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
Jay Vosburghdd957c52007-10-09 19:57:24 -07004037 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07004038
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004039 if (!is_valid_ether_addr(sa->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
4042 /* Can't hold bond->lock with bh disabled here since
4043 * some base drivers panic. On the other hand we can't
4044 * hold bond->lock without bh disabled because we'll
4045 * deadlock. The only solution is to rely on the fact
4046 * that we're under rtnl_lock here, and the slaves
4047 * list won't change. This doesn't solve the problem
4048 * of setting the slave's hw address while it is
4049 * transmitting, but the assumption is that the base
4050 * driver can handle that.
4051 *
4052 * TODO: figure out a way to safely iterate the slaves
4053 * list, but without holding a lock around the actual
4054 * call to the base driver.
4055 */
4056
4057 bond_for_each_slave(bond, slave, i) {
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004058 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004059 pr_debug("slave %p %s\n", slave, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004061 if (slave_ops->ndo_set_mac_address == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 res = -EOPNOTSUPP;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004063 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 goto unwind;
4065 }
4066
4067 res = dev_set_mac_address(slave->dev, addr);
4068 if (res) {
4069 /* TODO: consider downing the slave
4070 * and retry ?
4071 * User should expect communications
4072 * breakage anyway until ARP finish
4073 * updating, so...
4074 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004075 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 goto unwind;
4077 }
4078 }
4079
4080 /* success */
4081 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4082 return 0;
4083
4084unwind:
4085 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4086 tmp_sa.sa_family = bond_dev->type;
4087
4088 /* unwind from head to the slave that failed */
4089 stop_at = slave;
4090 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4091 int tmp_res;
4092
4093 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4094 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004095 pr_debug("unwind err %d dev %s\n",
4096 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 }
4098 }
4099
4100 return res;
4101}
4102
4103static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4104{
Wang Chen454d7c92008-11-12 23:37:49 -08004105 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 struct slave *slave, *start_at;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004107 int i, slave_no, res = 1;
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004108 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
4110 read_lock(&bond->lock);
4111
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004112 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 goto out;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004114 /*
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004115 * Start with the curr_active_slave that joined the bond as the
4116 * default for sending IGMP traffic. For failover purposes one
4117 * needs to maintain some consistency for the interface that will
4118 * send the join/membership reports. The curr_active_slave found
4119 * will send all of this type of traffic.
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004120 */
Eric Dumazet00ae7022010-03-30 23:08:37 +00004121 if ((iph->protocol == IPPROTO_IGMP) &&
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004122 (skb->protocol == htons(ETH_P_IP))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004124 read_lock(&bond->curr_slave_lock);
4125 slave = bond->curr_active_slave;
4126 read_unlock(&bond->curr_slave_lock);
4127
4128 if (!slave)
4129 goto out;
4130 } else {
4131 /*
4132 * Concurrent TX may collide on rr_tx_counter; we accept
4133 * that as being rare enough not to justify using an
4134 * atomic op here.
4135 */
4136 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
4137
4138 bond_for_each_slave(bond, slave, i) {
4139 slave_no--;
4140 if (slave_no < 0)
4141 break;
4142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 }
4144
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004145 start_at = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 bond_for_each_slave_from(bond, slave, i, start_at) {
4147 if (IS_UP(slave->dev) &&
4148 (slave->link == BOND_LINK_UP) &&
Jiri Pirkoe30bc062011-03-12 03:14:37 +00004149 bond_is_active_slave(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 res = bond_dev_queue_xmit(bond, skb, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 break;
4152 }
4153 }
4154
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155out:
4156 if (res) {
4157 /* no suitable interface, frame not sent */
4158 dev_kfree_skb(skb);
4159 }
4160 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004161 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162}
4163
John W. Linville075897c2005-09-28 17:50:53 -04004164
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165/*
4166 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4167 * the bond has a usable interface.
4168 */
4169static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4170{
Wang Chen454d7c92008-11-12 23:37:49 -08004171 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 int res = 1;
4173
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 read_lock(&bond->lock);
4175 read_lock(&bond->curr_slave_lock);
4176
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004177 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
John W. Linville075897c2005-09-28 17:50:53 -04004180 if (!bond->curr_active_slave)
4181 goto out;
4182
John W. Linville075897c2005-09-28 17:50:53 -04004183 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4184
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004186 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 /* no suitable interface, frame not sent */
4188 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004189
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 read_unlock(&bond->curr_slave_lock);
4191 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004192 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193}
4194
4195/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004196 * In bond_xmit_xor() , we determine the output device by using a pre-
4197 * determined xmit_hash_policy(), If the selected device is not enabled,
4198 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 */
4200static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4201{
Wang Chen454d7c92008-11-12 23:37:49 -08004202 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 struct slave *slave, *start_at;
4204 int slave_no;
4205 int i;
4206 int res = 1;
4207
4208 read_lock(&bond->lock);
4209
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004210 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212
Jasper Spaansa361c832009-10-23 04:09:24 +00004213 slave_no = bond->xmit_hash_policy(skb, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
4215 bond_for_each_slave(bond, slave, i) {
4216 slave_no--;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004217 if (slave_no < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 }
4220
4221 start_at = slave;
4222
4223 bond_for_each_slave_from(bond, slave, i, start_at) {
4224 if (IS_UP(slave->dev) &&
4225 (slave->link == BOND_LINK_UP) &&
Jiri Pirkoe30bc062011-03-12 03:14:37 +00004226 bond_is_active_slave(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4228 break;
4229 }
4230 }
4231
4232out:
4233 if (res) {
4234 /* no suitable interface, frame not sent */
4235 dev_kfree_skb(skb);
4236 }
4237 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004238 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239}
4240
4241/*
4242 * in broadcast mode, we send everything to all usable interfaces.
4243 */
4244static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4245{
Wang Chen454d7c92008-11-12 23:37:49 -08004246 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 struct slave *slave, *start_at;
4248 struct net_device *tx_dev = NULL;
4249 int i;
4250 int res = 1;
4251
4252 read_lock(&bond->lock);
4253
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004254 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
4257 read_lock(&bond->curr_slave_lock);
4258 start_at = bond->curr_active_slave;
4259 read_unlock(&bond->curr_slave_lock);
4260
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004261 if (!start_at)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
4264 bond_for_each_slave_from(bond, slave, i, start_at) {
4265 if (IS_UP(slave->dev) &&
4266 (slave->link == BOND_LINK_UP) &&
Jiri Pirkoe30bc062011-03-12 03:14:37 +00004267 bond_is_active_slave(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 if (tx_dev) {
4269 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4270 if (!skb2) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004271 pr_err("%s: Error: bond_xmit_broadcast(): skb_clone() failed\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08004272 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 continue;
4274 }
4275
4276 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4277 if (res) {
4278 dev_kfree_skb(skb2);
4279 continue;
4280 }
4281 }
4282 tx_dev = slave->dev;
4283 }
4284 }
4285
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004286 if (tx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 res = bond_dev_queue_xmit(bond, skb, tx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
4289out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004290 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 /* no suitable interface, frame not sent */
4292 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004293
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 /* frame sent to all suitable interfaces */
4295 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004296 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297}
4298
4299/*------------------------- Device initialization ---------------------------*/
4300
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004301static void bond_set_xmit_hash_policy(struct bonding *bond)
4302{
4303 switch (bond->params.xmit_policy) {
4304 case BOND_XMIT_POLICY_LAYER23:
4305 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4306 break;
4307 case BOND_XMIT_POLICY_LAYER34:
4308 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4309 break;
4310 case BOND_XMIT_POLICY_LAYER2:
4311 default:
4312 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4313 break;
4314 }
4315}
4316
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004317/*
4318 * Lookup the slave that corresponds to a qid
4319 */
4320static inline int bond_slave_override(struct bonding *bond,
4321 struct sk_buff *skb)
4322{
4323 int i, res = 1;
4324 struct slave *slave = NULL;
4325 struct slave *check_slave;
4326
4327 read_lock(&bond->lock);
4328
4329 if (!BOND_IS_OK(bond) || !skb->queue_mapping)
4330 goto out;
4331
4332 /* Find out if any slaves have the same mapping as this skb. */
4333 bond_for_each_slave(bond, check_slave, i) {
4334 if (check_slave->queue_id == skb->queue_mapping) {
4335 slave = check_slave;
4336 break;
4337 }
4338 }
4339
4340 /* If the slave isn't UP, use default transmit policy. */
4341 if (slave && slave->queue_id && IS_UP(slave->dev) &&
4342 (slave->link == BOND_LINK_UP)) {
4343 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4344 }
4345
4346out:
4347 read_unlock(&bond->lock);
4348 return res;
4349}
4350
4351static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb)
4352{
4353 /*
4354 * This helper function exists to help dev_pick_tx get the correct
Phil Oesterfd0e4352011-03-14 06:22:04 +00004355 * destination queue. Using a helper function skips a call to
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004356 * skb_tx_hash and will put the skbs in the queue we expect on their
4357 * way down to the bonding driver.
4358 */
Phil Oesterfd0e4352011-03-14 06:22:04 +00004359 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
4360
4361 if (unlikely(txq >= dev->real_num_tx_queues)) {
David Decotignyd30ee672011-04-13 15:22:29 +00004362 do {
Phil Oesterfd0e4352011-03-14 06:22:04 +00004363 txq -= dev->real_num_tx_queues;
David Decotignyd30ee672011-04-13 15:22:29 +00004364 } while (txq >= dev->real_num_tx_queues);
Phil Oesterfd0e4352011-03-14 06:22:04 +00004365 }
4366 return txq;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004367}
4368
Stephen Hemminger424efe92009-08-31 19:50:51 +00004369static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
Stephen Hemminger00829822008-11-20 20:14:53 -08004370{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004371 struct bonding *bond = netdev_priv(dev);
4372
Neil Hormane843fa52010-10-13 16:01:50 +00004373 /*
4374 * If we risk deadlock from transmitting this in the
4375 * netpoll path, tell netpoll to queue the frame for later tx
4376 */
4377 if (is_netpoll_tx_blocked(dev))
4378 return NETDEV_TX_BUSY;
4379
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004380 if (TX_QUEUE_OVERRIDE(bond->params.mode)) {
4381 if (!bond_slave_override(bond, skb))
4382 return NETDEV_TX_OK;
4383 }
Stephen Hemminger00829822008-11-20 20:14:53 -08004384
4385 switch (bond->params.mode) {
4386 case BOND_MODE_ROUNDROBIN:
4387 return bond_xmit_roundrobin(skb, dev);
4388 case BOND_MODE_ACTIVEBACKUP:
4389 return bond_xmit_activebackup(skb, dev);
4390 case BOND_MODE_XOR:
4391 return bond_xmit_xor(skb, dev);
4392 case BOND_MODE_BROADCAST:
4393 return bond_xmit_broadcast(skb, dev);
4394 case BOND_MODE_8023AD:
4395 return bond_3ad_xmit_xor(skb, dev);
4396 case BOND_MODE_ALB:
4397 case BOND_MODE_TLB:
4398 return bond_alb_xmit(skb, dev);
4399 default:
4400 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004401 pr_err("%s: Error: Unknown bonding mode %d\n",
4402 dev->name, bond->params.mode);
Stephen Hemminger00829822008-11-20 20:14:53 -08004403 WARN_ON_ONCE(1);
4404 dev_kfree_skb(skb);
4405 return NETDEV_TX_OK;
4406 }
4407}
4408
4409
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410/*
4411 * set bond mode specific net device operations
4412 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004413void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004415 struct net_device *bond_dev = bond->dev;
4416
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 switch (mode) {
4418 case BOND_MODE_ROUNDROBIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 break;
4420 case BOND_MODE_ACTIVEBACKUP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 break;
4422 case BOND_MODE_XOR:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004423 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 break;
4425 case BOND_MODE_BROADCAST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 break;
4427 case BOND_MODE_8023AD:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004428 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004431 /* FALLTHRU */
4432 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 break;
4434 default:
4435 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004436 pr_err("%s: Error: Unknown bonding mode %d\n",
4437 bond_dev->name, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 break;
4439 }
4440}
4441
Jay Vosburgh217df672005-09-26 16:11:50 -07004442static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4443 struct ethtool_drvinfo *drvinfo)
4444{
4445 strncpy(drvinfo->driver, DRV_NAME, 32);
4446 strncpy(drvinfo->version, DRV_VERSION, 32);
4447 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4448}
4449
Jeff Garzik7282d492006-09-13 14:30:00 -04004450static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004451 .get_drvinfo = bond_ethtool_get_drvinfo,
Stephen Hemmingerfa53eba2008-09-13 21:17:09 -04004452 .get_link = ethtool_op_get_link,
4453 .get_tx_csum = ethtool_op_get_tx_csum,
4454 .get_sg = ethtool_op_get_sg,
4455 .get_tso = ethtool_op_get_tso,
4456 .get_ufo = ethtool_op_get_ufo,
4457 .get_flags = ethtool_op_get_flags,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004458};
4459
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004460static const struct net_device_ops bond_netdev_ops = {
Stephen Hemminger181470f2009-06-12 19:02:52 +00004461 .ndo_init = bond_init,
Stephen Hemminger9e716262009-06-12 19:02:47 +00004462 .ndo_uninit = bond_uninit,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004463 .ndo_open = bond_open,
4464 .ndo_stop = bond_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08004465 .ndo_start_xmit = bond_start_xmit,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004466 .ndo_select_queue = bond_select_queue,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00004467 .ndo_get_stats64 = bond_get_stats,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004468 .ndo_do_ioctl = bond_do_ioctl,
4469 .ndo_set_multicast_list = bond_set_multicast_list,
4470 .ndo_change_mtu = bond_change_mtu,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004471 .ndo_set_mac_address = bond_set_mac_address,
Stephen Hemminger00829822008-11-20 20:14:53 -08004472 .ndo_neigh_setup = bond_neigh_setup,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004473 .ndo_vlan_rx_register = bond_vlan_rx_register,
4474 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4475 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
WANG Congf6dc31a2010-05-06 00:48:51 -07004476#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00004477 .ndo_netpoll_setup = bond_netpoll_setup,
WANG Congf6dc31a2010-05-06 00:48:51 -07004478 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
4479 .ndo_poll_controller = bond_poll_controller,
4480#endif
Jiri Pirko9232ecc2011-02-13 09:33:01 +00004481 .ndo_add_slave = bond_enslave,
4482 .ndo_del_slave = bond_release,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004483};
4484
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004485static void bond_destructor(struct net_device *bond_dev)
4486{
4487 struct bonding *bond = netdev_priv(bond_dev);
4488 if (bond->wq)
4489 destroy_workqueue(bond->wq);
4490 free_netdev(bond_dev);
4491}
4492
Stephen Hemminger181470f2009-06-12 19:02:52 +00004493static void bond_setup(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494{
Wang Chen454d7c92008-11-12 23:37:49 -08004495 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 /* initialize rwlocks */
4498 rwlock_init(&bond->lock);
4499 rwlock_init(&bond->curr_slave_lock);
4500
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00004501 bond->params = bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
4503 /* Initialize pointers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 bond->dev = bond_dev;
4505 INIT_LIST_HEAD(&bond->vlan_list);
4506
4507 /* Initialize the device entry points */
Stephen Hemminger181470f2009-06-12 19:02:52 +00004508 ether_setup(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004509 bond_dev->netdev_ops = &bond_netdev_ops;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004510 bond_dev->ethtool_ops = &bond_ethtool_ops;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004511 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004513 bond_dev->destructor = bond_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
4515 /* Initialize the device options */
4516 bond_dev->tx_queue_len = 0;
4517 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004518 bond_dev->priv_flags |= IFF_BONDING;
Stephen Hemminger181470f2009-06-12 19:02:52 +00004519 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
4520
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 /* At first, we block adding VLANs. That's the only way to
4522 * prevent problems that occur when adding VLANs over an
4523 * empty bond. The block will be removed once non-challenged
4524 * slaves are enslaved.
4525 */
4526 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4527
Herbert Xu932ff272006-06-09 12:20:56 -07004528 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 * transmitting */
4530 bond_dev->features |= NETIF_F_LLTX;
4531
4532 /* By default, we declare the bond to be fully
4533 * VLAN hardware accelerated capable. Special
4534 * care is taken in the various xmit functions
4535 * when there are slaves that are not hw accel
4536 * capable
4537 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4539 NETIF_F_HW_VLAN_RX |
4540 NETIF_F_HW_VLAN_FILTER);
4541
Eric Dumazete6599c22010-09-17 09:25:07 +00004542 /* By default, we enable GRO on bonding devices.
4543 * Actual support requires lowlevel drivers are GRO ready.
4544 */
4545 bond_dev->features |= NETIF_F_GRO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546}
4547
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004548static void bond_work_cancel_all(struct bonding *bond)
4549{
4550 write_lock_bh(&bond->lock);
4551 bond->kill_timers = 1;
4552 write_unlock_bh(&bond->lock);
4553
4554 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4555 cancel_delayed_work(&bond->mii_work);
4556
4557 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4558 cancel_delayed_work(&bond->arp_work);
4559
4560 if (bond->params.mode == BOND_MODE_ALB &&
4561 delayed_work_pending(&bond->alb_work))
4562 cancel_delayed_work(&bond->alb_work);
4563
4564 if (bond->params.mode == BOND_MODE_8023AD &&
4565 delayed_work_pending(&bond->ad_work))
4566 cancel_delayed_work(&bond->ad_work);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00004567
4568 if (delayed_work_pending(&bond->mcast_work))
4569 cancel_delayed_work(&bond->mcast_work);
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004570}
4571
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004572/*
4573* Destroy a bonding device.
4574* Must be under rtnl_lock when this function is called.
4575*/
4576static void bond_uninit(struct net_device *bond_dev)
Jay Vosburgha434e432008-10-30 17:41:15 -07004577{
Wang Chen454d7c92008-11-12 23:37:49 -08004578 struct bonding *bond = netdev_priv(bond_dev);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004579 struct vlan_entry *vlan, *tmp;
Jay Vosburgha434e432008-10-30 17:41:15 -07004580
WANG Congf6dc31a2010-05-06 00:48:51 -07004581 bond_netpoll_cleanup(bond_dev);
4582
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004583 /* Release the bonded slaves */
4584 bond_release_all(bond_dev);
4585
Jay Vosburgha434e432008-10-30 17:41:15 -07004586 list_del(&bond->bond_list);
4587
4588 bond_work_cancel_all(bond);
4589
Jay Vosburgha434e432008-10-30 17:41:15 -07004590 bond_remove_proc_entry(bond);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004591
Taku Izumif073c7c2010-12-09 15:17:13 +00004592 bond_debug_unregister(bond);
4593
Jiri Pirko22bedad32010-04-01 21:22:57 +00004594 __hw_addr_flush(&bond->mc_list);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004595
4596 list_for_each_entry_safe(vlan, tmp, &bond->vlan_list, vlan_list) {
4597 list_del(&vlan->vlan_list);
4598 kfree(vlan);
4599 }
Jay Vosburgha434e432008-10-30 17:41:15 -07004600}
4601
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602/*------------------------- Module initialization ---------------------------*/
4603
4604/*
4605 * Convert string input module parms. Accept either the
Jay Vosburghece95f72008-01-17 16:25:01 -08004606 * number of the mode or its string name. A bit complicated because
4607 * some mode names are substrings of other names, and calls from sysfs
4608 * may have whitespace in the name (trailing newlines, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 */
Holger Eitzenberger325dcf72008-12-09 23:10:17 -08004610int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611{
Hannes Eder54b87322009-02-14 11:15:49 +00004612 int modeint = -1, i, rv;
Jay Vosburgha42e5342008-01-29 18:07:43 -08004613 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
Jay Vosburghece95f72008-01-17 16:25:01 -08004614
Jay Vosburgha42e5342008-01-29 18:07:43 -08004615 for (p = (char *)buf; *p; p++)
4616 if (!(isdigit(*p) || isspace(*p)))
4617 break;
4618
4619 if (*p)
Jay Vosburghece95f72008-01-17 16:25:01 -08004620 rv = sscanf(buf, "%20s", modestr);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004621 else
Hannes Eder54b87322009-02-14 11:15:49 +00004622 rv = sscanf(buf, "%d", &modeint);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004623
4624 if (!rv)
4625 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626
4627 for (i = 0; tbl[i].modename; i++) {
Hannes Eder54b87322009-02-14 11:15:49 +00004628 if (modeint == tbl[i].mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 return tbl[i].mode;
Jay Vosburghece95f72008-01-17 16:25:01 -08004630 if (strcmp(modestr, tbl[i].modename) == 0)
4631 return tbl[i].mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 }
4633
4634 return -1;
4635}
4636
4637static int bond_check_params(struct bond_params *params)
4638{
Jiri Pirkoa5499522009-09-25 03:28:09 +00004639 int arp_validate_value, fail_over_mac_value, primary_reselect_value;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004640
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 /*
4642 * Convert string parameters.
4643 */
4644 if (mode) {
4645 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4646 if (bond_mode == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004647 pr_err("Error: Invalid bonding mode \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 mode == NULL ? "NULL" : mode);
4649 return -EINVAL;
4650 }
4651 }
4652
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004653 if (xmit_hash_policy) {
4654 if ((bond_mode != BOND_MODE_XOR) &&
4655 (bond_mode != BOND_MODE_8023AD)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004656 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004657 bond_mode_name(bond_mode));
4658 } else {
4659 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4660 xmit_hashtype_tbl);
4661 if (xmit_hashtype == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004662 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004663 xmit_hash_policy == NULL ? "NULL" :
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004664 xmit_hash_policy);
4665 return -EINVAL;
4666 }
4667 }
4668 }
4669
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 if (lacp_rate) {
4671 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004672 pr_info("lacp_rate param is irrelevant in mode %s\n",
4673 bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 } else {
4675 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4676 if (lacp_fast == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004677 pr_err("Error: Invalid lacp rate \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 lacp_rate == NULL ? "NULL" : lacp_rate);
4679 return -EINVAL;
4680 }
4681 }
4682 }
4683
Jay Vosburghfd989c82008-11-04 17:51:16 -08004684 if (ad_select) {
4685 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4686 if (params->ad_select == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004687 pr_err("Error: Invalid ad_select \"%s\"\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -08004688 ad_select == NULL ? "NULL" : ad_select);
4689 return -EINVAL;
4690 }
4691
4692 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004693 pr_warning("ad_select param only affects 802.3ad mode\n");
Jay Vosburghfd989c82008-11-04 17:51:16 -08004694 }
4695 } else {
4696 params->ad_select = BOND_AD_STABLE;
4697 }
4698
Nicolas de Pesloüanf5841302009-08-28 13:18:34 +00004699 if (max_bonds < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004700 pr_warning("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4701 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 max_bonds = BOND_DEFAULT_MAX_BONDS;
4703 }
4704
4705 if (miimon < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004706 pr_warning("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to %d\n",
4707 miimon, INT_MAX, BOND_LINK_MON_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 miimon = BOND_LINK_MON_INTERV;
4709 }
4710
4711 if (updelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004712 pr_warning("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4713 updelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 updelay = 0;
4715 }
4716
4717 if (downdelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004718 pr_warning("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4719 downdelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 downdelay = 0;
4721 }
4722
4723 if ((use_carrier != 0) && (use_carrier != 1)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004724 pr_warning("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
4725 use_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 use_carrier = 1;
4727 }
4728
Moni Shoua7893b242008-05-17 21:10:12 -07004729 if (num_grat_arp < 0 || num_grat_arp > 255) {
Frans Pop2381a552010-03-24 07:57:36 +00004730 pr_warning("Warning: num_grat_arp (%d) not in range 0-255 so it was reset to 1\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004731 num_grat_arp);
Moni Shoua7893b242008-05-17 21:10:12 -07004732 num_grat_arp = 1;
4733 }
4734
Brian Haley305d5522008-11-04 17:51:14 -08004735 if (num_unsol_na < 0 || num_unsol_na > 255) {
Frans Pop2381a552010-03-24 07:57:36 +00004736 pr_warning("Warning: num_unsol_na (%d) not in range 0-255 so it was reset to 1\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004737 num_unsol_na);
Brian Haley305d5522008-11-04 17:51:14 -08004738 num_unsol_na = 1;
4739 }
4740
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 /* reset values for 802.3ad */
4742 if (bond_mode == BOND_MODE_8023AD) {
4743 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004744 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 +00004745 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 miimon = 100;
4747 }
4748 }
4749
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004750 if (tx_queues < 1 || tx_queues > 255) {
4751 pr_warning("Warning: tx_queues (%d) should be between "
4752 "1 and 255, resetting to %d\n",
4753 tx_queues, BOND_DEFAULT_TX_QUEUES);
4754 tx_queues = BOND_DEFAULT_TX_QUEUES;
4755 }
4756
Andy Gospodarekebd8e492010-06-02 08:39:21 +00004757 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
4758 pr_warning("Warning: all_slaves_active module parameter (%d), "
4759 "not of valid value (0/1), so it was set to "
4760 "0\n", all_slaves_active);
4761 all_slaves_active = 0;
4762 }
4763
Flavio Leitnerc2952c32010-10-05 14:23:59 +00004764 if (resend_igmp < 0 || resend_igmp > 255) {
4765 pr_warning("Warning: resend_igmp (%d) should be between "
4766 "0 and 255, resetting to %d\n",
4767 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
4768 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
4769 }
4770
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 /* reset values for TLB/ALB */
4772 if ((bond_mode == BOND_MODE_TLB) ||
4773 (bond_mode == BOND_MODE_ALB)) {
4774 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004775 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 +00004776 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 miimon = 100;
4778 }
4779 }
4780
4781 if (bond_mode == BOND_MODE_ALB) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004782 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",
4783 updelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 }
4785
4786 if (!miimon) {
4787 if (updelay || downdelay) {
4788 /* just warn the user the up/down delay will have
4789 * no effect since miimon is zero...
4790 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004791 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",
4792 updelay, downdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 }
4794 } else {
4795 /* don't allow arp monitoring */
4796 if (arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004797 pr_warning("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
4798 miimon, arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 arp_interval = 0;
4800 }
4801
4802 if ((updelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004803 pr_warning("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
4804 updelay, miimon,
4805 (updelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 }
4807
4808 updelay /= miimon;
4809
4810 if ((downdelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004811 pr_warning("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
4812 downdelay, miimon,
4813 (downdelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 }
4815
4816 downdelay /= miimon;
4817 }
4818
4819 if (arp_interval < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004820 pr_warning("Warning: arp_interval module parameter (%d) , not in range 0-%d, so it was reset to %d\n",
4821 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 arp_interval = BOND_LINK_ARP_INTERV;
4823 }
4824
4825 for (arp_ip_count = 0;
4826 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4827 arp_ip_count++) {
4828 /* not complete check, but should be good enough to
4829 catch mistakes */
4830 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004831 pr_warning("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
4832 arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 arp_interval = 0;
4834 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04004835 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 arp_target[arp_ip_count] = ip;
4837 }
4838 }
4839
4840 if (arp_interval && !arp_ip_count) {
4841 /* don't allow arping if no arp_ip_target given... */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004842 pr_warning("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
4843 arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 arp_interval = 0;
4845 }
4846
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004847 if (arp_validate) {
4848 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004849 pr_err("arp_validate only supported in active-backup mode\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004850 return -EINVAL;
4851 }
4852 if (!arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004853 pr_err("arp_validate requires arp_interval\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004854 return -EINVAL;
4855 }
4856
4857 arp_validate_value = bond_parse_parm(arp_validate,
4858 arp_validate_tbl);
4859 if (arp_validate_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004860 pr_err("Error: invalid arp_validate \"%s\"\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004861 arp_validate == NULL ? "NULL" : arp_validate);
4862 return -EINVAL;
4863 }
4864 } else
4865 arp_validate_value = 0;
4866
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 if (miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004868 pr_info("MII link monitoring set to %d ms\n", miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 } else if (arp_interval) {
4870 int i;
4871
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004872 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
4873 arp_interval,
4874 arp_validate_tbl[arp_validate_value].modename,
4875 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
4877 for (i = 0; i < arp_ip_count; i++)
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00004878 pr_info(" %s", arp_ip_target[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00004880 pr_info("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
Jay Vosburghb8a97872008-06-13 18:12:04 -07004882 } else if (max_bonds) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 /* miimon and arp_interval not set, we need one so things
4884 * work as expected, see bonding.txt for details
4885 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004886 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 -07004887 }
4888
4889 if (primary && !USES_PRIMARY(bond_mode)) {
4890 /* currently, using a primary only makes sense
4891 * in active backup, TLB or ALB modes
4892 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004893 pr_warning("Warning: %s primary device specified but has no effect in %s mode\n",
4894 primary, bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 primary = NULL;
4896 }
4897
Jiri Pirkoa5499522009-09-25 03:28:09 +00004898 if (primary && primary_reselect) {
4899 primary_reselect_value = bond_parse_parm(primary_reselect,
4900 pri_reselect_tbl);
4901 if (primary_reselect_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004902 pr_err("Error: Invalid primary_reselect \"%s\"\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00004903 primary_reselect ==
4904 NULL ? "NULL" : primary_reselect);
4905 return -EINVAL;
4906 }
4907 } else {
4908 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
4909 }
4910
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004911 if (fail_over_mac) {
4912 fail_over_mac_value = bond_parse_parm(fail_over_mac,
4913 fail_over_mac_tbl);
4914 if (fail_over_mac_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004915 pr_err("Error: invalid fail_over_mac \"%s\"\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004916 arp_validate == NULL ? "NULL" : arp_validate);
4917 return -EINVAL;
4918 }
4919
4920 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004921 pr_warning("Warning: fail_over_mac only affects active-backup mode.\n");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004922 } else {
4923 fail_over_mac_value = BOND_FOM_NONE;
4924 }
Jay Vosburghdd957c52007-10-09 19:57:24 -07004925
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 /* fill params struct with the proper values */
4927 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004928 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 params->miimon = miimon;
Moni Shoua7893b242008-05-17 21:10:12 -07004930 params->num_grat_arp = num_grat_arp;
Brian Haley305d5522008-11-04 17:51:14 -08004931 params->num_unsol_na = num_unsol_na;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004933 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 params->updelay = updelay;
4935 params->downdelay = downdelay;
4936 params->use_carrier = use_carrier;
4937 params->lacp_fast = lacp_fast;
4938 params->primary[0] = 0;
Jiri Pirkoa5499522009-09-25 03:28:09 +00004939 params->primary_reselect = primary_reselect_value;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004940 params->fail_over_mac = fail_over_mac_value;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004941 params->tx_queues = tx_queues;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00004942 params->all_slaves_active = all_slaves_active;
Flavio Leitnerc2952c32010-10-05 14:23:59 +00004943 params->resend_igmp = resend_igmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944
4945 if (primary) {
4946 strncpy(params->primary, primary, IFNAMSIZ);
4947 params->primary[IFNAMSIZ - 1] = 0;
4948 }
4949
4950 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4951
4952 return 0;
4953}
4954
Peter Zijlstra0daa23032006-11-08 19:51:01 -08004955static struct lock_class_key bonding_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -07004956static struct lock_class_key bonding_netdev_addr_lock_key;
Peter Zijlstra0daa23032006-11-08 19:51:01 -08004957
David S. Millere8a04642008-07-17 00:34:19 -07004958static void bond_set_lockdep_class_one(struct net_device *dev,
4959 struct netdev_queue *txq,
4960 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -07004961{
4962 lockdep_set_class(&txq->_xmit_lock,
4963 &bonding_netdev_xmit_lock_key);
4964}
4965
4966static void bond_set_lockdep_class(struct net_device *dev)
4967{
David S. Millercf508b12008-07-22 14:16:42 -07004968 lockdep_set_class(&dev->addr_list_lock,
4969 &bonding_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -07004970 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -07004971}
4972
Stephen Hemminger181470f2009-06-12 19:02:52 +00004973/*
4974 * Called from registration process
4975 */
4976static int bond_init(struct net_device *bond_dev)
4977{
4978 struct bonding *bond = netdev_priv(bond_dev);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004979 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Stephen Hemminger181470f2009-06-12 19:02:52 +00004980
4981 pr_debug("Begin bond_init for %s\n", bond_dev->name);
4982
4983 bond->wq = create_singlethread_workqueue(bond_dev->name);
4984 if (!bond->wq)
4985 return -ENOMEM;
4986
4987 bond_set_lockdep_class(bond_dev);
4988
Stephen Hemminger181470f2009-06-12 19:02:52 +00004989 bond_create_proc_entry(bond);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004990 list_add_tail(&bond->bond_list, &bn->dev_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00004991
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00004992 bond_prepare_sysfs_group(bond);
Jiri Pirko22bedad32010-04-01 21:22:57 +00004993
Taku Izumif073c7c2010-12-09 15:17:13 +00004994 bond_debug_register(bond);
4995
Jiri Pirko22bedad32010-04-01 21:22:57 +00004996 __hw_addr_init(&bond->mc_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00004997 return 0;
4998}
4999
Eric W. Biederman88ead972009-10-29 14:18:25 +00005000static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
5001{
5002 if (tb[IFLA_ADDRESS]) {
5003 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
5004 return -EINVAL;
5005 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
5006 return -EADDRNOTAVAIL;
5007 }
5008 return 0;
5009}
5010
5011static struct rtnl_link_ops bond_link_ops __read_mostly = {
5012 .kind = "bond",
Eric W. Biederman66391042009-10-29 23:58:54 +00005013 .priv_size = sizeof(struct bonding),
Eric W. Biederman88ead972009-10-29 14:18:25 +00005014 .setup = bond_setup,
5015 .validate = bond_validate,
5016};
5017
Mitch Williamsdfe60392005-11-09 10:36:04 -08005018/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005019 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08005020 * Caller must NOT hold rtnl_lock; we need to release it here before we
5021 * set up our sysfs entries.
5022 */
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005023int bond_create(struct net *net, const char *name)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005024{
5025 struct net_device *bond_dev;
5026 int res;
5027
5028 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08005029
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005030 bond_dev = alloc_netdev_mq(sizeof(struct bonding), name ? name : "",
5031 bond_setup, tx_queues);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005032 if (!bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005033 pr_err("%s: eek! can't alloc netdev!\n", name);
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005034 rtnl_unlock();
5035 return -ENOMEM;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005036 }
5037
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005038 dev_net_set(bond_dev, net);
Eric W. Biederman88ead972009-10-29 14:18:25 +00005039 bond_dev->rtnl_link_ops = &bond_link_ops;
5040
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005041 if (!name) {
5042 res = dev_alloc_name(bond_dev, "bond%d");
5043 if (res < 0)
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005044 goto out;
Neil Horman27e6f062010-10-05 03:39:21 +00005045 } else {
5046 /*
5047 * If we're given a name to register
5048 * we need to ensure that its not already
5049 * registered
5050 */
5051 res = -EEXIST;
5052 if (__dev_get_by_name(net, name) != NULL)
5053 goto out;
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005054 }
5055
Mitch Williamsdfe60392005-11-09 10:36:04 -08005056 res = register_netdevice(bond_dev);
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005057
Phil Oestere826eaf2011-03-14 06:22:05 +00005058 netif_carrier_off(bond_dev);
5059
Eric W. Biederman30c15ba2009-10-29 14:18:23 +00005060out:
Mitch Williamsdfe60392005-11-09 10:36:04 -08005061 rtnl_unlock();
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005062 if (res < 0)
5063 bond_destructor(bond_dev);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005064 return res;
5065}
5066
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005067static int __net_init bond_net_init(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005068{
Eric W. Biederman15449742009-11-29 15:46:04 +00005069 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005070
5071 bn->net = net;
5072 INIT_LIST_HEAD(&bn->dev_list);
5073
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005074 bond_create_proc_dir(bn);
Eric W. Biederman15449742009-11-29 15:46:04 +00005075
5076 return 0;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005077}
5078
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005079static void __net_exit bond_net_exit(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005080{
Eric W. Biederman15449742009-11-29 15:46:04 +00005081 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005082
5083 bond_destroy_proc_dir(bn);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005084}
5085
5086static struct pernet_operations bond_net_ops = {
5087 .init = bond_net_init,
5088 .exit = bond_net_exit,
Eric W. Biederman15449742009-11-29 15:46:04 +00005089 .id = &bond_net_id,
5090 .size = sizeof(struct bond_net),
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005091};
5092
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093static int __init bonding_init(void)
5094{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 int i;
5096 int res;
5097
Amerigo Wangbd33acc2011-03-06 21:58:46 +00005098 pr_info("%s", bond_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099
Mitch Williamsdfe60392005-11-09 10:36:04 -08005100 res = bond_check_params(&bonding_defaults);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005101 if (res)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005102 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103
Eric W. Biederman15449742009-11-29 15:46:04 +00005104 res = register_pernet_subsys(&bond_net_ops);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005105 if (res)
5106 goto out;
Jay Vosburgh027ea042008-01-17 16:25:02 -08005107
Eric W. Biederman88ead972009-10-29 14:18:25 +00005108 res = rtnl_link_register(&bond_link_ops);
5109 if (res)
Eric W. Biederman66391042009-10-29 23:58:54 +00005110 goto err_link;
Eric W. Biederman88ead972009-10-29 14:18:25 +00005111
Taku Izumif073c7c2010-12-09 15:17:13 +00005112 bond_create_debugfs();
5113
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 for (i = 0; i < max_bonds; i++) {
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005115 res = bond_create(&init_net, NULL);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005116 if (res)
5117 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 }
5119
Mitch Williamsb76cdba2005-11-09 10:36:41 -08005120 res = bond_create_sysfs();
5121 if (res)
5122 goto err;
5123
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005125 register_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005126 bond_register_ipv6_notifier();
Mitch Williamsdfe60392005-11-09 10:36:04 -08005127out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 return res;
Eric W. Biederman88ead972009-10-29 14:18:25 +00005129err:
5130 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman66391042009-10-29 23:58:54 +00005131err_link:
Eric W. Biederman15449742009-11-29 15:46:04 +00005132 unregister_pernet_subsys(&bond_net_ops);
Eric W. Biederman88ead972009-10-29 14:18:25 +00005133 goto out;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005134
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135}
5136
5137static void __exit bonding_exit(void)
5138{
5139 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005140 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005141 bond_unregister_ipv6_notifier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005143 bond_destroy_sysfs();
Taku Izumif073c7c2010-12-09 15:17:13 +00005144 bond_destroy_debugfs();
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005145
Eric W. Biederman88ead972009-10-29 14:18:25 +00005146 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman15449742009-11-29 15:46:04 +00005147 unregister_pernet_subsys(&bond_net_ops);
Neil Hormane843fa52010-10-13 16:01:50 +00005148
5149#ifdef CONFIG_NET_POLL_CONTROLLER
Neil Hormanfb4fa762010-12-06 09:05:50 +00005150 /*
5151 * Make sure we don't have an imbalance on our netpoll blocking
5152 */
5153 WARN_ON(atomic_read(&netpoll_block_tx));
Neil Hormane843fa52010-10-13 16:01:50 +00005154#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155}
5156
5157module_init(bonding_init);
5158module_exit(bonding_exit);
5159MODULE_LICENSE("GPL");
5160MODULE_VERSION(DRV_VERSION);
5161MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5162MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
Eric W. Biederman88ead972009-10-29 14:18:25 +00005163MODULE_ALIAS_RTNL_LINK("bond");