blob: 011f163c2c6771a9cbd335a507dfb390b081eca2 [file] [log] [blame]
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001/*
2 * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
Jeff Kirsheradf8d3f2013-12-06 06:28:47 -080015 * with this program; if not, see <http://www.gnu.org/licenses/>.
Mitch Williamsb76cdba2005-11-09 10:36:41 -080016 *
17 * The full GNU General Public License is included in this distribution in the
18 * file called LICENSE.
19 *
Mitch Williamsb76cdba2005-11-09 10:36:41 -080020 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -080021
22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Mitch Williamsb76cdba2005-11-09 10:36:41 -080024#include <linux/kernel.h>
25#include <linux/module.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080026#include <linux/device.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040027#include <linux/sched.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080028#include <linux/fs.h>
29#include <linux/types.h>
30#include <linux/string.h>
31#include <linux/netdevice.h>
32#include <linux/inetdevice.h>
33#include <linux/in.h>
34#include <linux/sysfs.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080035#include <linux/ctype.h>
36#include <linux/inet.h>
37#include <linux/rtnetlink.h>
Stephen Hemminger5c5129b2009-06-12 19:02:51 +000038#include <linux/etherdevice.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070039#include <net/net_namespace.h>
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000040#include <net/netns/generic.h>
41#include <linux/nsproxy.h>
Nikolay Aleksandrov73958322013-11-05 13:51:41 +010042#include <linux/reciprocal_div.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080043
Mitch Williamsb76cdba2005-11-09 10:36:41 -080044#include "bonding.h"
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -080045
Stephen Hemminger3d632c32009-06-12 19:02:48 +000046#define to_dev(obj) container_of(obj, struct device, kobj)
Wang Chen454d7c92008-11-12 23:37:49 -080047#define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
Mitch Williamsb76cdba2005-11-09 10:36:41 -080048
Mitch Williamsb76cdba2005-11-09 10:36:41 -080049/*
50 * "show" function for the bond_masters attribute.
51 * The class parameter is ignored.
52 */
Andi Kleen28812fe2010-01-05 12:48:07 +010053static ssize_t bonding_show_bonds(struct class *cls,
54 struct class_attribute *attr,
55 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080056{
Eric W. Biederman4c224002011-10-12 21:56:25 +000057 struct bond_net *bn =
58 container_of(attr, struct bond_net, class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080059 int res = 0;
60 struct bonding *bond;
61
Stephen Hemminger7e083842009-06-12 19:02:46 +000062 rtnl_lock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080063
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000064 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -080065 if (res > (PAGE_SIZE - IFNAMSIZ)) {
66 /* not enough space for another interface name */
67 if ((PAGE_SIZE - res) > 10)
68 res = PAGE_SIZE - 10;
Wagner Ferencb8843662007-12-06 23:40:30 -080069 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -080070 break;
71 }
Wagner Ferencb8843662007-12-06 23:40:30 -080072 res += sprintf(buf + res, "%s ", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080073 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -080074 if (res)
75 buf[res-1] = '\n'; /* eat the leftover space */
Stephen Hemminger7e083842009-06-12 19:02:46 +000076
77 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080078 return res;
79}
80
Eric W. Biederman4c224002011-10-12 21:56:25 +000081static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
Stephen Hemminger373500d2009-06-12 19:02:50 +000082{
83 struct bonding *bond;
84
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000085 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Stephen Hemminger373500d2009-06-12 19:02:50 +000086 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
87 return bond->dev;
88 }
89 return NULL;
90}
91
Mitch Williamsb76cdba2005-11-09 10:36:41 -080092/*
93 * "store" function for the bond_masters attribute. This is what
94 * creates and deletes entire bonds.
95 *
96 * The class parameter is ignored.
97 *
98 */
99
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000100static ssize_t bonding_store_bonds(struct class *cls,
Andi Kleen28812fe2010-01-05 12:48:07 +0100101 struct class_attribute *attr,
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000102 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800103{
Eric W. Biederman4c224002011-10-12 21:56:25 +0000104 struct bond_net *bn =
105 container_of(attr, struct bond_net, class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800106 char command[IFNAMSIZ + 1] = {0, };
107 char *ifname;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800108 int rv, res = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800109
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800110 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
111 ifname = command + 1;
112 if ((strlen(command) <= 1) ||
113 !dev_valid_name(ifname))
114 goto err_no_cmd;
115
116 if (command[0] == '+') {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800117 pr_info("%s is being created...\n", ifname);
Eric W. Biederman4c224002011-10-12 21:56:25 +0000118 rv = bond_create(bn->net, ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800119 if (rv) {
Phil Oester5f86cad12011-03-14 06:22:06 +0000120 if (rv == -EEXIST)
121 pr_info("%s already exists.\n", ifname);
122 else
123 pr_info("%s creation failed.\n", ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800124 res = rv;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800125 }
Stephen Hemminger373500d2009-06-12 19:02:50 +0000126 } else if (command[0] == '-') {
127 struct net_device *bond_dev;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800128
Jay Vosburgh027ea042008-01-17 16:25:02 -0800129 rtnl_lock();
Eric W. Biederman4c224002011-10-12 21:56:25 +0000130 bond_dev = bond_get_by_name(bn, ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000131 if (bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800132 pr_info("%s is being deleted...\n", ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000133 unregister_netdevice(bond_dev);
134 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800135 pr_err("unable to delete non-existent %s\n", ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000136 res = -ENODEV;
137 }
138 rtnl_unlock();
139 } else
140 goto err_no_cmd;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800141
Stephen Hemminger373500d2009-06-12 19:02:50 +0000142 /* Always return either count or an error. If you return 0, you'll
143 * get called forever, which is bad.
144 */
145 return res;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800146
147err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800148 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700149 return -EPERM;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800150}
Stephen Hemminger373500d2009-06-12 19:02:50 +0000151
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800152/* class attribute for bond_masters file. This ends up in /sys/class/net */
Eric W. Biederman4c224002011-10-12 21:56:25 +0000153static const struct class_attribute class_attr_bonding_masters = {
154 .attr = {
155 .name = "bonding_masters",
156 .mode = S_IWUSR | S_IRUGO,
157 },
158 .show = bonding_show_bonds,
159 .store = bonding_store_bonds,
Eric W. Biederman4c224002011-10-12 21:56:25 +0000160};
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800161
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800162/*
163 * Show the slaves in the current bond.
164 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700165static ssize_t bonding_show_slaves(struct device *d,
166 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800167{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700168 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200169 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200170 struct slave *slave;
171 int res = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800172
dingtianhong4d1ae5f2013-10-15 16:28:42 +0800173 if (!rtnl_trylock())
174 return restart_syscall();
175
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200176 bond_for_each_slave(bond, slave, iter) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800177 if (res > (PAGE_SIZE - IFNAMSIZ)) {
178 /* not enough space for another interface name */
179 if ((PAGE_SIZE - res) > 10)
180 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800181 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800182 break;
183 }
184 res += sprintf(buf + res, "%s ", slave->dev->name);
185 }
dingtianhong4d1ae5f2013-10-15 16:28:42 +0800186
187 rtnl_unlock();
188
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800189 if (res)
190 buf[res-1] = '\n'; /* eat the leftover space */
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200191
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800192 return res;
193}
194
195/*
Veaceslav Falicod6641cc2013-05-28 01:26:13 +0000196 * Set the slaves in the current bond.
Jiri Pirkof9f35452010-05-18 05:46:39 +0000197 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
198 * All hard work should be done there.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800199 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700200static ssize_t bonding_store_slaves(struct device *d,
201 struct device_attribute *attr,
202 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800203{
204 char command[IFNAMSIZ + 1] = { 0, };
205 char *ifname;
Jiri Pirkof9f35452010-05-18 05:46:39 +0000206 int res, ret = count;
207 struct net_device *dev;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700208 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800209
Eric W. Biederman496a60c2009-05-13 17:02:50 +0000210 if (!rtnl_trylock())
211 return restart_syscall();
Jay Vosburgh027ea042008-01-17 16:25:02 -0800212
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800213 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
214 ifname = command + 1;
215 if ((strlen(command) <= 1) ||
216 !dev_valid_name(ifname))
217 goto err_no_cmd;
218
Jiri Pirkof9f35452010-05-18 05:46:39 +0000219 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
220 if (!dev) {
221 pr_info("%s: Interface %s does not exist!\n",
222 bond->dev->name, ifname);
223 ret = -ENODEV;
224 goto out;
225 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800226
Jiri Pirkof9f35452010-05-18 05:46:39 +0000227 switch (command[0]) {
228 case '+':
229 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800230 res = bond_enslave(bond->dev, dev);
Jiri Pirkof9f35452010-05-18 05:46:39 +0000231 break;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000232
Jiri Pirkof9f35452010-05-18 05:46:39 +0000233 case '-':
234 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
235 res = bond_release(bond->dev, dev);
236 break;
237
238 default:
239 goto err_no_cmd;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800240 }
241
Jiri Pirkof9f35452010-05-18 05:46:39 +0000242 if (res)
243 ret = res;
244 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800245
246err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800247 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
248 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800249 ret = -EPERM;
250
251out:
Jay Vosburgh027ea042008-01-17 16:25:02 -0800252 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800253 return ret;
254}
255
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000256static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
257 bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800258
259/*
260 * Show and set the bonding mode. The bond interface must be down to
261 * change the mode.
262 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700263static ssize_t bonding_show_mode(struct device *d,
264 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800265{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700266 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800267
268 return sprintf(buf, "%s %d\n",
269 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800270 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800271}
272
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700273static ssize_t bonding_store_mode(struct device *d,
274 struct device_attribute *attr,
275 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800276{
Jiri Pirko72be35f2013-10-18 17:43:34 +0200277 int new_value, ret;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700278 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800279
Jay Vosburghece95f72008-01-17 16:25:01 -0800280 new_value = bond_parse_parm(buf, bond_mode_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800281 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800282 pr_err("%s: Ignoring invalid mode value %.*s.\n",
283 bond->dev->name, (int)strlen(buf) - 1, buf);
Jiri Pirko72be35f2013-10-18 17:43:34 +0200284 return -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800285 }
Jiri Pirko72be35f2013-10-18 17:43:34 +0200286 if (!rtnl_trylock())
287 return restart_syscall();
288
289 ret = bond_option_mode_set(bond, new_value);
290 if (!ret) {
291 pr_info("%s: setting mode to %s (%d).\n",
292 bond->dev->name, bond_mode_tbl[new_value].modename,
293 new_value);
294 ret = count;
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000295 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000296
nikolay@redhat.comea6836d2013-05-18 01:18:28 +0000297 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800298 return ret;
299}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000300static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
301 bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800302
303/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000304 * Show and set the bonding transmit hash method.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800305 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700306static ssize_t bonding_show_xmit_hash(struct device *d,
307 struct device_attribute *attr,
308 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800309{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700310 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800311
Wagner Ferenc8e4b9322007-12-06 23:40:32 -0800312 return sprintf(buf, "%s %d\n",
313 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
314 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800315}
316
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700317static ssize_t bonding_store_xmit_hash(struct device *d,
318 struct device_attribute *attr,
319 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800320{
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -0800321 int new_value, ret;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700322 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800323
Jay Vosburghece95f72008-01-17 16:25:01 -0800324 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800325 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800326 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800327 bond->dev->name,
328 (int)strlen(buf) - 1, buf);
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -0800329 return -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800330 }
Nikolay Aleksandrov53edee22013-05-24 00:59:47 +0000331
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -0800332 if (!rtnl_trylock())
333 return restart_syscall();
334
335 ret = bond_option_xmit_hash_policy_set(bond, new_value);
336 if (!ret)
337 ret = count;
338
339 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800340 return ret;
341}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000342static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
343 bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800344
345/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700346 * Show and set arp_validate.
347 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700348static ssize_t bonding_show_arp_validate(struct device *d,
349 struct device_attribute *attr,
350 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700351{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700352 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700353
354 return sprintf(buf, "%s %d\n",
355 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800356 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700357}
358
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700359static ssize_t bonding_store_arp_validate(struct device *d,
360 struct device_attribute *attr,
361 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700362{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700363 struct bonding *bond = to_bond(d);
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800364 int new_value, ret;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700365
Jay Vosburghece95f72008-01-17 16:25:01 -0800366 new_value = bond_parse_parm(buf, arp_validate_tbl);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700367 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800368 pr_err("%s: Ignoring invalid arp_validate value %s\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700369 bond->dev->name, buf);
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800370 return -EINVAL;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700371 }
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800372 if (!rtnl_trylock())
373 return restart_syscall();
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700374
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800375 ret = bond_option_arp_validate_set(bond, new_value);
376 if (!ret)
377 ret = count;
378
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200379 rtnl_unlock();
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700380
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200381 return ret;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700382}
383
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000384static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
385 bonding_store_arp_validate);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200386/*
387 * Show and set arp_all_targets.
388 */
389static ssize_t bonding_show_arp_all_targets(struct device *d,
390 struct device_attribute *attr,
391 char *buf)
392{
393 struct bonding *bond = to_bond(d);
394 int value = bond->params.arp_all_targets;
395
396 return sprintf(buf, "%s %d\n", arp_all_targets_tbl[value].modename,
397 value);
398}
399
400static ssize_t bonding_store_arp_all_targets(struct device *d,
401 struct device_attribute *attr,
402 const char *buf, size_t count)
403{
404 struct bonding *bond = to_bond(d);
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -0800405 int new_value, ret;
Veaceslav Falico8599b522013-06-24 11:49:34 +0200406
407 new_value = bond_parse_parm(buf, arp_all_targets_tbl);
408 if (new_value < 0) {
409 pr_err("%s: Ignoring invalid arp_all_targets value %s\n",
410 bond->dev->name, buf);
411 return -EINVAL;
412 }
Veaceslav Falico8599b522013-06-24 11:49:34 +0200413
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -0800414 if (!rtnl_trylock())
415 return restart_syscall();
Veaceslav Falico8599b522013-06-24 11:49:34 +0200416
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -0800417 ret = bond_option_arp_all_targets_set(bond, new_value);
418 if (!ret)
419 ret = count;
420
421 rtnl_unlock();
422
423 return ret;
Veaceslav Falico8599b522013-06-24 11:49:34 +0200424}
425
426static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
427 bonding_show_arp_all_targets, bonding_store_arp_all_targets);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700428
429/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700430 * Show and store fail_over_mac. User only allowed to change the
431 * value when there are no slaves.
432 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000433static ssize_t bonding_show_fail_over_mac(struct device *d,
434 struct device_attribute *attr,
435 char *buf)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700436{
437 struct bonding *bond = to_bond(d);
438
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700439 return sprintf(buf, "%s %d\n",
440 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
441 bond->params.fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700442}
443
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000444static ssize_t bonding_store_fail_over_mac(struct device *d,
445 struct device_attribute *attr,
446 const char *buf, size_t count)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700447{
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -0800448 int new_value, ret;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700449 struct bonding *bond = to_bond(d);
450
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700451 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
452 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800453 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700454 bond->dev->name, buf);
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -0800455 return -EINVAL;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700456 }
457
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -0800458 if (!rtnl_trylock())
459 return restart_syscall();
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700460
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -0800461 ret = bond_option_fail_over_mac_set(bond, new_value);
462 if (!ret)
463 ret = count;
464
dingtianhong9402b742013-07-23 15:25:39 +0800465 rtnl_unlock();
466 return ret;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700467}
468
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000469static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
470 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700471
472/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800473 * Show and set the arp timer interval. There are two tricky bits
474 * here. First, if ARP monitoring is activated, then we must disable
475 * MII monitoring. Second, if the ARP timer isn't running, we must
476 * start it.
477 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700478static ssize_t bonding_show_arp_interval(struct device *d,
479 struct device_attribute *attr,
480 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800481{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700482 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800483
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800484 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800485}
486
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700487static ssize_t bonding_store_arp_interval(struct device *d,
488 struct device_attribute *attr,
489 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800490{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700491 struct bonding *bond = to_bond(d);
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800492 int new_value, ret;
493
494 if (sscanf(buf, "%d", &new_value) != 1) {
495 pr_err("%s: no arp_interval value specified.\n",
496 bond->dev->name);
497 return -EINVAL;
498 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800499
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000500 if (!rtnl_trylock())
501 return restart_syscall();
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800502
503 ret = bond_option_arp_interval_set(bond, new_value);
504 if (!ret)
505 ret = count;
506
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000507 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800508 return ret;
509}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000510static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
511 bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800512
513/*
514 * Show and set the arp targets.
515 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700516static ssize_t bonding_show_arp_targets(struct device *d,
517 struct device_attribute *attr,
518 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800519{
520 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700521 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800522
523 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
524 if (bond->params.arp_targets[i])
Harvey Harrison63779432008-10-31 00:56:00 -0700525 res += sprintf(buf + res, "%pI4 ",
526 &bond->params.arp_targets[i]);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800527 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800528 if (res)
529 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800530 return res;
531}
532
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700533static ssize_t bonding_store_arp_targets(struct device *d,
534 struct device_attribute *attr,
535 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800536{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700537 struct bonding *bond = to_bond(d);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800538 __be32 target;
539 int ret = -EPERM;
540
541 if (!in4_pton(buf + 1, -1, (u8 *)&target, -1, NULL)) {
542 pr_err("%s: invalid ARP target %pI4 specified\n",
543 bond->dev->name, &target);
544 return -EPERM;
545 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800546
dingtianhong4d1ae5f2013-10-15 16:28:42 +0800547 if (!rtnl_trylock())
548 return restart_syscall();
549
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800550 if (buf[0] == '+')
551 ret = bond_option_arp_ip_target_add(bond, target);
552 else if (buf[0] == '-')
553 ret = bond_option_arp_ip_target_rem(bond, target);
554 else
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800555 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
556 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800557
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800558 if (!ret)
559 ret = count;
560
dingtianhong4d1ae5f2013-10-15 16:28:42 +0800561 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800562 return ret;
563}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700564static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800565
566/*
567 * Show and set the up and down delays. These must be multiples of the
568 * MII monitoring value, and are stored internally as the multiplier.
569 * Thus, we must translate to MS for the real world.
570 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700571static ssize_t bonding_show_downdelay(struct device *d,
572 struct device_attribute *attr,
573 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800574{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700575 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800576
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800577 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800578}
579
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700580static ssize_t bonding_store_downdelay(struct device *d,
581 struct device_attribute *attr,
582 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800583{
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800584 int new_value, ret;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700585 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800586
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800587 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800588 pr_err("%s: no down delay value specified.\n", bond->dev->name);
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800589 return -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800590 }
591
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800592 if (!rtnl_trylock())
593 return restart_syscall();
594
595 ret = bond_option_downdelay_set(bond, new_value);
596 if (!ret)
597 ret = count;
598
Nikolay Aleksandrovb869ccf2013-11-13 17:07:46 +0100599 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800600 return ret;
601}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000602static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
603 bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800604
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700605static ssize_t bonding_show_updelay(struct device *d,
606 struct device_attribute *attr,
607 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800608{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700609 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800610
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800611 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800612
613}
614
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700615static ssize_t bonding_store_updelay(struct device *d,
616 struct device_attribute *attr,
617 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800618{
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800619 int new_value, ret;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700620 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800621
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800622 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800623 pr_err("%s: no up delay value specified.\n",
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800624 bond->dev->name);
625 return -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800626 }
627
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800628 if (!rtnl_trylock())
629 return restart_syscall();
630
631 ret = bond_option_updelay_set(bond, new_value);
632 if (!ret)
633 ret = count;
634
Nikolay Aleksandrovb869ccf2013-11-13 17:07:46 +0100635 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800636 return ret;
637}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000638static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
639 bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800640
641/*
642 * Show and set the LACP interval. Interface must be down, and the mode
643 * must be set to 802.3ad mode.
644 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700645static ssize_t bonding_show_lacp(struct device *d,
646 struct device_attribute *attr,
647 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800648{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700649 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800650
651 return sprintf(buf, "%s %d\n",
652 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800653 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800654}
655
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700656static ssize_t bonding_store_lacp(struct device *d,
657 struct device_attribute *attr,
658 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800659{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700660 struct bonding *bond = to_bond(d);
sfeldma@cumulusnetworks.com998e40bb2014-01-03 14:18:41 -0800661 int new_value, ret;
662
663 new_value = bond_parse_parm(buf, bond_lacp_tbl);
664 if (new_value < 0) {
665 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
666 bond->dev->name, (int)strlen(buf) - 1, buf);
667 return -EINVAL;
668 }
nikolay@redhat.comc5093162013-09-02 13:51:40 +0200669
670 if (!rtnl_trylock())
671 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800672
sfeldma@cumulusnetworks.com998e40bb2014-01-03 14:18:41 -0800673 ret = bond_option_lacp_rate_set(bond, new_value);
674 if (!ret)
675 ret = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800676
nikolay@redhat.comc5093162013-09-02 13:51:40 +0200677 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800678 return ret;
679}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000680static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
681 bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800682
stephen hemminger655f8912011-06-22 09:54:39 +0000683static ssize_t bonding_show_min_links(struct device *d,
684 struct device_attribute *attr,
685 char *buf)
686{
687 struct bonding *bond = to_bond(d);
688
689 return sprintf(buf, "%d\n", bond->params.min_links);
690}
691
692static ssize_t bonding_store_min_links(struct device *d,
693 struct device_attribute *attr,
694 const char *buf, size_t count)
695{
696 struct bonding *bond = to_bond(d);
697 int ret;
698 unsigned int new_value;
699
700 ret = kstrtouint(buf, 0, &new_value);
701 if (ret < 0) {
702 pr_err("%s: Ignoring invalid min links value %s.\n",
703 bond->dev->name, buf);
704 return ret;
705 }
706
sfeldma@cumulusnetworks.com7d101002013-12-17 21:30:23 -0800707 if (!rtnl_trylock())
708 return restart_syscall();
709
710 ret = bond_option_min_links_set(bond, new_value);
711 if (!ret)
712 ret = count;
713
714 rtnl_unlock();
715 return ret;
stephen hemminger655f8912011-06-22 09:54:39 +0000716}
717static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
718 bonding_show_min_links, bonding_store_min_links);
719
Jay Vosburghfd989c82008-11-04 17:51:16 -0800720static ssize_t bonding_show_ad_select(struct device *d,
721 struct device_attribute *attr,
722 char *buf)
723{
724 struct bonding *bond = to_bond(d);
725
726 return sprintf(buf, "%s %d\n",
727 ad_select_tbl[bond->params.ad_select].modename,
728 bond->params.ad_select);
729}
730
731
732static ssize_t bonding_store_ad_select(struct device *d,
733 struct device_attribute *attr,
734 const char *buf, size_t count)
735{
sfeldma@cumulusnetworks.comec029fa2014-01-03 14:18:49 -0800736 int new_value, ret;
Jay Vosburghfd989c82008-11-04 17:51:16 -0800737 struct bonding *bond = to_bond(d);
738
Jay Vosburghfd989c82008-11-04 17:51:16 -0800739 new_value = bond_parse_parm(buf, ad_select_tbl);
sfeldma@cumulusnetworks.comec029fa2014-01-03 14:18:49 -0800740 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800741 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -0800742 bond->dev->name, (int)strlen(buf) - 1, buf);
sfeldma@cumulusnetworks.comec029fa2014-01-03 14:18:49 -0800743 return -EINVAL;
Jay Vosburghfd989c82008-11-04 17:51:16 -0800744 }
sfeldma@cumulusnetworks.comec029fa2014-01-03 14:18:49 -0800745
746 if (!rtnl_trylock())
747 return restart_syscall();
748
749 ret = bond_option_ad_select_set(bond, new_value);
750 if (!ret)
751 ret = count;
752
753 rtnl_unlock();
Jay Vosburghfd989c82008-11-04 17:51:16 -0800754 return ret;
755}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000756static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
757 bonding_show_ad_select, bonding_store_ad_select);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800758
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800759/*
Ben Hutchingsad246c92011-04-26 15:25:52 +0000760 * Show and set the number of peer notifications to send after a failover event.
761 */
762static ssize_t bonding_show_num_peer_notif(struct device *d,
763 struct device_attribute *attr,
764 char *buf)
765{
766 struct bonding *bond = to_bond(d);
767 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
768}
769
770static ssize_t bonding_store_num_peer_notif(struct device *d,
771 struct device_attribute *attr,
772 const char *buf, size_t count)
773{
774 struct bonding *bond = to_bond(d);
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -0800775 u8 new_value;
776 int ret;
777
778 ret = kstrtou8(buf, 10, &new_value);
Veaceslav Falico0b238102014-01-06 11:54:40 +0100779 if (ret) {
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -0800780 pr_err("%s: invalid value %s specified.\n",
781 bond->dev->name, buf);
782 return ret;
783 }
784
785 if (!rtnl_trylock())
786 return restart_syscall();
787
788 ret = bond_option_num_peer_notif_set(bond, new_value);
789 if (!ret)
790 ret = count;
791
792 rtnl_unlock();
793 return ret;
Ben Hutchingsad246c92011-04-26 15:25:52 +0000794}
795static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
796 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
797static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
798 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
799
800/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800801 * Show and set the MII monitor interval. There are two tricky bits
802 * here. First, if MII monitoring is activated, then we must disable
803 * ARP monitoring. Second, if the timer isn't running, we must
804 * start it.
805 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700806static ssize_t bonding_show_miimon(struct device *d,
807 struct device_attribute *attr,
808 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800809{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700810 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800811
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800812 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800813}
814
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700815static ssize_t bonding_store_miimon(struct device *d,
816 struct device_attribute *attr,
817 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800818{
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800819 int new_value, ret;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700820 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800821
822 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800823 pr_err("%s: no miimon value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800824 bond->dev->name);
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800825 return -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800826 }
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800827
828 if (!rtnl_trylock())
829 return restart_syscall();
830
831 ret = bond_option_miimon_set(bond, new_value);
832 if (!ret)
833 ret = count;
834
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000835 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800836 return ret;
837}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000838static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
839 bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800840
841/*
842 * Show and set the primary slave. The store function is much
843 * simpler than bonding_store_slaves function because it only needs to
844 * handle one interface name.
845 * The bond must be a mode that supports a primary for this be
846 * set.
847 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700848static ssize_t bonding_show_primary(struct device *d,
849 struct device_attribute *attr,
850 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800851{
852 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700853 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800854
855 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800856 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800857
858 return count;
859}
860
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700861static ssize_t bonding_store_primary(struct device *d,
862 struct device_attribute *attr,
863 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800864{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700865 struct bonding *bond = to_bond(d);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +0000866 char ifname[IFNAMSIZ];
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -0800867 int ret;
868
869 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
870 if (ifname[0] == '\n')
871 ifname[0] = '\0';
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800872
Eric W. Biederman496a60c2009-05-13 17:02:50 +0000873 if (!rtnl_trylock())
874 return restart_syscall();
Jay Vosburghe934dd72008-01-17 16:24:57 -0800875
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -0800876 ret = bond_option_primary_set(bond, ifname);
877 if (!ret)
878 ret = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800879
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700880 rtnl_unlock();
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -0800881 return ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800882}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000883static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
884 bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800885
886/*
Jiri Pirkoa5499522009-09-25 03:28:09 +0000887 * Show and set the primary_reselect flag.
888 */
889static ssize_t bonding_show_primary_reselect(struct device *d,
890 struct device_attribute *attr,
891 char *buf)
892{
893 struct bonding *bond = to_bond(d);
894
895 return sprintf(buf, "%s %d\n",
896 pri_reselect_tbl[bond->params.primary_reselect].modename,
897 bond->params.primary_reselect);
898}
899
900static ssize_t bonding_store_primary_reselect(struct device *d,
901 struct device_attribute *attr,
902 const char *buf, size_t count)
903{
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -0800904 int new_value, ret;
Jiri Pirkoa5499522009-09-25 03:28:09 +0000905 struct bonding *bond = to_bond(d);
906
Jiri Pirkoa5499522009-09-25 03:28:09 +0000907 new_value = bond_parse_parm(buf, pri_reselect_tbl);
908 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800909 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +0000910 bond->dev->name,
911 (int) strlen(buf) - 1, buf);
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -0800912 return -EINVAL;
Jiri Pirkoa5499522009-09-25 03:28:09 +0000913 }
914
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -0800915 if (!rtnl_trylock())
916 return restart_syscall();
Jiri Pirkoa5499522009-09-25 03:28:09 +0000917
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -0800918 ret = bond_option_primary_reselect_set(bond, new_value);
919 if (!ret)
920 ret = count;
921
Jiri Pirkoa5499522009-09-25 03:28:09 +0000922 rtnl_unlock();
923 return ret;
924}
925static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
926 bonding_show_primary_reselect,
927 bonding_store_primary_reselect);
928
929/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800930 * Show and set the use_carrier flag.
931 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700932static ssize_t bonding_show_carrier(struct device *d,
933 struct device_attribute *attr,
934 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800935{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700936 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800937
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800938 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800939}
940
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700941static ssize_t bonding_store_carrier(struct device *d,
942 struct device_attribute *attr,
943 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800944{
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800945 int new_value, ret;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700946 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800947
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800948 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800949 pr_err("%s: no use_carrier value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800950 bond->dev->name);
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800951 return -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800952 }
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800953
954 if (!rtnl_trylock())
955 return restart_syscall();
956
957 ret = bond_option_use_carrier_set(bond, new_value);
958 if (!ret)
959 ret = count;
960
961 rtnl_unlock();
Jiri Pirko672bda32011-01-25 11:03:25 +0000962 return ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800963}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000964static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
965 bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800966
967
968/*
969 * Show and set currently active_slave.
970 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700971static ssize_t bonding_show_active_slave(struct device *d,
972 struct device_attribute *attr,
973 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800974{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700975 struct bonding *bond = to_bond(d);
Jiri Pirko752d48b2013-10-18 17:43:37 +0200976 struct net_device *slave_dev;
Wagner Ferenc16cd0162007-12-06 23:40:29 -0800977 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800978
nikolay@redhat.com278b2082013-08-01 16:54:51 +0200979 rcu_read_lock();
Jiri Pirko752d48b2013-10-18 17:43:37 +0200980 slave_dev = bond_option_active_slave_get_rcu(bond);
981 if (slave_dev)
982 count = sprintf(buf, "%s\n", slave_dev->name);
nikolay@redhat.com278b2082013-08-01 16:54:51 +0200983 rcu_read_unlock();
984
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800985 return count;
986}
987
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700988static ssize_t bonding_store_active_slave(struct device *d,
989 struct device_attribute *attr,
990 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800991{
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200992 int ret;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700993 struct bonding *bond = to_bond(d);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +0000994 char ifname[IFNAMSIZ];
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200995 struct net_device *dev;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800996
Eric W. Biederman496a60c2009-05-13 17:02:50 +0000997 if (!rtnl_trylock())
998 return restart_syscall();
Neil Hormane843fa52010-10-13 16:01:50 +0000999
nikolay@redhat.comc84e1592012-10-31 06:03:52 +00001000 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001001 if (!strlen(ifname) || buf[0] == '\n') {
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001002 dev = NULL;
1003 } else {
1004 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1005 if (!dev) {
1006 ret = -ENODEV;
1007 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001008 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001009 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001010
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001011 ret = bond_option_active_slave_set(bond, dev);
1012 if (!ret)
1013 ret = count;
Neil Hormane843fa52010-10-13 16:01:50 +00001014
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001015 out:
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001016 rtnl_unlock();
1017
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001018 return ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001019
1020}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001021static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1022 bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001023
1024
1025/*
1026 * Show link status of the bond interface.
1027 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001028static ssize_t bonding_show_mii_status(struct device *d,
1029 struct device_attribute *attr,
1030 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001031{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001032 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001033
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001034 return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001035}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001036static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001037
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001038/*
1039 * Show current 802.3ad aggregator ID.
1040 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001041static ssize_t bonding_show_ad_aggregator(struct device *d,
1042 struct device_attribute *attr,
1043 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001044{
1045 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001046 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001047
1048 if (bond->params.mode == BOND_MODE_8023AD) {
1049 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001050 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001051 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001052 ? 0 : ad_info.aggregator_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001053 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001054
1055 return count;
1056}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001057static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001058
1059
1060/*
1061 * Show number of active 802.3ad ports.
1062 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001063static ssize_t bonding_show_ad_num_ports(struct device *d,
1064 struct device_attribute *attr,
1065 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001066{
1067 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001068 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001069
1070 if (bond->params.mode == BOND_MODE_8023AD) {
1071 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001072 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001073 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001074 ? 0 : ad_info.ports);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001075 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001076
1077 return count;
1078}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001079static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001080
1081
1082/*
1083 * Show current 802.3ad actor key.
1084 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001085static ssize_t bonding_show_ad_actor_key(struct device *d,
1086 struct device_attribute *attr,
1087 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001088{
1089 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001090 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001091
1092 if (bond->params.mode == BOND_MODE_8023AD) {
1093 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001094 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001095 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001096 ? 0 : ad_info.actor_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001097 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001098
1099 return count;
1100}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001101static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001102
1103
1104/*
1105 * Show current 802.3ad partner key.
1106 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001107static ssize_t bonding_show_ad_partner_key(struct device *d,
1108 struct device_attribute *attr,
1109 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001110{
1111 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001112 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001113
1114 if (bond->params.mode == BOND_MODE_8023AD) {
1115 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001116 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001117 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001118 ? 0 : ad_info.partner_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001119 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001120
1121 return count;
1122}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001123static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001124
1125
1126/*
1127 * Show current 802.3ad partner mac.
1128 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001129static ssize_t bonding_show_ad_partner_mac(struct device *d,
1130 struct device_attribute *attr,
1131 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001132{
1133 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001134 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001135
1136 if (bond->params.mode == BOND_MODE_8023AD) {
1137 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001138 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
Johannes Berge1749612008-10-27 15:59:26 -07001139 count = sprintf(buf, "%pM\n", ad_info.partner_system);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001140 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001141
1142 return count;
1143}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001144static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001145
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001146/*
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001147 * Show the queue_ids of the slaves in the current bond.
1148 */
1149static ssize_t bonding_show_queue_id(struct device *d,
1150 struct device_attribute *attr,
1151 char *buf)
1152{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001153 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001154 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001155 struct slave *slave;
1156 int res = 0;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001157
1158 if (!rtnl_trylock())
1159 return restart_syscall();
1160
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001161 bond_for_each_slave(bond, slave, iter) {
Nicolas de Pesloüan79236682010-07-14 18:24:54 -07001162 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1163 /* not enough space for another interface_name:queue_id pair */
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001164 if ((PAGE_SIZE - res) > 10)
1165 res = PAGE_SIZE - 10;
1166 res += sprintf(buf + res, "++more++ ");
1167 break;
1168 }
1169 res += sprintf(buf + res, "%s:%d ",
1170 slave->dev->name, slave->queue_id);
1171 }
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001172 if (res)
1173 buf[res-1] = '\n'; /* eat the leftover space */
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001174
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001175 rtnl_unlock();
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001176
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001177 return res;
1178}
1179
1180/*
1181 * Set the queue_ids of the slaves in the current bond. The bond
1182 * interface must be enslaved for this to work.
1183 */
1184static ssize_t bonding_store_queue_id(struct device *d,
1185 struct device_attribute *attr,
1186 const char *buffer, size_t count)
1187{
1188 struct slave *slave, *update_slave;
1189 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001190 struct list_head *iter;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001191 u16 qid;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001192 int ret = count;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001193 char *delim;
1194 struct net_device *sdev = NULL;
1195
1196 if (!rtnl_trylock())
1197 return restart_syscall();
1198
1199 /* delim will point to queue id if successful */
1200 delim = strchr(buffer, ':');
1201 if (!delim)
1202 goto err_no_cmd;
1203
1204 /*
1205 * Terminate string that points to device name and bump it
1206 * up one, so we can read the queue id there.
1207 */
1208 *delim = '\0';
1209 if (sscanf(++delim, "%hd\n", &qid) != 1)
1210 goto err_no_cmd;
1211
1212 /* Check buffer length, valid ifname and queue id */
1213 if (strlen(buffer) > IFNAMSIZ ||
1214 !dev_valid_name(buffer) ||
Jiri Pirko8a540ff2012-07-20 02:28:50 +00001215 qid > bond->dev->real_num_tx_queues)
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001216 goto err_no_cmd;
1217
1218 /* Get the pointer to that interface if it exists */
1219 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1220 if (!sdev)
1221 goto err_no_cmd;
1222
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001223 /* Search for thes slave and check for duplicate qids */
1224 update_slave = NULL;
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001225 bond_for_each_slave(bond, slave, iter) {
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001226 if (sdev == slave->dev)
1227 /*
1228 * We don't need to check the matching
1229 * slave for dups, since we're overwriting it
1230 */
1231 update_slave = slave;
1232 else if (qid && qid == slave->queue_id) {
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001233 goto err_no_cmd;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001234 }
1235 }
1236
1237 if (!update_slave)
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001238 goto err_no_cmd;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001239
1240 /* Actually set the qids for the slave */
1241 update_slave->queue_id = qid;
1242
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001243out:
1244 rtnl_unlock();
1245 return ret;
1246
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001247err_no_cmd:
1248 pr_info("invalid input for queue_id set for %s.\n",
1249 bond->dev->name);
1250 ret = -EPERM;
1251 goto out;
1252}
1253
1254static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1255 bonding_store_queue_id);
1256
1257
1258/*
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001259 * Show and set the all_slaves_active flag.
1260 */
1261static ssize_t bonding_show_slaves_active(struct device *d,
1262 struct device_attribute *attr,
1263 char *buf)
1264{
1265 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001266
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001267 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1268}
1269
1270static ssize_t bonding_store_slaves_active(struct device *d,
1271 struct device_attribute *attr,
1272 const char *buf, size_t count)
1273{
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001274 struct bonding *bond = to_bond(d);
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001275 int new_value, ret;
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001276
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001277 if (sscanf(buf, "%d", &new_value) != 1) {
1278 pr_err("%s: no all_slaves_active value specified.\n",
1279 bond->dev->name);
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001280 return -EINVAL;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001281 }
1282
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001283 if (!rtnl_trylock())
1284 return restart_syscall();
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001285
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001286 ret = bond_option_all_slaves_active_set(bond, new_value);
1287 if (!ret)
1288 ret = count;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001289
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001290 rtnl_unlock();
Jiri Pirko672bda32011-01-25 11:03:25 +00001291 return ret;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001292}
1293static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1294 bonding_show_slaves_active, bonding_store_slaves_active);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001295
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001296/*
1297 * Show and set the number of IGMP membership reports to send on link failure
1298 */
1299static ssize_t bonding_show_resend_igmp(struct device *d,
Flavio Leitner94265cf2011-05-25 08:38:58 +00001300 struct device_attribute *attr,
1301 char *buf)
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001302{
1303 struct bonding *bond = to_bond(d);
1304
1305 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1306}
1307
1308static ssize_t bonding_store_resend_igmp(struct device *d,
Flavio Leitner94265cf2011-05-25 08:38:58 +00001309 struct device_attribute *attr,
1310 const char *buf, size_t count)
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001311{
1312 int new_value, ret = count;
1313 struct bonding *bond = to_bond(d);
1314
1315 if (sscanf(buf, "%d", &new_value) != 1) {
1316 pr_err("%s: no resend_igmp value specified.\n",
1317 bond->dev->name);
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -08001318 return -EINVAL;
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001319 }
1320
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -08001321 if (!rtnl_trylock())
1322 return restart_syscall();
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001323
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -08001324 ret = bond_option_resend_igmp_set(bond, new_value);
1325 if (!ret)
1326 ret = count;
1327
1328 rtnl_unlock();
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001329 return ret;
1330}
1331
1332static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1333 bonding_show_resend_igmp, bonding_store_resend_igmp);
1334
Neil Horman7eacd032013-09-13 11:05:33 -04001335
1336static ssize_t bonding_show_lp_interval(struct device *d,
1337 struct device_attribute *attr,
1338 char *buf)
1339{
1340 struct bonding *bond = to_bond(d);
1341 return sprintf(buf, "%d\n", bond->params.lp_interval);
1342}
1343
1344static ssize_t bonding_store_lp_interval(struct device *d,
1345 struct device_attribute *attr,
1346 const char *buf, size_t count)
1347{
1348 struct bonding *bond = to_bond(d);
sfeldma@cumulusnetworks.com8d836d02013-12-17 21:30:30 -08001349 int new_value, ret;
Neil Horman7eacd032013-09-13 11:05:33 -04001350
1351 if (sscanf(buf, "%d", &new_value) != 1) {
1352 pr_err("%s: no lp interval value specified.\n",
1353 bond->dev->name);
sfeldma@cumulusnetworks.com8d836d02013-12-17 21:30:30 -08001354 return -EINVAL;
Neil Horman7eacd032013-09-13 11:05:33 -04001355 }
1356
sfeldma@cumulusnetworks.com8d836d02013-12-17 21:30:30 -08001357 if (!rtnl_trylock())
1358 return restart_syscall();
Neil Horman7eacd032013-09-13 11:05:33 -04001359
sfeldma@cumulusnetworks.com8d836d02013-12-17 21:30:30 -08001360 ret = bond_option_lp_interval_set(bond, new_value);
1361 if (!ret)
1362 ret = count;
1363
1364 rtnl_unlock();
Neil Horman7eacd032013-09-13 11:05:33 -04001365 return ret;
1366}
1367
1368static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1369 bonding_show_lp_interval, bonding_store_lp_interval);
1370
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01001371static ssize_t bonding_show_packets_per_slave(struct device *d,
1372 struct device_attribute *attr,
1373 char *buf)
1374{
1375 struct bonding *bond = to_bond(d);
Nikolay Aleksandrova752a8b2013-12-05 11:36:58 +01001376 unsigned int packets_per_slave = bond->params.packets_per_slave;
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01001377
1378 if (packets_per_slave > 1)
1379 packets_per_slave = reciprocal_value(packets_per_slave);
1380
Nikolay Aleksandrova752a8b2013-12-05 11:36:58 +01001381 return sprintf(buf, "%u\n", packets_per_slave);
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01001382}
1383
1384static ssize_t bonding_store_packets_per_slave(struct device *d,
1385 struct device_attribute *attr,
1386 const char *buf, size_t count)
1387{
1388 struct bonding *bond = to_bond(d);
sfeldma@cumulusnetworks.comc13ab3f2013-12-17 21:30:37 -08001389 int new_value, ret;
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01001390
1391 if (sscanf(buf, "%d", &new_value) != 1) {
1392 pr_err("%s: no packets_per_slave value specified.\n",
1393 bond->dev->name);
sfeldma@cumulusnetworks.comc13ab3f2013-12-17 21:30:37 -08001394 return -EINVAL;
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01001395 }
sfeldma@cumulusnetworks.comc13ab3f2013-12-17 21:30:37 -08001396
1397 if (!rtnl_trylock())
1398 return restart_syscall();
1399
1400 ret = bond_option_packets_per_slave_set(bond, new_value);
1401 if (!ret)
1402 ret = count;
1403
1404 rtnl_unlock();
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01001405 return ret;
1406}
1407
1408static DEVICE_ATTR(packets_per_slave, S_IRUGO | S_IWUSR,
1409 bonding_show_packets_per_slave,
1410 bonding_store_packets_per_slave);
1411
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001412static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001413 &dev_attr_slaves.attr,
1414 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001415 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001416 &dev_attr_arp_validate.attr,
Veaceslav Falico8599b522013-06-24 11:49:34 +02001417 &dev_attr_arp_all_targets.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001418 &dev_attr_arp_interval.attr,
1419 &dev_attr_arp_ip_target.attr,
1420 &dev_attr_downdelay.attr,
1421 &dev_attr_updelay.attr,
1422 &dev_attr_lacp_rate.attr,
Jay Vosburghfd989c82008-11-04 17:51:16 -08001423 &dev_attr_ad_select.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001424 &dev_attr_xmit_hash_policy.attr,
Ben Hutchingsad246c92011-04-26 15:25:52 +00001425 &dev_attr_num_grat_arp.attr,
1426 &dev_attr_num_unsol_na.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001427 &dev_attr_miimon.attr,
1428 &dev_attr_primary.attr,
Jiri Pirkoa5499522009-09-25 03:28:09 +00001429 &dev_attr_primary_reselect.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001430 &dev_attr_use_carrier.attr,
1431 &dev_attr_active_slave.attr,
1432 &dev_attr_mii_status.attr,
1433 &dev_attr_ad_aggregator.attr,
1434 &dev_attr_ad_num_ports.attr,
1435 &dev_attr_ad_actor_key.attr,
1436 &dev_attr_ad_partner_key.attr,
1437 &dev_attr_ad_partner_mac.attr,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001438 &dev_attr_queue_id.attr,
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001439 &dev_attr_all_slaves_active.attr,
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001440 &dev_attr_resend_igmp.attr,
stephen hemminger655f8912011-06-22 09:54:39 +00001441 &dev_attr_min_links.attr,
Neil Horman7eacd032013-09-13 11:05:33 -04001442 &dev_attr_lp_interval.attr,
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01001443 &dev_attr_packets_per_slave.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001444 NULL,
1445};
1446
1447static struct attribute_group bonding_group = {
1448 .name = "bonding",
1449 .attrs = per_bond_attrs,
1450};
1451
1452/*
1453 * Initialize sysfs. This sets up the bonding_masters file in
1454 * /sys/class/net.
1455 */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001456int bond_create_sysfs(struct bond_net *bn)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001457{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001458 int ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001459
Eric W. Biederman4c224002011-10-12 21:56:25 +00001460 bn->class_attr_bonding_masters = class_attr_bonding_masters;
Eric W. Biederman01718e32011-10-21 22:43:07 +00001461 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
Eric W. Biederman4c224002011-10-12 21:56:25 +00001462
Tejun Heo58292cbe2013-09-11 22:29:04 -04001463 ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
1464 bn->net);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001465 /*
1466 * Permit multiple loads of the module by ignoring failures to
1467 * create the bonding_masters sysfs file. Bonding devices
1468 * created by second or subsequent loads of the module will
1469 * not be listed in, or controllable by, bonding_masters, but
1470 * will have the usual "bonding" sysfs directory.
1471 *
1472 * This is done to preserve backwards compatibility for
1473 * initscripts/sysconfig, which load bonding multiple times to
1474 * configure multiple bonding devices.
1475 */
1476 if (ret == -EEXIST) {
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001477 /* Is someone being kinky and naming a device bonding_master? */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001478 if (__dev_get_by_name(bn->net,
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001479 class_attr_bonding_masters.attr.name))
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001480 pr_err("network device named %s already exists in sysfs",
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001481 class_attr_bonding_masters.attr.name);
Stephen Hemminger130aa612009-06-11 05:46:04 -07001482 ret = 0;
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001483 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001484
1485 return ret;
1486
1487}
1488
1489/*
1490 * Remove /sys/class/net/bonding_masters.
1491 */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001492void bond_destroy_sysfs(struct bond_net *bn)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001493{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001494 netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001495}
1496
1497/*
1498 * Initialize sysfs for each bond. This sets up and registers
1499 * the 'bondctl' directory for each individual bond under /sys/class/net.
1500 */
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001501void bond_prepare_sysfs_group(struct bonding *bond)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001502{
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001503 bond->dev->sysfs_groups[0] = &bonding_group;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001504}
1505