blob: 04d95d6f6c63dcf44f2a16cb6072406ef2876fdb [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{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700212 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200213 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200214 struct slave *slave;
215 int res = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800216
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700217 read_lock(&bond->lock);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200218 bond_for_each_slave(bond, slave, iter) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800219 if (res > (PAGE_SIZE - IFNAMSIZ)) {
220 /* not enough space for another interface name */
221 if ((PAGE_SIZE - res) > 10)
222 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800223 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800224 break;
225 }
226 res += sprintf(buf + res, "%s ", slave->dev->name);
227 }
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700228 read_unlock(&bond->lock);
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800229 if (res)
230 buf[res-1] = '\n'; /* eat the leftover space */
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200231
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800232 return res;
233}
234
235/*
Veaceslav Falicod6641cc2013-05-28 01:26:13 +0000236 * Set the slaves in the current bond.
Jiri Pirkof9f35452010-05-18 05:46:39 +0000237 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
238 * All hard work should be done there.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800239 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700240static ssize_t bonding_store_slaves(struct device *d,
241 struct device_attribute *attr,
242 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800243{
244 char command[IFNAMSIZ + 1] = { 0, };
245 char *ifname;
Jiri Pirkof9f35452010-05-18 05:46:39 +0000246 int res, ret = count;
247 struct net_device *dev;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700248 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800249
Eric W. Biederman496a60c2009-05-13 17:02:50 +0000250 if (!rtnl_trylock())
251 return restart_syscall();
Jay Vosburgh027ea042008-01-17 16:25:02 -0800252
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800253 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
254 ifname = command + 1;
255 if ((strlen(command) <= 1) ||
256 !dev_valid_name(ifname))
257 goto err_no_cmd;
258
Jiri Pirkof9f35452010-05-18 05:46:39 +0000259 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
260 if (!dev) {
261 pr_info("%s: Interface %s does not exist!\n",
262 bond->dev->name, ifname);
263 ret = -ENODEV;
264 goto out;
265 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800266
Jiri Pirkof9f35452010-05-18 05:46:39 +0000267 switch (command[0]) {
268 case '+':
269 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800270 res = bond_enslave(bond->dev, dev);
Jiri Pirkof9f35452010-05-18 05:46:39 +0000271 break;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000272
Jiri Pirkof9f35452010-05-18 05:46:39 +0000273 case '-':
274 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
275 res = bond_release(bond->dev, dev);
276 break;
277
278 default:
279 goto err_no_cmd;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800280 }
281
Jiri Pirkof9f35452010-05-18 05:46:39 +0000282 if (res)
283 ret = res;
284 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800285
286err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800287 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
288 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800289 ret = -EPERM;
290
291out:
Jay Vosburgh027ea042008-01-17 16:25:02 -0800292 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800293 return ret;
294}
295
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000296static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
297 bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800298
299/*
300 * Show and set the bonding mode. The bond interface must be down to
301 * change the mode.
302 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700303static ssize_t bonding_show_mode(struct device *d,
304 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800305{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700306 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800307
308 return sprintf(buf, "%s %d\n",
309 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800310 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800311}
312
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700313static ssize_t bonding_store_mode(struct device *d,
314 struct device_attribute *attr,
315 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800316{
317 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700318 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800319
nikolay@redhat.comea6836d2013-05-18 01:18:28 +0000320 if (!rtnl_trylock())
321 return restart_syscall();
322
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800323 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800324 pr_err("unable to update mode of %s because interface is up.\n",
325 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800326 ret = -EPERM;
327 goto out;
328 }
329
Veaceslav Falico0965a1f2013-09-25 09:20:21 +0200330 if (bond_has_slaves(bond)) {
Veaceslav Falico4a8bb7e2011-11-15 06:44:42 +0000331 pr_err("unable to update mode of %s because it has slaves.\n",
332 bond->dev->name);
333 ret = -EPERM;
334 goto out;
335 }
336
Jay Vosburghece95f72008-01-17 16:25:01 -0800337 new_value = bond_parse_parm(buf, bond_mode_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800338 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800339 pr_err("%s: Ignoring invalid mode value %.*s.\n",
340 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800341 ret = -EINVAL;
342 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800343 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000344 if ((new_value == BOND_MODE_ALB ||
345 new_value == BOND_MODE_TLB) &&
346 bond->params.arp_interval) {
347 pr_err("%s: %s mode is incompatible with arp monitoring.\n",
348 bond->dev->name, bond_mode_tbl[new_value].modename);
349 ret = -EINVAL;
350 goto out;
351 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000352
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200353 /* don't cache arp_validate between modes */
354 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000355 bond->params.mode = new_value;
356 bond_set_mode_ops(bond, bond->params.mode);
357 pr_info("%s: setting mode to %s (%d).\n",
358 bond->dev->name, bond_mode_tbl[new_value].modename,
359 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800360out:
nikolay@redhat.comea6836d2013-05-18 01:18:28 +0000361 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800362 return ret;
363}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000364static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
365 bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800366
367/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000368 * Show and set the bonding transmit hash method.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800369 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700370static ssize_t bonding_show_xmit_hash(struct device *d,
371 struct device_attribute *attr,
372 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800373{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700374 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800375
Wagner Ferenc8e4b9322007-12-06 23:40:32 -0800376 return sprintf(buf, "%s %d\n",
377 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
378 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800379}
380
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700381static ssize_t bonding_store_xmit_hash(struct device *d,
382 struct device_attribute *attr,
383 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800384{
385 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700386 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800387
Jay Vosburghece95f72008-01-17 16:25:01 -0800388 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800389 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800390 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800391 bond->dev->name,
392 (int)strlen(buf) - 1, buf);
393 ret = -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800394 } else {
395 bond->params.xmit_policy = new_value;
396 bond_set_mode_ops(bond, bond->params.mode);
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800397 pr_info("%s: setting xmit hash policy to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000398 bond->dev->name,
399 xmit_hashtype_tbl[new_value].modename, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800400 }
Nikolay Aleksandrov53edee22013-05-24 00:59:47 +0000401
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800402 return ret;
403}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000404static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
405 bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800406
407/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700408 * Show and set arp_validate.
409 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700410static ssize_t bonding_show_arp_validate(struct device *d,
411 struct device_attribute *attr,
412 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700413{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700414 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700415
416 return sprintf(buf, "%s %d\n",
417 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800418 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700419}
420
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700421static ssize_t bonding_store_arp_validate(struct device *d,
422 struct device_attribute *attr,
423 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700424{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700425 struct bonding *bond = to_bond(d);
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200426 int new_value, ret = count;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700427
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200428 if (!rtnl_trylock())
429 return restart_syscall();
Jay Vosburghece95f72008-01-17 16:25:01 -0800430 new_value = bond_parse_parm(buf, arp_validate_tbl);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700431 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800432 pr_err("%s: Ignoring invalid arp_validate value %s\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700433 bond->dev->name, buf);
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200434 ret = -EINVAL;
435 goto out;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700436 }
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200437 if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800438 pr_err("%s: arp_validate only supported in active-backup mode.\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700439 bond->dev->name);
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200440 ret = -EINVAL;
441 goto out;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700442 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800443 pr_info("%s: setting arp_validate to %s (%d).\n",
444 bond->dev->name, arp_validate_tbl[new_value].modename,
445 new_value);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700446
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200447 if (bond->dev->flags & IFF_UP) {
448 if (!new_value)
449 bond->recv_probe = NULL;
450 else if (bond->params.arp_interval)
451 bond->recv_probe = bond_arp_rcv;
452 }
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700453 bond->params.arp_validate = new_value;
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200454out:
455 rtnl_unlock();
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700456
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200457 return ret;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700458}
459
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000460static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
461 bonding_store_arp_validate);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200462/*
463 * Show and set arp_all_targets.
464 */
465static ssize_t bonding_show_arp_all_targets(struct device *d,
466 struct device_attribute *attr,
467 char *buf)
468{
469 struct bonding *bond = to_bond(d);
470 int value = bond->params.arp_all_targets;
471
472 return sprintf(buf, "%s %d\n", arp_all_targets_tbl[value].modename,
473 value);
474}
475
476static ssize_t bonding_store_arp_all_targets(struct device *d,
477 struct device_attribute *attr,
478 const char *buf, size_t count)
479{
480 struct bonding *bond = to_bond(d);
481 int new_value;
482
483 new_value = bond_parse_parm(buf, arp_all_targets_tbl);
484 if (new_value < 0) {
485 pr_err("%s: Ignoring invalid arp_all_targets value %s\n",
486 bond->dev->name, buf);
487 return -EINVAL;
488 }
489 pr_info("%s: setting arp_all_targets to %s (%d).\n",
490 bond->dev->name, arp_all_targets_tbl[new_value].modename,
491 new_value);
492
493 bond->params.arp_all_targets = new_value;
494
495 return count;
496}
497
498static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
499 bonding_show_arp_all_targets, bonding_store_arp_all_targets);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700500
501/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700502 * Show and store fail_over_mac. User only allowed to change the
503 * value when there are no slaves.
504 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000505static ssize_t bonding_show_fail_over_mac(struct device *d,
506 struct device_attribute *attr,
507 char *buf)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700508{
509 struct bonding *bond = to_bond(d);
510
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700511 return sprintf(buf, "%s %d\n",
512 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
513 bond->params.fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700514}
515
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000516static ssize_t bonding_store_fail_over_mac(struct device *d,
517 struct device_attribute *attr,
518 const char *buf, size_t count)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700519{
dingtianhong9402b742013-07-23 15:25:39 +0800520 int new_value, ret = count;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700521 struct bonding *bond = to_bond(d);
522
dingtianhong9402b742013-07-23 15:25:39 +0800523 if (!rtnl_trylock())
524 return restart_syscall();
525
Veaceslav Falico0965a1f2013-09-25 09:20:21 +0200526 if (bond_has_slaves(bond)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800527 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
Jay Vosburghdd957c52007-10-09 19:57:24 -0700528 bond->dev->name);
dingtianhong9402b742013-07-23 15:25:39 +0800529 ret = -EPERM;
530 goto out;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700531 }
532
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700533 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
534 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800535 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700536 bond->dev->name, buf);
dingtianhong9402b742013-07-23 15:25:39 +0800537 ret = -EINVAL;
538 goto out;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700539 }
540
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700541 bond->params.fail_over_mac = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800542 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
543 bond->dev->name, fail_over_mac_tbl[new_value].modename,
544 new_value);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700545
dingtianhong9402b742013-07-23 15:25:39 +0800546out:
547 rtnl_unlock();
548 return ret;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700549}
550
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000551static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
552 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700553
554/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800555 * Show and set the arp timer interval. There are two tricky bits
556 * here. First, if ARP monitoring is activated, then we must disable
557 * MII monitoring. Second, if the ARP timer isn't running, we must
558 * start it.
559 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700560static ssize_t bonding_show_arp_interval(struct device *d,
561 struct device_attribute *attr,
562 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800563{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700564 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800565
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800566 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800567}
568
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700569static ssize_t bonding_store_arp_interval(struct device *d,
570 struct device_attribute *attr,
571 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800572{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700573 struct bonding *bond = to_bond(d);
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200574 int new_value, ret = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800575
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000576 if (!rtnl_trylock())
577 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800578 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800579 pr_err("%s: no arp_interval value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800580 bond->dev->name);
581 ret = -EINVAL;
582 goto out;
583 }
584 if (new_value < 0) {
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000585 pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800586 bond->dev->name, new_value, INT_MAX);
587 ret = -EINVAL;
588 goto out;
589 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000590 if (bond->params.mode == BOND_MODE_ALB ||
591 bond->params.mode == BOND_MODE_TLB) {
592 pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
593 bond->dev->name, bond->dev->name);
594 ret = -EINVAL;
595 goto out;
596 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800597 pr_info("%s: Setting ARP monitoring interval to %d.\n",
598 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800599 bond->params.arp_interval = new_value;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000600 if (new_value) {
601 if (bond->params.miimon) {
602 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
603 bond->dev->name, bond->dev->name);
604 bond->params.miimon = 0;
605 }
606 if (!bond->params.arp_targets[0])
607 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
608 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800609 }
610 if (bond->dev->flags & IFF_UP) {
611 /* If the interface is up, we may need to fire off
612 * the ARP timer. If the interface is down, the
613 * timer will get fired off when the open function
614 * is called.
615 */
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000616 if (!new_value) {
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200617 if (bond->params.arp_validate)
618 bond->recv_probe = NULL;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000619 cancel_delayed_work_sync(&bond->arp_work);
620 } else {
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200621 /* arp_validate can be set only in active-backup mode */
622 if (bond->params.arp_validate)
623 bond->recv_probe = bond_arp_rcv;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000624 cancel_delayed_work_sync(&bond->mii_work);
625 queue_delayed_work(bond->wq, &bond->arp_work, 0);
626 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800627 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800628out:
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000629 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800630 return ret;
631}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000632static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
633 bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800634
635/*
636 * Show and set the arp targets.
637 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700638static ssize_t bonding_show_arp_targets(struct device *d,
639 struct device_attribute *attr,
640 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800641{
642 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700643 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800644
645 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
646 if (bond->params.arp_targets[i])
Harvey Harrison63779432008-10-31 00:56:00 -0700647 res += sprintf(buf + res, "%pI4 ",
648 &bond->params.arp_targets[i]);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800649 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800650 if (res)
651 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800652 return res;
653}
654
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700655static ssize_t bonding_store_arp_targets(struct device *d,
656 struct device_attribute *attr,
657 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800658{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700659 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200660 struct list_head *iter;
Veaceslav Falico8599b522013-06-24 11:49:34 +0200661 struct slave *slave;
662 __be32 newtarget, *targets;
663 unsigned long *targets_rx;
664 int ind, i, j, ret = -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800665
666 targets = bond->params.arp_targets;
667 newtarget = in_aton(buf + 1);
668 /* look for adds */
669 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400670 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800671 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700672 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800673 goto out;
674 }
675
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200676 if (bond_get_targets_ip(targets, newtarget) != -1) { /* dup */
677 pr_err("%s: ARP target %pI4 is already present\n",
678 bond->dev->name, &newtarget);
679 goto out;
680 }
681
Veaceslav Falico8599b522013-06-24 11:49:34 +0200682 ind = bond_get_targets_ip(targets, 0); /* first free slot */
683 if (ind == -1) {
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200684 pr_err("%s: ARP target table is full!\n",
685 bond->dev->name);
686 goto out;
687 }
688
689 pr_info("%s: adding ARP target %pI4.\n", bond->dev->name,
690 &newtarget);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200691 /* not to race with bond_arp_rcv */
692 write_lock_bh(&bond->lock);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200693 bond_for_each_slave(bond, slave, iter)
Veaceslav Falico8599b522013-06-24 11:49:34 +0200694 slave->target_last_arp_rx[ind] = jiffies;
695 targets[ind] = newtarget;
696 write_unlock_bh(&bond->lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000697 } else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400698 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800699 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700700 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800701 goto out;
702 }
703
Veaceslav Falico8599b522013-06-24 11:49:34 +0200704 ind = bond_get_targets_ip(targets, newtarget);
705 if (ind == -1) {
706 pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800707 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800708 goto out;
709 }
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200710
Veaceslav Falico8599b522013-06-24 11:49:34 +0200711 if (ind == 0 && !targets[1] && bond->params.arp_interval)
712 pr_warn("%s: removing last arp target with arp_interval on\n",
713 bond->dev->name);
714
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200715 pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
716 &newtarget);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200717
718 write_lock_bh(&bond->lock);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200719 bond_for_each_slave(bond, slave, iter) {
Veaceslav Falico8599b522013-06-24 11:49:34 +0200720 targets_rx = slave->target_last_arp_rx;
721 j = ind;
722 for (; (j < BOND_MAX_ARP_TARGETS-1) && targets[j+1]; j++)
723 targets_rx[j] = targets_rx[j+1];
724 targets_rx[j] = 0;
725 }
726 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200727 targets[i] = targets[i+1];
728 targets[i] = 0;
Veaceslav Falico8599b522013-06-24 11:49:34 +0200729 write_unlock_bh(&bond->lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000730 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800731 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
732 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800733 ret = -EPERM;
734 goto out;
735 }
736
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200737 ret = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800738out:
739 return ret;
740}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700741static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800742
743/*
744 * Show and set the up and down delays. These must be multiples of the
745 * MII monitoring value, and are stored internally as the multiplier.
746 * Thus, we must translate to MS for the real world.
747 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700748static ssize_t bonding_show_downdelay(struct device *d,
749 struct device_attribute *attr,
750 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800751{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700752 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800753
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800754 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800755}
756
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700757static ssize_t bonding_store_downdelay(struct device *d,
758 struct device_attribute *attr,
759 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800760{
761 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700762 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800763
764 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800765 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800766 bond->dev->name);
767 ret = -EPERM;
768 goto out;
769 }
770
771 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800772 pr_err("%s: no down delay value specified.\n", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800773 ret = -EINVAL;
774 goto out;
775 }
776 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800777 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000778 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800779 ret = -EINVAL;
780 goto out;
781 } else {
782 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800783 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 +0000784 bond->dev->name, new_value,
785 bond->params.miimon,
786 (new_value / bond->params.miimon) *
787 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800788 }
789 bond->params.downdelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800790 pr_info("%s: Setting down delay to %d.\n",
791 bond->dev->name,
792 bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800793
794 }
795
796out:
797 return ret;
798}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000799static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
800 bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800801
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700802static ssize_t bonding_show_updelay(struct device *d,
803 struct device_attribute *attr,
804 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800805{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700806 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800807
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800808 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800809
810}
811
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700812static ssize_t bonding_store_updelay(struct device *d,
813 struct device_attribute *attr,
814 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800815{
816 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700817 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800818
819 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800820 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800821 bond->dev->name);
822 ret = -EPERM;
823 goto out;
824 }
825
826 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800827 pr_err("%s: no up delay value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800828 bond->dev->name);
829 ret = -EINVAL;
830 goto out;
831 }
832 if (new_value < 0) {
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000833 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
834 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800835 ret = -EINVAL;
836 goto out;
837 } else {
838 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800839 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 +0000840 bond->dev->name, new_value,
841 bond->params.miimon,
842 (new_value / bond->params.miimon) *
843 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800844 }
845 bond->params.updelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800846 pr_info("%s: Setting up delay to %d.\n",
847 bond->dev->name,
848 bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800849 }
850
851out:
852 return ret;
853}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000854static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
855 bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800856
857/*
858 * Show and set the LACP interval. Interface must be down, and the mode
859 * must be set to 802.3ad mode.
860 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700861static ssize_t bonding_show_lacp(struct device *d,
862 struct device_attribute *attr,
863 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800864{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700865 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800866
867 return sprintf(buf, "%s %d\n",
868 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800869 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800870}
871
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700872static ssize_t bonding_store_lacp(struct device *d,
873 struct device_attribute *attr,
874 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800875{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700876 struct bonding *bond = to_bond(d);
nikolay@redhat.comc5093162013-09-02 13:51:40 +0200877 int new_value, ret = count;
878
879 if (!rtnl_trylock())
880 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800881
882 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800883 pr_err("%s: Unable to update LACP rate because interface is up.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800884 bond->dev->name);
885 ret = -EPERM;
886 goto out;
887 }
888
889 if (bond->params.mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800890 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800891 bond->dev->name);
892 ret = -EPERM;
893 goto out;
894 }
895
Jay Vosburghece95f72008-01-17 16:25:01 -0800896 new_value = bond_parse_parm(buf, bond_lacp_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800897
898 if ((new_value == 1) || (new_value == 0)) {
899 bond->params.lacp_fast = new_value;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +0000900 bond_3ad_update_lacp_rate(bond);
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800901 pr_info("%s: Setting LACP rate to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000902 bond->dev->name, bond_lacp_tbl[new_value].modename,
903 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800904 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800905 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000906 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800907 ret = -EINVAL;
908 }
909out:
nikolay@redhat.comc5093162013-09-02 13:51:40 +0200910 rtnl_unlock();
911
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800912 return ret;
913}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000914static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
915 bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800916
stephen hemminger655f8912011-06-22 09:54:39 +0000917static ssize_t bonding_show_min_links(struct device *d,
918 struct device_attribute *attr,
919 char *buf)
920{
921 struct bonding *bond = to_bond(d);
922
923 return sprintf(buf, "%d\n", bond->params.min_links);
924}
925
926static ssize_t bonding_store_min_links(struct device *d,
927 struct device_attribute *attr,
928 const char *buf, size_t count)
929{
930 struct bonding *bond = to_bond(d);
931 int ret;
932 unsigned int new_value;
933
934 ret = kstrtouint(buf, 0, &new_value);
935 if (ret < 0) {
936 pr_err("%s: Ignoring invalid min links value %s.\n",
937 bond->dev->name, buf);
938 return ret;
939 }
940
941 pr_info("%s: Setting min links value to %u\n",
942 bond->dev->name, new_value);
943 bond->params.min_links = new_value;
944 return count;
945}
946static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
947 bonding_show_min_links, bonding_store_min_links);
948
Jay Vosburghfd989c82008-11-04 17:51:16 -0800949static ssize_t bonding_show_ad_select(struct device *d,
950 struct device_attribute *attr,
951 char *buf)
952{
953 struct bonding *bond = to_bond(d);
954
955 return sprintf(buf, "%s %d\n",
956 ad_select_tbl[bond->params.ad_select].modename,
957 bond->params.ad_select);
958}
959
960
961static ssize_t bonding_store_ad_select(struct device *d,
962 struct device_attribute *attr,
963 const char *buf, size_t count)
964{
965 int new_value, ret = count;
966 struct bonding *bond = to_bond(d);
967
968 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800969 pr_err("%s: Unable to update ad_select because interface is up.\n",
970 bond->dev->name);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800971 ret = -EPERM;
972 goto out;
973 }
974
975 new_value = bond_parse_parm(buf, ad_select_tbl);
976
977 if (new_value != -1) {
978 bond->params.ad_select = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800979 pr_info("%s: Setting ad_select to %s (%d).\n",
980 bond->dev->name, ad_select_tbl[new_value].modename,
981 new_value);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800982 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800983 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -0800984 bond->dev->name, (int)strlen(buf) - 1, buf);
985 ret = -EINVAL;
986 }
987out:
988 return ret;
989}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000990static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
991 bonding_show_ad_select, bonding_store_ad_select);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800992
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800993/*
Ben Hutchingsad246c92011-04-26 15:25:52 +0000994 * Show and set the number of peer notifications to send after a failover event.
995 */
996static ssize_t bonding_show_num_peer_notif(struct device *d,
997 struct device_attribute *attr,
998 char *buf)
999{
1000 struct bonding *bond = to_bond(d);
1001 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
1002}
1003
1004static ssize_t bonding_store_num_peer_notif(struct device *d,
1005 struct device_attribute *attr,
1006 const char *buf, size_t count)
1007{
1008 struct bonding *bond = to_bond(d);
1009 int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
1010 return err ? err : count;
1011}
1012static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
1013 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
1014static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
1015 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
1016
1017/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001018 * Show and set the MII monitor interval. There are two tricky bits
1019 * here. First, if MII monitoring is activated, then we must disable
1020 * ARP monitoring. Second, if the timer isn't running, we must
1021 * start it.
1022 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001023static ssize_t bonding_show_miimon(struct device *d,
1024 struct device_attribute *attr,
1025 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001026{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001027 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001028
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001029 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001030}
1031
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001032static ssize_t bonding_store_miimon(struct device *d,
1033 struct device_attribute *attr,
1034 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001035{
1036 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001037 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001038
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00001039 if (!rtnl_trylock())
1040 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001041 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001042 pr_err("%s: no miimon value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001043 bond->dev->name);
1044 ret = -EINVAL;
1045 goto out;
1046 }
1047 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001048 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +00001049 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001050 ret = -EINVAL;
1051 goto out;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +00001052 }
1053 pr_info("%s: Setting MII monitoring interval to %d.\n",
1054 bond->dev->name, new_value);
1055 bond->params.miimon = new_value;
1056 if (bond->params.updelay)
1057 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
1058 bond->dev->name,
1059 bond->params.updelay * bond->params.miimon);
1060 if (bond->params.downdelay)
1061 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
1062 bond->dev->name,
1063 bond->params.downdelay * bond->params.miimon);
1064 if (new_value && bond->params.arp_interval) {
1065 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
1066 bond->dev->name);
1067 bond->params.arp_interval = 0;
1068 if (bond->params.arp_validate)
1069 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
1070 }
1071 if (bond->dev->flags & IFF_UP) {
1072 /* If the interface is up, we may need to fire off
1073 * the MII timer. If the interface is down, the
1074 * timer will get fired off when the open function
1075 * is called.
1076 */
1077 if (!new_value) {
1078 cancel_delayed_work_sync(&bond->mii_work);
1079 } else {
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00001080 cancel_delayed_work_sync(&bond->arp_work);
1081 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001082 }
1083 }
1084out:
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00001085 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001086 return ret;
1087}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001088static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1089 bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001090
1091/*
1092 * Show and set the primary slave. The store function is much
1093 * simpler than bonding_store_slaves function because it only needs to
1094 * handle one interface name.
1095 * The bond must be a mode that supports a primary for this be
1096 * set.
1097 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001098static ssize_t bonding_show_primary(struct device *d,
1099 struct device_attribute *attr,
1100 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001101{
1102 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001103 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001104
1105 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001106 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001107
1108 return count;
1109}
1110
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001111static ssize_t bonding_store_primary(struct device *d,
1112 struct device_attribute *attr,
1113 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001114{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001115 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001116 struct list_head *iter;
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001117 char ifname[IFNAMSIZ];
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001118 struct slave *slave;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001119
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001120 if (!rtnl_trylock())
1121 return restart_syscall();
Neil Hormane843fa52010-10-13 16:01:50 +00001122 block_netpoll_tx();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001123 read_lock(&bond->lock);
1124 write_lock_bh(&bond->curr_slave_lock);
1125
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001126 if (!USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001127 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1128 bond->dev->name, bond->dev->name, bond->params.mode);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001129 goto out;
1130 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001131
nikolay@redhat.comeb6e98a2012-10-31 04:42:51 +00001132 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001133
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001134 /* check to see if we are clearing primary */
1135 if (!strlen(ifname) || buf[0] == '\n') {
1136 pr_info("%s: Setting primary slave to None.\n",
1137 bond->dev->name);
1138 bond->primary_slave = NULL;
Milos Vyleteleb492f72013-01-29 09:59:00 +00001139 memset(bond->params.primary, 0, sizeof(bond->params.primary));
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001140 bond_select_active_slave(bond);
1141 goto out;
1142 }
1143
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001144 bond_for_each_slave(bond, slave, iter) {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001145 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1146 pr_info("%s: Setting %s as primary slave.\n",
1147 bond->dev->name, slave->dev->name);
1148 bond->primary_slave = slave;
1149 strcpy(bond->params.primary, slave->dev->name);
1150 bond_select_active_slave(bond);
1151 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001152 }
1153 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001154
Weiping Pan8a936642012-06-10 23:00:20 +00001155 strncpy(bond->params.primary, ifname, IFNAMSIZ);
1156 bond->params.primary[IFNAMSIZ - 1] = 0;
1157
1158 pr_info("%s: Recording %s as primary, "
1159 "but it has not been enslaved to %s yet.\n",
1160 bond->dev->name, ifname, bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001161out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001162 write_unlock_bh(&bond->curr_slave_lock);
1163 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001164 unblock_netpoll_tx();
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001165 rtnl_unlock();
1166
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001167 return count;
1168}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001169static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1170 bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001171
1172/*
Jiri Pirkoa5499522009-09-25 03:28:09 +00001173 * Show and set the primary_reselect flag.
1174 */
1175static ssize_t bonding_show_primary_reselect(struct device *d,
1176 struct device_attribute *attr,
1177 char *buf)
1178{
1179 struct bonding *bond = to_bond(d);
1180
1181 return sprintf(buf, "%s %d\n",
1182 pri_reselect_tbl[bond->params.primary_reselect].modename,
1183 bond->params.primary_reselect);
1184}
1185
1186static ssize_t bonding_store_primary_reselect(struct device *d,
1187 struct device_attribute *attr,
1188 const char *buf, size_t count)
1189{
1190 int new_value, ret = count;
1191 struct bonding *bond = to_bond(d);
1192
1193 if (!rtnl_trylock())
1194 return restart_syscall();
1195
1196 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1197 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001198 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001199 bond->dev->name,
1200 (int) strlen(buf) - 1, buf);
1201 ret = -EINVAL;
1202 goto out;
1203 }
1204
1205 bond->params.primary_reselect = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001206 pr_info("%s: setting primary_reselect to %s (%d).\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001207 bond->dev->name, pri_reselect_tbl[new_value].modename,
1208 new_value);
1209
Neil Hormane843fa52010-10-13 16:01:50 +00001210 block_netpoll_tx();
Jiri Pirkoa5499522009-09-25 03:28:09 +00001211 read_lock(&bond->lock);
1212 write_lock_bh(&bond->curr_slave_lock);
1213 bond_select_active_slave(bond);
1214 write_unlock_bh(&bond->curr_slave_lock);
1215 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001216 unblock_netpoll_tx();
Jiri Pirkoa5499522009-09-25 03:28:09 +00001217out:
1218 rtnl_unlock();
1219 return ret;
1220}
1221static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1222 bonding_show_primary_reselect,
1223 bonding_store_primary_reselect);
1224
1225/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001226 * Show and set the use_carrier flag.
1227 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001228static ssize_t bonding_show_carrier(struct device *d,
1229 struct device_attribute *attr,
1230 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001231{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001232 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001233
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001234 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001235}
1236
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001237static ssize_t bonding_store_carrier(struct device *d,
1238 struct device_attribute *attr,
1239 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001240{
1241 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001242 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001243
1244
1245 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001246 pr_err("%s: no use_carrier value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001247 bond->dev->name);
1248 ret = -EINVAL;
1249 goto out;
1250 }
1251 if ((new_value == 0) || (new_value == 1)) {
1252 bond->params.use_carrier = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001253 pr_info("%s: Setting use_carrier to %d.\n",
1254 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001255 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001256 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1257 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001258 }
1259out:
Jiri Pirko672bda32011-01-25 11:03:25 +00001260 return ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001261}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001262static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1263 bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001264
1265
1266/*
1267 * Show and set currently active_slave.
1268 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001269static ssize_t bonding_show_active_slave(struct device *d,
1270 struct device_attribute *attr,
1271 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001272{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001273 struct bonding *bond = to_bond(d);
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001274 struct slave *curr;
Wagner Ferenc16cd0162007-12-06 23:40:29 -08001275 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001276
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001277 rcu_read_lock();
1278 curr = rcu_dereference(bond->curr_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001279 if (USES_PRIMARY(bond->params.mode) && curr)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001280 count = sprintf(buf, "%s\n", curr->dev->name);
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001281 rcu_read_unlock();
1282
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001283 return count;
1284}
1285
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001286static ssize_t bonding_store_active_slave(struct device *d,
1287 struct device_attribute *attr,
1288 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001289{
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001290 struct slave *slave, *old_active, *new_active;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001291 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001292 struct list_head *iter;
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001293 char ifname[IFNAMSIZ];
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001294
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001295 if (!rtnl_trylock())
1296 return restart_syscall();
Neil Hormane843fa52010-10-13 16:01:50 +00001297
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001298 old_active = new_active = NULL;
Neil Hormane843fa52010-10-13 16:01:50 +00001299 block_netpoll_tx();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001300 read_lock(&bond->lock);
1301 write_lock_bh(&bond->curr_slave_lock);
Jay Vosburgh1466a212007-11-06 13:33:28 -08001302
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001303 if (!USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001304 pr_info("%s: Unable to change active slave; %s is in mode %d\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001305 bond->dev->name, bond->dev->name, bond->params.mode);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001306 goto out;
1307 }
1308
nikolay@redhat.comc84e1592012-10-31 06:03:52 +00001309 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001310
1311 /* check to see if we are clearing active */
1312 if (!strlen(ifname) || buf[0] == '\n') {
1313 pr_info("%s: Clearing current active slave.\n",
1314 bond->dev->name);
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001315 rcu_assign_pointer(bond->curr_active_slave, NULL);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001316 bond_select_active_slave(bond);
1317 goto out;
1318 }
1319
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001320 bond_for_each_slave(bond, slave, iter) {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001321 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1322 old_active = bond->curr_active_slave;
1323 new_active = slave;
1324 if (new_active == old_active) {
1325 /* do nothing */
1326 pr_info("%s: %s is already the current"
1327 " active slave.\n",
1328 bond->dev->name,
1329 slave->dev->name);
1330 goto out;
dingtianhong38c49162013-07-23 15:25:32 +08001331 } else {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001332 if ((new_active) &&
1333 (old_active) &&
1334 (new_active->link == BOND_LINK_UP) &&
1335 IS_UP(new_active->dev)) {
1336 pr_info("%s: Setting %s as active"
1337 " slave.\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001338 bond->dev->name,
1339 slave->dev->name);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001340 bond_change_active_slave(bond,
1341 new_active);
dingtianhong38c49162013-07-23 15:25:32 +08001342 } else {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001343 pr_info("%s: Could not set %s as"
1344 " active slave; either %s is"
1345 " down or the link is down.\n",
1346 bond->dev->name,
1347 slave->dev->name,
1348 slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001349 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001350 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001351 }
1352 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001353 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001354
1355 pr_info("%s: Unable to set %.*s as active slave.\n",
1356 bond->dev->name, (int)strlen(buf) - 1, buf);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001357 out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001358 write_unlock_bh(&bond->curr_slave_lock);
1359 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001360 unblock_netpoll_tx();
1361
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001362 rtnl_unlock();
1363
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001364 return count;
1365
1366}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001367static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1368 bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001369
1370
1371/*
1372 * Show link status of the bond interface.
1373 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001374static ssize_t bonding_show_mii_status(struct device *d,
1375 struct device_attribute *attr,
1376 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001377{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001378 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001379
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001380 return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001381}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001382static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001383
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001384/*
1385 * Show current 802.3ad aggregator ID.
1386 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001387static ssize_t bonding_show_ad_aggregator(struct device *d,
1388 struct device_attribute *attr,
1389 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001390{
1391 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001392 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001393
1394 if (bond->params.mode == BOND_MODE_8023AD) {
1395 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001396 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001397 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001398 ? 0 : ad_info.aggregator_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001399 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001400
1401 return count;
1402}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001403static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001404
1405
1406/*
1407 * Show number of active 802.3ad ports.
1408 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001409static ssize_t bonding_show_ad_num_ports(struct device *d,
1410 struct device_attribute *attr,
1411 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001412{
1413 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001414 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001415
1416 if (bond->params.mode == BOND_MODE_8023AD) {
1417 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001418 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001419 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001420 ? 0 : ad_info.ports);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001421 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001422
1423 return count;
1424}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001425static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001426
1427
1428/*
1429 * Show current 802.3ad actor key.
1430 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001431static ssize_t bonding_show_ad_actor_key(struct device *d,
1432 struct device_attribute *attr,
1433 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001434{
1435 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001436 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001437
1438 if (bond->params.mode == BOND_MODE_8023AD) {
1439 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001440 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001441 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001442 ? 0 : ad_info.actor_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001443 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001444
1445 return count;
1446}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001447static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001448
1449
1450/*
1451 * Show current 802.3ad partner key.
1452 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001453static ssize_t bonding_show_ad_partner_key(struct device *d,
1454 struct device_attribute *attr,
1455 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001456{
1457 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001458 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001459
1460 if (bond->params.mode == BOND_MODE_8023AD) {
1461 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001462 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001463 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001464 ? 0 : ad_info.partner_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001465 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001466
1467 return count;
1468}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001469static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001470
1471
1472/*
1473 * Show current 802.3ad partner mac.
1474 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001475static ssize_t bonding_show_ad_partner_mac(struct device *d,
1476 struct device_attribute *attr,
1477 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001478{
1479 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001480 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001481
1482 if (bond->params.mode == BOND_MODE_8023AD) {
1483 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001484 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
Johannes Berge1749612008-10-27 15:59:26 -07001485 count = sprintf(buf, "%pM\n", ad_info.partner_system);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001486 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001487
1488 return count;
1489}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001490static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001491
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001492/*
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001493 * Show the queue_ids of the slaves in the current bond.
1494 */
1495static ssize_t bonding_show_queue_id(struct device *d,
1496 struct device_attribute *attr,
1497 char *buf)
1498{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001499 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001500 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001501 struct slave *slave;
1502 int res = 0;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001503
1504 if (!rtnl_trylock())
1505 return restart_syscall();
1506
1507 read_lock(&bond->lock);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001508 bond_for_each_slave(bond, slave, iter) {
Nicolas de Pesloüan79236682010-07-14 18:24:54 -07001509 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1510 /* not enough space for another interface_name:queue_id pair */
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001511 if ((PAGE_SIZE - res) > 10)
1512 res = PAGE_SIZE - 10;
1513 res += sprintf(buf + res, "++more++ ");
1514 break;
1515 }
1516 res += sprintf(buf + res, "%s:%d ",
1517 slave->dev->name, slave->queue_id);
1518 }
1519 read_unlock(&bond->lock);
1520 if (res)
1521 buf[res-1] = '\n'; /* eat the leftover space */
1522 rtnl_unlock();
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001523
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001524 return res;
1525}
1526
1527/*
1528 * Set the queue_ids of the slaves in the current bond. The bond
1529 * interface must be enslaved for this to work.
1530 */
1531static ssize_t bonding_store_queue_id(struct device *d,
1532 struct device_attribute *attr,
1533 const char *buffer, size_t count)
1534{
1535 struct slave *slave, *update_slave;
1536 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001537 struct list_head *iter;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001538 u16 qid;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001539 int ret = count;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001540 char *delim;
1541 struct net_device *sdev = NULL;
1542
1543 if (!rtnl_trylock())
1544 return restart_syscall();
1545
1546 /* delim will point to queue id if successful */
1547 delim = strchr(buffer, ':');
1548 if (!delim)
1549 goto err_no_cmd;
1550
1551 /*
1552 * Terminate string that points to device name and bump it
1553 * up one, so we can read the queue id there.
1554 */
1555 *delim = '\0';
1556 if (sscanf(++delim, "%hd\n", &qid) != 1)
1557 goto err_no_cmd;
1558
1559 /* Check buffer length, valid ifname and queue id */
1560 if (strlen(buffer) > IFNAMSIZ ||
1561 !dev_valid_name(buffer) ||
Jiri Pirko8a540ff2012-07-20 02:28:50 +00001562 qid > bond->dev->real_num_tx_queues)
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001563 goto err_no_cmd;
1564
1565 /* Get the pointer to that interface if it exists */
1566 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1567 if (!sdev)
1568 goto err_no_cmd;
1569
1570 read_lock(&bond->lock);
1571
1572 /* Search for thes slave and check for duplicate qids */
1573 update_slave = NULL;
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001574 bond_for_each_slave(bond, slave, iter) {
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001575 if (sdev == slave->dev)
1576 /*
1577 * We don't need to check the matching
1578 * slave for dups, since we're overwriting it
1579 */
1580 update_slave = slave;
1581 else if (qid && qid == slave->queue_id) {
1582 goto err_no_cmd_unlock;
1583 }
1584 }
1585
1586 if (!update_slave)
1587 goto err_no_cmd_unlock;
1588
1589 /* Actually set the qids for the slave */
1590 update_slave->queue_id = qid;
1591
1592 read_unlock(&bond->lock);
1593out:
1594 rtnl_unlock();
1595 return ret;
1596
1597err_no_cmd_unlock:
1598 read_unlock(&bond->lock);
1599err_no_cmd:
1600 pr_info("invalid input for queue_id set for %s.\n",
1601 bond->dev->name);
1602 ret = -EPERM;
1603 goto out;
1604}
1605
1606static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1607 bonding_store_queue_id);
1608
1609
1610/*
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001611 * Show and set the all_slaves_active flag.
1612 */
1613static ssize_t bonding_show_slaves_active(struct device *d,
1614 struct device_attribute *attr,
1615 char *buf)
1616{
1617 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001618
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001619 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1620}
1621
1622static ssize_t bonding_store_slaves_active(struct device *d,
1623 struct device_attribute *attr,
1624 const char *buf, size_t count)
1625{
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001626 struct bonding *bond = to_bond(d);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001627 int new_value, ret = count;
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001628 struct list_head *iter;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001629 struct slave *slave;
1630
1631 if (sscanf(buf, "%d", &new_value) != 1) {
1632 pr_err("%s: no all_slaves_active value specified.\n",
1633 bond->dev->name);
1634 ret = -EINVAL;
1635 goto out;
1636 }
1637
1638 if (new_value == bond->params.all_slaves_active)
1639 goto out;
1640
1641 if ((new_value == 0) || (new_value == 1)) {
1642 bond->params.all_slaves_active = new_value;
1643 } else {
1644 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1645 bond->dev->name, new_value);
1646 ret = -EINVAL;
1647 goto out;
1648 }
1649
nikolay@redhat.come196c0e2012-11-29 01:37:59 +00001650 read_lock(&bond->lock);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001651 bond_for_each_slave(bond, slave, iter) {
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001652 if (!bond_is_active_slave(slave)) {
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001653 if (new_value)
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001654 slave->inactive = 0;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001655 else
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001656 slave->inactive = 1;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001657 }
1658 }
nikolay@redhat.come196c0e2012-11-29 01:37:59 +00001659 read_unlock(&bond->lock);
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001660out:
Jiri Pirko672bda32011-01-25 11:03:25 +00001661 return ret;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001662}
1663static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1664 bonding_show_slaves_active, bonding_store_slaves_active);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001665
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001666/*
1667 * Show and set the number of IGMP membership reports to send on link failure
1668 */
1669static ssize_t bonding_show_resend_igmp(struct device *d,
Flavio Leitner94265cf2011-05-25 08:38:58 +00001670 struct device_attribute *attr,
1671 char *buf)
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001672{
1673 struct bonding *bond = to_bond(d);
1674
1675 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1676}
1677
1678static ssize_t bonding_store_resend_igmp(struct device *d,
Flavio Leitner94265cf2011-05-25 08:38:58 +00001679 struct device_attribute *attr,
1680 const char *buf, size_t count)
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001681{
1682 int new_value, ret = count;
1683 struct bonding *bond = to_bond(d);
1684
1685 if (sscanf(buf, "%d", &new_value) != 1) {
1686 pr_err("%s: no resend_igmp value specified.\n",
1687 bond->dev->name);
1688 ret = -EINVAL;
1689 goto out;
1690 }
1691
Flavio Leitner94265cf2011-05-25 08:38:58 +00001692 if (new_value < 0 || new_value > 255) {
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001693 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1694 bond->dev->name, new_value);
1695 ret = -EINVAL;
1696 goto out;
1697 }
1698
1699 pr_info("%s: Setting resend_igmp to %d.\n",
1700 bond->dev->name, new_value);
1701 bond->params.resend_igmp = new_value;
1702out:
1703 return ret;
1704}
1705
1706static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1707 bonding_show_resend_igmp, bonding_store_resend_igmp);
1708
Neil Horman7eacd032013-09-13 11:05:33 -04001709
1710static ssize_t bonding_show_lp_interval(struct device *d,
1711 struct device_attribute *attr,
1712 char *buf)
1713{
1714 struct bonding *bond = to_bond(d);
1715 return sprintf(buf, "%d\n", bond->params.lp_interval);
1716}
1717
1718static ssize_t bonding_store_lp_interval(struct device *d,
1719 struct device_attribute *attr,
1720 const char *buf, size_t count)
1721{
1722 struct bonding *bond = to_bond(d);
1723 int new_value, ret = count;
1724
1725 if (sscanf(buf, "%d", &new_value) != 1) {
1726 pr_err("%s: no lp interval value specified.\n",
1727 bond->dev->name);
1728 ret = -EINVAL;
1729 goto out;
1730 }
1731
1732 if (new_value <= 0) {
1733 pr_err ("%s: lp_interval must be between 1 and %d\n",
1734 bond->dev->name, INT_MAX);
1735 ret = -EINVAL;
1736 goto out;
1737 }
1738
1739 bond->params.lp_interval = new_value;
1740out:
1741 return ret;
1742}
1743
1744static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1745 bonding_show_lp_interval, bonding_store_lp_interval);
1746
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001747static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001748 &dev_attr_slaves.attr,
1749 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001750 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001751 &dev_attr_arp_validate.attr,
Veaceslav Falico8599b522013-06-24 11:49:34 +02001752 &dev_attr_arp_all_targets.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001753 &dev_attr_arp_interval.attr,
1754 &dev_attr_arp_ip_target.attr,
1755 &dev_attr_downdelay.attr,
1756 &dev_attr_updelay.attr,
1757 &dev_attr_lacp_rate.attr,
Jay Vosburghfd989c82008-11-04 17:51:16 -08001758 &dev_attr_ad_select.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001759 &dev_attr_xmit_hash_policy.attr,
Ben Hutchingsad246c92011-04-26 15:25:52 +00001760 &dev_attr_num_grat_arp.attr,
1761 &dev_attr_num_unsol_na.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001762 &dev_attr_miimon.attr,
1763 &dev_attr_primary.attr,
Jiri Pirkoa5499522009-09-25 03:28:09 +00001764 &dev_attr_primary_reselect.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001765 &dev_attr_use_carrier.attr,
1766 &dev_attr_active_slave.attr,
1767 &dev_attr_mii_status.attr,
1768 &dev_attr_ad_aggregator.attr,
1769 &dev_attr_ad_num_ports.attr,
1770 &dev_attr_ad_actor_key.attr,
1771 &dev_attr_ad_partner_key.attr,
1772 &dev_attr_ad_partner_mac.attr,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001773 &dev_attr_queue_id.attr,
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001774 &dev_attr_all_slaves_active.attr,
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001775 &dev_attr_resend_igmp.attr,
stephen hemminger655f8912011-06-22 09:54:39 +00001776 &dev_attr_min_links.attr,
Neil Horman7eacd032013-09-13 11:05:33 -04001777 &dev_attr_lp_interval.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001778 NULL,
1779};
1780
1781static struct attribute_group bonding_group = {
1782 .name = "bonding",
1783 .attrs = per_bond_attrs,
1784};
1785
1786/*
1787 * Initialize sysfs. This sets up the bonding_masters file in
1788 * /sys/class/net.
1789 */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001790int bond_create_sysfs(struct bond_net *bn)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001791{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001792 int ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001793
Eric W. Biederman4c224002011-10-12 21:56:25 +00001794 bn->class_attr_bonding_masters = class_attr_bonding_masters;
Eric W. Biederman01718e32011-10-21 22:43:07 +00001795 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
Eric W. Biederman4c224002011-10-12 21:56:25 +00001796
1797 ret = netdev_class_create_file(&bn->class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001798 /*
1799 * Permit multiple loads of the module by ignoring failures to
1800 * create the bonding_masters sysfs file. Bonding devices
1801 * created by second or subsequent loads of the module will
1802 * not be listed in, or controllable by, bonding_masters, but
1803 * will have the usual "bonding" sysfs directory.
1804 *
1805 * This is done to preserve backwards compatibility for
1806 * initscripts/sysconfig, which load bonding multiple times to
1807 * configure multiple bonding devices.
1808 */
1809 if (ret == -EEXIST) {
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001810 /* Is someone being kinky and naming a device bonding_master? */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001811 if (__dev_get_by_name(bn->net,
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001812 class_attr_bonding_masters.attr.name))
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001813 pr_err("network device named %s already exists in sysfs",
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001814 class_attr_bonding_masters.attr.name);
Stephen Hemminger130aa612009-06-11 05:46:04 -07001815 ret = 0;
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001816 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001817
1818 return ret;
1819
1820}
1821
1822/*
1823 * Remove /sys/class/net/bonding_masters.
1824 */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001825void bond_destroy_sysfs(struct bond_net *bn)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001826{
Eric W. Biederman4c224002011-10-12 21:56:25 +00001827 netdev_class_remove_file(&bn->class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001828}
1829
1830/*
1831 * Initialize sysfs for each bond. This sets up and registers
1832 * the 'bondctl' directory for each individual bond under /sys/class/net.
1833 */
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001834void bond_prepare_sysfs_group(struct bonding *bond)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001835{
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001836 bond->dev->sysfs_groups[0] = &bonding_group;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001837}
1838