blob: 55bf34f59bbf60fe673bd4d4671aa53a03302157 [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>
Stephen Hemminger5c5129b2009-06-12 19:02:51 +000036#include <linux/etherdevice.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070037#include <net/net_namespace.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080038
Mitch Williamsb76cdba2005-11-09 10:36:41 -080039#include "bonding.h"
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -080040
Stephen Hemminger3d632c32009-06-12 19:02:48 +000041#define to_dev(obj) container_of(obj, struct device, kobj)
Wang Chen454d7c92008-11-12 23:37:49 -080042#define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
Mitch Williamsb76cdba2005-11-09 10:36:41 -080043
Mitch Williamsb76cdba2005-11-09 10:36:41 -080044/*
45 * "show" function for the bond_masters attribute.
46 * The class parameter is ignored.
47 */
Wagner Ferencb8843662007-12-06 23:40:30 -080048static ssize_t bonding_show_bonds(struct class *cls, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080049{
50 int res = 0;
51 struct bonding *bond;
52
Stephen Hemminger7e083842009-06-12 19:02:46 +000053 rtnl_lock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080054
55 list_for_each_entry(bond, &bond_dev_list, bond_list) {
56 if (res > (PAGE_SIZE - IFNAMSIZ)) {
57 /* not enough space for another interface name */
58 if ((PAGE_SIZE - res) > 10)
59 res = PAGE_SIZE - 10;
Wagner Ferencb8843662007-12-06 23:40:30 -080060 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -080061 break;
62 }
Wagner Ferencb8843662007-12-06 23:40:30 -080063 res += sprintf(buf + res, "%s ", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080064 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -080065 if (res)
66 buf[res-1] = '\n'; /* eat the leftover space */
Stephen Hemminger7e083842009-06-12 19:02:46 +000067
68 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080069 return res;
70}
71
Stephen Hemminger373500d2009-06-12 19:02:50 +000072static struct net_device *bond_get_by_name(const char *ifname)
73{
74 struct bonding *bond;
75
76 list_for_each_entry(bond, &bond_dev_list, bond_list) {
77 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
78 return bond->dev;
79 }
80 return NULL;
81}
82
Mitch Williamsb76cdba2005-11-09 10:36:41 -080083/*
84 * "store" function for the bond_masters attribute. This is what
85 * creates and deletes entire bonds.
86 *
87 * The class parameter is ignored.
88 *
89 */
90
Stephen Hemminger3d632c32009-06-12 19:02:48 +000091static ssize_t bonding_store_bonds(struct class *cls,
92 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080093{
94 char command[IFNAMSIZ + 1] = {0, };
95 char *ifname;
Jay Vosburgh027ea042008-01-17 16:25:02 -080096 int rv, res = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -080097
Mitch Williamsb76cdba2005-11-09 10:36:41 -080098 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
99 ifname = command + 1;
100 if ((strlen(command) <= 1) ||
101 !dev_valid_name(ifname))
102 goto err_no_cmd;
103
104 if (command[0] == '+') {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000105 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800106 ": %s is being created...\n", ifname);
Stephen Hemmingerd2991f72009-06-12 19:02:44 +0000107 rv = bond_create(ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800108 if (rv) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000109 pr_info(DRV_NAME ": Bond creation failed.\n");
Jay Vosburgh027ea042008-01-17 16:25:02 -0800110 res = rv;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800111 }
Stephen Hemminger373500d2009-06-12 19:02:50 +0000112 } else if (command[0] == '-') {
113 struct net_device *bond_dev;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800114
Jay Vosburgh027ea042008-01-17 16:25:02 -0800115 rtnl_lock();
Stephen Hemminger373500d2009-06-12 19:02:50 +0000116 bond_dev = bond_get_by_name(ifname);
117 if (bond_dev) {
118 pr_info(DRV_NAME ": %s is being deleted...\n",
119 ifname);
120 unregister_netdevice(bond_dev);
121 } else {
122 pr_err(DRV_NAME ": unable to delete non-existent %s\n",
123 ifname);
124 res = -ENODEV;
125 }
126 rtnl_unlock();
127 } else
128 goto err_no_cmd;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800129
Stephen Hemminger373500d2009-06-12 19:02:50 +0000130 /* Always return either count or an error. If you return 0, you'll
131 * get called forever, which is bad.
132 */
133 return res;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800134
135err_no_cmd:
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000136 pr_err(DRV_NAME ": no command found in bonding_masters."
137 " Use +ifname or -ifname.\n");
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700138 return -EPERM;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800139}
Stephen Hemminger373500d2009-06-12 19:02:50 +0000140
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800141/* class attribute for bond_masters file. This ends up in /sys/class/net */
142static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
143 bonding_show_bonds, bonding_store_bonds);
144
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000145int bond_create_slave_symlinks(struct net_device *master,
146 struct net_device *slave)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800147{
148 char linkname[IFNAMSIZ+7];
149 int ret = 0;
150
151 /* first, create a link from the slave back to the master */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700152 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800153 "master");
154 if (ret)
155 return ret;
156 /* next, create a link from the master to the slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000157 sprintf(linkname, "slave_%s", slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700158 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800159 linkname);
160 return ret;
161
162}
163
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000164void bond_destroy_slave_symlinks(struct net_device *master,
165 struct net_device *slave)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800166{
167 char linkname[IFNAMSIZ+7];
168
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700169 sysfs_remove_link(&(slave->dev.kobj), "master");
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000170 sprintf(linkname, "slave_%s", slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700171 sysfs_remove_link(&(master->dev.kobj), linkname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800172}
173
174
175/*
176 * Show the slaves in the current bond.
177 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700178static ssize_t bonding_show_slaves(struct device *d,
179 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800180{
181 struct slave *slave;
182 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700183 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800184
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700185 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800186 bond_for_each_slave(bond, slave, i) {
187 if (res > (PAGE_SIZE - IFNAMSIZ)) {
188 /* not enough space for another interface name */
189 if ((PAGE_SIZE - res) > 10)
190 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800191 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800192 break;
193 }
194 res += sprintf(buf + res, "%s ", slave->dev->name);
195 }
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700196 read_unlock(&bond->lock);
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800197 if (res)
198 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800199 return res;
200}
201
202/*
203 * Set the slaves in the current bond. The bond interface must be
204 * up for this to succeed.
205 * This function is largely the same flow as bonding_update_bonds().
206 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700207static ssize_t bonding_store_slaves(struct device *d,
208 struct device_attribute *attr,
209 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800210{
211 char command[IFNAMSIZ + 1] = { 0, };
212 char *ifname;
213 int i, res, found, ret = count;
Moni Shoua3158bf72007-10-09 19:43:41 -0700214 u32 original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800215 struct slave *slave;
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -0800216 struct net_device *dev = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700217 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800218
219 /* Quick sanity check -- is the bond interface up? */
220 if (!(bond->dev->flags & IFF_UP)) {
Moni Shoua6b1bf092007-10-09 19:43:40 -0700221 printk(KERN_WARNING DRV_NAME
222 ": %s: doing slave updates when interface is down.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800223 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800224 }
225
226 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
227
Eric W. Biederman496a60c2009-05-13 17:02:50 +0000228 if (!rtnl_trylock())
229 return restart_syscall();
Jay Vosburgh027ea042008-01-17 16:25:02 -0800230
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800231 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
232 ifname = command + 1;
233 if ((strlen(command) <= 1) ||
234 !dev_valid_name(ifname))
235 goto err_no_cmd;
236
237 if (command[0] == '+') {
238
239 /* Got a slave name in ifname. Is it already in the list? */
240 found = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800241
Stephen Hemminger373500d2009-06-12 19:02:50 +0000242 /* FIXME: get netns from sysfs object */
243 dev = __dev_get_by_name(&init_net, ifname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800244 if (!dev) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000245 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800246 ": %s: Interface %s does not exist!\n",
247 bond->dev->name, ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000248 ret = -ENODEV;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800249 goto out;
Stephen Hemminger373500d2009-06-12 19:02:50 +0000250 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800251
252 if (dev->flags & IFF_UP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000253 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800254 ": %s: Error: Unable to enslave %s "
255 "because it is already up.\n",
256 bond->dev->name, dev->name);
257 ret = -EPERM;
258 goto out;
259 }
Stephen Hemminger373500d2009-06-12 19:02:50 +0000260
261 read_lock(&bond->lock);
262 bond_for_each_slave(bond, slave, i)
263 if (slave->dev == dev) {
264 pr_err(DRV_NAME
265 ": %s: Interface %s is already enslaved!\n",
266 bond->dev->name, ifname);
267 ret = -EPERM;
268 read_unlock(&bond->lock);
269 goto out;
270 }
271 read_unlock(&bond->lock);
272
273 pr_info(DRV_NAME ": %s: Adding slave %s.\n",
274 bond->dev->name, ifname);
275
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800276 /* If this is the first slave, then we need to set
277 the master's hardware address to be the same as the
278 slave's. */
Stephen Hemminger5c5129b2009-06-12 19:02:51 +0000279 if (is_zero_ether_addr(bond->dev->dev_addr))
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800280 memcpy(bond->dev->dev_addr, dev->dev_addr,
281 dev->addr_len);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800282
283 /* Set the slave's MTU to match the bond */
Moni Shoua3158bf72007-10-09 19:43:41 -0700284 original_mtu = dev->mtu;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800285 res = dev_set_mtu(dev, bond->dev->mtu);
286 if (res) {
287 ret = res;
288 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800289 }
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800290
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800291 res = bond_enslave(bond->dev, dev);
Moni Shoua3158bf72007-10-09 19:43:41 -0700292 bond_for_each_slave(bond, slave, i)
293 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
294 slave->original_mtu = original_mtu;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000295 if (res)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800296 ret = res;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000297
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800298 goto out;
299 }
300
301 if (command[0] == '-') {
302 dev = NULL;
David S. Miller6952d8922008-03-28 16:15:38 -0700303 original_mtu = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800304 bond_for_each_slave(bond, slave, i)
305 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
306 dev = slave->dev;
Moni Shoua3158bf72007-10-09 19:43:41 -0700307 original_mtu = slave->original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800308 break;
309 }
310 if (dev) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000311 pr_info(DRV_NAME ": %s: Removing slave %s\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800312 bond->dev->name, dev->name);
Moni Shouad90a1622007-10-09 19:43:43 -0700313 res = bond_release(bond->dev, dev);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800314 if (res) {
315 ret = res;
316 goto out;
317 }
318 /* set the slave MTU to the default */
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800319 dev_set_mtu(dev, original_mtu);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000320 } else {
321 pr_err(DRV_NAME ": unable to remove non-existent"
322 " slave %s for bond %s.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800323 ifname, bond->dev->name);
324 ret = -ENODEV;
325 }
326 goto out;
327 }
328
329err_no_cmd:
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000330 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 -0800331 ret = -EPERM;
332
333out:
Jay Vosburgh027ea042008-01-17 16:25:02 -0800334 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800335 return ret;
336}
337
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000338static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
339 bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800340
341/*
342 * Show and set the bonding mode. The bond interface must be down to
343 * change the mode.
344 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700345static ssize_t bonding_show_mode(struct device *d,
346 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800347{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700348 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800349
350 return sprintf(buf, "%s %d\n",
351 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800352 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800353}
354
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700355static ssize_t bonding_store_mode(struct device *d,
356 struct device_attribute *attr,
357 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800358{
359 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700360 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800361
362 if (bond->dev->flags & IFF_UP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000363 pr_err(DRV_NAME ": unable to update mode of %s"
364 " because interface is up.\n", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800365 ret = -EPERM;
366 goto out;
367 }
368
Jay Vosburghece95f72008-01-17 16:25:01 -0800369 new_value = bond_parse_parm(buf, bond_mode_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800370 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000371 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800372 ": %s: Ignoring invalid mode value %.*s.\n",
373 bond->dev->name,
374 (int)strlen(buf) - 1, buf);
375 ret = -EINVAL;
376 goto out;
377 } else {
Jay Vosburgh8f903c72006-02-21 16:36:44 -0800378 if (bond->params.mode == BOND_MODE_8023AD)
379 bond_unset_master_3ad_flags(bond);
380
381 if (bond->params.mode == BOND_MODE_ALB)
382 bond_unset_master_alb_flags(bond);
383
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800384 bond->params.mode = new_value;
385 bond_set_mode_ops(bond, bond->params.mode);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000386 pr_info(DRV_NAME ": %s: setting mode to %s (%d).\n",
387 bond->dev->name, bond_mode_tbl[new_value].modename,
388 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800389 }
390out:
391 return ret;
392}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000393static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
394 bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800395
396/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000397 * Show and set the bonding transmit hash method.
398 * The bond interface must be down to change the xmit hash policy.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800399 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700400static ssize_t bonding_show_xmit_hash(struct device *d,
401 struct device_attribute *attr,
402 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800403{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700404 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800405
Wagner Ferenc8e4b9322007-12-06 23:40:32 -0800406 return sprintf(buf, "%s %d\n",
407 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
408 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800409}
410
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700411static ssize_t bonding_store_xmit_hash(struct device *d,
412 struct device_attribute *attr,
413 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800414{
415 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700416 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800417
418 if (bond->dev->flags & IFF_UP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000419 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800420 "%s: Interface is up. Unable to update xmit policy.\n",
421 bond->dev->name);
422 ret = -EPERM;
423 goto out;
424 }
425
Jay Vosburghece95f72008-01-17 16:25:01 -0800426 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800427 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000428 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800429 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
430 bond->dev->name,
431 (int)strlen(buf) - 1, buf);
432 ret = -EINVAL;
433 goto out;
434 } else {
435 bond->params.xmit_policy = new_value;
436 bond_set_mode_ops(bond, bond->params.mode);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000437 pr_info(DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
438 bond->dev->name,
439 xmit_hashtype_tbl[new_value].modename, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800440 }
441out:
442 return ret;
443}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000444static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
445 bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800446
447/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700448 * Show and set arp_validate.
449 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700450static ssize_t bonding_show_arp_validate(struct device *d,
451 struct device_attribute *attr,
452 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700453{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700454 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700455
456 return sprintf(buf, "%s %d\n",
457 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800458 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700459}
460
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700461static ssize_t bonding_store_arp_validate(struct device *d,
462 struct device_attribute *attr,
463 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700464{
465 int new_value;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700466 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700467
Jay Vosburghece95f72008-01-17 16:25:01 -0800468 new_value = bond_parse_parm(buf, arp_validate_tbl);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700469 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000470 pr_err(DRV_NAME
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700471 ": %s: Ignoring invalid arp_validate value %s\n",
472 bond->dev->name, buf);
473 return -EINVAL;
474 }
475 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000476 pr_err(DRV_NAME
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700477 ": %s: arp_validate only supported in active-backup mode.\n",
478 bond->dev->name);
479 return -EINVAL;
480 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000481 pr_info(DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700482 bond->dev->name, arp_validate_tbl[new_value].modename,
483 new_value);
484
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000485 if (!bond->params.arp_validate && new_value)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700486 bond_register_arp(bond);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000487 else if (bond->params.arp_validate && !new_value)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700488 bond_unregister_arp(bond);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700489
490 bond->params.arp_validate = new_value;
491
492 return count;
493}
494
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000495static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
496 bonding_store_arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700497
498/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700499 * Show and store fail_over_mac. User only allowed to change the
500 * value when there are no slaves.
501 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000502static ssize_t bonding_show_fail_over_mac(struct device *d,
503 struct device_attribute *attr,
504 char *buf)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700505{
506 struct bonding *bond = to_bond(d);
507
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700508 return sprintf(buf, "%s %d\n",
509 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
510 bond->params.fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700511}
512
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000513static ssize_t bonding_store_fail_over_mac(struct device *d,
514 struct device_attribute *attr,
515 const char *buf, size_t count)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700516{
517 int new_value;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700518 struct bonding *bond = to_bond(d);
519
520 if (bond->slave_cnt != 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000521 pr_err(DRV_NAME
Jay Vosburghdd957c52007-10-09 19:57:24 -0700522 ": %s: Can't alter fail_over_mac with slaves in bond.\n",
523 bond->dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700524 return -EPERM;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700525 }
526
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700527 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
528 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000529 pr_err(DRV_NAME
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700530 ": %s: Ignoring invalid fail_over_mac value %s.\n",
531 bond->dev->name, buf);
532 return -EINVAL;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700533 }
534
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700535 bond->params.fail_over_mac = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000536 pr_info(DRV_NAME ": %s: Setting fail_over_mac to %s (%d).\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700537 bond->dev->name, fail_over_mac_tbl[new_value].modename,
538 new_value);
539
540 return count;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700541}
542
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000543static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
544 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700545
546/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800547 * Show and set the arp timer interval. There are two tricky bits
548 * here. First, if ARP monitoring is activated, then we must disable
549 * MII monitoring. Second, if the ARP timer isn't running, we must
550 * start it.
551 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700552static ssize_t bonding_show_arp_interval(struct device *d,
553 struct device_attribute *attr,
554 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800555{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700556 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800557
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800558 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800559}
560
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700561static ssize_t bonding_store_arp_interval(struct device *d,
562 struct device_attribute *attr,
563 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800564{
565 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700566 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800567
568 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000569 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800570 ": %s: no arp_interval value specified.\n",
571 bond->dev->name);
572 ret = -EINVAL;
573 goto out;
574 }
575 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000576 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800577 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
578 bond->dev->name, new_value, INT_MAX);
579 ret = -EINVAL;
580 goto out;
581 }
582
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000583 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800584 ": %s: Setting ARP monitoring interval to %d.\n",
585 bond->dev->name, new_value);
586 bond->params.arp_interval = new_value;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -0800587 if (bond->params.arp_interval)
588 bond->dev->priv_flags |= IFF_MASTER_ARPMON;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800589 if (bond->params.miimon) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000590 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800591 ": %s: ARP monitoring cannot be used with MII monitoring. "
592 "%s Disabling MII monitoring.\n",
593 bond->dev->name, bond->dev->name);
594 bond->params.miimon = 0;
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700595 if (delayed_work_pending(&bond->mii_work)) {
596 cancel_delayed_work(&bond->mii_work);
597 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800598 }
599 }
600 if (!bond->params.arp_targets[0]) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000601 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800602 ": %s: ARP monitoring has been set up, "
603 "but no ARP targets have been specified.\n",
604 bond->dev->name);
605 }
606 if (bond->dev->flags & IFF_UP) {
607 /* If the interface is up, we may need to fire off
608 * the ARP timer. If the interface is down, the
609 * timer will get fired off when the open function
610 * is called.
611 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700612 if (!delayed_work_pending(&bond->arp_work)) {
613 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
614 INIT_DELAYED_WORK(&bond->arp_work,
615 bond_activebackup_arp_mon);
616 else
617 INIT_DELAYED_WORK(&bond->arp_work,
618 bond_loadbalance_arp_mon);
619
620 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800621 }
622 }
623
624out:
625 return ret;
626}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000627static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
628 bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800629
630/*
631 * Show and set the arp targets.
632 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700633static ssize_t bonding_show_arp_targets(struct device *d,
634 struct device_attribute *attr,
635 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800636{
637 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700638 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800639
640 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
641 if (bond->params.arp_targets[i])
Harvey Harrison63779432008-10-31 00:56:00 -0700642 res += sprintf(buf + res, "%pI4 ",
643 &bond->params.arp_targets[i]);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800644 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800645 if (res)
646 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800647 return res;
648}
649
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700650static ssize_t bonding_store_arp_targets(struct device *d,
651 struct device_attribute *attr,
652 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800653{
Al Virod3bb52b2007-08-22 20:06:58 -0400654 __be32 newtarget;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800655 int i = 0, done = 0, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700656 struct bonding *bond = to_bond(d);
Al Virod3bb52b2007-08-22 20:06:58 -0400657 __be32 *targets;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800658
659 targets = bond->params.arp_targets;
660 newtarget = in_aton(buf + 1);
661 /* look for adds */
662 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400663 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000664 pr_err(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700665 ": %s: invalid ARP target %pI4 specified for addition\n",
666 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800667 ret = -EINVAL;
668 goto out;
669 }
670 /* look for an empty slot to put the target in, and check for dupes */
Brian Haley5a31bec2009-04-13 00:11:30 -0700671 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800672 if (targets[i] == newtarget) { /* duplicate */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000673 pr_err(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700674 ": %s: ARP target %pI4 is already present\n",
675 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800676 ret = -EINVAL;
677 goto out;
678 }
Brian Haley5a31bec2009-04-13 00:11:30 -0700679 if (targets[i] == 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000680 pr_info(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700681 ": %s: adding ARP target %pI4.\n",
682 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800683 done = 1;
684 targets[i] = newtarget;
685 }
686 }
687 if (!done) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000688 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800689 ": %s: ARP target table is full!\n",
690 bond->dev->name);
691 ret = -EINVAL;
692 goto out;
693 }
694
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000695 } else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400696 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000697 pr_err(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700698 ": %s: invalid ARP target %pI4 specified for removal\n",
699 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800700 ret = -EINVAL;
701 goto out;
702 }
703
Brian Haley5a31bec2009-04-13 00:11:30 -0700704 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800705 if (targets[i] == newtarget) {
Brian Haley5a31bec2009-04-13 00:11:30 -0700706 int j;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000707 pr_info(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700708 ": %s: removing ARP target %pI4.\n",
709 bond->dev->name, &newtarget);
Brian Haley5a31bec2009-04-13 00:11:30 -0700710 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
711 targets[j] = targets[j+1];
712
713 targets[j] = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800714 done = 1;
715 }
716 }
717 if (!done) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000718 pr_info(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700719 ": %s: unable to remove nonexistent ARP target %pI4.\n",
720 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800721 ret = -EINVAL;
722 goto out;
723 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000724 } else {
725 pr_err(DRV_NAME ": no command found in arp_ip_targets file"
726 " for bond %s. Use +<addr> or -<addr>.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800727 bond->dev->name);
728 ret = -EPERM;
729 goto out;
730 }
731
732out:
733 return ret;
734}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700735static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800736
737/*
738 * Show and set the up and down delays. These must be multiples of the
739 * MII monitoring value, and are stored internally as the multiplier.
740 * Thus, we must translate to MS for the real world.
741 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700742static ssize_t bonding_show_downdelay(struct device *d,
743 struct device_attribute *attr,
744 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800745{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700746 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800747
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800748 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800749}
750
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700751static ssize_t bonding_store_downdelay(struct device *d,
752 struct device_attribute *attr,
753 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800754{
755 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700756 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800757
758 if (!(bond->params.miimon)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000759 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800760 ": %s: Unable to set down delay as MII monitoring is disabled\n",
761 bond->dev->name);
762 ret = -EPERM;
763 goto out;
764 }
765
766 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000767 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800768 ": %s: no down delay value specified.\n",
769 bond->dev->name);
770 ret = -EINVAL;
771 goto out;
772 }
773 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000774 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800775 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
776 bond->dev->name, new_value, 1, INT_MAX);
777 ret = -EINVAL;
778 goto out;
779 } else {
780 if ((new_value % bond->params.miimon) != 0) {
781 printk(KERN_WARNING DRV_NAME
782 ": %s: Warning: down delay (%d) is not a multiple "
783 "of miimon (%d), delay rounded to %d ms\n",
784 bond->dev->name, new_value, bond->params.miimon,
785 (new_value / bond->params.miimon) *
786 bond->params.miimon);
787 }
788 bond->params.downdelay = new_value / bond->params.miimon;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000789 pr_info(DRV_NAME ": %s: Setting down delay to %d.\n",
790 bond->dev->name,
791 bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800792
793 }
794
795out:
796 return ret;
797}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000798static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
799 bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800800
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700801static ssize_t bonding_show_updelay(struct device *d,
802 struct device_attribute *attr,
803 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800804{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700805 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800806
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800807 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800808
809}
810
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700811static ssize_t bonding_store_updelay(struct device *d,
812 struct device_attribute *attr,
813 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800814{
815 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700816 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800817
818 if (!(bond->params.miimon)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000819 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800820 ": %s: Unable to set up delay as MII monitoring is disabled\n",
821 bond->dev->name);
822 ret = -EPERM;
823 goto out;
824 }
825
826 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000827 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800828 ": %s: no up delay value specified.\n",
829 bond->dev->name);
830 ret = -EINVAL;
831 goto out;
832 }
833 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000834 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800835 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
836 bond->dev->name, new_value, 1, INT_MAX);
837 ret = -EINVAL;
838 goto out;
839 } else {
840 if ((new_value % bond->params.miimon) != 0) {
841 printk(KERN_WARNING DRV_NAME
842 ": %s: Warning: up delay (%d) is not a multiple "
843 "of miimon (%d), updelay rounded to %d ms\n",
844 bond->dev->name, new_value, bond->params.miimon,
845 (new_value / bond->params.miimon) *
846 bond->params.miimon);
847 }
848 bond->params.updelay = new_value / bond->params.miimon;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000849 pr_info(DRV_NAME ": %s: Setting up delay to %d.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800850 bond->dev->name, bond->params.updelay * bond->params.miimon);
851
852 }
853
854out:
855 return ret;
856}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000857static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
858 bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800859
860/*
861 * Show and set the LACP interval. Interface must be down, and the mode
862 * must be set to 802.3ad mode.
863 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700864static ssize_t bonding_show_lacp(struct device *d,
865 struct device_attribute *attr,
866 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800867{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700868 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800869
870 return sprintf(buf, "%s %d\n",
871 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800872 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800873}
874
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700875static ssize_t bonding_store_lacp(struct device *d,
876 struct device_attribute *attr,
877 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800878{
879 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700880 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800881
882 if (bond->dev->flags & IFF_UP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000883 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800884 ": %s: Unable to update LACP rate because interface is up.\n",
885 bond->dev->name);
886 ret = -EPERM;
887 goto out;
888 }
889
890 if (bond->params.mode != BOND_MODE_8023AD) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000891 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800892 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
893 bond->dev->name);
894 ret = -EPERM;
895 goto out;
896 }
897
Jay Vosburghece95f72008-01-17 16:25:01 -0800898 new_value = bond_parse_parm(buf, bond_lacp_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800899
900 if ((new_value == 1) || (new_value == 0)) {
901 bond->params.lacp_fast = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000902 pr_info(DRV_NAME ": %s: Setting LACP rate to %s (%d).\n",
903 bond->dev->name, bond_lacp_tbl[new_value].modename,
904 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800905 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000906 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800907 ": %s: Ignoring invalid LACP rate value %.*s.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000908 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800909 ret = -EINVAL;
910 }
911out:
912 return ret;
913}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000914static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
915 bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800916
Jay Vosburghfd989c82008-11-04 17:51:16 -0800917static ssize_t bonding_show_ad_select(struct device *d,
918 struct device_attribute *attr,
919 char *buf)
920{
921 struct bonding *bond = to_bond(d);
922
923 return sprintf(buf, "%s %d\n",
924 ad_select_tbl[bond->params.ad_select].modename,
925 bond->params.ad_select);
926}
927
928
929static ssize_t bonding_store_ad_select(struct device *d,
930 struct device_attribute *attr,
931 const char *buf, size_t count)
932{
933 int new_value, ret = count;
934 struct bonding *bond = to_bond(d);
935
936 if (bond->dev->flags & IFF_UP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000937 pr_err(DRV_NAME
Jay Vosburghfd989c82008-11-04 17:51:16 -0800938 ": %s: Unable to update ad_select because interface "
939 "is up.\n", bond->dev->name);
940 ret = -EPERM;
941 goto out;
942 }
943
944 new_value = bond_parse_parm(buf, ad_select_tbl);
945
946 if (new_value != -1) {
947 bond->params.ad_select = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000948 pr_info(DRV_NAME
Jay Vosburghfd989c82008-11-04 17:51:16 -0800949 ": %s: Setting ad_select to %s (%d).\n",
950 bond->dev->name, ad_select_tbl[new_value].modename,
951 new_value);
952 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000953 pr_err(DRV_NAME
Jay Vosburghfd989c82008-11-04 17:51:16 -0800954 ": %s: Ignoring invalid ad_select value %.*s.\n",
955 bond->dev->name, (int)strlen(buf) - 1, buf);
956 ret = -EINVAL;
957 }
958out:
959 return ret;
960}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000961static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
962 bonding_show_ad_select, bonding_store_ad_select);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800963
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800964/*
Moni Shoua7893b242008-05-17 21:10:12 -0700965 * Show and set the number of grat ARP to send after a failover event.
966 */
967static ssize_t bonding_show_n_grat_arp(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_grat_arp);
974}
975
976static ssize_t bonding_store_n_grat_arp(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) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000984 pr_err(DRV_NAME
Moni Shoua7893b242008-05-17 21:10:12 -0700985 ": %s: no num_grat_arp value specified.\n",
986 bond->dev->name);
987 ret = -EINVAL;
988 goto out;
989 }
990 if (new_value < 0 || new_value > 255) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000991 pr_err(DRV_NAME
Moni Shoua7893b242008-05-17 21:10:12 -0700992 ": %s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
993 bond->dev->name, new_value);
994 ret = -EINVAL;
995 goto out;
996 } else {
997 bond->params.num_grat_arp = new_value;
998 }
999out:
1000 return ret;
1001}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001002static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
1003 bonding_show_n_grat_arp, bonding_store_n_grat_arp);
Brian Haley305d5522008-11-04 17:51:14 -08001004
1005/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001006 * Show and set the number of unsolicited NA's to send after a failover event.
Brian Haley305d5522008-11-04 17:51:14 -08001007 */
1008static ssize_t bonding_show_n_unsol_na(struct device *d,
1009 struct device_attribute *attr,
1010 char *buf)
1011{
1012 struct bonding *bond = to_bond(d);
1013
1014 return sprintf(buf, "%d\n", bond->params.num_unsol_na);
1015}
1016
1017static ssize_t bonding_store_n_unsol_na(struct device *d,
1018 struct device_attribute *attr,
1019 const char *buf, size_t count)
1020{
1021 int new_value, ret = count;
1022 struct bonding *bond = to_bond(d);
1023
1024 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001025 pr_err(DRV_NAME
Brian Haley305d5522008-11-04 17:51:14 -08001026 ": %s: no num_unsol_na value specified.\n",
1027 bond->dev->name);
1028 ret = -EINVAL;
1029 goto out;
1030 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001031
Brian Haley305d5522008-11-04 17:51:14 -08001032 if (new_value < 0 || new_value > 255) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001033 pr_err(DRV_NAME
Brian Haley305d5522008-11-04 17:51:14 -08001034 ": %s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
1035 bond->dev->name, new_value);
1036 ret = -EINVAL;
1037 goto out;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001038 } else
Brian Haley305d5522008-11-04 17:51:14 -08001039 bond->params.num_unsol_na = new_value;
Brian Haley305d5522008-11-04 17:51:14 -08001040out:
1041 return ret;
1042}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001043static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
1044 bonding_show_n_unsol_na, bonding_store_n_unsol_na);
Brian Haley305d5522008-11-04 17:51:14 -08001045
Moni Shoua7893b242008-05-17 21:10:12 -07001046/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001047 * Show and set the MII monitor interval. There are two tricky bits
1048 * here. First, if MII monitoring is activated, then we must disable
1049 * ARP monitoring. Second, if the timer isn't running, we must
1050 * start it.
1051 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001052static ssize_t bonding_show_miimon(struct device *d,
1053 struct device_attribute *attr,
1054 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001055{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001056 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001057
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001058 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001059}
1060
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001061static ssize_t bonding_store_miimon(struct device *d,
1062 struct device_attribute *attr,
1063 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001064{
1065 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001066 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001067
1068 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001069 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001070 ": %s: no miimon value specified.\n",
1071 bond->dev->name);
1072 ret = -EINVAL;
1073 goto out;
1074 }
1075 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001076 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001077 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1078 bond->dev->name, new_value, 1, INT_MAX);
1079 ret = -EINVAL;
1080 goto out;
1081 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001082 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001083 ": %s: Setting MII monitoring interval to %d.\n",
1084 bond->dev->name, new_value);
1085 bond->params.miimon = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001086 if (bond->params.updelay)
1087 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001088 ": %s: Note: Updating updelay (to %d) "
1089 "since it is a multiple of the miimon value.\n",
1090 bond->dev->name,
1091 bond->params.updelay * bond->params.miimon);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001092 if (bond->params.downdelay)
1093 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001094 ": %s: Note: Updating downdelay (to %d) "
1095 "since it is a multiple of the miimon value.\n",
1096 bond->dev->name,
1097 bond->params.downdelay * bond->params.miimon);
1098 if (bond->params.arp_interval) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001099 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001100 ": %s: MII monitoring cannot be used with "
1101 "ARP monitoring. Disabling ARP monitoring...\n",
1102 bond->dev->name);
1103 bond->params.arp_interval = 0;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -08001104 bond->dev->priv_flags &= ~IFF_MASTER_ARPMON;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001105 if (bond->params.arp_validate) {
1106 bond_unregister_arp(bond);
1107 bond->params.arp_validate =
1108 BOND_ARP_VALIDATE_NONE;
1109 }
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001110 if (delayed_work_pending(&bond->arp_work)) {
1111 cancel_delayed_work(&bond->arp_work);
1112 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001113 }
1114 }
1115
1116 if (bond->dev->flags & IFF_UP) {
1117 /* If the interface is up, we may need to fire off
1118 * the MII timer. If the interface is down, the
1119 * timer will get fired off when the open function
1120 * is called.
1121 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001122 if (!delayed_work_pending(&bond->mii_work)) {
1123 INIT_DELAYED_WORK(&bond->mii_work,
1124 bond_mii_monitor);
1125 queue_delayed_work(bond->wq,
1126 &bond->mii_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001127 }
1128 }
1129 }
1130out:
1131 return ret;
1132}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001133static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1134 bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001135
1136/*
1137 * Show and set the primary slave. The store function is much
1138 * simpler than bonding_store_slaves function because it only needs to
1139 * handle one interface name.
1140 * The bond must be a mode that supports a primary for this be
1141 * set.
1142 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001143static ssize_t bonding_show_primary(struct device *d,
1144 struct device_attribute *attr,
1145 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001146{
1147 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001148 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001149
1150 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001151 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001152
1153 return count;
1154}
1155
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001156static ssize_t bonding_store_primary(struct device *d,
1157 struct device_attribute *attr,
1158 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001159{
1160 int i;
1161 struct slave *slave;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001162 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001163
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001164 if (!rtnl_trylock())
1165 return restart_syscall();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001166 read_lock(&bond->lock);
1167 write_lock_bh(&bond->curr_slave_lock);
1168
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001169 if (!USES_PRIMARY(bond->params.mode)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001170 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001171 ": %s: Unable to set primary slave; %s is in mode %d\n",
1172 bond->dev->name, bond->dev->name, bond->params.mode);
1173 } else {
1174 bond_for_each_slave(bond, slave, i) {
1175 if (strnicmp
1176 (slave->dev->name, buf,
1177 strlen(slave->dev->name)) == 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001178 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001179 ": %s: Setting %s as primary slave.\n",
1180 bond->dev->name, slave->dev->name);
1181 bond->primary_slave = slave;
1182 bond_select_active_slave(bond);
1183 goto out;
1184 }
1185 }
1186
1187 /* if we got here, then we didn't match the name of any slave */
1188
1189 if (strlen(buf) == 0 || buf[0] == '\n') {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001190 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001191 ": %s: Setting primary slave to None.\n",
1192 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001193 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001194 bond_select_active_slave(bond);
1195 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001196 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001197 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1198 bond->dev->name, (int)strlen(buf) - 1, buf);
1199 }
1200 }
1201out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001202 write_unlock_bh(&bond->curr_slave_lock);
1203 read_unlock(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001204 rtnl_unlock();
1205
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001206 return count;
1207}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001208static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1209 bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001210
1211/*
1212 * Show and set the use_carrier flag.
1213 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001214static ssize_t bonding_show_carrier(struct device *d,
1215 struct device_attribute *attr,
1216 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001217{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001218 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001219
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001220 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001221}
1222
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001223static ssize_t bonding_store_carrier(struct device *d,
1224 struct device_attribute *attr,
1225 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001226{
1227 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001228 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001229
1230
1231 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001232 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001233 ": %s: no use_carrier value specified.\n",
1234 bond->dev->name);
1235 ret = -EINVAL;
1236 goto out;
1237 }
1238 if ((new_value == 0) || (new_value == 1)) {
1239 bond->params.use_carrier = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001240 pr_info(DRV_NAME ": %s: Setting use_carrier to %d.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001241 bond->dev->name, new_value);
1242 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001243 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001244 ": %s: Ignoring invalid use_carrier value %d.\n",
1245 bond->dev->name, new_value);
1246 }
1247out:
1248 return count;
1249}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001250static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1251 bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001252
1253
1254/*
1255 * Show and set currently active_slave.
1256 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001257static ssize_t bonding_show_active_slave(struct device *d,
1258 struct device_attribute *attr,
1259 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001260{
1261 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001262 struct bonding *bond = to_bond(d);
Wagner Ferenc16cd0162007-12-06 23:40:29 -08001263 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001264
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001265 read_lock(&bond->curr_slave_lock);
1266 curr = bond->curr_active_slave;
1267 read_unlock(&bond->curr_slave_lock);
1268
1269 if (USES_PRIMARY(bond->params.mode) && curr)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001270 count = sprintf(buf, "%s\n", curr->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001271 return count;
1272}
1273
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001274static ssize_t bonding_store_active_slave(struct device *d,
1275 struct device_attribute *attr,
1276 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001277{
1278 int i;
1279 struct slave *slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001280 struct slave *old_active = NULL;
1281 struct slave *new_active = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001282 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001283
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001284 if (!rtnl_trylock())
1285 return restart_syscall();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001286 read_lock(&bond->lock);
1287 write_lock_bh(&bond->curr_slave_lock);
Jay Vosburgh1466a212007-11-06 13:33:28 -08001288
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001289 if (!USES_PRIMARY(bond->params.mode))
1290 pr_info(DRV_NAME ": %s: Unable to change active slave;"
1291 " %s is in mode %d\n",
1292 bond->dev->name, bond->dev->name, bond->params.mode);
1293 else {
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001294 bond_for_each_slave(bond, slave, i) {
1295 if (strnicmp
1296 (slave->dev->name, buf,
1297 strlen(slave->dev->name)) == 0) {
1298 old_active = bond->curr_active_slave;
1299 new_active = slave;
Jay Vosburgha50d8de2006-09-22 21:53:25 -07001300 if (new_active == old_active) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001301 /* do nothing */
1302 printk(KERN_INFO DRV_NAME
1303 ": %s: %s is already the current active slave.\n",
1304 bond->dev->name, slave->dev->name);
1305 goto out;
1306 }
1307 else {
1308 if ((new_active) &&
1309 (old_active) &&
1310 (new_active->link == BOND_LINK_UP) &&
1311 IS_UP(new_active->dev)) {
1312 printk(KERN_INFO DRV_NAME
1313 ": %s: Setting %s as active slave.\n",
1314 bond->dev->name, slave->dev->name);
1315 bond_change_active_slave(bond, new_active);
1316 }
1317 else {
1318 printk(KERN_INFO DRV_NAME
1319 ": %s: Could not set %s as active slave; "
1320 "either %s is down or the link is down.\n",
1321 bond->dev->name, slave->dev->name,
1322 slave->dev->name);
1323 }
1324 goto out;
1325 }
1326 }
1327 }
1328
1329 /* if we got here, then we didn't match the name of any slave */
1330
1331 if (strlen(buf) == 0 || buf[0] == '\n') {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001332 pr_info(DRV_NAME
1333 ": %s: Setting active slave to None.\n",
1334 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001335 bond->primary_slave = NULL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001336 bond_select_active_slave(bond);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001337 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001338 pr_info(DRV_NAME ": %s: Unable to set %.*s"
1339 " as active slave as it is not a slave.\n",
1340 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001341 }
1342 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001343 out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001344 write_unlock_bh(&bond->curr_slave_lock);
1345 read_unlock(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001346 rtnl_unlock();
1347
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001348 return count;
1349
1350}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001351static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1352 bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001353
1354
1355/*
1356 * Show link status of the bond interface.
1357 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001358static ssize_t bonding_show_mii_status(struct device *d,
1359 struct device_attribute *attr,
1360 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001361{
1362 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001363 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001364
1365 read_lock(&bond->curr_slave_lock);
1366 curr = bond->curr_active_slave;
1367 read_unlock(&bond->curr_slave_lock);
1368
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001369 return sprintf(buf, "%s\n", curr ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001370}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001371static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001372
1373
1374/*
1375 * Show current 802.3ad aggregator ID.
1376 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001377static ssize_t bonding_show_ad_aggregator(struct device *d,
1378 struct device_attribute *attr,
1379 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001380{
1381 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001382 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001383
1384 if (bond->params.mode == BOND_MODE_8023AD) {
1385 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001386 count = sprintf(buf, "%d\n",
1387 (bond_3ad_get_active_agg_info(bond, &ad_info))
1388 ? 0 : ad_info.aggregator_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001389 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001390
1391 return count;
1392}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001393static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001394
1395
1396/*
1397 * Show number of active 802.3ad ports.
1398 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001399static ssize_t bonding_show_ad_num_ports(struct device *d,
1400 struct device_attribute *attr,
1401 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001402{
1403 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001404 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001405
1406 if (bond->params.mode == BOND_MODE_8023AD) {
1407 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001408 count = sprintf(buf, "%d\n",
1409 (bond_3ad_get_active_agg_info(bond, &ad_info))
1410 ? 0 : ad_info.ports);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001411 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001412
1413 return count;
1414}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001415static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001416
1417
1418/*
1419 * Show current 802.3ad actor key.
1420 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001421static ssize_t bonding_show_ad_actor_key(struct device *d,
1422 struct device_attribute *attr,
1423 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001424{
1425 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001426 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001427
1428 if (bond->params.mode == BOND_MODE_8023AD) {
1429 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001430 count = sprintf(buf, "%d\n",
1431 (bond_3ad_get_active_agg_info(bond, &ad_info))
1432 ? 0 : ad_info.actor_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001433 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001434
1435 return count;
1436}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001437static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001438
1439
1440/*
1441 * Show current 802.3ad partner key.
1442 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001443static ssize_t bonding_show_ad_partner_key(struct device *d,
1444 struct device_attribute *attr,
1445 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001446{
1447 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001448 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001449
1450 if (bond->params.mode == BOND_MODE_8023AD) {
1451 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001452 count = sprintf(buf, "%d\n",
1453 (bond_3ad_get_active_agg_info(bond, &ad_info))
1454 ? 0 : ad_info.partner_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001455 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001456
1457 return count;
1458}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001459static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001460
1461
1462/*
1463 * Show current 802.3ad partner mac.
1464 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001465static ssize_t bonding_show_ad_partner_mac(struct device *d,
1466 struct device_attribute *attr,
1467 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001468{
1469 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001470 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001471
1472 if (bond->params.mode == BOND_MODE_8023AD) {
1473 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001474 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
Johannes Berge1749612008-10-27 15:59:26 -07001475 count = sprintf(buf, "%pM\n", ad_info.partner_system);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001476 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001477
1478 return count;
1479}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001480static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001481
1482
1483
1484static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001485 &dev_attr_slaves.attr,
1486 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001487 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001488 &dev_attr_arp_validate.attr,
1489 &dev_attr_arp_interval.attr,
1490 &dev_attr_arp_ip_target.attr,
1491 &dev_attr_downdelay.attr,
1492 &dev_attr_updelay.attr,
1493 &dev_attr_lacp_rate.attr,
Jay Vosburghfd989c82008-11-04 17:51:16 -08001494 &dev_attr_ad_select.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001495 &dev_attr_xmit_hash_policy.attr,
Moni Shoua7893b242008-05-17 21:10:12 -07001496 &dev_attr_num_grat_arp.attr,
Brian Haley305d5522008-11-04 17:51:14 -08001497 &dev_attr_num_unsol_na.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001498 &dev_attr_miimon.attr,
1499 &dev_attr_primary.attr,
1500 &dev_attr_use_carrier.attr,
1501 &dev_attr_active_slave.attr,
1502 &dev_attr_mii_status.attr,
1503 &dev_attr_ad_aggregator.attr,
1504 &dev_attr_ad_num_ports.attr,
1505 &dev_attr_ad_actor_key.attr,
1506 &dev_attr_ad_partner_key.attr,
1507 &dev_attr_ad_partner_mac.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001508 NULL,
1509};
1510
1511static struct attribute_group bonding_group = {
1512 .name = "bonding",
1513 .attrs = per_bond_attrs,
1514};
1515
1516/*
1517 * Initialize sysfs. This sets up the bonding_masters file in
1518 * /sys/class/net.
1519 */
1520int bond_create_sysfs(void)
1521{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001522 int ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001523
Jay Vosburghb8a97872008-06-13 18:12:04 -07001524 ret = netdev_class_create_file(&class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001525 /*
1526 * Permit multiple loads of the module by ignoring failures to
1527 * create the bonding_masters sysfs file. Bonding devices
1528 * created by second or subsequent loads of the module will
1529 * not be listed in, or controllable by, bonding_masters, but
1530 * will have the usual "bonding" sysfs directory.
1531 *
1532 * This is done to preserve backwards compatibility for
1533 * initscripts/sysconfig, which load bonding multiple times to
1534 * configure multiple bonding devices.
1535 */
1536 if (ret == -EEXIST) {
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001537 /* Is someone being kinky and naming a device bonding_master? */
1538 if (__dev_get_by_name(&init_net,
1539 class_attr_bonding_masters.attr.name))
1540 printk(KERN_ERR
1541 "network device named %s already exists in sysfs",
1542 class_attr_bonding_masters.attr.name);
Stephen Hemminger130aa612009-06-11 05:46:04 -07001543 ret = 0;
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001544 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001545
1546 return ret;
1547
1548}
1549
1550/*
1551 * Remove /sys/class/net/bonding_masters.
1552 */
1553void bond_destroy_sysfs(void)
1554{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001555 netdev_class_remove_file(&class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001556}
1557
1558/*
1559 * Initialize sysfs for each bond. This sets up and registers
1560 * the 'bondctl' directory for each individual bond under /sys/class/net.
1561 */
1562int bond_create_sysfs_entry(struct bonding *bond)
1563{
1564 struct net_device *dev = bond->dev;
1565 int err;
1566
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001567 err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001568 if (err)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001569 printk(KERN_EMERG "eek! didn't create group!\n");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001570
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001571 return err;
1572}
1573/*
1574 * Remove sysfs entries for each bond.
1575 */
1576void bond_destroy_sysfs_entry(struct bonding *bond)
1577{
1578 struct net_device *dev = bond->dev;
1579
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001580 sysfs_remove_group(&(dev->dev.kobj), &bonding_group);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001581}
1582