blob: 5fb861a08664fd6d9fe9087823ead532ea2304dc [file] [log] [blame]
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001
2/*
3 * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 * The full GNU General Public License is included in this distribution in the
20 * file called LICENSE.
21 *
Mitch Williamsb76cdba2005-11-09 10:36:41 -080022 */
Mitch Williamsb76cdba2005-11-09 10:36:41 -080023#include <linux/kernel.h>
24#include <linux/module.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080025#include <linux/device.h>
26#include <linux/sysdev.h>
27#include <linux/fs.h>
28#include <linux/types.h>
29#include <linux/string.h>
30#include <linux/netdevice.h>
31#include <linux/inetdevice.h>
32#include <linux/in.h>
33#include <linux/sysfs.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080034#include <linux/ctype.h>
35#include <linux/inet.h>
36#include <linux/rtnetlink.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
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070041#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
44/*---------------------------- Declarations -------------------------------*/
45
Mitch Williamsb76cdba2005-11-09 10:36:41 -080046static int expected_refcount = -1;
Mitch Williamsb76cdba2005-11-09 10:36:41 -080047/*--------------------------- Data Structures -----------------------------*/
48
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020049/* Bonding sysfs lock. Why can't we just use the subsystem lock?
Mitch Williamsb76cdba2005-11-09 10:36:41 -080050 * Because kobject_register tries to acquire the subsystem lock. If
51 * we already hold the lock (which we would if the user was creating
52 * a new bond through the sysfs interface), we deadlock.
53 * This lock is only needed when deleting a bond - we need to make sure
54 * that we don't collide with an ongoing ioctl.
55 */
56
57struct rw_semaphore bonding_rwsem;
58
59
60
61
62/*------------------------------ Functions --------------------------------*/
63
64/*
65 * "show" function for the bond_masters attribute.
66 * The class parameter is ignored.
67 */
Wagner Ferencb8843662007-12-06 23:40:30 -080068static ssize_t bonding_show_bonds(struct class *cls, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080069{
70 int res = 0;
71 struct bonding *bond;
72
73 down_read(&(bonding_rwsem));
74
75 list_for_each_entry(bond, &bond_dev_list, bond_list) {
76 if (res > (PAGE_SIZE - IFNAMSIZ)) {
77 /* not enough space for another interface name */
78 if ((PAGE_SIZE - res) > 10)
79 res = PAGE_SIZE - 10;
Wagner Ferencb8843662007-12-06 23:40:30 -080080 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -080081 break;
82 }
Wagner Ferencb8843662007-12-06 23:40:30 -080083 res += sprintf(buf + res, "%s ", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080084 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -080085 if (res)
86 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -080087 up_read(&(bonding_rwsem));
88 return res;
89}
90
91/*
92 * "store" function for the bond_masters attribute. This is what
93 * creates and deletes entire bonds.
94 *
95 * The class parameter is ignored.
96 *
97 */
98
99static ssize_t bonding_store_bonds(struct class *cls, const char *buffer, size_t count)
100{
101 char command[IFNAMSIZ + 1] = {0, };
102 char *ifname;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800103 int rv, res = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800104 struct bonding *bond;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800105
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800106 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
107 ifname = command + 1;
108 if ((strlen(command) <= 1) ||
109 !dev_valid_name(ifname))
110 goto err_no_cmd;
111
112 if (command[0] == '+') {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800113 printk(KERN_INFO DRV_NAME
114 ": %s is being created...\n", ifname);
Pavel Emelyanov0dd646f2008-05-17 21:10:09 -0700115 rv = bond_create(ifname, &bonding_defaults);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800116 if (rv) {
117 printk(KERN_INFO DRV_NAME ": Bond creation failed.\n");
118 res = rv;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800119 }
120 goto out;
121 }
122
123 if (command[0] == '-') {
Jay Vosburgh027ea042008-01-17 16:25:02 -0800124 rtnl_lock();
125 down_write(&bonding_rwsem);
126
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700127 list_for_each_entry(bond, &bond_dev_list, bond_list)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800128 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800129 /* check the ref count on the bond's kobject.
130 * If it's > expected, then there's a file open,
131 * and we have to fail.
132 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700133 if (atomic_read(&bond->dev->dev.kobj.kref.refcount)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800134 > expected_refcount){
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800135 printk(KERN_INFO DRV_NAME
136 ": Unable remove bond %s due to open references.\n",
137 ifname);
138 res = -EPERM;
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700139 goto out_unlock;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800140 }
141 printk(KERN_INFO DRV_NAME
142 ": %s is being deleted...\n",
143 bond->dev->name);
Moni Shouad90a1622007-10-09 19:43:43 -0700144 bond_destroy(bond);
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700145 goto out_unlock;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800146 }
147
148 printk(KERN_ERR DRV_NAME
149 ": unable to delete non-existent bond %s\n", ifname);
150 res = -ENODEV;
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700151 goto out_unlock;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800152 }
153
154err_no_cmd:
155 printk(KERN_ERR DRV_NAME
156 ": no command found in bonding_masters. Use +ifname or -ifname.\n");
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700157 return -EPERM;
158
159out_unlock:
160 up_write(&bonding_rwsem);
161 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800162
163 /* Always return either count or an error. If you return 0, you'll
164 * get called forever, which is bad.
165 */
166out:
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800167 return res;
168}
169/* class attribute for bond_masters file. This ends up in /sys/class/net */
170static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
171 bonding_show_bonds, bonding_store_bonds);
172
173int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave)
174{
175 char linkname[IFNAMSIZ+7];
176 int ret = 0;
177
178 /* first, create a link from the slave back to the master */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700179 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800180 "master");
181 if (ret)
182 return ret;
183 /* next, create a link from the master to the slave */
184 sprintf(linkname,"slave_%s",slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700185 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800186 linkname);
187 return ret;
188
189}
190
191void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave)
192{
193 char linkname[IFNAMSIZ+7];
194
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700195 sysfs_remove_link(&(slave->dev.kobj), "master");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800196 sprintf(linkname,"slave_%s",slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700197 sysfs_remove_link(&(master->dev.kobj), linkname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800198}
199
200
201/*
202 * Show the slaves in the current bond.
203 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700204static ssize_t bonding_show_slaves(struct device *d,
205 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800206{
207 struct slave *slave;
208 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700209 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800210
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700211 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800212 bond_for_each_slave(bond, slave, i) {
213 if (res > (PAGE_SIZE - IFNAMSIZ)) {
214 /* not enough space for another interface name */
215 if ((PAGE_SIZE - res) > 10)
216 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800217 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800218 break;
219 }
220 res += sprintf(buf + res, "%s ", slave->dev->name);
221 }
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700222 read_unlock(&bond->lock);
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800223 if (res)
224 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800225 return res;
226}
227
228/*
229 * Set the slaves in the current bond. The bond interface must be
230 * up for this to succeed.
231 * This function is largely the same flow as bonding_update_bonds().
232 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700233static ssize_t bonding_store_slaves(struct device *d,
234 struct device_attribute *attr,
235 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800236{
237 char command[IFNAMSIZ + 1] = { 0, };
238 char *ifname;
239 int i, res, found, ret = count;
Moni Shoua3158bf72007-10-09 19:43:41 -0700240 u32 original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800241 struct slave *slave;
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -0800242 struct net_device *dev = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700243 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800244
245 /* Quick sanity check -- is the bond interface up? */
246 if (!(bond->dev->flags & IFF_UP)) {
Moni Shoua6b1bf092007-10-09 19:43:40 -0700247 printk(KERN_WARNING DRV_NAME
248 ": %s: doing slave updates when interface is down.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800249 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800250 }
251
252 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
253
Eric W. Biederman496a60c2009-05-13 17:02:50 +0000254 if (!rtnl_trylock())
255 return restart_syscall();
Jay Vosburgh027ea042008-01-17 16:25:02 -0800256 down_write(&(bonding_rwsem));
257
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800258 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
259 ifname = command + 1;
260 if ((strlen(command) <= 1) ||
261 !dev_valid_name(ifname))
262 goto err_no_cmd;
263
264 if (command[0] == '+') {
265
266 /* Got a slave name in ifname. Is it already in the list? */
267 found = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700268 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800269 bond_for_each_slave(bond, slave, i)
270 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
271 printk(KERN_ERR DRV_NAME
272 ": %s: Interface %s is already enslaved!\n",
273 bond->dev->name, ifname);
274 ret = -EPERM;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700275 read_unlock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800276 goto out;
277 }
278
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700279 read_unlock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800280 printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n",
281 bond->dev->name, ifname);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700282 dev = dev_get_by_name(&init_net, ifname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800283 if (!dev) {
284 printk(KERN_INFO DRV_NAME
285 ": %s: Interface %s does not exist!\n",
286 bond->dev->name, ifname);
287 ret = -EPERM;
288 goto out;
289 }
290 else
291 dev_put(dev);
292
293 if (dev->flags & IFF_UP) {
294 printk(KERN_ERR DRV_NAME
295 ": %s: Error: Unable to enslave %s "
296 "because it is already up.\n",
297 bond->dev->name, dev->name);
298 ret = -EPERM;
299 goto out;
300 }
301 /* If this is the first slave, then we need to set
302 the master's hardware address to be the same as the
303 slave's. */
304 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
305 memcpy(bond->dev->dev_addr, dev->dev_addr,
306 dev->addr_len);
307 }
308
309 /* Set the slave's MTU to match the bond */
Moni Shoua3158bf72007-10-09 19:43:41 -0700310 original_mtu = dev->mtu;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800311 res = dev_set_mtu(dev, bond->dev->mtu);
312 if (res) {
313 ret = res;
314 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800315 }
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800316
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800317 res = bond_enslave(bond->dev, dev);
Moni Shoua3158bf72007-10-09 19:43:41 -0700318 bond_for_each_slave(bond, slave, i)
319 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
320 slave->original_mtu = original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800321 if (res) {
322 ret = res;
323 }
324 goto out;
325 }
326
327 if (command[0] == '-') {
328 dev = NULL;
David S. Miller6952d8922008-03-28 16:15:38 -0700329 original_mtu = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800330 bond_for_each_slave(bond, slave, i)
331 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
332 dev = slave->dev;
Moni Shoua3158bf72007-10-09 19:43:41 -0700333 original_mtu = slave->original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800334 break;
335 }
336 if (dev) {
337 printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n",
338 bond->dev->name, dev->name);
Moni Shouad90a1622007-10-09 19:43:43 -0700339 res = bond_release(bond->dev, dev);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800340 if (res) {
341 ret = res;
342 goto out;
343 }
344 /* set the slave MTU to the default */
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800345 dev_set_mtu(dev, original_mtu);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800346 }
347 else {
348 printk(KERN_ERR DRV_NAME ": unable to remove non-existent slave %s for bond %s.\n",
349 ifname, bond->dev->name);
350 ret = -ENODEV;
351 }
352 goto out;
353 }
354
355err_no_cmd:
356 printk(KERN_ERR DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
357 ret = -EPERM;
358
359out:
Jay Vosburgh027ea042008-01-17 16:25:02 -0800360 up_write(&(bonding_rwsem));
361 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800362 return ret;
363}
364
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700365static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves, bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800366
367/*
368 * Show and set the bonding mode. The bond interface must be down to
369 * change the mode.
370 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700371static ssize_t bonding_show_mode(struct device *d,
372 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800373{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700374 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800375
376 return sprintf(buf, "%s %d\n",
377 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800378 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800379}
380
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700381static ssize_t bonding_store_mode(struct device *d,
382 struct device_attribute *attr,
383 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800384{
385 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700386 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800387
388 if (bond->dev->flags & IFF_UP) {
389 printk(KERN_ERR DRV_NAME
390 ": unable to update mode of %s because interface is up.\n",
391 bond->dev->name);
392 ret = -EPERM;
393 goto out;
394 }
395
Jay Vosburghece95f72008-01-17 16:25:01 -0800396 new_value = bond_parse_parm(buf, bond_mode_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800397 if (new_value < 0) {
398 printk(KERN_ERR DRV_NAME
399 ": %s: Ignoring invalid mode value %.*s.\n",
400 bond->dev->name,
401 (int)strlen(buf) - 1, buf);
402 ret = -EINVAL;
403 goto out;
404 } else {
Jay Vosburgh8f903c72006-02-21 16:36:44 -0800405 if (bond->params.mode == BOND_MODE_8023AD)
406 bond_unset_master_3ad_flags(bond);
407
408 if (bond->params.mode == BOND_MODE_ALB)
409 bond_unset_master_alb_flags(bond);
410
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800411 bond->params.mode = new_value;
412 bond_set_mode_ops(bond, bond->params.mode);
413 printk(KERN_INFO DRV_NAME ": %s: setting mode to %s (%d).\n",
414 bond->dev->name, bond_mode_tbl[new_value].modename, new_value);
415 }
416out:
417 return ret;
418}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700419static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800420
421/*
422 * Show and set the bonding transmit hash method. The bond interface must be down to
423 * change the xmit hash policy.
424 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700425static ssize_t bonding_show_xmit_hash(struct device *d,
426 struct device_attribute *attr,
427 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800428{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700429 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800430
Wagner Ferenc8e4b9322007-12-06 23:40:32 -0800431 return sprintf(buf, "%s %d\n",
432 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
433 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800434}
435
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700436static ssize_t bonding_store_xmit_hash(struct device *d,
437 struct device_attribute *attr,
438 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800439{
440 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700441 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800442
443 if (bond->dev->flags & IFF_UP) {
444 printk(KERN_ERR DRV_NAME
445 "%s: Interface is up. Unable to update xmit policy.\n",
446 bond->dev->name);
447 ret = -EPERM;
448 goto out;
449 }
450
Jay Vosburghece95f72008-01-17 16:25:01 -0800451 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800452 if (new_value < 0) {
453 printk(KERN_ERR DRV_NAME
454 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
455 bond->dev->name,
456 (int)strlen(buf) - 1, buf);
457 ret = -EINVAL;
458 goto out;
459 } else {
460 bond->params.xmit_policy = new_value;
461 bond_set_mode_ops(bond, bond->params.mode);
462 printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
463 bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value);
464 }
465out:
466 return ret;
467}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700468static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800469
470/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700471 * Show and set arp_validate.
472 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700473static ssize_t bonding_show_arp_validate(struct device *d,
474 struct device_attribute *attr,
475 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700476{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700477 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700478
479 return sprintf(buf, "%s %d\n",
480 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800481 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700482}
483
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700484static ssize_t bonding_store_arp_validate(struct device *d,
485 struct device_attribute *attr,
486 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700487{
488 int new_value;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700489 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700490
Jay Vosburghece95f72008-01-17 16:25:01 -0800491 new_value = bond_parse_parm(buf, arp_validate_tbl);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700492 if (new_value < 0) {
493 printk(KERN_ERR DRV_NAME
494 ": %s: Ignoring invalid arp_validate value %s\n",
495 bond->dev->name, buf);
496 return -EINVAL;
497 }
498 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
499 printk(KERN_ERR DRV_NAME
500 ": %s: arp_validate only supported in active-backup mode.\n",
501 bond->dev->name);
502 return -EINVAL;
503 }
504 printk(KERN_INFO DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
505 bond->dev->name, arp_validate_tbl[new_value].modename,
506 new_value);
507
508 if (!bond->params.arp_validate && new_value) {
509 bond_register_arp(bond);
510 } else if (bond->params.arp_validate && !new_value) {
511 bond_unregister_arp(bond);
512 }
513
514 bond->params.arp_validate = new_value;
515
516 return count;
517}
518
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700519static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate, bonding_store_arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700520
521/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700522 * Show and store fail_over_mac. User only allowed to change the
523 * value when there are no slaves.
524 */
525static ssize_t bonding_show_fail_over_mac(struct device *d, struct device_attribute *attr, char *buf)
526{
527 struct bonding *bond = to_bond(d);
528
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700529 return sprintf(buf, "%s %d\n",
530 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
531 bond->params.fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700532}
533
534static ssize_t bonding_store_fail_over_mac(struct device *d, struct device_attribute *attr, const char *buf, size_t count)
535{
536 int new_value;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700537 struct bonding *bond = to_bond(d);
538
539 if (bond->slave_cnt != 0) {
540 printk(KERN_ERR DRV_NAME
541 ": %s: Can't alter fail_over_mac with slaves in bond.\n",
542 bond->dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700543 return -EPERM;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700544 }
545
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700546 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
547 if (new_value < 0) {
Jay Vosburghdd957c52007-10-09 19:57:24 -0700548 printk(KERN_ERR DRV_NAME
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700549 ": %s: Ignoring invalid fail_over_mac value %s.\n",
550 bond->dev->name, buf);
551 return -EINVAL;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700552 }
553
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700554 bond->params.fail_over_mac = new_value;
555 printk(KERN_INFO DRV_NAME ": %s: Setting fail_over_mac to %s (%d).\n",
556 bond->dev->name, fail_over_mac_tbl[new_value].modename,
557 new_value);
558
559 return count;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700560}
561
562static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR, bonding_show_fail_over_mac, bonding_store_fail_over_mac);
563
564/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800565 * Show and set the arp timer interval. There are two tricky bits
566 * here. First, if ARP monitoring is activated, then we must disable
567 * MII monitoring. Second, if the ARP timer isn't running, we must
568 * start it.
569 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700570static ssize_t bonding_show_arp_interval(struct device *d,
571 struct device_attribute *attr,
572 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800573{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700574 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800575
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800576 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800577}
578
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700579static ssize_t bonding_store_arp_interval(struct device *d,
580 struct device_attribute *attr,
581 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800582{
583 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700584 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800585
586 if (sscanf(buf, "%d", &new_value) != 1) {
587 printk(KERN_ERR DRV_NAME
588 ": %s: no arp_interval value specified.\n",
589 bond->dev->name);
590 ret = -EINVAL;
591 goto out;
592 }
593 if (new_value < 0) {
594 printk(KERN_ERR DRV_NAME
595 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
596 bond->dev->name, new_value, INT_MAX);
597 ret = -EINVAL;
598 goto out;
599 }
600
601 printk(KERN_INFO DRV_NAME
602 ": %s: Setting ARP monitoring interval to %d.\n",
603 bond->dev->name, new_value);
604 bond->params.arp_interval = new_value;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -0800605 if (bond->params.arp_interval)
606 bond->dev->priv_flags |= IFF_MASTER_ARPMON;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800607 if (bond->params.miimon) {
608 printk(KERN_INFO DRV_NAME
609 ": %s: ARP monitoring cannot be used with MII monitoring. "
610 "%s Disabling MII monitoring.\n",
611 bond->dev->name, bond->dev->name);
612 bond->params.miimon = 0;
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700613 if (delayed_work_pending(&bond->mii_work)) {
614 cancel_delayed_work(&bond->mii_work);
615 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800616 }
617 }
618 if (!bond->params.arp_targets[0]) {
619 printk(KERN_INFO DRV_NAME
620 ": %s: ARP monitoring has been set up, "
621 "but no ARP targets have been specified.\n",
622 bond->dev->name);
623 }
624 if (bond->dev->flags & IFF_UP) {
625 /* If the interface is up, we may need to fire off
626 * the ARP timer. If the interface is down, the
627 * timer will get fired off when the open function
628 * is called.
629 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700630 if (!delayed_work_pending(&bond->arp_work)) {
631 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
632 INIT_DELAYED_WORK(&bond->arp_work,
633 bond_activebackup_arp_mon);
634 else
635 INIT_DELAYED_WORK(&bond->arp_work,
636 bond_loadbalance_arp_mon);
637
638 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800639 }
640 }
641
642out:
643 return ret;
644}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700645static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800646
647/*
648 * Show and set the arp targets.
649 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700650static ssize_t bonding_show_arp_targets(struct device *d,
651 struct device_attribute *attr,
652 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800653{
654 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700655 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800656
657 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
658 if (bond->params.arp_targets[i])
Harvey Harrison63779432008-10-31 00:56:00 -0700659 res += sprintf(buf + res, "%pI4 ",
660 &bond->params.arp_targets[i]);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800661 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800662 if (res)
663 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800664 return res;
665}
666
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700667static ssize_t bonding_store_arp_targets(struct device *d,
668 struct device_attribute *attr,
669 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800670{
Al Virod3bb52b2007-08-22 20:06:58 -0400671 __be32 newtarget;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800672 int i = 0, done = 0, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700673 struct bonding *bond = to_bond(d);
Al Virod3bb52b2007-08-22 20:06:58 -0400674 __be32 *targets;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800675
676 targets = bond->params.arp_targets;
677 newtarget = in_aton(buf + 1);
678 /* look for adds */
679 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400680 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800681 printk(KERN_ERR DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700682 ": %s: invalid ARP target %pI4 specified for addition\n",
683 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800684 ret = -EINVAL;
685 goto out;
686 }
687 /* look for an empty slot to put the target in, and check for dupes */
Brian Haley5a31bec2009-04-13 00:11:30 -0700688 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800689 if (targets[i] == newtarget) { /* duplicate */
690 printk(KERN_ERR DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700691 ": %s: ARP target %pI4 is already present\n",
692 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800693 ret = -EINVAL;
694 goto out;
695 }
Brian Haley5a31bec2009-04-13 00:11:30 -0700696 if (targets[i] == 0) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800697 printk(KERN_INFO DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700698 ": %s: adding ARP target %pI4.\n",
699 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800700 done = 1;
701 targets[i] = newtarget;
702 }
703 }
704 if (!done) {
705 printk(KERN_ERR DRV_NAME
706 ": %s: ARP target table is full!\n",
707 bond->dev->name);
708 ret = -EINVAL;
709 goto out;
710 }
711
712 }
713 else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400714 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800715 printk(KERN_ERR DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700716 ": %s: invalid ARP target %pI4 specified for removal\n",
717 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800718 ret = -EINVAL;
719 goto out;
720 }
721
Brian Haley5a31bec2009-04-13 00:11:30 -0700722 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800723 if (targets[i] == newtarget) {
Brian Haley5a31bec2009-04-13 00:11:30 -0700724 int j;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800725 printk(KERN_INFO DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700726 ": %s: removing ARP target %pI4.\n",
727 bond->dev->name, &newtarget);
Brian Haley5a31bec2009-04-13 00:11:30 -0700728 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
729 targets[j] = targets[j+1];
730
731 targets[j] = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800732 done = 1;
733 }
734 }
735 if (!done) {
736 printk(KERN_INFO DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700737 ": %s: unable to remove nonexistent ARP target %pI4.\n",
738 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800739 ret = -EINVAL;
740 goto out;
741 }
742 }
743 else {
744 printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
745 bond->dev->name);
746 ret = -EPERM;
747 goto out;
748 }
749
750out:
751 return ret;
752}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700753static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800754
755/*
756 * Show and set the up and down delays. These must be multiples of the
757 * MII monitoring value, and are stored internally as the multiplier.
758 * Thus, we must translate to MS for the real world.
759 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700760static ssize_t bonding_show_downdelay(struct device *d,
761 struct device_attribute *attr,
762 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800763{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700764 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800765
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800766 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800767}
768
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700769static ssize_t bonding_store_downdelay(struct device *d,
770 struct device_attribute *attr,
771 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800772{
773 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700774 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800775
776 if (!(bond->params.miimon)) {
777 printk(KERN_ERR DRV_NAME
778 ": %s: Unable to set down delay as MII monitoring is disabled\n",
779 bond->dev->name);
780 ret = -EPERM;
781 goto out;
782 }
783
784 if (sscanf(buf, "%d", &new_value) != 1) {
785 printk(KERN_ERR DRV_NAME
786 ": %s: no down delay value specified.\n",
787 bond->dev->name);
788 ret = -EINVAL;
789 goto out;
790 }
791 if (new_value < 0) {
792 printk(KERN_ERR DRV_NAME
793 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
794 bond->dev->name, new_value, 1, INT_MAX);
795 ret = -EINVAL;
796 goto out;
797 } else {
798 if ((new_value % bond->params.miimon) != 0) {
799 printk(KERN_WARNING DRV_NAME
800 ": %s: Warning: down delay (%d) is not a multiple "
801 "of miimon (%d), delay rounded to %d ms\n",
802 bond->dev->name, new_value, bond->params.miimon,
803 (new_value / bond->params.miimon) *
804 bond->params.miimon);
805 }
806 bond->params.downdelay = new_value / bond->params.miimon;
807 printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n",
808 bond->dev->name, bond->params.downdelay * bond->params.miimon);
809
810 }
811
812out:
813 return ret;
814}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700815static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800816
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700817static ssize_t bonding_show_updelay(struct device *d,
818 struct device_attribute *attr,
819 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800820{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700821 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800822
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800823 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800824
825}
826
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700827static ssize_t bonding_store_updelay(struct device *d,
828 struct device_attribute *attr,
829 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800830{
831 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700832 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800833
834 if (!(bond->params.miimon)) {
835 printk(KERN_ERR DRV_NAME
836 ": %s: Unable to set up delay as MII monitoring is disabled\n",
837 bond->dev->name);
838 ret = -EPERM;
839 goto out;
840 }
841
842 if (sscanf(buf, "%d", &new_value) != 1) {
843 printk(KERN_ERR DRV_NAME
844 ": %s: no up delay value specified.\n",
845 bond->dev->name);
846 ret = -EINVAL;
847 goto out;
848 }
849 if (new_value < 0) {
850 printk(KERN_ERR DRV_NAME
851 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
852 bond->dev->name, new_value, 1, INT_MAX);
853 ret = -EINVAL;
854 goto out;
855 } else {
856 if ((new_value % bond->params.miimon) != 0) {
857 printk(KERN_WARNING DRV_NAME
858 ": %s: Warning: up delay (%d) is not a multiple "
859 "of miimon (%d), updelay rounded to %d ms\n",
860 bond->dev->name, new_value, bond->params.miimon,
861 (new_value / bond->params.miimon) *
862 bond->params.miimon);
863 }
864 bond->params.updelay = new_value / bond->params.miimon;
865 printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n",
866 bond->dev->name, bond->params.updelay * bond->params.miimon);
867
868 }
869
870out:
871 return ret;
872}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700873static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800874
875/*
876 * Show and set the LACP interval. Interface must be down, and the mode
877 * must be set to 802.3ad mode.
878 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700879static ssize_t bonding_show_lacp(struct device *d,
880 struct device_attribute *attr,
881 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800882{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700883 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800884
885 return sprintf(buf, "%s %d\n",
886 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800887 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800888}
889
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700890static ssize_t bonding_store_lacp(struct device *d,
891 struct device_attribute *attr,
892 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800893{
894 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700895 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800896
897 if (bond->dev->flags & IFF_UP) {
898 printk(KERN_ERR DRV_NAME
899 ": %s: Unable to update LACP rate because interface is up.\n",
900 bond->dev->name);
901 ret = -EPERM;
902 goto out;
903 }
904
905 if (bond->params.mode != BOND_MODE_8023AD) {
906 printk(KERN_ERR DRV_NAME
907 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
908 bond->dev->name);
909 ret = -EPERM;
910 goto out;
911 }
912
Jay Vosburghece95f72008-01-17 16:25:01 -0800913 new_value = bond_parse_parm(buf, bond_lacp_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800914
915 if ((new_value == 1) || (new_value == 0)) {
916 bond->params.lacp_fast = new_value;
917 printk(KERN_INFO DRV_NAME
918 ": %s: Setting LACP rate to %s (%d).\n",
919 bond->dev->name, bond_lacp_tbl[new_value].modename, new_value);
920 } else {
921 printk(KERN_ERR DRV_NAME
922 ": %s: Ignoring invalid LACP rate value %.*s.\n",
923 bond->dev->name, (int)strlen(buf) - 1, buf);
924 ret = -EINVAL;
925 }
926out:
927 return ret;
928}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700929static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800930
Jay Vosburghfd989c82008-11-04 17:51:16 -0800931static ssize_t bonding_show_ad_select(struct device *d,
932 struct device_attribute *attr,
933 char *buf)
934{
935 struct bonding *bond = to_bond(d);
936
937 return sprintf(buf, "%s %d\n",
938 ad_select_tbl[bond->params.ad_select].modename,
939 bond->params.ad_select);
940}
941
942
943static ssize_t bonding_store_ad_select(struct device *d,
944 struct device_attribute *attr,
945 const char *buf, size_t count)
946{
947 int new_value, ret = count;
948 struct bonding *bond = to_bond(d);
949
950 if (bond->dev->flags & IFF_UP) {
951 printk(KERN_ERR DRV_NAME
952 ": %s: Unable to update ad_select because interface "
953 "is up.\n", bond->dev->name);
954 ret = -EPERM;
955 goto out;
956 }
957
958 new_value = bond_parse_parm(buf, ad_select_tbl);
959
960 if (new_value != -1) {
961 bond->params.ad_select = new_value;
962 printk(KERN_INFO DRV_NAME
963 ": %s: Setting ad_select to %s (%d).\n",
964 bond->dev->name, ad_select_tbl[new_value].modename,
965 new_value);
966 } else {
967 printk(KERN_ERR DRV_NAME
968 ": %s: Ignoring invalid ad_select value %.*s.\n",
969 bond->dev->name, (int)strlen(buf) - 1, buf);
970 ret = -EINVAL;
971 }
972out:
973 return ret;
974}
975
976static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR, bonding_show_ad_select, bonding_store_ad_select);
977
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800978/*
Moni Shoua7893b242008-05-17 21:10:12 -0700979 * Show and set the number of grat ARP to send after a failover event.
980 */
981static ssize_t bonding_show_n_grat_arp(struct device *d,
982 struct device_attribute *attr,
983 char *buf)
984{
985 struct bonding *bond = to_bond(d);
986
987 return sprintf(buf, "%d\n", bond->params.num_grat_arp);
988}
989
990static ssize_t bonding_store_n_grat_arp(struct device *d,
991 struct device_attribute *attr,
992 const char *buf, size_t count)
993{
994 int new_value, ret = count;
995 struct bonding *bond = to_bond(d);
996
997 if (sscanf(buf, "%d", &new_value) != 1) {
998 printk(KERN_ERR DRV_NAME
999 ": %s: no num_grat_arp value specified.\n",
1000 bond->dev->name);
1001 ret = -EINVAL;
1002 goto out;
1003 }
1004 if (new_value < 0 || new_value > 255) {
1005 printk(KERN_ERR DRV_NAME
1006 ": %s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
1007 bond->dev->name, new_value);
1008 ret = -EINVAL;
1009 goto out;
1010 } else {
1011 bond->params.num_grat_arp = new_value;
1012 }
1013out:
1014 return ret;
1015}
1016static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR, bonding_show_n_grat_arp, bonding_store_n_grat_arp);
Brian Haley305d5522008-11-04 17:51:14 -08001017
1018/*
1019 * Show and set the number of unsolicted NA's to send after a failover event.
1020 */
1021static ssize_t bonding_show_n_unsol_na(struct device *d,
1022 struct device_attribute *attr,
1023 char *buf)
1024{
1025 struct bonding *bond = to_bond(d);
1026
1027 return sprintf(buf, "%d\n", bond->params.num_unsol_na);
1028}
1029
1030static ssize_t bonding_store_n_unsol_na(struct device *d,
1031 struct device_attribute *attr,
1032 const char *buf, size_t count)
1033{
1034 int new_value, ret = count;
1035 struct bonding *bond = to_bond(d);
1036
1037 if (sscanf(buf, "%d", &new_value) != 1) {
1038 printk(KERN_ERR DRV_NAME
1039 ": %s: no num_unsol_na value specified.\n",
1040 bond->dev->name);
1041 ret = -EINVAL;
1042 goto out;
1043 }
1044 if (new_value < 0 || new_value > 255) {
1045 printk(KERN_ERR DRV_NAME
1046 ": %s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
1047 bond->dev->name, new_value);
1048 ret = -EINVAL;
1049 goto out;
1050 } else {
1051 bond->params.num_unsol_na = new_value;
1052 }
1053out:
1054 return ret;
1055}
1056static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR, bonding_show_n_unsol_na, bonding_store_n_unsol_na);
1057
Moni Shoua7893b242008-05-17 21:10:12 -07001058/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001059 * Show and set the MII monitor interval. There are two tricky bits
1060 * here. First, if MII monitoring is activated, then we must disable
1061 * ARP monitoring. Second, if the timer isn't running, we must
1062 * start it.
1063 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001064static ssize_t bonding_show_miimon(struct device *d,
1065 struct device_attribute *attr,
1066 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001067{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001068 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001069
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001070 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001071}
1072
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001073static ssize_t bonding_store_miimon(struct device *d,
1074 struct device_attribute *attr,
1075 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001076{
1077 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001078 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001079
1080 if (sscanf(buf, "%d", &new_value) != 1) {
1081 printk(KERN_ERR DRV_NAME
1082 ": %s: no miimon value specified.\n",
1083 bond->dev->name);
1084 ret = -EINVAL;
1085 goto out;
1086 }
1087 if (new_value < 0) {
1088 printk(KERN_ERR DRV_NAME
1089 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1090 bond->dev->name, new_value, 1, INT_MAX);
1091 ret = -EINVAL;
1092 goto out;
1093 } else {
1094 printk(KERN_INFO DRV_NAME
1095 ": %s: Setting MII monitoring interval to %d.\n",
1096 bond->dev->name, new_value);
1097 bond->params.miimon = new_value;
1098 if(bond->params.updelay)
1099 printk(KERN_INFO DRV_NAME
1100 ": %s: Note: Updating updelay (to %d) "
1101 "since it is a multiple of the miimon value.\n",
1102 bond->dev->name,
1103 bond->params.updelay * bond->params.miimon);
1104 if(bond->params.downdelay)
1105 printk(KERN_INFO DRV_NAME
1106 ": %s: Note: Updating downdelay (to %d) "
1107 "since it is a multiple of the miimon value.\n",
1108 bond->dev->name,
1109 bond->params.downdelay * bond->params.miimon);
1110 if (bond->params.arp_interval) {
1111 printk(KERN_INFO DRV_NAME
1112 ": %s: MII monitoring cannot be used with "
1113 "ARP monitoring. Disabling ARP monitoring...\n",
1114 bond->dev->name);
1115 bond->params.arp_interval = 0;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -08001116 bond->dev->priv_flags &= ~IFF_MASTER_ARPMON;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001117 if (bond->params.arp_validate) {
1118 bond_unregister_arp(bond);
1119 bond->params.arp_validate =
1120 BOND_ARP_VALIDATE_NONE;
1121 }
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001122 if (delayed_work_pending(&bond->arp_work)) {
1123 cancel_delayed_work(&bond->arp_work);
1124 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001125 }
1126 }
1127
1128 if (bond->dev->flags & IFF_UP) {
1129 /* If the interface is up, we may need to fire off
1130 * the MII timer. If the interface is down, the
1131 * timer will get fired off when the open function
1132 * is called.
1133 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001134 if (!delayed_work_pending(&bond->mii_work)) {
1135 INIT_DELAYED_WORK(&bond->mii_work,
1136 bond_mii_monitor);
1137 queue_delayed_work(bond->wq,
1138 &bond->mii_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001139 }
1140 }
1141 }
1142out:
1143 return ret;
1144}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001145static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001146
1147/*
1148 * Show and set the primary slave. The store function is much
1149 * simpler than bonding_store_slaves function because it only needs to
1150 * handle one interface name.
1151 * The bond must be a mode that supports a primary for this be
1152 * set.
1153 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001154static ssize_t bonding_show_primary(struct device *d,
1155 struct device_attribute *attr,
1156 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001157{
1158 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001159 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001160
1161 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001162 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001163
1164 return count;
1165}
1166
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001167static ssize_t bonding_store_primary(struct device *d,
1168 struct device_attribute *attr,
1169 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001170{
1171 int i;
1172 struct slave *slave;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001173 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001174
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001175 if (!rtnl_trylock())
1176 return restart_syscall();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001177 read_lock(&bond->lock);
1178 write_lock_bh(&bond->curr_slave_lock);
1179
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001180 if (!USES_PRIMARY(bond->params.mode)) {
1181 printk(KERN_INFO DRV_NAME
1182 ": %s: Unable to set primary slave; %s is in mode %d\n",
1183 bond->dev->name, bond->dev->name, bond->params.mode);
1184 } else {
1185 bond_for_each_slave(bond, slave, i) {
1186 if (strnicmp
1187 (slave->dev->name, buf,
1188 strlen(slave->dev->name)) == 0) {
1189 printk(KERN_INFO DRV_NAME
1190 ": %s: Setting %s as primary slave.\n",
1191 bond->dev->name, slave->dev->name);
1192 bond->primary_slave = slave;
1193 bond_select_active_slave(bond);
1194 goto out;
1195 }
1196 }
1197
1198 /* if we got here, then we didn't match the name of any slave */
1199
1200 if (strlen(buf) == 0 || buf[0] == '\n') {
1201 printk(KERN_INFO DRV_NAME
1202 ": %s: Setting primary slave to None.\n",
1203 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001204 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001205 bond_select_active_slave(bond);
1206 } else {
1207 printk(KERN_INFO DRV_NAME
1208 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1209 bond->dev->name, (int)strlen(buf) - 1, buf);
1210 }
1211 }
1212out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001213 write_unlock_bh(&bond->curr_slave_lock);
1214 read_unlock(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001215 rtnl_unlock();
1216
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001217 return count;
1218}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001219static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001220
1221/*
1222 * Show and set the use_carrier flag.
1223 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001224static ssize_t bonding_show_carrier(struct device *d,
1225 struct device_attribute *attr,
1226 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001227{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001228 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001229
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001230 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001231}
1232
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001233static ssize_t bonding_store_carrier(struct device *d,
1234 struct device_attribute *attr,
1235 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001236{
1237 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001238 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001239
1240
1241 if (sscanf(buf, "%d", &new_value) != 1) {
1242 printk(KERN_ERR DRV_NAME
1243 ": %s: no use_carrier value specified.\n",
1244 bond->dev->name);
1245 ret = -EINVAL;
1246 goto out;
1247 }
1248 if ((new_value == 0) || (new_value == 1)) {
1249 bond->params.use_carrier = new_value;
1250 printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
1251 bond->dev->name, new_value);
1252 } else {
1253 printk(KERN_INFO DRV_NAME
1254 ": %s: Ignoring invalid use_carrier value %d.\n",
1255 bond->dev->name, new_value);
1256 }
1257out:
1258 return count;
1259}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001260static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001261
1262
1263/*
1264 * Show and set currently active_slave.
1265 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001266static ssize_t bonding_show_active_slave(struct device *d,
1267 struct device_attribute *attr,
1268 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001269{
1270 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001271 struct bonding *bond = to_bond(d);
Wagner Ferenc16cd0162007-12-06 23:40:29 -08001272 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001273
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001274 read_lock(&bond->curr_slave_lock);
1275 curr = bond->curr_active_slave;
1276 read_unlock(&bond->curr_slave_lock);
1277
1278 if (USES_PRIMARY(bond->params.mode) && curr)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001279 count = sprintf(buf, "%s\n", curr->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001280 return count;
1281}
1282
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001283static ssize_t bonding_store_active_slave(struct device *d,
1284 struct device_attribute *attr,
1285 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001286{
1287 int i;
1288 struct slave *slave;
1289 struct slave *old_active = NULL;
1290 struct slave *new_active = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001291 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001292
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001293 if (!rtnl_trylock())
1294 return restart_syscall();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001295 read_lock(&bond->lock);
1296 write_lock_bh(&bond->curr_slave_lock);
Jay Vosburgh1466a212007-11-06 13:33:28 -08001297
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001298 if (!USES_PRIMARY(bond->params.mode)) {
1299 printk(KERN_INFO DRV_NAME
1300 ": %s: Unable to change active slave; %s is in mode %d\n",
1301 bond->dev->name, bond->dev->name, bond->params.mode);
1302 } else {
1303 bond_for_each_slave(bond, slave, i) {
1304 if (strnicmp
1305 (slave->dev->name, buf,
1306 strlen(slave->dev->name)) == 0) {
1307 old_active = bond->curr_active_slave;
1308 new_active = slave;
Jay Vosburgha50d8de2006-09-22 21:53:25 -07001309 if (new_active == old_active) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001310 /* do nothing */
1311 printk(KERN_INFO DRV_NAME
1312 ": %s: %s is already the current active slave.\n",
1313 bond->dev->name, slave->dev->name);
1314 goto out;
1315 }
1316 else {
1317 if ((new_active) &&
1318 (old_active) &&
1319 (new_active->link == BOND_LINK_UP) &&
1320 IS_UP(new_active->dev)) {
1321 printk(KERN_INFO DRV_NAME
1322 ": %s: Setting %s as active slave.\n",
1323 bond->dev->name, slave->dev->name);
1324 bond_change_active_slave(bond, new_active);
1325 }
1326 else {
1327 printk(KERN_INFO DRV_NAME
1328 ": %s: Could not set %s as active slave; "
1329 "either %s is down or the link is down.\n",
1330 bond->dev->name, slave->dev->name,
1331 slave->dev->name);
1332 }
1333 goto out;
1334 }
1335 }
1336 }
1337
1338 /* if we got here, then we didn't match the name of any slave */
1339
1340 if (strlen(buf) == 0 || buf[0] == '\n') {
1341 printk(KERN_INFO DRV_NAME
1342 ": %s: Setting active slave to None.\n",
1343 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001344 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001345 bond_select_active_slave(bond);
1346 } else {
1347 printk(KERN_INFO DRV_NAME
1348 ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
1349 bond->dev->name, (int)strlen(buf) - 1, buf);
1350 }
1351 }
1352out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001353 write_unlock_bh(&bond->curr_slave_lock);
1354 read_unlock(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001355 rtnl_unlock();
1356
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001357 return count;
1358
1359}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001360static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001361
1362
1363/*
1364 * Show link status of the bond interface.
1365 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001366static ssize_t bonding_show_mii_status(struct device *d,
1367 struct device_attribute *attr,
1368 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001369{
1370 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001371 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001372
1373 read_lock(&bond->curr_slave_lock);
1374 curr = bond->curr_active_slave;
1375 read_unlock(&bond->curr_slave_lock);
1376
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001377 return sprintf(buf, "%s\n", (curr) ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001378}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001379static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001380
1381
1382/*
1383 * Show current 802.3ad aggregator ID.
1384 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001385static ssize_t bonding_show_ad_aggregator(struct device *d,
1386 struct device_attribute *attr,
1387 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001388{
1389 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001390 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001391
1392 if (bond->params.mode == BOND_MODE_8023AD) {
1393 struct ad_info ad_info;
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001394 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.aggregator_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001395 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001396
1397 return count;
1398}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001399static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001400
1401
1402/*
1403 * Show number of active 802.3ad ports.
1404 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001405static ssize_t bonding_show_ad_num_ports(struct device *d,
1406 struct device_attribute *attr,
1407 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001408{
1409 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001410 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001411
1412 if (bond->params.mode == BOND_MODE_8023AD) {
1413 struct ad_info ad_info;
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001414 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0: ad_info.ports);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001415 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001416
1417 return count;
1418}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001419static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001420
1421
1422/*
1423 * Show current 802.3ad actor key.
1424 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001425static ssize_t bonding_show_ad_actor_key(struct device *d,
1426 struct device_attribute *attr,
1427 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001428{
1429 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001430 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001431
1432 if (bond->params.mode == BOND_MODE_8023AD) {
1433 struct ad_info ad_info;
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001434 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.actor_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001435 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001436
1437 return count;
1438}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001439static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001440
1441
1442/*
1443 * Show current 802.3ad partner key.
1444 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001445static ssize_t bonding_show_ad_partner_key(struct device *d,
1446 struct device_attribute *attr,
1447 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001448{
1449 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001450 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001451
1452 if (bond->params.mode == BOND_MODE_8023AD) {
1453 struct ad_info ad_info;
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001454 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 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;
1474 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 }
1477 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001478
1479 return count;
1480}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001481static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001482
1483
1484
1485static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001486 &dev_attr_slaves.attr,
1487 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001488 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001489 &dev_attr_arp_validate.attr,
1490 &dev_attr_arp_interval.attr,
1491 &dev_attr_arp_ip_target.attr,
1492 &dev_attr_downdelay.attr,
1493 &dev_attr_updelay.attr,
1494 &dev_attr_lacp_rate.attr,
Jay Vosburghfd989c82008-11-04 17:51:16 -08001495 &dev_attr_ad_select.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001496 &dev_attr_xmit_hash_policy.attr,
Moni Shoua7893b242008-05-17 21:10:12 -07001497 &dev_attr_num_grat_arp.attr,
Brian Haley305d5522008-11-04 17:51:14 -08001498 &dev_attr_num_unsol_na.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001499 &dev_attr_miimon.attr,
1500 &dev_attr_primary.attr,
1501 &dev_attr_use_carrier.attr,
1502 &dev_attr_active_slave.attr,
1503 &dev_attr_mii_status.attr,
1504 &dev_attr_ad_aggregator.attr,
1505 &dev_attr_ad_num_ports.attr,
1506 &dev_attr_ad_actor_key.attr,
1507 &dev_attr_ad_partner_key.attr,
1508 &dev_attr_ad_partner_mac.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001509 NULL,
1510};
1511
1512static struct attribute_group bonding_group = {
1513 .name = "bonding",
1514 .attrs = per_bond_attrs,
1515};
1516
1517/*
1518 * Initialize sysfs. This sets up the bonding_masters file in
1519 * /sys/class/net.
1520 */
1521int bond_create_sysfs(void)
1522{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001523 int ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001524
Jay Vosburghb8a97872008-06-13 18:12:04 -07001525 ret = netdev_class_create_file(&class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001526 /*
1527 * Permit multiple loads of the module by ignoring failures to
1528 * create the bonding_masters sysfs file. Bonding devices
1529 * created by second or subsequent loads of the module will
1530 * not be listed in, or controllable by, bonding_masters, but
1531 * will have the usual "bonding" sysfs directory.
1532 *
1533 * This is done to preserve backwards compatibility for
1534 * initscripts/sysconfig, which load bonding multiple times to
1535 * configure multiple bonding devices.
1536 */
1537 if (ret == -EEXIST) {
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001538 /* Is someone being kinky and naming a device bonding_master? */
1539 if (__dev_get_by_name(&init_net,
1540 class_attr_bonding_masters.attr.name))
1541 printk(KERN_ERR
1542 "network device named %s already exists in sysfs",
1543 class_attr_bonding_masters.attr.name);
Stephen Hemminger130aa612009-06-11 05:46:04 -07001544 ret = 0;
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001545 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001546
1547 return ret;
1548
1549}
1550
1551/*
1552 * Remove /sys/class/net/bonding_masters.
1553 */
1554void bond_destroy_sysfs(void)
1555{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001556 netdev_class_remove_file(&class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001557}
1558
1559/*
1560 * Initialize sysfs for each bond. This sets up and registers
1561 * the 'bondctl' directory for each individual bond under /sys/class/net.
1562 */
1563int bond_create_sysfs_entry(struct bonding *bond)
1564{
1565 struct net_device *dev = bond->dev;
1566 int err;
1567
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001568 err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001569 if (err) {
1570 printk(KERN_EMERG "eek! didn't create group!\n");
1571 }
1572
1573 if (expected_refcount < 1)
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001574 expected_refcount = atomic_read(&bond->dev->dev.kobj.kref.refcount);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001575
1576 return err;
1577}
1578/*
1579 * Remove sysfs entries for each bond.
1580 */
1581void bond_destroy_sysfs_entry(struct bonding *bond)
1582{
1583 struct net_device *dev = bond->dev;
1584
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001585 sysfs_remove_group(&(dev->dev.kobj), &bonding_group);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001586}
1587