blob: 5acd557cea9ba7ab4c4cbcb36323dccee02bff20 [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 */
Wagner Ferencb8843662007-12-06 23:40:30 -080054static ssize_t bonding_show_bonds(struct class *cls, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080055{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000056 struct net *net = current->nsproxy->net_ns;
57 struct bond_net *bn = net_generic(net, bond_net_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080058 int res = 0;
59 struct bonding *bond;
60
Stephen Hemminger7e083842009-06-12 19:02:46 +000061 rtnl_lock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080062
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000063 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -080064 if (res > (PAGE_SIZE - IFNAMSIZ)) {
65 /* not enough space for another interface name */
66 if ((PAGE_SIZE - res) > 10)
67 res = PAGE_SIZE - 10;
Wagner Ferencb8843662007-12-06 23:40:30 -080068 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -080069 break;
70 }
Wagner Ferencb8843662007-12-06 23:40:30 -080071 res += sprintf(buf + res, "%s ", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080072 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -080073 if (res)
74 buf[res-1] = '\n'; /* eat the leftover space */
Stephen Hemminger7e083842009-06-12 19:02:46 +000075
76 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080077 return res;
78}
79
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000080static struct net_device *bond_get_by_name(struct net *net, const char *ifname)
Stephen Hemminger373500d2009-06-12 19:02:50 +000081{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000082 struct bond_net *bn = net_generic(net, bond_net_id);
Stephen Hemminger373500d2009-06-12 19:02:50 +000083 struct bonding *bond;
84
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000085 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Stephen Hemminger373500d2009-06-12 19:02:50 +000086 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
87 return bond->dev;
88 }
89 return NULL;
90}
91
Mitch Williamsb76cdba2005-11-09 10:36:41 -080092/*
93 * "store" function for the bond_masters attribute. This is what
94 * creates and deletes entire bonds.
95 *
96 * The class parameter is ignored.
97 *
98 */
99
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000100static ssize_t bonding_store_bonds(struct class *cls,
101 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800102{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +0000103 struct net *net = current->nsproxy->net_ns;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800104 char command[IFNAMSIZ + 1] = {0, };
105 char *ifname;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800106 int rv, res = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800107
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800108 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
109 ifname = command + 1;
110 if ((strlen(command) <= 1) ||
111 !dev_valid_name(ifname))
112 goto err_no_cmd;
113
114 if (command[0] == '+') {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800115 pr_info("%s is being created...\n", ifname);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +0000116 rv = bond_create(net, ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800117 if (rv) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800118 pr_info("Bond creation failed.\n");
Jay Vosburgh027ea042008-01-17 16:25:02 -0800119 res = rv;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800120 }
Stephen Hemminger373500d2009-06-12 19:02:50 +0000121 } else if (command[0] == '-') {
122 struct net_device *bond_dev;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800123
Jay Vosburgh027ea042008-01-17 16:25:02 -0800124 rtnl_lock();
Eric W. Biedermanec87fd32009-10-29 14:18:26 +0000125 bond_dev = bond_get_by_name(net, ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000126 if (bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800127 pr_info("%s is being deleted...\n", ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000128 unregister_netdevice(bond_dev);
129 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800130 pr_err("unable to delete non-existent %s\n", ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000131 res = -ENODEV;
132 }
133 rtnl_unlock();
134 } else
135 goto err_no_cmd;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800136
Stephen Hemminger373500d2009-06-12 19:02:50 +0000137 /* Always return either count or an error. If you return 0, you'll
138 * get called forever, which is bad.
139 */
140 return res;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800141
142err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800143 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700144 return -EPERM;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800145}
Stephen Hemminger373500d2009-06-12 19:02:50 +0000146
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800147/* class attribute for bond_masters file. This ends up in /sys/class/net */
148static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
149 bonding_show_bonds, bonding_store_bonds);
150
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000151int bond_create_slave_symlinks(struct net_device *master,
152 struct net_device *slave)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800153{
154 char linkname[IFNAMSIZ+7];
155 int ret = 0;
156
157 /* first, create a link from the slave back to the master */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700158 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800159 "master");
160 if (ret)
161 return ret;
162 /* next, create a link from the master to the slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000163 sprintf(linkname, "slave_%s", slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700164 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800165 linkname);
166 return ret;
167
168}
169
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000170void bond_destroy_slave_symlinks(struct net_device *master,
171 struct net_device *slave)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800172{
173 char linkname[IFNAMSIZ+7];
174
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700175 sysfs_remove_link(&(slave->dev.kobj), "master");
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000176 sprintf(linkname, "slave_%s", slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700177 sysfs_remove_link(&(master->dev.kobj), linkname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800178}
179
180
181/*
182 * Show the slaves in the current bond.
183 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700184static ssize_t bonding_show_slaves(struct device *d,
185 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800186{
187 struct slave *slave;
188 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700189 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800190
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700191 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800192 bond_for_each_slave(bond, slave, i) {
193 if (res > (PAGE_SIZE - IFNAMSIZ)) {
194 /* not enough space for another interface name */
195 if ((PAGE_SIZE - res) > 10)
196 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800197 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800198 break;
199 }
200 res += sprintf(buf + res, "%s ", slave->dev->name);
201 }
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700202 read_unlock(&bond->lock);
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800203 if (res)
204 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800205 return res;
206}
207
208/*
209 * Set the slaves in the current bond. The bond interface must be
210 * up for this to succeed.
211 * This function is largely the same flow as bonding_update_bonds().
212 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700213static ssize_t bonding_store_slaves(struct device *d,
214 struct device_attribute *attr,
215 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800216{
217 char command[IFNAMSIZ + 1] = { 0, };
218 char *ifname;
219 int i, res, found, ret = count;
Moni Shoua3158bf72007-10-09 19:43:41 -0700220 u32 original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800221 struct slave *slave;
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -0800222 struct net_device *dev = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700223 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800224
225 /* Quick sanity check -- is the bond interface up? */
226 if (!(bond->dev->flags & IFF_UP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800227 pr_warning("%s: doing slave updates when interface is down.\n",
228 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800229 }
230
231 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
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
242 if (command[0] == '+') {
243
244 /* Got a slave name in ifname. Is it already in the list? */
245 found = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800246
Eric W. Biedermanec87fd32009-10-29 14:18:26 +0000247 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800248 if (!dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800249 pr_info("%s: Interface %s does not exist!\n",
250 bond->dev->name, ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000251 ret = -ENODEV;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800252 goto out;
Stephen Hemminger373500d2009-06-12 19:02:50 +0000253 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800254
255 if (dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800256 pr_err("%s: Error: Unable to enslave %s because it is already up.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800257 bond->dev->name, dev->name);
258 ret = -EPERM;
259 goto out;
260 }
Stephen Hemminger373500d2009-06-12 19:02:50 +0000261
262 read_lock(&bond->lock);
263 bond_for_each_slave(bond, slave, i)
264 if (slave->dev == dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800265 pr_err("%s: Interface %s is already enslaved!\n",
Stephen Hemminger373500d2009-06-12 19:02:50 +0000266 bond->dev->name, ifname);
267 ret = -EPERM;
268 read_unlock(&bond->lock);
269 goto out;
270 }
271 read_unlock(&bond->lock);
272
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800273 pr_info("%s: Adding slave %s.\n", bond->dev->name, ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000274
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800275 /* If this is the first slave, then we need to set
276 the master's hardware address to be the same as the
277 slave's. */
Stephen Hemminger5c5129b2009-06-12 19:02:51 +0000278 if (is_zero_ether_addr(bond->dev->dev_addr))
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800279 memcpy(bond->dev->dev_addr, dev->dev_addr,
280 dev->addr_len);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800281
282 /* Set the slave's MTU to match the bond */
Moni Shoua3158bf72007-10-09 19:43:41 -0700283 original_mtu = dev->mtu;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800284 res = dev_set_mtu(dev, bond->dev->mtu);
285 if (res) {
286 ret = res;
287 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800288 }
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800289
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800290 res = bond_enslave(bond->dev, dev);
Moni Shoua3158bf72007-10-09 19:43:41 -0700291 bond_for_each_slave(bond, slave, i)
292 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
293 slave->original_mtu = original_mtu;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000294 if (res)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800295 ret = res;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000296
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800297 goto out;
298 }
299
300 if (command[0] == '-') {
301 dev = NULL;
David S. Miller6952d8922008-03-28 16:15:38 -0700302 original_mtu = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800303 bond_for_each_slave(bond, slave, i)
304 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
305 dev = slave->dev;
Moni Shoua3158bf72007-10-09 19:43:41 -0700306 original_mtu = slave->original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800307 break;
308 }
309 if (dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800310 pr_info("%s: Removing slave %s\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800311 bond->dev->name, dev->name);
Moni Shouad90a1622007-10-09 19:43:43 -0700312 res = bond_release(bond->dev, dev);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800313 if (res) {
314 ret = res;
315 goto out;
316 }
317 /* set the slave MTU to the default */
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800318 dev_set_mtu(dev, original_mtu);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000319 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800320 pr_err("unable to remove non-existent slave %s for bond %s.\n",
321 ifname, bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800322 ret = -ENODEV;
323 }
324 goto out;
325 }
326
327err_no_cmd:
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800328 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
329 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800330 ret = -EPERM;
331
332out:
Jay Vosburgh027ea042008-01-17 16:25:02 -0800333 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800334 return ret;
335}
336
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000337static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
338 bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800339
340/*
341 * Show and set the bonding mode. The bond interface must be down to
342 * change the mode.
343 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700344static ssize_t bonding_show_mode(struct device *d,
345 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800346{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700347 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800348
349 return sprintf(buf, "%s %d\n",
350 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800351 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800352}
353
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700354static ssize_t bonding_store_mode(struct device *d,
355 struct device_attribute *attr,
356 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800357{
358 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700359 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800360
361 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800362 pr_err("unable to update mode of %s because interface is up.\n",
363 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800364 ret = -EPERM;
365 goto out;
366 }
367
Jay Vosburghece95f72008-01-17 16:25:01 -0800368 new_value = bond_parse_parm(buf, bond_mode_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800369 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800370 pr_err("%s: Ignoring invalid mode value %.*s.\n",
371 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800372 ret = -EINVAL;
373 goto out;
374 } else {
Jay Vosburgh8f903c72006-02-21 16:36:44 -0800375 if (bond->params.mode == BOND_MODE_8023AD)
376 bond_unset_master_3ad_flags(bond);
377
378 if (bond->params.mode == BOND_MODE_ALB)
379 bond_unset_master_alb_flags(bond);
380
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800381 bond->params.mode = new_value;
382 bond_set_mode_ops(bond, bond->params.mode);
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800383 pr_info("%s: setting mode to %s (%d).\n",
384 bond->dev->name, bond_mode_tbl[new_value].modename,
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000385 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800386 }
387out:
388 return ret;
389}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000390static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
391 bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800392
393/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000394 * Show and set the bonding transmit hash method.
395 * The bond interface must be down to change the xmit hash policy.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800396 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700397static ssize_t bonding_show_xmit_hash(struct device *d,
398 struct device_attribute *attr,
399 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800400{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700401 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800402
Wagner Ferenc8e4b9322007-12-06 23:40:32 -0800403 return sprintf(buf, "%s %d\n",
404 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
405 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800406}
407
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700408static ssize_t bonding_store_xmit_hash(struct device *d,
409 struct device_attribute *attr,
410 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800411{
412 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700413 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800414
415 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800416 pr_err("%s: Interface is up. Unable to update xmit policy.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800417 bond->dev->name);
418 ret = -EPERM;
419 goto out;
420 }
421
Jay Vosburghece95f72008-01-17 16:25:01 -0800422 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800423 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800424 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800425 bond->dev->name,
426 (int)strlen(buf) - 1, buf);
427 ret = -EINVAL;
428 goto out;
429 } else {
430 bond->params.xmit_policy = new_value;
431 bond_set_mode_ops(bond, bond->params.mode);
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800432 pr_info("%s: setting xmit hash policy to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000433 bond->dev->name,
434 xmit_hashtype_tbl[new_value].modename, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800435 }
436out:
437 return ret;
438}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000439static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
440 bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800441
442/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700443 * Show and set arp_validate.
444 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700445static ssize_t bonding_show_arp_validate(struct device *d,
446 struct device_attribute *attr,
447 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700448{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700449 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700450
451 return sprintf(buf, "%s %d\n",
452 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800453 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700454}
455
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700456static ssize_t bonding_store_arp_validate(struct device *d,
457 struct device_attribute *attr,
458 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700459{
460 int new_value;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700461 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700462
Jay Vosburghece95f72008-01-17 16:25:01 -0800463 new_value = bond_parse_parm(buf, arp_validate_tbl);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700464 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800465 pr_err("%s: Ignoring invalid arp_validate value %s\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700466 bond->dev->name, buf);
467 return -EINVAL;
468 }
469 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800470 pr_err("%s: arp_validate only supported in active-backup mode.\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700471 bond->dev->name);
472 return -EINVAL;
473 }
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800474 pr_info("%s: setting arp_validate to %s (%d).\n",
475 bond->dev->name, arp_validate_tbl[new_value].modename,
476 new_value);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700477
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000478 if (!bond->params.arp_validate && new_value)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700479 bond_register_arp(bond);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000480 else if (bond->params.arp_validate && !new_value)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700481 bond_unregister_arp(bond);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700482
483 bond->params.arp_validate = new_value;
484
485 return count;
486}
487
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000488static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
489 bonding_store_arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700490
491/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700492 * Show and store fail_over_mac. User only allowed to change the
493 * value when there are no slaves.
494 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000495static ssize_t bonding_show_fail_over_mac(struct device *d,
496 struct device_attribute *attr,
497 char *buf)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700498{
499 struct bonding *bond = to_bond(d);
500
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700501 return sprintf(buf, "%s %d\n",
502 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
503 bond->params.fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700504}
505
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000506static ssize_t bonding_store_fail_over_mac(struct device *d,
507 struct device_attribute *attr,
508 const char *buf, size_t count)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700509{
510 int new_value;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700511 struct bonding *bond = to_bond(d);
512
513 if (bond->slave_cnt != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800514 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
Jay Vosburghdd957c52007-10-09 19:57:24 -0700515 bond->dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700516 return -EPERM;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700517 }
518
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700519 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
520 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800521 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700522 bond->dev->name, buf);
523 return -EINVAL;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700524 }
525
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700526 bond->params.fail_over_mac = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800527 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
528 bond->dev->name, fail_over_mac_tbl[new_value].modename,
529 new_value);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700530
531 return count;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700532}
533
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000534static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
535 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700536
537/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800538 * Show and set the arp timer interval. There are two tricky bits
539 * here. First, if ARP monitoring is activated, then we must disable
540 * MII monitoring. Second, if the ARP timer isn't running, we must
541 * start it.
542 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700543static ssize_t bonding_show_arp_interval(struct device *d,
544 struct device_attribute *attr,
545 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800546{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700547 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800548
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800549 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800550}
551
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700552static ssize_t bonding_store_arp_interval(struct device *d,
553 struct device_attribute *attr,
554 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800555{
556 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700557 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800558
559 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800560 pr_err("%s: no arp_interval value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800561 bond->dev->name);
562 ret = -EINVAL;
563 goto out;
564 }
565 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800566 pr_err("%s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800567 bond->dev->name, new_value, INT_MAX);
568 ret = -EINVAL;
569 goto out;
570 }
571
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800572 pr_info("%s: Setting ARP monitoring interval to %d.\n",
573 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800574 bond->params.arp_interval = new_value;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -0800575 if (bond->params.arp_interval)
576 bond->dev->priv_flags |= IFF_MASTER_ARPMON;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800577 if (bond->params.miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800578 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
579 bond->dev->name, bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800580 bond->params.miimon = 0;
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700581 if (delayed_work_pending(&bond->mii_work)) {
582 cancel_delayed_work(&bond->mii_work);
583 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800584 }
585 }
586 if (!bond->params.arp_targets[0]) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800587 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
588 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800589 }
590 if (bond->dev->flags & IFF_UP) {
591 /* If the interface is up, we may need to fire off
592 * the ARP timer. If the interface is down, the
593 * timer will get fired off when the open function
594 * is called.
595 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700596 if (!delayed_work_pending(&bond->arp_work)) {
597 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
598 INIT_DELAYED_WORK(&bond->arp_work,
599 bond_activebackup_arp_mon);
600 else
601 INIT_DELAYED_WORK(&bond->arp_work,
602 bond_loadbalance_arp_mon);
603
604 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800605 }
606 }
607
608out:
609 return ret;
610}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000611static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
612 bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800613
614/*
615 * Show and set the arp targets.
616 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700617static ssize_t bonding_show_arp_targets(struct device *d,
618 struct device_attribute *attr,
619 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800620{
621 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700622 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800623
624 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
625 if (bond->params.arp_targets[i])
Harvey Harrison63779432008-10-31 00:56:00 -0700626 res += sprintf(buf + res, "%pI4 ",
627 &bond->params.arp_targets[i]);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800628 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800629 if (res)
630 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800631 return res;
632}
633
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700634static ssize_t bonding_store_arp_targets(struct device *d,
635 struct device_attribute *attr,
636 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800637{
Al Virod3bb52b2007-08-22 20:06:58 -0400638 __be32 newtarget;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800639 int i = 0, done = 0, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700640 struct bonding *bond = to_bond(d);
Al Virod3bb52b2007-08-22 20:06:58 -0400641 __be32 *targets;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800642
643 targets = bond->params.arp_targets;
644 newtarget = in_aton(buf + 1);
645 /* look for adds */
646 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400647 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800648 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700649 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800650 ret = -EINVAL;
651 goto out;
652 }
653 /* look for an empty slot to put the target in, and check for dupes */
Brian Haley5a31bec2009-04-13 00:11:30 -0700654 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800655 if (targets[i] == newtarget) { /* duplicate */
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800656 pr_err("%s: ARP target %pI4 is already present\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700657 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800658 ret = -EINVAL;
659 goto out;
660 }
Brian Haley5a31bec2009-04-13 00:11:30 -0700661 if (targets[i] == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800662 pr_info("%s: adding ARP target %pI4.\n",
663 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800664 done = 1;
665 targets[i] = newtarget;
666 }
667 }
668 if (!done) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800669 pr_err("%s: ARP target table is full!\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800670 bond->dev->name);
671 ret = -EINVAL;
672 goto out;
673 }
674
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000675 } else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400676 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800677 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
Harvey Harrison63779432008-10-31 00:56:00 -0700678 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800679 ret = -EINVAL;
680 goto out;
681 }
682
Brian Haley5a31bec2009-04-13 00:11:30 -0700683 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800684 if (targets[i] == newtarget) {
Brian Haley5a31bec2009-04-13 00:11:30 -0700685 int j;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800686 pr_info("%s: removing ARP target %pI4.\n",
687 bond->dev->name, &newtarget);
Brian Haley5a31bec2009-04-13 00:11:30 -0700688 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
689 targets[j] = targets[j+1];
690
691 targets[j] = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800692 done = 1;
693 }
694 }
695 if (!done) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800696 pr_info("%s: unable to remove nonexistent ARP target %pI4.\n",
697 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800698 ret = -EINVAL;
699 goto out;
700 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000701 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800702 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
703 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800704 ret = -EPERM;
705 goto out;
706 }
707
708out:
709 return ret;
710}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700711static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800712
713/*
714 * Show and set the up and down delays. These must be multiples of the
715 * MII monitoring value, and are stored internally as the multiplier.
716 * Thus, we must translate to MS for the real world.
717 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700718static ssize_t bonding_show_downdelay(struct device *d,
719 struct device_attribute *attr,
720 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800721{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700722 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800723
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800724 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800725}
726
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700727static ssize_t bonding_store_downdelay(struct device *d,
728 struct device_attribute *attr,
729 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800730{
731 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700732 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800733
734 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800735 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800736 bond->dev->name);
737 ret = -EPERM;
738 goto out;
739 }
740
741 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800742 pr_err("%s: no down delay value specified.\n", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800743 ret = -EINVAL;
744 goto out;
745 }
746 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800747 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800748 bond->dev->name, new_value, 1, INT_MAX);
749 ret = -EINVAL;
750 goto out;
751 } else {
752 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800753 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 +0000754 bond->dev->name, new_value,
755 bond->params.miimon,
756 (new_value / bond->params.miimon) *
757 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800758 }
759 bond->params.downdelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800760 pr_info("%s: Setting down delay to %d.\n",
761 bond->dev->name,
762 bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800763
764 }
765
766out:
767 return ret;
768}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000769static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
770 bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800771
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700772static ssize_t bonding_show_updelay(struct device *d,
773 struct device_attribute *attr,
774 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800775{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700776 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800777
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800778 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800779
780}
781
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700782static ssize_t bonding_store_updelay(struct device *d,
783 struct device_attribute *attr,
784 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800785{
786 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700787 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800788
789 if (!(bond->params.miimon)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800790 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800791 bond->dev->name);
792 ret = -EPERM;
793 goto out;
794 }
795
796 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800797 pr_err("%s: no up delay value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800798 bond->dev->name);
799 ret = -EINVAL;
800 goto out;
801 }
802 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800803 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800804 bond->dev->name, new_value, 1, INT_MAX);
805 ret = -EINVAL;
806 goto out;
807 } else {
808 if ((new_value % bond->params.miimon) != 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800809 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 +0000810 bond->dev->name, new_value,
811 bond->params.miimon,
812 (new_value / bond->params.miimon) *
813 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800814 }
815 bond->params.updelay = new_value / bond->params.miimon;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800816 pr_info("%s: Setting up delay to %d.\n",
817 bond->dev->name,
818 bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800819 }
820
821out:
822 return ret;
823}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000824static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
825 bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800826
827/*
828 * Show and set the LACP interval. Interface must be down, and the mode
829 * must be set to 802.3ad mode.
830 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700831static ssize_t bonding_show_lacp(struct device *d,
832 struct device_attribute *attr,
833 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800834{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700835 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800836
837 return sprintf(buf, "%s %d\n",
838 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800839 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800840}
841
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700842static ssize_t bonding_store_lacp(struct device *d,
843 struct device_attribute *attr,
844 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800845{
846 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700847 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800848
849 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800850 pr_err("%s: Unable to update LACP rate because interface is up.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800851 bond->dev->name);
852 ret = -EPERM;
853 goto out;
854 }
855
856 if (bond->params.mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800857 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800858 bond->dev->name);
859 ret = -EPERM;
860 goto out;
861 }
862
Jay Vosburghece95f72008-01-17 16:25:01 -0800863 new_value = bond_parse_parm(buf, bond_lacp_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800864
865 if ((new_value == 1) || (new_value == 0)) {
866 bond->params.lacp_fast = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800867 pr_info("%s: Setting LACP rate to %s (%d).\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000868 bond->dev->name, bond_lacp_tbl[new_value].modename,
869 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800870 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800871 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000872 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800873 ret = -EINVAL;
874 }
875out:
876 return ret;
877}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000878static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
879 bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800880
Jay Vosburghfd989c82008-11-04 17:51:16 -0800881static ssize_t bonding_show_ad_select(struct device *d,
882 struct device_attribute *attr,
883 char *buf)
884{
885 struct bonding *bond = to_bond(d);
886
887 return sprintf(buf, "%s %d\n",
888 ad_select_tbl[bond->params.ad_select].modename,
889 bond->params.ad_select);
890}
891
892
893static ssize_t bonding_store_ad_select(struct device *d,
894 struct device_attribute *attr,
895 const char *buf, size_t count)
896{
897 int new_value, ret = count;
898 struct bonding *bond = to_bond(d);
899
900 if (bond->dev->flags & IFF_UP) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800901 pr_err("%s: Unable to update ad_select because interface is up.\n",
902 bond->dev->name);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800903 ret = -EPERM;
904 goto out;
905 }
906
907 new_value = bond_parse_parm(buf, ad_select_tbl);
908
909 if (new_value != -1) {
910 bond->params.ad_select = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800911 pr_info("%s: Setting ad_select to %s (%d).\n",
912 bond->dev->name, ad_select_tbl[new_value].modename,
913 new_value);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800914 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800915 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
Jay Vosburghfd989c82008-11-04 17:51:16 -0800916 bond->dev->name, (int)strlen(buf) - 1, buf);
917 ret = -EINVAL;
918 }
919out:
920 return ret;
921}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000922static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
923 bonding_show_ad_select, bonding_store_ad_select);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800924
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800925/*
Moni Shoua7893b242008-05-17 21:10:12 -0700926 * Show and set the number of grat ARP to send after a failover event.
927 */
928static ssize_t bonding_show_n_grat_arp(struct device *d,
929 struct device_attribute *attr,
930 char *buf)
931{
932 struct bonding *bond = to_bond(d);
933
934 return sprintf(buf, "%d\n", bond->params.num_grat_arp);
935}
936
937static ssize_t bonding_store_n_grat_arp(struct device *d,
938 struct device_attribute *attr,
939 const char *buf, size_t count)
940{
941 int new_value, ret = count;
942 struct bonding *bond = to_bond(d);
943
944 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800945 pr_err("%s: no num_grat_arp value specified.\n",
Moni Shoua7893b242008-05-17 21:10:12 -0700946 bond->dev->name);
947 ret = -EINVAL;
948 goto out;
949 }
950 if (new_value < 0 || new_value > 255) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800951 pr_err("%s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
Moni Shoua7893b242008-05-17 21:10:12 -0700952 bond->dev->name, new_value);
953 ret = -EINVAL;
954 goto out;
955 } else {
956 bond->params.num_grat_arp = new_value;
957 }
958out:
959 return ret;
960}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000961static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
962 bonding_show_n_grat_arp, bonding_store_n_grat_arp);
Brian Haley305d5522008-11-04 17:51:14 -0800963
964/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000965 * Show and set the number of unsolicited NA's to send after a failover event.
Brian Haley305d5522008-11-04 17:51:14 -0800966 */
967static ssize_t bonding_show_n_unsol_na(struct device *d,
968 struct device_attribute *attr,
969 char *buf)
970{
971 struct bonding *bond = to_bond(d);
972
973 return sprintf(buf, "%d\n", bond->params.num_unsol_na);
974}
975
976static ssize_t bonding_store_n_unsol_na(struct device *d,
977 struct device_attribute *attr,
978 const char *buf, size_t count)
979{
980 int new_value, ret = count;
981 struct bonding *bond = to_bond(d);
982
983 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800984 pr_err("%s: no num_unsol_na value specified.\n",
Brian Haley305d5522008-11-04 17:51:14 -0800985 bond->dev->name);
986 ret = -EINVAL;
987 goto out;
988 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000989
Brian Haley305d5522008-11-04 17:51:14 -0800990 if (new_value < 0 || new_value > 255) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800991 pr_err("%s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
Brian Haley305d5522008-11-04 17:51:14 -0800992 bond->dev->name, new_value);
993 ret = -EINVAL;
994 goto out;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000995 } else
Brian Haley305d5522008-11-04 17:51:14 -0800996 bond->params.num_unsol_na = new_value;
Brian Haley305d5522008-11-04 17:51:14 -0800997out:
998 return ret;
999}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001000static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
1001 bonding_show_n_unsol_na, bonding_store_n_unsol_na);
Brian Haley305d5522008-11-04 17:51:14 -08001002
Moni Shoua7893b242008-05-17 21:10:12 -07001003/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001004 * Show and set the MII monitor interval. There are two tricky bits
1005 * here. First, if MII monitoring is activated, then we must disable
1006 * ARP monitoring. Second, if the timer isn't running, we must
1007 * start it.
1008 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001009static ssize_t bonding_show_miimon(struct device *d,
1010 struct device_attribute *attr,
1011 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001012{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001013 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001014
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001015 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001016}
1017
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001018static ssize_t bonding_store_miimon(struct device *d,
1019 struct device_attribute *attr,
1020 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001021{
1022 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001023 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001024
1025 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001026 pr_err("%s: no miimon value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001027 bond->dev->name);
1028 ret = -EINVAL;
1029 goto out;
1030 }
1031 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001032 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001033 bond->dev->name, new_value, 1, INT_MAX);
1034 ret = -EINVAL;
1035 goto out;
1036 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001037 pr_info("%s: Setting MII monitoring interval to %d.\n",
1038 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001039 bond->params.miimon = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001040 if (bond->params.updelay)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001041 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
1042 bond->dev->name,
1043 bond->params.updelay * bond->params.miimon);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001044 if (bond->params.downdelay)
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001045 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
1046 bond->dev->name,
1047 bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001048 if (bond->params.arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001049 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
1050 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001051 bond->params.arp_interval = 0;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -08001052 bond->dev->priv_flags &= ~IFF_MASTER_ARPMON;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001053 if (bond->params.arp_validate) {
1054 bond_unregister_arp(bond);
1055 bond->params.arp_validate =
1056 BOND_ARP_VALIDATE_NONE;
1057 }
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001058 if (delayed_work_pending(&bond->arp_work)) {
1059 cancel_delayed_work(&bond->arp_work);
1060 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001061 }
1062 }
1063
1064 if (bond->dev->flags & IFF_UP) {
1065 /* If the interface is up, we may need to fire off
1066 * the MII timer. If the interface is down, the
1067 * timer will get fired off when the open function
1068 * is called.
1069 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001070 if (!delayed_work_pending(&bond->mii_work)) {
1071 INIT_DELAYED_WORK(&bond->mii_work,
1072 bond_mii_monitor);
1073 queue_delayed_work(bond->wq,
1074 &bond->mii_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001075 }
1076 }
1077 }
1078out:
1079 return ret;
1080}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001081static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1082 bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001083
1084/*
1085 * Show and set the primary slave. The store function is much
1086 * simpler than bonding_store_slaves function because it only needs to
1087 * handle one interface name.
1088 * The bond must be a mode that supports a primary for this be
1089 * set.
1090 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001091static ssize_t bonding_show_primary(struct device *d,
1092 struct device_attribute *attr,
1093 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001094{
1095 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001096 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001097
1098 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001099 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001100
1101 return count;
1102}
1103
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001104static ssize_t bonding_store_primary(struct device *d,
1105 struct device_attribute *attr,
1106 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001107{
1108 int i;
1109 struct slave *slave;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001110 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001111
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001112 if (!rtnl_trylock())
1113 return restart_syscall();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001114 read_lock(&bond->lock);
1115 write_lock_bh(&bond->curr_slave_lock);
1116
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001117 if (!USES_PRIMARY(bond->params.mode)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001118 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1119 bond->dev->name, bond->dev->name, bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001120 } else {
1121 bond_for_each_slave(bond, slave, i) {
1122 if (strnicmp
1123 (slave->dev->name, buf,
1124 strlen(slave->dev->name)) == 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001125 pr_info("%s: Setting %s as primary slave.\n",
1126 bond->dev->name, slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001127 bond->primary_slave = slave;
Jiri Pirkoce501ca2009-09-18 02:13:22 +00001128 strcpy(bond->params.primary, slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001129 bond_select_active_slave(bond);
1130 goto out;
1131 }
1132 }
1133
1134 /* if we got here, then we didn't match the name of any slave */
1135
1136 if (strlen(buf) == 0 || buf[0] == '\n') {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001137 pr_info("%s: Setting primary slave to None.\n",
1138 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001139 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001140 bond_select_active_slave(bond);
1141 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001142 pr_info("%s: Unable to set %.*s as primary slave as it is not a slave.\n",
1143 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001144 }
1145 }
1146out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001147 write_unlock_bh(&bond->curr_slave_lock);
1148 read_unlock(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001149 rtnl_unlock();
1150
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001151 return count;
1152}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001153static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1154 bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001155
1156/*
Jiri Pirkoa5499522009-09-25 03:28:09 +00001157 * Show and set the primary_reselect flag.
1158 */
1159static ssize_t bonding_show_primary_reselect(struct device *d,
1160 struct device_attribute *attr,
1161 char *buf)
1162{
1163 struct bonding *bond = to_bond(d);
1164
1165 return sprintf(buf, "%s %d\n",
1166 pri_reselect_tbl[bond->params.primary_reselect].modename,
1167 bond->params.primary_reselect);
1168}
1169
1170static ssize_t bonding_store_primary_reselect(struct device *d,
1171 struct device_attribute *attr,
1172 const char *buf, size_t count)
1173{
1174 int new_value, ret = count;
1175 struct bonding *bond = to_bond(d);
1176
1177 if (!rtnl_trylock())
1178 return restart_syscall();
1179
1180 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1181 if (new_value < 0) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001182 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001183 bond->dev->name,
1184 (int) strlen(buf) - 1, buf);
1185 ret = -EINVAL;
1186 goto out;
1187 }
1188
1189 bond->params.primary_reselect = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001190 pr_info("%s: setting primary_reselect to %s (%d).\n",
Jiri Pirkoa5499522009-09-25 03:28:09 +00001191 bond->dev->name, pri_reselect_tbl[new_value].modename,
1192 new_value);
1193
1194 read_lock(&bond->lock);
1195 write_lock_bh(&bond->curr_slave_lock);
1196 bond_select_active_slave(bond);
1197 write_unlock_bh(&bond->curr_slave_lock);
1198 read_unlock(&bond->lock);
1199out:
1200 rtnl_unlock();
1201 return ret;
1202}
1203static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1204 bonding_show_primary_reselect,
1205 bonding_store_primary_reselect);
1206
1207/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001208 * Show and set the use_carrier flag.
1209 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001210static ssize_t bonding_show_carrier(struct device *d,
1211 struct device_attribute *attr,
1212 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001213{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001214 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001215
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001216 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001217}
1218
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001219static ssize_t bonding_store_carrier(struct device *d,
1220 struct device_attribute *attr,
1221 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001222{
1223 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001224 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001225
1226
1227 if (sscanf(buf, "%d", &new_value) != 1) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001228 pr_err("%s: no use_carrier value specified.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001229 bond->dev->name);
1230 ret = -EINVAL;
1231 goto out;
1232 }
1233 if ((new_value == 0) || (new_value == 1)) {
1234 bond->params.use_carrier = new_value;
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001235 pr_info("%s: Setting use_carrier to %d.\n",
1236 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001237 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001238 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1239 bond->dev->name, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001240 }
1241out:
1242 return count;
1243}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001244static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1245 bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001246
1247
1248/*
1249 * Show and set currently active_slave.
1250 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001251static ssize_t bonding_show_active_slave(struct device *d,
1252 struct device_attribute *attr,
1253 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001254{
1255 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001256 struct bonding *bond = to_bond(d);
Wagner Ferenc16cd0162007-12-06 23:40:29 -08001257 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001258
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001259 read_lock(&bond->curr_slave_lock);
1260 curr = bond->curr_active_slave;
1261 read_unlock(&bond->curr_slave_lock);
1262
1263 if (USES_PRIMARY(bond->params.mode) && curr)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001264 count = sprintf(buf, "%s\n", curr->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001265 return count;
1266}
1267
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001268static ssize_t bonding_store_active_slave(struct device *d,
1269 struct device_attribute *attr,
1270 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001271{
1272 int i;
1273 struct slave *slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001274 struct slave *old_active = NULL;
1275 struct slave *new_active = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001276 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001277
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001278 if (!rtnl_trylock())
1279 return restart_syscall();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001280 read_lock(&bond->lock);
1281 write_lock_bh(&bond->curr_slave_lock);
Jay Vosburgh1466a212007-11-06 13:33:28 -08001282
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001283 if (!USES_PRIMARY(bond->params.mode))
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001284 pr_info("%s: Unable to change active slave; %s is in mode %d\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001285 bond->dev->name, bond->dev->name, bond->params.mode);
1286 else {
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001287 bond_for_each_slave(bond, slave, i) {
1288 if (strnicmp
1289 (slave->dev->name, buf,
1290 strlen(slave->dev->name)) == 0) {
1291 old_active = bond->curr_active_slave;
1292 new_active = slave;
Jay Vosburgha50d8de2006-09-22 21:53:25 -07001293 if (new_active == old_active) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001294 /* do nothing */
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001295 pr_info("%s: %s is already the current active slave.\n",
1296 bond->dev->name,
1297 slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001298 goto out;
1299 }
1300 else {
1301 if ((new_active) &&
1302 (old_active) &&
1303 (new_active->link == BOND_LINK_UP) &&
1304 IS_UP(new_active->dev)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001305 pr_info("%s: Setting %s as active slave.\n",
1306 bond->dev->name,
1307 slave->dev->name);
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00001308 bond_change_active_slave(bond, new_active);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001309 }
1310 else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001311 pr_info("%s: Could not set %s as active slave; either %s is down or the link is down.\n",
1312 bond->dev->name,
1313 slave->dev->name,
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00001314 slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001315 }
1316 goto out;
1317 }
1318 }
1319 }
1320
1321 /* if we got here, then we didn't match the name of any slave */
1322
1323 if (strlen(buf) == 0 || buf[0] == '\n') {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001324 pr_info("%s: Setting active slave to None.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001325 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001326 bond->primary_slave = NULL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001327 bond_select_active_slave(bond);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001328 } else {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001329 pr_info("%s: Unable to set %.*s as active slave as it is not a slave.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001330 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001331 }
1332 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001333 out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001334 write_unlock_bh(&bond->curr_slave_lock);
1335 read_unlock(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001336 rtnl_unlock();
1337
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001338 return count;
1339
1340}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001341static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1342 bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001343
1344
1345/*
1346 * Show link status of the bond interface.
1347 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001348static ssize_t bonding_show_mii_status(struct device *d,
1349 struct device_attribute *attr,
1350 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001351{
1352 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001353 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001354
1355 read_lock(&bond->curr_slave_lock);
1356 curr = bond->curr_active_slave;
1357 read_unlock(&bond->curr_slave_lock);
1358
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001359 return sprintf(buf, "%s\n", curr ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001360}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001361static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001362
1363
1364/*
1365 * Show current 802.3ad aggregator ID.
1366 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001367static ssize_t bonding_show_ad_aggregator(struct device *d,
1368 struct device_attribute *attr,
1369 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001370{
1371 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001372 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001373
1374 if (bond->params.mode == BOND_MODE_8023AD) {
1375 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001376 count = sprintf(buf, "%d\n",
1377 (bond_3ad_get_active_agg_info(bond, &ad_info))
1378 ? 0 : ad_info.aggregator_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001379 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001380
1381 return count;
1382}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001383static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001384
1385
1386/*
1387 * Show number of active 802.3ad ports.
1388 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001389static ssize_t bonding_show_ad_num_ports(struct device *d,
1390 struct device_attribute *attr,
1391 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001392{
1393 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001394 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001395
1396 if (bond->params.mode == BOND_MODE_8023AD) {
1397 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001398 count = sprintf(buf, "%d\n",
1399 (bond_3ad_get_active_agg_info(bond, &ad_info))
1400 ? 0 : ad_info.ports);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001401 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001402
1403 return count;
1404}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001405static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001406
1407
1408/*
1409 * Show current 802.3ad actor key.
1410 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001411static ssize_t bonding_show_ad_actor_key(struct device *d,
1412 struct device_attribute *attr,
1413 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001414{
1415 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001416 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001417
1418 if (bond->params.mode == BOND_MODE_8023AD) {
1419 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001420 count = sprintf(buf, "%d\n",
1421 (bond_3ad_get_active_agg_info(bond, &ad_info))
1422 ? 0 : ad_info.actor_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001423 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001424
1425 return count;
1426}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001427static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001428
1429
1430/*
1431 * Show current 802.3ad partner key.
1432 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001433static ssize_t bonding_show_ad_partner_key(struct device *d,
1434 struct device_attribute *attr,
1435 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001436{
1437 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001438 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001439
1440 if (bond->params.mode == BOND_MODE_8023AD) {
1441 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001442 count = sprintf(buf, "%d\n",
1443 (bond_3ad_get_active_agg_info(bond, &ad_info))
1444 ? 0 : ad_info.partner_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001445 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001446
1447 return count;
1448}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001449static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001450
1451
1452/*
1453 * Show current 802.3ad partner mac.
1454 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001455static ssize_t bonding_show_ad_partner_mac(struct device *d,
1456 struct device_attribute *attr,
1457 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001458{
1459 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001460 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001461
1462 if (bond->params.mode == BOND_MODE_8023AD) {
1463 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001464 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
Johannes Berge1749612008-10-27 15:59:26 -07001465 count = sprintf(buf, "%pM\n", ad_info.partner_system);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001466 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001467
1468 return count;
1469}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001470static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001471
1472
1473
1474static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001475 &dev_attr_slaves.attr,
1476 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001477 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001478 &dev_attr_arp_validate.attr,
1479 &dev_attr_arp_interval.attr,
1480 &dev_attr_arp_ip_target.attr,
1481 &dev_attr_downdelay.attr,
1482 &dev_attr_updelay.attr,
1483 &dev_attr_lacp_rate.attr,
Jay Vosburghfd989c82008-11-04 17:51:16 -08001484 &dev_attr_ad_select.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001485 &dev_attr_xmit_hash_policy.attr,
Moni Shoua7893b242008-05-17 21:10:12 -07001486 &dev_attr_num_grat_arp.attr,
Brian Haley305d5522008-11-04 17:51:14 -08001487 &dev_attr_num_unsol_na.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001488 &dev_attr_miimon.attr,
1489 &dev_attr_primary.attr,
Jiri Pirkoa5499522009-09-25 03:28:09 +00001490 &dev_attr_primary_reselect.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001491 &dev_attr_use_carrier.attr,
1492 &dev_attr_active_slave.attr,
1493 &dev_attr_mii_status.attr,
1494 &dev_attr_ad_aggregator.attr,
1495 &dev_attr_ad_num_ports.attr,
1496 &dev_attr_ad_actor_key.attr,
1497 &dev_attr_ad_partner_key.attr,
1498 &dev_attr_ad_partner_mac.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001499 NULL,
1500};
1501
1502static struct attribute_group bonding_group = {
1503 .name = "bonding",
1504 .attrs = per_bond_attrs,
1505};
1506
1507/*
1508 * Initialize sysfs. This sets up the bonding_masters file in
1509 * /sys/class/net.
1510 */
1511int bond_create_sysfs(void)
1512{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001513 int ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001514
Jay Vosburghb8a97872008-06-13 18:12:04 -07001515 ret = netdev_class_create_file(&class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001516 /*
1517 * Permit multiple loads of the module by ignoring failures to
1518 * create the bonding_masters sysfs file. Bonding devices
1519 * created by second or subsequent loads of the module will
1520 * not be listed in, or controllable by, bonding_masters, but
1521 * will have the usual "bonding" sysfs directory.
1522 *
1523 * This is done to preserve backwards compatibility for
1524 * initscripts/sysconfig, which load bonding multiple times to
1525 * configure multiple bonding devices.
1526 */
1527 if (ret == -EEXIST) {
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001528 /* Is someone being kinky and naming a device bonding_master? */
1529 if (__dev_get_by_name(&init_net,
1530 class_attr_bonding_masters.attr.name))
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001531 pr_err("network device named %s already exists in sysfs",
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001532 class_attr_bonding_masters.attr.name);
Stephen Hemminger130aa612009-06-11 05:46:04 -07001533 ret = 0;
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001534 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001535
1536 return ret;
1537
1538}
1539
1540/*
1541 * Remove /sys/class/net/bonding_masters.
1542 */
1543void bond_destroy_sysfs(void)
1544{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001545 netdev_class_remove_file(&class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001546}
1547
1548/*
1549 * Initialize sysfs for each bond. This sets up and registers
1550 * the 'bondctl' directory for each individual bond under /sys/class/net.
1551 */
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001552void bond_prepare_sysfs_group(struct bonding *bond)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001553{
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001554 bond->dev->sysfs_groups[0] = &bonding_group;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001555}
1556