blob: e9249527e7e70970798f7b4ee43fda075a4a4421 [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/fs.h>
30#include <linux/types.h>
31#include <linux/string.h>
32#include <linux/netdevice.h>
33#include <linux/inetdevice.h>
34#include <linux/in.h>
35#include <linux/sysfs.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080036#include <linux/ctype.h>
37#include <linux/inet.h>
38#include <linux/rtnetlink.h>
Stephen Hemminger5c5129b2009-06-12 19:02:51 +000039#include <linux/etherdevice.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070040#include <net/net_namespace.h>
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000041#include <net/netns/generic.h>
42#include <linux/nsproxy.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080043
Mitch Williamsb76cdba2005-11-09 10:36:41 -080044#include "bonding.h"
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -080045
Stephen Hemminger3d632c32009-06-12 19:02:48 +000046#define to_dev(obj) container_of(obj, struct device, kobj)
Wang Chen454d7c92008-11-12 23:37:49 -080047#define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
Mitch Williamsb76cdba2005-11-09 10:36:41 -080048
Mitch Williamsb76cdba2005-11-09 10:36:41 -080049/*
50 * "show" function for the bond_masters attribute.
51 * The class parameter is ignored.
52 */
Andi Kleen28812fe2010-01-05 12:48:07 +010053static ssize_t bonding_show_bonds(struct class *cls,
54 struct class_attribute *attr,
55 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080056{
Eric W. Biederman4c224002011-10-12 21:56:25 +000057 struct bond_net *bn =
58 container_of(attr, struct bond_net, class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080059 int res = 0;
60 struct bonding *bond;
61
Stephen Hemminger7e083842009-06-12 19:02:46 +000062 rtnl_lock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080063
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000064 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -080065 if (res > (PAGE_SIZE - IFNAMSIZ)) {
66 /* not enough space for another interface name */
67 if ((PAGE_SIZE - res) > 10)
68 res = PAGE_SIZE - 10;
Wagner Ferencb8843662007-12-06 23:40:30 -080069 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -080070 break;
71 }
Wagner Ferencb8843662007-12-06 23:40:30 -080072 res += sprintf(buf + res, "%s ", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080073 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -080074 if (res)
75 buf[res-1] = '\n'; /* eat the leftover space */
Stephen Hemminger7e083842009-06-12 19:02:46 +000076
77 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080078 return res;
79}
80
Eric W. Biederman4c224002011-10-12 21:56:25 +000081static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
Stephen Hemminger373500d2009-06-12 19:02:50 +000082{
83 struct bonding *bond;
84
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000085 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Stephen Hemminger373500d2009-06-12 19:02:50 +000086 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
87 return bond->dev;
88 }
89 return NULL;
90}
91
Mitch Williamsb76cdba2005-11-09 10:36:41 -080092/*
93 * "store" function for the bond_masters attribute. This is what
94 * creates and deletes entire bonds.
95 *
96 * The class parameter is ignored.
97 *
98 */
99
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000100static ssize_t bonding_store_bonds(struct class *cls,
Andi Kleen28812fe2010-01-05 12:48:07 +0100101 struct class_attribute *attr,
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000102 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800103{
Eric W. Biederman4c224002011-10-12 21:56:25 +0000104 struct bond_net *bn =
105 container_of(attr, struct bond_net, class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800106 char command[IFNAMSIZ + 1] = {0, };
107 char *ifname;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800108 int rv, res = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800109
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800110 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
111 ifname = command + 1;
112 if ((strlen(command) <= 1) ||
113 !dev_valid_name(ifname))
114 goto err_no_cmd;
115
116 if (command[0] == '+') {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800117 pr_info("%s is being created...\n", ifname);
Eric W. Biederman4c224002011-10-12 21:56:25 +0000118 rv = bond_create(bn->net, ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800119 if (rv) {
Phil Oester5f86cad12011-03-14 06:22:06 +0000120 if (rv == -EEXIST)
121 pr_info("%s already exists.\n", ifname);
122 else
123 pr_info("%s creation failed.\n", ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800124 res = rv;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800125 }
Stephen Hemminger373500d2009-06-12 19:02:50 +0000126 } else if (command[0] == '-') {
127 struct net_device *bond_dev;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800128
Jay Vosburgh027ea042008-01-17 16:25:02 -0800129 rtnl_lock();
Eric W. Biederman4c224002011-10-12 21:56:25 +0000130 bond_dev = bond_get_by_name(bn, ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000131 if (bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800132 pr_info("%s is being deleted...\n", ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000133 unregister_netdevice(bond_dev);
134 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800135 pr_err("unable to delete non-existent %s\n", ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000136 res = -ENODEV;
137 }
138 rtnl_unlock();
139 } else
140 goto err_no_cmd;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800141
Stephen Hemminger373500d2009-06-12 19:02:50 +0000142 /* Always return either count or an error. If you return 0, you'll
143 * get called forever, which is bad.
144 */
145 return res;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800146
147err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800148 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700149 return -EPERM;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800150}
Stephen Hemminger373500d2009-06-12 19:02:50 +0000151
Eric W. Biederman4c224002011-10-12 21:56:25 +0000152static const void *bonding_namespace(struct class *cls,
153 const struct class_attribute *attr)
154{
155 const struct bond_net *bn =
156 container_of(attr, struct bond_net, class_attr_bonding_masters);
157 return bn->net;
158}
159
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800160/* class attribute for bond_masters file. This ends up in /sys/class/net */
Eric W. Biederman4c224002011-10-12 21:56:25 +0000161static const struct class_attribute class_attr_bonding_masters = {
162 .attr = {
163 .name = "bonding_masters",
164 .mode = S_IWUSR | S_IRUGO,
165 },
166 .show = bonding_show_bonds,
167 .store = bonding_store_bonds,
168 .namespace = bonding_namespace,
169};
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800170
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800171/*
172 * Show the slaves in the current bond.
173 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700174static ssize_t bonding_show_slaves(struct device *d,
175 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800176{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700177 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200178 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200179 struct slave *slave;
180 int res = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800181
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700182 read_lock(&bond->lock);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200183 bond_for_each_slave(bond, slave, iter) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800184 if (res > (PAGE_SIZE - IFNAMSIZ)) {
185 /* not enough space for another interface name */
186 if ((PAGE_SIZE - res) > 10)
187 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800188 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800189 break;
190 }
191 res += sprintf(buf + res, "%s ", slave->dev->name);
192 }
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700193 read_unlock(&bond->lock);
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800194 if (res)
195 buf[res-1] = '\n'; /* eat the leftover space */
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200196
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800197 return res;
198}
199
200/*
Veaceslav Falicod6641cc2013-05-28 01:26:13 +0000201 * Set the slaves in the current bond.
Jiri Pirkof9f35452010-05-18 05:46:39 +0000202 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
203 * All hard work should be done there.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800204 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700205static ssize_t bonding_store_slaves(struct device *d,
206 struct device_attribute *attr,
207 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800208{
209 char command[IFNAMSIZ + 1] = { 0, };
210 char *ifname;
Jiri Pirkof9f35452010-05-18 05:46:39 +0000211 int res, ret = count;
212 struct net_device *dev;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700213 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800214
Eric W. Biederman496a60c2009-05-13 17:02:50 +0000215 if (!rtnl_trylock())
216 return restart_syscall();
Jay Vosburgh027ea042008-01-17 16:25:02 -0800217
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800218 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
219 ifname = command + 1;
220 if ((strlen(command) <= 1) ||
221 !dev_valid_name(ifname))
222 goto err_no_cmd;
223
Jiri Pirkof9f35452010-05-18 05:46:39 +0000224 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
225 if (!dev) {
226 pr_info("%s: Interface %s does not exist!\n",
227 bond->dev->name, ifname);
228 ret = -ENODEV;
229 goto out;
230 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800231
Jiri Pirkof9f35452010-05-18 05:46:39 +0000232 switch (command[0]) {
233 case '+':
234 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800235 res = bond_enslave(bond->dev, dev);
Jiri Pirkof9f35452010-05-18 05:46:39 +0000236 break;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000237
Jiri Pirkof9f35452010-05-18 05:46:39 +0000238 case '-':
239 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
240 res = bond_release(bond->dev, dev);
241 break;
242
243 default:
244 goto err_no_cmd;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800245 }
246
Jiri Pirkof9f35452010-05-18 05:46:39 +0000247 if (res)
248 ret = res;
249 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800250
251err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800252 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
253 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800254 ret = -EPERM;
255
256out:
Jay Vosburgh027ea042008-01-17 16:25:02 -0800257 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800258 return ret;
259}
260
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000261static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
262 bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800263
264/*
265 * Show and set the bonding mode. The bond interface must be down to
266 * change the mode.
267 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700268static ssize_t bonding_show_mode(struct device *d,
269 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800270{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700271 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800272
273 return sprintf(buf, "%s %d\n",
274 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800275 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800276}
277
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700278static ssize_t bonding_store_mode(struct device *d,
279 struct device_attribute *attr,
280 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800281{
282 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700283 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800284
nikolay@redhat.comea6836d2013-05-18 01:18:28 +0000285 if (!rtnl_trylock())
286 return restart_syscall();
287
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800288 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800289 pr_err("unable to update mode of %s because interface is up.\n",
290 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800291 ret = -EPERM;
292 goto out;
293 }
294
Veaceslav Falico0965a1f2013-09-25 09:20:21 +0200295 if (bond_has_slaves(bond)) {
Veaceslav Falico4a8bb7e2011-11-15 06:44:42 +0000296 pr_err("unable to update mode of %s because it has slaves.\n",
297 bond->dev->name);
298 ret = -EPERM;
299 goto out;
300 }
301
Jay Vosburghece95f72008-01-17 16:25:01 -0800302 new_value = bond_parse_parm(buf, bond_mode_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800303 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800304 pr_err("%s: Ignoring invalid mode value %.*s.\n",
305 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800306 ret = -EINVAL;
307 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800308 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000309 if ((new_value == BOND_MODE_ALB ||
310 new_value == BOND_MODE_TLB) &&
311 bond->params.arp_interval) {
312 pr_err("%s: %s mode is incompatible with arp monitoring.\n",
313 bond->dev->name, bond_mode_tbl[new_value].modename);
314 ret = -EINVAL;
315 goto out;
316 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000317
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200318 /* don't cache arp_validate between modes */
319 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000320 bond->params.mode = new_value;
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000321 pr_info("%s: setting mode to %s (%d).\n",
322 bond->dev->name, bond_mode_tbl[new_value].modename,
323 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800324out:
nikolay@redhat.comea6836d2013-05-18 01:18:28 +0000325 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800326 return ret;
327}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000328static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
329 bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800330
331/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000332 * Show and set the bonding transmit hash method.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800333 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700334static ssize_t bonding_show_xmit_hash(struct device *d,
335 struct device_attribute *attr,
336 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800337{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700338 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800339
Wagner Ferenc8e4b9322007-12-06 23:40:32 -0800340 return sprintf(buf, "%s %d\n",
341 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
342 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800343}
344
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700345static ssize_t bonding_store_xmit_hash(struct device *d,
346 struct device_attribute *attr,
347 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800348{
349 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700350 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800351
Jay Vosburghece95f72008-01-17 16:25:01 -0800352 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800353 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800354 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800355 bond->dev->name,
356 (int)strlen(buf) - 1, buf);
357 ret = -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800358 } else {
359 bond->params.xmit_policy = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800360 pr_info("%s: setting xmit hash policy to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000361 bond->dev->name,
362 xmit_hashtype_tbl[new_value].modename, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800363 }
Nikolay Aleksandrov53edee22013-05-24 00:59:47 +0000364
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800365 return ret;
366}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000367static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
368 bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800369
370/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700371 * Show and set arp_validate.
372 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700373static ssize_t bonding_show_arp_validate(struct device *d,
374 struct device_attribute *attr,
375 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700376{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700377 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700378
379 return sprintf(buf, "%s %d\n",
380 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800381 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700382}
383
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700384static ssize_t bonding_store_arp_validate(struct device *d,
385 struct device_attribute *attr,
386 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700387{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700388 struct bonding *bond = to_bond(d);
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200389 int new_value, ret = count;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700390
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200391 if (!rtnl_trylock())
392 return restart_syscall();
Jay Vosburghece95f72008-01-17 16:25:01 -0800393 new_value = bond_parse_parm(buf, arp_validate_tbl);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700394 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800395 pr_err("%s: Ignoring invalid arp_validate value %s\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700396 bond->dev->name, buf);
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200397 ret = -EINVAL;
398 goto out;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700399 }
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200400 if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800401 pr_err("%s: arp_validate only supported in active-backup mode.\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700402 bond->dev->name);
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200403 ret = -EINVAL;
404 goto out;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700405 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800406 pr_info("%s: setting arp_validate to %s (%d).\n",
407 bond->dev->name, arp_validate_tbl[new_value].modename,
408 new_value);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700409
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200410 if (bond->dev->flags & IFF_UP) {
411 if (!new_value)
412 bond->recv_probe = NULL;
413 else if (bond->params.arp_interval)
414 bond->recv_probe = bond_arp_rcv;
415 }
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700416 bond->params.arp_validate = new_value;
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200417out:
418 rtnl_unlock();
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700419
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200420 return ret;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700421}
422
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000423static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
424 bonding_store_arp_validate);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200425/*
426 * Show and set arp_all_targets.
427 */
428static ssize_t bonding_show_arp_all_targets(struct device *d,
429 struct device_attribute *attr,
430 char *buf)
431{
432 struct bonding *bond = to_bond(d);
433 int value = bond->params.arp_all_targets;
434
435 return sprintf(buf, "%s %d\n", arp_all_targets_tbl[value].modename,
436 value);
437}
438
439static ssize_t bonding_store_arp_all_targets(struct device *d,
440 struct device_attribute *attr,
441 const char *buf, size_t count)
442{
443 struct bonding *bond = to_bond(d);
444 int new_value;
445
446 new_value = bond_parse_parm(buf, arp_all_targets_tbl);
447 if (new_value < 0) {
448 pr_err("%s: Ignoring invalid arp_all_targets value %s\n",
449 bond->dev->name, buf);
450 return -EINVAL;
451 }
452 pr_info("%s: setting arp_all_targets to %s (%d).\n",
453 bond->dev->name, arp_all_targets_tbl[new_value].modename,
454 new_value);
455
456 bond->params.arp_all_targets = new_value;
457
458 return count;
459}
460
461static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
462 bonding_show_arp_all_targets, bonding_store_arp_all_targets);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700463
464/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700465 * Show and store fail_over_mac. User only allowed to change the
466 * value when there are no slaves.
467 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000468static ssize_t bonding_show_fail_over_mac(struct device *d,
469 struct device_attribute *attr,
470 char *buf)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700471{
472 struct bonding *bond = to_bond(d);
473
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700474 return sprintf(buf, "%s %d\n",
475 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
476 bond->params.fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700477}
478
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000479static ssize_t bonding_store_fail_over_mac(struct device *d,
480 struct device_attribute *attr,
481 const char *buf, size_t count)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700482{
dingtianhong9402b742013-07-23 15:25:39 +0800483 int new_value, ret = count;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700484 struct bonding *bond = to_bond(d);
485
dingtianhong9402b742013-07-23 15:25:39 +0800486 if (!rtnl_trylock())
487 return restart_syscall();
488
Veaceslav Falico0965a1f2013-09-25 09:20:21 +0200489 if (bond_has_slaves(bond)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800490 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
Jay Vosburghdd957c52007-10-09 19:57:24 -0700491 bond->dev->name);
dingtianhong9402b742013-07-23 15:25:39 +0800492 ret = -EPERM;
493 goto out;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700494 }
495
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700496 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
497 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800498 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700499 bond->dev->name, buf);
dingtianhong9402b742013-07-23 15:25:39 +0800500 ret = -EINVAL;
501 goto out;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700502 }
503
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700504 bond->params.fail_over_mac = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800505 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
506 bond->dev->name, fail_over_mac_tbl[new_value].modename,
507 new_value);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700508
dingtianhong9402b742013-07-23 15:25:39 +0800509out:
510 rtnl_unlock();
511 return ret;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700512}
513
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000514static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
515 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700516
517/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800518 * Show and set the arp timer interval. There are two tricky bits
519 * here. First, if ARP monitoring is activated, then we must disable
520 * MII monitoring. Second, if the ARP timer isn't running, we must
521 * start it.
522 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700523static ssize_t bonding_show_arp_interval(struct device *d,
524 struct device_attribute *attr,
525 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800526{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700527 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800528
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800529 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800530}
531
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700532static ssize_t bonding_store_arp_interval(struct device *d,
533 struct device_attribute *attr,
534 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800535{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700536 struct bonding *bond = to_bond(d);
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200537 int new_value, ret = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800538
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000539 if (!rtnl_trylock())
540 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800541 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800542 pr_err("%s: no arp_interval value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800543 bond->dev->name);
544 ret = -EINVAL;
545 goto out;
546 }
547 if (new_value < 0) {
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000548 pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800549 bond->dev->name, new_value, INT_MAX);
550 ret = -EINVAL;
551 goto out;
552 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000553 if (bond->params.mode == BOND_MODE_ALB ||
554 bond->params.mode == BOND_MODE_TLB) {
555 pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
556 bond->dev->name, bond->dev->name);
557 ret = -EINVAL;
558 goto out;
559 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800560 pr_info("%s: Setting ARP monitoring interval to %d.\n",
561 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800562 bond->params.arp_interval = new_value;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000563 if (new_value) {
564 if (bond->params.miimon) {
565 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
566 bond->dev->name, bond->dev->name);
567 bond->params.miimon = 0;
568 }
569 if (!bond->params.arp_targets[0])
570 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
571 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800572 }
573 if (bond->dev->flags & IFF_UP) {
574 /* If the interface is up, we may need to fire off
575 * the ARP timer. If the interface is down, the
576 * timer will get fired off when the open function
577 * is called.
578 */
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000579 if (!new_value) {
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200580 if (bond->params.arp_validate)
581 bond->recv_probe = NULL;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000582 cancel_delayed_work_sync(&bond->arp_work);
583 } else {
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200584 /* arp_validate can be set only in active-backup mode */
585 if (bond->params.arp_validate)
586 bond->recv_probe = bond_arp_rcv;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000587 cancel_delayed_work_sync(&bond->mii_work);
588 queue_delayed_work(bond->wq, &bond->arp_work, 0);
589 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800590 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800591out:
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000592 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800593 return ret;
594}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000595static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
596 bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800597
598/*
599 * Show and set the arp targets.
600 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700601static ssize_t bonding_show_arp_targets(struct device *d,
602 struct device_attribute *attr,
603 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800604{
605 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700606 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800607
608 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
609 if (bond->params.arp_targets[i])
Harvey Harrison63779432008-10-31 00:56:00 -0700610 res += sprintf(buf + res, "%pI4 ",
611 &bond->params.arp_targets[i]);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800612 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800613 if (res)
614 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800615 return res;
616}
617
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700618static ssize_t bonding_store_arp_targets(struct device *d,
619 struct device_attribute *attr,
620 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800621{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700622 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200623 struct list_head *iter;
Veaceslav Falico8599b522013-06-24 11:49:34 +0200624 struct slave *slave;
625 __be32 newtarget, *targets;
626 unsigned long *targets_rx;
627 int ind, i, j, ret = -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800628
629 targets = bond->params.arp_targets;
630 newtarget = in_aton(buf + 1);
631 /* look for adds */
632 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400633 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800634 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700635 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800636 goto out;
637 }
638
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200639 if (bond_get_targets_ip(targets, newtarget) != -1) { /* dup */
640 pr_err("%s: ARP target %pI4 is already present\n",
641 bond->dev->name, &newtarget);
642 goto out;
643 }
644
Veaceslav Falico8599b522013-06-24 11:49:34 +0200645 ind = bond_get_targets_ip(targets, 0); /* first free slot */
646 if (ind == -1) {
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200647 pr_err("%s: ARP target table is full!\n",
648 bond->dev->name);
649 goto out;
650 }
651
652 pr_info("%s: adding ARP target %pI4.\n", bond->dev->name,
653 &newtarget);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200654 /* not to race with bond_arp_rcv */
655 write_lock_bh(&bond->lock);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200656 bond_for_each_slave(bond, slave, iter)
Veaceslav Falico8599b522013-06-24 11:49:34 +0200657 slave->target_last_arp_rx[ind] = jiffies;
658 targets[ind] = newtarget;
659 write_unlock_bh(&bond->lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000660 } else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400661 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800662 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700663 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800664 goto out;
665 }
666
Veaceslav Falico8599b522013-06-24 11:49:34 +0200667 ind = bond_get_targets_ip(targets, newtarget);
668 if (ind == -1) {
669 pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800670 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800671 goto out;
672 }
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200673
Veaceslav Falico8599b522013-06-24 11:49:34 +0200674 if (ind == 0 && !targets[1] && bond->params.arp_interval)
675 pr_warn("%s: removing last arp target with arp_interval on\n",
676 bond->dev->name);
677
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200678 pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
679 &newtarget);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200680
681 write_lock_bh(&bond->lock);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200682 bond_for_each_slave(bond, slave, iter) {
Veaceslav Falico8599b522013-06-24 11:49:34 +0200683 targets_rx = slave->target_last_arp_rx;
684 j = ind;
685 for (; (j < BOND_MAX_ARP_TARGETS-1) && targets[j+1]; j++)
686 targets_rx[j] = targets_rx[j+1];
687 targets_rx[j] = 0;
688 }
689 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200690 targets[i] = targets[i+1];
691 targets[i] = 0;
Veaceslav Falico8599b522013-06-24 11:49:34 +0200692 write_unlock_bh(&bond->lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000693 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800694 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
695 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800696 ret = -EPERM;
697 goto out;
698 }
699
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200700 ret = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800701out:
702 return ret;
703}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700704static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800705
706/*
707 * Show and set the up and down delays. These must be multiples of the
708 * MII monitoring value, and are stored internally as the multiplier.
709 * Thus, we must translate to MS for the real world.
710 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700711static ssize_t bonding_show_downdelay(struct device *d,
712 struct device_attribute *attr,
713 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800714{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700715 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800716
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800717 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800718}
719
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700720static ssize_t bonding_store_downdelay(struct device *d,
721 struct device_attribute *attr,
722 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800723{
724 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700725 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800726
727 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800728 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800729 bond->dev->name);
730 ret = -EPERM;
731 goto out;
732 }
733
734 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800735 pr_err("%s: no down delay value specified.\n", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800736 ret = -EINVAL;
737 goto out;
738 }
739 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800740 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000741 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800742 ret = -EINVAL;
743 goto out;
744 } else {
745 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800746 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 +0000747 bond->dev->name, new_value,
748 bond->params.miimon,
749 (new_value / bond->params.miimon) *
750 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800751 }
752 bond->params.downdelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800753 pr_info("%s: Setting down delay to %d.\n",
754 bond->dev->name,
755 bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800756
757 }
758
759out:
760 return ret;
761}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000762static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
763 bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800764
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700765static ssize_t bonding_show_updelay(struct device *d,
766 struct device_attribute *attr,
767 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800768{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700769 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800770
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800771 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800772
773}
774
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700775static ssize_t bonding_store_updelay(struct device *d,
776 struct device_attribute *attr,
777 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800778{
779 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700780 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800781
782 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800783 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800784 bond->dev->name);
785 ret = -EPERM;
786 goto out;
787 }
788
789 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800790 pr_err("%s: no up delay value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800791 bond->dev->name);
792 ret = -EINVAL;
793 goto out;
794 }
795 if (new_value < 0) {
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000796 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
797 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800798 ret = -EINVAL;
799 goto out;
800 } else {
801 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800802 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 +0000803 bond->dev->name, new_value,
804 bond->params.miimon,
805 (new_value / bond->params.miimon) *
806 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800807 }
808 bond->params.updelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800809 pr_info("%s: Setting up delay to %d.\n",
810 bond->dev->name,
811 bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800812 }
813
814out:
815 return ret;
816}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000817static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
818 bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800819
820/*
821 * Show and set the LACP interval. Interface must be down, and the mode
822 * must be set to 802.3ad mode.
823 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700824static ssize_t bonding_show_lacp(struct device *d,
825 struct device_attribute *attr,
826 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800827{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700828 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800829
830 return sprintf(buf, "%s %d\n",
831 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800832 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800833}
834
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700835static ssize_t bonding_store_lacp(struct device *d,
836 struct device_attribute *attr,
837 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800838{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700839 struct bonding *bond = to_bond(d);
nikolay@redhat.comc5093162013-09-02 13:51:40 +0200840 int new_value, ret = count;
841
842 if (!rtnl_trylock())
843 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800844
845 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800846 pr_err("%s: Unable to update LACP rate because interface is up.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800847 bond->dev->name);
848 ret = -EPERM;
849 goto out;
850 }
851
852 if (bond->params.mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800853 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800854 bond->dev->name);
855 ret = -EPERM;
856 goto out;
857 }
858
Jay Vosburghece95f72008-01-17 16:25:01 -0800859 new_value = bond_parse_parm(buf, bond_lacp_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800860
861 if ((new_value == 1) || (new_value == 0)) {
862 bond->params.lacp_fast = new_value;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +0000863 bond_3ad_update_lacp_rate(bond);
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800864 pr_info("%s: Setting LACP rate to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000865 bond->dev->name, bond_lacp_tbl[new_value].modename,
866 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800867 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800868 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000869 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800870 ret = -EINVAL;
871 }
872out:
nikolay@redhat.comc5093162013-09-02 13:51:40 +0200873 rtnl_unlock();
874
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800875 return ret;
876}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000877static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
878 bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800879
stephen hemminger655f8912011-06-22 09:54:39 +0000880static ssize_t bonding_show_min_links(struct device *d,
881 struct device_attribute *attr,
882 char *buf)
883{
884 struct bonding *bond = to_bond(d);
885
886 return sprintf(buf, "%d\n", bond->params.min_links);
887}
888
889static ssize_t bonding_store_min_links(struct device *d,
890 struct device_attribute *attr,
891 const char *buf, size_t count)
892{
893 struct bonding *bond = to_bond(d);
894 int ret;
895 unsigned int new_value;
896
897 ret = kstrtouint(buf, 0, &new_value);
898 if (ret < 0) {
899 pr_err("%s: Ignoring invalid min links value %s.\n",
900 bond->dev->name, buf);
901 return ret;
902 }
903
904 pr_info("%s: Setting min links value to %u\n",
905 bond->dev->name, new_value);
906 bond->params.min_links = new_value;
907 return count;
908}
909static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
910 bonding_show_min_links, bonding_store_min_links);
911
Jay Vosburghfd989c82008-11-04 17:51:16 -0800912static ssize_t bonding_show_ad_select(struct device *d,
913 struct device_attribute *attr,
914 char *buf)
915{
916 struct bonding *bond = to_bond(d);
917
918 return sprintf(buf, "%s %d\n",
919 ad_select_tbl[bond->params.ad_select].modename,
920 bond->params.ad_select);
921}
922
923
924static ssize_t bonding_store_ad_select(struct device *d,
925 struct device_attribute *attr,
926 const char *buf, size_t count)
927{
928 int new_value, ret = count;
929 struct bonding *bond = to_bond(d);
930
931 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800932 pr_err("%s: Unable to update ad_select because interface is up.\n",
933 bond->dev->name);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800934 ret = -EPERM;
935 goto out;
936 }
937
938 new_value = bond_parse_parm(buf, ad_select_tbl);
939
940 if (new_value != -1) {
941 bond->params.ad_select = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800942 pr_info("%s: Setting ad_select to %s (%d).\n",
943 bond->dev->name, ad_select_tbl[new_value].modename,
944 new_value);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800945 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800946 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -0800947 bond->dev->name, (int)strlen(buf) - 1, buf);
948 ret = -EINVAL;
949 }
950out:
951 return ret;
952}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000953static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
954 bonding_show_ad_select, bonding_store_ad_select);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800955
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800956/*
Ben Hutchingsad246c92011-04-26 15:25:52 +0000957 * Show and set the number of peer notifications to send after a failover event.
958 */
959static ssize_t bonding_show_num_peer_notif(struct device *d,
960 struct device_attribute *attr,
961 char *buf)
962{
963 struct bonding *bond = to_bond(d);
964 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
965}
966
967static ssize_t bonding_store_num_peer_notif(struct device *d,
968 struct device_attribute *attr,
969 const char *buf, size_t count)
970{
971 struct bonding *bond = to_bond(d);
972 int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
973 return err ? err : count;
974}
975static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
976 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
977static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
978 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
979
980/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800981 * Show and set the MII monitor interval. There are two tricky bits
982 * here. First, if MII monitoring is activated, then we must disable
983 * ARP monitoring. Second, if the timer isn't running, we must
984 * start it.
985 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700986static ssize_t bonding_show_miimon(struct device *d,
987 struct device_attribute *attr,
988 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800989{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700990 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800991
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800992 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800993}
994
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700995static ssize_t bonding_store_miimon(struct device *d,
996 struct device_attribute *attr,
997 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800998{
999 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001000 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001001
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00001002 if (!rtnl_trylock())
1003 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001004 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001005 pr_err("%s: no miimon value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001006 bond->dev->name);
1007 ret = -EINVAL;
1008 goto out;
1009 }
1010 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001011 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +00001012 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001013 ret = -EINVAL;
1014 goto out;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +00001015 }
1016 pr_info("%s: Setting MII monitoring interval to %d.\n",
1017 bond->dev->name, new_value);
1018 bond->params.miimon = new_value;
1019 if (bond->params.updelay)
1020 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
1021 bond->dev->name,
1022 bond->params.updelay * bond->params.miimon);
1023 if (bond->params.downdelay)
1024 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
1025 bond->dev->name,
1026 bond->params.downdelay * bond->params.miimon);
1027 if (new_value && bond->params.arp_interval) {
1028 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
1029 bond->dev->name);
1030 bond->params.arp_interval = 0;
1031 if (bond->params.arp_validate)
1032 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
1033 }
1034 if (bond->dev->flags & IFF_UP) {
1035 /* If the interface is up, we may need to fire off
1036 * the MII timer. If the interface is down, the
1037 * timer will get fired off when the open function
1038 * is called.
1039 */
1040 if (!new_value) {
1041 cancel_delayed_work_sync(&bond->mii_work);
1042 } else {
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00001043 cancel_delayed_work_sync(&bond->arp_work);
1044 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001045 }
1046 }
1047out:
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00001048 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001049 return ret;
1050}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001051static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1052 bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001053
1054/*
1055 * Show and set the primary slave. The store function is much
1056 * simpler than bonding_store_slaves function because it only needs to
1057 * handle one interface name.
1058 * The bond must be a mode that supports a primary for this be
1059 * set.
1060 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001061static ssize_t bonding_show_primary(struct device *d,
1062 struct device_attribute *attr,
1063 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001064{
1065 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001066 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001067
1068 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001069 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001070
1071 return count;
1072}
1073
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001074static ssize_t bonding_store_primary(struct device *d,
1075 struct device_attribute *attr,
1076 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001077{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001078 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001079 struct list_head *iter;
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001080 char ifname[IFNAMSIZ];
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001081 struct slave *slave;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001082
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001083 if (!rtnl_trylock())
1084 return restart_syscall();
Neil Hormane843fa52010-10-13 16:01:50 +00001085 block_netpoll_tx();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001086 read_lock(&bond->lock);
1087 write_lock_bh(&bond->curr_slave_lock);
1088
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001089 if (!USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001090 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1091 bond->dev->name, bond->dev->name, bond->params.mode);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001092 goto out;
1093 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001094
nikolay@redhat.comeb6e98a2012-10-31 04:42:51 +00001095 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001096
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001097 /* check to see if we are clearing primary */
1098 if (!strlen(ifname) || buf[0] == '\n') {
1099 pr_info("%s: Setting primary slave to None.\n",
1100 bond->dev->name);
1101 bond->primary_slave = NULL;
Milos Vyleteleb492f72013-01-29 09:59:00 +00001102 memset(bond->params.primary, 0, sizeof(bond->params.primary));
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001103 bond_select_active_slave(bond);
1104 goto out;
1105 }
1106
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001107 bond_for_each_slave(bond, slave, iter) {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001108 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1109 pr_info("%s: Setting %s as primary slave.\n",
1110 bond->dev->name, slave->dev->name);
1111 bond->primary_slave = slave;
1112 strcpy(bond->params.primary, slave->dev->name);
1113 bond_select_active_slave(bond);
1114 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001115 }
1116 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001117
Weiping Pan8a936642012-06-10 23:00:20 +00001118 strncpy(bond->params.primary, ifname, IFNAMSIZ);
1119 bond->params.primary[IFNAMSIZ - 1] = 0;
1120
1121 pr_info("%s: Recording %s as primary, "
1122 "but it has not been enslaved to %s yet.\n",
1123 bond->dev->name, ifname, bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001124out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001125 write_unlock_bh(&bond->curr_slave_lock);
1126 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001127 unblock_netpoll_tx();
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001128 rtnl_unlock();
1129
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001130 return count;
1131}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001132static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1133 bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001134
1135/*
Jiri Pirkoa5499522009-09-25 03:28:09 +00001136 * Show and set the primary_reselect flag.
1137 */
1138static ssize_t bonding_show_primary_reselect(struct device *d,
1139 struct device_attribute *attr,
1140 char *buf)
1141{
1142 struct bonding *bond = to_bond(d);
1143
1144 return sprintf(buf, "%s %d\n",
1145 pri_reselect_tbl[bond->params.primary_reselect].modename,
1146 bond->params.primary_reselect);
1147}
1148
1149static ssize_t bonding_store_primary_reselect(struct device *d,
1150 struct device_attribute *attr,
1151 const char *buf, size_t count)
1152{
1153 int new_value, ret = count;
1154 struct bonding *bond = to_bond(d);
1155
1156 if (!rtnl_trylock())
1157 return restart_syscall();
1158
1159 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1160 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001161 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001162 bond->dev->name,
1163 (int) strlen(buf) - 1, buf);
1164 ret = -EINVAL;
1165 goto out;
1166 }
1167
1168 bond->params.primary_reselect = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001169 pr_info("%s: setting primary_reselect to %s (%d).\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001170 bond->dev->name, pri_reselect_tbl[new_value].modename,
1171 new_value);
1172
Neil Hormane843fa52010-10-13 16:01:50 +00001173 block_netpoll_tx();
Jiri Pirkoa5499522009-09-25 03:28:09 +00001174 read_lock(&bond->lock);
1175 write_lock_bh(&bond->curr_slave_lock);
1176 bond_select_active_slave(bond);
1177 write_unlock_bh(&bond->curr_slave_lock);
1178 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001179 unblock_netpoll_tx();
Jiri Pirkoa5499522009-09-25 03:28:09 +00001180out:
1181 rtnl_unlock();
1182 return ret;
1183}
1184static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1185 bonding_show_primary_reselect,
1186 bonding_store_primary_reselect);
1187
1188/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001189 * Show and set the use_carrier flag.
1190 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001191static ssize_t bonding_show_carrier(struct device *d,
1192 struct device_attribute *attr,
1193 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001194{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001195 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001196
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001197 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001198}
1199
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001200static ssize_t bonding_store_carrier(struct device *d,
1201 struct device_attribute *attr,
1202 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001203{
1204 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001205 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001206
1207
1208 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001209 pr_err("%s: no use_carrier value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001210 bond->dev->name);
1211 ret = -EINVAL;
1212 goto out;
1213 }
1214 if ((new_value == 0) || (new_value == 1)) {
1215 bond->params.use_carrier = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001216 pr_info("%s: Setting use_carrier to %d.\n",
1217 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001218 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001219 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1220 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001221 }
1222out:
Jiri Pirko672bda32011-01-25 11:03:25 +00001223 return ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001224}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001225static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1226 bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001227
1228
1229/*
1230 * Show and set currently active_slave.
1231 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001232static ssize_t bonding_show_active_slave(struct device *d,
1233 struct device_attribute *attr,
1234 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001235{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001236 struct bonding *bond = to_bond(d);
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001237 struct slave *curr;
Wagner Ferenc16cd0162007-12-06 23:40:29 -08001238 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001239
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001240 rcu_read_lock();
1241 curr = rcu_dereference(bond->curr_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001242 if (USES_PRIMARY(bond->params.mode) && curr)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001243 count = sprintf(buf, "%s\n", curr->dev->name);
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001244 rcu_read_unlock();
1245
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001246 return count;
1247}
1248
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001249static ssize_t bonding_store_active_slave(struct device *d,
1250 struct device_attribute *attr,
1251 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001252{
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001253 struct slave *slave, *old_active, *new_active;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001254 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001255 struct list_head *iter;
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001256 char ifname[IFNAMSIZ];
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001257
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001258 if (!rtnl_trylock())
1259 return restart_syscall();
Neil Hormane843fa52010-10-13 16:01:50 +00001260
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001261 old_active = new_active = NULL;
Neil Hormane843fa52010-10-13 16:01:50 +00001262 block_netpoll_tx();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001263 read_lock(&bond->lock);
1264 write_lock_bh(&bond->curr_slave_lock);
Jay Vosburgh1466a212007-11-06 13:33:28 -08001265
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001266 if (!USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001267 pr_info("%s: Unable to change active slave; %s is in mode %d\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001268 bond->dev->name, bond->dev->name, bond->params.mode);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001269 goto out;
1270 }
1271
nikolay@redhat.comc84e1592012-10-31 06:03:52 +00001272 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001273
1274 /* check to see if we are clearing active */
1275 if (!strlen(ifname) || buf[0] == '\n') {
1276 pr_info("%s: Clearing current active slave.\n",
1277 bond->dev->name);
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001278 rcu_assign_pointer(bond->curr_active_slave, NULL);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001279 bond_select_active_slave(bond);
1280 goto out;
1281 }
1282
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001283 bond_for_each_slave(bond, slave, iter) {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001284 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1285 old_active = bond->curr_active_slave;
1286 new_active = slave;
1287 if (new_active == old_active) {
1288 /* do nothing */
1289 pr_info("%s: %s is already the current"
1290 " active slave.\n",
1291 bond->dev->name,
1292 slave->dev->name);
1293 goto out;
dingtianhong38c49162013-07-23 15:25:32 +08001294 } else {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001295 if ((new_active) &&
1296 (old_active) &&
1297 (new_active->link == BOND_LINK_UP) &&
1298 IS_UP(new_active->dev)) {
1299 pr_info("%s: Setting %s as active"
1300 " slave.\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001301 bond->dev->name,
1302 slave->dev->name);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001303 bond_change_active_slave(bond,
1304 new_active);
dingtianhong38c49162013-07-23 15:25:32 +08001305 } else {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001306 pr_info("%s: Could not set %s as"
1307 " active slave; either %s is"
1308 " down or the link is down.\n",
1309 bond->dev->name,
1310 slave->dev->name,
1311 slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001312 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001313 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001314 }
1315 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001316 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001317
1318 pr_info("%s: Unable to set %.*s as active slave.\n",
1319 bond->dev->name, (int)strlen(buf) - 1, buf);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001320 out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001321 write_unlock_bh(&bond->curr_slave_lock);
1322 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001323 unblock_netpoll_tx();
1324
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001325 rtnl_unlock();
1326
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001327 return count;
1328
1329}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001330static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1331 bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001332
1333
1334/*
1335 * Show link status of the bond interface.
1336 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001337static ssize_t bonding_show_mii_status(struct device *d,
1338 struct device_attribute *attr,
1339 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001340{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001341 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001342
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001343 return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001344}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001345static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001346
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001347/*
1348 * Show current 802.3ad aggregator ID.
1349 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001350static ssize_t bonding_show_ad_aggregator(struct device *d,
1351 struct device_attribute *attr,
1352 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001353{
1354 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001355 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001356
1357 if (bond->params.mode == BOND_MODE_8023AD) {
1358 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001359 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001360 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001361 ? 0 : ad_info.aggregator_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001362 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001363
1364 return count;
1365}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001366static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001367
1368
1369/*
1370 * Show number of active 802.3ad ports.
1371 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001372static ssize_t bonding_show_ad_num_ports(struct device *d,
1373 struct device_attribute *attr,
1374 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001375{
1376 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001377 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001378
1379 if (bond->params.mode == BOND_MODE_8023AD) {
1380 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001381 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001382 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001383 ? 0 : ad_info.ports);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001384 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001385
1386 return count;
1387}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001388static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001389
1390
1391/*
1392 * Show current 802.3ad actor key.
1393 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001394static ssize_t bonding_show_ad_actor_key(struct device *d,
1395 struct device_attribute *attr,
1396 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001397{
1398 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001399 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001400
1401 if (bond->params.mode == BOND_MODE_8023AD) {
1402 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001403 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001404 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001405 ? 0 : ad_info.actor_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001406 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001407
1408 return count;
1409}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001410static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001411
1412
1413/*
1414 * Show current 802.3ad partner key.
1415 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001416static ssize_t bonding_show_ad_partner_key(struct device *d,
1417 struct device_attribute *attr,
1418 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001419{
1420 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001421 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001422
1423 if (bond->params.mode == BOND_MODE_8023AD) {
1424 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001425 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001426 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001427 ? 0 : ad_info.partner_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001428 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001429
1430 return count;
1431}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001432static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001433
1434
1435/*
1436 * Show current 802.3ad partner mac.
1437 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001438static ssize_t bonding_show_ad_partner_mac(struct device *d,
1439 struct device_attribute *attr,
1440 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001441{
1442 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001443 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001444
1445 if (bond->params.mode == BOND_MODE_8023AD) {
1446 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001447 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
Johannes Berge1749612008-10-27 15:59:26 -07001448 count = sprintf(buf, "%pM\n", ad_info.partner_system);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001449 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001450
1451 return count;
1452}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001453static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001454
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001455/*
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001456 * Show the queue_ids of the slaves in the current bond.
1457 */
1458static ssize_t bonding_show_queue_id(struct device *d,
1459 struct device_attribute *attr,
1460 char *buf)
1461{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001462 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001463 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001464 struct slave *slave;
1465 int res = 0;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001466
1467 if (!rtnl_trylock())
1468 return restart_syscall();
1469
1470 read_lock(&bond->lock);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001471 bond_for_each_slave(bond, slave, iter) {
Nicolas de Pesloüan79236682010-07-14 18:24:54 -07001472 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1473 /* not enough space for another interface_name:queue_id pair */
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001474 if ((PAGE_SIZE - res) > 10)
1475 res = PAGE_SIZE - 10;
1476 res += sprintf(buf + res, "++more++ ");
1477 break;
1478 }
1479 res += sprintf(buf + res, "%s:%d ",
1480 slave->dev->name, slave->queue_id);
1481 }
1482 read_unlock(&bond->lock);
1483 if (res)
1484 buf[res-1] = '\n'; /* eat the leftover space */
1485 rtnl_unlock();
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001486
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001487 return res;
1488}
1489
1490/*
1491 * Set the queue_ids of the slaves in the current bond. The bond
1492 * interface must be enslaved for this to work.
1493 */
1494static ssize_t bonding_store_queue_id(struct device *d,
1495 struct device_attribute *attr,
1496 const char *buffer, size_t count)
1497{
1498 struct slave *slave, *update_slave;
1499 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001500 struct list_head *iter;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001501 u16 qid;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001502 int ret = count;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001503 char *delim;
1504 struct net_device *sdev = NULL;
1505
1506 if (!rtnl_trylock())
1507 return restart_syscall();
1508
1509 /* delim will point to queue id if successful */
1510 delim = strchr(buffer, ':');
1511 if (!delim)
1512 goto err_no_cmd;
1513
1514 /*
1515 * Terminate string that points to device name and bump it
1516 * up one, so we can read the queue id there.
1517 */
1518 *delim = '\0';
1519 if (sscanf(++delim, "%hd\n", &qid) != 1)
1520 goto err_no_cmd;
1521
1522 /* Check buffer length, valid ifname and queue id */
1523 if (strlen(buffer) > IFNAMSIZ ||
1524 !dev_valid_name(buffer) ||
Jiri Pirko8a540ff2012-07-20 02:28:50 +00001525 qid > bond->dev->real_num_tx_queues)
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001526 goto err_no_cmd;
1527
1528 /* Get the pointer to that interface if it exists */
1529 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1530 if (!sdev)
1531 goto err_no_cmd;
1532
1533 read_lock(&bond->lock);
1534
1535 /* Search for thes slave and check for duplicate qids */
1536 update_slave = NULL;
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001537 bond_for_each_slave(bond, slave, iter) {
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001538 if (sdev == slave->dev)
1539 /*
1540 * We don't need to check the matching
1541 * slave for dups, since we're overwriting it
1542 */
1543 update_slave = slave;
1544 else if (qid && qid == slave->queue_id) {
1545 goto err_no_cmd_unlock;
1546 }
1547 }
1548
1549 if (!update_slave)
1550 goto err_no_cmd_unlock;
1551
1552 /* Actually set the qids for the slave */
1553 update_slave->queue_id = qid;
1554
1555 read_unlock(&bond->lock);
1556out:
1557 rtnl_unlock();
1558 return ret;
1559
1560err_no_cmd_unlock:
1561 read_unlock(&bond->lock);
1562err_no_cmd:
1563 pr_info("invalid input for queue_id set for %s.\n",
1564 bond->dev->name);
1565 ret = -EPERM;
1566 goto out;
1567}
1568
1569static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1570 bonding_store_queue_id);
1571
1572
1573/*
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001574 * Show and set the all_slaves_active flag.
1575 */
1576static ssize_t bonding_show_slaves_active(struct device *d,
1577 struct device_attribute *attr,
1578 char *buf)
1579{
1580 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001581
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001582 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1583}
1584
1585static ssize_t bonding_store_slaves_active(struct device *d,
1586 struct device_attribute *attr,
1587 const char *buf, size_t count)
1588{
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001589 struct bonding *bond = to_bond(d);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001590 int new_value, ret = count;
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001591 struct list_head *iter;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001592 struct slave *slave;
1593
1594 if (sscanf(buf, "%d", &new_value) != 1) {
1595 pr_err("%s: no all_slaves_active value specified.\n",
1596 bond->dev->name);
1597 ret = -EINVAL;
1598 goto out;
1599 }
1600
1601 if (new_value == bond->params.all_slaves_active)
1602 goto out;
1603
1604 if ((new_value == 0) || (new_value == 1)) {
1605 bond->params.all_slaves_active = new_value;
1606 } else {
1607 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1608 bond->dev->name, new_value);
1609 ret = -EINVAL;
1610 goto out;
1611 }
1612
nikolay@redhat.come196c0e2012-11-29 01:37:59 +00001613 read_lock(&bond->lock);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001614 bond_for_each_slave(bond, slave, iter) {
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001615 if (!bond_is_active_slave(slave)) {
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001616 if (new_value)
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001617 slave->inactive = 0;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001618 else
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001619 slave->inactive = 1;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001620 }
1621 }
nikolay@redhat.come196c0e2012-11-29 01:37:59 +00001622 read_unlock(&bond->lock);
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001623out:
Jiri Pirko672bda32011-01-25 11:03:25 +00001624 return ret;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001625}
1626static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1627 bonding_show_slaves_active, bonding_store_slaves_active);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001628
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001629/*
1630 * Show and set the number of IGMP membership reports to send on link failure
1631 */
1632static ssize_t bonding_show_resend_igmp(struct device *d,
Flavio Leitner94265cf2011-05-25 08:38:58 +00001633 struct device_attribute *attr,
1634 char *buf)
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001635{
1636 struct bonding *bond = to_bond(d);
1637
1638 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1639}
1640
1641static ssize_t bonding_store_resend_igmp(struct device *d,
Flavio Leitner94265cf2011-05-25 08:38:58 +00001642 struct device_attribute *attr,
1643 const char *buf, size_t count)
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001644{
1645 int new_value, ret = count;
1646 struct bonding *bond = to_bond(d);
1647
1648 if (sscanf(buf, "%d", &new_value) != 1) {
1649 pr_err("%s: no resend_igmp value specified.\n",
1650 bond->dev->name);
1651 ret = -EINVAL;
1652 goto out;
1653 }
1654
Flavio Leitner94265cf2011-05-25 08:38:58 +00001655 if (new_value < 0 || new_value > 255) {
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001656 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1657 bond->dev->name, new_value);
1658 ret = -EINVAL;
1659 goto out;
1660 }
1661
1662 pr_info("%s: Setting resend_igmp to %d.\n",
1663 bond->dev->name, new_value);
1664 bond->params.resend_igmp = new_value;
1665out:
1666 return ret;
1667}
1668
1669static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1670 bonding_show_resend_igmp, bonding_store_resend_igmp);
1671
Neil Horman7eacd032013-09-13 11:05:33 -04001672
1673static ssize_t bonding_show_lp_interval(struct device *d,
1674 struct device_attribute *attr,
1675 char *buf)
1676{
1677 struct bonding *bond = to_bond(d);
1678 return sprintf(buf, "%d\n", bond->params.lp_interval);
1679}
1680
1681static ssize_t bonding_store_lp_interval(struct device *d,
1682 struct device_attribute *attr,
1683 const char *buf, size_t count)
1684{
1685 struct bonding *bond = to_bond(d);
1686 int new_value, ret = count;
1687
1688 if (sscanf(buf, "%d", &new_value) != 1) {
1689 pr_err("%s: no lp interval value specified.\n",
1690 bond->dev->name);
1691 ret = -EINVAL;
1692 goto out;
1693 }
1694
1695 if (new_value <= 0) {
1696 pr_err ("%s: lp_interval must be between 1 and %d\n",
1697 bond->dev->name, INT_MAX);
1698 ret = -EINVAL;
1699 goto out;
1700 }
1701
1702 bond->params.lp_interval = new_value;
1703out:
1704 return ret;
1705}
1706
1707static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1708 bonding_show_lp_interval, bonding_store_lp_interval);
1709
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001710static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001711 &dev_attr_slaves.attr,
1712 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001713 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001714 &dev_attr_arp_validate.attr,
Veaceslav Falico8599b522013-06-24 11:49:34 +02001715 &dev_attr_arp_all_targets.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001716 &dev_attr_arp_interval.attr,
1717 &dev_attr_arp_ip_target.attr,
1718 &dev_attr_downdelay.attr,
1719 &dev_attr_updelay.attr,
1720 &dev_attr_lacp_rate.attr,
Jay Vosburghfd989c82008-11-04 17:51:16 -08001721 &dev_attr_ad_select.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001722 &dev_attr_xmit_hash_policy.attr,
Ben Hutchingsad246c92011-04-26 15:25:52 +00001723 &dev_attr_num_grat_arp.attr,
1724 &dev_attr_num_unsol_na.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001725 &dev_attr_miimon.attr,
1726 &dev_attr_primary.attr,
Jiri Pirkoa5499522009-09-25 03:28:09 +00001727 &dev_attr_primary_reselect.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001728 &dev_attr_use_carrier.attr,
1729 &dev_attr_active_slave.attr,
1730 &dev_attr_mii_status.attr,
1731 &dev_attr_ad_aggregator.attr,
1732 &dev_attr_ad_num_ports.attr,
1733 &dev_attr_ad_actor_key.attr,
1734 &dev_attr_ad_partner_key.attr,
1735 &dev_attr_ad_partner_mac.attr,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001736 &dev_attr_queue_id.attr,
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001737 &dev_attr_all_slaves_active.attr,
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001738 &dev_attr_resend_igmp.attr,
stephen hemminger655f8912011-06-22 09:54:39 +00001739 &dev_attr_min_links.attr,
Neil Horman7eacd032013-09-13 11:05:33 -04001740 &dev_attr_lp_interval.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001741 NULL,
1742};
1743
1744static struct attribute_group bonding_group = {
1745 .name = "bonding",
1746 .attrs = per_bond_attrs,
1747};
1748
1749/*
1750 * Initialize sysfs. This sets up the bonding_masters file in
1751 * /sys/class/net.
1752 */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001753int bond_create_sysfs(struct bond_net *bn)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001754{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001755 int ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001756
Eric W. Biederman4c224002011-10-12 21:56:25 +00001757 bn->class_attr_bonding_masters = class_attr_bonding_masters;
Eric W. Biederman01718e32011-10-21 22:43:07 +00001758 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
Eric W. Biederman4c224002011-10-12 21:56:25 +00001759
1760 ret = netdev_class_create_file(&bn->class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001761 /*
1762 * Permit multiple loads of the module by ignoring failures to
1763 * create the bonding_masters sysfs file. Bonding devices
1764 * created by second or subsequent loads of the module will
1765 * not be listed in, or controllable by, bonding_masters, but
1766 * will have the usual "bonding" sysfs directory.
1767 *
1768 * This is done to preserve backwards compatibility for
1769 * initscripts/sysconfig, which load bonding multiple times to
1770 * configure multiple bonding devices.
1771 */
1772 if (ret == -EEXIST) {
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001773 /* Is someone being kinky and naming a device bonding_master? */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001774 if (__dev_get_by_name(bn->net,
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001775 class_attr_bonding_masters.attr.name))
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001776 pr_err("network device named %s already exists in sysfs",
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001777 class_attr_bonding_masters.attr.name);
Stephen Hemminger130aa612009-06-11 05:46:04 -07001778 ret = 0;
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001779 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001780
1781 return ret;
1782
1783}
1784
1785/*
1786 * Remove /sys/class/net/bonding_masters.
1787 */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001788void bond_destroy_sysfs(struct bond_net *bn)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001789{
Eric W. Biederman4c224002011-10-12 21:56:25 +00001790 netdev_class_remove_file(&bn->class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001791}
1792
1793/*
1794 * Initialize sysfs for each bond. This sets up and registers
1795 * the 'bondctl' directory for each individual bond under /sys/class/net.
1796 */
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001797void bond_prepare_sysfs_group(struct bonding *bond)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001798{
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001799 bond->dev->sysfs_groups[0] = &bonding_group;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001800}
1801