blob: 75dc4d0efb340ecb5da41431806785fa9193aaab [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>
Nikolay Aleksandrov73958322013-11-05 13:51:41 +010043#include <linux/reciprocal_div.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080044
Mitch Williamsb76cdba2005-11-09 10:36:41 -080045#include "bonding.h"
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -080046
Stephen Hemminger3d632c32009-06-12 19:02:48 +000047#define to_dev(obj) container_of(obj, struct device, kobj)
Wang Chen454d7c92008-11-12 23:37:49 -080048#define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
Mitch Williamsb76cdba2005-11-09 10:36:41 -080049
Mitch Williamsb76cdba2005-11-09 10:36:41 -080050/*
51 * "show" function for the bond_masters attribute.
52 * The class parameter is ignored.
53 */
Andi Kleen28812fe2010-01-05 12:48:07 +010054static ssize_t bonding_show_bonds(struct class *cls,
55 struct class_attribute *attr,
56 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080057{
Eric W. Biederman4c224002011-10-12 21:56:25 +000058 struct bond_net *bn =
59 container_of(attr, struct bond_net, class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080060 int res = 0;
61 struct bonding *bond;
62
Stephen Hemminger7e083842009-06-12 19:02:46 +000063 rtnl_lock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080064
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000065 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -080066 if (res > (PAGE_SIZE - IFNAMSIZ)) {
67 /* not enough space for another interface name */
68 if ((PAGE_SIZE - res) > 10)
69 res = PAGE_SIZE - 10;
Wagner Ferencb8843662007-12-06 23:40:30 -080070 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -080071 break;
72 }
Wagner Ferencb8843662007-12-06 23:40:30 -080073 res += sprintf(buf + res, "%s ", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080074 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -080075 if (res)
76 buf[res-1] = '\n'; /* eat the leftover space */
Stephen Hemminger7e083842009-06-12 19:02:46 +000077
78 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080079 return res;
80}
81
Eric W. Biederman4c224002011-10-12 21:56:25 +000082static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
Stephen Hemminger373500d2009-06-12 19:02:50 +000083{
84 struct bonding *bond;
85
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000086 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Stephen Hemminger373500d2009-06-12 19:02:50 +000087 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
88 return bond->dev;
89 }
90 return NULL;
91}
92
Mitch Williamsb76cdba2005-11-09 10:36:41 -080093/*
94 * "store" function for the bond_masters attribute. This is what
95 * creates and deletes entire bonds.
96 *
97 * The class parameter is ignored.
98 *
99 */
100
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000101static ssize_t bonding_store_bonds(struct class *cls,
Andi Kleen28812fe2010-01-05 12:48:07 +0100102 struct class_attribute *attr,
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000103 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800104{
Eric W. Biederman4c224002011-10-12 21:56:25 +0000105 struct bond_net *bn =
106 container_of(attr, struct bond_net, class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800107 char command[IFNAMSIZ + 1] = {0, };
108 char *ifname;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800109 int rv, res = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800110
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800111 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
112 ifname = command + 1;
113 if ((strlen(command) <= 1) ||
114 !dev_valid_name(ifname))
115 goto err_no_cmd;
116
117 if (command[0] == '+') {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800118 pr_info("%s is being created...\n", ifname);
Eric W. Biederman4c224002011-10-12 21:56:25 +0000119 rv = bond_create(bn->net, ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800120 if (rv) {
Phil Oester5f86cad12011-03-14 06:22:06 +0000121 if (rv == -EEXIST)
122 pr_info("%s already exists.\n", ifname);
123 else
124 pr_info("%s creation failed.\n", ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800125 res = rv;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800126 }
Stephen Hemminger373500d2009-06-12 19:02:50 +0000127 } else if (command[0] == '-') {
128 struct net_device *bond_dev;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800129
Jay Vosburgh027ea042008-01-17 16:25:02 -0800130 rtnl_lock();
Eric W. Biederman4c224002011-10-12 21:56:25 +0000131 bond_dev = bond_get_by_name(bn, ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000132 if (bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800133 pr_info("%s is being deleted...\n", ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000134 unregister_netdevice(bond_dev);
135 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800136 pr_err("unable to delete non-existent %s\n", ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000137 res = -ENODEV;
138 }
139 rtnl_unlock();
140 } else
141 goto err_no_cmd;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800142
Stephen Hemminger373500d2009-06-12 19:02:50 +0000143 /* Always return either count or an error. If you return 0, you'll
144 * get called forever, which is bad.
145 */
146 return res;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800147
148err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800149 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700150 return -EPERM;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800151}
Stephen Hemminger373500d2009-06-12 19:02:50 +0000152
Eric W. Biederman4c224002011-10-12 21:56:25 +0000153static const void *bonding_namespace(struct class *cls,
154 const struct class_attribute *attr)
155{
156 const struct bond_net *bn =
157 container_of(attr, struct bond_net, class_attr_bonding_masters);
158 return bn->net;
159}
160
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800161/* class attribute for bond_masters file. This ends up in /sys/class/net */
Eric W. Biederman4c224002011-10-12 21:56:25 +0000162static const struct class_attribute class_attr_bonding_masters = {
163 .attr = {
164 .name = "bonding_masters",
165 .mode = S_IWUSR | S_IRUGO,
166 },
167 .show = bonding_show_bonds,
168 .store = bonding_store_bonds,
169 .namespace = bonding_namespace,
170};
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800171
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800172/*
173 * Show the slaves in the current bond.
174 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700175static ssize_t bonding_show_slaves(struct device *d,
176 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800177{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700178 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200179 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200180 struct slave *slave;
181 int res = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800182
dingtianhong4d1ae5f2013-10-15 16:28:42 +0800183 if (!rtnl_trylock())
184 return restart_syscall();
185
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200186 bond_for_each_slave(bond, slave, iter) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800187 if (res > (PAGE_SIZE - IFNAMSIZ)) {
188 /* not enough space for another interface name */
189 if ((PAGE_SIZE - res) > 10)
190 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800191 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800192 break;
193 }
194 res += sprintf(buf + res, "%s ", slave->dev->name);
195 }
dingtianhong4d1ae5f2013-10-15 16:28:42 +0800196
197 rtnl_unlock();
198
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800199 if (res)
200 buf[res-1] = '\n'; /* eat the leftover space */
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200201
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800202 return res;
203}
204
205/*
Veaceslav Falicod6641cc2013-05-28 01:26:13 +0000206 * Set the slaves in the current bond.
Jiri Pirkof9f35452010-05-18 05:46:39 +0000207 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
208 * All hard work should be done there.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800209 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700210static ssize_t bonding_store_slaves(struct device *d,
211 struct device_attribute *attr,
212 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800213{
214 char command[IFNAMSIZ + 1] = { 0, };
215 char *ifname;
Jiri Pirkof9f35452010-05-18 05:46:39 +0000216 int res, ret = count;
217 struct net_device *dev;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700218 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800219
Eric W. Biederman496a60c2009-05-13 17:02:50 +0000220 if (!rtnl_trylock())
221 return restart_syscall();
Jay Vosburgh027ea042008-01-17 16:25:02 -0800222
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800223 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
224 ifname = command + 1;
225 if ((strlen(command) <= 1) ||
226 !dev_valid_name(ifname))
227 goto err_no_cmd;
228
Jiri Pirkof9f35452010-05-18 05:46:39 +0000229 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
230 if (!dev) {
231 pr_info("%s: Interface %s does not exist!\n",
232 bond->dev->name, ifname);
233 ret = -ENODEV;
234 goto out;
235 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800236
Jiri Pirkof9f35452010-05-18 05:46:39 +0000237 switch (command[0]) {
238 case '+':
239 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800240 res = bond_enslave(bond->dev, dev);
Jiri Pirkof9f35452010-05-18 05:46:39 +0000241 break;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000242
Jiri Pirkof9f35452010-05-18 05:46:39 +0000243 case '-':
244 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
245 res = bond_release(bond->dev, dev);
246 break;
247
248 default:
249 goto err_no_cmd;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800250 }
251
Jiri Pirkof9f35452010-05-18 05:46:39 +0000252 if (res)
253 ret = res;
254 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800255
256err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800257 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
258 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800259 ret = -EPERM;
260
261out:
Jay Vosburgh027ea042008-01-17 16:25:02 -0800262 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800263 return ret;
264}
265
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000266static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
267 bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800268
269/*
270 * Show and set the bonding mode. The bond interface must be down to
271 * change the mode.
272 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700273static ssize_t bonding_show_mode(struct device *d,
274 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800275{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700276 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800277
278 return sprintf(buf, "%s %d\n",
279 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800280 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800281}
282
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700283static ssize_t bonding_store_mode(struct device *d,
284 struct device_attribute *attr,
285 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800286{
Jiri Pirko72be35f2013-10-18 17:43:34 +0200287 int new_value, ret;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700288 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800289
Jay Vosburghece95f72008-01-17 16:25:01 -0800290 new_value = bond_parse_parm(buf, bond_mode_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800291 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800292 pr_err("%s: Ignoring invalid mode value %.*s.\n",
293 bond->dev->name, (int)strlen(buf) - 1, buf);
Jiri Pirko72be35f2013-10-18 17:43:34 +0200294 return -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800295 }
Jiri Pirko72be35f2013-10-18 17:43:34 +0200296 if (!rtnl_trylock())
297 return restart_syscall();
298
299 ret = bond_option_mode_set(bond, new_value);
300 if (!ret) {
301 pr_info("%s: setting mode to %s (%d).\n",
302 bond->dev->name, bond_mode_tbl[new_value].modename,
303 new_value);
304 ret = count;
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000305 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000306
nikolay@redhat.comea6836d2013-05-18 01:18:28 +0000307 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800308 return ret;
309}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000310static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
311 bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800312
313/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000314 * Show and set the bonding transmit hash method.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800315 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700316static ssize_t bonding_show_xmit_hash(struct device *d,
317 struct device_attribute *attr,
318 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800319{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700320 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800321
Wagner Ferenc8e4b9322007-12-06 23:40:32 -0800322 return sprintf(buf, "%s %d\n",
323 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
324 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800325}
326
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700327static ssize_t bonding_store_xmit_hash(struct device *d,
328 struct device_attribute *attr,
329 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800330{
331 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700332 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800333
Jay Vosburghece95f72008-01-17 16:25:01 -0800334 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800335 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800336 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800337 bond->dev->name,
338 (int)strlen(buf) - 1, buf);
339 ret = -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800340 } else {
341 bond->params.xmit_policy = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800342 pr_info("%s: setting xmit hash policy to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000343 bond->dev->name,
344 xmit_hashtype_tbl[new_value].modename, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800345 }
Nikolay Aleksandrov53edee22013-05-24 00:59:47 +0000346
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800347 return ret;
348}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000349static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
350 bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800351
352/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700353 * Show and set arp_validate.
354 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700355static ssize_t bonding_show_arp_validate(struct device *d,
356 struct device_attribute *attr,
357 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700358{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700359 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700360
361 return sprintf(buf, "%s %d\n",
362 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800363 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700364}
365
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700366static ssize_t bonding_store_arp_validate(struct device *d,
367 struct device_attribute *attr,
368 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700369{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700370 struct bonding *bond = to_bond(d);
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200371 int new_value, ret = count;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700372
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200373 if (!rtnl_trylock())
374 return restart_syscall();
Jay Vosburghece95f72008-01-17 16:25:01 -0800375 new_value = bond_parse_parm(buf, arp_validate_tbl);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700376 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800377 pr_err("%s: Ignoring invalid arp_validate value %s\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700378 bond->dev->name, buf);
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200379 ret = -EINVAL;
380 goto out;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700381 }
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200382 if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800383 pr_err("%s: arp_validate only supported in active-backup mode.\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700384 bond->dev->name);
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200385 ret = -EINVAL;
386 goto out;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700387 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800388 pr_info("%s: setting arp_validate to %s (%d).\n",
389 bond->dev->name, arp_validate_tbl[new_value].modename,
390 new_value);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700391
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200392 if (bond->dev->flags & IFF_UP) {
393 if (!new_value)
394 bond->recv_probe = NULL;
395 else if (bond->params.arp_interval)
396 bond->recv_probe = bond_arp_rcv;
397 }
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700398 bond->params.arp_validate = new_value;
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200399out:
400 rtnl_unlock();
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700401
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200402 return ret;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700403}
404
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000405static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
406 bonding_store_arp_validate);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200407/*
408 * Show and set arp_all_targets.
409 */
410static ssize_t bonding_show_arp_all_targets(struct device *d,
411 struct device_attribute *attr,
412 char *buf)
413{
414 struct bonding *bond = to_bond(d);
415 int value = bond->params.arp_all_targets;
416
417 return sprintf(buf, "%s %d\n", arp_all_targets_tbl[value].modename,
418 value);
419}
420
421static ssize_t bonding_store_arp_all_targets(struct device *d,
422 struct device_attribute *attr,
423 const char *buf, size_t count)
424{
425 struct bonding *bond = to_bond(d);
426 int new_value;
427
428 new_value = bond_parse_parm(buf, arp_all_targets_tbl);
429 if (new_value < 0) {
430 pr_err("%s: Ignoring invalid arp_all_targets value %s\n",
431 bond->dev->name, buf);
432 return -EINVAL;
433 }
434 pr_info("%s: setting arp_all_targets to %s (%d).\n",
435 bond->dev->name, arp_all_targets_tbl[new_value].modename,
436 new_value);
437
438 bond->params.arp_all_targets = new_value;
439
440 return count;
441}
442
443static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
444 bonding_show_arp_all_targets, bonding_store_arp_all_targets);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700445
446/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700447 * Show and store fail_over_mac. User only allowed to change the
448 * value when there are no slaves.
449 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000450static ssize_t bonding_show_fail_over_mac(struct device *d,
451 struct device_attribute *attr,
452 char *buf)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700453{
454 struct bonding *bond = to_bond(d);
455
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700456 return sprintf(buf, "%s %d\n",
457 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
458 bond->params.fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700459}
460
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000461static ssize_t bonding_store_fail_over_mac(struct device *d,
462 struct device_attribute *attr,
463 const char *buf, size_t count)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700464{
dingtianhong9402b742013-07-23 15:25:39 +0800465 int new_value, ret = count;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700466 struct bonding *bond = to_bond(d);
467
dingtianhong9402b742013-07-23 15:25:39 +0800468 if (!rtnl_trylock())
469 return restart_syscall();
470
Veaceslav Falico0965a1f2013-09-25 09:20:21 +0200471 if (bond_has_slaves(bond)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800472 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
Jay Vosburghdd957c52007-10-09 19:57:24 -0700473 bond->dev->name);
dingtianhong9402b742013-07-23 15:25:39 +0800474 ret = -EPERM;
475 goto out;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700476 }
477
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700478 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
479 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800480 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700481 bond->dev->name, buf);
dingtianhong9402b742013-07-23 15:25:39 +0800482 ret = -EINVAL;
483 goto out;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700484 }
485
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700486 bond->params.fail_over_mac = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800487 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
488 bond->dev->name, fail_over_mac_tbl[new_value].modename,
489 new_value);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700490
dingtianhong9402b742013-07-23 15:25:39 +0800491out:
492 rtnl_unlock();
493 return ret;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700494}
495
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000496static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
497 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700498
499/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800500 * Show and set the arp timer interval. There are two tricky bits
501 * here. First, if ARP monitoring is activated, then we must disable
502 * MII monitoring. Second, if the ARP timer isn't running, we must
503 * start it.
504 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700505static ssize_t bonding_show_arp_interval(struct device *d,
506 struct device_attribute *attr,
507 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800508{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700509 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800510
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800511 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800512}
513
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700514static ssize_t bonding_store_arp_interval(struct device *d,
515 struct device_attribute *attr,
516 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800517{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700518 struct bonding *bond = to_bond(d);
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200519 int new_value, ret = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800520
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000521 if (!rtnl_trylock())
522 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800523 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800524 pr_err("%s: no arp_interval value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800525 bond->dev->name);
526 ret = -EINVAL;
527 goto out;
528 }
529 if (new_value < 0) {
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000530 pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800531 bond->dev->name, new_value, INT_MAX);
532 ret = -EINVAL;
533 goto out;
534 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000535 if (bond->params.mode == BOND_MODE_ALB ||
536 bond->params.mode == BOND_MODE_TLB) {
537 pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
538 bond->dev->name, bond->dev->name);
539 ret = -EINVAL;
540 goto out;
541 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800542 pr_info("%s: Setting ARP monitoring interval to %d.\n",
543 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800544 bond->params.arp_interval = new_value;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000545 if (new_value) {
546 if (bond->params.miimon) {
547 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
548 bond->dev->name, bond->dev->name);
549 bond->params.miimon = 0;
550 }
551 if (!bond->params.arp_targets[0])
552 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
553 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800554 }
555 if (bond->dev->flags & IFF_UP) {
556 /* If the interface is up, we may need to fire off
557 * the ARP timer. If the interface is down, the
558 * timer will get fired off when the open function
559 * is called.
560 */
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000561 if (!new_value) {
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200562 if (bond->params.arp_validate)
563 bond->recv_probe = NULL;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000564 cancel_delayed_work_sync(&bond->arp_work);
565 } else {
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200566 /* arp_validate can be set only in active-backup mode */
567 if (bond->params.arp_validate)
568 bond->recv_probe = bond_arp_rcv;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000569 cancel_delayed_work_sync(&bond->mii_work);
570 queue_delayed_work(bond->wq, &bond->arp_work, 0);
571 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800572 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800573out:
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000574 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800575 return ret;
576}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000577static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
578 bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800579
580/*
581 * Show and set the arp targets.
582 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700583static ssize_t bonding_show_arp_targets(struct device *d,
584 struct device_attribute *attr,
585 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800586{
587 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700588 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800589
590 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
591 if (bond->params.arp_targets[i])
Harvey Harrison63779432008-10-31 00:56:00 -0700592 res += sprintf(buf + res, "%pI4 ",
593 &bond->params.arp_targets[i]);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800594 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800595 if (res)
596 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800597 return res;
598}
599
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700600static ssize_t bonding_store_arp_targets(struct device *d,
601 struct device_attribute *attr,
602 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800603{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700604 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200605 struct list_head *iter;
Veaceslav Falico8599b522013-06-24 11:49:34 +0200606 struct slave *slave;
607 __be32 newtarget, *targets;
608 unsigned long *targets_rx;
609 int ind, i, j, ret = -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800610
dingtianhong4d1ae5f2013-10-15 16:28:42 +0800611 if (!rtnl_trylock())
612 return restart_syscall();
613
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800614 targets = bond->params.arp_targets;
615 newtarget = in_aton(buf + 1);
616 /* look for adds */
617 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400618 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800619 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700620 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800621 goto out;
622 }
623
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200624 if (bond_get_targets_ip(targets, newtarget) != -1) { /* dup */
625 pr_err("%s: ARP target %pI4 is already present\n",
626 bond->dev->name, &newtarget);
627 goto out;
628 }
629
Veaceslav Falico8599b522013-06-24 11:49:34 +0200630 ind = bond_get_targets_ip(targets, 0); /* first free slot */
631 if (ind == -1) {
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200632 pr_err("%s: ARP target table is full!\n",
633 bond->dev->name);
634 goto out;
635 }
636
637 pr_info("%s: adding ARP target %pI4.\n", bond->dev->name,
638 &newtarget);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200639 /* not to race with bond_arp_rcv */
640 write_lock_bh(&bond->lock);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200641 bond_for_each_slave(bond, slave, iter)
Veaceslav Falico8599b522013-06-24 11:49:34 +0200642 slave->target_last_arp_rx[ind] = jiffies;
643 targets[ind] = newtarget;
644 write_unlock_bh(&bond->lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000645 } else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400646 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800647 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700648 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800649 goto out;
650 }
651
Veaceslav Falico8599b522013-06-24 11:49:34 +0200652 ind = bond_get_targets_ip(targets, newtarget);
653 if (ind == -1) {
654 pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800655 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800656 goto out;
657 }
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200658
Veaceslav Falico8599b522013-06-24 11:49:34 +0200659 if (ind == 0 && !targets[1] && bond->params.arp_interval)
660 pr_warn("%s: removing last arp target with arp_interval on\n",
661 bond->dev->name);
662
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200663 pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
664 &newtarget);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200665
666 write_lock_bh(&bond->lock);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200667 bond_for_each_slave(bond, slave, iter) {
Veaceslav Falico8599b522013-06-24 11:49:34 +0200668 targets_rx = slave->target_last_arp_rx;
669 j = ind;
670 for (; (j < BOND_MAX_ARP_TARGETS-1) && targets[j+1]; j++)
671 targets_rx[j] = targets_rx[j+1];
672 targets_rx[j] = 0;
673 }
674 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200675 targets[i] = targets[i+1];
676 targets[i] = 0;
Veaceslav Falico8599b522013-06-24 11:49:34 +0200677 write_unlock_bh(&bond->lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000678 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800679 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
680 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800681 ret = -EPERM;
682 goto out;
683 }
684
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200685 ret = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800686out:
dingtianhong4d1ae5f2013-10-15 16:28:42 +0800687 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800688 return ret;
689}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700690static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800691
692/*
693 * Show and set the up and down delays. These must be multiples of the
694 * MII monitoring value, and are stored internally as the multiplier.
695 * Thus, we must translate to MS for the real world.
696 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700697static ssize_t bonding_show_downdelay(struct device *d,
698 struct device_attribute *attr,
699 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800700{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700701 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800702
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800703 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800704}
705
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700706static ssize_t bonding_store_downdelay(struct device *d,
707 struct device_attribute *attr,
708 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800709{
710 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700711 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800712
713 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800714 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800715 bond->dev->name);
716 ret = -EPERM;
717 goto out;
718 }
719
720 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800721 pr_err("%s: no down delay value specified.\n", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800722 ret = -EINVAL;
723 goto out;
724 }
725 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800726 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000727 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800728 ret = -EINVAL;
729 goto out;
730 } else {
731 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800732 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 +0000733 bond->dev->name, new_value,
734 bond->params.miimon,
735 (new_value / bond->params.miimon) *
736 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800737 }
738 bond->params.downdelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800739 pr_info("%s: Setting down delay to %d.\n",
740 bond->dev->name,
741 bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800742
743 }
744
745out:
746 return ret;
747}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000748static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
749 bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800750
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700751static ssize_t bonding_show_updelay(struct device *d,
752 struct device_attribute *attr,
753 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800754{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700755 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800756
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800757 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800758
759}
760
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700761static ssize_t bonding_store_updelay(struct device *d,
762 struct device_attribute *attr,
763 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800764{
765 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700766 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800767
768 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800769 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800770 bond->dev->name);
771 ret = -EPERM;
772 goto out;
773 }
774
775 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800776 pr_err("%s: no up delay value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800777 bond->dev->name);
778 ret = -EINVAL;
779 goto out;
780 }
781 if (new_value < 0) {
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000782 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
783 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800784 ret = -EINVAL;
785 goto out;
786 } else {
787 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800788 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 +0000789 bond->dev->name, new_value,
790 bond->params.miimon,
791 (new_value / bond->params.miimon) *
792 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800793 }
794 bond->params.updelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800795 pr_info("%s: Setting up delay to %d.\n",
796 bond->dev->name,
797 bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800798 }
799
800out:
801 return ret;
802}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000803static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
804 bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800805
806/*
807 * Show and set the LACP interval. Interface must be down, and the mode
808 * must be set to 802.3ad mode.
809 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700810static ssize_t bonding_show_lacp(struct device *d,
811 struct device_attribute *attr,
812 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800813{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700814 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800815
816 return sprintf(buf, "%s %d\n",
817 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800818 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800819}
820
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700821static ssize_t bonding_store_lacp(struct device *d,
822 struct device_attribute *attr,
823 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800824{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700825 struct bonding *bond = to_bond(d);
nikolay@redhat.comc5093162013-09-02 13:51:40 +0200826 int new_value, ret = count;
827
828 if (!rtnl_trylock())
829 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800830
831 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800832 pr_err("%s: Unable to update LACP rate because interface is up.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800833 bond->dev->name);
834 ret = -EPERM;
835 goto out;
836 }
837
838 if (bond->params.mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800839 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800840 bond->dev->name);
841 ret = -EPERM;
842 goto out;
843 }
844
Jay Vosburghece95f72008-01-17 16:25:01 -0800845 new_value = bond_parse_parm(buf, bond_lacp_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800846
847 if ((new_value == 1) || (new_value == 0)) {
848 bond->params.lacp_fast = new_value;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +0000849 bond_3ad_update_lacp_rate(bond);
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800850 pr_info("%s: Setting LACP rate to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000851 bond->dev->name, bond_lacp_tbl[new_value].modename,
852 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800853 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800854 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000855 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800856 ret = -EINVAL;
857 }
858out:
nikolay@redhat.comc5093162013-09-02 13:51:40 +0200859 rtnl_unlock();
860
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800861 return ret;
862}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000863static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
864 bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800865
stephen hemminger655f8912011-06-22 09:54:39 +0000866static ssize_t bonding_show_min_links(struct device *d,
867 struct device_attribute *attr,
868 char *buf)
869{
870 struct bonding *bond = to_bond(d);
871
872 return sprintf(buf, "%d\n", bond->params.min_links);
873}
874
875static ssize_t bonding_store_min_links(struct device *d,
876 struct device_attribute *attr,
877 const char *buf, size_t count)
878{
879 struct bonding *bond = to_bond(d);
880 int ret;
881 unsigned int new_value;
882
883 ret = kstrtouint(buf, 0, &new_value);
884 if (ret < 0) {
885 pr_err("%s: Ignoring invalid min links value %s.\n",
886 bond->dev->name, buf);
887 return ret;
888 }
889
890 pr_info("%s: Setting min links value to %u\n",
891 bond->dev->name, new_value);
892 bond->params.min_links = new_value;
893 return count;
894}
895static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
896 bonding_show_min_links, bonding_store_min_links);
897
Jay Vosburghfd989c82008-11-04 17:51:16 -0800898static ssize_t bonding_show_ad_select(struct device *d,
899 struct device_attribute *attr,
900 char *buf)
901{
902 struct bonding *bond = to_bond(d);
903
904 return sprintf(buf, "%s %d\n",
905 ad_select_tbl[bond->params.ad_select].modename,
906 bond->params.ad_select);
907}
908
909
910static ssize_t bonding_store_ad_select(struct device *d,
911 struct device_attribute *attr,
912 const char *buf, size_t count)
913{
914 int new_value, ret = count;
915 struct bonding *bond = to_bond(d);
916
917 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800918 pr_err("%s: Unable to update ad_select because interface is up.\n",
919 bond->dev->name);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800920 ret = -EPERM;
921 goto out;
922 }
923
924 new_value = bond_parse_parm(buf, ad_select_tbl);
925
926 if (new_value != -1) {
927 bond->params.ad_select = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800928 pr_info("%s: Setting ad_select to %s (%d).\n",
929 bond->dev->name, ad_select_tbl[new_value].modename,
930 new_value);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800931 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800932 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -0800933 bond->dev->name, (int)strlen(buf) - 1, buf);
934 ret = -EINVAL;
935 }
936out:
937 return ret;
938}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000939static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
940 bonding_show_ad_select, bonding_store_ad_select);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800941
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800942/*
Ben Hutchingsad246c92011-04-26 15:25:52 +0000943 * Show and set the number of peer notifications to send after a failover event.
944 */
945static ssize_t bonding_show_num_peer_notif(struct device *d,
946 struct device_attribute *attr,
947 char *buf)
948{
949 struct bonding *bond = to_bond(d);
950 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
951}
952
953static ssize_t bonding_store_num_peer_notif(struct device *d,
954 struct device_attribute *attr,
955 const char *buf, size_t count)
956{
957 struct bonding *bond = to_bond(d);
958 int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
959 return err ? err : count;
960}
961static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
962 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
963static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
964 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
965
966/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800967 * Show and set the MII monitor interval. There are two tricky bits
968 * here. First, if MII monitoring is activated, then we must disable
969 * ARP monitoring. Second, if the timer isn't running, we must
970 * start it.
971 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700972static ssize_t bonding_show_miimon(struct device *d,
973 struct device_attribute *attr,
974 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800975{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700976 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800977
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800978 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800979}
980
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700981static ssize_t bonding_store_miimon(struct device *d,
982 struct device_attribute *attr,
983 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800984{
985 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700986 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800987
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000988 if (!rtnl_trylock())
989 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800990 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800991 pr_err("%s: no miimon value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800992 bond->dev->name);
993 ret = -EINVAL;
994 goto out;
995 }
996 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800997 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000998 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800999 ret = -EINVAL;
1000 goto out;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +00001001 }
1002 pr_info("%s: Setting MII monitoring interval to %d.\n",
1003 bond->dev->name, new_value);
1004 bond->params.miimon = new_value;
1005 if (bond->params.updelay)
1006 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
1007 bond->dev->name,
1008 bond->params.updelay * bond->params.miimon);
1009 if (bond->params.downdelay)
1010 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
1011 bond->dev->name,
1012 bond->params.downdelay * bond->params.miimon);
1013 if (new_value && bond->params.arp_interval) {
1014 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
1015 bond->dev->name);
1016 bond->params.arp_interval = 0;
1017 if (bond->params.arp_validate)
1018 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
1019 }
1020 if (bond->dev->flags & IFF_UP) {
1021 /* If the interface is up, we may need to fire off
1022 * the MII timer. If the interface is down, the
1023 * timer will get fired off when the open function
1024 * is called.
1025 */
1026 if (!new_value) {
1027 cancel_delayed_work_sync(&bond->mii_work);
1028 } else {
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00001029 cancel_delayed_work_sync(&bond->arp_work);
1030 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001031 }
1032 }
1033out:
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00001034 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001035 return ret;
1036}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001037static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1038 bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001039
1040/*
1041 * Show and set the primary slave. The store function is much
1042 * simpler than bonding_store_slaves function because it only needs to
1043 * handle one interface name.
1044 * The bond must be a mode that supports a primary for this be
1045 * set.
1046 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001047static ssize_t bonding_show_primary(struct device *d,
1048 struct device_attribute *attr,
1049 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001050{
1051 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001052 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001053
1054 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001055 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001056
1057 return count;
1058}
1059
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001060static ssize_t bonding_store_primary(struct device *d,
1061 struct device_attribute *attr,
1062 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001063{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001064 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001065 struct list_head *iter;
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001066 char ifname[IFNAMSIZ];
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001067 struct slave *slave;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001068
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001069 if (!rtnl_trylock())
1070 return restart_syscall();
Neil Hormane843fa52010-10-13 16:01:50 +00001071 block_netpoll_tx();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001072 read_lock(&bond->lock);
1073 write_lock_bh(&bond->curr_slave_lock);
1074
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001075 if (!USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001076 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1077 bond->dev->name, bond->dev->name, bond->params.mode);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001078 goto out;
1079 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001080
nikolay@redhat.comeb6e98a2012-10-31 04:42:51 +00001081 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001082
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001083 /* check to see if we are clearing primary */
1084 if (!strlen(ifname) || buf[0] == '\n') {
1085 pr_info("%s: Setting primary slave to None.\n",
1086 bond->dev->name);
1087 bond->primary_slave = NULL;
Milos Vyleteleb492f72013-01-29 09:59:00 +00001088 memset(bond->params.primary, 0, sizeof(bond->params.primary));
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001089 bond_select_active_slave(bond);
1090 goto out;
1091 }
1092
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001093 bond_for_each_slave(bond, slave, iter) {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001094 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1095 pr_info("%s: Setting %s as primary slave.\n",
1096 bond->dev->name, slave->dev->name);
1097 bond->primary_slave = slave;
1098 strcpy(bond->params.primary, slave->dev->name);
1099 bond_select_active_slave(bond);
1100 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001101 }
1102 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001103
Weiping Pan8a936642012-06-10 23:00:20 +00001104 strncpy(bond->params.primary, ifname, IFNAMSIZ);
1105 bond->params.primary[IFNAMSIZ - 1] = 0;
1106
1107 pr_info("%s: Recording %s as primary, "
1108 "but it has not been enslaved to %s yet.\n",
1109 bond->dev->name, ifname, bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001110out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001111 write_unlock_bh(&bond->curr_slave_lock);
1112 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001113 unblock_netpoll_tx();
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001114 rtnl_unlock();
1115
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001116 return count;
1117}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001118static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1119 bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001120
1121/*
Jiri Pirkoa5499522009-09-25 03:28:09 +00001122 * Show and set the primary_reselect flag.
1123 */
1124static ssize_t bonding_show_primary_reselect(struct device *d,
1125 struct device_attribute *attr,
1126 char *buf)
1127{
1128 struct bonding *bond = to_bond(d);
1129
1130 return sprintf(buf, "%s %d\n",
1131 pri_reselect_tbl[bond->params.primary_reselect].modename,
1132 bond->params.primary_reselect);
1133}
1134
1135static ssize_t bonding_store_primary_reselect(struct device *d,
1136 struct device_attribute *attr,
1137 const char *buf, size_t count)
1138{
1139 int new_value, ret = count;
1140 struct bonding *bond = to_bond(d);
1141
1142 if (!rtnl_trylock())
1143 return restart_syscall();
1144
1145 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1146 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001147 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001148 bond->dev->name,
1149 (int) strlen(buf) - 1, buf);
1150 ret = -EINVAL;
1151 goto out;
1152 }
1153
1154 bond->params.primary_reselect = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001155 pr_info("%s: setting primary_reselect to %s (%d).\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001156 bond->dev->name, pri_reselect_tbl[new_value].modename,
1157 new_value);
1158
Neil Hormane843fa52010-10-13 16:01:50 +00001159 block_netpoll_tx();
Jiri Pirkoa5499522009-09-25 03:28:09 +00001160 read_lock(&bond->lock);
1161 write_lock_bh(&bond->curr_slave_lock);
1162 bond_select_active_slave(bond);
1163 write_unlock_bh(&bond->curr_slave_lock);
1164 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001165 unblock_netpoll_tx();
Jiri Pirkoa5499522009-09-25 03:28:09 +00001166out:
1167 rtnl_unlock();
1168 return ret;
1169}
1170static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1171 bonding_show_primary_reselect,
1172 bonding_store_primary_reselect);
1173
1174/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001175 * Show and set the use_carrier flag.
1176 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001177static ssize_t bonding_show_carrier(struct device *d,
1178 struct device_attribute *attr,
1179 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001180{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001181 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001182
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001183 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001184}
1185
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001186static ssize_t bonding_store_carrier(struct device *d,
1187 struct device_attribute *attr,
1188 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001189{
1190 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001191 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001192
1193
1194 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001195 pr_err("%s: no use_carrier value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001196 bond->dev->name);
1197 ret = -EINVAL;
1198 goto out;
1199 }
1200 if ((new_value == 0) || (new_value == 1)) {
1201 bond->params.use_carrier = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001202 pr_info("%s: Setting use_carrier to %d.\n",
1203 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001204 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001205 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1206 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001207 }
1208out:
Jiri Pirko672bda32011-01-25 11:03:25 +00001209 return ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001210}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001211static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1212 bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001213
1214
1215/*
1216 * Show and set currently active_slave.
1217 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001218static ssize_t bonding_show_active_slave(struct device *d,
1219 struct device_attribute *attr,
1220 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001221{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001222 struct bonding *bond = to_bond(d);
Jiri Pirko752d48b2013-10-18 17:43:37 +02001223 struct net_device *slave_dev;
Wagner Ferenc16cd0162007-12-06 23:40:29 -08001224 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001225
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001226 rcu_read_lock();
Jiri Pirko752d48b2013-10-18 17:43:37 +02001227 slave_dev = bond_option_active_slave_get_rcu(bond);
1228 if (slave_dev)
1229 count = sprintf(buf, "%s\n", slave_dev->name);
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001230 rcu_read_unlock();
1231
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001232 return count;
1233}
1234
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001235static ssize_t bonding_store_active_slave(struct device *d,
1236 struct device_attribute *attr,
1237 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001238{
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001239 int ret;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001240 struct bonding *bond = to_bond(d);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001241 char ifname[IFNAMSIZ];
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001242 struct net_device *dev;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001243
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001244 if (!rtnl_trylock())
1245 return restart_syscall();
Neil Hormane843fa52010-10-13 16:01:50 +00001246
nikolay@redhat.comc84e1592012-10-31 06:03:52 +00001247 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001248 if (!strlen(ifname) || buf[0] == '\n') {
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001249 dev = NULL;
1250 } else {
1251 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1252 if (!dev) {
1253 ret = -ENODEV;
1254 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001255 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001256 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001257
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001258 ret = bond_option_active_slave_set(bond, dev);
1259 if (!ret)
1260 ret = count;
Neil Hormane843fa52010-10-13 16:01:50 +00001261
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001262 out:
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001263 rtnl_unlock();
1264
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001265 return ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001266
1267}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001268static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1269 bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001270
1271
1272/*
1273 * Show link status of the bond interface.
1274 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001275static ssize_t bonding_show_mii_status(struct device *d,
1276 struct device_attribute *attr,
1277 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001278{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001279 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001280
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001281 return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001282}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001283static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001284
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001285/*
1286 * Show current 802.3ad aggregator ID.
1287 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001288static ssize_t bonding_show_ad_aggregator(struct device *d,
1289 struct device_attribute *attr,
1290 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001291{
1292 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001293 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001294
1295 if (bond->params.mode == BOND_MODE_8023AD) {
1296 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001297 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001298 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001299 ? 0 : ad_info.aggregator_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001300 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001301
1302 return count;
1303}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001304static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001305
1306
1307/*
1308 * Show number of active 802.3ad ports.
1309 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001310static ssize_t bonding_show_ad_num_ports(struct device *d,
1311 struct device_attribute *attr,
1312 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001313{
1314 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001315 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001316
1317 if (bond->params.mode == BOND_MODE_8023AD) {
1318 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001319 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001320 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001321 ? 0 : ad_info.ports);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001322 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001323
1324 return count;
1325}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001326static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001327
1328
1329/*
1330 * Show current 802.3ad actor key.
1331 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001332static ssize_t bonding_show_ad_actor_key(struct device *d,
1333 struct device_attribute *attr,
1334 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001335{
1336 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001337 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001338
1339 if (bond->params.mode == BOND_MODE_8023AD) {
1340 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001341 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001342 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001343 ? 0 : ad_info.actor_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001344 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001345
1346 return count;
1347}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001348static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001349
1350
1351/*
1352 * Show current 802.3ad partner key.
1353 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001354static ssize_t bonding_show_ad_partner_key(struct device *d,
1355 struct device_attribute *attr,
1356 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001357{
1358 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001359 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001360
1361 if (bond->params.mode == BOND_MODE_8023AD) {
1362 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001363 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001364 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001365 ? 0 : ad_info.partner_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001366 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001367
1368 return count;
1369}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001370static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001371
1372
1373/*
1374 * Show current 802.3ad partner mac.
1375 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001376static ssize_t bonding_show_ad_partner_mac(struct device *d,
1377 struct device_attribute *attr,
1378 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001379{
1380 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001381 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001382
1383 if (bond->params.mode == BOND_MODE_8023AD) {
1384 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001385 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
Johannes Berge1749612008-10-27 15:59:26 -07001386 count = sprintf(buf, "%pM\n", ad_info.partner_system);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001387 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001388
1389 return count;
1390}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001391static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001392
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001393/*
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001394 * Show the queue_ids of the slaves in the current bond.
1395 */
1396static ssize_t bonding_show_queue_id(struct device *d,
1397 struct device_attribute *attr,
1398 char *buf)
1399{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001400 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001401 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001402 struct slave *slave;
1403 int res = 0;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001404
1405 if (!rtnl_trylock())
1406 return restart_syscall();
1407
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001408 bond_for_each_slave(bond, slave, iter) {
Nicolas de Pesloüan79236682010-07-14 18:24:54 -07001409 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1410 /* not enough space for another interface_name:queue_id pair */
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001411 if ((PAGE_SIZE - res) > 10)
1412 res = PAGE_SIZE - 10;
1413 res += sprintf(buf + res, "++more++ ");
1414 break;
1415 }
1416 res += sprintf(buf + res, "%s:%d ",
1417 slave->dev->name, slave->queue_id);
1418 }
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001419 if (res)
1420 buf[res-1] = '\n'; /* eat the leftover space */
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001421
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001422 rtnl_unlock();
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001423
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001424 return res;
1425}
1426
1427/*
1428 * Set the queue_ids of the slaves in the current bond. The bond
1429 * interface must be enslaved for this to work.
1430 */
1431static ssize_t bonding_store_queue_id(struct device *d,
1432 struct device_attribute *attr,
1433 const char *buffer, size_t count)
1434{
1435 struct slave *slave, *update_slave;
1436 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001437 struct list_head *iter;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001438 u16 qid;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001439 int ret = count;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001440 char *delim;
1441 struct net_device *sdev = NULL;
1442
1443 if (!rtnl_trylock())
1444 return restart_syscall();
1445
1446 /* delim will point to queue id if successful */
1447 delim = strchr(buffer, ':');
1448 if (!delim)
1449 goto err_no_cmd;
1450
1451 /*
1452 * Terminate string that points to device name and bump it
1453 * up one, so we can read the queue id there.
1454 */
1455 *delim = '\0';
1456 if (sscanf(++delim, "%hd\n", &qid) != 1)
1457 goto err_no_cmd;
1458
1459 /* Check buffer length, valid ifname and queue id */
1460 if (strlen(buffer) > IFNAMSIZ ||
1461 !dev_valid_name(buffer) ||
Jiri Pirko8a540ff2012-07-20 02:28:50 +00001462 qid > bond->dev->real_num_tx_queues)
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001463 goto err_no_cmd;
1464
1465 /* Get the pointer to that interface if it exists */
1466 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1467 if (!sdev)
1468 goto err_no_cmd;
1469
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001470 /* Search for thes slave and check for duplicate qids */
1471 update_slave = NULL;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001472 bond_for_each_slave(bond, slave, iter) {
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001473 if (sdev == slave->dev)
1474 /*
1475 * We don't need to check the matching
1476 * slave for dups, since we're overwriting it
1477 */
1478 update_slave = slave;
1479 else if (qid && qid == slave->queue_id) {
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001480 goto err_no_cmd;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001481 }
1482 }
1483
1484 if (!update_slave)
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001485 goto err_no_cmd;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001486
1487 /* Actually set the qids for the slave */
1488 update_slave->queue_id = qid;
1489
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001490out:
1491 rtnl_unlock();
1492 return ret;
1493
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001494err_no_cmd:
1495 pr_info("invalid input for queue_id set for %s.\n",
1496 bond->dev->name);
1497 ret = -EPERM;
1498 goto out;
1499}
1500
1501static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1502 bonding_store_queue_id);
1503
1504
1505/*
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001506 * Show and set the all_slaves_active flag.
1507 */
1508static ssize_t bonding_show_slaves_active(struct device *d,
1509 struct device_attribute *attr,
1510 char *buf)
1511{
1512 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001513
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001514 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1515}
1516
1517static ssize_t bonding_store_slaves_active(struct device *d,
1518 struct device_attribute *attr,
1519 const char *buf, size_t count)
1520{
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001521 struct bonding *bond = to_bond(d);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001522 int new_value, ret = count;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001523 struct list_head *iter;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001524 struct slave *slave;
1525
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001526 if (!rtnl_trylock())
1527 return restart_syscall();
1528
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001529 if (sscanf(buf, "%d", &new_value) != 1) {
1530 pr_err("%s: no all_slaves_active value specified.\n",
1531 bond->dev->name);
1532 ret = -EINVAL;
1533 goto out;
1534 }
1535
1536 if (new_value == bond->params.all_slaves_active)
1537 goto out;
1538
1539 if ((new_value == 0) || (new_value == 1)) {
1540 bond->params.all_slaves_active = new_value;
1541 } else {
1542 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1543 bond->dev->name, new_value);
1544 ret = -EINVAL;
1545 goto out;
1546 }
1547
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001548 bond_for_each_slave(bond, slave, iter) {
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001549 if (!bond_is_active_slave(slave)) {
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001550 if (new_value)
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001551 slave->inactive = 0;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001552 else
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001553 slave->inactive = 1;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001554 }
1555 }
1556out:
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001557 rtnl_unlock();
Jiri Pirko672bda32011-01-25 11:03:25 +00001558 return ret;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001559}
1560static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1561 bonding_show_slaves_active, bonding_store_slaves_active);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001562
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001563/*
1564 * Show and set the number of IGMP membership reports to send on link failure
1565 */
1566static ssize_t bonding_show_resend_igmp(struct device *d,
Flavio Leitner94265cf2011-05-25 08:38:58 +00001567 struct device_attribute *attr,
1568 char *buf)
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001569{
1570 struct bonding *bond = to_bond(d);
1571
1572 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1573}
1574
1575static ssize_t bonding_store_resend_igmp(struct device *d,
Flavio Leitner94265cf2011-05-25 08:38:58 +00001576 struct device_attribute *attr,
1577 const char *buf, size_t count)
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001578{
1579 int new_value, ret = count;
1580 struct bonding *bond = to_bond(d);
1581
1582 if (sscanf(buf, "%d", &new_value) != 1) {
1583 pr_err("%s: no resend_igmp value specified.\n",
1584 bond->dev->name);
1585 ret = -EINVAL;
1586 goto out;
1587 }
1588
Flavio Leitner94265cf2011-05-25 08:38:58 +00001589 if (new_value < 0 || new_value > 255) {
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001590 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1591 bond->dev->name, new_value);
1592 ret = -EINVAL;
1593 goto out;
1594 }
1595
1596 pr_info("%s: Setting resend_igmp to %d.\n",
1597 bond->dev->name, new_value);
1598 bond->params.resend_igmp = new_value;
1599out:
1600 return ret;
1601}
1602
1603static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1604 bonding_show_resend_igmp, bonding_store_resend_igmp);
1605
Neil Horman7eacd032013-09-13 11:05:33 -04001606
1607static ssize_t bonding_show_lp_interval(struct device *d,
1608 struct device_attribute *attr,
1609 char *buf)
1610{
1611 struct bonding *bond = to_bond(d);
1612 return sprintf(buf, "%d\n", bond->params.lp_interval);
1613}
1614
1615static ssize_t bonding_store_lp_interval(struct device *d,
1616 struct device_attribute *attr,
1617 const char *buf, size_t count)
1618{
1619 struct bonding *bond = to_bond(d);
1620 int new_value, ret = count;
1621
1622 if (sscanf(buf, "%d", &new_value) != 1) {
1623 pr_err("%s: no lp interval value specified.\n",
1624 bond->dev->name);
1625 ret = -EINVAL;
1626 goto out;
1627 }
1628
1629 if (new_value <= 0) {
1630 pr_err ("%s: lp_interval must be between 1 and %d\n",
1631 bond->dev->name, INT_MAX);
1632 ret = -EINVAL;
1633 goto out;
1634 }
1635
1636 bond->params.lp_interval = new_value;
1637out:
1638 return ret;
1639}
1640
1641static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1642 bonding_show_lp_interval, bonding_store_lp_interval);
1643
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01001644static ssize_t bonding_show_packets_per_slave(struct device *d,
1645 struct device_attribute *attr,
1646 char *buf)
1647{
1648 struct bonding *bond = to_bond(d);
1649 int packets_per_slave = bond->params.packets_per_slave;
1650
1651 if (packets_per_slave > 1)
1652 packets_per_slave = reciprocal_value(packets_per_slave);
1653
1654 return sprintf(buf, "%d\n", packets_per_slave);
1655}
1656
1657static ssize_t bonding_store_packets_per_slave(struct device *d,
1658 struct device_attribute *attr,
1659 const char *buf, size_t count)
1660{
1661 struct bonding *bond = to_bond(d);
1662 int new_value, ret = count;
1663
1664 if (sscanf(buf, "%d", &new_value) != 1) {
1665 pr_err("%s: no packets_per_slave value specified.\n",
1666 bond->dev->name);
1667 ret = -EINVAL;
1668 goto out;
1669 }
1670 if (new_value < 0 || new_value > USHRT_MAX) {
1671 pr_err("%s: packets_per_slave must be between 0 and %u\n",
1672 bond->dev->name, USHRT_MAX);
1673 ret = -EINVAL;
1674 goto out;
1675 }
1676 if (bond->params.mode != BOND_MODE_ROUNDROBIN)
1677 pr_warn("%s: Warning: packets_per_slave has effect only in balance-rr mode\n",
1678 bond->dev->name);
1679 if (new_value > 1)
1680 bond->params.packets_per_slave = reciprocal_value(new_value);
1681 else
1682 bond->params.packets_per_slave = new_value;
1683out:
1684 return ret;
1685}
1686
1687static DEVICE_ATTR(packets_per_slave, S_IRUGO | S_IWUSR,
1688 bonding_show_packets_per_slave,
1689 bonding_store_packets_per_slave);
1690
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001691static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001692 &dev_attr_slaves.attr,
1693 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001694 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001695 &dev_attr_arp_validate.attr,
Veaceslav Falico8599b522013-06-24 11:49:34 +02001696 &dev_attr_arp_all_targets.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001697 &dev_attr_arp_interval.attr,
1698 &dev_attr_arp_ip_target.attr,
1699 &dev_attr_downdelay.attr,
1700 &dev_attr_updelay.attr,
1701 &dev_attr_lacp_rate.attr,
Jay Vosburghfd989c82008-11-04 17:51:16 -08001702 &dev_attr_ad_select.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001703 &dev_attr_xmit_hash_policy.attr,
Ben Hutchingsad246c92011-04-26 15:25:52 +00001704 &dev_attr_num_grat_arp.attr,
1705 &dev_attr_num_unsol_na.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001706 &dev_attr_miimon.attr,
1707 &dev_attr_primary.attr,
Jiri Pirkoa5499522009-09-25 03:28:09 +00001708 &dev_attr_primary_reselect.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001709 &dev_attr_use_carrier.attr,
1710 &dev_attr_active_slave.attr,
1711 &dev_attr_mii_status.attr,
1712 &dev_attr_ad_aggregator.attr,
1713 &dev_attr_ad_num_ports.attr,
1714 &dev_attr_ad_actor_key.attr,
1715 &dev_attr_ad_partner_key.attr,
1716 &dev_attr_ad_partner_mac.attr,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001717 &dev_attr_queue_id.attr,
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001718 &dev_attr_all_slaves_active.attr,
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001719 &dev_attr_resend_igmp.attr,
stephen hemminger655f8912011-06-22 09:54:39 +00001720 &dev_attr_min_links.attr,
Neil Horman7eacd032013-09-13 11:05:33 -04001721 &dev_attr_lp_interval.attr,
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01001722 &dev_attr_packets_per_slave.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001723 NULL,
1724};
1725
1726static struct attribute_group bonding_group = {
1727 .name = "bonding",
1728 .attrs = per_bond_attrs,
1729};
1730
1731/*
1732 * Initialize sysfs. This sets up the bonding_masters file in
1733 * /sys/class/net.
1734 */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001735int bond_create_sysfs(struct bond_net *bn)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001736{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001737 int ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001738
Eric W. Biederman4c224002011-10-12 21:56:25 +00001739 bn->class_attr_bonding_masters = class_attr_bonding_masters;
Eric W. Biederman01718e32011-10-21 22:43:07 +00001740 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
Eric W. Biederman4c224002011-10-12 21:56:25 +00001741
1742 ret = netdev_class_create_file(&bn->class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001743 /*
1744 * Permit multiple loads of the module by ignoring failures to
1745 * create the bonding_masters sysfs file. Bonding devices
1746 * created by second or subsequent loads of the module will
1747 * not be listed in, or controllable by, bonding_masters, but
1748 * will have the usual "bonding" sysfs directory.
1749 *
1750 * This is done to preserve backwards compatibility for
1751 * initscripts/sysconfig, which load bonding multiple times to
1752 * configure multiple bonding devices.
1753 */
1754 if (ret == -EEXIST) {
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001755 /* Is someone being kinky and naming a device bonding_master? */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001756 if (__dev_get_by_name(bn->net,
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001757 class_attr_bonding_masters.attr.name))
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001758 pr_err("network device named %s already exists in sysfs",
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001759 class_attr_bonding_masters.attr.name);
Stephen Hemminger130aa612009-06-11 05:46:04 -07001760 ret = 0;
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001761 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001762
1763 return ret;
1764
1765}
1766
1767/*
1768 * Remove /sys/class/net/bonding_masters.
1769 */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001770void bond_destroy_sysfs(struct bond_net *bn)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001771{
Eric W. Biederman4c224002011-10-12 21:56:25 +00001772 netdev_class_remove_file(&bn->class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001773}
1774
1775/*
1776 * Initialize sysfs for each bond. This sets up and registers
1777 * the 'bondctl' directory for each individual bond under /sys/class/net.
1778 */
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001779void bond_prepare_sysfs_group(struct bonding *bond)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001780{
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001781 bond->dev->sysfs_groups[0] = &bonding_group;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001782}
1783