blob: a3f1b4afb40aab7e0d1bb5ce10c63e38fb6743a7 [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[];
51extern struct bond_parm_tbl xmit_hashtype_tbl[];
Jay Vosburghf5b2b962006-09-22 21:54:53 -070052extern struct bond_parm_tbl arp_validate_tbl[];
Mitch Williamsb76cdba2005-11-09 10:36:41 -080053
54static int expected_refcount = -1;
55static struct class *netdev_class;
56/*--------------------------- Data Structures -----------------------------*/
57
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020058/* Bonding sysfs lock. Why can't we just use the subsystem lock?
Mitch Williamsb76cdba2005-11-09 10:36:41 -080059 * Because kobject_register tries to acquire the subsystem lock. If
60 * we already hold the lock (which we would if the user was creating
61 * a new bond through the sysfs interface), we deadlock.
62 * This lock is only needed when deleting a bond - we need to make sure
63 * that we don't collide with an ongoing ioctl.
64 */
65
66struct rw_semaphore bonding_rwsem;
67
68
69
70
71/*------------------------------ Functions --------------------------------*/
72
73/*
74 * "show" function for the bond_masters attribute.
75 * The class parameter is ignored.
76 */
77static ssize_t bonding_show_bonds(struct class *cls, char *buffer)
78{
79 int res = 0;
80 struct bonding *bond;
81
82 down_read(&(bonding_rwsem));
83
84 list_for_each_entry(bond, &bond_dev_list, bond_list) {
85 if (res > (PAGE_SIZE - IFNAMSIZ)) {
86 /* not enough space for another interface name */
87 if ((PAGE_SIZE - res) > 10)
88 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -080089 res += sprintf(buffer + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -080090 break;
91 }
92 res += sprintf(buffer + res, "%s ",
93 bond->dev->name);
94 }
Wagner Ferenc7bd46502007-12-06 23:40:28 -080095 if (res) buffer[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -080096 up_read(&(bonding_rwsem));
97 return res;
98}
99
100/*
101 * "store" function for the bond_masters attribute. This is what
102 * creates and deletes entire bonds.
103 *
104 * The class parameter is ignored.
105 *
106 */
107
108static ssize_t bonding_store_bonds(struct class *cls, const char *buffer, size_t count)
109{
110 char command[IFNAMSIZ + 1] = {0, };
111 char *ifname;
112 int res = count;
113 struct bonding *bond;
114 struct bonding *nxt;
115
116 down_write(&(bonding_rwsem));
117 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
118 ifname = command + 1;
119 if ((strlen(command) <= 1) ||
120 !dev_valid_name(ifname))
121 goto err_no_cmd;
122
123 if (command[0] == '+') {
124
125 /* Check to see if the bond already exists. */
126 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
127 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
128 printk(KERN_ERR DRV_NAME
129 ": cannot add bond %s; it already exists\n",
130 ifname);
131 res = -EPERM;
132 goto out;
133 }
134
135 printk(KERN_INFO DRV_NAME
136 ": %s is being created...\n", ifname);
137 if (bond_create(ifname, &bonding_defaults, &bond)) {
138 printk(KERN_INFO DRV_NAME
139 ": %s interface already exists. Bond creation failed.\n",
140 ifname);
141 res = -EPERM;
142 }
143 goto out;
144 }
145
146 if (command[0] == '-') {
147 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
148 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
149 rtnl_lock();
150 /* check the ref count on the bond's kobject.
151 * If it's > expected, then there's a file open,
152 * and we have to fail.
153 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700154 if (atomic_read(&bond->dev->dev.kobj.kref.refcount)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800155 > expected_refcount){
156 rtnl_unlock();
157 printk(KERN_INFO DRV_NAME
158 ": Unable remove bond %s due to open references.\n",
159 ifname);
160 res = -EPERM;
161 goto out;
162 }
163 printk(KERN_INFO DRV_NAME
164 ": %s is being deleted...\n",
165 bond->dev->name);
Moni Shouad90a1622007-10-09 19:43:43 -0700166 bond_destroy(bond);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800167 rtnl_unlock();
168 goto out;
169 }
170
171 printk(KERN_ERR DRV_NAME
172 ": unable to delete non-existent bond %s\n", ifname);
173 res = -ENODEV;
174 goto out;
175 }
176
177err_no_cmd:
178 printk(KERN_ERR DRV_NAME
179 ": no command found in bonding_masters. Use +ifname or -ifname.\n");
180 res = -EPERM;
181
182 /* Always return either count or an error. If you return 0, you'll
183 * get called forever, which is bad.
184 */
185out:
186 up_write(&(bonding_rwsem));
187 return res;
188}
189/* class attribute for bond_masters file. This ends up in /sys/class/net */
190static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
191 bonding_show_bonds, bonding_store_bonds);
192
193int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave)
194{
195 char linkname[IFNAMSIZ+7];
196 int ret = 0;
197
198 /* first, create a link from the slave back to the master */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700199 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800200 "master");
201 if (ret)
202 return ret;
203 /* next, create a link from the master to the slave */
204 sprintf(linkname,"slave_%s",slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700205 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800206 linkname);
207 return ret;
208
209}
210
211void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave)
212{
213 char linkname[IFNAMSIZ+7];
214
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700215 sysfs_remove_link(&(slave->dev.kobj), "master");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800216 sprintf(linkname,"slave_%s",slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700217 sysfs_remove_link(&(master->dev.kobj), linkname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800218}
219
220
221/*
222 * Show the slaves in the current bond.
223 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700224static ssize_t bonding_show_slaves(struct device *d,
225 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800226{
227 struct slave *slave;
228 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700229 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800230
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700231 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800232 bond_for_each_slave(bond, slave, i) {
233 if (res > (PAGE_SIZE - IFNAMSIZ)) {
234 /* not enough space for another interface name */
235 if ((PAGE_SIZE - res) > 10)
236 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800237 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800238 break;
239 }
240 res += sprintf(buf + res, "%s ", slave->dev->name);
241 }
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700242 read_unlock(&bond->lock);
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800243 if (res) buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800244 return res;
245}
246
247/*
248 * Set the slaves in the current bond. The bond interface must be
249 * up for this to succeed.
250 * This function is largely the same flow as bonding_update_bonds().
251 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700252static ssize_t bonding_store_slaves(struct device *d,
253 struct device_attribute *attr,
254 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800255{
256 char command[IFNAMSIZ + 1] = { 0, };
257 char *ifname;
258 int i, res, found, ret = count;
Moni Shoua3158bf72007-10-09 19:43:41 -0700259 u32 original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800260 struct slave *slave;
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -0800261 struct net_device *dev = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700262 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800263
264 /* Quick sanity check -- is the bond interface up? */
265 if (!(bond->dev->flags & IFF_UP)) {
Moni Shoua6b1bf092007-10-09 19:43:40 -0700266 printk(KERN_WARNING DRV_NAME
267 ": %s: doing slave updates when interface is down.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800268 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800269 }
270
271 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
272
273 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
274 ifname = command + 1;
275 if ((strlen(command) <= 1) ||
276 !dev_valid_name(ifname))
277 goto err_no_cmd;
278
279 if (command[0] == '+') {
280
281 /* Got a slave name in ifname. Is it already in the list? */
282 found = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700283 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800284 bond_for_each_slave(bond, slave, i)
285 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
286 printk(KERN_ERR DRV_NAME
287 ": %s: Interface %s is already enslaved!\n",
288 bond->dev->name, ifname);
289 ret = -EPERM;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700290 read_unlock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800291 goto out;
292 }
293
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700294 read_unlock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800295 printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n",
296 bond->dev->name, ifname);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700297 dev = dev_get_by_name(&init_net, ifname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800298 if (!dev) {
299 printk(KERN_INFO DRV_NAME
300 ": %s: Interface %s does not exist!\n",
301 bond->dev->name, ifname);
302 ret = -EPERM;
303 goto out;
304 }
305 else
306 dev_put(dev);
307
308 if (dev->flags & IFF_UP) {
309 printk(KERN_ERR DRV_NAME
310 ": %s: Error: Unable to enslave %s "
311 "because it is already up.\n",
312 bond->dev->name, dev->name);
313 ret = -EPERM;
314 goto out;
315 }
316 /* If this is the first slave, then we need to set
317 the master's hardware address to be the same as the
318 slave's. */
319 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
320 memcpy(bond->dev->dev_addr, dev->dev_addr,
321 dev->addr_len);
322 }
323
324 /* Set the slave's MTU to match the bond */
Moni Shoua3158bf72007-10-09 19:43:41 -0700325 original_mtu = dev->mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800326 if (dev->mtu != bond->dev->mtu) {
327 if (dev->change_mtu) {
328 res = dev->change_mtu(dev,
329 bond->dev->mtu);
330 if (res) {
331 ret = res;
332 goto out;
333 }
334 } else {
335 dev->mtu = bond->dev->mtu;
336 }
337 }
338 rtnl_lock();
339 res = bond_enslave(bond->dev, dev);
Moni Shoua3158bf72007-10-09 19:43:41 -0700340 bond_for_each_slave(bond, slave, i)
341 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
342 slave->original_mtu = original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800343 rtnl_unlock();
344 if (res) {
345 ret = res;
346 }
347 goto out;
348 }
349
350 if (command[0] == '-') {
351 dev = NULL;
352 bond_for_each_slave(bond, slave, i)
353 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
354 dev = slave->dev;
Moni Shoua3158bf72007-10-09 19:43:41 -0700355 original_mtu = slave->original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800356 break;
357 }
358 if (dev) {
359 printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n",
360 bond->dev->name, dev->name);
361 rtnl_lock();
Moni Shouad90a1622007-10-09 19:43:43 -0700362 if (bond->setup_by_slave)
363 res = bond_release_and_destroy(bond->dev, dev);
364 else
365 res = bond_release(bond->dev, dev);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800366 rtnl_unlock();
367 if (res) {
368 ret = res;
369 goto out;
370 }
371 /* set the slave MTU to the default */
372 if (dev->change_mtu) {
Moni Shoua3158bf72007-10-09 19:43:41 -0700373 dev->change_mtu(dev, original_mtu);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800374 } else {
Moni Shoua3158bf72007-10-09 19:43:41 -0700375 dev->mtu = original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800376 }
377 }
378 else {
379 printk(KERN_ERR DRV_NAME ": unable to remove non-existent slave %s for bond %s.\n",
380 ifname, bond->dev->name);
381 ret = -ENODEV;
382 }
383 goto out;
384 }
385
386err_no_cmd:
387 printk(KERN_ERR DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
388 ret = -EPERM;
389
390out:
391 return ret;
392}
393
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700394static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves, bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800395
396/*
397 * Show and set the bonding mode. The bond interface must be down to
398 * change the mode.
399 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700400static ssize_t bonding_show_mode(struct device *d,
401 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800402{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700403 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800404
405 return sprintf(buf, "%s %d\n",
406 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800407 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800408}
409
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700410static ssize_t bonding_store_mode(struct device *d,
411 struct device_attribute *attr,
412 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800413{
414 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700415 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800416
417 if (bond->dev->flags & IFF_UP) {
418 printk(KERN_ERR DRV_NAME
419 ": unable to update mode of %s because interface is up.\n",
420 bond->dev->name);
421 ret = -EPERM;
422 goto out;
423 }
424
425 new_value = bond_parse_parm((char *)buf, bond_mode_tbl);
426 if (new_value < 0) {
427 printk(KERN_ERR DRV_NAME
428 ": %s: Ignoring invalid mode value %.*s.\n",
429 bond->dev->name,
430 (int)strlen(buf) - 1, buf);
431 ret = -EINVAL;
432 goto out;
433 } else {
Jay Vosburgh8f903c72006-02-21 16:36:44 -0800434 if (bond->params.mode == BOND_MODE_8023AD)
435 bond_unset_master_3ad_flags(bond);
436
437 if (bond->params.mode == BOND_MODE_ALB)
438 bond_unset_master_alb_flags(bond);
439
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800440 bond->params.mode = new_value;
441 bond_set_mode_ops(bond, bond->params.mode);
442 printk(KERN_INFO DRV_NAME ": %s: setting mode to %s (%d).\n",
443 bond->dev->name, bond_mode_tbl[new_value].modename, new_value);
444 }
445out:
446 return ret;
447}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700448static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800449
450/*
451 * Show and set the bonding transmit hash method. The bond interface must be down to
452 * change the xmit hash policy.
453 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700454static ssize_t bonding_show_xmit_hash(struct device *d,
455 struct device_attribute *attr,
456 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800457{
458 int count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700459 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800460
461 if ((bond->params.mode != BOND_MODE_XOR) &&
462 (bond->params.mode != BOND_MODE_8023AD)) {
463 // Not Applicable
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800464 count = sprintf(buf, "NA\n");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800465 } else {
466 count = sprintf(buf, "%s %d\n",
467 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800468 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800469 }
470
471 return count;
472}
473
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700474static ssize_t bonding_store_xmit_hash(struct device *d,
475 struct device_attribute *attr,
476 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800477{
478 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700479 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800480
481 if (bond->dev->flags & IFF_UP) {
482 printk(KERN_ERR DRV_NAME
483 "%s: Interface is up. Unable to update xmit policy.\n",
484 bond->dev->name);
485 ret = -EPERM;
486 goto out;
487 }
488
489 if ((bond->params.mode != BOND_MODE_XOR) &&
490 (bond->params.mode != BOND_MODE_8023AD)) {
491 printk(KERN_ERR DRV_NAME
492 "%s: Transmit hash policy is irrelevant in this mode.\n",
493 bond->dev->name);
494 ret = -EPERM;
495 goto out;
496 }
497
498 new_value = bond_parse_parm((char *)buf, xmit_hashtype_tbl);
499 if (new_value < 0) {
500 printk(KERN_ERR DRV_NAME
501 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
502 bond->dev->name,
503 (int)strlen(buf) - 1, buf);
504 ret = -EINVAL;
505 goto out;
506 } else {
507 bond->params.xmit_policy = new_value;
508 bond_set_mode_ops(bond, bond->params.mode);
509 printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
510 bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value);
511 }
512out:
513 return ret;
514}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700515static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800516
517/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700518 * Show and set arp_validate.
519 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700520static ssize_t bonding_show_arp_validate(struct device *d,
521 struct device_attribute *attr,
522 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700523{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700524 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700525
526 return sprintf(buf, "%s %d\n",
527 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800528 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700529}
530
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700531static ssize_t bonding_store_arp_validate(struct device *d,
532 struct device_attribute *attr,
533 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700534{
535 int new_value;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700536 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700537
538 new_value = bond_parse_parm((char *)buf, arp_validate_tbl);
539 if (new_value < 0) {
540 printk(KERN_ERR DRV_NAME
541 ": %s: Ignoring invalid arp_validate value %s\n",
542 bond->dev->name, buf);
543 return -EINVAL;
544 }
545 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
546 printk(KERN_ERR DRV_NAME
547 ": %s: arp_validate only supported in active-backup mode.\n",
548 bond->dev->name);
549 return -EINVAL;
550 }
551 printk(KERN_INFO DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
552 bond->dev->name, arp_validate_tbl[new_value].modename,
553 new_value);
554
555 if (!bond->params.arp_validate && new_value) {
556 bond_register_arp(bond);
557 } else if (bond->params.arp_validate && !new_value) {
558 bond_unregister_arp(bond);
559 }
560
561 bond->params.arp_validate = new_value;
562
563 return count;
564}
565
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700566static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate, bonding_store_arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700567
568/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700569 * Show and store fail_over_mac. User only allowed to change the
570 * value when there are no slaves.
571 */
572static ssize_t bonding_show_fail_over_mac(struct device *d, struct device_attribute *attr, char *buf)
573{
574 struct bonding *bond = to_bond(d);
575
576 return sprintf(buf, "%d\n", bond->params.fail_over_mac) + 1;
577}
578
579static ssize_t bonding_store_fail_over_mac(struct device *d, struct device_attribute *attr, const char *buf, size_t count)
580{
581 int new_value;
582 int ret = count;
583 struct bonding *bond = to_bond(d);
584
585 if (bond->slave_cnt != 0) {
586 printk(KERN_ERR DRV_NAME
587 ": %s: Can't alter fail_over_mac with slaves in bond.\n",
588 bond->dev->name);
589 ret = -EPERM;
590 goto out;
591 }
592
593 if (sscanf(buf, "%d", &new_value) != 1) {
594 printk(KERN_ERR DRV_NAME
595 ": %s: no fail_over_mac value specified.\n",
596 bond->dev->name);
597 ret = -EINVAL;
598 goto out;
599 }
600
601 if ((new_value == 0) || (new_value == 1)) {
602 bond->params.fail_over_mac = new_value;
603 printk(KERN_INFO DRV_NAME ": %s: Setting fail_over_mac to %d.\n",
604 bond->dev->name, new_value);
605 } else {
606 printk(KERN_INFO DRV_NAME
607 ": %s: Ignoring invalid fail_over_mac value %d.\n",
608 bond->dev->name, new_value);
609 }
610out:
611 return ret;
612}
613
614static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR, bonding_show_fail_over_mac, bonding_store_fail_over_mac);
615
616/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800617 * Show and set the arp timer interval. There are two tricky bits
618 * here. First, if ARP monitoring is activated, then we must disable
619 * MII monitoring. Second, if the ARP timer isn't running, we must
620 * start it.
621 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700622static ssize_t bonding_show_arp_interval(struct device *d,
623 struct device_attribute *attr,
624 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800625{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700626 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800627
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800628 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800629}
630
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700631static ssize_t bonding_store_arp_interval(struct device *d,
632 struct device_attribute *attr,
633 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800634{
635 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700636 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800637
638 if (sscanf(buf, "%d", &new_value) != 1) {
639 printk(KERN_ERR DRV_NAME
640 ": %s: no arp_interval value specified.\n",
641 bond->dev->name);
642 ret = -EINVAL;
643 goto out;
644 }
645 if (new_value < 0) {
646 printk(KERN_ERR DRV_NAME
647 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
648 bond->dev->name, new_value, INT_MAX);
649 ret = -EINVAL;
650 goto out;
651 }
652
653 printk(KERN_INFO DRV_NAME
654 ": %s: Setting ARP monitoring interval to %d.\n",
655 bond->dev->name, new_value);
656 bond->params.arp_interval = new_value;
657 if (bond->params.miimon) {
658 printk(KERN_INFO DRV_NAME
659 ": %s: ARP monitoring cannot be used with MII monitoring. "
660 "%s Disabling MII monitoring.\n",
661 bond->dev->name, bond->dev->name);
662 bond->params.miimon = 0;
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700663 if (delayed_work_pending(&bond->mii_work)) {
664 cancel_delayed_work(&bond->mii_work);
665 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800666 }
667 }
668 if (!bond->params.arp_targets[0]) {
669 printk(KERN_INFO DRV_NAME
670 ": %s: ARP monitoring has been set up, "
671 "but no ARP targets have been specified.\n",
672 bond->dev->name);
673 }
674 if (bond->dev->flags & IFF_UP) {
675 /* If the interface is up, we may need to fire off
676 * the ARP timer. If the interface is down, the
677 * timer will get fired off when the open function
678 * is called.
679 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700680 if (!delayed_work_pending(&bond->arp_work)) {
681 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
682 INIT_DELAYED_WORK(&bond->arp_work,
683 bond_activebackup_arp_mon);
684 else
685 INIT_DELAYED_WORK(&bond->arp_work,
686 bond_loadbalance_arp_mon);
687
688 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800689 }
690 }
691
692out:
693 return ret;
694}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700695static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800696
697/*
698 * Show and set the arp targets.
699 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700700static ssize_t bonding_show_arp_targets(struct device *d,
701 struct device_attribute *attr,
702 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800703{
704 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700705 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800706
707 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
708 if (bond->params.arp_targets[i])
709 res += sprintf(buf + res, "%u.%u.%u.%u ",
710 NIPQUAD(bond->params.arp_targets[i]));
711 }
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800712 if (res) buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800713 return res;
714}
715
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700716static ssize_t bonding_store_arp_targets(struct device *d,
717 struct device_attribute *attr,
718 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800719{
Al Virod3bb52b2007-08-22 20:06:58 -0400720 __be32 newtarget;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800721 int i = 0, done = 0, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700722 struct bonding *bond = to_bond(d);
Al Virod3bb52b2007-08-22 20:06:58 -0400723 __be32 *targets;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800724
725 targets = bond->params.arp_targets;
726 newtarget = in_aton(buf + 1);
727 /* look for adds */
728 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400729 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800730 printk(KERN_ERR DRV_NAME
731 ": %s: invalid ARP target %u.%u.%u.%u specified for addition\n",
732 bond->dev->name, NIPQUAD(newtarget));
733 ret = -EINVAL;
734 goto out;
735 }
736 /* look for an empty slot to put the target in, and check for dupes */
737 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
738 if (targets[i] == newtarget) { /* duplicate */
739 printk(KERN_ERR DRV_NAME
740 ": %s: ARP target %u.%u.%u.%u is already present\n",
741 bond->dev->name, NIPQUAD(newtarget));
742 if (done)
743 targets[i] = 0;
744 ret = -EINVAL;
745 goto out;
746 }
747 if (targets[i] == 0 && !done) {
748 printk(KERN_INFO DRV_NAME
749 ": %s: adding ARP target %d.%d.%d.%d.\n",
750 bond->dev->name, NIPQUAD(newtarget));
751 done = 1;
752 targets[i] = newtarget;
753 }
754 }
755 if (!done) {
756 printk(KERN_ERR DRV_NAME
757 ": %s: ARP target table is full!\n",
758 bond->dev->name);
759 ret = -EINVAL;
760 goto out;
761 }
762
763 }
764 else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400765 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800766 printk(KERN_ERR DRV_NAME
767 ": %s: invalid ARP target %d.%d.%d.%d specified for removal\n",
768 bond->dev->name, NIPQUAD(newtarget));
769 ret = -EINVAL;
770 goto out;
771 }
772
773 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
774 if (targets[i] == newtarget) {
775 printk(KERN_INFO DRV_NAME
776 ": %s: removing ARP target %d.%d.%d.%d.\n",
777 bond->dev->name, NIPQUAD(newtarget));
778 targets[i] = 0;
779 done = 1;
780 }
781 }
782 if (!done) {
783 printk(KERN_INFO DRV_NAME
784 ": %s: unable to remove nonexistent ARP target %d.%d.%d.%d.\n",
785 bond->dev->name, NIPQUAD(newtarget));
786 ret = -EINVAL;
787 goto out;
788 }
789 }
790 else {
791 printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
792 bond->dev->name);
793 ret = -EPERM;
794 goto out;
795 }
796
797out:
798 return ret;
799}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700800static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800801
802/*
803 * Show and set the up and down delays. These must be multiples of the
804 * MII monitoring value, and are stored internally as the multiplier.
805 * Thus, we must translate to MS for the real world.
806 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700807static ssize_t bonding_show_downdelay(struct device *d,
808 struct device_attribute *attr,
809 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800810{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700811 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800812
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800813 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800814}
815
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700816static ssize_t bonding_store_downdelay(struct device *d,
817 struct device_attribute *attr,
818 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800819{
820 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700821 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800822
823 if (!(bond->params.miimon)) {
824 printk(KERN_ERR DRV_NAME
825 ": %s: Unable to set down delay as MII monitoring is disabled\n",
826 bond->dev->name);
827 ret = -EPERM;
828 goto out;
829 }
830
831 if (sscanf(buf, "%d", &new_value) != 1) {
832 printk(KERN_ERR DRV_NAME
833 ": %s: no down delay value specified.\n",
834 bond->dev->name);
835 ret = -EINVAL;
836 goto out;
837 }
838 if (new_value < 0) {
839 printk(KERN_ERR DRV_NAME
840 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
841 bond->dev->name, new_value, 1, INT_MAX);
842 ret = -EINVAL;
843 goto out;
844 } else {
845 if ((new_value % bond->params.miimon) != 0) {
846 printk(KERN_WARNING DRV_NAME
847 ": %s: Warning: down delay (%d) is not a multiple "
848 "of miimon (%d), delay rounded to %d ms\n",
849 bond->dev->name, new_value, bond->params.miimon,
850 (new_value / bond->params.miimon) *
851 bond->params.miimon);
852 }
853 bond->params.downdelay = new_value / bond->params.miimon;
854 printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n",
855 bond->dev->name, bond->params.downdelay * bond->params.miimon);
856
857 }
858
859out:
860 return ret;
861}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700862static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800863
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700864static ssize_t bonding_show_updelay(struct device *d,
865 struct device_attribute *attr,
866 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800867{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700868 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800869
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800870 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800871
872}
873
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700874static ssize_t bonding_store_updelay(struct device *d,
875 struct device_attribute *attr,
876 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800877{
878 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700879 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800880
881 if (!(bond->params.miimon)) {
882 printk(KERN_ERR DRV_NAME
883 ": %s: Unable to set up delay as MII monitoring is disabled\n",
884 bond->dev->name);
885 ret = -EPERM;
886 goto out;
887 }
888
889 if (sscanf(buf, "%d", &new_value) != 1) {
890 printk(KERN_ERR DRV_NAME
891 ": %s: no up delay value specified.\n",
892 bond->dev->name);
893 ret = -EINVAL;
894 goto out;
895 }
896 if (new_value < 0) {
897 printk(KERN_ERR DRV_NAME
898 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
899 bond->dev->name, new_value, 1, INT_MAX);
900 ret = -EINVAL;
901 goto out;
902 } else {
903 if ((new_value % bond->params.miimon) != 0) {
904 printk(KERN_WARNING DRV_NAME
905 ": %s: Warning: up delay (%d) is not a multiple "
906 "of miimon (%d), updelay rounded to %d ms\n",
907 bond->dev->name, new_value, bond->params.miimon,
908 (new_value / bond->params.miimon) *
909 bond->params.miimon);
910 }
911 bond->params.updelay = new_value / bond->params.miimon;
912 printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n",
913 bond->dev->name, bond->params.updelay * bond->params.miimon);
914
915 }
916
917out:
918 return ret;
919}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700920static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800921
922/*
923 * Show and set the LACP interval. Interface must be down, and the mode
924 * must be set to 802.3ad mode.
925 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700926static ssize_t bonding_show_lacp(struct device *d,
927 struct device_attribute *attr,
928 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800929{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700930 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800931
932 return sprintf(buf, "%s %d\n",
933 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800934 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800935}
936
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700937static ssize_t bonding_store_lacp(struct device *d,
938 struct device_attribute *attr,
939 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800940{
941 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700942 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800943
944 if (bond->dev->flags & IFF_UP) {
945 printk(KERN_ERR DRV_NAME
946 ": %s: Unable to update LACP rate because interface is up.\n",
947 bond->dev->name);
948 ret = -EPERM;
949 goto out;
950 }
951
952 if (bond->params.mode != BOND_MODE_8023AD) {
953 printk(KERN_ERR DRV_NAME
954 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
955 bond->dev->name);
956 ret = -EPERM;
957 goto out;
958 }
959
960 new_value = bond_parse_parm((char *)buf, bond_lacp_tbl);
961
962 if ((new_value == 1) || (new_value == 0)) {
963 bond->params.lacp_fast = new_value;
964 printk(KERN_INFO DRV_NAME
965 ": %s: Setting LACP rate to %s (%d).\n",
966 bond->dev->name, bond_lacp_tbl[new_value].modename, new_value);
967 } else {
968 printk(KERN_ERR DRV_NAME
969 ": %s: Ignoring invalid LACP rate value %.*s.\n",
970 bond->dev->name, (int)strlen(buf) - 1, buf);
971 ret = -EINVAL;
972 }
973out:
974 return ret;
975}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700976static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800977
978/*
979 * Show and set the MII monitor interval. There are two tricky bits
980 * here. First, if MII monitoring is activated, then we must disable
981 * ARP monitoring. Second, if the timer isn't running, we must
982 * start it.
983 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700984static ssize_t bonding_show_miimon(struct device *d,
985 struct device_attribute *attr,
986 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800987{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700988 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800989
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800990 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800991}
992
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700993static ssize_t bonding_store_miimon(struct device *d,
994 struct device_attribute *attr,
995 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800996{
997 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700998 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800999
1000 if (sscanf(buf, "%d", &new_value) != 1) {
1001 printk(KERN_ERR DRV_NAME
1002 ": %s: no miimon value specified.\n",
1003 bond->dev->name);
1004 ret = -EINVAL;
1005 goto out;
1006 }
1007 if (new_value < 0) {
1008 printk(KERN_ERR DRV_NAME
1009 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1010 bond->dev->name, new_value, 1, INT_MAX);
1011 ret = -EINVAL;
1012 goto out;
1013 } else {
1014 printk(KERN_INFO DRV_NAME
1015 ": %s: Setting MII monitoring interval to %d.\n",
1016 bond->dev->name, new_value);
1017 bond->params.miimon = new_value;
1018 if(bond->params.updelay)
1019 printk(KERN_INFO DRV_NAME
1020 ": %s: Note: Updating updelay (to %d) "
1021 "since it is a multiple of the miimon value.\n",
1022 bond->dev->name,
1023 bond->params.updelay * bond->params.miimon);
1024 if(bond->params.downdelay)
1025 printk(KERN_INFO DRV_NAME
1026 ": %s: Note: Updating downdelay (to %d) "
1027 "since it is a multiple of the miimon value.\n",
1028 bond->dev->name,
1029 bond->params.downdelay * bond->params.miimon);
1030 if (bond->params.arp_interval) {
1031 printk(KERN_INFO DRV_NAME
1032 ": %s: MII monitoring cannot be used with "
1033 "ARP monitoring. Disabling ARP monitoring...\n",
1034 bond->dev->name);
1035 bond->params.arp_interval = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001036 if (bond->params.arp_validate) {
1037 bond_unregister_arp(bond);
1038 bond->params.arp_validate =
1039 BOND_ARP_VALIDATE_NONE;
1040 }
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001041 if (delayed_work_pending(&bond->arp_work)) {
1042 cancel_delayed_work(&bond->arp_work);
1043 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001044 }
1045 }
1046
1047 if (bond->dev->flags & IFF_UP) {
1048 /* If the interface is up, we may need to fire off
1049 * the MII timer. If the interface is down, the
1050 * timer will get fired off when the open function
1051 * is called.
1052 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001053 if (!delayed_work_pending(&bond->mii_work)) {
1054 INIT_DELAYED_WORK(&bond->mii_work,
1055 bond_mii_monitor);
1056 queue_delayed_work(bond->wq,
1057 &bond->mii_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001058 }
1059 }
1060 }
1061out:
1062 return ret;
1063}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001064static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001065
1066/*
1067 * Show and set the primary slave. The store function is much
1068 * simpler than bonding_store_slaves function because it only needs to
1069 * handle one interface name.
1070 * The bond must be a mode that supports a primary for this be
1071 * set.
1072 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001073static ssize_t bonding_show_primary(struct device *d,
1074 struct device_attribute *attr,
1075 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001076{
1077 int count = 0;
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 (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001081 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001082 else
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001083 count = sprintf(buf, "\n");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001084
1085 return count;
1086}
1087
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001088static ssize_t bonding_store_primary(struct device *d,
1089 struct device_attribute *attr,
1090 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001091{
1092 int i;
1093 struct slave *slave;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001094 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001095
1096 write_lock_bh(&bond->lock);
1097 if (!USES_PRIMARY(bond->params.mode)) {
1098 printk(KERN_INFO DRV_NAME
1099 ": %s: Unable to set primary slave; %s is in mode %d\n",
1100 bond->dev->name, bond->dev->name, bond->params.mode);
1101 } else {
1102 bond_for_each_slave(bond, slave, i) {
1103 if (strnicmp
1104 (slave->dev->name, buf,
1105 strlen(slave->dev->name)) == 0) {
1106 printk(KERN_INFO DRV_NAME
1107 ": %s: Setting %s as primary slave.\n",
1108 bond->dev->name, slave->dev->name);
1109 bond->primary_slave = slave;
1110 bond_select_active_slave(bond);
1111 goto out;
1112 }
1113 }
1114
1115 /* if we got here, then we didn't match the name of any slave */
1116
1117 if (strlen(buf) == 0 || buf[0] == '\n') {
1118 printk(KERN_INFO DRV_NAME
1119 ": %s: Setting primary slave to None.\n",
1120 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001121 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001122 bond_select_active_slave(bond);
1123 } else {
1124 printk(KERN_INFO DRV_NAME
1125 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1126 bond->dev->name, (int)strlen(buf) - 1, buf);
1127 }
1128 }
1129out:
1130 write_unlock_bh(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001131
1132 rtnl_unlock();
1133
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001134 return count;
1135}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001136static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001137
1138/*
1139 * Show and set the use_carrier flag.
1140 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001141static ssize_t bonding_show_carrier(struct device *d,
1142 struct device_attribute *attr,
1143 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001144{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001145 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001146
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001147 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001148}
1149
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001150static ssize_t bonding_store_carrier(struct device *d,
1151 struct device_attribute *attr,
1152 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001153{
1154 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001155 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001156
1157
1158 if (sscanf(buf, "%d", &new_value) != 1) {
1159 printk(KERN_ERR DRV_NAME
1160 ": %s: no use_carrier value specified.\n",
1161 bond->dev->name);
1162 ret = -EINVAL;
1163 goto out;
1164 }
1165 if ((new_value == 0) || (new_value == 1)) {
1166 bond->params.use_carrier = new_value;
1167 printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
1168 bond->dev->name, new_value);
1169 } else {
1170 printk(KERN_INFO DRV_NAME
1171 ": %s: Ignoring invalid use_carrier value %d.\n",
1172 bond->dev->name, new_value);
1173 }
1174out:
1175 return count;
1176}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001177static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001178
1179
1180/*
1181 * Show and set currently active_slave.
1182 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001183static ssize_t bonding_show_active_slave(struct device *d,
1184 struct device_attribute *attr,
1185 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001186{
1187 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001188 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001189 int count;
1190
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001191 read_lock(&bond->curr_slave_lock);
1192 curr = bond->curr_active_slave;
1193 read_unlock(&bond->curr_slave_lock);
1194
1195 if (USES_PRIMARY(bond->params.mode) && curr)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001196 count = sprintf(buf, "%s\n", curr->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001197 else
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001198 count = sprintf(buf, "\n");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001199 return count;
1200}
1201
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001202static ssize_t bonding_store_active_slave(struct device *d,
1203 struct device_attribute *attr,
1204 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001205{
1206 int i;
1207 struct slave *slave;
1208 struct slave *old_active = NULL;
1209 struct slave *new_active = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001210 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001211
Jay Vosburgh1466a212007-11-06 13:33:28 -08001212 rtnl_lock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001213 write_lock_bh(&bond->lock);
Jay Vosburgh1466a212007-11-06 13:33:28 -08001214
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001215 if (!USES_PRIMARY(bond->params.mode)) {
1216 printk(KERN_INFO DRV_NAME
1217 ": %s: Unable to change active slave; %s is in mode %d\n",
1218 bond->dev->name, bond->dev->name, bond->params.mode);
1219 } else {
1220 bond_for_each_slave(bond, slave, i) {
1221 if (strnicmp
1222 (slave->dev->name, buf,
1223 strlen(slave->dev->name)) == 0) {
1224 old_active = bond->curr_active_slave;
1225 new_active = slave;
Jay Vosburgha50d8de2006-09-22 21:53:25 -07001226 if (new_active == old_active) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001227 /* do nothing */
1228 printk(KERN_INFO DRV_NAME
1229 ": %s: %s is already the current active slave.\n",
1230 bond->dev->name, slave->dev->name);
1231 goto out;
1232 }
1233 else {
1234 if ((new_active) &&
1235 (old_active) &&
1236 (new_active->link == BOND_LINK_UP) &&
1237 IS_UP(new_active->dev)) {
1238 printk(KERN_INFO DRV_NAME
1239 ": %s: Setting %s as active slave.\n",
1240 bond->dev->name, slave->dev->name);
1241 bond_change_active_slave(bond, new_active);
1242 }
1243 else {
1244 printk(KERN_INFO DRV_NAME
1245 ": %s: Could not set %s as active slave; "
1246 "either %s is down or the link is down.\n",
1247 bond->dev->name, slave->dev->name,
1248 slave->dev->name);
1249 }
1250 goto out;
1251 }
1252 }
1253 }
1254
1255 /* if we got here, then we didn't match the name of any slave */
1256
1257 if (strlen(buf) == 0 || buf[0] == '\n') {
1258 printk(KERN_INFO DRV_NAME
1259 ": %s: Setting active slave to None.\n",
1260 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001261 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001262 bond_select_active_slave(bond);
1263 } else {
1264 printk(KERN_INFO DRV_NAME
1265 ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
1266 bond->dev->name, (int)strlen(buf) - 1, buf);
1267 }
1268 }
1269out:
1270 write_unlock_bh(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001271 rtnl_unlock();
1272
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001273 return count;
1274
1275}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001276static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001277
1278
1279/*
1280 * Show link status of the bond interface.
1281 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001282static ssize_t bonding_show_mii_status(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);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001288
1289 read_lock(&bond->curr_slave_lock);
1290 curr = bond->curr_active_slave;
1291 read_unlock(&bond->curr_slave_lock);
1292
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001293 return sprintf(buf, "%s\n", (curr) ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001294}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001295static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001296
1297
1298/*
1299 * Show current 802.3ad aggregator ID.
1300 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001301static ssize_t bonding_show_ad_aggregator(struct device *d,
1302 struct device_attribute *attr,
1303 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001304{
1305 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001306 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001307
1308 if (bond->params.mode == BOND_MODE_8023AD) {
1309 struct ad_info ad_info;
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001310 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 -08001311 }
1312 else
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001313 count = sprintf(buf, "\n");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001314
1315 return count;
1316}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001317static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001318
1319
1320/*
1321 * Show number of active 802.3ad ports.
1322 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001323static ssize_t bonding_show_ad_num_ports(struct device *d,
1324 struct device_attribute *attr,
1325 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001326{
1327 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001328 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001329
1330 if (bond->params.mode == BOND_MODE_8023AD) {
1331 struct ad_info ad_info;
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001332 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 -08001333 }
1334 else
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001335 count = sprintf(buf, "\n");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001336
1337 return count;
1338}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001339static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001340
1341
1342/*
1343 * Show current 802.3ad actor key.
1344 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001345static ssize_t bonding_show_ad_actor_key(struct device *d,
1346 struct device_attribute *attr,
1347 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001348{
1349 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001350 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001351
1352 if (bond->params.mode == BOND_MODE_8023AD) {
1353 struct ad_info ad_info;
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001354 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 -08001355 }
1356 else
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001357 count = sprintf(buf, "\n");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001358
1359 return count;
1360}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001361static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001362
1363
1364/*
1365 * Show current 802.3ad partner key.
1366 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001367static ssize_t bonding_show_ad_partner_key(struct device *d,
1368 struct device_attribute *attr,
1369 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001370{
1371 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001372 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001373
1374 if (bond->params.mode == BOND_MODE_8023AD) {
1375 struct ad_info ad_info;
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001376 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 -08001377 }
1378 else
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001379 count = sprintf(buf, "\n");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001380
1381 return count;
1382}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001383static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001384
1385
1386/*
1387 * Show current 802.3ad partner mac.
1388 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001389static ssize_t bonding_show_ad_partner_mac(struct device *d,
1390 struct device_attribute *attr,
1391 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001392{
1393 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001394 struct bonding *bond = to_bond(d);
Joe Perches0795af52007-10-03 17:59:30 -07001395 DECLARE_MAC_BUF(mac);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001396
1397 if (bond->params.mode == BOND_MODE_8023AD) {
1398 struct ad_info ad_info;
1399 if (!bond_3ad_get_active_agg_info(bond, &ad_info)) {
Joe Perches0795af52007-10-03 17:59:30 -07001400 count = sprintf(buf,"%s\n",
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001401 print_mac(mac, ad_info.partner_system));
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001402 }
1403 }
1404 else
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001405 count = sprintf(buf, "\n");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001406
1407 return count;
1408}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001409static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001410
1411
1412
1413static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001414 &dev_attr_slaves.attr,
1415 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001416 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001417 &dev_attr_arp_validate.attr,
1418 &dev_attr_arp_interval.attr,
1419 &dev_attr_arp_ip_target.attr,
1420 &dev_attr_downdelay.attr,
1421 &dev_attr_updelay.attr,
1422 &dev_attr_lacp_rate.attr,
1423 &dev_attr_xmit_hash_policy.attr,
1424 &dev_attr_miimon.attr,
1425 &dev_attr_primary.attr,
1426 &dev_attr_use_carrier.attr,
1427 &dev_attr_active_slave.attr,
1428 &dev_attr_mii_status.attr,
1429 &dev_attr_ad_aggregator.attr,
1430 &dev_attr_ad_num_ports.attr,
1431 &dev_attr_ad_actor_key.attr,
1432 &dev_attr_ad_partner_key.attr,
1433 &dev_attr_ad_partner_mac.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001434 NULL,
1435};
1436
1437static struct attribute_group bonding_group = {
1438 .name = "bonding",
1439 .attrs = per_bond_attrs,
1440};
1441
1442/*
1443 * Initialize sysfs. This sets up the bonding_masters file in
1444 * /sys/class/net.
1445 */
1446int bond_create_sysfs(void)
1447{
1448 int ret = 0;
1449 struct bonding *firstbond;
1450
1451 init_rwsem(&bonding_rwsem);
1452
1453 /* get the netdev class pointer */
1454 firstbond = container_of(bond_dev_list.next, struct bonding, bond_list);
1455 if (!firstbond)
1456 return -ENODEV;
1457
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001458 netdev_class = firstbond->dev->dev.class;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001459 if (!netdev_class)
1460 return -ENODEV;
1461
1462 ret = class_create_file(netdev_class, &class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001463 /*
1464 * Permit multiple loads of the module by ignoring failures to
1465 * create the bonding_masters sysfs file. Bonding devices
1466 * created by second or subsequent loads of the module will
1467 * not be listed in, or controllable by, bonding_masters, but
1468 * will have the usual "bonding" sysfs directory.
1469 *
1470 * This is done to preserve backwards compatibility for
1471 * initscripts/sysconfig, which load bonding multiple times to
1472 * configure multiple bonding devices.
1473 */
1474 if (ret == -EEXIST) {
1475 netdev_class = NULL;
1476 return 0;
1477 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001478
1479 return ret;
1480
1481}
1482
1483/*
1484 * Remove /sys/class/net/bonding_masters.
1485 */
1486void bond_destroy_sysfs(void)
1487{
1488 if (netdev_class)
1489 class_remove_file(netdev_class, &class_attr_bonding_masters);
1490}
1491
1492/*
1493 * Initialize sysfs for each bond. This sets up and registers
1494 * the 'bondctl' directory for each individual bond under /sys/class/net.
1495 */
1496int bond_create_sysfs_entry(struct bonding *bond)
1497{
1498 struct net_device *dev = bond->dev;
1499 int err;
1500
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001501 err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001502 if (err) {
1503 printk(KERN_EMERG "eek! didn't create group!\n");
1504 }
1505
1506 if (expected_refcount < 1)
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001507 expected_refcount = atomic_read(&bond->dev->dev.kobj.kref.refcount);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001508
1509 return err;
1510}
1511/*
1512 * Remove sysfs entries for each bond.
1513 */
1514void bond_destroy_sysfs_entry(struct bonding *bond)
1515{
1516 struct net_device *dev = bond->dev;
1517
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001518 sysfs_remove_group(&(dev->dev.kobj), &bonding_group);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001519}
1520