blob: ece57f146a6069a1d3cf1d019913d53a97f50000 [file] [log] [blame]
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001/*
2 * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
Mitch Williamsb76cdba2005-11-09 10:36:41 -080021 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -080022
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Mitch Williamsb76cdba2005-11-09 10:36:41 -080025#include <linux/kernel.h>
26#include <linux/module.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080027#include <linux/device.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040028#include <linux/sched.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080029#include <linux/fs.h>
30#include <linux/types.h>
31#include <linux/string.h>
32#include <linux/netdevice.h>
33#include <linux/inetdevice.h>
34#include <linux/in.h>
35#include <linux/sysfs.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080036#include <linux/ctype.h>
37#include <linux/inet.h>
38#include <linux/rtnetlink.h>
Stephen Hemminger5c5129b2009-06-12 19:02:51 +000039#include <linux/etherdevice.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070040#include <net/net_namespace.h>
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000041#include <net/netns/generic.h>
42#include <linux/nsproxy.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080043
Mitch Williamsb76cdba2005-11-09 10:36:41 -080044#include "bonding.h"
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -080045
Stephen Hemminger3d632c32009-06-12 19:02:48 +000046#define to_dev(obj) container_of(obj, struct device, kobj)
Wang Chen454d7c92008-11-12 23:37:49 -080047#define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
Mitch Williamsb76cdba2005-11-09 10:36:41 -080048
Mitch Williamsb76cdba2005-11-09 10:36:41 -080049/*
50 * "show" function for the bond_masters attribute.
51 * The class parameter is ignored.
52 */
Andi Kleen28812fe2010-01-05 12:48:07 +010053static ssize_t bonding_show_bonds(struct class *cls,
54 struct class_attribute *attr,
55 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080056{
Eric W. Biederman4c224002011-10-12 21:56:25 +000057 struct bond_net *bn =
58 container_of(attr, struct bond_net, class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080059 int res = 0;
60 struct bonding *bond;
61
Stephen Hemminger7e083842009-06-12 19:02:46 +000062 rtnl_lock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080063
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000064 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -080065 if (res > (PAGE_SIZE - IFNAMSIZ)) {
66 /* not enough space for another interface name */
67 if ((PAGE_SIZE - res) > 10)
68 res = PAGE_SIZE - 10;
Wagner Ferencb8843662007-12-06 23:40:30 -080069 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -080070 break;
71 }
Wagner Ferencb8843662007-12-06 23:40:30 -080072 res += sprintf(buf + res, "%s ", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080073 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -080074 if (res)
75 buf[res-1] = '\n'; /* eat the leftover space */
Stephen Hemminger7e083842009-06-12 19:02:46 +000076
77 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080078 return res;
79}
80
Eric W. Biederman4c224002011-10-12 21:56:25 +000081static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
Stephen Hemminger373500d2009-06-12 19:02:50 +000082{
83 struct bonding *bond;
84
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000085 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Stephen Hemminger373500d2009-06-12 19:02:50 +000086 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
87 return bond->dev;
88 }
89 return NULL;
90}
91
Mitch Williamsb76cdba2005-11-09 10:36:41 -080092/*
93 * "store" function for the bond_masters attribute. This is what
94 * creates and deletes entire bonds.
95 *
96 * The class parameter is ignored.
97 *
98 */
99
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000100static ssize_t bonding_store_bonds(struct class *cls,
Andi Kleen28812fe2010-01-05 12:48:07 +0100101 struct class_attribute *attr,
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000102 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800103{
Eric W. Biederman4c224002011-10-12 21:56:25 +0000104 struct bond_net *bn =
105 container_of(attr, struct bond_net, class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800106 char command[IFNAMSIZ + 1] = {0, };
107 char *ifname;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800108 int rv, res = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800109
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800110 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
111 ifname = command + 1;
112 if ((strlen(command) <= 1) ||
113 !dev_valid_name(ifname))
114 goto err_no_cmd;
115
116 if (command[0] == '+') {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800117 pr_info("%s is being created...\n", ifname);
Eric W. Biederman4c224002011-10-12 21:56:25 +0000118 rv = bond_create(bn->net, ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800119 if (rv) {
Phil Oester5f86cad12011-03-14 06:22:06 +0000120 if (rv == -EEXIST)
121 pr_info("%s already exists.\n", ifname);
122 else
123 pr_info("%s creation failed.\n", ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800124 res = rv;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800125 }
Stephen Hemminger373500d2009-06-12 19:02:50 +0000126 } else if (command[0] == '-') {
127 struct net_device *bond_dev;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800128
Jay Vosburgh027ea042008-01-17 16:25:02 -0800129 rtnl_lock();
Eric W. Biederman4c224002011-10-12 21:56:25 +0000130 bond_dev = bond_get_by_name(bn, ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000131 if (bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800132 pr_info("%s is being deleted...\n", ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000133 unregister_netdevice(bond_dev);
134 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800135 pr_err("unable to delete non-existent %s\n", ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000136 res = -ENODEV;
137 }
138 rtnl_unlock();
139 } else
140 goto err_no_cmd;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800141
Stephen Hemminger373500d2009-06-12 19:02:50 +0000142 /* Always return either count or an error. If you return 0, you'll
143 * get called forever, which is bad.
144 */
145 return res;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800146
147err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800148 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700149 return -EPERM;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800150}
Stephen Hemminger373500d2009-06-12 19:02:50 +0000151
Eric W. Biederman4c224002011-10-12 21:56:25 +0000152static const void *bonding_namespace(struct class *cls,
153 const struct class_attribute *attr)
154{
155 const struct bond_net *bn =
156 container_of(attr, struct bond_net, class_attr_bonding_masters);
157 return bn->net;
158}
159
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800160/* class attribute for bond_masters file. This ends up in /sys/class/net */
Eric W. Biederman4c224002011-10-12 21:56:25 +0000161static const struct class_attribute class_attr_bonding_masters = {
162 .attr = {
163 .name = "bonding_masters",
164 .mode = S_IWUSR | S_IRUGO,
165 },
166 .show = bonding_show_bonds,
167 .store = bonding_store_bonds,
168 .namespace = bonding_namespace,
169};
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800170
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000171int bond_create_slave_symlinks(struct net_device *master,
172 struct net_device *slave)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800173{
174 char linkname[IFNAMSIZ+7];
175 int ret = 0;
176
177 /* first, create a link from the slave back to the master */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700178 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800179 "master");
180 if (ret)
181 return ret;
182 /* next, create a link from the master to the slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000183 sprintf(linkname, "slave_%s", slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700184 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800185 linkname);
Veaceslav Falico9fe16b72013-03-26 17:43:28 +0100186
187 /* free the master link created earlier in case of error */
188 if (ret)
189 sysfs_remove_link(&(slave->dev.kobj), "master");
190
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800191 return ret;
192
193}
194
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000195void bond_destroy_slave_symlinks(struct net_device *master,
196 struct net_device *slave)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800197{
198 char linkname[IFNAMSIZ+7];
199
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700200 sysfs_remove_link(&(slave->dev.kobj), "master");
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000201 sprintf(linkname, "slave_%s", slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700202 sysfs_remove_link(&(master->dev.kobj), linkname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800203}
204
205
206/*
207 * Show the slaves in the current bond.
208 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700209static ssize_t bonding_show_slaves(struct device *d,
210 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800211{
212 struct slave *slave;
213 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700214 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800215
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700216 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800217 bond_for_each_slave(bond, slave, i) {
218 if (res > (PAGE_SIZE - IFNAMSIZ)) {
219 /* not enough space for another interface name */
220 if ((PAGE_SIZE - res) > 10)
221 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800222 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800223 break;
224 }
225 res += sprintf(buf + res, "%s ", slave->dev->name);
226 }
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700227 read_unlock(&bond->lock);
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800228 if (res)
229 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800230 return res;
231}
232
233/*
Veaceslav Falicod6641cc2013-05-28 01:26:13 +0000234 * Set the slaves in the current bond.
Jiri Pirkof9f35452010-05-18 05:46:39 +0000235 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
236 * All hard work should be done there.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800237 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700238static ssize_t bonding_store_slaves(struct device *d,
239 struct device_attribute *attr,
240 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800241{
242 char command[IFNAMSIZ + 1] = { 0, };
243 char *ifname;
Jiri Pirkof9f35452010-05-18 05:46:39 +0000244 int res, ret = count;
245 struct net_device *dev;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700246 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800247
Eric W. Biederman496a60c2009-05-13 17:02:50 +0000248 if (!rtnl_trylock())
249 return restart_syscall();
Jay Vosburgh027ea042008-01-17 16:25:02 -0800250
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800251 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
252 ifname = command + 1;
253 if ((strlen(command) <= 1) ||
254 !dev_valid_name(ifname))
255 goto err_no_cmd;
256
Jiri Pirkof9f35452010-05-18 05:46:39 +0000257 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
258 if (!dev) {
259 pr_info("%s: Interface %s does not exist!\n",
260 bond->dev->name, ifname);
261 ret = -ENODEV;
262 goto out;
263 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800264
Jiri Pirkof9f35452010-05-18 05:46:39 +0000265 switch (command[0]) {
266 case '+':
267 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800268 res = bond_enslave(bond->dev, dev);
Jiri Pirkof9f35452010-05-18 05:46:39 +0000269 break;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000270
Jiri Pirkof9f35452010-05-18 05:46:39 +0000271 case '-':
272 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
273 res = bond_release(bond->dev, dev);
274 break;
275
276 default:
277 goto err_no_cmd;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800278 }
279
Jiri Pirkof9f35452010-05-18 05:46:39 +0000280 if (res)
281 ret = res;
282 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800283
284err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800285 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
286 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800287 ret = -EPERM;
288
289out:
Jay Vosburgh027ea042008-01-17 16:25:02 -0800290 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800291 return ret;
292}
293
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000294static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
295 bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800296
297/*
298 * Show and set the bonding mode. The bond interface must be down to
299 * change the mode.
300 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700301static ssize_t bonding_show_mode(struct device *d,
302 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800303{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700304 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800305
306 return sprintf(buf, "%s %d\n",
307 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800308 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800309}
310
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700311static ssize_t bonding_store_mode(struct device *d,
312 struct device_attribute *attr,
313 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800314{
315 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700316 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800317
nikolay@redhat.comea6836d2013-05-18 01:18:28 +0000318 if (!rtnl_trylock())
319 return restart_syscall();
320
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800321 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800322 pr_err("unable to update mode of %s because interface is up.\n",
323 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800324 ret = -EPERM;
325 goto out;
326 }
327
Veaceslav Falico4a8bb7e2011-11-15 06:44:42 +0000328 if (bond->slave_cnt > 0) {
329 pr_err("unable to update mode of %s because it has slaves.\n",
330 bond->dev->name);
331 ret = -EPERM;
332 goto out;
333 }
334
Jay Vosburghece95f72008-01-17 16:25:01 -0800335 new_value = bond_parse_parm(buf, bond_mode_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800336 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800337 pr_err("%s: Ignoring invalid mode value %.*s.\n",
338 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800339 ret = -EINVAL;
340 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800341 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000342 if ((new_value == BOND_MODE_ALB ||
343 new_value == BOND_MODE_TLB) &&
344 bond->params.arp_interval) {
345 pr_err("%s: %s mode is incompatible with arp monitoring.\n",
346 bond->dev->name, bond_mode_tbl[new_value].modename);
347 ret = -EINVAL;
348 goto out;
349 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000350
351 bond->params.mode = new_value;
352 bond_set_mode_ops(bond, bond->params.mode);
353 pr_info("%s: setting mode to %s (%d).\n",
354 bond->dev->name, bond_mode_tbl[new_value].modename,
355 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800356out:
nikolay@redhat.comea6836d2013-05-18 01:18:28 +0000357 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800358 return ret;
359}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000360static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
361 bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800362
363/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000364 * Show and set the bonding transmit hash method.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800365 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700366static ssize_t bonding_show_xmit_hash(struct device *d,
367 struct device_attribute *attr,
368 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800369{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700370 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800371
Wagner Ferenc8e4b9322007-12-06 23:40:32 -0800372 return sprintf(buf, "%s %d\n",
373 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
374 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800375}
376
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700377static ssize_t bonding_store_xmit_hash(struct device *d,
378 struct device_attribute *attr,
379 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800380{
381 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700382 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800383
Jay Vosburghece95f72008-01-17 16:25:01 -0800384 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800385 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800386 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800387 bond->dev->name,
388 (int)strlen(buf) - 1, buf);
389 ret = -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800390 } else {
391 bond->params.xmit_policy = new_value;
392 bond_set_mode_ops(bond, bond->params.mode);
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800393 pr_info("%s: setting xmit hash policy to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000394 bond->dev->name,
395 xmit_hashtype_tbl[new_value].modename, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800396 }
Nikolay Aleksandrov53edee22013-05-24 00:59:47 +0000397
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800398 return ret;
399}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000400static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
401 bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800402
403/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700404 * Show and set arp_validate.
405 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700406static ssize_t bonding_show_arp_validate(struct device *d,
407 struct device_attribute *attr,
408 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700409{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700410 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700411
412 return sprintf(buf, "%s %d\n",
413 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800414 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700415}
416
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700417static ssize_t bonding_store_arp_validate(struct device *d,
418 struct device_attribute *attr,
419 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700420{
421 int new_value;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700422 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700423
Jay Vosburghece95f72008-01-17 16:25:01 -0800424 new_value = bond_parse_parm(buf, arp_validate_tbl);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700425 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800426 pr_err("%s: Ignoring invalid arp_validate value %s\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700427 bond->dev->name, buf);
428 return -EINVAL;
429 }
430 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800431 pr_err("%s: arp_validate only supported in active-backup mode.\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700432 bond->dev->name);
433 return -EINVAL;
434 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800435 pr_info("%s: setting arp_validate to %s (%d).\n",
436 bond->dev->name, arp_validate_tbl[new_value].modename,
437 new_value);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700438
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700439 bond->params.arp_validate = new_value;
440
441 return count;
442}
443
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000444static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
445 bonding_store_arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700446
447/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700448 * Show and store fail_over_mac. User only allowed to change the
449 * value when there are no slaves.
450 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000451static ssize_t bonding_show_fail_over_mac(struct device *d,
452 struct device_attribute *attr,
453 char *buf)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700454{
455 struct bonding *bond = to_bond(d);
456
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700457 return sprintf(buf, "%s %d\n",
458 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
459 bond->params.fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700460}
461
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000462static ssize_t bonding_store_fail_over_mac(struct device *d,
463 struct device_attribute *attr,
464 const char *buf, size_t count)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700465{
466 int new_value;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700467 struct bonding *bond = to_bond(d);
468
469 if (bond->slave_cnt != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800470 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
Jay Vosburghdd957c52007-10-09 19:57:24 -0700471 bond->dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700472 return -EPERM;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700473 }
474
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700475 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
476 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800477 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700478 bond->dev->name, buf);
479 return -EINVAL;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700480 }
481
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700482 bond->params.fail_over_mac = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800483 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
484 bond->dev->name, fail_over_mac_tbl[new_value].modename,
485 new_value);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700486
487 return count;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700488}
489
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000490static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
491 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700492
493/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800494 * Show and set the arp timer interval. There are two tricky bits
495 * here. First, if ARP monitoring is activated, then we must disable
496 * MII monitoring. Second, if the ARP timer isn't running, we must
497 * start it.
498 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700499static ssize_t bonding_show_arp_interval(struct device *d,
500 struct device_attribute *attr,
501 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800502{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700503 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800504
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800505 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800506}
507
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700508static ssize_t bonding_store_arp_interval(struct device *d,
509 struct device_attribute *attr,
510 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800511{
512 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700513 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800514
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000515 if (!rtnl_trylock())
516 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800517 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800518 pr_err("%s: no arp_interval value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800519 bond->dev->name);
520 ret = -EINVAL;
521 goto out;
522 }
523 if (new_value < 0) {
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000524 pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800525 bond->dev->name, new_value, INT_MAX);
526 ret = -EINVAL;
527 goto out;
528 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000529 if (bond->params.mode == BOND_MODE_ALB ||
530 bond->params.mode == BOND_MODE_TLB) {
531 pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
532 bond->dev->name, bond->dev->name);
533 ret = -EINVAL;
534 goto out;
535 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800536 pr_info("%s: Setting ARP monitoring interval to %d.\n",
537 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800538 bond->params.arp_interval = new_value;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000539 if (new_value) {
540 if (bond->params.miimon) {
541 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
542 bond->dev->name, bond->dev->name);
543 bond->params.miimon = 0;
544 }
545 if (!bond->params.arp_targets[0])
546 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
547 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800548 }
549 if (bond->dev->flags & IFF_UP) {
550 /* If the interface is up, we may need to fire off
551 * the ARP timer. If the interface is down, the
552 * timer will get fired off when the open function
553 * is called.
554 */
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000555 if (!new_value) {
556 cancel_delayed_work_sync(&bond->arp_work);
557 } else {
558 cancel_delayed_work_sync(&bond->mii_work);
559 queue_delayed_work(bond->wq, &bond->arp_work, 0);
560 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800561 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800562out:
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000563 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800564 return ret;
565}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000566static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
567 bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800568
569/*
570 * Show and set the arp targets.
571 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700572static ssize_t bonding_show_arp_targets(struct device *d,
573 struct device_attribute *attr,
574 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800575{
576 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700577 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800578
579 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
580 if (bond->params.arp_targets[i])
Harvey Harrison63779432008-10-31 00:56:00 -0700581 res += sprintf(buf + res, "%pI4 ",
582 &bond->params.arp_targets[i]);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800583 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800584 if (res)
585 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800586 return res;
587}
588
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700589static ssize_t bonding_store_arp_targets(struct device *d,
590 struct device_attribute *attr,
591 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800592{
Al Virod3bb52b2007-08-22 20:06:58 -0400593 __be32 newtarget;
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200594 int i = 0, ret = -EINVAL;
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 goto out;
606 }
607
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200608 if (bond_get_targets_ip(targets, newtarget) != -1) { /* dup */
609 pr_err("%s: ARP target %pI4 is already present\n",
610 bond->dev->name, &newtarget);
611 goto out;
612 }
613
614 i = bond_get_targets_ip(targets, 0); /* first free slot */
615 if (i == -1) {
616 pr_err("%s: ARP target table is full!\n",
617 bond->dev->name);
618 goto out;
619 }
620
621 pr_info("%s: adding ARP target %pI4.\n", bond->dev->name,
622 &newtarget);
623 targets[i] = newtarget;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000624 } else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400625 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800626 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700627 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800628 goto out;
629 }
630
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200631 i = bond_get_targets_ip(targets, newtarget);
632 if (i == -1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800633 pr_info("%s: unable to remove nonexistent ARP target %pI4.\n",
634 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800635 goto out;
636 }
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200637
638 pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
639 &newtarget);
640 for (; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
641 targets[i] = targets[i+1];
642 targets[i] = 0;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000643 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800644 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
645 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800646 ret = -EPERM;
647 goto out;
648 }
649
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200650 ret = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800651out:
652 return ret;
653}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700654static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800655
656/*
657 * Show and set the up and down delays. These must be multiples of the
658 * MII monitoring value, and are stored internally as the multiplier.
659 * Thus, we must translate to MS for the real world.
660 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700661static ssize_t bonding_show_downdelay(struct device *d,
662 struct device_attribute *attr,
663 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800664{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700665 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800666
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800667 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800668}
669
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700670static ssize_t bonding_store_downdelay(struct device *d,
671 struct device_attribute *attr,
672 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800673{
674 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700675 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800676
677 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800678 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800679 bond->dev->name);
680 ret = -EPERM;
681 goto out;
682 }
683
684 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800685 pr_err("%s: no down delay value specified.\n", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800686 ret = -EINVAL;
687 goto out;
688 }
689 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800690 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000691 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800692 ret = -EINVAL;
693 goto out;
694 } else {
695 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800696 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 +0000697 bond->dev->name, new_value,
698 bond->params.miimon,
699 (new_value / bond->params.miimon) *
700 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800701 }
702 bond->params.downdelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800703 pr_info("%s: Setting down delay to %d.\n",
704 bond->dev->name,
705 bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800706
707 }
708
709out:
710 return ret;
711}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000712static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
713 bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800714
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700715static ssize_t bonding_show_updelay(struct device *d,
716 struct device_attribute *attr,
717 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800718{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700719 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800720
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800721 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800722
723}
724
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700725static ssize_t bonding_store_updelay(struct device *d,
726 struct device_attribute *attr,
727 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800728{
729 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700730 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800731
732 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800733 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800734 bond->dev->name);
735 ret = -EPERM;
736 goto out;
737 }
738
739 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800740 pr_err("%s: no up delay value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800741 bond->dev->name);
742 ret = -EINVAL;
743 goto out;
744 }
745 if (new_value < 0) {
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000746 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
747 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800748 ret = -EINVAL;
749 goto out;
750 } else {
751 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800752 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 +0000753 bond->dev->name, new_value,
754 bond->params.miimon,
755 (new_value / bond->params.miimon) *
756 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800757 }
758 bond->params.updelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800759 pr_info("%s: Setting up delay to %d.\n",
760 bond->dev->name,
761 bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800762 }
763
764out:
765 return ret;
766}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000767static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
768 bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800769
770/*
771 * Show and set the LACP interval. Interface must be down, and the mode
772 * must be set to 802.3ad mode.
773 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700774static ssize_t bonding_show_lacp(struct device *d,
775 struct device_attribute *attr,
776 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800777{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700778 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800779
780 return sprintf(buf, "%s %d\n",
781 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800782 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800783}
784
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700785static ssize_t bonding_store_lacp(struct device *d,
786 struct device_attribute *attr,
787 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800788{
789 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700790 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800791
792 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800793 pr_err("%s: Unable to update LACP rate because interface is up.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800794 bond->dev->name);
795 ret = -EPERM;
796 goto out;
797 }
798
799 if (bond->params.mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800800 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800801 bond->dev->name);
802 ret = -EPERM;
803 goto out;
804 }
805
Jay Vosburghece95f72008-01-17 16:25:01 -0800806 new_value = bond_parse_parm(buf, bond_lacp_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800807
808 if ((new_value == 1) || (new_value == 0)) {
809 bond->params.lacp_fast = new_value;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +0000810 bond_3ad_update_lacp_rate(bond);
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800811 pr_info("%s: Setting LACP rate to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000812 bond->dev->name, bond_lacp_tbl[new_value].modename,
813 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800814 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800815 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000816 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800817 ret = -EINVAL;
818 }
819out:
820 return ret;
821}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000822static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
823 bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800824
stephen hemminger655f8912011-06-22 09:54:39 +0000825static ssize_t bonding_show_min_links(struct device *d,
826 struct device_attribute *attr,
827 char *buf)
828{
829 struct bonding *bond = to_bond(d);
830
831 return sprintf(buf, "%d\n", bond->params.min_links);
832}
833
834static ssize_t bonding_store_min_links(struct device *d,
835 struct device_attribute *attr,
836 const char *buf, size_t count)
837{
838 struct bonding *bond = to_bond(d);
839 int ret;
840 unsigned int new_value;
841
842 ret = kstrtouint(buf, 0, &new_value);
843 if (ret < 0) {
844 pr_err("%s: Ignoring invalid min links value %s.\n",
845 bond->dev->name, buf);
846 return ret;
847 }
848
849 pr_info("%s: Setting min links value to %u\n",
850 bond->dev->name, new_value);
851 bond->params.min_links = new_value;
852 return count;
853}
854static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
855 bonding_show_min_links, bonding_store_min_links);
856
Jay Vosburghfd989c82008-11-04 17:51:16 -0800857static ssize_t bonding_show_ad_select(struct device *d,
858 struct device_attribute *attr,
859 char *buf)
860{
861 struct bonding *bond = to_bond(d);
862
863 return sprintf(buf, "%s %d\n",
864 ad_select_tbl[bond->params.ad_select].modename,
865 bond->params.ad_select);
866}
867
868
869static ssize_t bonding_store_ad_select(struct device *d,
870 struct device_attribute *attr,
871 const char *buf, size_t count)
872{
873 int new_value, ret = count;
874 struct bonding *bond = to_bond(d);
875
876 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800877 pr_err("%s: Unable to update ad_select because interface is up.\n",
878 bond->dev->name);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800879 ret = -EPERM;
880 goto out;
881 }
882
883 new_value = bond_parse_parm(buf, ad_select_tbl);
884
885 if (new_value != -1) {
886 bond->params.ad_select = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800887 pr_info("%s: Setting ad_select to %s (%d).\n",
888 bond->dev->name, ad_select_tbl[new_value].modename,
889 new_value);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800890 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800891 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -0800892 bond->dev->name, (int)strlen(buf) - 1, buf);
893 ret = -EINVAL;
894 }
895out:
896 return ret;
897}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000898static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
899 bonding_show_ad_select, bonding_store_ad_select);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800900
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800901/*
Ben Hutchingsad246c92011-04-26 15:25:52 +0000902 * Show and set the number of peer notifications to send after a failover event.
903 */
904static ssize_t bonding_show_num_peer_notif(struct device *d,
905 struct device_attribute *attr,
906 char *buf)
907{
908 struct bonding *bond = to_bond(d);
909 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
910}
911
912static ssize_t bonding_store_num_peer_notif(struct device *d,
913 struct device_attribute *attr,
914 const char *buf, size_t count)
915{
916 struct bonding *bond = to_bond(d);
917 int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
918 return err ? err : count;
919}
920static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
921 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
922static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
923 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
924
925/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800926 * Show and set the MII monitor interval. There are two tricky bits
927 * here. First, if MII monitoring is activated, then we must disable
928 * ARP monitoring. Second, if the timer isn't running, we must
929 * start it.
930 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700931static ssize_t bonding_show_miimon(struct device *d,
932 struct device_attribute *attr,
933 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800934{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700935 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800936
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800937 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800938}
939
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700940static ssize_t bonding_store_miimon(struct device *d,
941 struct device_attribute *attr,
942 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800943{
944 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700945 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800946
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000947 if (!rtnl_trylock())
948 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800949 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800950 pr_err("%s: no miimon value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800951 bond->dev->name);
952 ret = -EINVAL;
953 goto out;
954 }
955 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800956 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000957 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800958 ret = -EINVAL;
959 goto out;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000960 }
961 pr_info("%s: Setting MII monitoring interval to %d.\n",
962 bond->dev->name, new_value);
963 bond->params.miimon = new_value;
964 if (bond->params.updelay)
965 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
966 bond->dev->name,
967 bond->params.updelay * bond->params.miimon);
968 if (bond->params.downdelay)
969 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
970 bond->dev->name,
971 bond->params.downdelay * bond->params.miimon);
972 if (new_value && bond->params.arp_interval) {
973 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
974 bond->dev->name);
975 bond->params.arp_interval = 0;
976 if (bond->params.arp_validate)
977 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
978 }
979 if (bond->dev->flags & IFF_UP) {
980 /* If the interface is up, we may need to fire off
981 * the MII timer. If the interface is down, the
982 * timer will get fired off when the open function
983 * is called.
984 */
985 if (!new_value) {
986 cancel_delayed_work_sync(&bond->mii_work);
987 } else {
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000988 cancel_delayed_work_sync(&bond->arp_work);
989 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800990 }
991 }
992out:
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000993 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800994 return ret;
995}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000996static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
997 bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800998
999/*
1000 * Show and set the primary slave. The store function is much
1001 * simpler than bonding_store_slaves function because it only needs to
1002 * handle one interface name.
1003 * The bond must be a mode that supports a primary for this be
1004 * set.
1005 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001006static ssize_t bonding_show_primary(struct device *d,
1007 struct device_attribute *attr,
1008 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001009{
1010 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001011 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001012
1013 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001014 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001015
1016 return count;
1017}
1018
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001019static ssize_t bonding_store_primary(struct device *d,
1020 struct device_attribute *attr,
1021 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001022{
1023 int i;
1024 struct slave *slave;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001025 struct bonding *bond = to_bond(d);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001026 char ifname[IFNAMSIZ];
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001027
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001028 if (!rtnl_trylock())
1029 return restart_syscall();
Neil Hormane843fa52010-10-13 16:01:50 +00001030 block_netpoll_tx();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001031 read_lock(&bond->lock);
1032 write_lock_bh(&bond->curr_slave_lock);
1033
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001034 if (!USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001035 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1036 bond->dev->name, bond->dev->name, bond->params.mode);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001037 goto out;
1038 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001039
nikolay@redhat.comeb6e98a2012-10-31 04:42:51 +00001040 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001041
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001042 /* check to see if we are clearing primary */
1043 if (!strlen(ifname) || buf[0] == '\n') {
1044 pr_info("%s: Setting primary slave to None.\n",
1045 bond->dev->name);
1046 bond->primary_slave = NULL;
Milos Vyleteleb492f72013-01-29 09:59:00 +00001047 memset(bond->params.primary, 0, sizeof(bond->params.primary));
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001048 bond_select_active_slave(bond);
1049 goto out;
1050 }
1051
1052 bond_for_each_slave(bond, slave, i) {
1053 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1054 pr_info("%s: Setting %s as primary slave.\n",
1055 bond->dev->name, slave->dev->name);
1056 bond->primary_slave = slave;
1057 strcpy(bond->params.primary, slave->dev->name);
1058 bond_select_active_slave(bond);
1059 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001060 }
1061 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001062
Weiping Pan8a936642012-06-10 23:00:20 +00001063 strncpy(bond->params.primary, ifname, IFNAMSIZ);
1064 bond->params.primary[IFNAMSIZ - 1] = 0;
1065
1066 pr_info("%s: Recording %s as primary, "
1067 "but it has not been enslaved to %s yet.\n",
1068 bond->dev->name, ifname, bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001069out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001070 write_unlock_bh(&bond->curr_slave_lock);
1071 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001072 unblock_netpoll_tx();
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001073 rtnl_unlock();
1074
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001075 return count;
1076}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001077static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1078 bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001079
1080/*
Jiri Pirkoa5499522009-09-25 03:28:09 +00001081 * Show and set the primary_reselect flag.
1082 */
1083static ssize_t bonding_show_primary_reselect(struct device *d,
1084 struct device_attribute *attr,
1085 char *buf)
1086{
1087 struct bonding *bond = to_bond(d);
1088
1089 return sprintf(buf, "%s %d\n",
1090 pri_reselect_tbl[bond->params.primary_reselect].modename,
1091 bond->params.primary_reselect);
1092}
1093
1094static ssize_t bonding_store_primary_reselect(struct device *d,
1095 struct device_attribute *attr,
1096 const char *buf, size_t count)
1097{
1098 int new_value, ret = count;
1099 struct bonding *bond = to_bond(d);
1100
1101 if (!rtnl_trylock())
1102 return restart_syscall();
1103
1104 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1105 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001106 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001107 bond->dev->name,
1108 (int) strlen(buf) - 1, buf);
1109 ret = -EINVAL;
1110 goto out;
1111 }
1112
1113 bond->params.primary_reselect = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001114 pr_info("%s: setting primary_reselect to %s (%d).\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001115 bond->dev->name, pri_reselect_tbl[new_value].modename,
1116 new_value);
1117
Neil Hormane843fa52010-10-13 16:01:50 +00001118 block_netpoll_tx();
Jiri Pirkoa5499522009-09-25 03:28:09 +00001119 read_lock(&bond->lock);
1120 write_lock_bh(&bond->curr_slave_lock);
1121 bond_select_active_slave(bond);
1122 write_unlock_bh(&bond->curr_slave_lock);
1123 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001124 unblock_netpoll_tx();
Jiri Pirkoa5499522009-09-25 03:28:09 +00001125out:
1126 rtnl_unlock();
1127 return ret;
1128}
1129static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1130 bonding_show_primary_reselect,
1131 bonding_store_primary_reselect);
1132
1133/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001134 * Show and set the use_carrier flag.
1135 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001136static ssize_t bonding_show_carrier(struct device *d,
1137 struct device_attribute *attr,
1138 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001139{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001140 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001141
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001142 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001143}
1144
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001145static ssize_t bonding_store_carrier(struct device *d,
1146 struct device_attribute *attr,
1147 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001148{
1149 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001150 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001151
1152
1153 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001154 pr_err("%s: no use_carrier value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001155 bond->dev->name);
1156 ret = -EINVAL;
1157 goto out;
1158 }
1159 if ((new_value == 0) || (new_value == 1)) {
1160 bond->params.use_carrier = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001161 pr_info("%s: Setting use_carrier to %d.\n",
1162 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001163 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001164 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1165 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001166 }
1167out:
Jiri Pirko672bda32011-01-25 11:03:25 +00001168 return ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001169}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001170static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1171 bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001172
1173
1174/*
1175 * Show and set currently active_slave.
1176 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001177static ssize_t bonding_show_active_slave(struct device *d,
1178 struct device_attribute *attr,
1179 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001180{
1181 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001182 struct bonding *bond = to_bond(d);
Wagner Ferenc16cd0162007-12-06 23:40:29 -08001183 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001184
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001185 read_lock(&bond->curr_slave_lock);
1186 curr = bond->curr_active_slave;
1187 read_unlock(&bond->curr_slave_lock);
1188
1189 if (USES_PRIMARY(bond->params.mode) && curr)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001190 count = sprintf(buf, "%s\n", curr->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001191 return count;
1192}
1193
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001194static ssize_t bonding_store_active_slave(struct device *d,
1195 struct device_attribute *attr,
1196 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001197{
1198 int i;
1199 struct slave *slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001200 struct slave *old_active = NULL;
1201 struct slave *new_active = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001202 struct bonding *bond = to_bond(d);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001203 char ifname[IFNAMSIZ];
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001204
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001205 if (!rtnl_trylock())
1206 return restart_syscall();
Neil Hormane843fa52010-10-13 16:01:50 +00001207
1208 block_netpoll_tx();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001209 read_lock(&bond->lock);
1210 write_lock_bh(&bond->curr_slave_lock);
Jay Vosburgh1466a212007-11-06 13:33:28 -08001211
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001212 if (!USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001213 pr_info("%s: Unable to change active slave; %s is in mode %d\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001214 bond->dev->name, bond->dev->name, bond->params.mode);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001215 goto out;
1216 }
1217
nikolay@redhat.comc84e1592012-10-31 06:03:52 +00001218 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001219
1220 /* check to see if we are clearing active */
1221 if (!strlen(ifname) || buf[0] == '\n') {
1222 pr_info("%s: Clearing current active slave.\n",
1223 bond->dev->name);
1224 bond->curr_active_slave = NULL;
1225 bond_select_active_slave(bond);
1226 goto out;
1227 }
1228
1229 bond_for_each_slave(bond, slave, i) {
1230 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1231 old_active = bond->curr_active_slave;
1232 new_active = slave;
1233 if (new_active == old_active) {
1234 /* do nothing */
1235 pr_info("%s: %s is already the current"
1236 " active slave.\n",
1237 bond->dev->name,
1238 slave->dev->name);
1239 goto out;
1240 }
1241 else {
1242 if ((new_active) &&
1243 (old_active) &&
1244 (new_active->link == BOND_LINK_UP) &&
1245 IS_UP(new_active->dev)) {
1246 pr_info("%s: Setting %s as active"
1247 " slave.\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001248 bond->dev->name,
1249 slave->dev->name);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001250 bond_change_active_slave(bond,
1251 new_active);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001252 }
1253 else {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001254 pr_info("%s: Could not set %s as"
1255 " active slave; either %s is"
1256 " down or the link is down.\n",
1257 bond->dev->name,
1258 slave->dev->name,
1259 slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001260 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001261 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001262 }
1263 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001264 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001265
1266 pr_info("%s: Unable to set %.*s as active slave.\n",
1267 bond->dev->name, (int)strlen(buf) - 1, buf);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001268 out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001269 write_unlock_bh(&bond->curr_slave_lock);
1270 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001271 unblock_netpoll_tx();
1272
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001273 rtnl_unlock();
1274
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001275 return count;
1276
1277}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001278static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1279 bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001280
1281
1282/*
1283 * Show link status of the bond interface.
1284 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001285static ssize_t bonding_show_mii_status(struct device *d,
1286 struct device_attribute *attr,
1287 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001288{
1289 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001290 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001291
1292 read_lock(&bond->curr_slave_lock);
1293 curr = bond->curr_active_slave;
1294 read_unlock(&bond->curr_slave_lock);
1295
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001296 return sprintf(buf, "%s\n", curr ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001297}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001298static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001299
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001300/*
1301 * Show current 802.3ad aggregator ID.
1302 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001303static ssize_t bonding_show_ad_aggregator(struct device *d,
1304 struct device_attribute *attr,
1305 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001306{
1307 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001308 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001309
1310 if (bond->params.mode == BOND_MODE_8023AD) {
1311 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001312 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001313 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001314 ? 0 : ad_info.aggregator_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001315 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001316
1317 return count;
1318}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001319static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001320
1321
1322/*
1323 * Show number of active 802.3ad ports.
1324 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001325static ssize_t bonding_show_ad_num_ports(struct device *d,
1326 struct device_attribute *attr,
1327 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001328{
1329 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001330 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001331
1332 if (bond->params.mode == BOND_MODE_8023AD) {
1333 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001334 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001335 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001336 ? 0 : ad_info.ports);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001337 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001338
1339 return count;
1340}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001341static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001342
1343
1344/*
1345 * Show current 802.3ad actor key.
1346 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001347static ssize_t bonding_show_ad_actor_key(struct device *d,
1348 struct device_attribute *attr,
1349 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001350{
1351 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001352 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001353
1354 if (bond->params.mode == BOND_MODE_8023AD) {
1355 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001356 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001357 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001358 ? 0 : ad_info.actor_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001359 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001360
1361 return count;
1362}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001363static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001364
1365
1366/*
1367 * Show current 802.3ad partner key.
1368 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001369static ssize_t bonding_show_ad_partner_key(struct device *d,
1370 struct device_attribute *attr,
1371 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001372{
1373 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001374 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001375
1376 if (bond->params.mode == BOND_MODE_8023AD) {
1377 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001378 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001379 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001380 ? 0 : ad_info.partner_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001381 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001382
1383 return count;
1384}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001385static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001386
1387
1388/*
1389 * Show current 802.3ad partner mac.
1390 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001391static ssize_t bonding_show_ad_partner_mac(struct device *d,
1392 struct device_attribute *attr,
1393 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001394{
1395 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001396 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001397
1398 if (bond->params.mode == BOND_MODE_8023AD) {
1399 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001400 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
Johannes Berge1749612008-10-27 15:59:26 -07001401 count = sprintf(buf, "%pM\n", ad_info.partner_system);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001402 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001403
1404 return count;
1405}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001406static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001407
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001408/*
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001409 * Show the queue_ids of the slaves in the current bond.
1410 */
1411static ssize_t bonding_show_queue_id(struct device *d,
1412 struct device_attribute *attr,
1413 char *buf)
1414{
1415 struct slave *slave;
1416 int i, res = 0;
1417 struct bonding *bond = to_bond(d);
1418
1419 if (!rtnl_trylock())
1420 return restart_syscall();
1421
1422 read_lock(&bond->lock);
1423 bond_for_each_slave(bond, slave, i) {
Nicolas de Pesloüan79236682010-07-14 18:24:54 -07001424 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1425 /* not enough space for another interface_name:queue_id pair */
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001426 if ((PAGE_SIZE - res) > 10)
1427 res = PAGE_SIZE - 10;
1428 res += sprintf(buf + res, "++more++ ");
1429 break;
1430 }
1431 res += sprintf(buf + res, "%s:%d ",
1432 slave->dev->name, slave->queue_id);
1433 }
1434 read_unlock(&bond->lock);
1435 if (res)
1436 buf[res-1] = '\n'; /* eat the leftover space */
1437 rtnl_unlock();
1438 return res;
1439}
1440
1441/*
1442 * Set the queue_ids of the slaves in the current bond. The bond
1443 * interface must be enslaved for this to work.
1444 */
1445static ssize_t bonding_store_queue_id(struct device *d,
1446 struct device_attribute *attr,
1447 const char *buffer, size_t count)
1448{
1449 struct slave *slave, *update_slave;
1450 struct bonding *bond = to_bond(d);
1451 u16 qid;
1452 int i, ret = count;
1453 char *delim;
1454 struct net_device *sdev = NULL;
1455
1456 if (!rtnl_trylock())
1457 return restart_syscall();
1458
1459 /* delim will point to queue id if successful */
1460 delim = strchr(buffer, ':');
1461 if (!delim)
1462 goto err_no_cmd;
1463
1464 /*
1465 * Terminate string that points to device name and bump it
1466 * up one, so we can read the queue id there.
1467 */
1468 *delim = '\0';
1469 if (sscanf(++delim, "%hd\n", &qid) != 1)
1470 goto err_no_cmd;
1471
1472 /* Check buffer length, valid ifname and queue id */
1473 if (strlen(buffer) > IFNAMSIZ ||
1474 !dev_valid_name(buffer) ||
Jiri Pirko8a540ff2012-07-20 02:28:50 +00001475 qid > bond->dev->real_num_tx_queues)
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001476 goto err_no_cmd;
1477
1478 /* Get the pointer to that interface if it exists */
1479 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1480 if (!sdev)
1481 goto err_no_cmd;
1482
1483 read_lock(&bond->lock);
1484
1485 /* Search for thes slave and check for duplicate qids */
1486 update_slave = NULL;
1487 bond_for_each_slave(bond, slave, i) {
1488 if (sdev == slave->dev)
1489 /*
1490 * We don't need to check the matching
1491 * slave for dups, since we're overwriting it
1492 */
1493 update_slave = slave;
1494 else if (qid && qid == slave->queue_id) {
1495 goto err_no_cmd_unlock;
1496 }
1497 }
1498
1499 if (!update_slave)
1500 goto err_no_cmd_unlock;
1501
1502 /* Actually set the qids for the slave */
1503 update_slave->queue_id = qid;
1504
1505 read_unlock(&bond->lock);
1506out:
1507 rtnl_unlock();
1508 return ret;
1509
1510err_no_cmd_unlock:
1511 read_unlock(&bond->lock);
1512err_no_cmd:
1513 pr_info("invalid input for queue_id set for %s.\n",
1514 bond->dev->name);
1515 ret = -EPERM;
1516 goto out;
1517}
1518
1519static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1520 bonding_store_queue_id);
1521
1522
1523/*
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001524 * Show and set the all_slaves_active flag.
1525 */
1526static ssize_t bonding_show_slaves_active(struct device *d,
1527 struct device_attribute *attr,
1528 char *buf)
1529{
1530 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001531
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001532 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1533}
1534
1535static ssize_t bonding_store_slaves_active(struct device *d,
1536 struct device_attribute *attr,
1537 const char *buf, size_t count)
1538{
1539 int i, new_value, ret = count;
1540 struct bonding *bond = to_bond(d);
1541 struct slave *slave;
1542
1543 if (sscanf(buf, "%d", &new_value) != 1) {
1544 pr_err("%s: no all_slaves_active value specified.\n",
1545 bond->dev->name);
1546 ret = -EINVAL;
1547 goto out;
1548 }
1549
1550 if (new_value == bond->params.all_slaves_active)
1551 goto out;
1552
1553 if ((new_value == 0) || (new_value == 1)) {
1554 bond->params.all_slaves_active = new_value;
1555 } else {
1556 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1557 bond->dev->name, new_value);
1558 ret = -EINVAL;
1559 goto out;
1560 }
1561
nikolay@redhat.come196c0e2012-11-29 01:37:59 +00001562 read_lock(&bond->lock);
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001563 bond_for_each_slave(bond, slave, i) {
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001564 if (!bond_is_active_slave(slave)) {
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001565 if (new_value)
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001566 slave->inactive = 0;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001567 else
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001568 slave->inactive = 1;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001569 }
1570 }
nikolay@redhat.come196c0e2012-11-29 01:37:59 +00001571 read_unlock(&bond->lock);
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001572out:
Jiri Pirko672bda32011-01-25 11:03:25 +00001573 return ret;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001574}
1575static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1576 bonding_show_slaves_active, bonding_store_slaves_active);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001577
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001578/*
1579 * Show and set the number of IGMP membership reports to send on link failure
1580 */
1581static ssize_t bonding_show_resend_igmp(struct device *d,
Flavio Leitner94265cf2011-05-25 08:38:58 +00001582 struct device_attribute *attr,
1583 char *buf)
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001584{
1585 struct bonding *bond = to_bond(d);
1586
1587 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1588}
1589
1590static ssize_t bonding_store_resend_igmp(struct device *d,
Flavio Leitner94265cf2011-05-25 08:38:58 +00001591 struct device_attribute *attr,
1592 const char *buf, size_t count)
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001593{
1594 int new_value, ret = count;
1595 struct bonding *bond = to_bond(d);
1596
1597 if (sscanf(buf, "%d", &new_value) != 1) {
1598 pr_err("%s: no resend_igmp value specified.\n",
1599 bond->dev->name);
1600 ret = -EINVAL;
1601 goto out;
1602 }
1603
Flavio Leitner94265cf2011-05-25 08:38:58 +00001604 if (new_value < 0 || new_value > 255) {
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001605 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1606 bond->dev->name, new_value);
1607 ret = -EINVAL;
1608 goto out;
1609 }
1610
1611 pr_info("%s: Setting resend_igmp to %d.\n",
1612 bond->dev->name, new_value);
1613 bond->params.resend_igmp = new_value;
1614out:
1615 return ret;
1616}
1617
1618static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1619 bonding_show_resend_igmp, bonding_store_resend_igmp);
1620
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001621static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001622 &dev_attr_slaves.attr,
1623 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001624 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001625 &dev_attr_arp_validate.attr,
1626 &dev_attr_arp_interval.attr,
1627 &dev_attr_arp_ip_target.attr,
1628 &dev_attr_downdelay.attr,
1629 &dev_attr_updelay.attr,
1630 &dev_attr_lacp_rate.attr,
Jay Vosburghfd989c82008-11-04 17:51:16 -08001631 &dev_attr_ad_select.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001632 &dev_attr_xmit_hash_policy.attr,
Ben Hutchingsad246c92011-04-26 15:25:52 +00001633 &dev_attr_num_grat_arp.attr,
1634 &dev_attr_num_unsol_na.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001635 &dev_attr_miimon.attr,
1636 &dev_attr_primary.attr,
Jiri Pirkoa5499522009-09-25 03:28:09 +00001637 &dev_attr_primary_reselect.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001638 &dev_attr_use_carrier.attr,
1639 &dev_attr_active_slave.attr,
1640 &dev_attr_mii_status.attr,
1641 &dev_attr_ad_aggregator.attr,
1642 &dev_attr_ad_num_ports.attr,
1643 &dev_attr_ad_actor_key.attr,
1644 &dev_attr_ad_partner_key.attr,
1645 &dev_attr_ad_partner_mac.attr,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001646 &dev_attr_queue_id.attr,
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001647 &dev_attr_all_slaves_active.attr,
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001648 &dev_attr_resend_igmp.attr,
stephen hemminger655f8912011-06-22 09:54:39 +00001649 &dev_attr_min_links.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001650 NULL,
1651};
1652
1653static struct attribute_group bonding_group = {
1654 .name = "bonding",
1655 .attrs = per_bond_attrs,
1656};
1657
1658/*
1659 * Initialize sysfs. This sets up the bonding_masters file in
1660 * /sys/class/net.
1661 */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001662int bond_create_sysfs(struct bond_net *bn)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001663{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001664 int ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001665
Eric W. Biederman4c224002011-10-12 21:56:25 +00001666 bn->class_attr_bonding_masters = class_attr_bonding_masters;
Eric W. Biederman01718e32011-10-21 22:43:07 +00001667 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
Eric W. Biederman4c224002011-10-12 21:56:25 +00001668
1669 ret = netdev_class_create_file(&bn->class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001670 /*
1671 * Permit multiple loads of the module by ignoring failures to
1672 * create the bonding_masters sysfs file. Bonding devices
1673 * created by second or subsequent loads of the module will
1674 * not be listed in, or controllable by, bonding_masters, but
1675 * will have the usual "bonding" sysfs directory.
1676 *
1677 * This is done to preserve backwards compatibility for
1678 * initscripts/sysconfig, which load bonding multiple times to
1679 * configure multiple bonding devices.
1680 */
1681 if (ret == -EEXIST) {
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001682 /* Is someone being kinky and naming a device bonding_master? */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001683 if (__dev_get_by_name(bn->net,
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001684 class_attr_bonding_masters.attr.name))
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001685 pr_err("network device named %s already exists in sysfs",
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001686 class_attr_bonding_masters.attr.name);
Stephen Hemminger130aa612009-06-11 05:46:04 -07001687 ret = 0;
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001688 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001689
1690 return ret;
1691
1692}
1693
1694/*
1695 * Remove /sys/class/net/bonding_masters.
1696 */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001697void bond_destroy_sysfs(struct bond_net *bn)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001698{
Eric W. Biederman4c224002011-10-12 21:56:25 +00001699 netdev_class_remove_file(&bn->class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001700}
1701
1702/*
1703 * Initialize sysfs for each bond. This sets up and registers
1704 * the 'bondctl' directory for each individual bond under /sys/class/net.
1705 */
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001706void bond_prepare_sysfs_group(struct bonding *bond)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001707{
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001708 bond->dev->sysfs_groups[0] = &bonding_group;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001709}
1710