blob: d6d6fc7812d5ac32c2bce83770ab04418554e5ee [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
Jeff Kirsheradf8d3f2013-12-06 06:28:47 -080015 * with this program; if not, see <http://www.gnu.org/licenses/>.
Mitch Williamsb76cdba2005-11-09 10:36:41 -080016 *
17 * The full GNU General Public License is included in this distribution in the
18 * file called LICENSE.
19 *
Mitch Williamsb76cdba2005-11-09 10:36:41 -080020 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -080021
22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Mitch Williamsb76cdba2005-11-09 10:36:41 -080024#include <linux/kernel.h>
25#include <linux/module.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080026#include <linux/device.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040027#include <linux/sched.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080028#include <linux/fs.h>
29#include <linux/types.h>
30#include <linux/string.h>
31#include <linux/netdevice.h>
32#include <linux/inetdevice.h>
33#include <linux/in.h>
34#include <linux/sysfs.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080035#include <linux/ctype.h>
36#include <linux/inet.h>
37#include <linux/rtnetlink.h>
Stephen Hemminger5c5129b2009-06-12 19:02:51 +000038#include <linux/etherdevice.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070039#include <net/net_namespace.h>
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000040#include <net/netns/generic.h>
41#include <linux/nsproxy.h>
Nikolay Aleksandrov73958322013-11-05 13:51:41 +010042#include <linux/reciprocal_div.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
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800152/* class attribute for bond_masters file. This ends up in /sys/class/net */
Eric W. Biederman4c224002011-10-12 21:56:25 +0000153static const struct class_attribute class_attr_bonding_masters = {
154 .attr = {
155 .name = "bonding_masters",
156 .mode = S_IWUSR | S_IRUGO,
157 },
158 .show = bonding_show_bonds,
159 .store = bonding_store_bonds,
Eric W. Biederman4c224002011-10-12 21:56:25 +0000160};
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800161
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800162/*
163 * Show the slaves in the current bond.
164 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700165static ssize_t bonding_show_slaves(struct device *d,
166 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800167{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700168 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200169 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200170 struct slave *slave;
171 int res = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800172
dingtianhong4d1ae5f2013-10-15 16:28:42 +0800173 if (!rtnl_trylock())
174 return restart_syscall();
175
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200176 bond_for_each_slave(bond, slave, iter) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800177 if (res > (PAGE_SIZE - IFNAMSIZ)) {
178 /* not enough space for another interface name */
179 if ((PAGE_SIZE - res) > 10)
180 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800181 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800182 break;
183 }
184 res += sprintf(buf + res, "%s ", slave->dev->name);
185 }
dingtianhong4d1ae5f2013-10-15 16:28:42 +0800186
187 rtnl_unlock();
188
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800189 if (res)
190 buf[res-1] = '\n'; /* eat the leftover space */
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200191
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800192 return res;
193}
194
195/*
Veaceslav Falicod6641cc2013-05-28 01:26:13 +0000196 * Set the slaves in the current bond.
Jiri Pirkof9f35452010-05-18 05:46:39 +0000197 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
198 * All hard work should be done there.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800199 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700200static ssize_t bonding_store_slaves(struct device *d,
201 struct device_attribute *attr,
202 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800203{
204 char command[IFNAMSIZ + 1] = { 0, };
205 char *ifname;
Jiri Pirkof9f35452010-05-18 05:46:39 +0000206 int res, ret = count;
207 struct net_device *dev;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700208 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800209
Eric W. Biederman496a60c2009-05-13 17:02:50 +0000210 if (!rtnl_trylock())
211 return restart_syscall();
Jay Vosburgh027ea042008-01-17 16:25:02 -0800212
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800213 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
214 ifname = command + 1;
215 if ((strlen(command) <= 1) ||
216 !dev_valid_name(ifname))
217 goto err_no_cmd;
218
Jiri Pirkof9f35452010-05-18 05:46:39 +0000219 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
220 if (!dev) {
221 pr_info("%s: Interface %s does not exist!\n",
222 bond->dev->name, ifname);
223 ret = -ENODEV;
224 goto out;
225 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800226
Jiri Pirkof9f35452010-05-18 05:46:39 +0000227 switch (command[0]) {
228 case '+':
229 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800230 res = bond_enslave(bond->dev, dev);
Jiri Pirkof9f35452010-05-18 05:46:39 +0000231 break;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000232
Jiri Pirkof9f35452010-05-18 05:46:39 +0000233 case '-':
234 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
235 res = bond_release(bond->dev, dev);
236 break;
237
238 default:
239 goto err_no_cmd;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800240 }
241
Jiri Pirkof9f35452010-05-18 05:46:39 +0000242 if (res)
243 ret = res;
244 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800245
246err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800247 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
248 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800249 ret = -EPERM;
250
251out:
Jay Vosburgh027ea042008-01-17 16:25:02 -0800252 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800253 return ret;
254}
255
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000256static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
257 bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800258
259/*
260 * Show and set the bonding mode. The bond interface must be down to
261 * change the mode.
262 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700263static ssize_t bonding_show_mode(struct device *d,
264 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800265{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700266 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800267
268 return sprintf(buf, "%s %d\n",
269 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800270 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800271}
272
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700273static ssize_t bonding_store_mode(struct device *d,
274 struct device_attribute *attr,
275 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800276{
Jiri Pirko72be35f2013-10-18 17:43:34 +0200277 int new_value, ret;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700278 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800279
Jay Vosburghece95f72008-01-17 16:25:01 -0800280 new_value = bond_parse_parm(buf, bond_mode_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800281 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800282 pr_err("%s: Ignoring invalid mode value %.*s.\n",
283 bond->dev->name, (int)strlen(buf) - 1, buf);
Jiri Pirko72be35f2013-10-18 17:43:34 +0200284 return -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800285 }
Jiri Pirko72be35f2013-10-18 17:43:34 +0200286 if (!rtnl_trylock())
287 return restart_syscall();
288
289 ret = bond_option_mode_set(bond, new_value);
290 if (!ret) {
291 pr_info("%s: setting mode to %s (%d).\n",
292 bond->dev->name, bond_mode_tbl[new_value].modename,
293 new_value);
294 ret = count;
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000295 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000296
nikolay@redhat.comea6836d2013-05-18 01:18:28 +0000297 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800298 return ret;
299}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000300static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
301 bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800302
303/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000304 * Show and set the bonding transmit hash method.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800305 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700306static ssize_t bonding_show_xmit_hash(struct device *d,
307 struct device_attribute *attr,
308 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800309{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700310 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800311
Wagner Ferenc8e4b9322007-12-06 23:40:32 -0800312 return sprintf(buf, "%s %d\n",
313 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
314 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800315}
316
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700317static ssize_t bonding_store_xmit_hash(struct device *d,
318 struct device_attribute *attr,
319 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800320{
321 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700322 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800323
Jay Vosburghece95f72008-01-17 16:25:01 -0800324 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800325 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800326 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800327 bond->dev->name,
328 (int)strlen(buf) - 1, buf);
329 ret = -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800330 } else {
331 bond->params.xmit_policy = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800332 pr_info("%s: setting xmit hash policy to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000333 bond->dev->name,
334 xmit_hashtype_tbl[new_value].modename, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800335 }
Nikolay Aleksandrov53edee22013-05-24 00:59:47 +0000336
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800337 return ret;
338}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000339static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
340 bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800341
342/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700343 * Show and set arp_validate.
344 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700345static ssize_t bonding_show_arp_validate(struct device *d,
346 struct device_attribute *attr,
347 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700348{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700349 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700350
351 return sprintf(buf, "%s %d\n",
352 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800353 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700354}
355
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700356static ssize_t bonding_store_arp_validate(struct device *d,
357 struct device_attribute *attr,
358 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700359{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700360 struct bonding *bond = to_bond(d);
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200361 int new_value, ret = count;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700362
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200363 if (!rtnl_trylock())
364 return restart_syscall();
Jay Vosburghece95f72008-01-17 16:25:01 -0800365 new_value = bond_parse_parm(buf, arp_validate_tbl);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700366 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800367 pr_err("%s: Ignoring invalid arp_validate value %s\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700368 bond->dev->name, buf);
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200369 ret = -EINVAL;
370 goto out;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700371 }
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200372 if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800373 pr_err("%s: arp_validate only supported in active-backup mode.\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700374 bond->dev->name);
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200375 ret = -EINVAL;
376 goto out;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700377 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800378 pr_info("%s: setting arp_validate to %s (%d).\n",
379 bond->dev->name, arp_validate_tbl[new_value].modename,
380 new_value);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700381
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200382 if (bond->dev->flags & IFF_UP) {
383 if (!new_value)
384 bond->recv_probe = NULL;
385 else if (bond->params.arp_interval)
386 bond->recv_probe = bond_arp_rcv;
387 }
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700388 bond->params.arp_validate = new_value;
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200389out:
390 rtnl_unlock();
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700391
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200392 return ret;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700393}
394
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000395static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
396 bonding_store_arp_validate);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200397/*
398 * Show and set arp_all_targets.
399 */
400static ssize_t bonding_show_arp_all_targets(struct device *d,
401 struct device_attribute *attr,
402 char *buf)
403{
404 struct bonding *bond = to_bond(d);
405 int value = bond->params.arp_all_targets;
406
407 return sprintf(buf, "%s %d\n", arp_all_targets_tbl[value].modename,
408 value);
409}
410
411static ssize_t bonding_store_arp_all_targets(struct device *d,
412 struct device_attribute *attr,
413 const char *buf, size_t count)
414{
415 struct bonding *bond = to_bond(d);
416 int new_value;
417
418 new_value = bond_parse_parm(buf, arp_all_targets_tbl);
419 if (new_value < 0) {
420 pr_err("%s: Ignoring invalid arp_all_targets value %s\n",
421 bond->dev->name, buf);
422 return -EINVAL;
423 }
424 pr_info("%s: setting arp_all_targets to %s (%d).\n",
425 bond->dev->name, arp_all_targets_tbl[new_value].modename,
426 new_value);
427
428 bond->params.arp_all_targets = new_value;
429
430 return count;
431}
432
433static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
434 bonding_show_arp_all_targets, bonding_store_arp_all_targets);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700435
436/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700437 * Show and store fail_over_mac. User only allowed to change the
438 * value when there are no slaves.
439 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000440static ssize_t bonding_show_fail_over_mac(struct device *d,
441 struct device_attribute *attr,
442 char *buf)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700443{
444 struct bonding *bond = to_bond(d);
445
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700446 return sprintf(buf, "%s %d\n",
447 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
448 bond->params.fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700449}
450
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000451static ssize_t bonding_store_fail_over_mac(struct device *d,
452 struct device_attribute *attr,
453 const char *buf, size_t count)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700454{
dingtianhong9402b742013-07-23 15:25:39 +0800455 int new_value, ret = count;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700456 struct bonding *bond = to_bond(d);
457
dingtianhong9402b742013-07-23 15:25:39 +0800458 if (!rtnl_trylock())
459 return restart_syscall();
460
Veaceslav Falico0965a1f2013-09-25 09:20:21 +0200461 if (bond_has_slaves(bond)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800462 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
Jay Vosburghdd957c52007-10-09 19:57:24 -0700463 bond->dev->name);
dingtianhong9402b742013-07-23 15:25:39 +0800464 ret = -EPERM;
465 goto out;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700466 }
467
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700468 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
469 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800470 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700471 bond->dev->name, buf);
dingtianhong9402b742013-07-23 15:25:39 +0800472 ret = -EINVAL;
473 goto out;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700474 }
475
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700476 bond->params.fail_over_mac = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800477 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
478 bond->dev->name, fail_over_mac_tbl[new_value].modename,
479 new_value);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700480
dingtianhong9402b742013-07-23 15:25:39 +0800481out:
482 rtnl_unlock();
483 return ret;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700484}
485
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000486static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
487 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700488
489/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800490 * Show and set the arp timer interval. There are two tricky bits
491 * here. First, if ARP monitoring is activated, then we must disable
492 * MII monitoring. Second, if the ARP timer isn't running, we must
493 * start it.
494 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700495static ssize_t bonding_show_arp_interval(struct device *d,
496 struct device_attribute *attr,
497 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800498{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700499 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800500
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800501 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800502}
503
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700504static ssize_t bonding_store_arp_interval(struct device *d,
505 struct device_attribute *attr,
506 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800507{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700508 struct bonding *bond = to_bond(d);
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200509 int new_value, ret = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800510
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000511 if (!rtnl_trylock())
512 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800513 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800514 pr_err("%s: no arp_interval value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800515 bond->dev->name);
516 ret = -EINVAL;
517 goto out;
518 }
519 if (new_value < 0) {
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000520 pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800521 bond->dev->name, new_value, INT_MAX);
522 ret = -EINVAL;
523 goto out;
524 }
dingtianhongfe9d04a2013-11-22 22:28:43 +0800525 if (BOND_NO_USES_ARP(bond->params.mode)) {
Veaceslav Falicoec9f1d12013-11-12 15:37:40 +0100526 pr_info("%s: ARP monitoring cannot be used with ALB/TLB/802.3ad. Only MII monitoring is supported on %s.\n",
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000527 bond->dev->name, bond->dev->name);
528 ret = -EINVAL;
529 goto out;
530 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800531 pr_info("%s: Setting ARP monitoring interval to %d.\n",
532 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800533 bond->params.arp_interval = new_value;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000534 if (new_value) {
535 if (bond->params.miimon) {
536 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
537 bond->dev->name, bond->dev->name);
538 bond->params.miimon = 0;
539 }
540 if (!bond->params.arp_targets[0])
541 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
542 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800543 }
544 if (bond->dev->flags & IFF_UP) {
545 /* If the interface is up, we may need to fire off
546 * the ARP timer. If the interface is down, the
547 * timer will get fired off when the open function
548 * is called.
549 */
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000550 if (!new_value) {
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200551 if (bond->params.arp_validate)
552 bond->recv_probe = NULL;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000553 cancel_delayed_work_sync(&bond->arp_work);
554 } else {
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200555 /* arp_validate can be set only in active-backup mode */
556 if (bond->params.arp_validate)
557 bond->recv_probe = bond_arp_rcv;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000558 cancel_delayed_work_sync(&bond->mii_work);
559 queue_delayed_work(bond->wq, &bond->arp_work, 0);
560 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800561 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800562out:
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000563 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800564 return ret;
565}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000566static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
567 bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800568
569/*
570 * Show and set the arp targets.
571 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700572static ssize_t bonding_show_arp_targets(struct device *d,
573 struct device_attribute *attr,
574 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800575{
576 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700577 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800578
579 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
580 if (bond->params.arp_targets[i])
Harvey Harrison63779432008-10-31 00:56:00 -0700581 res += sprintf(buf + res, "%pI4 ",
582 &bond->params.arp_targets[i]);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800583 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800584 if (res)
585 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800586 return res;
587}
588
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700589static ssize_t bonding_store_arp_targets(struct device *d,
590 struct device_attribute *attr,
591 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800592{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700593 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200594 struct list_head *iter;
Veaceslav Falico8599b522013-06-24 11:49:34 +0200595 struct slave *slave;
596 __be32 newtarget, *targets;
597 unsigned long *targets_rx;
598 int ind, i, j, ret = -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800599
dingtianhong4d1ae5f2013-10-15 16:28:42 +0800600 if (!rtnl_trylock())
601 return restart_syscall();
602
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800603 targets = bond->params.arp_targets;
Wang Weidongf9de11a2013-11-15 10:34:30 -0500604 if (!in4_pton(buf + 1, -1, (u8 *)&newtarget, -1, NULL) ||
605 IS_IP_TARGET_UNUSABLE_ADDRESS(newtarget)) {
606 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
607 bond->dev->name, &newtarget);
608 goto out;
609 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800610 /* look for adds */
611 if (buf[0] == '+') {
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200612 if (bond_get_targets_ip(targets, newtarget) != -1) { /* dup */
613 pr_err("%s: ARP target %pI4 is already present\n",
614 bond->dev->name, &newtarget);
615 goto out;
616 }
617
Veaceslav Falico8599b522013-06-24 11:49:34 +0200618 ind = bond_get_targets_ip(targets, 0); /* first free slot */
619 if (ind == -1) {
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200620 pr_err("%s: ARP target table is full!\n",
621 bond->dev->name);
622 goto out;
623 }
624
625 pr_info("%s: adding ARP target %pI4.\n", bond->dev->name,
626 &newtarget);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200627 /* not to race with bond_arp_rcv */
628 write_lock_bh(&bond->lock);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200629 bond_for_each_slave(bond, slave, iter)
Veaceslav Falico8599b522013-06-24 11:49:34 +0200630 slave->target_last_arp_rx[ind] = jiffies;
631 targets[ind] = newtarget;
632 write_unlock_bh(&bond->lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000633 } else if (buf[0] == '-') {
Veaceslav Falico8599b522013-06-24 11:49:34 +0200634 ind = bond_get_targets_ip(targets, newtarget);
635 if (ind == -1) {
636 pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800637 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800638 goto out;
639 }
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200640
Veaceslav Falico8599b522013-06-24 11:49:34 +0200641 if (ind == 0 && !targets[1] && bond->params.arp_interval)
642 pr_warn("%s: removing last arp target with arp_interval on\n",
643 bond->dev->name);
644
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200645 pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
646 &newtarget);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200647
648 write_lock_bh(&bond->lock);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200649 bond_for_each_slave(bond, slave, iter) {
Veaceslav Falico8599b522013-06-24 11:49:34 +0200650 targets_rx = slave->target_last_arp_rx;
651 j = ind;
652 for (; (j < BOND_MAX_ARP_TARGETS-1) && targets[j+1]; j++)
653 targets_rx[j] = targets_rx[j+1];
654 targets_rx[j] = 0;
655 }
656 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200657 targets[i] = targets[i+1];
658 targets[i] = 0;
Veaceslav Falico8599b522013-06-24 11:49:34 +0200659 write_unlock_bh(&bond->lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000660 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800661 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
662 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800663 ret = -EPERM;
664 goto out;
665 }
666
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200667 ret = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800668out:
dingtianhong4d1ae5f2013-10-15 16:28:42 +0800669 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800670 return ret;
671}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700672static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800673
674/*
675 * Show and set the up and down delays. These must be multiples of the
676 * MII monitoring value, and are stored internally as the multiplier.
677 * Thus, we must translate to MS for the real world.
678 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700679static ssize_t bonding_show_downdelay(struct device *d,
680 struct device_attribute *attr,
681 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800682{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700683 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800684
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800685 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800686}
687
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700688static ssize_t bonding_store_downdelay(struct device *d,
689 struct device_attribute *attr,
690 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800691{
692 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700693 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800694
Nikolay Aleksandrovb869ccf2013-11-13 17:07:46 +0100695 if (!rtnl_trylock())
696 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800697 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800698 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800699 bond->dev->name);
700 ret = -EPERM;
701 goto out;
702 }
703
704 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800705 pr_err("%s: no down delay value specified.\n", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800706 ret = -EINVAL;
707 goto out;
708 }
709 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800710 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000711 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800712 ret = -EINVAL;
713 goto out;
714 } else {
715 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800716 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 +0000717 bond->dev->name, new_value,
718 bond->params.miimon,
719 (new_value / bond->params.miimon) *
720 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800721 }
722 bond->params.downdelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800723 pr_info("%s: Setting down delay to %d.\n",
724 bond->dev->name,
725 bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800726
727 }
728
729out:
Nikolay Aleksandrovb869ccf2013-11-13 17:07:46 +0100730 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800731 return ret;
732}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000733static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
734 bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800735
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700736static ssize_t bonding_show_updelay(struct device *d,
737 struct device_attribute *attr,
738 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800739{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700740 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800741
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800742 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800743
744}
745
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700746static ssize_t bonding_store_updelay(struct device *d,
747 struct device_attribute *attr,
748 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800749{
750 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700751 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800752
Nikolay Aleksandrovb869ccf2013-11-13 17:07:46 +0100753 if (!rtnl_trylock())
754 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800755 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800756 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800757 bond->dev->name);
758 ret = -EPERM;
759 goto out;
760 }
761
762 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800763 pr_err("%s: no up delay value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800764 bond->dev->name);
765 ret = -EINVAL;
766 goto out;
767 }
768 if (new_value < 0) {
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000769 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
770 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800771 ret = -EINVAL;
772 goto out;
773 } else {
774 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800775 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 +0000776 bond->dev->name, new_value,
777 bond->params.miimon,
778 (new_value / bond->params.miimon) *
779 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800780 }
781 bond->params.updelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800782 pr_info("%s: Setting up delay to %d.\n",
783 bond->dev->name,
784 bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800785 }
786
787out:
Nikolay Aleksandrovb869ccf2013-11-13 17:07:46 +0100788 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800789 return ret;
790}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000791static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
792 bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800793
794/*
795 * Show and set the LACP interval. Interface must be down, and the mode
796 * must be set to 802.3ad mode.
797 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700798static ssize_t bonding_show_lacp(struct device *d,
799 struct device_attribute *attr,
800 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800801{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700802 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800803
804 return sprintf(buf, "%s %d\n",
805 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800806 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800807}
808
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700809static ssize_t bonding_store_lacp(struct device *d,
810 struct device_attribute *attr,
811 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800812{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700813 struct bonding *bond = to_bond(d);
nikolay@redhat.comc5093162013-09-02 13:51:40 +0200814 int new_value, ret = count;
815
816 if (!rtnl_trylock())
817 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800818
819 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800820 pr_err("%s: Unable to update LACP rate because interface is up.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800821 bond->dev->name);
822 ret = -EPERM;
823 goto out;
824 }
825
826 if (bond->params.mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800827 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800828 bond->dev->name);
829 ret = -EPERM;
830 goto out;
831 }
832
Jay Vosburghece95f72008-01-17 16:25:01 -0800833 new_value = bond_parse_parm(buf, bond_lacp_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800834
835 if ((new_value == 1) || (new_value == 0)) {
836 bond->params.lacp_fast = new_value;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +0000837 bond_3ad_update_lacp_rate(bond);
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800838 pr_info("%s: Setting LACP rate to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000839 bond->dev->name, bond_lacp_tbl[new_value].modename,
840 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800841 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800842 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000843 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800844 ret = -EINVAL;
845 }
846out:
nikolay@redhat.comc5093162013-09-02 13:51:40 +0200847 rtnl_unlock();
848
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800849 return ret;
850}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000851static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
852 bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800853
stephen hemminger655f8912011-06-22 09:54:39 +0000854static ssize_t bonding_show_min_links(struct device *d,
855 struct device_attribute *attr,
856 char *buf)
857{
858 struct bonding *bond = to_bond(d);
859
860 return sprintf(buf, "%d\n", bond->params.min_links);
861}
862
863static ssize_t bonding_store_min_links(struct device *d,
864 struct device_attribute *attr,
865 const char *buf, size_t count)
866{
867 struct bonding *bond = to_bond(d);
868 int ret;
869 unsigned int new_value;
870
871 ret = kstrtouint(buf, 0, &new_value);
872 if (ret < 0) {
873 pr_err("%s: Ignoring invalid min links value %s.\n",
874 bond->dev->name, buf);
875 return ret;
876 }
877
878 pr_info("%s: Setting min links value to %u\n",
879 bond->dev->name, new_value);
880 bond->params.min_links = new_value;
881 return count;
882}
883static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
884 bonding_show_min_links, bonding_store_min_links);
885
Jay Vosburghfd989c82008-11-04 17:51:16 -0800886static ssize_t bonding_show_ad_select(struct device *d,
887 struct device_attribute *attr,
888 char *buf)
889{
890 struct bonding *bond = to_bond(d);
891
892 return sprintf(buf, "%s %d\n",
893 ad_select_tbl[bond->params.ad_select].modename,
894 bond->params.ad_select);
895}
896
897
898static ssize_t bonding_store_ad_select(struct device *d,
899 struct device_attribute *attr,
900 const char *buf, size_t count)
901{
902 int new_value, ret = count;
903 struct bonding *bond = to_bond(d);
904
905 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800906 pr_err("%s: Unable to update ad_select because interface is up.\n",
907 bond->dev->name);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800908 ret = -EPERM;
909 goto out;
910 }
911
912 new_value = bond_parse_parm(buf, ad_select_tbl);
913
914 if (new_value != -1) {
915 bond->params.ad_select = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800916 pr_info("%s: Setting ad_select to %s (%d).\n",
917 bond->dev->name, ad_select_tbl[new_value].modename,
918 new_value);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800919 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800920 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -0800921 bond->dev->name, (int)strlen(buf) - 1, buf);
922 ret = -EINVAL;
923 }
924out:
925 return ret;
926}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000927static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
928 bonding_show_ad_select, bonding_store_ad_select);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800929
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800930/*
Ben Hutchingsad246c92011-04-26 15:25:52 +0000931 * Show and set the number of peer notifications to send after a failover event.
932 */
933static ssize_t bonding_show_num_peer_notif(struct device *d,
934 struct device_attribute *attr,
935 char *buf)
936{
937 struct bonding *bond = to_bond(d);
938 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
939}
940
941static ssize_t bonding_store_num_peer_notif(struct device *d,
942 struct device_attribute *attr,
943 const char *buf, size_t count)
944{
945 struct bonding *bond = to_bond(d);
946 int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
947 return err ? err : count;
948}
949static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
950 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
951static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
952 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
953
954/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800955 * Show and set the MII monitor interval. There are two tricky bits
956 * here. First, if MII monitoring is activated, then we must disable
957 * ARP monitoring. Second, if the timer isn't running, we must
958 * start it.
959 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700960static ssize_t bonding_show_miimon(struct device *d,
961 struct device_attribute *attr,
962 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800963{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700964 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800965
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800966 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800967}
968
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700969static ssize_t bonding_store_miimon(struct device *d,
970 struct device_attribute *attr,
971 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800972{
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800973 int new_value, ret;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700974 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800975
976 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800977 pr_err("%s: no miimon value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800978 bond->dev->name);
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800979 return -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800980 }
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800981
982 if (!rtnl_trylock())
983 return restart_syscall();
984
985 ret = bond_option_miimon_set(bond, new_value);
986 if (!ret)
987 ret = count;
988
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000989 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800990 return ret;
991}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000992static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
993 bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800994
995/*
996 * Show and set the primary slave. The store function is much
997 * simpler than bonding_store_slaves function because it only needs to
998 * handle one interface name.
999 * The bond must be a mode that supports a primary for this be
1000 * set.
1001 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001002static ssize_t bonding_show_primary(struct device *d,
1003 struct device_attribute *attr,
1004 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001005{
1006 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001007 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001008
1009 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001010 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001011
1012 return count;
1013}
1014
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001015static ssize_t bonding_store_primary(struct device *d,
1016 struct device_attribute *attr,
1017 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001018{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001019 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001020 struct list_head *iter;
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001021 char ifname[IFNAMSIZ];
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001022 struct slave *slave;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001023
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001024 if (!rtnl_trylock())
1025 return restart_syscall();
Neil Hormane843fa52010-10-13 16:01:50 +00001026 block_netpoll_tx();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001027 read_lock(&bond->lock);
1028 write_lock_bh(&bond->curr_slave_lock);
1029
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001030 if (!USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001031 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1032 bond->dev->name, bond->dev->name, bond->params.mode);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001033 goto out;
1034 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001035
nikolay@redhat.comeb6e98a2012-10-31 04:42:51 +00001036 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001037
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001038 /* check to see if we are clearing primary */
1039 if (!strlen(ifname) || buf[0] == '\n') {
1040 pr_info("%s: Setting primary slave to None.\n",
1041 bond->dev->name);
1042 bond->primary_slave = NULL;
Milos Vyleteleb492f72013-01-29 09:59:00 +00001043 memset(bond->params.primary, 0, sizeof(bond->params.primary));
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001044 bond_select_active_slave(bond);
1045 goto out;
1046 }
1047
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001048 bond_for_each_slave(bond, slave, iter) {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001049 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1050 pr_info("%s: Setting %s as primary slave.\n",
1051 bond->dev->name, slave->dev->name);
1052 bond->primary_slave = slave;
1053 strcpy(bond->params.primary, slave->dev->name);
1054 bond_select_active_slave(bond);
1055 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001056 }
1057 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001058
Weiping Pan8a936642012-06-10 23:00:20 +00001059 strncpy(bond->params.primary, ifname, IFNAMSIZ);
1060 bond->params.primary[IFNAMSIZ - 1] = 0;
1061
1062 pr_info("%s: Recording %s as primary, "
1063 "but it has not been enslaved to %s yet.\n",
1064 bond->dev->name, ifname, bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001065out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001066 write_unlock_bh(&bond->curr_slave_lock);
1067 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001068 unblock_netpoll_tx();
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001069 rtnl_unlock();
1070
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001071 return count;
1072}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001073static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1074 bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001075
1076/*
Jiri Pirkoa5499522009-09-25 03:28:09 +00001077 * Show and set the primary_reselect flag.
1078 */
1079static ssize_t bonding_show_primary_reselect(struct device *d,
1080 struct device_attribute *attr,
1081 char *buf)
1082{
1083 struct bonding *bond = to_bond(d);
1084
1085 return sprintf(buf, "%s %d\n",
1086 pri_reselect_tbl[bond->params.primary_reselect].modename,
1087 bond->params.primary_reselect);
1088}
1089
1090static ssize_t bonding_store_primary_reselect(struct device *d,
1091 struct device_attribute *attr,
1092 const char *buf, size_t count)
1093{
1094 int new_value, ret = count;
1095 struct bonding *bond = to_bond(d);
1096
1097 if (!rtnl_trylock())
1098 return restart_syscall();
1099
1100 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1101 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001102 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001103 bond->dev->name,
1104 (int) strlen(buf) - 1, buf);
1105 ret = -EINVAL;
1106 goto out;
1107 }
1108
1109 bond->params.primary_reselect = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001110 pr_info("%s: setting primary_reselect to %s (%d).\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001111 bond->dev->name, pri_reselect_tbl[new_value].modename,
1112 new_value);
1113
Neil Hormane843fa52010-10-13 16:01:50 +00001114 block_netpoll_tx();
Jiri Pirkoa5499522009-09-25 03:28:09 +00001115 read_lock(&bond->lock);
1116 write_lock_bh(&bond->curr_slave_lock);
1117 bond_select_active_slave(bond);
1118 write_unlock_bh(&bond->curr_slave_lock);
1119 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001120 unblock_netpoll_tx();
Jiri Pirkoa5499522009-09-25 03:28:09 +00001121out:
1122 rtnl_unlock();
1123 return ret;
1124}
1125static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1126 bonding_show_primary_reselect,
1127 bonding_store_primary_reselect);
1128
1129/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001130 * Show and set the use_carrier flag.
1131 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001132static ssize_t bonding_show_carrier(struct device *d,
1133 struct device_attribute *attr,
1134 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001135{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001136 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001137
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001138 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001139}
1140
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001141static ssize_t bonding_store_carrier(struct device *d,
1142 struct device_attribute *attr,
1143 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001144{
1145 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001146 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001147
1148
1149 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001150 pr_err("%s: no use_carrier value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001151 bond->dev->name);
1152 ret = -EINVAL;
1153 goto out;
1154 }
1155 if ((new_value == 0) || (new_value == 1)) {
1156 bond->params.use_carrier = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001157 pr_info("%s: Setting use_carrier to %d.\n",
1158 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001159 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001160 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1161 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001162 }
1163out:
Jiri Pirko672bda32011-01-25 11:03:25 +00001164 return ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001165}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001166static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1167 bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001168
1169
1170/*
1171 * Show and set currently active_slave.
1172 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001173static ssize_t bonding_show_active_slave(struct device *d,
1174 struct device_attribute *attr,
1175 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001176{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001177 struct bonding *bond = to_bond(d);
Jiri Pirko752d48b2013-10-18 17:43:37 +02001178 struct net_device *slave_dev;
Wagner Ferenc16cd0162007-12-06 23:40:29 -08001179 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001180
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001181 rcu_read_lock();
Jiri Pirko752d48b2013-10-18 17:43:37 +02001182 slave_dev = bond_option_active_slave_get_rcu(bond);
1183 if (slave_dev)
1184 count = sprintf(buf, "%s\n", slave_dev->name);
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001185 rcu_read_unlock();
1186
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001187 return count;
1188}
1189
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001190static ssize_t bonding_store_active_slave(struct device *d,
1191 struct device_attribute *attr,
1192 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001193{
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001194 int ret;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001195 struct bonding *bond = to_bond(d);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001196 char ifname[IFNAMSIZ];
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001197 struct net_device *dev;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001198
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001199 if (!rtnl_trylock())
1200 return restart_syscall();
Neil Hormane843fa52010-10-13 16:01:50 +00001201
nikolay@redhat.comc84e1592012-10-31 06:03:52 +00001202 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001203 if (!strlen(ifname) || buf[0] == '\n') {
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001204 dev = NULL;
1205 } else {
1206 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1207 if (!dev) {
1208 ret = -ENODEV;
1209 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001210 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001211 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001212
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001213 ret = bond_option_active_slave_set(bond, dev);
1214 if (!ret)
1215 ret = count;
Neil Hormane843fa52010-10-13 16:01:50 +00001216
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001217 out:
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001218 rtnl_unlock();
1219
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001220 return ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001221
1222}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001223static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1224 bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001225
1226
1227/*
1228 * Show link status of the bond interface.
1229 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001230static ssize_t bonding_show_mii_status(struct device *d,
1231 struct device_attribute *attr,
1232 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001233{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001234 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001235
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001236 return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001237}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001238static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001239
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001240/*
1241 * Show current 802.3ad aggregator ID.
1242 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001243static ssize_t bonding_show_ad_aggregator(struct device *d,
1244 struct device_attribute *attr,
1245 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001246{
1247 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001248 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001249
1250 if (bond->params.mode == BOND_MODE_8023AD) {
1251 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001252 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001253 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001254 ? 0 : ad_info.aggregator_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001255 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001256
1257 return count;
1258}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001259static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001260
1261
1262/*
1263 * Show number of active 802.3ad ports.
1264 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001265static ssize_t bonding_show_ad_num_ports(struct device *d,
1266 struct device_attribute *attr,
1267 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001268{
1269 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001270 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001271
1272 if (bond->params.mode == BOND_MODE_8023AD) {
1273 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001274 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001275 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001276 ? 0 : ad_info.ports);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001277 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001278
1279 return count;
1280}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001281static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001282
1283
1284/*
1285 * Show current 802.3ad actor key.
1286 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001287static ssize_t bonding_show_ad_actor_key(struct device *d,
1288 struct device_attribute *attr,
1289 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001290{
1291 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001292 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001293
1294 if (bond->params.mode == BOND_MODE_8023AD) {
1295 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001296 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001297 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001298 ? 0 : ad_info.actor_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001299 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001300
1301 return count;
1302}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001303static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001304
1305
1306/*
1307 * Show current 802.3ad partner key.
1308 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001309static ssize_t bonding_show_ad_partner_key(struct device *d,
1310 struct device_attribute *attr,
1311 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001312{
1313 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001314 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001315
1316 if (bond->params.mode == BOND_MODE_8023AD) {
1317 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001318 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001319 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001320 ? 0 : ad_info.partner_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001321 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001322
1323 return count;
1324}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001325static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001326
1327
1328/*
1329 * Show current 802.3ad partner mac.
1330 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001331static ssize_t bonding_show_ad_partner_mac(struct device *d,
1332 struct device_attribute *attr,
1333 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001334{
1335 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001336 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001337
1338 if (bond->params.mode == BOND_MODE_8023AD) {
1339 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001340 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
Johannes Berge1749612008-10-27 15:59:26 -07001341 count = sprintf(buf, "%pM\n", ad_info.partner_system);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001342 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001343
1344 return count;
1345}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001346static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001347
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001348/*
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001349 * Show the queue_ids of the slaves in the current bond.
1350 */
1351static ssize_t bonding_show_queue_id(struct device *d,
1352 struct device_attribute *attr,
1353 char *buf)
1354{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001355 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001356 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001357 struct slave *slave;
1358 int res = 0;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001359
1360 if (!rtnl_trylock())
1361 return restart_syscall();
1362
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001363 bond_for_each_slave(bond, slave, iter) {
Nicolas de Pesloüan79236682010-07-14 18:24:54 -07001364 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1365 /* not enough space for another interface_name:queue_id pair */
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001366 if ((PAGE_SIZE - res) > 10)
1367 res = PAGE_SIZE - 10;
1368 res += sprintf(buf + res, "++more++ ");
1369 break;
1370 }
1371 res += sprintf(buf + res, "%s:%d ",
1372 slave->dev->name, slave->queue_id);
1373 }
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001374 if (res)
1375 buf[res-1] = '\n'; /* eat the leftover space */
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001376
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001377 rtnl_unlock();
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001378
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001379 return res;
1380}
1381
1382/*
1383 * Set the queue_ids of the slaves in the current bond. The bond
1384 * interface must be enslaved for this to work.
1385 */
1386static ssize_t bonding_store_queue_id(struct device *d,
1387 struct device_attribute *attr,
1388 const char *buffer, size_t count)
1389{
1390 struct slave *slave, *update_slave;
1391 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001392 struct list_head *iter;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001393 u16 qid;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001394 int ret = count;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001395 char *delim;
1396 struct net_device *sdev = NULL;
1397
1398 if (!rtnl_trylock())
1399 return restart_syscall();
1400
1401 /* delim will point to queue id if successful */
1402 delim = strchr(buffer, ':');
1403 if (!delim)
1404 goto err_no_cmd;
1405
1406 /*
1407 * Terminate string that points to device name and bump it
1408 * up one, so we can read the queue id there.
1409 */
1410 *delim = '\0';
1411 if (sscanf(++delim, "%hd\n", &qid) != 1)
1412 goto err_no_cmd;
1413
1414 /* Check buffer length, valid ifname and queue id */
1415 if (strlen(buffer) > IFNAMSIZ ||
1416 !dev_valid_name(buffer) ||
Jiri Pirko8a540ff2012-07-20 02:28:50 +00001417 qid > bond->dev->real_num_tx_queues)
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001418 goto err_no_cmd;
1419
1420 /* Get the pointer to that interface if it exists */
1421 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1422 if (!sdev)
1423 goto err_no_cmd;
1424
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001425 /* Search for thes slave and check for duplicate qids */
1426 update_slave = NULL;
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001427 bond_for_each_slave(bond, slave, iter) {
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001428 if (sdev == slave->dev)
1429 /*
1430 * We don't need to check the matching
1431 * slave for dups, since we're overwriting it
1432 */
1433 update_slave = slave;
1434 else if (qid && qid == slave->queue_id) {
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001435 goto err_no_cmd;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001436 }
1437 }
1438
1439 if (!update_slave)
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001440 goto err_no_cmd;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001441
1442 /* Actually set the qids for the slave */
1443 update_slave->queue_id = qid;
1444
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001445out:
1446 rtnl_unlock();
1447 return ret;
1448
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001449err_no_cmd:
1450 pr_info("invalid input for queue_id set for %s.\n",
1451 bond->dev->name);
1452 ret = -EPERM;
1453 goto out;
1454}
1455
1456static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1457 bonding_store_queue_id);
1458
1459
1460/*
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001461 * Show and set the all_slaves_active flag.
1462 */
1463static ssize_t bonding_show_slaves_active(struct device *d,
1464 struct device_attribute *attr,
1465 char *buf)
1466{
1467 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001468
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001469 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1470}
1471
1472static ssize_t bonding_store_slaves_active(struct device *d,
1473 struct device_attribute *attr,
1474 const char *buf, size_t count)
1475{
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001476 struct bonding *bond = to_bond(d);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001477 int new_value, ret = count;
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001478 struct list_head *iter;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001479 struct slave *slave;
1480
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001481 if (!rtnl_trylock())
1482 return restart_syscall();
1483
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001484 if (sscanf(buf, "%d", &new_value) != 1) {
1485 pr_err("%s: no all_slaves_active value specified.\n",
1486 bond->dev->name);
1487 ret = -EINVAL;
1488 goto out;
1489 }
1490
1491 if (new_value == bond->params.all_slaves_active)
1492 goto out;
1493
1494 if ((new_value == 0) || (new_value == 1)) {
1495 bond->params.all_slaves_active = new_value;
1496 } else {
1497 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1498 bond->dev->name, new_value);
1499 ret = -EINVAL;
1500 goto out;
1501 }
1502
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001503 bond_for_each_slave(bond, slave, iter) {
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001504 if (!bond_is_active_slave(slave)) {
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001505 if (new_value)
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001506 slave->inactive = 0;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001507 else
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001508 slave->inactive = 1;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001509 }
1510 }
1511out:
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001512 rtnl_unlock();
Jiri Pirko672bda32011-01-25 11:03:25 +00001513 return ret;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001514}
1515static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1516 bonding_show_slaves_active, bonding_store_slaves_active);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001517
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001518/*
1519 * Show and set the number of IGMP membership reports to send on link failure
1520 */
1521static ssize_t bonding_show_resend_igmp(struct device *d,
Flavio Leitner94265cf2011-05-25 08:38:58 +00001522 struct device_attribute *attr,
1523 char *buf)
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001524{
1525 struct bonding *bond = to_bond(d);
1526
1527 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1528}
1529
1530static ssize_t bonding_store_resend_igmp(struct device *d,
Flavio Leitner94265cf2011-05-25 08:38:58 +00001531 struct device_attribute *attr,
1532 const char *buf, size_t count)
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001533{
1534 int new_value, ret = count;
1535 struct bonding *bond = to_bond(d);
1536
1537 if (sscanf(buf, "%d", &new_value) != 1) {
1538 pr_err("%s: no resend_igmp value specified.\n",
1539 bond->dev->name);
1540 ret = -EINVAL;
1541 goto out;
1542 }
1543
Flavio Leitner94265cf2011-05-25 08:38:58 +00001544 if (new_value < 0 || new_value > 255) {
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001545 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1546 bond->dev->name, new_value);
1547 ret = -EINVAL;
1548 goto out;
1549 }
1550
1551 pr_info("%s: Setting resend_igmp to %d.\n",
1552 bond->dev->name, new_value);
1553 bond->params.resend_igmp = new_value;
1554out:
1555 return ret;
1556}
1557
1558static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1559 bonding_show_resend_igmp, bonding_store_resend_igmp);
1560
Neil Horman7eacd032013-09-13 11:05:33 -04001561
1562static ssize_t bonding_show_lp_interval(struct device *d,
1563 struct device_attribute *attr,
1564 char *buf)
1565{
1566 struct bonding *bond = to_bond(d);
1567 return sprintf(buf, "%d\n", bond->params.lp_interval);
1568}
1569
1570static ssize_t bonding_store_lp_interval(struct device *d,
1571 struct device_attribute *attr,
1572 const char *buf, size_t count)
1573{
1574 struct bonding *bond = to_bond(d);
1575 int new_value, ret = count;
1576
1577 if (sscanf(buf, "%d", &new_value) != 1) {
1578 pr_err("%s: no lp interval value specified.\n",
1579 bond->dev->name);
1580 ret = -EINVAL;
1581 goto out;
1582 }
1583
1584 if (new_value <= 0) {
1585 pr_err ("%s: lp_interval must be between 1 and %d\n",
1586 bond->dev->name, INT_MAX);
1587 ret = -EINVAL;
1588 goto out;
1589 }
1590
1591 bond->params.lp_interval = new_value;
1592out:
1593 return ret;
1594}
1595
1596static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1597 bonding_show_lp_interval, bonding_store_lp_interval);
1598
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01001599static ssize_t bonding_show_packets_per_slave(struct device *d,
1600 struct device_attribute *attr,
1601 char *buf)
1602{
1603 struct bonding *bond = to_bond(d);
Nikolay Aleksandrova752a8b2013-12-05 11:36:58 +01001604 unsigned int packets_per_slave = bond->params.packets_per_slave;
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01001605
1606 if (packets_per_slave > 1)
1607 packets_per_slave = reciprocal_value(packets_per_slave);
1608
Nikolay Aleksandrova752a8b2013-12-05 11:36:58 +01001609 return sprintf(buf, "%u\n", packets_per_slave);
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01001610}
1611
1612static ssize_t bonding_store_packets_per_slave(struct device *d,
1613 struct device_attribute *attr,
1614 const char *buf, size_t count)
1615{
1616 struct bonding *bond = to_bond(d);
1617 int new_value, ret = count;
1618
1619 if (sscanf(buf, "%d", &new_value) != 1) {
1620 pr_err("%s: no packets_per_slave value specified.\n",
1621 bond->dev->name);
1622 ret = -EINVAL;
1623 goto out;
1624 }
1625 if (new_value < 0 || new_value > USHRT_MAX) {
1626 pr_err("%s: packets_per_slave must be between 0 and %u\n",
1627 bond->dev->name, USHRT_MAX);
1628 ret = -EINVAL;
1629 goto out;
1630 }
1631 if (bond->params.mode != BOND_MODE_ROUNDROBIN)
1632 pr_warn("%s: Warning: packets_per_slave has effect only in balance-rr mode\n",
1633 bond->dev->name);
1634 if (new_value > 1)
1635 bond->params.packets_per_slave = reciprocal_value(new_value);
1636 else
1637 bond->params.packets_per_slave = new_value;
1638out:
1639 return ret;
1640}
1641
1642static DEVICE_ATTR(packets_per_slave, S_IRUGO | S_IWUSR,
1643 bonding_show_packets_per_slave,
1644 bonding_store_packets_per_slave);
1645
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001646static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001647 &dev_attr_slaves.attr,
1648 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001649 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001650 &dev_attr_arp_validate.attr,
Veaceslav Falico8599b522013-06-24 11:49:34 +02001651 &dev_attr_arp_all_targets.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001652 &dev_attr_arp_interval.attr,
1653 &dev_attr_arp_ip_target.attr,
1654 &dev_attr_downdelay.attr,
1655 &dev_attr_updelay.attr,
1656 &dev_attr_lacp_rate.attr,
Jay Vosburghfd989c82008-11-04 17:51:16 -08001657 &dev_attr_ad_select.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001658 &dev_attr_xmit_hash_policy.attr,
Ben Hutchingsad246c92011-04-26 15:25:52 +00001659 &dev_attr_num_grat_arp.attr,
1660 &dev_attr_num_unsol_na.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001661 &dev_attr_miimon.attr,
1662 &dev_attr_primary.attr,
Jiri Pirkoa5499522009-09-25 03:28:09 +00001663 &dev_attr_primary_reselect.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001664 &dev_attr_use_carrier.attr,
1665 &dev_attr_active_slave.attr,
1666 &dev_attr_mii_status.attr,
1667 &dev_attr_ad_aggregator.attr,
1668 &dev_attr_ad_num_ports.attr,
1669 &dev_attr_ad_actor_key.attr,
1670 &dev_attr_ad_partner_key.attr,
1671 &dev_attr_ad_partner_mac.attr,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001672 &dev_attr_queue_id.attr,
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001673 &dev_attr_all_slaves_active.attr,
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001674 &dev_attr_resend_igmp.attr,
stephen hemminger655f8912011-06-22 09:54:39 +00001675 &dev_attr_min_links.attr,
Neil Horman7eacd032013-09-13 11:05:33 -04001676 &dev_attr_lp_interval.attr,
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01001677 &dev_attr_packets_per_slave.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001678 NULL,
1679};
1680
1681static struct attribute_group bonding_group = {
1682 .name = "bonding",
1683 .attrs = per_bond_attrs,
1684};
1685
1686/*
1687 * Initialize sysfs. This sets up the bonding_masters file in
1688 * /sys/class/net.
1689 */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001690int bond_create_sysfs(struct bond_net *bn)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001691{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001692 int ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001693
Eric W. Biederman4c224002011-10-12 21:56:25 +00001694 bn->class_attr_bonding_masters = class_attr_bonding_masters;
Eric W. Biederman01718e32011-10-21 22:43:07 +00001695 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
Eric W. Biederman4c224002011-10-12 21:56:25 +00001696
Tejun Heo58292cbe2013-09-11 22:29:04 -04001697 ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
1698 bn->net);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001699 /*
1700 * Permit multiple loads of the module by ignoring failures to
1701 * create the bonding_masters sysfs file. Bonding devices
1702 * created by second or subsequent loads of the module will
1703 * not be listed in, or controllable by, bonding_masters, but
1704 * will have the usual "bonding" sysfs directory.
1705 *
1706 * This is done to preserve backwards compatibility for
1707 * initscripts/sysconfig, which load bonding multiple times to
1708 * configure multiple bonding devices.
1709 */
1710 if (ret == -EEXIST) {
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001711 /* Is someone being kinky and naming a device bonding_master? */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001712 if (__dev_get_by_name(bn->net,
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001713 class_attr_bonding_masters.attr.name))
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001714 pr_err("network device named %s already exists in sysfs",
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001715 class_attr_bonding_masters.attr.name);
Stephen Hemminger130aa612009-06-11 05:46:04 -07001716 ret = 0;
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001717 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001718
1719 return ret;
1720
1721}
1722
1723/*
1724 * Remove /sys/class/net/bonding_masters.
1725 */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001726void bond_destroy_sysfs(struct bond_net *bn)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001727{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001728 netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001729}
1730
1731/*
1732 * Initialize sysfs for each bond. This sets up and registers
1733 * the 'bondctl' directory for each individual bond under /sys/class/net.
1734 */
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001735void bond_prepare_sysfs_group(struct bonding *bond)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001736{
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001737 bond->dev->sysfs_groups[0] = &bonding_group;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001738}
1739