blob: 9f32e23040044c85f268677e4cdf0f89f117024e [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>
Nikolay Aleksandrov73958322013-11-05 13:51:41 +010043#include <linux/reciprocal_div.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080044
Mitch Williamsb76cdba2005-11-09 10:36:41 -080045#include "bonding.h"
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -080046
Stephen Hemminger3d632c32009-06-12 19:02:48 +000047#define to_dev(obj) container_of(obj, struct device, kobj)
Wang Chen454d7c92008-11-12 23:37:49 -080048#define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
Mitch Williamsb76cdba2005-11-09 10:36:41 -080049
Mitch Williamsb76cdba2005-11-09 10:36:41 -080050/*
51 * "show" function for the bond_masters attribute.
52 * The class parameter is ignored.
53 */
Andi Kleen28812fe2010-01-05 12:48:07 +010054static ssize_t bonding_show_bonds(struct class *cls,
55 struct class_attribute *attr,
56 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080057{
Eric W. Biederman4c224002011-10-12 21:56:25 +000058 struct bond_net *bn =
59 container_of(attr, struct bond_net, class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080060 int res = 0;
61 struct bonding *bond;
62
Stephen Hemminger7e083842009-06-12 19:02:46 +000063 rtnl_lock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080064
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000065 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -080066 if (res > (PAGE_SIZE - IFNAMSIZ)) {
67 /* not enough space for another interface name */
68 if ((PAGE_SIZE - res) > 10)
69 res = PAGE_SIZE - 10;
Wagner Ferencb8843662007-12-06 23:40:30 -080070 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -080071 break;
72 }
Wagner Ferencb8843662007-12-06 23:40:30 -080073 res += sprintf(buf + res, "%s ", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080074 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -080075 if (res)
76 buf[res-1] = '\n'; /* eat the leftover space */
Stephen Hemminger7e083842009-06-12 19:02:46 +000077
78 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080079 return res;
80}
81
Eric W. Biederman4c224002011-10-12 21:56:25 +000082static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
Stephen Hemminger373500d2009-06-12 19:02:50 +000083{
84 struct bonding *bond;
85
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000086 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Stephen Hemminger373500d2009-06-12 19:02:50 +000087 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
88 return bond->dev;
89 }
90 return NULL;
91}
92
Mitch Williamsb76cdba2005-11-09 10:36:41 -080093/*
94 * "store" function for the bond_masters attribute. This is what
95 * creates and deletes entire bonds.
96 *
97 * The class parameter is ignored.
98 *
99 */
100
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000101static ssize_t bonding_store_bonds(struct class *cls,
Andi Kleen28812fe2010-01-05 12:48:07 +0100102 struct class_attribute *attr,
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000103 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800104{
Eric W. Biederman4c224002011-10-12 21:56:25 +0000105 struct bond_net *bn =
106 container_of(attr, struct bond_net, class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800107 char command[IFNAMSIZ + 1] = {0, };
108 char *ifname;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800109 int rv, res = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800110
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800111 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
112 ifname = command + 1;
113 if ((strlen(command) <= 1) ||
114 !dev_valid_name(ifname))
115 goto err_no_cmd;
116
117 if (command[0] == '+') {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800118 pr_info("%s is being created...\n", ifname);
Eric W. Biederman4c224002011-10-12 21:56:25 +0000119 rv = bond_create(bn->net, ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800120 if (rv) {
Phil Oester5f86cad12011-03-14 06:22:06 +0000121 if (rv == -EEXIST)
122 pr_info("%s already exists.\n", ifname);
123 else
124 pr_info("%s creation failed.\n", ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800125 res = rv;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800126 }
Stephen Hemminger373500d2009-06-12 19:02:50 +0000127 } else if (command[0] == '-') {
128 struct net_device *bond_dev;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800129
Jay Vosburgh027ea042008-01-17 16:25:02 -0800130 rtnl_lock();
Eric W. Biederman4c224002011-10-12 21:56:25 +0000131 bond_dev = bond_get_by_name(bn, ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000132 if (bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800133 pr_info("%s is being deleted...\n", ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000134 unregister_netdevice(bond_dev);
135 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800136 pr_err("unable to delete non-existent %s\n", ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000137 res = -ENODEV;
138 }
139 rtnl_unlock();
140 } else
141 goto err_no_cmd;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800142
Stephen Hemminger373500d2009-06-12 19:02:50 +0000143 /* Always return either count or an error. If you return 0, you'll
144 * get called forever, which is bad.
145 */
146 return res;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800147
148err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800149 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700150 return -EPERM;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800151}
Stephen Hemminger373500d2009-06-12 19:02:50 +0000152
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800153/* class attribute for bond_masters file. This ends up in /sys/class/net */
Eric W. Biederman4c224002011-10-12 21:56:25 +0000154static const struct class_attribute class_attr_bonding_masters = {
155 .attr = {
156 .name = "bonding_masters",
157 .mode = S_IWUSR | S_IRUGO,
158 },
159 .show = bonding_show_bonds,
160 .store = bonding_store_bonds,
Eric W. Biederman4c224002011-10-12 21:56:25 +0000161};
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800162
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800163/*
164 * Show the slaves in the current bond.
165 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700166static ssize_t bonding_show_slaves(struct device *d,
167 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800168{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700169 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200170 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200171 struct slave *slave;
172 int res = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800173
dingtianhong4d1ae5f2013-10-15 16:28:42 +0800174 if (!rtnl_trylock())
175 return restart_syscall();
176
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200177 bond_for_each_slave(bond, slave, iter) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800178 if (res > (PAGE_SIZE - IFNAMSIZ)) {
179 /* not enough space for another interface name */
180 if ((PAGE_SIZE - res) > 10)
181 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800182 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800183 break;
184 }
185 res += sprintf(buf + res, "%s ", slave->dev->name);
186 }
dingtianhong4d1ae5f2013-10-15 16:28:42 +0800187
188 rtnl_unlock();
189
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800190 if (res)
191 buf[res-1] = '\n'; /* eat the leftover space */
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200192
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800193 return res;
194}
195
196/*
Veaceslav Falicod6641cc2013-05-28 01:26:13 +0000197 * Set the slaves in the current bond.
Jiri Pirkof9f35452010-05-18 05:46:39 +0000198 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
199 * All hard work should be done there.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800200 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700201static ssize_t bonding_store_slaves(struct device *d,
202 struct device_attribute *attr,
203 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800204{
205 char command[IFNAMSIZ + 1] = { 0, };
206 char *ifname;
Jiri Pirkof9f35452010-05-18 05:46:39 +0000207 int res, ret = count;
208 struct net_device *dev;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700209 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800210
Eric W. Biederman496a60c2009-05-13 17:02:50 +0000211 if (!rtnl_trylock())
212 return restart_syscall();
Jay Vosburgh027ea042008-01-17 16:25:02 -0800213
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800214 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
215 ifname = command + 1;
216 if ((strlen(command) <= 1) ||
217 !dev_valid_name(ifname))
218 goto err_no_cmd;
219
Jiri Pirkof9f35452010-05-18 05:46:39 +0000220 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
221 if (!dev) {
222 pr_info("%s: Interface %s does not exist!\n",
223 bond->dev->name, ifname);
224 ret = -ENODEV;
225 goto out;
226 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800227
Jiri Pirkof9f35452010-05-18 05:46:39 +0000228 switch (command[0]) {
229 case '+':
230 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800231 res = bond_enslave(bond->dev, dev);
Jiri Pirkof9f35452010-05-18 05:46:39 +0000232 break;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000233
Jiri Pirkof9f35452010-05-18 05:46:39 +0000234 case '-':
235 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
236 res = bond_release(bond->dev, dev);
237 break;
238
239 default:
240 goto err_no_cmd;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800241 }
242
Jiri Pirkof9f35452010-05-18 05:46:39 +0000243 if (res)
244 ret = res;
245 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800246
247err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800248 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
249 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800250 ret = -EPERM;
251
252out:
Jay Vosburgh027ea042008-01-17 16:25:02 -0800253 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800254 return ret;
255}
256
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000257static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
258 bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800259
260/*
261 * Show and set the bonding mode. The bond interface must be down to
262 * change the mode.
263 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700264static ssize_t bonding_show_mode(struct device *d,
265 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800266{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700267 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800268
269 return sprintf(buf, "%s %d\n",
270 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800271 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800272}
273
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700274static ssize_t bonding_store_mode(struct device *d,
275 struct device_attribute *attr,
276 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800277{
Jiri Pirko72be35f2013-10-18 17:43:34 +0200278 int new_value, ret;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700279 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800280
Jay Vosburghece95f72008-01-17 16:25:01 -0800281 new_value = bond_parse_parm(buf, bond_mode_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800282 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800283 pr_err("%s: Ignoring invalid mode value %.*s.\n",
284 bond->dev->name, (int)strlen(buf) - 1, buf);
Jiri Pirko72be35f2013-10-18 17:43:34 +0200285 return -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800286 }
Jiri Pirko72be35f2013-10-18 17:43:34 +0200287 if (!rtnl_trylock())
288 return restart_syscall();
289
290 ret = bond_option_mode_set(bond, new_value);
291 if (!ret) {
292 pr_info("%s: setting mode to %s (%d).\n",
293 bond->dev->name, bond_mode_tbl[new_value].modename,
294 new_value);
295 ret = count;
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000296 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000297
nikolay@redhat.comea6836d2013-05-18 01:18:28 +0000298 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800299 return ret;
300}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000301static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
302 bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800303
304/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000305 * Show and set the bonding transmit hash method.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800306 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700307static ssize_t bonding_show_xmit_hash(struct device *d,
308 struct device_attribute *attr,
309 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800310{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700311 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800312
Wagner Ferenc8e4b9322007-12-06 23:40:32 -0800313 return sprintf(buf, "%s %d\n",
314 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
315 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800316}
317
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700318static ssize_t bonding_store_xmit_hash(struct device *d,
319 struct device_attribute *attr,
320 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800321{
322 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700323 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800324
Jay Vosburghece95f72008-01-17 16:25:01 -0800325 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800326 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800327 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800328 bond->dev->name,
329 (int)strlen(buf) - 1, buf);
330 ret = -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800331 } else {
332 bond->params.xmit_policy = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800333 pr_info("%s: setting xmit hash policy to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000334 bond->dev->name,
335 xmit_hashtype_tbl[new_value].modename, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800336 }
Nikolay Aleksandrov53edee22013-05-24 00:59:47 +0000337
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800338 return ret;
339}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000340static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
341 bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800342
343/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700344 * Show and set arp_validate.
345 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700346static ssize_t bonding_show_arp_validate(struct device *d,
347 struct device_attribute *attr,
348 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700349{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700350 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700351
352 return sprintf(buf, "%s %d\n",
353 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800354 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700355}
356
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700357static ssize_t bonding_store_arp_validate(struct device *d,
358 struct device_attribute *attr,
359 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700360{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700361 struct bonding *bond = to_bond(d);
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200362 int new_value, ret = count;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700363
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200364 if (!rtnl_trylock())
365 return restart_syscall();
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);
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200370 ret = -EINVAL;
371 goto out;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700372 }
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200373 if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800374 pr_err("%s: arp_validate only supported in active-backup mode.\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700375 bond->dev->name);
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200376 ret = -EINVAL;
377 goto out;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700378 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800379 pr_info("%s: setting arp_validate to %s (%d).\n",
380 bond->dev->name, arp_validate_tbl[new_value].modename,
381 new_value);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700382
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200383 if (bond->dev->flags & IFF_UP) {
384 if (!new_value)
385 bond->recv_probe = NULL;
386 else if (bond->params.arp_interval)
387 bond->recv_probe = bond_arp_rcv;
388 }
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700389 bond->params.arp_validate = new_value;
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200390out:
391 rtnl_unlock();
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700392
nikolay@redhat.com5c5038d2013-09-07 00:00:25 +0200393 return ret;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700394}
395
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000396static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
397 bonding_store_arp_validate);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200398/*
399 * Show and set arp_all_targets.
400 */
401static ssize_t bonding_show_arp_all_targets(struct device *d,
402 struct device_attribute *attr,
403 char *buf)
404{
405 struct bonding *bond = to_bond(d);
406 int value = bond->params.arp_all_targets;
407
408 return sprintf(buf, "%s %d\n", arp_all_targets_tbl[value].modename,
409 value);
410}
411
412static ssize_t bonding_store_arp_all_targets(struct device *d,
413 struct device_attribute *attr,
414 const char *buf, size_t count)
415{
416 struct bonding *bond = to_bond(d);
417 int new_value;
418
419 new_value = bond_parse_parm(buf, arp_all_targets_tbl);
420 if (new_value < 0) {
421 pr_err("%s: Ignoring invalid arp_all_targets value %s\n",
422 bond->dev->name, buf);
423 return -EINVAL;
424 }
425 pr_info("%s: setting arp_all_targets to %s (%d).\n",
426 bond->dev->name, arp_all_targets_tbl[new_value].modename,
427 new_value);
428
429 bond->params.arp_all_targets = new_value;
430
431 return count;
432}
433
434static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
435 bonding_show_arp_all_targets, bonding_store_arp_all_targets);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700436
437/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700438 * Show and store fail_over_mac. User only allowed to change the
439 * value when there are no slaves.
440 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000441static ssize_t bonding_show_fail_over_mac(struct device *d,
442 struct device_attribute *attr,
443 char *buf)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700444{
445 struct bonding *bond = to_bond(d);
446
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700447 return sprintf(buf, "%s %d\n",
448 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
449 bond->params.fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700450}
451
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000452static ssize_t bonding_store_fail_over_mac(struct device *d,
453 struct device_attribute *attr,
454 const char *buf, size_t count)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700455{
dingtianhong9402b742013-07-23 15:25:39 +0800456 int new_value, ret = count;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700457 struct bonding *bond = to_bond(d);
458
dingtianhong9402b742013-07-23 15:25:39 +0800459 if (!rtnl_trylock())
460 return restart_syscall();
461
Veaceslav Falico0965a1f2013-09-25 09:20:21 +0200462 if (bond_has_slaves(bond)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800463 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
Jay Vosburghdd957c52007-10-09 19:57:24 -0700464 bond->dev->name);
dingtianhong9402b742013-07-23 15:25:39 +0800465 ret = -EPERM;
466 goto out;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700467 }
468
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700469 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
470 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800471 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700472 bond->dev->name, buf);
dingtianhong9402b742013-07-23 15:25:39 +0800473 ret = -EINVAL;
474 goto out;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700475 }
476
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700477 bond->params.fail_over_mac = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800478 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
479 bond->dev->name, fail_over_mac_tbl[new_value].modename,
480 new_value);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700481
dingtianhong9402b742013-07-23 15:25:39 +0800482out:
483 rtnl_unlock();
484 return ret;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700485}
486
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000487static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
488 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700489
490/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800491 * Show and set the arp timer interval. There are two tricky bits
492 * here. First, if ARP monitoring is activated, then we must disable
493 * MII monitoring. Second, if the ARP timer isn't running, we must
494 * start it.
495 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700496static ssize_t bonding_show_arp_interval(struct device *d,
497 struct device_attribute *attr,
498 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800499{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700500 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800501
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800502 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800503}
504
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700505static ssize_t bonding_store_arp_interval(struct device *d,
506 struct device_attribute *attr,
507 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800508{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700509 struct bonding *bond = to_bond(d);
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200510 int new_value, ret = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800511
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000512 if (!rtnl_trylock())
513 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800514 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800515 pr_err("%s: no arp_interval value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800516 bond->dev->name);
517 ret = -EINVAL;
518 goto out;
519 }
520 if (new_value < 0) {
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000521 pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800522 bond->dev->name, new_value, INT_MAX);
523 ret = -EINVAL;
524 goto out;
525 }
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000526 if (bond->params.mode == BOND_MODE_ALB ||
Veaceslav Falicoec9f1d12013-11-12 15:37:40 +0100527 bond->params.mode == BOND_MODE_TLB ||
528 bond->params.mode == BOND_MODE_8023AD) {
529 pr_info("%s: ARP monitoring cannot be used with ALB/TLB/802.3ad. Only MII monitoring is supported on %s.\n",
Andy Gospodarekc5cb0022010-07-28 15:13:56 +0000530 bond->dev->name, bond->dev->name);
531 ret = -EINVAL;
532 goto out;
533 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800534 pr_info("%s: Setting ARP monitoring interval to %d.\n",
535 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800536 bond->params.arp_interval = new_value;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000537 if (new_value) {
538 if (bond->params.miimon) {
539 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
540 bond->dev->name, bond->dev->name);
541 bond->params.miimon = 0;
542 }
543 if (!bond->params.arp_targets[0])
544 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
545 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800546 }
547 if (bond->dev->flags & IFF_UP) {
548 /* If the interface is up, we may need to fire off
549 * the ARP timer. If the interface is down, the
550 * timer will get fired off when the open function
551 * is called.
552 */
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000553 if (!new_value) {
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200554 if (bond->params.arp_validate)
555 bond->recv_probe = NULL;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000556 cancel_delayed_work_sync(&bond->arp_work);
557 } else {
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +0200558 /* arp_validate can be set only in active-backup mode */
559 if (bond->params.arp_validate)
560 bond->recv_probe = bond_arp_rcv;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000561 cancel_delayed_work_sync(&bond->mii_work);
562 queue_delayed_work(bond->wq, &bond->arp_work, 0);
563 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800564 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800565out:
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000566 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800567 return ret;
568}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000569static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
570 bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800571
572/*
573 * Show and set the arp targets.
574 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700575static ssize_t bonding_show_arp_targets(struct device *d,
576 struct device_attribute *attr,
577 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800578{
579 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700580 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800581
582 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
583 if (bond->params.arp_targets[i])
Harvey Harrison63779432008-10-31 00:56:00 -0700584 res += sprintf(buf + res, "%pI4 ",
585 &bond->params.arp_targets[i]);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800586 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800587 if (res)
588 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800589 return res;
590}
591
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700592static ssize_t bonding_store_arp_targets(struct device *d,
593 struct device_attribute *attr,
594 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800595{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700596 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200597 struct list_head *iter;
Veaceslav Falico8599b522013-06-24 11:49:34 +0200598 struct slave *slave;
599 __be32 newtarget, *targets;
600 unsigned long *targets_rx;
601 int ind, i, j, ret = -EINVAL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800602
dingtianhong4d1ae5f2013-10-15 16:28:42 +0800603 if (!rtnl_trylock())
604 return restart_syscall();
605
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800606 targets = bond->params.arp_targets;
607 newtarget = in_aton(buf + 1);
608 /* look for adds */
609 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400610 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800611 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700612 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800613 goto out;
614 }
615
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200616 if (bond_get_targets_ip(targets, newtarget) != -1) { /* dup */
617 pr_err("%s: ARP target %pI4 is already present\n",
618 bond->dev->name, &newtarget);
619 goto out;
620 }
621
Veaceslav Falico8599b522013-06-24 11:49:34 +0200622 ind = bond_get_targets_ip(targets, 0); /* first free slot */
623 if (ind == -1) {
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200624 pr_err("%s: ARP target table is full!\n",
625 bond->dev->name);
626 goto out;
627 }
628
629 pr_info("%s: adding ARP target %pI4.\n", bond->dev->name,
630 &newtarget);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200631 /* not to race with bond_arp_rcv */
632 write_lock_bh(&bond->lock);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200633 bond_for_each_slave(bond, slave, iter)
Veaceslav Falico8599b522013-06-24 11:49:34 +0200634 slave->target_last_arp_rx[ind] = jiffies;
635 targets[ind] = newtarget;
636 write_unlock_bh(&bond->lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000637 } else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400638 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800639 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700640 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800641 goto out;
642 }
643
Veaceslav Falico8599b522013-06-24 11:49:34 +0200644 ind = bond_get_targets_ip(targets, newtarget);
645 if (ind == -1) {
646 pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800647 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800648 goto out;
649 }
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200650
Veaceslav Falico8599b522013-06-24 11:49:34 +0200651 if (ind == 0 && !targets[1] && bond->params.arp_interval)
652 pr_warn("%s: removing last arp target with arp_interval on\n",
653 bond->dev->name);
654
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200655 pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
656 &newtarget);
Veaceslav Falico8599b522013-06-24 11:49:34 +0200657
658 write_lock_bh(&bond->lock);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +0200659 bond_for_each_slave(bond, slave, iter) {
Veaceslav Falico8599b522013-06-24 11:49:34 +0200660 targets_rx = slave->target_last_arp_rx;
661 j = ind;
662 for (; (j < BOND_MAX_ARP_TARGETS-1) && targets[j+1]; j++)
663 targets_rx[j] = targets_rx[j+1];
664 targets_rx[j] = 0;
665 }
666 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200667 targets[i] = targets[i+1];
668 targets[i] = 0;
Veaceslav Falico8599b522013-06-24 11:49:34 +0200669 write_unlock_bh(&bond->lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000670 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800671 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
672 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800673 ret = -EPERM;
674 goto out;
675 }
676
Veaceslav Falico87a7b842013-06-24 11:49:29 +0200677 ret = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800678out:
dingtianhong4d1ae5f2013-10-15 16:28:42 +0800679 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800680 return ret;
681}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700682static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800683
684/*
685 * Show and set the up and down delays. These must be multiples of the
686 * MII monitoring value, and are stored internally as the multiplier.
687 * Thus, we must translate to MS for the real world.
688 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700689static ssize_t bonding_show_downdelay(struct device *d,
690 struct device_attribute *attr,
691 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800692{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700693 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800694
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800695 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800696}
697
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700698static ssize_t bonding_store_downdelay(struct device *d,
699 struct device_attribute *attr,
700 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800701{
702 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700703 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800704
Nikolay Aleksandrovb869ccf2013-11-13 17:07:46 +0100705 if (!rtnl_trylock())
706 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800707 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800708 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800709 bond->dev->name);
710 ret = -EPERM;
711 goto out;
712 }
713
714 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800715 pr_err("%s: no down delay value specified.\n", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800716 ret = -EINVAL;
717 goto out;
718 }
719 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800720 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000721 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800722 ret = -EINVAL;
723 goto out;
724 } else {
725 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800726 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 +0000727 bond->dev->name, new_value,
728 bond->params.miimon,
729 (new_value / bond->params.miimon) *
730 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800731 }
732 bond->params.downdelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800733 pr_info("%s: Setting down delay to %d.\n",
734 bond->dev->name,
735 bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800736
737 }
738
739out:
Nikolay Aleksandrovb869ccf2013-11-13 17:07:46 +0100740 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800741 return ret;
742}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000743static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
744 bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800745
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700746static ssize_t bonding_show_updelay(struct device *d,
747 struct device_attribute *attr,
748 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800749{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700750 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800751
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800752 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800753
754}
755
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700756static ssize_t bonding_store_updelay(struct device *d,
757 struct device_attribute *attr,
758 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800759{
760 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700761 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800762
Nikolay Aleksandrovb869ccf2013-11-13 17:07:46 +0100763 if (!rtnl_trylock())
764 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800765 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800766 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800767 bond->dev->name);
768 ret = -EPERM;
769 goto out;
770 }
771
772 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800773 pr_err("%s: no up delay value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800774 bond->dev->name);
775 ret = -EINVAL;
776 goto out;
777 }
778 if (new_value < 0) {
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000779 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
780 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800781 ret = -EINVAL;
782 goto out;
783 } else {
784 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800785 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 +0000786 bond->dev->name, new_value,
787 bond->params.miimon,
788 (new_value / bond->params.miimon) *
789 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800790 }
791 bond->params.updelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800792 pr_info("%s: Setting up delay to %d.\n",
793 bond->dev->name,
794 bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800795 }
796
797out:
Nikolay Aleksandrovb869ccf2013-11-13 17:07:46 +0100798 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800799 return ret;
800}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000801static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
802 bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800803
804/*
805 * Show and set the LACP interval. Interface must be down, and the mode
806 * must be set to 802.3ad mode.
807 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700808static ssize_t bonding_show_lacp(struct device *d,
809 struct device_attribute *attr,
810 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800811{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700812 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800813
814 return sprintf(buf, "%s %d\n",
815 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800816 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800817}
818
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700819static ssize_t bonding_store_lacp(struct device *d,
820 struct device_attribute *attr,
821 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800822{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700823 struct bonding *bond = to_bond(d);
nikolay@redhat.comc5093162013-09-02 13:51:40 +0200824 int new_value, ret = count;
825
826 if (!rtnl_trylock())
827 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800828
829 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800830 pr_err("%s: Unable to update LACP rate because interface is up.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800831 bond->dev->name);
832 ret = -EPERM;
833 goto out;
834 }
835
836 if (bond->params.mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800837 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800838 bond->dev->name);
839 ret = -EPERM;
840 goto out;
841 }
842
Jay Vosburghece95f72008-01-17 16:25:01 -0800843 new_value = bond_parse_parm(buf, bond_lacp_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800844
845 if ((new_value == 1) || (new_value == 0)) {
846 bond->params.lacp_fast = new_value;
Peter Pan(潘卫平)ba824a82011-06-08 21:19:01 +0000847 bond_3ad_update_lacp_rate(bond);
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800848 pr_info("%s: Setting LACP rate to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000849 bond->dev->name, bond_lacp_tbl[new_value].modename,
850 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800851 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800852 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000853 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800854 ret = -EINVAL;
855 }
856out:
nikolay@redhat.comc5093162013-09-02 13:51:40 +0200857 rtnl_unlock();
858
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800859 return ret;
860}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000861static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
862 bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800863
stephen hemminger655f8912011-06-22 09:54:39 +0000864static ssize_t bonding_show_min_links(struct device *d,
865 struct device_attribute *attr,
866 char *buf)
867{
868 struct bonding *bond = to_bond(d);
869
870 return sprintf(buf, "%d\n", bond->params.min_links);
871}
872
873static ssize_t bonding_store_min_links(struct device *d,
874 struct device_attribute *attr,
875 const char *buf, size_t count)
876{
877 struct bonding *bond = to_bond(d);
878 int ret;
879 unsigned int new_value;
880
881 ret = kstrtouint(buf, 0, &new_value);
882 if (ret < 0) {
883 pr_err("%s: Ignoring invalid min links value %s.\n",
884 bond->dev->name, buf);
885 return ret;
886 }
887
888 pr_info("%s: Setting min links value to %u\n",
889 bond->dev->name, new_value);
890 bond->params.min_links = new_value;
891 return count;
892}
893static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
894 bonding_show_min_links, bonding_store_min_links);
895
Jay Vosburghfd989c82008-11-04 17:51:16 -0800896static ssize_t bonding_show_ad_select(struct device *d,
897 struct device_attribute *attr,
898 char *buf)
899{
900 struct bonding *bond = to_bond(d);
901
902 return sprintf(buf, "%s %d\n",
903 ad_select_tbl[bond->params.ad_select].modename,
904 bond->params.ad_select);
905}
906
907
908static ssize_t bonding_store_ad_select(struct device *d,
909 struct device_attribute *attr,
910 const char *buf, size_t count)
911{
912 int new_value, ret = count;
913 struct bonding *bond = to_bond(d);
914
915 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800916 pr_err("%s: Unable to update ad_select because interface is up.\n",
917 bond->dev->name);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800918 ret = -EPERM;
919 goto out;
920 }
921
922 new_value = bond_parse_parm(buf, ad_select_tbl);
923
924 if (new_value != -1) {
925 bond->params.ad_select = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800926 pr_info("%s: Setting ad_select to %s (%d).\n",
927 bond->dev->name, ad_select_tbl[new_value].modename,
928 new_value);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800929 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800930 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -0800931 bond->dev->name, (int)strlen(buf) - 1, buf);
932 ret = -EINVAL;
933 }
934out:
935 return ret;
936}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000937static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
938 bonding_show_ad_select, bonding_store_ad_select);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800939
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800940/*
Ben Hutchingsad246c92011-04-26 15:25:52 +0000941 * Show and set the number of peer notifications to send after a failover event.
942 */
943static ssize_t bonding_show_num_peer_notif(struct device *d,
944 struct device_attribute *attr,
945 char *buf)
946{
947 struct bonding *bond = to_bond(d);
948 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
949}
950
951static ssize_t bonding_store_num_peer_notif(struct device *d,
952 struct device_attribute *attr,
953 const char *buf, size_t count)
954{
955 struct bonding *bond = to_bond(d);
956 int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
957 return err ? err : count;
958}
959static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
960 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
961static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
962 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
963
964/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800965 * Show and set the MII monitor interval. There are two tricky bits
966 * here. First, if MII monitoring is activated, then we must disable
967 * ARP monitoring. Second, if the timer isn't running, we must
968 * start it.
969 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700970static ssize_t bonding_show_miimon(struct device *d,
971 struct device_attribute *attr,
972 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800973{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700974 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800975
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800976 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800977}
978
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700979static ssize_t bonding_store_miimon(struct device *d,
980 struct device_attribute *attr,
981 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800982{
983 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700984 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800985
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +0000986 if (!rtnl_trylock())
987 return restart_syscall();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800988 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800989 pr_err("%s: no miimon value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800990 bond->dev->name);
991 ret = -EINVAL;
992 goto out;
993 }
994 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800995 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000996 bond->dev->name, new_value, 0, INT_MAX);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800997 ret = -EINVAL;
998 goto out;
nikolay@redhat.com1bc7db12013-03-27 03:32:41 +0000999 }
1000 pr_info("%s: Setting MII monitoring interval to %d.\n",
1001 bond->dev->name, new_value);
1002 bond->params.miimon = new_value;
1003 if (bond->params.updelay)
1004 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
1005 bond->dev->name,
1006 bond->params.updelay * bond->params.miimon);
1007 if (bond->params.downdelay)
1008 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
1009 bond->dev->name,
1010 bond->params.downdelay * bond->params.miimon);
1011 if (new_value && bond->params.arp_interval) {
1012 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
1013 bond->dev->name);
1014 bond->params.arp_interval = 0;
1015 if (bond->params.arp_validate)
1016 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
1017 }
1018 if (bond->dev->flags & IFF_UP) {
1019 /* If the interface is up, we may need to fire off
1020 * the MII timer. If the interface is down, the
1021 * timer will get fired off when the open function
1022 * is called.
1023 */
1024 if (!new_value) {
1025 cancel_delayed_work_sync(&bond->mii_work);
1026 } else {
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00001027 cancel_delayed_work_sync(&bond->arp_work);
1028 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001029 }
1030 }
1031out:
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00001032 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001033 return ret;
1034}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001035static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1036 bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001037
1038/*
1039 * Show and set the primary slave. The store function is much
1040 * simpler than bonding_store_slaves function because it only needs to
1041 * handle one interface name.
1042 * The bond must be a mode that supports a primary for this be
1043 * set.
1044 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001045static ssize_t bonding_show_primary(struct device *d,
1046 struct device_attribute *attr,
1047 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001048{
1049 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001050 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001051
1052 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001053 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001054
1055 return count;
1056}
1057
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001058static ssize_t bonding_store_primary(struct device *d,
1059 struct device_attribute *attr,
1060 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001061{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001062 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001063 struct list_head *iter;
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001064 char ifname[IFNAMSIZ];
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001065 struct slave *slave;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001066
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001067 if (!rtnl_trylock())
1068 return restart_syscall();
Neil Hormane843fa52010-10-13 16:01:50 +00001069 block_netpoll_tx();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001070 read_lock(&bond->lock);
1071 write_lock_bh(&bond->curr_slave_lock);
1072
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001073 if (!USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001074 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1075 bond->dev->name, bond->dev->name, bond->params.mode);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001076 goto out;
1077 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001078
nikolay@redhat.comeb6e98a2012-10-31 04:42:51 +00001079 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001080
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001081 /* check to see if we are clearing primary */
1082 if (!strlen(ifname) || buf[0] == '\n') {
1083 pr_info("%s: Setting primary slave to None.\n",
1084 bond->dev->name);
1085 bond->primary_slave = NULL;
Milos Vyleteleb492f72013-01-29 09:59:00 +00001086 memset(bond->params.primary, 0, sizeof(bond->params.primary));
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001087 bond_select_active_slave(bond);
1088 goto out;
1089 }
1090
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001091 bond_for_each_slave(bond, slave, iter) {
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001092 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1093 pr_info("%s: Setting %s as primary slave.\n",
1094 bond->dev->name, slave->dev->name);
1095 bond->primary_slave = slave;
1096 strcpy(bond->params.primary, slave->dev->name);
1097 bond_select_active_slave(bond);
1098 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001099 }
1100 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001101
Weiping Pan8a936642012-06-10 23:00:20 +00001102 strncpy(bond->params.primary, ifname, IFNAMSIZ);
1103 bond->params.primary[IFNAMSIZ - 1] = 0;
1104
1105 pr_info("%s: Recording %s as primary, "
1106 "but it has not been enslaved to %s yet.\n",
1107 bond->dev->name, ifname, bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001108out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001109 write_unlock_bh(&bond->curr_slave_lock);
1110 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001111 unblock_netpoll_tx();
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001112 rtnl_unlock();
1113
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001114 return count;
1115}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001116static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1117 bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001118
1119/*
Jiri Pirkoa5499522009-09-25 03:28:09 +00001120 * Show and set the primary_reselect flag.
1121 */
1122static ssize_t bonding_show_primary_reselect(struct device *d,
1123 struct device_attribute *attr,
1124 char *buf)
1125{
1126 struct bonding *bond = to_bond(d);
1127
1128 return sprintf(buf, "%s %d\n",
1129 pri_reselect_tbl[bond->params.primary_reselect].modename,
1130 bond->params.primary_reselect);
1131}
1132
1133static ssize_t bonding_store_primary_reselect(struct device *d,
1134 struct device_attribute *attr,
1135 const char *buf, size_t count)
1136{
1137 int new_value, ret = count;
1138 struct bonding *bond = to_bond(d);
1139
1140 if (!rtnl_trylock())
1141 return restart_syscall();
1142
1143 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1144 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001145 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001146 bond->dev->name,
1147 (int) strlen(buf) - 1, buf);
1148 ret = -EINVAL;
1149 goto out;
1150 }
1151
1152 bond->params.primary_reselect = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001153 pr_info("%s: setting primary_reselect to %s (%d).\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001154 bond->dev->name, pri_reselect_tbl[new_value].modename,
1155 new_value);
1156
Neil Hormane843fa52010-10-13 16:01:50 +00001157 block_netpoll_tx();
Jiri Pirkoa5499522009-09-25 03:28:09 +00001158 read_lock(&bond->lock);
1159 write_lock_bh(&bond->curr_slave_lock);
1160 bond_select_active_slave(bond);
1161 write_unlock_bh(&bond->curr_slave_lock);
1162 read_unlock(&bond->lock);
Neil Hormane843fa52010-10-13 16:01:50 +00001163 unblock_netpoll_tx();
Jiri Pirkoa5499522009-09-25 03:28:09 +00001164out:
1165 rtnl_unlock();
1166 return ret;
1167}
1168static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1169 bonding_show_primary_reselect,
1170 bonding_store_primary_reselect);
1171
1172/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001173 * Show and set the use_carrier flag.
1174 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001175static ssize_t bonding_show_carrier(struct device *d,
1176 struct device_attribute *attr,
1177 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001178{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001179 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001180
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001181 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001182}
1183
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001184static ssize_t bonding_store_carrier(struct device *d,
1185 struct device_attribute *attr,
1186 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001187{
1188 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001189 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001190
1191
1192 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001193 pr_err("%s: no use_carrier value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001194 bond->dev->name);
1195 ret = -EINVAL;
1196 goto out;
1197 }
1198 if ((new_value == 0) || (new_value == 1)) {
1199 bond->params.use_carrier = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001200 pr_info("%s: Setting use_carrier to %d.\n",
1201 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001202 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001203 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1204 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001205 }
1206out:
Jiri Pirko672bda32011-01-25 11:03:25 +00001207 return ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001208}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001209static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1210 bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001211
1212
1213/*
1214 * Show and set currently active_slave.
1215 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001216static ssize_t bonding_show_active_slave(struct device *d,
1217 struct device_attribute *attr,
1218 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001219{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001220 struct bonding *bond = to_bond(d);
Jiri Pirko752d48b2013-10-18 17:43:37 +02001221 struct net_device *slave_dev;
Wagner Ferenc16cd0162007-12-06 23:40:29 -08001222 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001223
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001224 rcu_read_lock();
Jiri Pirko752d48b2013-10-18 17:43:37 +02001225 slave_dev = bond_option_active_slave_get_rcu(bond);
1226 if (slave_dev)
1227 count = sprintf(buf, "%s\n", slave_dev->name);
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001228 rcu_read_unlock();
1229
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001230 return count;
1231}
1232
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001233static ssize_t bonding_store_active_slave(struct device *d,
1234 struct device_attribute *attr,
1235 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001236{
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001237 int ret;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001238 struct bonding *bond = to_bond(d);
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001239 char ifname[IFNAMSIZ];
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001240 struct net_device *dev;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001241
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001242 if (!rtnl_trylock())
1243 return restart_syscall();
Neil Hormane843fa52010-10-13 16:01:50 +00001244
nikolay@redhat.comc84e1592012-10-31 06:03:52 +00001245 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001246 if (!strlen(ifname) || buf[0] == '\n') {
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001247 dev = NULL;
1248 } else {
1249 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1250 if (!dev) {
1251 ret = -ENODEV;
1252 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001253 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001254 }
Andy Gospodarekf4bb2e92011-07-26 11:12:27 +00001255
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001256 ret = bond_option_active_slave_set(bond, dev);
1257 if (!ret)
1258 ret = count;
Neil Hormane843fa52010-10-13 16:01:50 +00001259
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001260 out:
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001261 rtnl_unlock();
1262
Jiri Pirkod9e32b22013-10-18 17:43:35 +02001263 return ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001264
1265}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001266static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1267 bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001268
1269
1270/*
1271 * Show link status of the bond interface.
1272 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001273static ssize_t bonding_show_mii_status(struct device *d,
1274 struct device_attribute *attr,
1275 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001276{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001277 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001278
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001279 return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001280}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001281static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001282
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001283/*
1284 * Show current 802.3ad aggregator ID.
1285 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001286static ssize_t bonding_show_ad_aggregator(struct device *d,
1287 struct device_attribute *attr,
1288 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001289{
1290 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001291 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001292
1293 if (bond->params.mode == BOND_MODE_8023AD) {
1294 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001295 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001296 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001297 ? 0 : ad_info.aggregator_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001298 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001299
1300 return count;
1301}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001302static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001303
1304
1305/*
1306 * Show number of active 802.3ad ports.
1307 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001308static ssize_t bonding_show_ad_num_ports(struct device *d,
1309 struct device_attribute *attr,
1310 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001311{
1312 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001313 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001314
1315 if (bond->params.mode == BOND_MODE_8023AD) {
1316 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001317 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001318 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001319 ? 0 : ad_info.ports);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001320 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001321
1322 return count;
1323}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001324static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001325
1326
1327/*
1328 * Show current 802.3ad actor key.
1329 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001330static ssize_t bonding_show_ad_actor_key(struct device *d,
1331 struct device_attribute *attr,
1332 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001333{
1334 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001335 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001336
1337 if (bond->params.mode == BOND_MODE_8023AD) {
1338 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001339 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001340 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001341 ? 0 : ad_info.actor_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001342 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001343
1344 return count;
1345}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001346static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001347
1348
1349/*
1350 * Show current 802.3ad partner key.
1351 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001352static ssize_t bonding_show_ad_partner_key(struct device *d,
1353 struct device_attribute *attr,
1354 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001355{
1356 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001357 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001358
1359 if (bond->params.mode == BOND_MODE_8023AD) {
1360 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001361 count = sprintf(buf, "%d\n",
nikolay@redhat.com318debd2013-05-18 01:18:31 +00001362 bond_3ad_get_active_agg_info(bond, &ad_info)
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001363 ? 0 : ad_info.partner_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001364 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001365
1366 return count;
1367}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001368static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001369
1370
1371/*
1372 * Show current 802.3ad partner mac.
1373 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001374static ssize_t bonding_show_ad_partner_mac(struct device *d,
1375 struct device_attribute *attr,
1376 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001377{
1378 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001379 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001380
1381 if (bond->params.mode == BOND_MODE_8023AD) {
1382 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001383 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
Johannes Berge1749612008-10-27 15:59:26 -07001384 count = sprintf(buf, "%pM\n", ad_info.partner_system);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001385 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001386
1387 return count;
1388}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001389static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001390
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001391/*
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001392 * Show the queue_ids of the slaves in the current bond.
1393 */
1394static ssize_t bonding_show_queue_id(struct device *d,
1395 struct device_attribute *attr,
1396 char *buf)
1397{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001398 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001399 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001400 struct slave *slave;
1401 int res = 0;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001402
1403 if (!rtnl_trylock())
1404 return restart_syscall();
1405
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001406 bond_for_each_slave(bond, slave, iter) {
Nicolas de Pesloüan79236682010-07-14 18:24:54 -07001407 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1408 /* not enough space for another interface_name:queue_id pair */
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001409 if ((PAGE_SIZE - res) > 10)
1410 res = PAGE_SIZE - 10;
1411 res += sprintf(buf + res, "++more++ ");
1412 break;
1413 }
1414 res += sprintf(buf + res, "%s:%d ",
1415 slave->dev->name, slave->queue_id);
1416 }
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001417 if (res)
1418 buf[res-1] = '\n'; /* eat the leftover space */
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001419
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001420 rtnl_unlock();
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001421
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001422 return res;
1423}
1424
1425/*
1426 * Set the queue_ids of the slaves in the current bond. The bond
1427 * interface must be enslaved for this to work.
1428 */
1429static ssize_t bonding_store_queue_id(struct device *d,
1430 struct device_attribute *attr,
1431 const char *buffer, size_t count)
1432{
1433 struct slave *slave, *update_slave;
1434 struct bonding *bond = to_bond(d);
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001435 struct list_head *iter;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001436 u16 qid;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001437 int ret = count;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001438 char *delim;
1439 struct net_device *sdev = NULL;
1440
1441 if (!rtnl_trylock())
1442 return restart_syscall();
1443
1444 /* delim will point to queue id if successful */
1445 delim = strchr(buffer, ':');
1446 if (!delim)
1447 goto err_no_cmd;
1448
1449 /*
1450 * Terminate string that points to device name and bump it
1451 * up one, so we can read the queue id there.
1452 */
1453 *delim = '\0';
1454 if (sscanf(++delim, "%hd\n", &qid) != 1)
1455 goto err_no_cmd;
1456
1457 /* Check buffer length, valid ifname and queue id */
1458 if (strlen(buffer) > IFNAMSIZ ||
1459 !dev_valid_name(buffer) ||
Jiri Pirko8a540ff2012-07-20 02:28:50 +00001460 qid > bond->dev->real_num_tx_queues)
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001461 goto err_no_cmd;
1462
1463 /* Get the pointer to that interface if it exists */
1464 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1465 if (!sdev)
1466 goto err_no_cmd;
1467
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001468 /* Search for thes slave and check for duplicate qids */
1469 update_slave = NULL;
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001470 bond_for_each_slave(bond, slave, iter) {
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001471 if (sdev == slave->dev)
1472 /*
1473 * We don't need to check the matching
1474 * slave for dups, since we're overwriting it
1475 */
1476 update_slave = slave;
1477 else if (qid && qid == slave->queue_id) {
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001478 goto err_no_cmd;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001479 }
1480 }
1481
1482 if (!update_slave)
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001483 goto err_no_cmd;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001484
1485 /* Actually set the qids for the slave */
1486 update_slave->queue_id = qid;
1487
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001488out:
1489 rtnl_unlock();
1490 return ret;
1491
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001492err_no_cmd:
1493 pr_info("invalid input for queue_id set for %s.\n",
1494 bond->dev->name);
1495 ret = -EPERM;
1496 goto out;
1497}
1498
1499static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1500 bonding_store_queue_id);
1501
1502
1503/*
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001504 * Show and set the all_slaves_active flag.
1505 */
1506static ssize_t bonding_show_slaves_active(struct device *d,
1507 struct device_attribute *attr,
1508 char *buf)
1509{
1510 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001511
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001512 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1513}
1514
1515static ssize_t bonding_store_slaves_active(struct device *d,
1516 struct device_attribute *attr,
1517 const char *buf, size_t count)
1518{
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001519 struct bonding *bond = to_bond(d);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001520 int new_value, ret = count;
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001521 struct list_head *iter;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001522 struct slave *slave;
1523
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001524 if (!rtnl_trylock())
1525 return restart_syscall();
1526
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001527 if (sscanf(buf, "%d", &new_value) != 1) {
1528 pr_err("%s: no all_slaves_active value specified.\n",
1529 bond->dev->name);
1530 ret = -EINVAL;
1531 goto out;
1532 }
1533
1534 if (new_value == bond->params.all_slaves_active)
1535 goto out;
1536
1537 if ((new_value == 0) || (new_value == 1)) {
1538 bond->params.all_slaves_active = new_value;
1539 } else {
1540 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1541 bond->dev->name, new_value);
1542 ret = -EINVAL;
1543 goto out;
1544 }
1545
Veaceslav Falico9caff1e2013-09-25 09:20:14 +02001546 bond_for_each_slave(bond, slave, iter) {
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001547 if (!bond_is_active_slave(slave)) {
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001548 if (new_value)
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001549 slave->inactive = 0;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001550 else
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001551 slave->inactive = 1;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001552 }
1553 }
1554out:
dingtianhong4d1ae5f2013-10-15 16:28:42 +08001555 rtnl_unlock();
Jiri Pirko672bda32011-01-25 11:03:25 +00001556 return ret;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001557}
1558static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1559 bonding_show_slaves_active, bonding_store_slaves_active);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001560
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001561/*
1562 * Show and set the number of IGMP membership reports to send on link failure
1563 */
1564static ssize_t bonding_show_resend_igmp(struct device *d,
Flavio Leitner94265cf2011-05-25 08:38:58 +00001565 struct device_attribute *attr,
1566 char *buf)
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001567{
1568 struct bonding *bond = to_bond(d);
1569
1570 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1571}
1572
1573static ssize_t bonding_store_resend_igmp(struct device *d,
Flavio Leitner94265cf2011-05-25 08:38:58 +00001574 struct device_attribute *attr,
1575 const char *buf, size_t count)
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001576{
1577 int new_value, ret = count;
1578 struct bonding *bond = to_bond(d);
1579
1580 if (sscanf(buf, "%d", &new_value) != 1) {
1581 pr_err("%s: no resend_igmp value specified.\n",
1582 bond->dev->name);
1583 ret = -EINVAL;
1584 goto out;
1585 }
1586
Flavio Leitner94265cf2011-05-25 08:38:58 +00001587 if (new_value < 0 || new_value > 255) {
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001588 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1589 bond->dev->name, new_value);
1590 ret = -EINVAL;
1591 goto out;
1592 }
1593
1594 pr_info("%s: Setting resend_igmp to %d.\n",
1595 bond->dev->name, new_value);
1596 bond->params.resend_igmp = new_value;
1597out:
1598 return ret;
1599}
1600
1601static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1602 bonding_show_resend_igmp, bonding_store_resend_igmp);
1603
Neil Horman7eacd032013-09-13 11:05:33 -04001604
1605static ssize_t bonding_show_lp_interval(struct device *d,
1606 struct device_attribute *attr,
1607 char *buf)
1608{
1609 struct bonding *bond = to_bond(d);
1610 return sprintf(buf, "%d\n", bond->params.lp_interval);
1611}
1612
1613static ssize_t bonding_store_lp_interval(struct device *d,
1614 struct device_attribute *attr,
1615 const char *buf, size_t count)
1616{
1617 struct bonding *bond = to_bond(d);
1618 int new_value, ret = count;
1619
1620 if (sscanf(buf, "%d", &new_value) != 1) {
1621 pr_err("%s: no lp interval value specified.\n",
1622 bond->dev->name);
1623 ret = -EINVAL;
1624 goto out;
1625 }
1626
1627 if (new_value <= 0) {
1628 pr_err ("%s: lp_interval must be between 1 and %d\n",
1629 bond->dev->name, INT_MAX);
1630 ret = -EINVAL;
1631 goto out;
1632 }
1633
1634 bond->params.lp_interval = new_value;
1635out:
1636 return ret;
1637}
1638
1639static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1640 bonding_show_lp_interval, bonding_store_lp_interval);
1641
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01001642static ssize_t bonding_show_packets_per_slave(struct device *d,
1643 struct device_attribute *attr,
1644 char *buf)
1645{
1646 struct bonding *bond = to_bond(d);
1647 int packets_per_slave = bond->params.packets_per_slave;
1648
1649 if (packets_per_slave > 1)
1650 packets_per_slave = reciprocal_value(packets_per_slave);
1651
1652 return sprintf(buf, "%d\n", packets_per_slave);
1653}
1654
1655static ssize_t bonding_store_packets_per_slave(struct device *d,
1656 struct device_attribute *attr,
1657 const char *buf, size_t count)
1658{
1659 struct bonding *bond = to_bond(d);
1660 int new_value, ret = count;
1661
1662 if (sscanf(buf, "%d", &new_value) != 1) {
1663 pr_err("%s: no packets_per_slave value specified.\n",
1664 bond->dev->name);
1665 ret = -EINVAL;
1666 goto out;
1667 }
1668 if (new_value < 0 || new_value > USHRT_MAX) {
1669 pr_err("%s: packets_per_slave must be between 0 and %u\n",
1670 bond->dev->name, USHRT_MAX);
1671 ret = -EINVAL;
1672 goto out;
1673 }
1674 if (bond->params.mode != BOND_MODE_ROUNDROBIN)
1675 pr_warn("%s: Warning: packets_per_slave has effect only in balance-rr mode\n",
1676 bond->dev->name);
1677 if (new_value > 1)
1678 bond->params.packets_per_slave = reciprocal_value(new_value);
1679 else
1680 bond->params.packets_per_slave = new_value;
1681out:
1682 return ret;
1683}
1684
1685static DEVICE_ATTR(packets_per_slave, S_IRUGO | S_IWUSR,
1686 bonding_show_packets_per_slave,
1687 bonding_store_packets_per_slave);
1688
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001689static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001690 &dev_attr_slaves.attr,
1691 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001692 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001693 &dev_attr_arp_validate.attr,
Veaceslav Falico8599b522013-06-24 11:49:34 +02001694 &dev_attr_arp_all_targets.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001695 &dev_attr_arp_interval.attr,
1696 &dev_attr_arp_ip_target.attr,
1697 &dev_attr_downdelay.attr,
1698 &dev_attr_updelay.attr,
1699 &dev_attr_lacp_rate.attr,
Jay Vosburghfd989c82008-11-04 17:51:16 -08001700 &dev_attr_ad_select.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001701 &dev_attr_xmit_hash_policy.attr,
Ben Hutchingsad246c92011-04-26 15:25:52 +00001702 &dev_attr_num_grat_arp.attr,
1703 &dev_attr_num_unsol_na.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001704 &dev_attr_miimon.attr,
1705 &dev_attr_primary.attr,
Jiri Pirkoa5499522009-09-25 03:28:09 +00001706 &dev_attr_primary_reselect.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001707 &dev_attr_use_carrier.attr,
1708 &dev_attr_active_slave.attr,
1709 &dev_attr_mii_status.attr,
1710 &dev_attr_ad_aggregator.attr,
1711 &dev_attr_ad_num_ports.attr,
1712 &dev_attr_ad_actor_key.attr,
1713 &dev_attr_ad_partner_key.attr,
1714 &dev_attr_ad_partner_mac.attr,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001715 &dev_attr_queue_id.attr,
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001716 &dev_attr_all_slaves_active.attr,
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001717 &dev_attr_resend_igmp.attr,
stephen hemminger655f8912011-06-22 09:54:39 +00001718 &dev_attr_min_links.attr,
Neil Horman7eacd032013-09-13 11:05:33 -04001719 &dev_attr_lp_interval.attr,
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01001720 &dev_attr_packets_per_slave.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001721 NULL,
1722};
1723
1724static struct attribute_group bonding_group = {
1725 .name = "bonding",
1726 .attrs = per_bond_attrs,
1727};
1728
1729/*
1730 * Initialize sysfs. This sets up the bonding_masters file in
1731 * /sys/class/net.
1732 */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001733int bond_create_sysfs(struct bond_net *bn)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001734{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001735 int ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001736
Eric W. Biederman4c224002011-10-12 21:56:25 +00001737 bn->class_attr_bonding_masters = class_attr_bonding_masters;
Eric W. Biederman01718e32011-10-21 22:43:07 +00001738 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
Eric W. Biederman4c224002011-10-12 21:56:25 +00001739
Tejun Heo58292cbe2013-09-11 22:29:04 -04001740 ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
1741 bn->net);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001742 /*
1743 * Permit multiple loads of the module by ignoring failures to
1744 * create the bonding_masters sysfs file. Bonding devices
1745 * created by second or subsequent loads of the module will
1746 * not be listed in, or controllable by, bonding_masters, but
1747 * will have the usual "bonding" sysfs directory.
1748 *
1749 * This is done to preserve backwards compatibility for
1750 * initscripts/sysconfig, which load bonding multiple times to
1751 * configure multiple bonding devices.
1752 */
1753 if (ret == -EEXIST) {
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001754 /* Is someone being kinky and naming a device bonding_master? */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001755 if (__dev_get_by_name(bn->net,
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001756 class_attr_bonding_masters.attr.name))
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001757 pr_err("network device named %s already exists in sysfs",
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001758 class_attr_bonding_masters.attr.name);
Stephen Hemminger130aa612009-06-11 05:46:04 -07001759 ret = 0;
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001760 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001761
1762 return ret;
1763
1764}
1765
1766/*
1767 * Remove /sys/class/net/bonding_masters.
1768 */
Eric W. Biederman4c224002011-10-12 21:56:25 +00001769void bond_destroy_sysfs(struct bond_net *bn)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001770{
Tejun Heo58292cbe2013-09-11 22:29:04 -04001771 netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001772}
1773
1774/*
1775 * Initialize sysfs for each bond. This sets up and registers
1776 * the 'bondctl' directory for each individual bond under /sys/class/net.
1777 */
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001778void bond_prepare_sysfs_group(struct bonding *bond)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001779{
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001780 bond->dev->sysfs_groups[0] = &bonding_group;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001781}
1782