blob: e3fb7f515150b753a55aa2bfd6bd0ef95c0acb75 [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 */
Mitch Williamsb76cdba2005-11-09 10:36:41 -080022#include <linux/kernel.h>
23#include <linux/module.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080024#include <linux/device.h>
25#include <linux/sysdev.h>
26#include <linux/fs.h>
27#include <linux/types.h>
28#include <linux/string.h>
29#include <linux/netdevice.h>
30#include <linux/inetdevice.h>
31#include <linux/in.h>
32#include <linux/sysfs.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080033#include <linux/ctype.h>
34#include <linux/inet.h>
35#include <linux/rtnetlink.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070036#include <net/net_namespace.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080037
Mitch Williamsb76cdba2005-11-09 10:36:41 -080038#include "bonding.h"
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -080039
Stephen Hemminger3d632c32009-06-12 19:02:48 +000040#define to_dev(obj) container_of(obj, struct device, kobj)
Wang Chen454d7c92008-11-12 23:37:49 -080041#define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
Mitch Williamsb76cdba2005-11-09 10:36:41 -080042
Mitch Williamsb76cdba2005-11-09 10:36:41 -080043/*
44 * "show" function for the bond_masters attribute.
45 * The class parameter is ignored.
46 */
Wagner Ferencb8843662007-12-06 23:40:30 -080047static ssize_t bonding_show_bonds(struct class *cls, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080048{
49 int res = 0;
50 struct bonding *bond;
51
Stephen Hemminger7e083842009-06-12 19:02:46 +000052 rtnl_lock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080053
54 list_for_each_entry(bond, &bond_dev_list, bond_list) {
55 if (res > (PAGE_SIZE - IFNAMSIZ)) {
56 /* not enough space for another interface name */
57 if ((PAGE_SIZE - res) > 10)
58 res = PAGE_SIZE - 10;
Wagner Ferencb8843662007-12-06 23:40:30 -080059 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -080060 break;
61 }
Wagner Ferencb8843662007-12-06 23:40:30 -080062 res += sprintf(buf + res, "%s ", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080063 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -080064 if (res)
65 buf[res-1] = '\n'; /* eat the leftover space */
Stephen Hemminger7e083842009-06-12 19:02:46 +000066
67 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080068 return res;
69}
70
71/*
72 * "store" function for the bond_masters attribute. This is what
73 * creates and deletes entire bonds.
74 *
75 * The class parameter is ignored.
76 *
77 */
78
Stephen Hemminger3d632c32009-06-12 19:02:48 +000079static ssize_t bonding_store_bonds(struct class *cls,
80 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080081{
82 char command[IFNAMSIZ + 1] = {0, };
83 char *ifname;
Jay Vosburgh027ea042008-01-17 16:25:02 -080084 int rv, res = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -080085 struct bonding *bond;
Mitch Williamsb76cdba2005-11-09 10:36:41 -080086
Mitch Williamsb76cdba2005-11-09 10:36:41 -080087 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
88 ifname = command + 1;
89 if ((strlen(command) <= 1) ||
90 !dev_valid_name(ifname))
91 goto err_no_cmd;
92
93 if (command[0] == '+') {
Stephen Hemminger3d632c32009-06-12 19:02:48 +000094 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -080095 ": %s is being created...\n", ifname);
Stephen Hemmingerd2991f72009-06-12 19:02:44 +000096 rv = bond_create(ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -080097 if (rv) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +000098 pr_info(DRV_NAME ": Bond creation failed.\n");
Jay Vosburgh027ea042008-01-17 16:25:02 -080099 res = rv;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800100 }
101 goto out;
102 }
103
104 if (command[0] == '-') {
Jay Vosburgh027ea042008-01-17 16:25:02 -0800105 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -0800106
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700107 list_for_each_entry(bond, &bond_dev_list, bond_list)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800108 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000109 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800110 ": %s is being deleted...\n",
111 bond->dev->name);
Stephen Hemminger9e716262009-06-12 19:02:47 +0000112 unregister_netdevice(bond->dev);
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700113 goto out_unlock;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800114 }
115
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000116 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800117 ": unable to delete non-existent bond %s\n", ifname);
118 res = -ENODEV;
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700119 goto out_unlock;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800120 }
121
122err_no_cmd:
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000123 pr_err(DRV_NAME ": no command found in bonding_masters."
124 " Use +ifname or -ifname.\n");
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700125 return -EPERM;
126
127out_unlock:
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700128 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800129
130 /* Always return either count or an error. If you return 0, you'll
131 * get called forever, which is bad.
132 */
133out:
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800134 return res;
135}
136/* class attribute for bond_masters file. This ends up in /sys/class/net */
137static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
138 bonding_show_bonds, bonding_store_bonds);
139
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000140int bond_create_slave_symlinks(struct net_device *master,
141 struct net_device *slave)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800142{
143 char linkname[IFNAMSIZ+7];
144 int ret = 0;
145
146 /* first, create a link from the slave back to the master */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700147 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800148 "master");
149 if (ret)
150 return ret;
151 /* next, create a link from the master to the slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000152 sprintf(linkname, "slave_%s", slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700153 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800154 linkname);
155 return ret;
156
157}
158
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000159void bond_destroy_slave_symlinks(struct net_device *master,
160 struct net_device *slave)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800161{
162 char linkname[IFNAMSIZ+7];
163
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700164 sysfs_remove_link(&(slave->dev.kobj), "master");
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000165 sprintf(linkname, "slave_%s", slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700166 sysfs_remove_link(&(master->dev.kobj), linkname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800167}
168
169
170/*
171 * Show the slaves in the current bond.
172 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700173static ssize_t bonding_show_slaves(struct device *d,
174 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800175{
176 struct slave *slave;
177 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700178 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800179
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700180 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800181 bond_for_each_slave(bond, slave, i) {
182 if (res > (PAGE_SIZE - IFNAMSIZ)) {
183 /* not enough space for another interface name */
184 if ((PAGE_SIZE - res) > 10)
185 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800186 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800187 break;
188 }
189 res += sprintf(buf + res, "%s ", slave->dev->name);
190 }
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700191 read_unlock(&bond->lock);
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800192 if (res)
193 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800194 return res;
195}
196
197/*
198 * Set the slaves in the current bond. The bond interface must be
199 * up for this to succeed.
200 * This function is largely the same flow as bonding_update_bonds().
201 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700202static ssize_t bonding_store_slaves(struct device *d,
203 struct device_attribute *attr,
204 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800205{
206 char command[IFNAMSIZ + 1] = { 0, };
207 char *ifname;
208 int i, res, found, ret = count;
Moni Shoua3158bf72007-10-09 19:43:41 -0700209 u32 original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800210 struct slave *slave;
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -0800211 struct net_device *dev = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700212 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800213
214 /* Quick sanity check -- is the bond interface up? */
215 if (!(bond->dev->flags & IFF_UP)) {
Moni Shoua6b1bf092007-10-09 19:43:40 -0700216 printk(KERN_WARNING DRV_NAME
217 ": %s: doing slave updates when interface is down.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800218 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800219 }
220
221 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
222
Eric W. Biederman496a60c2009-05-13 17:02:50 +0000223 if (!rtnl_trylock())
224 return restart_syscall();
Jay Vosburgh027ea042008-01-17 16:25:02 -0800225
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800226 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
227 ifname = command + 1;
228 if ((strlen(command) <= 1) ||
229 !dev_valid_name(ifname))
230 goto err_no_cmd;
231
232 if (command[0] == '+') {
233
234 /* Got a slave name in ifname. Is it already in the list? */
235 found = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700236 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800237 bond_for_each_slave(bond, slave, i)
238 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000239 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800240 ": %s: Interface %s is already enslaved!\n",
241 bond->dev->name, ifname);
242 ret = -EPERM;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700243 read_unlock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800244 goto out;
245 }
246
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700247 read_unlock(&bond->lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000248 pr_info(DRV_NAME ": %s: Adding slave %s.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800249 bond->dev->name, ifname);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700250 dev = dev_get_by_name(&init_net, ifname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800251 if (!dev) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000252 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800253 ": %s: Interface %s does not exist!\n",
254 bond->dev->name, ifname);
255 ret = -EPERM;
256 goto out;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000257 } else
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800258 dev_put(dev);
259
260 if (dev->flags & IFF_UP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000261 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800262 ": %s: Error: Unable to enslave %s "
263 "because it is already up.\n",
264 bond->dev->name, dev->name);
265 ret = -EPERM;
266 goto out;
267 }
268 /* If this is the first slave, then we need to set
269 the master's hardware address to be the same as the
270 slave's. */
271 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
272 memcpy(bond->dev->dev_addr, dev->dev_addr,
273 dev->addr_len);
274 }
275
276 /* Set the slave's MTU to match the bond */
Moni Shoua3158bf72007-10-09 19:43:41 -0700277 original_mtu = dev->mtu;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800278 res = dev_set_mtu(dev, bond->dev->mtu);
279 if (res) {
280 ret = res;
281 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800282 }
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800283
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800284 res = bond_enslave(bond->dev, dev);
Moni Shoua3158bf72007-10-09 19:43:41 -0700285 bond_for_each_slave(bond, slave, i)
286 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
287 slave->original_mtu = original_mtu;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000288 if (res)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800289 ret = res;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000290
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800291 goto out;
292 }
293
294 if (command[0] == '-') {
295 dev = NULL;
David S. Miller6952d8922008-03-28 16:15:38 -0700296 original_mtu = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800297 bond_for_each_slave(bond, slave, i)
298 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
299 dev = slave->dev;
Moni Shoua3158bf72007-10-09 19:43:41 -0700300 original_mtu = slave->original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800301 break;
302 }
303 if (dev) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000304 pr_info(DRV_NAME ": %s: Removing slave %s\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800305 bond->dev->name, dev->name);
Moni Shouad90a1622007-10-09 19:43:43 -0700306 res = bond_release(bond->dev, dev);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800307 if (res) {
308 ret = res;
309 goto out;
310 }
311 /* set the slave MTU to the default */
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800312 dev_set_mtu(dev, original_mtu);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000313 } else {
314 pr_err(DRV_NAME ": unable to remove non-existent"
315 " slave %s for bond %s.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800316 ifname, bond->dev->name);
317 ret = -ENODEV;
318 }
319 goto out;
320 }
321
322err_no_cmd:
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000323 pr_err(DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800324 ret = -EPERM;
325
326out:
Jay Vosburgh027ea042008-01-17 16:25:02 -0800327 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800328 return ret;
329}
330
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000331static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
332 bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800333
334/*
335 * Show and set the bonding mode. The bond interface must be down to
336 * change the mode.
337 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700338static ssize_t bonding_show_mode(struct device *d,
339 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800340{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700341 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800342
343 return sprintf(buf, "%s %d\n",
344 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800345 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800346}
347
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700348static ssize_t bonding_store_mode(struct device *d,
349 struct device_attribute *attr,
350 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800351{
352 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700353 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800354
355 if (bond->dev->flags & IFF_UP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000356 pr_err(DRV_NAME ": unable to update mode of %s"
357 " because interface is up.\n", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800358 ret = -EPERM;
359 goto out;
360 }
361
Jay Vosburghece95f72008-01-17 16:25:01 -0800362 new_value = bond_parse_parm(buf, bond_mode_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800363 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000364 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800365 ": %s: Ignoring invalid mode value %.*s.\n",
366 bond->dev->name,
367 (int)strlen(buf) - 1, buf);
368 ret = -EINVAL;
369 goto out;
370 } else {
Jay Vosburgh8f903c72006-02-21 16:36:44 -0800371 if (bond->params.mode == BOND_MODE_8023AD)
372 bond_unset_master_3ad_flags(bond);
373
374 if (bond->params.mode == BOND_MODE_ALB)
375 bond_unset_master_alb_flags(bond);
376
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800377 bond->params.mode = new_value;
378 bond_set_mode_ops(bond, bond->params.mode);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000379 pr_info(DRV_NAME ": %s: setting mode to %s (%d).\n",
380 bond->dev->name, bond_mode_tbl[new_value].modename,
381 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800382 }
383out:
384 return ret;
385}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000386static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
387 bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800388
389/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000390 * Show and set the bonding transmit hash method.
391 * The bond interface must be down to change the xmit hash policy.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800392 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700393static ssize_t bonding_show_xmit_hash(struct device *d,
394 struct device_attribute *attr,
395 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800396{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700397 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800398
Wagner Ferenc8e4b9322007-12-06 23:40:32 -0800399 return sprintf(buf, "%s %d\n",
400 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
401 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800402}
403
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700404static ssize_t bonding_store_xmit_hash(struct device *d,
405 struct device_attribute *attr,
406 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800407{
408 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700409 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800410
411 if (bond->dev->flags & IFF_UP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000412 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800413 "%s: Interface is up. Unable to update xmit policy.\n",
414 bond->dev->name);
415 ret = -EPERM;
416 goto out;
417 }
418
Jay Vosburghece95f72008-01-17 16:25:01 -0800419 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800420 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000421 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800422 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
423 bond->dev->name,
424 (int)strlen(buf) - 1, buf);
425 ret = -EINVAL;
426 goto out;
427 } else {
428 bond->params.xmit_policy = new_value;
429 bond_set_mode_ops(bond, bond->params.mode);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000430 pr_info(DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
431 bond->dev->name,
432 xmit_hashtype_tbl[new_value].modename, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800433 }
434out:
435 return ret;
436}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000437static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
438 bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800439
440/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700441 * Show and set arp_validate.
442 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700443static ssize_t bonding_show_arp_validate(struct device *d,
444 struct device_attribute *attr,
445 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700446{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700447 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700448
449 return sprintf(buf, "%s %d\n",
450 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800451 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700452}
453
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700454static ssize_t bonding_store_arp_validate(struct device *d,
455 struct device_attribute *attr,
456 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700457{
458 int new_value;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700459 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700460
Jay Vosburghece95f72008-01-17 16:25:01 -0800461 new_value = bond_parse_parm(buf, arp_validate_tbl);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700462 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000463 pr_err(DRV_NAME
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700464 ": %s: Ignoring invalid arp_validate value %s\n",
465 bond->dev->name, buf);
466 return -EINVAL;
467 }
468 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000469 pr_err(DRV_NAME
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700470 ": %s: arp_validate only supported in active-backup mode.\n",
471 bond->dev->name);
472 return -EINVAL;
473 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000474 pr_info(DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700475 bond->dev->name, arp_validate_tbl[new_value].modename,
476 new_value);
477
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) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000514 pr_err(DRV_NAME
Jay Vosburghdd957c52007-10-09 19:57:24 -0700515 ": %s: Can't alter fail_over_mac with slaves in bond.\n",
516 bond->dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700517 return -EPERM;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700518 }
519
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700520 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
521 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000522 pr_err(DRV_NAME
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700523 ": %s: Ignoring invalid fail_over_mac value %s.\n",
524 bond->dev->name, buf);
525 return -EINVAL;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700526 }
527
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700528 bond->params.fail_over_mac = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000529 pr_info(DRV_NAME ": %s: Setting fail_over_mac to %s (%d).\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700530 bond->dev->name, fail_over_mac_tbl[new_value].modename,
531 new_value);
532
533 return count;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700534}
535
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000536static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
537 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700538
539/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800540 * Show and set the arp timer interval. There are two tricky bits
541 * here. First, if ARP monitoring is activated, then we must disable
542 * MII monitoring. Second, if the ARP timer isn't running, we must
543 * start it.
544 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700545static ssize_t bonding_show_arp_interval(struct device *d,
546 struct device_attribute *attr,
547 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800548{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700549 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800550
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800551 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800552}
553
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700554static ssize_t bonding_store_arp_interval(struct device *d,
555 struct device_attribute *attr,
556 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800557{
558 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700559 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800560
561 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000562 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800563 ": %s: no arp_interval value specified.\n",
564 bond->dev->name);
565 ret = -EINVAL;
566 goto out;
567 }
568 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000569 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800570 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
571 bond->dev->name, new_value, INT_MAX);
572 ret = -EINVAL;
573 goto out;
574 }
575
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000576 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800577 ": %s: Setting ARP monitoring interval to %d.\n",
578 bond->dev->name, new_value);
579 bond->params.arp_interval = new_value;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -0800580 if (bond->params.arp_interval)
581 bond->dev->priv_flags |= IFF_MASTER_ARPMON;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800582 if (bond->params.miimon) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000583 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800584 ": %s: ARP monitoring cannot be used with MII monitoring. "
585 "%s Disabling MII monitoring.\n",
586 bond->dev->name, bond->dev->name);
587 bond->params.miimon = 0;
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700588 if (delayed_work_pending(&bond->mii_work)) {
589 cancel_delayed_work(&bond->mii_work);
590 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800591 }
592 }
593 if (!bond->params.arp_targets[0]) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000594 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800595 ": %s: ARP monitoring has been set up, "
596 "but no ARP targets have been specified.\n",
597 bond->dev->name);
598 }
599 if (bond->dev->flags & IFF_UP) {
600 /* If the interface is up, we may need to fire off
601 * the ARP timer. If the interface is down, the
602 * timer will get fired off when the open function
603 * is called.
604 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700605 if (!delayed_work_pending(&bond->arp_work)) {
606 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
607 INIT_DELAYED_WORK(&bond->arp_work,
608 bond_activebackup_arp_mon);
609 else
610 INIT_DELAYED_WORK(&bond->arp_work,
611 bond_loadbalance_arp_mon);
612
613 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800614 }
615 }
616
617out:
618 return ret;
619}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000620static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
621 bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800622
623/*
624 * Show and set the arp targets.
625 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700626static ssize_t bonding_show_arp_targets(struct device *d,
627 struct device_attribute *attr,
628 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800629{
630 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700631 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800632
633 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
634 if (bond->params.arp_targets[i])
Harvey Harrison63779432008-10-31 00:56:00 -0700635 res += sprintf(buf + res, "%pI4 ",
636 &bond->params.arp_targets[i]);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800637 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800638 if (res)
639 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800640 return res;
641}
642
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700643static ssize_t bonding_store_arp_targets(struct device *d,
644 struct device_attribute *attr,
645 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800646{
Al Virod3bb52b2007-08-22 20:06:58 -0400647 __be32 newtarget;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800648 int i = 0, done = 0, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700649 struct bonding *bond = to_bond(d);
Al Virod3bb52b2007-08-22 20:06:58 -0400650 __be32 *targets;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800651
652 targets = bond->params.arp_targets;
653 newtarget = in_aton(buf + 1);
654 /* look for adds */
655 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400656 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000657 pr_err(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700658 ": %s: invalid ARP target %pI4 specified for addition\n",
659 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800660 ret = -EINVAL;
661 goto out;
662 }
663 /* look for an empty slot to put the target in, and check for dupes */
Brian Haley5a31bec2009-04-13 00:11:30 -0700664 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800665 if (targets[i] == newtarget) { /* duplicate */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000666 pr_err(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700667 ": %s: ARP target %pI4 is already present\n",
668 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800669 ret = -EINVAL;
670 goto out;
671 }
Brian Haley5a31bec2009-04-13 00:11:30 -0700672 if (targets[i] == 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000673 pr_info(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700674 ": %s: adding ARP target %pI4.\n",
675 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800676 done = 1;
677 targets[i] = newtarget;
678 }
679 }
680 if (!done) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000681 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800682 ": %s: ARP target table is full!\n",
683 bond->dev->name);
684 ret = -EINVAL;
685 goto out;
686 }
687
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000688 } else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400689 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000690 pr_err(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700691 ": %s: invalid ARP target %pI4 specified for removal\n",
692 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800693 ret = -EINVAL;
694 goto out;
695 }
696
Brian Haley5a31bec2009-04-13 00:11:30 -0700697 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800698 if (targets[i] == newtarget) {
Brian Haley5a31bec2009-04-13 00:11:30 -0700699 int j;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000700 pr_info(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700701 ": %s: removing ARP target %pI4.\n",
702 bond->dev->name, &newtarget);
Brian Haley5a31bec2009-04-13 00:11:30 -0700703 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
704 targets[j] = targets[j+1];
705
706 targets[j] = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800707 done = 1;
708 }
709 }
710 if (!done) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000711 pr_info(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700712 ": %s: unable to remove nonexistent ARP target %pI4.\n",
713 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800714 ret = -EINVAL;
715 goto out;
716 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000717 } else {
718 pr_err(DRV_NAME ": no command found in arp_ip_targets file"
719 " for bond %s. Use +<addr> or -<addr>.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800720 bond->dev->name);
721 ret = -EPERM;
722 goto out;
723 }
724
725out:
726 return ret;
727}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700728static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800729
730/*
731 * Show and set the up and down delays. These must be multiples of the
732 * MII monitoring value, and are stored internally as the multiplier.
733 * Thus, we must translate to MS for the real world.
734 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700735static ssize_t bonding_show_downdelay(struct device *d,
736 struct device_attribute *attr,
737 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800738{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700739 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800740
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800741 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800742}
743
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700744static ssize_t bonding_store_downdelay(struct device *d,
745 struct device_attribute *attr,
746 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800747{
748 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700749 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800750
751 if (!(bond->params.miimon)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000752 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800753 ": %s: Unable to set down delay as MII monitoring is disabled\n",
754 bond->dev->name);
755 ret = -EPERM;
756 goto out;
757 }
758
759 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000760 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800761 ": %s: no down delay value specified.\n",
762 bond->dev->name);
763 ret = -EINVAL;
764 goto out;
765 }
766 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000767 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800768 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
769 bond->dev->name, new_value, 1, INT_MAX);
770 ret = -EINVAL;
771 goto out;
772 } else {
773 if ((new_value % bond->params.miimon) != 0) {
774 printk(KERN_WARNING DRV_NAME
775 ": %s: Warning: down delay (%d) is not a multiple "
776 "of miimon (%d), delay rounded to %d ms\n",
777 bond->dev->name, new_value, bond->params.miimon,
778 (new_value / bond->params.miimon) *
779 bond->params.miimon);
780 }
781 bond->params.downdelay = new_value / bond->params.miimon;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000782 pr_info(DRV_NAME ": %s: Setting down delay to %d.\n",
783 bond->dev->name,
784 bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800785
786 }
787
788out:
789 return ret;
790}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000791static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
792 bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800793
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700794static ssize_t bonding_show_updelay(struct device *d,
795 struct device_attribute *attr,
796 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800797{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700798 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800799
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800800 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800801
802}
803
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700804static ssize_t bonding_store_updelay(struct device *d,
805 struct device_attribute *attr,
806 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800807{
808 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700809 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800810
811 if (!(bond->params.miimon)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000812 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800813 ": %s: Unable to set up delay as MII monitoring is disabled\n",
814 bond->dev->name);
815 ret = -EPERM;
816 goto out;
817 }
818
819 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000820 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800821 ": %s: no up delay value specified.\n",
822 bond->dev->name);
823 ret = -EINVAL;
824 goto out;
825 }
826 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000827 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800828 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
829 bond->dev->name, new_value, 1, INT_MAX);
830 ret = -EINVAL;
831 goto out;
832 } else {
833 if ((new_value % bond->params.miimon) != 0) {
834 printk(KERN_WARNING DRV_NAME
835 ": %s: Warning: up delay (%d) is not a multiple "
836 "of miimon (%d), updelay rounded to %d ms\n",
837 bond->dev->name, new_value, bond->params.miimon,
838 (new_value / bond->params.miimon) *
839 bond->params.miimon);
840 }
841 bond->params.updelay = new_value / bond->params.miimon;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000842 pr_info(DRV_NAME ": %s: Setting up delay to %d.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800843 bond->dev->name, bond->params.updelay * bond->params.miimon);
844
845 }
846
847out:
848 return ret;
849}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000850static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
851 bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800852
853/*
854 * Show and set the LACP interval. Interface must be down, and the mode
855 * must be set to 802.3ad mode.
856 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700857static ssize_t bonding_show_lacp(struct device *d,
858 struct device_attribute *attr,
859 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800860{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700861 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800862
863 return sprintf(buf, "%s %d\n",
864 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800865 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800866}
867
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700868static ssize_t bonding_store_lacp(struct device *d,
869 struct device_attribute *attr,
870 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800871{
872 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700873 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800874
875 if (bond->dev->flags & IFF_UP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000876 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800877 ": %s: Unable to update LACP rate because interface is up.\n",
878 bond->dev->name);
879 ret = -EPERM;
880 goto out;
881 }
882
883 if (bond->params.mode != BOND_MODE_8023AD) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000884 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800885 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
886 bond->dev->name);
887 ret = -EPERM;
888 goto out;
889 }
890
Jay Vosburghece95f72008-01-17 16:25:01 -0800891 new_value = bond_parse_parm(buf, bond_lacp_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800892
893 if ((new_value == 1) || (new_value == 0)) {
894 bond->params.lacp_fast = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000895 pr_info(DRV_NAME ": %s: Setting LACP rate to %s (%d).\n",
896 bond->dev->name, bond_lacp_tbl[new_value].modename,
897 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800898 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000899 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800900 ": %s: Ignoring invalid LACP rate value %.*s.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000901 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800902 ret = -EINVAL;
903 }
904out:
905 return ret;
906}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000907static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
908 bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800909
Jay Vosburghfd989c82008-11-04 17:51:16 -0800910static ssize_t bonding_show_ad_select(struct device *d,
911 struct device_attribute *attr,
912 char *buf)
913{
914 struct bonding *bond = to_bond(d);
915
916 return sprintf(buf, "%s %d\n",
917 ad_select_tbl[bond->params.ad_select].modename,
918 bond->params.ad_select);
919}
920
921
922static ssize_t bonding_store_ad_select(struct device *d,
923 struct device_attribute *attr,
924 const char *buf, size_t count)
925{
926 int new_value, ret = count;
927 struct bonding *bond = to_bond(d);
928
929 if (bond->dev->flags & IFF_UP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000930 pr_err(DRV_NAME
Jay Vosburghfd989c82008-11-04 17:51:16 -0800931 ": %s: Unable to update ad_select because interface "
932 "is up.\n", bond->dev->name);
933 ret = -EPERM;
934 goto out;
935 }
936
937 new_value = bond_parse_parm(buf, ad_select_tbl);
938
939 if (new_value != -1) {
940 bond->params.ad_select = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000941 pr_info(DRV_NAME
Jay Vosburghfd989c82008-11-04 17:51:16 -0800942 ": %s: Setting ad_select to %s (%d).\n",
943 bond->dev->name, ad_select_tbl[new_value].modename,
944 new_value);
945 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000946 pr_err(DRV_NAME
Jay Vosburghfd989c82008-11-04 17:51:16 -0800947 ": %s: Ignoring invalid ad_select value %.*s.\n",
948 bond->dev->name, (int)strlen(buf) - 1, buf);
949 ret = -EINVAL;
950 }
951out:
952 return ret;
953}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000954static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
955 bonding_show_ad_select, bonding_store_ad_select);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800956
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800957/*
Moni Shoua7893b242008-05-17 21:10:12 -0700958 * Show and set the number of grat ARP to send after a failover event.
959 */
960static ssize_t bonding_show_n_grat_arp(struct device *d,
961 struct device_attribute *attr,
962 char *buf)
963{
964 struct bonding *bond = to_bond(d);
965
966 return sprintf(buf, "%d\n", bond->params.num_grat_arp);
967}
968
969static ssize_t bonding_store_n_grat_arp(struct device *d,
970 struct device_attribute *attr,
971 const char *buf, size_t count)
972{
973 int new_value, ret = count;
974 struct bonding *bond = to_bond(d);
975
976 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000977 pr_err(DRV_NAME
Moni Shoua7893b242008-05-17 21:10:12 -0700978 ": %s: no num_grat_arp value specified.\n",
979 bond->dev->name);
980 ret = -EINVAL;
981 goto out;
982 }
983 if (new_value < 0 || new_value > 255) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000984 pr_err(DRV_NAME
Moni Shoua7893b242008-05-17 21:10:12 -0700985 ": %s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
986 bond->dev->name, new_value);
987 ret = -EINVAL;
988 goto out;
989 } else {
990 bond->params.num_grat_arp = new_value;
991 }
992out:
993 return ret;
994}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000995static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
996 bonding_show_n_grat_arp, bonding_store_n_grat_arp);
Brian Haley305d5522008-11-04 17:51:14 -0800997
998/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000999 * Show and set the number of unsolicited NA's to send after a failover event.
Brian Haley305d5522008-11-04 17:51:14 -08001000 */
1001static ssize_t bonding_show_n_unsol_na(struct device *d,
1002 struct device_attribute *attr,
1003 char *buf)
1004{
1005 struct bonding *bond = to_bond(d);
1006
1007 return sprintf(buf, "%d\n", bond->params.num_unsol_na);
1008}
1009
1010static ssize_t bonding_store_n_unsol_na(struct device *d,
1011 struct device_attribute *attr,
1012 const char *buf, size_t count)
1013{
1014 int new_value, ret = count;
1015 struct bonding *bond = to_bond(d);
1016
1017 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001018 pr_err(DRV_NAME
Brian Haley305d5522008-11-04 17:51:14 -08001019 ": %s: no num_unsol_na value specified.\n",
1020 bond->dev->name);
1021 ret = -EINVAL;
1022 goto out;
1023 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001024
Brian Haley305d5522008-11-04 17:51:14 -08001025 if (new_value < 0 || new_value > 255) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001026 pr_err(DRV_NAME
Brian Haley305d5522008-11-04 17:51:14 -08001027 ": %s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
1028 bond->dev->name, new_value);
1029 ret = -EINVAL;
1030 goto out;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001031 } else
Brian Haley305d5522008-11-04 17:51:14 -08001032 bond->params.num_unsol_na = new_value;
Brian Haley305d5522008-11-04 17:51:14 -08001033out:
1034 return ret;
1035}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001036static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
1037 bonding_show_n_unsol_na, bonding_store_n_unsol_na);
Brian Haley305d5522008-11-04 17:51:14 -08001038
Moni Shoua7893b242008-05-17 21:10:12 -07001039/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001040 * Show and set the MII monitor interval. There are two tricky bits
1041 * here. First, if MII monitoring is activated, then we must disable
1042 * ARP monitoring. Second, if the timer isn't running, we must
1043 * start it.
1044 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001045static ssize_t bonding_show_miimon(struct device *d,
1046 struct device_attribute *attr,
1047 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001048{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001049 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001050
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001051 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001052}
1053
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001054static ssize_t bonding_store_miimon(struct device *d,
1055 struct device_attribute *attr,
1056 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001057{
1058 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001059 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001060
1061 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001062 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001063 ": %s: no miimon value specified.\n",
1064 bond->dev->name);
1065 ret = -EINVAL;
1066 goto out;
1067 }
1068 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001069 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001070 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1071 bond->dev->name, new_value, 1, INT_MAX);
1072 ret = -EINVAL;
1073 goto out;
1074 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001075 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001076 ": %s: Setting MII monitoring interval to %d.\n",
1077 bond->dev->name, new_value);
1078 bond->params.miimon = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001079 if (bond->params.updelay)
1080 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001081 ": %s: Note: Updating updelay (to %d) "
1082 "since it is a multiple of the miimon value.\n",
1083 bond->dev->name,
1084 bond->params.updelay * bond->params.miimon);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001085 if (bond->params.downdelay)
1086 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001087 ": %s: Note: Updating downdelay (to %d) "
1088 "since it is a multiple of the miimon value.\n",
1089 bond->dev->name,
1090 bond->params.downdelay * bond->params.miimon);
1091 if (bond->params.arp_interval) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001092 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001093 ": %s: MII monitoring cannot be used with "
1094 "ARP monitoring. Disabling ARP monitoring...\n",
1095 bond->dev->name);
1096 bond->params.arp_interval = 0;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -08001097 bond->dev->priv_flags &= ~IFF_MASTER_ARPMON;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001098 if (bond->params.arp_validate) {
1099 bond_unregister_arp(bond);
1100 bond->params.arp_validate =
1101 BOND_ARP_VALIDATE_NONE;
1102 }
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001103 if (delayed_work_pending(&bond->arp_work)) {
1104 cancel_delayed_work(&bond->arp_work);
1105 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001106 }
1107 }
1108
1109 if (bond->dev->flags & IFF_UP) {
1110 /* If the interface is up, we may need to fire off
1111 * the MII timer. If the interface is down, the
1112 * timer will get fired off when the open function
1113 * is called.
1114 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001115 if (!delayed_work_pending(&bond->mii_work)) {
1116 INIT_DELAYED_WORK(&bond->mii_work,
1117 bond_mii_monitor);
1118 queue_delayed_work(bond->wq,
1119 &bond->mii_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001120 }
1121 }
1122 }
1123out:
1124 return ret;
1125}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001126static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1127 bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001128
1129/*
1130 * Show and set the primary slave. The store function is much
1131 * simpler than bonding_store_slaves function because it only needs to
1132 * handle one interface name.
1133 * The bond must be a mode that supports a primary for this be
1134 * set.
1135 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001136static ssize_t bonding_show_primary(struct device *d,
1137 struct device_attribute *attr,
1138 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001139{
1140 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001141 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001142
1143 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001144 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001145
1146 return count;
1147}
1148
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001149static ssize_t bonding_store_primary(struct device *d,
1150 struct device_attribute *attr,
1151 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001152{
1153 int i;
1154 struct slave *slave;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001155 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001156
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001157 if (!rtnl_trylock())
1158 return restart_syscall();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001159 read_lock(&bond->lock);
1160 write_lock_bh(&bond->curr_slave_lock);
1161
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001162 if (!USES_PRIMARY(bond->params.mode)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001163 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001164 ": %s: Unable to set primary slave; %s is in mode %d\n",
1165 bond->dev->name, bond->dev->name, bond->params.mode);
1166 } else {
1167 bond_for_each_slave(bond, slave, i) {
1168 if (strnicmp
1169 (slave->dev->name, buf,
1170 strlen(slave->dev->name)) == 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001171 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001172 ": %s: Setting %s as primary slave.\n",
1173 bond->dev->name, slave->dev->name);
1174 bond->primary_slave = slave;
1175 bond_select_active_slave(bond);
1176 goto out;
1177 }
1178 }
1179
1180 /* if we got here, then we didn't match the name of any slave */
1181
1182 if (strlen(buf) == 0 || buf[0] == '\n') {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001183 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001184 ": %s: Setting primary slave to None.\n",
1185 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001186 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001187 bond_select_active_slave(bond);
1188 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001189 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001190 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1191 bond->dev->name, (int)strlen(buf) - 1, buf);
1192 }
1193 }
1194out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001195 write_unlock_bh(&bond->curr_slave_lock);
1196 read_unlock(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001197 rtnl_unlock();
1198
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001199 return count;
1200}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001201static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1202 bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001203
1204/*
1205 * Show and set the use_carrier flag.
1206 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001207static ssize_t bonding_show_carrier(struct device *d,
1208 struct device_attribute *attr,
1209 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001210{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001211 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001212
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001213 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001214}
1215
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001216static ssize_t bonding_store_carrier(struct device *d,
1217 struct device_attribute *attr,
1218 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001219{
1220 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001221 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001222
1223
1224 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001225 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001226 ": %s: no use_carrier value specified.\n",
1227 bond->dev->name);
1228 ret = -EINVAL;
1229 goto out;
1230 }
1231 if ((new_value == 0) || (new_value == 1)) {
1232 bond->params.use_carrier = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001233 pr_info(DRV_NAME ": %s: Setting use_carrier to %d.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001234 bond->dev->name, new_value);
1235 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001236 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001237 ": %s: Ignoring invalid use_carrier value %d.\n",
1238 bond->dev->name, new_value);
1239 }
1240out:
1241 return count;
1242}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001243static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1244 bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001245
1246
1247/*
1248 * Show and set currently active_slave.
1249 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001250static ssize_t bonding_show_active_slave(struct device *d,
1251 struct device_attribute *attr,
1252 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001253{
1254 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001255 struct bonding *bond = to_bond(d);
Wagner Ferenc16cd0162007-12-06 23:40:29 -08001256 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001257
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001258 read_lock(&bond->curr_slave_lock);
1259 curr = bond->curr_active_slave;
1260 read_unlock(&bond->curr_slave_lock);
1261
1262 if (USES_PRIMARY(bond->params.mode) && curr)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001263 count = sprintf(buf, "%s\n", curr->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001264 return count;
1265}
1266
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001267static ssize_t bonding_store_active_slave(struct device *d,
1268 struct device_attribute *attr,
1269 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001270{
1271 int i;
1272 struct slave *slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001273 struct slave *old_active = NULL;
1274 struct slave *new_active = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001275 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001276
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001277 if (!rtnl_trylock())
1278 return restart_syscall();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001279 read_lock(&bond->lock);
1280 write_lock_bh(&bond->curr_slave_lock);
Jay Vosburgh1466a212007-11-06 13:33:28 -08001281
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001282 if (!USES_PRIMARY(bond->params.mode))
1283 pr_info(DRV_NAME ": %s: Unable to change active slave;"
1284 " %s is in mode %d\n",
1285 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 */
1295 printk(KERN_INFO DRV_NAME
1296 ": %s: %s is already the current active slave.\n",
1297 bond->dev->name, slave->dev->name);
1298 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)) {
1305 printk(KERN_INFO DRV_NAME
1306 ": %s: Setting %s as active slave.\n",
1307 bond->dev->name, slave->dev->name);
1308 bond_change_active_slave(bond, new_active);
1309 }
1310 else {
1311 printk(KERN_INFO DRV_NAME
1312 ": %s: Could not set %s as active slave; "
1313 "either %s is down or the link is down.\n",
1314 bond->dev->name, slave->dev->name,
1315 slave->dev->name);
1316 }
1317 goto out;
1318 }
1319 }
1320 }
1321
1322 /* if we got here, then we didn't match the name of any slave */
1323
1324 if (strlen(buf) == 0 || buf[0] == '\n') {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001325 pr_info(DRV_NAME
1326 ": %s: Setting active slave to None.\n",
1327 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001328 bond->primary_slave = NULL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001329 bond_select_active_slave(bond);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001330 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001331 pr_info(DRV_NAME ": %s: Unable to set %.*s"
1332 " as active slave as it is not a slave.\n",
1333 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001334 }
1335 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001336 out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001337 write_unlock_bh(&bond->curr_slave_lock);
1338 read_unlock(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001339 rtnl_unlock();
1340
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001341 return count;
1342
1343}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001344static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1345 bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001346
1347
1348/*
1349 * Show link status of the bond interface.
1350 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001351static ssize_t bonding_show_mii_status(struct device *d,
1352 struct device_attribute *attr,
1353 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001354{
1355 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001356 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001357
1358 read_lock(&bond->curr_slave_lock);
1359 curr = bond->curr_active_slave;
1360 read_unlock(&bond->curr_slave_lock);
1361
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001362 return sprintf(buf, "%s\n", curr ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001363}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001364static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001365
1366
1367/*
1368 * Show current 802.3ad aggregator ID.
1369 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001370static ssize_t bonding_show_ad_aggregator(struct device *d,
1371 struct device_attribute *attr,
1372 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001373{
1374 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001375 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001376
1377 if (bond->params.mode == BOND_MODE_8023AD) {
1378 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001379 count = sprintf(buf, "%d\n",
1380 (bond_3ad_get_active_agg_info(bond, &ad_info))
1381 ? 0 : ad_info.aggregator_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001382 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001383
1384 return count;
1385}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001386static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001387
1388
1389/*
1390 * Show number of active 802.3ad ports.
1391 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001392static ssize_t bonding_show_ad_num_ports(struct device *d,
1393 struct device_attribute *attr,
1394 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001395{
1396 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001397 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001398
1399 if (bond->params.mode == BOND_MODE_8023AD) {
1400 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001401 count = sprintf(buf, "%d\n",
1402 (bond_3ad_get_active_agg_info(bond, &ad_info))
1403 ? 0 : ad_info.ports);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001404 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001405
1406 return count;
1407}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001408static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001409
1410
1411/*
1412 * Show current 802.3ad actor key.
1413 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001414static ssize_t bonding_show_ad_actor_key(struct device *d,
1415 struct device_attribute *attr,
1416 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001417{
1418 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001419 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001420
1421 if (bond->params.mode == BOND_MODE_8023AD) {
1422 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001423 count = sprintf(buf, "%d\n",
1424 (bond_3ad_get_active_agg_info(bond, &ad_info))
1425 ? 0 : ad_info.actor_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001426 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001427
1428 return count;
1429}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001430static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001431
1432
1433/*
1434 * Show current 802.3ad partner key.
1435 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001436static ssize_t bonding_show_ad_partner_key(struct device *d,
1437 struct device_attribute *attr,
1438 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001439{
1440 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001441 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001442
1443 if (bond->params.mode == BOND_MODE_8023AD) {
1444 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001445 count = sprintf(buf, "%d\n",
1446 (bond_3ad_get_active_agg_info(bond, &ad_info))
1447 ? 0 : ad_info.partner_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001448 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001449
1450 return count;
1451}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001452static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001453
1454
1455/*
1456 * Show current 802.3ad partner mac.
1457 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001458static ssize_t bonding_show_ad_partner_mac(struct device *d,
1459 struct device_attribute *attr,
1460 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001461{
1462 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001463 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001464
1465 if (bond->params.mode == BOND_MODE_8023AD) {
1466 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001467 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
Johannes Berge1749612008-10-27 15:59:26 -07001468 count = sprintf(buf, "%pM\n", ad_info.partner_system);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001469 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001470
1471 return count;
1472}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001473static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001474
1475
1476
1477static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001478 &dev_attr_slaves.attr,
1479 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001480 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001481 &dev_attr_arp_validate.attr,
1482 &dev_attr_arp_interval.attr,
1483 &dev_attr_arp_ip_target.attr,
1484 &dev_attr_downdelay.attr,
1485 &dev_attr_updelay.attr,
1486 &dev_attr_lacp_rate.attr,
Jay Vosburghfd989c82008-11-04 17:51:16 -08001487 &dev_attr_ad_select.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001488 &dev_attr_xmit_hash_policy.attr,
Moni Shoua7893b242008-05-17 21:10:12 -07001489 &dev_attr_num_grat_arp.attr,
Brian Haley305d5522008-11-04 17:51:14 -08001490 &dev_attr_num_unsol_na.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001491 &dev_attr_miimon.attr,
1492 &dev_attr_primary.attr,
1493 &dev_attr_use_carrier.attr,
1494 &dev_attr_active_slave.attr,
1495 &dev_attr_mii_status.attr,
1496 &dev_attr_ad_aggregator.attr,
1497 &dev_attr_ad_num_ports.attr,
1498 &dev_attr_ad_actor_key.attr,
1499 &dev_attr_ad_partner_key.attr,
1500 &dev_attr_ad_partner_mac.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001501 NULL,
1502};
1503
1504static struct attribute_group bonding_group = {
1505 .name = "bonding",
1506 .attrs = per_bond_attrs,
1507};
1508
1509/*
1510 * Initialize sysfs. This sets up the bonding_masters file in
1511 * /sys/class/net.
1512 */
1513int bond_create_sysfs(void)
1514{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001515 int ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001516
Jay Vosburghb8a97872008-06-13 18:12:04 -07001517 ret = netdev_class_create_file(&class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001518 /*
1519 * Permit multiple loads of the module by ignoring failures to
1520 * create the bonding_masters sysfs file. Bonding devices
1521 * created by second or subsequent loads of the module will
1522 * not be listed in, or controllable by, bonding_masters, but
1523 * will have the usual "bonding" sysfs directory.
1524 *
1525 * This is done to preserve backwards compatibility for
1526 * initscripts/sysconfig, which load bonding multiple times to
1527 * configure multiple bonding devices.
1528 */
1529 if (ret == -EEXIST) {
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001530 /* Is someone being kinky and naming a device bonding_master? */
1531 if (__dev_get_by_name(&init_net,
1532 class_attr_bonding_masters.attr.name))
1533 printk(KERN_ERR
1534 "network device named %s already exists in sysfs",
1535 class_attr_bonding_masters.attr.name);
Stephen Hemminger130aa612009-06-11 05:46:04 -07001536 ret = 0;
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001537 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001538
1539 return ret;
1540
1541}
1542
1543/*
1544 * Remove /sys/class/net/bonding_masters.
1545 */
1546void bond_destroy_sysfs(void)
1547{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001548 netdev_class_remove_file(&class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001549}
1550
1551/*
1552 * Initialize sysfs for each bond. This sets up and registers
1553 * the 'bondctl' directory for each individual bond under /sys/class/net.
1554 */
1555int bond_create_sysfs_entry(struct bonding *bond)
1556{
1557 struct net_device *dev = bond->dev;
1558 int err;
1559
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001560 err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001561 if (err)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001562 printk(KERN_EMERG "eek! didn't create group!\n");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001563
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001564 return err;
1565}
1566/*
1567 * Remove sysfs entries for each bond.
1568 */
1569void bond_destroy_sysfs_entry(struct bonding *bond)
1570{
1571 struct net_device *dev = bond->dev;
1572
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001573 sysfs_remove_group(&(dev->dev.kobj), &bonding_group);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001574}
1575