blob: 03bed0ca935e8bf9476901b1ea7e08c99f4ea4ce [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
dingtianhong4d1ae5f2013-10-15 16:28:42 +0800182 if (!rtnl_trylock())
183 return restart_syscall();
184
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200185 bond_for_each_slave(bond, slave, iter) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800186 if (res > (PAGE_SIZE - IFNAMSIZ)) {
187 /* not enough space for another interface name */
188 if ((PAGE_SIZE - res) > 10)
189 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800190 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800191 break;
192 }
193 res += sprintf(buf + res, "%s ", slave->dev->name);
194 }
dingtianhong4d1ae5f2013-10-15 16:28:42 +0800195
196 rtnl_unlock();
197
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800198 if (res)
199 buf[res-1] = '\n'; /* eat the leftover space */
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200200
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800201 return res;
202}
203
204/*
Veaceslav Falicod6641cc2013-05-28 01:26:13 +0000205 * Set the slaves in the current bond.
Jiri Pirkof9f35452010-05-18 05:46:39 +0000206 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
207 * All hard work should be done there.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800208 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700209static ssize_t bonding_store_slaves(struct device *d,
210 struct device_attribute *attr,
211 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800212{
213 char command[IFNAMSIZ + 1] = { 0, };
214 char *ifname;
Jiri Pirkof9f35452010-05-18 05:46:39 +0000215 int res, ret = count;
216 struct net_device *dev;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700217 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800218
Eric W. Biederman496a60c2009-05-13 17:02:50 +0000219 if (!rtnl_trylock())
220 return restart_syscall();
Jay Vosburgh027ea042008-01-17 16:25:02 -0800221
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800222 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
223 ifname = command + 1;
224 if ((strlen(command) <= 1) ||
225 !dev_valid_name(ifname))
226 goto err_no_cmd;
227
Jiri Pirkof9f35452010-05-18 05:46:39 +0000228 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
229 if (!dev) {
230 pr_info("%s: Interface %s does not exist!\n",
231 bond->dev->name, ifname);
232 ret = -ENODEV;
233 goto out;
234 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800235
Jiri Pirkof9f35452010-05-18 05:46:39 +0000236 switch (command[0]) {
237 case '+':
238 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800239 res = bond_enslave(bond->dev, dev);
Jiri Pirkof9f35452010-05-18 05:46:39 +0000240 break;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000241
Jiri Pirkof9f35452010-05-18 05:46:39 +0000242 case '-':
243 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
244 res = bond_release(bond->dev, dev);
245 break;
246
247 default:
248 goto err_no_cmd;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800249 }
250
Jiri Pirkof9f35452010-05-18 05:46:39 +0000251 if (res)
252 ret = res;
253 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800254
255err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800256 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
257 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800258 ret = -EPERM;
259
260out:
Jay Vosburgh027ea042008-01-17 16:25:02 -0800261 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800262 return ret;
263}
264
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000265static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
266 bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800267
268/*
269 * Show and set the bonding mode. The bond interface must be down to
270 * change the mode.
271 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700272static ssize_t bonding_show_mode(struct device *d,
273 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800274{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700275 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800276
277 return sprintf(buf, "%s %d\n",
278 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800279 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800280}
281
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700282static ssize_t bonding_store_mode(struct device *d,
283 struct device_attribute *attr,
284 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800285{
286 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700287 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800288
nikolay@redhat.comea6836d2013-05-18 01:18:28 +0000289 if (!rtnl_trylock())
290 return restart_syscall();
291
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800292 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800293 pr_err("unable to update mode of %s because interface is up.\n",
294 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800295 ret = -EPERM;
296 goto out;
297 }
298
Veaceslav Falico0965a1f2013-09-25 09:20:21 +0200299 if (bond_has_slaves(bond)) {
Veaceslav Falico4a8bb7e2011-11-15 06:44:42 +0000300 pr_err("unable to update mode of %s because it has slaves.\n",
301 bond->dev->name);
302 ret = -EPERM;
303 goto out;
304 }
305
Jay Vosburghece95f72008-01-17 16:25:01 -0800306 new_value = bond_parse_parm(buf, bond_mode_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800307 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800308 pr_err("%s: Ignoring invalid mode value %.*s.\n",
309 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800310 ret = -EINVAL;
311 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800312 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000313 if ((new_value == BOND_MODE_ALB ||
314 new_value == BOND_MODE_TLB) &&
315 bond->params.arp_interval) {
316 pr_err("%s: %s mode is incompatible with arp monitoring.\n",
317 bond->dev->name, bond_mode_tbl[new_value].modename);
318 ret = -EINVAL;
319 goto out;
320 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000321
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200322 /* don't cache arp_validate between modes */
323 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000324 bond->params.mode = new_value;
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000325 pr_info("%s: setting mode to %s (%d).\n",
326 bond->dev->name, bond_mode_tbl[new_value].modename,
327 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800328out:
nikolay@redhat.comea6836d2013-05-18 01:18:28 +0000329 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800330 return ret;
331}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000332static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
333 bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800334
335/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000336 * Show and set the bonding transmit hash method.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800337 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700338static ssize_t bonding_show_xmit_hash(struct device *d,
339 struct device_attribute *attr,
340 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800341{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700342 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800343
Wagner Ferenc8e4b9322007-12-06 23:40:32 -0800344 return sprintf(buf, "%s %d\n",
345 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
346 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800347}
348
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700349static ssize_t bonding_store_xmit_hash(struct device *d,
350 struct device_attribute *attr,
351 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800352{
353 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700354 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800355
Jay Vosburghece95f72008-01-17 16:25:01 -0800356 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800357 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800358 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800359 bond->dev->name,
360 (int)strlen(buf) - 1, buf);
361 ret = -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800362 } else {
363 bond->params.xmit_policy = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800364 pr_info("%s: setting xmit hash policy to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000365 bond->dev->name,
366 xmit_hashtype_tbl[new_value].modename, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800367 }
Nikolay Aleksandrov53edee22013-05-24 00:59:47 +0000368
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800369 return ret;
370}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000371static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
372 bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800373
374/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700375 * Show and set arp_validate.
376 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700377static ssize_t bonding_show_arp_validate(struct device *d,
378 struct device_attribute *attr,
379 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700380{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700381 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700382
383 return sprintf(buf, "%s %d\n",
384 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800385 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700386}
387
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700388static ssize_t bonding_store_arp_validate(struct device *d,
389 struct device_attribute *attr,
390 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700391{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700392 struct bonding *bond = to_bond(d);
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200393 int new_value, ret = count;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700394
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200395 if (!rtnl_trylock())
396 return restart_syscall();
Jay Vosburghece95f72008-01-17 16:25:01 -0800397 new_value = bond_parse_parm(buf, arp_validate_tbl);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700398 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800399 pr_err("%s: Ignoring invalid arp_validate value %s\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700400 bond->dev->name, buf);
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200401 ret = -EINVAL;
402 goto out;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700403 }
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200404 if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800405 pr_err("%s: arp_validate only supported in active-backup mode.\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700406 bond->dev->name);
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200407 ret = -EINVAL;
408 goto out;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700409 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800410 pr_info("%s: setting arp_validate to %s (%d).\n",
411 bond->dev->name, arp_validate_tbl[new_value].modename,
412 new_value);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700413
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200414 if (bond->dev->flags & IFF_UP) {
415 if (!new_value)
416 bond->recv_probe = NULL;
417 else if (bond->params.arp_interval)
418 bond->recv_probe = bond_arp_rcv;
419 }
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700420 bond->params.arp_validate = new_value;
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200421out:
422 rtnl_unlock();
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700423
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200424 return ret;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700425}
426
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000427static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
428 bonding_store_arp_validate);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200429/*
430 * Show and set arp_all_targets.
431 */
432static ssize_t bonding_show_arp_all_targets(struct device *d,
433 struct device_attribute *attr,
434 char *buf)
435{
436 struct bonding *bond = to_bond(d);
437 int value = bond->params.arp_all_targets;
438
439 return sprintf(buf, "%s %d\n", arp_all_targets_tbl[value].modename,
440 value);
441}
442
443static ssize_t bonding_store_arp_all_targets(struct device *d,
444 struct device_attribute *attr,
445 const char *buf, size_t count)
446{
447 struct bonding *bond = to_bond(d);
448 int new_value;
449
450 new_value = bond_parse_parm(buf, arp_all_targets_tbl);
451 if (new_value < 0) {
452 pr_err("%s: Ignoring invalid arp_all_targets value %s\n",
453 bond->dev->name, buf);
454 return -EINVAL;
455 }
456 pr_info("%s: setting arp_all_targets to %s (%d).\n",
457 bond->dev->name, arp_all_targets_tbl[new_value].modename,
458 new_value);
459
460 bond->params.arp_all_targets = new_value;
461
462 return count;
463}
464
465static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
466 bonding_show_arp_all_targets, bonding_store_arp_all_targets);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700467
468/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700469 * Show and store fail_over_mac. User only allowed to change the
470 * value when there are no slaves.
471 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000472static ssize_t bonding_show_fail_over_mac(struct device *d,
473 struct device_attribute *attr,
474 char *buf)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700475{
476 struct bonding *bond = to_bond(d);
477
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700478 return sprintf(buf, "%s %d\n",
479 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
480 bond->params.fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700481}
482
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000483static ssize_t bonding_store_fail_over_mac(struct device *d,
484 struct device_attribute *attr,
485 const char *buf, size_t count)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700486{
dingtianhong9402b742013-07-23 15:25:39 +0800487 int new_value, ret = count;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700488 struct bonding *bond = to_bond(d);
489
dingtianhong9402b742013-07-23 15:25:39 +0800490 if (!rtnl_trylock())
491 return restart_syscall();
492
Veaceslav Falico0965a1f2013-09-25 09:20:21 +0200493 if (bond_has_slaves(bond)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800494 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
Jay Vosburghdd957c52007-10-09 19:57:24 -0700495 bond->dev->name);
dingtianhong9402b742013-07-23 15:25:39 +0800496 ret = -EPERM;
497 goto out;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700498 }
499
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700500 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
501 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800502 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700503 bond->dev->name, buf);
dingtianhong9402b742013-07-23 15:25:39 +0800504 ret = -EINVAL;
505 goto out;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700506 }
507
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700508 bond->params.fail_over_mac = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800509 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
510 bond->dev->name, fail_over_mac_tbl[new_value].modename,
511 new_value);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700512
dingtianhong9402b742013-07-23 15:25:39 +0800513out:
514 rtnl_unlock();
515 return ret;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700516}
517
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000518static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
519 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700520
521/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800522 * Show and set the arp timer interval. There are two tricky bits
523 * here. First, if ARP monitoring is activated, then we must disable
524 * MII monitoring. Second, if the ARP timer isn't running, we must
525 * start it.
526 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700527static ssize_t bonding_show_arp_interval(struct device *d,
528 struct device_attribute *attr,
529 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800530{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700531 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800532
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800533 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800534}
535
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700536static ssize_t bonding_store_arp_interval(struct device *d,
537 struct device_attribute *attr,
538 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800539{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700540 struct bonding *bond = to_bond(d);
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200541 int new_value, ret = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800542
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000543 if (!rtnl_trylock())
544 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800545 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800546 pr_err("%s: no arp_interval value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800547 bond->dev->name);
548 ret = -EINVAL;
549 goto out;
550 }
551 if (new_value < 0) {
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000552 pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800553 bond->dev->name, new_value, INT_MAX);
554 ret = -EINVAL;
555 goto out;
556 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000557 if (bond->params.mode == BOND_MODE_ALB ||
558 bond->params.mode == BOND_MODE_TLB) {
559 pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
560 bond->dev->name, bond->dev->name);
561 ret = -EINVAL;
562 goto out;
563 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800564 pr_info("%s: Setting ARP monitoring interval to %d.\n",
565 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800566 bond->params.arp_interval = new_value;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000567 if (new_value) {
568 if (bond->params.miimon) {
569 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
570 bond->dev->name, bond->dev->name);
571 bond->params.miimon = 0;
572 }
573 if (!bond->params.arp_targets[0])
574 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
575 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800576 }
577 if (bond->dev->flags & IFF_UP) {
578 /* If the interface is up, we may need to fire off
579 * the ARP timer. If the interface is down, the
580 * timer will get fired off when the open function
581 * is called.
582 */
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000583 if (!new_value) {
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200584 if (bond->params.arp_validate)
585 bond->recv_probe = NULL;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000586 cancel_delayed_work_sync(&bond->arp_work);
587 } else {
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200588 /* arp_validate can be set only in active-backup mode */
589 if (bond->params.arp_validate)
590 bond->recv_probe = bond_arp_rcv;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000591 cancel_delayed_work_sync(&bond->mii_work);
592 queue_delayed_work(bond->wq, &bond->arp_work, 0);
593 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800594 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800595out:
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000596 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800597 return ret;
598}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000599static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
600 bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800601
602/*
603 * Show and set the arp targets.
604 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700605static ssize_t bonding_show_arp_targets(struct device *d,
606 struct device_attribute *attr,
607 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800608{
609 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700610 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800611
612 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
613 if (bond->params.arp_targets[i])
Harvey Harrison63779432008-10-31 00:56:00 -0700614 res += sprintf(buf + res, "%pI4 ",
615 &bond->params.arp_targets[i]);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800616 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800617 if (res)
618 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800619 return res;
620}
621
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700622static ssize_t bonding_store_arp_targets(struct device *d,
623 struct device_attribute *attr,
624 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800625{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700626 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200627 struct list_head *iter;
Veaceslav Falico8599b522013-06-24 11:49:34 +0200628 struct slave *slave;
629 __be32 newtarget, *targets;
630 unsigned long *targets_rx;
631 int ind, i, j, ret = -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800632
dingtianhong4d1ae5f2013-10-15 16:28:42 +0800633 if (!rtnl_trylock())
634 return restart_syscall();
635
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800636 targets = bond->params.arp_targets;
637 newtarget = in_aton(buf + 1);
638 /* look for adds */
639 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400640 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800641 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700642 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800643 goto out;
644 }
645
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200646 if (bond_get_targets_ip(targets, newtarget) != -1) { /* dup */
647 pr_err("%s: ARP target %pI4 is already present\n",
648 bond->dev->name, &newtarget);
649 goto out;
650 }
651
Veaceslav Falico8599b522013-06-24 11:49:34 +0200652 ind = bond_get_targets_ip(targets, 0); /* first free slot */
653 if (ind == -1) {
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200654 pr_err("%s: ARP target table is full!\n",
655 bond->dev->name);
656 goto out;
657 }
658
659 pr_info("%s: adding ARP target %pI4.\n", bond->dev->name,
660 &newtarget);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200661 /* not to race with bond_arp_rcv */
662 write_lock_bh(&bond->lock);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200663 bond_for_each_slave(bond, slave, iter)
Veaceslav Falico8599b522013-06-24 11:49:34 +0200664 slave->target_last_arp_rx[ind] = jiffies;
665 targets[ind] = newtarget;
666 write_unlock_bh(&bond->lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000667 } else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400668 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800669 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700670 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800671 goto out;
672 }
673
Veaceslav Falico8599b522013-06-24 11:49:34 +0200674 ind = bond_get_targets_ip(targets, newtarget);
675 if (ind == -1) {
676 pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800677 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800678 goto out;
679 }
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200680
Veaceslav Falico8599b522013-06-24 11:49:34 +0200681 if (ind == 0 && !targets[1] && bond->params.arp_interval)
682 pr_warn("%s: removing last arp target with arp_interval on\n",
683 bond->dev->name);
684
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200685 pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
686 &newtarget);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200687
688 write_lock_bh(&bond->lock);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200689 bond_for_each_slave(bond, slave, iter) {
Veaceslav Falico8599b522013-06-24 11:49:34 +0200690 targets_rx = slave->target_last_arp_rx;
691 j = ind;
692 for (; (j < BOND_MAX_ARP_TARGETS-1) && targets[j+1]; j++)
693 targets_rx[j] = targets_rx[j+1];
694 targets_rx[j] = 0;
695 }
696 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200697 targets[i] = targets[i+1];
698 targets[i] = 0;
Veaceslav Falico8599b522013-06-24 11:49:34 +0200699 write_unlock_bh(&bond->lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000700 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800701 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
702 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800703 ret = -EPERM;
704 goto out;
705 }
706
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200707 ret = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800708out:
dingtianhong4d1ae5f2013-10-15 16:28:42 +0800709 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800710 return ret;
711}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700712static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800713
714/*
715 * Show and set the up and down delays. These must be multiples of the
716 * MII monitoring value, and are stored internally as the multiplier.
717 * Thus, we must translate to MS for the real world.
718 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700719static ssize_t bonding_show_downdelay(struct device *d,
720 struct device_attribute *attr,
721 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800722{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700723 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800724
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800725 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800726}
727
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700728static ssize_t bonding_store_downdelay(struct device *d,
729 struct device_attribute *attr,
730 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800731{
732 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700733 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800734
735 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800736 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800737 bond->dev->name);
738 ret = -EPERM;
739 goto out;
740 }
741
742 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800743 pr_err("%s: no down delay value specified.\n", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800744 ret = -EINVAL;
745 goto out;
746 }
747 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800748 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000749 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800750 ret = -EINVAL;
751 goto out;
752 } else {
753 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800754 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 +0000755 bond->dev->name, new_value,
756 bond->params.miimon,
757 (new_value / bond->params.miimon) *
758 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800759 }
760 bond->params.downdelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800761 pr_info("%s: Setting down delay to %d.\n",
762 bond->dev->name,
763 bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800764
765 }
766
767out:
768 return ret;
769}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000770static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
771 bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800772
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700773static ssize_t bonding_show_updelay(struct device *d,
774 struct device_attribute *attr,
775 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800776{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700777 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800778
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800779 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800780
781}
782
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700783static ssize_t bonding_store_updelay(struct device *d,
784 struct device_attribute *attr,
785 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800786{
787 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700788 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800789
790 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800791 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800792 bond->dev->name);
793 ret = -EPERM;
794 goto out;
795 }
796
797 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800798 pr_err("%s: no up delay value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800799 bond->dev->name);
800 ret = -EINVAL;
801 goto out;
802 }
803 if (new_value < 0) {
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000804 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
805 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800806 ret = -EINVAL;
807 goto out;
808 } else {
809 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800810 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 +0000811 bond->dev->name, new_value,
812 bond->params.miimon,
813 (new_value / bond->params.miimon) *
814 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800815 }
816 bond->params.updelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800817 pr_info("%s: Setting up delay to %d.\n",
818 bond->dev->name,
819 bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800820 }
821
822out:
823 return ret;
824}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000825static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
826 bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800827
828/*
829 * Show and set the LACP interval. Interface must be down, and the mode
830 * must be set to 802.3ad mode.
831 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700832static ssize_t bonding_show_lacp(struct device *d,
833 struct device_attribute *attr,
834 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800835{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700836 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800837
838 return sprintf(buf, "%s %d\n",
839 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800840 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800841}
842
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700843static ssize_t bonding_store_lacp(struct device *d,
844 struct device_attribute *attr,
845 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800846{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700847 struct bonding *bond = to_bond(d);
nikolay@redhat.comc5093162013-09-02 13:51:40 +0200848 int new_value, ret = count;
849
850 if (!rtnl_trylock())
851 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800852
853 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800854 pr_err("%s: Unable to update LACP rate because interface is up.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800855 bond->dev->name);
856 ret = -EPERM;
857 goto out;
858 }
859
860 if (bond->params.mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800861 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800862 bond->dev->name);
863 ret = -EPERM;
864 goto out;
865 }
866
Jay Vosburghece95f72008-01-17 16:25:01 -0800867 new_value = bond_parse_parm(buf, bond_lacp_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800868
869 if ((new_value == 1) || (new_value == 0)) {
870 bond->params.lacp_fast = new_value;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +0000871 bond_3ad_update_lacp_rate(bond);
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800872 pr_info("%s: Setting LACP rate to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000873 bond->dev->name, bond_lacp_tbl[new_value].modename,
874 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800875 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800876 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000877 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800878 ret = -EINVAL;
879 }
880out:
nikolay@redhat.comc5093162013-09-02 13:51:40 +0200881 rtnl_unlock();
882
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800883 return ret;
884}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000885static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
886 bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800887
stephen hemminger655f8912011-06-22 09:54:39 +0000888static ssize_t bonding_show_min_links(struct device *d,
889 struct device_attribute *attr,
890 char *buf)
891{
892 struct bonding *bond = to_bond(d);
893
894 return sprintf(buf, "%d\n", bond->params.min_links);
895}
896
897static ssize_t bonding_store_min_links(struct device *d,
898 struct device_attribute *attr,
899 const char *buf, size_t count)
900{
901 struct bonding *bond = to_bond(d);
902 int ret;
903 unsigned int new_value;
904
905 ret = kstrtouint(buf, 0, &new_value);
906 if (ret < 0) {
907 pr_err("%s: Ignoring invalid min links value %s.\n",
908 bond->dev->name, buf);
909 return ret;
910 }
911
912 pr_info("%s: Setting min links value to %u\n",
913 bond->dev->name, new_value);
914 bond->params.min_links = new_value;
915 return count;
916}
917static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
918 bonding_show_min_links, bonding_store_min_links);
919
Jay Vosburghfd989c82008-11-04 17:51:16 -0800920static ssize_t bonding_show_ad_select(struct device *d,
921 struct device_attribute *attr,
922 char *buf)
923{
924 struct bonding *bond = to_bond(d);
925
926 return sprintf(buf, "%s %d\n",
927 ad_select_tbl[bond->params.ad_select].modename,
928 bond->params.ad_select);
929}
930
931
932static ssize_t bonding_store_ad_select(struct device *d,
933 struct device_attribute *attr,
934 const char *buf, size_t count)
935{
936 int new_value, ret = count;
937 struct bonding *bond = to_bond(d);
938
939 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800940 pr_err("%s: Unable to update ad_select because interface is up.\n",
941 bond->dev->name);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800942 ret = -EPERM;
943 goto out;
944 }
945
946 new_value = bond_parse_parm(buf, ad_select_tbl);
947
948 if (new_value != -1) {
949 bond->params.ad_select = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800950 pr_info("%s: Setting ad_select to %s (%d).\n",
951 bond->dev->name, ad_select_tbl[new_value].modename,
952 new_value);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800953 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800954 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -0800955 bond->dev->name, (int)strlen(buf) - 1, buf);
956 ret = -EINVAL;
957 }
958out:
959 return ret;
960}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000961static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
962 bonding_show_ad_select, bonding_store_ad_select);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800963
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800964/*
Ben Hutchingsad246c92011-04-26 15:25:52 +0000965 * Show and set the number of peer notifications to send after a failover event.
966 */
967static ssize_t bonding_show_num_peer_notif(struct device *d,
968 struct device_attribute *attr,
969 char *buf)
970{
971 struct bonding *bond = to_bond(d);
972 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
973}
974
975static ssize_t bonding_store_num_peer_notif(struct device *d,
976 struct device_attribute *attr,
977 const char *buf, size_t count)
978{
979 struct bonding *bond = to_bond(d);
980 int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
981 return err ? err : count;
982}
983static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
984 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
985static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
986 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
987
988/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800989 * Show and set the MII monitor interval. There are two tricky bits
990 * here. First, if MII monitoring is activated, then we must disable
991 * ARP monitoring. Second, if the timer isn't running, we must
992 * start it.
993 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700994static ssize_t bonding_show_miimon(struct device *d,
995 struct device_attribute *attr,
996 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800997{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700998 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800999
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001000 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001001}
1002
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001003static ssize_t bonding_store_miimon(struct device *d,
1004 struct device_attribute *attr,
1005 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001006{
1007 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001008 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001009
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00001010 if (!rtnl_trylock())
1011 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001012 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001013 pr_err("%s: no miimon value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001014 bond->dev->name);
1015 ret = -EINVAL;
1016 goto out;
1017 }
1018 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001019 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +00001020 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001021 ret = -EINVAL;
1022 goto out;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +00001023 }
1024 pr_info("%s: Setting MII monitoring interval to %d.\n",
1025 bond->dev->name, new_value);
1026 bond->params.miimon = new_value;
1027 if (bond->params.updelay)
1028 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
1029 bond->dev->name,
1030 bond->params.updelay * bond->params.miimon);
1031 if (bond->params.downdelay)
1032 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
1033 bond->dev->name,
1034 bond->params.downdelay * bond->params.miimon);
1035 if (new_value && bond->params.arp_interval) {
1036 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
1037 bond->dev->name);
1038 bond->params.arp_interval = 0;
1039 if (bond->params.arp_validate)
1040 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
1041 }
1042 if (bond->dev->flags & IFF_UP) {
1043 /* If the interface is up, we may need to fire off
1044 * the MII timer. If the interface is down, the
1045 * timer will get fired off when the open function
1046 * is called.
1047 */
1048 if (!new_value) {
1049 cancel_delayed_work_sync(&bond->mii_work);
1050 } else {
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00001051 cancel_delayed_work_sync(&bond->arp_work);
1052 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001053 }
1054 }
1055out:
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00001056 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001057 return ret;
1058}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001059static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1060 bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001061
1062/*
1063 * Show and set the primary slave. The store function is much
1064 * simpler than bonding_store_slaves function because it only needs to
1065 * handle one interface name.
1066 * The bond must be a mode that supports a primary for this be
1067 * set.
1068 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001069static ssize_t bonding_show_primary(struct device *d,
1070 struct device_attribute *attr,
1071 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001072{
1073 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001074 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001075
1076 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001077 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001078
1079 return count;
1080}
1081
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001082static ssize_t bonding_store_primary(struct device *d,
1083 struct device_attribute *attr,
1084 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001085{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001086 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001087 struct list_head *iter;
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001088 char ifname[IFNAMSIZ];
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001089 struct slave *slave;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001090
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001091 if (!rtnl_trylock())
1092 return restart_syscall();
Neil Hormane843fa52010-10-13 16:01:50 +00001093 block_netpoll_tx();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001094 read_lock(&bond->lock);
1095 write_lock_bh(&bond->curr_slave_lock);
1096
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001097 if (!USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001098 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1099 bond->dev->name, bond->dev->name, bond->params.mode);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001100 goto out;
1101 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001102
nikolay@redhat.comeb6e98a2012-10-31 04:42:51 +00001103 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001104
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001105 /* check to see if we are clearing primary */
1106 if (!strlen(ifname) || buf[0] == '\n') {
1107 pr_info("%s: Setting primary slave to None.\n",
1108 bond->dev->name);
1109 bond->primary_slave = NULL;
Milos Vyleteleb492f72013-01-29 09:59:00 +00001110 memset(bond->params.primary, 0, sizeof(bond->params.primary));
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001111 bond_select_active_slave(bond);
1112 goto out;
1113 }
1114
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001115 bond_for_each_slave(bond, slave, iter) {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001116 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1117 pr_info("%s: Setting %s as primary slave.\n",
1118 bond->dev->name, slave->dev->name);
1119 bond->primary_slave = slave;
1120 strcpy(bond->params.primary, slave->dev->name);
1121 bond_select_active_slave(bond);
1122 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001123 }
1124 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001125
Weiping Pan8a936642012-06-10 23:00:20 +00001126 strncpy(bond->params.primary, ifname, IFNAMSIZ);
1127 bond->params.primary[IFNAMSIZ - 1] = 0;
1128
1129 pr_info("%s: Recording %s as primary, "
1130 "but it has not been enslaved to %s yet.\n",
1131 bond->dev->name, ifname, bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001132out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001133 write_unlock_bh(&bond->curr_slave_lock);
1134 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001135 unblock_netpoll_tx();
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001136 rtnl_unlock();
1137
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001138 return count;
1139}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001140static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1141 bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001142
1143/*
Jiri Pirkoa5499522009-09-25 03:28:09 +00001144 * Show and set the primary_reselect flag.
1145 */
1146static ssize_t bonding_show_primary_reselect(struct device *d,
1147 struct device_attribute *attr,
1148 char *buf)
1149{
1150 struct bonding *bond = to_bond(d);
1151
1152 return sprintf(buf, "%s %d\n",
1153 pri_reselect_tbl[bond->params.primary_reselect].modename,
1154 bond->params.primary_reselect);
1155}
1156
1157static ssize_t bonding_store_primary_reselect(struct device *d,
1158 struct device_attribute *attr,
1159 const char *buf, size_t count)
1160{
1161 int new_value, ret = count;
1162 struct bonding *bond = to_bond(d);
1163
1164 if (!rtnl_trylock())
1165 return restart_syscall();
1166
1167 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1168 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001169 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001170 bond->dev->name,
1171 (int) strlen(buf) - 1, buf);
1172 ret = -EINVAL;
1173 goto out;
1174 }
1175
1176 bond->params.primary_reselect = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001177 pr_info("%s: setting primary_reselect to %s (%d).\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001178 bond->dev->name, pri_reselect_tbl[new_value].modename,
1179 new_value);
1180
Neil Hormane843fa52010-10-13 16:01:50 +00001181 block_netpoll_tx();
Jiri Pirkoa5499522009-09-25 03:28:09 +00001182 read_lock(&bond->lock);
1183 write_lock_bh(&bond->curr_slave_lock);
1184 bond_select_active_slave(bond);
1185 write_unlock_bh(&bond->curr_slave_lock);
1186 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001187 unblock_netpoll_tx();
Jiri Pirkoa5499522009-09-25 03:28:09 +00001188out:
1189 rtnl_unlock();
1190 return ret;
1191}
1192static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1193 bonding_show_primary_reselect,
1194 bonding_store_primary_reselect);
1195
1196/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001197 * Show and set the use_carrier flag.
1198 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001199static ssize_t bonding_show_carrier(struct device *d,
1200 struct device_attribute *attr,
1201 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001202{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001203 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001204
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001205 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001206}
1207
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001208static ssize_t bonding_store_carrier(struct device *d,
1209 struct device_attribute *attr,
1210 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001211{
1212 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001213 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001214
1215
1216 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001217 pr_err("%s: no use_carrier value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001218 bond->dev->name);
1219 ret = -EINVAL;
1220 goto out;
1221 }
1222 if ((new_value == 0) || (new_value == 1)) {
1223 bond->params.use_carrier = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001224 pr_info("%s: Setting use_carrier to %d.\n",
1225 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001226 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001227 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1228 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001229 }
1230out:
Jiri Pirko672bda32011-01-25 11:03:25 +00001231 return ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001232}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001233static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1234 bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001235
1236
1237/*
1238 * Show and set currently active_slave.
1239 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001240static ssize_t bonding_show_active_slave(struct device *d,
1241 struct device_attribute *attr,
1242 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001243{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001244 struct bonding *bond = to_bond(d);
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001245 struct slave *curr;
Wagner Ferenc16cd0162007-12-06 23:40:29 -08001246 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001247
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001248 rcu_read_lock();
1249 curr = rcu_dereference(bond->curr_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001250 if (USES_PRIMARY(bond->params.mode) && curr)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001251 count = sprintf(buf, "%s\n", curr->dev->name);
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001252 rcu_read_unlock();
1253
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001254 return count;
1255}
1256
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001257static ssize_t bonding_store_active_slave(struct device *d,
1258 struct device_attribute *attr,
1259 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001260{
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001261 struct slave *slave, *old_active, *new_active;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001262 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001263 struct list_head *iter;
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001264 char ifname[IFNAMSIZ];
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001265
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001266 if (!rtnl_trylock())
1267 return restart_syscall();
Neil Hormane843fa52010-10-13 16:01:50 +00001268
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001269 old_active = new_active = NULL;
Neil Hormane843fa52010-10-13 16:01:50 +00001270 block_netpoll_tx();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001271 read_lock(&bond->lock);
1272 write_lock_bh(&bond->curr_slave_lock);
Jay Vosburgh1466a212007-11-06 13:33:28 -08001273
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001274 if (!USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001275 pr_info("%s: Unable to change active slave; %s is in mode %d\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001276 bond->dev->name, bond->dev->name, bond->params.mode);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001277 goto out;
1278 }
1279
nikolay@redhat.comc84e1592012-10-31 06:03:52 +00001280 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001281
1282 /* check to see if we are clearing active */
1283 if (!strlen(ifname) || buf[0] == '\n') {
1284 pr_info("%s: Clearing current active slave.\n",
1285 bond->dev->name);
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001286 rcu_assign_pointer(bond->curr_active_slave, NULL);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001287 bond_select_active_slave(bond);
1288 goto out;
1289 }
1290
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001291 bond_for_each_slave(bond, slave, iter) {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001292 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1293 old_active = bond->curr_active_slave;
1294 new_active = slave;
1295 if (new_active == old_active) {
1296 /* do nothing */
1297 pr_info("%s: %s is already the current"
1298 " active slave.\n",
1299 bond->dev->name,
1300 slave->dev->name);
1301 goto out;
dingtianhong38c49162013-07-23 15:25:32 +08001302 } else {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001303 if ((new_active) &&
1304 (old_active) &&
1305 (new_active->link == BOND_LINK_UP) &&
1306 IS_UP(new_active->dev)) {
1307 pr_info("%s: Setting %s as active"
1308 " slave.\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001309 bond->dev->name,
1310 slave->dev->name);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001311 bond_change_active_slave(bond,
1312 new_active);
dingtianhong38c49162013-07-23 15:25:32 +08001313 } else {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001314 pr_info("%s: Could not set %s as"
1315 " active slave; either %s is"
1316 " down or the link is down.\n",
1317 bond->dev->name,
1318 slave->dev->name,
1319 slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001320 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001321 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001322 }
1323 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001324 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001325
1326 pr_info("%s: Unable to set %.*s as active slave.\n",
1327 bond->dev->name, (int)strlen(buf) - 1, buf);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001328 out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001329 write_unlock_bh(&bond->curr_slave_lock);
1330 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001331 unblock_netpoll_tx();
1332
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001333 rtnl_unlock();
1334
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001335 return count;
1336
1337}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001338static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1339 bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001340
1341
1342/*
1343 * Show link status of the bond interface.
1344 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001345static ssize_t bonding_show_mii_status(struct device *d,
1346 struct device_attribute *attr,
1347 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001348{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001349 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001350
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001351 return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001352}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001353static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001354
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001355/*
1356 * Show current 802.3ad aggregator ID.
1357 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001358static ssize_t bonding_show_ad_aggregator(struct device *d,
1359 struct device_attribute *attr,
1360 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001361{
1362 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001363 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001364
1365 if (bond->params.mode == BOND_MODE_8023AD) {
1366 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001367 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001368 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001369 ? 0 : ad_info.aggregator_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001370 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001371
1372 return count;
1373}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001374static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001375
1376
1377/*
1378 * Show number of active 802.3ad ports.
1379 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001380static ssize_t bonding_show_ad_num_ports(struct device *d,
1381 struct device_attribute *attr,
1382 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001383{
1384 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001385 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001386
1387 if (bond->params.mode == BOND_MODE_8023AD) {
1388 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001389 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001390 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001391 ? 0 : ad_info.ports);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001392 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001393
1394 return count;
1395}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001396static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001397
1398
1399/*
1400 * Show current 802.3ad actor key.
1401 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001402static ssize_t bonding_show_ad_actor_key(struct device *d,
1403 struct device_attribute *attr,
1404 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001405{
1406 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001407 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001408
1409 if (bond->params.mode == BOND_MODE_8023AD) {
1410 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001411 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001412 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001413 ? 0 : ad_info.actor_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001414 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001415
1416 return count;
1417}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001418static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001419
1420
1421/*
1422 * Show current 802.3ad partner key.
1423 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001424static ssize_t bonding_show_ad_partner_key(struct device *d,
1425 struct device_attribute *attr,
1426 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001427{
1428 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001429 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001430
1431 if (bond->params.mode == BOND_MODE_8023AD) {
1432 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001433 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001434 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001435 ? 0 : ad_info.partner_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001436 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001437
1438 return count;
1439}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001440static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001441
1442
1443/*
1444 * Show current 802.3ad partner mac.
1445 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001446static ssize_t bonding_show_ad_partner_mac(struct device *d,
1447 struct device_attribute *attr,
1448 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001449{
1450 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001451 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001452
1453 if (bond->params.mode == BOND_MODE_8023AD) {
1454 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001455 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
Johannes Berge1749612008-10-27 15:59:26 -07001456 count = sprintf(buf, "%pM\n", ad_info.partner_system);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001457 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001458
1459 return count;
1460}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001461static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001462
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001463/*
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001464 * Show the queue_ids of the slaves in the current bond.
1465 */
1466static ssize_t bonding_show_queue_id(struct device *d,
1467 struct device_attribute *attr,
1468 char *buf)
1469{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001470 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001471 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001472 struct slave *slave;
1473 int res = 0;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001474
1475 if (!rtnl_trylock())
1476 return restart_syscall();
1477
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001478 bond_for_each_slave(bond, slave, iter) {
Nicolas de Pesloüan79236682010-07-14 18:24:54 -07001479 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1480 /* not enough space for another interface_name:queue_id pair */
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001481 if ((PAGE_SIZE - res) > 10)
1482 res = PAGE_SIZE - 10;
1483 res += sprintf(buf + res, "++more++ ");
1484 break;
1485 }
1486 res += sprintf(buf + res, "%s:%d ",
1487 slave->dev->name, slave->queue_id);
1488 }
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001489 if (res)
1490 buf[res-1] = '\n'; /* eat the leftover space */
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001491
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001492 rtnl_unlock();
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001493
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001494 return res;
1495}
1496
1497/*
1498 * Set the queue_ids of the slaves in the current bond. The bond
1499 * interface must be enslaved for this to work.
1500 */
1501static ssize_t bonding_store_queue_id(struct device *d,
1502 struct device_attribute *attr,
1503 const char *buffer, size_t count)
1504{
1505 struct slave *slave, *update_slave;
1506 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001507 struct list_head *iter;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001508 u16 qid;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001509 int ret = count;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001510 char *delim;
1511 struct net_device *sdev = NULL;
1512
1513 if (!rtnl_trylock())
1514 return restart_syscall();
1515
1516 /* delim will point to queue id if successful */
1517 delim = strchr(buffer, ':');
1518 if (!delim)
1519 goto err_no_cmd;
1520
1521 /*
1522 * Terminate string that points to device name and bump it
1523 * up one, so we can read the queue id there.
1524 */
1525 *delim = '\0';
1526 if (sscanf(++delim, "%hd\n", &qid) != 1)
1527 goto err_no_cmd;
1528
1529 /* Check buffer length, valid ifname and queue id */
1530 if (strlen(buffer) > IFNAMSIZ ||
1531 !dev_valid_name(buffer) ||
Jiri Pirko8a540ff2012-07-20 02:28:50 +00001532 qid > bond->dev->real_num_tx_queues)
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001533 goto err_no_cmd;
1534
1535 /* Get the pointer to that interface if it exists */
1536 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1537 if (!sdev)
1538 goto err_no_cmd;
1539
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001540 /* Search for thes slave and check for duplicate qids */
1541 update_slave = NULL;
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001542 bond_for_each_slave(bond, slave, iter) {
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001543 if (sdev == slave->dev)
1544 /*
1545 * We don't need to check the matching
1546 * slave for dups, since we're overwriting it
1547 */
1548 update_slave = slave;
1549 else if (qid && qid == slave->queue_id) {
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001550 goto err_no_cmd;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001551 }
1552 }
1553
1554 if (!update_slave)
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001555 goto err_no_cmd;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001556
1557 /* Actually set the qids for the slave */
1558 update_slave->queue_id = qid;
1559
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001560out:
1561 rtnl_unlock();
1562 return ret;
1563
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001564err_no_cmd:
1565 pr_info("invalid input for queue_id set for %s.\n",
1566 bond->dev->name);
1567 ret = -EPERM;
1568 goto out;
1569}
1570
1571static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1572 bonding_store_queue_id);
1573
1574
1575/*
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001576 * Show and set the all_slaves_active flag.
1577 */
1578static ssize_t bonding_show_slaves_active(struct device *d,
1579 struct device_attribute *attr,
1580 char *buf)
1581{
1582 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001583
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001584 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1585}
1586
1587static ssize_t bonding_store_slaves_active(struct device *d,
1588 struct device_attribute *attr,
1589 const char *buf, size_t count)
1590{
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001591 struct bonding *bond = to_bond(d);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001592 int new_value, ret = count;
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001593 struct list_head *iter;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001594 struct slave *slave;
1595
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001596 if (!rtnl_trylock())
1597 return restart_syscall();
1598
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001599 if (sscanf(buf, "%d", &new_value) != 1) {
1600 pr_err("%s: no all_slaves_active value specified.\n",
1601 bond->dev->name);
1602 ret = -EINVAL;
1603 goto out;
1604 }
1605
1606 if (new_value == bond->params.all_slaves_active)
1607 goto out;
1608
1609 if ((new_value == 0) || (new_value == 1)) {
1610 bond->params.all_slaves_active = new_value;
1611 } else {
1612 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1613 bond->dev->name, new_value);
1614 ret = -EINVAL;
1615 goto out;
1616 }
1617
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001618 bond_for_each_slave(bond, slave, iter) {
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001619 if (!bond_is_active_slave(slave)) {
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001620 if (new_value)
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001621 slave->inactive = 0;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001622 else
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001623 slave->inactive = 1;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001624 }
1625 }
1626out:
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001627 rtnl_unlock();
Jiri Pirko672bda32011-01-25 11:03:25 +00001628 return ret;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001629}
1630static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1631 bonding_show_slaves_active, bonding_store_slaves_active);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001632
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001633/*
1634 * Show and set the number of IGMP membership reports to send on link failure
1635 */
1636static ssize_t bonding_show_resend_igmp(struct device *d,
Flavio Leitner94265cf2011-05-25 08:38:58 +00001637 struct device_attribute *attr,
1638 char *buf)
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001639{
1640 struct bonding *bond = to_bond(d);
1641
1642 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1643}
1644
1645static ssize_t bonding_store_resend_igmp(struct device *d,
Flavio Leitner94265cf2011-05-25 08:38:58 +00001646 struct device_attribute *attr,
1647 const char *buf, size_t count)
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001648{
1649 int new_value, ret = count;
1650 struct bonding *bond = to_bond(d);
1651
1652 if (sscanf(buf, "%d", &new_value) != 1) {
1653 pr_err("%s: no resend_igmp value specified.\n",
1654 bond->dev->name);
1655 ret = -EINVAL;
1656 goto out;
1657 }
1658
Flavio Leitner94265cf2011-05-25 08:38:58 +00001659 if (new_value < 0 || new_value > 255) {
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001660 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1661 bond->dev->name, new_value);
1662 ret = -EINVAL;
1663 goto out;
1664 }
1665
1666 pr_info("%s: Setting resend_igmp to %d.\n",
1667 bond->dev->name, new_value);
1668 bond->params.resend_igmp = new_value;
1669out:
1670 return ret;
1671}
1672
1673static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1674 bonding_show_resend_igmp, bonding_store_resend_igmp);
1675
Neil Horman7eacd032013-09-13 11:05:33 -04001676
1677static ssize_t bonding_show_lp_interval(struct device *d,
1678 struct device_attribute *attr,
1679 char *buf)
1680{
1681 struct bonding *bond = to_bond(d);
1682 return sprintf(buf, "%d\n", bond->params.lp_interval);
1683}
1684
1685static ssize_t bonding_store_lp_interval(struct device *d,
1686 struct device_attribute *attr,
1687 const char *buf, size_t count)
1688{
1689 struct bonding *bond = to_bond(d);
1690 int new_value, ret = count;
1691
1692 if (sscanf(buf, "%d", &new_value) != 1) {
1693 pr_err("%s: no lp interval value specified.\n",
1694 bond->dev->name);
1695 ret = -EINVAL;
1696 goto out;
1697 }
1698
1699 if (new_value <= 0) {
1700 pr_err ("%s: lp_interval must be between 1 and %d\n",
1701 bond->dev->name, INT_MAX);
1702 ret = -EINVAL;
1703 goto out;
1704 }
1705
1706 bond->params.lp_interval = new_value;
1707out:
1708 return ret;
1709}
1710
1711static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1712 bonding_show_lp_interval, bonding_store_lp_interval);
1713
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001714static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001715 &dev_attr_slaves.attr,
1716 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001717 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001718 &dev_attr_arp_validate.attr,
Veaceslav Falico8599b522013-06-24 11:49:34 +02001719 &dev_attr_arp_all_targets.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001720 &dev_attr_arp_interval.attr,
1721 &dev_attr_arp_ip_target.attr,
1722 &dev_attr_downdelay.attr,
1723 &dev_attr_updelay.attr,
1724 &dev_attr_lacp_rate.attr,
Jay Vosburghfd989c82008-11-04 17:51:16 -08001725 &dev_attr_ad_select.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001726 &dev_attr_xmit_hash_policy.attr,
Ben Hutchingsad246c92011-04-26 15:25:52 +00001727 &dev_attr_num_grat_arp.attr,
1728 &dev_attr_num_unsol_na.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001729 &dev_attr_miimon.attr,
1730 &dev_attr_primary.attr,
Jiri Pirkoa5499522009-09-25 03:28:09 +00001731 &dev_attr_primary_reselect.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001732 &dev_attr_use_carrier.attr,
1733 &dev_attr_active_slave.attr,
1734 &dev_attr_mii_status.attr,
1735 &dev_attr_ad_aggregator.attr,
1736 &dev_attr_ad_num_ports.attr,
1737 &dev_attr_ad_actor_key.attr,
1738 &dev_attr_ad_partner_key.attr,
1739 &dev_attr_ad_partner_mac.attr,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001740 &dev_attr_queue_id.attr,
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001741 &dev_attr_all_slaves_active.attr,
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001742 &dev_attr_resend_igmp.attr,
stephen hemminger655f8912011-06-22 09:54:39 +00001743 &dev_attr_min_links.attr,
Neil Horman7eacd032013-09-13 11:05:33 -04001744 &dev_attr_lp_interval.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001745 NULL,
1746};
1747
1748static struct attribute_group bonding_group = {
1749 .name = "bonding",
1750 .attrs = per_bond_attrs,
1751};
1752
1753/*
1754 * Initialize sysfs. This sets up the bonding_masters file in
1755 * /sys/class/net.
1756 */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001757int bond_create_sysfs(struct bond_net *bn)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001758{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001759 int ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001760
Eric W. Biederman4c224002011-10-12 21:56:25 +00001761 bn->class_attr_bonding_masters = class_attr_bonding_masters;
Eric W. Biederman01718e32011-10-21 22:43:07 +00001762 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
Eric W. Biederman4c224002011-10-12 21:56:25 +00001763
1764 ret = netdev_class_create_file(&bn->class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001765 /*
1766 * Permit multiple loads of the module by ignoring failures to
1767 * create the bonding_masters sysfs file. Bonding devices
1768 * created by second or subsequent loads of the module will
1769 * not be listed in, or controllable by, bonding_masters, but
1770 * will have the usual "bonding" sysfs directory.
1771 *
1772 * This is done to preserve backwards compatibility for
1773 * initscripts/sysconfig, which load bonding multiple times to
1774 * configure multiple bonding devices.
1775 */
1776 if (ret == -EEXIST) {
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001777 /* Is someone being kinky and naming a device bonding_master? */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001778 if (__dev_get_by_name(bn->net,
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001779 class_attr_bonding_masters.attr.name))
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001780 pr_err("network device named %s already exists in sysfs",
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001781 class_attr_bonding_masters.attr.name);
Stephen Hemminger130aa612009-06-11 05:46:04 -07001782 ret = 0;
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001783 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001784
1785 return ret;
1786
1787}
1788
1789/*
1790 * Remove /sys/class/net/bonding_masters.
1791 */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001792void bond_destroy_sysfs(struct bond_net *bn)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001793{
Eric W. Biederman4c224002011-10-12 21:56:25 +00001794 netdev_class_remove_file(&bn->class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001795}
1796
1797/*
1798 * Initialize sysfs for each bond. This sets up and registers
1799 * the 'bondctl' directory for each individual bond under /sys/class/net.
1800 */
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001801void bond_prepare_sysfs_group(struct bonding *bond)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001802{
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001803 bond->dev->sysfs_groups[0] = &bonding_group;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001804}
1805