blob: ec9b6460a38dd0549b5b69f281b36f6cce0a3e7d [file] [log] [blame]
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001/*
2 * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
Mitch Williamsb76cdba2005-11-09 10:36:41 -080021 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -080022
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Mitch Williamsb76cdba2005-11-09 10:36:41 -080025#include <linux/kernel.h>
26#include <linux/module.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080027#include <linux/device.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040028#include <linux/sched.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080029#include <linux/fs.h>
30#include <linux/types.h>
31#include <linux/string.h>
32#include <linux/netdevice.h>
33#include <linux/inetdevice.h>
34#include <linux/in.h>
35#include <linux/sysfs.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080036#include <linux/ctype.h>
37#include <linux/inet.h>
38#include <linux/rtnetlink.h>
Stephen Hemminger5c5129b2009-06-12 19:02:51 +000039#include <linux/etherdevice.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070040#include <net/net_namespace.h>
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000041#include <net/netns/generic.h>
42#include <linux/nsproxy.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080043
Mitch Williamsb76cdba2005-11-09 10:36:41 -080044#include "bonding.h"
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -080045
Stephen Hemminger3d632c32009-06-12 19:02:48 +000046#define to_dev(obj) container_of(obj, struct device, kobj)
Wang Chen454d7c92008-11-12 23:37:49 -080047#define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
Mitch Williamsb76cdba2005-11-09 10:36:41 -080048
Mitch Williamsb76cdba2005-11-09 10:36:41 -080049/*
50 * "show" function for the bond_masters attribute.
51 * The class parameter is ignored.
52 */
Andi Kleen28812fe2010-01-05 12:48:07 +010053static ssize_t bonding_show_bonds(struct class *cls,
54 struct class_attribute *attr,
55 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080056{
Eric W. Biederman4c224002011-10-12 21:56:25 +000057 struct bond_net *bn =
58 container_of(attr, struct bond_net, class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080059 int res = 0;
60 struct bonding *bond;
61
Stephen Hemminger7e083842009-06-12 19:02:46 +000062 rtnl_lock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080063
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000064 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -080065 if (res > (PAGE_SIZE - IFNAMSIZ)) {
66 /* not enough space for another interface name */
67 if ((PAGE_SIZE - res) > 10)
68 res = PAGE_SIZE - 10;
Wagner Ferencb8843662007-12-06 23:40:30 -080069 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -080070 break;
71 }
Wagner Ferencb8843662007-12-06 23:40:30 -080072 res += sprintf(buf + res, "%s ", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080073 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -080074 if (res)
75 buf[res-1] = '\n'; /* eat the leftover space */
Stephen Hemminger7e083842009-06-12 19:02:46 +000076
77 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080078 return res;
79}
80
Eric W. Biederman4c224002011-10-12 21:56:25 +000081static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
Stephen Hemminger373500d2009-06-12 19:02:50 +000082{
83 struct bonding *bond;
84
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000085 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Stephen Hemminger373500d2009-06-12 19:02:50 +000086 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
87 return bond->dev;
88 }
89 return NULL;
90}
91
Mitch Williamsb76cdba2005-11-09 10:36:41 -080092/*
93 * "store" function for the bond_masters attribute. This is what
94 * creates and deletes entire bonds.
95 *
96 * The class parameter is ignored.
97 *
98 */
99
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000100static ssize_t bonding_store_bonds(struct class *cls,
Andi Kleen28812fe2010-01-05 12:48:07 +0100101 struct class_attribute *attr,
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000102 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800103{
Eric W. Biederman4c224002011-10-12 21:56:25 +0000104 struct bond_net *bn =
105 container_of(attr, struct bond_net, class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800106 char command[IFNAMSIZ + 1] = {0, };
107 char *ifname;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800108 int rv, res = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800109
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800110 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
111 ifname = command + 1;
112 if ((strlen(command) <= 1) ||
113 !dev_valid_name(ifname))
114 goto err_no_cmd;
115
116 if (command[0] == '+') {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800117 pr_info("%s is being created...\n", ifname);
Eric W. Biederman4c224002011-10-12 21:56:25 +0000118 rv = bond_create(bn->net, ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800119 if (rv) {
Phil Oester5f86cad12011-03-14 06:22:06 +0000120 if (rv == -EEXIST)
121 pr_info("%s already exists.\n", ifname);
122 else
123 pr_info("%s creation failed.\n", ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800124 res = rv;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800125 }
Stephen Hemminger373500d2009-06-12 19:02:50 +0000126 } else if (command[0] == '-') {
127 struct net_device *bond_dev;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800128
Jay Vosburgh027ea042008-01-17 16:25:02 -0800129 rtnl_lock();
Eric W. Biederman4c224002011-10-12 21:56:25 +0000130 bond_dev = bond_get_by_name(bn, ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000131 if (bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800132 pr_info("%s is being deleted...\n", ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000133 unregister_netdevice(bond_dev);
134 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800135 pr_err("unable to delete non-existent %s\n", ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000136 res = -ENODEV;
137 }
138 rtnl_unlock();
139 } else
140 goto err_no_cmd;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800141
Stephen Hemminger373500d2009-06-12 19:02:50 +0000142 /* Always return either count or an error. If you return 0, you'll
143 * get called forever, which is bad.
144 */
145 return res;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800146
147err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800148 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700149 return -EPERM;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800150}
Stephen Hemminger373500d2009-06-12 19:02:50 +0000151
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
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000162int bond_create_slave_symlinks(struct net_device *master,
163 struct net_device *slave)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800164{
165 char linkname[IFNAMSIZ+7];
166 int ret = 0;
167
168 /* first, create a link from the slave back to the master */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700169 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800170 "master");
171 if (ret)
172 return ret;
173 /* next, create a link from the master to the slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000174 sprintf(linkname, "slave_%s", slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700175 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800176 linkname);
Veaceslav Falico9fe16b72013-03-26 17:43:28 +0100177
178 /* free the master link created earlier in case of error */
179 if (ret)
180 sysfs_remove_link(&(slave->dev.kobj), "master");
181
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800182 return ret;
183
184}
185
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000186void bond_destroy_slave_symlinks(struct net_device *master,
187 struct net_device *slave)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800188{
189 char linkname[IFNAMSIZ+7];
190
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700191 sysfs_remove_link(&(slave->dev.kobj), "master");
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000192 sprintf(linkname, "slave_%s", slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700193 sysfs_remove_link(&(master->dev.kobj), linkname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800194}
195
196
197/*
198 * Show the slaves in the current bond.
199 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700200static ssize_t bonding_show_slaves(struct device *d,
201 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800202{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700203 struct bonding *bond = to_bond(d);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200204 struct slave *slave;
205 int res = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800206
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700207 read_lock(&bond->lock);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200208 bond_for_each_slave(bond, slave) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800209 if (res > (PAGE_SIZE - IFNAMSIZ)) {
210 /* not enough space for another interface name */
211 if ((PAGE_SIZE - res) > 10)
212 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800213 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800214 break;
215 }
216 res += sprintf(buf + res, "%s ", slave->dev->name);
217 }
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700218 read_unlock(&bond->lock);
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800219 if (res)
220 buf[res-1] = '\n'; /* eat the leftover space */
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200221
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800222 return res;
223}
224
225/*
Veaceslav Falicod6641cc2013-05-28 01:26:13 +0000226 * Set the slaves in the current bond.
Jiri Pirkof9f35452010-05-18 05:46:39 +0000227 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
228 * All hard work should be done there.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800229 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700230static ssize_t bonding_store_slaves(struct device *d,
231 struct device_attribute *attr,
232 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800233{
234 char command[IFNAMSIZ + 1] = { 0, };
235 char *ifname;
Jiri Pirkof9f35452010-05-18 05:46:39 +0000236 int res, ret = count;
237 struct net_device *dev;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700238 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800239
Eric W. Biederman496a60c2009-05-13 17:02:50 +0000240 if (!rtnl_trylock())
241 return restart_syscall();
Jay Vosburgh027ea042008-01-17 16:25:02 -0800242
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800243 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
244 ifname = command + 1;
245 if ((strlen(command) <= 1) ||
246 !dev_valid_name(ifname))
247 goto err_no_cmd;
248
Jiri Pirkof9f35452010-05-18 05:46:39 +0000249 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
250 if (!dev) {
251 pr_info("%s: Interface %s does not exist!\n",
252 bond->dev->name, ifname);
253 ret = -ENODEV;
254 goto out;
255 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800256
Jiri Pirkof9f35452010-05-18 05:46:39 +0000257 switch (command[0]) {
258 case '+':
259 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800260 res = bond_enslave(bond->dev, dev);
Jiri Pirkof9f35452010-05-18 05:46:39 +0000261 break;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000262
Jiri Pirkof9f35452010-05-18 05:46:39 +0000263 case '-':
264 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
265 res = bond_release(bond->dev, dev);
266 break;
267
268 default:
269 goto err_no_cmd;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800270 }
271
Jiri Pirkof9f35452010-05-18 05:46:39 +0000272 if (res)
273 ret = res;
274 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800275
276err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800277 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
278 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800279 ret = -EPERM;
280
281out:
Jay Vosburgh027ea042008-01-17 16:25:02 -0800282 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800283 return ret;
284}
285
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000286static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
287 bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800288
289/*
290 * Show and set the bonding mode. The bond interface must be down to
291 * change the mode.
292 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700293static ssize_t bonding_show_mode(struct device *d,
294 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800295{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700296 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800297
298 return sprintf(buf, "%s %d\n",
299 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800300 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800301}
302
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700303static ssize_t bonding_store_mode(struct device *d,
304 struct device_attribute *attr,
305 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800306{
307 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700308 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800309
nikolay@redhat.comea6836d2013-05-18 01:18:28 +0000310 if (!rtnl_trylock())
311 return restart_syscall();
312
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800313 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800314 pr_err("unable to update mode of %s because interface is up.\n",
315 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800316 ret = -EPERM;
317 goto out;
318 }
319
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200320 if (!list_empty(&bond->slave_list)) {
Veaceslav Falico4a8bb7e2011-11-15 06:44:42 +0000321 pr_err("unable to update mode of %s because it has slaves.\n",
322 bond->dev->name);
323 ret = -EPERM;
324 goto out;
325 }
326
Jay Vosburghece95f72008-01-17 16:25:01 -0800327 new_value = bond_parse_parm(buf, bond_mode_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800328 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800329 pr_err("%s: Ignoring invalid mode value %.*s.\n",
330 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800331 ret = -EINVAL;
332 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800333 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000334 if ((new_value == BOND_MODE_ALB ||
335 new_value == BOND_MODE_TLB) &&
336 bond->params.arp_interval) {
337 pr_err("%s: %s mode is incompatible with arp monitoring.\n",
338 bond->dev->name, bond_mode_tbl[new_value].modename);
339 ret = -EINVAL;
340 goto out;
341 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000342
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200343 /* don't cache arp_validate between modes */
344 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000345 bond->params.mode = new_value;
346 bond_set_mode_ops(bond, bond->params.mode);
347 pr_info("%s: setting mode to %s (%d).\n",
348 bond->dev->name, bond_mode_tbl[new_value].modename,
349 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800350out:
nikolay@redhat.comea6836d2013-05-18 01:18:28 +0000351 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800352 return ret;
353}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000354static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
355 bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800356
357/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000358 * Show and set the bonding transmit hash method.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800359 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700360static ssize_t bonding_show_xmit_hash(struct device *d,
361 struct device_attribute *attr,
362 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800363{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700364 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800365
Wagner Ferenc8e4b9322007-12-06 23:40:32 -0800366 return sprintf(buf, "%s %d\n",
367 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
368 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800369}
370
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700371static ssize_t bonding_store_xmit_hash(struct device *d,
372 struct device_attribute *attr,
373 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800374{
375 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700376 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800377
Jay Vosburghece95f72008-01-17 16:25:01 -0800378 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800379 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800380 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800381 bond->dev->name,
382 (int)strlen(buf) - 1, buf);
383 ret = -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800384 } else {
385 bond->params.xmit_policy = new_value;
386 bond_set_mode_ops(bond, bond->params.mode);
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800387 pr_info("%s: setting xmit hash policy to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000388 bond->dev->name,
389 xmit_hashtype_tbl[new_value].modename, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800390 }
Nikolay Aleksandrov53edee22013-05-24 00:59:47 +0000391
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800392 return ret;
393}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000394static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
395 bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800396
397/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700398 * Show and set arp_validate.
399 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700400static ssize_t bonding_show_arp_validate(struct device *d,
401 struct device_attribute *attr,
402 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700403{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700404 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700405
406 return sprintf(buf, "%s %d\n",
407 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800408 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700409}
410
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700411static ssize_t bonding_store_arp_validate(struct device *d,
412 struct device_attribute *attr,
413 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700414{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700415 struct bonding *bond = to_bond(d);
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200416 int new_value, ret = count;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700417
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200418 if (!rtnl_trylock())
419 return restart_syscall();
Jay Vosburghece95f72008-01-17 16:25:01 -0800420 new_value = bond_parse_parm(buf, arp_validate_tbl);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700421 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800422 pr_err("%s: Ignoring invalid arp_validate value %s\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700423 bond->dev->name, buf);
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200424 ret = -EINVAL;
425 goto out;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700426 }
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200427 if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800428 pr_err("%s: arp_validate only supported in active-backup mode.\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700429 bond->dev->name);
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200430 ret = -EINVAL;
431 goto out;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700432 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800433 pr_info("%s: setting arp_validate to %s (%d).\n",
434 bond->dev->name, arp_validate_tbl[new_value].modename,
435 new_value);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700436
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200437 if (bond->dev->flags & IFF_UP) {
438 if (!new_value)
439 bond->recv_probe = NULL;
440 else if (bond->params.arp_interval)
441 bond->recv_probe = bond_arp_rcv;
442 }
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700443 bond->params.arp_validate = new_value;
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200444out:
445 rtnl_unlock();
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700446
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200447 return ret;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700448}
449
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000450static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
451 bonding_store_arp_validate);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200452/*
453 * Show and set arp_all_targets.
454 */
455static ssize_t bonding_show_arp_all_targets(struct device *d,
456 struct device_attribute *attr,
457 char *buf)
458{
459 struct bonding *bond = to_bond(d);
460 int value = bond->params.arp_all_targets;
461
462 return sprintf(buf, "%s %d\n", arp_all_targets_tbl[value].modename,
463 value);
464}
465
466static ssize_t bonding_store_arp_all_targets(struct device *d,
467 struct device_attribute *attr,
468 const char *buf, size_t count)
469{
470 struct bonding *bond = to_bond(d);
471 int new_value;
472
473 new_value = bond_parse_parm(buf, arp_all_targets_tbl);
474 if (new_value < 0) {
475 pr_err("%s: Ignoring invalid arp_all_targets value %s\n",
476 bond->dev->name, buf);
477 return -EINVAL;
478 }
479 pr_info("%s: setting arp_all_targets to %s (%d).\n",
480 bond->dev->name, arp_all_targets_tbl[new_value].modename,
481 new_value);
482
483 bond->params.arp_all_targets = new_value;
484
485 return count;
486}
487
488static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
489 bonding_show_arp_all_targets, bonding_store_arp_all_targets);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700490
491/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700492 * Show and store fail_over_mac. User only allowed to change the
493 * value when there are no slaves.
494 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000495static ssize_t bonding_show_fail_over_mac(struct device *d,
496 struct device_attribute *attr,
497 char *buf)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700498{
499 struct bonding *bond = to_bond(d);
500
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700501 return sprintf(buf, "%s %d\n",
502 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
503 bond->params.fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700504}
505
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000506static ssize_t bonding_store_fail_over_mac(struct device *d,
507 struct device_attribute *attr,
508 const char *buf, size_t count)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700509{
dingtianhong9402b742013-07-23 15:25:39 +0800510 int new_value, ret = count;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700511 struct bonding *bond = to_bond(d);
512
dingtianhong9402b742013-07-23 15:25:39 +0800513 if (!rtnl_trylock())
514 return restart_syscall();
515
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200516 if (!list_empty(&bond->slave_list)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800517 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
Jay Vosburghdd957c52007-10-09 19:57:24 -0700518 bond->dev->name);
dingtianhong9402b742013-07-23 15:25:39 +0800519 ret = -EPERM;
520 goto out;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700521 }
522
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700523 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
524 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800525 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700526 bond->dev->name, buf);
dingtianhong9402b742013-07-23 15:25:39 +0800527 ret = -EINVAL;
528 goto out;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700529 }
530
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700531 bond->params.fail_over_mac = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800532 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
533 bond->dev->name, fail_over_mac_tbl[new_value].modename,
534 new_value);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700535
dingtianhong9402b742013-07-23 15:25:39 +0800536out:
537 rtnl_unlock();
538 return ret;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700539}
540
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000541static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
542 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700543
544/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800545 * Show and set the arp timer interval. There are two tricky bits
546 * here. First, if ARP monitoring is activated, then we must disable
547 * MII monitoring. Second, if the ARP timer isn't running, we must
548 * start it.
549 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700550static ssize_t bonding_show_arp_interval(struct device *d,
551 struct device_attribute *attr,
552 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800553{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700554 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800555
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800556 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800557}
558
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700559static ssize_t bonding_store_arp_interval(struct device *d,
560 struct device_attribute *attr,
561 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800562{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700563 struct bonding *bond = to_bond(d);
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200564 int new_value, ret = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800565
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000566 if (!rtnl_trylock())
567 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800568 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800569 pr_err("%s: no arp_interval value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800570 bond->dev->name);
571 ret = -EINVAL;
572 goto out;
573 }
574 if (new_value < 0) {
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000575 pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800576 bond->dev->name, new_value, INT_MAX);
577 ret = -EINVAL;
578 goto out;
579 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000580 if (bond->params.mode == BOND_MODE_ALB ||
581 bond->params.mode == BOND_MODE_TLB) {
582 pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
583 bond->dev->name, bond->dev->name);
584 ret = -EINVAL;
585 goto out;
586 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800587 pr_info("%s: Setting ARP monitoring interval to %d.\n",
588 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800589 bond->params.arp_interval = new_value;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000590 if (new_value) {
591 if (bond->params.miimon) {
592 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
593 bond->dev->name, bond->dev->name);
594 bond->params.miimon = 0;
595 }
596 if (!bond->params.arp_targets[0])
597 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
598 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800599 }
600 if (bond->dev->flags & IFF_UP) {
601 /* If the interface is up, we may need to fire off
602 * the ARP timer. If the interface is down, the
603 * timer will get fired off when the open function
604 * is called.
605 */
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000606 if (!new_value) {
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200607 if (bond->params.arp_validate)
608 bond->recv_probe = NULL;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000609 cancel_delayed_work_sync(&bond->arp_work);
610 } else {
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200611 /* arp_validate can be set only in active-backup mode */
612 if (bond->params.arp_validate)
613 bond->recv_probe = bond_arp_rcv;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000614 cancel_delayed_work_sync(&bond->mii_work);
615 queue_delayed_work(bond->wq, &bond->arp_work, 0);
616 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800617 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800618out:
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000619 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800620 return ret;
621}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000622static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
623 bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800624
625/*
626 * Show and set the arp targets.
627 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700628static ssize_t bonding_show_arp_targets(struct device *d,
629 struct device_attribute *attr,
630 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800631{
632 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700633 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800634
635 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
636 if (bond->params.arp_targets[i])
Harvey Harrison63779432008-10-31 00:56:00 -0700637 res += sprintf(buf + res, "%pI4 ",
638 &bond->params.arp_targets[i]);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800639 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800640 if (res)
641 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800642 return res;
643}
644
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700645static ssize_t bonding_store_arp_targets(struct device *d,
646 struct device_attribute *attr,
647 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800648{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700649 struct bonding *bond = to_bond(d);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200650 struct slave *slave;
651 __be32 newtarget, *targets;
652 unsigned long *targets_rx;
653 int ind, i, j, ret = -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800654
655 targets = bond->params.arp_targets;
656 newtarget = in_aton(buf + 1);
657 /* look for adds */
658 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400659 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800660 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700661 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800662 goto out;
663 }
664
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200665 if (bond_get_targets_ip(targets, newtarget) != -1) { /* dup */
666 pr_err("%s: ARP target %pI4 is already present\n",
667 bond->dev->name, &newtarget);
668 goto out;
669 }
670
Veaceslav Falico8599b522013-06-24 11:49:34 +0200671 ind = bond_get_targets_ip(targets, 0); /* first free slot */
672 if (ind == -1) {
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200673 pr_err("%s: ARP target table is full!\n",
674 bond->dev->name);
675 goto out;
676 }
677
678 pr_info("%s: adding ARP target %pI4.\n", bond->dev->name,
679 &newtarget);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200680 /* not to race with bond_arp_rcv */
681 write_lock_bh(&bond->lock);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200682 bond_for_each_slave(bond, slave)
Veaceslav Falico8599b522013-06-24 11:49:34 +0200683 slave->target_last_arp_rx[ind] = jiffies;
684 targets[ind] = newtarget;
685 write_unlock_bh(&bond->lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000686 } else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400687 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800688 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700689 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800690 goto out;
691 }
692
Veaceslav Falico8599b522013-06-24 11:49:34 +0200693 ind = bond_get_targets_ip(targets, newtarget);
694 if (ind == -1) {
695 pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800696 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800697 goto out;
698 }
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200699
Veaceslav Falico8599b522013-06-24 11:49:34 +0200700 if (ind == 0 && !targets[1] && bond->params.arp_interval)
701 pr_warn("%s: removing last arp target with arp_interval on\n",
702 bond->dev->name);
703
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200704 pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
705 &newtarget);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200706
707 write_lock_bh(&bond->lock);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200708 bond_for_each_slave(bond, slave) {
Veaceslav Falico8599b522013-06-24 11:49:34 +0200709 targets_rx = slave->target_last_arp_rx;
710 j = ind;
711 for (; (j < BOND_MAX_ARP_TARGETS-1) && targets[j+1]; j++)
712 targets_rx[j] = targets_rx[j+1];
713 targets_rx[j] = 0;
714 }
715 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200716 targets[i] = targets[i+1];
717 targets[i] = 0;
Veaceslav Falico8599b522013-06-24 11:49:34 +0200718 write_unlock_bh(&bond->lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000719 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800720 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
721 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800722 ret = -EPERM;
723 goto out;
724 }
725
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200726 ret = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800727out:
728 return ret;
729}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700730static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800731
732/*
733 * Show and set the up and down delays. These must be multiples of the
734 * MII monitoring value, and are stored internally as the multiplier.
735 * Thus, we must translate to MS for the real world.
736 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700737static ssize_t bonding_show_downdelay(struct device *d,
738 struct device_attribute *attr,
739 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800740{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700741 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800742
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800743 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800744}
745
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700746static ssize_t bonding_store_downdelay(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
753 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800754 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800755 bond->dev->name);
756 ret = -EPERM;
757 goto out;
758 }
759
760 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800761 pr_err("%s: no down delay value specified.\n", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800762 ret = -EINVAL;
763 goto out;
764 }
765 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800766 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000767 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800768 ret = -EINVAL;
769 goto out;
770 } else {
771 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800772 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 +0000773 bond->dev->name, new_value,
774 bond->params.miimon,
775 (new_value / bond->params.miimon) *
776 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800777 }
778 bond->params.downdelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800779 pr_info("%s: Setting down delay to %d.\n",
780 bond->dev->name,
781 bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800782
783 }
784
785out:
786 return ret;
787}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000788static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
789 bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800790
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700791static ssize_t bonding_show_updelay(struct device *d,
792 struct device_attribute *attr,
793 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800794{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700795 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800796
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800797 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800798
799}
800
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700801static ssize_t bonding_store_updelay(struct device *d,
802 struct device_attribute *attr,
803 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800804{
805 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700806 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800807
808 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800809 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800810 bond->dev->name);
811 ret = -EPERM;
812 goto out;
813 }
814
815 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800816 pr_err("%s: no up delay value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800817 bond->dev->name);
818 ret = -EINVAL;
819 goto out;
820 }
821 if (new_value < 0) {
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000822 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
823 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800824 ret = -EINVAL;
825 goto out;
826 } else {
827 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800828 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 +0000829 bond->dev->name, new_value,
830 bond->params.miimon,
831 (new_value / bond->params.miimon) *
832 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800833 }
834 bond->params.updelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800835 pr_info("%s: Setting up delay to %d.\n",
836 bond->dev->name,
837 bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800838 }
839
840out:
841 return ret;
842}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000843static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
844 bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800845
846/*
847 * Show and set the LACP interval. Interface must be down, and the mode
848 * must be set to 802.3ad mode.
849 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700850static ssize_t bonding_show_lacp(struct device *d,
851 struct device_attribute *attr,
852 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800853{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700854 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800855
856 return sprintf(buf, "%s %d\n",
857 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800858 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800859}
860
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700861static ssize_t bonding_store_lacp(struct device *d,
862 struct device_attribute *attr,
863 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800864{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700865 struct bonding *bond = to_bond(d);
nikolay@redhat.comc5093162013-09-02 13:51:40 +0200866 int new_value, ret = count;
867
868 if (!rtnl_trylock())
869 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800870
871 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800872 pr_err("%s: Unable to update LACP rate because interface is up.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800873 bond->dev->name);
874 ret = -EPERM;
875 goto out;
876 }
877
878 if (bond->params.mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800879 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800880 bond->dev->name);
881 ret = -EPERM;
882 goto out;
883 }
884
Jay Vosburghece95f72008-01-17 16:25:01 -0800885 new_value = bond_parse_parm(buf, bond_lacp_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800886
887 if ((new_value == 1) || (new_value == 0)) {
888 bond->params.lacp_fast = new_value;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +0000889 bond_3ad_update_lacp_rate(bond);
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800890 pr_info("%s: Setting LACP rate to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000891 bond->dev->name, bond_lacp_tbl[new_value].modename,
892 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800893 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800894 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000895 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800896 ret = -EINVAL;
897 }
898out:
nikolay@redhat.comc5093162013-09-02 13:51:40 +0200899 rtnl_unlock();
900
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800901 return ret;
902}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000903static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
904 bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800905
stephen hemminger655f8912011-06-22 09:54:39 +0000906static ssize_t bonding_show_min_links(struct device *d,
907 struct device_attribute *attr,
908 char *buf)
909{
910 struct bonding *bond = to_bond(d);
911
912 return sprintf(buf, "%d\n", bond->params.min_links);
913}
914
915static ssize_t bonding_store_min_links(struct device *d,
916 struct device_attribute *attr,
917 const char *buf, size_t count)
918{
919 struct bonding *bond = to_bond(d);
920 int ret;
921 unsigned int new_value;
922
923 ret = kstrtouint(buf, 0, &new_value);
924 if (ret < 0) {
925 pr_err("%s: Ignoring invalid min links value %s.\n",
926 bond->dev->name, buf);
927 return ret;
928 }
929
930 pr_info("%s: Setting min links value to %u\n",
931 bond->dev->name, new_value);
932 bond->params.min_links = new_value;
933 return count;
934}
935static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
936 bonding_show_min_links, bonding_store_min_links);
937
Jay Vosburghfd989c82008-11-04 17:51:16 -0800938static ssize_t bonding_show_ad_select(struct device *d,
939 struct device_attribute *attr,
940 char *buf)
941{
942 struct bonding *bond = to_bond(d);
943
944 return sprintf(buf, "%s %d\n",
945 ad_select_tbl[bond->params.ad_select].modename,
946 bond->params.ad_select);
947}
948
949
950static ssize_t bonding_store_ad_select(struct device *d,
951 struct device_attribute *attr,
952 const char *buf, size_t count)
953{
954 int new_value, ret = count;
955 struct bonding *bond = to_bond(d);
956
957 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800958 pr_err("%s: Unable to update ad_select because interface is up.\n",
959 bond->dev->name);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800960 ret = -EPERM;
961 goto out;
962 }
963
964 new_value = bond_parse_parm(buf, ad_select_tbl);
965
966 if (new_value != -1) {
967 bond->params.ad_select = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800968 pr_info("%s: Setting ad_select to %s (%d).\n",
969 bond->dev->name, ad_select_tbl[new_value].modename,
970 new_value);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800971 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800972 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -0800973 bond->dev->name, (int)strlen(buf) - 1, buf);
974 ret = -EINVAL;
975 }
976out:
977 return ret;
978}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000979static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
980 bonding_show_ad_select, bonding_store_ad_select);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800981
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800982/*
Ben Hutchingsad246c92011-04-26 15:25:52 +0000983 * Show and set the number of peer notifications to send after a failover event.
984 */
985static ssize_t bonding_show_num_peer_notif(struct device *d,
986 struct device_attribute *attr,
987 char *buf)
988{
989 struct bonding *bond = to_bond(d);
990 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
991}
992
993static ssize_t bonding_store_num_peer_notif(struct device *d,
994 struct device_attribute *attr,
995 const char *buf, size_t count)
996{
997 struct bonding *bond = to_bond(d);
998 int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
999 return err ? err : count;
1000}
1001static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
1002 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
1003static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
1004 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
1005
1006/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001007 * Show and set the MII monitor interval. There are two tricky bits
1008 * here. First, if MII monitoring is activated, then we must disable
1009 * ARP monitoring. Second, if the timer isn't running, we must
1010 * start it.
1011 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001012static ssize_t bonding_show_miimon(struct device *d,
1013 struct device_attribute *attr,
1014 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001015{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001016 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001017
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001018 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001019}
1020
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001021static ssize_t bonding_store_miimon(struct device *d,
1022 struct device_attribute *attr,
1023 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001024{
1025 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001026 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001027
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00001028 if (!rtnl_trylock())
1029 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001030 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001031 pr_err("%s: no miimon value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001032 bond->dev->name);
1033 ret = -EINVAL;
1034 goto out;
1035 }
1036 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001037 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +00001038 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001039 ret = -EINVAL;
1040 goto out;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +00001041 }
1042 pr_info("%s: Setting MII monitoring interval to %d.\n",
1043 bond->dev->name, new_value);
1044 bond->params.miimon = new_value;
1045 if (bond->params.updelay)
1046 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
1047 bond->dev->name,
1048 bond->params.updelay * bond->params.miimon);
1049 if (bond->params.downdelay)
1050 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
1051 bond->dev->name,
1052 bond->params.downdelay * bond->params.miimon);
1053 if (new_value && bond->params.arp_interval) {
1054 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
1055 bond->dev->name);
1056 bond->params.arp_interval = 0;
1057 if (bond->params.arp_validate)
1058 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
1059 }
1060 if (bond->dev->flags & IFF_UP) {
1061 /* If the interface is up, we may need to fire off
1062 * the MII timer. If the interface is down, the
1063 * timer will get fired off when the open function
1064 * is called.
1065 */
1066 if (!new_value) {
1067 cancel_delayed_work_sync(&bond->mii_work);
1068 } else {
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00001069 cancel_delayed_work_sync(&bond->arp_work);
1070 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001071 }
1072 }
1073out:
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00001074 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001075 return ret;
1076}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001077static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1078 bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001079
1080/*
1081 * Show and set the primary slave. The store function is much
1082 * simpler than bonding_store_slaves function because it only needs to
1083 * handle one interface name.
1084 * The bond must be a mode that supports a primary for this be
1085 * set.
1086 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001087static ssize_t bonding_show_primary(struct device *d,
1088 struct device_attribute *attr,
1089 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001090{
1091 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001092 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001093
1094 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001095 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001096
1097 return count;
1098}
1099
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001100static ssize_t bonding_store_primary(struct device *d,
1101 struct device_attribute *attr,
1102 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001103{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001104 struct bonding *bond = to_bond(d);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001105 char ifname[IFNAMSIZ];
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001106 struct slave *slave;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001107
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001108 if (!rtnl_trylock())
1109 return restart_syscall();
Neil Hormane843fa52010-10-13 16:01:50 +00001110 block_netpoll_tx();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001111 read_lock(&bond->lock);
1112 write_lock_bh(&bond->curr_slave_lock);
1113
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001114 if (!USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001115 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1116 bond->dev->name, bond->dev->name, bond->params.mode);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001117 goto out;
1118 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001119
nikolay@redhat.comeb6e98a2012-10-31 04:42:51 +00001120 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001121
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001122 /* check to see if we are clearing primary */
1123 if (!strlen(ifname) || buf[0] == '\n') {
1124 pr_info("%s: Setting primary slave to None.\n",
1125 bond->dev->name);
1126 bond->primary_slave = NULL;
Milos Vyleteleb492f72013-01-29 09:59:00 +00001127 memset(bond->params.primary, 0, sizeof(bond->params.primary));
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001128 bond_select_active_slave(bond);
1129 goto out;
1130 }
1131
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001132 bond_for_each_slave(bond, slave) {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001133 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1134 pr_info("%s: Setting %s as primary slave.\n",
1135 bond->dev->name, slave->dev->name);
1136 bond->primary_slave = slave;
1137 strcpy(bond->params.primary, slave->dev->name);
1138 bond_select_active_slave(bond);
1139 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001140 }
1141 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001142
Weiping Pan8a936642012-06-10 23:00:20 +00001143 strncpy(bond->params.primary, ifname, IFNAMSIZ);
1144 bond->params.primary[IFNAMSIZ - 1] = 0;
1145
1146 pr_info("%s: Recording %s as primary, "
1147 "but it has not been enslaved to %s yet.\n",
1148 bond->dev->name, ifname, bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001149out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001150 write_unlock_bh(&bond->curr_slave_lock);
1151 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001152 unblock_netpoll_tx();
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001153 rtnl_unlock();
1154
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001155 return count;
1156}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001157static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1158 bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001159
1160/*
Jiri Pirkoa5499522009-09-25 03:28:09 +00001161 * Show and set the primary_reselect flag.
1162 */
1163static ssize_t bonding_show_primary_reselect(struct device *d,
1164 struct device_attribute *attr,
1165 char *buf)
1166{
1167 struct bonding *bond = to_bond(d);
1168
1169 return sprintf(buf, "%s %d\n",
1170 pri_reselect_tbl[bond->params.primary_reselect].modename,
1171 bond->params.primary_reselect);
1172}
1173
1174static ssize_t bonding_store_primary_reselect(struct device *d,
1175 struct device_attribute *attr,
1176 const char *buf, size_t count)
1177{
1178 int new_value, ret = count;
1179 struct bonding *bond = to_bond(d);
1180
1181 if (!rtnl_trylock())
1182 return restart_syscall();
1183
1184 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1185 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001186 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001187 bond->dev->name,
1188 (int) strlen(buf) - 1, buf);
1189 ret = -EINVAL;
1190 goto out;
1191 }
1192
1193 bond->params.primary_reselect = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001194 pr_info("%s: setting primary_reselect to %s (%d).\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001195 bond->dev->name, pri_reselect_tbl[new_value].modename,
1196 new_value);
1197
Neil Hormane843fa52010-10-13 16:01:50 +00001198 block_netpoll_tx();
Jiri Pirkoa5499522009-09-25 03:28:09 +00001199 read_lock(&bond->lock);
1200 write_lock_bh(&bond->curr_slave_lock);
1201 bond_select_active_slave(bond);
1202 write_unlock_bh(&bond->curr_slave_lock);
1203 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001204 unblock_netpoll_tx();
Jiri Pirkoa5499522009-09-25 03:28:09 +00001205out:
1206 rtnl_unlock();
1207 return ret;
1208}
1209static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1210 bonding_show_primary_reselect,
1211 bonding_store_primary_reselect);
1212
1213/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001214 * Show and set the use_carrier flag.
1215 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001216static ssize_t bonding_show_carrier(struct device *d,
1217 struct device_attribute *attr,
1218 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001219{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001220 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001221
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001222 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001223}
1224
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001225static ssize_t bonding_store_carrier(struct device *d,
1226 struct device_attribute *attr,
1227 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001228{
1229 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001230 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001231
1232
1233 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001234 pr_err("%s: no use_carrier value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001235 bond->dev->name);
1236 ret = -EINVAL;
1237 goto out;
1238 }
1239 if ((new_value == 0) || (new_value == 1)) {
1240 bond->params.use_carrier = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001241 pr_info("%s: Setting use_carrier to %d.\n",
1242 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001243 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001244 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1245 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001246 }
1247out:
Jiri Pirko672bda32011-01-25 11:03:25 +00001248 return ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001249}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001250static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1251 bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001252
1253
1254/*
1255 * Show and set currently active_slave.
1256 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001257static ssize_t bonding_show_active_slave(struct device *d,
1258 struct device_attribute *attr,
1259 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001260{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001261 struct bonding *bond = to_bond(d);
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001262 struct slave *curr;
Wagner Ferenc16cd0162007-12-06 23:40:29 -08001263 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001264
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001265 rcu_read_lock();
1266 curr = rcu_dereference(bond->curr_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001267 if (USES_PRIMARY(bond->params.mode) && curr)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001268 count = sprintf(buf, "%s\n", curr->dev->name);
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001269 rcu_read_unlock();
1270
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001271 return count;
1272}
1273
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001274static ssize_t bonding_store_active_slave(struct device *d,
1275 struct device_attribute *attr,
1276 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001277{
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001278 struct slave *slave, *old_active, *new_active;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001279 struct bonding *bond = to_bond(d);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001280 char ifname[IFNAMSIZ];
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001281
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001282 if (!rtnl_trylock())
1283 return restart_syscall();
Neil Hormane843fa52010-10-13 16:01:50 +00001284
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001285 old_active = new_active = NULL;
Neil Hormane843fa52010-10-13 16:01:50 +00001286 block_netpoll_tx();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001287 read_lock(&bond->lock);
1288 write_lock_bh(&bond->curr_slave_lock);
Jay Vosburgh1466a212007-11-06 13:33:28 -08001289
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001290 if (!USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001291 pr_info("%s: Unable to change active slave; %s is in mode %d\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001292 bond->dev->name, bond->dev->name, bond->params.mode);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001293 goto out;
1294 }
1295
nikolay@redhat.comc84e1592012-10-31 06:03:52 +00001296 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001297
1298 /* check to see if we are clearing active */
1299 if (!strlen(ifname) || buf[0] == '\n') {
1300 pr_info("%s: Clearing current active slave.\n",
1301 bond->dev->name);
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001302 rcu_assign_pointer(bond->curr_active_slave, NULL);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001303 bond_select_active_slave(bond);
1304 goto out;
1305 }
1306
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001307 bond_for_each_slave(bond, slave) {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001308 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1309 old_active = bond->curr_active_slave;
1310 new_active = slave;
1311 if (new_active == old_active) {
1312 /* do nothing */
1313 pr_info("%s: %s is already the current"
1314 " active slave.\n",
1315 bond->dev->name,
1316 slave->dev->name);
1317 goto out;
dingtianhong38c49162013-07-23 15:25:32 +08001318 } else {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001319 if ((new_active) &&
1320 (old_active) &&
1321 (new_active->link == BOND_LINK_UP) &&
1322 IS_UP(new_active->dev)) {
1323 pr_info("%s: Setting %s as active"
1324 " slave.\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001325 bond->dev->name,
1326 slave->dev->name);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001327 bond_change_active_slave(bond,
1328 new_active);
dingtianhong38c49162013-07-23 15:25:32 +08001329 } else {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001330 pr_info("%s: Could not set %s as"
1331 " active slave; either %s is"
1332 " down or the link is down.\n",
1333 bond->dev->name,
1334 slave->dev->name,
1335 slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001336 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001337 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001338 }
1339 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001340 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001341
1342 pr_info("%s: Unable to set %.*s as active slave.\n",
1343 bond->dev->name, (int)strlen(buf) - 1, buf);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001344 out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001345 write_unlock_bh(&bond->curr_slave_lock);
1346 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001347 unblock_netpoll_tx();
1348
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001349 rtnl_unlock();
1350
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001351 return count;
1352
1353}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001354static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1355 bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001356
1357
1358/*
1359 * Show link status of the bond interface.
1360 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001361static ssize_t bonding_show_mii_status(struct device *d,
1362 struct device_attribute *attr,
1363 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001364{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001365 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001366
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001367 return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001368}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001369static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001370
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001371/*
1372 * Show current 802.3ad aggregator ID.
1373 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001374static ssize_t bonding_show_ad_aggregator(struct device *d,
1375 struct device_attribute *attr,
1376 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001377{
1378 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001379 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001380
1381 if (bond->params.mode == BOND_MODE_8023AD) {
1382 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001383 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001384 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001385 ? 0 : ad_info.aggregator_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001386 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001387
1388 return count;
1389}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001390static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001391
1392
1393/*
1394 * Show number of active 802.3ad ports.
1395 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001396static ssize_t bonding_show_ad_num_ports(struct device *d,
1397 struct device_attribute *attr,
1398 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001399{
1400 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001401 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001402
1403 if (bond->params.mode == BOND_MODE_8023AD) {
1404 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001405 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001406 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001407 ? 0 : ad_info.ports);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001408 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001409
1410 return count;
1411}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001412static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001413
1414
1415/*
1416 * Show current 802.3ad actor key.
1417 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001418static ssize_t bonding_show_ad_actor_key(struct device *d,
1419 struct device_attribute *attr,
1420 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001421{
1422 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001423 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001424
1425 if (bond->params.mode == BOND_MODE_8023AD) {
1426 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001427 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001428 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001429 ? 0 : ad_info.actor_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001430 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001431
1432 return count;
1433}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001434static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001435
1436
1437/*
1438 * Show current 802.3ad partner key.
1439 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001440static ssize_t bonding_show_ad_partner_key(struct device *d,
1441 struct device_attribute *attr,
1442 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001443{
1444 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001445 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001446
1447 if (bond->params.mode == BOND_MODE_8023AD) {
1448 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001449 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001450 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001451 ? 0 : ad_info.partner_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001452 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001453
1454 return count;
1455}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001456static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001457
1458
1459/*
1460 * Show current 802.3ad partner mac.
1461 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001462static ssize_t bonding_show_ad_partner_mac(struct device *d,
1463 struct device_attribute *attr,
1464 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001465{
1466 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001467 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001468
1469 if (bond->params.mode == BOND_MODE_8023AD) {
1470 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001471 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
Johannes Berge1749612008-10-27 15:59:26 -07001472 count = sprintf(buf, "%pM\n", ad_info.partner_system);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001473 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001474
1475 return count;
1476}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001477static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001478
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001479/*
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001480 * Show the queue_ids of the slaves in the current bond.
1481 */
1482static ssize_t bonding_show_queue_id(struct device *d,
1483 struct device_attribute *attr,
1484 char *buf)
1485{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001486 struct bonding *bond = to_bond(d);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001487 struct slave *slave;
1488 int res = 0;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001489
1490 if (!rtnl_trylock())
1491 return restart_syscall();
1492
1493 read_lock(&bond->lock);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001494 bond_for_each_slave(bond, slave) {
Nicolas de Pesloüan79236682010-07-14 18:24:54 -07001495 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1496 /* not enough space for another interface_name:queue_id pair */
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001497 if ((PAGE_SIZE - res) > 10)
1498 res = PAGE_SIZE - 10;
1499 res += sprintf(buf + res, "++more++ ");
1500 break;
1501 }
1502 res += sprintf(buf + res, "%s:%d ",
1503 slave->dev->name, slave->queue_id);
1504 }
1505 read_unlock(&bond->lock);
1506 if (res)
1507 buf[res-1] = '\n'; /* eat the leftover space */
1508 rtnl_unlock();
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001509
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001510 return res;
1511}
1512
1513/*
1514 * Set the queue_ids of the slaves in the current bond. The bond
1515 * interface must be enslaved for this to work.
1516 */
1517static ssize_t bonding_store_queue_id(struct device *d,
1518 struct device_attribute *attr,
1519 const char *buffer, size_t count)
1520{
1521 struct slave *slave, *update_slave;
1522 struct bonding *bond = to_bond(d);
1523 u16 qid;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001524 int ret = count;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001525 char *delim;
1526 struct net_device *sdev = NULL;
1527
1528 if (!rtnl_trylock())
1529 return restart_syscall();
1530
1531 /* delim will point to queue id if successful */
1532 delim = strchr(buffer, ':');
1533 if (!delim)
1534 goto err_no_cmd;
1535
1536 /*
1537 * Terminate string that points to device name and bump it
1538 * up one, so we can read the queue id there.
1539 */
1540 *delim = '\0';
1541 if (sscanf(++delim, "%hd\n", &qid) != 1)
1542 goto err_no_cmd;
1543
1544 /* Check buffer length, valid ifname and queue id */
1545 if (strlen(buffer) > IFNAMSIZ ||
1546 !dev_valid_name(buffer) ||
Jiri Pirko8a540ff2012-07-20 02:28:50 +00001547 qid > bond->dev->real_num_tx_queues)
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001548 goto err_no_cmd;
1549
1550 /* Get the pointer to that interface if it exists */
1551 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1552 if (!sdev)
1553 goto err_no_cmd;
1554
1555 read_lock(&bond->lock);
1556
1557 /* Search for thes slave and check for duplicate qids */
1558 update_slave = NULL;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001559 bond_for_each_slave(bond, slave) {
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001560 if (sdev == slave->dev)
1561 /*
1562 * We don't need to check the matching
1563 * slave for dups, since we're overwriting it
1564 */
1565 update_slave = slave;
1566 else if (qid && qid == slave->queue_id) {
1567 goto err_no_cmd_unlock;
1568 }
1569 }
1570
1571 if (!update_slave)
1572 goto err_no_cmd_unlock;
1573
1574 /* Actually set the qids for the slave */
1575 update_slave->queue_id = qid;
1576
1577 read_unlock(&bond->lock);
1578out:
1579 rtnl_unlock();
1580 return ret;
1581
1582err_no_cmd_unlock:
1583 read_unlock(&bond->lock);
1584err_no_cmd:
1585 pr_info("invalid input for queue_id set for %s.\n",
1586 bond->dev->name);
1587 ret = -EPERM;
1588 goto out;
1589}
1590
1591static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1592 bonding_store_queue_id);
1593
1594
1595/*
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001596 * Show and set the all_slaves_active flag.
1597 */
1598static ssize_t bonding_show_slaves_active(struct device *d,
1599 struct device_attribute *attr,
1600 char *buf)
1601{
1602 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001603
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001604 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1605}
1606
1607static ssize_t bonding_store_slaves_active(struct device *d,
1608 struct device_attribute *attr,
1609 const char *buf, size_t count)
1610{
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001611 struct bonding *bond = to_bond(d);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001612 int new_value, ret = count;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001613 struct slave *slave;
1614
1615 if (sscanf(buf, "%d", &new_value) != 1) {
1616 pr_err("%s: no all_slaves_active value specified.\n",
1617 bond->dev->name);
1618 ret = -EINVAL;
1619 goto out;
1620 }
1621
1622 if (new_value == bond->params.all_slaves_active)
1623 goto out;
1624
1625 if ((new_value == 0) || (new_value == 1)) {
1626 bond->params.all_slaves_active = new_value;
1627 } else {
1628 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1629 bond->dev->name, new_value);
1630 ret = -EINVAL;
1631 goto out;
1632 }
1633
nikolay@redhat.come196c0e2012-11-29 01:37:59 +00001634 read_lock(&bond->lock);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001635 bond_for_each_slave(bond, slave) {
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001636 if (!bond_is_active_slave(slave)) {
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001637 if (new_value)
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001638 slave->inactive = 0;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001639 else
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001640 slave->inactive = 1;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001641 }
1642 }
nikolay@redhat.come196c0e2012-11-29 01:37:59 +00001643 read_unlock(&bond->lock);
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001644out:
Jiri Pirko672bda32011-01-25 11:03:25 +00001645 return ret;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001646}
1647static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1648 bonding_show_slaves_active, bonding_store_slaves_active);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001649
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001650/*
1651 * Show and set the number of IGMP membership reports to send on link failure
1652 */
1653static ssize_t bonding_show_resend_igmp(struct device *d,
Flavio Leitner94265cf2011-05-25 08:38:58 +00001654 struct device_attribute *attr,
1655 char *buf)
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001656{
1657 struct bonding *bond = to_bond(d);
1658
1659 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1660}
1661
1662static ssize_t bonding_store_resend_igmp(struct device *d,
Flavio Leitner94265cf2011-05-25 08:38:58 +00001663 struct device_attribute *attr,
1664 const char *buf, size_t count)
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001665{
1666 int new_value, ret = count;
1667 struct bonding *bond = to_bond(d);
1668
1669 if (sscanf(buf, "%d", &new_value) != 1) {
1670 pr_err("%s: no resend_igmp value specified.\n",
1671 bond->dev->name);
1672 ret = -EINVAL;
1673 goto out;
1674 }
1675
Flavio Leitner94265cf2011-05-25 08:38:58 +00001676 if (new_value < 0 || new_value > 255) {
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001677 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1678 bond->dev->name, new_value);
1679 ret = -EINVAL;
1680 goto out;
1681 }
1682
1683 pr_info("%s: Setting resend_igmp to %d.\n",
1684 bond->dev->name, new_value);
1685 bond->params.resend_igmp = new_value;
1686out:
1687 return ret;
1688}
1689
1690static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1691 bonding_show_resend_igmp, bonding_store_resend_igmp);
1692
Neil Horman7eacd032013-09-13 11:05:33 -04001693
1694static ssize_t bonding_show_lp_interval(struct device *d,
1695 struct device_attribute *attr,
1696 char *buf)
1697{
1698 struct bonding *bond = to_bond(d);
1699 return sprintf(buf, "%d\n", bond->params.lp_interval);
1700}
1701
1702static ssize_t bonding_store_lp_interval(struct device *d,
1703 struct device_attribute *attr,
1704 const char *buf, size_t count)
1705{
1706 struct bonding *bond = to_bond(d);
1707 int new_value, ret = count;
1708
1709 if (sscanf(buf, "%d", &new_value) != 1) {
1710 pr_err("%s: no lp interval value specified.\n",
1711 bond->dev->name);
1712 ret = -EINVAL;
1713 goto out;
1714 }
1715
1716 if (new_value <= 0) {
1717 pr_err ("%s: lp_interval must be between 1 and %d\n",
1718 bond->dev->name, INT_MAX);
1719 ret = -EINVAL;
1720 goto out;
1721 }
1722
1723 bond->params.lp_interval = new_value;
1724out:
1725 return ret;
1726}
1727
1728static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1729 bonding_show_lp_interval, bonding_store_lp_interval);
1730
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001731static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001732 &dev_attr_slaves.attr,
1733 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001734 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001735 &dev_attr_arp_validate.attr,
Veaceslav Falico8599b522013-06-24 11:49:34 +02001736 &dev_attr_arp_all_targets.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001737 &dev_attr_arp_interval.attr,
1738 &dev_attr_arp_ip_target.attr,
1739 &dev_attr_downdelay.attr,
1740 &dev_attr_updelay.attr,
1741 &dev_attr_lacp_rate.attr,
Jay Vosburghfd989c82008-11-04 17:51:16 -08001742 &dev_attr_ad_select.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001743 &dev_attr_xmit_hash_policy.attr,
Ben Hutchingsad246c92011-04-26 15:25:52 +00001744 &dev_attr_num_grat_arp.attr,
1745 &dev_attr_num_unsol_na.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001746 &dev_attr_miimon.attr,
1747 &dev_attr_primary.attr,
Jiri Pirkoa5499522009-09-25 03:28:09 +00001748 &dev_attr_primary_reselect.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001749 &dev_attr_use_carrier.attr,
1750 &dev_attr_active_slave.attr,
1751 &dev_attr_mii_status.attr,
1752 &dev_attr_ad_aggregator.attr,
1753 &dev_attr_ad_num_ports.attr,
1754 &dev_attr_ad_actor_key.attr,
1755 &dev_attr_ad_partner_key.attr,
1756 &dev_attr_ad_partner_mac.attr,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001757 &dev_attr_queue_id.attr,
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001758 &dev_attr_all_slaves_active.attr,
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001759 &dev_attr_resend_igmp.attr,
stephen hemminger655f8912011-06-22 09:54:39 +00001760 &dev_attr_min_links.attr,
Neil Horman7eacd032013-09-13 11:05:33 -04001761 &dev_attr_lp_interval.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001762 NULL,
1763};
1764
1765static struct attribute_group bonding_group = {
1766 .name = "bonding",
1767 .attrs = per_bond_attrs,
1768};
1769
1770/*
1771 * Initialize sysfs. This sets up the bonding_masters file in
1772 * /sys/class/net.
1773 */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001774int bond_create_sysfs(struct bond_net *bn)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001775{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001776 int ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001777
Eric W. Biederman4c224002011-10-12 21:56:25 +00001778 bn->class_attr_bonding_masters = class_attr_bonding_masters;
Eric W. Biederman01718e32011-10-21 22:43:07 +00001779 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
Eric W. Biederman4c224002011-10-12 21:56:25 +00001780
Tejun Heo58292cbe2013-09-11 22:29:04 -04001781 ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
1782 bn->net);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001783 /*
1784 * Permit multiple loads of the module by ignoring failures to
1785 * create the bonding_masters sysfs file. Bonding devices
1786 * created by second or subsequent loads of the module will
1787 * not be listed in, or controllable by, bonding_masters, but
1788 * will have the usual "bonding" sysfs directory.
1789 *
1790 * This is done to preserve backwards compatibility for
1791 * initscripts/sysconfig, which load bonding multiple times to
1792 * configure multiple bonding devices.
1793 */
1794 if (ret == -EEXIST) {
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001795 /* Is someone being kinky and naming a device bonding_master? */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001796 if (__dev_get_by_name(bn->net,
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001797 class_attr_bonding_masters.attr.name))
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001798 pr_err("network device named %s already exists in sysfs",
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001799 class_attr_bonding_masters.attr.name);
Stephen Hemminger130aa612009-06-11 05:46:04 -07001800 ret = 0;
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001801 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001802
1803 return ret;
1804
1805}
1806
1807/*
1808 * Remove /sys/class/net/bonding_masters.
1809 */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001810void bond_destroy_sysfs(struct bond_net *bn)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001811{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001812 netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001813}
1814
1815/*
1816 * Initialize sysfs for each bond. This sets up and registers
1817 * the 'bondctl' directory for each individual bond under /sys/class/net.
1818 */
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001819void bond_prepare_sysfs_group(struct bonding *bond)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001820{
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001821 bond->dev->sysfs_groups[0] = &bonding_group;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001822}
1823