blob: b8bec086daa155b2c6f36884a059f4749f5636fd [file] [log] [blame]
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001/*
2 * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
Mitch Williamsb76cdba2005-11-09 10:36:41 -080021 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -080022
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Mitch Williamsb76cdba2005-11-09 10:36:41 -080025#include <linux/kernel.h>
26#include <linux/module.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080027#include <linux/device.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040028#include <linux/sched.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080029#include <linux/sysdev.h>
30#include <linux/fs.h>
31#include <linux/types.h>
32#include <linux/string.h>
33#include <linux/netdevice.h>
34#include <linux/inetdevice.h>
35#include <linux/in.h>
36#include <linux/sysfs.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080037#include <linux/ctype.h>
38#include <linux/inet.h>
39#include <linux/rtnetlink.h>
Stephen Hemminger5c5129b2009-06-12 19:02:51 +000040#include <linux/etherdevice.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070041#include <net/net_namespace.h>
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000042#include <net/netns/generic.h>
43#include <linux/nsproxy.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080044
Mitch Williamsb76cdba2005-11-09 10:36:41 -080045#include "bonding.h"
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -080046
Stephen Hemminger3d632c32009-06-12 19:02:48 +000047#define to_dev(obj) container_of(obj, struct device, kobj)
Wang Chen454d7c92008-11-12 23:37:49 -080048#define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
Mitch Williamsb76cdba2005-11-09 10:36:41 -080049
Mitch Williamsb76cdba2005-11-09 10:36:41 -080050/*
51 * "show" function for the bond_masters attribute.
52 * The class parameter is ignored.
53 */
Andi Kleen28812fe2010-01-05 12:48:07 +010054static ssize_t bonding_show_bonds(struct class *cls,
55 struct class_attribute *attr,
56 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080057{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000058 struct net *net = current->nsproxy->net_ns;
59 struct bond_net *bn = net_generic(net, bond_net_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080060 int res = 0;
61 struct bonding *bond;
62
Stephen Hemminger7e083842009-06-12 19:02:46 +000063 rtnl_lock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080064
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000065 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -080066 if (res > (PAGE_SIZE - IFNAMSIZ)) {
67 /* not enough space for another interface name */
68 if ((PAGE_SIZE - res) > 10)
69 res = PAGE_SIZE - 10;
Wagner Ferencb8843662007-12-06 23:40:30 -080070 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -080071 break;
72 }
Wagner Ferencb8843662007-12-06 23:40:30 -080073 res += sprintf(buf + res, "%s ", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080074 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -080075 if (res)
76 buf[res-1] = '\n'; /* eat the leftover space */
Stephen Hemminger7e083842009-06-12 19:02:46 +000077
78 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080079 return res;
80}
81
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000082static struct net_device *bond_get_by_name(struct net *net, const char *ifname)
Stephen Hemminger373500d2009-06-12 19:02:50 +000083{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000084 struct bond_net *bn = net_generic(net, bond_net_id);
Stephen Hemminger373500d2009-06-12 19:02:50 +000085 struct bonding *bond;
86
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000087 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Stephen Hemminger373500d2009-06-12 19:02:50 +000088 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
89 return bond->dev;
90 }
91 return NULL;
92}
93
Mitch Williamsb76cdba2005-11-09 10:36:41 -080094/*
95 * "store" function for the bond_masters attribute. This is what
96 * creates and deletes entire bonds.
97 *
98 * The class parameter is ignored.
99 *
100 */
101
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000102static ssize_t bonding_store_bonds(struct class *cls,
Andi Kleen28812fe2010-01-05 12:48:07 +0100103 struct class_attribute *attr,
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000104 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800105{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +0000106 struct net *net = current->nsproxy->net_ns;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800107 char command[IFNAMSIZ + 1] = {0, };
108 char *ifname;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800109 int rv, res = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800110
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800111 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
112 ifname = command + 1;
113 if ((strlen(command) <= 1) ||
114 !dev_valid_name(ifname))
115 goto err_no_cmd;
116
117 if (command[0] == '+') {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800118 pr_info("%s is being created...\n", ifname);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +0000119 rv = bond_create(net, ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800120 if (rv) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800121 pr_info("Bond creation failed.\n");
Jay Vosburgh027ea042008-01-17 16:25:02 -0800122 res = rv;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800123 }
Stephen Hemminger373500d2009-06-12 19:02:50 +0000124 } else if (command[0] == '-') {
125 struct net_device *bond_dev;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800126
Jay Vosburgh027ea042008-01-17 16:25:02 -0800127 rtnl_lock();
Eric W. Biedermanec87fd32009-10-29 14:18:26 +0000128 bond_dev = bond_get_by_name(net, ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000129 if (bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800130 pr_info("%s is being deleted...\n", ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000131 unregister_netdevice(bond_dev);
132 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800133 pr_err("unable to delete non-existent %s\n", ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000134 res = -ENODEV;
135 }
136 rtnl_unlock();
137 } else
138 goto err_no_cmd;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800139
Stephen Hemminger373500d2009-06-12 19:02:50 +0000140 /* Always return either count or an error. If you return 0, you'll
141 * get called forever, which is bad.
142 */
143 return res;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800144
145err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800146 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700147 return -EPERM;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800148}
Stephen Hemminger373500d2009-06-12 19:02:50 +0000149
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800150/* class attribute for bond_masters file. This ends up in /sys/class/net */
151static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
152 bonding_show_bonds, bonding_store_bonds);
153
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000154int bond_create_slave_symlinks(struct net_device *master,
155 struct net_device *slave)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800156{
157 char linkname[IFNAMSIZ+7];
158 int ret = 0;
159
160 /* first, create a link from the slave back to the master */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700161 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800162 "master");
163 if (ret)
164 return ret;
165 /* next, create a link from the master to the slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000166 sprintf(linkname, "slave_%s", slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700167 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800168 linkname);
169 return ret;
170
171}
172
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000173void bond_destroy_slave_symlinks(struct net_device *master,
174 struct net_device *slave)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800175{
176 char linkname[IFNAMSIZ+7];
177
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700178 sysfs_remove_link(&(slave->dev.kobj), "master");
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000179 sprintf(linkname, "slave_%s", slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700180 sysfs_remove_link(&(master->dev.kobj), linkname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800181}
182
183
184/*
185 * Show the slaves in the current bond.
186 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700187static ssize_t bonding_show_slaves(struct device *d,
188 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800189{
190 struct slave *slave;
191 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700192 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800193
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700194 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800195 bond_for_each_slave(bond, slave, i) {
196 if (res > (PAGE_SIZE - IFNAMSIZ)) {
197 /* not enough space for another interface name */
198 if ((PAGE_SIZE - res) > 10)
199 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800200 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800201 break;
202 }
203 res += sprintf(buf + res, "%s ", slave->dev->name);
204 }
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700205 read_unlock(&bond->lock);
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800206 if (res)
207 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800208 return res;
209}
210
211/*
212 * Set the slaves in the current bond. The bond interface must be
213 * up for this to succeed.
214 * This function is largely the same flow as bonding_update_bonds().
215 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700216static ssize_t bonding_store_slaves(struct device *d,
217 struct device_attribute *attr,
218 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800219{
220 char command[IFNAMSIZ + 1] = { 0, };
221 char *ifname;
222 int i, res, found, ret = count;
Moni Shoua3158bf72007-10-09 19:43:41 -0700223 u32 original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800224 struct slave *slave;
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -0800225 struct net_device *dev = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700226 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800227
228 /* Quick sanity check -- is the bond interface up? */
229 if (!(bond->dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800230 pr_warning("%s: doing slave updates when interface is down.\n",
231 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800232 }
233
234 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
235
Eric W. Biederman496a60c2009-05-13 17:02:50 +0000236 if (!rtnl_trylock())
237 return restart_syscall();
Jay Vosburgh027ea042008-01-17 16:25:02 -0800238
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800239 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
240 ifname = command + 1;
241 if ((strlen(command) <= 1) ||
242 !dev_valid_name(ifname))
243 goto err_no_cmd;
244
245 if (command[0] == '+') {
246
247 /* Got a slave name in ifname. Is it already in the list? */
248 found = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800249
Eric W. Biedermanec87fd32009-10-29 14:18:26 +0000250 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800251 if (!dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800252 pr_info("%s: Interface %s does not exist!\n",
253 bond->dev->name, ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000254 ret = -ENODEV;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800255 goto out;
Stephen Hemminger373500d2009-06-12 19:02:50 +0000256 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800257
258 if (dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800259 pr_err("%s: Error: Unable to enslave %s because it is already up.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800260 bond->dev->name, dev->name);
261 ret = -EPERM;
262 goto out;
263 }
Stephen Hemminger373500d2009-06-12 19:02:50 +0000264
265 read_lock(&bond->lock);
266 bond_for_each_slave(bond, slave, i)
267 if (slave->dev == dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800268 pr_err("%s: Interface %s is already enslaved!\n",
Stephen Hemminger373500d2009-06-12 19:02:50 +0000269 bond->dev->name, ifname);
270 ret = -EPERM;
271 read_unlock(&bond->lock);
272 goto out;
273 }
274 read_unlock(&bond->lock);
275
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800276 pr_info("%s: Adding slave %s.\n", bond->dev->name, ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000277
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800278 /* If this is the first slave, then we need to set
279 the master's hardware address to be the same as the
280 slave's. */
Stephen Hemminger5c5129b2009-06-12 19:02:51 +0000281 if (is_zero_ether_addr(bond->dev->dev_addr))
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800282 memcpy(bond->dev->dev_addr, dev->dev_addr,
283 dev->addr_len);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800284
285 /* Set the slave's MTU to match the bond */
Moni Shoua3158bf72007-10-09 19:43:41 -0700286 original_mtu = dev->mtu;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800287 res = dev_set_mtu(dev, bond->dev->mtu);
288 if (res) {
289 ret = res;
290 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800291 }
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800292
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800293 res = bond_enslave(bond->dev, dev);
Moni Shoua3158bf72007-10-09 19:43:41 -0700294 bond_for_each_slave(bond, slave, i)
295 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
296 slave->original_mtu = original_mtu;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000297 if (res)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800298 ret = res;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000299
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800300 goto out;
301 }
302
303 if (command[0] == '-') {
304 dev = NULL;
David S. Miller6952d8922008-03-28 16:15:38 -0700305 original_mtu = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800306 bond_for_each_slave(bond, slave, i)
307 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
308 dev = slave->dev;
Moni Shoua3158bf72007-10-09 19:43:41 -0700309 original_mtu = slave->original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800310 break;
311 }
312 if (dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800313 pr_info("%s: Removing slave %s\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800314 bond->dev->name, dev->name);
Moni Shouad90a1622007-10-09 19:43:43 -0700315 res = bond_release(bond->dev, dev);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800316 if (res) {
317 ret = res;
318 goto out;
319 }
320 /* set the slave MTU to the default */
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800321 dev_set_mtu(dev, original_mtu);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000322 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800323 pr_err("unable to remove non-existent slave %s for bond %s.\n",
324 ifname, bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800325 ret = -ENODEV;
326 }
327 goto out;
328 }
329
330err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800331 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
332 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800333 ret = -EPERM;
334
335out:
Jay Vosburgh027ea042008-01-17 16:25:02 -0800336 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800337 return ret;
338}
339
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000340static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
341 bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800342
343/*
344 * Show and set the bonding mode. The bond interface must be down to
345 * change the mode.
346 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700347static ssize_t bonding_show_mode(struct device *d,
348 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800349{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700350 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800351
352 return sprintf(buf, "%s %d\n",
353 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800354 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800355}
356
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700357static ssize_t bonding_store_mode(struct device *d,
358 struct device_attribute *attr,
359 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800360{
361 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700362 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800363
364 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800365 pr_err("unable to update mode of %s because interface is up.\n",
366 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800367 ret = -EPERM;
368 goto out;
369 }
370
Jay Vosburghece95f72008-01-17 16:25:01 -0800371 new_value = bond_parse_parm(buf, bond_mode_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800372 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800373 pr_err("%s: Ignoring invalid mode value %.*s.\n",
374 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800375 ret = -EINVAL;
376 goto out;
377 } else {
Jay Vosburgh8f903c72006-02-21 16:36:44 -0800378 if (bond->params.mode == BOND_MODE_8023AD)
379 bond_unset_master_3ad_flags(bond);
380
381 if (bond->params.mode == BOND_MODE_ALB)
382 bond_unset_master_alb_flags(bond);
383
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800384 bond->params.mode = new_value;
385 bond_set_mode_ops(bond, bond->params.mode);
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800386 pr_info("%s: setting mode to %s (%d).\n",
387 bond->dev->name, bond_mode_tbl[new_value].modename,
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000388 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800389 }
390out:
391 return ret;
392}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000393static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
394 bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800395
396/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000397 * Show and set the bonding transmit hash method.
398 * The bond interface must be down to change the xmit hash policy.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800399 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700400static ssize_t bonding_show_xmit_hash(struct device *d,
401 struct device_attribute *attr,
402 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800403{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700404 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800405
Wagner Ferenc8e4b9322007-12-06 23:40:32 -0800406 return sprintf(buf, "%s %d\n",
407 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
408 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800409}
410
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700411static ssize_t bonding_store_xmit_hash(struct device *d,
412 struct device_attribute *attr,
413 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800414{
415 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700416 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800417
418 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800419 pr_err("%s: Interface is up. Unable to update xmit policy.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800420 bond->dev->name);
421 ret = -EPERM;
422 goto out;
423 }
424
Jay Vosburghece95f72008-01-17 16:25:01 -0800425 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800426 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800427 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800428 bond->dev->name,
429 (int)strlen(buf) - 1, buf);
430 ret = -EINVAL;
431 goto out;
432 } else {
433 bond->params.xmit_policy = new_value;
434 bond_set_mode_ops(bond, bond->params.mode);
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800435 pr_info("%s: setting xmit hash policy to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000436 bond->dev->name,
437 xmit_hashtype_tbl[new_value].modename, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800438 }
439out:
440 return ret;
441}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000442static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
443 bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800444
445/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700446 * Show and set arp_validate.
447 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700448static ssize_t bonding_show_arp_validate(struct device *d,
449 struct device_attribute *attr,
450 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700451{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700452 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700453
454 return sprintf(buf, "%s %d\n",
455 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800456 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700457}
458
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700459static ssize_t bonding_store_arp_validate(struct device *d,
460 struct device_attribute *attr,
461 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700462{
463 int new_value;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700464 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700465
Jay Vosburghece95f72008-01-17 16:25:01 -0800466 new_value = bond_parse_parm(buf, arp_validate_tbl);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700467 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800468 pr_err("%s: Ignoring invalid arp_validate value %s\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700469 bond->dev->name, buf);
470 return -EINVAL;
471 }
472 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800473 pr_err("%s: arp_validate only supported in active-backup mode.\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700474 bond->dev->name);
475 return -EINVAL;
476 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800477 pr_info("%s: setting arp_validate to %s (%d).\n",
478 bond->dev->name, arp_validate_tbl[new_value].modename,
479 new_value);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700480
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000481 if (!bond->params.arp_validate && new_value)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700482 bond_register_arp(bond);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000483 else if (bond->params.arp_validate && !new_value)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700484 bond_unregister_arp(bond);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700485
486 bond->params.arp_validate = new_value;
487
488 return count;
489}
490
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000491static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
492 bonding_store_arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700493
494/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700495 * Show and store fail_over_mac. User only allowed to change the
496 * value when there are no slaves.
497 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000498static ssize_t bonding_show_fail_over_mac(struct device *d,
499 struct device_attribute *attr,
500 char *buf)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700501{
502 struct bonding *bond = to_bond(d);
503
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700504 return sprintf(buf, "%s %d\n",
505 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
506 bond->params.fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700507}
508
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000509static ssize_t bonding_store_fail_over_mac(struct device *d,
510 struct device_attribute *attr,
511 const char *buf, size_t count)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700512{
513 int new_value;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700514 struct bonding *bond = to_bond(d);
515
516 if (bond->slave_cnt != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800517 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
Jay Vosburghdd957c52007-10-09 19:57:24 -0700518 bond->dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700519 return -EPERM;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700520 }
521
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700522 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
523 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800524 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700525 bond->dev->name, buf);
526 return -EINVAL;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700527 }
528
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700529 bond->params.fail_over_mac = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800530 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
531 bond->dev->name, fail_over_mac_tbl[new_value].modename,
532 new_value);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700533
534 return count;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700535}
536
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000537static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
538 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700539
540/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800541 * Show and set the arp timer interval. There are two tricky bits
542 * here. First, if ARP monitoring is activated, then we must disable
543 * MII monitoring. Second, if the ARP timer isn't running, we must
544 * start it.
545 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700546static ssize_t bonding_show_arp_interval(struct device *d,
547 struct device_attribute *attr,
548 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800549{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700550 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800551
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800552 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800553}
554
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700555static ssize_t bonding_store_arp_interval(struct device *d,
556 struct device_attribute *attr,
557 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800558{
559 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700560 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800561
562 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800563 pr_err("%s: no arp_interval value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800564 bond->dev->name);
565 ret = -EINVAL;
566 goto out;
567 }
568 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800569 pr_err("%s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800570 bond->dev->name, new_value, INT_MAX);
571 ret = -EINVAL;
572 goto out;
573 }
574
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800575 pr_info("%s: Setting ARP monitoring interval to %d.\n",
576 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800577 bond->params.arp_interval = new_value;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -0800578 if (bond->params.arp_interval)
579 bond->dev->priv_flags |= IFF_MASTER_ARPMON;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800580 if (bond->params.miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800581 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
582 bond->dev->name, bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800583 bond->params.miimon = 0;
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700584 if (delayed_work_pending(&bond->mii_work)) {
585 cancel_delayed_work(&bond->mii_work);
586 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800587 }
588 }
589 if (!bond->params.arp_targets[0]) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800590 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
591 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800592 }
593 if (bond->dev->flags & IFF_UP) {
594 /* If the interface is up, we may need to fire off
595 * the ARP timer. If the interface is down, the
596 * timer will get fired off when the open function
597 * is called.
598 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700599 if (!delayed_work_pending(&bond->arp_work)) {
600 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
601 INIT_DELAYED_WORK(&bond->arp_work,
602 bond_activebackup_arp_mon);
603 else
604 INIT_DELAYED_WORK(&bond->arp_work,
605 bond_loadbalance_arp_mon);
606
607 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800608 }
609 }
610
611out:
612 return ret;
613}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000614static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
615 bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800616
617/*
618 * Show and set the arp targets.
619 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700620static ssize_t bonding_show_arp_targets(struct device *d,
621 struct device_attribute *attr,
622 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800623{
624 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700625 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800626
627 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
628 if (bond->params.arp_targets[i])
Harvey Harrison63779432008-10-31 00:56:00 -0700629 res += sprintf(buf + res, "%pI4 ",
630 &bond->params.arp_targets[i]);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800631 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800632 if (res)
633 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800634 return res;
635}
636
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700637static ssize_t bonding_store_arp_targets(struct device *d,
638 struct device_attribute *attr,
639 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800640{
Al Virod3bb52b2007-08-22 20:06:58 -0400641 __be32 newtarget;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800642 int i = 0, done = 0, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700643 struct bonding *bond = to_bond(d);
Al Virod3bb52b2007-08-22 20:06:58 -0400644 __be32 *targets;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800645
646 targets = bond->params.arp_targets;
647 newtarget = in_aton(buf + 1);
648 /* look for adds */
649 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400650 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800651 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700652 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800653 ret = -EINVAL;
654 goto out;
655 }
656 /* look for an empty slot to put the target in, and check for dupes */
Brian Haley5a31bec2009-04-13 00:11:30 -0700657 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800658 if (targets[i] == newtarget) { /* duplicate */
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800659 pr_err("%s: ARP target %pI4 is already present\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700660 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800661 ret = -EINVAL;
662 goto out;
663 }
Brian Haley5a31bec2009-04-13 00:11:30 -0700664 if (targets[i] == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800665 pr_info("%s: adding ARP target %pI4.\n",
666 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800667 done = 1;
668 targets[i] = newtarget;
669 }
670 }
671 if (!done) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800672 pr_err("%s: ARP target table is full!\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800673 bond->dev->name);
674 ret = -EINVAL;
675 goto out;
676 }
677
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000678 } else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400679 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800680 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700681 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800682 ret = -EINVAL;
683 goto out;
684 }
685
Brian Haley5a31bec2009-04-13 00:11:30 -0700686 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800687 if (targets[i] == newtarget) {
Brian Haley5a31bec2009-04-13 00:11:30 -0700688 int j;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800689 pr_info("%s: removing ARP target %pI4.\n",
690 bond->dev->name, &newtarget);
Brian Haley5a31bec2009-04-13 00:11:30 -0700691 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
692 targets[j] = targets[j+1];
693
694 targets[j] = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800695 done = 1;
696 }
697 }
698 if (!done) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800699 pr_info("%s: unable to remove nonexistent ARP target %pI4.\n",
700 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800701 ret = -EINVAL;
702 goto out;
703 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000704 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800705 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
706 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800707 ret = -EPERM;
708 goto out;
709 }
710
711out:
712 return ret;
713}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700714static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800715
716/*
717 * Show and set the up and down delays. These must be multiples of the
718 * MII monitoring value, and are stored internally as the multiplier.
719 * Thus, we must translate to MS for the real world.
720 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700721static ssize_t bonding_show_downdelay(struct device *d,
722 struct device_attribute *attr,
723 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800724{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700725 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800726
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800727 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800728}
729
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700730static ssize_t bonding_store_downdelay(struct device *d,
731 struct device_attribute *attr,
732 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800733{
734 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700735 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800736
737 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800738 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800739 bond->dev->name);
740 ret = -EPERM;
741 goto out;
742 }
743
744 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800745 pr_err("%s: no down delay value specified.\n", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800746 ret = -EINVAL;
747 goto out;
748 }
749 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800750 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800751 bond->dev->name, new_value, 1, INT_MAX);
752 ret = -EINVAL;
753 goto out;
754 } else {
755 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800756 pr_warning("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +0000757 bond->dev->name, new_value,
758 bond->params.miimon,
759 (new_value / bond->params.miimon) *
760 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800761 }
762 bond->params.downdelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800763 pr_info("%s: Setting down delay to %d.\n",
764 bond->dev->name,
765 bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800766
767 }
768
769out:
770 return ret;
771}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000772static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
773 bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800774
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700775static ssize_t bonding_show_updelay(struct device *d,
776 struct device_attribute *attr,
777 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800778{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700779 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800780
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800781 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800782
783}
784
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700785static ssize_t bonding_store_updelay(struct device *d,
786 struct device_attribute *attr,
787 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800788{
789 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700790 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800791
792 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800793 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800794 bond->dev->name);
795 ret = -EPERM;
796 goto out;
797 }
798
799 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800800 pr_err("%s: no up delay value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800801 bond->dev->name);
802 ret = -EINVAL;
803 goto out;
804 }
805 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800806 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800807 bond->dev->name, new_value, 1, INT_MAX);
808 ret = -EINVAL;
809 goto out;
810 } else {
811 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800812 pr_warning("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +0000813 bond->dev->name, new_value,
814 bond->params.miimon,
815 (new_value / bond->params.miimon) *
816 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800817 }
818 bond->params.updelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800819 pr_info("%s: Setting up delay to %d.\n",
820 bond->dev->name,
821 bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800822 }
823
824out:
825 return ret;
826}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000827static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
828 bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800829
830/*
831 * Show and set the LACP interval. Interface must be down, and the mode
832 * must be set to 802.3ad mode.
833 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700834static ssize_t bonding_show_lacp(struct device *d,
835 struct device_attribute *attr,
836 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800837{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700838 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800839
840 return sprintf(buf, "%s %d\n",
841 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800842 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800843}
844
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700845static ssize_t bonding_store_lacp(struct device *d,
846 struct device_attribute *attr,
847 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800848{
849 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700850 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800851
852 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800853 pr_err("%s: Unable to update LACP rate because interface is up.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800854 bond->dev->name);
855 ret = -EPERM;
856 goto out;
857 }
858
859 if (bond->params.mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800860 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800861 bond->dev->name);
862 ret = -EPERM;
863 goto out;
864 }
865
Jay Vosburghece95f72008-01-17 16:25:01 -0800866 new_value = bond_parse_parm(buf, bond_lacp_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800867
868 if ((new_value == 1) || (new_value == 0)) {
869 bond->params.lacp_fast = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800870 pr_info("%s: Setting LACP rate to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000871 bond->dev->name, bond_lacp_tbl[new_value].modename,
872 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800873 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800874 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000875 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800876 ret = -EINVAL;
877 }
878out:
879 return ret;
880}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000881static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
882 bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800883
Jay Vosburghfd989c82008-11-04 17:51:16 -0800884static ssize_t bonding_show_ad_select(struct device *d,
885 struct device_attribute *attr,
886 char *buf)
887{
888 struct bonding *bond = to_bond(d);
889
890 return sprintf(buf, "%s %d\n",
891 ad_select_tbl[bond->params.ad_select].modename,
892 bond->params.ad_select);
893}
894
895
896static ssize_t bonding_store_ad_select(struct device *d,
897 struct device_attribute *attr,
898 const char *buf, size_t count)
899{
900 int new_value, ret = count;
901 struct bonding *bond = to_bond(d);
902
903 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800904 pr_err("%s: Unable to update ad_select because interface is up.\n",
905 bond->dev->name);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800906 ret = -EPERM;
907 goto out;
908 }
909
910 new_value = bond_parse_parm(buf, ad_select_tbl);
911
912 if (new_value != -1) {
913 bond->params.ad_select = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800914 pr_info("%s: Setting ad_select to %s (%d).\n",
915 bond->dev->name, ad_select_tbl[new_value].modename,
916 new_value);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800917 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800918 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -0800919 bond->dev->name, (int)strlen(buf) - 1, buf);
920 ret = -EINVAL;
921 }
922out:
923 return ret;
924}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000925static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
926 bonding_show_ad_select, bonding_store_ad_select);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800927
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800928/*
Moni Shoua7893b242008-05-17 21:10:12 -0700929 * Show and set the number of grat ARP to send after a failover event.
930 */
931static ssize_t bonding_show_n_grat_arp(struct device *d,
932 struct device_attribute *attr,
933 char *buf)
934{
935 struct bonding *bond = to_bond(d);
936
937 return sprintf(buf, "%d\n", bond->params.num_grat_arp);
938}
939
940static ssize_t bonding_store_n_grat_arp(struct device *d,
941 struct device_attribute *attr,
942 const char *buf, size_t count)
943{
944 int new_value, ret = count;
945 struct bonding *bond = to_bond(d);
946
947 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800948 pr_err("%s: no num_grat_arp value specified.\n",
Moni Shoua7893b242008-05-17 21:10:12 -0700949 bond->dev->name);
950 ret = -EINVAL;
951 goto out;
952 }
953 if (new_value < 0 || new_value > 255) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800954 pr_err("%s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
Moni Shoua7893b242008-05-17 21:10:12 -0700955 bond->dev->name, new_value);
956 ret = -EINVAL;
957 goto out;
958 } else {
959 bond->params.num_grat_arp = new_value;
960 }
961out:
962 return ret;
963}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000964static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
965 bonding_show_n_grat_arp, bonding_store_n_grat_arp);
Brian Haley305d5522008-11-04 17:51:14 -0800966
967/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000968 * Show and set the number of unsolicited NA's to send after a failover event.
Brian Haley305d5522008-11-04 17:51:14 -0800969 */
970static ssize_t bonding_show_n_unsol_na(struct device *d,
971 struct device_attribute *attr,
972 char *buf)
973{
974 struct bonding *bond = to_bond(d);
975
976 return sprintf(buf, "%d\n", bond->params.num_unsol_na);
977}
978
979static ssize_t bonding_store_n_unsol_na(struct device *d,
980 struct device_attribute *attr,
981 const char *buf, size_t count)
982{
983 int new_value, ret = count;
984 struct bonding *bond = to_bond(d);
985
986 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800987 pr_err("%s: no num_unsol_na value specified.\n",
Brian Haley305d5522008-11-04 17:51:14 -0800988 bond->dev->name);
989 ret = -EINVAL;
990 goto out;
991 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000992
Brian Haley305d5522008-11-04 17:51:14 -0800993 if (new_value < 0 || new_value > 255) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800994 pr_err("%s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
Brian Haley305d5522008-11-04 17:51:14 -0800995 bond->dev->name, new_value);
996 ret = -EINVAL;
997 goto out;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000998 } else
Brian Haley305d5522008-11-04 17:51:14 -0800999 bond->params.num_unsol_na = new_value;
Brian Haley305d5522008-11-04 17:51:14 -08001000out:
1001 return ret;
1002}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001003static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
1004 bonding_show_n_unsol_na, bonding_store_n_unsol_na);
Brian Haley305d5522008-11-04 17:51:14 -08001005
Moni Shoua7893b242008-05-17 21:10:12 -07001006/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001007 * Show and set the MII monitor interval. There are two tricky bits
1008 * here. First, if MII monitoring is activated, then we must disable
1009 * ARP monitoring. Second, if the timer isn't running, we must
1010 * start it.
1011 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001012static ssize_t bonding_show_miimon(struct device *d,
1013 struct device_attribute *attr,
1014 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001015{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001016 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001017
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001018 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001019}
1020
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001021static ssize_t bonding_store_miimon(struct device *d,
1022 struct device_attribute *attr,
1023 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001024{
1025 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001026 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001027
1028 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001029 pr_err("%s: no miimon value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001030 bond->dev->name);
1031 ret = -EINVAL;
1032 goto out;
1033 }
1034 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001035 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001036 bond->dev->name, new_value, 1, INT_MAX);
1037 ret = -EINVAL;
1038 goto out;
1039 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001040 pr_info("%s: Setting MII monitoring interval to %d.\n",
1041 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001042 bond->params.miimon = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001043 if (bond->params.updelay)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001044 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
1045 bond->dev->name,
1046 bond->params.updelay * bond->params.miimon);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001047 if (bond->params.downdelay)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001048 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
1049 bond->dev->name,
1050 bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001051 if (bond->params.arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001052 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
1053 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001054 bond->params.arp_interval = 0;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -08001055 bond->dev->priv_flags &= ~IFF_MASTER_ARPMON;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001056 if (bond->params.arp_validate) {
1057 bond_unregister_arp(bond);
1058 bond->params.arp_validate =
1059 BOND_ARP_VALIDATE_NONE;
1060 }
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001061 if (delayed_work_pending(&bond->arp_work)) {
1062 cancel_delayed_work(&bond->arp_work);
1063 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001064 }
1065 }
1066
1067 if (bond->dev->flags & IFF_UP) {
1068 /* If the interface is up, we may need to fire off
1069 * the MII timer. If the interface is down, the
1070 * timer will get fired off when the open function
1071 * is called.
1072 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001073 if (!delayed_work_pending(&bond->mii_work)) {
1074 INIT_DELAYED_WORK(&bond->mii_work,
1075 bond_mii_monitor);
1076 queue_delayed_work(bond->wq,
1077 &bond->mii_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001078 }
1079 }
1080 }
1081out:
1082 return ret;
1083}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001084static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1085 bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001086
1087/*
1088 * Show and set the primary slave. The store function is much
1089 * simpler than bonding_store_slaves function because it only needs to
1090 * handle one interface name.
1091 * The bond must be a mode that supports a primary for this be
1092 * set.
1093 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001094static ssize_t bonding_show_primary(struct device *d,
1095 struct device_attribute *attr,
1096 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001097{
1098 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001099 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001100
1101 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001102 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001103
1104 return count;
1105}
1106
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001107static ssize_t bonding_store_primary(struct device *d,
1108 struct device_attribute *attr,
1109 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001110{
1111 int i;
1112 struct slave *slave;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001113 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001114
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001115 if (!rtnl_trylock())
1116 return restart_syscall();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001117 read_lock(&bond->lock);
1118 write_lock_bh(&bond->curr_slave_lock);
1119
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001120 if (!USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001121 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1122 bond->dev->name, bond->dev->name, bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001123 } else {
1124 bond_for_each_slave(bond, slave, i) {
1125 if (strnicmp
1126 (slave->dev->name, buf,
1127 strlen(slave->dev->name)) == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001128 pr_info("%s: Setting %s as primary slave.\n",
1129 bond->dev->name, slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001130 bond->primary_slave = slave;
Jiri Pirkoce501ca2009-09-18 02:13:22 +00001131 strcpy(bond->params.primary, slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001132 bond_select_active_slave(bond);
1133 goto out;
1134 }
1135 }
1136
1137 /* if we got here, then we didn't match the name of any slave */
1138
1139 if (strlen(buf) == 0 || buf[0] == '\n') {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001140 pr_info("%s: Setting primary slave to None.\n",
1141 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001142 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001143 bond_select_active_slave(bond);
1144 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001145 pr_info("%s: Unable to set %.*s as primary slave as it is not a slave.\n",
1146 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001147 }
1148 }
1149out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001150 write_unlock_bh(&bond->curr_slave_lock);
1151 read_unlock(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001152 rtnl_unlock();
1153
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001154 return count;
1155}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001156static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1157 bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001158
1159/*
Jiri Pirkoa5499522009-09-25 03:28:09 +00001160 * Show and set the primary_reselect flag.
1161 */
1162static ssize_t bonding_show_primary_reselect(struct device *d,
1163 struct device_attribute *attr,
1164 char *buf)
1165{
1166 struct bonding *bond = to_bond(d);
1167
1168 return sprintf(buf, "%s %d\n",
1169 pri_reselect_tbl[bond->params.primary_reselect].modename,
1170 bond->params.primary_reselect);
1171}
1172
1173static ssize_t bonding_store_primary_reselect(struct device *d,
1174 struct device_attribute *attr,
1175 const char *buf, size_t count)
1176{
1177 int new_value, ret = count;
1178 struct bonding *bond = to_bond(d);
1179
1180 if (!rtnl_trylock())
1181 return restart_syscall();
1182
1183 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1184 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001185 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001186 bond->dev->name,
1187 (int) strlen(buf) - 1, buf);
1188 ret = -EINVAL;
1189 goto out;
1190 }
1191
1192 bond->params.primary_reselect = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001193 pr_info("%s: setting primary_reselect to %s (%d).\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001194 bond->dev->name, pri_reselect_tbl[new_value].modename,
1195 new_value);
1196
1197 read_lock(&bond->lock);
1198 write_lock_bh(&bond->curr_slave_lock);
1199 bond_select_active_slave(bond);
1200 write_unlock_bh(&bond->curr_slave_lock);
1201 read_unlock(&bond->lock);
1202out:
1203 rtnl_unlock();
1204 return ret;
1205}
1206static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1207 bonding_show_primary_reselect,
1208 bonding_store_primary_reselect);
1209
1210/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001211 * Show and set the use_carrier flag.
1212 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001213static ssize_t bonding_show_carrier(struct device *d,
1214 struct device_attribute *attr,
1215 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001216{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001217 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001218
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001219 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001220}
1221
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001222static ssize_t bonding_store_carrier(struct device *d,
1223 struct device_attribute *attr,
1224 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001225{
1226 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001227 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001228
1229
1230 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001231 pr_err("%s: no use_carrier value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001232 bond->dev->name);
1233 ret = -EINVAL;
1234 goto out;
1235 }
1236 if ((new_value == 0) || (new_value == 1)) {
1237 bond->params.use_carrier = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001238 pr_info("%s: Setting use_carrier to %d.\n",
1239 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001240 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001241 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1242 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001243 }
1244out:
1245 return count;
1246}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001247static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1248 bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001249
1250
1251/*
1252 * Show and set currently active_slave.
1253 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001254static ssize_t bonding_show_active_slave(struct device *d,
1255 struct device_attribute *attr,
1256 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001257{
1258 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001259 struct bonding *bond = to_bond(d);
Wagner Ferenc16cd0162007-12-06 23:40:29 -08001260 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001261
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001262 read_lock(&bond->curr_slave_lock);
1263 curr = bond->curr_active_slave;
1264 read_unlock(&bond->curr_slave_lock);
1265
1266 if (USES_PRIMARY(bond->params.mode) && curr)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001267 count = sprintf(buf, "%s\n", curr->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001268 return count;
1269}
1270
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001271static ssize_t bonding_store_active_slave(struct device *d,
1272 struct device_attribute *attr,
1273 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001274{
1275 int i;
1276 struct slave *slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001277 struct slave *old_active = NULL;
1278 struct slave *new_active = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001279 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001280
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001281 if (!rtnl_trylock())
1282 return restart_syscall();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001283 read_lock(&bond->lock);
1284 write_lock_bh(&bond->curr_slave_lock);
Jay Vosburgh1466a212007-11-06 13:33:28 -08001285
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001286 if (!USES_PRIMARY(bond->params.mode))
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001287 pr_info("%s: Unable to change active slave; %s is in mode %d\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001288 bond->dev->name, bond->dev->name, bond->params.mode);
1289 else {
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001290 bond_for_each_slave(bond, slave, i) {
1291 if (strnicmp
1292 (slave->dev->name, buf,
1293 strlen(slave->dev->name)) == 0) {
1294 old_active = bond->curr_active_slave;
1295 new_active = slave;
Jay Vosburgha50d8de2006-09-22 21:53:25 -07001296 if (new_active == old_active) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001297 /* do nothing */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001298 pr_info("%s: %s is already the current active slave.\n",
1299 bond->dev->name,
1300 slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001301 goto out;
1302 }
1303 else {
1304 if ((new_active) &&
1305 (old_active) &&
1306 (new_active->link == BOND_LINK_UP) &&
1307 IS_UP(new_active->dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001308 pr_info("%s: Setting %s as active slave.\n",
1309 bond->dev->name,
1310 slave->dev->name);
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00001311 bond_change_active_slave(bond, new_active);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001312 }
1313 else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001314 pr_info("%s: Could not set %s as active slave; either %s is down or the link is down.\n",
1315 bond->dev->name,
1316 slave->dev->name,
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00001317 slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001318 }
1319 goto out;
1320 }
1321 }
1322 }
1323
1324 /* if we got here, then we didn't match the name of any slave */
1325
1326 if (strlen(buf) == 0 || buf[0] == '\n') {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001327 pr_info("%s: Setting active slave to None.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001328 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001329 bond->primary_slave = NULL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001330 bond_select_active_slave(bond);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001331 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001332 pr_info("%s: Unable to set %.*s as active slave as it is not a slave.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001333 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001334 }
1335 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001336 out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001337 write_unlock_bh(&bond->curr_slave_lock);
1338 read_unlock(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001339 rtnl_unlock();
1340
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001341 return count;
1342
1343}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001344static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1345 bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001346
1347
1348/*
1349 * Show link status of the bond interface.
1350 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001351static ssize_t bonding_show_mii_status(struct device *d,
1352 struct device_attribute *attr,
1353 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001354{
1355 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001356 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001357
1358 read_lock(&bond->curr_slave_lock);
1359 curr = bond->curr_active_slave;
1360 read_unlock(&bond->curr_slave_lock);
1361
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001362 return sprintf(buf, "%s\n", curr ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001363}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001364static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001365
1366
1367/*
1368 * Show current 802.3ad aggregator ID.
1369 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001370static ssize_t bonding_show_ad_aggregator(struct device *d,
1371 struct device_attribute *attr,
1372 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001373{
1374 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001375 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001376
1377 if (bond->params.mode == BOND_MODE_8023AD) {
1378 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001379 count = sprintf(buf, "%d\n",
1380 (bond_3ad_get_active_agg_info(bond, &ad_info))
1381 ? 0 : ad_info.aggregator_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001382 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001383
1384 return count;
1385}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001386static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001387
1388
1389/*
1390 * Show number of active 802.3ad ports.
1391 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001392static ssize_t bonding_show_ad_num_ports(struct device *d,
1393 struct device_attribute *attr,
1394 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001395{
1396 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001397 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001398
1399 if (bond->params.mode == BOND_MODE_8023AD) {
1400 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001401 count = sprintf(buf, "%d\n",
1402 (bond_3ad_get_active_agg_info(bond, &ad_info))
1403 ? 0 : ad_info.ports);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001404 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001405
1406 return count;
1407}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001408static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001409
1410
1411/*
1412 * Show current 802.3ad actor key.
1413 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001414static ssize_t bonding_show_ad_actor_key(struct device *d,
1415 struct device_attribute *attr,
1416 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001417{
1418 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001419 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001420
1421 if (bond->params.mode == BOND_MODE_8023AD) {
1422 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001423 count = sprintf(buf, "%d\n",
1424 (bond_3ad_get_active_agg_info(bond, &ad_info))
1425 ? 0 : ad_info.actor_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001426 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001427
1428 return count;
1429}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001430static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001431
1432
1433/*
1434 * Show current 802.3ad partner key.
1435 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001436static ssize_t bonding_show_ad_partner_key(struct device *d,
1437 struct device_attribute *attr,
1438 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001439{
1440 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001441 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001442
1443 if (bond->params.mode == BOND_MODE_8023AD) {
1444 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001445 count = sprintf(buf, "%d\n",
1446 (bond_3ad_get_active_agg_info(bond, &ad_info))
1447 ? 0 : ad_info.partner_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001448 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001449
1450 return count;
1451}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001452static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001453
1454
1455/*
1456 * Show current 802.3ad partner mac.
1457 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001458static ssize_t bonding_show_ad_partner_mac(struct device *d,
1459 struct device_attribute *attr,
1460 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001461{
1462 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001463 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001464
1465 if (bond->params.mode == BOND_MODE_8023AD) {
1466 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001467 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
Johannes Berge1749612008-10-27 15:59:26 -07001468 count = sprintf(buf, "%pM\n", ad_info.partner_system);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001469 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001470
1471 return count;
1472}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001473static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001474
1475
1476
1477static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001478 &dev_attr_slaves.attr,
1479 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001480 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001481 &dev_attr_arp_validate.attr,
1482 &dev_attr_arp_interval.attr,
1483 &dev_attr_arp_ip_target.attr,
1484 &dev_attr_downdelay.attr,
1485 &dev_attr_updelay.attr,
1486 &dev_attr_lacp_rate.attr,
Jay Vosburghfd989c82008-11-04 17:51:16 -08001487 &dev_attr_ad_select.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001488 &dev_attr_xmit_hash_policy.attr,
Moni Shoua7893b242008-05-17 21:10:12 -07001489 &dev_attr_num_grat_arp.attr,
Brian Haley305d5522008-11-04 17:51:14 -08001490 &dev_attr_num_unsol_na.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001491 &dev_attr_miimon.attr,
1492 &dev_attr_primary.attr,
Jiri Pirkoa5499522009-09-25 03:28:09 +00001493 &dev_attr_primary_reselect.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001494 &dev_attr_use_carrier.attr,
1495 &dev_attr_active_slave.attr,
1496 &dev_attr_mii_status.attr,
1497 &dev_attr_ad_aggregator.attr,
1498 &dev_attr_ad_num_ports.attr,
1499 &dev_attr_ad_actor_key.attr,
1500 &dev_attr_ad_partner_key.attr,
1501 &dev_attr_ad_partner_mac.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001502 NULL,
1503};
1504
1505static struct attribute_group bonding_group = {
1506 .name = "bonding",
1507 .attrs = per_bond_attrs,
1508};
1509
1510/*
1511 * Initialize sysfs. This sets up the bonding_masters file in
1512 * /sys/class/net.
1513 */
1514int bond_create_sysfs(void)
1515{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001516 int ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001517
Jay Vosburghb8a97872008-06-13 18:12:04 -07001518 ret = netdev_class_create_file(&class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001519 /*
1520 * Permit multiple loads of the module by ignoring failures to
1521 * create the bonding_masters sysfs file. Bonding devices
1522 * created by second or subsequent loads of the module will
1523 * not be listed in, or controllable by, bonding_masters, but
1524 * will have the usual "bonding" sysfs directory.
1525 *
1526 * This is done to preserve backwards compatibility for
1527 * initscripts/sysconfig, which load bonding multiple times to
1528 * configure multiple bonding devices.
1529 */
1530 if (ret == -EEXIST) {
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001531 /* Is someone being kinky and naming a device bonding_master? */
1532 if (__dev_get_by_name(&init_net,
1533 class_attr_bonding_masters.attr.name))
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001534 pr_err("network device named %s already exists in sysfs",
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001535 class_attr_bonding_masters.attr.name);
Stephen Hemminger130aa612009-06-11 05:46:04 -07001536 ret = 0;
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001537 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001538
1539 return ret;
1540
1541}
1542
1543/*
1544 * Remove /sys/class/net/bonding_masters.
1545 */
1546void bond_destroy_sysfs(void)
1547{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001548 netdev_class_remove_file(&class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001549}
1550
1551/*
1552 * Initialize sysfs for each bond. This sets up and registers
1553 * the 'bondctl' directory for each individual bond under /sys/class/net.
1554 */
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001555void bond_prepare_sysfs_group(struct bonding *bond)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001556{
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001557 bond->dev->sysfs_groups[0] = &bonding_group;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001558}
1559