blob: aaf2927b5c385f901693dd1363aaafc0265c0a0c [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
39/* #define BONDING_DEBUG 1 */
40#include "bonding.h"
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070041#define to_dev(obj) container_of(obj,struct device,kobj)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080042#define to_bond(cd) ((struct bonding *)(to_net_dev(cd)->priv))
43
44/*---------------------------- Declarations -------------------------------*/
45
46
47extern struct list_head bond_dev_list;
48extern struct bond_params bonding_defaults;
49extern struct bond_parm_tbl bond_mode_tbl[];
50extern struct bond_parm_tbl bond_lacp_tbl[];
Jay Vosburghfd989c82008-11-04 17:51:16 -080051extern struct bond_parm_tbl ad_select_tbl[];
Mitch Williamsb76cdba2005-11-09 10:36:41 -080052extern struct bond_parm_tbl xmit_hashtype_tbl[];
Jay Vosburghf5b2b962006-09-22 21:54:53 -070053extern struct bond_parm_tbl arp_validate_tbl[];
Jay Vosburgh3915c1e82008-05-17 21:10:14 -070054extern struct bond_parm_tbl fail_over_mac_tbl[];
Mitch Williamsb76cdba2005-11-09 10:36:41 -080055
56static int expected_refcount = -1;
Mitch Williamsb76cdba2005-11-09 10:36:41 -080057/*--------------------------- Data Structures -----------------------------*/
58
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020059/* Bonding sysfs lock. Why can't we just use the subsystem lock?
Mitch Williamsb76cdba2005-11-09 10:36:41 -080060 * Because kobject_register tries to acquire the subsystem lock. If
61 * we already hold the lock (which we would if the user was creating
62 * a new bond through the sysfs interface), we deadlock.
63 * This lock is only needed when deleting a bond - we need to make sure
64 * that we don't collide with an ongoing ioctl.
65 */
66
67struct rw_semaphore bonding_rwsem;
68
69
70
71
72/*------------------------------ Functions --------------------------------*/
73
74/*
75 * "show" function for the bond_masters attribute.
76 * The class parameter is ignored.
77 */
Wagner Ferencb8843662007-12-06 23:40:30 -080078static ssize_t bonding_show_bonds(struct class *cls, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080079{
80 int res = 0;
81 struct bonding *bond;
82
83 down_read(&(bonding_rwsem));
84
85 list_for_each_entry(bond, &bond_dev_list, bond_list) {
86 if (res > (PAGE_SIZE - IFNAMSIZ)) {
87 /* not enough space for another interface name */
88 if ((PAGE_SIZE - res) > 10)
89 res = PAGE_SIZE - 10;
Wagner Ferencb8843662007-12-06 23:40:30 -080090 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -080091 break;
92 }
Wagner Ferencb8843662007-12-06 23:40:30 -080093 res += sprintf(buf + res, "%s ", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080094 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -080095 if (res)
96 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -080097 up_read(&(bonding_rwsem));
98 return res;
99}
100
101/*
102 * "store" function for the bond_masters attribute. This is what
103 * creates and deletes entire bonds.
104 *
105 * The class parameter is ignored.
106 *
107 */
108
109static ssize_t bonding_store_bonds(struct class *cls, const char *buffer, size_t count)
110{
111 char command[IFNAMSIZ + 1] = {0, };
112 char *ifname;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800113 int rv, res = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800114 struct bonding *bond;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800115
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800116 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
117 ifname = command + 1;
118 if ((strlen(command) <= 1) ||
119 !dev_valid_name(ifname))
120 goto err_no_cmd;
121
122 if (command[0] == '+') {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800123 printk(KERN_INFO DRV_NAME
124 ": %s is being created...\n", ifname);
Pavel Emelyanov0dd646f2008-05-17 21:10:09 -0700125 rv = bond_create(ifname, &bonding_defaults);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800126 if (rv) {
127 printk(KERN_INFO DRV_NAME ": Bond creation failed.\n");
128 res = rv;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800129 }
130 goto out;
131 }
132
133 if (command[0] == '-') {
Jay Vosburgh027ea042008-01-17 16:25:02 -0800134 rtnl_lock();
135 down_write(&bonding_rwsem);
136
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700137 list_for_each_entry(bond, &bond_dev_list, bond_list)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800138 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800139 /* check the ref count on the bond's kobject.
140 * If it's > expected, then there's a file open,
141 * and we have to fail.
142 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700143 if (atomic_read(&bond->dev->dev.kobj.kref.refcount)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800144 > expected_refcount){
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800145 printk(KERN_INFO DRV_NAME
146 ": Unable remove bond %s due to open references.\n",
147 ifname);
148 res = -EPERM;
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700149 goto out_unlock;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800150 }
151 printk(KERN_INFO DRV_NAME
152 ": %s is being deleted...\n",
153 bond->dev->name);
Moni Shouad90a1622007-10-09 19:43:43 -0700154 bond_destroy(bond);
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700155 goto out_unlock;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800156 }
157
158 printk(KERN_ERR DRV_NAME
159 ": unable to delete non-existent bond %s\n", ifname);
160 res = -ENODEV;
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700161 goto out_unlock;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800162 }
163
164err_no_cmd:
165 printk(KERN_ERR DRV_NAME
166 ": no command found in bonding_masters. Use +ifname or -ifname.\n");
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700167 return -EPERM;
168
169out_unlock:
170 up_write(&bonding_rwsem);
171 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800172
173 /* Always return either count or an error. If you return 0, you'll
174 * get called forever, which is bad.
175 */
176out:
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800177 return res;
178}
179/* class attribute for bond_masters file. This ends up in /sys/class/net */
180static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
181 bonding_show_bonds, bonding_store_bonds);
182
183int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave)
184{
185 char linkname[IFNAMSIZ+7];
186 int ret = 0;
187
188 /* first, create a link from the slave back to the master */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700189 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800190 "master");
191 if (ret)
192 return ret;
193 /* next, create a link from the master to the slave */
194 sprintf(linkname,"slave_%s",slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700195 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800196 linkname);
197 return ret;
198
199}
200
201void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave)
202{
203 char linkname[IFNAMSIZ+7];
204
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700205 sysfs_remove_link(&(slave->dev.kobj), "master");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800206 sprintf(linkname,"slave_%s",slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700207 sysfs_remove_link(&(master->dev.kobj), linkname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800208}
209
210
211/*
212 * Show the slaves in the current bond.
213 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700214static ssize_t bonding_show_slaves(struct device *d,
215 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800216{
217 struct slave *slave;
218 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700219 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800220
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700221 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800222 bond_for_each_slave(bond, slave, i) {
223 if (res > (PAGE_SIZE - IFNAMSIZ)) {
224 /* not enough space for another interface name */
225 if ((PAGE_SIZE - res) > 10)
226 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800227 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800228 break;
229 }
230 res += sprintf(buf + res, "%s ", slave->dev->name);
231 }
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700232 read_unlock(&bond->lock);
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800233 if (res)
234 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800235 return res;
236}
237
238/*
239 * Set the slaves in the current bond. The bond interface must be
240 * up for this to succeed.
241 * This function is largely the same flow as bonding_update_bonds().
242 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700243static ssize_t bonding_store_slaves(struct device *d,
244 struct device_attribute *attr,
245 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800246{
247 char command[IFNAMSIZ + 1] = { 0, };
248 char *ifname;
249 int i, res, found, ret = count;
Moni Shoua3158bf72007-10-09 19:43:41 -0700250 u32 original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800251 struct slave *slave;
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -0800252 struct net_device *dev = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700253 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800254
255 /* Quick sanity check -- is the bond interface up? */
256 if (!(bond->dev->flags & IFF_UP)) {
Moni Shoua6b1bf092007-10-09 19:43:40 -0700257 printk(KERN_WARNING DRV_NAME
258 ": %s: doing slave updates when interface is down.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800259 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800260 }
261
262 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
263
Jay Vosburgh027ea042008-01-17 16:25:02 -0800264 rtnl_lock();
265 down_write(&(bonding_rwsem));
266
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800267 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
268 ifname = command + 1;
269 if ((strlen(command) <= 1) ||
270 !dev_valid_name(ifname))
271 goto err_no_cmd;
272
273 if (command[0] == '+') {
274
275 /* Got a slave name in ifname. Is it already in the list? */
276 found = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700277 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800278 bond_for_each_slave(bond, slave, i)
279 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
280 printk(KERN_ERR DRV_NAME
281 ": %s: Interface %s is already enslaved!\n",
282 bond->dev->name, ifname);
283 ret = -EPERM;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700284 read_unlock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800285 goto out;
286 }
287
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700288 read_unlock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800289 printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n",
290 bond->dev->name, ifname);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700291 dev = dev_get_by_name(&init_net, ifname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800292 if (!dev) {
293 printk(KERN_INFO DRV_NAME
294 ": %s: Interface %s does not exist!\n",
295 bond->dev->name, ifname);
296 ret = -EPERM;
297 goto out;
298 }
299 else
300 dev_put(dev);
301
302 if (dev->flags & IFF_UP) {
303 printk(KERN_ERR DRV_NAME
304 ": %s: Error: Unable to enslave %s "
305 "because it is already up.\n",
306 bond->dev->name, dev->name);
307 ret = -EPERM;
308 goto out;
309 }
310 /* If this is the first slave, then we need to set
311 the master's hardware address to be the same as the
312 slave's. */
313 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
314 memcpy(bond->dev->dev_addr, dev->dev_addr,
315 dev->addr_len);
316 }
317
318 /* Set the slave's MTU to match the bond */
Moni Shoua3158bf72007-10-09 19:43:41 -0700319 original_mtu = dev->mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800320 if (dev->mtu != bond->dev->mtu) {
321 if (dev->change_mtu) {
322 res = dev->change_mtu(dev,
323 bond->dev->mtu);
324 if (res) {
325 ret = res;
326 goto out;
327 }
328 } else {
329 dev->mtu = bond->dev->mtu;
330 }
331 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800332 res = bond_enslave(bond->dev, dev);
Moni Shoua3158bf72007-10-09 19:43:41 -0700333 bond_for_each_slave(bond, slave, i)
334 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
335 slave->original_mtu = original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800336 if (res) {
337 ret = res;
338 }
339 goto out;
340 }
341
342 if (command[0] == '-') {
343 dev = NULL;
David S. Miller6952d8922008-03-28 16:15:38 -0700344 original_mtu = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800345 bond_for_each_slave(bond, slave, i)
346 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
347 dev = slave->dev;
Moni Shoua3158bf72007-10-09 19:43:41 -0700348 original_mtu = slave->original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800349 break;
350 }
351 if (dev) {
352 printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n",
353 bond->dev->name, dev->name);
Moni Shouad90a1622007-10-09 19:43:43 -0700354 res = bond_release(bond->dev, dev);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800355 if (res) {
356 ret = res;
357 goto out;
358 }
359 /* set the slave MTU to the default */
360 if (dev->change_mtu) {
Moni Shoua3158bf72007-10-09 19:43:41 -0700361 dev->change_mtu(dev, original_mtu);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800362 } else {
Moni Shoua3158bf72007-10-09 19:43:41 -0700363 dev->mtu = original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800364 }
365 }
366 else {
367 printk(KERN_ERR DRV_NAME ": unable to remove non-existent slave %s for bond %s.\n",
368 ifname, bond->dev->name);
369 ret = -ENODEV;
370 }
371 goto out;
372 }
373
374err_no_cmd:
375 printk(KERN_ERR DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
376 ret = -EPERM;
377
378out:
Jay Vosburgh027ea042008-01-17 16:25:02 -0800379 up_write(&(bonding_rwsem));
380 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800381 return ret;
382}
383
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700384static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves, bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800385
386/*
387 * Show and set the bonding mode. The bond interface must be down to
388 * change the mode.
389 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700390static ssize_t bonding_show_mode(struct device *d,
391 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800392{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700393 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800394
395 return sprintf(buf, "%s %d\n",
396 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800397 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800398}
399
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700400static ssize_t bonding_store_mode(struct device *d,
401 struct device_attribute *attr,
402 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800403{
404 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700405 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800406
407 if (bond->dev->flags & IFF_UP) {
408 printk(KERN_ERR DRV_NAME
409 ": unable to update mode of %s because interface is up.\n",
410 bond->dev->name);
411 ret = -EPERM;
412 goto out;
413 }
414
Jay Vosburghece95f72008-01-17 16:25:01 -0800415 new_value = bond_parse_parm(buf, bond_mode_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800416 if (new_value < 0) {
417 printk(KERN_ERR DRV_NAME
418 ": %s: Ignoring invalid mode value %.*s.\n",
419 bond->dev->name,
420 (int)strlen(buf) - 1, buf);
421 ret = -EINVAL;
422 goto out;
423 } else {
Jay Vosburgh8f903c72006-02-21 16:36:44 -0800424 if (bond->params.mode == BOND_MODE_8023AD)
425 bond_unset_master_3ad_flags(bond);
426
427 if (bond->params.mode == BOND_MODE_ALB)
428 bond_unset_master_alb_flags(bond);
429
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800430 bond->params.mode = new_value;
431 bond_set_mode_ops(bond, bond->params.mode);
432 printk(KERN_INFO DRV_NAME ": %s: setting mode to %s (%d).\n",
433 bond->dev->name, bond_mode_tbl[new_value].modename, new_value);
434 }
435out:
436 return ret;
437}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700438static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800439
440/*
441 * Show and set the bonding transmit hash method. The bond interface must be down to
442 * change the xmit hash policy.
443 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700444static ssize_t bonding_show_xmit_hash(struct device *d,
445 struct device_attribute *attr,
446 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800447{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700448 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800449
Wagner Ferenc8e4b9322007-12-06 23:40:32 -0800450 return sprintf(buf, "%s %d\n",
451 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
452 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800453}
454
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700455static ssize_t bonding_store_xmit_hash(struct device *d,
456 struct device_attribute *attr,
457 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800458{
459 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700460 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800461
462 if (bond->dev->flags & IFF_UP) {
463 printk(KERN_ERR DRV_NAME
464 "%s: Interface is up. Unable to update xmit policy.\n",
465 bond->dev->name);
466 ret = -EPERM;
467 goto out;
468 }
469
Jay Vosburghece95f72008-01-17 16:25:01 -0800470 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800471 if (new_value < 0) {
472 printk(KERN_ERR DRV_NAME
473 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
474 bond->dev->name,
475 (int)strlen(buf) - 1, buf);
476 ret = -EINVAL;
477 goto out;
478 } else {
479 bond->params.xmit_policy = new_value;
480 bond_set_mode_ops(bond, bond->params.mode);
481 printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
482 bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value);
483 }
484out:
485 return ret;
486}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700487static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800488
489/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700490 * Show and set arp_validate.
491 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700492static ssize_t bonding_show_arp_validate(struct device *d,
493 struct device_attribute *attr,
494 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700495{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700496 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700497
498 return sprintf(buf, "%s %d\n",
499 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800500 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700501}
502
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700503static ssize_t bonding_store_arp_validate(struct device *d,
504 struct device_attribute *attr,
505 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700506{
507 int new_value;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700508 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700509
Jay Vosburghece95f72008-01-17 16:25:01 -0800510 new_value = bond_parse_parm(buf, arp_validate_tbl);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700511 if (new_value < 0) {
512 printk(KERN_ERR DRV_NAME
513 ": %s: Ignoring invalid arp_validate value %s\n",
514 bond->dev->name, buf);
515 return -EINVAL;
516 }
517 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
518 printk(KERN_ERR DRV_NAME
519 ": %s: arp_validate only supported in active-backup mode.\n",
520 bond->dev->name);
521 return -EINVAL;
522 }
523 printk(KERN_INFO DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
524 bond->dev->name, arp_validate_tbl[new_value].modename,
525 new_value);
526
527 if (!bond->params.arp_validate && new_value) {
528 bond_register_arp(bond);
529 } else if (bond->params.arp_validate && !new_value) {
530 bond_unregister_arp(bond);
531 }
532
533 bond->params.arp_validate = new_value;
534
535 return count;
536}
537
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700538static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate, bonding_store_arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700539
540/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700541 * Show and store fail_over_mac. User only allowed to change the
542 * value when there are no slaves.
543 */
544static ssize_t bonding_show_fail_over_mac(struct device *d, struct device_attribute *attr, char *buf)
545{
546 struct bonding *bond = to_bond(d);
547
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700548 return sprintf(buf, "%s %d\n",
549 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
550 bond->params.fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700551}
552
553static ssize_t bonding_store_fail_over_mac(struct device *d, struct device_attribute *attr, const char *buf, size_t count)
554{
555 int new_value;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700556 struct bonding *bond = to_bond(d);
557
558 if (bond->slave_cnt != 0) {
559 printk(KERN_ERR DRV_NAME
560 ": %s: Can't alter fail_over_mac with slaves in bond.\n",
561 bond->dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700562 return -EPERM;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700563 }
564
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700565 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
566 if (new_value < 0) {
Jay Vosburghdd957c52007-10-09 19:57:24 -0700567 printk(KERN_ERR DRV_NAME
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700568 ": %s: Ignoring invalid fail_over_mac value %s.\n",
569 bond->dev->name, buf);
570 return -EINVAL;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700571 }
572
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700573 bond->params.fail_over_mac = new_value;
574 printk(KERN_INFO DRV_NAME ": %s: Setting fail_over_mac to %s (%d).\n",
575 bond->dev->name, fail_over_mac_tbl[new_value].modename,
576 new_value);
577
578 return count;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700579}
580
581static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR, bonding_show_fail_over_mac, bonding_store_fail_over_mac);
582
583/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800584 * Show and set the arp timer interval. There are two tricky bits
585 * here. First, if ARP monitoring is activated, then we must disable
586 * MII monitoring. Second, if the ARP timer isn't running, we must
587 * start it.
588 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700589static ssize_t bonding_show_arp_interval(struct device *d,
590 struct device_attribute *attr,
591 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800592{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700593 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800594
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800595 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800596}
597
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700598static ssize_t bonding_store_arp_interval(struct device *d,
599 struct device_attribute *attr,
600 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800601{
602 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700603 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800604
605 if (sscanf(buf, "%d", &new_value) != 1) {
606 printk(KERN_ERR DRV_NAME
607 ": %s: no arp_interval value specified.\n",
608 bond->dev->name);
609 ret = -EINVAL;
610 goto out;
611 }
612 if (new_value < 0) {
613 printk(KERN_ERR DRV_NAME
614 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
615 bond->dev->name, new_value, INT_MAX);
616 ret = -EINVAL;
617 goto out;
618 }
619
620 printk(KERN_INFO DRV_NAME
621 ": %s: Setting ARP monitoring interval to %d.\n",
622 bond->dev->name, new_value);
623 bond->params.arp_interval = new_value;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -0800624 if (bond->params.arp_interval)
625 bond->dev->priv_flags |= IFF_MASTER_ARPMON;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800626 if (bond->params.miimon) {
627 printk(KERN_INFO DRV_NAME
628 ": %s: ARP monitoring cannot be used with MII monitoring. "
629 "%s Disabling MII monitoring.\n",
630 bond->dev->name, bond->dev->name);
631 bond->params.miimon = 0;
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700632 if (delayed_work_pending(&bond->mii_work)) {
633 cancel_delayed_work(&bond->mii_work);
634 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800635 }
636 }
637 if (!bond->params.arp_targets[0]) {
638 printk(KERN_INFO DRV_NAME
639 ": %s: ARP monitoring has been set up, "
640 "but no ARP targets have been specified.\n",
641 bond->dev->name);
642 }
643 if (bond->dev->flags & IFF_UP) {
644 /* If the interface is up, we may need to fire off
645 * the ARP timer. If the interface is down, the
646 * timer will get fired off when the open function
647 * is called.
648 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700649 if (!delayed_work_pending(&bond->arp_work)) {
650 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
651 INIT_DELAYED_WORK(&bond->arp_work,
652 bond_activebackup_arp_mon);
653 else
654 INIT_DELAYED_WORK(&bond->arp_work,
655 bond_loadbalance_arp_mon);
656
657 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800658 }
659 }
660
661out:
662 return ret;
663}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700664static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800665
666/*
667 * Show and set the arp targets.
668 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700669static ssize_t bonding_show_arp_targets(struct device *d,
670 struct device_attribute *attr,
671 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800672{
673 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700674 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800675
676 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
677 if (bond->params.arp_targets[i])
Harvey Harrison63779432008-10-31 00:56:00 -0700678 res += sprintf(buf + res, "%pI4 ",
679 &bond->params.arp_targets[i]);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800680 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800681 if (res)
682 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800683 return res;
684}
685
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700686static ssize_t bonding_store_arp_targets(struct device *d,
687 struct device_attribute *attr,
688 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800689{
Al Virod3bb52b2007-08-22 20:06:58 -0400690 __be32 newtarget;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800691 int i = 0, done = 0, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700692 struct bonding *bond = to_bond(d);
Al Virod3bb52b2007-08-22 20:06:58 -0400693 __be32 *targets;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800694
695 targets = bond->params.arp_targets;
696 newtarget = in_aton(buf + 1);
697 /* look for adds */
698 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400699 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800700 printk(KERN_ERR DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700701 ": %s: invalid ARP target %pI4 specified for addition\n",
702 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800703 ret = -EINVAL;
704 goto out;
705 }
706 /* look for an empty slot to put the target in, and check for dupes */
707 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
708 if (targets[i] == newtarget) { /* duplicate */
709 printk(KERN_ERR DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700710 ": %s: ARP target %pI4 is already present\n",
711 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800712 if (done)
713 targets[i] = 0;
714 ret = -EINVAL;
715 goto out;
716 }
717 if (targets[i] == 0 && !done) {
718 printk(KERN_INFO DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700719 ": %s: adding ARP target %pI4.\n",
720 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800721 done = 1;
722 targets[i] = newtarget;
723 }
724 }
725 if (!done) {
726 printk(KERN_ERR DRV_NAME
727 ": %s: ARP target table is full!\n",
728 bond->dev->name);
729 ret = -EINVAL;
730 goto out;
731 }
732
733 }
734 else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400735 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800736 printk(KERN_ERR DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700737 ": %s: invalid ARP target %pI4 specified for removal\n",
738 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800739 ret = -EINVAL;
740 goto out;
741 }
742
743 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
744 if (targets[i] == newtarget) {
745 printk(KERN_INFO DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700746 ": %s: removing ARP target %pI4.\n",
747 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800748 targets[i] = 0;
749 done = 1;
750 }
751 }
752 if (!done) {
753 printk(KERN_INFO DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700754 ": %s: unable to remove nonexistent ARP target %pI4.\n",
755 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800756 ret = -EINVAL;
757 goto out;
758 }
759 }
760 else {
761 printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
762 bond->dev->name);
763 ret = -EPERM;
764 goto out;
765 }
766
767out:
768 return ret;
769}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700770static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800771
772/*
773 * Show and set the up and down delays. These must be multiples of the
774 * MII monitoring value, and are stored internally as the multiplier.
775 * Thus, we must translate to MS for the real world.
776 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700777static ssize_t bonding_show_downdelay(struct device *d,
778 struct device_attribute *attr,
779 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800780{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700781 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800782
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800783 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800784}
785
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700786static ssize_t bonding_store_downdelay(struct device *d,
787 struct device_attribute *attr,
788 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800789{
790 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700791 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800792
793 if (!(bond->params.miimon)) {
794 printk(KERN_ERR DRV_NAME
795 ": %s: Unable to set down delay as MII monitoring is disabled\n",
796 bond->dev->name);
797 ret = -EPERM;
798 goto out;
799 }
800
801 if (sscanf(buf, "%d", &new_value) != 1) {
802 printk(KERN_ERR DRV_NAME
803 ": %s: no down delay value specified.\n",
804 bond->dev->name);
805 ret = -EINVAL;
806 goto out;
807 }
808 if (new_value < 0) {
809 printk(KERN_ERR DRV_NAME
810 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
811 bond->dev->name, new_value, 1, INT_MAX);
812 ret = -EINVAL;
813 goto out;
814 } else {
815 if ((new_value % bond->params.miimon) != 0) {
816 printk(KERN_WARNING DRV_NAME
817 ": %s: Warning: down delay (%d) is not a multiple "
818 "of miimon (%d), delay rounded to %d ms\n",
819 bond->dev->name, new_value, bond->params.miimon,
820 (new_value / bond->params.miimon) *
821 bond->params.miimon);
822 }
823 bond->params.downdelay = new_value / bond->params.miimon;
824 printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n",
825 bond->dev->name, bond->params.downdelay * bond->params.miimon);
826
827 }
828
829out:
830 return ret;
831}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700832static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800833
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700834static ssize_t bonding_show_updelay(struct device *d,
835 struct device_attribute *attr,
836 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800837{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700838 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800839
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800840 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800841
842}
843
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700844static ssize_t bonding_store_updelay(struct device *d,
845 struct device_attribute *attr,
846 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800847{
848 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700849 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800850
851 if (!(bond->params.miimon)) {
852 printk(KERN_ERR DRV_NAME
853 ": %s: Unable to set up delay as MII monitoring is disabled\n",
854 bond->dev->name);
855 ret = -EPERM;
856 goto out;
857 }
858
859 if (sscanf(buf, "%d", &new_value) != 1) {
860 printk(KERN_ERR DRV_NAME
861 ": %s: no up delay value specified.\n",
862 bond->dev->name);
863 ret = -EINVAL;
864 goto out;
865 }
866 if (new_value < 0) {
867 printk(KERN_ERR DRV_NAME
868 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
869 bond->dev->name, new_value, 1, INT_MAX);
870 ret = -EINVAL;
871 goto out;
872 } else {
873 if ((new_value % bond->params.miimon) != 0) {
874 printk(KERN_WARNING DRV_NAME
875 ": %s: Warning: up delay (%d) is not a multiple "
876 "of miimon (%d), updelay rounded to %d ms\n",
877 bond->dev->name, new_value, bond->params.miimon,
878 (new_value / bond->params.miimon) *
879 bond->params.miimon);
880 }
881 bond->params.updelay = new_value / bond->params.miimon;
882 printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n",
883 bond->dev->name, bond->params.updelay * bond->params.miimon);
884
885 }
886
887out:
888 return ret;
889}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700890static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800891
892/*
893 * Show and set the LACP interval. Interface must be down, and the mode
894 * must be set to 802.3ad mode.
895 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700896static ssize_t bonding_show_lacp(struct device *d,
897 struct device_attribute *attr,
898 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800899{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700900 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800901
902 return sprintf(buf, "%s %d\n",
903 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800904 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800905}
906
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700907static ssize_t bonding_store_lacp(struct device *d,
908 struct device_attribute *attr,
909 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800910{
911 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700912 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800913
914 if (bond->dev->flags & IFF_UP) {
915 printk(KERN_ERR DRV_NAME
916 ": %s: Unable to update LACP rate because interface is up.\n",
917 bond->dev->name);
918 ret = -EPERM;
919 goto out;
920 }
921
922 if (bond->params.mode != BOND_MODE_8023AD) {
923 printk(KERN_ERR DRV_NAME
924 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
925 bond->dev->name);
926 ret = -EPERM;
927 goto out;
928 }
929
Jay Vosburghece95f72008-01-17 16:25:01 -0800930 new_value = bond_parse_parm(buf, bond_lacp_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800931
932 if ((new_value == 1) || (new_value == 0)) {
933 bond->params.lacp_fast = new_value;
934 printk(KERN_INFO DRV_NAME
935 ": %s: Setting LACP rate to %s (%d).\n",
936 bond->dev->name, bond_lacp_tbl[new_value].modename, new_value);
937 } else {
938 printk(KERN_ERR DRV_NAME
939 ": %s: Ignoring invalid LACP rate value %.*s.\n",
940 bond->dev->name, (int)strlen(buf) - 1, buf);
941 ret = -EINVAL;
942 }
943out:
944 return ret;
945}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700946static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800947
Jay Vosburghfd989c82008-11-04 17:51:16 -0800948static ssize_t bonding_show_ad_select(struct device *d,
949 struct device_attribute *attr,
950 char *buf)
951{
952 struct bonding *bond = to_bond(d);
953
954 return sprintf(buf, "%s %d\n",
955 ad_select_tbl[bond->params.ad_select].modename,
956 bond->params.ad_select);
957}
958
959
960static ssize_t bonding_store_ad_select(struct device *d,
961 struct device_attribute *attr,
962 const char *buf, size_t count)
963{
964 int new_value, ret = count;
965 struct bonding *bond = to_bond(d);
966
967 if (bond->dev->flags & IFF_UP) {
968 printk(KERN_ERR DRV_NAME
969 ": %s: Unable to update ad_select because interface "
970 "is up.\n", bond->dev->name);
971 ret = -EPERM;
972 goto out;
973 }
974
975 new_value = bond_parse_parm(buf, ad_select_tbl);
976
977 if (new_value != -1) {
978 bond->params.ad_select = new_value;
979 printk(KERN_INFO DRV_NAME
980 ": %s: Setting ad_select to %s (%d).\n",
981 bond->dev->name, ad_select_tbl[new_value].modename,
982 new_value);
983 } else {
984 printk(KERN_ERR DRV_NAME
985 ": %s: Ignoring invalid ad_select value %.*s.\n",
986 bond->dev->name, (int)strlen(buf) - 1, buf);
987 ret = -EINVAL;
988 }
989out:
990 return ret;
991}
992
993static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR, bonding_show_ad_select, bonding_store_ad_select);
994
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800995/*
Moni Shoua7893b242008-05-17 21:10:12 -0700996 * Show and set the number of grat ARP to send after a failover event.
997 */
998static ssize_t bonding_show_n_grat_arp(struct device *d,
999 struct device_attribute *attr,
1000 char *buf)
1001{
1002 struct bonding *bond = to_bond(d);
1003
1004 return sprintf(buf, "%d\n", bond->params.num_grat_arp);
1005}
1006
1007static ssize_t bonding_store_n_grat_arp(struct device *d,
1008 struct device_attribute *attr,
1009 const char *buf, size_t count)
1010{
1011 int new_value, ret = count;
1012 struct bonding *bond = to_bond(d);
1013
1014 if (sscanf(buf, "%d", &new_value) != 1) {
1015 printk(KERN_ERR DRV_NAME
1016 ": %s: no num_grat_arp value specified.\n",
1017 bond->dev->name);
1018 ret = -EINVAL;
1019 goto out;
1020 }
1021 if (new_value < 0 || new_value > 255) {
1022 printk(KERN_ERR DRV_NAME
1023 ": %s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
1024 bond->dev->name, new_value);
1025 ret = -EINVAL;
1026 goto out;
1027 } else {
1028 bond->params.num_grat_arp = new_value;
1029 }
1030out:
1031 return ret;
1032}
1033static 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 -08001034
1035/*
1036 * Show and set the number of unsolicted NA's to send after a failover event.
1037 */
1038static ssize_t bonding_show_n_unsol_na(struct device *d,
1039 struct device_attribute *attr,
1040 char *buf)
1041{
1042 struct bonding *bond = to_bond(d);
1043
1044 return sprintf(buf, "%d\n", bond->params.num_unsol_na);
1045}
1046
1047static ssize_t bonding_store_n_unsol_na(struct device *d,
1048 struct device_attribute *attr,
1049 const char *buf, size_t count)
1050{
1051 int new_value, ret = count;
1052 struct bonding *bond = to_bond(d);
1053
1054 if (sscanf(buf, "%d", &new_value) != 1) {
1055 printk(KERN_ERR DRV_NAME
1056 ": %s: no num_unsol_na value specified.\n",
1057 bond->dev->name);
1058 ret = -EINVAL;
1059 goto out;
1060 }
1061 if (new_value < 0 || new_value > 255) {
1062 printk(KERN_ERR DRV_NAME
1063 ": %s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
1064 bond->dev->name, new_value);
1065 ret = -EINVAL;
1066 goto out;
1067 } else {
1068 bond->params.num_unsol_na = new_value;
1069 }
1070out:
1071 return ret;
1072}
1073static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR, bonding_show_n_unsol_na, bonding_store_n_unsol_na);
1074
Moni Shoua7893b242008-05-17 21:10:12 -07001075/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001076 * Show and set the MII monitor interval. There are two tricky bits
1077 * here. First, if MII monitoring is activated, then we must disable
1078 * ARP monitoring. Second, if the timer isn't running, we must
1079 * start it.
1080 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001081static ssize_t bonding_show_miimon(struct device *d,
1082 struct device_attribute *attr,
1083 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001084{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001085 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001086
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001087 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001088}
1089
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001090static ssize_t bonding_store_miimon(struct device *d,
1091 struct device_attribute *attr,
1092 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001093{
1094 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001095 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001096
1097 if (sscanf(buf, "%d", &new_value) != 1) {
1098 printk(KERN_ERR DRV_NAME
1099 ": %s: no miimon value specified.\n",
1100 bond->dev->name);
1101 ret = -EINVAL;
1102 goto out;
1103 }
1104 if (new_value < 0) {
1105 printk(KERN_ERR DRV_NAME
1106 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1107 bond->dev->name, new_value, 1, INT_MAX);
1108 ret = -EINVAL;
1109 goto out;
1110 } else {
1111 printk(KERN_INFO DRV_NAME
1112 ": %s: Setting MII monitoring interval to %d.\n",
1113 bond->dev->name, new_value);
1114 bond->params.miimon = new_value;
1115 if(bond->params.updelay)
1116 printk(KERN_INFO DRV_NAME
1117 ": %s: Note: Updating updelay (to %d) "
1118 "since it is a multiple of the miimon value.\n",
1119 bond->dev->name,
1120 bond->params.updelay * bond->params.miimon);
1121 if(bond->params.downdelay)
1122 printk(KERN_INFO DRV_NAME
1123 ": %s: Note: Updating downdelay (to %d) "
1124 "since it is a multiple of the miimon value.\n",
1125 bond->dev->name,
1126 bond->params.downdelay * bond->params.miimon);
1127 if (bond->params.arp_interval) {
1128 printk(KERN_INFO DRV_NAME
1129 ": %s: MII monitoring cannot be used with "
1130 "ARP monitoring. Disabling ARP monitoring...\n",
1131 bond->dev->name);
1132 bond->params.arp_interval = 0;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -08001133 bond->dev->priv_flags &= ~IFF_MASTER_ARPMON;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001134 if (bond->params.arp_validate) {
1135 bond_unregister_arp(bond);
1136 bond->params.arp_validate =
1137 BOND_ARP_VALIDATE_NONE;
1138 }
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001139 if (delayed_work_pending(&bond->arp_work)) {
1140 cancel_delayed_work(&bond->arp_work);
1141 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001142 }
1143 }
1144
1145 if (bond->dev->flags & IFF_UP) {
1146 /* If the interface is up, we may need to fire off
1147 * the MII timer. If the interface is down, the
1148 * timer will get fired off when the open function
1149 * is called.
1150 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001151 if (!delayed_work_pending(&bond->mii_work)) {
1152 INIT_DELAYED_WORK(&bond->mii_work,
1153 bond_mii_monitor);
1154 queue_delayed_work(bond->wq,
1155 &bond->mii_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001156 }
1157 }
1158 }
1159out:
1160 return ret;
1161}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001162static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001163
1164/*
1165 * Show and set the primary slave. The store function is much
1166 * simpler than bonding_store_slaves function because it only needs to
1167 * handle one interface name.
1168 * The bond must be a mode that supports a primary for this be
1169 * set.
1170 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001171static ssize_t bonding_show_primary(struct device *d,
1172 struct device_attribute *attr,
1173 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001174{
1175 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001176 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001177
1178 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001179 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001180
1181 return count;
1182}
1183
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001184static ssize_t bonding_store_primary(struct device *d,
1185 struct device_attribute *attr,
1186 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001187{
1188 int i;
1189 struct slave *slave;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001190 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001191
Jay Vosburghe934dd72008-01-17 16:24:57 -08001192 rtnl_lock();
1193 read_lock(&bond->lock);
1194 write_lock_bh(&bond->curr_slave_lock);
1195
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001196 if (!USES_PRIMARY(bond->params.mode)) {
1197 printk(KERN_INFO DRV_NAME
1198 ": %s: Unable to set primary slave; %s is in mode %d\n",
1199 bond->dev->name, bond->dev->name, bond->params.mode);
1200 } else {
1201 bond_for_each_slave(bond, slave, i) {
1202 if (strnicmp
1203 (slave->dev->name, buf,
1204 strlen(slave->dev->name)) == 0) {
1205 printk(KERN_INFO DRV_NAME
1206 ": %s: Setting %s as primary slave.\n",
1207 bond->dev->name, slave->dev->name);
1208 bond->primary_slave = slave;
1209 bond_select_active_slave(bond);
1210 goto out;
1211 }
1212 }
1213
1214 /* if we got here, then we didn't match the name of any slave */
1215
1216 if (strlen(buf) == 0 || buf[0] == '\n') {
1217 printk(KERN_INFO DRV_NAME
1218 ": %s: Setting primary slave to None.\n",
1219 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001220 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001221 bond_select_active_slave(bond);
1222 } else {
1223 printk(KERN_INFO DRV_NAME
1224 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1225 bond->dev->name, (int)strlen(buf) - 1, buf);
1226 }
1227 }
1228out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001229 write_unlock_bh(&bond->curr_slave_lock);
1230 read_unlock(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001231 rtnl_unlock();
1232
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001233 return count;
1234}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001235static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001236
1237/*
1238 * Show and set the use_carrier flag.
1239 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001240static ssize_t bonding_show_carrier(struct device *d,
1241 struct device_attribute *attr,
1242 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001243{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001244 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001245
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001246 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001247}
1248
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001249static ssize_t bonding_store_carrier(struct device *d,
1250 struct device_attribute *attr,
1251 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001252{
1253 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001254 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001255
1256
1257 if (sscanf(buf, "%d", &new_value) != 1) {
1258 printk(KERN_ERR DRV_NAME
1259 ": %s: no use_carrier value specified.\n",
1260 bond->dev->name);
1261 ret = -EINVAL;
1262 goto out;
1263 }
1264 if ((new_value == 0) || (new_value == 1)) {
1265 bond->params.use_carrier = new_value;
1266 printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
1267 bond->dev->name, new_value);
1268 } else {
1269 printk(KERN_INFO DRV_NAME
1270 ": %s: Ignoring invalid use_carrier value %d.\n",
1271 bond->dev->name, new_value);
1272 }
1273out:
1274 return count;
1275}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001276static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001277
1278
1279/*
1280 * Show and set currently active_slave.
1281 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001282static ssize_t bonding_show_active_slave(struct device *d,
1283 struct device_attribute *attr,
1284 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001285{
1286 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001287 struct bonding *bond = to_bond(d);
Wagner Ferenc16cd0162007-12-06 23:40:29 -08001288 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001289
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001290 read_lock(&bond->curr_slave_lock);
1291 curr = bond->curr_active_slave;
1292 read_unlock(&bond->curr_slave_lock);
1293
1294 if (USES_PRIMARY(bond->params.mode) && curr)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001295 count = sprintf(buf, "%s\n", curr->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001296 return count;
1297}
1298
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001299static ssize_t bonding_store_active_slave(struct device *d,
1300 struct device_attribute *attr,
1301 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001302{
1303 int i;
1304 struct slave *slave;
1305 struct slave *old_active = NULL;
1306 struct slave *new_active = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001307 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001308
Jay Vosburgh1466a212007-11-06 13:33:28 -08001309 rtnl_lock();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001310 read_lock(&bond->lock);
1311 write_lock_bh(&bond->curr_slave_lock);
Jay Vosburgh1466a212007-11-06 13:33:28 -08001312
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001313 if (!USES_PRIMARY(bond->params.mode)) {
1314 printk(KERN_INFO DRV_NAME
1315 ": %s: Unable to change active slave; %s is in mode %d\n",
1316 bond->dev->name, bond->dev->name, bond->params.mode);
1317 } else {
1318 bond_for_each_slave(bond, slave, i) {
1319 if (strnicmp
1320 (slave->dev->name, buf,
1321 strlen(slave->dev->name)) == 0) {
1322 old_active = bond->curr_active_slave;
1323 new_active = slave;
Jay Vosburgha50d8de2006-09-22 21:53:25 -07001324 if (new_active == old_active) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001325 /* do nothing */
1326 printk(KERN_INFO DRV_NAME
1327 ": %s: %s is already the current active slave.\n",
1328 bond->dev->name, slave->dev->name);
1329 goto out;
1330 }
1331 else {
1332 if ((new_active) &&
1333 (old_active) &&
1334 (new_active->link == BOND_LINK_UP) &&
1335 IS_UP(new_active->dev)) {
1336 printk(KERN_INFO DRV_NAME
1337 ": %s: Setting %s as active slave.\n",
1338 bond->dev->name, slave->dev->name);
1339 bond_change_active_slave(bond, new_active);
1340 }
1341 else {
1342 printk(KERN_INFO DRV_NAME
1343 ": %s: Could not set %s as active slave; "
1344 "either %s is down or the link is down.\n",
1345 bond->dev->name, slave->dev->name,
1346 slave->dev->name);
1347 }
1348 goto out;
1349 }
1350 }
1351 }
1352
1353 /* if we got here, then we didn't match the name of any slave */
1354
1355 if (strlen(buf) == 0 || buf[0] == '\n') {
1356 printk(KERN_INFO DRV_NAME
1357 ": %s: Setting active slave to None.\n",
1358 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001359 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001360 bond_select_active_slave(bond);
1361 } else {
1362 printk(KERN_INFO DRV_NAME
1363 ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
1364 bond->dev->name, (int)strlen(buf) - 1, buf);
1365 }
1366 }
1367out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001368 write_unlock_bh(&bond->curr_slave_lock);
1369 read_unlock(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001370 rtnl_unlock();
1371
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001372 return count;
1373
1374}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001375static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001376
1377
1378/*
1379 * Show link status of the bond interface.
1380 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001381static ssize_t bonding_show_mii_status(struct device *d,
1382 struct device_attribute *attr,
1383 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001384{
1385 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001386 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001387
1388 read_lock(&bond->curr_slave_lock);
1389 curr = bond->curr_active_slave;
1390 read_unlock(&bond->curr_slave_lock);
1391
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001392 return sprintf(buf, "%s\n", (curr) ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001393}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001394static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001395
1396
1397/*
1398 * Show current 802.3ad aggregator ID.
1399 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001400static ssize_t bonding_show_ad_aggregator(struct device *d,
1401 struct device_attribute *attr,
1402 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001403{
1404 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001405 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001406
1407 if (bond->params.mode == BOND_MODE_8023AD) {
1408 struct ad_info ad_info;
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001409 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 -08001410 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001411
1412 return count;
1413}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001414static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001415
1416
1417/*
1418 * Show number of active 802.3ad ports.
1419 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001420static ssize_t bonding_show_ad_num_ports(struct device *d,
1421 struct device_attribute *attr,
1422 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001423{
1424 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001425 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001426
1427 if (bond->params.mode == BOND_MODE_8023AD) {
1428 struct ad_info ad_info;
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001429 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 -08001430 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001431
1432 return count;
1433}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001434static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001435
1436
1437/*
1438 * Show current 802.3ad actor key.
1439 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001440static ssize_t bonding_show_ad_actor_key(struct device *d,
1441 struct device_attribute *attr,
1442 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001443{
1444 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001445 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001446
1447 if (bond->params.mode == BOND_MODE_8023AD) {
1448 struct ad_info ad_info;
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001449 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 -08001450 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001451
1452 return count;
1453}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001454static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001455
1456
1457/*
1458 * Show current 802.3ad partner key.
1459 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001460static ssize_t bonding_show_ad_partner_key(struct device *d,
1461 struct device_attribute *attr,
1462 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001463{
1464 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001465 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001466
1467 if (bond->params.mode == BOND_MODE_8023AD) {
1468 struct ad_info ad_info;
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001469 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 -08001470 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001471
1472 return count;
1473}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001474static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001475
1476
1477/*
1478 * Show current 802.3ad partner mac.
1479 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001480static ssize_t bonding_show_ad_partner_mac(struct device *d,
1481 struct device_attribute *attr,
1482 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001483{
1484 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001485 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001486
1487 if (bond->params.mode == BOND_MODE_8023AD) {
1488 struct ad_info ad_info;
1489 if (!bond_3ad_get_active_agg_info(bond, &ad_info)) {
Johannes Berge1749612008-10-27 15:59:26 -07001490 count = sprintf(buf, "%pM\n", ad_info.partner_system);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001491 }
1492 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001493
1494 return count;
1495}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001496static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001497
1498
1499
1500static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001501 &dev_attr_slaves.attr,
1502 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001503 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001504 &dev_attr_arp_validate.attr,
1505 &dev_attr_arp_interval.attr,
1506 &dev_attr_arp_ip_target.attr,
1507 &dev_attr_downdelay.attr,
1508 &dev_attr_updelay.attr,
1509 &dev_attr_lacp_rate.attr,
Jay Vosburghfd989c82008-11-04 17:51:16 -08001510 &dev_attr_ad_select.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001511 &dev_attr_xmit_hash_policy.attr,
Moni Shoua7893b242008-05-17 21:10:12 -07001512 &dev_attr_num_grat_arp.attr,
Brian Haley305d5522008-11-04 17:51:14 -08001513 &dev_attr_num_unsol_na.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001514 &dev_attr_miimon.attr,
1515 &dev_attr_primary.attr,
1516 &dev_attr_use_carrier.attr,
1517 &dev_attr_active_slave.attr,
1518 &dev_attr_mii_status.attr,
1519 &dev_attr_ad_aggregator.attr,
1520 &dev_attr_ad_num_ports.attr,
1521 &dev_attr_ad_actor_key.attr,
1522 &dev_attr_ad_partner_key.attr,
1523 &dev_attr_ad_partner_mac.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001524 NULL,
1525};
1526
1527static struct attribute_group bonding_group = {
1528 .name = "bonding",
1529 .attrs = per_bond_attrs,
1530};
1531
1532/*
1533 * Initialize sysfs. This sets up the bonding_masters file in
1534 * /sys/class/net.
1535 */
1536int bond_create_sysfs(void)
1537{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001538 int ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001539
Jay Vosburghb8a97872008-06-13 18:12:04 -07001540 ret = netdev_class_create_file(&class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001541 /*
1542 * Permit multiple loads of the module by ignoring failures to
1543 * create the bonding_masters sysfs file. Bonding devices
1544 * created by second or subsequent loads of the module will
1545 * not be listed in, or controllable by, bonding_masters, but
1546 * will have the usual "bonding" sysfs directory.
1547 *
1548 * This is done to preserve backwards compatibility for
1549 * initscripts/sysconfig, which load bonding multiple times to
1550 * configure multiple bonding devices.
1551 */
1552 if (ret == -EEXIST) {
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001553 /* Is someone being kinky and naming a device bonding_master? */
1554 if (__dev_get_by_name(&init_net,
1555 class_attr_bonding_masters.attr.name))
1556 printk(KERN_ERR
1557 "network device named %s already exists in sysfs",
1558 class_attr_bonding_masters.attr.name);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001559 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001560
1561 return ret;
1562
1563}
1564
1565/*
1566 * Remove /sys/class/net/bonding_masters.
1567 */
1568void bond_destroy_sysfs(void)
1569{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001570 netdev_class_remove_file(&class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001571}
1572
1573/*
1574 * Initialize sysfs for each bond. This sets up and registers
1575 * the 'bondctl' directory for each individual bond under /sys/class/net.
1576 */
1577int bond_create_sysfs_entry(struct bonding *bond)
1578{
1579 struct net_device *dev = bond->dev;
1580 int err;
1581
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001582 err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001583 if (err) {
1584 printk(KERN_EMERG "eek! didn't create group!\n");
1585 }
1586
1587 if (expected_refcount < 1)
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001588 expected_refcount = atomic_read(&bond->dev->dev.kobj.kref.refcount);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001589
1590 return err;
1591}
1592/*
1593 * Remove sysfs entries for each bond.
1594 */
1595void bond_destroy_sysfs_entry(struct bonding *bond)
1596{
1597 struct net_device *dev = bond->dev;
1598
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001599 sysfs_remove_group(&(dev->dev.kobj), &bonding_group);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001600}
1601