blob: d72c37f03e506d269de69c1c3b4e1f5240d28e9e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * originally based on the dummy device.
3 *
4 * Copyright 1999, Thomas Davis, tadavis@lbl.gov.
5 * Licensed under the GPL. Based on dummy.c, and eql.c devices.
6 *
7 * bonding.c: an Ethernet Bonding driver
8 *
9 * This is useful to talk to a Cisco EtherChannel compatible equipment:
10 * Cisco 5500
11 * Sun Trunking (Solaris)
12 * Alteon AceDirector Trunks
13 * Linux Bonding
14 * and probably many L2 switches ...
15 *
16 * How it works:
17 * ifconfig bond0 ipaddress netmask up
18 * will setup a network device, with an ip address. No mac address
19 * will be assigned at this time. The hw mac address will come from
20 * the first slave bonded to the channel. All slaves will then use
21 * this hw mac address.
22 *
23 * ifconfig bond0 down
24 * will release all slaves, marking them as down.
25 *
26 * ifenslave bond0 eth0
27 * will attach eth0 to bond0 as a slave. eth0 hw mac address will either
28 * a: be used as initial mac address
29 * b: if a hw mac address already is there, eth0's hw mac address
30 * will then be set from bond0.
31 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 */
33
Joe Perchesa4aee5c2009-12-13 20:06:07 -080034#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/kernel.h>
37#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/types.h>
39#include <linux/fcntl.h>
40#include <linux/interrupt.h>
41#include <linux/ptrace.h>
42#include <linux/ioport.h>
43#include <linux/in.h>
Jay Vosburgh169a3e62005-06-26 17:54:11 -040044#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/ip.h>
Jay Vosburgh169a3e62005-06-26 17:54:11 -040046#include <linux/tcp.h>
47#include <linux/udp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/slab.h>
49#include <linux/string.h>
50#include <linux/init.h>
51#include <linux/timer.h>
52#include <linux/socket.h>
53#include <linux/ctype.h>
54#include <linux/inet.h>
55#include <linux/bitops.h>
Stephen Hemminger3d632c32009-06-12 19:02:48 +000056#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <asm/dma.h>
Stephen Hemminger3d632c32009-06-12 19:02:48 +000059#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/errno.h>
61#include <linux/netdevice.h>
62#include <linux/inetdevice.h>
Jay Vosburgha816c7c2007-02-28 17:03:37 -080063#include <linux/igmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/etherdevice.h>
65#include <linux/skbuff.h>
66#include <net/sock.h>
67#include <linux/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/smp.h>
69#include <linux/if_ether.h>
70#include <net/arp.h>
71#include <linux/mii.h>
72#include <linux/ethtool.h>
73#include <linux/if_vlan.h>
74#include <linux/if_bonding.h>
David Sterbab63bb732007-12-06 23:40:33 -080075#include <linux/jiffies.h>
Neil Hormane843fa52010-10-13 16:01:50 +000076#include <linux/preempt.h>
Jay Vosburghc3ade5c2005-06-26 17:52:20 -040077#include <net/route.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020078#include <net/net_namespace.h>
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000079#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include "bonding.h"
81#include "bond_3ad.h"
82#include "bond_alb.h"
83
84/*---------------------------- Module parameters ----------------------------*/
85
86/* monitor all links that often (in milliseconds). <=0 disables monitoring */
87#define BOND_LINK_MON_INTERV 0
88#define BOND_LINK_ARP_INTERV 0
89
90static int max_bonds = BOND_DEFAULT_MAX_BONDS;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +000091static int tx_queues = BOND_DEFAULT_TX_QUEUES;
Ben Hutchingsad246c92011-04-26 15:25:52 +000092static int num_peer_notif = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static int miimon = BOND_LINK_MON_INTERV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +000094static int updelay;
95static int downdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static int use_carrier = 1;
Stephen Hemminger3d632c32009-06-12 19:02:48 +000097static char *mode;
98static char *primary;
Jiri Pirkoa5499522009-09-25 03:28:09 +000099static char *primary_reselect;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000100static char *lacp_rate;
stephen hemminger655f8912011-06-22 09:54:39 +0000101static int min_links;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000102static char *ad_select;
103static char *xmit_hash_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static int arp_interval = BOND_LINK_ARP_INTERV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000105static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
106static char *arp_validate;
107static char *fail_over_mac;
Andy Gospodarekebd8e492010-06-02 08:39:21 +0000108static int all_slaves_active = 0;
Stephen Hemmingerd2991f72009-06-12 19:02:44 +0000109static struct bond_params bonding_defaults;
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000110static int resend_igmp = BOND_DEFAULT_RESEND_IGMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112module_param(max_bonds, int, 0);
113MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
Andy Gospodarekbb1d9122010-06-02 08:40:18 +0000114module_param(tx_queues, int, 0);
115MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)");
Ben Hutchingsad246c92011-04-26 15:25:52 +0000116module_param_named(num_grat_arp, num_peer_notif, int, 0644);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000117MODULE_PARM_DESC(num_grat_arp, "Number of peer notifications to send on "
118 "failover event (alias of num_unsol_na)");
Ben Hutchingsad246c92011-04-26 15:25:52 +0000119module_param_named(num_unsol_na, num_peer_notif, int, 0644);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000120MODULE_PARM_DESC(num_unsol_na, "Number of peer notifications to send on "
121 "failover event (alias of num_grat_arp)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122module_param(miimon, int, 0);
123MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
124module_param(updelay, int, 0);
125MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
126module_param(downdelay, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800127MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
128 "in milliseconds");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129module_param(use_carrier, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800130MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
131 "0 for off, 1 for on (default)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132module_param(mode, charp, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000133MODULE_PARM_DESC(mode, "Mode of operation; 0 for balance-rr, "
Mitch Williams2ac47662005-11-09 10:35:03 -0800134 "1 for active-backup, 2 for balance-xor, "
135 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
136 "6 for balance-alb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137module_param(primary, charp, 0);
138MODULE_PARM_DESC(primary, "Primary network device to use");
Jiri Pirkoa5499522009-09-25 03:28:09 +0000139module_param(primary_reselect, charp, 0);
140MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
141 "once it comes up; "
142 "0 for always (default), "
143 "1 for only if speed of primary is "
144 "better, "
145 "2 for only on active slave "
146 "failure");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147module_param(lacp_rate, charp, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000148MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner; "
149 "0 for slow, 1 for fast");
Jay Vosburghfd989c82008-11-04 17:51:16 -0800150module_param(ad_select, charp, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000151MODULE_PARM_DESC(ad_select, "803.ad aggregation selection logic; "
152 "0 for stable (default), 1 for bandwidth, "
153 "2 for count");
stephen hemminger655f8912011-06-22 09:54:39 +0000154module_param(min_links, int, 0);
155MODULE_PARM_DESC(min_links, "Minimum number of available links before turning on carrier");
156
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400157module_param(xmit_hash_policy, charp, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000158MODULE_PARM_DESC(xmit_hash_policy, "balance-xor and 802.3ad hashing method; "
159 "0 for layer 2 (default), 1 for layer 3+4, "
160 "2 for layer 2+3");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161module_param(arp_interval, int, 0);
162MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
163module_param_array(arp_ip_target, charp, NULL, 0);
164MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700165module_param(arp_validate, charp, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000166MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes; "
167 "0 for none (default), 1 for active, "
168 "2 for backup, 3 for all");
Jay Vosburgh3915c1e2008-05-17 21:10:14 -0700169module_param(fail_over_mac, charp, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000170MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to "
171 "the same MAC; 0 for none (default), "
172 "1 for active, 2 for follow");
Andy Gospodarekebd8e492010-06-02 08:39:21 +0000173module_param(all_slaves_active, int, 0);
174MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface"
Andy Gospodarek90e62472011-05-25 04:41:59 +0000175 "by setting active flag for all slaves; "
Andy Gospodarekebd8e492010-06-02 08:39:21 +0000176 "0 for never (default), 1 for always.");
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000177module_param(resend_igmp, int, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000178MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on "
179 "link failure");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181/*----------------------------- Global variables ----------------------------*/
182
Neil Hormane843fa52010-10-13 16:01:50 +0000183#ifdef CONFIG_NET_POLL_CONTROLLER
Neil Hormanfb4fa762010-12-06 09:05:50 +0000184atomic_t netpoll_block_tx = ATOMIC_INIT(0);
Neil Hormane843fa52010-10-13 16:01:50 +0000185#endif
186
Eric Dumazetf99189b2009-11-17 10:42:49 +0000187int bond_net_id __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000189static __be32 arp_target[BOND_MAX_ARP_TARGETS];
190static int arp_ip_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191static int bond_mode = BOND_MODE_ROUNDROBIN;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000192static int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
193static int lacp_fast;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800195const struct bond_parm_tbl bond_lacp_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{ "slow", AD_LACP_SLOW},
197{ "fast", AD_LACP_FAST},
198{ NULL, -1},
199};
200
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800201const struct bond_parm_tbl bond_mode_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{ "balance-rr", BOND_MODE_ROUNDROBIN},
203{ "active-backup", BOND_MODE_ACTIVEBACKUP},
204{ "balance-xor", BOND_MODE_XOR},
205{ "broadcast", BOND_MODE_BROADCAST},
206{ "802.3ad", BOND_MODE_8023AD},
207{ "balance-tlb", BOND_MODE_TLB},
208{ "balance-alb", BOND_MODE_ALB},
209{ NULL, -1},
210};
211
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800212const struct bond_parm_tbl xmit_hashtype_tbl[] = {
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400213{ "layer2", BOND_XMIT_POLICY_LAYER2},
214{ "layer3+4", BOND_XMIT_POLICY_LAYER34},
Jay Vosburgh6f6652b2007-12-06 23:40:34 -0800215{ "layer2+3", BOND_XMIT_POLICY_LAYER23},
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400216{ NULL, -1},
217};
218
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800219const struct bond_parm_tbl arp_validate_tbl[] = {
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700220{ "none", BOND_ARP_VALIDATE_NONE},
221{ "active", BOND_ARP_VALIDATE_ACTIVE},
222{ "backup", BOND_ARP_VALIDATE_BACKUP},
223{ "all", BOND_ARP_VALIDATE_ALL},
224{ NULL, -1},
225};
226
Holger Eitzenbergere97fd7c2008-12-09 23:10:38 -0800227const struct bond_parm_tbl fail_over_mac_tbl[] = {
Jay Vosburgh3915c1e2008-05-17 21:10:14 -0700228{ "none", BOND_FOM_NONE},
229{ "active", BOND_FOM_ACTIVE},
230{ "follow", BOND_FOM_FOLLOW},
231{ NULL, -1},
232};
233
Jiri Pirkoa5499522009-09-25 03:28:09 +0000234const struct bond_parm_tbl pri_reselect_tbl[] = {
235{ "always", BOND_PRI_RESELECT_ALWAYS},
236{ "better", BOND_PRI_RESELECT_BETTER},
237{ "failure", BOND_PRI_RESELECT_FAILURE},
238{ NULL, -1},
239};
240
Jay Vosburghfd989c82008-11-04 17:51:16 -0800241struct bond_parm_tbl ad_select_tbl[] = {
242{ "stable", BOND_AD_STABLE},
243{ "bandwidth", BOND_AD_BANDWIDTH},
244{ "count", BOND_AD_COUNT},
245{ NULL, -1},
246};
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/*-------------------------- Forward declarations ---------------------------*/
249
Stephen Hemminger181470f2009-06-12 19:02:52 +0000250static int bond_init(struct net_device *bond_dev);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +0000251static void bond_uninit(struct net_device *bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253/*---------------------------- General routines -----------------------------*/
254
Amerigo Wangbd33acc2011-03-06 21:58:46 +0000255const char *bond_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800257 static const char *names[] = {
258 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
259 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
260 [BOND_MODE_XOR] = "load balancing (xor)",
261 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000262 [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800263 [BOND_MODE_TLB] = "transmit load balancing",
264 [BOND_MODE_ALB] = "adaptive load balancing",
265 };
266
267 if (mode < 0 || mode > BOND_MODE_ALB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return "unknown";
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800269
270 return names[mode];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273/*---------------------------------- VLAN -----------------------------------*/
274
275/**
276 * bond_add_vlan - add a new vlan id on bond
277 * @bond: bond that got the notification
278 * @vlan_id: the vlan id to add
279 *
280 * Returns -ENOMEM if allocation failed.
281 */
282static int bond_add_vlan(struct bonding *bond, unsigned short vlan_id)
283{
284 struct vlan_entry *vlan;
285
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800286 pr_debug("bond: %s, vlan id %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800287 (bond ? bond->dev->name : "None"), vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Brian Haley305d5522008-11-04 17:51:14 -0800289 vlan = kzalloc(sizeof(struct vlan_entry), GFP_KERNEL);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000290 if (!vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 INIT_LIST_HEAD(&vlan->vlan_list);
294 vlan->vlan_id = vlan_id;
295
296 write_lock_bh(&bond->lock);
297
298 list_add_tail(&vlan->vlan_list, &bond->vlan_list);
299
300 write_unlock_bh(&bond->lock);
301
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800302 pr_debug("added VLAN ID %d on bond %s\n", vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 return 0;
305}
306
307/**
308 * bond_del_vlan - delete a vlan id from bond
309 * @bond: bond that got the notification
310 * @vlan_id: the vlan id to delete
311 *
312 * returns -ENODEV if @vlan_id was not found in @bond.
313 */
314static int bond_del_vlan(struct bonding *bond, unsigned short vlan_id)
315{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700316 struct vlan_entry *vlan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 int res = -ENODEV;
318
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800319 pr_debug("bond: %s, vlan id %d\n", bond->dev->name, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Neil Hormane843fa52010-10-13 16:01:50 +0000321 block_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 write_lock_bh(&bond->lock);
323
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700324 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (vlan->vlan_id == vlan_id) {
326 list_del(&vlan->vlan_list);
327
Holger Eitzenberger58402052008-12-09 23:07:13 -0800328 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 bond_alb_clear_vlan(bond, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800331 pr_debug("removed VLAN ID %d from bond %s\n",
332 vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 kfree(vlan);
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 res = 0;
337 goto out;
338 }
339 }
340
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800341 pr_debug("couldn't find VLAN ID %d in bond %s\n",
342 vlan_id, bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344out:
345 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +0000346 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return res;
348}
349
350/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 * bond_next_vlan - safely skip to the next item in the vlans list.
352 * @bond: the bond we're working on
353 * @curr: item we're advancing from
354 *
355 * Returns %NULL if list is empty, bond->next_vlan if @curr is %NULL,
356 * or @curr->next otherwise (even if it is @curr itself again).
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000357 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 * Caller must hold bond->lock
359 */
360struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr)
361{
362 struct vlan_entry *next, *last;
363
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000364 if (list_empty(&bond->vlan_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 if (!curr) {
368 next = list_entry(bond->vlan_list.next,
369 struct vlan_entry, vlan_list);
370 } else {
371 last = list_entry(bond->vlan_list.prev,
372 struct vlan_entry, vlan_list);
373 if (last == curr) {
374 next = list_entry(bond->vlan_list.next,
375 struct vlan_entry, vlan_list);
376 } else {
377 next = list_entry(curr->vlan_list.next,
378 struct vlan_entry, vlan_list);
379 }
380 }
381
382 return next;
383}
384
Neil Horman374eeb52011-06-03 10:35:52 +0000385#define bond_queue_mapping(skb) (*(u16 *)((skb)->cb))
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387/**
388 * bond_dev_queue_xmit - Prepare skb for xmit.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000389 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 * @bond: bond device that got this skb for tx.
391 * @skb: hw accel VLAN tagged skb to transmit
392 * @slave_dev: slave that is supposed to xmit this skbuff
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000394int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
395 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Ben Hutchings83874512010-12-13 08:19:28 +0000397 skb->dev = slave_dev;
Neil Horman374eeb52011-06-03 10:35:52 +0000398
399 skb->queue_mapping = bond_queue_mapping(skb);
400
Amerigo Wang080e4132011-02-17 23:43:33 +0000401 if (unlikely(netpoll_tx_running(slave_dev)))
Amerigo Wang8a8efa22011-02-17 23:43:32 +0000402 bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
Amerigo Wang080e4132011-02-17 23:43:33 +0000403 else
WANG Congf6dc31a2010-05-06 00:48:51 -0700404 dev_queue_xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 return 0;
407}
408
409/*
Jiri Pirkocc0e4072011-07-20 04:54:46 +0000410 * In the following 2 functions, bond_vlan_rx_add_vid and bond_vlan_rx_kill_vid,
411 * We don't protect the slave list iteration with a lock because:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 * a. This operation is performed in IOCTL context,
413 * b. The operation is protected by the RTNL semaphore in the 8021q code,
414 * c. Holding a lock with BH disabled while directly calling a base driver
415 * entry point is generally a BAD idea.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000416 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 * The design of synchronization/protection for this operation in the 8021q
418 * module is good for one or more VLAN devices over a single physical device
419 * and cannot be extended for a teaming solution like bonding, so there is a
420 * potential race condition here where a net device from the vlan group might
421 * be referenced (either by a base driver or the 8021q code) while it is being
422 * removed from the system. However, it turns out we're not making matters
423 * worse, and if it works for regular VLAN usage it will work here too.
424*/
425
426/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
428 * @bond_dev: bonding net device that got called
429 * @vid: vlan id being added
430 */
Jiri Pirko8e586132011-12-08 19:52:37 -0500431static int bond_vlan_rx_add_vid(struct net_device *bond_dev, uint16_t vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Wang Chen454d7c92008-11-12 23:37:49 -0800433 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct slave *slave;
435 int i, res;
436
437 bond_for_each_slave(bond, slave, i) {
438 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800439 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800442 slave_ops->ndo_vlan_rx_add_vid) {
443 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445 }
446
447 res = bond_add_vlan(bond, vid);
448 if (res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800449 pr_err("%s: Error: Failed to add vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 bond_dev->name, vid);
Jiri Pirko8e586132011-12-08 19:52:37 -0500451 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
Jiri Pirko8e586132011-12-08 19:52:37 -0500453
454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457/**
458 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
459 * @bond_dev: bonding net device that got called
460 * @vid: vlan id being removed
461 */
Jiri Pirko8e586132011-12-08 19:52:37 -0500462static int bond_vlan_rx_kill_vid(struct net_device *bond_dev, uint16_t vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Wang Chen454d7c92008-11-12 23:37:49 -0800464 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int i, res;
467
468 bond_for_each_slave(bond, slave, i) {
469 struct net_device *slave_dev = slave->dev;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800470 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 if ((slave_dev->features & NETIF_F_HW_VLAN_FILTER) &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800473 slave_ops->ndo_vlan_rx_kill_vid) {
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800474 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476 }
477
478 res = bond_del_vlan(bond, vid);
479 if (res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800480 pr_err("%s: Error: Failed to remove vlan id %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 bond_dev->name, vid);
Jiri Pirko8e586132011-12-08 19:52:37 -0500482 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Jiri Pirko8e586132011-12-08 19:52:37 -0500484
485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488static void bond_add_vlans_on_slave(struct bonding *bond, struct net_device *slave_dev)
489{
490 struct vlan_entry *vlan;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800491 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800494 !(slave_ops->ndo_vlan_rx_add_vid))
Jay Vosburgh03dc2f42010-07-21 12:14:48 +0000495 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800497 list_for_each_entry(vlan, &bond->vlan_list, vlan_list)
498 slave_ops->ndo_vlan_rx_add_vid(slave_dev, vlan->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000501static void bond_del_vlans_from_slave(struct bonding *bond,
502 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800504 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 struct vlan_entry *vlan;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 if (!(slave_dev->features & NETIF_F_HW_VLAN_FILTER) ||
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800508 !(slave_ops->ndo_vlan_rx_kill_vid))
Jiri Pirkocc0e4072011-07-20 04:54:46 +0000509 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf35188f2010-07-21 12:14:47 +0000512 if (!vlan->vlan_id)
513 continue;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800514 slave_ops->ndo_vlan_rx_kill_vid(slave_dev, vlan->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
518/*------------------------------- Link status -------------------------------*/
519
520/*
Jay Vosburghff59c452006-03-27 13:27:43 -0800521 * Set the carrier state for the master according to the state of its
522 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
523 * do special 802.3ad magic.
524 *
525 * Returns zero if carrier state does not change, nonzero if it does.
526 */
527static int bond_set_carrier(struct bonding *bond)
528{
529 struct slave *slave;
530 int i;
531
532 if (bond->slave_cnt == 0)
533 goto down;
534
535 if (bond->params.mode == BOND_MODE_8023AD)
536 return bond_3ad_set_carrier(bond);
537
538 bond_for_each_slave(bond, slave, i) {
539 if (slave->link == BOND_LINK_UP) {
540 if (!netif_carrier_ok(bond->dev)) {
541 netif_carrier_on(bond->dev);
542 return 1;
543 }
544 return 0;
545 }
546 }
547
548down:
549 if (netif_carrier_ok(bond->dev)) {
550 netif_carrier_off(bond->dev);
551 return 1;
552 }
553 return 0;
554}
555
556/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 * Get link speed and duplex from the slave's base driver
558 * using ethtool. If for some reason the call fails or the
Weiping Pan98f41f62011-10-31 17:20:48 +0000559 * values are invalid, set speed and duplex to -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 * and return error.
561 */
562static int bond_update_speed_duplex(struct slave *slave)
563{
564 struct net_device *slave_dev = slave->dev;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000565 struct ethtool_cmd ecmd;
David Decotigny5d305302011-04-13 15:22:31 +0000566 u32 slave_speed;
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700567 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Dan Carpenter589665f2011-11-04 08:21:38 +0000569 slave->speed = SPEED_UNKNOWN;
570 slave->duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000572 res = __ethtool_get_settings(slave_dev, &ecmd);
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700573 if (res < 0)
574 return -1;
575
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000576 slave_speed = ethtool_cmd_speed(&ecmd);
Jiri Pirko6f92c662011-06-01 10:36:33 +0000577 if (slave_speed == 0 || slave_speed == ((__u32) -1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000580 switch (ecmd.duplex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 case DUPLEX_FULL:
582 case DUPLEX_HALF:
583 break;
584 default:
585 return -1;
586 }
587
David Decotigny5d305302011-04-13 15:22:31 +0000588 slave->speed = slave_speed;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000589 slave->duplex = ecmd.duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 return 0;
592}
593
594/*
595 * if <dev> supports MII link status reporting, check its link status.
596 *
597 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000598 * depending upon the setting of the use_carrier parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 *
600 * Return either BMSR_LSTATUS, meaning that the link is up (or we
601 * can't tell and just pretend it is), or 0, meaning that the link is
602 * down.
603 *
604 * If reporting is non-zero, instead of faking link up, return -1 if
605 * both ETHTOOL and MII ioctls fail (meaning the device does not
606 * support them). If use_carrier is set, return whatever it says.
607 * It'd be nice if there was a good way to tell if a driver supports
608 * netif_carrier, but there really isn't.
609 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000610static int bond_check_dev_link(struct bonding *bond,
611 struct net_device *slave_dev, int reporting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800613 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Jiri Bohacd9d52832009-10-28 22:23:54 -0700614 int (*ioctl)(struct net_device *, struct ifreq *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 struct ifreq ifr;
616 struct mii_ioctl_data *mii;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Petri Gynther6c988852009-08-28 12:05:15 +0000618 if (!reporting && !netif_running(slave_dev))
619 return 0;
620
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800621 if (bond->params.use_carrier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Jiri Pirko29112f42009-04-24 01:58:23 +0000624 /* Try to get link status using Ethtool first. */
625 if (slave_dev->ethtool_ops) {
626 if (slave_dev->ethtool_ops->get_link) {
627 u32 link;
628
629 link = slave_dev->ethtool_ops->get_link(slave_dev);
630
631 return link ? BMSR_LSTATUS : 0;
632 }
633 }
634
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000635 /* Ethtool can't be used, fallback to MII ioctls. */
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800636 ioctl = slave_ops->ndo_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (ioctl) {
638 /* TODO: set pointer to correct ioctl on a per team member */
639 /* bases to make this more efficient. that is, once */
640 /* we determine the correct ioctl, we will always */
641 /* call it and not the others for that team */
642 /* member. */
643
644 /*
645 * We cannot assume that SIOCGMIIPHY will also read a
646 * register; not all network drivers (e.g., e100)
647 * support that.
648 */
649
650 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
651 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
652 mii = if_mii(&ifr);
653 if (IOCTL(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
654 mii->reg_num = MII_BMSR;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000655 if (IOCTL(slave_dev, &ifr, SIOCGMIIREG) == 0)
656 return mii->val_out & BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658 }
659
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700660 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 * If reporting, report that either there's no dev->do_ioctl,
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700662 * or both SIOCGMIIREG and get_link failed (meaning that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 * cannot report link status). If not reporting, pretend
664 * we're ok.
665 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000666 return reporting ? -1 : BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669/*----------------------------- Multicast list ------------------------------*/
670
671/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 * Push the promiscuity flag down to appropriate slaves
673 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700674static int bond_set_promiscuity(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700676 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (USES_PRIMARY(bond->params.mode)) {
678 /* write lock already acquired */
679 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700680 err = dev_set_promiscuity(bond->curr_active_slave->dev,
681 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683 } else {
684 struct slave *slave;
685 int i;
686 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700687 err = dev_set_promiscuity(slave->dev, inc);
688 if (err)
689 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700692 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
695/*
696 * Push the allmulti flag down to all slaves
697 */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700698static int bond_set_allmulti(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700700 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (USES_PRIMARY(bond->params.mode)) {
702 /* write lock already acquired */
703 if (bond->curr_active_slave) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700704 err = dev_set_allmulti(bond->curr_active_slave->dev,
705 inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707 } else {
708 struct slave *slave;
709 int i;
710 bond_for_each_slave(bond, slave, i) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700711 err = dev_set_allmulti(slave->dev, inc);
712 if (err)
713 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700716 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
719/*
720 * Add a Multicast address to slaves
721 * according to mode
722 */
Jiri Pirko22bedad2010-04-01 21:22:57 +0000723static void bond_mc_add(struct bonding *bond, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 if (USES_PRIMARY(bond->params.mode)) {
726 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000727 if (bond->curr_active_slave)
Jiri Pirko22bedad2010-04-01 21:22:57 +0000728 dev_mc_add(bond->curr_active_slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 } else {
730 struct slave *slave;
731 int i;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000732
733 bond_for_each_slave(bond, slave, i)
Jiri Pirko22bedad2010-04-01 21:22:57 +0000734 dev_mc_add(slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736}
737
738/*
739 * Remove a multicast address from slave
740 * according to mode
741 */
Jiri Pirko22bedad2010-04-01 21:22:57 +0000742static void bond_mc_del(struct bonding *bond, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 if (USES_PRIMARY(bond->params.mode)) {
745 /* write lock already acquired */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000746 if (bond->curr_active_slave)
Jiri Pirko22bedad2010-04-01 21:22:57 +0000747 dev_mc_del(bond->curr_active_slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 } else {
749 struct slave *slave;
750 int i;
751 bond_for_each_slave(bond, slave, i) {
Jiri Pirko22bedad2010-04-01 21:22:57 +0000752 dev_mc_del(slave->dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754 }
755}
756
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800757
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000758static void __bond_resend_igmp_join_requests(struct net_device *dev)
759{
760 struct in_device *in_dev;
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000761
762 rcu_read_lock();
763 in_dev = __in_dev_get_rcu(dev);
Eric Dumazet866f3b22010-11-18 09:33:19 -0800764 if (in_dev)
David S. Miller24912422010-11-19 13:13:47 -0800765 ip_mc_rejoin_groups(in_dev);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000766 rcu_read_unlock();
767}
768
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800769/*
770 * Retrieve the list of registered multicast addresses for the bonding
771 * device and retransmit an IGMP JOIN request to the current active
772 * slave.
773 */
774static void bond_resend_igmp_join_requests(struct bonding *bond)
775{
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000776 struct net_device *vlan_dev;
777 struct vlan_entry *vlan;
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800778
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000779 read_lock(&bond->lock);
780
781 /* rejoin all groups on bond device */
782 __bond_resend_igmp_join_requests(bond->dev);
783
784 /* rejoin all groups on vlan devices */
Jiri Pirkocc0e4072011-07-20 04:54:46 +0000785 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
786 rcu_read_lock();
787 vlan_dev = __vlan_find_dev_deep(bond->dev,
788 vlan->vlan_id);
789 rcu_read_unlock();
790 if (vlan_dev)
791 __bond_resend_igmp_join_requests(vlan_dev);
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800792 }
793
Jay Vosburghe6d265e2011-10-28 15:42:50 +0000794 if (--bond->igmp_retrans > 0)
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000795 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
Jay Vosburghe6d265e2011-10-28 15:42:50 +0000796
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000797 read_unlock(&bond->lock);
798}
799
stephen hemminger379b7382010-10-15 11:02:56 +0000800static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000801{
802 struct bonding *bond = container_of(work, struct bonding,
Flavio Leitner94265cf2011-05-25 08:38:58 +0000803 mcast_work.work);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000804 bond_resend_igmp_join_requests(bond);
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800805}
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 * flush all members of flush->mc_list from device dev->mc_list
809 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000810static void bond_mc_list_flush(struct net_device *bond_dev,
811 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Wang Chen454d7c92008-11-12 23:37:49 -0800813 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad2010-04-01 21:22:57 +0000814 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Jiri Pirko22bedad2010-04-01 21:22:57 +0000816 netdev_for_each_mc_addr(ha, bond_dev)
817 dev_mc_del(slave_dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 if (bond->params.mode == BOND_MODE_8023AD) {
820 /* del lacpdu mc addr from mc list */
821 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
822
Jiri Pirko22bedad2010-04-01 21:22:57 +0000823 dev_mc_del(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825}
826
827/*--------------------------- Active slave change ---------------------------*/
828
829/*
830 * Update the mc list and multicast-related flags for the new and
831 * old active slaves (if any) according to the multicast mode, and
832 * promiscuous flags unconditionally.
833 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000834static void bond_mc_swap(struct bonding *bond, struct slave *new_active,
835 struct slave *old_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Jiri Pirko22bedad2010-04-01 21:22:57 +0000837 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000839 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 /* nothing to do - mc list is already up-to-date on
841 * all slaves
842 */
843 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 if (old_active) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000846 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 dev_set_promiscuity(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000849 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 dev_set_allmulti(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Jiri Pirko22bedad2010-04-01 21:22:57 +0000852 netdev_for_each_mc_addr(ha, bond->dev)
853 dev_mc_del(old_active->dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855
856 if (new_active) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700857 /* FIXME: Signal errors upstream. */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000858 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 dev_set_promiscuity(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000861 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 dev_set_allmulti(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Jiri Pirko22bedad2010-04-01 21:22:57 +0000864 netdev_for_each_mc_addr(ha, bond->dev)
865 dev_mc_add(new_active->dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867}
868
Jay Vosburgh3915c1e2008-05-17 21:10:14 -0700869/*
870 * bond_do_fail_over_mac
871 *
872 * Perform special MAC address swapping for fail_over_mac settings
873 *
874 * Called with RTNL, bond->lock for read, curr_slave_lock for write_bh.
875 */
876static void bond_do_fail_over_mac(struct bonding *bond,
877 struct slave *new_active,
878 struct slave *old_active)
Hannes Eder1f78d9f2009-02-14 11:15:33 +0000879 __releases(&bond->curr_slave_lock)
880 __releases(&bond->lock)
881 __acquires(&bond->lock)
882 __acquires(&bond->curr_slave_lock)
Jay Vosburgh3915c1e2008-05-17 21:10:14 -0700883{
884 u8 tmp_mac[ETH_ALEN];
885 struct sockaddr saddr;
886 int rv;
887
888 switch (bond->params.fail_over_mac) {
889 case BOND_FOM_ACTIVE:
890 if (new_active)
891 memcpy(bond->dev->dev_addr, new_active->dev->dev_addr,
892 new_active->dev->addr_len);
893 break;
894 case BOND_FOM_FOLLOW:
895 /*
896 * if new_active && old_active, swap them
897 * if just old_active, do nothing (going to no active slave)
898 * if just new_active, set new_active to bond's MAC
899 */
900 if (!new_active)
901 return;
902
903 write_unlock_bh(&bond->curr_slave_lock);
904 read_unlock(&bond->lock);
905
906 if (old_active) {
907 memcpy(tmp_mac, new_active->dev->dev_addr, ETH_ALEN);
908 memcpy(saddr.sa_data, old_active->dev->dev_addr,
909 ETH_ALEN);
910 saddr.sa_family = new_active->dev->type;
911 } else {
912 memcpy(saddr.sa_data, bond->dev->dev_addr, ETH_ALEN);
913 saddr.sa_family = bond->dev->type;
914 }
915
916 rv = dev_set_mac_address(new_active->dev, &saddr);
917 if (rv) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800918 pr_err("%s: Error %d setting MAC of slave %s\n",
Jay Vosburgh3915c1e2008-05-17 21:10:14 -0700919 bond->dev->name, -rv, new_active->dev->name);
920 goto out;
921 }
922
923 if (!old_active)
924 goto out;
925
926 memcpy(saddr.sa_data, tmp_mac, ETH_ALEN);
927 saddr.sa_family = old_active->dev->type;
928
929 rv = dev_set_mac_address(old_active->dev, &saddr);
930 if (rv)
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800931 pr_err("%s: Error %d setting MAC of slave %s\n",
Jay Vosburgh3915c1e2008-05-17 21:10:14 -0700932 bond->dev->name, -rv, new_active->dev->name);
933out:
934 read_lock(&bond->lock);
935 write_lock_bh(&bond->curr_slave_lock);
936 break;
937 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800938 pr_err("%s: bond_do_fail_over_mac impossible: bad policy %d\n",
Jay Vosburgh3915c1e2008-05-17 21:10:14 -0700939 bond->dev->name, bond->params.fail_over_mac);
940 break;
941 }
942
943}
944
Jiri Pirkoa5499522009-09-25 03:28:09 +0000945static bool bond_should_change_active(struct bonding *bond)
946{
947 struct slave *prim = bond->primary_slave;
948 struct slave *curr = bond->curr_active_slave;
949
950 if (!prim || !curr || curr->link != BOND_LINK_UP)
951 return true;
952 if (bond->force_primary) {
953 bond->force_primary = false;
954 return true;
955 }
956 if (bond->params.primary_reselect == BOND_PRI_RESELECT_BETTER &&
957 (prim->speed < curr->speed ||
958 (prim->speed == curr->speed && prim->duplex <= curr->duplex)))
959 return false;
960 if (bond->params.primary_reselect == BOND_PRI_RESELECT_FAILURE)
961 return false;
962 return true;
963}
Jay Vosburgh3915c1e2008-05-17 21:10:14 -0700964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965/**
966 * find_best_interface - select the best available slave to be the active one
967 * @bond: our bonding struct
968 *
969 * Warning: Caller must hold curr_slave_lock for writing.
970 */
971static struct slave *bond_find_best_slave(struct bonding *bond)
972{
973 struct slave *new_active, *old_active;
974 struct slave *bestslave = NULL;
975 int mintime = bond->params.updelay;
976 int i;
977
Nicolas de Pesloüan49b4ad92009-10-07 14:11:00 -0700978 new_active = bond->curr_active_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 if (!new_active) { /* there were no active slaves left */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000981 if (bond->slave_cnt > 0) /* found one slave */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 new_active = bond->first_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000983 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return NULL; /* still no slave, return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if ((bond->primary_slave) &&
Jiri Pirkoa5499522009-09-25 03:28:09 +0000988 bond->primary_slave->link == BOND_LINK_UP &&
989 bond_should_change_active(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 new_active = bond->primary_slave;
991 }
992
993 /* remember where to stop iterating over the slaves */
994 old_active = new_active;
995
996 bond_for_each_slave_from(bond, new_active, i, old_active) {
Jiri Pirkob9f60252009-08-31 11:09:38 +0000997 if (new_active->link == BOND_LINK_UP) {
998 return new_active;
999 } else if (new_active->link == BOND_LINK_BACK &&
1000 IS_UP(new_active->dev)) {
1001 /* link up, but waiting for stabilization */
1002 if (new_active->delay < mintime) {
1003 mintime = new_active->delay;
1004 bestslave = new_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006 }
1007 }
1008
1009 return bestslave;
1010}
1011
Ben Hutchingsad246c92011-04-26 15:25:52 +00001012static bool bond_should_notify_peers(struct bonding *bond)
1013{
1014 struct slave *slave = bond->curr_active_slave;
1015
1016 pr_debug("bond_should_notify_peers: bond %s slave %s\n",
1017 bond->dev->name, slave ? slave->dev->name : "NULL");
1018
1019 if (!slave || !bond->send_peer_notif ||
1020 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
1021 return false;
1022
1023 bond->send_peer_notif--;
1024 return true;
1025}
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027/**
1028 * change_active_interface - change the active slave into the specified one
1029 * @bond: our bonding struct
1030 * @new: the new slave to make the active one
1031 *
1032 * Set the new slave to the bond's settings and unset them on the old
1033 * curr_active_slave.
1034 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1035 *
1036 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1037 * because it is apparently the best available slave we have, even though its
1038 * updelay hasn't timed out yet.
1039 *
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001040 * If new_active is not NULL, caller must hold bond->lock for read and
1041 * curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001043void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 struct slave *old_active = bond->curr_active_slave;
1046
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001047 if (old_active == new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 if (new_active) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07001051 new_active->jiffies = jiffies;
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (new_active->link == BOND_LINK_BACK) {
1054 if (USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001055 pr_info("%s: making interface %s the new active one %d ms earlier.\n",
1056 bond->dev->name, new_active->dev->name,
1057 (bond->params.updelay - new_active->delay) * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059
1060 new_active->delay = 0;
1061 new_active->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001063 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Holger Eitzenberger58402052008-12-09 23:07:13 -08001066 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 } else {
1069 if (USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001070 pr_info("%s: making interface %s the new active one.\n",
1071 bond->dev->name, new_active->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073 }
1074 }
1075
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001076 if (USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 bond_mc_swap(bond, new_active, old_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Holger Eitzenberger58402052008-12-09 23:07:13 -08001079 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 bond_alb_handle_active_change(bond, new_active);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001081 if (old_active)
1082 bond_set_slave_inactive_flags(old_active);
1083 if (new_active)
1084 bond_set_slave_active_flags(new_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 } else {
1086 bond->curr_active_slave = new_active;
1087 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001088
1089 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001090 if (old_active)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001091 bond_set_slave_inactive_flags(old_active);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001092
1093 if (new_active) {
Ben Hutchingsad246c92011-04-26 15:25:52 +00001094 bool should_notify_peers = false;
1095
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001096 bond_set_slave_active_flags(new_active);
Moni Shoua2ab82852007-10-09 19:43:39 -07001097
Or Gerlitz709f8a42008-06-13 18:12:01 -07001098 if (bond->params.fail_over_mac)
1099 bond_do_fail_over_mac(bond, new_active,
1100 old_active);
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001101
Ben Hutchingsad246c92011-04-26 15:25:52 +00001102 if (netif_running(bond->dev)) {
1103 bond->send_peer_notif =
1104 bond->params.num_peer_notif;
1105 should_notify_peers =
1106 bond_should_notify_peers(bond);
1107 }
1108
Or Gerlitz01f31092008-06-13 18:12:02 -07001109 write_unlock_bh(&bond->curr_slave_lock);
1110 read_unlock(&bond->lock);
1111
Moni Shoua75c78502009-09-15 02:37:40 -07001112 netdev_bonding_change(bond->dev, NETDEV_BONDING_FAILOVER);
Ben Hutchingsad246c92011-04-26 15:25:52 +00001113 if (should_notify_peers)
1114 netdev_bonding_change(bond->dev,
1115 NETDEV_NOTIFY_PEERS);
Or Gerlitz01f31092008-06-13 18:12:02 -07001116
1117 read_lock(&bond->lock);
1118 write_lock_bh(&bond->curr_slave_lock);
Moni Shoua7893b242008-05-17 21:10:12 -07001119 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001120 }
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001121
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001122 /* resend IGMP joins since active slave has changed or
Flavio Leitner94265cf2011-05-25 08:38:58 +00001123 * all were sent on curr_active_slave.
1124 * resend only if bond is brought up with the affected
1125 * bonding modes and the retransmission is enabled */
1126 if (netif_running(bond->dev) && (bond->params.resend_igmp > 0) &&
1127 ((USES_PRIMARY(bond->params.mode) && new_active) ||
1128 bond->params.mode == BOND_MODE_ROUNDROBIN)) {
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001129 bond->igmp_retrans = bond->params.resend_igmp;
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001130 queue_delayed_work(bond->wq, &bond->mcast_work, 0);
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
1134/**
1135 * bond_select_active_slave - select a new active slave, if needed
1136 * @bond: our bonding struct
1137 *
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001138 * This functions should be called when one of the following occurs:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 * - The old curr_active_slave has been released or lost its link.
1140 * - The primary_slave has got its link back.
1141 * - A slave has got its link back and there's no old curr_active_slave.
1142 *
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001143 * Caller must hold bond->lock for read and curr_slave_lock for write_bh.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001145void bond_select_active_slave(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 struct slave *best_slave;
Jay Vosburghff59c452006-03-27 13:27:43 -08001148 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 best_slave = bond_find_best_slave(bond);
1151 if (best_slave != bond->curr_active_slave) {
1152 bond_change_active_slave(bond, best_slave);
Jay Vosburghff59c452006-03-27 13:27:43 -08001153 rv = bond_set_carrier(bond);
1154 if (!rv)
1155 return;
1156
1157 if (netif_carrier_ok(bond->dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001158 pr_info("%s: first active interface up!\n",
1159 bond->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08001160 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001161 pr_info("%s: now running without any active interface !\n",
1162 bond->dev->name);
Jay Vosburghff59c452006-03-27 13:27:43 -08001163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165}
1166
1167/*--------------------------- slave list handling ---------------------------*/
1168
1169/*
1170 * This function attaches the slave to the end of list.
1171 *
1172 * bond->lock held for writing by caller.
1173 */
1174static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
1175{
1176 if (bond->first_slave == NULL) { /* attaching the first slave */
1177 new_slave->next = new_slave;
1178 new_slave->prev = new_slave;
1179 bond->first_slave = new_slave;
1180 } else {
1181 new_slave->next = bond->first_slave;
1182 new_slave->prev = bond->first_slave->prev;
1183 new_slave->next->prev = new_slave;
1184 new_slave->prev->next = new_slave;
1185 }
1186
1187 bond->slave_cnt++;
1188}
1189
1190/*
1191 * This function detaches the slave from the list.
1192 * WARNING: no check is made to verify if the slave effectively
1193 * belongs to <bond>.
1194 * Nothing is freed on return, structures are just unchained.
1195 * If any slave pointer in bond was pointing to <slave>,
1196 * it should be changed by the calling function.
1197 *
1198 * bond->lock held for writing by caller.
1199 */
1200static void bond_detach_slave(struct bonding *bond, struct slave *slave)
1201{
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001202 if (slave->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 slave->next->prev = slave->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001205 if (slave->prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 slave->prev->next = slave->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 if (bond->first_slave == slave) { /* slave is the first slave */
1209 if (bond->slave_cnt > 1) { /* there are more slave */
1210 bond->first_slave = slave->next;
1211 } else {
1212 bond->first_slave = NULL; /* slave was the last one */
1213 }
1214 }
1215
1216 slave->next = NULL;
1217 slave->prev = NULL;
1218 bond->slave_cnt--;
1219}
1220
WANG Congf6dc31a2010-05-06 00:48:51 -07001221#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001222static inline int slave_enable_netpoll(struct slave *slave)
WANG Congf6dc31a2010-05-06 00:48:51 -07001223{
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001224 struct netpoll *np;
1225 int err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001226
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001227 np = kzalloc(sizeof(*np), GFP_KERNEL);
1228 err = -ENOMEM;
1229 if (!np)
1230 goto out;
1231
1232 np->dev = slave->dev;
WANG Congcefa9992011-06-19 16:13:01 -07001233 strlcpy(np->dev_name, slave->dev->name, IFNAMSIZ);
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001234 err = __netpoll_setup(np);
1235 if (err) {
1236 kfree(np);
1237 goto out;
WANG Congf6dc31a2010-05-06 00:48:51 -07001238 }
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001239 slave->np = np;
1240out:
1241 return err;
1242}
1243static inline void slave_disable_netpoll(struct slave *slave)
1244{
1245 struct netpoll *np = slave->np;
1246
1247 if (!np)
1248 return;
1249
1250 slave->np = NULL;
1251 synchronize_rcu_bh();
1252 __netpoll_cleanup(np);
1253 kfree(np);
1254}
1255static inline bool slave_dev_support_netpoll(struct net_device *slave_dev)
1256{
1257 if (slave_dev->priv_flags & IFF_DISABLE_NETPOLL)
1258 return false;
1259 if (!slave_dev->netdev_ops->ndo_poll_controller)
1260 return false;
1261 return true;
WANG Congf6dc31a2010-05-06 00:48:51 -07001262}
1263
1264static void bond_poll_controller(struct net_device *bond_dev)
1265{
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001266}
1267
1268static void __bond_netpoll_cleanup(struct bonding *bond)
1269{
Neil Hormanc2355e12010-10-13 16:01:49 +00001270 struct slave *slave;
1271 int i;
1272
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001273 bond_for_each_slave(bond, slave, i)
1274 if (IS_UP(slave->dev))
1275 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07001276}
WANG Congf6dc31a2010-05-06 00:48:51 -07001277static void bond_netpoll_cleanup(struct net_device *bond_dev)
1278{
1279 struct bonding *bond = netdev_priv(bond_dev);
WANG Congf6dc31a2010-05-06 00:48:51 -07001280
1281 read_lock(&bond->lock);
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001282 __bond_netpoll_cleanup(bond);
WANG Congf6dc31a2010-05-06 00:48:51 -07001283 read_unlock(&bond->lock);
1284}
1285
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001286static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
1287{
1288 struct bonding *bond = netdev_priv(dev);
1289 struct slave *slave;
1290 int i, err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001291
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001292 read_lock(&bond->lock);
1293 bond_for_each_slave(bond, slave, i) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001294 err = slave_enable_netpoll(slave);
1295 if (err) {
1296 __bond_netpoll_cleanup(bond);
1297 break;
1298 }
1299 }
1300 read_unlock(&bond->lock);
1301 return err;
1302}
1303
1304static struct netpoll_info *bond_netpoll_info(struct bonding *bond)
1305{
1306 return bond->dev->npinfo;
1307}
1308
1309#else
1310static inline int slave_enable_netpoll(struct slave *slave)
1311{
1312 return 0;
1313}
1314static inline void slave_disable_netpoll(struct slave *slave)
1315{
1316}
WANG Congf6dc31a2010-05-06 00:48:51 -07001317static void bond_netpoll_cleanup(struct net_device *bond_dev)
1318{
1319}
WANG Congf6dc31a2010-05-06 00:48:51 -07001320#endif
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322/*---------------------------------- IOCTL ----------------------------------*/
1323
Adrian Bunk4ad072c2007-07-09 11:51:12 -07001324static int bond_sethwaddr(struct net_device *bond_dev,
1325 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001327 pr_debug("bond_dev=%p\n", bond_dev);
1328 pr_debug("slave_dev=%p\n", slave_dev);
1329 pr_debug("slave_dev->addr_len=%d\n", slave_dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
1331 return 0;
1332}
1333
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001334static netdev_features_t bond_fix_features(struct net_device *dev,
1335 netdev_features_t features)
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001336{
1337 struct slave *slave;
1338 struct bonding *bond = netdev_priv(dev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001339 netdev_features_t mask;
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001340 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001341
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001342 read_lock(&bond->lock);
1343
1344 if (!bond->first_slave) {
1345 /* Disable adding VLANs to empty bond. But why? --mq */
1346 features |= NETIF_F_VLAN_CHALLENGED;
1347 goto out;
1348 }
1349
1350 mask = features;
1351 features &= ~NETIF_F_ONE_FOR_ALL;
1352 features |= NETIF_F_ALL_FOR_ALL;
1353
1354 bond_for_each_slave(bond, slave, i) {
1355 features = netdev_increment_features(features,
1356 slave->dev->features,
1357 mask);
1358 }
1359
1360out:
1361 read_unlock(&bond->lock);
1362 return features;
1363}
1364
Michał Mirosław62f2a3a2011-07-13 14:10:29 +00001365#define BOND_VLAN_FEATURES (NETIF_F_ALL_CSUM | NETIF_F_SG | \
1366 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
1367 NETIF_F_HIGHDMA | NETIF_F_LRO)
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001368
1369static void bond_compute_features(struct bonding *bond)
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001370{
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001371 struct slave *slave;
1372 struct net_device *bond_dev = bond->dev;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001373 netdev_features_t vlan_features = BOND_VLAN_FEATURES;
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001374 unsigned short max_hard_header_len = ETH_HLEN;
Jay Vosburgh8e3babc2005-11-04 18:45:45 -08001375 int i;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001376
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001377 read_lock(&bond->lock);
Herbert Xub63365a2008-10-23 01:11:29 -07001378
1379 if (!bond->first_slave)
1380 goto done;
1381
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001382 bond_for_each_slave(bond, slave, i) {
Jay Vosburgh278339a2009-08-28 12:05:12 +00001383 vlan_features = netdev_increment_features(vlan_features,
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001384 slave->dev->vlan_features, BOND_VLAN_FEATURES);
1385
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001386 if (slave->dev->hard_header_len > max_hard_header_len)
1387 max_hard_header_len = slave->dev->hard_header_len;
1388 }
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001389
Herbert Xub63365a2008-10-23 01:11:29 -07001390done:
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001391 bond_dev->vlan_features = vlan_features;
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001392 bond_dev->hard_header_len = max_hard_header_len;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001393
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001394 read_unlock(&bond->lock);
1395
1396 netdev_change_features(bond_dev);
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001397}
1398
Moni Shoua872254d2007-10-09 19:43:38 -07001399static void bond_setup_by_slave(struct net_device *bond_dev,
1400 struct net_device *slave_dev)
1401{
Wang Chen454d7c92008-11-12 23:37:49 -08001402 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07001403
Stephen Hemminger00829822008-11-20 20:14:53 -08001404 bond_dev->header_ops = slave_dev->header_ops;
Moni Shoua872254d2007-10-09 19:43:38 -07001405
1406 bond_dev->type = slave_dev->type;
1407 bond_dev->hard_header_len = slave_dev->hard_header_len;
1408 bond_dev->addr_len = slave_dev->addr_len;
1409
1410 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1411 slave_dev->addr_len);
Moni Shouad90a1622007-10-09 19:43:43 -07001412 bond->setup_by_slave = 1;
Moni Shoua872254d2007-10-09 19:43:38 -07001413}
1414
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001415/* On bonding slaves other than the currently active slave, suppress
Jiri Pirko3aba8912011-04-19 03:48:16 +00001416 * duplicates except for alb non-mcast/bcast.
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001417 */
1418static bool bond_should_deliver_exact_match(struct sk_buff *skb,
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001419 struct slave *slave,
1420 struct bonding *bond)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001421{
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001422 if (bond_is_slave_inactive(slave)) {
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001423 if (bond->params.mode == BOND_MODE_ALB &&
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001424 skb->pkt_type != PACKET_BROADCAST &&
1425 skb->pkt_type != PACKET_MULTICAST)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001426 return false;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001427 return true;
1428 }
1429 return false;
1430}
1431
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001432static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001433{
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001434 struct sk_buff *skb = *pskb;
Jiri Pirkof1c17752011-03-12 03:14:35 +00001435 struct slave *slave;
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001436 struct bonding *bond;
Mitsuo Hayasaka4d974802011-10-12 16:04:29 +00001437 void (*recv_probe)(struct sk_buff *, struct bonding *,
1438 struct slave *);
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001439
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001440 skb = skb_share_check(skb, GFP_ATOMIC);
1441 if (unlikely(!skb))
1442 return RX_HANDLER_CONSUMED;
1443
1444 *pskb = skb;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001445
Jiri Pirko35d48902011-03-22 02:38:12 +00001446 slave = bond_slave_get_rcu(skb->dev);
1447 bond = slave->bond;
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001448
1449 if (bond->params.arp_interval)
Jiri Pirkof1c17752011-03-12 03:14:35 +00001450 slave->dev->last_rx = jiffies;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001451
Mitsuo Hayasaka4d974802011-10-12 16:04:29 +00001452 recv_probe = ACCESS_ONCE(bond->recv_probe);
1453 if (recv_probe) {
Jiri Pirko3aba8912011-04-19 03:48:16 +00001454 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
1455
1456 if (likely(nskb)) {
Mitsuo Hayasaka4d974802011-10-12 16:04:29 +00001457 recv_probe(nskb, bond, slave);
Jiri Pirko3aba8912011-04-19 03:48:16 +00001458 dev_kfree_skb(nskb);
1459 }
1460 }
1461
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001462 if (bond_should_deliver_exact_match(skb, slave, bond)) {
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001463 return RX_HANDLER_EXACT;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001464 }
1465
Jiri Pirko35d48902011-03-22 02:38:12 +00001466 skb->dev = bond->dev;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001467
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001468 if (bond->params.mode == BOND_MODE_ALB &&
Jiri Pirko35d48902011-03-22 02:38:12 +00001469 bond->dev->priv_flags & IFF_BRIDGE_PORT &&
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001470 skb->pkt_type == PACKET_HOST) {
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001471
Changli Gao541ac7c2011-03-02 21:07:14 +00001472 if (unlikely(skb_cow_head(skb,
1473 skb->data - skb_mac_header(skb)))) {
1474 kfree_skb(skb);
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001475 return RX_HANDLER_CONSUMED;
Changli Gao541ac7c2011-03-02 21:07:14 +00001476 }
Jiri Pirko35d48902011-03-22 02:38:12 +00001477 memcpy(eth_hdr(skb)->h_dest, bond->dev->dev_addr, ETH_ALEN);
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001478 }
1479
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001480 return RX_HANDLER_ANOTHER;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001481}
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483/* enslave device <slave> to bond device <master> */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001484int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
Wang Chen454d7c92008-11-12 23:37:49 -08001486 struct bonding *bond = netdev_priv(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001487 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 struct slave *new_slave = NULL;
Jiri Pirko22bedad2010-04-01 21:22:57 +00001489 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 struct sockaddr addr;
1491 int link_reporting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 int res = 0;
1493
nsxfreddy@gmail.com552709d2005-09-21 14:18:04 -05001494 if (!bond->params.use_carrier && slave_dev->ethtool_ops == NULL &&
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001495 slave_ops->ndo_do_ioctl == NULL) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001496 pr_warning("%s: Warning: no link monitoring support for %s\n",
1497 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 /* already enslaved */
1501 if (slave_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001502 pr_debug("Error, Device was already enslaved\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return -EBUSY;
1504 }
1505
1506 /* vlan challenged mutual exclusion */
1507 /* no need to lock since we're protected by rtnl_lock */
1508 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001509 pr_debug("%s: NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Jiri Pirkocc0e4072011-07-20 04:54:46 +00001510 if (bond_vlan_used(bond)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001511 pr_err("%s: Error: cannot enslave VLAN challenged slave %s on VLAN enabled bond %s\n",
1512 bond_dev->name, slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return -EPERM;
1514 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001515 pr_warning("%s: Warning: enslaved VLAN challenged slave %s. Adding VLANs will be blocked as long as %s is part of bond %s\n",
1516 bond_dev->name, slave_dev->name,
1517 slave_dev->name, bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
1519 } else {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001520 pr_debug("%s: ! NETIF_F_VLAN_CHALLENGED\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522
Jay Vosburgh217df672005-09-26 16:11:50 -07001523 /*
1524 * Old ifenslave binaries are no longer supported. These can
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001525 * be identified with moderate accuracy by the state of the slave:
Jay Vosburgh217df672005-09-26 16:11:50 -07001526 * the current ifenslave will set the interface down prior to
1527 * enslaving it; the old ifenslave will not.
1528 */
1529 if ((slave_dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001530 pr_err("%s is up. This may be due to an out of date ifenslave.\n",
Jay Vosburgh217df672005-09-26 16:11:50 -07001531 slave_dev->name);
1532 res = -EPERM;
1533 goto err_undo_flags;
1534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Moni Shoua872254d2007-10-09 19:43:38 -07001536 /* set bonding device ether type by slave - bonding netdevices are
1537 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1538 * there is a need to override some of the type dependent attribs/funcs.
1539 *
1540 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1541 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1542 */
1543 if (bond->slave_cnt == 0) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001544 if (bond_dev->type != slave_dev->type) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001545 pr_debug("%s: change device type from %d to %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001546 bond_dev->name,
1547 bond_dev->type, slave_dev->type);
Moni Shoua75c78502009-09-15 02:37:40 -07001548
Jiri Pirko3ca5b402010-03-10 10:29:35 +00001549 res = netdev_bonding_change(bond_dev,
1550 NETDEV_PRE_TYPE_CHANGE);
1551 res = notifier_to_errno(res);
1552 if (res) {
1553 pr_err("%s: refused to change device type\n",
1554 bond_dev->name);
1555 res = -EBUSY;
1556 goto err_undo_flags;
1557 }
Moni Shoua75c78502009-09-15 02:37:40 -07001558
Jiri Pirko32a806c2010-03-19 04:00:23 +00001559 /* Flush unicast and multicast addresses */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001560 dev_uc_flush(bond_dev);
Jiri Pirko22bedad2010-04-01 21:22:57 +00001561 dev_mc_flush(bond_dev);
Jiri Pirko32a806c2010-03-19 04:00:23 +00001562
Moni Shouae36b9d12009-07-15 04:56:31 +00001563 if (slave_dev->type != ARPHRD_ETHER)
1564 bond_setup_by_slave(bond_dev, slave_dev);
Neil Horman550fd082011-07-26 06:05:38 +00001565 else {
Moni Shouae36b9d12009-07-15 04:56:31 +00001566 ether_setup(bond_dev);
Neil Horman550fd082011-07-26 06:05:38 +00001567 bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1568 }
Moni Shoua75c78502009-09-15 02:37:40 -07001569
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00001570 netdev_bonding_change(bond_dev,
1571 NETDEV_POST_TYPE_CHANGE);
Moni Shouae36b9d12009-07-15 04:56:31 +00001572 }
Moni Shoua872254d2007-10-09 19:43:38 -07001573 } else if (bond_dev->type != slave_dev->type) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001574 pr_err("%s ether type (%d) is different from other slaves (%d), can not enslave it.\n",
1575 slave_dev->name,
1576 slave_dev->type, bond_dev->type);
1577 res = -EINVAL;
1578 goto err_undo_flags;
Moni Shoua872254d2007-10-09 19:43:38 -07001579 }
1580
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001581 if (slave_ops->ndo_set_mac_address == NULL) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001582 if (bond->slave_cnt == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001583 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.",
1584 bond_dev->name);
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001585 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1586 } else if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001587 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",
1588 bond_dev->name);
Moni Shoua2ab82852007-10-09 19:43:39 -07001589 res = -EOPNOTSUPP;
1590 goto err_undo_flags;
1591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593
Amerigo Wang8d8fc292011-05-19 21:39:10 +00001594 call_netdevice_notifiers(NETDEV_JOIN, slave_dev);
1595
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001596 /* If this is the first slave, then we need to set the master's hardware
1597 * address to be the same as the slave's. */
David Strandd13a2cb2010-12-01 11:43:08 -08001598 if (is_zero_ether_addr(bond->dev->dev_addr))
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001599 memcpy(bond->dev->dev_addr, slave_dev->dev_addr,
1600 slave_dev->addr_len);
1601
1602
Joe Jin243cb4e2007-02-06 14:16:40 -08001603 new_slave = kzalloc(sizeof(struct slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (!new_slave) {
1605 res = -ENOMEM;
1606 goto err_undo_flags;
1607 }
1608
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001609 /*
1610 * Set the new_slave's queue_id to be zero. Queue ID mapping
1611 * is set via sysfs or module option if desired.
1612 */
1613 new_slave->queue_id = 0;
1614
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001615 /* Save slave's original mtu and then set it to match the bond */
1616 new_slave->original_mtu = slave_dev->mtu;
1617 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1618 if (res) {
1619 pr_debug("Error %d calling dev_set_mtu\n", res);
1620 goto err_free;
1621 }
1622
Jay Vosburgh217df672005-09-26 16:11:50 -07001623 /*
1624 * Save slave's original ("permanent") mac address for modes
1625 * that need it, and for restoring it upon release, and then
1626 * set it to the master's address
1627 */
1628 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Jay Vosburghdd957c52007-10-09 19:57:24 -07001630 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07001631 /*
1632 * Set slave to master's mac address. The application already
1633 * set the master's mac address to that of the first slave
1634 */
1635 memcpy(addr.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
1636 addr.sa_family = slave_dev->type;
1637 res = dev_set_mac_address(slave_dev, &addr);
1638 if (res) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001639 pr_debug("Error %d calling set_mac_address\n", res);
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001640 goto err_restore_mtu;
Moni Shoua2ab82852007-10-09 19:43:39 -07001641 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Jiri Pirko1765a572011-02-12 06:48:36 +00001644 res = netdev_set_bond_master(slave_dev, bond_dev);
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001645 if (res) {
Jiri Pirko1765a572011-02-12 06:48:36 +00001646 pr_debug("Error %d calling netdev_set_bond_master\n", res);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001647 goto err_restore_mac;
Jay Vosburghc2edacf2007-07-09 10:42:47 -07001648 }
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001649
Jay Vosburgh217df672005-09-26 16:11:50 -07001650 /* open the slave since the application closed it */
1651 res = dev_open(slave_dev);
1652 if (res) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001653 pr_debug("Opening slave %s failed\n", slave_dev->name);
Jiri Pirko35d48902011-03-22 02:38:12 +00001654 goto err_unset_master;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
1656
Jiri Pirko35d48902011-03-22 02:38:12 +00001657 new_slave->bond = bond;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 new_slave->dev = slave_dev;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001659 slave_dev->priv_flags |= IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Holger Eitzenberger58402052008-12-09 23:07:13 -08001661 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 /* bond_alb_init_slave() must be called before all other stages since
1663 * it might fail and we do not want to have to undo everything
1664 */
1665 res = bond_alb_init_slave(bond, new_slave);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001666 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001667 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
1669
1670 /* If the mode USES_PRIMARY, then the new slave gets the
1671 * master's promisc (and mc) settings only if it becomes the
1672 * curr_active_slave, and that is taken care of later when calling
1673 * bond_change_active()
1674 */
1675 if (!USES_PRIMARY(bond->params.mode)) {
1676 /* set promiscuity level to new slave */
1677 if (bond_dev->flags & IFF_PROMISC) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001678 res = dev_set_promiscuity(slave_dev, 1);
1679 if (res)
1680 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682
1683 /* set allmulti level to new slave */
1684 if (bond_dev->flags & IFF_ALLMULTI) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -07001685 res = dev_set_allmulti(slave_dev, 1);
1686 if (res)
1687 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
1689
David S. Millerb9e40852008-07-15 00:15:08 -07001690 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 /* upload master's mc_list to new slave */
Jiri Pirko22bedad2010-04-01 21:22:57 +00001692 netdev_for_each_mc_addr(ha, bond_dev)
1693 dev_mc_add(slave_dev, ha->addr);
David S. Millerb9e40852008-07-15 00:15:08 -07001694 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696
1697 if (bond->params.mode == BOND_MODE_8023AD) {
1698 /* add lacpdu mc addr to mc list */
1699 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1700
Jiri Pirko22bedad2010-04-01 21:22:57 +00001701 dev_mc_add(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
1703
1704 bond_add_vlans_on_slave(bond, slave_dev);
1705
1706 write_lock_bh(&bond->lock);
1707
1708 bond_attach_slave(bond, new_slave);
1709
1710 new_slave->delay = 0;
1711 new_slave->link_failure_count = 0;
1712
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001713 write_unlock_bh(&bond->lock);
1714
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001715 bond_compute_features(bond);
1716
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001717 read_lock(&bond->lock);
1718
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001719 new_slave->last_arp_rx = jiffies;
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 if (bond->params.miimon && !bond->params.use_carrier) {
1722 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1723
1724 if ((link_reporting == -1) && !bond->params.arp_interval) {
1725 /*
1726 * miimon is set but a bonded network driver
1727 * does not support ETHTOOL/MII and
1728 * arp_interval is not set. Note: if
1729 * use_carrier is enabled, we will never go
1730 * here (because netif_carrier is always
1731 * supported); thus, we don't need to change
1732 * the messages for netif_carrier.
1733 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001734 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 -08001735 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 } else if (link_reporting == -1) {
1737 /* unable get link status using mii/ethtool */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001738 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",
1739 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
1741 }
1742
1743 /* check for initial state */
1744 if (!bond->params.miimon ||
1745 (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS)) {
1746 if (bond->params.updelay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001747 pr_debug("Initial state of slave_dev is BOND_LINK_BACK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 new_slave->link = BOND_LINK_BACK;
1749 new_slave->delay = bond->params.updelay;
1750 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001751 pr_debug("Initial state of slave_dev is BOND_LINK_UP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 new_slave->link = BOND_LINK_UP;
1753 }
1754 new_slave->jiffies = jiffies;
1755 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001756 pr_debug("Initial state of slave_dev is BOND_LINK_DOWN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 new_slave->link = BOND_LINK_DOWN;
1758 }
1759
Weiping Pan98f41f62011-10-31 17:20:48 +00001760 bond_update_speed_duplex(new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 if (USES_PRIMARY(bond->params.mode) && bond->params.primary[0]) {
1763 /* if there is a primary slave, remember it */
Jiri Pirkoa5499522009-09-25 03:28:09 +00001764 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 bond->primary_slave = new_slave;
Jiri Pirkoa5499522009-09-25 03:28:09 +00001766 bond->force_primary = true;
1767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
1769
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001770 write_lock_bh(&bond->curr_slave_lock);
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 switch (bond->params.mode) {
1773 case BOND_MODE_ACTIVEBACKUP:
Jay Vosburgh8a8e4472006-09-22 21:56:15 -07001774 bond_set_slave_inactive_flags(new_slave);
1775 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 break;
1777 case BOND_MODE_8023AD:
1778 /* in 802.3ad mode, the internal mechanism
1779 * will activate the slaves in the selected
1780 * aggregator
1781 */
1782 bond_set_slave_inactive_flags(new_slave);
1783 /* if this is the first slave */
1784 if (bond->slave_cnt == 1) {
1785 SLAVE_AD_INFO(new_slave).id = 1;
1786 /* Initialize AD with the number of times that the AD timer is called in 1 second
1787 * can be called only after the mac address of the bond is set
1788 */
Peter Pan(潘卫平)56d00c672011-06-08 21:19:02 +00001789 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 } else {
1791 SLAVE_AD_INFO(new_slave).id =
1792 SLAVE_AD_INFO(new_slave->prev).id + 1;
1793 }
1794
1795 bond_3ad_bind_slave(new_slave);
1796 break;
1797 case BOND_MODE_TLB:
1798 case BOND_MODE_ALB:
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001799 bond_set_active_slave(new_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001800 bond_set_slave_inactive_flags(new_slave);
Jiri Pirko5a29f782009-03-25 17:23:38 -07001801 bond_select_active_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 break;
1803 default:
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08001804 pr_debug("This slave is always active in trunk mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 /* always active in trunk mode */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001807 bond_set_active_slave(new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 /* In trunking mode there is little meaning to curr_active_slave
1810 * anyway (it holds no special properties of the bond device),
1811 * so we can change it without calling change_active_interface()
1812 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001813 if (!bond->curr_active_slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 bond->curr_active_slave = new_slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 break;
1817 } /* switch(bond_mode) */
1818
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001819 write_unlock_bh(&bond->curr_slave_lock);
1820
Jay Vosburghff59c452006-03-27 13:27:43 -08001821 bond_set_carrier(bond);
1822
WANG Congf6dc31a2010-05-06 00:48:51 -07001823#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001824 slave_dev->npinfo = bond_netpoll_info(bond);
1825 if (slave_dev->npinfo) {
1826 if (slave_enable_netpoll(new_slave)) {
1827 read_unlock(&bond->lock);
1828 pr_info("Error, %s: master_dev is using netpoll, "
1829 "but new slave device does not support netpoll.\n",
1830 bond_dev->name);
1831 res = -EBUSY;
1832 goto err_close;
1833 }
WANG Congf6dc31a2010-05-06 00:48:51 -07001834 }
1835#endif
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001836
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001837 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001839 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1840 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001841 goto err_close;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001842
Jiri Pirko35d48902011-03-22 02:38:12 +00001843 res = netdev_rx_handler_register(slave_dev, bond_handle_frame,
1844 new_slave);
1845 if (res) {
1846 pr_debug("Error %d calling netdev_rx_handler_register\n", res);
1847 goto err_dest_symlinks;
1848 }
1849
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001850 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
1851 bond_dev->name, slave_dev->name,
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001852 bond_is_active_slave(new_slave) ? "n active" : " backup",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001853 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 /* enslave is successful */
1856 return 0;
1857
1858/* Undo stages on error */
Jiri Pirko35d48902011-03-22 02:38:12 +00001859err_dest_symlinks:
1860 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862err_close:
1863 dev_close(slave_dev);
1864
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001865err_unset_master:
Jiri Pirko1765a572011-02-12 06:48:36 +00001866 netdev_set_bond_master(slave_dev, NULL);
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868err_restore_mac:
Jay Vosburghdd957c52007-10-09 19:57:24 -07001869 if (!bond->params.fail_over_mac) {
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001870 /* XXX TODO - fom follow mode needs to change master's
1871 * MAC if this slave's MAC is in use by the bond, or at
1872 * least print a warning.
1873 */
Moni Shoua2ab82852007-10-09 19:43:39 -07001874 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1875 addr.sa_family = slave_dev->type;
1876 dev_set_mac_address(slave_dev, &addr);
1877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001879err_restore_mtu:
1880 dev_set_mtu(slave_dev, new_slave->original_mtu);
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882err_free:
1883 kfree(new_slave);
1884
1885err_undo_flags:
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001886 bond_compute_features(bond);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return res;
1889}
1890
1891/*
1892 * Try to release the slave device <slave> from the bond device <master>
1893 * It is legal to access curr_active_slave without a lock because all the function
1894 * is write-locked.
1895 *
1896 * The rules for slave state should be:
1897 * for Active/Backup:
1898 * Active stays on all backups go down
1899 * for Bonded connections:
1900 * The first up interface should be left on and all others downed.
1901 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001902int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Wang Chen454d7c92008-11-12 23:37:49 -08001904 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 struct slave *slave, *oldcurrent;
1906 struct sockaddr addr;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001907 netdev_features_t old_features = bond_dev->features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 /* slave is not a slave or master is not master of this slave */
1910 if (!(slave_dev->flags & IFF_SLAVE) ||
1911 (slave_dev->master != bond_dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001912 pr_err("%s: Error: cannot release %s.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 bond_dev->name, slave_dev->name);
1914 return -EINVAL;
1915 }
1916
Neil Hormane843fa52010-10-13 16:01:50 +00001917 block_netpoll_tx();
Amerigo Wangdaf92092011-05-19 21:39:12 +00001918 netdev_bonding_change(bond_dev, NETDEV_RELEASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 write_lock_bh(&bond->lock);
1920
1921 slave = bond_get_slave_by_dev(bond, slave_dev);
1922 if (!slave) {
1923 /* not a slave of this bond */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001924 pr_info("%s: %s not enslaved\n",
1925 bond_dev->name, slave_dev->name);
Jay Vosburghf5e2a7b2006-02-07 21:17:22 -08001926 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001927 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 return -EINVAL;
1929 }
1930
Jiri Pirko35d48902011-03-22 02:38:12 +00001931 /* unregister rx_handler early so bond_handle_frame wouldn't be called
1932 * for this slave anymore.
1933 */
1934 netdev_rx_handler_unregister(slave_dev);
1935 write_unlock_bh(&bond->lock);
1936 synchronize_net();
1937 write_lock_bh(&bond->lock);
1938
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07001939 if (!bond->params.fail_over_mac) {
Joe Perches8e95a202009-12-03 07:58:21 +00001940 if (!compare_ether_addr(bond_dev->dev_addr, slave->perm_hwaddr) &&
1941 bond->slave_cnt > 1)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001942 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",
1943 bond_dev->name, slave_dev->name,
1944 slave->perm_hwaddr,
1945 bond_dev->name, slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
1947
1948 /* Inform AD package of unbinding of slave. */
1949 if (bond->params.mode == BOND_MODE_8023AD) {
1950 /* must be called before the slave is
1951 * detached from the list
1952 */
1953 bond_3ad_unbind_slave(slave);
1954 }
1955
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001956 pr_info("%s: releasing %s interface %s\n",
1957 bond_dev->name,
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001958 bond_is_active_slave(slave) ? "active" : "backup",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001959 slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 oldcurrent = bond->curr_active_slave;
1962
1963 bond->current_arp_slave = NULL;
1964
1965 /* release the slave from its bond */
1966 bond_detach_slave(bond, slave);
1967
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001968 if (bond->primary_slave == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 bond->primary_slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001971 if (oldcurrent == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 bond_change_active_slave(bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Holger Eitzenberger58402052008-12-09 23:07:13 -08001974 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 /* Must be called only after the slave has been
1976 * detached from the list and the curr_active_slave
1977 * has been cleared (if our_slave == old_current),
1978 * but before a new active slave is selected.
1979 */
Jay Vosburgh25433312008-01-17 16:24:59 -08001980 write_unlock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 bond_alb_deinit_slave(bond, slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08001982 write_lock_bh(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001985 if (oldcurrent == slave) {
1986 /*
1987 * Note that we hold RTNL over this sequence, so there
1988 * is no concern that another slave add/remove event
1989 * will interfere.
1990 */
1991 write_unlock_bh(&bond->lock);
1992 read_lock(&bond->lock);
1993 write_lock_bh(&bond->curr_slave_lock);
1994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 bond_select_active_slave(bond);
1996
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001997 write_unlock_bh(&bond->curr_slave_lock);
1998 read_unlock(&bond->lock);
1999 write_lock_bh(&bond->lock);
2000 }
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (bond->slave_cnt == 0) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002003 bond_set_carrier(bond);
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 /* if the last slave was removed, zero the mac address
2006 * of the master so it will be set by the application
2007 * to the mac address of the first slave
2008 */
2009 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2010
Jiri Pirkocc0e4072011-07-20 04:54:46 +00002011 if (bond_vlan_used(bond)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002012 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2013 bond_dev->name, bond_dev->name);
2014 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2015 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
2018
2019 write_unlock_bh(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002020 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Michał Mirosławb2a103e2011-05-07 03:22:17 +00002022 bond_compute_features(bond);
2023 if (!(bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2024 (old_features & NETIF_F_VLAN_CHALLENGED))
2025 pr_info("%s: last VLAN challenged slave %s left bond %s. VLAN blocking is removed\n",
2026 bond_dev->name, slave_dev->name, bond_dev->name);
2027
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002028 /* must do this from outside any spinlocks */
2029 bond_destroy_slave_symlinks(bond_dev, slave_dev);
2030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 bond_del_vlans_from_slave(bond, slave_dev);
2032
2033 /* If the mode USES_PRIMARY, then we should only remove its
2034 * promisc and mc settings if it was the curr_active_slave, but that was
2035 * already taken care of above when we detached the slave
2036 */
2037 if (!USES_PRIMARY(bond->params.mode)) {
2038 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002039 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002043 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002047 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002049 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
2051
Jiri Pirko1765a572011-02-12 06:48:36 +00002052 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002054 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07002055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 /* close slave before restoring its mac address */
2057 dev_close(slave_dev);
2058
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07002059 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002060 /* restore original ("permanent") mac address */
2061 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2062 addr.sa_family = slave_dev->type;
2063 dev_set_mac_address(slave_dev, &addr);
2064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00002066 dev_set_mtu(slave_dev, slave->original_mtu);
2067
Jiri Pirko2d7011c2011-03-16 08:46:43 +00002068 slave_dev->priv_flags &= ~IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 kfree(slave);
2071
2072 return 0; /* deletion OK */
2073}
2074
2075/*
Nicolas de Pesloüandadaa102011-03-19 13:36:18 -07002076* First release a slave and then destroy the bond if no more slaves are left.
Moni Shouad90a1622007-10-09 19:43:43 -07002077* Must be under rtnl_lock when this function is called.
2078*/
stephen hemminger26d8ee72010-10-15 05:09:34 +00002079static int bond_release_and_destroy(struct net_device *bond_dev,
2080 struct net_device *slave_dev)
Moni Shouad90a1622007-10-09 19:43:43 -07002081{
Wang Chen454d7c92008-11-12 23:37:49 -08002082 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002083 int ret;
2084
2085 ret = bond_release(bond_dev, slave_dev);
2086 if ((ret == 0) && (bond->slave_cnt == 0)) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002087 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002088 pr_info("%s: destroying bond %s.\n",
2089 bond_dev->name, bond_dev->name);
Stephen Hemminger9e716262009-06-12 19:02:47 +00002090 unregister_netdevice(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002091 }
2092 return ret;
2093}
2094
2095/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 * This function releases all slaves.
2097 */
2098static int bond_release_all(struct net_device *bond_dev)
2099{
Wang Chen454d7c92008-11-12 23:37:49 -08002100 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 struct slave *slave;
2102 struct net_device *slave_dev;
2103 struct sockaddr addr;
2104
2105 write_lock_bh(&bond->lock);
2106
Jay Vosburghff59c452006-03-27 13:27:43 -08002107 netif_carrier_off(bond_dev);
2108
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002109 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 bond->current_arp_slave = NULL;
2113 bond->primary_slave = NULL;
2114 bond_change_active_slave(bond, NULL);
2115
2116 while ((slave = bond->first_slave) != NULL) {
2117 /* Inform AD package of unbinding of slave
2118 * before slave is detached from the list.
2119 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002120 if (bond->params.mode == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 bond_3ad_unbind_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
2123 slave_dev = slave->dev;
2124 bond_detach_slave(bond, slave);
2125
Jay Vosburgh25433312008-01-17 16:24:59 -08002126 /* now that the slave is detached, unlock and perform
2127 * all the undo steps that should not be called from
2128 * within a lock.
2129 */
2130 write_unlock_bh(&bond->lock);
2131
Jiri Pirko35d48902011-03-22 02:38:12 +00002132 /* unregister rx_handler early so bond_handle_frame wouldn't
2133 * be called for this slave anymore.
2134 */
2135 netdev_rx_handler_unregister(slave_dev);
2136 synchronize_net();
2137
Holger Eitzenberger58402052008-12-09 23:07:13 -08002138 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 /* must be called only after the slave
2140 * has been detached from the list
2141 */
2142 bond_alb_deinit_slave(bond, slave);
2143 }
2144
Mitch Williamsb76cdba2005-11-09 10:36:41 -08002145 bond_destroy_slave_symlinks(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 bond_del_vlans_from_slave(bond, slave_dev);
2147
2148 /* If the mode USES_PRIMARY, then we should only remove its
2149 * promisc and mc settings if it was the curr_active_slave, but that was
2150 * already taken care of above when we detached the slave
2151 */
2152 if (!USES_PRIMARY(bond->params.mode)) {
2153 /* unset promiscuity level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002154 if (bond_dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 /* unset allmulti level from slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002158 if (bond_dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 /* flush master's mc_list from slave */
David S. Millerb9e40852008-07-15 00:15:08 -07002162 netif_addr_lock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 bond_mc_list_flush(bond_dev, slave_dev);
David S. Millerb9e40852008-07-15 00:15:08 -07002164 netif_addr_unlock_bh(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
2166
Jiri Pirko1765a572011-02-12 06:48:36 +00002167 netdev_set_bond_master(slave_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002169 slave_disable_netpoll(slave);
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 /* close slave before restoring its mac address */
2172 dev_close(slave_dev);
2173
Jay Vosburghdd957c52007-10-09 19:57:24 -07002174 if (!bond->params.fail_over_mac) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002175 /* restore original ("permanent") mac address*/
2176 memcpy(addr.sa_data, slave->perm_hwaddr, ETH_ALEN);
2177 addr.sa_family = slave_dev->type;
2178 dev_set_mac_address(slave_dev, &addr);
2179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 kfree(slave);
2182
2183 /* re-acquire the lock before getting the next slave */
2184 write_lock_bh(&bond->lock);
2185 }
2186
2187 /* zero the mac address of the master so it will be
2188 * set by the application to the mac address of the
2189 * first slave
2190 */
2191 memset(bond_dev->dev_addr, 0, bond_dev->addr_len);
2192
Jiri Pirkocc0e4072011-07-20 04:54:46 +00002193 if (bond_vlan_used(bond)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002194 pr_warning("%s: Warning: clearing HW address of %s while it still has VLANs.\n",
2195 bond_dev->name, bond_dev->name);
2196 pr_warning("%s: When re-adding slaves, make sure the bond's HW address matches its VLANs'.\n",
2197 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
2199
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002200 pr_info("%s: released all slaves\n", bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
2202out:
2203 write_unlock_bh(&bond->lock);
Michał Mirosławb2a103e2011-05-07 03:22:17 +00002204
2205 bond_compute_features(bond);
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 return 0;
2208}
2209
2210/*
2211 * This function changes the active slave to slave <slave_dev>.
2212 * It returns -EINVAL in the following cases.
2213 * - <slave_dev> is not found in the list.
2214 * - There is not active slave now.
2215 * - <slave_dev> is already active.
2216 * - The link state of <slave_dev> is not BOND_LINK_UP.
2217 * - <slave_dev> is not running.
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002218 * In these cases, this function does nothing.
2219 * In the other cases, current_slave pointer is changed and 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 */
2221static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
2222{
Wang Chen454d7c92008-11-12 23:37:49 -08002223 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 struct slave *old_active = NULL;
2225 struct slave *new_active = NULL;
2226 int res = 0;
2227
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002228 if (!USES_PRIMARY(bond->params.mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 /* Verify that master_dev is indeed the master of slave_dev */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002232 if (!(slave_dev->flags & IFF_SLAVE) || (slave_dev->master != bond_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002235 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002237 read_lock(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 old_active = bond->curr_active_slave;
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002239 read_unlock(&bond->curr_slave_lock);
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 new_active = bond_get_slave_by_dev(bond, slave_dev);
2242
2243 /*
2244 * Changing to the current active: do nothing; return success.
2245 */
2246 if (new_active && (new_active == old_active)) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002247 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 return 0;
2249 }
2250
2251 if ((new_active) &&
2252 (old_active) &&
2253 (new_active->link == BOND_LINK_UP) &&
2254 IS_UP(new_active->dev)) {
Neil Hormane843fa52010-10-13 16:01:50 +00002255 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002256 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 bond_change_active_slave(bond, new_active);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002258 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002259 unblock_netpoll_tx();
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002260 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002263 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265 return res;
2266}
2267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
2269{
Wang Chen454d7c92008-11-12 23:37:49 -08002270 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272 info->bond_mode = bond->params.mode;
2273 info->miimon = bond->params.miimon;
2274
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002275 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 info->num_slaves = bond->slave_cnt;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002277 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 return 0;
2280}
2281
2282static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2283{
Wang Chen454d7c92008-11-12 23:37:49 -08002284 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 struct slave *slave;
Eric Dumazet689c96c2009-04-23 03:39:04 +00002286 int i, res = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002288 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
2290 bond_for_each_slave(bond, slave, i) {
2291 if (i == (int)info->slave_id) {
Eric Dumazet689c96c2009-04-23 03:39:04 +00002292 res = 0;
2293 strcpy(info->slave_name, slave->dev->name);
2294 info->link = slave->link;
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002295 info->state = bond_slave_state(slave);
Eric Dumazet689c96c2009-04-23 03:39:04 +00002296 info->link_failure_count = slave->link_failure_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 break;
2298 }
2299 }
2300
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07002301 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Eric Dumazet689c96c2009-04-23 03:39:04 +00002303 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
2306/*-------------------------------- Monitoring -------------------------------*/
2307
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002308
2309static int bond_miimon_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002311 struct slave *slave;
2312 int i, link_state, commit = 0;
Jiri Pirko41f89102009-04-24 03:57:29 +00002313 bool ignore_updelay;
2314
2315 ignore_updelay = !bond->curr_active_slave ? true : false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 bond_for_each_slave(bond, slave, i) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002318 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002320 link_state = bond_check_dev_link(bond, slave->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 switch (slave->link) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002323 case BOND_LINK_UP:
2324 if (link_state)
2325 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002327 slave->link = BOND_LINK_FAIL;
2328 slave->delay = bond->params.downdelay;
2329 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002330 pr_info("%s: link status down for %sinterface %s, disabling it in %d ms.\n",
2331 bond->dev->name,
2332 (bond->params.mode ==
2333 BOND_MODE_ACTIVEBACKUP) ?
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002334 (bond_is_active_slave(slave) ?
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002335 "active " : "backup ") : "",
2336 slave->dev->name,
2337 bond->params.downdelay * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002339 /*FALLTHRU*/
2340 case BOND_LINK_FAIL:
2341 if (link_state) {
2342 /*
2343 * recovered before downdelay expired
2344 */
2345 slave->link = BOND_LINK_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 slave->jiffies = jiffies;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002347 pr_info("%s: link status up again after %d ms for interface %s.\n",
2348 bond->dev->name,
2349 (bond->params.downdelay - slave->delay) *
2350 bond->params.miimon,
2351 slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002352 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002354
2355 if (slave->delay <= 0) {
2356 slave->new_link = BOND_LINK_DOWN;
2357 commit++;
2358 continue;
2359 }
2360
2361 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002364 case BOND_LINK_DOWN:
2365 if (!link_state)
2366 continue;
2367
2368 slave->link = BOND_LINK_BACK;
2369 slave->delay = bond->params.updelay;
2370
2371 if (slave->delay) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002372 pr_info("%s: link status up for interface %s, enabling it in %d ms.\n",
2373 bond->dev->name, slave->dev->name,
2374 ignore_updelay ? 0 :
2375 bond->params.updelay *
2376 bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002378 /*FALLTHRU*/
2379 case BOND_LINK_BACK:
2380 if (!link_state) {
2381 slave->link = BOND_LINK_DOWN;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002382 pr_info("%s: link status down again after %d ms for interface %s.\n",
2383 bond->dev->name,
2384 (bond->params.updelay - slave->delay) *
2385 bond->params.miimon,
2386 slave->dev->name);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002387
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002388 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002390
Jiri Pirko41f89102009-04-24 03:57:29 +00002391 if (ignore_updelay)
2392 slave->delay = 0;
2393
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002394 if (slave->delay <= 0) {
2395 slave->new_link = BOND_LINK_UP;
2396 commit++;
Jiri Pirko41f89102009-04-24 03:57:29 +00002397 ignore_updelay = false;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002398 continue;
2399 }
2400
2401 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 break;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002403 }
2404 }
2405
2406 return commit;
2407}
2408
2409static void bond_miimon_commit(struct bonding *bond)
2410{
2411 struct slave *slave;
2412 int i;
2413
2414 bond_for_each_slave(bond, slave, i) {
2415 switch (slave->new_link) {
2416 case BOND_LINK_NOCHANGE:
2417 continue;
2418
2419 case BOND_LINK_UP:
2420 slave->link = BOND_LINK_UP;
2421 slave->jiffies = jiffies;
2422
2423 if (bond->params.mode == BOND_MODE_8023AD) {
2424 /* prevent it from being the active one */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002425 bond_set_backup_slave(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002426 } else if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
2427 /* make it immediately active */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002428 bond_set_active_slave(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002429 } else if (slave != bond->primary_slave) {
2430 /* prevent it from being the active one */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002431 bond_set_backup_slave(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002432 }
2433
Krzysztof Piotr Oledzki546add72010-10-06 14:28:22 -07002434 bond_update_speed_duplex(slave);
2435
David Decotigny5d305302011-04-13 15:22:31 +00002436 pr_info("%s: link status definitely up for interface %s, %u Mbps %s duplex.\n",
Krzysztof Piotr Oledzki700c2a72010-10-06 14:25:06 -07002437 bond->dev->name, slave->dev->name,
2438 slave->speed, slave->duplex ? "full" : "half");
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002439
2440 /* notify ad that the link status has changed */
2441 if (bond->params.mode == BOND_MODE_8023AD)
2442 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2443
Holger Eitzenberger58402052008-12-09 23:07:13 -08002444 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002445 bond_alb_handle_link_change(bond, slave,
2446 BOND_LINK_UP);
2447
2448 if (!bond->curr_active_slave ||
2449 (slave == bond->primary_slave))
2450 goto do_failover;
2451
2452 continue;
2453
2454 case BOND_LINK_DOWN:
Jay Vosburghfba4acd2008-10-30 17:41:14 -07002455 if (slave->link_failure_count < UINT_MAX)
2456 slave->link_failure_count++;
2457
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002458 slave->link = BOND_LINK_DOWN;
2459
2460 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP ||
2461 bond->params.mode == BOND_MODE_8023AD)
2462 bond_set_slave_inactive_flags(slave);
2463
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002464 pr_info("%s: link status definitely down for interface %s, disabling it\n",
2465 bond->dev->name, slave->dev->name);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002466
2467 if (bond->params.mode == BOND_MODE_8023AD)
2468 bond_3ad_handle_link_change(slave,
2469 BOND_LINK_DOWN);
2470
Jiri Pirkoae63e802009-05-27 05:42:36 +00002471 if (bond_is_lb(bond))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002472 bond_alb_handle_link_change(bond, slave,
2473 BOND_LINK_DOWN);
2474
2475 if (slave == bond->curr_active_slave)
2476 goto do_failover;
2477
2478 continue;
2479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002481 pr_err("%s: invalid new link %d on slave %s\n",
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002482 bond->dev->name, slave->new_link,
2483 slave->dev->name);
2484 slave->new_link = BOND_LINK_NOCHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002486 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
2488
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002489do_failover:
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002490 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00002491 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002492 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 bond_select_active_slave(bond);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002494 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002495 unblock_netpoll_tx();
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002496 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002497
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002498 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499}
2500
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002501/*
2502 * bond_mii_monitor
2503 *
2504 * Really a wrapper that splits the mii monitor into two phases: an
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002505 * inspection, then (if inspection indicates something needs to be done)
2506 * an acquisition of appropriate locks followed by a commit phase to
2507 * implement whatever link state changes are indicated.
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002508 */
2509void bond_mii_monitor(struct work_struct *work)
2510{
2511 struct bonding *bond = container_of(work, struct bonding,
2512 mii_work.work);
Ben Hutchingsad246c92011-04-26 15:25:52 +00002513 bool should_notify_peers = false;
Jay Vosburghe6d265e2011-10-28 15:42:50 +00002514 unsigned long delay;
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002515
2516 read_lock(&bond->lock);
Jay Vosburghe6d265e2011-10-28 15:42:50 +00002517
2518 delay = msecs_to_jiffies(bond->params.miimon);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002519
2520 if (bond->slave_cnt == 0)
2521 goto re_arm;
Jay Vosburghb59f9f72008-06-13 18:12:03 -07002522
Ben Hutchingsad246c92011-04-26 15:25:52 +00002523 should_notify_peers = bond_should_notify_peers(bond);
2524
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002525 if (bond_miimon_inspect(bond)) {
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002526 read_unlock(&bond->lock);
Jay Vosburghe6d265e2011-10-28 15:42:50 +00002527
2528 /* Race avoidance with bond_close cancel of workqueue */
2529 if (!rtnl_trylock()) {
2530 read_lock(&bond->lock);
2531 delay = 1;
2532 should_notify_peers = false;
2533 goto re_arm;
2534 }
2535
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002536 read_lock(&bond->lock);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002537
2538 bond_miimon_commit(bond);
2539
Jay Vosburgh56556622008-01-17 16:25:03 -08002540 read_unlock(&bond->lock);
2541 rtnl_unlock(); /* might sleep, hold no other locks */
2542 read_lock(&bond->lock);
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002543 }
2544
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002545re_arm:
Jay Vosburghe6d265e2011-10-28 15:42:50 +00002546 if (bond->params.miimon)
2547 queue_delayed_work(bond->wq, &bond->mii_work, delay);
2548
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002549 read_unlock(&bond->lock);
Ben Hutchingsad246c92011-04-26 15:25:52 +00002550
2551 if (should_notify_peers) {
Jay Vosburghe6d265e2011-10-28 15:42:50 +00002552 if (!rtnl_trylock()) {
2553 read_lock(&bond->lock);
2554 bond->send_peer_notif++;
2555 read_unlock(&bond->lock);
2556 return;
2557 }
Ben Hutchingsad246c92011-04-26 15:25:52 +00002558 netdev_bonding_change(bond->dev, NETDEV_NOTIFY_PEERS);
2559 rtnl_unlock();
2560 }
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002561}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002562
Al Virod3bb52b2007-08-22 20:06:58 -04002563static int bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002564{
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002565 struct vlan_entry *vlan;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002566
2567 if (ip == bond->master_ip)
2568 return 1;
2569
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002570 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002571 if (ip == vlan->vlan_ip)
2572 return 1;
2573 }
2574
2575 return 0;
2576}
2577
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002578/*
2579 * We go to the (large) trouble of VLAN tagging ARP frames because
2580 * switches in VLAN mode (especially if ports are configured as
2581 * "native" to a VLAN) might not pass non-tagged frames.
2582 */
Al Virod3bb52b2007-08-22 20:06:58 -04002583static 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 -04002584{
2585 struct sk_buff *skb;
2586
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002587 pr_debug("arp %d on slave %s: dst %x src %x vid %d\n", arp_op,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002588 slave_dev->name, dest_ip, src_ip, vlan_id);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002589
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002590 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2591 NULL, slave_dev->dev_addr, NULL);
2592
2593 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002594 pr_err("ARP packet allocation failed\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002595 return;
2596 }
2597 if (vlan_id) {
2598 skb = vlan_put_tag(skb, vlan_id);
2599 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002600 pr_err("failed to insert VLAN tag\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002601 return;
2602 }
2603 }
2604 arp_xmit(skb);
2605}
2606
2607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2609{
David S. Millerb23dd4f2011-03-02 14:31:35 -08002610 int i, vlan_id;
Al Virod3bb52b2007-08-22 20:06:58 -04002611 __be32 *targets = bond->params.arp_targets;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002612 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002613 struct net_device *vlan_dev;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002614 struct rtable *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Mitch Williams6b780562005-11-09 10:36:19 -08002616 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
2617 if (!targets[i])
Brian Haley5a31bec2009-04-13 00:11:30 -07002618 break;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002619 pr_debug("basa: target %x\n", targets[i]);
Jiri Pirkocc0e4072011-07-20 04:54:46 +00002620 if (!bond_vlan_used(bond)) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002621 pr_debug("basa: empty vlan: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002622 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2623 bond->master_ip, 0);
2624 continue;
2625 }
2626
2627 /*
2628 * If VLANs are configured, we do a route lookup to
2629 * determine which VLAN interface would be used, so we
2630 * can tag the ARP with the proper VLAN tag.
2631 */
David S. Miller78fbfd82011-03-12 00:00:52 -05002632 rt = ip_route_output(dev_net(bond->dev), targets[i], 0,
2633 RTO_ONLINK, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08002634 if (IS_ERR(rt)) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002635 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002636 pr_warning("%s: no route to arp_ip_target %pI4\n",
David S. Miller78fbfd82011-03-12 00:00:52 -05002637 bond->dev->name, &targets[i]);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002638 }
2639 continue;
2640 }
2641
2642 /*
2643 * This target is not on a VLAN
2644 */
Changli Gaod8d1f302010-06-10 23:31:35 -07002645 if (rt->dst.dev == bond->dev) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002646 ip_rt_put(rt);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002647 pr_debug("basa: rtdev == bond->dev: arp_send\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002648 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2649 bond->master_ip, 0);
2650 continue;
2651 }
2652
2653 vlan_id = 0;
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07002654 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jiri Pirkocc0e4072011-07-20 04:54:46 +00002655 rcu_read_lock();
2656 vlan_dev = __vlan_find_dev_deep(bond->dev,
2657 vlan->vlan_id);
2658 rcu_read_unlock();
Changli Gaod8d1f302010-06-10 23:31:35 -07002659 if (vlan_dev == rt->dst.dev) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002660 vlan_id = vlan->vlan_id;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002661 pr_debug("basa: vlan match on %s %d\n",
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002662 vlan_dev->name, vlan_id);
2663 break;
2664 }
2665 }
2666
2667 if (vlan_id) {
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002668 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002669 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i],
2670 vlan->vlan_ip, vlan_id);
2671 continue;
2672 }
2673
2674 if (net_ratelimit()) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002675 pr_warning("%s: no path to arp_ip_target %pI4 via rt.dev %s\n",
David S. Miller78fbfd82011-03-12 00:00:52 -05002676 bond->dev->name, &targets[i],
Changli Gaod8d1f302010-06-10 23:31:35 -07002677 rt->dst.dev ? rt->dst.dev->name : "NULL");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002678 }
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002679 ip_rt_put(rt);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002680 }
2681}
2682
Al Virod3bb52b2007-08-22 20:06:58 -04002683static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002684{
2685 int i;
Al Virod3bb52b2007-08-22 20:06:58 -04002686 __be32 *targets = bond->params.arp_targets;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002687
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002688 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && targets[i]; i++) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002689 pr_debug("bva: sip %pI4 tip %pI4 t[%d] %pI4 bhti(tip) %d\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002690 &sip, &tip, i, &targets[i],
2691 bond_has_this_ip(bond, tip));
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002692 if (sip == targets[i]) {
2693 if (bond_has_this_ip(bond, tip))
2694 slave->last_arp_rx = jiffies;
2695 return;
2696 }
2697 }
2698}
2699
Jiri Pirko3aba8912011-04-19 03:48:16 +00002700static void bond_arp_rcv(struct sk_buff *skb, struct bonding *bond,
2701 struct slave *slave)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002702{
2703 struct arphdr *arp;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002704 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002705 __be32 sip, tip;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002706
Jiri Pirko3aba8912011-04-19 03:48:16 +00002707 if (skb->protocol != __cpu_to_be16(ETH_P_ARP))
2708 return;
Andy Gospodarek1f3c8802009-12-14 10:48:58 +00002709
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002710 read_lock(&bond->lock);
2711
Jiri Pirko3aba8912011-04-19 03:48:16 +00002712 pr_debug("bond_arp_rcv: bond %s skb->dev %s\n",
2713 bond->dev->name, skb->dev->name);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002714
Jiri Pirko3aba8912011-04-19 03:48:16 +00002715 if (!pskb_may_pull(skb, arp_hdr_len(bond->dev)))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002716 goto out_unlock;
2717
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -03002718 arp = arp_hdr(skb);
Jiri Pirko3aba8912011-04-19 03:48:16 +00002719 if (arp->ar_hln != bond->dev->addr_len ||
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002720 skb->pkt_type == PACKET_OTHERHOST ||
2721 skb->pkt_type == PACKET_LOOPBACK ||
2722 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2723 arp->ar_pro != htons(ETH_P_IP) ||
2724 arp->ar_pln != 4)
2725 goto out_unlock;
2726
2727 arp_ptr = (unsigned char *)(arp + 1);
Jiri Pirko3aba8912011-04-19 03:48:16 +00002728 arp_ptr += bond->dev->addr_len;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002729 memcpy(&sip, arp_ptr, 4);
Jiri Pirko3aba8912011-04-19 03:48:16 +00002730 arp_ptr += 4 + bond->dev->addr_len;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002731 memcpy(&tip, arp_ptr, 4);
2732
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08002733 pr_debug("bond_arp_rcv: %s %s/%d av %d sv %d sip %pI4 tip %pI4\n",
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002734 bond->dev->name, slave->dev->name, bond_slave_state(slave),
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002735 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2736 &sip, &tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002737
2738 /*
2739 * Backup slaves won't see the ARP reply, but do come through
2740 * here for each ARP probe (so we swap the sip/tip to validate
2741 * the probe). In a "redundant switch, common router" type of
2742 * configuration, the ARP probe will (hopefully) travel from
2743 * the active, through one switch, the router, then the other
2744 * switch before reaching the backup.
2745 */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002746 if (bond_is_active_slave(slave))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002747 bond_validate_arp(bond, slave, sip, tip);
2748 else
2749 bond_validate_arp(bond, slave, tip, sip);
2750
2751out_unlock:
2752 read_unlock(&bond->lock);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002753}
2754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755/*
2756 * this function is called regularly to monitor each slave's link
2757 * ensuring that traffic is being sent and received when arp monitoring
2758 * is used in load-balancing mode. if the adapter has been dormant, then an
2759 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2760 * arp monitoring in active backup mode.
2761 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002762void bond_loadbalance_arp_mon(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002764 struct bonding *bond = container_of(work, struct bonding,
2765 arp_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 struct slave *slave, *oldcurrent;
2767 int do_failover = 0;
2768 int delta_in_ticks;
2769 int i;
2770
2771 read_lock(&bond->lock);
2772
Jay Vosburgh5ce0da82008-05-17 21:10:07 -07002773 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002775 if (bond->slave_cnt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
2778 read_lock(&bond->curr_slave_lock);
2779 oldcurrent = bond->curr_active_slave;
2780 read_unlock(&bond->curr_slave_lock);
2781
2782 /* see if any of the previous devices are up now (i.e. they have
2783 * xmt and rcv traffic). the curr_active_slave does not come into
2784 * the picture unless it is null. also, slave->jiffies is not needed
2785 * here because we send an arp on each slave and give a slave as
2786 * long as it needs to get the tx/rx within the delta.
2787 * TODO: what about up/down delay in arp mode? it wasn't here before
2788 * so it can wait
2789 */
2790 bond_for_each_slave(bond, slave, i) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002791 unsigned long trans_start = dev_trans_start(slave->dev);
2792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002794 if (time_in_range(jiffies,
2795 trans_start - delta_in_ticks,
2796 trans_start + delta_in_ticks) &&
2797 time_in_range(jiffies,
2798 slave->dev->last_rx - delta_in_ticks,
2799 slave->dev->last_rx + delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
2801 slave->link = BOND_LINK_UP;
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002802 bond_set_active_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
2804 /* primary_slave has no meaning in round-robin
2805 * mode. the window of a slave being up and
2806 * curr_active_slave being null after enslaving
2807 * is closed.
2808 */
2809 if (!oldcurrent) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002810 pr_info("%s: link status definitely up for interface %s, ",
2811 bond->dev->name,
2812 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 do_failover = 1;
2814 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002815 pr_info("%s: interface %s is now up\n",
2816 bond->dev->name,
2817 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 }
2819 }
2820 } else {
2821 /* slave->link == BOND_LINK_UP */
2822
2823 /* not all switches will respond to an arp request
2824 * when the source ip is 0, so don't take the link down
2825 * if we don't know our ip yet
2826 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002827 if (!time_in_range(jiffies,
2828 trans_start - delta_in_ticks,
2829 trans_start + 2 * delta_in_ticks) ||
2830 !time_in_range(jiffies,
2831 slave->dev->last_rx - delta_in_ticks,
2832 slave->dev->last_rx + 2 * delta_in_ticks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
2834 slave->link = BOND_LINK_DOWN;
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002835 bond_set_backup_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002837 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002840 pr_info("%s: interface %s is now down.\n",
2841 bond->dev->name,
2842 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002844 if (slave == oldcurrent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 do_failover = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 }
2847 }
2848
2849 /* note: if switch is in round-robin mode, all links
2850 * must tx arp to ensure all links rx an arp - otherwise
2851 * links may oscillate or not come up at all; if switch is
2852 * in something like xor mode, there is nothing we can
2853 * do - all replies will be rx'ed on same link causing slaves
2854 * to be unstable during low/no traffic periods
2855 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002856 if (IS_UP(slave->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 bond_arp_send_all(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 }
2859
2860 if (do_failover) {
Neil Hormane843fa52010-10-13 16:01:50 +00002861 block_netpoll_tx();
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002862 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
2864 bond_select_active_slave(bond);
2865
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002866 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00002867 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 }
2869
2870re_arm:
Jay Vosburghe6d265e2011-10-28 15:42:50 +00002871 if (bond->params.arp_interval)
Jay Vosburgh1b76b312007-10-17 17:37:45 -07002872 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Jay Vosburghe6d265e2011-10-28 15:42:50 +00002873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 read_unlock(&bond->lock);
2875}
2876
2877/*
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002878 * Called to inspect slaves for active-backup mode ARP monitor link state
2879 * changes. Sets new_link in slaves to specify what action should take
2880 * place for the slave. Returns 0 if no changes are found, >0 if changes
2881 * to link states must be committed.
2882 *
2883 * Called with bond->lock held for read.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002885static int bond_ab_arp_inspect(struct bonding *bond, int delta_in_ticks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 struct slave *slave;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002888 int i, commit = 0;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002889 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 bond_for_each_slave(bond, slave, i) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002892 slave->new_link = BOND_LINK_NOCHANGE;
2893
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 if (slave->link != BOND_LINK_UP) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002895 if (time_in_range(jiffies,
2896 slave_last_rx(bond, slave) - delta_in_ticks,
2897 slave_last_rx(bond, slave) + delta_in_ticks)) {
2898
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002899 slave->new_link = BOND_LINK_UP;
2900 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002903 continue;
2904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002906 /*
2907 * Give slaves 2*delta after being enslaved or made
2908 * active. This avoids bouncing, as the last receive
2909 * times need a full ARP monitor cycle to be updated.
2910 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002911 if (time_in_range(jiffies,
2912 slave->jiffies - delta_in_ticks,
2913 slave->jiffies + 2 * delta_in_ticks))
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002914 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002916 /*
2917 * Backup slave is down if:
2918 * - No current_arp_slave AND
2919 * - more than 3*delta since last receive AND
2920 * - the bond has an IP address
2921 *
2922 * Note: a non-null current_arp_slave indicates
2923 * the curr_active_slave went down and we are
2924 * searching for a new one; under this condition
2925 * we only take the curr_active_slave down - this
2926 * gives each slave a chance to tx/rx traffic
2927 * before being taken out
2928 */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002929 if (!bond_is_active_slave(slave) &&
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002930 !bond->current_arp_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002931 !time_in_range(jiffies,
2932 slave_last_rx(bond, slave) - delta_in_ticks,
2933 slave_last_rx(bond, slave) + 3 * delta_in_ticks)) {
2934
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002935 slave->new_link = BOND_LINK_DOWN;
2936 commit++;
2937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002939 /*
2940 * Active slave is down if:
2941 * - more than 2*delta since transmitting OR
2942 * - (more than 2*delta since receive AND
2943 * the bond has an IP address)
2944 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002945 trans_start = dev_trans_start(slave->dev);
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002946 if (bond_is_active_slave(slave) &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002947 (!time_in_range(jiffies,
2948 trans_start - delta_in_ticks,
2949 trans_start + 2 * delta_in_ticks) ||
2950 !time_in_range(jiffies,
2951 slave_last_rx(bond, slave) - delta_in_ticks,
2952 slave_last_rx(bond, slave) + 2 * delta_in_ticks))) {
2953
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002954 slave->new_link = BOND_LINK_DOWN;
2955 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 }
2957 }
2958
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002959 return commit;
2960}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002962/*
2963 * Called to commit link state changes noted by inspection step of
2964 * active-backup mode ARP monitor.
2965 *
2966 * Called with RTNL and bond->lock for read.
2967 */
2968static void bond_ab_arp_commit(struct bonding *bond, int delta_in_ticks)
2969{
2970 struct slave *slave;
2971 int i;
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002972 unsigned long trans_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002974 bond_for_each_slave(bond, slave, i) {
2975 switch (slave->new_link) {
2976 case BOND_LINK_NOCHANGE:
2977 continue;
2978
2979 case BOND_LINK_UP:
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002980 trans_start = dev_trans_start(slave->dev);
Jiri Pirkob9f60252009-08-31 11:09:38 +00002981 if ((!bond->curr_active_slave &&
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002982 time_in_range(jiffies,
2983 trans_start - delta_in_ticks,
2984 trans_start + delta_in_ticks)) ||
Jiri Pirkob9f60252009-08-31 11:09:38 +00002985 bond->curr_active_slave != slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002986 slave->link = BOND_LINK_UP;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002987 bond->current_arp_slave = NULL;
2988
Joe Perchesa4aee5c2009-12-13 20:06:07 -08002989 pr_info("%s: link status definitely up for interface %s.\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00002990 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002991
Jiri Pirkob9f60252009-08-31 11:09:38 +00002992 if (!bond->curr_active_slave ||
2993 (slave == bond->primary_slave))
2994 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002995
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002996 }
2997
Jiri Pirkob9f60252009-08-31 11:09:38 +00002998 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002999
3000 case BOND_LINK_DOWN:
3001 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003004 slave->link = BOND_LINK_DOWN;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003005 bond_set_slave_inactive_flags(slave);
3006
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003007 pr_info("%s: link status definitely down for interface %s, disabling it\n",
Jiri Pirkob9f60252009-08-31 11:09:38 +00003008 bond->dev->name, slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003009
3010 if (slave == bond->curr_active_slave) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003011 bond->current_arp_slave = NULL;
Jiri Pirkob9f60252009-08-31 11:09:38 +00003012 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003013 }
Jiri Pirkob9f60252009-08-31 11:09:38 +00003014
3015 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003016
3017 default:
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003018 pr_err("%s: impossible: new_link %d on slave %s\n",
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003019 bond->dev->name, slave->new_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 slave->dev->name);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003021 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Jiri Pirkob9f60252009-08-31 11:09:38 +00003024do_failover:
3025 ASSERT_RTNL();
Neil Hormane843fa52010-10-13 16:01:50 +00003026 block_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003027 write_lock_bh(&bond->curr_slave_lock);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003028 bond_select_active_slave(bond);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003029 write_unlock_bh(&bond->curr_slave_lock);
Neil Hormane843fa52010-10-13 16:01:50 +00003030 unblock_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003031 }
3032
3033 bond_set_carrier(bond);
3034}
3035
3036/*
3037 * Send ARP probes for active-backup mode ARP monitor.
3038 *
3039 * Called with bond->lock held for read.
3040 */
3041static void bond_ab_arp_probe(struct bonding *bond)
3042{
3043 struct slave *slave;
3044 int i;
3045
3046 read_lock(&bond->curr_slave_lock);
3047
3048 if (bond->current_arp_slave && bond->curr_active_slave)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003049 pr_info("PROBE: c_arp %s && cas %s BAD\n",
3050 bond->current_arp_slave->dev->name,
3051 bond->curr_active_slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003052
3053 if (bond->curr_active_slave) {
3054 bond_arp_send_all(bond, bond->curr_active_slave);
3055 read_unlock(&bond->curr_slave_lock);
3056 return;
3057 }
3058
3059 read_unlock(&bond->curr_slave_lock);
3060
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 /* if we don't have a curr_active_slave, search for the next available
3062 * backup slave from the current_arp_slave and make it the candidate
3063 * for becoming the curr_active_slave
3064 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003065
3066 if (!bond->current_arp_slave) {
3067 bond->current_arp_slave = bond->first_slave;
3068 if (!bond->current_arp_slave)
3069 return;
3070 }
3071
3072 bond_set_slave_inactive_flags(bond->current_arp_slave);
3073
3074 /* search for next candidate */
3075 bond_for_each_slave_from(bond, slave, i, bond->current_arp_slave->next) {
3076 if (IS_UP(slave->dev)) {
3077 slave->link = BOND_LINK_BACK;
3078 bond_set_slave_active_flags(slave);
3079 bond_arp_send_all(bond, slave);
3080 slave->jiffies = jiffies;
3081 bond->current_arp_slave = slave;
3082 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 }
3084
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003085 /* if the link state is up at this point, we
3086 * mark it down - this can happen if we have
3087 * simultaneous link failures and
3088 * reselect_active_interface doesn't make this
3089 * one the current slave so it is still marked
3090 * up when it is actually down
3091 */
3092 if (slave->link == BOND_LINK_UP) {
3093 slave->link = BOND_LINK_DOWN;
3094 if (slave->link_failure_count < UINT_MAX)
3095 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003097 bond_set_slave_inactive_flags(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003099 pr_info("%s: backup interface %s is now down.\n",
3100 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 }
3102 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003103}
3104
3105void bond_activebackup_arp_mon(struct work_struct *work)
3106{
3107 struct bonding *bond = container_of(work, struct bonding,
3108 arp_work.work);
Ben Hutchingsad246c92011-04-26 15:25:52 +00003109 bool should_notify_peers = false;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003110 int delta_in_ticks;
3111
3112 read_lock(&bond->lock);
3113
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003114 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3115
3116 if (bond->slave_cnt == 0)
3117 goto re_arm;
3118
Ben Hutchingsad246c92011-04-26 15:25:52 +00003119 should_notify_peers = bond_should_notify_peers(bond);
3120
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003121 if (bond_ab_arp_inspect(bond, delta_in_ticks)) {
3122 read_unlock(&bond->lock);
Jay Vosburghe6d265e2011-10-28 15:42:50 +00003123
3124 /* Race avoidance with bond_close flush of workqueue */
3125 if (!rtnl_trylock()) {
3126 read_lock(&bond->lock);
3127 delta_in_ticks = 1;
3128 should_notify_peers = false;
3129 goto re_arm;
3130 }
3131
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003132 read_lock(&bond->lock);
3133
3134 bond_ab_arp_commit(bond, delta_in_ticks);
3135
3136 read_unlock(&bond->lock);
3137 rtnl_unlock();
3138 read_lock(&bond->lock);
3139 }
3140
3141 bond_ab_arp_probe(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
3143re_arm:
Jay Vosburghe6d265e2011-10-28 15:42:50 +00003144 if (bond->params.arp_interval)
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003145 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
Jay Vosburghe6d265e2011-10-28 15:42:50 +00003146
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 read_unlock(&bond->lock);
Ben Hutchingsad246c92011-04-26 15:25:52 +00003148
3149 if (should_notify_peers) {
Jay Vosburghe6d265e2011-10-28 15:42:50 +00003150 if (!rtnl_trylock()) {
3151 read_lock(&bond->lock);
3152 bond->send_peer_notif++;
3153 read_unlock(&bond->lock);
3154 return;
3155 }
Ben Hutchingsad246c92011-04-26 15:25:52 +00003156 netdev_bonding_change(bond->dev, NETDEV_NOTIFY_PEERS);
3157 rtnl_unlock();
3158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159}
3160
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161/*-------------------------- netdev event handling --------------------------*/
3162
3163/*
3164 * Change device name
3165 */
3166static int bond_event_changename(struct bonding *bond)
3167{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 bond_remove_proc_entry(bond);
3169 bond_create_proc_entry(bond);
Stephen Hemminger7e083842009-06-12 19:02:46 +00003170
Taku Izumif073c7c2010-12-09 15:17:13 +00003171 bond_debug_reregister(bond);
3172
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 return NOTIFY_DONE;
3174}
3175
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003176static int bond_master_netdev_event(unsigned long event,
3177 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178{
Wang Chen454d7c92008-11-12 23:37:49 -08003179 struct bonding *event_bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
3181 switch (event) {
3182 case NETDEV_CHANGENAME:
3183 return bond_event_changename(event_bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 default:
3185 break;
3186 }
3187
3188 return NOTIFY_DONE;
3189}
3190
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003191static int bond_slave_netdev_event(unsigned long event,
3192 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
3194 struct net_device *bond_dev = slave_dev->master;
Wang Chen454d7c92008-11-12 23:37:49 -08003195 struct bonding *bond = netdev_priv(bond_dev);
Weiping Pan98f41f62011-10-31 17:20:48 +00003196 struct slave *slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
3198 switch (event) {
3199 case NETDEV_UNREGISTER:
3200 if (bond_dev) {
Jay Vosburgh1284cd32007-10-15 16:44:27 -07003201 if (bond->setup_by_slave)
3202 bond_release_and_destroy(bond_dev, slave_dev);
3203 else
3204 bond_release(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 }
3206 break;
Weiping Pan98f41f62011-10-31 17:20:48 +00003207 case NETDEV_UP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 case NETDEV_CHANGE:
Weiping Pan98f41f62011-10-31 17:20:48 +00003209 slave = bond_get_slave_by_dev(bond, slave_dev);
3210 if (slave) {
3211 u32 old_speed = slave->speed;
3212 u8 old_duplex = slave->duplex;
Jay Vosburgh17d04502009-03-18 18:38:25 -07003213
Weiping Pan98f41f62011-10-31 17:20:48 +00003214 bond_update_speed_duplex(slave);
Jay Vosburgh17d04502009-03-18 18:38:25 -07003215
Weiping Pan98f41f62011-10-31 17:20:48 +00003216 if (bond->params.mode == BOND_MODE_8023AD) {
Jay Vosburgh17d04502009-03-18 18:38:25 -07003217 if (old_speed != slave->speed)
3218 bond_3ad_adapter_speed_changed(slave);
3219 if (old_duplex != slave->duplex)
3220 bond_3ad_adapter_duplex_changed(slave);
3221 }
3222 }
3223
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 break;
3225 case NETDEV_DOWN:
3226 /*
3227 * ... Or is it this?
3228 */
3229 break;
3230 case NETDEV_CHANGEMTU:
3231 /*
3232 * TODO: Should slaves be allowed to
3233 * independently alter their MTU? For
3234 * an active-backup bond, slaves need
3235 * not be the same type of device, so
3236 * MTUs may vary. For other modes,
3237 * slaves arguably should have the
3238 * same MTUs. To do this, we'd need to
3239 * take over the slave's change_mtu
3240 * function for the duration of their
3241 * servitude.
3242 */
3243 break;
3244 case NETDEV_CHANGENAME:
3245 /*
3246 * TODO: handle changing the primary's name
3247 */
3248 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003249 case NETDEV_FEAT_CHANGE:
3250 bond_compute_features(bond);
3251 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 default:
3253 break;
3254 }
3255
3256 return NOTIFY_DONE;
3257}
3258
3259/*
3260 * bond_netdev_event: handle netdev notifier chain events.
3261 *
3262 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003263 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 * locks for us to safely manipulate the slave devices (RTNL lock,
3265 * dev_probe_lock).
3266 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003267static int bond_netdev_event(struct notifier_block *this,
3268 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
3270 struct net_device *event_dev = (struct net_device *)ptr;
3271
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003272 pr_debug("event_dev: %s, event: %lx\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003273 event_dev ? event_dev->name : "None",
3274 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003276 if (!(event_dev->priv_flags & IFF_BONDING))
3277 return NOTIFY_DONE;
3278
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 if (event_dev->flags & IFF_MASTER) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003280 pr_debug("IFF_MASTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 return bond_master_netdev_event(event, event_dev);
3282 }
3283
3284 if (event_dev->flags & IFF_SLAVE) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003285 pr_debug("IFF_SLAVE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 return bond_slave_netdev_event(event, event_dev);
3287 }
3288
3289 return NOTIFY_DONE;
3290}
3291
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003292/*
3293 * bond_inetaddr_event: handle inetaddr notifier chain events.
3294 *
3295 * We keep track of device IPs primarily to use as source addresses in
3296 * ARP monitor probes (rather than spewing out broadcasts all the time).
3297 *
3298 * We track one IP for the main device (if it has one), plus one per VLAN.
3299 */
3300static int bond_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
3301{
3302 struct in_ifaddr *ifa = ptr;
3303 struct net_device *vlan_dev, *event_dev = ifa->ifa_dev->dev;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003304 struct bond_net *bn = net_generic(dev_net(event_dev), bond_net_id);
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003305 struct bonding *bond;
3306 struct vlan_entry *vlan;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003307
Henrik Saavedra Persson917fbdb2011-11-23 23:37:15 +00003308 /* we only care about primary address */
3309 if(ifa->ifa_flags & IFA_F_SECONDARY)
3310 return NOTIFY_DONE;
3311
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003312 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003313 if (bond->dev == event_dev) {
3314 switch (event) {
3315 case NETDEV_UP:
3316 bond->master_ip = ifa->ifa_local;
3317 return NOTIFY_OK;
3318 case NETDEV_DOWN:
Henrik Saavedra Persson917fbdb2011-11-23 23:37:15 +00003319 bond->master_ip = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003320 return NOTIFY_OK;
3321 default:
3322 return NOTIFY_DONE;
3323 }
3324 }
3325
Pavel Emelyanov0883bec2008-05-17 21:10:10 -07003326 list_for_each_entry(vlan, &bond->vlan_list, vlan_list) {
Jiri Pirkocc0e4072011-07-20 04:54:46 +00003327 vlan_dev = __vlan_find_dev_deep(bond->dev,
3328 vlan->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003329 if (vlan_dev == event_dev) {
3330 switch (event) {
3331 case NETDEV_UP:
3332 vlan->vlan_ip = ifa->ifa_local;
3333 return NOTIFY_OK;
3334 case NETDEV_DOWN:
Henrik Saavedra Persson917fbdb2011-11-23 23:37:15 +00003335 vlan->vlan_ip = 0;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003336 return NOTIFY_OK;
3337 default:
3338 return NOTIFY_DONE;
3339 }
3340 }
3341 }
3342 }
3343 return NOTIFY_DONE;
3344}
3345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346static struct notifier_block bond_netdev_notifier = {
3347 .notifier_call = bond_netdev_event,
3348};
3349
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04003350static struct notifier_block bond_inetaddr_notifier = {
3351 .notifier_call = bond_inetaddr_event,
3352};
3353
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003354/*---------------------------- Hashing Policies -----------------------------*/
3355
3356/*
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003357 * Hash for the output device based upon layer 2 and layer 3 data. If
3358 * the packet is not IP mimic bond_xmit_hash_policy_l2()
3359 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003360static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003361{
3362 struct ethhdr *data = (struct ethhdr *)skb->data;
3363 struct iphdr *iph = ip_hdr(skb);
3364
Brian Haleyf14c4e42008-09-02 10:08:08 -04003365 if (skb->protocol == htons(ETH_P_IP)) {
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003366 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^
Jasper Spaansd3da6832009-10-23 04:08:46 +00003367 (data->h_dest[5] ^ data->h_source[5])) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003368 }
3369
Jasper Spaansd3da6832009-10-23 04:08:46 +00003370 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08003371}
3372
3373/*
Michael Opdenacker59c51592007-05-09 08:57:56 +02003374 * Hash for the output device based upon layer 3 and layer 4 data. If
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003375 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is
3376 * altogether not IP, mimic bond_xmit_hash_policy_l2()
3377 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003378static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003379{
3380 struct ethhdr *data = (struct ethhdr *)skb->data;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07003381 struct iphdr *iph = ip_hdr(skb);
Al Virod3bb52b2007-08-22 20:06:58 -04003382 __be16 *layer4hdr = (__be16 *)((u32 *)iph + iph->ihl);
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003383 int layer4_xor = 0;
3384
Brian Haleyf14c4e42008-09-02 10:08:08 -04003385 if (skb->protocol == htons(ETH_P_IP)) {
Paul Gortmaker56f8a752011-06-21 20:33:34 -07003386 if (!ip_is_fragment(iph) &&
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003387 (iph->protocol == IPPROTO_TCP ||
3388 iph->protocol == IPPROTO_UDP)) {
Al Virod3bb52b2007-08-22 20:06:58 -04003389 layer4_xor = ntohs((*layer4hdr ^ *(layer4hdr + 1)));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003390 }
3391 return (layer4_xor ^
3392 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count;
3393
3394 }
3395
Jasper Spaansd3da6832009-10-23 04:08:46 +00003396 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003397}
3398
3399/*
3400 * Hash for the output device based upon layer 2 data
3401 */
Jasper Spaansa361c832009-10-23 04:09:24 +00003402static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003403{
3404 struct ethhdr *data = (struct ethhdr *)skb->data;
3405
Jasper Spaansd3da6832009-10-23 04:08:46 +00003406 return (data->h_dest[5] ^ data->h_source[5]) % count;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003407}
3408
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409/*-------------------------- Device entry points ----------------------------*/
3410
3411static int bond_open(struct net_device *bond_dev)
3412{
Wang Chen454d7c92008-11-12 23:37:49 -08003413 struct bonding *bond = netdev_priv(bond_dev);
Peter Pan(潘卫平)ba3211c2011-08-15 15:57:35 +00003414 struct slave *slave;
3415 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
Peter Pan(潘卫平)ba3211c2011-08-15 15:57:35 +00003417 /* reset slave->backup and slave->inactive */
3418 read_lock(&bond->lock);
3419 if (bond->slave_cnt > 0) {
3420 read_lock(&bond->curr_slave_lock);
3421 bond_for_each_slave(bond, slave, i) {
3422 if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3423 && (slave != bond->curr_active_slave)) {
3424 bond_set_slave_inactive_flags(slave);
3425 } else {
3426 bond_set_slave_active_flags(slave);
3427 }
3428 }
3429 read_unlock(&bond->curr_slave_lock);
3430 }
3431 read_unlock(&bond->lock);
3432
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003433 INIT_DELAYED_WORK(&bond->mcast_work, bond_resend_igmp_join_requests_delayed);
3434
Holger Eitzenberger58402052008-12-09 23:07:13 -08003435 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 /* bond_alb_initialize must be called before the timer
3437 * is started.
3438 */
3439 if (bond_alb_initialize(bond, (bond->params.mode == BOND_MODE_ALB))) {
3440 /* something went wrong - fail the open operation */
stephen hemmingerb4739462010-01-25 23:34:15 +00003441 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 }
3443
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003444 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3445 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 }
3447
3448 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003449 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3450 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 }
3452
3453 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003454 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3455 INIT_DELAYED_WORK(&bond->arp_work,
3456 bond_activebackup_arp_mon);
3457 else
3458 INIT_DELAYED_WORK(&bond->arp_work,
3459 bond_loadbalance_arp_mon);
3460
3461 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003462 if (bond->params.arp_validate)
Jiri Pirko3aba8912011-04-19 03:48:16 +00003463 bond->recv_probe = bond_arp_rcv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 }
3465
3466 if (bond->params.mode == BOND_MODE_8023AD) {
Adrian Bunka40745f2007-10-24 18:27:43 +02003467 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003468 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 /* register to receive LACPDUs */
Jiri Pirko3aba8912011-04-19 03:48:16 +00003470 bond->recv_probe = bond_3ad_lacpdu_recv;
Jay Vosburghfd989c82008-11-04 17:51:16 -08003471 bond_3ad_initiate_agg_selection(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 }
3473
3474 return 0;
3475}
3476
3477static int bond_close(struct net_device *bond_dev)
3478{
Wang Chen454d7c92008-11-12 23:37:49 -08003479 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 write_lock_bh(&bond->lock);
3482
Ben Hutchingsad246c92011-04-26 15:25:52 +00003483 bond->send_peer_notif = 0;
3484
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 write_unlock_bh(&bond->lock);
3486
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 if (bond->params.miimon) { /* link check interval, in milliseconds. */
Jay Vosburghe6d265e2011-10-28 15:42:50 +00003488 cancel_delayed_work_sync(&bond->mii_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 }
3490
3491 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburghe6d265e2011-10-28 15:42:50 +00003492 cancel_delayed_work_sync(&bond->arp_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 }
3494
3495 switch (bond->params.mode) {
3496 case BOND_MODE_8023AD:
Jay Vosburghe6d265e2011-10-28 15:42:50 +00003497 cancel_delayed_work_sync(&bond->ad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 break;
3499 case BOND_MODE_TLB:
3500 case BOND_MODE_ALB:
Jay Vosburghe6d265e2011-10-28 15:42:50 +00003501 cancel_delayed_work_sync(&bond->alb_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 break;
3503 default:
3504 break;
3505 }
3506
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00003507 if (delayed_work_pending(&bond->mcast_work))
Jay Vosburghe6d265e2011-10-28 15:42:50 +00003508 cancel_delayed_work_sync(&bond->mcast_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
Holger Eitzenberger58402052008-12-09 23:07:13 -08003510 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 /* Must be called only after all
3512 * slaves have been released
3513 */
3514 bond_alb_deinitialize(bond);
3515 }
Jiri Pirko3aba8912011-04-19 03:48:16 +00003516 bond->recv_probe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
3518 return 0;
3519}
3520
Eric Dumazet28172732010-07-07 14:58:56 -07003521static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3522 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523{
Wang Chen454d7c92008-11-12 23:37:49 -08003524 struct bonding *bond = netdev_priv(bond_dev);
Eric Dumazet28172732010-07-07 14:58:56 -07003525 struct rtnl_link_stats64 temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 struct slave *slave;
3527 int i;
3528
Eric Dumazet28172732010-07-07 14:58:56 -07003529 memset(stats, 0, sizeof(*stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
3531 read_lock_bh(&bond->lock);
3532
3533 bond_for_each_slave(bond, slave, i) {
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00003534 const struct rtnl_link_stats64 *sstats =
Eric Dumazet28172732010-07-07 14:58:56 -07003535 dev_get_stats(slave->dev, &temp);
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08003536
Eric Dumazet28172732010-07-07 14:58:56 -07003537 stats->rx_packets += sstats->rx_packets;
3538 stats->rx_bytes += sstats->rx_bytes;
3539 stats->rx_errors += sstats->rx_errors;
3540 stats->rx_dropped += sstats->rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Eric Dumazet28172732010-07-07 14:58:56 -07003542 stats->tx_packets += sstats->tx_packets;
3543 stats->tx_bytes += sstats->tx_bytes;
3544 stats->tx_errors += sstats->tx_errors;
3545 stats->tx_dropped += sstats->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
Eric Dumazet28172732010-07-07 14:58:56 -07003547 stats->multicast += sstats->multicast;
3548 stats->collisions += sstats->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
Eric Dumazet28172732010-07-07 14:58:56 -07003550 stats->rx_length_errors += sstats->rx_length_errors;
3551 stats->rx_over_errors += sstats->rx_over_errors;
3552 stats->rx_crc_errors += sstats->rx_crc_errors;
3553 stats->rx_frame_errors += sstats->rx_frame_errors;
3554 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3555 stats->rx_missed_errors += sstats->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
Eric Dumazet28172732010-07-07 14:58:56 -07003557 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3558 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3559 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3560 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3561 stats->tx_window_errors += sstats->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 }
3563
3564 read_unlock_bh(&bond->lock);
3565
3566 return stats;
3567}
3568
3569static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3570{
3571 struct net_device *slave_dev = NULL;
3572 struct ifbond k_binfo;
3573 struct ifbond __user *u_binfo = NULL;
3574 struct ifslave k_sinfo;
3575 struct ifslave __user *u_sinfo = NULL;
3576 struct mii_ioctl_data *mii = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 int res = 0;
3578
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003579 pr_debug("bond_ioctl: master=%s, cmd=%d\n", bond_dev->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
3581 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 case SIOCGMIIPHY:
3583 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003584 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003586
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 mii->phy_id = 0;
3588 /* Fall Through */
3589 case SIOCGMIIREG:
3590 /*
3591 * We do this again just in case we were called by SIOCGMIIREG
3592 * instead of SIOCGMIIPHY.
3593 */
3594 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003595 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003597
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
3599 if (mii->reg_num == 1) {
Wang Chen454d7c92008-11-12 23:37:49 -08003600 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 mii->val_out = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003602 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 read_lock(&bond->curr_slave_lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003604 if (netif_carrier_ok(bond->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 mii->val_out = BMSR_LSTATUS;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003606
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 read_unlock(&bond->curr_slave_lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07003608 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 }
3610
3611 return 0;
3612 case BOND_INFO_QUERY_OLD:
3613 case SIOCBONDINFOQUERY:
3614 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3615
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003616 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
3619 res = bond_info_query(bond_dev, &k_binfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003620 if (res == 0 &&
3621 copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
3622 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623
3624 return res;
3625 case BOND_SLAVE_INFO_QUERY_OLD:
3626 case SIOCBONDSLAVEINFOQUERY:
3627 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3628
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003629 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
3632 res = bond_slave_info_query(bond_dev, &k_sinfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003633 if (res == 0 &&
3634 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
3635 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
3637 return res;
3638 default:
3639 /* Go on */
3640 break;
3641 }
3642
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003643 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00003646 slave_dev = dev_get_by_name(dev_net(bond_dev), ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003648 pr_debug("slave_dev=%p:\n", slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003650 if (!slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 res = -ENODEV;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003652 else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003653 pr_debug("slave_dev->name=%s:\n", slave_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 switch (cmd) {
3655 case BOND_ENSLAVE_OLD:
3656 case SIOCBONDENSLAVE:
3657 res = bond_enslave(bond_dev, slave_dev);
3658 break;
3659 case BOND_RELEASE_OLD:
3660 case SIOCBONDRELEASE:
3661 res = bond_release(bond_dev, slave_dev);
3662 break;
3663 case BOND_SETHWADDR_OLD:
3664 case SIOCBONDSETHWADDR:
3665 res = bond_sethwaddr(bond_dev, slave_dev);
3666 break;
3667 case BOND_CHANGE_ACTIVE_OLD:
3668 case SIOCBONDCHANGEACTIVE:
3669 res = bond_ioctl_change_active(bond_dev, slave_dev);
3670 break;
3671 default:
3672 res = -EOPNOTSUPP;
3673 }
3674
3675 dev_put(slave_dev);
3676 }
3677
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 return res;
3679}
3680
Jiri Pirko22bedad2010-04-01 21:22:57 +00003681static bool bond_addr_in_mc_list(unsigned char *addr,
3682 struct netdev_hw_addr_list *list,
3683 int addrlen)
3684{
3685 struct netdev_hw_addr *ha;
3686
3687 netdev_hw_addr_list_for_each(ha, list)
3688 if (!memcmp(ha->addr, addr, addrlen))
3689 return true;
3690
3691 return false;
3692}
3693
Jiri Pirkod03462b2011-08-16 03:15:04 +00003694static void bond_change_rx_flags(struct net_device *bond_dev, int change)
3695{
3696 struct bonding *bond = netdev_priv(bond_dev);
3697
3698 if (change & IFF_PROMISC)
3699 bond_set_promiscuity(bond,
3700 bond_dev->flags & IFF_PROMISC ? 1 : -1);
3701
3702 if (change & IFF_ALLMULTI)
3703 bond_set_allmulti(bond,
3704 bond_dev->flags & IFF_ALLMULTI ? 1 : -1);
3705}
3706
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707static void bond_set_multicast_list(struct net_device *bond_dev)
3708{
Wang Chen454d7c92008-11-12 23:37:49 -08003709 struct bonding *bond = netdev_priv(bond_dev);
Jiri Pirko22bedad2010-04-01 21:22:57 +00003710 struct netdev_hw_addr *ha;
3711 bool found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08003713 read_lock(&bond->lock);
3714
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 /* looking for addresses to add to slaves' mc list */
Jiri Pirko22bedad2010-04-01 21:22:57 +00003716 netdev_for_each_mc_addr(ha, bond_dev) {
3717 found = bond_addr_in_mc_list(ha->addr, &bond->mc_list,
3718 bond_dev->addr_len);
3719 if (!found)
3720 bond_mc_add(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 }
3722
3723 /* looking for addresses to delete from slaves' list */
Jiri Pirko22bedad2010-04-01 21:22:57 +00003724 netdev_hw_addr_list_for_each(ha, &bond->mc_list) {
3725 found = bond_addr_in_mc_list(ha->addr, &bond_dev->mc,
3726 bond_dev->addr_len);
3727 if (!found)
3728 bond_mc_del(bond, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 }
3730
3731 /* save master's multicast list */
Jiri Pirko22bedad2010-04-01 21:22:57 +00003732 __hw_addr_flush(&bond->mc_list);
3733 __hw_addr_add_multiple(&bond->mc_list, &bond_dev->mc,
3734 bond_dev->addr_len, NETDEV_HW_ADDR_T_MULTICAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
Jay Vosburgh80ee5ad2008-01-29 18:07:44 -08003736 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737}
3738
Stephen Hemminger00829822008-11-20 20:14:53 -08003739static int bond_neigh_setup(struct net_device *dev, struct neigh_parms *parms)
3740{
3741 struct bonding *bond = netdev_priv(dev);
3742 struct slave *slave = bond->first_slave;
3743
3744 if (slave) {
3745 const struct net_device_ops *slave_ops
3746 = slave->dev->netdev_ops;
3747 if (slave_ops->ndo_neigh_setup)
Patrick McHardy72e22402009-03-05 01:57:44 -08003748 return slave_ops->ndo_neigh_setup(slave->dev, parms);
Stephen Hemminger00829822008-11-20 20:14:53 -08003749 }
3750 return 0;
3751}
3752
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753/*
3754 * Change the MTU of all of a master's slaves to match the master
3755 */
3756static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3757{
Wang Chen454d7c92008-11-12 23:37:49 -08003758 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 struct slave *slave, *stop_at;
3760 int res = 0;
3761 int i;
3762
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003763 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003764 (bond_dev ? bond_dev->name : "None"), new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
3766 /* Can't hold bond->lock with bh disabled here since
3767 * some base drivers panic. On the other hand we can't
3768 * hold bond->lock without bh disabled because we'll
3769 * deadlock. The only solution is to rely on the fact
3770 * that we're under rtnl_lock here, and the slaves
3771 * list won't change. This doesn't solve the problem
3772 * of setting the slave's MTU while it is
3773 * transmitting, but the assumption is that the base
3774 * driver can handle that.
3775 *
3776 * TODO: figure out a way to safely iterate the slaves
3777 * list, but without holding a lock around the actual
3778 * call to the base driver.
3779 */
3780
3781 bond_for_each_slave(bond, slave, i) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003782 pr_debug("s %p s->p %p c_m %p\n",
3783 slave,
3784 slave->prev,
3785 slave->dev->netdev_ops->ndo_change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08003786
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 res = dev_set_mtu(slave->dev, new_mtu);
3788
3789 if (res) {
3790 /* If we failed to set the slave's mtu to the new value
3791 * we must abort the operation even in ACTIVE_BACKUP
3792 * mode, because if we allow the backup slaves to have
3793 * different mtu values than the active slave we'll
3794 * need to change their mtu when doing a failover. That
3795 * means changing their mtu from timer context, which
3796 * is probably not a good idea.
3797 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003798 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 goto unwind;
3800 }
3801 }
3802
3803 bond_dev->mtu = new_mtu;
3804
3805 return 0;
3806
3807unwind:
3808 /* unwind from head to the slave that failed */
3809 stop_at = slave;
3810 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3811 int tmp_res;
3812
3813 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu);
3814 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003815 pr_debug("unwind err %d dev %s\n",
3816 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 }
3818 }
3819
3820 return res;
3821}
3822
3823/*
3824 * Change HW address
3825 *
3826 * Note that many devices must be down to change the HW address, and
3827 * downing the master releases all slaves. We can make bonds full of
3828 * bonding devices to test this, however.
3829 */
3830static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
3831{
Wang Chen454d7c92008-11-12 23:37:49 -08003832 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 struct sockaddr *sa = addr, tmp_sa;
3834 struct slave *slave, *stop_at;
3835 int res = 0;
3836 int i;
3837
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08003838 if (bond->params.mode == BOND_MODE_ALB)
3839 return bond_alb_set_mac_address(bond_dev, addr);
3840
3841
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003842 pr_debug("bond=%p, name=%s\n",
3843 bond, bond_dev ? bond_dev->name : "None");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
Jay Vosburghdd957c52007-10-09 19:57:24 -07003845 /*
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07003846 * If fail_over_mac is set to active, do nothing and return
3847 * success. Returning an error causes ifenslave to fail.
Jay Vosburghdd957c52007-10-09 19:57:24 -07003848 */
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07003849 if (bond->params.fail_over_mac == BOND_FOM_ACTIVE)
Jay Vosburghdd957c52007-10-09 19:57:24 -07003850 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07003851
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003852 if (!is_valid_ether_addr(sa->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
3855 /* Can't hold bond->lock with bh disabled here since
3856 * some base drivers panic. On the other hand we can't
3857 * hold bond->lock without bh disabled because we'll
3858 * deadlock. The only solution is to rely on the fact
3859 * that we're under rtnl_lock here, and the slaves
3860 * list won't change. This doesn't solve the problem
3861 * of setting the slave's hw address while it is
3862 * transmitting, but the assumption is that the base
3863 * driver can handle that.
3864 *
3865 * TODO: figure out a way to safely iterate the slaves
3866 * list, but without holding a lock around the actual
3867 * call to the base driver.
3868 */
3869
3870 bond_for_each_slave(bond, slave, i) {
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08003871 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003872 pr_debug("slave %p %s\n", slave, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08003874 if (slave_ops->ndo_set_mac_address == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 res = -EOPNOTSUPP;
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003876 pr_debug("EOPNOTSUPP %s\n", slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 goto unwind;
3878 }
3879
3880 res = dev_set_mac_address(slave->dev, addr);
3881 if (res) {
3882 /* TODO: consider downing the slave
3883 * and retry ?
3884 * User should expect communications
3885 * breakage anyway until ARP finish
3886 * updating, so...
3887 */
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -08003888 pr_debug("err %d %s\n", res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 goto unwind;
3890 }
3891 }
3892
3893 /* success */
3894 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
3895 return 0;
3896
3897unwind:
3898 memcpy(tmp_sa.sa_data, bond_dev->dev_addr, bond_dev->addr_len);
3899 tmp_sa.sa_family = bond_dev->type;
3900
3901 /* unwind from head to the slave that failed */
3902 stop_at = slave;
3903 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
3904 int tmp_res;
3905
3906 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa);
3907 if (tmp_res) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08003908 pr_debug("unwind err %d dev %s\n",
3909 tmp_res, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 }
3911 }
3912
3913 return res;
3914}
3915
3916static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
3917{
Wang Chen454d7c92008-11-12 23:37:49 -08003918 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 struct slave *slave, *start_at;
Jay Vosburghcf5f9042007-10-17 17:37:47 -07003920 int i, slave_no, res = 1;
Andy Gospodareka2fd9402010-03-25 14:49:05 +00003921 struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922
Jay Vosburghcf5f9042007-10-17 17:37:47 -07003923 /*
Andy Gospodareka2fd9402010-03-25 14:49:05 +00003924 * Start with the curr_active_slave that joined the bond as the
3925 * default for sending IGMP traffic. For failover purposes one
3926 * needs to maintain some consistency for the interface that will
3927 * send the join/membership reports. The curr_active_slave found
3928 * will send all of this type of traffic.
Jay Vosburghcf5f9042007-10-17 17:37:47 -07003929 */
Eric Dumazet00ae7022010-03-30 23:08:37 +00003930 if ((iph->protocol == IPPROTO_IGMP) &&
Andy Gospodareka2fd9402010-03-25 14:49:05 +00003931 (skb->protocol == htons(ETH_P_IP))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
Andy Gospodareka2fd9402010-03-25 14:49:05 +00003933 read_lock(&bond->curr_slave_lock);
3934 slave = bond->curr_active_slave;
3935 read_unlock(&bond->curr_slave_lock);
3936
3937 if (!slave)
3938 goto out;
3939 } else {
3940 /*
3941 * Concurrent TX may collide on rr_tx_counter; we accept
3942 * that as being rare enough not to justify using an
3943 * atomic op here.
3944 */
3945 slave_no = bond->rr_tx_counter++ % bond->slave_cnt;
3946
3947 bond_for_each_slave(bond, slave, i) {
3948 slave_no--;
3949 if (slave_no < 0)
3950 break;
3951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 }
3953
Jay Vosburghcf5f9042007-10-17 17:37:47 -07003954 start_at = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 bond_for_each_slave_from(bond, slave, i, start_at) {
3956 if (IS_UP(slave->dev) &&
3957 (slave->link == BOND_LINK_UP) &&
Jiri Pirkoe30bc062011-03-12 03:14:37 +00003958 bond_is_active_slave(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 res = bond_dev_queue_xmit(bond, skb, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 break;
3961 }
3962 }
3963
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964out:
3965 if (res) {
3966 /* no suitable interface, frame not sent */
3967 dev_kfree_skb(skb);
3968 }
Michał Mirosław0693e882011-05-07 01:48:02 +00003969
Patrick McHardyec634fe2009-07-05 19:23:38 -07003970 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971}
3972
John W. Linville075897c2005-09-28 17:50:53 -04003973
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974/*
3975 * in active-backup mode, we know that bond->curr_active_slave is always valid if
3976 * the bond has a usable interface.
3977 */
3978static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev)
3979{
Wang Chen454d7c92008-11-12 23:37:49 -08003980 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 int res = 1;
3982
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 read_lock(&bond->curr_slave_lock);
3984
Michał Mirosław0693e882011-05-07 01:48:02 +00003985 if (bond->curr_active_slave)
3986 res = bond_dev_queue_xmit(bond, skb,
3987 bond->curr_active_slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003989 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 /* no suitable interface, frame not sent */
3991 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003992
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 read_unlock(&bond->curr_slave_lock);
Michał Mirosław0693e882011-05-07 01:48:02 +00003994
Patrick McHardyec634fe2009-07-05 19:23:38 -07003995 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996}
3997
3998/*
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003999 * In bond_xmit_xor() , we determine the output device by using a pre-
4000 * determined xmit_hash_policy(), If the selected device is not enabled,
4001 * find the next active slave.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 */
4003static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
4004{
Wang Chen454d7c92008-11-12 23:37:49 -08004005 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 struct slave *slave, *start_at;
4007 int slave_no;
4008 int i;
4009 int res = 1;
4010
Jasper Spaansa361c832009-10-23 04:09:24 +00004011 slave_no = bond->xmit_hash_policy(skb, bond->slave_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
4013 bond_for_each_slave(bond, slave, i) {
4014 slave_no--;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004015 if (slave_no < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 }
4018
4019 start_at = slave;
4020
4021 bond_for_each_slave_from(bond, slave, i, start_at) {
4022 if (IS_UP(slave->dev) &&
4023 (slave->link == BOND_LINK_UP) &&
Jiri Pirkoe30bc062011-03-12 03:14:37 +00004024 bond_is_active_slave(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4026 break;
4027 }
4028 }
4029
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 if (res) {
4031 /* no suitable interface, frame not sent */
4032 dev_kfree_skb(skb);
4033 }
Michał Mirosław0693e882011-05-07 01:48:02 +00004034
Patrick McHardyec634fe2009-07-05 19:23:38 -07004035 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036}
4037
4038/*
4039 * in broadcast mode, we send everything to all usable interfaces.
4040 */
4041static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
4042{
Wang Chen454d7c92008-11-12 23:37:49 -08004043 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 struct slave *slave, *start_at;
4045 struct net_device *tx_dev = NULL;
4046 int i;
4047 int res = 1;
4048
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 read_lock(&bond->curr_slave_lock);
4050 start_at = bond->curr_active_slave;
4051 read_unlock(&bond->curr_slave_lock);
4052
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004053 if (!start_at)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
4056 bond_for_each_slave_from(bond, slave, i, start_at) {
4057 if (IS_UP(slave->dev) &&
4058 (slave->link == BOND_LINK_UP) &&
Jiri Pirkoe30bc062011-03-12 03:14:37 +00004059 bond_is_active_slave(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 if (tx_dev) {
4061 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
4062 if (!skb2) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004063 pr_err("%s: Error: bond_xmit_broadcast(): skb_clone() failed\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08004064 bond_dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 continue;
4066 }
4067
4068 res = bond_dev_queue_xmit(bond, skb2, tx_dev);
4069 if (res) {
4070 dev_kfree_skb(skb2);
4071 continue;
4072 }
4073 }
4074 tx_dev = slave->dev;
4075 }
4076 }
4077
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004078 if (tx_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 res = bond_dev_queue_xmit(bond, skb, tx_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
4081out:
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004082 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 /* no suitable interface, frame not sent */
4084 dev_kfree_skb(skb);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004085
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 /* frame sent to all suitable interfaces */
Patrick McHardyec634fe2009-07-05 19:23:38 -07004087 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088}
4089
4090/*------------------------- Device initialization ---------------------------*/
4091
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004092static void bond_set_xmit_hash_policy(struct bonding *bond)
4093{
4094 switch (bond->params.xmit_policy) {
4095 case BOND_XMIT_POLICY_LAYER23:
4096 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
4097 break;
4098 case BOND_XMIT_POLICY_LAYER34:
4099 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
4100 break;
4101 case BOND_XMIT_POLICY_LAYER2:
4102 default:
4103 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
4104 break;
4105 }
4106}
4107
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004108/*
4109 * Lookup the slave that corresponds to a qid
4110 */
4111static inline int bond_slave_override(struct bonding *bond,
4112 struct sk_buff *skb)
4113{
4114 int i, res = 1;
4115 struct slave *slave = NULL;
4116 struct slave *check_slave;
4117
Michał Mirosław0693e882011-05-07 01:48:02 +00004118 if (!skb->queue_mapping)
4119 return 1;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004120
4121 /* Find out if any slaves have the same mapping as this skb. */
4122 bond_for_each_slave(bond, check_slave, i) {
4123 if (check_slave->queue_id == skb->queue_mapping) {
4124 slave = check_slave;
4125 break;
4126 }
4127 }
4128
4129 /* If the slave isn't UP, use default transmit policy. */
4130 if (slave && slave->queue_id && IS_UP(slave->dev) &&
4131 (slave->link == BOND_LINK_UP)) {
4132 res = bond_dev_queue_xmit(bond, skb, slave->dev);
4133 }
4134
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004135 return res;
4136}
4137
Neil Horman374eeb52011-06-03 10:35:52 +00004138
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004139static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb)
4140{
4141 /*
4142 * This helper function exists to help dev_pick_tx get the correct
Phil Oesterfd0e4352011-03-14 06:22:04 +00004143 * destination queue. Using a helper function skips a call to
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004144 * skb_tx_hash and will put the skbs in the queue we expect on their
4145 * way down to the bonding driver.
4146 */
Phil Oesterfd0e4352011-03-14 06:22:04 +00004147 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
4148
Neil Horman374eeb52011-06-03 10:35:52 +00004149 /*
4150 * Save the original txq to restore before passing to the driver
4151 */
4152 bond_queue_mapping(skb) = skb->queue_mapping;
4153
Phil Oesterfd0e4352011-03-14 06:22:04 +00004154 if (unlikely(txq >= dev->real_num_tx_queues)) {
David Decotignyd30ee672011-04-13 15:22:29 +00004155 do {
Phil Oesterfd0e4352011-03-14 06:22:04 +00004156 txq -= dev->real_num_tx_queues;
David Decotignyd30ee672011-04-13 15:22:29 +00004157 } while (txq >= dev->real_num_tx_queues);
Phil Oesterfd0e4352011-03-14 06:22:04 +00004158 }
4159 return txq;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004160}
4161
Michał Mirosław0693e882011-05-07 01:48:02 +00004162static netdev_tx_t __bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
Stephen Hemminger00829822008-11-20 20:14:53 -08004163{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004164 struct bonding *bond = netdev_priv(dev);
4165
4166 if (TX_QUEUE_OVERRIDE(bond->params.mode)) {
4167 if (!bond_slave_override(bond, skb))
4168 return NETDEV_TX_OK;
4169 }
Stephen Hemminger00829822008-11-20 20:14:53 -08004170
4171 switch (bond->params.mode) {
4172 case BOND_MODE_ROUNDROBIN:
4173 return bond_xmit_roundrobin(skb, dev);
4174 case BOND_MODE_ACTIVEBACKUP:
4175 return bond_xmit_activebackup(skb, dev);
4176 case BOND_MODE_XOR:
4177 return bond_xmit_xor(skb, dev);
4178 case BOND_MODE_BROADCAST:
4179 return bond_xmit_broadcast(skb, dev);
4180 case BOND_MODE_8023AD:
4181 return bond_3ad_xmit_xor(skb, dev);
4182 case BOND_MODE_ALB:
4183 case BOND_MODE_TLB:
4184 return bond_alb_xmit(skb, dev);
4185 default:
4186 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004187 pr_err("%s: Error: Unknown bonding mode %d\n",
4188 dev->name, bond->params.mode);
Stephen Hemminger00829822008-11-20 20:14:53 -08004189 WARN_ON_ONCE(1);
4190 dev_kfree_skb(skb);
4191 return NETDEV_TX_OK;
4192 }
4193}
4194
Michał Mirosław0693e882011-05-07 01:48:02 +00004195static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
4196{
4197 struct bonding *bond = netdev_priv(dev);
4198 netdev_tx_t ret = NETDEV_TX_OK;
4199
4200 /*
4201 * If we risk deadlock from transmitting this in the
4202 * netpoll path, tell netpoll to queue the frame for later tx
4203 */
4204 if (is_netpoll_tx_blocked(dev))
4205 return NETDEV_TX_BUSY;
4206
4207 read_lock(&bond->lock);
4208
4209 if (bond->slave_cnt)
4210 ret = __bond_start_xmit(skb, dev);
4211 else
4212 dev_kfree_skb(skb);
4213
4214 read_unlock(&bond->lock);
4215
4216 return ret;
4217}
Stephen Hemminger00829822008-11-20 20:14:53 -08004218
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219/*
4220 * set bond mode specific net device operations
4221 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08004222void bond_set_mode_ops(struct bonding *bond, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223{
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004224 struct net_device *bond_dev = bond->dev;
4225
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 switch (mode) {
4227 case BOND_MODE_ROUNDROBIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 break;
4229 case BOND_MODE_ACTIVEBACKUP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 break;
4231 case BOND_MODE_XOR:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004232 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 break;
4234 case BOND_MODE_BROADCAST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 break;
4236 case BOND_MODE_8023AD:
Jay Vosburgh6f6652b2007-12-06 23:40:34 -08004237 bond_set_xmit_hash_policy(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 case BOND_MODE_ALB:
Jay Vosburgh8f903c72006-02-21 16:36:44 -08004240 /* FALLTHRU */
4241 case BOND_MODE_TLB:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 break;
4243 default:
4244 /* Should never happen, mode already checked */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004245 pr_err("%s: Error: Unknown bonding mode %d\n",
4246 bond_dev->name, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 break;
4248 }
4249}
4250
Jay Vosburgh217df672005-09-26 16:11:50 -07004251static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
4252 struct ethtool_drvinfo *drvinfo)
4253{
4254 strncpy(drvinfo->driver, DRV_NAME, 32);
4255 strncpy(drvinfo->version, DRV_VERSION, 32);
4256 snprintf(drvinfo->fw_version, 32, "%d", BOND_ABI_VERSION);
4257}
4258
Jeff Garzik7282d492006-09-13 14:30:00 -04004259static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004260 .get_drvinfo = bond_ethtool_get_drvinfo,
Stephen Hemmingerfa53eba2008-09-13 21:17:09 -04004261 .get_link = ethtool_op_get_link,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004262};
4263
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004264static const struct net_device_ops bond_netdev_ops = {
Stephen Hemminger181470f2009-06-12 19:02:52 +00004265 .ndo_init = bond_init,
Stephen Hemminger9e716262009-06-12 19:02:47 +00004266 .ndo_uninit = bond_uninit,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004267 .ndo_open = bond_open,
4268 .ndo_stop = bond_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08004269 .ndo_start_xmit = bond_start_xmit,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004270 .ndo_select_queue = bond_select_queue,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00004271 .ndo_get_stats64 = bond_get_stats,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004272 .ndo_do_ioctl = bond_do_ioctl,
Jiri Pirkod03462b2011-08-16 03:15:04 +00004273 .ndo_change_rx_flags = bond_change_rx_flags,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00004274 .ndo_set_rx_mode = bond_set_multicast_list,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004275 .ndo_change_mtu = bond_change_mtu,
Jiri Pirkocc0e4072011-07-20 04:54:46 +00004276 .ndo_set_mac_address = bond_set_mac_address,
Stephen Hemminger00829822008-11-20 20:14:53 -08004277 .ndo_neigh_setup = bond_neigh_setup,
Jiri Pirkocc0e4072011-07-20 04:54:46 +00004278 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004279 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
WANG Congf6dc31a2010-05-06 00:48:51 -07004280#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00004281 .ndo_netpoll_setup = bond_netpoll_setup,
WANG Congf6dc31a2010-05-06 00:48:51 -07004282 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
4283 .ndo_poll_controller = bond_poll_controller,
4284#endif
Jiri Pirko9232ecc2011-02-13 09:33:01 +00004285 .ndo_add_slave = bond_enslave,
4286 .ndo_del_slave = bond_release,
Michał Mirosławb2a103e2011-05-07 03:22:17 +00004287 .ndo_fix_features = bond_fix_features,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004288};
4289
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004290static void bond_destructor(struct net_device *bond_dev)
4291{
4292 struct bonding *bond = netdev_priv(bond_dev);
4293 if (bond->wq)
4294 destroy_workqueue(bond->wq);
4295 free_netdev(bond_dev);
4296}
4297
Stephen Hemminger181470f2009-06-12 19:02:52 +00004298static void bond_setup(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299{
Wang Chen454d7c92008-11-12 23:37:49 -08004300 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 /* initialize rwlocks */
4303 rwlock_init(&bond->lock);
4304 rwlock_init(&bond->curr_slave_lock);
4305
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00004306 bond->params = bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
4308 /* Initialize pointers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 bond->dev = bond_dev;
4310 INIT_LIST_HEAD(&bond->vlan_list);
4311
4312 /* Initialize the device entry points */
Stephen Hemminger181470f2009-06-12 19:02:52 +00004313 ether_setup(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004314 bond_dev->netdev_ops = &bond_netdev_ops;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004315 bond_dev->ethtool_ops = &bond_ethtool_ops;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004316 bond_set_mode_ops(bond, bond->params.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004318 bond_dev->destructor = bond_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319
4320 /* Initialize the device options */
4321 bond_dev->tx_queue_len = 0;
4322 bond_dev->flags |= IFF_MASTER|IFF_MULTICAST;
Jay Vosburgh0b680e72006-09-22 21:54:10 -07004323 bond_dev->priv_flags |= IFF_BONDING;
Neil Horman550fd082011-07-26 06:05:38 +00004324 bond_dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
Stephen Hemminger181470f2009-06-12 19:02:52 +00004325
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 /* At first, we block adding VLANs. That's the only way to
4327 * prevent problems that occur when adding VLANs over an
4328 * empty bond. The block will be removed once non-challenged
4329 * slaves are enslaved.
4330 */
4331 bond_dev->features |= NETIF_F_VLAN_CHALLENGED;
4332
Herbert Xu932ff272006-06-09 12:20:56 -07004333 /* don't acquire bond device's netif_tx_lock when
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 * transmitting */
4335 bond_dev->features |= NETIF_F_LLTX;
4336
4337 /* By default, we declare the bond to be fully
4338 * VLAN hardware accelerated capable. Special
4339 * care is taken in the various xmit functions
4340 * when there are slaves that are not hw accel
4341 * capable
4342 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
Michał Mirosławb2a103e2011-05-07 03:22:17 +00004344 bond_dev->hw_features = BOND_VLAN_FEATURES |
4345 NETIF_F_HW_VLAN_TX |
4346 NETIF_F_HW_VLAN_RX |
4347 NETIF_F_HW_VLAN_FILTER;
4348
Michał Mirosław34324dc2011-11-15 15:29:55 +00004349 bond_dev->hw_features &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_HW_CSUM);
Michał Mirosławb2a103e2011-05-07 03:22:17 +00004350 bond_dev->features |= bond_dev->hw_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351}
4352
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004353static void bond_work_cancel_all(struct bonding *bond)
4354{
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004355 if (bond->params.miimon && delayed_work_pending(&bond->mii_work))
Jay Vosburghe6d265e2011-10-28 15:42:50 +00004356 cancel_delayed_work_sync(&bond->mii_work);
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004357
4358 if (bond->params.arp_interval && delayed_work_pending(&bond->arp_work))
Jay Vosburghe6d265e2011-10-28 15:42:50 +00004359 cancel_delayed_work_sync(&bond->arp_work);
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004360
4361 if (bond->params.mode == BOND_MODE_ALB &&
4362 delayed_work_pending(&bond->alb_work))
Jay Vosburghe6d265e2011-10-28 15:42:50 +00004363 cancel_delayed_work_sync(&bond->alb_work);
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004364
4365 if (bond->params.mode == BOND_MODE_8023AD &&
4366 delayed_work_pending(&bond->ad_work))
Jay Vosburghe6d265e2011-10-28 15:42:50 +00004367 cancel_delayed_work_sync(&bond->ad_work);
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00004368
4369 if (delayed_work_pending(&bond->mcast_work))
Jay Vosburghe6d265e2011-10-28 15:42:50 +00004370 cancel_delayed_work_sync(&bond->mcast_work);
Jay Vosburghfdaea7a2007-12-06 23:40:35 -08004371}
4372
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004373/*
4374* Destroy a bonding device.
4375* Must be under rtnl_lock when this function is called.
4376*/
4377static void bond_uninit(struct net_device *bond_dev)
Jay Vosburgha434e432008-10-30 17:41:15 -07004378{
Wang Chen454d7c92008-11-12 23:37:49 -08004379 struct bonding *bond = netdev_priv(bond_dev);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004380 struct vlan_entry *vlan, *tmp;
Jay Vosburgha434e432008-10-30 17:41:15 -07004381
WANG Congf6dc31a2010-05-06 00:48:51 -07004382 bond_netpoll_cleanup(bond_dev);
4383
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004384 /* Release the bonded slaves */
4385 bond_release_all(bond_dev);
4386
Jay Vosburgha434e432008-10-30 17:41:15 -07004387 list_del(&bond->bond_list);
4388
4389 bond_work_cancel_all(bond);
4390
Jay Vosburgha434e432008-10-30 17:41:15 -07004391 bond_remove_proc_entry(bond);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004392
Taku Izumif073c7c2010-12-09 15:17:13 +00004393 bond_debug_unregister(bond);
4394
Jiri Pirko22bedad2010-04-01 21:22:57 +00004395 __hw_addr_flush(&bond->mc_list);
Jay Vosburghf35188f2010-07-21 12:14:47 +00004396
4397 list_for_each_entry_safe(vlan, tmp, &bond->vlan_list, vlan_list) {
4398 list_del(&vlan->vlan_list);
4399 kfree(vlan);
4400 }
Jay Vosburgha434e432008-10-30 17:41:15 -07004401}
4402
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403/*------------------------- Module initialization ---------------------------*/
4404
4405/*
4406 * Convert string input module parms. Accept either the
Jay Vosburghece95f72008-01-17 16:25:01 -08004407 * number of the mode or its string name. A bit complicated because
4408 * some mode names are substrings of other names, and calls from sysfs
4409 * may have whitespace in the name (trailing newlines, for example).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 */
Holger Eitzenberger325dcf72008-12-09 23:10:17 -08004411int bond_parse_parm(const char *buf, const struct bond_parm_tbl *tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412{
Hannes Eder54b87322009-02-14 11:15:49 +00004413 int modeint = -1, i, rv;
Jay Vosburgha42e5342008-01-29 18:07:43 -08004414 char *p, modestr[BOND_MAX_MODENAME_LEN + 1] = { 0, };
Jay Vosburghece95f72008-01-17 16:25:01 -08004415
Jay Vosburgha42e5342008-01-29 18:07:43 -08004416 for (p = (char *)buf; *p; p++)
4417 if (!(isdigit(*p) || isspace(*p)))
4418 break;
4419
4420 if (*p)
Jay Vosburghece95f72008-01-17 16:25:01 -08004421 rv = sscanf(buf, "%20s", modestr);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004422 else
Hannes Eder54b87322009-02-14 11:15:49 +00004423 rv = sscanf(buf, "%d", &modeint);
Jay Vosburgha42e5342008-01-29 18:07:43 -08004424
4425 if (!rv)
4426 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
4428 for (i = 0; tbl[i].modename; i++) {
Hannes Eder54b87322009-02-14 11:15:49 +00004429 if (modeint == tbl[i].mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 return tbl[i].mode;
Jay Vosburghece95f72008-01-17 16:25:01 -08004431 if (strcmp(modestr, tbl[i].modename) == 0)
4432 return tbl[i].mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 }
4434
4435 return -1;
4436}
4437
4438static int bond_check_params(struct bond_params *params)
4439{
Jiri Pirkoa5499522009-09-25 03:28:09 +00004440 int arp_validate_value, fail_over_mac_value, primary_reselect_value;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004441
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 /*
4443 * Convert string parameters.
4444 */
4445 if (mode) {
4446 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4447 if (bond_mode == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004448 pr_err("Error: Invalid bonding mode \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 mode == NULL ? "NULL" : mode);
4450 return -EINVAL;
4451 }
4452 }
4453
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004454 if (xmit_hash_policy) {
4455 if ((bond_mode != BOND_MODE_XOR) &&
4456 (bond_mode != BOND_MODE_8023AD)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004457 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004458 bond_mode_name(bond_mode));
4459 } else {
4460 xmit_hashtype = bond_parse_parm(xmit_hash_policy,
4461 xmit_hashtype_tbl);
4462 if (xmit_hashtype == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004463 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004464 xmit_hash_policy == NULL ? "NULL" :
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004465 xmit_hash_policy);
4466 return -EINVAL;
4467 }
4468 }
4469 }
4470
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 if (lacp_rate) {
4472 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004473 pr_info("lacp_rate param is irrelevant in mode %s\n",
4474 bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 } else {
4476 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4477 if (lacp_fast == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004478 pr_err("Error: Invalid lacp rate \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 lacp_rate == NULL ? "NULL" : lacp_rate);
4480 return -EINVAL;
4481 }
4482 }
4483 }
4484
Jay Vosburghfd989c82008-11-04 17:51:16 -08004485 if (ad_select) {
4486 params->ad_select = bond_parse_parm(ad_select, ad_select_tbl);
4487 if (params->ad_select == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004488 pr_err("Error: Invalid ad_select \"%s\"\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -08004489 ad_select == NULL ? "NULL" : ad_select);
4490 return -EINVAL;
4491 }
4492
4493 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004494 pr_warning("ad_select param only affects 802.3ad mode\n");
Jay Vosburghfd989c82008-11-04 17:51:16 -08004495 }
4496 } else {
4497 params->ad_select = BOND_AD_STABLE;
4498 }
4499
Nicolas de Pesloüanf5841302009-08-28 13:18:34 +00004500 if (max_bonds < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004501 pr_warning("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4502 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 max_bonds = BOND_DEFAULT_MAX_BONDS;
4504 }
4505
4506 if (miimon < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004507 pr_warning("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to %d\n",
4508 miimon, INT_MAX, BOND_LINK_MON_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 miimon = BOND_LINK_MON_INTERV;
4510 }
4511
4512 if (updelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004513 pr_warning("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4514 updelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 updelay = 0;
4516 }
4517
4518 if (downdelay < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004519 pr_warning("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4520 downdelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 downdelay = 0;
4522 }
4523
4524 if ((use_carrier != 0) && (use_carrier != 1)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004525 pr_warning("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
4526 use_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 use_carrier = 1;
4528 }
4529
Ben Hutchingsad246c92011-04-26 15:25:52 +00004530 if (num_peer_notif < 0 || num_peer_notif > 255) {
4531 pr_warning("Warning: num_grat_arp/num_unsol_na (%d) not in range 0-255 so it was reset to 1\n",
4532 num_peer_notif);
4533 num_peer_notif = 1;
4534 }
4535
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 /* reset values for 802.3ad */
4537 if (bond_mode == BOND_MODE_8023AD) {
4538 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004539 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 +00004540 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 miimon = 100;
4542 }
4543 }
4544
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004545 if (tx_queues < 1 || tx_queues > 255) {
4546 pr_warning("Warning: tx_queues (%d) should be between "
4547 "1 and 255, resetting to %d\n",
4548 tx_queues, BOND_DEFAULT_TX_QUEUES);
4549 tx_queues = BOND_DEFAULT_TX_QUEUES;
4550 }
4551
Andy Gospodarekebd8e492010-06-02 08:39:21 +00004552 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
4553 pr_warning("Warning: all_slaves_active module parameter (%d), "
4554 "not of valid value (0/1), so it was set to "
4555 "0\n", all_slaves_active);
4556 all_slaves_active = 0;
4557 }
4558
Flavio Leitnerc2952c32010-10-05 14:23:59 +00004559 if (resend_igmp < 0 || resend_igmp > 255) {
4560 pr_warning("Warning: resend_igmp (%d) should be between "
4561 "0 and 255, resetting to %d\n",
4562 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
4563 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
4564 }
4565
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 /* reset values for TLB/ALB */
4567 if ((bond_mode == BOND_MODE_TLB) ||
4568 (bond_mode == BOND_MODE_ALB)) {
4569 if (!miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004570 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 +00004571 pr_warning("Forcing miimon to 100msec\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 miimon = 100;
4573 }
4574 }
4575
4576 if (bond_mode == BOND_MODE_ALB) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004577 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",
4578 updelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 }
4580
4581 if (!miimon) {
4582 if (updelay || downdelay) {
4583 /* just warn the user the up/down delay will have
4584 * no effect since miimon is zero...
4585 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004586 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",
4587 updelay, downdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 }
4589 } else {
4590 /* don't allow arp monitoring */
4591 if (arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004592 pr_warning("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
4593 miimon, arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 arp_interval = 0;
4595 }
4596
4597 if ((updelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004598 pr_warning("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
4599 updelay, miimon,
4600 (updelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 }
4602
4603 updelay /= miimon;
4604
4605 if ((downdelay % miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004606 pr_warning("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
4607 downdelay, miimon,
4608 (downdelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 }
4610
4611 downdelay /= miimon;
4612 }
4613
4614 if (arp_interval < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004615 pr_warning("Warning: arp_interval module parameter (%d) , not in range 0-%d, so it was reset to %d\n",
4616 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 arp_interval = BOND_LINK_ARP_INTERV;
4618 }
4619
4620 for (arp_ip_count = 0;
4621 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[arp_ip_count];
4622 arp_ip_count++) {
4623 /* not complete check, but should be good enough to
4624 catch mistakes */
4625 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004626 pr_warning("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
4627 arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 arp_interval = 0;
4629 } else {
Al Virod3bb52b2007-08-22 20:06:58 -04004630 __be32 ip = in_aton(arp_ip_target[arp_ip_count]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 arp_target[arp_ip_count] = ip;
4632 }
4633 }
4634
4635 if (arp_interval && !arp_ip_count) {
4636 /* don't allow arping if no arp_ip_target given... */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004637 pr_warning("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
4638 arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 arp_interval = 0;
4640 }
4641
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004642 if (arp_validate) {
4643 if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004644 pr_err("arp_validate only supported in active-backup mode\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004645 return -EINVAL;
4646 }
4647 if (!arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004648 pr_err("arp_validate requires arp_interval\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004649 return -EINVAL;
4650 }
4651
4652 arp_validate_value = bond_parse_parm(arp_validate,
4653 arp_validate_tbl);
4654 if (arp_validate_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004655 pr_err("Error: invalid arp_validate \"%s\"\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004656 arp_validate == NULL ? "NULL" : arp_validate);
4657 return -EINVAL;
4658 }
4659 } else
4660 arp_validate_value = 0;
4661
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 if (miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004663 pr_info("MII link monitoring set to %d ms\n", miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 } else if (arp_interval) {
4665 int i;
4666
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004667 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
4668 arp_interval,
4669 arp_validate_tbl[arp_validate_value].modename,
4670 arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671
4672 for (i = 0; i < arp_ip_count; i++)
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00004673 pr_info(" %s", arp_ip_target[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00004675 pr_info("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
Jay Vosburghb8a97872008-06-13 18:12:04 -07004677 } else if (max_bonds) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 /* miimon and arp_interval not set, we need one so things
4679 * work as expected, see bonding.txt for details
4680 */
Andy Gospodarekb2730f42011-07-27 10:09:26 +00004681 pr_debug("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 -07004682 }
4683
4684 if (primary && !USES_PRIMARY(bond_mode)) {
4685 /* currently, using a primary only makes sense
4686 * in active backup, TLB or ALB modes
4687 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004688 pr_warning("Warning: %s primary device specified but has no effect in %s mode\n",
4689 primary, bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 primary = NULL;
4691 }
4692
Jiri Pirkoa5499522009-09-25 03:28:09 +00004693 if (primary && primary_reselect) {
4694 primary_reselect_value = bond_parse_parm(primary_reselect,
4695 pri_reselect_tbl);
4696 if (primary_reselect_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004697 pr_err("Error: Invalid primary_reselect \"%s\"\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00004698 primary_reselect ==
4699 NULL ? "NULL" : primary_reselect);
4700 return -EINVAL;
4701 }
4702 } else {
4703 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
4704 }
4705
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07004706 if (fail_over_mac) {
4707 fail_over_mac_value = bond_parse_parm(fail_over_mac,
4708 fail_over_mac_tbl);
4709 if (fail_over_mac_value == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004710 pr_err("Error: invalid fail_over_mac \"%s\"\n",
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07004711 arp_validate == NULL ? "NULL" : arp_validate);
4712 return -EINVAL;
4713 }
4714
4715 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004716 pr_warning("Warning: fail_over_mac only affects active-backup mode.\n");
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07004717 } else {
4718 fail_over_mac_value = BOND_FOM_NONE;
4719 }
Jay Vosburghdd957c52007-10-09 19:57:24 -07004720
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 /* fill params struct with the proper values */
4722 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004723 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 params->miimon = miimon;
Ben Hutchingsad246c92011-04-26 15:25:52 +00004725 params->num_peer_notif = num_peer_notif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004727 params->arp_validate = arp_validate_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 params->updelay = updelay;
4729 params->downdelay = downdelay;
4730 params->use_carrier = use_carrier;
4731 params->lacp_fast = lacp_fast;
4732 params->primary[0] = 0;
Jiri Pirkoa5499522009-09-25 03:28:09 +00004733 params->primary_reselect = primary_reselect_value;
Jay Vosburgh3915c1e2008-05-17 21:10:14 -07004734 params->fail_over_mac = fail_over_mac_value;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004735 params->tx_queues = tx_queues;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00004736 params->all_slaves_active = all_slaves_active;
Flavio Leitnerc2952c32010-10-05 14:23:59 +00004737 params->resend_igmp = resend_igmp;
stephen hemminger655f8912011-06-22 09:54:39 +00004738 params->min_links = min_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739
4740 if (primary) {
4741 strncpy(params->primary, primary, IFNAMSIZ);
4742 params->primary[IFNAMSIZ - 1] = 0;
4743 }
4744
4745 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
4746
4747 return 0;
4748}
4749
Peter Zijlstra0daa2302006-11-08 19:51:01 -08004750static struct lock_class_key bonding_netdev_xmit_lock_key;
David S. Millercf508b12008-07-22 14:16:42 -07004751static struct lock_class_key bonding_netdev_addr_lock_key;
Peter Zijlstra0daa2302006-11-08 19:51:01 -08004752
David S. Millere8a04642008-07-17 00:34:19 -07004753static void bond_set_lockdep_class_one(struct net_device *dev,
4754 struct netdev_queue *txq,
4755 void *_unused)
David S. Millerc773e842008-07-08 23:13:53 -07004756{
4757 lockdep_set_class(&txq->_xmit_lock,
4758 &bonding_netdev_xmit_lock_key);
4759}
4760
4761static void bond_set_lockdep_class(struct net_device *dev)
4762{
David S. Millercf508b12008-07-22 14:16:42 -07004763 lockdep_set_class(&dev->addr_list_lock,
4764 &bonding_netdev_addr_lock_key);
David S. Millere8a04642008-07-17 00:34:19 -07004765 netdev_for_each_tx_queue(dev, bond_set_lockdep_class_one, NULL);
David S. Millerc773e842008-07-08 23:13:53 -07004766}
4767
Stephen Hemminger181470f2009-06-12 19:02:52 +00004768/*
4769 * Called from registration process
4770 */
4771static int bond_init(struct net_device *bond_dev)
4772{
4773 struct bonding *bond = netdev_priv(bond_dev);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004774 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Neil Horman9fe06172011-05-25 08:13:01 +00004775 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Stephen Hemminger181470f2009-06-12 19:02:52 +00004776
4777 pr_debug("Begin bond_init for %s\n", bond_dev->name);
4778
Neil Horman9fe06172011-05-25 08:13:01 +00004779 /*
4780 * Initialize locks that may be required during
4781 * en/deslave operations. All of the bond_open work
4782 * (of which this is part) should really be moved to
4783 * a phase prior to dev_open
4784 */
4785 spin_lock_init(&(bond_info->tx_hashtbl_lock));
4786 spin_lock_init(&(bond_info->rx_hashtbl_lock));
4787
Stephen Hemminger181470f2009-06-12 19:02:52 +00004788 bond->wq = create_singlethread_workqueue(bond_dev->name);
4789 if (!bond->wq)
4790 return -ENOMEM;
4791
4792 bond_set_lockdep_class(bond_dev);
4793
Stephen Hemminger181470f2009-06-12 19:02:52 +00004794 bond_create_proc_entry(bond);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004795 list_add_tail(&bond->bond_list, &bn->dev_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00004796
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00004797 bond_prepare_sysfs_group(bond);
Jiri Pirko22bedad2010-04-01 21:22:57 +00004798
Taku Izumif073c7c2010-12-09 15:17:13 +00004799 bond_debug_register(bond);
4800
Jiri Pirko22bedad2010-04-01 21:22:57 +00004801 __hw_addr_init(&bond->mc_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00004802 return 0;
4803}
4804
Eric W. Biederman88ead972009-10-29 14:18:25 +00004805static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
4806{
4807 if (tb[IFLA_ADDRESS]) {
4808 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
4809 return -EINVAL;
4810 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
4811 return -EADDRNOTAVAIL;
4812 }
4813 return 0;
4814}
4815
Jiri Pirkod5da4512011-08-10 06:09:44 +00004816static int bond_get_tx_queues(struct net *net, struct nlattr *tb[],
4817 unsigned int *num_queues,
4818 unsigned int *real_num_queues)
4819{
4820 *num_queues = tx_queues;
4821 return 0;
4822}
4823
Eric W. Biederman88ead972009-10-29 14:18:25 +00004824static struct rtnl_link_ops bond_link_ops __read_mostly = {
4825 .kind = "bond",
Eric W. Biederman66391042009-10-29 23:58:54 +00004826 .priv_size = sizeof(struct bonding),
Eric W. Biederman88ead972009-10-29 14:18:25 +00004827 .setup = bond_setup,
4828 .validate = bond_validate,
Jiri Pirkod5da4512011-08-10 06:09:44 +00004829 .get_tx_queues = bond_get_tx_queues,
Eric W. Biederman88ead972009-10-29 14:18:25 +00004830};
4831
Mitch Williamsdfe60392005-11-09 10:36:04 -08004832/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08004833 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08004834 * Caller must NOT hold rtnl_lock; we need to release it here before we
4835 * set up our sysfs entries.
4836 */
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004837int bond_create(struct net *net, const char *name)
Mitch Williamsdfe60392005-11-09 10:36:04 -08004838{
4839 struct net_device *bond_dev;
4840 int res;
4841
4842 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08004843
Jiri Pirko1c5cae82011-04-30 01:21:32 +00004844 bond_dev = alloc_netdev_mq(sizeof(struct bonding),
4845 name ? name : "bond%d",
4846 bond_setup, tx_queues);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004847 if (!bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004848 pr_err("%s: eek! can't alloc netdev!\n", name);
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004849 rtnl_unlock();
4850 return -ENOMEM;
Mitch Williamsdfe60392005-11-09 10:36:04 -08004851 }
4852
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004853 dev_net_set(bond_dev, net);
Eric W. Biederman88ead972009-10-29 14:18:25 +00004854 bond_dev->rtnl_link_ops = &bond_link_ops;
4855
Mitch Williamsdfe60392005-11-09 10:36:04 -08004856 res = register_netdevice(bond_dev);
Peter Zijlstra0daa2302006-11-08 19:51:01 -08004857
Phil Oestere826eaf2011-03-14 06:22:05 +00004858 netif_carrier_off(bond_dev);
4859
Mitch Williamsdfe60392005-11-09 10:36:04 -08004860 rtnl_unlock();
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004861 if (res < 0)
4862 bond_destructor(bond_dev);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004863 return res;
4864}
4865
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004866static int __net_init bond_net_init(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004867{
Eric W. Biederman15449742009-11-29 15:46:04 +00004868 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004869
4870 bn->net = net;
4871 INIT_LIST_HEAD(&bn->dev_list);
4872
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004873 bond_create_proc_dir(bn);
Eric W. Biederman4c224002011-10-12 21:56:25 +00004874 bond_create_sysfs(bn);
Eric W. Biederman15449742009-11-29 15:46:04 +00004875
4876 return 0;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004877}
4878
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004879static void __net_exit bond_net_exit(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004880{
Eric W. Biederman15449742009-11-29 15:46:04 +00004881 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004882
Eric W. Biederman4c224002011-10-12 21:56:25 +00004883 bond_destroy_sysfs(bn);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004884 bond_destroy_proc_dir(bn);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004885}
4886
4887static struct pernet_operations bond_net_ops = {
4888 .init = bond_net_init,
4889 .exit = bond_net_exit,
Eric W. Biederman15449742009-11-29 15:46:04 +00004890 .id = &bond_net_id,
4891 .size = sizeof(struct bond_net),
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004892};
4893
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894static int __init bonding_init(void)
4895{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 int i;
4897 int res;
4898
Amerigo Wangbd33acc2011-03-06 21:58:46 +00004899 pr_info("%s", bond_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
Mitch Williamsdfe60392005-11-09 10:36:04 -08004901 res = bond_check_params(&bonding_defaults);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004902 if (res)
Mitch Williamsdfe60392005-11-09 10:36:04 -08004903 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904
Eric W. Biederman15449742009-11-29 15:46:04 +00004905 res = register_pernet_subsys(&bond_net_ops);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004906 if (res)
4907 goto out;
Jay Vosburgh027ea042008-01-17 16:25:02 -08004908
Eric W. Biederman88ead972009-10-29 14:18:25 +00004909 res = rtnl_link_register(&bond_link_ops);
4910 if (res)
Eric W. Biederman66391042009-10-29 23:58:54 +00004911 goto err_link;
Eric W. Biederman88ead972009-10-29 14:18:25 +00004912
Taku Izumif073c7c2010-12-09 15:17:13 +00004913 bond_create_debugfs();
4914
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 for (i = 0; i < max_bonds; i++) {
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00004916 res = bond_create(&init_net, NULL);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004917 if (res)
4918 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 }
4920
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 register_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04004922 register_inetaddr_notifier(&bond_inetaddr_notifier);
Mitch Williamsdfe60392005-11-09 10:36:04 -08004923out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 return res;
Eric W. Biederman88ead972009-10-29 14:18:25 +00004925err:
4926 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman66391042009-10-29 23:58:54 +00004927err_link:
Eric W. Biederman15449742009-11-29 15:46:04 +00004928 unregister_pernet_subsys(&bond_net_ops);
Eric W. Biederman88ead972009-10-29 14:18:25 +00004929 goto out;
Mitch Williamsdfe60392005-11-09 10:36:04 -08004930
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931}
4932
4933static void __exit bonding_exit(void)
4934{
4935 unregister_netdevice_notifier(&bond_netdev_notifier);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04004936 unregister_inetaddr_notifier(&bond_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
Taku Izumif073c7c2010-12-09 15:17:13 +00004938 bond_destroy_debugfs();
Pavel Emelyanovae68c392008-05-02 17:49:39 -07004939
Eric W. Biederman88ead972009-10-29 14:18:25 +00004940 rtnl_link_unregister(&bond_link_ops);
Eric W. Biederman15449742009-11-29 15:46:04 +00004941 unregister_pernet_subsys(&bond_net_ops);
Neil Hormane843fa52010-10-13 16:01:50 +00004942
4943#ifdef CONFIG_NET_POLL_CONTROLLER
Neil Hormanfb4fa762010-12-06 09:05:50 +00004944 /*
4945 * Make sure we don't have an imbalance on our netpoll blocking
4946 */
4947 WARN_ON(atomic_read(&netpoll_block_tx));
Neil Hormane843fa52010-10-13 16:01:50 +00004948#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949}
4950
4951module_init(bonding_init);
4952module_exit(bonding_exit);
4953MODULE_LICENSE("GPL");
4954MODULE_VERSION(DRV_VERSION);
4955MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
4956MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
Eric W. Biederman88ead972009-10-29 14:18:25 +00004957MODULE_ALIAS_RTNL_LINK("bond");