blob: 066311a5e084758974ffadf433a95a3b678ca827 [file] [log] [blame]
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001/*
2 * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
Mitch Williamsb76cdba2005-11-09 10:36:41 -080021 */
Joe Perchesa4aee5c2009-12-13 20:06:07 -080022
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Mitch Williamsb76cdba2005-11-09 10:36:41 -080025#include <linux/kernel.h>
26#include <linux/module.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080027#include <linux/device.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040028#include <linux/sched.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080029#include <linux/sysdev.h>
30#include <linux/fs.h>
31#include <linux/types.h>
32#include <linux/string.h>
33#include <linux/netdevice.h>
34#include <linux/inetdevice.h>
35#include <linux/in.h>
36#include <linux/sysfs.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080037#include <linux/ctype.h>
38#include <linux/inet.h>
39#include <linux/rtnetlink.h>
Stephen Hemminger5c5129b2009-06-12 19:02:51 +000040#include <linux/etherdevice.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070041#include <net/net_namespace.h>
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000042#include <net/netns/generic.h>
43#include <linux/nsproxy.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080044
Mitch Williamsb76cdba2005-11-09 10:36:41 -080045#include "bonding.h"
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -080046
Stephen Hemminger3d632c32009-06-12 19:02:48 +000047#define to_dev(obj) container_of(obj, struct device, kobj)
Wang Chen454d7c92008-11-12 23:37:49 -080048#define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
Mitch Williamsb76cdba2005-11-09 10:36:41 -080049
Mitch Williamsb76cdba2005-11-09 10:36:41 -080050/*
51 * "show" function for the bond_masters attribute.
52 * The class parameter is ignored.
53 */
Andi Kleen28812fe2010-01-05 12:48:07 +010054static ssize_t bonding_show_bonds(struct class *cls,
55 struct class_attribute *attr,
56 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080057{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000058 struct net *net = current->nsproxy->net_ns;
59 struct bond_net *bn = net_generic(net, bond_net_id);
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. Biedermanec87fd32009-10-29 14:18:26 +000082static struct net_device *bond_get_by_name(struct net *net, const char *ifname)
Stephen Hemminger373500d2009-06-12 19:02:50 +000083{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000084 struct bond_net *bn = net_generic(net, bond_net_id);
Stephen Hemminger373500d2009-06-12 19:02:50 +000085 struct bonding *bond;
86
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000087 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Stephen Hemminger373500d2009-06-12 19:02:50 +000088 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
89 return bond->dev;
90 }
91 return NULL;
92}
93
Mitch Williamsb76cdba2005-11-09 10:36:41 -080094/*
95 * "store" function for the bond_masters attribute. This is what
96 * creates and deletes entire bonds.
97 *
98 * The class parameter is ignored.
99 *
100 */
101
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000102static ssize_t bonding_store_bonds(struct class *cls,
Andi Kleen28812fe2010-01-05 12:48:07 +0100103 struct class_attribute *attr,
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000104 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800105{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +0000106 struct net *net = current->nsproxy->net_ns;
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. Biedermanec87fd32009-10-29 14:18:26 +0000119 rv = bond_create(net, ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800120 if (rv) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800121 pr_info("Bond creation failed.\n");
Jay Vosburgh027ea042008-01-17 16:25:02 -0800122 res = rv;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800123 }
Stephen Hemminger373500d2009-06-12 19:02:50 +0000124 } else if (command[0] == '-') {
125 struct net_device *bond_dev;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800126
Jay Vosburgh027ea042008-01-17 16:25:02 -0800127 rtnl_lock();
Eric W. Biedermanec87fd32009-10-29 14:18:26 +0000128 bond_dev = bond_get_by_name(net, ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000129 if (bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800130 pr_info("%s is being deleted...\n", ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000131 unregister_netdevice(bond_dev);
132 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800133 pr_err("unable to delete non-existent %s\n", ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000134 res = -ENODEV;
135 }
136 rtnl_unlock();
137 } else
138 goto err_no_cmd;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800139
Stephen Hemminger373500d2009-06-12 19:02:50 +0000140 /* Always return either count or an error. If you return 0, you'll
141 * get called forever, which is bad.
142 */
143 return res;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800144
145err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800146 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700147 return -EPERM;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800148}
Stephen Hemminger373500d2009-06-12 19:02:50 +0000149
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800150/* class attribute for bond_masters file. This ends up in /sys/class/net */
151static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
152 bonding_show_bonds, bonding_store_bonds);
153
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000154int bond_create_slave_symlinks(struct net_device *master,
155 struct net_device *slave)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800156{
157 char linkname[IFNAMSIZ+7];
158 int ret = 0;
159
160 /* first, create a link from the slave back to the master */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700161 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800162 "master");
163 if (ret)
164 return ret;
165 /* next, create a link from the master to the slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000166 sprintf(linkname, "slave_%s", slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700167 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800168 linkname);
169 return ret;
170
171}
172
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000173void bond_destroy_slave_symlinks(struct net_device *master,
174 struct net_device *slave)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800175{
176 char linkname[IFNAMSIZ+7];
177
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700178 sysfs_remove_link(&(slave->dev.kobj), "master");
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000179 sprintf(linkname, "slave_%s", slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700180 sysfs_remove_link(&(master->dev.kobj), linkname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800181}
182
183
184/*
185 * Show the slaves in the current bond.
186 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700187static ssize_t bonding_show_slaves(struct device *d,
188 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800189{
190 struct slave *slave;
191 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700192 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800193
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700194 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800195 bond_for_each_slave(bond, slave, i) {
196 if (res > (PAGE_SIZE - IFNAMSIZ)) {
197 /* not enough space for another interface name */
198 if ((PAGE_SIZE - res) > 10)
199 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800200 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800201 break;
202 }
203 res += sprintf(buf + res, "%s ", slave->dev->name);
204 }
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700205 read_unlock(&bond->lock);
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800206 if (res)
207 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800208 return res;
209}
210
211/*
212 * Set the slaves in the current bond. The bond interface must be
213 * up for this to succeed.
Jiri Pirkof9f35452010-05-18 05:46:39 +0000214 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
215 * All hard work should be done there.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800216 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700217static ssize_t bonding_store_slaves(struct device *d,
218 struct device_attribute *attr,
219 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800220{
221 char command[IFNAMSIZ + 1] = { 0, };
222 char *ifname;
Jiri Pirkof9f35452010-05-18 05:46:39 +0000223 int res, ret = count;
224 struct net_device *dev;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700225 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800226
227 /* Quick sanity check -- is the bond interface up? */
228 if (!(bond->dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800229 pr_warning("%s: doing slave updates when interface is down.\n",
230 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800231 }
232
Eric W. Biederman496a60c2009-05-13 17:02:50 +0000233 if (!rtnl_trylock())
234 return restart_syscall();
Jay Vosburgh027ea042008-01-17 16:25:02 -0800235
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800236 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
237 ifname = command + 1;
238 if ((strlen(command) <= 1) ||
239 !dev_valid_name(ifname))
240 goto err_no_cmd;
241
Jiri Pirkof9f35452010-05-18 05:46:39 +0000242 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
243 if (!dev) {
244 pr_info("%s: Interface %s does not exist!\n",
245 bond->dev->name, ifname);
246 ret = -ENODEV;
247 goto out;
248 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800249
Jiri Pirkof9f35452010-05-18 05:46:39 +0000250 switch (command[0]) {
251 case '+':
252 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800253 res = bond_enslave(bond->dev, dev);
Jiri Pirkof9f35452010-05-18 05:46:39 +0000254 break;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000255
Jiri Pirkof9f35452010-05-18 05:46:39 +0000256 case '-':
257 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
258 res = bond_release(bond->dev, dev);
259 break;
260
261 default:
262 goto err_no_cmd;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800263 }
264
Jiri Pirkof9f35452010-05-18 05:46:39 +0000265 if (res)
266 ret = res;
267 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800268
269err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800270 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
271 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800272 ret = -EPERM;
273
274out:
Jay Vosburgh027ea042008-01-17 16:25:02 -0800275 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800276 return ret;
277}
278
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000279static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
280 bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800281
282/*
283 * Show and set the bonding mode. The bond interface must be down to
284 * change the mode.
285 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700286static ssize_t bonding_show_mode(struct device *d,
287 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800288{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700289 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800290
291 return sprintf(buf, "%s %d\n",
292 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800293 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800294}
295
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700296static ssize_t bonding_store_mode(struct device *d,
297 struct device_attribute *attr,
298 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800299{
300 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700301 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800302
303 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800304 pr_err("unable to update mode of %s because interface is up.\n",
305 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800306 ret = -EPERM;
307 goto out;
308 }
309
Jay Vosburghece95f72008-01-17 16:25:01 -0800310 new_value = bond_parse_parm(buf, bond_mode_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800311 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800312 pr_err("%s: Ignoring invalid mode value %.*s.\n",
313 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800314 ret = -EINVAL;
315 goto out;
316 } else {
Jay Vosburgh8f903c72006-02-21 16:36:44 -0800317 if (bond->params.mode == BOND_MODE_8023AD)
318 bond_unset_master_3ad_flags(bond);
319
320 if (bond->params.mode == BOND_MODE_ALB)
321 bond_unset_master_alb_flags(bond);
322
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800323 bond->params.mode = new_value;
324 bond_set_mode_ops(bond, bond->params.mode);
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800325 pr_info("%s: setting mode to %s (%d).\n",
326 bond->dev->name, bond_mode_tbl[new_value].modename,
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000327 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800328 }
329out:
330 return ret;
331}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000332static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
333 bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800334
335/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000336 * Show and set the bonding transmit hash method.
337 * The bond interface must be down to change the xmit hash policy.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800338 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700339static ssize_t bonding_show_xmit_hash(struct device *d,
340 struct device_attribute *attr,
341 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800342{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700343 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800344
Wagner Ferenc8e4b9322007-12-06 23:40:32 -0800345 return sprintf(buf, "%s %d\n",
346 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
347 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800348}
349
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700350static ssize_t bonding_store_xmit_hash(struct device *d,
351 struct device_attribute *attr,
352 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800353{
354 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700355 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800356
357 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800358 pr_err("%s: Interface is up. Unable to update xmit policy.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800359 bond->dev->name);
360 ret = -EPERM;
361 goto out;
362 }
363
Jay Vosburghece95f72008-01-17 16:25:01 -0800364 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800365 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800366 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800367 bond->dev->name,
368 (int)strlen(buf) - 1, buf);
369 ret = -EINVAL;
370 goto out;
371 } else {
372 bond->params.xmit_policy = new_value;
373 bond_set_mode_ops(bond, bond->params.mode);
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800374 pr_info("%s: setting xmit hash policy to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000375 bond->dev->name,
376 xmit_hashtype_tbl[new_value].modename, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800377 }
378out:
379 return ret;
380}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000381static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
382 bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800383
384/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700385 * Show and set arp_validate.
386 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700387static ssize_t bonding_show_arp_validate(struct device *d,
388 struct device_attribute *attr,
389 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700390{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700391 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700392
393 return sprintf(buf, "%s %d\n",
394 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800395 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700396}
397
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700398static ssize_t bonding_store_arp_validate(struct device *d,
399 struct device_attribute *attr,
400 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700401{
402 int new_value;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700403 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700404
Jay Vosburghece95f72008-01-17 16:25:01 -0800405 new_value = bond_parse_parm(buf, arp_validate_tbl);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700406 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800407 pr_err("%s: Ignoring invalid arp_validate value %s\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700408 bond->dev->name, buf);
409 return -EINVAL;
410 }
411 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800412 pr_err("%s: arp_validate only supported in active-backup mode.\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700413 bond->dev->name);
414 return -EINVAL;
415 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800416 pr_info("%s: setting arp_validate to %s (%d).\n",
417 bond->dev->name, arp_validate_tbl[new_value].modename,
418 new_value);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700419
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000420 if (!bond->params.arp_validate && new_value)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700421 bond_register_arp(bond);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000422 else if (bond->params.arp_validate && !new_value)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700423 bond_unregister_arp(bond);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700424
425 bond->params.arp_validate = new_value;
426
427 return count;
428}
429
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000430static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
431 bonding_store_arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700432
433/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700434 * Show and store fail_over_mac. User only allowed to change the
435 * value when there are no slaves.
436 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000437static ssize_t bonding_show_fail_over_mac(struct device *d,
438 struct device_attribute *attr,
439 char *buf)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700440{
441 struct bonding *bond = to_bond(d);
442
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700443 return sprintf(buf, "%s %d\n",
444 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
445 bond->params.fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700446}
447
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000448static ssize_t bonding_store_fail_over_mac(struct device *d,
449 struct device_attribute *attr,
450 const char *buf, size_t count)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700451{
452 int new_value;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700453 struct bonding *bond = to_bond(d);
454
455 if (bond->slave_cnt != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800456 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
Jay Vosburghdd957c52007-10-09 19:57:24 -0700457 bond->dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700458 return -EPERM;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700459 }
460
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700461 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
462 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800463 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700464 bond->dev->name, buf);
465 return -EINVAL;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700466 }
467
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700468 bond->params.fail_over_mac = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800469 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
470 bond->dev->name, fail_over_mac_tbl[new_value].modename,
471 new_value);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700472
473 return count;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700474}
475
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000476static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
477 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700478
479/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800480 * Show and set the arp timer interval. There are two tricky bits
481 * here. First, if ARP monitoring is activated, then we must disable
482 * MII monitoring. Second, if the ARP timer isn't running, we must
483 * start it.
484 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700485static ssize_t bonding_show_arp_interval(struct device *d,
486 struct device_attribute *attr,
487 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800488{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700489 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800490
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800491 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800492}
493
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700494static ssize_t bonding_store_arp_interval(struct device *d,
495 struct device_attribute *attr,
496 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800497{
498 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700499 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800500
501 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800502 pr_err("%s: no arp_interval value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800503 bond->dev->name);
504 ret = -EINVAL;
505 goto out;
506 }
507 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800508 pr_err("%s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800509 bond->dev->name, new_value, INT_MAX);
510 ret = -EINVAL;
511 goto out;
512 }
513
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800514 pr_info("%s: Setting ARP monitoring interval to %d.\n",
515 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800516 bond->params.arp_interval = new_value;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -0800517 if (bond->params.arp_interval)
518 bond->dev->priv_flags |= IFF_MASTER_ARPMON;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800519 if (bond->params.miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800520 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
521 bond->dev->name, bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800522 bond->params.miimon = 0;
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700523 if (delayed_work_pending(&bond->mii_work)) {
524 cancel_delayed_work(&bond->mii_work);
525 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800526 }
527 }
528 if (!bond->params.arp_targets[0]) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800529 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
530 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800531 }
532 if (bond->dev->flags & IFF_UP) {
533 /* If the interface is up, we may need to fire off
534 * the ARP timer. If the interface is down, the
535 * timer will get fired off when the open function
536 * is called.
537 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700538 if (!delayed_work_pending(&bond->arp_work)) {
539 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
540 INIT_DELAYED_WORK(&bond->arp_work,
541 bond_activebackup_arp_mon);
542 else
543 INIT_DELAYED_WORK(&bond->arp_work,
544 bond_loadbalance_arp_mon);
545
546 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800547 }
548 }
549
550out:
551 return ret;
552}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000553static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
554 bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800555
556/*
557 * Show and set the arp targets.
558 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700559static ssize_t bonding_show_arp_targets(struct device *d,
560 struct device_attribute *attr,
561 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800562{
563 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700564 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800565
566 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
567 if (bond->params.arp_targets[i])
Harvey Harrison63779432008-10-31 00:56:00 -0700568 res += sprintf(buf + res, "%pI4 ",
569 &bond->params.arp_targets[i]);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800570 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800571 if (res)
572 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800573 return res;
574}
575
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700576static ssize_t bonding_store_arp_targets(struct device *d,
577 struct device_attribute *attr,
578 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800579{
Al Virod3bb52b2007-08-22 20:06:58 -0400580 __be32 newtarget;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800581 int i = 0, done = 0, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700582 struct bonding *bond = to_bond(d);
Al Virod3bb52b2007-08-22 20:06:58 -0400583 __be32 *targets;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800584
585 targets = bond->params.arp_targets;
586 newtarget = in_aton(buf + 1);
587 /* look for adds */
588 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400589 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800590 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700591 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800592 ret = -EINVAL;
593 goto out;
594 }
595 /* look for an empty slot to put the target in, and check for dupes */
Brian Haley5a31bec2009-04-13 00:11:30 -0700596 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800597 if (targets[i] == newtarget) { /* duplicate */
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800598 pr_err("%s: ARP target %pI4 is already present\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700599 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800600 ret = -EINVAL;
601 goto out;
602 }
Brian Haley5a31bec2009-04-13 00:11:30 -0700603 if (targets[i] == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800604 pr_info("%s: adding ARP target %pI4.\n",
605 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800606 done = 1;
607 targets[i] = newtarget;
608 }
609 }
610 if (!done) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800611 pr_err("%s: ARP target table is full!\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800612 bond->dev->name);
613 ret = -EINVAL;
614 goto out;
615 }
616
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000617 } else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400618 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800619 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700620 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800621 ret = -EINVAL;
622 goto out;
623 }
624
Brian Haley5a31bec2009-04-13 00:11:30 -0700625 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800626 if (targets[i] == newtarget) {
Brian Haley5a31bec2009-04-13 00:11:30 -0700627 int j;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800628 pr_info("%s: removing ARP target %pI4.\n",
629 bond->dev->name, &newtarget);
Brian Haley5a31bec2009-04-13 00:11:30 -0700630 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
631 targets[j] = targets[j+1];
632
633 targets[j] = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800634 done = 1;
635 }
636 }
637 if (!done) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800638 pr_info("%s: unable to remove nonexistent ARP target %pI4.\n",
639 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800640 ret = -EINVAL;
641 goto out;
642 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000643 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800644 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
645 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800646 ret = -EPERM;
647 goto out;
648 }
649
650out:
651 return ret;
652}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700653static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800654
655/*
656 * Show and set the up and down delays. These must be multiples of the
657 * MII monitoring value, and are stored internally as the multiplier.
658 * Thus, we must translate to MS for the real world.
659 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700660static ssize_t bonding_show_downdelay(struct device *d,
661 struct device_attribute *attr,
662 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800663{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700664 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800665
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800666 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800667}
668
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700669static ssize_t bonding_store_downdelay(struct device *d,
670 struct device_attribute *attr,
671 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800672{
673 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700674 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800675
676 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800677 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800678 bond->dev->name);
679 ret = -EPERM;
680 goto out;
681 }
682
683 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800684 pr_err("%s: no down delay value specified.\n", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800685 ret = -EINVAL;
686 goto out;
687 }
688 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800689 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800690 bond->dev->name, new_value, 1, INT_MAX);
691 ret = -EINVAL;
692 goto out;
693 } else {
694 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800695 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 +0000696 bond->dev->name, new_value,
697 bond->params.miimon,
698 (new_value / bond->params.miimon) *
699 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800700 }
701 bond->params.downdelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800702 pr_info("%s: Setting down delay to %d.\n",
703 bond->dev->name,
704 bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800705
706 }
707
708out:
709 return ret;
710}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000711static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
712 bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800713
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700714static ssize_t bonding_show_updelay(struct device *d,
715 struct device_attribute *attr,
716 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800717{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700718 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800719
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800720 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800721
722}
723
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700724static ssize_t bonding_store_updelay(struct device *d,
725 struct device_attribute *attr,
726 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800727{
728 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700729 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800730
731 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800732 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800733 bond->dev->name);
734 ret = -EPERM;
735 goto out;
736 }
737
738 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800739 pr_err("%s: no up delay value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800740 bond->dev->name);
741 ret = -EINVAL;
742 goto out;
743 }
744 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800745 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800746 bond->dev->name, new_value, 1, INT_MAX);
747 ret = -EINVAL;
748 goto out;
749 } else {
750 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800751 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 +0000752 bond->dev->name, new_value,
753 bond->params.miimon,
754 (new_value / bond->params.miimon) *
755 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800756 }
757 bond->params.updelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800758 pr_info("%s: Setting up delay to %d.\n",
759 bond->dev->name,
760 bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800761 }
762
763out:
764 return ret;
765}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000766static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
767 bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800768
769/*
770 * Show and set the LACP interval. Interface must be down, and the mode
771 * must be set to 802.3ad mode.
772 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700773static ssize_t bonding_show_lacp(struct device *d,
774 struct device_attribute *attr,
775 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800776{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700777 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800778
779 return sprintf(buf, "%s %d\n",
780 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800781 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800782}
783
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700784static ssize_t bonding_store_lacp(struct device *d,
785 struct device_attribute *attr,
786 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800787{
788 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700789 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800790
791 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800792 pr_err("%s: Unable to update LACP rate because interface is up.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800793 bond->dev->name);
794 ret = -EPERM;
795 goto out;
796 }
797
798 if (bond->params.mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800799 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800800 bond->dev->name);
801 ret = -EPERM;
802 goto out;
803 }
804
Jay Vosburghece95f72008-01-17 16:25:01 -0800805 new_value = bond_parse_parm(buf, bond_lacp_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800806
807 if ((new_value == 1) || (new_value == 0)) {
808 bond->params.lacp_fast = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800809 pr_info("%s: Setting LACP rate to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000810 bond->dev->name, bond_lacp_tbl[new_value].modename,
811 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800812 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800813 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000814 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800815 ret = -EINVAL;
816 }
817out:
818 return ret;
819}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000820static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
821 bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800822
Jay Vosburghfd989c82008-11-04 17:51:16 -0800823static ssize_t bonding_show_ad_select(struct device *d,
824 struct device_attribute *attr,
825 char *buf)
826{
827 struct bonding *bond = to_bond(d);
828
829 return sprintf(buf, "%s %d\n",
830 ad_select_tbl[bond->params.ad_select].modename,
831 bond->params.ad_select);
832}
833
834
835static ssize_t bonding_store_ad_select(struct device *d,
836 struct device_attribute *attr,
837 const char *buf, size_t count)
838{
839 int new_value, ret = count;
840 struct bonding *bond = to_bond(d);
841
842 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800843 pr_err("%s: Unable to update ad_select because interface is up.\n",
844 bond->dev->name);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800845 ret = -EPERM;
846 goto out;
847 }
848
849 new_value = bond_parse_parm(buf, ad_select_tbl);
850
851 if (new_value != -1) {
852 bond->params.ad_select = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800853 pr_info("%s: Setting ad_select to %s (%d).\n",
854 bond->dev->name, ad_select_tbl[new_value].modename,
855 new_value);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800856 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800857 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -0800858 bond->dev->name, (int)strlen(buf) - 1, buf);
859 ret = -EINVAL;
860 }
861out:
862 return ret;
863}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000864static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
865 bonding_show_ad_select, bonding_store_ad_select);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800866
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800867/*
Moni Shoua7893b242008-05-17 21:10:12 -0700868 * Show and set the number of grat ARP to send after a failover event.
869 */
870static ssize_t bonding_show_n_grat_arp(struct device *d,
871 struct device_attribute *attr,
872 char *buf)
873{
874 struct bonding *bond = to_bond(d);
875
876 return sprintf(buf, "%d\n", bond->params.num_grat_arp);
877}
878
879static ssize_t bonding_store_n_grat_arp(struct device *d,
880 struct device_attribute *attr,
881 const char *buf, size_t count)
882{
883 int new_value, ret = count;
884 struct bonding *bond = to_bond(d);
885
886 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800887 pr_err("%s: no num_grat_arp value specified.\n",
Moni Shoua7893b242008-05-17 21:10:12 -0700888 bond->dev->name);
889 ret = -EINVAL;
890 goto out;
891 }
892 if (new_value < 0 || new_value > 255) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800893 pr_err("%s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
Moni Shoua7893b242008-05-17 21:10:12 -0700894 bond->dev->name, new_value);
895 ret = -EINVAL;
896 goto out;
897 } else {
898 bond->params.num_grat_arp = new_value;
899 }
900out:
901 return ret;
902}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000903static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
904 bonding_show_n_grat_arp, bonding_store_n_grat_arp);
Brian Haley305d5522008-11-04 17:51:14 -0800905
906/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000907 * Show and set the number of unsolicited NA's to send after a failover event.
Brian Haley305d5522008-11-04 17:51:14 -0800908 */
909static ssize_t bonding_show_n_unsol_na(struct device *d,
910 struct device_attribute *attr,
911 char *buf)
912{
913 struct bonding *bond = to_bond(d);
914
915 return sprintf(buf, "%d\n", bond->params.num_unsol_na);
916}
917
918static ssize_t bonding_store_n_unsol_na(struct device *d,
919 struct device_attribute *attr,
920 const char *buf, size_t count)
921{
922 int new_value, ret = count;
923 struct bonding *bond = to_bond(d);
924
925 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800926 pr_err("%s: no num_unsol_na value specified.\n",
Brian Haley305d5522008-11-04 17:51:14 -0800927 bond->dev->name);
928 ret = -EINVAL;
929 goto out;
930 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000931
Brian Haley305d5522008-11-04 17:51:14 -0800932 if (new_value < 0 || new_value > 255) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800933 pr_err("%s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
Brian Haley305d5522008-11-04 17:51:14 -0800934 bond->dev->name, new_value);
935 ret = -EINVAL;
936 goto out;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000937 } else
Brian Haley305d5522008-11-04 17:51:14 -0800938 bond->params.num_unsol_na = new_value;
Brian Haley305d5522008-11-04 17:51:14 -0800939out:
940 return ret;
941}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000942static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
943 bonding_show_n_unsol_na, bonding_store_n_unsol_na);
Brian Haley305d5522008-11-04 17:51:14 -0800944
Moni Shoua7893b242008-05-17 21:10:12 -0700945/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800946 * Show and set the MII monitor interval. There are two tricky bits
947 * here. First, if MII monitoring is activated, then we must disable
948 * ARP monitoring. Second, if the timer isn't running, we must
949 * start it.
950 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700951static ssize_t bonding_show_miimon(struct device *d,
952 struct device_attribute *attr,
953 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800954{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700955 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800956
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800957 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800958}
959
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700960static ssize_t bonding_store_miimon(struct device *d,
961 struct device_attribute *attr,
962 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800963{
964 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700965 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800966
967 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800968 pr_err("%s: no miimon value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800969 bond->dev->name);
970 ret = -EINVAL;
971 goto out;
972 }
973 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800974 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800975 bond->dev->name, new_value, 1, INT_MAX);
976 ret = -EINVAL;
977 goto out;
978 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800979 pr_info("%s: Setting MII monitoring interval to %d.\n",
980 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800981 bond->params.miimon = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000982 if (bond->params.updelay)
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800983 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
984 bond->dev->name,
985 bond->params.updelay * bond->params.miimon);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000986 if (bond->params.downdelay)
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800987 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
988 bond->dev->name,
989 bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800990 if (bond->params.arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800991 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
992 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800993 bond->params.arp_interval = 0;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -0800994 bond->dev->priv_flags &= ~IFF_MASTER_ARPMON;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700995 if (bond->params.arp_validate) {
996 bond_unregister_arp(bond);
997 bond->params.arp_validate =
998 BOND_ARP_VALIDATE_NONE;
999 }
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001000 if (delayed_work_pending(&bond->arp_work)) {
1001 cancel_delayed_work(&bond->arp_work);
1002 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001003 }
1004 }
1005
1006 if (bond->dev->flags & IFF_UP) {
1007 /* If the interface is up, we may need to fire off
1008 * the MII timer. If the interface is down, the
1009 * timer will get fired off when the open function
1010 * is called.
1011 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001012 if (!delayed_work_pending(&bond->mii_work)) {
1013 INIT_DELAYED_WORK(&bond->mii_work,
1014 bond_mii_monitor);
1015 queue_delayed_work(bond->wq,
1016 &bond->mii_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001017 }
1018 }
1019 }
1020out:
1021 return ret;
1022}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001023static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1024 bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001025
1026/*
1027 * Show and set the primary slave. The store function is much
1028 * simpler than bonding_store_slaves function because it only needs to
1029 * handle one interface name.
1030 * The bond must be a mode that supports a primary for this be
1031 * set.
1032 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001033static ssize_t bonding_show_primary(struct device *d,
1034 struct device_attribute *attr,
1035 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001036{
1037 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001038 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001039
1040 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001041 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001042
1043 return count;
1044}
1045
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001046static ssize_t bonding_store_primary(struct device *d,
1047 struct device_attribute *attr,
1048 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001049{
1050 int i;
1051 struct slave *slave;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001052 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001053
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001054 if (!rtnl_trylock())
1055 return restart_syscall();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001056 read_lock(&bond->lock);
1057 write_lock_bh(&bond->curr_slave_lock);
1058
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001059 if (!USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001060 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1061 bond->dev->name, bond->dev->name, bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001062 } else {
1063 bond_for_each_slave(bond, slave, i) {
1064 if (strnicmp
1065 (slave->dev->name, buf,
1066 strlen(slave->dev->name)) == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001067 pr_info("%s: Setting %s as primary slave.\n",
1068 bond->dev->name, slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001069 bond->primary_slave = slave;
Jiri Pirkoce501ca2009-09-18 02:13:22 +00001070 strcpy(bond->params.primary, slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001071 bond_select_active_slave(bond);
1072 goto out;
1073 }
1074 }
1075
1076 /* if we got here, then we didn't match the name of any slave */
1077
1078 if (strlen(buf) == 0 || buf[0] == '\n') {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001079 pr_info("%s: Setting primary slave to None.\n",
1080 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001081 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001082 bond_select_active_slave(bond);
1083 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001084 pr_info("%s: Unable to set %.*s as primary slave as it is not a slave.\n",
1085 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001086 }
1087 }
1088out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001089 write_unlock_bh(&bond->curr_slave_lock);
1090 read_unlock(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001091 rtnl_unlock();
1092
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001093 return count;
1094}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001095static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1096 bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001097
1098/*
Jiri Pirkoa5499522009-09-25 03:28:09 +00001099 * Show and set the primary_reselect flag.
1100 */
1101static ssize_t bonding_show_primary_reselect(struct device *d,
1102 struct device_attribute *attr,
1103 char *buf)
1104{
1105 struct bonding *bond = to_bond(d);
1106
1107 return sprintf(buf, "%s %d\n",
1108 pri_reselect_tbl[bond->params.primary_reselect].modename,
1109 bond->params.primary_reselect);
1110}
1111
1112static ssize_t bonding_store_primary_reselect(struct device *d,
1113 struct device_attribute *attr,
1114 const char *buf, size_t count)
1115{
1116 int new_value, ret = count;
1117 struct bonding *bond = to_bond(d);
1118
1119 if (!rtnl_trylock())
1120 return restart_syscall();
1121
1122 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1123 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001124 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001125 bond->dev->name,
1126 (int) strlen(buf) - 1, buf);
1127 ret = -EINVAL;
1128 goto out;
1129 }
1130
1131 bond->params.primary_reselect = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001132 pr_info("%s: setting primary_reselect to %s (%d).\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001133 bond->dev->name, pri_reselect_tbl[new_value].modename,
1134 new_value);
1135
1136 read_lock(&bond->lock);
1137 write_lock_bh(&bond->curr_slave_lock);
1138 bond_select_active_slave(bond);
1139 write_unlock_bh(&bond->curr_slave_lock);
1140 read_unlock(&bond->lock);
1141out:
1142 rtnl_unlock();
1143 return ret;
1144}
1145static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1146 bonding_show_primary_reselect,
1147 bonding_store_primary_reselect);
1148
1149/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001150 * Show and set the use_carrier flag.
1151 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001152static ssize_t bonding_show_carrier(struct device *d,
1153 struct device_attribute *attr,
1154 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001155{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001156 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001157
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001158 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001159}
1160
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001161static ssize_t bonding_store_carrier(struct device *d,
1162 struct device_attribute *attr,
1163 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001164{
1165 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001166 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001167
1168
1169 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001170 pr_err("%s: no use_carrier value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001171 bond->dev->name);
1172 ret = -EINVAL;
1173 goto out;
1174 }
1175 if ((new_value == 0) || (new_value == 1)) {
1176 bond->params.use_carrier = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001177 pr_info("%s: Setting use_carrier to %d.\n",
1178 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001179 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001180 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1181 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001182 }
1183out:
1184 return count;
1185}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001186static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1187 bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001188
1189
1190/*
1191 * Show and set currently active_slave.
1192 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001193static ssize_t bonding_show_active_slave(struct device *d,
1194 struct device_attribute *attr,
1195 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001196{
1197 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001198 struct bonding *bond = to_bond(d);
Wagner Ferenc16cd0162007-12-06 23:40:29 -08001199 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001200
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001201 read_lock(&bond->curr_slave_lock);
1202 curr = bond->curr_active_slave;
1203 read_unlock(&bond->curr_slave_lock);
1204
1205 if (USES_PRIMARY(bond->params.mode) && curr)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001206 count = sprintf(buf, "%s\n", curr->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001207 return count;
1208}
1209
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001210static ssize_t bonding_store_active_slave(struct device *d,
1211 struct device_attribute *attr,
1212 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001213{
1214 int i;
1215 struct slave *slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001216 struct slave *old_active = NULL;
1217 struct slave *new_active = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001218 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001219
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001220 if (!rtnl_trylock())
1221 return restart_syscall();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001222 read_lock(&bond->lock);
1223 write_lock_bh(&bond->curr_slave_lock);
Jay Vosburgh1466a212007-11-06 13:33:28 -08001224
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001225 if (!USES_PRIMARY(bond->params.mode))
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001226 pr_info("%s: Unable to change active slave; %s is in mode %d\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001227 bond->dev->name, bond->dev->name, bond->params.mode);
1228 else {
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001229 bond_for_each_slave(bond, slave, i) {
1230 if (strnicmp
1231 (slave->dev->name, buf,
1232 strlen(slave->dev->name)) == 0) {
1233 old_active = bond->curr_active_slave;
1234 new_active = slave;
Jay Vosburgha50d8de2006-09-22 21:53:25 -07001235 if (new_active == old_active) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001236 /* do nothing */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001237 pr_info("%s: %s is already the current active slave.\n",
1238 bond->dev->name,
1239 slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001240 goto out;
1241 }
1242 else {
1243 if ((new_active) &&
1244 (old_active) &&
1245 (new_active->link == BOND_LINK_UP) &&
1246 IS_UP(new_active->dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001247 pr_info("%s: Setting %s as active slave.\n",
1248 bond->dev->name,
1249 slave->dev->name);
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00001250 bond_change_active_slave(bond, new_active);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001251 }
1252 else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001253 pr_info("%s: Could not set %s as active slave; either %s is down or the link is down.\n",
1254 bond->dev->name,
1255 slave->dev->name,
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00001256 slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001257 }
1258 goto out;
1259 }
1260 }
1261 }
1262
1263 /* if we got here, then we didn't match the name of any slave */
1264
1265 if (strlen(buf) == 0 || buf[0] == '\n') {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001266 pr_info("%s: Setting active slave to None.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001267 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001268 bond->primary_slave = NULL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001269 bond_select_active_slave(bond);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001270 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001271 pr_info("%s: Unable to set %.*s as active slave as it is not a slave.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001272 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001273 }
1274 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001275 out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001276 write_unlock_bh(&bond->curr_slave_lock);
1277 read_unlock(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001278 rtnl_unlock();
1279
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001280 return count;
1281
1282}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001283static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1284 bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001285
1286
1287/*
1288 * Show link status of the bond interface.
1289 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001290static ssize_t bonding_show_mii_status(struct device *d,
1291 struct device_attribute *attr,
1292 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001293{
1294 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001295 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001296
1297 read_lock(&bond->curr_slave_lock);
1298 curr = bond->curr_active_slave;
1299 read_unlock(&bond->curr_slave_lock);
1300
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001301 return sprintf(buf, "%s\n", curr ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001302}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001303static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001304
1305
1306/*
1307 * Show current 802.3ad aggregator ID.
1308 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001309static ssize_t bonding_show_ad_aggregator(struct device *d,
1310 struct device_attribute *attr,
1311 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001312{
1313 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001314 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001315
1316 if (bond->params.mode == BOND_MODE_8023AD) {
1317 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001318 count = sprintf(buf, "%d\n",
1319 (bond_3ad_get_active_agg_info(bond, &ad_info))
1320 ? 0 : ad_info.aggregator_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001321 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001322
1323 return count;
1324}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001325static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001326
1327
1328/*
1329 * Show number of active 802.3ad ports.
1330 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001331static ssize_t bonding_show_ad_num_ports(struct device *d,
1332 struct device_attribute *attr,
1333 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001334{
1335 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001336 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001337
1338 if (bond->params.mode == BOND_MODE_8023AD) {
1339 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001340 count = sprintf(buf, "%d\n",
1341 (bond_3ad_get_active_agg_info(bond, &ad_info))
1342 ? 0 : ad_info.ports);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001343 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001344
1345 return count;
1346}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001347static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001348
1349
1350/*
1351 * Show current 802.3ad actor key.
1352 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001353static ssize_t bonding_show_ad_actor_key(struct device *d,
1354 struct device_attribute *attr,
1355 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001356{
1357 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001358 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001359
1360 if (bond->params.mode == BOND_MODE_8023AD) {
1361 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001362 count = sprintf(buf, "%d\n",
1363 (bond_3ad_get_active_agg_info(bond, &ad_info))
1364 ? 0 : ad_info.actor_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001365 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001366
1367 return count;
1368}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001369static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001370
1371
1372/*
1373 * Show current 802.3ad partner key.
1374 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001375static ssize_t bonding_show_ad_partner_key(struct device *d,
1376 struct device_attribute *attr,
1377 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001378{
1379 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001380 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001381
1382 if (bond->params.mode == BOND_MODE_8023AD) {
1383 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001384 count = sprintf(buf, "%d\n",
1385 (bond_3ad_get_active_agg_info(bond, &ad_info))
1386 ? 0 : ad_info.partner_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001387 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001388
1389 return count;
1390}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001391static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001392
1393
1394/*
1395 * Show current 802.3ad partner mac.
1396 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001397static ssize_t bonding_show_ad_partner_mac(struct device *d,
1398 struct device_attribute *attr,
1399 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001400{
1401 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001402 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001403
1404 if (bond->params.mode == BOND_MODE_8023AD) {
1405 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001406 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
Johannes Berge1749612008-10-27 15:59:26 -07001407 count = sprintf(buf, "%pM\n", ad_info.partner_system);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001408 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001409
1410 return count;
1411}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001412static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001413
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001414/*
1415 * Show and set the all_slaves_active flag.
1416 */
1417static ssize_t bonding_show_slaves_active(struct device *d,
1418 struct device_attribute *attr,
1419 char *buf)
1420{
1421 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001422
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001423 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1424}
1425
1426static ssize_t bonding_store_slaves_active(struct device *d,
1427 struct device_attribute *attr,
1428 const char *buf, size_t count)
1429{
1430 int i, new_value, ret = count;
1431 struct bonding *bond = to_bond(d);
1432 struct slave *slave;
1433
1434 if (sscanf(buf, "%d", &new_value) != 1) {
1435 pr_err("%s: no all_slaves_active value specified.\n",
1436 bond->dev->name);
1437 ret = -EINVAL;
1438 goto out;
1439 }
1440
1441 if (new_value == bond->params.all_slaves_active)
1442 goto out;
1443
1444 if ((new_value == 0) || (new_value == 1)) {
1445 bond->params.all_slaves_active = new_value;
1446 } else {
1447 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1448 bond->dev->name, new_value);
1449 ret = -EINVAL;
1450 goto out;
1451 }
1452
1453 bond_for_each_slave(bond, slave, i) {
1454 if (slave->state == BOND_STATE_BACKUP) {
1455 if (new_value)
1456 slave->dev->priv_flags &= ~IFF_SLAVE_INACTIVE;
1457 else
1458 slave->dev->priv_flags |= IFF_SLAVE_INACTIVE;
1459 }
1460 }
1461out:
1462 return count;
1463}
1464static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1465 bonding_show_slaves_active, bonding_store_slaves_active);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001466
1467static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001468 &dev_attr_slaves.attr,
1469 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001470 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001471 &dev_attr_arp_validate.attr,
1472 &dev_attr_arp_interval.attr,
1473 &dev_attr_arp_ip_target.attr,
1474 &dev_attr_downdelay.attr,
1475 &dev_attr_updelay.attr,
1476 &dev_attr_lacp_rate.attr,
Jay Vosburghfd989c82008-11-04 17:51:16 -08001477 &dev_attr_ad_select.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001478 &dev_attr_xmit_hash_policy.attr,
Moni Shoua7893b242008-05-17 21:10:12 -07001479 &dev_attr_num_grat_arp.attr,
Brian Haley305d5522008-11-04 17:51:14 -08001480 &dev_attr_num_unsol_na.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001481 &dev_attr_miimon.attr,
1482 &dev_attr_primary.attr,
Jiri Pirkoa5499522009-09-25 03:28:09 +00001483 &dev_attr_primary_reselect.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001484 &dev_attr_use_carrier.attr,
1485 &dev_attr_active_slave.attr,
1486 &dev_attr_mii_status.attr,
1487 &dev_attr_ad_aggregator.attr,
1488 &dev_attr_ad_num_ports.attr,
1489 &dev_attr_ad_actor_key.attr,
1490 &dev_attr_ad_partner_key.attr,
1491 &dev_attr_ad_partner_mac.attr,
Andy Gospodarekebd8e492010-06-02 08:39:21 +00001492 &dev_attr_all_slaves_active.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001493 NULL,
1494};
1495
1496static struct attribute_group bonding_group = {
1497 .name = "bonding",
1498 .attrs = per_bond_attrs,
1499};
1500
1501/*
1502 * Initialize sysfs. This sets up the bonding_masters file in
1503 * /sys/class/net.
1504 */
1505int bond_create_sysfs(void)
1506{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001507 int ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001508
Jay Vosburghb8a97872008-06-13 18:12:04 -07001509 ret = netdev_class_create_file(&class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001510 /*
1511 * Permit multiple loads of the module by ignoring failures to
1512 * create the bonding_masters sysfs file. Bonding devices
1513 * created by second or subsequent loads of the module will
1514 * not be listed in, or controllable by, bonding_masters, but
1515 * will have the usual "bonding" sysfs directory.
1516 *
1517 * This is done to preserve backwards compatibility for
1518 * initscripts/sysconfig, which load bonding multiple times to
1519 * configure multiple bonding devices.
1520 */
1521 if (ret == -EEXIST) {
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001522 /* Is someone being kinky and naming a device bonding_master? */
1523 if (__dev_get_by_name(&init_net,
1524 class_attr_bonding_masters.attr.name))
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001525 pr_err("network device named %s already exists in sysfs",
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001526 class_attr_bonding_masters.attr.name);
Stephen Hemminger130aa612009-06-11 05:46:04 -07001527 ret = 0;
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001528 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001529
1530 return ret;
1531
1532}
1533
1534/*
1535 * Remove /sys/class/net/bonding_masters.
1536 */
1537void bond_destroy_sysfs(void)
1538{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001539 netdev_class_remove_file(&class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001540}
1541
1542/*
1543 * Initialize sysfs for each bond. This sets up and registers
1544 * the 'bondctl' directory for each individual bond under /sys/class/net.
1545 */
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001546void bond_prepare_sysfs_group(struct bonding *bond)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001547{
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001548 bond->dev->sysfs_groups[0] = &bonding_group;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001549}
1550