blob: 66d9dc6e5cacd0b5197234a2ea7cd426252661a3 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int miimon = BOND_LINK_MON_INTERV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +000093static int updelay;
94static int downdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static int use_carrier = 1;
Stephen Hemminger3d632c32009-06-12 19:02:48 +000096static char *mode;
97static char *primary;
Jiri Pirkoa5499522009-09-25 03:28:09 +000098static char *primary_reselect;
Stephen Hemminger3d632c32009-06-12 19:02:48 +000099static char *lacp_rate;
100static char *ad_select;
101static char *xmit_hash_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static int arp_interval = BOND_LINK_ARP_INTERV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000103static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
104static char *arp_validate;
105static char *fail_over_mac;
Andy Gospodarekebd8e492010-06-02 08:39:21 +0000106static int all_slaves_active = 0;
Stephen Hemmingerd2991f72009-06-12 19:02:44 +0000107static struct bond_params bonding_defaults;
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000108static int resend_igmp = BOND_DEFAULT_RESEND_IGMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110module_param(max_bonds, int, 0);
111MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
Andy Gospodarekbb1d9122010-06-02 08:40:18 +0000112module_param(tx_queues, int, 0);
113MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114module_param(miimon, int, 0);
115MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
116module_param(updelay, int, 0);
117MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
118module_param(downdelay, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800119MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
120 "in milliseconds");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121module_param(use_carrier, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800122MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
123 "0 for off, 1 for on (default)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124module_param(mode, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800125MODULE_PARM_DESC(mode, "Mode of operation : 0 for balance-rr, "
126 "1 for active-backup, 2 for balance-xor, "
127 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
128 "6 for balance-alb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129module_param(primary, charp, 0);
130MODULE_PARM_DESC(primary, "Primary network device to use");
Jiri Pirkoa5499522009-09-25 03:28:09 +0000131module_param(primary_reselect, charp, 0);
132MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
133 "once it comes up; "
134 "0 for always (default), "
135 "1 for only if speed of primary is "
136 "better, "
137 "2 for only on active slave "
138 "failure");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139module_param(lacp_rate, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800140MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner "
141 "(slow/fast)");
Jay Vosburghfd989c82008-11-04 17:51:16 -0800142module_param(ad_select, charp, 0);
143MODULE_PARM_DESC(ad_select, "803.ad aggregation selection logic: stable (0, default), bandwidth (1), count (2)");
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400144module_param(xmit_hash_policy, charp, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800145MODULE_PARM_DESC(xmit_hash_policy, "XOR hashing method: 0 for layer 2 (default)"
146 ", 1 for layer 3+4");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147module_param(arp_interval, int, 0);
148MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
149module_param_array(arp_ip_target, charp, NULL, 0);
150MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700151module_param(arp_validate, charp, 0);
152MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes: none (default), active, backup or all");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700153module_param(fail_over_mac, charp, 0);
154MODULE_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 +0000155module_param(all_slaves_active, int, 0);
156MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface"
157 "by setting active flag for all slaves. "
158 "0 for never (default), 1 for always.");
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000159module_param(resend_igmp, int, 0);
160MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on link failure");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162/*----------------------------- Global variables ----------------------------*/
163
Neil Hormane843fa52010-10-13 16:01:50 +0000164#ifdef CONFIG_NET_POLL_CONTROLLER
Neil Hormanfb4fa762010-12-06 09:05:50 +0000165atomic_t netpoll_block_tx = ATOMIC_INIT(0);
Neil Hormane843fa52010-10-13 16:01:50 +0000166#endif
167
Eric Dumazetf99189b2009-11-17 10:42:49 +0000168int bond_net_id __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000170static __be32 arp_target[BOND_MAX_ARP_TARGETS];
171static int arp_ip_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static int bond_mode = BOND_MODE_ROUNDROBIN;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000173static int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
174static int lacp_fast;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800176const struct bond_parm_tbl bond_lacp_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{ "slow", AD_LACP_SLOW},
178{ "fast", AD_LACP_FAST},
179{ NULL, -1},
180};
181
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800182const struct bond_parm_tbl bond_mode_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{ "balance-rr", BOND_MODE_ROUNDROBIN},
184{ "active-backup", BOND_MODE_ACTIVEBACKUP},
185{ "balance-xor", BOND_MODE_XOR},
186{ "broadcast", BOND_MODE_BROADCAST},
187{ "802.3ad", BOND_MODE_8023AD},
188{ "balance-tlb", BOND_MODE_TLB},
189{ "balance-alb", BOND_MODE_ALB},
190{ NULL, -1},
191};
192
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800193const struct bond_parm_tbl xmit_hashtype_tbl[] = {
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400194{ "layer2", BOND_XMIT_POLICY_LAYER2},
195{ "layer3+4", BOND_XMIT_POLICY_LAYER34},
Jay Vosburgh6f6652b2007-12-06 23:40:34 -0800196{ "layer2+3", BOND_XMIT_POLICY_LAYER23},
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400197{ NULL, -1},
198};
199
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800200const struct bond_parm_tbl arp_validate_tbl[] = {
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700201{ "none", BOND_ARP_VALIDATE_NONE},
202{ "active", BOND_ARP_VALIDATE_ACTIVE},
203{ "backup", BOND_ARP_VALIDATE_BACKUP},
204{ "all", BOND_ARP_VALIDATE_ALL},
205{ NULL, -1},
206};
207
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800208const struct bond_parm_tbl fail_over_mac_tbl[] = {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700209{ "none", BOND_FOM_NONE},
210{ "active", BOND_FOM_ACTIVE},
211{ "follow", BOND_FOM_FOLLOW},
212{ NULL, -1},
213};
214
Jiri Pirkoa5499522009-09-25 03:28:09 +0000215const struct bond_parm_tbl pri_reselect_tbl[] = {
216{ "always", BOND_PRI_RESELECT_ALWAYS},
217{ "better", BOND_PRI_RESELECT_BETTER},
218{ "failure", BOND_PRI_RESELECT_FAILURE},
219{ NULL, -1},
220};
221
Jay Vosburghfd989c82008-11-04 17:51:16 -0800222struct bond_parm_tbl ad_select_tbl[] = {
223{ "stable", BOND_AD_STABLE},
224{ "bandwidth", BOND_AD_BANDWIDTH},
225{ "count", BOND_AD_COUNT},
226{ NULL, -1},
227};
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/*-------------------------- Forward declarations ---------------------------*/
230
Stephen Hemminger181470f2009-06-12 19:02:52 +0000231static int bond_init(struct net_device *bond_dev);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +0000232static void bond_uninit(struct net_device *bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234/*---------------------------- General routines -----------------------------*/
235
Amerigo Wangbd33acc2011-03-06 21:58:46 +0000236const char *bond_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800238 static const char *names[] = {
239 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
240 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
241 [BOND_MODE_XOR] = "load balancing (xor)",
242 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000243 [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800244 [BOND_MODE_TLB] = "transmit load balancing",
245 [BOND_MODE_ALB] = "adaptive load balancing",
246 };
247
248 if (mode < 0 || mode > BOND_MODE_ALB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return "unknown";
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800250
251 return names[mode];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254/*---------------------------------- VLAN -----------------------------------*/
255
256/**
257 * bond_add_vlan - add a new vlan id on bond
258 * @bond: bond that got the notification
259 * @vlan_id: the vlan id to add
260 *
261 * Returns -ENOMEM if allocation failed.
262 */
263static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
264{
265 struct vlan_entry *vlan;
266
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800267 pr_debug("bond: %s, vlan id %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800268 (bond ? bond->dev->name : "None"), vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Brian Haley305d5522008-11-04 17:51:14 -0800270 vlan = kzalloc(sizeof(struct vlan_entry), GFP_KERNEL);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000271 if (!vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 INIT_LIST_HEAD(&vlan->vlan_list);
275 vlan->vlan_id = vlan_id;
276
277 write_lock_bh(&bond->lock);
278
279 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
280
281 write_unlock_bh(&bond->lock);
282
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800283 pr_debug("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 return 0;
286}
287
288/**
289 * bond_del_vlan - delete a vlan id from bond
290 * @bond: bond that got the notification
291 * @vlan_id: the vlan id to delete
292 *
293 * returns -ENODEV if @vlan_id was not found in @bond.
294 */
295static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
296{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700297 struct vlan_entry *vlan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int res = -ENODEV;
299
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800300 pr_debug("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Neil Hormane843fa52010-10-13 16:01:50 +0000302 block_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 write_lock_bh(&bond->lock);
304
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700305 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (vlan->vlan_id == vlan_id) {
307 list_del(&vlan->vlan_list);
308
Holger Eitzenberger58402052008-12-09 23:07:13 -0800309 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 bond_alb_clear_vlan(bond, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800312 pr_debug("removed VLAN ID %d from bond %s\n",
313 vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 kfree(vlan);
316
317 if (list_empty(&bond->vlan_list) &&
318 (bond->slave_cnt == 0)) {
319 /* Last VLAN removed and no slaves, so
320 * restore block on adding VLANs. This will
321 * be removed once new slaves that are not
322 * VLAN challenged will be added.
323 */
324 bond->dev->features |= NETIF_F_VLAN_CHALLENGED;
325 }
326
327 res = 0;
328 goto out;
329 }
330 }
331
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800332 pr_debug("couldn't find VLAN ID %d in bond %s\n",
333 vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335out:
336 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +0000337 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return res;
339}
340
341/**
342 * bond_has_challenged_slaves
343 * @bond: the bond we're working on
344 *
345 * Searches the slave list. Returns 1 if a vlan challenged slave
346 * was found, 0 otherwise.
347 *
348 * Assumes bond->lock is held.
349 */
350static int bond_has_challenged_slaves(struct bonding *bond)
351{
352 struct slave *slave;
353 int i;
354
355 bond_for_each_slave(bond, slave, i) {
356 if (slave->dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800357 pr_debug("found VLAN challenged slave - %s\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800358 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return 1;
360 }
361 }
362
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800363 pr_debug("no VLAN challenged slaves found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 0;
365}
366
367/**
368 * bond_next_vlan - safely skip to the next item in the vlans list.
369 * @bond: the bond we're working on
370 * @curr: item we're advancing from
371 *
372 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
373 * or @curr->next otherwise (even if it is @curr itself again).
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000374 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 * Caller must hold bond->lock
376 */
377struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
378{
379 struct vlan_entry *next, *last;
380
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000381 if (list_empty(&bond->vlan_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 if (!curr) {
385 next = list_entry(bond->vlan_list.next,
386 struct vlan_entry, vlan_list);
387 } else {
388 last = list_entry(bond->vlan_list.prev,
389 struct vlan_entry, vlan_list);
390 if (last == curr) {
391 next = list_entry(bond->vlan_list.next,
392 struct vlan_entry, vlan_list);
393 } else {
394 next = list_entry(curr->vlan_list.next,
395 struct vlan_entry, vlan_list);
396 }
397 }
398
399 return next;
400}
401
402/**
403 * bond_dev_queue_xmit - Prepare skb for xmit.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000404 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 * @bond: bond device that got this skb for tx.
406 * @skb: hw accel VLAN tagged skb to transmit
407 * @slave_dev: slave that is supposed to xmit this skbuff
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000409int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
410 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Ben Hutchings83874512010-12-13 08:19:28 +0000412 skb->dev = slave_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 skb->priority = 1;
Amerigo Wang080e4132011-02-17 23:43:33 +0000414 if (unlikely(netpoll_tx_running(slave_dev)))
Amerigo Wang8a8efa22011-02-17 23:43:32 +0000415 bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
Amerigo Wang080e4132011-02-17 23:43:33 +0000416 else
WANG Congf6dc31a2010-05-06 00:48:51 -0700417 dev_queue_xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 return 0;
420}
421
422/*
423 * In the following 3 functions, bond_vlan_rx_register(), bond_vlan_rx_add_vid
424 * and bond_vlan_rx_kill_vid, We don't protect the slave list iteration with a
425 * lock because:
426 * a. This operation is performed in IOCTL context,
427 * b. The operation is protected by the RTNL semaphore in the 8021q code,
428 * c. Holding a lock with BH disabled while directly calling a base driver
429 * entry point is generally a BAD idea.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000430 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * The design of synchronization/protection for this operation in the 8021q
432 * module is good for one or more VLAN devices over a single physical device
433 * and cannot be extended for a teaming solution like bonding, so there is a
434 * potential race condition here where a net device from the vlan group might
435 * be referenced (either by a base driver or the 8021q code) while it is being
436 * removed from the system. However, it turns out we're not making matters
437 * worse, and if it works for regular VLAN usage it will work here too.
438*/
439
440/**
441 * bond_vlan_rx_register - Propagates registration to slaves
442 * @bond_dev: bonding net device that got called
443 * @grp: vlan group being registered
444 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000445static void bond_vlan_rx_register(struct net_device *bond_dev,
446 struct vlan_group *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Wang Chen454d7c92008-11-12 23:37:49 -0800448 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct slave *slave;
450 int i;
451
Jarek Poplawskia71fb882010-10-27 07:08:22 +0000452 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 bond->vlgrp = grp;
Jarek Poplawskia71fb882010-10-27 07:08:22 +0000454 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 bond_for_each_slave(bond, slave, i) {
457 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800458 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800461 slave_ops->ndo_vlan_rx_register) {
462 slave_ops->ndo_vlan_rx_register(slave_dev, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464 }
465}
466
467/**
468 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
469 * @bond_dev: bonding net device that got called
470 * @vid: vlan id being added
471 */
472static void bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
473{
Wang Chen454d7c92008-11-12 23:37:49 -0800474 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 struct slave *slave;
476 int i, res;
477
478 bond_for_each_slave(bond, slave, i) {
479 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800480 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800483 slave_ops->ndo_vlan_rx_add_vid) {
484 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486 }
487
488 res = bond_add_vlan(bond, vid);
489 if (res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800490 pr_err("%s: Error: Failed to add vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 bond_dev->name, vid);
492 }
493}
494
495/**
496 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
497 * @bond_dev: bonding net device that got called
498 * @vid: vlan id being removed
499 */
500static void bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
501{
Wang Chen454d7c92008-11-12 23:37:49 -0800502 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct slave *slave;
504 struct net_device *vlan_dev;
505 int i, res;
506
507 bond_for_each_slave(bond, slave, i) {
508 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800509 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800512 slave_ops->ndo_vlan_rx_kill_vid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /* Save and then restore vlan_dev in the grp array,
514 * since the slave's driver might clear it.
515 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800516 vlan_dev = vlan_group_get_device(bond->vlgrp, vid);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800517 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vid);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800518 vlan_group_set_device(bond->vlgrp, vid, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 }
521
522 res = bond_del_vlan(bond, vid);
523 if (res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800524 pr_err("%s: Error: Failed to remove vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 bond_dev->name, vid);
526 }
527}
528
529static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
530{
531 struct vlan_entry *vlan;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800532 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Jay Vosburghf35188f2010-07-21 12:14:47 +0000534 if (!bond->vlgrp)
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000535 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800538 slave_ops->ndo_vlan_rx_register)
539 slave_ops->ndo_vlan_rx_register(slave_dev, bond->vlgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800542 !(slave_ops->ndo_vlan_rx_add_vid))
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000543 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800545 list_for_each_entry(vlan, &bond->vlan_list, vlan_list)
546 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vlan->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000549static void bond_del_vlans_from_slave(struct bonding *bond,
550 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800552 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 struct vlan_entry *vlan;
554 struct net_device *vlan_dev;
555
Jay Vosburghf35188f2010-07-21 12:14:47 +0000556 if (!bond->vlgrp)
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000557 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800560 !(slave_ops->ndo_vlan_rx_kill_vid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 goto unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +0000564 if (!vlan->vlan_id)
565 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /* Save and then restore vlan_dev in the grp array,
567 * since the slave's driver might clear it.
568 */
Dan Aloni5c15bde2007-03-02 20:44:51 -0800569 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800570 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
Dan Aloni5c15bde2007-03-02 20:44:51 -0800571 vlan_group_set_device(bond->vlgrp, vlan->vlan_id, vlan_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573
574unreg:
575 if ((slave_dev->features & NETIF_F_HW_VLAN_RX) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800576 slave_ops->ndo_vlan_rx_register)
577 slave_ops->ndo_vlan_rx_register(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
580/*------------------------------- Link status -------------------------------*/
581
582/*
Jay Vosburghff59c452006-03-27 13:27:43 -0800583 * Set the carrier state for the master according to the state of its
584 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
585 * do special 802.3ad magic.
586 *
587 * Returns zero if carrier state does not change, nonzero if it does.
588 */
589static int bond_set_carrier(struct bonding *bond)
590{
591 struct slave *slave;
592 int i;
593
594 if (bond->slave_cnt == 0)
595 goto down;
596
597 if (bond->params.mode == BOND_MODE_8023AD)
598 return bond_3ad_set_carrier(bond);
599
600 bond_for_each_slave(bond, slave, i) {
601 if (slave->link == BOND_LINK_UP) {
602 if (!netif_carrier_ok(bond->dev)) {
603 netif_carrier_on(bond->dev);
604 return 1;
605 }
606 return 0;
607 }
608 }
609
610down:
611 if (netif_carrier_ok(bond->dev)) {
612 netif_carrier_off(bond->dev);
613 return 1;
614 }
615 return 0;
616}
617
618/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 * Get link speed and duplex from the slave's base driver
620 * using ethtool. If for some reason the call fails or the
621 * values are invalid, fake speed and duplex to 100/Full
622 * and return error.
623 */
624static int bond_update_speed_duplex(struct slave *slave)
625{
626 struct net_device *slave_dev = slave->dev;
David Decotigny5d305302011-04-13 15:22:31 +0000627 struct ethtool_cmd etool = { .cmd = ETHTOOL_GSET };
628 u32 slave_speed;
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700629 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 /* Fake speed and duplex */
632 slave->speed = SPEED_100;
633 slave->duplex = DUPLEX_FULL;
634
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700635 if (!slave_dev->ethtool_ops || !slave_dev->ethtool_ops->get_settings)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700638 res = slave_dev->ethtool_ops->get_settings(slave_dev, &etool);
639 if (res < 0)
640 return -1;
641
David Decotigny5d305302011-04-13 15:22:31 +0000642 slave_speed = ethtool_cmd_speed(&etool);
643 switch (slave_speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 case SPEED_10:
645 case SPEED_100:
646 case SPEED_1000:
Jay Vosburgh94dbffd2006-09-22 21:52:15 -0700647 case SPEED_10000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 break;
649 default:
650 return -1;
651 }
652
653 switch (etool.duplex) {
654 case DUPLEX_FULL:
655 case DUPLEX_HALF:
656 break;
657 default:
658 return -1;
659 }
660
David Decotigny5d305302011-04-13 15:22:31 +0000661 slave->speed = slave_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 slave->duplex = etool.duplex;
663
664 return 0;
665}
666
667/*
668 * if <dev> supports MII link status reporting, check its link status.
669 *
670 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000671 * depending upon the setting of the use_carrier parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 *
673 * Return either BMSR_LSTATUS, meaning that the link is up (or we
674 * can't tell and just pretend it is), or 0, meaning that the link is
675 * down.
676 *
677 * If reporting is non-zero, instead of faking link up, return -1 if
678 * both ETHTOOL and MII ioctls fail (meaning the device does not
679 * support them). If use_carrier is set, return whatever it says.
680 * It'd be nice if there was a good way to tell if a driver supports
681 * netif_carrier, but there really isn't.
682 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000683static int bond_check_dev_link(struct bonding *bond,
684 struct net_device *slave_dev, int reporting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800686 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Jiri Bohacd9d52832009-10-28 22:23:54 -0700687 int (*ioctl)(struct net_device *, struct ifreq *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct ifreq ifr;
689 struct mii_ioctl_data *mii;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Petri Gynther6c988852009-08-28 12:05:15 +0000691 if (!reporting && !netif_running(slave_dev))
692 return 0;
693
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800694 if (bond->params.use_carrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Jiri Pirko29112f42009-04-24 01:58:23 +0000697 /* Try to get link status using Ethtool first. */
698 if (slave_dev->ethtool_ops) {
699 if (slave_dev->ethtool_ops->get_link) {
700 u32 link;
701
702 link = slave_dev->ethtool_ops->get_link(slave_dev);
703
704 return link ? BMSR_LSTATUS : 0;
705 }
706 }
707
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000708 /* Ethtool can't be used, fallback to MII ioctls. */
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800709 ioctl = slave_ops->ndo_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (ioctl) {
711 /* TODO: set pointer to correct ioctl on a per team member */
712 /* bases to make this more efficient. that is, once */
713 /* we determine the correct ioctl, we will always */
714 /* call it and not the others for that team */
715 /* member. */
716
717 /*
718 * We cannot assume that SIOCGMIIPHY will also read a
719 * register; not all network drivers (e.g., e100)
720 * support that.
721 */
722
723 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
724 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
725 mii = if_mii(&ifr);
726 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
727 mii->reg_num = MII_BMSR;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000728 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0)
729 return mii->val_out & BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731 }
732
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700733 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 * If reporting, report that either there's no dev->do_ioctl,
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700735 * or both SIOCGMIIREG and get_link failed (meaning that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 * cannot report link status). If not reporting, pretend
737 * we're ok.
738 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000739 return reporting ? -1 : BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742/*----------------------------- Multicast list ------------------------------*/
743
744/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 * Push the promiscuity flag down to appropriate slaves
746 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700747static int bond_set_promiscuity(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700749 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (USES_PRIMARY(bond->params.mode)) {
751 /* write lock already acquired */
752 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700753 err = dev_set_promiscuity(bond->curr_active_slave->dev,
754 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756 } else {
757 struct slave *slave;
758 int i;
759 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700760 err = dev_set_promiscuity(slave->dev, inc);
761 if (err)
762 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700765 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
768/*
769 * Push the allmulti flag down to all slaves
770 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700771static int bond_set_allmulti(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700773 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (USES_PRIMARY(bond->params.mode)) {
775 /* write lock already acquired */
776 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700777 err = dev_set_allmulti(bond->curr_active_slave->dev,
778 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780 } else {
781 struct slave *slave;
782 int i;
783 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700784 err = dev_set_allmulti(slave->dev, inc);
785 if (err)
786 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700789 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
792/*
793 * Add a Multicast address to slaves
794 * according to mode
795 */
Jiri Pirko22bedad2010-04-01 21:22:57 +0000796static void bond_mc_add(struct bonding *bond, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
798 if (USES_PRIMARY(bond->params.mode)) {
799 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000800 if (bond->curr_active_slave)
Jiri Pirko22bedad2010-04-01 21:22:57 +0000801 dev_mc_add(bond->curr_active_slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 } else {
803 struct slave *slave;
804 int i;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000805
806 bond_for_each_slave(bond, slave, i)
Jiri Pirko22bedad2010-04-01 21:22:57 +0000807 dev_mc_add(slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809}
810
811/*
812 * Remove a multicast address from slave
813 * according to mode
814 */
Jiri Pirko22bedad2010-04-01 21:22:57 +0000815static void bond_mc_del(struct bonding *bond, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 if (USES_PRIMARY(bond->params.mode)) {
818 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000819 if (bond->curr_active_slave)
Jiri Pirko22bedad2010-04-01 21:22:57 +0000820 dev_mc_del(bond->curr_active_slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 } else {
822 struct slave *slave;
823 int i;
824 bond_for_each_slave(bond, slave, i) {
Jiri Pirko22bedad2010-04-01 21:22:57 +0000825 dev_mc_del(slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827 }
828}
829
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800830
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000831static void __bond_resend_igmp_join_requests(struct net_device *dev)
832{
833 struct in_device *in_dev;
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000834
835 rcu_read_lock();
836 in_dev = __in_dev_get_rcu(dev);
Eric Dumazet866f3b22010-11-18 09:33:19 -0800837 if (in_dev)
David S. Miller24912422010-11-19 13:13:47 -0800838 ip_mc_rejoin_groups(in_dev);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000839 rcu_read_unlock();
840}
841
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800842/*
843 * Retrieve the list of registered multicast addresses for the bonding
844 * device and retransmit an IGMP JOIN request to the current active
845 * slave.
846 */
847static void bond_resend_igmp_join_requests(struct bonding *bond)
848{
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000849 struct net_device *vlan_dev;
850 struct vlan_entry *vlan;
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800851
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000852 read_lock(&bond->lock);
853
854 /* rejoin all groups on bond device */
855 __bond_resend_igmp_join_requests(bond->dev);
856
857 /* rejoin all groups on vlan devices */
858 if (bond->vlgrp) {
859 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
860 vlan_dev = vlan_group_get_device(bond->vlgrp,
861 vlan->vlan_id);
862 if (vlan_dev)
863 __bond_resend_igmp_join_requests(vlan_dev);
864 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800865 }
866
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000867 if (--bond->igmp_retrans > 0)
868 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
869
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000870 read_unlock(&bond->lock);
871}
872
stephen hemminger379b7382010-10-15 11:02:56 +0000873static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000874{
875 struct bonding *bond = container_of(work, struct bonding,
876 mcast_work.work);
877 bond_resend_igmp_join_requests(bond);
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800878}
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 * flush all members of flush->mc_list from device dev->mc_list
882 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000883static void bond_mc_list_flush(struct net_device *bond_dev,
884 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Wang Chen454d7c92008-11-12 23:37:49 -0800886 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad2010-04-01 21:22:57 +0000887 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Jiri Pirko22bedad2010-04-01 21:22:57 +0000889 netdev_for_each_mc_addr(ha, bond_dev)
890 dev_mc_del(slave_dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 if (bond->params.mode == BOND_MODE_8023AD) {
893 /* del lacpdu mc addr from mc list */
894 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
895
Jiri Pirko22bedad2010-04-01 21:22:57 +0000896 dev_mc_del(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898}
899
900/*--------------------------- Active slave change ---------------------------*/
901
902/*
903 * Update the mc list and multicast-related flags for the new and
904 * old active slaves (if any) according to the multicast mode, and
905 * promiscuous flags unconditionally.
906 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000907static void bond_mc_swap(struct bonding *bond, struct slave *new_active,
908 struct slave *old_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Jiri Pirko22bedad2010-04-01 21:22:57 +0000910 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000912 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /* nothing to do - mc list is already up-to-date on
914 * all slaves
915 */
916 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 if (old_active) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000919 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 dev_set_promiscuity(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000922 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 dev_set_allmulti(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Jiri Pirko22bedad2010-04-01 21:22:57 +0000925 netdev_for_each_mc_addr(ha, bond->dev)
926 dev_mc_del(old_active->dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928
929 if (new_active) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700930 /* FIXME: Signal errors upstream. */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000931 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 dev_set_promiscuity(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000934 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 dev_set_allmulti(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Jiri Pirko22bedad2010-04-01 21:22:57 +0000937 netdev_for_each_mc_addr(ha, bond->dev)
938 dev_mc_add(new_active->dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940}
941
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700942/*
943 * bond_do_fail_over_mac
944 *
945 * Perform special MAC address swapping for fail_over_mac settings
946 *
947 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
948 */
949static void bond_do_fail_over_mac(struct bonding *bond,
950 struct slave *new_active,
951 struct slave *old_active)
Hannes Eder1f78d9f2009-02-14 11:15:33 +0000952 __releases(&bond->curr_slave_lock)
953 __releases(&bond->lock)
954 __acquires(&bond->lock)
955 __acquires(&bond->curr_slave_lock)
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700956{
957 u8 tmp_mac[ETH_ALEN];
958 struct sockaddr saddr;
959 int rv;
960
961 switch (bond->params.fail_over_mac) {
962 case BOND_FOM_ACTIVE:
963 if (new_active)
964 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
965 new_active->dev->addr_len);
966 break;
967 case BOND_FOM_FOLLOW:
968 /*
969 * if new_active && old_active, swap them
970 * if just old_active, do nothing (going to no active slave)
971 * if just new_active, set new_active to bond's MAC
972 */
973 if (!new_active)
974 return;
975
976 write_unlock_bh(&bond->curr_slave_lock);
977 read_unlock(&bond->lock);
978
979 if (old_active) {
980 memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
981 memcpy(saddr.sa_data, old_active->dev->dev_addr,
982 ETH_ALEN);
983 saddr.sa_family = new_active->dev->type;
984 } else {
985 memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
986 saddr.sa_family = bond->dev->type;
987 }
988
989 rv = dev_set_mac_address(new_active->dev, &saddr);
990 if (rv) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800991 pr_err("%s: Error %d setting MAC of slave %s\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700992 bond->dev->name, -rv, new_active->dev->name);
993 goto out;
994 }
995
996 if (!old_active)
997 goto out;
998
999 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
1000 saddr.sa_family = old_active->dev->type;
1001
1002 rv = dev_set_mac_address(old_active->dev, &saddr);
1003 if (rv)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001004 pr_err("%s: Error %d setting MAC of slave %s\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001005 bond->dev->name, -rv, new_active->dev->name);
1006out:
1007 read_lock(&bond->lock);
1008 write_lock_bh(&bond->curr_slave_lock);
1009 break;
1010 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001011 pr_err("%s: bond_do_fail_over_mac impossible: bad policy %d\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001012 bond->dev->name, bond->params.fail_over_mac);
1013 break;
1014 }
1015
1016}
1017
Jiri Pirkoa5499522009-09-25 03:28:09 +00001018static bool bond_should_change_active(struct bonding *bond)
1019{
1020 struct slave *prim = bond->primary_slave;
1021 struct slave *curr = bond->curr_active_slave;
1022
1023 if (!prim || !curr || curr->link != BOND_LINK_UP)
1024 return true;
1025 if (bond->force_primary) {
1026 bond->force_primary = false;
1027 return true;
1028 }
1029 if (bond->params.primary_reselect == BOND_PRI_RESELECT_BETTER &&
1030 (prim->speed < curr->speed ||
1031 (prim->speed == curr->speed && prim->duplex <= curr->duplex)))
1032 return false;
1033 if (bond->params.primary_reselect == BOND_PRI_RESELECT_FAILURE)
1034 return false;
1035 return true;
1036}
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038/**
1039 * find_best_interface - select the best available slave to be the active one
1040 * @bond: our bonding struct
1041 *
1042 * Warning: Caller must hold curr_slave_lock for writing.
1043 */
1044static struct slave *bond_find_best_slave(struct bonding *bond)
1045{
1046 struct slave *new_active, *old_active;
1047 struct slave *bestslave = NULL;
1048 int mintime = bond->params.updelay;
1049 int i;
1050
Nicolas de Pesloüan49b4ad92009-10-07 14:11:00 -07001051 new_active = bond->curr_active_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 if (!new_active) { /* there were no active slaves left */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001054 if (bond->slave_cnt > 0) /* found one slave */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 new_active = bond->first_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001056 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return NULL; /* still no slave, return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if ((bond->primary_slave) &&
Jiri Pirkoa5499522009-09-25 03:28:09 +00001061 bond->primary_slave->link == BOND_LINK_UP &&
1062 bond_should_change_active(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 new_active = bond->primary_slave;
1064 }
1065
1066 /* remember where to stop iterating over the slaves */
1067 old_active = new_active;
1068
1069 bond_for_each_slave_from(bond, new_active, i, old_active) {
Jiri Pirkob9f60252009-08-31 11:09:38 +00001070 if (new_active->link == BOND_LINK_UP) {
1071 return new_active;
1072 } else if (new_active->link == BOND_LINK_BACK &&
1073 IS_UP(new_active->dev)) {
1074 /* link up, but waiting for stabilization */
1075 if (new_active->delay < mintime) {
1076 mintime = new_active->delay;
1077 bestslave = new_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079 }
1080 }
1081
1082 return bestslave;
1083}
1084
1085/**
1086 * change_active_interface - change the active slave into the specified one
1087 * @bond: our bonding struct
1088 * @new: the new slave to make the active one
1089 *
1090 * Set the new slave to the bond's settings and unset them on the old
1091 * curr_active_slave.
1092 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1093 *
1094 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1095 * because it is apparently the best available slave we have, even though its
1096 * updelay hasn't timed out yet.
1097 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001098 * If new_active is not NULL, caller must hold bond->lock for read and
1099 * curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001101void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 struct slave *old_active = bond->curr_active_slave;
1104
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001105 if (old_active == new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 if (new_active) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07001109 new_active->jiffies = jiffies;
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (new_active->link == BOND_LINK_BACK) {
1112 if (USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001113 pr_info("%s: making interface %s the new active one %d ms earlier.\n",
1114 bond->dev->name, new_active->dev->name,
1115 (bond->params.updelay - new_active->delay) * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
1117
1118 new_active->delay = 0;
1119 new_active->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001121 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Holger Eitzenberger58402052008-12-09 23:07:13 -08001124 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 } else {
1127 if (USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001128 pr_info("%s: making interface %s the new active one.\n",
1129 bond->dev->name, new_active->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131 }
1132 }
1133
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001134 if (USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 bond_mc_swap(bond, new_active, old_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Holger Eitzenberger58402052008-12-09 23:07:13 -08001137 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 bond_alb_handle_active_change(bond, new_active);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001139 if (old_active)
1140 bond_set_slave_inactive_flags(old_active);
1141 if (new_active)
1142 bond_set_slave_active_flags(new_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 } else {
1144 bond->curr_active_slave = new_active;
1145 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001146
1147 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001148 if (old_active)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001149 bond_set_slave_inactive_flags(old_active);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001150
1151 if (new_active) {
1152 bond_set_slave_active_flags(new_active);
Moni Shoua2ab82852007-10-09 19:43:39 -07001153
Or Gerlitz709f8a42008-06-13 18:12:01 -07001154 if (bond->params.fail_over_mac)
1155 bond_do_fail_over_mac(bond, new_active,
1156 old_active);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001157
Or Gerlitz01f31092008-06-13 18:12:02 -07001158 write_unlock_bh(&bond->curr_slave_lock);
1159 read_unlock(&bond->lock);
1160
Moni Shoua75c78502009-09-15 02:37:40 -07001161 netdev_bonding_change(bond->dev, NETDEV_BONDING_FAILOVER);
Or Gerlitz01f31092008-06-13 18:12:02 -07001162
1163 read_lock(&bond->lock);
1164 write_lock_bh(&bond->curr_slave_lock);
Moni Shoua7893b242008-05-17 21:10:12 -07001165 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001166 }
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001167
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001168 /* resend IGMP joins since active slave has changed or
1169 * all were sent on curr_active_slave */
Ben Hutchingsffa95ed2010-12-13 08:19:56 +00001170 if (((USES_PRIMARY(bond->params.mode) && new_active) ||
1171 bond->params.mode == BOND_MODE_ROUNDROBIN) &&
1172 netif_running(bond->dev)) {
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001173 bond->igmp_retrans = bond->params.resend_igmp;
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001174 queue_delayed_work(bond->wq, &bond->mcast_work, 0);
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
1178/**
1179 * bond_select_active_slave - select a new active slave, if needed
1180 * @bond: our bonding struct
1181 *
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001182 * This functions should be called when one of the following occurs:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 * - The old curr_active_slave has been released or lost its link.
1184 * - The primary_slave has got its link back.
1185 * - A slave has got its link back and there's no old curr_active_slave.
1186 *
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001187 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001189void bond_select_active_slave(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 struct slave *best_slave;
Jay Vosburghff59c452006-03-27 13:27:43 -08001192 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 best_slave = bond_find_best_slave(bond);
1195 if (best_slave != bond->curr_active_slave) {
1196 bond_change_active_slave(bond, best_slave);
Jay Vosburghff59c452006-03-27 13:27:43 -08001197 rv = bond_set_carrier(bond);
1198 if (!rv)
1199 return;
1200
1201 if (netif_carrier_ok(bond->dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001202 pr_info("%s: first active interface up!\n",
1203 bond->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08001204 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001205 pr_info("%s: now running without any active interface !\n",
1206 bond->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08001207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209}
1210
1211/*--------------------------- slave list handling ---------------------------*/
1212
1213/*
1214 * This function attaches the slave to the end of list.
1215 *
1216 * bond->lock held for writing by caller.
1217 */
1218static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1219{
1220 if (bond->first_slave == NULL) { /* attaching the first slave */
1221 new_slave->next = new_slave;
1222 new_slave->prev = new_slave;
1223 bond->first_slave = new_slave;
1224 } else {
1225 new_slave->next = bond->first_slave;
1226 new_slave->prev = bond->first_slave->prev;
1227 new_slave->next->prev = new_slave;
1228 new_slave->prev->next = new_slave;
1229 }
1230
1231 bond->slave_cnt++;
1232}
1233
1234/*
1235 * This function detaches the slave from the list.
1236 * WARNING: no check is made to verify if the slave effectively
1237 * belongs to <bond>.
1238 * Nothing is freed on return, structures are just unchained.
1239 * If any slave pointer in bond was pointing to <slave>,
1240 * it should be changed by the calling function.
1241 *
1242 * bond->lock held for writing by caller.
1243 */
1244static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1245{
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001246 if (slave->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 slave->next->prev = slave->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001249 if (slave->prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 slave->prev->next = slave->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 if (bond->first_slave == slave) { /* slave is the first slave */
1253 if (bond->slave_cnt > 1) { /* there are more slave */
1254 bond->first_slave = slave->next;
1255 } else {
1256 bond->first_slave = NULL; /* slave was the last one */
1257 }
1258 }
1259
1260 slave->next = NULL;
1261 slave->prev = NULL;
1262 bond->slave_cnt--;
1263}
1264
WANG Congf6dc31a2010-05-06 00:48:51 -07001265#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001266static inline int slave_enable_netpoll(struct slave *slave)
WANG Congf6dc31a2010-05-06 00:48:51 -07001267{
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001268 struct netpoll *np;
1269 int err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001270
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001271 np = kzalloc(sizeof(*np), GFP_KERNEL);
1272 err = -ENOMEM;
1273 if (!np)
1274 goto out;
1275
1276 np->dev = slave->dev;
1277 err = __netpoll_setup(np);
1278 if (err) {
1279 kfree(np);
1280 goto out;
WANG Congf6dc31a2010-05-06 00:48:51 -07001281 }
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001282 slave->np = np;
1283out:
1284 return err;
1285}
1286static inline void slave_disable_netpoll(struct slave *slave)
1287{
1288 struct netpoll *np = slave->np;
1289
1290 if (!np)
1291 return;
1292
1293 slave->np = NULL;
1294 synchronize_rcu_bh();
1295 __netpoll_cleanup(np);
1296 kfree(np);
1297}
1298static inline bool slave_dev_support_netpoll(struct net_device *slave_dev)
1299{
1300 if (slave_dev->priv_flags & IFF_DISABLE_NETPOLL)
1301 return false;
1302 if (!slave_dev->netdev_ops->ndo_poll_controller)
1303 return false;
1304 return true;
WANG Congf6dc31a2010-05-06 00:48:51 -07001305}
1306
1307static void bond_poll_controller(struct net_device *bond_dev)
1308{
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001309}
1310
1311static void __bond_netpoll_cleanup(struct bonding *bond)
1312{
Neil Hormanc2355e12010-10-13 16:01:49 +00001313 struct slave *slave;
1314 int i;
1315
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001316 bond_for_each_slave(bond, slave, i)
1317 if (IS_UP(slave->dev))
1318 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07001319}
WANG Congf6dc31a2010-05-06 00:48:51 -07001320static void bond_netpoll_cleanup(struct net_device *bond_dev)
1321{
1322 struct bonding *bond = netdev_priv(bond_dev);
WANG Congf6dc31a2010-05-06 00:48:51 -07001323
1324 read_lock(&bond->lock);
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001325 __bond_netpoll_cleanup(bond);
WANG Congf6dc31a2010-05-06 00:48:51 -07001326 read_unlock(&bond->lock);
1327}
1328
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001329static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
1330{
1331 struct bonding *bond = netdev_priv(dev);
1332 struct slave *slave;
1333 int i, err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001334
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001335 read_lock(&bond->lock);
1336 bond_for_each_slave(bond, slave, i) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001337 err = slave_enable_netpoll(slave);
1338 if (err) {
1339 __bond_netpoll_cleanup(bond);
1340 break;
1341 }
1342 }
1343 read_unlock(&bond->lock);
1344 return err;
1345}
1346
1347static struct netpoll_info *bond_netpoll_info(struct bonding *bond)
1348{
1349 return bond->dev->npinfo;
1350}
1351
1352#else
1353static inline int slave_enable_netpoll(struct slave *slave)
1354{
1355 return 0;
1356}
1357static inline void slave_disable_netpoll(struct slave *slave)
1358{
1359}
WANG Congf6dc31a2010-05-06 00:48:51 -07001360static void bond_netpoll_cleanup(struct net_device *bond_dev)
1361{
1362}
WANG Congf6dc31a2010-05-06 00:48:51 -07001363#endif
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365/*---------------------------------- IOCTL ----------------------------------*/
1366
Adrian Bunk4ad072c2007-07-09 11:51:12 -07001367static int bond_sethwaddr(struct net_device *bond_dev,
1368 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001370 pr_debug("bond_dev=%p\n", bond_dev);
1371 pr_debug("slave_dev=%p\n", slave_dev);
1372 pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1374 return 0;
1375}
1376
Herbert Xu7f353bf2007-08-10 15:47:58 -07001377#define BOND_VLAN_FEATURES \
1378 (NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX | \
1379 NETIF_F_HW_VLAN_FILTER)
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001380
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001381/*
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001382 * Compute the common dev->feature set available to all slaves. Some
Herbert Xu7f353bf2007-08-10 15:47:58 -07001383 * feature bits are managed elsewhere, so preserve those feature bits
1384 * on the master device.
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001385 */
1386static int bond_compute_features(struct bonding *bond)
1387{
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001388 struct slave *slave;
1389 struct net_device *bond_dev = bond->dev;
Michał Mirosław04ed3e72011-01-24 15:32:47 -08001390 u32 features = bond_dev->features;
1391 u32 vlan_features = 0;
Moni Shoua3158bf72007-10-09 19:43:41 -07001392 unsigned short max_hard_header_len = max((u16)ETH_HLEN,
1393 bond_dev->hard_header_len);
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001394 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001395
Herbert Xu7f353bf2007-08-10 15:47:58 -07001396 features &= ~(NETIF_F_ALL_CSUM | BOND_VLAN_FEATURES);
Tom Herbertc6e1a0d2011-04-04 22:30:30 -07001397 features |= NETIF_F_GSO_MASK | NETIF_F_NO_CSUM | NETIF_F_NOCACHE_COPY;
Herbert Xub63365a2008-10-23 01:11:29 -07001398
1399 if (!bond->first_slave)
1400 goto done;
1401
1402 features &= ~NETIF_F_ONE_FOR_ALL;
Herbert Xu7f353bf2007-08-10 15:47:58 -07001403
Jay Vosburgh278339a2009-08-28 12:05:12 +00001404 vlan_features = bond->first_slave->dev->vlan_features;
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001405 bond_for_each_slave(bond, slave, i) {
Herbert Xub63365a2008-10-23 01:11:29 -07001406 features = netdev_increment_features(features,
1407 slave->dev->features,
1408 NETIF_F_ONE_FOR_ALL);
Jay Vosburgh278339a2009-08-28 12:05:12 +00001409 vlan_features = netdev_increment_features(vlan_features,
1410 slave->dev->vlan_features,
1411 NETIF_F_ONE_FOR_ALL);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001412 if (slave->dev->hard_header_len > max_hard_header_len)
1413 max_hard_header_len = slave->dev->hard_header_len;
1414 }
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001415
Herbert Xub63365a2008-10-23 01:11:29 -07001416done:
Herbert Xu7f353bf2007-08-10 15:47:58 -07001417 features |= (bond_dev->features & BOND_VLAN_FEATURES);
Michał Mirosławacd11302011-01-24 15:45:15 -08001418 bond_dev->features = netdev_fix_features(bond_dev, features);
1419 bond_dev->vlan_features = netdev_fix_features(bond_dev, vlan_features);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001420 bond_dev->hard_header_len = max_hard_header_len;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001421
1422 return 0;
1423}
1424
Moni Shoua872254d2007-10-09 19:43:38 -07001425static void bond_setup_by_slave(struct net_device *bond_dev,
1426 struct net_device *slave_dev)
1427{
Wang Chen454d7c92008-11-12 23:37:49 -08001428 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07001429
Stephen Hemminger00829822008-11-20 20:14:53 -08001430 bond_dev->header_ops = slave_dev->header_ops;
Moni Shoua872254d2007-10-09 19:43:38 -07001431
1432 bond_dev->type = slave_dev->type;
1433 bond_dev->hard_header_len = slave_dev->hard_header_len;
1434 bond_dev->addr_len = slave_dev->addr_len;
1435
1436 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1437 slave_dev->addr_len);
Moni Shouad90a1622007-10-09 19:43:43 -07001438 bond->setup_by_slave = 1;
Moni Shoua872254d2007-10-09 19:43:38 -07001439}
1440
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001441/* On bonding slaves other than the currently active slave, suppress
Jiri Pirko3aba8912011-04-19 03:48:16 +00001442 * duplicates except for alb non-mcast/bcast.
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001443 */
1444static bool bond_should_deliver_exact_match(struct sk_buff *skb,
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001445 struct slave *slave,
1446 struct bonding *bond)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001447{
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001448 if (bond_is_slave_inactive(slave)) {
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001449 if (bond->params.mode == BOND_MODE_ALB &&
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001450 skb->pkt_type != PACKET_BROADCAST &&
1451 skb->pkt_type != PACKET_MULTICAST)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001452 return false;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001453 return true;
1454 }
1455 return false;
1456}
1457
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001458static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001459{
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001460 struct sk_buff *skb = *pskb;
Jiri Pirkof1c17752011-03-12 03:14:35 +00001461 struct slave *slave;
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001462 struct bonding *bond;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001463
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001464 skb = skb_share_check(skb, GFP_ATOMIC);
1465 if (unlikely(!skb))
1466 return RX_HANDLER_CONSUMED;
1467
1468 *pskb = skb;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001469
Jiri Pirko35d48902011-03-22 02:38:12 +00001470 slave = bond_slave_get_rcu(skb->dev);
1471 bond = slave->bond;
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001472
1473 if (bond->params.arp_interval)
Jiri Pirkof1c17752011-03-12 03:14:35 +00001474 slave->dev->last_rx = jiffies;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001475
Jiri Pirko3aba8912011-04-19 03:48:16 +00001476 if (bond->recv_probe) {
1477 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
1478
1479 if (likely(nskb)) {
1480 bond->recv_probe(nskb, bond, slave);
1481 dev_kfree_skb(nskb);
1482 }
1483 }
1484
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001485 if (bond_should_deliver_exact_match(skb, slave, bond)) {
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001486 return RX_HANDLER_EXACT;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001487 }
1488
Jiri Pirko35d48902011-03-22 02:38:12 +00001489 skb->dev = bond->dev;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001490
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001491 if (bond->params.mode == BOND_MODE_ALB &&
Jiri Pirko35d48902011-03-22 02:38:12 +00001492 bond->dev->priv_flags & IFF_BRIDGE_PORT &&
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001493 skb->pkt_type == PACKET_HOST) {
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001494
Changli Gao541ac7c2011-03-02 21:07:14 +00001495 if (unlikely(skb_cow_head(skb,
1496 skb->data - skb_mac_header(skb)))) {
1497 kfree_skb(skb);
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001498 return RX_HANDLER_CONSUMED;
Changli Gao541ac7c2011-03-02 21:07:14 +00001499 }
Jiri Pirko35d48902011-03-22 02:38:12 +00001500 memcpy(eth_hdr(skb)->h_dest, bond->dev->dev_addr, ETH_ALEN);
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001501 }
1502
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001503 return RX_HANDLER_ANOTHER;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001504}
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506/* enslave device <slave> to bond device <master> */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001507int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Wang Chen454d7c92008-11-12 23:37:49 -08001509 struct bonding *bond = netdev_priv(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001510 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 struct slave *new_slave = NULL;
Jiri Pirko22bedad2010-04-01 21:22:57 +00001512 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 struct sockaddr addr;
1514 int link_reporting;
1515 int old_features = bond_dev->features;
1516 int res = 0;
1517
nsxfreddy@gmail.com552709d2005-09-21 14:18:04 -05001518 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001519 slave_ops->ndo_do_ioctl == NULL) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001520 pr_warning("%s: Warning: no link monitoring support for %s\n",
1521 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523
1524 /* bond must be initialized by bond_open() before enslaving */
1525 if (!(bond_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001526 pr_warning("%s: master_dev is not up in bond_enslave\n",
1527 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
1529
1530 /* already enslaved */
1531 if (slave_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001532 pr_debug("Error, Device was already enslaved\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return -EBUSY;
1534 }
1535
1536 /* vlan challenged mutual exclusion */
1537 /* no need to lock since we're protected by rtnl_lock */
1538 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001539 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Jay Vosburghf35188f2010-07-21 12:14:47 +00001540 if (bond->vlgrp) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001541 pr_err("%s: Error: cannot enslave VLAN challenged slave %s on VLAN enabled bond %s\n",
1542 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return -EPERM;
1544 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001545 pr_warning("%s: Warning: enslaved VLAN challenged slave %s. Adding VLANs will be blocked as long as %s is part of bond %s\n",
1546 bond_dev->name, slave_dev->name,
1547 slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
1549 }
1550 } else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001551 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (bond->slave_cnt == 0) {
1553 /* First slave, and it is not VLAN challenged,
1554 * so remove the block of adding VLANs over the bond.
1555 */
1556 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
1557 }
1558 }
1559
Jay Vosburgh217df672005-09-26 16:11:50 -07001560 /*
1561 * Old ifenslave binaries are no longer supported. These can
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001562 * be identified with moderate accuracy by the state of the slave:
Jay Vosburgh217df672005-09-26 16:11:50 -07001563 * the current ifenslave will set the interface down prior to
1564 * enslaving it; the old ifenslave will not.
1565 */
1566 if ((slave_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001567 pr_err("%s is up. This may be due to an out of date ifenslave.\n",
Jay Vosburgh217df672005-09-26 16:11:50 -07001568 slave_dev->name);
1569 res = -EPERM;
1570 goto err_undo_flags;
1571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Moni Shoua872254d2007-10-09 19:43:38 -07001573 /* set bonding device ether type by slave - bonding netdevices are
1574 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1575 * there is a need to override some of the type dependent attribs/funcs.
1576 *
1577 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1578 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1579 */
1580 if (bond->slave_cnt == 0) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001581 if (bond_dev->type != slave_dev->type) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001582 pr_debug("%s: change device type from %d to %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001583 bond_dev->name,
1584 bond_dev->type, slave_dev->type);
Moni Shoua75c78502009-09-15 02:37:40 -07001585
Jiri Pirko3ca5b402010-03-10 10:29:35 +00001586 res = netdev_bonding_change(bond_dev,
1587 NETDEV_PRE_TYPE_CHANGE);
1588 res = notifier_to_errno(res);
1589 if (res) {
1590 pr_err("%s: refused to change device type\n",
1591 bond_dev->name);
1592 res = -EBUSY;
1593 goto err_undo_flags;
1594 }
Moni Shoua75c78502009-09-15 02:37:40 -07001595
Jiri Pirko32a806c2010-03-19 04:00:23 +00001596 /* Flush unicast and multicast addresses */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001597 dev_uc_flush(bond_dev);
Jiri Pirko22bedad2010-04-01 21:22:57 +00001598 dev_mc_flush(bond_dev);
Jiri Pirko32a806c2010-03-19 04:00:23 +00001599
Moni Shouae36b9d12009-07-15 04:56:31 +00001600 if (slave_dev->type != ARPHRD_ETHER)
1601 bond_setup_by_slave(bond_dev, slave_dev);
1602 else
1603 ether_setup(bond_dev);
Moni Shoua75c78502009-09-15 02:37:40 -07001604
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001605 netdev_bonding_change(bond_dev,
1606 NETDEV_POST_TYPE_CHANGE);
Moni Shouae36b9d12009-07-15 04:56:31 +00001607 }
Moni Shoua872254d2007-10-09 19:43:38 -07001608 } else if (bond_dev->type != slave_dev->type) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001609 pr_err("%s ether type (%d) is different from other slaves (%d), can not enslave it.\n",
1610 slave_dev->name,
1611 slave_dev->type, bond_dev->type);
1612 res = -EINVAL;
1613 goto err_undo_flags;
Moni Shoua872254d2007-10-09 19:43:38 -07001614 }
1615
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001616 if (slave_ops->ndo_set_mac_address == NULL) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001617 if (bond->slave_cnt == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001618 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.",
1619 bond_dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001620 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1621 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001622 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",
1623 bond_dev->name);
Moni Shoua2ab82852007-10-09 19:43:39 -07001624 res = -EOPNOTSUPP;
1625 goto err_undo_flags;
1626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
1628
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001629 /* If this is the first slave, then we need to set the master's hardware
1630 * address to be the same as the slave's. */
David Strandd13a2cb2010-12-01 11:43:08 -08001631 if (is_zero_ether_addr(bond->dev->dev_addr))
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001632 memcpy(bond->dev->dev_addr, slave_dev->dev_addr,
1633 slave_dev->addr_len);
1634
1635
Joe Jin243cb4e2007-02-06 14:16:40 -08001636 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (!new_slave) {
1638 res = -ENOMEM;
1639 goto err_undo_flags;
1640 }
1641
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001642 /*
1643 * Set the new_slave's queue_id to be zero. Queue ID mapping
1644 * is set via sysfs or module option if desired.
1645 */
1646 new_slave->queue_id = 0;
1647
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001648 /* Save slave's original mtu and then set it to match the bond */
1649 new_slave->original_mtu = slave_dev->mtu;
1650 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1651 if (res) {
1652 pr_debug("Error %d calling dev_set_mtu\n", res);
1653 goto err_free;
1654 }
1655
Jay Vosburgh217df672005-09-26 16:11:50 -07001656 /*
1657 * Save slave's original ("permanent") mac address for modes
1658 * that need it, and for restoring it upon release, and then
1659 * set it to the master's address
1660 */
1661 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Jay Vosburghdd957c52007-10-09 19:57:24 -07001663 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001664 /*
1665 * Set slave to master's mac address. The application already
1666 * set the master's mac address to that of the first slave
1667 */
1668 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1669 addr.sa_family = slave_dev->type;
1670 res = dev_set_mac_address(slave_dev, &addr);
1671 if (res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001672 pr_debug("Error %d calling set_mac_address\n", res);
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001673 goto err_restore_mtu;
Moni Shoua2ab82852007-10-09 19:43:39 -07001674 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Jiri Pirko1765a572011-02-12 06:48:36 +00001677 res = netdev_set_bond_master(slave_dev, bond_dev);
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001678 if (res) {
Jiri Pirko1765a572011-02-12 06:48:36 +00001679 pr_debug("Error %d calling netdev_set_bond_master\n", res);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001680 goto err_restore_mac;
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001681 }
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001682
Jay Vosburgh217df672005-09-26 16:11:50 -07001683 /* open the slave since the application closed it */
1684 res = dev_open(slave_dev);
1685 if (res) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001686 pr_debug("Opening slave %s failed\n", slave_dev->name);
Jiri Pirko35d48902011-03-22 02:38:12 +00001687 goto err_unset_master;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
1689
Jiri Pirko35d48902011-03-22 02:38:12 +00001690 new_slave->bond = bond;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 new_slave->dev = slave_dev;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001692 slave_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Holger Eitzenberger58402052008-12-09 23:07:13 -08001694 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /* bond_alb_init_slave() must be called before all other stages since
1696 * it might fail and we do not want to have to undo everything
1697 */
1698 res = bond_alb_init_slave(bond, new_slave);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001699 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001700 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
1702
1703 /* If the mode USES_PRIMARY, then the new slave gets the
1704 * master's promisc (and mc) settings only if it becomes the
1705 * curr_active_slave, and that is taken care of later when calling
1706 * bond_change_active()
1707 */
1708 if (!USES_PRIMARY(bond->params.mode)) {
1709 /* set promiscuity level to new slave */
1710 if (bond_dev->flags & IFF_PROMISC) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001711 res = dev_set_promiscuity(slave_dev, 1);
1712 if (res)
1713 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
1715
1716 /* set allmulti level to new slave */
1717 if (bond_dev->flags & IFF_ALLMULTI) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001718 res = dev_set_allmulti(slave_dev, 1);
1719 if (res)
1720 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722
David S. Millerb9e40852008-07-15 00:15:08 -07001723 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 /* upload master's mc_list to new slave */
Jiri Pirko22bedad2010-04-01 21:22:57 +00001725 netdev_for_each_mc_addr(ha, bond_dev)
1726 dev_mc_add(slave_dev, ha->addr);
David S. Millerb9e40852008-07-15 00:15:08 -07001727 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729
1730 if (bond->params.mode == BOND_MODE_8023AD) {
1731 /* add lacpdu mc addr to mc list */
1732 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1733
Jiri Pirko22bedad2010-04-01 21:22:57 +00001734 dev_mc_add(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 }
1736
1737 bond_add_vlans_on_slave(bond, slave_dev);
1738
1739 write_lock_bh(&bond->lock);
1740
1741 bond_attach_slave(bond, new_slave);
1742
1743 new_slave->delay = 0;
1744 new_slave->link_failure_count = 0;
1745
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001746 bond_compute_features(bond);
1747
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001748 write_unlock_bh(&bond->lock);
1749
1750 read_lock(&bond->lock);
1751
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001752 new_slave->last_arp_rx = jiffies;
1753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (bond->params.miimon && !bond->params.use_carrier) {
1755 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1756
1757 if ((link_reporting == -1) && !bond->params.arp_interval) {
1758 /*
1759 * miimon is set but a bonded network driver
1760 * does not support ETHTOOL/MII and
1761 * arp_interval is not set. Note: if
1762 * use_carrier is enabled, we will never go
1763 * here (because netif_carrier is always
1764 * supported); thus, we don't need to change
1765 * the messages for netif_carrier.
1766 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001767 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 -08001768 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 } else if (link_reporting == -1) {
1770 /* unable get link status using mii/ethtool */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001771 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",
1772 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774 }
1775
1776 /* check for initial state */
1777 if (!bond->params.miimon ||
1778 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1779 if (bond->params.updelay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001780 pr_debug("Initial state of slave_dev is BOND_LINK_BACK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 new_slave->link = BOND_LINK_BACK;
1782 new_slave->delay = bond->params.updelay;
1783 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001784 pr_debug("Initial state of slave_dev is BOND_LINK_UP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 new_slave->link = BOND_LINK_UP;
1786 }
1787 new_slave->jiffies = jiffies;
1788 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001789 pr_debug("Initial state of slave_dev is BOND_LINK_DOWN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 new_slave->link = BOND_LINK_DOWN;
1791 }
1792
1793 if (bond_update_speed_duplex(new_slave) &&
1794 (new_slave->link != BOND_LINK_DOWN)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001795 pr_warning("%s: Warning: failed to get speed and duplex from %s, assumed to be 100Mb/sec and Full.\n",
1796 bond_dev->name, new_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 if (bond->params.mode == BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001799 pr_warning("%s: Warning: Operation of 802.3ad mode requires ETHTOOL support in base driver for proper aggregator selection.\n",
1800 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
1802 }
1803
1804 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1805 /* if there is a primary slave, remember it */
Jiri Pirkoa5499522009-09-25 03:28:09 +00001806 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 bond->primary_slave = new_slave;
Jiri Pirkoa5499522009-09-25 03:28:09 +00001808 bond->force_primary = true;
1809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
1811
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001812 write_lock_bh(&bond->curr_slave_lock);
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 switch (bond->params.mode) {
1815 case BOND_MODE_ACTIVEBACKUP:
Jay Vosburgh8a8e4472006-09-22 21:56:15 -07001816 bond_set_slave_inactive_flags(new_slave);
1817 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 break;
1819 case BOND_MODE_8023AD:
1820 /* in 802.3ad mode, the internal mechanism
1821 * will activate the slaves in the selected
1822 * aggregator
1823 */
1824 bond_set_slave_inactive_flags(new_slave);
1825 /* if this is the first slave */
1826 if (bond->slave_cnt == 1) {
1827 SLAVE_AD_INFO(new_slave).id = 1;
1828 /* Initialize AD with the number of times that the AD timer is called in 1 second
1829 * can be called only after the mac address of the bond is set
1830 */
1831 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1832 bond->params.lacp_fast);
1833 } else {
1834 SLAVE_AD_INFO(new_slave).id =
1835 SLAVE_AD_INFO(new_slave->prev).id + 1;
1836 }
1837
1838 bond_3ad_bind_slave(new_slave);
1839 break;
1840 case BOND_MODE_TLB:
1841 case BOND_MODE_ALB:
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001842 bond_set_active_slave(new_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001843 bond_set_slave_inactive_flags(new_slave);
Jiri Pirko5a29f782009-03-25 17:23:38 -07001844 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 break;
1846 default:
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001847 pr_debug("This slave is always active in trunk mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 /* always active in trunk mode */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001850 bond_set_active_slave(new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 /* In trunking mode there is little meaning to curr_active_slave
1853 * anyway (it holds no special properties of the bond device),
1854 * so we can change it without calling change_active_interface()
1855 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001856 if (!bond->curr_active_slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 bond->curr_active_slave = new_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 break;
1860 } /* switch(bond_mode) */
1861
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001862 write_unlock_bh(&bond->curr_slave_lock);
1863
Jay Vosburghff59c452006-03-27 13:27:43 -08001864 bond_set_carrier(bond);
1865
WANG Congf6dc31a2010-05-06 00:48:51 -07001866#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001867 slave_dev->npinfo = bond_netpoll_info(bond);
1868 if (slave_dev->npinfo) {
1869 if (slave_enable_netpoll(new_slave)) {
1870 read_unlock(&bond->lock);
1871 pr_info("Error, %s: master_dev is using netpoll, "
1872 "but new slave device does not support netpoll.\n",
1873 bond_dev->name);
1874 res = -EBUSY;
1875 goto err_close;
1876 }
WANG Congf6dc31a2010-05-06 00:48:51 -07001877 }
1878#endif
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001879
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001880 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001882 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1883 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001884 goto err_close;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001885
Jiri Pirko35d48902011-03-22 02:38:12 +00001886 res = netdev_rx_handler_register(slave_dev, bond_handle_frame,
1887 new_slave);
1888 if (res) {
1889 pr_debug("Error %d calling netdev_rx_handler_register\n", res);
1890 goto err_dest_symlinks;
1891 }
1892
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001893 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
1894 bond_dev->name, slave_dev->name,
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001895 bond_is_active_slave(new_slave) ? "n active" : " backup",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001896 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 /* enslave is successful */
1899 return 0;
1900
1901/* Undo stages on error */
Jiri Pirko35d48902011-03-22 02:38:12 +00001902err_dest_symlinks:
1903 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905err_close:
1906 dev_close(slave_dev);
1907
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001908err_unset_master:
Jiri Pirko1765a572011-02-12 06:48:36 +00001909 netdev_set_bond_master(slave_dev, NULL);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911err_restore_mac:
Jay Vosburghdd957c52007-10-09 19:57:24 -07001912 if (!bond->params.fail_over_mac) {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001913 /* XXX TODO - fom follow mode needs to change master's
1914 * MAC if this slave's MAC is in use by the bond, or at
1915 * least print a warning.
1916 */
Moni Shoua2ab82852007-10-09 19:43:39 -07001917 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1918 addr.sa_family = slave_dev->type;
1919 dev_set_mac_address(slave_dev, &addr);
1920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001922err_restore_mtu:
1923 dev_set_mtu(slave_dev, new_slave->original_mtu);
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925err_free:
1926 kfree(new_slave);
1927
1928err_undo_flags:
1929 bond_dev->features = old_features;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return res;
1932}
1933
1934/*
1935 * Try to release the slave device <slave> from the bond device <master>
1936 * It is legal to access curr_active_slave without a lock because all the function
1937 * is write-locked.
1938 *
1939 * The rules for slave state should be:
1940 * for Active/Backup:
1941 * Active stays on all backups go down
1942 * for Bonded connections:
1943 * The first up interface should be left on and all others downed.
1944 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001945int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Wang Chen454d7c92008-11-12 23:37:49 -08001947 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 struct slave *slave, *oldcurrent;
1949 struct sockaddr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 /* slave is not a slave or master is not master of this slave */
1952 if (!(slave_dev->flags & IFF_SLAVE) ||
1953 (slave_dev->master != bond_dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001954 pr_err("%s: Error: cannot release %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 bond_dev->name, slave_dev->name);
1956 return -EINVAL;
1957 }
1958
Neil Hormane843fa52010-10-13 16:01:50 +00001959 block_netpoll_tx();
WANG Congf6dc31a2010-05-06 00:48:51 -07001960 netdev_bonding_change(bond_dev, NETDEV_BONDING_DESLAVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 write_lock_bh(&bond->lock);
1962
1963 slave = bond_get_slave_by_dev(bond, slave_dev);
1964 if (!slave) {
1965 /* not a slave of this bond */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001966 pr_info("%s: %s not enslaved\n",
1967 bond_dev->name, slave_dev->name);
Jay Vosburghf5e2a7b2006-02-07 21:17:22 -08001968 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001969 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 return -EINVAL;
1971 }
1972
Jiri Pirko35d48902011-03-22 02:38:12 +00001973 /* unregister rx_handler early so bond_handle_frame wouldn't be called
1974 * for this slave anymore.
1975 */
1976 netdev_rx_handler_unregister(slave_dev);
1977 write_unlock_bh(&bond->lock);
1978 synchronize_net();
1979 write_lock_bh(&bond->lock);
1980
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001981 if (!bond->params.fail_over_mac) {
Joe Perches8e95a202009-12-03 07:58:21 +00001982 if (!compare_ether_addr(bond_dev->dev_addr, slave->perm_hwaddr) &&
1983 bond->slave_cnt > 1)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001984 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",
1985 bond_dev->name, slave_dev->name,
1986 slave->perm_hwaddr,
1987 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
1989
1990 /* Inform AD package of unbinding of slave. */
1991 if (bond->params.mode == BOND_MODE_8023AD) {
1992 /* must be called before the slave is
1993 * detached from the list
1994 */
1995 bond_3ad_unbind_slave(slave);
1996 }
1997
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001998 pr_info("%s: releasing %s interface %s\n",
1999 bond_dev->name,
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002000 bond_is_active_slave(slave) ? "active" : "backup",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002001 slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 oldcurrent = bond->curr_active_slave;
2004
2005 bond->current_arp_slave = NULL;
2006
2007 /* release the slave from its bond */
2008 bond_detach_slave(bond, slave);
2009
Arthur Kepner8531c5f2005-08-23 01:34:53 -04002010 bond_compute_features(bond);
2011
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002012 if (bond->primary_slave == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 bond->primary_slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002015 if (oldcurrent == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 bond_change_active_slave(bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Holger Eitzenberger58402052008-12-09 23:07:13 -08002018 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 /* Must be called only after the slave has been
2020 * detached from the list and the curr_active_slave
2021 * has been cleared (if our_slave == old_current),
2022 * but before a new active slave is selected.
2023 */
Jay Vosburgh25433312008-01-17 16:24:59 -08002024 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 bond_alb_deinit_slave(bond, slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08002026 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
2028
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002029 if (oldcurrent == slave) {
2030 /*
2031 * Note that we hold RTNL over this sequence, so there
2032 * is no concern that another slave add/remove event
2033 * will interfere.
2034 */
2035 write_unlock_bh(&bond->lock);
2036 read_lock(&bond->lock);
2037 write_lock_bh(&bond->curr_slave_lock);
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 bond_select_active_slave(bond);
2040
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002041 write_unlock_bh(&bond->curr_slave_lock);
2042 read_unlock(&bond->lock);
2043 write_lock_bh(&bond->lock);
2044 }
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 if (bond->slave_cnt == 0) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002047 bond_set_carrier(bond);
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 /* if the last slave was removed, zero the mac address
2050 * of the master so it will be set by the application
2051 * to the mac address of the first slave
2052 */
2053 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2054
Jay Vosburghf35188f2010-07-21 12:14:47 +00002055 if (!bond->vlgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
2057 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002058 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2059 bond_dev->name, bond_dev->name);
2060 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2061 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
2063 } else if ((bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2064 !bond_has_challenged_slaves(bond)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002065 pr_info("%s: last VLAN challenged slave %s left bond %s. VLAN blocking is removed\n",
2066 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 bond_dev->features &= ~NETIF_F_VLAN_CHALLENGED;
2068 }
2069
2070 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002071 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002073 /* must do this from outside any spinlocks */
2074 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 bond_del_vlans_from_slave(bond, slave_dev);
2077
2078 /* If the mode USES_PRIMARY, then we should only remove its
2079 * promisc and mc settings if it was the curr_active_slave, but that was
2080 * already taken care of above when we detached the slave
2081 */
2082 if (!USES_PRIMARY(bond->params.mode)) {
2083 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002084 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002088 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002092 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002094 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
2096
Jiri Pirko1765a572011-02-12 06:48:36 +00002097 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002099 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07002100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 /* close slave before restoring its mac address */
2102 dev_close(slave_dev);
2103
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07002104 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002105 /* restore original ("permanent") mac address */
2106 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2107 addr.sa_family = slave_dev->type;
2108 dev_set_mac_address(slave_dev, &addr);
2109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00002111 dev_set_mtu(slave_dev, slave->original_mtu);
2112
Jiri Pirko2d7011c2011-03-16 08:46:43 +00002113 slave_dev->priv_flags &= ~IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 kfree(slave);
2116
2117 return 0; /* deletion OK */
2118}
2119
2120/*
Nicolas de Pesloüandadaa102011-03-19 13:36:18 -07002121* First release a slave and then destroy the bond if no more slaves are left.
Moni Shouad90a1622007-10-09 19:43:43 -07002122* Must be under rtnl_lock when this function is called.
2123*/
stephen hemminger26d8ee72010-10-15 05:09:34 +00002124static int bond_release_and_destroy(struct net_device *bond_dev,
2125 struct net_device *slave_dev)
Moni Shouad90a1622007-10-09 19:43:43 -07002126{
Wang Chen454d7c92008-11-12 23:37:49 -08002127 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002128 int ret;
2129
2130 ret = bond_release(bond_dev, slave_dev);
2131 if ((ret == 0) && (bond->slave_cnt == 0)) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002132 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002133 pr_info("%s: destroying bond %s.\n",
2134 bond_dev->name, bond_dev->name);
Stephen Hemminger9e716262009-06-12 19:02:47 +00002135 unregister_netdevice(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002136 }
2137 return ret;
2138}
2139
2140/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 * This function releases all slaves.
2142 */
2143static int bond_release_all(struct net_device *bond_dev)
2144{
Wang Chen454d7c92008-11-12 23:37:49 -08002145 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 struct slave *slave;
2147 struct net_device *slave_dev;
2148 struct sockaddr addr;
2149
2150 write_lock_bh(&bond->lock);
2151
Jay Vosburghff59c452006-03-27 13:27:43 -08002152 netif_carrier_off(bond_dev);
2153
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002154 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 bond->current_arp_slave = NULL;
2158 bond->primary_slave = NULL;
2159 bond_change_active_slave(bond, NULL);
2160
2161 while ((slave = bond->first_slave) != NULL) {
2162 /* Inform AD package of unbinding of slave
2163 * before slave is detached from the list.
2164 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002165 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 bond_3ad_unbind_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 slave_dev = slave->dev;
2169 bond_detach_slave(bond, slave);
2170
Jay Vosburgh25433312008-01-17 16:24:59 -08002171 /* now that the slave is detached, unlock and perform
2172 * all the undo steps that should not be called from
2173 * within a lock.
2174 */
2175 write_unlock_bh(&bond->lock);
2176
Jiri Pirko35d48902011-03-22 02:38:12 +00002177 /* unregister rx_handler early so bond_handle_frame wouldn't
2178 * be called for this slave anymore.
2179 */
2180 netdev_rx_handler_unregister(slave_dev);
2181 synchronize_net();
2182
Holger Eitzenberger58402052008-12-09 23:07:13 -08002183 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 /* must be called only after the slave
2185 * has been detached from the list
2186 */
2187 bond_alb_deinit_slave(bond, slave);
2188 }
2189
Arthur Kepner8531c5f2005-08-23 01:34:53 -04002190 bond_compute_features(bond);
2191
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002192 bond_destroy_slave_symlinks(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 bond_del_vlans_from_slave(bond, slave_dev);
2194
2195 /* If the mode USES_PRIMARY, then we should only remove its
2196 * promisc and mc settings if it was the curr_active_slave, but that was
2197 * already taken care of above when we detached the slave
2198 */
2199 if (!USES_PRIMARY(bond->params.mode)) {
2200 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002201 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002205 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002209 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002211 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
2213
Jiri Pirko1765a572011-02-12 06:48:36 +00002214 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002216 slave_disable_netpoll(slave);
2217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 /* close slave before restoring its mac address */
2219 dev_close(slave_dev);
2220
Jay Vosburghdd957c52007-10-09 19:57:24 -07002221 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002222 /* restore original ("permanent") mac address*/
2223 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2224 addr.sa_family = slave_dev->type;
2225 dev_set_mac_address(slave_dev, &addr);
2226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 kfree(slave);
2229
2230 /* re-acquire the lock before getting the next slave */
2231 write_lock_bh(&bond->lock);
2232 }
2233
2234 /* zero the mac address of the master so it will be
2235 * set by the application to the mac address of the
2236 * first slave
2237 */
2238 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2239
Jay Vosburghf35188f2010-07-21 12:14:47 +00002240 if (!bond->vlgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
Jay Vosburghf35188f2010-07-21 12:14:47 +00002242 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002243 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2244 bond_dev->name, bond_dev->name);
2245 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2246 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
2248
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002249 pr_info("%s: released all slaves\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251out:
2252 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return 0;
2254}
2255
2256/*
2257 * This function changes the active slave to slave <slave_dev>.
2258 * It returns -EINVAL in the following cases.
2259 * - <slave_dev> is not found in the list.
2260 * - There is not active slave now.
2261 * - <slave_dev> is already active.
2262 * - The link state of <slave_dev> is not BOND_LINK_UP.
2263 * - <slave_dev> is not running.
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002264 * In these cases, this function does nothing.
2265 * In the other cases, current_slave pointer is changed and 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 */
2267static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2268{
Wang Chen454d7c92008-11-12 23:37:49 -08002269 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 struct slave *old_active = NULL;
2271 struct slave *new_active = NULL;
2272 int res = 0;
2273
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002274 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
2277 /* Verify that master_dev is indeed the master of slave_dev */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002278 if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002281 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002283 read_lock(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 old_active = bond->curr_active_slave;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002285 read_unlock(&bond->curr_slave_lock);
2286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 new_active = bond_get_slave_by_dev(bond, slave_dev);
2288
2289 /*
2290 * Changing to the current active: do nothing; return success.
2291 */
2292 if (new_active && (new_active == old_active)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002293 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 return 0;
2295 }
2296
2297 if ((new_active) &&
2298 (old_active) &&
2299 (new_active->link == BOND_LINK_UP) &&
2300 IS_UP(new_active->dev)) {
Neil Hormane843fa52010-10-13 16:01:50 +00002301 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002302 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 bond_change_active_slave(bond, new_active);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002304 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002305 unblock_netpoll_tx();
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002306 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002309 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311 return res;
2312}
2313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2315{
Wang Chen454d7c92008-11-12 23:37:49 -08002316 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 info->bond_mode = bond->params.mode;
2319 info->miimon = bond->params.miimon;
2320
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002321 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 info->num_slaves = bond->slave_cnt;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002323 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 return 0;
2326}
2327
2328static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2329{
Wang Chen454d7c92008-11-12 23:37:49 -08002330 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 struct slave *slave;
Eric Dumazet689c96c2009-04-23 03:39:04 +00002332 int i, res = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002334 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
2336 bond_for_each_slave(bond, slave, i) {
2337 if (i == (int)info->slave_id) {
Eric Dumazet689c96c2009-04-23 03:39:04 +00002338 res = 0;
2339 strcpy(info->slave_name, slave->dev->name);
2340 info->link = slave->link;
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002341 info->state = bond_slave_state(slave);
Eric Dumazet689c96c2009-04-23 03:39:04 +00002342 info->link_failure_count = slave->link_failure_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 break;
2344 }
2345 }
2346
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002347 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Eric Dumazet689c96c2009-04-23 03:39:04 +00002349 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350}
2351
2352/*-------------------------------- Monitoring -------------------------------*/
2353
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002354
2355static int bond_miimon_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002357 struct slave *slave;
2358 int i, link_state, commit = 0;
Jiri Pirko41f89102009-04-24 03:57:29 +00002359 bool ignore_updelay;
2360
2361 ignore_updelay = !bond->curr_active_slave ? true : false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
2363 bond_for_each_slave(bond, slave, i) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002364 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002366 link_state = bond_check_dev_link(bond, slave->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368 switch (slave->link) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002369 case BOND_LINK_UP:
2370 if (link_state)
2371 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002373 slave->link = BOND_LINK_FAIL;
2374 slave->delay = bond->params.downdelay;
2375 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002376 pr_info("%s: link status down for %sinterface %s, disabling it in %d ms.\n",
2377 bond->dev->name,
2378 (bond->params.mode ==
2379 BOND_MODE_ACTIVEBACKUP) ?
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002380 (bond_is_active_slave(slave) ?
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002381 "active " : "backup ") : "",
2382 slave->dev->name,
2383 bond->params.downdelay * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002385 /*FALLTHRU*/
2386 case BOND_LINK_FAIL:
2387 if (link_state) {
2388 /*
2389 * recovered before downdelay expired
2390 */
2391 slave->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 slave->jiffies = jiffies;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002393 pr_info("%s: link status up again after %d ms for interface %s.\n",
2394 bond->dev->name,
2395 (bond->params.downdelay - slave->delay) *
2396 bond->params.miimon,
2397 slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002398 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002400
2401 if (slave->delay <= 0) {
2402 slave->new_link = BOND_LINK_DOWN;
2403 commit++;
2404 continue;
2405 }
2406
2407 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002410 case BOND_LINK_DOWN:
2411 if (!link_state)
2412 continue;
2413
2414 slave->link = BOND_LINK_BACK;
2415 slave->delay = bond->params.updelay;
2416
2417 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002418 pr_info("%s: link status up for interface %s, enabling it in %d ms.\n",
2419 bond->dev->name, slave->dev->name,
2420 ignore_updelay ? 0 :
2421 bond->params.updelay *
2422 bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002424 /*FALLTHRU*/
2425 case BOND_LINK_BACK:
2426 if (!link_state) {
2427 slave->link = BOND_LINK_DOWN;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002428 pr_info("%s: link status down again after %d ms for interface %s.\n",
2429 bond->dev->name,
2430 (bond->params.updelay - slave->delay) *
2431 bond->params.miimon,
2432 slave->dev->name);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002433
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002434 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002436
Jiri Pirko41f89102009-04-24 03:57:29 +00002437 if (ignore_updelay)
2438 slave->delay = 0;
2439
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002440 if (slave->delay <= 0) {
2441 slave->new_link = BOND_LINK_UP;
2442 commit++;
Jiri Pirko41f89102009-04-24 03:57:29 +00002443 ignore_updelay = false;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002444 continue;
2445 }
2446
2447 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 break;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002449 }
2450 }
2451
2452 return commit;
2453}
2454
2455static void bond_miimon_commit(struct bonding *bond)
2456{
2457 struct slave *slave;
2458 int i;
2459
2460 bond_for_each_slave(bond, slave, i) {
2461 switch (slave->new_link) {
2462 case BOND_LINK_NOCHANGE:
2463 continue;
2464
2465 case BOND_LINK_UP:
2466 slave->link = BOND_LINK_UP;
2467 slave->jiffies = jiffies;
2468
2469 if (bond->params.mode == BOND_MODE_8023AD) {
2470 /* prevent it from being the active one */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002471 bond_set_backup_slave(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002472 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2473 /* make it immediately active */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002474 bond_set_active_slave(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002475 } else if (slave != bond->primary_slave) {
2476 /* prevent it from being the active one */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002477 bond_set_backup_slave(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002478 }
2479
Krzysztof Piotr Oledzki546add72010-10-06 14:28:22 -07002480 bond_update_speed_duplex(slave);
2481
David Decotigny5d305302011-04-13 15:22:31 +00002482 pr_info("%s: link status definitely up for interface %s, %u Mbps %s duplex.\n",
Krzysztof Piotr Oledzki700c2a72010-10-06 14:25:06 -07002483 bond->dev->name, slave->dev->name,
2484 slave->speed, slave->duplex ? "full" : "half");
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002485
2486 /* notify ad that the link status has changed */
2487 if (bond->params.mode == BOND_MODE_8023AD)
2488 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2489
Holger Eitzenberger58402052008-12-09 23:07:13 -08002490 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002491 bond_alb_handle_link_change(bond, slave,
2492 BOND_LINK_UP);
2493
2494 if (!bond->curr_active_slave ||
2495 (slave == bond->primary_slave))
2496 goto do_failover;
2497
2498 continue;
2499
2500 case BOND_LINK_DOWN:
Jay Vosburghfba4acd2008-10-30 17:41:14 -07002501 if (slave->link_failure_count < UINT_MAX)
2502 slave->link_failure_count++;
2503
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002504 slave->link = BOND_LINK_DOWN;
2505
2506 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2507 bond->params.mode == BOND_MODE_8023AD)
2508 bond_set_slave_inactive_flags(slave);
2509
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002510 pr_info("%s: link status definitely down for interface %s, disabling it\n",
2511 bond->dev->name, slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002512
2513 if (bond->params.mode == BOND_MODE_8023AD)
2514 bond_3ad_handle_link_change(slave,
2515 BOND_LINK_DOWN);
2516
Jiri Pirkoae63e802009-05-27 05:42:36 +00002517 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002518 bond_alb_handle_link_change(bond, slave,
2519 BOND_LINK_DOWN);
2520
2521 if (slave == bond->curr_active_slave)
2522 goto do_failover;
2523
2524 continue;
2525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002527 pr_err("%s: invalid new link %d on slave %s\n",
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002528 bond->dev->name, slave->new_link,
2529 slave->dev->name);
2530 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002532 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 }
2534
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002535do_failover:
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002536 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00002537 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002538 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 bond_select_active_slave(bond);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002540 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002541 unblock_netpoll_tx();
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002542 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002543
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002544 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
2546
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002547/*
2548 * bond_mii_monitor
2549 *
2550 * Really a wrapper that splits the mii monitor into two phases: an
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002551 * inspection, then (if inspection indicates something needs to be done)
2552 * an acquisition of appropriate locks followed by a commit phase to
2553 * implement whatever link state changes are indicated.
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002554 */
2555void bond_mii_monitor(struct work_struct *work)
2556{
2557 struct bonding *bond = container_of(work, struct bonding,
2558 mii_work.work);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002559
2560 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002561 if (bond->kill_timers)
2562 goto out;
2563
2564 if (bond->slave_cnt == 0)
2565 goto re_arm;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002566
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002567 if (bond_miimon_inspect(bond)) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002568 read_unlock(&bond->lock);
2569 rtnl_lock();
2570 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002571
2572 bond_miimon_commit(bond);
2573
Jay Vosburgh56556622008-01-17 16:25:03 -08002574 read_unlock(&bond->lock);
2575 rtnl_unlock(); /* might sleep, hold no other locks */
2576 read_lock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002577 }
2578
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002579re_arm:
2580 if (bond->params.miimon)
2581 queue_delayed_work(bond->wq, &bond->mii_work,
2582 msecs_to_jiffies(bond->params.miimon));
2583out:
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002584 read_unlock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002585}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002586
Al Virod3bb52b2007-08-22 20:06:58 -04002587static __be32 bond_glean_dev_ip(struct net_device *dev)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002588{
2589 struct in_device *idev;
2590 struct in_ifaddr *ifa;
Al Viroa144ea42006-09-28 18:00:55 -07002591 __be32 addr = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002592
2593 if (!dev)
2594 return 0;
2595
2596 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -07002597 idev = __in_dev_get_rcu(dev);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002598 if (!idev)
2599 goto out;
2600
2601 ifa = idev->ifa_list;
2602 if (!ifa)
2603 goto out;
2604
2605 addr = ifa->ifa_local;
2606out:
2607 rcu_read_unlock();
2608 return addr;
2609}
2610
Al Virod3bb52b2007-08-22 20:06:58 -04002611static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002612{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002613 struct vlan_entry *vlan;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002614
2615 if (ip == bond->master_ip)
2616 return 1;
2617
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002618 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002619 if (ip == vlan->vlan_ip)
2620 return 1;
2621 }
2622
2623 return 0;
2624}
2625
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002626/*
2627 * We go to the (large) trouble of VLAN tagging ARP frames because
2628 * switches in VLAN mode (especially if ports are configured as
2629 * "native" to a VLAN) might not pass non-tagged frames.
2630 */
Al Virod3bb52b2007-08-22 20:06:58 -04002631static 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 -04002632{
2633 struct sk_buff *skb;
2634
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002635 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002636 slave_dev->name, dest_ip, src_ip, vlan_id);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002637
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002638 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2639 NULL, slave_dev->dev_addr, NULL);
2640
2641 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002642 pr_err("ARP packet allocation failed\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002643 return;
2644 }
2645 if (vlan_id) {
2646 skb = vlan_put_tag(skb, vlan_id);
2647 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002648 pr_err("failed to insert VLAN tag\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002649 return;
2650 }
2651 }
2652 arp_xmit(skb);
2653}
2654
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2657{
David S. Millerb23dd4f2011-03-02 14:31:35 -08002658 int i, vlan_id;
Al Virod3bb52b2007-08-22 20:06:58 -04002659 __be32 *targets = bond->params.arp_targets;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002660 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002661 struct net_device *vlan_dev;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002662 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Mitch Williams6b780562005-11-09 10:36:19 -08002664 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2665 if (!targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07002666 break;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002667 pr_debug("basa: target %x\n", targets[i]);
Jay Vosburghf35188f2010-07-21 12:14:47 +00002668 if (!bond->vlgrp) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002669 pr_debug("basa: empty vlan: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002670 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2671 bond->master_ip, 0);
2672 continue;
2673 }
2674
2675 /*
2676 * If VLANs are configured, we do a route lookup to
2677 * determine which VLAN interface would be used, so we
2678 * can tag the ARP with the proper VLAN tag.
2679 */
David S. Miller78fbfd82011-03-12 00:00:52 -05002680 rt = ip_route_output(dev_net(bond->dev), targets[i], 0,
2681 RTO_ONLINK, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08002682 if (IS_ERR(rt)) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002683 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002684 pr_warning("%s: no route to arp_ip_target %pI4\n",
David S. Miller78fbfd82011-03-12 00:00:52 -05002685 bond->dev->name, &targets[i]);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002686 }
2687 continue;
2688 }
2689
2690 /*
2691 * This target is not on a VLAN
2692 */
Changli Gaod8d1f302010-06-10 23:31:35 -07002693 if (rt->dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002694 ip_rt_put(rt);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002695 pr_debug("basa: rtdev == bond->dev: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002696 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2697 bond->master_ip, 0);
2698 continue;
2699 }
2700
2701 vlan_id = 0;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002702 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Dan Aloni5c15bde2007-03-02 20:44:51 -08002703 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Changli Gaod8d1f302010-06-10 23:31:35 -07002704 if (vlan_dev == rt->dst.dev) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002705 vlan_id = vlan->vlan_id;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002706 pr_debug("basa: vlan match on %s %d\n",
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002707 vlan_dev->name, vlan_id);
2708 break;
2709 }
2710 }
2711
2712 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002713 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002714 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2715 vlan->vlan_ip, vlan_id);
2716 continue;
2717 }
2718
2719 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002720 pr_warning("%s: no path to arp_ip_target %pI4 via rt.dev %s\n",
David S. Miller78fbfd82011-03-12 00:00:52 -05002721 bond->dev->name, &targets[i],
Changli Gaod8d1f302010-06-10 23:31:35 -07002722 rt->dst.dev ? rt->dst.dev->name : "NULL");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002723 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002724 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002725 }
2726}
2727
Al Virod3bb52b2007-08-22 20:06:58 -04002728static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002729{
2730 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002731 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002732
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002733 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002734 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002735 &sip, &tip, i, &targets[i],
2736 bond_has_this_ip(bond, tip));
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002737 if (sip == targets[i]) {
2738 if (bond_has_this_ip(bond, tip))
2739 slave->last_arp_rx = jiffies;
2740 return;
2741 }
2742 }
2743}
2744
Jiri Pirko3aba8912011-04-19 03:48:16 +00002745static void bond_arp_rcv(struct sk_buff *skb, struct bonding *bond,
2746 struct slave *slave)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002747{
2748 struct arphdr *arp;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002749 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002750 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002751
Jiri Pirko3aba8912011-04-19 03:48:16 +00002752 if (skb->protocol != __cpu_to_be16(ETH_P_ARP))
2753 return;
Andy Gospodarek1f3c8802009-12-14 10:48:58 +00002754
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002755 read_lock(&bond->lock);
2756
Jiri Pirko3aba8912011-04-19 03:48:16 +00002757 pr_debug("bond_arp_rcv: bond %s skb->dev %s\n",
2758 bond->dev->name, skb->dev->name);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002759
Jiri Pirko3aba8912011-04-19 03:48:16 +00002760 if (!pskb_may_pull(skb, arp_hdr_len(bond->dev)))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002761 goto out_unlock;
2762
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002763 arp = arp_hdr(skb);
Jiri Pirko3aba8912011-04-19 03:48:16 +00002764 if (arp->ar_hln != bond->dev->addr_len ||
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002765 skb->pkt_type == PACKET_OTHERHOST ||
2766 skb->pkt_type == PACKET_LOOPBACK ||
2767 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2768 arp->ar_pro != htons(ETH_P_IP) ||
2769 arp->ar_pln != 4)
2770 goto out_unlock;
2771
2772 arp_ptr = (unsigned char *)(arp + 1);
Jiri Pirko3aba8912011-04-19 03:48:16 +00002773 arp_ptr += bond->dev->addr_len;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002774 memcpy(&sip, arp_ptr, 4);
Jiri Pirko3aba8912011-04-19 03:48:16 +00002775 arp_ptr += 4 + bond->dev->addr_len;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002776 memcpy(&tip, arp_ptr, 4);
2777
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002778 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002779 bond->dev->name, slave->dev->name, bond_slave_state(slave),
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002780 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2781 &sip, &tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002782
2783 /*
2784 * Backup slaves won't see the ARP reply, but do come through
2785 * here for each ARP probe (so we swap the sip/tip to validate
2786 * the probe). In a "redundant switch, common router" type of
2787 * configuration, the ARP probe will (hopefully) travel from
2788 * the active, through one switch, the router, then the other
2789 * switch before reaching the backup.
2790 */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002791 if (bond_is_active_slave(slave))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002792 bond_validate_arp(bond, slave, sip, tip);
2793 else
2794 bond_validate_arp(bond, slave, tip, sip);
2795
2796out_unlock:
2797 read_unlock(&bond->lock);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002798}
2799
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800/*
2801 * this function is called regularly to monitor each slave's link
2802 * ensuring that traffic is being sent and received when arp monitoring
2803 * is used in load-balancing mode. if the adapter has been dormant, then an
2804 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2805 * arp monitoring in active backup mode.
2806 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002807void bond_loadbalance_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002809 struct bonding *bond = container_of(work, struct bonding,
2810 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 struct slave *slave, *oldcurrent;
2812 int do_failover = 0;
2813 int delta_in_ticks;
2814 int i;
2815
2816 read_lock(&bond->lock);
2817
Jay Vosburgh5ce0da82008-05-17 21:10:07 -07002818 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002820 if (bond->kill_timers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002823 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
2826 read_lock(&bond->curr_slave_lock);
2827 oldcurrent = bond->curr_active_slave;
2828 read_unlock(&bond->curr_slave_lock);
2829
2830 /* see if any of the previous devices are up now (i.e. they have
2831 * xmt and rcv traffic). the curr_active_slave does not come into
2832 * the picture unless it is null. also, slave->jiffies is not needed
2833 * here because we send an arp on each slave and give a slave as
2834 * long as it needs to get the tx/rx within the delta.
2835 * TODO: what about up/down delay in arp mode? it wasn't here before
2836 * so it can wait
2837 */
2838 bond_for_each_slave(bond, slave, i) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002839 unsigned long trans_start = dev_trans_start(slave->dev);
2840
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002842 if (time_in_range(jiffies,
2843 trans_start - delta_in_ticks,
2844 trans_start + delta_in_ticks) &&
2845 time_in_range(jiffies,
2846 slave->dev->last_rx - delta_in_ticks,
2847 slave->dev->last_rx + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 slave->link = BOND_LINK_UP;
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002850 bond_set_active_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
2852 /* primary_slave has no meaning in round-robin
2853 * mode. the window of a slave being up and
2854 * curr_active_slave being null after enslaving
2855 * is closed.
2856 */
2857 if (!oldcurrent) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002858 pr_info("%s: link status definitely up for interface %s, ",
2859 bond->dev->name,
2860 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 do_failover = 1;
2862 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002863 pr_info("%s: interface %s is now up\n",
2864 bond->dev->name,
2865 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 }
2867 }
2868 } else {
2869 /* slave->link == BOND_LINK_UP */
2870
2871 /* not all switches will respond to an arp request
2872 * when the source ip is 0, so don't take the link down
2873 * if we don't know our ip yet
2874 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002875 if (!time_in_range(jiffies,
2876 trans_start - delta_in_ticks,
2877 trans_start + 2 * delta_in_ticks) ||
2878 !time_in_range(jiffies,
2879 slave->dev->last_rx - delta_in_ticks,
2880 slave->dev->last_rx + 2 * delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
2882 slave->link = BOND_LINK_DOWN;
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002883 bond_set_backup_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002885 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002888 pr_info("%s: interface %s is now down.\n",
2889 bond->dev->name,
2890 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002892 if (slave == oldcurrent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 do_failover = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
2895 }
2896
2897 /* note: if switch is in round-robin mode, all links
2898 * must tx arp to ensure all links rx an arp - otherwise
2899 * links may oscillate or not come up at all; if switch is
2900 * in something like xor mode, there is nothing we can
2901 * do - all replies will be rx'ed on same link causing slaves
2902 * to be unstable during low/no traffic periods
2903 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002904 if (IS_UP(slave->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 bond_arp_send_all(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 }
2907
2908 if (do_failover) {
Neil Hormane843fa52010-10-13 16:01:50 +00002909 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002910 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
2912 bond_select_active_slave(bond);
2913
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002914 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002915 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 }
2917
2918re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002919 if (bond->params.arp_interval)
2920 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921out:
2922 read_unlock(&bond->lock);
2923}
2924
2925/*
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002926 * Called to inspect slaves for active-backup mode ARP monitor link state
2927 * changes. Sets new_link in slaves to specify what action should take
2928 * place for the slave. Returns 0 if no changes are found, >0 if changes
2929 * to link states must be committed.
2930 *
2931 * Called with bond->lock held for read.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002933static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 struct slave *slave;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002936 int i, commit = 0;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002937 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 bond_for_each_slave(bond, slave, i) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002940 slave->new_link = BOND_LINK_NOCHANGE;
2941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002943 if (time_in_range(jiffies,
2944 slave_last_rx(bond, slave) - delta_in_ticks,
2945 slave_last_rx(bond, slave) + delta_in_ticks)) {
2946
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002947 slave->new_link = BOND_LINK_UP;
2948 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002951 continue;
2952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002954 /*
2955 * Give slaves 2*delta after being enslaved or made
2956 * active. This avoids bouncing, as the last receive
2957 * times need a full ARP monitor cycle to be updated.
2958 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002959 if (time_in_range(jiffies,
2960 slave->jiffies - delta_in_ticks,
2961 slave->jiffies + 2 * delta_in_ticks))
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002962 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002964 /*
2965 * Backup slave is down if:
2966 * - No current_arp_slave AND
2967 * - more than 3*delta since last receive AND
2968 * - the bond has an IP address
2969 *
2970 * Note: a non-null current_arp_slave indicates
2971 * the curr_active_slave went down and we are
2972 * searching for a new one; under this condition
2973 * we only take the curr_active_slave down - this
2974 * gives each slave a chance to tx/rx traffic
2975 * before being taken out
2976 */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002977 if (!bond_is_active_slave(slave) &&
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002978 !bond->current_arp_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002979 !time_in_range(jiffies,
2980 slave_last_rx(bond, slave) - delta_in_ticks,
2981 slave_last_rx(bond, slave) + 3 * delta_in_ticks)) {
2982
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002983 slave->new_link = BOND_LINK_DOWN;
2984 commit++;
2985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002987 /*
2988 * Active slave is down if:
2989 * - more than 2*delta since transmitting OR
2990 * - (more than 2*delta since receive AND
2991 * the bond has an IP address)
2992 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002993 trans_start = dev_trans_start(slave->dev);
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002994 if (bond_is_active_slave(slave) &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002995 (!time_in_range(jiffies,
2996 trans_start - delta_in_ticks,
2997 trans_start + 2 * delta_in_ticks) ||
2998 !time_in_range(jiffies,
2999 slave_last_rx(bond, slave) - delta_in_ticks,
3000 slave_last_rx(bond, slave) + 2 * delta_in_ticks))) {
3001
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003002 slave->new_link = BOND_LINK_DOWN;
3003 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 }
3005 }
3006
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003007 return commit;
3008}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003010/*
3011 * Called to commit link state changes noted by inspection step of
3012 * active-backup mode ARP monitor.
3013 *
3014 * Called with RTNL and bond->lock for read.
3015 */
3016static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
3017{
3018 struct slave *slave;
3019 int i;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003020 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003022 bond_for_each_slave(bond, slave, i) {
3023 switch (slave->new_link) {
3024 case BOND_LINK_NOCHANGE:
3025 continue;
3026
3027 case BOND_LINK_UP:
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003028 trans_start = dev_trans_start(slave->dev);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003029 if ((!bond->curr_active_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003030 time_in_range(jiffies,
3031 trans_start - delta_in_ticks,
3032 trans_start + delta_in_ticks)) ||
Jiri Pirkob9f60252009-08-31 11:09:38 +00003033 bond->curr_active_slave != slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003034 slave->link = BOND_LINK_UP;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003035 bond->current_arp_slave = NULL;
3036
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003037 pr_info("%s: link status definitely up for interface %s.\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003038 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003039
Jiri Pirkob9f60252009-08-31 11:09:38 +00003040 if (!bond->curr_active_slave ||
3041 (slave == bond->primary_slave))
3042 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003043
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003044 }
3045
Jiri Pirkob9f60252009-08-31 11:09:38 +00003046 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003047
3048 case BOND_LINK_DOWN:
3049 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003052 slave->link = BOND_LINK_DOWN;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003053 bond_set_slave_inactive_flags(slave);
3054
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003055 pr_info("%s: link status definitely down for interface %s, disabling it\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003056 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003057
3058 if (slave == bond->curr_active_slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003059 bond->current_arp_slave = NULL;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003060 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003061 }
Jiri Pirkob9f60252009-08-31 11:09:38 +00003062
3063 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003064
3065 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003066 pr_err("%s: impossible: new_link %d on slave %s\n",
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003067 bond->dev->name, slave->new_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 slave->dev->name);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003069 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Jiri Pirkob9f60252009-08-31 11:09:38 +00003072do_failover:
3073 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00003074 block_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003075 write_lock_bh(&bond->curr_slave_lock);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003076 bond_select_active_slave(bond);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003077 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00003078 unblock_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003079 }
3080
3081 bond_set_carrier(bond);
3082}
3083
3084/*
3085 * Send ARP probes for active-backup mode ARP monitor.
3086 *
3087 * Called with bond->lock held for read.
3088 */
3089static void bond_ab_arp_probe(struct bonding *bond)
3090{
3091 struct slave *slave;
3092 int i;
3093
3094 read_lock(&bond->curr_slave_lock);
3095
3096 if (bond->current_arp_slave && bond->curr_active_slave)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003097 pr_info("PROBE: c_arp %s && cas %s BAD\n",
3098 bond->current_arp_slave->dev->name,
3099 bond->curr_active_slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003100
3101 if (bond->curr_active_slave) {
3102 bond_arp_send_all(bond, bond->curr_active_slave);
3103 read_unlock(&bond->curr_slave_lock);
3104 return;
3105 }
3106
3107 read_unlock(&bond->curr_slave_lock);
3108
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 /* if we don't have a curr_active_slave, search for the next available
3110 * backup slave from the current_arp_slave and make it the candidate
3111 * for becoming the curr_active_slave
3112 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003113
3114 if (!bond->current_arp_slave) {
3115 bond->current_arp_slave = bond->first_slave;
3116 if (!bond->current_arp_slave)
3117 return;
3118 }
3119
3120 bond_set_slave_inactive_flags(bond->current_arp_slave);
3121
3122 /* search for next candidate */
3123 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3124 if (IS_UP(slave->dev)) {
3125 slave->link = BOND_LINK_BACK;
3126 bond_set_slave_active_flags(slave);
3127 bond_arp_send_all(bond, slave);
3128 slave->jiffies = jiffies;
3129 bond->current_arp_slave = slave;
3130 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 }
3132
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003133 /* if the link state is up at this point, we
3134 * mark it down - this can happen if we have
3135 * simultaneous link failures and
3136 * reselect_active_interface doesn't make this
3137 * one the current slave so it is still marked
3138 * up when it is actually down
3139 */
3140 if (slave->link == BOND_LINK_UP) {
3141 slave->link = BOND_LINK_DOWN;
3142 if (slave->link_failure_count < UINT_MAX)
3143 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003145 bond_set_slave_inactive_flags(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003147 pr_info("%s: backup interface %s is now down.\n",
3148 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 }
3150 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003151}
3152
3153void bond_activebackup_arp_mon(struct work_struct *work)
3154{
3155 struct bonding *bond = container_of(work, struct bonding,
3156 arp_work.work);
3157 int delta_in_ticks;
3158
3159 read_lock(&bond->lock);
3160
3161 if (bond->kill_timers)
3162 goto out;
3163
3164 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3165
3166 if (bond->slave_cnt == 0)
3167 goto re_arm;
3168
3169 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3170 read_unlock(&bond->lock);
3171 rtnl_lock();
3172 read_lock(&bond->lock);
3173
3174 bond_ab_arp_commit(bond, delta_in_ticks);
3175
3176 read_unlock(&bond->lock);
3177 rtnl_unlock();
3178 read_lock(&bond->lock);
3179 }
3180
3181 bond_ab_arp_probe(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
3183re_arm:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003184 if (bond->params.arp_interval)
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003185 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186out:
3187 read_unlock(&bond->lock);
3188}
3189
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190/*-------------------------- netdev event handling --------------------------*/
3191
3192/*
3193 * Change device name
3194 */
3195static int bond_event_changename(struct bonding *bond)
3196{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 bond_remove_proc_entry(bond);
3198 bond_create_proc_entry(bond);
Stephen Hemminger7e083842009-06-12 19:02:46 +00003199
Taku Izumif073c7c2010-12-09 15:17:13 +00003200 bond_debug_reregister(bond);
3201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 return NOTIFY_DONE;
3203}
3204
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003205static int bond_master_netdev_event(unsigned long event,
3206 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207{
Wang Chen454d7c92008-11-12 23:37:49 -08003208 struct bonding *event_bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
3210 switch (event) {
3211 case NETDEV_CHANGENAME:
3212 return bond_event_changename(event_bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 default:
3214 break;
3215 }
3216
3217 return NOTIFY_DONE;
3218}
3219
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003220static int bond_slave_netdev_event(unsigned long event,
3221 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222{
3223 struct net_device *bond_dev = slave_dev->master;
Wang Chen454d7c92008-11-12 23:37:49 -08003224 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
3226 switch (event) {
3227 case NETDEV_UNREGISTER:
3228 if (bond_dev) {
Jay Vosburgh1284cd32007-10-15 16:44:27 -07003229 if (bond->setup_by_slave)
3230 bond_release_and_destroy(bond_dev, slave_dev);
3231 else
3232 bond_release(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 }
3234 break;
3235 case NETDEV_CHANGE:
Jay Vosburgh17d04502009-03-18 18:38:25 -07003236 if (bond->params.mode == BOND_MODE_8023AD || bond_is_lb(bond)) {
3237 struct slave *slave;
3238
3239 slave = bond_get_slave_by_dev(bond, slave_dev);
3240 if (slave) {
David Decotigny5d305302011-04-13 15:22:31 +00003241 u32 old_speed = slave->speed;
David Decotigny65cce192011-04-13 15:22:30 +00003242 u8 old_duplex = slave->duplex;
Jay Vosburgh17d04502009-03-18 18:38:25 -07003243
3244 bond_update_speed_duplex(slave);
3245
3246 if (bond_is_lb(bond))
3247 break;
3248
3249 if (old_speed != slave->speed)
3250 bond_3ad_adapter_speed_changed(slave);
3251 if (old_duplex != slave->duplex)
3252 bond_3ad_adapter_duplex_changed(slave);
3253 }
3254 }
3255
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 break;
3257 case NETDEV_DOWN:
3258 /*
3259 * ... Or is it this?
3260 */
3261 break;
3262 case NETDEV_CHANGEMTU:
3263 /*
3264 * TODO: Should slaves be allowed to
3265 * independently alter their MTU? For
3266 * an active-backup bond, slaves need
3267 * not be the same type of device, so
3268 * MTUs may vary. For other modes,
3269 * slaves arguably should have the
3270 * same MTUs. To do this, we'd need to
3271 * take over the slave's change_mtu
3272 * function for the duration of their
3273 * servitude.
3274 */
3275 break;
3276 case NETDEV_CHANGENAME:
3277 /*
3278 * TODO: handle changing the primary's name
3279 */
3280 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003281 case NETDEV_FEAT_CHANGE:
3282 bond_compute_features(bond);
3283 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 default:
3285 break;
3286 }
3287
3288 return NOTIFY_DONE;
3289}
3290
3291/*
3292 * bond_netdev_event: handle netdev notifier chain events.
3293 *
3294 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003295 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 * locks for us to safely manipulate the slave devices (RTNL lock,
3297 * dev_probe_lock).
3298 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003299static int bond_netdev_event(struct notifier_block *this,
3300 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301{
3302 struct net_device *event_dev = (struct net_device *)ptr;
3303
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003304 pr_debug("event_dev: %s, event: %lx\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003305 event_dev ? event_dev->name : "None",
3306 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003308 if (!(event_dev->priv_flags & IFF_BONDING))
3309 return NOTIFY_DONE;
3310
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 if (event_dev->flags & IFF_MASTER) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003312 pr_debug("IFF_MASTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 return bond_master_netdev_event(event, event_dev);
3314 }
3315
3316 if (event_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003317 pr_debug("IFF_SLAVE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 return bond_slave_netdev_event(event, event_dev);
3319 }
3320
3321 return NOTIFY_DONE;
3322}
3323
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003324/*
3325 * bond_inetaddr_event: handle inetaddr notifier chain events.
3326 *
3327 * We keep track of device IPs primarily to use as source addresses in
3328 * ARP monitor probes (rather than spewing out broadcasts all the time).
3329 *
3330 * We track one IP for the main device (if it has one), plus one per VLAN.
3331 */
3332static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3333{
3334 struct in_ifaddr *ifa = ptr;
3335 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003336 struct bond_net *bn = net_generic(dev_net(event_dev), bond_net_id);
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003337 struct bonding *bond;
3338 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003339
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003340 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003341 if (bond->dev == event_dev) {
3342 switch (event) {
3343 case NETDEV_UP:
3344 bond->master_ip = ifa->ifa_local;
3345 return NOTIFY_OK;
3346 case NETDEV_DOWN:
3347 bond->master_ip = bond_glean_dev_ip(bond->dev);
3348 return NOTIFY_OK;
3349 default:
3350 return NOTIFY_DONE;
3351 }
3352 }
3353
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003354 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +00003355 if (!bond->vlgrp)
3356 continue;
Dan Aloni5c15bde2007-03-02 20:44:51 -08003357 vlan_dev = vlan_group_get_device(bond->vlgrp, vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003358 if (vlan_dev == event_dev) {
3359 switch (event) {
3360 case NETDEV_UP:
3361 vlan->vlan_ip = ifa->ifa_local;
3362 return NOTIFY_OK;
3363 case NETDEV_DOWN:
3364 vlan->vlan_ip =
3365 bond_glean_dev_ip(vlan_dev);
3366 return NOTIFY_OK;
3367 default:
3368 return NOTIFY_DONE;
3369 }
3370 }
3371 }
3372 }
3373 return NOTIFY_DONE;
3374}
3375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376static struct notifier_block bond_netdev_notifier = {
3377 .notifier_call = bond_netdev_event,
3378};
3379
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003380static struct notifier_block bond_inetaddr_notifier = {
3381 .notifier_call = bond_inetaddr_event,
3382};
3383
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003384/*---------------------------- Hashing Policies -----------------------------*/
3385
3386/*
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003387 * Hash for the output device based upon layer 2 and layer 3 data. If
3388 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3389 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003390static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003391{
3392 struct ethhdr *data = (struct ethhdr *)skb->data;
3393 struct iphdr *iph = ip_hdr(skb);
3394
Brian Haleyf14c4e42008-09-02 10:08:08 -04003395 if (skb->protocol == htons(ETH_P_IP)) {
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003396 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
Jasper Spaansd3da6832009-10-23 04:08:46 +00003397 (data->h_dest[5] ^ data->h_source[5])) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003398 }
3399
Jasper Spaansd3da6832009-10-23 04:08:46 +00003400 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003401}
3402
3403/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003404 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003405 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3406 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3407 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003408static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003409{
3410 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003411 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003412 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003413 int layer4_xor = 0;
3414
Brian Haleyf14c4e42008-09-02 10:08:08 -04003415 if (skb->protocol == htons(ETH_P_IP)) {
3416 if (!(iph->frag_off & htons(IP_MF|IP_OFFSET)) &&
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003417 (iph->protocol == IPPROTO_TCP ||
3418 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003419 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003420 }
3421 return (layer4_xor ^
3422 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3423
3424 }
3425
Jasper Spaansd3da6832009-10-23 04:08:46 +00003426 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003427}
3428
3429/*
3430 * Hash for the output device based upon layer 2 data
3431 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003432static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003433{
3434 struct ethhdr *data = (struct ethhdr *)skb->data;
3435
Jasper Spaansd3da6832009-10-23 04:08:46 +00003436 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003437}
3438
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439/*-------------------------- Device entry points ----------------------------*/
3440
3441static int bond_open(struct net_device *bond_dev)
3442{
Wang Chen454d7c92008-11-12 23:37:49 -08003443 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
3445 bond->kill_timers = 0;
3446
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003447 INIT_DELAYED_WORK(&bond->mcast_work, bond_resend_igmp_join_requests_delayed);
3448
Holger Eitzenberger58402052008-12-09 23:07:13 -08003449 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 /* bond_alb_initialize must be called before the timer
3451 * is started.
3452 */
3453 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3454 /* something went wrong - fail the open operation */
stephen hemmingerb4739462010-01-25 23:34:15 +00003455 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 }
3457
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003458 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3459 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 }
3461
3462 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003463 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3464 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 }
3466
3467 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003468 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3469 INIT_DELAYED_WORK(&bond->arp_work,
3470 bond_activebackup_arp_mon);
3471 else
3472 INIT_DELAYED_WORK(&bond->arp_work,
3473 bond_loadbalance_arp_mon);
3474
3475 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003476 if (bond->params.arp_validate)
Jiri Pirko3aba8912011-04-19 03:48:16 +00003477 bond->recv_probe = bond_arp_rcv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 }
3479
3480 if (bond->params.mode == BOND_MODE_8023AD) {
Adrian Bunka40745f2007-10-24 18:27:43 +02003481 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003482 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 /* register to receive LACPDUs */
Jiri Pirko3aba8912011-04-19 03:48:16 +00003484 bond->recv_probe = bond_3ad_lacpdu_recv;
Jay Vosburghfd989c82008-11-04 17:51:16 -08003485 bond_3ad_initiate_agg_selection(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 }
3487
3488 return 0;
3489}
3490
3491static int bond_close(struct net_device *bond_dev)
3492{
Wang Chen454d7c92008-11-12 23:37:49 -08003493 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 write_lock_bh(&bond->lock);
3496
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 /* signal timers not to re-arm */
3498 bond->kill_timers = 1;
3499
3500 write_unlock_bh(&bond->lock);
3501
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003503 cancel_delayed_work(&bond->mii_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 }
3505
3506 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003507 cancel_delayed_work(&bond->arp_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 }
3509
3510 switch (bond->params.mode) {
3511 case BOND_MODE_8023AD:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003512 cancel_delayed_work(&bond->ad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 break;
3514 case BOND_MODE_TLB:
3515 case BOND_MODE_ALB:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003516 cancel_delayed_work(&bond->alb_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 break;
3518 default:
3519 break;
3520 }
3521
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003522 if (delayed_work_pending(&bond->mcast_work))
3523 cancel_delayed_work(&bond->mcast_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
Holger Eitzenberger58402052008-12-09 23:07:13 -08003525 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 /* Must be called only after all
3527 * slaves have been released
3528 */
3529 bond_alb_deinitialize(bond);
3530 }
Jiri Pirko3aba8912011-04-19 03:48:16 +00003531 bond->recv_probe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
3533 return 0;
3534}
3535
Eric Dumazet28172732010-07-07 14:58:56 -07003536static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3537 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538{
Wang Chen454d7c92008-11-12 23:37:49 -08003539 struct bonding *bond = netdev_priv(bond_dev);
Eric Dumazet28172732010-07-07 14:58:56 -07003540 struct rtnl_link_stats64 temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 struct slave *slave;
3542 int i;
3543
Eric Dumazet28172732010-07-07 14:58:56 -07003544 memset(stats, 0, sizeof(*stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
3546 read_lock_bh(&bond->lock);
3547
3548 bond_for_each_slave(bond, slave, i) {
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00003549 const struct rtnl_link_stats64 *sstats =
Eric Dumazet28172732010-07-07 14:58:56 -07003550 dev_get_stats(slave->dev, &temp);
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08003551
Eric Dumazet28172732010-07-07 14:58:56 -07003552 stats->rx_packets += sstats->rx_packets;
3553 stats->rx_bytes += sstats->rx_bytes;
3554 stats->rx_errors += sstats->rx_errors;
3555 stats->rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
Eric Dumazet28172732010-07-07 14:58:56 -07003557 stats->tx_packets += sstats->tx_packets;
3558 stats->tx_bytes += sstats->tx_bytes;
3559 stats->tx_errors += sstats->tx_errors;
3560 stats->tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Eric Dumazet28172732010-07-07 14:58:56 -07003562 stats->multicast += sstats->multicast;
3563 stats->collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Eric Dumazet28172732010-07-07 14:58:56 -07003565 stats->rx_length_errors += sstats->rx_length_errors;
3566 stats->rx_over_errors += sstats->rx_over_errors;
3567 stats->rx_crc_errors += sstats->rx_crc_errors;
3568 stats->rx_frame_errors += sstats->rx_frame_errors;
3569 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3570 stats->rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Eric Dumazet28172732010-07-07 14:58:56 -07003572 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3573 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3574 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3575 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3576 stats->tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 }
3578
3579 read_unlock_bh(&bond->lock);
3580
3581 return stats;
3582}
3583
3584static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3585{
3586 struct net_device *slave_dev = NULL;
3587 struct ifbond k_binfo;
3588 struct ifbond __user *u_binfo = NULL;
3589 struct ifslave k_sinfo;
3590 struct ifslave __user *u_sinfo = NULL;
3591 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 int res = 0;
3593
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003594 pr_debug("bond_ioctl: master=%s, cmd=%d\n", bond_dev->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
3596 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 case SIOCGMIIPHY:
3598 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003599 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003601
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 mii->phy_id = 0;
3603 /* Fall Through */
3604 case SIOCGMIIREG:
3605 /*
3606 * We do this again just in case we were called by SIOCGMIIREG
3607 * instead of SIOCGMIIPHY.
3608 */
3609 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003610 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
3614 if (mii->reg_num == 1) {
Wang Chen454d7c92008-11-12 23:37:49 -08003615 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 mii->val_out = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003617 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 read_lock(&bond->curr_slave_lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003619 if (netif_carrier_ok(bond->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 mii->val_out = BMSR_LSTATUS;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003621
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 read_unlock(&bond->curr_slave_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003623 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 }
3625
3626 return 0;
3627 case BOND_INFO_QUERY_OLD:
3628 case SIOCBONDINFOQUERY:
3629 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3630
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003631 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
3634 res = bond_info_query(bond_dev, &k_binfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003635 if (res == 0 &&
3636 copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
3637 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
3639 return res;
3640 case BOND_SLAVE_INFO_QUERY_OLD:
3641 case SIOCBONDSLAVEINFOQUERY:
3642 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3643
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003644 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
3647 res = bond_slave_info_query(bond_dev, &k_sinfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003648 if (res == 0 &&
3649 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
3650 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651
3652 return res;
3653 default:
3654 /* Go on */
3655 break;
3656 }
3657
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003658 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003661 slave_dev = dev_get_by_name(dev_net(bond_dev), ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003663 pr_debug("slave_dev=%p:\n", slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003665 if (!slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 res = -ENODEV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003667 else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003668 pr_debug("slave_dev->name=%s:\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 switch (cmd) {
3670 case BOND_ENSLAVE_OLD:
3671 case SIOCBONDENSLAVE:
3672 res = bond_enslave(bond_dev, slave_dev);
3673 break;
3674 case BOND_RELEASE_OLD:
3675 case SIOCBONDRELEASE:
3676 res = bond_release(bond_dev, slave_dev);
3677 break;
3678 case BOND_SETHWADDR_OLD:
3679 case SIOCBONDSETHWADDR:
3680 res = bond_sethwaddr(bond_dev, slave_dev);
3681 break;
3682 case BOND_CHANGE_ACTIVE_OLD:
3683 case SIOCBONDCHANGEACTIVE:
3684 res = bond_ioctl_change_active(bond_dev, slave_dev);
3685 break;
3686 default:
3687 res = -EOPNOTSUPP;
3688 }
3689
3690 dev_put(slave_dev);
3691 }
3692
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 return res;
3694}
3695
Jiri Pirko22bedad2010-04-01 21:22:57 +00003696static bool bond_addr_in_mc_list(unsigned char *addr,
3697 struct netdev_hw_addr_list *list,
3698 int addrlen)
3699{
3700 struct netdev_hw_addr *ha;
3701
3702 netdev_hw_addr_list_for_each(ha, list)
3703 if (!memcmp(ha->addr, addr, addrlen))
3704 return true;
3705
3706 return false;
3707}
3708
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709static void bond_set_multicast_list(struct net_device *bond_dev)
3710{
Wang Chen454d7c92008-11-12 23:37:49 -08003711 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad2010-04-01 21:22:57 +00003712 struct netdev_hw_addr *ha;
3713 bool found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 /*
3716 * Do promisc before checking multicast_mode
3717 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003718 if ((bond_dev->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07003719 /*
3720 * FIXME: Need to handle the error when one of the multi-slaves
3721 * encounters error.
3722 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 bond_set_promiscuity(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003725
3726 if (!(bond_dev->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 bond_set_promiscuity(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003728
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
3730 /* set allmulti flag to slaves */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003731 if ((bond_dev->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI))
Wang Chen7e1a1ac2008-07-14 20:51:36 -07003732 /*
3733 * FIXME: Need to handle the error when one of the multi-slaves
3734 * encounters error.
3735 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 bond_set_allmulti(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003738
3739 if (!(bond_dev->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 bond_set_allmulti(bond, -1);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003741
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08003743 read_lock(&bond->lock);
3744
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 bond->flags = bond_dev->flags;
3746
3747 /* looking for addresses to add to slaves' mc list */
Jiri Pirko22bedad2010-04-01 21:22:57 +00003748 netdev_for_each_mc_addr(ha, bond_dev) {
3749 found = bond_addr_in_mc_list(ha->addr, &bond->mc_list,
3750 bond_dev->addr_len);
3751 if (!found)
3752 bond_mc_add(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 }
3754
3755 /* looking for addresses to delete from slaves' list */
Jiri Pirko22bedad2010-04-01 21:22:57 +00003756 netdev_hw_addr_list_for_each(ha, &bond->mc_list) {
3757 found = bond_addr_in_mc_list(ha->addr, &bond_dev->mc,
3758 bond_dev->addr_len);
3759 if (!found)
3760 bond_mc_del(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 }
3762
3763 /* save master's multicast list */
Jiri Pirko22bedad2010-04-01 21:22:57 +00003764 __hw_addr_flush(&bond->mc_list);
3765 __hw_addr_add_multiple(&bond->mc_list, &bond_dev->mc,
3766 bond_dev->addr_len, NETDEV_HW_ADDR_T_MULTICAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08003768 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769}
3770
Stephen Hemminger00829822008-11-20 20:14:53 -08003771static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
3772{
3773 struct bonding *bond = netdev_priv(dev);
3774 struct slave *slave = bond->first_slave;
3775
3776 if (slave) {
3777 const struct net_device_ops *slave_ops
3778 = slave->dev->netdev_ops;
3779 if (slave_ops->ndo_neigh_setup)
Patrick McHardy72e22402009-03-05 01:57:44 -08003780 return slave_ops->ndo_neigh_setup(slave->dev, parms);
Stephen Hemminger00829822008-11-20 20:14:53 -08003781 }
3782 return 0;
3783}
3784
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785/*
3786 * Change the MTU of all of a master's slaves to match the master
3787 */
3788static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3789{
Wang Chen454d7c92008-11-12 23:37:49 -08003790 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 struct slave *slave, *stop_at;
3792 int res = 0;
3793 int i;
3794
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003795 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003796 (bond_dev ? bond_dev->name : "None"), new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
3798 /* Can't hold bond->lock with bh disabled here since
3799 * some base drivers panic. On the other hand we can't
3800 * hold bond->lock without bh disabled because we'll
3801 * deadlock. The only solution is to rely on the fact
3802 * that we're under rtnl_lock here, and the slaves
3803 * list won't change. This doesn't solve the problem
3804 * of setting the slave's MTU while it is
3805 * transmitting, but the assumption is that the base
3806 * driver can handle that.
3807 *
3808 * TODO: figure out a way to safely iterate the slaves
3809 * list, but without holding a lock around the actual
3810 * call to the base driver.
3811 */
3812
3813 bond_for_each_slave(bond, slave, i) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003814 pr_debug("s %p s->p %p c_m %p\n",
3815 slave,
3816 slave->prev,
3817 slave->dev->netdev_ops->ndo_change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08003818
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 res = dev_set_mtu(slave->dev, new_mtu);
3820
3821 if (res) {
3822 /* If we failed to set the slave's mtu to the new value
3823 * we must abort the operation even in ACTIVE_BACKUP
3824 * mode, because if we allow the backup slaves to have
3825 * different mtu values than the active slave we'll
3826 * need to change their mtu when doing a failover. That
3827 * means changing their mtu from timer context, which
3828 * is probably not a good idea.
3829 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003830 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 goto unwind;
3832 }
3833 }
3834
3835 bond_dev->mtu = new_mtu;
3836
3837 return 0;
3838
3839unwind:
3840 /* unwind from head to the slave that failed */
3841 stop_at = slave;
3842 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3843 int tmp_res;
3844
3845 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
3846 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003847 pr_debug("unwind err %d dev %s\n",
3848 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 }
3850 }
3851
3852 return res;
3853}
3854
3855/*
3856 * Change HW address
3857 *
3858 * Note that many devices must be down to change the HW address, and
3859 * downing the master releases all slaves. We can make bonds full of
3860 * bonding devices to test this, however.
3861 */
3862static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
3863{
Wang Chen454d7c92008-11-12 23:37:49 -08003864 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 struct sockaddr *sa = addr, tmp_sa;
3866 struct slave *slave, *stop_at;
3867 int res = 0;
3868 int i;
3869
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08003870 if (bond->params.mode == BOND_MODE_ALB)
3871 return bond_alb_set_mac_address(bond_dev, addr);
3872
3873
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003874 pr_debug("bond=%p, name=%s\n",
3875 bond, bond_dev ? bond_dev->name : "None");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876
Jay Vosburghdd957c52007-10-09 19:57:24 -07003877 /*
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07003878 * If fail_over_mac is set to active, do nothing and return
3879 * success. Returning an error causes ifenslave to fail.
Jay Vosburghdd957c52007-10-09 19:57:24 -07003880 */
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07003881 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
Jay Vosburghdd957c52007-10-09 19:57:24 -07003882 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07003883
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003884 if (!is_valid_ether_addr(sa->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
3887 /* Can't hold bond->lock with bh disabled here since
3888 * some base drivers panic. On the other hand we can't
3889 * hold bond->lock without bh disabled because we'll
3890 * deadlock. The only solution is to rely on the fact
3891 * that we're under rtnl_lock here, and the slaves
3892 * list won't change. This doesn't solve the problem
3893 * of setting the slave's hw address while it is
3894 * transmitting, but the assumption is that the base
3895 * driver can handle that.
3896 *
3897 * TODO: figure out a way to safely iterate the slaves
3898 * list, but without holding a lock around the actual
3899 * call to the base driver.
3900 */
3901
3902 bond_for_each_slave(bond, slave, i) {
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08003903 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003904 pr_debug("slave %p %s\n", slave, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08003906 if (slave_ops->ndo_set_mac_address == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 res = -EOPNOTSUPP;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003908 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 goto unwind;
3910 }
3911
3912 res = dev_set_mac_address(slave->dev, addr);
3913 if (res) {
3914 /* TODO: consider downing the slave
3915 * and retry ?
3916 * User should expect communications
3917 * breakage anyway until ARP finish
3918 * updating, so...
3919 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003920 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 goto unwind;
3922 }
3923 }
3924
3925 /* success */
3926 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
3927 return 0;
3928
3929unwind:
3930 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
3931 tmp_sa.sa_family = bond_dev->type;
3932
3933 /* unwind from head to the slave that failed */
3934 stop_at = slave;
3935 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3936 int tmp_res;
3937
3938 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
3939 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003940 pr_debug("unwind err %d dev %s\n",
3941 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 }
3943 }
3944
3945 return res;
3946}
3947
3948static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
3949{
Wang Chen454d7c92008-11-12 23:37:49 -08003950 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 struct slave *slave, *start_at;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07003952 int i, slave_no, res = 1;
Andy Gospodareka2fd9402010-03-25 14:49:05 +00003953 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
3955 read_lock(&bond->lock);
3956
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003957 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 goto out;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07003959 /*
Andy Gospodareka2fd9402010-03-25 14:49:05 +00003960 * Start with the curr_active_slave that joined the bond as the
3961 * default for sending IGMP traffic. For failover purposes one
3962 * needs to maintain some consistency for the interface that will
3963 * send the join/membership reports. The curr_active_slave found
3964 * will send all of this type of traffic.
Jay Vosburghcf5f9042007-10-17 17:37:47 -07003965 */
Eric Dumazet00ae7022010-03-30 23:08:37 +00003966 if ((iph->protocol == IPPROTO_IGMP) &&
Andy Gospodareka2fd9402010-03-25 14:49:05 +00003967 (skb->protocol == htons(ETH_P_IP))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968
Andy Gospodareka2fd9402010-03-25 14:49:05 +00003969 read_lock(&bond->curr_slave_lock);
3970 slave = bond->curr_active_slave;
3971 read_unlock(&bond->curr_slave_lock);
3972
3973 if (!slave)
3974 goto out;
3975 } else {
3976 /*
3977 * Concurrent TX may collide on rr_tx_counter; we accept
3978 * that as being rare enough not to justify using an
3979 * atomic op here.
3980 */
3981 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
3982
3983 bond_for_each_slave(bond, slave, i) {
3984 slave_no--;
3985 if (slave_no < 0)
3986 break;
3987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 }
3989
Jay Vosburghcf5f9042007-10-17 17:37:47 -07003990 start_at = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 bond_for_each_slave_from(bond, slave, i, start_at) {
3992 if (IS_UP(slave->dev) &&
3993 (slave->link == BOND_LINK_UP) &&
Jiri Pirkoe30bc062011-03-12 03:14:37 +00003994 bond_is_active_slave(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 res = bond_dev_queue_xmit(bond, skb, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 break;
3997 }
3998 }
3999
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000out:
4001 if (res) {
4002 /* no suitable interface, frame not sent */
4003 dev_kfree_skb(skb);
4004 }
4005 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004006 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007}
4008
John W. Linville075897c2005-09-28 17:50:53 -04004009
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010/*
4011 * in active-backup mode, we know that bond->curr_active_slave is always valid if
4012 * the bond has a usable interface.
4013 */
4014static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
4015{
Wang Chen454d7c92008-11-12 23:37:49 -08004016 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 int res = 1;
4018
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 read_lock(&bond->lock);
4020 read_lock(&bond->curr_slave_lock);
4021
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004022 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
John W. Linville075897c2005-09-28 17:50:53 -04004025 if (!bond->curr_active_slave)
4026 goto out;
4027
John W. Linville075897c2005-09-28 17:50:53 -04004028 res = bond_dev_queue_xmit(bond, skb, bond->curr_active_slave->dev);
4029
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004031 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 /* no suitable interface, frame not sent */
4033 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004034
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 read_unlock(&bond->curr_slave_lock);
4036 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004037 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038}
4039
4040/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004041 * In bond_xmit_xor() , we determine the output device by using a pre-
4042 * determined xmit_hash_policy(), If the selected device is not enabled,
4043 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 */
4045static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4046{
Wang Chen454d7c92008-11-12 23:37:49 -08004047 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 struct slave *slave, *start_at;
4049 int slave_no;
4050 int i;
4051 int res = 1;
4052
4053 read_lock(&bond->lock);
4054
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004055 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057
Jasper Spaansa361c832009-10-23 04:09:24 +00004058 slave_no = bond->xmit_hash_policy(skb, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
4060 bond_for_each_slave(bond, slave, i) {
4061 slave_no--;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004062 if (slave_no < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 }
4065
4066 start_at = slave;
4067
4068 bond_for_each_slave_from(bond, slave, i, start_at) {
4069 if (IS_UP(slave->dev) &&
4070 (slave->link == BOND_LINK_UP) &&
Jiri Pirkoe30bc062011-03-12 03:14:37 +00004071 bond_is_active_slave(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4073 break;
4074 }
4075 }
4076
4077out:
4078 if (res) {
4079 /* no suitable interface, frame not sent */
4080 dev_kfree_skb(skb);
4081 }
4082 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004083 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084}
4085
4086/*
4087 * in broadcast mode, we send everything to all usable interfaces.
4088 */
4089static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4090{
Wang Chen454d7c92008-11-12 23:37:49 -08004091 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 struct slave *slave, *start_at;
4093 struct net_device *tx_dev = NULL;
4094 int i;
4095 int res = 1;
4096
4097 read_lock(&bond->lock);
4098
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004099 if (!BOND_IS_OK(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
4102 read_lock(&bond->curr_slave_lock);
4103 start_at = bond->curr_active_slave;
4104 read_unlock(&bond->curr_slave_lock);
4105
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004106 if (!start_at)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
4109 bond_for_each_slave_from(bond, slave, i, start_at) {
4110 if (IS_UP(slave->dev) &&
4111 (slave->link == BOND_LINK_UP) &&
Jiri Pirkoe30bc062011-03-12 03:14:37 +00004112 bond_is_active_slave(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 if (tx_dev) {
4114 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4115 if (!skb2) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004116 pr_err("%s: Error: bond_xmit_broadcast(): skb_clone() failed\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08004117 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 continue;
4119 }
4120
4121 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4122 if (res) {
4123 dev_kfree_skb(skb2);
4124 continue;
4125 }
4126 }
4127 tx_dev = slave->dev;
4128 }
4129 }
4130
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004131 if (tx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 res = bond_dev_queue_xmit(bond, skb, tx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
4134out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004135 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 /* no suitable interface, frame not sent */
4137 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004138
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 /* frame sent to all suitable interfaces */
4140 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07004141 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142}
4143
4144/*------------------------- Device initialization ---------------------------*/
4145
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004146static void bond_set_xmit_hash_policy(struct bonding *bond)
4147{
4148 switch (bond->params.xmit_policy) {
4149 case BOND_XMIT_POLICY_LAYER23:
4150 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4151 break;
4152 case BOND_XMIT_POLICY_LAYER34:
4153 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4154 break;
4155 case BOND_XMIT_POLICY_LAYER2:
4156 default:
4157 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4158 break;
4159 }
4160}
4161
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004162/*
4163 * Lookup the slave that corresponds to a qid
4164 */
4165static inline int bond_slave_override(struct bonding *bond,
4166 struct sk_buff *skb)
4167{
4168 int i, res = 1;
4169 struct slave *slave = NULL;
4170 struct slave *check_slave;
4171
4172 read_lock(&bond->lock);
4173
4174 if (!BOND_IS_OK(bond) || !skb->queue_mapping)
4175 goto out;
4176
4177 /* Find out if any slaves have the same mapping as this skb. */
4178 bond_for_each_slave(bond, check_slave, i) {
4179 if (check_slave->queue_id == skb->queue_mapping) {
4180 slave = check_slave;
4181 break;
4182 }
4183 }
4184
4185 /* If the slave isn't UP, use default transmit policy. */
4186 if (slave && slave->queue_id && IS_UP(slave->dev) &&
4187 (slave->link == BOND_LINK_UP)) {
4188 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4189 }
4190
4191out:
4192 read_unlock(&bond->lock);
4193 return res;
4194}
4195
4196static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb)
4197{
4198 /*
4199 * This helper function exists to help dev_pick_tx get the correct
Phil Oesterfd0e4352011-03-14 06:22:04 +00004200 * destination queue. Using a helper function skips a call to
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004201 * skb_tx_hash and will put the skbs in the queue we expect on their
4202 * way down to the bonding driver.
4203 */
Phil Oesterfd0e4352011-03-14 06:22:04 +00004204 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
4205
4206 if (unlikely(txq >= dev->real_num_tx_queues)) {
David Decotignyd30ee672011-04-13 15:22:29 +00004207 do {
Phil Oesterfd0e4352011-03-14 06:22:04 +00004208 txq -= dev->real_num_tx_queues;
David Decotignyd30ee672011-04-13 15:22:29 +00004209 } while (txq >= dev->real_num_tx_queues);
Phil Oesterfd0e4352011-03-14 06:22:04 +00004210 }
4211 return txq;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004212}
4213
Stephen Hemminger424efe92009-08-31 19:50:51 +00004214static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
Stephen Hemminger00829822008-11-20 20:14:53 -08004215{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004216 struct bonding *bond = netdev_priv(dev);
4217
Neil Hormane843fa52010-10-13 16:01:50 +00004218 /*
4219 * If we risk deadlock from transmitting this in the
4220 * netpoll path, tell netpoll to queue the frame for later tx
4221 */
4222 if (is_netpoll_tx_blocked(dev))
4223 return NETDEV_TX_BUSY;
4224
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004225 if (TX_QUEUE_OVERRIDE(bond->params.mode)) {
4226 if (!bond_slave_override(bond, skb))
4227 return NETDEV_TX_OK;
4228 }
Stephen Hemminger00829822008-11-20 20:14:53 -08004229
4230 switch (bond->params.mode) {
4231 case BOND_MODE_ROUNDROBIN:
4232 return bond_xmit_roundrobin(skb, dev);
4233 case BOND_MODE_ACTIVEBACKUP:
4234 return bond_xmit_activebackup(skb, dev);
4235 case BOND_MODE_XOR:
4236 return bond_xmit_xor(skb, dev);
4237 case BOND_MODE_BROADCAST:
4238 return bond_xmit_broadcast(skb, dev);
4239 case BOND_MODE_8023AD:
4240 return bond_3ad_xmit_xor(skb, dev);
4241 case BOND_MODE_ALB:
4242 case BOND_MODE_TLB:
4243 return bond_alb_xmit(skb, dev);
4244 default:
4245 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004246 pr_err("%s: Error: Unknown bonding mode %d\n",
4247 dev->name, bond->params.mode);
Stephen Hemminger00829822008-11-20 20:14:53 -08004248 WARN_ON_ONCE(1);
4249 dev_kfree_skb(skb);
4250 return NETDEV_TX_OK;
4251 }
4252}
4253
4254
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255/*
4256 * set bond mode specific net device operations
4257 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004258void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004260 struct net_device *bond_dev = bond->dev;
4261
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 switch (mode) {
4263 case BOND_MODE_ROUNDROBIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 break;
4265 case BOND_MODE_ACTIVEBACKUP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 break;
4267 case BOND_MODE_XOR:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004268 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 break;
4270 case BOND_MODE_BROADCAST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 break;
4272 case BOND_MODE_8023AD:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004273 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004276 /* FALLTHRU */
4277 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 break;
4279 default:
4280 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004281 pr_err("%s: Error: Unknown bonding mode %d\n",
4282 bond_dev->name, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 break;
4284 }
4285}
4286
Jay Vosburgh217df672005-09-26 16:11:50 -07004287static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4288 struct ethtool_drvinfo *drvinfo)
4289{
4290 strncpy(drvinfo->driver, DRV_NAME, 32);
4291 strncpy(drvinfo->version, DRV_VERSION, 32);
4292 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4293}
4294
Jeff Garzik7282d492006-09-13 14:30:00 -04004295static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004296 .get_drvinfo = bond_ethtool_get_drvinfo,
Stephen Hemmingerfa53eba2008-09-13 21:17:09 -04004297 .get_link = ethtool_op_get_link,
4298 .get_tx_csum = ethtool_op_get_tx_csum,
4299 .get_sg = ethtool_op_get_sg,
4300 .get_tso = ethtool_op_get_tso,
4301 .get_ufo = ethtool_op_get_ufo,
4302 .get_flags = ethtool_op_get_flags,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004303};
4304
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004305static const struct net_device_ops bond_netdev_ops = {
Stephen Hemminger181470f2009-06-12 19:02:52 +00004306 .ndo_init = bond_init,
Stephen Hemminger9e716262009-06-12 19:02:47 +00004307 .ndo_uninit = bond_uninit,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004308 .ndo_open = bond_open,
4309 .ndo_stop = bond_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08004310 .ndo_start_xmit = bond_start_xmit,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004311 .ndo_select_queue = bond_select_queue,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00004312 .ndo_get_stats64 = bond_get_stats,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004313 .ndo_do_ioctl = bond_do_ioctl,
4314 .ndo_set_multicast_list = bond_set_multicast_list,
4315 .ndo_change_mtu = bond_change_mtu,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004316 .ndo_set_mac_address = bond_set_mac_address,
Stephen Hemminger00829822008-11-20 20:14:53 -08004317 .ndo_neigh_setup = bond_neigh_setup,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004318 .ndo_vlan_rx_register = bond_vlan_rx_register,
4319 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
4320 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
WANG Congf6dc31a2010-05-06 00:48:51 -07004321#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00004322 .ndo_netpoll_setup = bond_netpoll_setup,
WANG Congf6dc31a2010-05-06 00:48:51 -07004323 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
4324 .ndo_poll_controller = bond_poll_controller,
4325#endif
Jiri Pirko9232ecc2011-02-13 09:33:01 +00004326 .ndo_add_slave = bond_enslave,
4327 .ndo_del_slave = bond_release,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004328};
4329
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004330static void bond_destructor(struct net_device *bond_dev)
4331{
4332 struct bonding *bond = netdev_priv(bond_dev);
4333 if (bond->wq)
4334 destroy_workqueue(bond->wq);
4335 free_netdev(bond_dev);
4336}
4337
Stephen Hemminger181470f2009-06-12 19:02:52 +00004338static void bond_setup(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339{
Wang Chen454d7c92008-11-12 23:37:49 -08004340 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 /* initialize rwlocks */
4343 rwlock_init(&bond->lock);
4344 rwlock_init(&bond->curr_slave_lock);
4345
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00004346 bond->params = bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
4348 /* Initialize pointers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 bond->dev = bond_dev;
4350 INIT_LIST_HEAD(&bond->vlan_list);
4351
4352 /* Initialize the device entry points */
Stephen Hemminger181470f2009-06-12 19:02:52 +00004353 ether_setup(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004354 bond_dev->netdev_ops = &bond_netdev_ops;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004355 bond_dev->ethtool_ops = &bond_ethtool_ops;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004356 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004358 bond_dev->destructor = bond_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
4360 /* Initialize the device options */
4361 bond_dev->tx_queue_len = 0;
4362 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004363 bond_dev->priv_flags |= IFF_BONDING;
Stephen Hemminger181470f2009-06-12 19:02:52 +00004364 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
4365
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 /* At first, we block adding VLANs. That's the only way to
4367 * prevent problems that occur when adding VLANs over an
4368 * empty bond. The block will be removed once non-challenged
4369 * slaves are enslaved.
4370 */
4371 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4372
Herbert Xu932ff272006-06-09 12:20:56 -07004373 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 * transmitting */
4375 bond_dev->features |= NETIF_F_LLTX;
4376
4377 /* By default, we declare the bond to be fully
4378 * VLAN hardware accelerated capable. Special
4379 * care is taken in the various xmit functions
4380 * when there are slaves that are not hw accel
4381 * capable
4382 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 bond_dev->features |= (NETIF_F_HW_VLAN_TX |
4384 NETIF_F_HW_VLAN_RX |
4385 NETIF_F_HW_VLAN_FILTER);
4386
Eric Dumazete6599c22010-09-17 09:25:07 +00004387 /* By default, we enable GRO on bonding devices.
4388 * Actual support requires lowlevel drivers are GRO ready.
4389 */
4390 bond_dev->features |= NETIF_F_GRO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391}
4392
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004393static void bond_work_cancel_all(struct bonding *bond)
4394{
4395 write_lock_bh(&bond->lock);
4396 bond->kill_timers = 1;
4397 write_unlock_bh(&bond->lock);
4398
4399 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
4400 cancel_delayed_work(&bond->mii_work);
4401
4402 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
4403 cancel_delayed_work(&bond->arp_work);
4404
4405 if (bond->params.mode == BOND_MODE_ALB &&
4406 delayed_work_pending(&bond->alb_work))
4407 cancel_delayed_work(&bond->alb_work);
4408
4409 if (bond->params.mode == BOND_MODE_8023AD &&
4410 delayed_work_pending(&bond->ad_work))
4411 cancel_delayed_work(&bond->ad_work);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00004412
4413 if (delayed_work_pending(&bond->mcast_work))
4414 cancel_delayed_work(&bond->mcast_work);
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004415}
4416
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004417/*
4418* Destroy a bonding device.
4419* Must be under rtnl_lock when this function is called.
4420*/
4421static void bond_uninit(struct net_device *bond_dev)
Jay Vosburgha434e432008-10-30 17:41:15 -07004422{
Wang Chen454d7c92008-11-12 23:37:49 -08004423 struct bonding *bond = netdev_priv(bond_dev);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004424 struct vlan_entry *vlan, *tmp;
Jay Vosburgha434e432008-10-30 17:41:15 -07004425
WANG Congf6dc31a2010-05-06 00:48:51 -07004426 bond_netpoll_cleanup(bond_dev);
4427
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004428 /* Release the bonded slaves */
4429 bond_release_all(bond_dev);
4430
Jay Vosburgha434e432008-10-30 17:41:15 -07004431 list_del(&bond->bond_list);
4432
4433 bond_work_cancel_all(bond);
4434
Jay Vosburgha434e432008-10-30 17:41:15 -07004435 bond_remove_proc_entry(bond);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004436
Taku Izumif073c7c2010-12-09 15:17:13 +00004437 bond_debug_unregister(bond);
4438
Jiri Pirko22bedad2010-04-01 21:22:57 +00004439 __hw_addr_flush(&bond->mc_list);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004440
4441 list_for_each_entry_safe(vlan, tmp, &bond->vlan_list, vlan_list) {
4442 list_del(&vlan->vlan_list);
4443 kfree(vlan);
4444 }
Jay Vosburgha434e432008-10-30 17:41:15 -07004445}
4446
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447/*------------------------- Module initialization ---------------------------*/
4448
4449/*
4450 * Convert string input module parms. Accept either the
Jay Vosburghece95f72008-01-17 16:25:01 -08004451 * number of the mode or its string name. A bit complicated because
4452 * some mode names are substrings of other names, and calls from sysfs
4453 * may have whitespace in the name (trailing newlines, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 */
Holger Eitzenberger325dcf72008-12-09 23:10:17 -08004455int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456{
Hannes Eder54b87322009-02-14 11:15:49 +00004457 int modeint = -1, i, rv;
Jay Vosburgha42e5342008-01-29 18:07:43 -08004458 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
Jay Vosburghece95f72008-01-17 16:25:01 -08004459
Jay Vosburgha42e5342008-01-29 18:07:43 -08004460 for (p = (char *)buf; *p; p++)
4461 if (!(isdigit(*p) || isspace(*p)))
4462 break;
4463
4464 if (*p)
Jay Vosburghece95f72008-01-17 16:25:01 -08004465 rv = sscanf(buf, "%20s", modestr);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004466 else
Hannes Eder54b87322009-02-14 11:15:49 +00004467 rv = sscanf(buf, "%d", &modeint);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004468
4469 if (!rv)
4470 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471
4472 for (i = 0; tbl[i].modename; i++) {
Hannes Eder54b87322009-02-14 11:15:49 +00004473 if (modeint == tbl[i].mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 return tbl[i].mode;
Jay Vosburghece95f72008-01-17 16:25:01 -08004475 if (strcmp(modestr, tbl[i].modename) == 0)
4476 return tbl[i].mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 }
4478
4479 return -1;
4480}
4481
4482static int bond_check_params(struct bond_params *params)
4483{
Jiri Pirkoa5499522009-09-25 03:28:09 +00004484 int arp_validate_value, fail_over_mac_value, primary_reselect_value;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 /*
4487 * Convert string parameters.
4488 */
4489 if (mode) {
4490 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4491 if (bond_mode == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004492 pr_err("Error: Invalid bonding mode \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 mode == NULL ? "NULL" : mode);
4494 return -EINVAL;
4495 }
4496 }
4497
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004498 if (xmit_hash_policy) {
4499 if ((bond_mode != BOND_MODE_XOR) &&
4500 (bond_mode != BOND_MODE_8023AD)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004501 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004502 bond_mode_name(bond_mode));
4503 } else {
4504 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4505 xmit_hashtype_tbl);
4506 if (xmit_hashtype == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004507 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004508 xmit_hash_policy == NULL ? "NULL" :
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004509 xmit_hash_policy);
4510 return -EINVAL;
4511 }
4512 }
4513 }
4514
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 if (lacp_rate) {
4516 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004517 pr_info("lacp_rate param is irrelevant in mode %s\n",
4518 bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 } else {
4520 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4521 if (lacp_fast == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004522 pr_err("Error: Invalid lacp rate \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 lacp_rate == NULL ? "NULL" : lacp_rate);
4524 return -EINVAL;
4525 }
4526 }
4527 }
4528
Jay Vosburghfd989c82008-11-04 17:51:16 -08004529 if (ad_select) {
4530 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4531 if (params->ad_select == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004532 pr_err("Error: Invalid ad_select \"%s\"\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -08004533 ad_select == NULL ? "NULL" : ad_select);
4534 return -EINVAL;
4535 }
4536
4537 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004538 pr_warning("ad_select param only affects 802.3ad mode\n");
Jay Vosburghfd989c82008-11-04 17:51:16 -08004539 }
4540 } else {
4541 params->ad_select = BOND_AD_STABLE;
4542 }
4543
Nicolas de Pesloüanf5841302009-08-28 13:18:34 +00004544 if (max_bonds < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004545 pr_warning("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4546 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 max_bonds = BOND_DEFAULT_MAX_BONDS;
4548 }
4549
4550 if (miimon < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004551 pr_warning("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to %d\n",
4552 miimon, INT_MAX, BOND_LINK_MON_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 miimon = BOND_LINK_MON_INTERV;
4554 }
4555
4556 if (updelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004557 pr_warning("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4558 updelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 updelay = 0;
4560 }
4561
4562 if (downdelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004563 pr_warning("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4564 downdelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 downdelay = 0;
4566 }
4567
4568 if ((use_carrier != 0) && (use_carrier != 1)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004569 pr_warning("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
4570 use_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 use_carrier = 1;
4572 }
4573
4574 /* reset values for 802.3ad */
4575 if (bond_mode == BOND_MODE_8023AD) {
4576 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004577 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 +00004578 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 miimon = 100;
4580 }
4581 }
4582
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004583 if (tx_queues < 1 || tx_queues > 255) {
4584 pr_warning("Warning: tx_queues (%d) should be between "
4585 "1 and 255, resetting to %d\n",
4586 tx_queues, BOND_DEFAULT_TX_QUEUES);
4587 tx_queues = BOND_DEFAULT_TX_QUEUES;
4588 }
4589
Andy Gospodarekebd8e492010-06-02 08:39:21 +00004590 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
4591 pr_warning("Warning: all_slaves_active module parameter (%d), "
4592 "not of valid value (0/1), so it was set to "
4593 "0\n", all_slaves_active);
4594 all_slaves_active = 0;
4595 }
4596
Flavio Leitnerc2952c32010-10-05 14:23:59 +00004597 if (resend_igmp < 0 || resend_igmp > 255) {
4598 pr_warning("Warning: resend_igmp (%d) should be between "
4599 "0 and 255, resetting to %d\n",
4600 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
4601 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
4602 }
4603
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 /* reset values for TLB/ALB */
4605 if ((bond_mode == BOND_MODE_TLB) ||
4606 (bond_mode == BOND_MODE_ALB)) {
4607 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004608 pr_warning("Warning: miimon must be specified, otherwise bonding will not detect link failure and link speed which are essential for TLB/ALB load balancing\n");
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004609 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 miimon = 100;
4611 }
4612 }
4613
4614 if (bond_mode == BOND_MODE_ALB) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004615 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",
4616 updelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 }
4618
4619 if (!miimon) {
4620 if (updelay || downdelay) {
4621 /* just warn the user the up/down delay will have
4622 * no effect since miimon is zero...
4623 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004624 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",
4625 updelay, downdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 }
4627 } else {
4628 /* don't allow arp monitoring */
4629 if (arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004630 pr_warning("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
4631 miimon, arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 arp_interval = 0;
4633 }
4634
4635 if ((updelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004636 pr_warning("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
4637 updelay, miimon,
4638 (updelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 }
4640
4641 updelay /= miimon;
4642
4643 if ((downdelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004644 pr_warning("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
4645 downdelay, miimon,
4646 (downdelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 }
4648
4649 downdelay /= miimon;
4650 }
4651
4652 if (arp_interval < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004653 pr_warning("Warning: arp_interval module parameter (%d) , not in range 0-%d, so it was reset to %d\n",
4654 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 arp_interval = BOND_LINK_ARP_INTERV;
4656 }
4657
4658 for (arp_ip_count = 0;
4659 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4660 arp_ip_count++) {
4661 /* not complete check, but should be good enough to
4662 catch mistakes */
4663 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004664 pr_warning("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
4665 arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 arp_interval = 0;
4667 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04004668 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 arp_target[arp_ip_count] = ip;
4670 }
4671 }
4672
4673 if (arp_interval && !arp_ip_count) {
4674 /* don't allow arping if no arp_ip_target given... */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004675 pr_warning("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
4676 arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 arp_interval = 0;
4678 }
4679
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004680 if (arp_validate) {
4681 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004682 pr_err("arp_validate only supported in active-backup mode\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004683 return -EINVAL;
4684 }
4685 if (!arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004686 pr_err("arp_validate requires arp_interval\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004687 return -EINVAL;
4688 }
4689
4690 arp_validate_value = bond_parse_parm(arp_validate,
4691 arp_validate_tbl);
4692 if (arp_validate_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004693 pr_err("Error: invalid arp_validate \"%s\"\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004694 arp_validate == NULL ? "NULL" : arp_validate);
4695 return -EINVAL;
4696 }
4697 } else
4698 arp_validate_value = 0;
4699
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 if (miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004701 pr_info("MII link monitoring set to %d ms\n", miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 } else if (arp_interval) {
4703 int i;
4704
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004705 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
4706 arp_interval,
4707 arp_validate_tbl[arp_validate_value].modename,
4708 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
4710 for (i = 0; i < arp_ip_count; i++)
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00004711 pr_info(" %s", arp_ip_target[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00004713 pr_info("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
Jay Vosburghb8a97872008-06-13 18:12:04 -07004715 } else if (max_bonds) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 /* miimon and arp_interval not set, we need one so things
4717 * work as expected, see bonding.txt for details
4718 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004719 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 -07004720 }
4721
4722 if (primary && !USES_PRIMARY(bond_mode)) {
4723 /* currently, using a primary only makes sense
4724 * in active backup, TLB or ALB modes
4725 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004726 pr_warning("Warning: %s primary device specified but has no effect in %s mode\n",
4727 primary, bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 primary = NULL;
4729 }
4730
Jiri Pirkoa5499522009-09-25 03:28:09 +00004731 if (primary && primary_reselect) {
4732 primary_reselect_value = bond_parse_parm(primary_reselect,
4733 pri_reselect_tbl);
4734 if (primary_reselect_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004735 pr_err("Error: Invalid primary_reselect \"%s\"\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00004736 primary_reselect ==
4737 NULL ? "NULL" : primary_reselect);
4738 return -EINVAL;
4739 }
4740 } else {
4741 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
4742 }
4743
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004744 if (fail_over_mac) {
4745 fail_over_mac_value = bond_parse_parm(fail_over_mac,
4746 fail_over_mac_tbl);
4747 if (fail_over_mac_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004748 pr_err("Error: invalid fail_over_mac \"%s\"\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004749 arp_validate == NULL ? "NULL" : arp_validate);
4750 return -EINVAL;
4751 }
4752
4753 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004754 pr_warning("Warning: fail_over_mac only affects active-backup mode.\n");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004755 } else {
4756 fail_over_mac_value = BOND_FOM_NONE;
4757 }
Jay Vosburghdd957c52007-10-09 19:57:24 -07004758
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 /* fill params struct with the proper values */
4760 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004761 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 params->miimon = miimon;
4763 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004764 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 params->updelay = updelay;
4766 params->downdelay = downdelay;
4767 params->use_carrier = use_carrier;
4768 params->lacp_fast = lacp_fast;
4769 params->primary[0] = 0;
Jiri Pirkoa5499522009-09-25 03:28:09 +00004770 params->primary_reselect = primary_reselect_value;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07004771 params->fail_over_mac = fail_over_mac_value;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004772 params->tx_queues = tx_queues;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00004773 params->all_slaves_active = all_slaves_active;
Flavio Leitnerc2952c32010-10-05 14:23:59 +00004774 params->resend_igmp = resend_igmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
4776 if (primary) {
4777 strncpy(params->primary, primary, IFNAMSIZ);
4778 params->primary[IFNAMSIZ - 1] = 0;
4779 }
4780
4781 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4782
4783 return 0;
4784}
4785
Peter Zijlstra0daa2302006-11-08 19:51:01 -08004786static struct lock_class_key bonding_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -07004787static struct lock_class_key bonding_netdev_addr_lock_key;
Peter Zijlstra0daa2302006-11-08 19:51:01 -08004788
David S. Millere8a04642008-07-17 00:34:19 -07004789static void bond_set_lockdep_class_one(struct net_device *dev,
4790 struct netdev_queue *txq,
4791 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -07004792{
4793 lockdep_set_class(&txq->_xmit_lock,
4794 &bonding_netdev_xmit_lock_key);
4795}
4796
4797static void bond_set_lockdep_class(struct net_device *dev)
4798{
David S. Millercf508b12008-07-22 14:16:42 -07004799 lockdep_set_class(&dev->addr_list_lock,
4800 &bonding_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -07004801 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -07004802}
4803
Stephen Hemminger181470f2009-06-12 19:02:52 +00004804/*
4805 * Called from registration process
4806 */
4807static int bond_init(struct net_device *bond_dev)
4808{
4809 struct bonding *bond = netdev_priv(bond_dev);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004810 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Stephen Hemminger181470f2009-06-12 19:02:52 +00004811
4812 pr_debug("Begin bond_init for %s\n", bond_dev->name);
4813
4814 bond->wq = create_singlethread_workqueue(bond_dev->name);
4815 if (!bond->wq)
4816 return -ENOMEM;
4817
4818 bond_set_lockdep_class(bond_dev);
4819
Stephen Hemminger181470f2009-06-12 19:02:52 +00004820 bond_create_proc_entry(bond);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004821 list_add_tail(&bond->bond_list, &bn->dev_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00004822
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00004823 bond_prepare_sysfs_group(bond);
Jiri Pirko22bedad2010-04-01 21:22:57 +00004824
Taku Izumif073c7c2010-12-09 15:17:13 +00004825 bond_debug_register(bond);
4826
Jiri Pirko22bedad2010-04-01 21:22:57 +00004827 __hw_addr_init(&bond->mc_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00004828 return 0;
4829}
4830
Eric W. Biederman88ead972009-10-29 14:18:25 +00004831static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
4832{
4833 if (tb[IFLA_ADDRESS]) {
4834 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
4835 return -EINVAL;
4836 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
4837 return -EADDRNOTAVAIL;
4838 }
4839 return 0;
4840}
4841
4842static struct rtnl_link_ops bond_link_ops __read_mostly = {
4843 .kind = "bond",
Eric W. Biederman66391042009-10-29 23:58:54 +00004844 .priv_size = sizeof(struct bonding),
Eric W. Biederman88ead972009-10-29 14:18:25 +00004845 .setup = bond_setup,
4846 .validate = bond_validate,
4847};
4848
Mitch Williamsdfe60392005-11-09 10:36:04 -08004849/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004850 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08004851 * Caller must NOT hold rtnl_lock; we need to release it here before we
4852 * set up our sysfs entries.
4853 */
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004854int bond_create(struct net *net, const char *name)
Mitch Williamsdfe60392005-11-09 10:36:04 -08004855{
4856 struct net_device *bond_dev;
4857 int res;
4858
4859 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08004860
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004861 bond_dev = alloc_netdev_mq(sizeof(struct bonding), name ? name : "",
4862 bond_setup, tx_queues);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004863 if (!bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004864 pr_err("%s: eek! can't alloc netdev!\n", name);
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004865 rtnl_unlock();
4866 return -ENOMEM;
Mitch Williamsdfe60392005-11-09 10:36:04 -08004867 }
4868
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004869 dev_net_set(bond_dev, net);
Eric W. Biederman88ead972009-10-29 14:18:25 +00004870 bond_dev->rtnl_link_ops = &bond_link_ops;
4871
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004872 if (!name) {
4873 res = dev_alloc_name(bond_dev, "bond%d");
4874 if (res < 0)
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004875 goto out;
Neil Horman27e6f062010-10-05 03:39:21 +00004876 } else {
4877 /*
4878 * If we're given a name to register
4879 * we need to ensure that its not already
4880 * registered
4881 */
4882 res = -EEXIST;
4883 if (__dev_get_by_name(net, name) != NULL)
4884 goto out;
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004885 }
4886
Mitch Williamsdfe60392005-11-09 10:36:04 -08004887 res = register_netdevice(bond_dev);
Peter Zijlstra0daa2302006-11-08 19:51:01 -08004888
Phil Oestere826eaf2011-03-14 06:22:05 +00004889 netif_carrier_off(bond_dev);
4890
Eric W. Biederman30c15ba2009-10-29 14:18:23 +00004891out:
Mitch Williamsdfe60392005-11-09 10:36:04 -08004892 rtnl_unlock();
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004893 if (res < 0)
4894 bond_destructor(bond_dev);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004895 return res;
4896}
4897
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004898static int __net_init bond_net_init(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004899{
Eric W. Biederman15449742009-11-29 15:46:04 +00004900 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004901
4902 bn->net = net;
4903 INIT_LIST_HEAD(&bn->dev_list);
4904
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004905 bond_create_proc_dir(bn);
Eric W. Biederman15449742009-11-29 15:46:04 +00004906
4907 return 0;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004908}
4909
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004910static void __net_exit bond_net_exit(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004911{
Eric W. Biederman15449742009-11-29 15:46:04 +00004912 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004913
4914 bond_destroy_proc_dir(bn);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004915}
4916
4917static struct pernet_operations bond_net_ops = {
4918 .init = bond_net_init,
4919 .exit = bond_net_exit,
Eric W. Biederman15449742009-11-29 15:46:04 +00004920 .id = &bond_net_id,
4921 .size = sizeof(struct bond_net),
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004922};
4923
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924static int __init bonding_init(void)
4925{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 int i;
4927 int res;
4928
Amerigo Wangbd33acc2011-03-06 21:58:46 +00004929 pr_info("%s", bond_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930
Mitch Williamsdfe60392005-11-09 10:36:04 -08004931 res = bond_check_params(&bonding_defaults);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004932 if (res)
Mitch Williamsdfe60392005-11-09 10:36:04 -08004933 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934
Eric W. Biederman15449742009-11-29 15:46:04 +00004935 res = register_pernet_subsys(&bond_net_ops);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004936 if (res)
4937 goto out;
Jay Vosburgh027ea042008-01-17 16:25:02 -08004938
Eric W. Biederman88ead972009-10-29 14:18:25 +00004939 res = rtnl_link_register(&bond_link_ops);
4940 if (res)
Eric W. Biederman66391042009-10-29 23:58:54 +00004941 goto err_link;
Eric W. Biederman88ead972009-10-29 14:18:25 +00004942
Taku Izumif073c7c2010-12-09 15:17:13 +00004943 bond_create_debugfs();
4944
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 for (i = 0; i < max_bonds; i++) {
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004946 res = bond_create(&init_net, NULL);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004947 if (res)
4948 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 }
4950
Mitch Williamsb76cdba2005-11-09 10:36:41 -08004951 res = bond_create_sysfs();
4952 if (res)
4953 goto err;
4954
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04004956 register_inetaddr_notifier(&bond_inetaddr_notifier);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004957out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 return res;
Eric W. Biederman88ead972009-10-29 14:18:25 +00004959err:
4960 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman66391042009-10-29 23:58:54 +00004961err_link:
Eric W. Biederman15449742009-11-29 15:46:04 +00004962 unregister_pernet_subsys(&bond_net_ops);
Eric W. Biederman88ead972009-10-29 14:18:25 +00004963 goto out;
Mitch Williamsdfe60392005-11-09 10:36:04 -08004964
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965}
4966
4967static void __exit bonding_exit(void)
4968{
4969 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04004970 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
Pavel Emelyanovae68c392008-05-02 17:49:39 -07004972 bond_destroy_sysfs();
Taku Izumif073c7c2010-12-09 15:17:13 +00004973 bond_destroy_debugfs();
Pavel Emelyanovae68c392008-05-02 17:49:39 -07004974
Eric W. Biederman88ead972009-10-29 14:18:25 +00004975 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman15449742009-11-29 15:46:04 +00004976 unregister_pernet_subsys(&bond_net_ops);
Neil Hormane843fa52010-10-13 16:01:50 +00004977
4978#ifdef CONFIG_NET_POLL_CONTROLLER
Neil Hormanfb4fa762010-12-06 09:05:50 +00004979 /*
4980 * Make sure we don't have an imbalance on our netpoll blocking
4981 */
4982 WARN_ON(atomic_read(&netpoll_block_tx));
Neil Hormane843fa52010-10-13 16:01:50 +00004983#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984}
4985
4986module_init(bonding_init);
4987module_exit(bonding_exit);
4988MODULE_LICENSE("GPL");
4989MODULE_VERSION(DRV_VERSION);
4990MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
4991MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
Eric W. Biederman88ead972009-10-29 14:18:25 +00004992MODULE_ALIAS_RTNL_LINK("bond");