blob: 5a20804fdece8d1c281e310e49509973185a33ed [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/sysdev.h>
30#include <linux/fs.h>
31#include <linux/types.h>
32#include <linux/string.h>
33#include <linux/netdevice.h>
34#include <linux/inetdevice.h>
35#include <linux/in.h>
36#include <linux/sysfs.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080037#include <linux/ctype.h>
38#include <linux/inet.h>
39#include <linux/rtnetlink.h>
Stephen Hemminger5c5129b2009-06-12 19:02:51 +000040#include <linux/etherdevice.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070041#include <net/net_namespace.h>
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000042#include <net/netns/generic.h>
43#include <linux/nsproxy.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080044
Mitch Williamsb76cdba2005-11-09 10:36:41 -080045#include "bonding.h"
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -080046
Stephen Hemminger3d632c32009-06-12 19:02:48 +000047#define to_dev(obj) container_of(obj, struct device, kobj)
Wang Chen454d7c92008-11-12 23:37:49 -080048#define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
Mitch Williamsb76cdba2005-11-09 10:36:41 -080049
Mitch Williamsb76cdba2005-11-09 10:36:41 -080050/*
51 * "show" function for the bond_masters attribute.
52 * The class parameter is ignored.
53 */
Andi Kleen28812fe2010-01-05 12:48:07 +010054static ssize_t bonding_show_bonds(struct class *cls,
55 struct class_attribute *attr,
56 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080057{
Eric W. Biederman4c224002011-10-12 21:56:25 +000058 struct bond_net *bn =
59 container_of(attr, struct bond_net, class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080060 int res = 0;
61 struct bonding *bond;
62
Stephen Hemminger7e083842009-06-12 19:02:46 +000063 rtnl_lock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080064
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000065 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -080066 if (res > (PAGE_SIZE - IFNAMSIZ)) {
67 /* not enough space for another interface name */
68 if ((PAGE_SIZE - res) > 10)
69 res = PAGE_SIZE - 10;
Wagner Ferencb8843662007-12-06 23:40:30 -080070 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -080071 break;
72 }
Wagner Ferencb8843662007-12-06 23:40:30 -080073 res += sprintf(buf + res, "%s ", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080074 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -080075 if (res)
76 buf[res-1] = '\n'; /* eat the leftover space */
Stephen Hemminger7e083842009-06-12 19:02:46 +000077
78 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080079 return res;
80}
81
Eric W. Biederman4c224002011-10-12 21:56:25 +000082static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
Stephen Hemminger373500d2009-06-12 19:02:50 +000083{
84 struct bonding *bond;
85
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000086 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Stephen Hemminger373500d2009-06-12 19:02:50 +000087 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
88 return bond->dev;
89 }
90 return NULL;
91}
92
Mitch Williamsb76cdba2005-11-09 10:36:41 -080093/*
94 * "store" function for the bond_masters attribute. This is what
95 * creates and deletes entire bonds.
96 *
97 * The class parameter is ignored.
98 *
99 */
100
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000101static ssize_t bonding_store_bonds(struct class *cls,
Andi Kleen28812fe2010-01-05 12:48:07 +0100102 struct class_attribute *attr,
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000103 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800104{
Eric W. Biederman4c224002011-10-12 21:56:25 +0000105 struct bond_net *bn =
106 container_of(attr, struct bond_net, class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800107 char command[IFNAMSIZ + 1] = {0, };
108 char *ifname;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800109 int rv, res = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800110
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800111 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
112 ifname = command + 1;
113 if ((strlen(command) <= 1) ||
114 !dev_valid_name(ifname))
115 goto err_no_cmd;
116
117 if (command[0] == '+') {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800118 pr_info("%s is being created...\n", ifname);
Eric W. Biederman4c224002011-10-12 21:56:25 +0000119 rv = bond_create(bn->net, ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800120 if (rv) {
Phil Oester5f86cad12011-03-14 06:22:06 +0000121 if (rv == -EEXIST)
122 pr_info("%s already exists.\n", ifname);
123 else
124 pr_info("%s creation failed.\n", ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800125 res = rv;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800126 }
Stephen Hemminger373500d2009-06-12 19:02:50 +0000127 } else if (command[0] == '-') {
128 struct net_device *bond_dev;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800129
Jay Vosburgh027ea042008-01-17 16:25:02 -0800130 rtnl_lock();
Eric W. Biederman4c224002011-10-12 21:56:25 +0000131 bond_dev = bond_get_by_name(bn, ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000132 if (bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800133 pr_info("%s is being deleted...\n", ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000134 unregister_netdevice(bond_dev);
135 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800136 pr_err("unable to delete non-existent %s\n", ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000137 res = -ENODEV;
138 }
139 rtnl_unlock();
140 } else
141 goto err_no_cmd;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800142
Stephen Hemminger373500d2009-06-12 19:02:50 +0000143 /* Always return either count or an error. If you return 0, you'll
144 * get called forever, which is bad.
145 */
146 return res;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800147
148err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800149 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700150 return -EPERM;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800151}
Stephen Hemminger373500d2009-06-12 19:02:50 +0000152
Eric W. Biederman4c224002011-10-12 21:56:25 +0000153static const void *bonding_namespace(struct class *cls,
154 const struct class_attribute *attr)
155{
156 const struct bond_net *bn =
157 container_of(attr, struct bond_net, class_attr_bonding_masters);
158 return bn->net;
159}
160
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800161/* class attribute for bond_masters file. This ends up in /sys/class/net */
Eric W. Biederman4c224002011-10-12 21:56:25 +0000162static const struct class_attribute class_attr_bonding_masters = {
163 .attr = {
164 .name = "bonding_masters",
165 .mode = S_IWUSR | S_IRUGO,
166 },
167 .show = bonding_show_bonds,
168 .store = bonding_store_bonds,
169 .namespace = bonding_namespace,
170};
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800171
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000172int bond_create_slave_symlinks(struct net_device *master,
173 struct net_device *slave)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800174{
175 char linkname[IFNAMSIZ+7];
176 int ret = 0;
177
178 /* first, create a link from the slave back to the master */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700179 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800180 "master");
181 if (ret)
182 return ret;
183 /* next, create a link from the master to the slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000184 sprintf(linkname, "slave_%s", slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700185 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800186 linkname);
187 return ret;
188
189}
190
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000191void bond_destroy_slave_symlinks(struct net_device *master,
192 struct net_device *slave)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800193{
194 char linkname[IFNAMSIZ+7];
195
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700196 sysfs_remove_link(&(slave->dev.kobj), "master");
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000197 sprintf(linkname, "slave_%s", slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700198 sysfs_remove_link(&(master->dev.kobj), linkname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800199}
200
201
202/*
203 * Show the slaves in the current bond.
204 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700205static ssize_t bonding_show_slaves(struct device *d,
206 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800207{
208 struct slave *slave;
209 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700210 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800211
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700212 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800213 bond_for_each_slave(bond, slave, i) {
214 if (res > (PAGE_SIZE - IFNAMSIZ)) {
215 /* not enough space for another interface name */
216 if ((PAGE_SIZE - res) > 10)
217 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800218 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800219 break;
220 }
221 res += sprintf(buf + res, "%s ", slave->dev->name);
222 }
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700223 read_unlock(&bond->lock);
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800224 if (res)
225 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800226 return res;
227}
228
229/*
230 * Set the slaves in the current bond. The bond interface must be
231 * up for this to succeed.
Jiri Pirkof9f35452010-05-18 05:46:39 +0000232 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
233 * All hard work should be done there.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800234 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700235static ssize_t bonding_store_slaves(struct device *d,
236 struct device_attribute *attr,
237 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800238{
239 char command[IFNAMSIZ + 1] = { 0, };
240 char *ifname;
Jiri Pirkof9f35452010-05-18 05:46:39 +0000241 int res, ret = count;
242 struct net_device *dev;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700243 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800244
Eric W. Biederman496a60c2009-05-13 17:02:50 +0000245 if (!rtnl_trylock())
246 return restart_syscall();
Jay Vosburgh027ea042008-01-17 16:25:02 -0800247
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800248 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
249 ifname = command + 1;
250 if ((strlen(command) <= 1) ||
251 !dev_valid_name(ifname))
252 goto err_no_cmd;
253
Jiri Pirkof9f35452010-05-18 05:46:39 +0000254 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
255 if (!dev) {
256 pr_info("%s: Interface %s does not exist!\n",
257 bond->dev->name, ifname);
258 ret = -ENODEV;
259 goto out;
260 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800261
Jiri Pirkof9f35452010-05-18 05:46:39 +0000262 switch (command[0]) {
263 case '+':
264 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800265 res = bond_enslave(bond->dev, dev);
Jiri Pirkof9f35452010-05-18 05:46:39 +0000266 break;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000267
Jiri Pirkof9f35452010-05-18 05:46:39 +0000268 case '-':
269 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
270 res = bond_release(bond->dev, dev);
271 break;
272
273 default:
274 goto err_no_cmd;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800275 }
276
Jiri Pirkof9f35452010-05-18 05:46:39 +0000277 if (res)
278 ret = res;
279 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800280
281err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800282 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
283 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800284 ret = -EPERM;
285
286out:
Jay Vosburgh027ea042008-01-17 16:25:02 -0800287 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800288 return ret;
289}
290
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000291static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
292 bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800293
294/*
295 * Show and set the bonding mode. The bond interface must be down to
296 * change the mode.
297 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700298static ssize_t bonding_show_mode(struct device *d,
299 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800300{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700301 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800302
303 return sprintf(buf, "%s %d\n",
304 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800305 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800306}
307
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700308static ssize_t bonding_store_mode(struct device *d,
309 struct device_attribute *attr,
310 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800311{
312 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700313 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800314
315 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800316 pr_err("unable to update mode of %s because interface is up.\n",
317 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800318 ret = -EPERM;
319 goto out;
320 }
321
Jay Vosburghece95f72008-01-17 16:25:01 -0800322 new_value = bond_parse_parm(buf, bond_mode_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800323 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800324 pr_err("%s: Ignoring invalid mode value %.*s.\n",
325 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800326 ret = -EINVAL;
327 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800328 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000329 if ((new_value == BOND_MODE_ALB ||
330 new_value == BOND_MODE_TLB) &&
331 bond->params.arp_interval) {
332 pr_err("%s: %s mode is incompatible with arp monitoring.\n",
333 bond->dev->name, bond_mode_tbl[new_value].modename);
334 ret = -EINVAL;
335 goto out;
336 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000337
338 bond->params.mode = new_value;
339 bond_set_mode_ops(bond, bond->params.mode);
340 pr_info("%s: setting mode to %s (%d).\n",
341 bond->dev->name, bond_mode_tbl[new_value].modename,
342 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800343out:
344 return ret;
345}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000346static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
347 bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800348
349/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000350 * Show and set the bonding transmit hash method.
351 * The bond interface must be down to change the xmit hash policy.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800352 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700353static ssize_t bonding_show_xmit_hash(struct device *d,
354 struct device_attribute *attr,
355 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800356{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700357 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800358
Wagner Ferenc8e4b9322007-12-06 23:40:32 -0800359 return sprintf(buf, "%s %d\n",
360 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
361 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800362}
363
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700364static ssize_t bonding_store_xmit_hash(struct device *d,
365 struct device_attribute *attr,
366 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800367{
368 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700369 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800370
371 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800372 pr_err("%s: Interface is up. Unable to update xmit policy.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800373 bond->dev->name);
374 ret = -EPERM;
375 goto out;
376 }
377
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;
384 goto out;
385 } else {
386 bond->params.xmit_policy = new_value;
387 bond_set_mode_ops(bond, bond->params.mode);
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800388 pr_info("%s: setting xmit hash policy to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000389 bond->dev->name,
390 xmit_hashtype_tbl[new_value].modename, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800391 }
392out:
393 return ret;
394}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000395static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
396 bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800397
398/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700399 * Show and set arp_validate.
400 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700401static ssize_t bonding_show_arp_validate(struct device *d,
402 struct device_attribute *attr,
403 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700404{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700405 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700406
407 return sprintf(buf, "%s %d\n",
408 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800409 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700410}
411
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700412static ssize_t bonding_store_arp_validate(struct device *d,
413 struct device_attribute *attr,
414 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700415{
416 int new_value;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700417 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700418
Jay Vosburghece95f72008-01-17 16:25:01 -0800419 new_value = bond_parse_parm(buf, arp_validate_tbl);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700420 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800421 pr_err("%s: Ignoring invalid arp_validate value %s\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700422 bond->dev->name, buf);
423 return -EINVAL;
424 }
425 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800426 pr_err("%s: arp_validate only supported in active-backup mode.\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700427 bond->dev->name);
428 return -EINVAL;
429 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800430 pr_info("%s: setting arp_validate to %s (%d).\n",
431 bond->dev->name, arp_validate_tbl[new_value].modename,
432 new_value);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700433
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700434 bond->params.arp_validate = new_value;
435
436 return count;
437}
438
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000439static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
440 bonding_store_arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700441
442/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700443 * Show and store fail_over_mac. User only allowed to change the
444 * value when there are no slaves.
445 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000446static ssize_t bonding_show_fail_over_mac(struct device *d,
447 struct device_attribute *attr,
448 char *buf)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700449{
450 struct bonding *bond = to_bond(d);
451
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700452 return sprintf(buf, "%s %d\n",
453 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
454 bond->params.fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700455}
456
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000457static ssize_t bonding_store_fail_over_mac(struct device *d,
458 struct device_attribute *attr,
459 const char *buf, size_t count)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700460{
461 int new_value;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700462 struct bonding *bond = to_bond(d);
463
464 if (bond->slave_cnt != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800465 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
Jay Vosburghdd957c52007-10-09 19:57:24 -0700466 bond->dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700467 return -EPERM;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700468 }
469
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700470 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
471 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800472 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700473 bond->dev->name, buf);
474 return -EINVAL;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700475 }
476
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700477 bond->params.fail_over_mac = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800478 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
479 bond->dev->name, fail_over_mac_tbl[new_value].modename,
480 new_value);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700481
482 return count;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700483}
484
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000485static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
486 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700487
488/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800489 * Show and set the arp timer interval. There are two tricky bits
490 * here. First, if ARP monitoring is activated, then we must disable
491 * MII monitoring. Second, if the ARP timer isn't running, we must
492 * start it.
493 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700494static ssize_t bonding_show_arp_interval(struct device *d,
495 struct device_attribute *attr,
496 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800497{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700498 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800499
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800500 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800501}
502
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700503static ssize_t bonding_store_arp_interval(struct device *d,
504 struct device_attribute *attr,
505 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800506{
507 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700508 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800509
510 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800511 pr_err("%s: no arp_interval value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800512 bond->dev->name);
513 ret = -EINVAL;
514 goto out;
515 }
516 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800517 pr_err("%s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800518 bond->dev->name, new_value, INT_MAX);
519 ret = -EINVAL;
520 goto out;
521 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000522 if (bond->params.mode == BOND_MODE_ALB ||
523 bond->params.mode == BOND_MODE_TLB) {
524 pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
525 bond->dev->name, bond->dev->name);
526 ret = -EINVAL;
527 goto out;
528 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800529 pr_info("%s: Setting ARP monitoring interval to %d.\n",
530 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800531 bond->params.arp_interval = new_value;
532 if (bond->params.miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800533 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
534 bond->dev->name, bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800535 bond->params.miimon = 0;
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700536 if (delayed_work_pending(&bond->mii_work)) {
537 cancel_delayed_work(&bond->mii_work);
538 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800539 }
540 }
541 if (!bond->params.arp_targets[0]) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800542 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
543 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800544 }
545 if (bond->dev->flags & IFF_UP) {
546 /* If the interface is up, we may need to fire off
547 * the ARP timer. If the interface is down, the
548 * timer will get fired off when the open function
549 * is called.
550 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700551 if (!delayed_work_pending(&bond->arp_work)) {
552 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
553 INIT_DELAYED_WORK(&bond->arp_work,
554 bond_activebackup_arp_mon);
555 else
556 INIT_DELAYED_WORK(&bond->arp_work,
557 bond_loadbalance_arp_mon);
558
559 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800560 }
561 }
562
563out:
564 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{
Al Virod3bb52b2007-08-22 20:06:58 -0400593 __be32 newtarget;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800594 int i = 0, done = 0, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700595 struct bonding *bond = to_bond(d);
Al Virod3bb52b2007-08-22 20:06:58 -0400596 __be32 *targets;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800597
598 targets = bond->params.arp_targets;
599 newtarget = in_aton(buf + 1);
600 /* look for adds */
601 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400602 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800603 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700604 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800605 ret = -EINVAL;
606 goto out;
607 }
608 /* look for an empty slot to put the target in, and check for dupes */
Brian Haley5a31bec2009-04-13 00:11:30 -0700609 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800610 if (targets[i] == newtarget) { /* duplicate */
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800611 pr_err("%s: ARP target %pI4 is already present\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700612 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800613 ret = -EINVAL;
614 goto out;
615 }
Brian Haley5a31bec2009-04-13 00:11:30 -0700616 if (targets[i] == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800617 pr_info("%s: adding ARP target %pI4.\n",
618 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800619 done = 1;
620 targets[i] = newtarget;
621 }
622 }
623 if (!done) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800624 pr_err("%s: ARP target table is full!\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800625 bond->dev->name);
626 ret = -EINVAL;
627 goto out;
628 }
629
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000630 } else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400631 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800632 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700633 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800634 ret = -EINVAL;
635 goto out;
636 }
637
Brian Haley5a31bec2009-04-13 00:11:30 -0700638 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800639 if (targets[i] == newtarget) {
Brian Haley5a31bec2009-04-13 00:11:30 -0700640 int j;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800641 pr_info("%s: removing ARP target %pI4.\n",
642 bond->dev->name, &newtarget);
Brian Haley5a31bec2009-04-13 00:11:30 -0700643 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
644 targets[j] = targets[j+1];
645
646 targets[j] = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800647 done = 1;
648 }
649 }
650 if (!done) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800651 pr_info("%s: unable to remove nonexistent ARP target %pI4.\n",
652 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800653 ret = -EINVAL;
654 goto out;
655 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000656 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800657 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
658 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800659 ret = -EPERM;
660 goto out;
661 }
662
663out:
664 return ret;
665}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700666static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800667
668/*
669 * Show and set the up and down delays. These must be multiples of the
670 * MII monitoring value, and are stored internally as the multiplier.
671 * Thus, we must translate to MS for the real world.
672 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700673static ssize_t bonding_show_downdelay(struct device *d,
674 struct device_attribute *attr,
675 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800676{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700677 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800678
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800679 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800680}
681
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700682static ssize_t bonding_store_downdelay(struct device *d,
683 struct device_attribute *attr,
684 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800685{
686 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700687 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800688
689 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800690 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800691 bond->dev->name);
692 ret = -EPERM;
693 goto out;
694 }
695
696 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800697 pr_err("%s: no down delay value specified.\n", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800698 ret = -EINVAL;
699 goto out;
700 }
701 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800702 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800703 bond->dev->name, new_value, 1, INT_MAX);
704 ret = -EINVAL;
705 goto out;
706 } else {
707 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800708 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 +0000709 bond->dev->name, new_value,
710 bond->params.miimon,
711 (new_value / bond->params.miimon) *
712 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800713 }
714 bond->params.downdelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800715 pr_info("%s: Setting down delay to %d.\n",
716 bond->dev->name,
717 bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800718
719 }
720
721out:
722 return ret;
723}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000724static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
725 bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800726
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700727static ssize_t bonding_show_updelay(struct device *d,
728 struct device_attribute *attr,
729 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800730{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700731 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800732
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800733 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800734
735}
736
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700737static ssize_t bonding_store_updelay(struct device *d,
738 struct device_attribute *attr,
739 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800740{
741 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700742 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800743
744 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800745 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800746 bond->dev->name);
747 ret = -EPERM;
748 goto out;
749 }
750
751 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800752 pr_err("%s: no up delay value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800753 bond->dev->name);
754 ret = -EINVAL;
755 goto out;
756 }
757 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800758 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800759 bond->dev->name, new_value, 1, INT_MAX);
760 ret = -EINVAL;
761 goto out;
762 } else {
763 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800764 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 +0000765 bond->dev->name, new_value,
766 bond->params.miimon,
767 (new_value / bond->params.miimon) *
768 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800769 }
770 bond->params.updelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800771 pr_info("%s: Setting up delay to %d.\n",
772 bond->dev->name,
773 bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800774 }
775
776out:
777 return ret;
778}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000779static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
780 bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800781
782/*
783 * Show and set the LACP interval. Interface must be down, and the mode
784 * must be set to 802.3ad mode.
785 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700786static ssize_t bonding_show_lacp(struct device *d,
787 struct device_attribute *attr,
788 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800789{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700790 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800791
792 return sprintf(buf, "%s %d\n",
793 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800794 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800795}
796
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700797static ssize_t bonding_store_lacp(struct device *d,
798 struct device_attribute *attr,
799 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800800{
801 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700802 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800803
804 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800805 pr_err("%s: Unable to update LACP rate because interface is up.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800806 bond->dev->name);
807 ret = -EPERM;
808 goto out;
809 }
810
811 if (bond->params.mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800812 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800813 bond->dev->name);
814 ret = -EPERM;
815 goto out;
816 }
817
Jay Vosburghece95f72008-01-17 16:25:01 -0800818 new_value = bond_parse_parm(buf, bond_lacp_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800819
820 if ((new_value == 1) || (new_value == 0)) {
821 bond->params.lacp_fast = new_value;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +0000822 bond_3ad_update_lacp_rate(bond);
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800823 pr_info("%s: Setting LACP rate to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000824 bond->dev->name, bond_lacp_tbl[new_value].modename,
825 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800826 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800827 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000828 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800829 ret = -EINVAL;
830 }
831out:
832 return ret;
833}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000834static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
835 bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800836
stephen hemminger655f8912011-06-22 09:54:39 +0000837static ssize_t bonding_show_min_links(struct device *d,
838 struct device_attribute *attr,
839 char *buf)
840{
841 struct bonding *bond = to_bond(d);
842
843 return sprintf(buf, "%d\n", bond->params.min_links);
844}
845
846static ssize_t bonding_store_min_links(struct device *d,
847 struct device_attribute *attr,
848 const char *buf, size_t count)
849{
850 struct bonding *bond = to_bond(d);
851 int ret;
852 unsigned int new_value;
853
854 ret = kstrtouint(buf, 0, &new_value);
855 if (ret < 0) {
856 pr_err("%s: Ignoring invalid min links value %s.\n",
857 bond->dev->name, buf);
858 return ret;
859 }
860
861 pr_info("%s: Setting min links value to %u\n",
862 bond->dev->name, new_value);
863 bond->params.min_links = new_value;
864 return count;
865}
866static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
867 bonding_show_min_links, bonding_store_min_links);
868
Jay Vosburghfd989c82008-11-04 17:51:16 -0800869static ssize_t bonding_show_ad_select(struct device *d,
870 struct device_attribute *attr,
871 char *buf)
872{
873 struct bonding *bond = to_bond(d);
874
875 return sprintf(buf, "%s %d\n",
876 ad_select_tbl[bond->params.ad_select].modename,
877 bond->params.ad_select);
878}
879
880
881static ssize_t bonding_store_ad_select(struct device *d,
882 struct device_attribute *attr,
883 const char *buf, size_t count)
884{
885 int new_value, ret = count;
886 struct bonding *bond = to_bond(d);
887
888 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800889 pr_err("%s: Unable to update ad_select because interface is up.\n",
890 bond->dev->name);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800891 ret = -EPERM;
892 goto out;
893 }
894
895 new_value = bond_parse_parm(buf, ad_select_tbl);
896
897 if (new_value != -1) {
898 bond->params.ad_select = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800899 pr_info("%s: Setting ad_select to %s (%d).\n",
900 bond->dev->name, ad_select_tbl[new_value].modename,
901 new_value);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800902 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800903 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -0800904 bond->dev->name, (int)strlen(buf) - 1, buf);
905 ret = -EINVAL;
906 }
907out:
908 return ret;
909}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000910static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
911 bonding_show_ad_select, bonding_store_ad_select);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800912
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800913/*
Ben Hutchingsad246c92011-04-26 15:25:52 +0000914 * Show and set the number of peer notifications to send after a failover event.
915 */
916static ssize_t bonding_show_num_peer_notif(struct device *d,
917 struct device_attribute *attr,
918 char *buf)
919{
920 struct bonding *bond = to_bond(d);
921 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
922}
923
924static ssize_t bonding_store_num_peer_notif(struct device *d,
925 struct device_attribute *attr,
926 const char *buf, size_t count)
927{
928 struct bonding *bond = to_bond(d);
929 int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
930 return err ? err : count;
931}
932static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
933 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
934static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
935 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
936
937/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800938 * Show and set the MII monitor interval. There are two tricky bits
939 * here. First, if MII monitoring is activated, then we must disable
940 * ARP monitoring. Second, if the timer isn't running, we must
941 * start it.
942 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700943static ssize_t bonding_show_miimon(struct device *d,
944 struct device_attribute *attr,
945 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800946{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700947 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800948
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800949 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800950}
951
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700952static ssize_t bonding_store_miimon(struct device *d,
953 struct device_attribute *attr,
954 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800955{
956 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700957 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800958
959 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800960 pr_err("%s: no miimon value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800961 bond->dev->name);
962 ret = -EINVAL;
963 goto out;
964 }
965 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800966 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800967 bond->dev->name, new_value, 1, INT_MAX);
968 ret = -EINVAL;
969 goto out;
970 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800971 pr_info("%s: Setting MII monitoring interval to %d.\n",
972 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800973 bond->params.miimon = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000974 if (bond->params.updelay)
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800975 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
976 bond->dev->name,
977 bond->params.updelay * bond->params.miimon);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000978 if (bond->params.downdelay)
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800979 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
980 bond->dev->name,
981 bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800982 if (bond->params.arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800983 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
984 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800985 bond->params.arp_interval = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700986 if (bond->params.arp_validate) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700987 bond->params.arp_validate =
988 BOND_ARP_VALIDATE_NONE;
989 }
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700990 if (delayed_work_pending(&bond->arp_work)) {
991 cancel_delayed_work(&bond->arp_work);
992 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800993 }
994 }
995
996 if (bond->dev->flags & IFF_UP) {
997 /* If the interface is up, we may need to fire off
998 * the MII timer. If the interface is down, the
999 * timer will get fired off when the open function
1000 * is called.
1001 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001002 if (!delayed_work_pending(&bond->mii_work)) {
1003 INIT_DELAYED_WORK(&bond->mii_work,
1004 bond_mii_monitor);
1005 queue_delayed_work(bond->wq,
1006 &bond->mii_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001007 }
1008 }
1009 }
1010out:
1011 return ret;
1012}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001013static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1014 bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001015
1016/*
1017 * Show and set the primary slave. The store function is much
1018 * simpler than bonding_store_slaves function because it only needs to
1019 * handle one interface name.
1020 * The bond must be a mode that supports a primary for this be
1021 * set.
1022 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001023static ssize_t bonding_show_primary(struct device *d,
1024 struct device_attribute *attr,
1025 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001026{
1027 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001028 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001029
1030 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001031 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001032
1033 return count;
1034}
1035
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001036static ssize_t bonding_store_primary(struct device *d,
1037 struct device_attribute *attr,
1038 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001039{
1040 int i;
1041 struct slave *slave;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001042 struct bonding *bond = to_bond(d);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001043 char ifname[IFNAMSIZ];
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001044
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001045 if (!rtnl_trylock())
1046 return restart_syscall();
Neil Hormane843fa52010-10-13 16:01:50 +00001047 block_netpoll_tx();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001048 read_lock(&bond->lock);
1049 write_lock_bh(&bond->curr_slave_lock);
1050
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001051 if (!USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001052 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1053 bond->dev->name, bond->dev->name, bond->params.mode);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001054 goto out;
1055 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001056
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001057 sscanf(buf, "%16s", ifname); /* IFNAMSIZ */
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001058
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001059 /* check to see if we are clearing primary */
1060 if (!strlen(ifname) || buf[0] == '\n') {
1061 pr_info("%s: Setting primary slave to None.\n",
1062 bond->dev->name);
1063 bond->primary_slave = NULL;
1064 bond_select_active_slave(bond);
1065 goto out;
1066 }
1067
1068 bond_for_each_slave(bond, slave, i) {
1069 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1070 pr_info("%s: Setting %s as primary slave.\n",
1071 bond->dev->name, slave->dev->name);
1072 bond->primary_slave = slave;
1073 strcpy(bond->params.primary, slave->dev->name);
1074 bond_select_active_slave(bond);
1075 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001076 }
1077 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001078
1079 pr_info("%s: Unable to set %.*s as primary slave.\n",
1080 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001081out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001082 write_unlock_bh(&bond->curr_slave_lock);
1083 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001084 unblock_netpoll_tx();
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001085 rtnl_unlock();
1086
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001087 return count;
1088}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001089static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1090 bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001091
1092/*
Jiri Pirkoa5499522009-09-25 03:28:09 +00001093 * Show and set the primary_reselect flag.
1094 */
1095static ssize_t bonding_show_primary_reselect(struct device *d,
1096 struct device_attribute *attr,
1097 char *buf)
1098{
1099 struct bonding *bond = to_bond(d);
1100
1101 return sprintf(buf, "%s %d\n",
1102 pri_reselect_tbl[bond->params.primary_reselect].modename,
1103 bond->params.primary_reselect);
1104}
1105
1106static ssize_t bonding_store_primary_reselect(struct device *d,
1107 struct device_attribute *attr,
1108 const char *buf, size_t count)
1109{
1110 int new_value, ret = count;
1111 struct bonding *bond = to_bond(d);
1112
1113 if (!rtnl_trylock())
1114 return restart_syscall();
1115
1116 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1117 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001118 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001119 bond->dev->name,
1120 (int) strlen(buf) - 1, buf);
1121 ret = -EINVAL;
1122 goto out;
1123 }
1124
1125 bond->params.primary_reselect = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001126 pr_info("%s: setting primary_reselect to %s (%d).\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001127 bond->dev->name, pri_reselect_tbl[new_value].modename,
1128 new_value);
1129
Neil Hormane843fa52010-10-13 16:01:50 +00001130 block_netpoll_tx();
Jiri Pirkoa5499522009-09-25 03:28:09 +00001131 read_lock(&bond->lock);
1132 write_lock_bh(&bond->curr_slave_lock);
1133 bond_select_active_slave(bond);
1134 write_unlock_bh(&bond->curr_slave_lock);
1135 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001136 unblock_netpoll_tx();
Jiri Pirkoa5499522009-09-25 03:28:09 +00001137out:
1138 rtnl_unlock();
1139 return ret;
1140}
1141static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1142 bonding_show_primary_reselect,
1143 bonding_store_primary_reselect);
1144
1145/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001146 * Show and set the use_carrier flag.
1147 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001148static ssize_t bonding_show_carrier(struct device *d,
1149 struct device_attribute *attr,
1150 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001151{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001152 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001153
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001154 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001155}
1156
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001157static ssize_t bonding_store_carrier(struct device *d,
1158 struct device_attribute *attr,
1159 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001160{
1161 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001162 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001163
1164
1165 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001166 pr_err("%s: no use_carrier value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001167 bond->dev->name);
1168 ret = -EINVAL;
1169 goto out;
1170 }
1171 if ((new_value == 0) || (new_value == 1)) {
1172 bond->params.use_carrier = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001173 pr_info("%s: Setting use_carrier to %d.\n",
1174 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001175 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001176 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1177 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001178 }
1179out:
Jiri Pirko672bda32011-01-25 11:03:25 +00001180 return ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001181}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001182static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1183 bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001184
1185
1186/*
1187 * Show and set currently active_slave.
1188 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001189static ssize_t bonding_show_active_slave(struct device *d,
1190 struct device_attribute *attr,
1191 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001192{
1193 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001194 struct bonding *bond = to_bond(d);
Wagner Ferenc16cd0162007-12-06 23:40:29 -08001195 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001196
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001197 read_lock(&bond->curr_slave_lock);
1198 curr = bond->curr_active_slave;
1199 read_unlock(&bond->curr_slave_lock);
1200
1201 if (USES_PRIMARY(bond->params.mode) && curr)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001202 count = sprintf(buf, "%s\n", curr->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001203 return count;
1204}
1205
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001206static ssize_t bonding_store_active_slave(struct device *d,
1207 struct device_attribute *attr,
1208 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001209{
1210 int i;
1211 struct slave *slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001212 struct slave *old_active = NULL;
1213 struct slave *new_active = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001214 struct bonding *bond = to_bond(d);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001215 char ifname[IFNAMSIZ];
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001216
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001217 if (!rtnl_trylock())
1218 return restart_syscall();
Neil Hormane843fa52010-10-13 16:01:50 +00001219
1220 block_netpoll_tx();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001221 read_lock(&bond->lock);
1222 write_lock_bh(&bond->curr_slave_lock);
Jay Vosburgh1466a212007-11-06 13:33:28 -08001223
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001224 if (!USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001225 pr_info("%s: Unable to change active slave; %s is in mode %d\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001226 bond->dev->name, bond->dev->name, bond->params.mode);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001227 goto out;
1228 }
1229
1230 sscanf(buf, "%16s", ifname); /* IFNAMSIZ */
1231
1232 /* check to see if we are clearing active */
1233 if (!strlen(ifname) || buf[0] == '\n') {
1234 pr_info("%s: Clearing current active slave.\n",
1235 bond->dev->name);
1236 bond->curr_active_slave = NULL;
1237 bond_select_active_slave(bond);
1238 goto out;
1239 }
1240
1241 bond_for_each_slave(bond, slave, i) {
1242 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1243 old_active = bond->curr_active_slave;
1244 new_active = slave;
1245 if (new_active == old_active) {
1246 /* do nothing */
1247 pr_info("%s: %s is already the current"
1248 " active slave.\n",
1249 bond->dev->name,
1250 slave->dev->name);
1251 goto out;
1252 }
1253 else {
1254 if ((new_active) &&
1255 (old_active) &&
1256 (new_active->link == BOND_LINK_UP) &&
1257 IS_UP(new_active->dev)) {
1258 pr_info("%s: Setting %s as active"
1259 " slave.\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001260 bond->dev->name,
1261 slave->dev->name);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001262 bond_change_active_slave(bond,
1263 new_active);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001264 }
1265 else {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001266 pr_info("%s: Could not set %s as"
1267 " active slave; either %s is"
1268 " down or the link is down.\n",
1269 bond->dev->name,
1270 slave->dev->name,
1271 slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001272 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001273 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001274 }
1275 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001276 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001277
1278 pr_info("%s: Unable to set %.*s as active slave.\n",
1279 bond->dev->name, (int)strlen(buf) - 1, buf);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001280 out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001281 write_unlock_bh(&bond->curr_slave_lock);
1282 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001283 unblock_netpoll_tx();
1284
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001285 rtnl_unlock();
1286
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001287 return count;
1288
1289}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001290static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1291 bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001292
1293
1294/*
1295 * Show link status of the bond interface.
1296 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001297static ssize_t bonding_show_mii_status(struct device *d,
1298 struct device_attribute *attr,
1299 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001300{
1301 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001302 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001303
1304 read_lock(&bond->curr_slave_lock);
1305 curr = bond->curr_active_slave;
1306 read_unlock(&bond->curr_slave_lock);
1307
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001308 return sprintf(buf, "%s\n", curr ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001309}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001310static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001311
1312
1313/*
1314 * Show current 802.3ad aggregator ID.
1315 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001316static ssize_t bonding_show_ad_aggregator(struct device *d,
1317 struct device_attribute *attr,
1318 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001319{
1320 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001321 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001322
1323 if (bond->params.mode == BOND_MODE_8023AD) {
1324 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001325 count = sprintf(buf, "%d\n",
1326 (bond_3ad_get_active_agg_info(bond, &ad_info))
1327 ? 0 : ad_info.aggregator_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001328 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001329
1330 return count;
1331}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001332static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001333
1334
1335/*
1336 * Show number of active 802.3ad ports.
1337 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001338static ssize_t bonding_show_ad_num_ports(struct device *d,
1339 struct device_attribute *attr,
1340 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001341{
1342 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001343 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001344
1345 if (bond->params.mode == BOND_MODE_8023AD) {
1346 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001347 count = sprintf(buf, "%d\n",
1348 (bond_3ad_get_active_agg_info(bond, &ad_info))
1349 ? 0 : ad_info.ports);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001350 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001351
1352 return count;
1353}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001354static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001355
1356
1357/*
1358 * Show current 802.3ad actor key.
1359 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001360static ssize_t bonding_show_ad_actor_key(struct device *d,
1361 struct device_attribute *attr,
1362 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001363{
1364 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001365 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001366
1367 if (bond->params.mode == BOND_MODE_8023AD) {
1368 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001369 count = sprintf(buf, "%d\n",
1370 (bond_3ad_get_active_agg_info(bond, &ad_info))
1371 ? 0 : ad_info.actor_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001372 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001373
1374 return count;
1375}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001376static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001377
1378
1379/*
1380 * Show current 802.3ad partner key.
1381 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001382static ssize_t bonding_show_ad_partner_key(struct device *d,
1383 struct device_attribute *attr,
1384 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001385{
1386 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001387 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001388
1389 if (bond->params.mode == BOND_MODE_8023AD) {
1390 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001391 count = sprintf(buf, "%d\n",
1392 (bond_3ad_get_active_agg_info(bond, &ad_info))
1393 ? 0 : ad_info.partner_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001394 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001395
1396 return count;
1397}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001398static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001399
1400
1401/*
1402 * Show current 802.3ad partner mac.
1403 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001404static ssize_t bonding_show_ad_partner_mac(struct device *d,
1405 struct device_attribute *attr,
1406 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001407{
1408 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001409 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001410
1411 if (bond->params.mode == BOND_MODE_8023AD) {
1412 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001413 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
Johannes Berge1749612008-10-27 15:59:26 -07001414 count = sprintf(buf, "%pM\n", ad_info.partner_system);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001415 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001416
1417 return count;
1418}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001419static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001420
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001421/*
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001422 * Show the queue_ids of the slaves in the current bond.
1423 */
1424static ssize_t bonding_show_queue_id(struct device *d,
1425 struct device_attribute *attr,
1426 char *buf)
1427{
1428 struct slave *slave;
1429 int i, res = 0;
1430 struct bonding *bond = to_bond(d);
1431
1432 if (!rtnl_trylock())
1433 return restart_syscall();
1434
1435 read_lock(&bond->lock);
1436 bond_for_each_slave(bond, slave, i) {
Nicolas de Pesloüan79236682010-07-14 18:24:54 -07001437 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1438 /* not enough space for another interface_name:queue_id pair */
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001439 if ((PAGE_SIZE - res) > 10)
1440 res = PAGE_SIZE - 10;
1441 res += sprintf(buf + res, "++more++ ");
1442 break;
1443 }
1444 res += sprintf(buf + res, "%s:%d ",
1445 slave->dev->name, slave->queue_id);
1446 }
1447 read_unlock(&bond->lock);
1448 if (res)
1449 buf[res-1] = '\n'; /* eat the leftover space */
1450 rtnl_unlock();
1451 return res;
1452}
1453
1454/*
1455 * Set the queue_ids of the slaves in the current bond. The bond
1456 * interface must be enslaved for this to work.
1457 */
1458static ssize_t bonding_store_queue_id(struct device *d,
1459 struct device_attribute *attr,
1460 const char *buffer, size_t count)
1461{
1462 struct slave *slave, *update_slave;
1463 struct bonding *bond = to_bond(d);
1464 u16 qid;
1465 int i, ret = count;
1466 char *delim;
1467 struct net_device *sdev = NULL;
1468
1469 if (!rtnl_trylock())
1470 return restart_syscall();
1471
1472 /* delim will point to queue id if successful */
1473 delim = strchr(buffer, ':');
1474 if (!delim)
1475 goto err_no_cmd;
1476
1477 /*
1478 * Terminate string that points to device name and bump it
1479 * up one, so we can read the queue id there.
1480 */
1481 *delim = '\0';
1482 if (sscanf(++delim, "%hd\n", &qid) != 1)
1483 goto err_no_cmd;
1484
1485 /* Check buffer length, valid ifname and queue id */
1486 if (strlen(buffer) > IFNAMSIZ ||
1487 !dev_valid_name(buffer) ||
1488 qid > bond->params.tx_queues)
1489 goto err_no_cmd;
1490
1491 /* Get the pointer to that interface if it exists */
1492 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1493 if (!sdev)
1494 goto err_no_cmd;
1495
1496 read_lock(&bond->lock);
1497
1498 /* Search for thes slave and check for duplicate qids */
1499 update_slave = NULL;
1500 bond_for_each_slave(bond, slave, i) {
1501 if (sdev == slave->dev)
1502 /*
1503 * We don't need to check the matching
1504 * slave for dups, since we're overwriting it
1505 */
1506 update_slave = slave;
1507 else if (qid && qid == slave->queue_id) {
1508 goto err_no_cmd_unlock;
1509 }
1510 }
1511
1512 if (!update_slave)
1513 goto err_no_cmd_unlock;
1514
1515 /* Actually set the qids for the slave */
1516 update_slave->queue_id = qid;
1517
1518 read_unlock(&bond->lock);
1519out:
1520 rtnl_unlock();
1521 return ret;
1522
1523err_no_cmd_unlock:
1524 read_unlock(&bond->lock);
1525err_no_cmd:
1526 pr_info("invalid input for queue_id set for %s.\n",
1527 bond->dev->name);
1528 ret = -EPERM;
1529 goto out;
1530}
1531
1532static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1533 bonding_store_queue_id);
1534
1535
1536/*
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001537 * Show and set the all_slaves_active flag.
1538 */
1539static ssize_t bonding_show_slaves_active(struct device *d,
1540 struct device_attribute *attr,
1541 char *buf)
1542{
1543 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001544
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001545 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1546}
1547
1548static ssize_t bonding_store_slaves_active(struct device *d,
1549 struct device_attribute *attr,
1550 const char *buf, size_t count)
1551{
1552 int i, new_value, ret = count;
1553 struct bonding *bond = to_bond(d);
1554 struct slave *slave;
1555
1556 if (sscanf(buf, "%d", &new_value) != 1) {
1557 pr_err("%s: no all_slaves_active value specified.\n",
1558 bond->dev->name);
1559 ret = -EINVAL;
1560 goto out;
1561 }
1562
1563 if (new_value == bond->params.all_slaves_active)
1564 goto out;
1565
1566 if ((new_value == 0) || (new_value == 1)) {
1567 bond->params.all_slaves_active = new_value;
1568 } else {
1569 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1570 bond->dev->name, new_value);
1571 ret = -EINVAL;
1572 goto out;
1573 }
1574
1575 bond_for_each_slave(bond, slave, i) {
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001576 if (!bond_is_active_slave(slave)) {
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001577 if (new_value)
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001578 slave->inactive = 0;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001579 else
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001580 slave->inactive = 1;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001581 }
1582 }
1583out:
Jiri Pirko672bda32011-01-25 11:03:25 +00001584 return ret;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001585}
1586static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1587 bonding_show_slaves_active, bonding_store_slaves_active);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001588
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001589/*
1590 * Show and set the number of IGMP membership reports to send on link failure
1591 */
1592static ssize_t bonding_show_resend_igmp(struct device *d,
Flavio Leitner94265cf2011-05-25 08:38:58 +00001593 struct device_attribute *attr,
1594 char *buf)
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001595{
1596 struct bonding *bond = to_bond(d);
1597
1598 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1599}
1600
1601static ssize_t bonding_store_resend_igmp(struct device *d,
Flavio Leitner94265cf2011-05-25 08:38:58 +00001602 struct device_attribute *attr,
1603 const char *buf, size_t count)
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001604{
1605 int new_value, ret = count;
1606 struct bonding *bond = to_bond(d);
1607
1608 if (sscanf(buf, "%d", &new_value) != 1) {
1609 pr_err("%s: no resend_igmp value specified.\n",
1610 bond->dev->name);
1611 ret = -EINVAL;
1612 goto out;
1613 }
1614
Flavio Leitner94265cf2011-05-25 08:38:58 +00001615 if (new_value < 0 || new_value > 255) {
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001616 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1617 bond->dev->name, new_value);
1618 ret = -EINVAL;
1619 goto out;
1620 }
1621
1622 pr_info("%s: Setting resend_igmp to %d.\n",
1623 bond->dev->name, new_value);
1624 bond->params.resend_igmp = new_value;
1625out:
1626 return ret;
1627}
1628
1629static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1630 bonding_show_resend_igmp, bonding_store_resend_igmp);
1631
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001632static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001633 &dev_attr_slaves.attr,
1634 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001635 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001636 &dev_attr_arp_validate.attr,
1637 &dev_attr_arp_interval.attr,
1638 &dev_attr_arp_ip_target.attr,
1639 &dev_attr_downdelay.attr,
1640 &dev_attr_updelay.attr,
1641 &dev_attr_lacp_rate.attr,
Jay Vosburghfd989c82008-11-04 17:51:16 -08001642 &dev_attr_ad_select.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001643 &dev_attr_xmit_hash_policy.attr,
Ben Hutchingsad246c92011-04-26 15:25:52 +00001644 &dev_attr_num_grat_arp.attr,
1645 &dev_attr_num_unsol_na.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001646 &dev_attr_miimon.attr,
1647 &dev_attr_primary.attr,
Jiri Pirkoa5499522009-09-25 03:28:09 +00001648 &dev_attr_primary_reselect.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001649 &dev_attr_use_carrier.attr,
1650 &dev_attr_active_slave.attr,
1651 &dev_attr_mii_status.attr,
1652 &dev_attr_ad_aggregator.attr,
1653 &dev_attr_ad_num_ports.attr,
1654 &dev_attr_ad_actor_key.attr,
1655 &dev_attr_ad_partner_key.attr,
1656 &dev_attr_ad_partner_mac.attr,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001657 &dev_attr_queue_id.attr,
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001658 &dev_attr_all_slaves_active.attr,
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001659 &dev_attr_resend_igmp.attr,
stephen hemminger655f8912011-06-22 09:54:39 +00001660 &dev_attr_min_links.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001661 NULL,
1662};
1663
1664static struct attribute_group bonding_group = {
1665 .name = "bonding",
1666 .attrs = per_bond_attrs,
1667};
1668
1669/*
1670 * Initialize sysfs. This sets up the bonding_masters file in
1671 * /sys/class/net.
1672 */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001673int bond_create_sysfs(struct bond_net *bn)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001674{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001675 int ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001676
Eric W. Biederman4c224002011-10-12 21:56:25 +00001677 bn->class_attr_bonding_masters = class_attr_bonding_masters;
Eric W. Biederman01718e32011-10-21 22:43:07 +00001678 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
Eric W. Biederman4c224002011-10-12 21:56:25 +00001679
1680 ret = netdev_class_create_file(&bn->class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001681 /*
1682 * Permit multiple loads of the module by ignoring failures to
1683 * create the bonding_masters sysfs file. Bonding devices
1684 * created by second or subsequent loads of the module will
1685 * not be listed in, or controllable by, bonding_masters, but
1686 * will have the usual "bonding" sysfs directory.
1687 *
1688 * This is done to preserve backwards compatibility for
1689 * initscripts/sysconfig, which load bonding multiple times to
1690 * configure multiple bonding devices.
1691 */
1692 if (ret == -EEXIST) {
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001693 /* Is someone being kinky and naming a device bonding_master? */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001694 if (__dev_get_by_name(bn->net,
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001695 class_attr_bonding_masters.attr.name))
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001696 pr_err("network device named %s already exists in sysfs",
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001697 class_attr_bonding_masters.attr.name);
Stephen Hemminger130aa612009-06-11 05:46:04 -07001698 ret = 0;
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001699 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001700
1701 return ret;
1702
1703}
1704
1705/*
1706 * Remove /sys/class/net/bonding_masters.
1707 */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001708void bond_destroy_sysfs(struct bond_net *bn)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001709{
Eric W. Biederman4c224002011-10-12 21:56:25 +00001710 netdev_class_remove_file(&bn->class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001711}
1712
1713/*
1714 * Initialize sysfs for each bond. This sets up and registers
1715 * the 'bondctl' directory for each individual bond under /sys/class/net.
1716 */
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001717void bond_prepare_sysfs_group(struct bonding *bond)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001718{
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001719 bond->dev->sysfs_groups[0] = &bonding_group;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001720}
1721