blob: b047d75c28d43495b82743ba3636e1c6f330f8c7 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 struct ethtool_cmd etool;
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700635 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 /* Fake speed and duplex */
638 slave->speed = SPEED_100;
639 slave->duplex = DUPLEX_FULL;
640
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700641 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700644 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
645 if (res < 0)
646 return -1;
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 switch (etool.speed) {
649 case SPEED_10:
650 case SPEED_100:
651 case SPEED_1000:
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700652 case SPEED_10000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 break;
654 default:
655 return -1;
656 }
657
658 switch (etool.duplex) {
659 case DUPLEX_FULL:
660 case DUPLEX_HALF:
661 break;
662 default:
663 return -1;
664 }
665
666 slave->speed = etool.speed;
667 slave->duplex = etool.duplex;
668
669 return 0;
670}
671
672/*
673 * if <dev> supports MII link status reporting, check its link status.
674 *
675 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000676 * depending upon the setting of the use_carrier parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 *
678 * Return either BMSR_LSTATUS, meaning that the link is up (or we
679 * can't tell and just pretend it is), or 0, meaning that the link is
680 * down.
681 *
682 * If reporting is non-zero, instead of faking link up, return -1 if
683 * both ETHTOOL and MII ioctls fail (meaning the device does not
684 * support them). If use_carrier is set, return whatever it says.
685 * It'd be nice if there was a good way to tell if a driver supports
686 * netif_carrier, but there really isn't.
687 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000688static int bond_check_dev_link(struct bonding *bond,
689 struct net_device *slave_dev, int reporting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800691 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Jiri Bohacd9d52832009-10-28 22:23:54 -0700692 int (*ioctl)(struct net_device *, struct ifreq *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 struct ifreq ifr;
694 struct mii_ioctl_data *mii;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Petri Gynther6c988852009-08-28 12:05:15 +0000696 if (!reporting && !netif_running(slave_dev))
697 return 0;
698
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800699 if (bond->params.use_carrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Jiri Pirko29112f42009-04-24 01:58:23 +0000702 /* Try to get link status using Ethtool first. */
703 if (slave_dev->ethtool_ops) {
704 if (slave_dev->ethtool_ops->get_link) {
705 u32 link;
706
707 link = slave_dev->ethtool_ops->get_link(slave_dev);
708
709 return link ? BMSR_LSTATUS : 0;
710 }
711 }
712
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000713 /* Ethtool can't be used, fallback to MII ioctls. */
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800714 ioctl = slave_ops->ndo_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (ioctl) {
716 /* TODO: set pointer to correct ioctl on a per team member */
717 /* bases to make this more efficient. that is, once */
718 /* we determine the correct ioctl, we will always */
719 /* call it and not the others for that team */
720 /* member. */
721
722 /*
723 * We cannot assume that SIOCGMIIPHY will also read a
724 * register; not all network drivers (e.g., e100)
725 * support that.
726 */
727
728 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
729 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
730 mii = if_mii(&ifr);
731 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
732 mii->reg_num = MII_BMSR;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000733 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0)
734 return mii->val_out & BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736 }
737
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700738 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 * If reporting, report that either there's no dev->do_ioctl,
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700740 * or both SIOCGMIIREG and get_link failed (meaning that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 * cannot report link status). If not reporting, pretend
742 * we're ok.
743 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000744 return reporting ? -1 : BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
747/*----------------------------- Multicast list ------------------------------*/
748
749/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 * Push the promiscuity flag down to appropriate slaves
751 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700752static int bond_set_promiscuity(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700754 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (USES_PRIMARY(bond->params.mode)) {
756 /* write lock already acquired */
757 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700758 err = dev_set_promiscuity(bond->curr_active_slave->dev,
759 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761 } else {
762 struct slave *slave;
763 int i;
764 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700765 err = dev_set_promiscuity(slave->dev, inc);
766 if (err)
767 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700770 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773/*
774 * Push the allmulti flag down to all slaves
775 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700776static int bond_set_allmulti(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700778 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (USES_PRIMARY(bond->params.mode)) {
780 /* write lock already acquired */
781 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700782 err = dev_set_allmulti(bond->curr_active_slave->dev,
783 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785 } else {
786 struct slave *slave;
787 int i;
788 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700789 err = dev_set_allmulti(slave->dev, inc);
790 if (err)
791 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700794 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
797/*
798 * Add a Multicast address to slaves
799 * according to mode
800 */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000801static void bond_mc_add(struct bonding *bond, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 if (USES_PRIMARY(bond->params.mode)) {
804 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000805 if (bond->curr_active_slave)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000806 dev_mc_add(bond->curr_active_slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 } else {
808 struct slave *slave;
809 int i;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000810
811 bond_for_each_slave(bond, slave, i)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000812 dev_mc_add(slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814}
815
816/*
817 * Remove a multicast address from slave
818 * according to mode
819 */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000820static void bond_mc_del(struct bonding *bond, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 if (USES_PRIMARY(bond->params.mode)) {
823 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000824 if (bond->curr_active_slave)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000825 dev_mc_del(bond->curr_active_slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 } else {
827 struct slave *slave;
828 int i;
829 bond_for_each_slave(bond, slave, i) {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000830 dev_mc_del(slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832 }
833}
834
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800835
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000836static void __bond_resend_igmp_join_requests(struct net_device *dev)
837{
838 struct in_device *in_dev;
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000839
840 rcu_read_lock();
841 in_dev = __in_dev_get_rcu(dev);
Eric Dumazet866f3b22010-11-18 09:33:19 -0800842 if (in_dev)
David S. Miller24912422010-11-19 13:13:47 -0800843 ip_mc_rejoin_groups(in_dev);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000844 rcu_read_unlock();
845}
846
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800847/*
848 * Retrieve the list of registered multicast addresses for the bonding
849 * device and retransmit an IGMP JOIN request to the current active
850 * slave.
851 */
852static void bond_resend_igmp_join_requests(struct bonding *bond)
853{
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000854 struct net_device *vlan_dev;
855 struct vlan_entry *vlan;
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800856
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000857 read_lock(&bond->lock);
858
859 /* rejoin all groups on bond device */
860 __bond_resend_igmp_join_requests(bond->dev);
861
862 /* rejoin all groups on vlan devices */
863 if (bond->vlgrp) {
864 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
865 vlan_dev = vlan_group_get_device(bond->vlgrp,
866 vlan->vlan_id);
867 if (vlan_dev)
868 __bond_resend_igmp_join_requests(vlan_dev);
869 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800870 }
871
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000872 if (--bond->igmp_retrans > 0)
873 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
874
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000875 read_unlock(&bond->lock);
876}
877
stephen hemminger379b7382010-10-15 11:02:56 +0000878static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000879{
880 struct bonding *bond = container_of(work, struct bonding,
881 mcast_work.work);
882 bond_resend_igmp_join_requests(bond);
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800883}
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 * flush all members of flush->mc_list from device dev->mc_list
887 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000888static void bond_mc_list_flush(struct net_device *bond_dev,
889 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Wang Chen454d7c92008-11-12 23:37:49 -0800891 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000892 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Jiri Pirko22bedad32010-04-01 21:22:57 +0000894 netdev_for_each_mc_addr(ha, bond_dev)
895 dev_mc_del(slave_dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 if (bond->params.mode == BOND_MODE_8023AD) {
898 /* del lacpdu mc addr from mc list */
899 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
900
Jiri Pirko22bedad32010-04-01 21:22:57 +0000901 dev_mc_del(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903}
904
905/*--------------------------- Active slave change ---------------------------*/
906
907/*
908 * Update the mc list and multicast-related flags for the new and
909 * old active slaves (if any) according to the multicast mode, and
910 * promiscuous flags unconditionally.
911 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000912static void bond_mc_swap(struct bonding *bond, struct slave *new_active,
913 struct slave *old_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Jiri Pirko22bedad32010-04-01 21:22:57 +0000915 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000917 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 /* nothing to do - mc list is already up-to-date on
919 * all slaves
920 */
921 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 if (old_active) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000924 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 dev_set_promiscuity(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000927 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 dev_set_allmulti(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Jiri Pirko22bedad32010-04-01 21:22:57 +0000930 netdev_for_each_mc_addr(ha, bond->dev)
931 dev_mc_del(old_active->dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933
934 if (new_active) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700935 /* FIXME: Signal errors upstream. */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000936 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 dev_set_promiscuity(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000939 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 dev_set_allmulti(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Jiri Pirko22bedad32010-04-01 21:22:57 +0000942 netdev_for_each_mc_addr(ha, bond->dev)
943 dev_mc_add(new_active->dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945}
946
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700947/*
948 * bond_do_fail_over_mac
949 *
950 * Perform special MAC address swapping for fail_over_mac settings
951 *
952 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
953 */
954static void bond_do_fail_over_mac(struct bonding *bond,
955 struct slave *new_active,
956 struct slave *old_active)
Hannes Eder1f78d9f2009-02-14 11:15:33 +0000957 __releases(&bond->curr_slave_lock)
958 __releases(&bond->lock)
959 __acquires(&bond->lock)
960 __acquires(&bond->curr_slave_lock)
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700961{
962 u8 tmp_mac[ETH_ALEN];
963 struct sockaddr saddr;
964 int rv;
965
966 switch (bond->params.fail_over_mac) {
967 case BOND_FOM_ACTIVE:
968 if (new_active)
969 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
970 new_active->dev->addr_len);
971 break;
972 case BOND_FOM_FOLLOW:
973 /*
974 * if new_active && old_active, swap them
975 * if just old_active, do nothing (going to no active slave)
976 * if just new_active, set new_active to bond's MAC
977 */
978 if (!new_active)
979 return;
980
981 write_unlock_bh(&bond->curr_slave_lock);
982 read_unlock(&bond->lock);
983
984 if (old_active) {
985 memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
986 memcpy(saddr.sa_data, old_active->dev->dev_addr,
987 ETH_ALEN);
988 saddr.sa_family = new_active->dev->type;
989 } else {
990 memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
991 saddr.sa_family = bond->dev->type;
992 }
993
994 rv = dev_set_mac_address(new_active->dev, &saddr);
995 if (rv) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800996 pr_err("%s: Error %d setting MAC of slave %s\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700997 bond->dev->name, -rv, new_active->dev->name);
998 goto out;
999 }
1000
1001 if (!old_active)
1002 goto out;
1003
1004 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
1005 saddr.sa_family = old_active->dev->type;
1006
1007 rv = dev_set_mac_address(old_active->dev, &saddr);
1008 if (rv)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001009 pr_err("%s: Error %d setting MAC of slave %s\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001010 bond->dev->name, -rv, new_active->dev->name);
1011out:
1012 read_lock(&bond->lock);
1013 write_lock_bh(&bond->curr_slave_lock);
1014 break;
1015 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001016 pr_err("%s: bond_do_fail_over_mac impossible: bad policy %d\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001017 bond->dev->name, bond->params.fail_over_mac);
1018 break;
1019 }
1020
1021}
1022
Jiri Pirkoa5499522009-09-25 03:28:09 +00001023static bool bond_should_change_active(struct bonding *bond)
1024{
1025 struct slave *prim = bond->primary_slave;
1026 struct slave *curr = bond->curr_active_slave;
1027
1028 if (!prim || !curr || curr->link != BOND_LINK_UP)
1029 return true;
1030 if (bond->force_primary) {
1031 bond->force_primary = false;
1032 return true;
1033 }
1034 if (bond->params.primary_reselect == BOND_PRI_RESELECT_BETTER &&
1035 (prim->speed < curr->speed ||
1036 (prim->speed == curr->speed && prim->duplex <= curr->duplex)))
1037 return false;
1038 if (bond->params.primary_reselect == BOND_PRI_RESELECT_FAILURE)
1039 return false;
1040 return true;
1041}
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043/**
1044 * find_best_interface - select the best available slave to be the active one
1045 * @bond: our bonding struct
1046 *
1047 * Warning: Caller must hold curr_slave_lock for writing.
1048 */
1049static struct slave *bond_find_best_slave(struct bonding *bond)
1050{
1051 struct slave *new_active, *old_active;
1052 struct slave *bestslave = NULL;
1053 int mintime = bond->params.updelay;
1054 int i;
1055
Nicolas de Pesloüan49b4ad92009-10-07 14:11:00 -07001056 new_active = bond->curr_active_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 if (!new_active) { /* there were no active slaves left */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001059 if (bond->slave_cnt > 0) /* found one slave */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 new_active = bond->first_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001061 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return NULL; /* still no slave, return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if ((bond->primary_slave) &&
Jiri Pirkoa5499522009-09-25 03:28:09 +00001066 bond->primary_slave->link == BOND_LINK_UP &&
1067 bond_should_change_active(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 new_active = bond->primary_slave;
1069 }
1070
1071 /* remember where to stop iterating over the slaves */
1072 old_active = new_active;
1073
1074 bond_for_each_slave_from(bond, new_active, i, old_active) {
Jiri Pirkob9f60252009-08-31 11:09:38 +00001075 if (new_active->link == BOND_LINK_UP) {
1076 return new_active;
1077 } else if (new_active->link == BOND_LINK_BACK &&
1078 IS_UP(new_active->dev)) {
1079 /* link up, but waiting for stabilization */
1080 if (new_active->delay < mintime) {
1081 mintime = new_active->delay;
1082 bestslave = new_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084 }
1085 }
1086
1087 return bestslave;
1088}
1089
1090/**
1091 * change_active_interface - change the active slave into the specified one
1092 * @bond: our bonding struct
1093 * @new: the new slave to make the active one
1094 *
1095 * Set the new slave to the bond's settings and unset them on the old
1096 * curr_active_slave.
1097 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1098 *
1099 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1100 * because it is apparently the best available slave we have, even though its
1101 * updelay hasn't timed out yet.
1102 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001103 * If new_active is not NULL, caller must hold bond->lock for read and
1104 * curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001106void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
1108 struct slave *old_active = bond->curr_active_slave;
1109
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001110 if (old_active == new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 if (new_active) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07001114 new_active->jiffies = jiffies;
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (new_active->link == BOND_LINK_BACK) {
1117 if (USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001118 pr_info("%s: making interface %s the new active one %d ms earlier.\n",
1119 bond->dev->name, new_active->dev->name,
1120 (bond->params.updelay - new_active->delay) * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
1122
1123 new_active->delay = 0;
1124 new_active->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001126 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Holger Eitzenberger58402052008-12-09 23:07:13 -08001129 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 } else {
1132 if (USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001133 pr_info("%s: making interface %s the new active one.\n",
1134 bond->dev->name, new_active->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136 }
1137 }
1138
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001139 if (USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 bond_mc_swap(bond, new_active, old_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Holger Eitzenberger58402052008-12-09 23:07:13 -08001142 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 bond_alb_handle_active_change(bond, new_active);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001144 if (old_active)
1145 bond_set_slave_inactive_flags(old_active);
1146 if (new_active)
1147 bond_set_slave_active_flags(new_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 } else {
1149 bond->curr_active_slave = new_active;
1150 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001151
1152 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001153 if (old_active)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001154 bond_set_slave_inactive_flags(old_active);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001155
1156 if (new_active) {
1157 bond_set_slave_active_flags(new_active);
Moni Shoua2ab82852007-10-09 19:43:39 -07001158
Or Gerlitz709f8a42008-06-13 18:12:01 -07001159 if (bond->params.fail_over_mac)
1160 bond_do_fail_over_mac(bond, new_active,
1161 old_active);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001162
Ben Hutchingsffa95ed2010-12-13 08:19:56 +00001163 if (netif_running(bond->dev)) {
1164 bond->send_grat_arp = bond->params.num_grat_arp;
1165 bond_send_gratuitous_arp(bond);
Or Gerlitz01f31092008-06-13 18:12:02 -07001166
Ben Hutchingsffa95ed2010-12-13 08:19:56 +00001167 bond->send_unsol_na = bond->params.num_unsol_na;
1168 bond_send_unsolicited_na(bond);
1169 }
Brian Haley305d5522008-11-04 17:51:14 -08001170
Or Gerlitz01f31092008-06-13 18:12:02 -07001171 write_unlock_bh(&bond->curr_slave_lock);
1172 read_unlock(&bond->lock);
1173
Moni Shoua75c78502009-09-15 02:37:40 -07001174 netdev_bonding_change(bond->dev, NETDEV_BONDING_FAILOVER);
Or Gerlitz01f31092008-06-13 18:12:02 -07001175
1176 read_lock(&bond->lock);
1177 write_lock_bh(&bond->curr_slave_lock);
Moni Shoua7893b242008-05-17 21:10:12 -07001178 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001179 }
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001180
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001181 /* resend IGMP joins since active slave has changed or
1182 * all were sent on curr_active_slave */
Ben Hutchingsffa95ed2010-12-13 08:19:56 +00001183 if (((USES_PRIMARY(bond->params.mode) && new_active) ||
1184 bond->params.mode == BOND_MODE_ROUNDROBIN) &&
1185 netif_running(bond->dev)) {
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001186 bond->igmp_retrans = bond->params.resend_igmp;
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001187 queue_delayed_work(bond->wq, &bond->mcast_work, 0);
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
1191/**
1192 * bond_select_active_slave - select a new active slave, if needed
1193 * @bond: our bonding struct
1194 *
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001195 * This functions should be called when one of the following occurs:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 * - The old curr_active_slave has been released or lost its link.
1197 * - The primary_slave has got its link back.
1198 * - A slave has got its link back and there's no old curr_active_slave.
1199 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001200 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001202void bond_select_active_slave(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 struct slave *best_slave;
Jay Vosburghff59c452006-03-27 13:27:43 -08001205 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 best_slave = bond_find_best_slave(bond);
1208 if (best_slave != bond->curr_active_slave) {
1209 bond_change_active_slave(bond, best_slave);
Jay Vosburghff59c452006-03-27 13:27:43 -08001210 rv = bond_set_carrier(bond);
1211 if (!rv)
1212 return;
1213
1214 if (netif_carrier_ok(bond->dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001215 pr_info("%s: first active interface up!\n",
1216 bond->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08001217 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001218 pr_info("%s: now running without any active interface !\n",
1219 bond->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08001220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222}
1223
1224/*--------------------------- slave list handling ---------------------------*/
1225
1226/*
1227 * This function attaches the slave to the end of list.
1228 *
1229 * bond->lock held for writing by caller.
1230 */
1231static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1232{
1233 if (bond->first_slave == NULL) { /* attaching the first slave */
1234 new_slave->next = new_slave;
1235 new_slave->prev = new_slave;
1236 bond->first_slave = new_slave;
1237 } else {
1238 new_slave->next = bond->first_slave;
1239 new_slave->prev = bond->first_slave->prev;
1240 new_slave->next->prev = new_slave;
1241 new_slave->prev->next = new_slave;
1242 }
1243
1244 bond->slave_cnt++;
1245}
1246
1247/*
1248 * This function detaches the slave from the list.
1249 * WARNING: no check is made to verify if the slave effectively
1250 * belongs to <bond>.
1251 * Nothing is freed on return, structures are just unchained.
1252 * If any slave pointer in bond was pointing to <slave>,
1253 * it should be changed by the calling function.
1254 *
1255 * bond->lock held for writing by caller.
1256 */
1257static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1258{
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001259 if (slave->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 slave->next->prev = slave->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001262 if (slave->prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 slave->prev->next = slave->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 if (bond->first_slave == slave) { /* slave is the first slave */
1266 if (bond->slave_cnt > 1) { /* there are more slave */
1267 bond->first_slave = slave->next;
1268 } else {
1269 bond->first_slave = NULL; /* slave was the last one */
1270 }
1271 }
1272
1273 slave->next = NULL;
1274 slave->prev = NULL;
1275 bond->slave_cnt--;
1276}
1277
WANG Congf6dc31a2010-05-06 00:48:51 -07001278#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001279static inline int slave_enable_netpoll(struct slave *slave)
WANG Congf6dc31a2010-05-06 00:48:51 -07001280{
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001281 struct netpoll *np;
1282 int err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001283
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001284 np = kzalloc(sizeof(*np), GFP_KERNEL);
1285 err = -ENOMEM;
1286 if (!np)
1287 goto out;
1288
1289 np->dev = slave->dev;
1290 err = __netpoll_setup(np);
1291 if (err) {
1292 kfree(np);
1293 goto out;
WANG Congf6dc31a2010-05-06 00:48:51 -07001294 }
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001295 slave->np = np;
1296out:
1297 return err;
1298}
1299static inline void slave_disable_netpoll(struct slave *slave)
1300{
1301 struct netpoll *np = slave->np;
1302
1303 if (!np)
1304 return;
1305
1306 slave->np = NULL;
1307 synchronize_rcu_bh();
1308 __netpoll_cleanup(np);
1309 kfree(np);
1310}
1311static inline bool slave_dev_support_netpoll(struct net_device *slave_dev)
1312{
1313 if (slave_dev->priv_flags & IFF_DISABLE_NETPOLL)
1314 return false;
1315 if (!slave_dev->netdev_ops->ndo_poll_controller)
1316 return false;
1317 return true;
WANG Congf6dc31a2010-05-06 00:48:51 -07001318}
1319
1320static void bond_poll_controller(struct net_device *bond_dev)
1321{
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001322}
1323
1324static void __bond_netpoll_cleanup(struct bonding *bond)
1325{
Neil Hormanc2355e12010-10-13 16:01:49 +00001326 struct slave *slave;
1327 int i;
1328
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001329 bond_for_each_slave(bond, slave, i)
1330 if (IS_UP(slave->dev))
1331 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07001332}
WANG Congf6dc31a2010-05-06 00:48:51 -07001333static void bond_netpoll_cleanup(struct net_device *bond_dev)
1334{
1335 struct bonding *bond = netdev_priv(bond_dev);
WANG Congf6dc31a2010-05-06 00:48:51 -07001336
1337 read_lock(&bond->lock);
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001338 __bond_netpoll_cleanup(bond);
WANG Congf6dc31a2010-05-06 00:48:51 -07001339 read_unlock(&bond->lock);
1340}
1341
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001342static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
1343{
1344 struct bonding *bond = netdev_priv(dev);
1345 struct slave *slave;
1346 int i, err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001347
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001348 read_lock(&bond->lock);
1349 bond_for_each_slave(bond, slave, i) {
1350 if (!IS_UP(slave->dev))
1351 continue;
1352 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);
Herbert Xub63365a2008-10-23 01:11:29 -07001412 features |= NETIF_F_GSO_MASK | NETIF_F_NO_CSUM;
1413
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,
1461 struct net_device *slave_dev,
1462 struct net_device *bond_dev)
1463{
1464 if (slave_dev->priv_flags & IFF_SLAVE_INACTIVE) {
1465 if (slave_dev->priv_flags & IFF_SLAVE_NEEDARP &&
1466 skb->protocol == __cpu_to_be16(ETH_P_ARP))
1467 return false;
1468
1469 if (bond_dev->priv_flags & IFF_MASTER_ALB &&
1470 skb->pkt_type != PACKET_BROADCAST &&
1471 skb->pkt_type != PACKET_MULTICAST)
1472 return false;
1473
1474 if (bond_dev->priv_flags & IFF_MASTER_8023AD &&
1475 skb->protocol == __cpu_to_be16(ETH_P_SLOW))
1476 return false;
1477
1478 return true;
1479 }
1480 return false;
1481}
1482
1483static struct sk_buff *bond_handle_frame(struct sk_buff *skb)
1484{
Jiri Pirkof1c17752011-03-12 03:14:35 +00001485 struct slave *slave;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001486 struct net_device *bond_dev;
1487
1488 skb = skb_share_check(skb, GFP_ATOMIC);
1489 if (unlikely(!skb))
1490 return NULL;
Jiri Pirkof1c17752011-03-12 03:14:35 +00001491
1492 slave = bond_slave_get_rcu(skb->dev);
1493 bond_dev = ACCESS_ONCE(slave->dev->master);
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001494 if (unlikely(!bond_dev))
1495 return skb;
1496
1497 if (bond_dev->priv_flags & IFF_MASTER_ARPMON)
Jiri Pirkof1c17752011-03-12 03:14:35 +00001498 slave->dev->last_rx = jiffies;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001499
Jiri Pirkof1c17752011-03-12 03:14:35 +00001500 if (bond_should_deliver_exact_match(skb, slave->dev, bond_dev)) {
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001501 skb->deliver_no_wcard = 1;
1502 return skb;
1503 }
1504
1505 skb->dev = bond_dev;
1506
1507 if (bond_dev->priv_flags & IFF_MASTER_ALB &&
1508 bond_dev->priv_flags & IFF_BRIDGE_PORT &&
1509 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);
1514 return NULL;
1515 }
1516 memcpy(eth_hdr(skb)->h_dest, bond_dev->dev_addr, ETH_ALEN);
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001517 }
1518
1519 return skb;
1520}
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522/* enslave device <slave> to bond device <master> */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001523int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Wang Chen454d7c92008-11-12 23:37:49 -08001525 struct bonding *bond = netdev_priv(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001526 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 struct slave *new_slave = NULL;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001528 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 struct sockaddr addr;
1530 int link_reporting;
1531 int old_features = bond_dev->features;
1532 int res = 0;
1533
nsxfreddy@gmail.com552709d2005-09-21 14:18:04 -05001534 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001535 slave_ops->ndo_do_ioctl == NULL) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001536 pr_warning("%s: Warning: no link monitoring support for %s\n",
1537 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539
1540 /* bond must be initialized by bond_open() before enslaving */
1541 if (!(bond_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001542 pr_warning("%s: master_dev is not up in bond_enslave\n",
1543 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545
1546 /* already enslaved */
1547 if (slave_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001548 pr_debug("Error, Device was already enslaved\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return -EBUSY;
1550 }
1551
1552 /* vlan challenged mutual exclusion */
1553 /* no need to lock since we're protected by rtnl_lock */
1554 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001555 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Jay Vosburghf35188f2010-07-21 12:14:47 +00001556 if (bond->vlgrp) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001557 pr_err("%s: Error: cannot enslave VLAN challenged slave %s on VLAN enabled bond %s\n",
1558 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return -EPERM;
1560 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001561 pr_warning("%s: Warning: enslaved VLAN challenged slave %s. Adding VLANs will be blocked as long as %s is part of bond %s\n",
1562 bond_dev->name, slave_dev->name,
1563 slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1565 }
1566 } else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001567 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (bond->slave_cnt == 0) {
1569 /* First slave, and it is not VLAN challenged,
1570 * so remove the block of adding VLANs over the bond.
1571 */
1572 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1573 }
1574 }
1575
Jay Vosburgh217df672005-09-26 16:11:50 -07001576 /*
1577 * Old ifenslave binaries are no longer supported. These can
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001578 * be identified with moderate accuracy by the state of the slave:
Jay Vosburgh217df672005-09-26 16:11:50 -07001579 * the current ifenslave will set the interface down prior to
1580 * enslaving it; the old ifenslave will not.
1581 */
1582 if ((slave_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001583 pr_err("%s is up. This may be due to an out of date ifenslave.\n",
Jay Vosburgh217df672005-09-26 16:11:50 -07001584 slave_dev->name);
1585 res = -EPERM;
1586 goto err_undo_flags;
1587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Moni Shoua872254d2007-10-09 19:43:38 -07001589 /* set bonding device ether type by slave - bonding netdevices are
1590 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1591 * there is a need to override some of the type dependent attribs/funcs.
1592 *
1593 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1594 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1595 */
1596 if (bond->slave_cnt == 0) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001597 if (bond_dev->type != slave_dev->type) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001598 pr_debug("%s: change device type from %d to %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001599 bond_dev->name,
1600 bond_dev->type, slave_dev->type);
Moni Shoua75c78502009-09-15 02:37:40 -07001601
Jiri Pirko3ca5b402010-03-10 10:29:35 +00001602 res = netdev_bonding_change(bond_dev,
1603 NETDEV_PRE_TYPE_CHANGE);
1604 res = notifier_to_errno(res);
1605 if (res) {
1606 pr_err("%s: refused to change device type\n",
1607 bond_dev->name);
1608 res = -EBUSY;
1609 goto err_undo_flags;
1610 }
Moni Shoua75c78502009-09-15 02:37:40 -07001611
Jiri Pirko32a806c2010-03-19 04:00:23 +00001612 /* Flush unicast and multicast addresses */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001613 dev_uc_flush(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001614 dev_mc_flush(bond_dev);
Jiri Pirko32a806c2010-03-19 04:00:23 +00001615
Moni Shouae36b9d12009-07-15 04:56:31 +00001616 if (slave_dev->type != ARPHRD_ETHER)
1617 bond_setup_by_slave(bond_dev, slave_dev);
1618 else
1619 ether_setup(bond_dev);
Moni Shoua75c78502009-09-15 02:37:40 -07001620
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001621 netdev_bonding_change(bond_dev,
1622 NETDEV_POST_TYPE_CHANGE);
Moni Shouae36b9d12009-07-15 04:56:31 +00001623 }
Moni Shoua872254d2007-10-09 19:43:38 -07001624 } else if (bond_dev->type != slave_dev->type) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001625 pr_err("%s ether type (%d) is different from other slaves (%d), can not enslave it.\n",
1626 slave_dev->name,
1627 slave_dev->type, bond_dev->type);
1628 res = -EINVAL;
1629 goto err_undo_flags;
Moni Shoua872254d2007-10-09 19:43:38 -07001630 }
1631
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001632 if (slave_ops->ndo_set_mac_address == NULL) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001633 if (bond->slave_cnt == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001634 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.",
1635 bond_dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001636 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1637 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001638 pr_err("%s: Error: The slave device specified does not support setting the MAC address, but fail_over_mac is not set to active.\n",
1639 bond_dev->name);
Moni Shoua2ab82852007-10-09 19:43:39 -07001640 res = -EOPNOTSUPP;
1641 goto err_undo_flags;
1642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001645 /* If this is the first slave, then we need to set the master's hardware
1646 * address to be the same as the slave's. */
David Strandd13a2cb2010-12-01 11:43:08 -08001647 if (is_zero_ether_addr(bond->dev->dev_addr))
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001648 memcpy(bond->dev->dev_addr, slave_dev->dev_addr,
1649 slave_dev->addr_len);
1650
1651
Joe Jin243cb4e2007-02-06 14:16:40 -08001652 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 if (!new_slave) {
1654 res = -ENOMEM;
1655 goto err_undo_flags;
1656 }
1657
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001658 /*
1659 * Set the new_slave's queue_id to be zero. Queue ID mapping
1660 * is set via sysfs or module option if desired.
1661 */
1662 new_slave->queue_id = 0;
1663
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001664 /* Save slave's original mtu and then set it to match the bond */
1665 new_slave->original_mtu = slave_dev->mtu;
1666 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1667 if (res) {
1668 pr_debug("Error %d calling dev_set_mtu\n", res);
1669 goto err_free;
1670 }
1671
Jay Vosburgh217df672005-09-26 16:11:50 -07001672 /*
1673 * Save slave's original ("permanent") mac address for modes
1674 * that need it, and for restoring it upon release, and then
1675 * set it to the master's address
1676 */
1677 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Jay Vosburghdd957c52007-10-09 19:57:24 -07001679 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001680 /*
1681 * Set slave to master's mac address. The application already
1682 * set the master's mac address to that of the first slave
1683 */
1684 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1685 addr.sa_family = slave_dev->type;
1686 res = dev_set_mac_address(slave_dev, &addr);
1687 if (res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001688 pr_debug("Error %d calling set_mac_address\n", res);
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001689 goto err_restore_mtu;
Moni Shoua2ab82852007-10-09 19:43:39 -07001690 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Jiri Pirko1765a572011-02-12 06:48:36 +00001693 res = netdev_set_bond_master(slave_dev, bond_dev);
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001694 if (res) {
Jiri Pirko1765a572011-02-12 06:48:36 +00001695 pr_debug("Error %d calling netdev_set_bond_master\n", res);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001696 goto err_restore_mac;
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001697 }
Jiri Pirkof1c17752011-03-12 03:14:35 +00001698 res = netdev_rx_handler_register(slave_dev, bond_handle_frame,
1699 new_slave);
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001700 if (res) {
1701 pr_debug("Error %d calling netdev_rx_handler_register\n", res);
1702 goto err_unset_master;
1703 }
1704
Jay Vosburgh217df672005-09-26 16:11:50 -07001705 /* open the slave since the application closed it */
1706 res = dev_open(slave_dev);
1707 if (res) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001708 pr_debug("Opening slave %s failed\n", slave_dev->name);
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001709 goto err_unreg_rxhandler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 new_slave->dev = slave_dev;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001713 slave_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Holger Eitzenberger58402052008-12-09 23:07:13 -08001715 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 /* bond_alb_init_slave() must be called before all other stages since
1717 * it might fail and we do not want to have to undo everything
1718 */
1719 res = bond_alb_init_slave(bond, new_slave);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001720 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001721 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723
1724 /* If the mode USES_PRIMARY, then the new slave gets the
1725 * master's promisc (and mc) settings only if it becomes the
1726 * curr_active_slave, and that is taken care of later when calling
1727 * bond_change_active()
1728 */
1729 if (!USES_PRIMARY(bond->params.mode)) {
1730 /* set promiscuity level to new slave */
1731 if (bond_dev->flags & IFF_PROMISC) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001732 res = dev_set_promiscuity(slave_dev, 1);
1733 if (res)
1734 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 }
1736
1737 /* set allmulti level to new slave */
1738 if (bond_dev->flags & IFF_ALLMULTI) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001739 res = dev_set_allmulti(slave_dev, 1);
1740 if (res)
1741 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
1743
David S. Millerb9e40852008-07-15 00:15:08 -07001744 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 /* upload master's mc_list to new slave */
Jiri Pirko22bedad32010-04-01 21:22:57 +00001746 netdev_for_each_mc_addr(ha, bond_dev)
1747 dev_mc_add(slave_dev, ha->addr);
David S. Millerb9e40852008-07-15 00:15:08 -07001748 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
1750
1751 if (bond->params.mode == BOND_MODE_8023AD) {
1752 /* add lacpdu mc addr to mc list */
1753 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1754
Jiri Pirko22bedad32010-04-01 21:22:57 +00001755 dev_mc_add(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
1757
1758 bond_add_vlans_on_slave(bond, slave_dev);
1759
1760 write_lock_bh(&bond->lock);
1761
1762 bond_attach_slave(bond, new_slave);
1763
1764 new_slave->delay = 0;
1765 new_slave->link_failure_count = 0;
1766
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001767 bond_compute_features(bond);
1768
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001769 write_unlock_bh(&bond->lock);
1770
1771 read_lock(&bond->lock);
1772
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001773 new_slave->last_arp_rx = jiffies;
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 if (bond->params.miimon && !bond->params.use_carrier) {
1776 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1777
1778 if ((link_reporting == -1) && !bond->params.arp_interval) {
1779 /*
1780 * miimon is set but a bonded network driver
1781 * does not support ETHTOOL/MII and
1782 * arp_interval is not set. Note: if
1783 * use_carrier is enabled, we will never go
1784 * here (because netif_carrier is always
1785 * supported); thus, we don't need to change
1786 * the messages for netif_carrier.
1787 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001788 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 -08001789 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 } else if (link_reporting == -1) {
1791 /* unable get link status using mii/ethtool */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001792 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",
1793 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
1795 }
1796
1797 /* check for initial state */
1798 if (!bond->params.miimon ||
1799 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1800 if (bond->params.updelay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001801 pr_debug("Initial state of slave_dev is BOND_LINK_BACK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 new_slave->link = BOND_LINK_BACK;
1803 new_slave->delay = bond->params.updelay;
1804 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001805 pr_debug("Initial state of slave_dev is BOND_LINK_UP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 new_slave->link = BOND_LINK_UP;
1807 }
1808 new_slave->jiffies = jiffies;
1809 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001810 pr_debug("Initial state of slave_dev is BOND_LINK_DOWN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 new_slave->link = BOND_LINK_DOWN;
1812 }
1813
1814 if (bond_update_speed_duplex(new_slave) &&
1815 (new_slave->link != BOND_LINK_DOWN)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001816 pr_warning("%s: Warning: failed to get speed and duplex from %s, assumed to be 100Mb/sec and Full.\n",
1817 bond_dev->name, new_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 if (bond->params.mode == BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001820 pr_warning("%s: Warning: Operation of 802.3ad mode requires ETHTOOL support in base driver for proper aggregator selection.\n",
1821 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
1823 }
1824
1825 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1826 /* if there is a primary slave, remember it */
Jiri Pirkoa5499522009-09-25 03:28:09 +00001827 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 bond->primary_slave = new_slave;
Jiri Pirkoa5499522009-09-25 03:28:09 +00001829 bond->force_primary = true;
1830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
1832
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001833 write_lock_bh(&bond->curr_slave_lock);
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 switch (bond->params.mode) {
1836 case BOND_MODE_ACTIVEBACKUP:
Jay Vosburgh8a8e4472006-09-22 21:56:15 -07001837 bond_set_slave_inactive_flags(new_slave);
1838 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 break;
1840 case BOND_MODE_8023AD:
1841 /* in 802.3ad mode, the internal mechanism
1842 * will activate the slaves in the selected
1843 * aggregator
1844 */
1845 bond_set_slave_inactive_flags(new_slave);
1846 /* if this is the first slave */
1847 if (bond->slave_cnt == 1) {
1848 SLAVE_AD_INFO(new_slave).id = 1;
1849 /* Initialize AD with the number of times that the AD timer is called in 1 second
1850 * can be called only after the mac address of the bond is set
1851 */
1852 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1853 bond->params.lacp_fast);
1854 } else {
1855 SLAVE_AD_INFO(new_slave).id =
1856 SLAVE_AD_INFO(new_slave->prev).id + 1;
1857 }
1858
1859 bond_3ad_bind_slave(new_slave);
1860 break;
1861 case BOND_MODE_TLB:
1862 case BOND_MODE_ALB:
1863 new_slave->state = BOND_STATE_ACTIVE;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001864 bond_set_slave_inactive_flags(new_slave);
Jiri Pirko5a29f782009-03-25 17:23:38 -07001865 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 break;
1867 default:
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001868 pr_debug("This slave is always active in trunk mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 /* always active in trunk mode */
1871 new_slave->state = BOND_STATE_ACTIVE;
1872
1873 /* In trunking mode there is little meaning to curr_active_slave
1874 * anyway (it holds no special properties of the bond device),
1875 * so we can change it without calling change_active_interface()
1876 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001877 if (!bond->curr_active_slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 bond->curr_active_slave = new_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 break;
1881 } /* switch(bond_mode) */
1882
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001883 write_unlock_bh(&bond->curr_slave_lock);
1884
Jay Vosburghff59c452006-03-27 13:27:43 -08001885 bond_set_carrier(bond);
1886
WANG Congf6dc31a2010-05-06 00:48:51 -07001887#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001888 slave_dev->npinfo = bond_netpoll_info(bond);
1889 if (slave_dev->npinfo) {
1890 if (slave_enable_netpoll(new_slave)) {
1891 read_unlock(&bond->lock);
1892 pr_info("Error, %s: master_dev is using netpoll, "
1893 "but new slave device does not support netpoll.\n",
1894 bond_dev->name);
1895 res = -EBUSY;
1896 goto err_close;
1897 }
WANG Congf6dc31a2010-05-06 00:48:51 -07001898 }
1899#endif
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001900
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001901 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001903 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1904 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001905 goto err_close;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001906
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001907 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
1908 bond_dev->name, slave_dev->name,
1909 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1910 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 /* enslave is successful */
1913 return 0;
1914
1915/* Undo stages on error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916err_close:
1917 dev_close(slave_dev);
1918
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001919err_unreg_rxhandler:
1920 netdev_rx_handler_unregister(slave_dev);
Jiri Pirkof1c17752011-03-12 03:14:35 +00001921 synchronize_net();
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001922
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001923err_unset_master:
Jiri Pirko1765a572011-02-12 06:48:36 +00001924 netdev_set_bond_master(slave_dev, NULL);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926err_restore_mac:
Jay Vosburghdd957c52007-10-09 19:57:24 -07001927 if (!bond->params.fail_over_mac) {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001928 /* XXX TODO - fom follow mode needs to change master's
1929 * MAC if this slave's MAC is in use by the bond, or at
1930 * least print a warning.
1931 */
Moni Shoua2ab82852007-10-09 19:43:39 -07001932 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1933 addr.sa_family = slave_dev->type;
1934 dev_set_mac_address(slave_dev, &addr);
1935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001937err_restore_mtu:
1938 dev_set_mtu(slave_dev, new_slave->original_mtu);
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940err_free:
1941 kfree(new_slave);
1942
1943err_undo_flags:
1944 bond_dev->features = old_features;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return res;
1947}
1948
1949/*
1950 * Try to release the slave device <slave> from the bond device <master>
1951 * It is legal to access curr_active_slave without a lock because all the function
1952 * is write-locked.
1953 *
1954 * The rules for slave state should be:
1955 * for Active/Backup:
1956 * Active stays on all backups go down
1957 * for Bonded connections:
1958 * The first up interface should be left on and all others downed.
1959 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001960int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
Wang Chen454d7c92008-11-12 23:37:49 -08001962 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 struct slave *slave, *oldcurrent;
1964 struct sockaddr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 /* slave is not a slave or master is not master of this slave */
1967 if (!(slave_dev->flags & IFF_SLAVE) ||
1968 (slave_dev->master != bond_dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001969 pr_err("%s: Error: cannot release %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 bond_dev->name, slave_dev->name);
1971 return -EINVAL;
1972 }
1973
Neil Hormane843fa52010-10-13 16:01:50 +00001974 block_netpoll_tx();
WANG Congf6dc31a2010-05-06 00:48:51 -07001975 netdev_bonding_change(bond_dev, NETDEV_BONDING_DESLAVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 write_lock_bh(&bond->lock);
1977
1978 slave = bond_get_slave_by_dev(bond, slave_dev);
1979 if (!slave) {
1980 /* not a slave of this bond */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001981 pr_info("%s: %s not enslaved\n",
1982 bond_dev->name, slave_dev->name);
Jay Vosburghf5e2a7b2006-02-07 21:17:22 -08001983 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001984 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 return -EINVAL;
1986 }
1987
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001988 if (!bond->params.fail_over_mac) {
Joe Perches8e95a202009-12-03 07:58:21 +00001989 if (!compare_ether_addr(bond_dev->dev_addr, slave->perm_hwaddr) &&
1990 bond->slave_cnt > 1)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001991 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",
1992 bond_dev->name, slave_dev->name,
1993 slave->perm_hwaddr,
1994 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
1996
1997 /* Inform AD package of unbinding of slave. */
1998 if (bond->params.mode == BOND_MODE_8023AD) {
1999 /* must be called before the slave is
2000 * detached from the list
2001 */
2002 bond_3ad_unbind_slave(slave);
2003 }
2004
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002005 pr_info("%s: releasing %s interface %s\n",
2006 bond_dev->name,
2007 (slave->state == BOND_STATE_ACTIVE) ? "active" : "backup",
2008 slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 oldcurrent = bond->curr_active_slave;
2011
2012 bond->current_arp_slave = NULL;
2013
2014 /* release the slave from its bond */
2015 bond_detach_slave(bond, slave);
2016
Arthur Kepner8531c5f2005-08-23 01:34:53 -04002017 bond_compute_features(bond);
2018
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002019 if (bond->primary_slave == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 bond->primary_slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002022 if (oldcurrent == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 bond_change_active_slave(bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Holger Eitzenberger58402052008-12-09 23:07:13 -08002025 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 /* Must be called only after the slave has been
2027 * detached from the list and the curr_active_slave
2028 * has been cleared (if our_slave == old_current),
2029 * but before a new active slave is selected.
2030 */
Jay Vosburgh25433312008-01-17 16:24:59 -08002031 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 bond_alb_deinit_slave(bond, slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08002033 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
2035
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002036 if (oldcurrent == slave) {
2037 /*
2038 * Note that we hold RTNL over this sequence, so there
2039 * is no concern that another slave add/remove event
2040 * will interfere.
2041 */
2042 write_unlock_bh(&bond->lock);
2043 read_lock(&bond->lock);
2044 write_lock_bh(&bond->curr_slave_lock);
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 bond_select_active_slave(bond);
2047
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002048 write_unlock_bh(&bond->curr_slave_lock);
2049 read_unlock(&bond->lock);
2050 write_lock_bh(&bond->lock);
2051 }
2052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (bond->slave_cnt == 0) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002054 bond_set_carrier(bond);
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 /* if the last slave was removed, zero the mac address
2057 * of the master so it will be set by the application
2058 * to the mac address of the first slave
2059 */
2060 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2061
Jay Vosburghf35188f2010-07-21 12:14:47 +00002062 if (!bond->vlgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2064 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002065 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2066 bond_dev->name, bond_dev->name);
2067 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2068 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
2070 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2071 !bond_has_challenged_slaves(bond)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002072 pr_info("%s: last VLAN challenged slave %s left bond %s. VLAN blocking is removed\n",
2073 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
2075 }
2076
2077 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002078 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002080 /* must do this from outside any spinlocks */
2081 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 bond_del_vlans_from_slave(bond, slave_dev);
2084
2085 /* If the mode USES_PRIMARY, then we should only remove its
2086 * promisc and mc settings if it was the curr_active_slave, but that was
2087 * already taken care of above when we detached the slave
2088 */
2089 if (!USES_PRIMARY(bond->params.mode)) {
2090 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002091 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002095 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002099 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002101 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
2103
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00002104 netdev_rx_handler_unregister(slave_dev);
Jiri Pirkof1c17752011-03-12 03:14:35 +00002105 synchronize_net();
Jiri Pirko1765a572011-02-12 06:48:36 +00002106 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002108 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07002109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 /* close slave before restoring its mac address */
2111 dev_close(slave_dev);
2112
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07002113 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002114 /* restore original ("permanent") mac address */
2115 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2116 addr.sa_family = slave_dev->type;
2117 dev_set_mac_address(slave_dev, &addr);
2118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00002120 dev_set_mtu(slave_dev, slave->original_mtu);
2121
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002122 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002123 IFF_SLAVE_INACTIVE | IFF_BONDING |
2124 IFF_SLAVE_NEEDARP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 kfree(slave);
2127
2128 return 0; /* deletion OK */
2129}
2130
2131/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002132* First release a slave and than destroy the bond if no more slaves are left.
Moni Shouad90a1622007-10-09 19:43:43 -07002133* Must be under rtnl_lock when this function is called.
2134*/
stephen hemminger26d8ee72010-10-15 05:09:34 +00002135static int bond_release_and_destroy(struct net_device *bond_dev,
2136 struct net_device *slave_dev)
Moni Shouad90a1622007-10-09 19:43:43 -07002137{
Wang Chen454d7c92008-11-12 23:37:49 -08002138 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002139 int ret;
2140
2141 ret = bond_release(bond_dev, slave_dev);
2142 if ((ret == 0) && (bond->slave_cnt == 0)) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002143 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002144 pr_info("%s: destroying bond %s.\n",
2145 bond_dev->name, bond_dev->name);
Stephen Hemminger9e716262009-06-12 19:02:47 +00002146 unregister_netdevice(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002147 }
2148 return ret;
2149}
2150
2151/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 * This function releases all slaves.
2153 */
2154static int bond_release_all(struct net_device *bond_dev)
2155{
Wang Chen454d7c92008-11-12 23:37:49 -08002156 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 struct slave *slave;
2158 struct net_device *slave_dev;
2159 struct sockaddr addr;
2160
2161 write_lock_bh(&bond->lock);
2162
Jay Vosburghff59c452006-03-27 13:27:43 -08002163 netif_carrier_off(bond_dev);
2164
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002165 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 bond->current_arp_slave = NULL;
2169 bond->primary_slave = NULL;
2170 bond_change_active_slave(bond, NULL);
2171
2172 while ((slave = bond->first_slave) != NULL) {
2173 /* Inform AD package of unbinding of slave
2174 * before slave is detached from the list.
2175 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002176 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 bond_3ad_unbind_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 slave_dev = slave->dev;
2180 bond_detach_slave(bond, slave);
2181
Jay Vosburgh25433312008-01-17 16:24:59 -08002182 /* now that the slave is detached, unlock and perform
2183 * all the undo steps that should not be called from
2184 * within a lock.
2185 */
2186 write_unlock_bh(&bond->lock);
2187
Holger Eitzenberger58402052008-12-09 23:07:13 -08002188 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 /* must be called only after the slave
2190 * has been detached from the list
2191 */
2192 bond_alb_deinit_slave(bond, slave);
2193 }
2194
Arthur Kepner8531c5f2005-08-23 01:34:53 -04002195 bond_compute_features(bond);
2196
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002197 bond_destroy_slave_symlinks(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 bond_del_vlans_from_slave(bond, slave_dev);
2199
2200 /* If the mode USES_PRIMARY, then we should only remove its
2201 * promisc and mc settings if it was the curr_active_slave, but that was
2202 * already taken care of above when we detached the slave
2203 */
2204 if (!USES_PRIMARY(bond->params.mode)) {
2205 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002206 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002210 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002214 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002216 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
2218
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00002219 netdev_rx_handler_unregister(slave_dev);
Jiri Pirkof1c17752011-03-12 03:14:35 +00002220 synchronize_net();
Jiri Pirko1765a572011-02-12 06:48:36 +00002221 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002223 slave_disable_netpoll(slave);
2224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 /* close slave before restoring its mac address */
2226 dev_close(slave_dev);
2227
Jay Vosburghdd957c52007-10-09 19:57:24 -07002228 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002229 /* restore original ("permanent") mac address*/
2230 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2231 addr.sa_family = slave_dev->type;
2232 dev_set_mac_address(slave_dev, &addr);
2233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Jay Vosburgh8f903c72006-02-21 16:36:44 -08002235 slave_dev->priv_flags &= ~(IFF_MASTER_8023AD | IFF_MASTER_ALB |
2236 IFF_SLAVE_INACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 kfree(slave);
2239
2240 /* re-acquire the lock before getting the next slave */
2241 write_lock_bh(&bond->lock);
2242 }
2243
2244 /* zero the mac address of the master so it will be
2245 * set by the application to the mac address of the
2246 * first slave
2247 */
2248 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2249
Jay Vosburghf35188f2010-07-21 12:14:47 +00002250 if (!bond->vlgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
Jay Vosburghf35188f2010-07-21 12:14:47 +00002252 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002253 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2254 bond_dev->name, bond_dev->name);
2255 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2256 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
2258
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002259 pr_info("%s: released all slaves\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261out:
2262 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 return 0;
2264}
2265
2266/*
2267 * This function changes the active slave to slave <slave_dev>.
2268 * It returns -EINVAL in the following cases.
2269 * - <slave_dev> is not found in the list.
2270 * - There is not active slave now.
2271 * - <slave_dev> is already active.
2272 * - The link state of <slave_dev> is not BOND_LINK_UP.
2273 * - <slave_dev> is not running.
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002274 * In these cases, this function does nothing.
2275 * In the other cases, current_slave pointer is changed and 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 */
2277static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2278{
Wang Chen454d7c92008-11-12 23:37:49 -08002279 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 struct slave *old_active = NULL;
2281 struct slave *new_active = NULL;
2282 int res = 0;
2283
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002284 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287 /* Verify that master_dev is indeed the master of slave_dev */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002288 if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002291 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002293 read_lock(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 old_active = bond->curr_active_slave;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002295 read_unlock(&bond->curr_slave_lock);
2296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 new_active = bond_get_slave_by_dev(bond, slave_dev);
2298
2299 /*
2300 * Changing to the current active: do nothing; return success.
2301 */
2302 if (new_active && (new_active == old_active)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002303 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 return 0;
2305 }
2306
2307 if ((new_active) &&
2308 (old_active) &&
2309 (new_active->link == BOND_LINK_UP) &&
2310 IS_UP(new_active->dev)) {
Neil Hormane843fa52010-10-13 16:01:50 +00002311 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002312 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 bond_change_active_slave(bond, new_active);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002314 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002315 unblock_netpoll_tx();
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002316 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002319 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 return res;
2322}
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2325{
Wang Chen454d7c92008-11-12 23:37:49 -08002326 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 info->bond_mode = bond->params.mode;
2329 info->miimon = bond->params.miimon;
2330
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002331 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 info->num_slaves = bond->slave_cnt;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002333 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 return 0;
2336}
2337
2338static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2339{
Wang Chen454d7c92008-11-12 23:37:49 -08002340 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 struct slave *slave;
Eric Dumazet689c96c2009-04-23 03:39:04 +00002342 int i, res = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002344 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
2346 bond_for_each_slave(bond, slave, i) {
2347 if (i == (int)info->slave_id) {
Eric Dumazet689c96c2009-04-23 03:39:04 +00002348 res = 0;
2349 strcpy(info->slave_name, slave->dev->name);
2350 info->link = slave->link;
2351 info->state = slave->state;
2352 info->link_failure_count = slave->link_failure_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 break;
2354 }
2355 }
2356
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002357 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Eric Dumazet689c96c2009-04-23 03:39:04 +00002359 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360}
2361
2362/*-------------------------------- Monitoring -------------------------------*/
2363
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002364
2365static int bond_miimon_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002367 struct slave *slave;
2368 int i, link_state, commit = 0;
Jiri Pirko41f89102009-04-24 03:57:29 +00002369 bool ignore_updelay;
2370
2371 ignore_updelay = !bond->curr_active_slave ? true : false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373 bond_for_each_slave(bond, slave, i) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002374 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002376 link_state = bond_check_dev_link(bond, slave->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
2378 switch (slave->link) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002379 case BOND_LINK_UP:
2380 if (link_state)
2381 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002383 slave->link = BOND_LINK_FAIL;
2384 slave->delay = bond->params.downdelay;
2385 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002386 pr_info("%s: link status down for %sinterface %s, disabling it in %d ms.\n",
2387 bond->dev->name,
2388 (bond->params.mode ==
2389 BOND_MODE_ACTIVEBACKUP) ?
2390 ((slave->state == BOND_STATE_ACTIVE) ?
2391 "active " : "backup ") : "",
2392 slave->dev->name,
2393 bond->params.downdelay * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002395 /*FALLTHRU*/
2396 case BOND_LINK_FAIL:
2397 if (link_state) {
2398 /*
2399 * recovered before downdelay expired
2400 */
2401 slave->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 slave->jiffies = jiffies;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002403 pr_info("%s: link status up again after %d ms for interface %s.\n",
2404 bond->dev->name,
2405 (bond->params.downdelay - slave->delay) *
2406 bond->params.miimon,
2407 slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002408 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002410
2411 if (slave->delay <= 0) {
2412 slave->new_link = BOND_LINK_DOWN;
2413 commit++;
2414 continue;
2415 }
2416
2417 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002420 case BOND_LINK_DOWN:
2421 if (!link_state)
2422 continue;
2423
2424 slave->link = BOND_LINK_BACK;
2425 slave->delay = bond->params.updelay;
2426
2427 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002428 pr_info("%s: link status up for interface %s, enabling it in %d ms.\n",
2429 bond->dev->name, slave->dev->name,
2430 ignore_updelay ? 0 :
2431 bond->params.updelay *
2432 bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002434 /*FALLTHRU*/
2435 case BOND_LINK_BACK:
2436 if (!link_state) {
2437 slave->link = BOND_LINK_DOWN;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002438 pr_info("%s: link status down again after %d ms for interface %s.\n",
2439 bond->dev->name,
2440 (bond->params.updelay - slave->delay) *
2441 bond->params.miimon,
2442 slave->dev->name);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002443
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002444 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002446
Jiri Pirko41f89102009-04-24 03:57:29 +00002447 if (ignore_updelay)
2448 slave->delay = 0;
2449
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002450 if (slave->delay <= 0) {
2451 slave->new_link = BOND_LINK_UP;
2452 commit++;
Jiri Pirko41f89102009-04-24 03:57:29 +00002453 ignore_updelay = false;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002454 continue;
2455 }
2456
2457 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 break;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002459 }
2460 }
2461
2462 return commit;
2463}
2464
2465static void bond_miimon_commit(struct bonding *bond)
2466{
2467 struct slave *slave;
2468 int i;
2469
2470 bond_for_each_slave(bond, slave, i) {
2471 switch (slave->new_link) {
2472 case BOND_LINK_NOCHANGE:
2473 continue;
2474
2475 case BOND_LINK_UP:
2476 slave->link = BOND_LINK_UP;
2477 slave->jiffies = jiffies;
2478
2479 if (bond->params.mode == BOND_MODE_8023AD) {
2480 /* prevent it from being the active one */
2481 slave->state = BOND_STATE_BACKUP;
2482 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2483 /* make it immediately active */
2484 slave->state = BOND_STATE_ACTIVE;
2485 } else if (slave != bond->primary_slave) {
2486 /* prevent it from being the active one */
2487 slave->state = BOND_STATE_BACKUP;
2488 }
2489
Krzysztof Piotr Oledzki546add72010-10-06 14:28:22 -07002490 bond_update_speed_duplex(slave);
2491
Krzysztof Piotr Oledzki700c2a72010-10-06 14:25:06 -07002492 pr_info("%s: link status definitely up for interface %s, %d Mbps %s duplex.\n",
2493 bond->dev->name, slave->dev->name,
2494 slave->speed, slave->duplex ? "full" : "half");
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002495
2496 /* notify ad that the link status has changed */
2497 if (bond->params.mode == BOND_MODE_8023AD)
2498 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2499
Holger Eitzenberger58402052008-12-09 23:07:13 -08002500 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002501 bond_alb_handle_link_change(bond, slave,
2502 BOND_LINK_UP);
2503
2504 if (!bond->curr_active_slave ||
2505 (slave == bond->primary_slave))
2506 goto do_failover;
2507
2508 continue;
2509
2510 case BOND_LINK_DOWN:
Jay Vosburghfba4acd2008-10-30 17:41:14 -07002511 if (slave->link_failure_count < UINT_MAX)
2512 slave->link_failure_count++;
2513
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002514 slave->link = BOND_LINK_DOWN;
2515
2516 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2517 bond->params.mode == BOND_MODE_8023AD)
2518 bond_set_slave_inactive_flags(slave);
2519
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002520 pr_info("%s: link status definitely down for interface %s, disabling it\n",
2521 bond->dev->name, slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002522
2523 if (bond->params.mode == BOND_MODE_8023AD)
2524 bond_3ad_handle_link_change(slave,
2525 BOND_LINK_DOWN);
2526
Jiri Pirkoae63e802009-05-27 05:42:36 +00002527 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002528 bond_alb_handle_link_change(bond, slave,
2529 BOND_LINK_DOWN);
2530
2531 if (slave == bond->curr_active_slave)
2532 goto do_failover;
2533
2534 continue;
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002537 pr_err("%s: invalid new link %d on slave %s\n",
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002538 bond->dev->name, slave->new_link,
2539 slave->dev->name);
2540 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002542 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
2544
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002545do_failover:
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002546 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00002547 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002548 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 bond_select_active_slave(bond);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002550 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002551 unblock_netpoll_tx();
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002552 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002553
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002554 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555}
2556
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002557/*
2558 * bond_mii_monitor
2559 *
2560 * Really a wrapper that splits the mii monitor into two phases: an
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002561 * inspection, then (if inspection indicates something needs to be done)
2562 * an acquisition of appropriate locks followed by a commit phase to
2563 * implement whatever link state changes are indicated.
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002564 */
2565void bond_mii_monitor(struct work_struct *work)
2566{
2567 struct bonding *bond = container_of(work, struct bonding,
2568 mii_work.work);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002569
2570 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002571 if (bond->kill_timers)
2572 goto out;
2573
2574 if (bond->slave_cnt == 0)
2575 goto re_arm;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002576
2577 if (bond->send_grat_arp) {
2578 read_lock(&bond->curr_slave_lock);
2579 bond_send_gratuitous_arp(bond);
2580 read_unlock(&bond->curr_slave_lock);
2581 }
2582
Brian Haley305d5522008-11-04 17:51:14 -08002583 if (bond->send_unsol_na) {
2584 read_lock(&bond->curr_slave_lock);
2585 bond_send_unsolicited_na(bond);
2586 read_unlock(&bond->curr_slave_lock);
2587 }
2588
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002589 if (bond_miimon_inspect(bond)) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002590 read_unlock(&bond->lock);
2591 rtnl_lock();
2592 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002593
2594 bond_miimon_commit(bond);
2595
Jay Vosburgh56556622008-01-17 16:25:03 -08002596 read_unlock(&bond->lock);
2597 rtnl_unlock(); /* might sleep, hold no other locks */
2598 read_lock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002599 }
2600
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002601re_arm:
2602 if (bond->params.miimon)
2603 queue_delayed_work(bond->wq, &bond->mii_work,
2604 msecs_to_jiffies(bond->params.miimon));
2605out:
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002606 read_unlock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002607}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002608
Al Virod3bb52b2007-08-22 20:06:58 -04002609static __be32 bond_glean_dev_ip(struct net_device *dev)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002610{
2611 struct in_device *idev;
2612 struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07002613 __be32 addr = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002614
2615 if (!dev)
2616 return 0;
2617
2618 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002619 idev = __in_dev_get_rcu(dev);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002620 if (!idev)
2621 goto out;
2622
2623 ifa = idev->ifa_list;
2624 if (!ifa)
2625 goto out;
2626
2627 addr = ifa->ifa_local;
2628out:
2629 rcu_read_unlock();
2630 return addr;
2631}
2632
Al Virod3bb52b2007-08-22 20:06:58 -04002633static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002634{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002635 struct vlan_entry *vlan;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002636
2637 if (ip == bond->master_ip)
2638 return 1;
2639
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002640 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002641 if (ip == vlan->vlan_ip)
2642 return 1;
2643 }
2644
2645 return 0;
2646}
2647
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002648/*
2649 * We go to the (large) trouble of VLAN tagging ARP frames because
2650 * switches in VLAN mode (especially if ports are configured as
2651 * "native" to a VLAN) might not pass non-tagged frames.
2652 */
Al Virod3bb52b2007-08-22 20:06:58 -04002653static 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 -04002654{
2655 struct sk_buff *skb;
2656
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002657 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002658 slave_dev->name, dest_ip, src_ip, vlan_id);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002659
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002660 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2661 NULL, slave_dev->dev_addr, NULL);
2662
2663 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002664 pr_err("ARP packet allocation failed\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002665 return;
2666 }
2667 if (vlan_id) {
2668 skb = vlan_put_tag(skb, vlan_id);
2669 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002670 pr_err("failed to insert VLAN tag\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002671 return;
2672 }
2673 }
2674 arp_xmit(skb);
2675}
2676
2677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2679{
David S. Millerb23dd4f2011-03-02 14:31:35 -08002680 int i, vlan_id;
Al Virod3bb52b2007-08-22 20:06:58 -04002681 __be32 *targets = bond->params.arp_targets;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002682 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002683 struct net_device *vlan_dev;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002684 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Mitch Williams6b780562005-11-09 10:36:19 -08002686 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2687 if (!targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07002688 break;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002689 pr_debug("basa: target %x\n", targets[i]);
Jay Vosburghf35188f2010-07-21 12:14:47 +00002690 if (!bond->vlgrp) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002691 pr_debug("basa: empty vlan: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002692 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2693 bond->master_ip, 0);
2694 continue;
2695 }
2696
2697 /*
2698 * If VLANs are configured, we do a route lookup to
2699 * determine which VLAN interface would be used, so we
2700 * can tag the ARP with the proper VLAN tag.
2701 */
David S. Miller78fbfd82011-03-12 00:00:52 -05002702 rt = ip_route_output(dev_net(bond->dev), targets[i], 0,
2703 RTO_ONLINK, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08002704 if (IS_ERR(rt)) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002705 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002706 pr_warning("%s: no route to arp_ip_target %pI4\n",
David S. Miller78fbfd82011-03-12 00:00:52 -05002707 bond->dev->name, &targets[i]);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002708 }
2709 continue;
2710 }
2711
2712 /*
2713 * This target is not on a VLAN
2714 */
Changli Gaod8d1f302010-06-10 23:31:35 -07002715 if (rt->dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002716 ip_rt_put(rt);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002717 pr_debug("basa: rtdev == bond->dev: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002718 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2719 bond->master_ip, 0);
2720 continue;
2721 }
2722
2723 vlan_id = 0;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002724 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002725 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Changli Gaod8d1f302010-06-10 23:31:35 -07002726 if (vlan_dev == rt->dst.dev) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002727 vlan_id = vlan->vlan_id;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002728 pr_debug("basa: vlan match on %s %d\n",
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002729 vlan_dev->name, vlan_id);
2730 break;
2731 }
2732 }
2733
2734 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002735 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002736 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2737 vlan->vlan_ip, vlan_id);
2738 continue;
2739 }
2740
2741 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002742 pr_warning("%s: no path to arp_ip_target %pI4 via rt.dev %s\n",
David S. Miller78fbfd82011-03-12 00:00:52 -05002743 bond->dev->name, &targets[i],
Changli Gaod8d1f302010-06-10 23:31:35 -07002744 rt->dst.dev ? rt->dst.dev->name : "NULL");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002745 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002746 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002747 }
2748}
2749
2750/*
2751 * Kick out a gratuitous ARP for an IP on the bonding master plus one
2752 * for each VLAN above us.
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002753 *
2754 * Caller must hold curr_slave_lock for read or better
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002755 */
2756static void bond_send_gratuitous_arp(struct bonding *bond)
2757{
2758 struct slave *slave = bond->curr_active_slave;
2759 struct vlan_entry *vlan;
2760 struct net_device *vlan_dev;
2761
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002762 pr_debug("bond_send_grat_arp: bond %s slave %s\n",
2763 bond->dev->name, slave ? slave->dev->name : "NULL");
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002764
2765 if (!slave || !bond->send_grat_arp ||
2766 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002767 return;
2768
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002769 bond->send_grat_arp--;
2770
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002771 if (bond->master_ip) {
2772 bond_arp_send(slave->dev, ARPOP_REPLY, bond->master_ip,
Moni Shoua1053f622007-10-09 19:43:42 -07002773 bond->master_ip, 0);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002774 }
2775
Jay Vosburghf35188f2010-07-21 12:14:47 +00002776 if (!bond->vlgrp)
2777 return;
2778
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002779 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002780 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002781 if (vlan->vlan_ip) {
2782 bond_arp_send(slave->dev, ARPOP_REPLY, vlan->vlan_ip,
2783 vlan->vlan_ip, vlan->vlan_id);
2784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 }
2786}
2787
Al Virod3bb52b2007-08-22 20:06:58 -04002788static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002789{
2790 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002791 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002792
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002793 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002794 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002795 &sip, &tip, i, &targets[i],
2796 bond_has_this_ip(bond, tip));
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002797 if (sip == targets[i]) {
2798 if (bond_has_this_ip(bond, tip))
2799 slave->last_arp_rx = jiffies;
2800 return;
2801 }
2802 }
2803}
2804
2805static int bond_arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
2806{
2807 struct arphdr *arp;
2808 struct slave *slave;
2809 struct bonding *bond;
2810 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002811 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002812
Andy Gospodarek1f3c8802009-12-14 10:48:58 +00002813 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2814 /*
2815 * When using VLANS and bonding, dev and oriv_dev may be
2816 * incorrect if the physical interface supports VLAN
2817 * acceleration. With this change ARP validation now
2818 * works for hosts only reachable on the VLAN interface.
2819 */
2820 dev = vlan_dev_real_dev(dev);
2821 orig_dev = dev_get_by_index_rcu(dev_net(skb->dev),skb->skb_iif);
2822 }
2823
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002824 if (!(dev->priv_flags & IFF_BONDING) || !(dev->flags & IFF_MASTER))
2825 goto out;
2826
Wang Chen454d7c92008-11-12 23:37:49 -08002827 bond = netdev_priv(dev);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002828 read_lock(&bond->lock);
2829
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002830 pr_debug("bond_arp_rcv: bond %s skb->dev %s orig_dev %s\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002831 bond->dev->name, skb->dev ? skb->dev->name : "NULL",
2832 orig_dev ? orig_dev->name : "NULL");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002833
2834 slave = bond_get_slave_by_dev(bond, orig_dev);
2835 if (!slave || !slave_do_arp_validate(bond, slave))
2836 goto out_unlock;
2837
Neil Hormanb3053252011-01-20 09:02:31 +00002838 skb = skb_share_check(skb, GFP_ATOMIC);
2839 if (!skb)
2840 goto out_unlock;
2841
Pavel Emelyanov988b7052008-03-03 12:20:57 -08002842 if (!pskb_may_pull(skb, arp_hdr_len(dev)))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002843 goto out_unlock;
2844
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002845 arp = arp_hdr(skb);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002846 if (arp->ar_hln != dev->addr_len ||
2847 skb->pkt_type == PACKET_OTHERHOST ||
2848 skb->pkt_type == PACKET_LOOPBACK ||
2849 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2850 arp->ar_pro != htons(ETH_P_IP) ||
2851 arp->ar_pln != 4)
2852 goto out_unlock;
2853
2854 arp_ptr = (unsigned char *)(arp + 1);
2855 arp_ptr += dev->addr_len;
2856 memcpy(&sip, arp_ptr, 4);
2857 arp_ptr += 4 + dev->addr_len;
2858 memcpy(&tip, arp_ptr, 4);
2859
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002860 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002861 bond->dev->name, slave->dev->name, slave->state,
2862 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2863 &sip, &tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002864
2865 /*
2866 * Backup slaves won't see the ARP reply, but do come through
2867 * here for each ARP probe (so we swap the sip/tip to validate
2868 * the probe). In a "redundant switch, common router" type of
2869 * configuration, the ARP probe will (hopefully) travel from
2870 * the active, through one switch, the router, then the other
2871 * switch before reaching the backup.
2872 */
2873 if (slave->state == BOND_STATE_ACTIVE)
2874 bond_validate_arp(bond, slave, sip, tip);
2875 else
2876 bond_validate_arp(bond, slave, tip, sip);
2877
2878out_unlock:
2879 read_unlock(&bond->lock);
2880out:
2881 dev_kfree_skb(skb);
2882 return NET_RX_SUCCESS;
2883}
2884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885/*
2886 * this function is called regularly to monitor each slave's link
2887 * ensuring that traffic is being sent and received when arp monitoring
2888 * is used in load-balancing mode. if the adapter has been dormant, then an
2889 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2890 * arp monitoring in active backup mode.
2891 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002892void bond_loadbalance_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002894 struct bonding *bond = container_of(work, struct bonding,
2895 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 struct slave *slave, *oldcurrent;
2897 int do_failover = 0;
2898 int delta_in_ticks;
2899 int i;
2900
2901 read_lock(&bond->lock);
2902
Jay Vosburgh5ce0da82008-05-17 21:10:07 -07002903 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002905 if (bond->kill_timers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002908 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
2911 read_lock(&bond->curr_slave_lock);
2912 oldcurrent = bond->curr_active_slave;
2913 read_unlock(&bond->curr_slave_lock);
2914
2915 /* see if any of the previous devices are up now (i.e. they have
2916 * xmt and rcv traffic). the curr_active_slave does not come into
2917 * the picture unless it is null. also, slave->jiffies is not needed
2918 * here because we send an arp on each slave and give a slave as
2919 * long as it needs to get the tx/rx within the delta.
2920 * TODO: what about up/down delay in arp mode? it wasn't here before
2921 * so it can wait
2922 */
2923 bond_for_each_slave(bond, slave, i) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002924 unsigned long trans_start = dev_trans_start(slave->dev);
2925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002927 if (time_in_range(jiffies,
2928 trans_start - delta_in_ticks,
2929 trans_start + delta_in_ticks) &&
2930 time_in_range(jiffies,
2931 slave->dev->last_rx - delta_in_ticks,
2932 slave->dev->last_rx + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
2934 slave->link = BOND_LINK_UP;
2935 slave->state = BOND_STATE_ACTIVE;
2936
2937 /* primary_slave has no meaning in round-robin
2938 * mode. the window of a slave being up and
2939 * curr_active_slave being null after enslaving
2940 * is closed.
2941 */
2942 if (!oldcurrent) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002943 pr_info("%s: link status definitely up for interface %s, ",
2944 bond->dev->name,
2945 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 do_failover = 1;
2947 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002948 pr_info("%s: interface %s is now up\n",
2949 bond->dev->name,
2950 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 }
2952 }
2953 } else {
2954 /* slave->link == BOND_LINK_UP */
2955
2956 /* not all switches will respond to an arp request
2957 * when the source ip is 0, so don't take the link down
2958 * if we don't know our ip yet
2959 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002960 if (!time_in_range(jiffies,
2961 trans_start - delta_in_ticks,
2962 trans_start + 2 * delta_in_ticks) ||
2963 !time_in_range(jiffies,
2964 slave->dev->last_rx - delta_in_ticks,
2965 slave->dev->last_rx + 2 * delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
2967 slave->link = BOND_LINK_DOWN;
2968 slave->state = BOND_STATE_BACKUP;
2969
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002970 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002973 pr_info("%s: interface %s is now down.\n",
2974 bond->dev->name,
2975 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002977 if (slave == oldcurrent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 do_failover = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 }
2980 }
2981
2982 /* note: if switch is in round-robin mode, all links
2983 * must tx arp to ensure all links rx an arp - otherwise
2984 * links may oscillate or not come up at all; if switch is
2985 * in something like xor mode, there is nothing we can
2986 * do - all replies will be rx'ed on same link causing slaves
2987 * to be unstable during low/no traffic periods
2988 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002989 if (IS_UP(slave->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 bond_arp_send_all(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 }
2992
2993 if (do_failover) {
Neil Hormane843fa52010-10-13 16:01:50 +00002994 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002995 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
2997 bond_select_active_slave(bond);
2998
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002999 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00003000 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 }
3002
3003re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003004 if (bond->params.arp_interval)
3005 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006out:
3007 read_unlock(&bond->lock);
3008}
3009
3010/*
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003011 * Called to inspect slaves for active-backup mode ARP monitor link state
3012 * changes. Sets new_link in slaves to specify what action should take
3013 * place for the slave. Returns 0 if no changes are found, >0 if changes
3014 * to link states must be committed.
3015 *
3016 * Called with bond->lock held for read.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003018static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 struct slave *slave;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003021 int i, commit = 0;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003022 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 bond_for_each_slave(bond, slave, i) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003025 slave->new_link = BOND_LINK_NOCHANGE;
3026
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003028 if (time_in_range(jiffies,
3029 slave_last_rx(bond, slave) - delta_in_ticks,
3030 slave_last_rx(bond, slave) + delta_in_ticks)) {
3031
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003032 slave->new_link = BOND_LINK_UP;
3033 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003036 continue;
3037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003039 /*
3040 * Give slaves 2*delta after being enslaved or made
3041 * active. This avoids bouncing, as the last receive
3042 * times need a full ARP monitor cycle to be updated.
3043 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003044 if (time_in_range(jiffies,
3045 slave->jiffies - delta_in_ticks,
3046 slave->jiffies + 2 * delta_in_ticks))
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003047 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003049 /*
3050 * Backup slave is down if:
3051 * - No current_arp_slave AND
3052 * - more than 3*delta since last receive AND
3053 * - the bond has an IP address
3054 *
3055 * Note: a non-null current_arp_slave indicates
3056 * the curr_active_slave went down and we are
3057 * searching for a new one; under this condition
3058 * we only take the curr_active_slave down - this
3059 * gives each slave a chance to tx/rx traffic
3060 * before being taken out
3061 */
3062 if (slave->state == BOND_STATE_BACKUP &&
3063 !bond->current_arp_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003064 !time_in_range(jiffies,
3065 slave_last_rx(bond, slave) - delta_in_ticks,
3066 slave_last_rx(bond, slave) + 3 * delta_in_ticks)) {
3067
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003068 slave->new_link = BOND_LINK_DOWN;
3069 commit++;
3070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003072 /*
3073 * Active slave is down if:
3074 * - more than 2*delta since transmitting OR
3075 * - (more than 2*delta since receive AND
3076 * the bond has an IP address)
3077 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003078 trans_start = dev_trans_start(slave->dev);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003079 if ((slave->state == BOND_STATE_ACTIVE) &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003080 (!time_in_range(jiffies,
3081 trans_start - delta_in_ticks,
3082 trans_start + 2 * delta_in_ticks) ||
3083 !time_in_range(jiffies,
3084 slave_last_rx(bond, slave) - delta_in_ticks,
3085 slave_last_rx(bond, slave) + 2 * delta_in_ticks))) {
3086
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003087 slave->new_link = BOND_LINK_DOWN;
3088 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 }
3090 }
3091
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003092 return commit;
3093}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003095/*
3096 * Called to commit link state changes noted by inspection step of
3097 * active-backup mode ARP monitor.
3098 *
3099 * Called with RTNL and bond->lock for read.
3100 */
3101static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
3102{
3103 struct slave *slave;
3104 int i;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003105 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003107 bond_for_each_slave(bond, slave, i) {
3108 switch (slave->new_link) {
3109 case BOND_LINK_NOCHANGE:
3110 continue;
3111
3112 case BOND_LINK_UP:
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003113 trans_start = dev_trans_start(slave->dev);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003114 if ((!bond->curr_active_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003115 time_in_range(jiffies,
3116 trans_start - delta_in_ticks,
3117 trans_start + delta_in_ticks)) ||
Jiri Pirkob9f60252009-08-31 11:09:38 +00003118 bond->curr_active_slave != slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003119 slave->link = BOND_LINK_UP;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003120 bond->current_arp_slave = NULL;
3121
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003122 pr_info("%s: link status definitely up for interface %s.\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003123 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003124
Jiri Pirkob9f60252009-08-31 11:09:38 +00003125 if (!bond->curr_active_slave ||
3126 (slave == bond->primary_slave))
3127 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003128
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003129 }
3130
Jiri Pirkob9f60252009-08-31 11:09:38 +00003131 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003132
3133 case BOND_LINK_DOWN:
3134 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003137 slave->link = BOND_LINK_DOWN;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003138 bond_set_slave_inactive_flags(slave);
3139
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003140 pr_info("%s: link status definitely down for interface %s, disabling it\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003141 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003142
3143 if (slave == bond->curr_active_slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003144 bond->current_arp_slave = NULL;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003145 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003146 }
Jiri Pirkob9f60252009-08-31 11:09:38 +00003147
3148 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003149
3150 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003151 pr_err("%s: impossible: new_link %d on slave %s\n",
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003152 bond->dev->name, slave->new_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 slave->dev->name);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003154 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Jiri Pirkob9f60252009-08-31 11:09:38 +00003157do_failover:
3158 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00003159 block_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003160 write_lock_bh(&bond->curr_slave_lock);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003161 bond_select_active_slave(bond);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003162 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00003163 unblock_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003164 }
3165
3166 bond_set_carrier(bond);
3167}
3168
3169/*
3170 * Send ARP probes for active-backup mode ARP monitor.
3171 *
3172 * Called with bond->lock held for read.
3173 */
3174static void bond_ab_arp_probe(struct bonding *bond)
3175{
3176 struct slave *slave;
3177 int i;
3178
3179 read_lock(&bond->curr_slave_lock);
3180
3181 if (bond->current_arp_slave && bond->curr_active_slave)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003182 pr_info("PROBE: c_arp %s && cas %s BAD\n",
3183 bond->current_arp_slave->dev->name,
3184 bond->curr_active_slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003185
3186 if (bond->curr_active_slave) {
3187 bond_arp_send_all(bond, bond->curr_active_slave);
3188 read_unlock(&bond->curr_slave_lock);
3189 return;
3190 }
3191
3192 read_unlock(&bond->curr_slave_lock);
3193
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 /* if we don't have a curr_active_slave, search for the next available
3195 * backup slave from the current_arp_slave and make it the candidate
3196 * for becoming the curr_active_slave
3197 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003198
3199 if (!bond->current_arp_slave) {
3200 bond->current_arp_slave = bond->first_slave;
3201 if (!bond->current_arp_slave)
3202 return;
3203 }
3204
3205 bond_set_slave_inactive_flags(bond->current_arp_slave);
3206
3207 /* search for next candidate */
3208 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3209 if (IS_UP(slave->dev)) {
3210 slave->link = BOND_LINK_BACK;
3211 bond_set_slave_active_flags(slave);
3212 bond_arp_send_all(bond, slave);
3213 slave->jiffies = jiffies;
3214 bond->current_arp_slave = slave;
3215 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 }
3217
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003218 /* if the link state is up at this point, we
3219 * mark it down - this can happen if we have
3220 * simultaneous link failures and
3221 * reselect_active_interface doesn't make this
3222 * one the current slave so it is still marked
3223 * up when it is actually down
3224 */
3225 if (slave->link == BOND_LINK_UP) {
3226 slave->link = BOND_LINK_DOWN;
3227 if (slave->link_failure_count < UINT_MAX)
3228 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003230 bond_set_slave_inactive_flags(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003232 pr_info("%s: backup interface %s is now down.\n",
3233 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 }
3235 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003236}
3237
3238void bond_activebackup_arp_mon(struct work_struct *work)
3239{
3240 struct bonding *bond = container_of(work, struct bonding,
3241 arp_work.work);
3242 int delta_in_ticks;
3243
3244 read_lock(&bond->lock);
3245
3246 if (bond->kill_timers)
3247 goto out;
3248
3249 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3250
3251 if (bond->slave_cnt == 0)
3252 goto re_arm;
3253
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003254 if (bond->send_grat_arp) {
3255 read_lock(&bond->curr_slave_lock);
3256 bond_send_gratuitous_arp(bond);
3257 read_unlock(&bond->curr_slave_lock);
3258 }
3259
Brian Haley305d5522008-11-04 17:51:14 -08003260 if (bond->send_unsol_na) {
3261 read_lock(&bond->curr_slave_lock);
3262 bond_send_unsolicited_na(bond);
3263 read_unlock(&bond->curr_slave_lock);
3264 }
3265
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003266 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3267 read_unlock(&bond->lock);
3268 rtnl_lock();
3269 read_lock(&bond->lock);
3270
3271 bond_ab_arp_commit(bond, delta_in_ticks);
3272
3273 read_unlock(&bond->lock);
3274 rtnl_unlock();
3275 read_lock(&bond->lock);
3276 }
3277
3278 bond_ab_arp_probe(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
3280re_arm:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003281 if (bond->params.arp_interval)
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003282 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283out:
3284 read_unlock(&bond->lock);
3285}
3286
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287/*-------------------------- netdev event handling --------------------------*/
3288
3289/*
3290 * Change device name
3291 */
3292static int bond_event_changename(struct bonding *bond)
3293{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 bond_remove_proc_entry(bond);
3295 bond_create_proc_entry(bond);
Stephen Hemminger7e083842009-06-12 19:02:46 +00003296
Taku Izumif073c7c2010-12-09 15:17:13 +00003297 bond_debug_reregister(bond);
3298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 return NOTIFY_DONE;
3300}
3301
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003302static int bond_master_netdev_event(unsigned long event,
3303 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304{
Wang Chen454d7c92008-11-12 23:37:49 -08003305 struct bonding *event_bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
3307 switch (event) {
3308 case NETDEV_CHANGENAME:
3309 return bond_event_changename(event_bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 default:
3311 break;
3312 }
3313
3314 return NOTIFY_DONE;
3315}
3316
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003317static int bond_slave_netdev_event(unsigned long event,
3318 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319{
3320 struct net_device *bond_dev = slave_dev->master;
Wang Chen454d7c92008-11-12 23:37:49 -08003321 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
3323 switch (event) {
3324 case NETDEV_UNREGISTER:
3325 if (bond_dev) {
Jay Vosburgh1284cd32007-10-15 16:44:27 -07003326 if (bond->setup_by_slave)
3327 bond_release_and_destroy(bond_dev, slave_dev);
3328 else
3329 bond_release(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 }
3331 break;
3332 case NETDEV_CHANGE:
Jay Vosburgh17d04502009-03-18 18:38:25 -07003333 if (bond->params.mode == BOND_MODE_8023AD || bond_is_lb(bond)) {
3334 struct slave *slave;
3335
3336 slave = bond_get_slave_by_dev(bond, slave_dev);
3337 if (slave) {
3338 u16 old_speed = slave->speed;
3339 u16 old_duplex = slave->duplex;
3340
3341 bond_update_speed_duplex(slave);
3342
3343 if (bond_is_lb(bond))
3344 break;
3345
3346 if (old_speed != slave->speed)
3347 bond_3ad_adapter_speed_changed(slave);
3348 if (old_duplex != slave->duplex)
3349 bond_3ad_adapter_duplex_changed(slave);
3350 }
3351 }
3352
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 break;
3354 case NETDEV_DOWN:
3355 /*
3356 * ... Or is it this?
3357 */
3358 break;
3359 case NETDEV_CHANGEMTU:
3360 /*
3361 * TODO: Should slaves be allowed to
3362 * independently alter their MTU? For
3363 * an active-backup bond, slaves need
3364 * not be the same type of device, so
3365 * MTUs may vary. For other modes,
3366 * slaves arguably should have the
3367 * same MTUs. To do this, we'd need to
3368 * take over the slave's change_mtu
3369 * function for the duration of their
3370 * servitude.
3371 */
3372 break;
3373 case NETDEV_CHANGENAME:
3374 /*
3375 * TODO: handle changing the primary's name
3376 */
3377 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003378 case NETDEV_FEAT_CHANGE:
3379 bond_compute_features(bond);
3380 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 default:
3382 break;
3383 }
3384
3385 return NOTIFY_DONE;
3386}
3387
3388/*
3389 * bond_netdev_event: handle netdev notifier chain events.
3390 *
3391 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003392 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 * locks for us to safely manipulate the slave devices (RTNL lock,
3394 * dev_probe_lock).
3395 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003396static int bond_netdev_event(struct notifier_block *this,
3397 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398{
3399 struct net_device *event_dev = (struct net_device *)ptr;
3400
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003401 pr_debug("event_dev: %s, event: %lx\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003402 event_dev ? event_dev->name : "None",
3403 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003405 if (!(event_dev->priv_flags & IFF_BONDING))
3406 return NOTIFY_DONE;
3407
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 if (event_dev->flags & IFF_MASTER) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003409 pr_debug("IFF_MASTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 return bond_master_netdev_event(event, event_dev);
3411 }
3412
3413 if (event_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003414 pr_debug("IFF_SLAVE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 return bond_slave_netdev_event(event, event_dev);
3416 }
3417
3418 return NOTIFY_DONE;
3419}
3420
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003421/*
3422 * bond_inetaddr_event: handle inetaddr notifier chain events.
3423 *
3424 * We keep track of device IPs primarily to use as source addresses in
3425 * ARP monitor probes (rather than spewing out broadcasts all the time).
3426 *
3427 * We track one IP for the main device (if it has one), plus one per VLAN.
3428 */
3429static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3430{
3431 struct in_ifaddr *ifa = ptr;
3432 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003433 struct bond_net *bn = net_generic(dev_net(event_dev), bond_net_id);
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003434 struct bonding *bond;
3435 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003436
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003437 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003438 if (bond->dev == event_dev) {
3439 switch (event) {
3440 case NETDEV_UP:
3441 bond->master_ip = ifa->ifa_local;
3442 return NOTIFY_OK;
3443 case NETDEV_DOWN:
3444 bond->master_ip = bond_glean_dev_ip(bond->dev);
3445 return NOTIFY_OK;
3446 default:
3447 return NOTIFY_DONE;
3448 }
3449 }
3450
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003451 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +00003452 if (!bond->vlgrp)
3453 continue;
Dan Aloni5c15bde2007-03-02 20:44:51 -08003454 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003455 if (vlan_dev == event_dev) {
3456 switch (event) {
3457 case NETDEV_UP:
3458 vlan->vlan_ip = ifa->ifa_local;
3459 return NOTIFY_OK;
3460 case NETDEV_DOWN:
3461 vlan->vlan_ip =
3462 bond_glean_dev_ip(vlan_dev);
3463 return NOTIFY_OK;
3464 default:
3465 return NOTIFY_DONE;
3466 }
3467 }
3468 }
3469 }
3470 return NOTIFY_DONE;
3471}
3472
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473static struct notifier_block bond_netdev_notifier = {
3474 .notifier_call = bond_netdev_event,
3475};
3476
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003477static struct notifier_block bond_inetaddr_notifier = {
3478 .notifier_call = bond_inetaddr_event,
3479};
3480
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481/*-------------------------- Packet type handling ---------------------------*/
3482
3483/* register to receive lacpdus on a bond */
3484static void bond_register_lacpdu(struct bonding *bond)
3485{
3486 struct packet_type *pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
3487
3488 /* initialize packet type */
3489 pk_type->type = PKT_TYPE_LACPDU;
3490 pk_type->dev = bond->dev;
3491 pk_type->func = bond_3ad_lacpdu_recv;
3492
3493 dev_add_pack(pk_type);
3494}
3495
3496/* unregister to receive lacpdus on a bond */
3497static void bond_unregister_lacpdu(struct bonding *bond)
3498{
3499 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
3500}
3501
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003502void bond_register_arp(struct bonding *bond)
3503{
3504 struct packet_type *pt = &bond->arp_mon_pt;
3505
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003506 if (pt->type)
3507 return;
3508
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003509 pt->type = htons(ETH_P_ARP);
Jay Vosburghe245cb72007-02-28 17:03:27 -08003510 pt->dev = bond->dev;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003511 pt->func = bond_arp_rcv;
3512 dev_add_pack(pt);
3513}
3514
3515void bond_unregister_arp(struct bonding *bond)
3516{
Jay Vosburghc4f283b2007-02-28 17:03:20 -08003517 struct packet_type *pt = &bond->arp_mon_pt;
3518
3519 dev_remove_pack(pt);
3520 pt->type = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003521}
3522
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003523/*---------------------------- Hashing Policies -----------------------------*/
3524
3525/*
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003526 * Hash for the output device based upon layer 2 and layer 3 data. If
3527 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3528 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003529static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003530{
3531 struct ethhdr *data = (struct ethhdr *)skb->data;
3532 struct iphdr *iph = ip_hdr(skb);
3533
Brian Haleyf14c4e42008-09-02 10:08:08 -04003534 if (skb->protocol == htons(ETH_P_IP)) {
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003535 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
Jasper Spaansd3da6832009-10-23 04:08:46 +00003536 (data->h_dest[5] ^ data->h_source[5])) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003537 }
3538
Jasper Spaansd3da6832009-10-23 04:08:46 +00003539 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003540}
3541
3542/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003543 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003544 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3545 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3546 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003547static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003548{
3549 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003550 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003551 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003552 int layer4_xor = 0;
3553
Brian Haleyf14c4e42008-09-02 10:08:08 -04003554 if (skb->protocol == htons(ETH_P_IP)) {
3555 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003556 (iph->protocol == IPPROTO_TCP ||
3557 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003558 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003559 }
3560 return (layer4_xor ^
3561 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3562
3563 }
3564
Jasper Spaansd3da6832009-10-23 04:08:46 +00003565 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003566}
3567
3568/*
3569 * Hash for the output device based upon layer 2 data
3570 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003571static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003572{
3573 struct ethhdr *data = (struct ethhdr *)skb->data;
3574
Jasper Spaansd3da6832009-10-23 04:08:46 +00003575 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003576}
3577
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578/*-------------------------- Device entry points ----------------------------*/
3579
3580static int bond_open(struct net_device *bond_dev)
3581{
Wang Chen454d7c92008-11-12 23:37:49 -08003582 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
3584 bond->kill_timers = 0;
3585
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003586 INIT_DELAYED_WORK(&bond->mcast_work, bond_resend_igmp_join_requests_delayed);
3587
Holger Eitzenberger58402052008-12-09 23:07:13 -08003588 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 /* bond_alb_initialize must be called before the timer
3590 * is started.
3591 */
3592 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3593 /* something went wrong - fail the open operation */
stephen hemmingerb4739462010-01-25 23:34:15 +00003594 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 }
3596
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003597 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3598 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 }
3600
3601 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003602 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3603 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 }
3605
3606 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003607 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3608 INIT_DELAYED_WORK(&bond->arp_work,
3609 bond_activebackup_arp_mon);
3610 else
3611 INIT_DELAYED_WORK(&bond->arp_work,
3612 bond_loadbalance_arp_mon);
3613
3614 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003615 if (bond->params.arp_validate)
3616 bond_register_arp(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 }
3618
3619 if (bond->params.mode == BOND_MODE_8023AD) {
Adrian Bunka40745f2007-10-24 18:27:43 +02003620 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003621 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 /* register to receive LACPDUs */
3623 bond_register_lacpdu(bond);
Jay Vosburghfd989c82008-11-04 17:51:16 -08003624 bond_3ad_initiate_agg_selection(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 }
3626
3627 return 0;
3628}
3629
3630static int bond_close(struct net_device *bond_dev)
3631{
Wang Chen454d7c92008-11-12 23:37:49 -08003632 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
3634 if (bond->params.mode == BOND_MODE_8023AD) {
3635 /* Unregister the receive of LACPDUs */
3636 bond_unregister_lacpdu(bond);
3637 }
3638
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003639 if (bond->params.arp_validate)
3640 bond_unregister_arp(bond);
3641
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 write_lock_bh(&bond->lock);
3643
Jay Vosburghb59f9f72008-06-13 18:12:03 -07003644 bond->send_grat_arp = 0;
Brian Haley305d5522008-11-04 17:51:14 -08003645 bond->send_unsol_na = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
3647 /* signal timers not to re-arm */
3648 bond->kill_timers = 1;
3649
3650 write_unlock_bh(&bond->lock);
3651
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003653 cancel_delayed_work(&bond->mii_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 }
3655
3656 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003657 cancel_delayed_work(&bond->arp_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 }
3659
3660 switch (bond->params.mode) {
3661 case BOND_MODE_8023AD:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003662 cancel_delayed_work(&bond->ad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 break;
3664 case BOND_MODE_TLB:
3665 case BOND_MODE_ALB:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003666 cancel_delayed_work(&bond->alb_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 break;
3668 default:
3669 break;
3670 }
3671
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003672 if (delayed_work_pending(&bond->mcast_work))
3673 cancel_delayed_work(&bond->mcast_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
Holger Eitzenberger58402052008-12-09 23:07:13 -08003675 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 /* Must be called only after all
3677 * slaves have been released
3678 */
3679 bond_alb_deinitialize(bond);
3680 }
3681
3682 return 0;
3683}
3684
Eric Dumazet28172732010-07-07 14:58:56 -07003685static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3686 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687{
Wang Chen454d7c92008-11-12 23:37:49 -08003688 struct bonding *bond = netdev_priv(bond_dev);
Eric Dumazet28172732010-07-07 14:58:56 -07003689 struct rtnl_link_stats64 temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 struct slave *slave;
3691 int i;
3692
Eric Dumazet28172732010-07-07 14:58:56 -07003693 memset(stats, 0, sizeof(*stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694
3695 read_lock_bh(&bond->lock);
3696
3697 bond_for_each_slave(bond, slave, i) {
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00003698 const struct rtnl_link_stats64 *sstats =
Eric Dumazet28172732010-07-07 14:58:56 -07003699 dev_get_stats(slave->dev, &temp);
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08003700
Eric Dumazet28172732010-07-07 14:58:56 -07003701 stats->rx_packets += sstats->rx_packets;
3702 stats->rx_bytes += sstats->rx_bytes;
3703 stats->rx_errors += sstats->rx_errors;
3704 stats->rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705
Eric Dumazet28172732010-07-07 14:58:56 -07003706 stats->tx_packets += sstats->tx_packets;
3707 stats->tx_bytes += sstats->tx_bytes;
3708 stats->tx_errors += sstats->tx_errors;
3709 stats->tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
Eric Dumazet28172732010-07-07 14:58:56 -07003711 stats->multicast += sstats->multicast;
3712 stats->collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713
Eric Dumazet28172732010-07-07 14:58:56 -07003714 stats->rx_length_errors += sstats->rx_length_errors;
3715 stats->rx_over_errors += sstats->rx_over_errors;
3716 stats->rx_crc_errors += sstats->rx_crc_errors;
3717 stats->rx_frame_errors += sstats->rx_frame_errors;
3718 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3719 stats->rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
Eric Dumazet28172732010-07-07 14:58:56 -07003721 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3722 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3723 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3724 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3725 stats->tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 }
3727
3728 read_unlock_bh(&bond->lock);
3729
3730 return stats;
3731}
3732
3733static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3734{
3735 struct net_device *slave_dev = NULL;
3736 struct ifbond k_binfo;
3737 struct ifbond __user *u_binfo = NULL;
3738 struct ifslave k_sinfo;
3739 struct ifslave __user *u_sinfo = NULL;
3740 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 int res = 0;
3742
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003743 pr_debug("bond_ioctl: master=%s, cmd=%d\n", bond_dev->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
3745 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 case SIOCGMIIPHY:
3747 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003748 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003750
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 mii->phy_id = 0;
3752 /* Fall Through */
3753 case SIOCGMIIREG:
3754 /*
3755 * We do this again just in case we were called by SIOCGMIIREG
3756 * instead of SIOCGMIIPHY.
3757 */
3758 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003759 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003761
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
3763 if (mii->reg_num == 1) {
Wang Chen454d7c92008-11-12 23:37:49 -08003764 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 mii->val_out = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003766 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 read_lock(&bond->curr_slave_lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003768 if (netif_carrier_ok(bond->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 mii->val_out = BMSR_LSTATUS;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003770
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 read_unlock(&bond->curr_slave_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003772 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 }
3774
3775 return 0;
3776 case BOND_INFO_QUERY_OLD:
3777 case SIOCBONDINFOQUERY:
3778 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3779
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003780 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782
3783 res = bond_info_query(bond_dev, &k_binfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003784 if (res == 0 &&
3785 copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
3786 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787
3788 return res;
3789 case BOND_SLAVE_INFO_QUERY_OLD:
3790 case SIOCBONDSLAVEINFOQUERY:
3791 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3792
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003793 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
3796 res = bond_slave_info_query(bond_dev, &k_sinfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003797 if (res == 0 &&
3798 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
3799 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800
3801 return res;
3802 default:
3803 /* Go on */
3804 break;
3805 }
3806
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003807 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003810 slave_dev = dev_get_by_name(dev_net(bond_dev), ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003812 pr_debug("slave_dev=%p:\n", slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003814 if (!slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 res = -ENODEV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003816 else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003817 pr_debug("slave_dev->name=%s:\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 switch (cmd) {
3819 case BOND_ENSLAVE_OLD:
3820 case SIOCBONDENSLAVE:
3821 res = bond_enslave(bond_dev, slave_dev);
3822 break;
3823 case BOND_RELEASE_OLD:
3824 case SIOCBONDRELEASE:
3825 res = bond_release(bond_dev, slave_dev);
3826 break;
3827 case BOND_SETHWADDR_OLD:
3828 case SIOCBONDSETHWADDR:
3829 res = bond_sethwaddr(bond_dev, slave_dev);
3830 break;
3831 case BOND_CHANGE_ACTIVE_OLD:
3832 case SIOCBONDCHANGEACTIVE:
3833 res = bond_ioctl_change_active(bond_dev, slave_dev);
3834 break;
3835 default:
3836 res = -EOPNOTSUPP;
3837 }
3838
3839 dev_put(slave_dev);
3840 }
3841
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 return res;
3843}
3844
Jiri Pirko22bedad32010-04-01 21:22:57 +00003845static bool bond_addr_in_mc_list(unsigned char *addr,
3846 struct netdev_hw_addr_list *list,
3847 int addrlen)
3848{
3849 struct netdev_hw_addr *ha;
3850
3851 netdev_hw_addr_list_for_each(ha, list)
3852 if (!memcmp(ha->addr, addr, addrlen))
3853 return true;
3854
3855 return false;
3856}
3857
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858static void bond_set_multicast_list(struct net_device *bond_dev)
3859{
Wang Chen454d7c92008-11-12 23:37:49 -08003860 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00003861 struct netdev_hw_addr *ha;
3862 bool found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 /*
3865 * Do promisc before checking multicast_mode
3866 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003867 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07003868 /*
3869 * FIXME: Need to handle the error when one of the multi-slaves
3870 * encounters error.
3871 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 bond_set_promiscuity(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003874
3875 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 bond_set_promiscuity(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003877
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
3879 /* set allmulti flag to slaves */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003880 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07003881 /*
3882 * FIXME: Need to handle the error when one of the multi-slaves
3883 * encounters error.
3884 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 bond_set_allmulti(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003887
3888 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 bond_set_allmulti(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003890
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08003892 read_lock(&bond->lock);
3893
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 bond->flags = bond_dev->flags;
3895
3896 /* looking for addresses to add to slaves' mc list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00003897 netdev_for_each_mc_addr(ha, bond_dev) {
3898 found = bond_addr_in_mc_list(ha->addr, &bond->mc_list,
3899 bond_dev->addr_len);
3900 if (!found)
3901 bond_mc_add(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 }
3903
3904 /* looking for addresses to delete from slaves' list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00003905 netdev_hw_addr_list_for_each(ha, &bond->mc_list) {
3906 found = bond_addr_in_mc_list(ha->addr, &bond_dev->mc,
3907 bond_dev->addr_len);
3908 if (!found)
3909 bond_mc_del(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 }
3911
3912 /* save master's multicast list */
Jiri Pirko22bedad32010-04-01 21:22:57 +00003913 __hw_addr_flush(&bond->mc_list);
3914 __hw_addr_add_multiple(&bond->mc_list, &bond_dev->mc,
3915 bond_dev->addr_len, NETDEV_HW_ADDR_T_MULTICAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08003917 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918}
3919
Stephen Hemminger00829822008-11-20 20:14:53 -08003920static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
3921{
3922 struct bonding *bond = netdev_priv(dev);
3923 struct slave *slave = bond->first_slave;
3924
3925 if (slave) {
3926 const struct net_device_ops *slave_ops
3927 = slave->dev->netdev_ops;
3928 if (slave_ops->ndo_neigh_setup)
Patrick McHardy72e22402009-03-05 01:57:44 -08003929 return slave_ops->ndo_neigh_setup(slave->dev, parms);
Stephen Hemminger00829822008-11-20 20:14:53 -08003930 }
3931 return 0;
3932}
3933
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934/*
3935 * Change the MTU of all of a master's slaves to match the master
3936 */
3937static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3938{
Wang Chen454d7c92008-11-12 23:37:49 -08003939 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 struct slave *slave, *stop_at;
3941 int res = 0;
3942 int i;
3943
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003944 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003945 (bond_dev ? bond_dev->name : "None"), new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
3947 /* Can't hold bond->lock with bh disabled here since
3948 * some base drivers panic. On the other hand we can't
3949 * hold bond->lock without bh disabled because we'll
3950 * deadlock. The only solution is to rely on the fact
3951 * that we're under rtnl_lock here, and the slaves
3952 * list won't change. This doesn't solve the problem
3953 * of setting the slave's MTU while it is
3954 * transmitting, but the assumption is that the base
3955 * driver can handle that.
3956 *
3957 * TODO: figure out a way to safely iterate the slaves
3958 * list, but without holding a lock around the actual
3959 * call to the base driver.
3960 */
3961
3962 bond_for_each_slave(bond, slave, i) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003963 pr_debug("s %p s->p %p c_m %p\n",
3964 slave,
3965 slave->prev,
3966 slave->dev->netdev_ops->ndo_change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08003967
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 res = dev_set_mtu(slave->dev, new_mtu);
3969
3970 if (res) {
3971 /* If we failed to set the slave's mtu to the new value
3972 * we must abort the operation even in ACTIVE_BACKUP
3973 * mode, because if we allow the backup slaves to have
3974 * different mtu values than the active slave we'll
3975 * need to change their mtu when doing a failover. That
3976 * means changing their mtu from timer context, which
3977 * is probably not a good idea.
3978 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003979 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 goto unwind;
3981 }
3982 }
3983
3984 bond_dev->mtu = new_mtu;
3985
3986 return 0;
3987
3988unwind:
3989 /* unwind from head to the slave that failed */
3990 stop_at = slave;
3991 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3992 int tmp_res;
3993
3994 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
3995 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003996 pr_debug("unwind err %d dev %s\n",
3997 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 }
3999 }
4000
4001 return res;
4002}
4003
4004/*
4005 * Change HW address
4006 *
4007 * Note that many devices must be down to change the HW address, and
4008 * downing the master releases all slaves. We can make bonds full of
4009 * bonding devices to test this, however.
4010 */
4011static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4012{
Wang Chen454d7c92008-11-12 23:37:49 -08004013 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 struct sockaddr *sa = addr, tmp_sa;
4015 struct slave *slave, *stop_at;
4016 int res = 0;
4017 int i;
4018
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004019 if (bond->params.mode == BOND_MODE_ALB)
4020 return bond_alb_set_mac_address(bond_dev, addr);
4021
4022
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004023 pr_debug("bond=%p, name=%s\n",
4024 bond, bond_dev ? bond_dev->name : "None");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025
Jay Vosburghdd957c52007-10-09 19:57:24 -07004026 /*
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004027 * If fail_over_mac is set to active, do nothing and return
4028 * success. Returning an error causes ifenslave to fail.
Jay Vosburghdd957c52007-10-09 19:57:24 -07004029 */
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004030 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
Jay Vosburghdd957c52007-10-09 19:57:24 -07004031 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07004032
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004033 if (!is_valid_ether_addr(sa->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
4036 /* Can't hold bond->lock with bh disabled here since
4037 * some base drivers panic. On the other hand we can't
4038 * hold bond->lock without bh disabled because we'll
4039 * deadlock. The only solution is to rely on the fact
4040 * that we're under rtnl_lock here, and the slaves
4041 * list won't change. This doesn't solve the problem
4042 * of setting the slave's hw address while it is
4043 * transmitting, but the assumption is that the base
4044 * driver can handle that.
4045 *
4046 * TODO: figure out a way to safely iterate the slaves
4047 * list, but without holding a lock around the actual
4048 * call to the base driver.
4049 */
4050
4051 bond_for_each_slave(bond, slave, i) {
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004052 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004053 pr_debug("slave %p %s\n", slave, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004055 if (slave_ops->ndo_set_mac_address == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 res = -EOPNOTSUPP;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004057 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 goto unwind;
4059 }
4060
4061 res = dev_set_mac_address(slave->dev, addr);
4062 if (res) {
4063 /* TODO: consider downing the slave
4064 * and retry ?
4065 * User should expect communications
4066 * breakage anyway until ARP finish
4067 * updating, so...
4068 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08004069 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 goto unwind;
4071 }
4072 }
4073
4074 /* success */
4075 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
4076 return 0;
4077
4078unwind:
4079 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
4080 tmp_sa.sa_family = bond_dev->type;
4081
4082 /* unwind from head to the slave that failed */
4083 stop_at = slave;
4084 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
4085 int tmp_res;
4086
4087 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
4088 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004089 pr_debug("unwind err %d dev %s\n",
4090 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 }
4092 }
4093
4094 return res;
4095}
4096
4097static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
4098{
Wang Chen454d7c92008-11-12 23:37:49 -08004099 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 struct slave *slave, *start_at;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004101 int i, slave_no, res = 1;
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004102 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103
4104 read_lock(&bond->lock);
4105
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004106 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 goto out;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004108 /*
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004109 * Start with the curr_active_slave that joined the bond as the
4110 * default for sending IGMP traffic. For failover purposes one
4111 * needs to maintain some consistency for the interface that will
4112 * send the join/membership reports. The curr_active_slave found
4113 * will send all of this type of traffic.
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004114 */
Eric Dumazet00ae7022010-03-30 23:08:37 +00004115 if ((iph->protocol == IPPROTO_IGMP) &&
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004116 (skb->protocol == htons(ETH_P_IP))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004118 read_lock(&bond->curr_slave_lock);
4119 slave = bond->curr_active_slave;
4120 read_unlock(&bond->curr_slave_lock);
4121
4122 if (!slave)
4123 goto out;
4124 } else {
4125 /*
4126 * Concurrent TX may collide on rr_tx_counter; we accept
4127 * that as being rare enough not to justify using an
4128 * atomic op here.
4129 */
4130 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
4131
4132 bond_for_each_slave(bond, slave, i) {
4133 slave_no--;
4134 if (slave_no < 0)
4135 break;
4136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 }
4138
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004139 start_at = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 bond_for_each_slave_from(bond, slave, i, start_at) {
4141 if (IS_UP(slave->dev) &&
4142 (slave->link == BOND_LINK_UP) &&
4143 (slave->state == BOND_STATE_ACTIVE)) {
4144 res = bond_dev_queue_xmit(bond, skb, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 break;
4146 }
4147 }
4148
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149out:
4150 if (res) {
4151 /* no suitable interface, frame not sent */
4152 dev_kfree_skb(skb);
4153 }
4154 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004155 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156}
4157
John W. Linville075897c2005-09-28 17:50:53 -04004158
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159/*
4160 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4161 * the bond has a usable interface.
4162 */
4163static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4164{
Wang Chen454d7c92008-11-12 23:37:49 -08004165 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 int res = 1;
4167
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 read_lock(&bond->lock);
4169 read_lock(&bond->curr_slave_lock);
4170
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004171 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173
John W. Linville075897c2005-09-28 17:50:53 -04004174 if (!bond->curr_active_slave)
4175 goto out;
4176
John W. Linville075897c2005-09-28 17:50:53 -04004177 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4178
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004180 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 /* no suitable interface, frame not sent */
4182 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004183
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 read_unlock(&bond->curr_slave_lock);
4185 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004186 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187}
4188
4189/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004190 * In bond_xmit_xor() , we determine the output device by using a pre-
4191 * determined xmit_hash_policy(), If the selected device is not enabled,
4192 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 */
4194static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4195{
Wang Chen454d7c92008-11-12 23:37:49 -08004196 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 struct slave *slave, *start_at;
4198 int slave_no;
4199 int i;
4200 int res = 1;
4201
4202 read_lock(&bond->lock);
4203
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004204 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
Jasper Spaansa361c832009-10-23 04:09:24 +00004207 slave_no = bond->xmit_hash_policy(skb, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
4209 bond_for_each_slave(bond, slave, i) {
4210 slave_no--;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004211 if (slave_no < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 }
4214
4215 start_at = slave;
4216
4217 bond_for_each_slave_from(bond, slave, i, start_at) {
4218 if (IS_UP(slave->dev) &&
4219 (slave->link == BOND_LINK_UP) &&
4220 (slave->state == BOND_STATE_ACTIVE)) {
4221 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4222 break;
4223 }
4224 }
4225
4226out:
4227 if (res) {
4228 /* no suitable interface, frame not sent */
4229 dev_kfree_skb(skb);
4230 }
4231 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004232 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233}
4234
4235/*
4236 * in broadcast mode, we send everything to all usable interfaces.
4237 */
4238static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4239{
Wang Chen454d7c92008-11-12 23:37:49 -08004240 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 struct slave *slave, *start_at;
4242 struct net_device *tx_dev = NULL;
4243 int i;
4244 int res = 1;
4245
4246 read_lock(&bond->lock);
4247
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004248 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
4251 read_lock(&bond->curr_slave_lock);
4252 start_at = bond->curr_active_slave;
4253 read_unlock(&bond->curr_slave_lock);
4254
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004255 if (!start_at)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257
4258 bond_for_each_slave_from(bond, slave, i, start_at) {
4259 if (IS_UP(slave->dev) &&
4260 (slave->link == BOND_LINK_UP) &&
4261 (slave->state == BOND_STATE_ACTIVE)) {
4262 if (tx_dev) {
4263 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4264 if (!skb2) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004265 pr_err("%s: Error: bond_xmit_broadcast(): skb_clone() failed\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08004266 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 continue;
4268 }
4269
4270 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4271 if (res) {
4272 dev_kfree_skb(skb2);
4273 continue;
4274 }
4275 }
4276 tx_dev = slave->dev;
4277 }
4278 }
4279
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004280 if (tx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 res = bond_dev_queue_xmit(bond, skb, tx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
4283out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004284 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 /* no suitable interface, frame not sent */
4286 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004287
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 /* frame sent to all suitable interfaces */
4289 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004290 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291}
4292
4293/*------------------------- Device initialization ---------------------------*/
4294
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004295static void bond_set_xmit_hash_policy(struct bonding *bond)
4296{
4297 switch (bond->params.xmit_policy) {
4298 case BOND_XMIT_POLICY_LAYER23:
4299 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4300 break;
4301 case BOND_XMIT_POLICY_LAYER34:
4302 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4303 break;
4304 case BOND_XMIT_POLICY_LAYER2:
4305 default:
4306 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4307 break;
4308 }
4309}
4310
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004311/*
4312 * Lookup the slave that corresponds to a qid
4313 */
4314static inline int bond_slave_override(struct bonding *bond,
4315 struct sk_buff *skb)
4316{
4317 int i, res = 1;
4318 struct slave *slave = NULL;
4319 struct slave *check_slave;
4320
4321 read_lock(&bond->lock);
4322
4323 if (!BOND_IS_OK(bond) || !skb->queue_mapping)
4324 goto out;
4325
4326 /* Find out if any slaves have the same mapping as this skb. */
4327 bond_for_each_slave(bond, check_slave, i) {
4328 if (check_slave->queue_id == skb->queue_mapping) {
4329 slave = check_slave;
4330 break;
4331 }
4332 }
4333
4334 /* If the slave isn't UP, use default transmit policy. */
4335 if (slave && slave->queue_id && IS_UP(slave->dev) &&
4336 (slave->link == BOND_LINK_UP)) {
4337 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4338 }
4339
4340out:
4341 read_unlock(&bond->lock);
4342 return res;
4343}
4344
4345static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb)
4346{
4347 /*
4348 * This helper function exists to help dev_pick_tx get the correct
Phil Oesterfd0e4352011-03-14 06:22:04 +00004349 * destination queue. Using a helper function skips a call to
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004350 * skb_tx_hash and will put the skbs in the queue we expect on their
4351 * way down to the bonding driver.
4352 */
Phil Oesterfd0e4352011-03-14 06:22:04 +00004353 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
4354
4355 if (unlikely(txq >= dev->real_num_tx_queues)) {
4356 do
4357 txq -= dev->real_num_tx_queues;
4358 while (txq >= dev->real_num_tx_queues);
4359 }
4360 return txq;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004361}
4362
Stephen Hemminger424efe92009-08-31 19:50:51 +00004363static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
Stephen Hemminger00829822008-11-20 20:14:53 -08004364{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004365 struct bonding *bond = netdev_priv(dev);
4366
Neil Hormane843fa52010-10-13 16:01:50 +00004367 /*
4368 * If we risk deadlock from transmitting this in the
4369 * netpoll path, tell netpoll to queue the frame for later tx
4370 */
4371 if (is_netpoll_tx_blocked(dev))
4372 return NETDEV_TX_BUSY;
4373
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004374 if (TX_QUEUE_OVERRIDE(bond->params.mode)) {
4375 if (!bond_slave_override(bond, skb))
4376 return NETDEV_TX_OK;
4377 }
Stephen Hemminger00829822008-11-20 20:14:53 -08004378
4379 switch (bond->params.mode) {
4380 case BOND_MODE_ROUNDROBIN:
4381 return bond_xmit_roundrobin(skb, dev);
4382 case BOND_MODE_ACTIVEBACKUP:
4383 return bond_xmit_activebackup(skb, dev);
4384 case BOND_MODE_XOR:
4385 return bond_xmit_xor(skb, dev);
4386 case BOND_MODE_BROADCAST:
4387 return bond_xmit_broadcast(skb, dev);
4388 case BOND_MODE_8023AD:
4389 return bond_3ad_xmit_xor(skb, dev);
4390 case BOND_MODE_ALB:
4391 case BOND_MODE_TLB:
4392 return bond_alb_xmit(skb, dev);
4393 default:
4394 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004395 pr_err("%s: Error: Unknown bonding mode %d\n",
4396 dev->name, bond->params.mode);
Stephen Hemminger00829822008-11-20 20:14:53 -08004397 WARN_ON_ONCE(1);
4398 dev_kfree_skb(skb);
4399 return NETDEV_TX_OK;
4400 }
4401}
4402
4403
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404/*
4405 * set bond mode specific net device operations
4406 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004407void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004409 struct net_device *bond_dev = bond->dev;
4410
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 switch (mode) {
4412 case BOND_MODE_ROUNDROBIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 break;
4414 case BOND_MODE_ACTIVEBACKUP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 break;
4416 case BOND_MODE_XOR:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004417 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 break;
4419 case BOND_MODE_BROADCAST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 break;
4421 case BOND_MODE_8023AD:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004422 bond_set_master_3ad_flags(bond);
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004423 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004426 bond_set_master_alb_flags(bond);
4427 /* FALLTHRU */
4428 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 break;
4430 default:
4431 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004432 pr_err("%s: Error: Unknown bonding mode %d\n",
4433 bond_dev->name, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 break;
4435 }
4436}
4437
Jay Vosburgh217df672005-09-26 16:11:50 -07004438static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4439 struct ethtool_drvinfo *drvinfo)
4440{
4441 strncpy(drvinfo->driver, DRV_NAME, 32);
4442 strncpy(drvinfo->version, DRV_VERSION, 32);
4443 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4444}
4445
Jeff Garzik7282d492006-09-13 14:30:00 -04004446static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004447 .get_drvinfo = bond_ethtool_get_drvinfo,
Stephen Hemmingerfa53eba2008-09-13 21:17:09 -04004448 .get_link = ethtool_op_get_link,
4449 .get_tx_csum = ethtool_op_get_tx_csum,
4450 .get_sg = ethtool_op_get_sg,
4451 .get_tso = ethtool_op_get_tso,
4452 .get_ufo = ethtool_op_get_ufo,
4453 .get_flags = ethtool_op_get_flags,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004454};
4455
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004456static const struct net_device_ops bond_netdev_ops = {
Stephen Hemminger181470f2009-06-12 19:02:52 +00004457 .ndo_init = bond_init,
Stephen Hemminger9e716262009-06-12 19:02:47 +00004458 .ndo_uninit = bond_uninit,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004459 .ndo_open = bond_open,
4460 .ndo_stop = bond_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08004461 .ndo_start_xmit = bond_start_xmit,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004462 .ndo_select_queue = bond_select_queue,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00004463 .ndo_get_stats64 = bond_get_stats,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004464 .ndo_do_ioctl = bond_do_ioctl,
4465 .ndo_set_multicast_list = bond_set_multicast_list,
4466 .ndo_change_mtu = bond_change_mtu,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004467 .ndo_set_mac_address = bond_set_mac_address,
Stephen Hemminger00829822008-11-20 20:14:53 -08004468 .ndo_neigh_setup = bond_neigh_setup,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004469 .ndo_vlan_rx_register = bond_vlan_rx_register,
4470 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4471 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
WANG Congf6dc31a2010-05-06 00:48:51 -07004472#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00004473 .ndo_netpoll_setup = bond_netpoll_setup,
WANG Congf6dc31a2010-05-06 00:48:51 -07004474 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
4475 .ndo_poll_controller = bond_poll_controller,
4476#endif
Jiri Pirko9232ecc2011-02-13 09:33:01 +00004477 .ndo_add_slave = bond_enslave,
4478 .ndo_del_slave = bond_release,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004479};
4480
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004481static void bond_destructor(struct net_device *bond_dev)
4482{
4483 struct bonding *bond = netdev_priv(bond_dev);
4484 if (bond->wq)
4485 destroy_workqueue(bond->wq);
4486 free_netdev(bond_dev);
4487}
4488
Stephen Hemminger181470f2009-06-12 19:02:52 +00004489static void bond_setup(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490{
Wang Chen454d7c92008-11-12 23:37:49 -08004491 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 /* initialize rwlocks */
4494 rwlock_init(&bond->lock);
4495 rwlock_init(&bond->curr_slave_lock);
4496
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00004497 bond->params = bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
4499 /* Initialize pointers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 bond->dev = bond_dev;
4501 INIT_LIST_HEAD(&bond->vlan_list);
4502
4503 /* Initialize the device entry points */
Stephen Hemminger181470f2009-06-12 19:02:52 +00004504 ether_setup(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004505 bond_dev->netdev_ops = &bond_netdev_ops;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004506 bond_dev->ethtool_ops = &bond_ethtool_ops;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004507 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004509 bond_dev->destructor = bond_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
4511 /* Initialize the device options */
4512 bond_dev->tx_queue_len = 0;
4513 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004514 bond_dev->priv_flags |= IFF_BONDING;
Stephen Hemminger181470f2009-06-12 19:02:52 +00004515 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
4516
Jay Vosburgh6cf3f412008-11-03 18:16:50 -08004517 if (bond->params.arp_interval)
4518 bond_dev->priv_flags |= IFF_MASTER_ARPMON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
4520 /* At first, we block adding VLANs. That's the only way to
4521 * prevent problems that occur when adding VLANs over an
4522 * empty bond. The block will be removed once non-challenged
4523 * slaves are enslaved.
4524 */
4525 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4526
Herbert Xu932ff272006-06-09 12:20:56 -07004527 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 * transmitting */
4529 bond_dev->features |= NETIF_F_LLTX;
4530
4531 /* By default, we declare the bond to be fully
4532 * VLAN hardware accelerated capable. Special
4533 * care is taken in the various xmit functions
4534 * when there are slaves that are not hw accel
4535 * capable
4536 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4538 NETIF_F_HW_VLAN_RX |
4539 NETIF_F_HW_VLAN_FILTER);
4540
Eric Dumazete6599c22010-09-17 09:25:07 +00004541 /* By default, we enable GRO on bonding devices.
4542 * Actual support requires lowlevel drivers are GRO ready.
4543 */
4544 bond_dev->features |= NETIF_F_GRO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545}
4546
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004547static void bond_work_cancel_all(struct bonding *bond)
4548{
4549 write_lock_bh(&bond->lock);
4550 bond->kill_timers = 1;
4551 write_unlock_bh(&bond->lock);
4552
4553 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4554 cancel_delayed_work(&bond->mii_work);
4555
4556 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4557 cancel_delayed_work(&bond->arp_work);
4558
4559 if (bond->params.mode == BOND_MODE_ALB &&
4560 delayed_work_pending(&bond->alb_work))
4561 cancel_delayed_work(&bond->alb_work);
4562
4563 if (bond->params.mode == BOND_MODE_8023AD &&
4564 delayed_work_pending(&bond->ad_work))
4565 cancel_delayed_work(&bond->ad_work);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00004566
4567 if (delayed_work_pending(&bond->mcast_work))
4568 cancel_delayed_work(&bond->mcast_work);
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004569}
4570
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004571/*
4572* Destroy a bonding device.
4573* Must be under rtnl_lock when this function is called.
4574*/
4575static void bond_uninit(struct net_device *bond_dev)
Jay Vosburgha434e432008-10-30 17:41:15 -07004576{
Wang Chen454d7c92008-11-12 23:37:49 -08004577 struct bonding *bond = netdev_priv(bond_dev);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004578 struct vlan_entry *vlan, *tmp;
Jay Vosburgha434e432008-10-30 17:41:15 -07004579
WANG Congf6dc31a2010-05-06 00:48:51 -07004580 bond_netpoll_cleanup(bond_dev);
4581
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004582 /* Release the bonded slaves */
4583 bond_release_all(bond_dev);
4584
Jay Vosburgha434e432008-10-30 17:41:15 -07004585 list_del(&bond->bond_list);
4586
4587 bond_work_cancel_all(bond);
4588
Jay Vosburgha434e432008-10-30 17:41:15 -07004589 bond_remove_proc_entry(bond);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004590
Taku Izumif073c7c2010-12-09 15:17:13 +00004591 bond_debug_unregister(bond);
4592
Jiri Pirko22bedad32010-04-01 21:22:57 +00004593 __hw_addr_flush(&bond->mc_list);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004594
4595 list_for_each_entry_safe(vlan, tmp, &bond->vlan_list, vlan_list) {
4596 list_del(&vlan->vlan_list);
4597 kfree(vlan);
4598 }
Jay Vosburgha434e432008-10-30 17:41:15 -07004599}
4600
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601/*------------------------- Module initialization ---------------------------*/
4602
4603/*
4604 * Convert string input module parms. Accept either the
Jay Vosburghece95f72008-01-17 16:25:01 -08004605 * number of the mode or its string name. A bit complicated because
4606 * some mode names are substrings of other names, and calls from sysfs
4607 * may have whitespace in the name (trailing newlines, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 */
Holger Eitzenberger325dcf72008-12-09 23:10:17 -08004609int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610{
Hannes Eder54b87322009-02-14 11:15:49 +00004611 int modeint = -1, i, rv;
Jay Vosburgha42e5342008-01-29 18:07:43 -08004612 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
Jay Vosburghece95f72008-01-17 16:25:01 -08004613
Jay Vosburgha42e5342008-01-29 18:07:43 -08004614 for (p = (char *)buf; *p; p++)
4615 if (!(isdigit(*p) || isspace(*p)))
4616 break;
4617
4618 if (*p)
Jay Vosburghece95f72008-01-17 16:25:01 -08004619 rv = sscanf(buf, "%20s", modestr);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004620 else
Hannes Eder54b87322009-02-14 11:15:49 +00004621 rv = sscanf(buf, "%d", &modeint);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004622
4623 if (!rv)
4624 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
4626 for (i = 0; tbl[i].modename; i++) {
Hannes Eder54b87322009-02-14 11:15:49 +00004627 if (modeint == tbl[i].mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 return tbl[i].mode;
Jay Vosburghece95f72008-01-17 16:25:01 -08004629 if (strcmp(modestr, tbl[i].modename) == 0)
4630 return tbl[i].mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 }
4632
4633 return -1;
4634}
4635
4636static int bond_check_params(struct bond_params *params)
4637{
Jiri Pirkoa5499522009-09-25 03:28:09 +00004638 int arp_validate_value, fail_over_mac_value, primary_reselect_value;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004639
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 /*
4641 * Convert string parameters.
4642 */
4643 if (mode) {
4644 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4645 if (bond_mode == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004646 pr_err("Error: Invalid bonding mode \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 mode == NULL ? "NULL" : mode);
4648 return -EINVAL;
4649 }
4650 }
4651
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004652 if (xmit_hash_policy) {
4653 if ((bond_mode != BOND_MODE_XOR) &&
4654 (bond_mode != BOND_MODE_8023AD)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004655 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004656 bond_mode_name(bond_mode));
4657 } else {
4658 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4659 xmit_hashtype_tbl);
4660 if (xmit_hashtype == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004661 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004662 xmit_hash_policy == NULL ? "NULL" :
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004663 xmit_hash_policy);
4664 return -EINVAL;
4665 }
4666 }
4667 }
4668
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 if (lacp_rate) {
4670 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004671 pr_info("lacp_rate param is irrelevant in mode %s\n",
4672 bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 } else {
4674 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4675 if (lacp_fast == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004676 pr_err("Error: Invalid lacp rate \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 lacp_rate == NULL ? "NULL" : lacp_rate);
4678 return -EINVAL;
4679 }
4680 }
4681 }
4682
Jay Vosburghfd989c82008-11-04 17:51:16 -08004683 if (ad_select) {
4684 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4685 if (params->ad_select == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004686 pr_err("Error: Invalid ad_select \"%s\"\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -08004687 ad_select == NULL ? "NULL" : ad_select);
4688 return -EINVAL;
4689 }
4690
4691 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004692 pr_warning("ad_select param only affects 802.3ad mode\n");
Jay Vosburghfd989c82008-11-04 17:51:16 -08004693 }
4694 } else {
4695 params->ad_select = BOND_AD_STABLE;
4696 }
4697
Nicolas de Pesloüanf5841302009-08-28 13:18:34 +00004698 if (max_bonds < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004699 pr_warning("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4700 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 max_bonds = BOND_DEFAULT_MAX_BONDS;
4702 }
4703
4704 if (miimon < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004705 pr_warning("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to %d\n",
4706 miimon, INT_MAX, BOND_LINK_MON_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 miimon = BOND_LINK_MON_INTERV;
4708 }
4709
4710 if (updelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004711 pr_warning("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4712 updelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 updelay = 0;
4714 }
4715
4716 if (downdelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004717 pr_warning("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4718 downdelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 downdelay = 0;
4720 }
4721
4722 if ((use_carrier != 0) && (use_carrier != 1)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004723 pr_warning("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
4724 use_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 use_carrier = 1;
4726 }
4727
Moni Shoua7893b242008-05-17 21:10:12 -07004728 if (num_grat_arp < 0 || num_grat_arp > 255) {
Frans Pop2381a552010-03-24 07:57:36 +00004729 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 -08004730 num_grat_arp);
Moni Shoua7893b242008-05-17 21:10:12 -07004731 num_grat_arp = 1;
4732 }
4733
Brian Haley305d5522008-11-04 17:51:14 -08004734 if (num_unsol_na < 0 || num_unsol_na > 255) {
Frans Pop2381a552010-03-24 07:57:36 +00004735 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 -08004736 num_unsol_na);
Brian Haley305d5522008-11-04 17:51:14 -08004737 num_unsol_na = 1;
4738 }
4739
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 /* reset values for 802.3ad */
4741 if (bond_mode == BOND_MODE_8023AD) {
4742 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004743 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 +00004744 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 miimon = 100;
4746 }
4747 }
4748
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004749 if (tx_queues < 1 || tx_queues > 255) {
4750 pr_warning("Warning: tx_queues (%d) should be between "
4751 "1 and 255, resetting to %d\n",
4752 tx_queues, BOND_DEFAULT_TX_QUEUES);
4753 tx_queues = BOND_DEFAULT_TX_QUEUES;
4754 }
4755
Andy Gospodarekebd8e492010-06-02 08:39:21 +00004756 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
4757 pr_warning("Warning: all_slaves_active module parameter (%d), "
4758 "not of valid value (0/1), so it was set to "
4759 "0\n", all_slaves_active);
4760 all_slaves_active = 0;
4761 }
4762
Flavio Leitnerc2952c32010-10-05 14:23:59 +00004763 if (resend_igmp < 0 || resend_igmp > 255) {
4764 pr_warning("Warning: resend_igmp (%d) should be between "
4765 "0 and 255, resetting to %d\n",
4766 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
4767 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
4768 }
4769
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 /* reset values for TLB/ALB */
4771 if ((bond_mode == BOND_MODE_TLB) ||
4772 (bond_mode == BOND_MODE_ALB)) {
4773 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004774 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 +00004775 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 miimon = 100;
4777 }
4778 }
4779
4780 if (bond_mode == BOND_MODE_ALB) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004781 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",
4782 updelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 }
4784
4785 if (!miimon) {
4786 if (updelay || downdelay) {
4787 /* just warn the user the up/down delay will have
4788 * no effect since miimon is zero...
4789 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004790 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",
4791 updelay, downdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 }
4793 } else {
4794 /* don't allow arp monitoring */
4795 if (arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004796 pr_warning("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
4797 miimon, arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 arp_interval = 0;
4799 }
4800
4801 if ((updelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004802 pr_warning("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
4803 updelay, miimon,
4804 (updelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 }
4806
4807 updelay /= miimon;
4808
4809 if ((downdelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004810 pr_warning("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
4811 downdelay, miimon,
4812 (downdelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 }
4814
4815 downdelay /= miimon;
4816 }
4817
4818 if (arp_interval < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004819 pr_warning("Warning: arp_interval module parameter (%d) , not in range 0-%d, so it was reset to %d\n",
4820 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 arp_interval = BOND_LINK_ARP_INTERV;
4822 }
4823
4824 for (arp_ip_count = 0;
4825 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4826 arp_ip_count++) {
4827 /* not complete check, but should be good enough to
4828 catch mistakes */
4829 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004830 pr_warning("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
4831 arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 arp_interval = 0;
4833 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04004834 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 arp_target[arp_ip_count] = ip;
4836 }
4837 }
4838
4839 if (arp_interval && !arp_ip_count) {
4840 /* don't allow arping if no arp_ip_target given... */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004841 pr_warning("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
4842 arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 arp_interval = 0;
4844 }
4845
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004846 if (arp_validate) {
4847 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004848 pr_err("arp_validate only supported in active-backup mode\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004849 return -EINVAL;
4850 }
4851 if (!arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004852 pr_err("arp_validate requires arp_interval\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004853 return -EINVAL;
4854 }
4855
4856 arp_validate_value = bond_parse_parm(arp_validate,
4857 arp_validate_tbl);
4858 if (arp_validate_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004859 pr_err("Error: invalid arp_validate \"%s\"\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004860 arp_validate == NULL ? "NULL" : arp_validate);
4861 return -EINVAL;
4862 }
4863 } else
4864 arp_validate_value = 0;
4865
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 if (miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004867 pr_info("MII link monitoring set to %d ms\n", miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 } else if (arp_interval) {
4869 int i;
4870
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004871 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
4872 arp_interval,
4873 arp_validate_tbl[arp_validate_value].modename,
4874 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875
4876 for (i = 0; i < arp_ip_count; i++)
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00004877 pr_info(" %s", arp_ip_target[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00004879 pr_info("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
Jay Vosburghb8a97872008-06-13 18:12:04 -07004881 } else if (max_bonds) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 /* miimon and arp_interval not set, we need one so things
4883 * work as expected, see bonding.txt for details
4884 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004885 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 -07004886 }
4887
4888 if (primary && !USES_PRIMARY(bond_mode)) {
4889 /* currently, using a primary only makes sense
4890 * in active backup, TLB or ALB modes
4891 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004892 pr_warning("Warning: %s primary device specified but has no effect in %s mode\n",
4893 primary, bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 primary = NULL;
4895 }
4896
Jiri Pirkoa5499522009-09-25 03:28:09 +00004897 if (primary && primary_reselect) {
4898 primary_reselect_value = bond_parse_parm(primary_reselect,
4899 pri_reselect_tbl);
4900 if (primary_reselect_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004901 pr_err("Error: Invalid primary_reselect \"%s\"\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00004902 primary_reselect ==
4903 NULL ? "NULL" : primary_reselect);
4904 return -EINVAL;
4905 }
4906 } else {
4907 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
4908 }
4909
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004910 if (fail_over_mac) {
4911 fail_over_mac_value = bond_parse_parm(fail_over_mac,
4912 fail_over_mac_tbl);
4913 if (fail_over_mac_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004914 pr_err("Error: invalid fail_over_mac \"%s\"\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004915 arp_validate == NULL ? "NULL" : arp_validate);
4916 return -EINVAL;
4917 }
4918
4919 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004920 pr_warning("Warning: fail_over_mac only affects active-backup mode.\n");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004921 } else {
4922 fail_over_mac_value = BOND_FOM_NONE;
4923 }
Jay Vosburghdd957c52007-10-09 19:57:24 -07004924
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 /* fill params struct with the proper values */
4926 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004927 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 params->miimon = miimon;
Moni Shoua7893b242008-05-17 21:10:12 -07004929 params->num_grat_arp = num_grat_arp;
Brian Haley305d5522008-11-04 17:51:14 -08004930 params->num_unsol_na = num_unsol_na;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004932 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 params->updelay = updelay;
4934 params->downdelay = downdelay;
4935 params->use_carrier = use_carrier;
4936 params->lacp_fast = lacp_fast;
4937 params->primary[0] = 0;
Jiri Pirkoa5499522009-09-25 03:28:09 +00004938 params->primary_reselect = primary_reselect_value;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004939 params->fail_over_mac = fail_over_mac_value;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004940 params->tx_queues = tx_queues;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00004941 params->all_slaves_active = all_slaves_active;
Flavio Leitnerc2952c32010-10-05 14:23:59 +00004942 params->resend_igmp = resend_igmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
4944 if (primary) {
4945 strncpy(params->primary, primary, IFNAMSIZ);
4946 params->primary[IFNAMSIZ - 1] = 0;
4947 }
4948
4949 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4950
4951 return 0;
4952}
4953
Peter Zijlstra0daa23032006-11-08 19:51:01 -08004954static struct lock_class_key bonding_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -07004955static struct lock_class_key bonding_netdev_addr_lock_key;
Peter Zijlstra0daa23032006-11-08 19:51:01 -08004956
David S. Millere8a04642008-07-17 00:34:19 -07004957static void bond_set_lockdep_class_one(struct net_device *dev,
4958 struct netdev_queue *txq,
4959 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -07004960{
4961 lockdep_set_class(&txq->_xmit_lock,
4962 &bonding_netdev_xmit_lock_key);
4963}
4964
4965static void bond_set_lockdep_class(struct net_device *dev)
4966{
David S. Millercf508b12008-07-22 14:16:42 -07004967 lockdep_set_class(&dev->addr_list_lock,
4968 &bonding_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -07004969 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -07004970}
4971
Stephen Hemminger181470f2009-06-12 19:02:52 +00004972/*
4973 * Called from registration process
4974 */
4975static int bond_init(struct net_device *bond_dev)
4976{
4977 struct bonding *bond = netdev_priv(bond_dev);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004978 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Stephen Hemminger181470f2009-06-12 19:02:52 +00004979
4980 pr_debug("Begin bond_init for %s\n", bond_dev->name);
4981
4982 bond->wq = create_singlethread_workqueue(bond_dev->name);
4983 if (!bond->wq)
4984 return -ENOMEM;
4985
4986 bond_set_lockdep_class(bond_dev);
4987
Stephen Hemminger181470f2009-06-12 19:02:52 +00004988 bond_create_proc_entry(bond);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004989 list_add_tail(&bond->bond_list, &bn->dev_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00004990
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00004991 bond_prepare_sysfs_group(bond);
Jiri Pirko22bedad32010-04-01 21:22:57 +00004992
Taku Izumif073c7c2010-12-09 15:17:13 +00004993 bond_debug_register(bond);
4994
Jiri Pirko22bedad32010-04-01 21:22:57 +00004995 __hw_addr_init(&bond->mc_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00004996 return 0;
4997}
4998
Eric W. Biederman88ead972009-10-29 14:18:25 +00004999static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
5000{
5001 if (tb[IFLA_ADDRESS]) {
5002 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
5003 return -EINVAL;
5004 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
5005 return -EADDRNOTAVAIL;
5006 }
5007 return 0;
5008}
5009
5010static struct rtnl_link_ops bond_link_ops __read_mostly = {
5011 .kind = "bond",
Eric W. Biederman66391042009-10-29 23:58:54 +00005012 .priv_size = sizeof(struct bonding),
Eric W. Biederman88ead972009-10-29 14:18:25 +00005013 .setup = bond_setup,
5014 .validate = bond_validate,
5015};
5016
Mitch Williamsdfe60392005-11-09 10:36:04 -08005017/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005018 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08005019 * Caller must NOT hold rtnl_lock; we need to release it here before we
5020 * set up our sysfs entries.
5021 */
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005022int bond_create(struct net *net, const char *name)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005023{
5024 struct net_device *bond_dev;
5025 int res;
5026
5027 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08005028
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005029 bond_dev = alloc_netdev_mq(sizeof(struct bonding), name ? name : "",
5030 bond_setup, tx_queues);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005031 if (!bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005032 pr_err("%s: eek! can't alloc netdev!\n", name);
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005033 rtnl_unlock();
5034 return -ENOMEM;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005035 }
5036
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005037 dev_net_set(bond_dev, net);
Eric W. Biederman88ead972009-10-29 14:18:25 +00005038 bond_dev->rtnl_link_ops = &bond_link_ops;
5039
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005040 if (!name) {
5041 res = dev_alloc_name(bond_dev, "bond%d");
5042 if (res < 0)
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005043 goto out;
Neil Horman27e6f062010-10-05 03:39:21 +00005044 } else {
5045 /*
5046 * If we're given a name to register
5047 * we need to ensure that its not already
5048 * registered
5049 */
5050 res = -EEXIST;
5051 if (__dev_get_by_name(net, name) != NULL)
5052 goto out;
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005053 }
5054
Mitch Williamsdfe60392005-11-09 10:36:04 -08005055 res = register_netdevice(bond_dev);
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005056
Phil Oestere826eaf2011-03-14 06:22:05 +00005057 netif_carrier_off(bond_dev);
5058
Eric W. Biederman30c15ba2009-10-29 14:18:23 +00005059out:
Mitch Williamsdfe60392005-11-09 10:36:04 -08005060 rtnl_unlock();
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005061 if (res < 0)
5062 bond_destructor(bond_dev);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005063 return res;
5064}
5065
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005066static int __net_init bond_net_init(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005067{
Eric W. Biederman15449742009-11-29 15:46:04 +00005068 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005069
5070 bn->net = net;
5071 INIT_LIST_HEAD(&bn->dev_list);
5072
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005073 bond_create_proc_dir(bn);
Eric W. Biederman15449742009-11-29 15:46:04 +00005074
5075 return 0;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005076}
5077
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005078static void __net_exit bond_net_exit(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005079{
Eric W. Biederman15449742009-11-29 15:46:04 +00005080 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005081
5082 bond_destroy_proc_dir(bn);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005083}
5084
5085static struct pernet_operations bond_net_ops = {
5086 .init = bond_net_init,
5087 .exit = bond_net_exit,
Eric W. Biederman15449742009-11-29 15:46:04 +00005088 .id = &bond_net_id,
5089 .size = sizeof(struct bond_net),
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005090};
5091
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092static int __init bonding_init(void)
5093{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 int i;
5095 int res;
5096
Amerigo Wangbd33acc2011-03-06 21:58:46 +00005097 pr_info("%s", bond_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098
Mitch Williamsdfe60392005-11-09 10:36:04 -08005099 res = bond_check_params(&bonding_defaults);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005100 if (res)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005101 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102
Eric W. Biederman15449742009-11-29 15:46:04 +00005103 res = register_pernet_subsys(&bond_net_ops);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005104 if (res)
5105 goto out;
Jay Vosburgh027ea042008-01-17 16:25:02 -08005106
Eric W. Biederman88ead972009-10-29 14:18:25 +00005107 res = rtnl_link_register(&bond_link_ops);
5108 if (res)
Eric W. Biederman66391042009-10-29 23:58:54 +00005109 goto err_link;
Eric W. Biederman88ead972009-10-29 14:18:25 +00005110
Taku Izumif073c7c2010-12-09 15:17:13 +00005111 bond_create_debugfs();
5112
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 for (i = 0; i < max_bonds; i++) {
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005114 res = bond_create(&init_net, NULL);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005115 if (res)
5116 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 }
5118
Mitch Williamsb76cdba2005-11-09 10:36:41 -08005119 res = bond_create_sysfs();
5120 if (res)
5121 goto err;
5122
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005124 register_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005125 bond_register_ipv6_notifier();
Mitch Williamsdfe60392005-11-09 10:36:04 -08005126out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 return res;
Eric W. Biederman88ead972009-10-29 14:18:25 +00005128err:
5129 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman66391042009-10-29 23:58:54 +00005130err_link:
Eric W. Biederman15449742009-11-29 15:46:04 +00005131 unregister_pernet_subsys(&bond_net_ops);
Eric W. Biederman88ead972009-10-29 14:18:25 +00005132 goto out;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005133
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134}
5135
5136static void __exit bonding_exit(void)
5137{
5138 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04005139 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Brian Haley305d5522008-11-04 17:51:14 -08005140 bond_unregister_ipv6_notifier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005142 bond_destroy_sysfs();
Taku Izumif073c7c2010-12-09 15:17:13 +00005143 bond_destroy_debugfs();
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005144
Eric W. Biederman88ead972009-10-29 14:18:25 +00005145 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman15449742009-11-29 15:46:04 +00005146 unregister_pernet_subsys(&bond_net_ops);
Neil Hormane843fa52010-10-13 16:01:50 +00005147
5148#ifdef CONFIG_NET_POLL_CONTROLLER
Neil Hormanfb4fa762010-12-06 09:05:50 +00005149 /*
5150 * Make sure we don't have an imbalance on our netpoll blocking
5151 */
5152 WARN_ON(atomic_read(&netpoll_block_tx));
Neil Hormane843fa52010-10-13 16:01:50 +00005153#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154}
5155
5156module_init(bonding_init);
5157module_exit(bonding_exit);
5158MODULE_LICENSE("GPL");
5159MODULE_VERSION(DRV_VERSION);
5160MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
5161MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
Eric W. Biederman88ead972009-10-29 14:18:25 +00005162MODULE_ALIAS_RTNL_LINK("bond");