blob: 5c31f5cec9f6e444b90e37b2737d6fe177a07fdf [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 */
Wagner Ferencb8843662007-12-06 23:40:30 -080077static ssize_t bonding_show_bonds(struct class *cls, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080078{
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 Ferencb8843662007-12-06 23:40:30 -080089 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -080090 break;
91 }
Wagner Ferencb8843662007-12-06 23:40:30 -080092 res += sprintf(buf + res, "%s ", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080093 }
Wagner Ferencb8843662007-12-06 23:40:30 -080094 if (res) buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -080095 up_read(&(bonding_rwsem));
96 return res;
97}
98
99/*
100 * "store" function for the bond_masters attribute. This is what
101 * creates and deletes entire bonds.
102 *
103 * The class parameter is ignored.
104 *
105 */
106
107static ssize_t bonding_store_bonds(struct class *cls, const char *buffer, size_t count)
108{
109 char command[IFNAMSIZ + 1] = {0, };
110 char *ifname;
111 int res = count;
112 struct bonding *bond;
113 struct bonding *nxt;
114
115 down_write(&(bonding_rwsem));
116 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] == '+') {
123
124 /* Check to see if the bond already exists. */
125 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
126 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
127 printk(KERN_ERR DRV_NAME
128 ": cannot add bond %s; it already exists\n",
129 ifname);
130 res = -EPERM;
131 goto out;
132 }
133
134 printk(KERN_INFO DRV_NAME
135 ": %s is being created...\n", ifname);
136 if (bond_create(ifname, &bonding_defaults, &bond)) {
137 printk(KERN_INFO DRV_NAME
138 ": %s interface already exists. Bond creation failed.\n",
139 ifname);
140 res = -EPERM;
141 }
142 goto out;
143 }
144
145 if (command[0] == '-') {
146 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
147 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
148 rtnl_lock();
149 /* check the ref count on the bond's kobject.
150 * If it's > expected, then there's a file open,
151 * and we have to fail.
152 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700153 if (atomic_read(&bond->dev->dev.kobj.kref.refcount)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800154 > expected_refcount){
155 rtnl_unlock();
156 printk(KERN_INFO DRV_NAME
157 ": Unable remove bond %s due to open references.\n",
158 ifname);
159 res = -EPERM;
160 goto out;
161 }
162 printk(KERN_INFO DRV_NAME
163 ": %s is being deleted...\n",
164 bond->dev->name);
Moni Shouad90a1622007-10-09 19:43:43 -0700165 bond_destroy(bond);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800166 rtnl_unlock();
167 goto out;
168 }
169
170 printk(KERN_ERR DRV_NAME
171 ": unable to delete non-existent bond %s\n", ifname);
172 res = -ENODEV;
173 goto out;
174 }
175
176err_no_cmd:
177 printk(KERN_ERR DRV_NAME
178 ": no command found in bonding_masters. Use +ifname or -ifname.\n");
179 res = -EPERM;
180
181 /* Always return either count or an error. If you return 0, you'll
182 * get called forever, which is bad.
183 */
184out:
185 up_write(&(bonding_rwsem));
186 return res;
187}
188/* class attribute for bond_masters file. This ends up in /sys/class/net */
189static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
190 bonding_show_bonds, bonding_store_bonds);
191
192int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave)
193{
194 char linkname[IFNAMSIZ+7];
195 int ret = 0;
196
197 /* first, create a link from the slave back to the master */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700198 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800199 "master");
200 if (ret)
201 return ret;
202 /* next, create a link from the master to the slave */
203 sprintf(linkname,"slave_%s",slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700204 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800205 linkname);
206 return ret;
207
208}
209
210void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave)
211{
212 char linkname[IFNAMSIZ+7];
213
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700214 sysfs_remove_link(&(slave->dev.kobj), "master");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800215 sprintf(linkname,"slave_%s",slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700216 sysfs_remove_link(&(master->dev.kobj), linkname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800217}
218
219
220/*
221 * Show the slaves in the current bond.
222 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700223static ssize_t bonding_show_slaves(struct device *d,
224 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800225{
226 struct slave *slave;
227 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700228 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800229
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700230 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800231 bond_for_each_slave(bond, slave, i) {
232 if (res > (PAGE_SIZE - IFNAMSIZ)) {
233 /* not enough space for another interface name */
234 if ((PAGE_SIZE - res) > 10)
235 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800236 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800237 break;
238 }
239 res += sprintf(buf + res, "%s ", slave->dev->name);
240 }
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700241 read_unlock(&bond->lock);
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800242 if (res) buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800243 return res;
244}
245
246/*
247 * Set the slaves in the current bond. The bond interface must be
248 * up for this to succeed.
249 * This function is largely the same flow as bonding_update_bonds().
250 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700251static ssize_t bonding_store_slaves(struct device *d,
252 struct device_attribute *attr,
253 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800254{
255 char command[IFNAMSIZ + 1] = { 0, };
256 char *ifname;
257 int i, res, found, ret = count;
Moni Shoua3158bf72007-10-09 19:43:41 -0700258 u32 original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800259 struct slave *slave;
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -0800260 struct net_device *dev = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700261 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800262
263 /* Quick sanity check -- is the bond interface up? */
264 if (!(bond->dev->flags & IFF_UP)) {
Moni Shoua6b1bf092007-10-09 19:43:40 -0700265 printk(KERN_WARNING DRV_NAME
266 ": %s: doing slave updates when interface is down.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800267 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800268 }
269
270 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
271
272 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
273 ifname = command + 1;
274 if ((strlen(command) <= 1) ||
275 !dev_valid_name(ifname))
276 goto err_no_cmd;
277
278 if (command[0] == '+') {
279
280 /* Got a slave name in ifname. Is it already in the list? */
281 found = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700282 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800283 bond_for_each_slave(bond, slave, i)
284 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
285 printk(KERN_ERR DRV_NAME
286 ": %s: Interface %s is already enslaved!\n",
287 bond->dev->name, ifname);
288 ret = -EPERM;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700289 read_unlock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800290 goto out;
291 }
292
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700293 read_unlock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800294 printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n",
295 bond->dev->name, ifname);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700296 dev = dev_get_by_name(&init_net, ifname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800297 if (!dev) {
298 printk(KERN_INFO DRV_NAME
299 ": %s: Interface %s does not exist!\n",
300 bond->dev->name, ifname);
301 ret = -EPERM;
302 goto out;
303 }
304 else
305 dev_put(dev);
306
307 if (dev->flags & IFF_UP) {
308 printk(KERN_ERR DRV_NAME
309 ": %s: Error: Unable to enslave %s "
310 "because it is already up.\n",
311 bond->dev->name, dev->name);
312 ret = -EPERM;
313 goto out;
314 }
315 /* If this is the first slave, then we need to set
316 the master's hardware address to be the same as the
317 slave's. */
318 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
319 memcpy(bond->dev->dev_addr, dev->dev_addr,
320 dev->addr_len);
321 }
322
323 /* Set the slave's MTU to match the bond */
Moni Shoua3158bf72007-10-09 19:43:41 -0700324 original_mtu = dev->mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800325 if (dev->mtu != bond->dev->mtu) {
326 if (dev->change_mtu) {
327 res = dev->change_mtu(dev,
328 bond->dev->mtu);
329 if (res) {
330 ret = res;
331 goto out;
332 }
333 } else {
334 dev->mtu = bond->dev->mtu;
335 }
336 }
337 rtnl_lock();
338 res = bond_enslave(bond->dev, dev);
Moni Shoua3158bf72007-10-09 19:43:41 -0700339 bond_for_each_slave(bond, slave, i)
340 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
341 slave->original_mtu = original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800342 rtnl_unlock();
343 if (res) {
344 ret = res;
345 }
346 goto out;
347 }
348
349 if (command[0] == '-') {
350 dev = NULL;
351 bond_for_each_slave(bond, slave, i)
352 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
353 dev = slave->dev;
Moni Shoua3158bf72007-10-09 19:43:41 -0700354 original_mtu = slave->original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800355 break;
356 }
357 if (dev) {
358 printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n",
359 bond->dev->name, dev->name);
360 rtnl_lock();
Moni Shouad90a1622007-10-09 19:43:43 -0700361 if (bond->setup_by_slave)
362 res = bond_release_and_destroy(bond->dev, dev);
363 else
364 res = bond_release(bond->dev, dev);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800365 rtnl_unlock();
366 if (res) {
367 ret = res;
368 goto out;
369 }
370 /* set the slave MTU to the default */
371 if (dev->change_mtu) {
Moni Shoua3158bf72007-10-09 19:43:41 -0700372 dev->change_mtu(dev, original_mtu);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800373 } else {
Moni Shoua3158bf72007-10-09 19:43:41 -0700374 dev->mtu = original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800375 }
376 }
377 else {
378 printk(KERN_ERR DRV_NAME ": unable to remove non-existent slave %s for bond %s.\n",
379 ifname, bond->dev->name);
380 ret = -ENODEV;
381 }
382 goto out;
383 }
384
385err_no_cmd:
386 printk(KERN_ERR DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
387 ret = -EPERM;
388
389out:
390 return ret;
391}
392
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700393static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves, bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800394
395/*
396 * Show and set the bonding mode. The bond interface must be down to
397 * change the mode.
398 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700399static ssize_t bonding_show_mode(struct device *d,
400 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800401{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700402 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800403
404 return sprintf(buf, "%s %d\n",
405 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800406 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800407}
408
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700409static ssize_t bonding_store_mode(struct device *d,
410 struct device_attribute *attr,
411 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800412{
413 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700414 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800415
416 if (bond->dev->flags & IFF_UP) {
417 printk(KERN_ERR DRV_NAME
418 ": unable to update mode of %s because interface is up.\n",
419 bond->dev->name);
420 ret = -EPERM;
421 goto out;
422 }
423
424 new_value = bond_parse_parm((char *)buf, bond_mode_tbl);
425 if (new_value < 0) {
426 printk(KERN_ERR DRV_NAME
427 ": %s: Ignoring invalid mode value %.*s.\n",
428 bond->dev->name,
429 (int)strlen(buf) - 1, buf);
430 ret = -EINVAL;
431 goto out;
432 } else {
Jay Vosburgh8f903c72006-02-21 16:36:44 -0800433 if (bond->params.mode == BOND_MODE_8023AD)
434 bond_unset_master_3ad_flags(bond);
435
436 if (bond->params.mode == BOND_MODE_ALB)
437 bond_unset_master_alb_flags(bond);
438
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800439 bond->params.mode = new_value;
440 bond_set_mode_ops(bond, bond->params.mode);
441 printk(KERN_INFO DRV_NAME ": %s: setting mode to %s (%d).\n",
442 bond->dev->name, bond_mode_tbl[new_value].modename, new_value);
443 }
444out:
445 return ret;
446}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700447static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800448
449/*
450 * Show and set the bonding transmit hash method. The bond interface must be down to
451 * change the xmit hash policy.
452 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700453static ssize_t bonding_show_xmit_hash(struct device *d,
454 struct device_attribute *attr,
455 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800456{
Wagner Ferenc16cd0162007-12-06 23:40:29 -0800457 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700458 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800459
Wagner Ferenc16cd0162007-12-06 23:40:29 -0800460 if ((bond->params.mode == BOND_MODE_XOR) ||
461 (bond->params.mode == BOND_MODE_8023AD)) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800462 count = sprintf(buf, "%s %d\n",
463 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800464 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800465 }
466
467 return count;
468}
469
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700470static ssize_t bonding_store_xmit_hash(struct device *d,
471 struct device_attribute *attr,
472 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800473{
474 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700475 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800476
477 if (bond->dev->flags & IFF_UP) {
478 printk(KERN_ERR DRV_NAME
479 "%s: Interface is up. Unable to update xmit policy.\n",
480 bond->dev->name);
481 ret = -EPERM;
482 goto out;
483 }
484
485 if ((bond->params.mode != BOND_MODE_XOR) &&
486 (bond->params.mode != BOND_MODE_8023AD)) {
487 printk(KERN_ERR DRV_NAME
488 "%s: Transmit hash policy is irrelevant in this mode.\n",
489 bond->dev->name);
490 ret = -EPERM;
491 goto out;
492 }
493
494 new_value = bond_parse_parm((char *)buf, xmit_hashtype_tbl);
495 if (new_value < 0) {
496 printk(KERN_ERR DRV_NAME
497 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
498 bond->dev->name,
499 (int)strlen(buf) - 1, buf);
500 ret = -EINVAL;
501 goto out;
502 } else {
503 bond->params.xmit_policy = new_value;
504 bond_set_mode_ops(bond, bond->params.mode);
505 printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
506 bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value);
507 }
508out:
509 return ret;
510}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700511static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800512
513/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700514 * Show and set arp_validate.
515 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700516static ssize_t bonding_show_arp_validate(struct device *d,
517 struct device_attribute *attr,
518 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700519{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700520 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700521
522 return sprintf(buf, "%s %d\n",
523 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800524 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700525}
526
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700527static ssize_t bonding_store_arp_validate(struct device *d,
528 struct device_attribute *attr,
529 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700530{
531 int new_value;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700532 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700533
534 new_value = bond_parse_parm((char *)buf, arp_validate_tbl);
535 if (new_value < 0) {
536 printk(KERN_ERR DRV_NAME
537 ": %s: Ignoring invalid arp_validate value %s\n",
538 bond->dev->name, buf);
539 return -EINVAL;
540 }
541 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
542 printk(KERN_ERR DRV_NAME
543 ": %s: arp_validate only supported in active-backup mode.\n",
544 bond->dev->name);
545 return -EINVAL;
546 }
547 printk(KERN_INFO DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
548 bond->dev->name, arp_validate_tbl[new_value].modename,
549 new_value);
550
551 if (!bond->params.arp_validate && new_value) {
552 bond_register_arp(bond);
553 } else if (bond->params.arp_validate && !new_value) {
554 bond_unregister_arp(bond);
555 }
556
557 bond->params.arp_validate = new_value;
558
559 return count;
560}
561
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700562static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate, bonding_store_arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700563
564/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700565 * Show and store fail_over_mac. User only allowed to change the
566 * value when there are no slaves.
567 */
568static ssize_t bonding_show_fail_over_mac(struct device *d, struct device_attribute *attr, char *buf)
569{
570 struct bonding *bond = to_bond(d);
571
572 return sprintf(buf, "%d\n", bond->params.fail_over_mac) + 1;
573}
574
575static ssize_t bonding_store_fail_over_mac(struct device *d, struct device_attribute *attr, const char *buf, size_t count)
576{
577 int new_value;
578 int ret = count;
579 struct bonding *bond = to_bond(d);
580
581 if (bond->slave_cnt != 0) {
582 printk(KERN_ERR DRV_NAME
583 ": %s: Can't alter fail_over_mac with slaves in bond.\n",
584 bond->dev->name);
585 ret = -EPERM;
586 goto out;
587 }
588
589 if (sscanf(buf, "%d", &new_value) != 1) {
590 printk(KERN_ERR DRV_NAME
591 ": %s: no fail_over_mac value specified.\n",
592 bond->dev->name);
593 ret = -EINVAL;
594 goto out;
595 }
596
597 if ((new_value == 0) || (new_value == 1)) {
598 bond->params.fail_over_mac = new_value;
599 printk(KERN_INFO DRV_NAME ": %s: Setting fail_over_mac to %d.\n",
600 bond->dev->name, new_value);
601 } else {
602 printk(KERN_INFO DRV_NAME
603 ": %s: Ignoring invalid fail_over_mac value %d.\n",
604 bond->dev->name, new_value);
605 }
606out:
607 return ret;
608}
609
610static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR, bonding_show_fail_over_mac, bonding_store_fail_over_mac);
611
612/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800613 * Show and set the arp timer interval. There are two tricky bits
614 * here. First, if ARP monitoring is activated, then we must disable
615 * MII monitoring. Second, if the ARP timer isn't running, we must
616 * start it.
617 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700618static ssize_t bonding_show_arp_interval(struct device *d,
619 struct device_attribute *attr,
620 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800621{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700622 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800623
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800624 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800625}
626
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700627static ssize_t bonding_store_arp_interval(struct device *d,
628 struct device_attribute *attr,
629 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800630{
631 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700632 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800633
634 if (sscanf(buf, "%d", &new_value) != 1) {
635 printk(KERN_ERR DRV_NAME
636 ": %s: no arp_interval value specified.\n",
637 bond->dev->name);
638 ret = -EINVAL;
639 goto out;
640 }
641 if (new_value < 0) {
642 printk(KERN_ERR DRV_NAME
643 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
644 bond->dev->name, new_value, INT_MAX);
645 ret = -EINVAL;
646 goto out;
647 }
648
649 printk(KERN_INFO DRV_NAME
650 ": %s: Setting ARP monitoring interval to %d.\n",
651 bond->dev->name, new_value);
652 bond->params.arp_interval = new_value;
653 if (bond->params.miimon) {
654 printk(KERN_INFO DRV_NAME
655 ": %s: ARP monitoring cannot be used with MII monitoring. "
656 "%s Disabling MII monitoring.\n",
657 bond->dev->name, bond->dev->name);
658 bond->params.miimon = 0;
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700659 if (delayed_work_pending(&bond->mii_work)) {
660 cancel_delayed_work(&bond->mii_work);
661 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800662 }
663 }
664 if (!bond->params.arp_targets[0]) {
665 printk(KERN_INFO DRV_NAME
666 ": %s: ARP monitoring has been set up, "
667 "but no ARP targets have been specified.\n",
668 bond->dev->name);
669 }
670 if (bond->dev->flags & IFF_UP) {
671 /* If the interface is up, we may need to fire off
672 * the ARP timer. If the interface is down, the
673 * timer will get fired off when the open function
674 * is called.
675 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700676 if (!delayed_work_pending(&bond->arp_work)) {
677 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
678 INIT_DELAYED_WORK(&bond->arp_work,
679 bond_activebackup_arp_mon);
680 else
681 INIT_DELAYED_WORK(&bond->arp_work,
682 bond_loadbalance_arp_mon);
683
684 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800685 }
686 }
687
688out:
689 return ret;
690}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700691static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800692
693/*
694 * Show and set the arp targets.
695 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700696static ssize_t bonding_show_arp_targets(struct device *d,
697 struct device_attribute *attr,
698 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800699{
700 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700701 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800702
703 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
704 if (bond->params.arp_targets[i])
705 res += sprintf(buf + res, "%u.%u.%u.%u ",
706 NIPQUAD(bond->params.arp_targets[i]));
707 }
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800708 if (res) buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800709 return res;
710}
711
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700712static ssize_t bonding_store_arp_targets(struct device *d,
713 struct device_attribute *attr,
714 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800715{
Al Virod3bb52b2007-08-22 20:06:58 -0400716 __be32 newtarget;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800717 int i = 0, done = 0, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700718 struct bonding *bond = to_bond(d);
Al Virod3bb52b2007-08-22 20:06:58 -0400719 __be32 *targets;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800720
721 targets = bond->params.arp_targets;
722 newtarget = in_aton(buf + 1);
723 /* look for adds */
724 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400725 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800726 printk(KERN_ERR DRV_NAME
727 ": %s: invalid ARP target %u.%u.%u.%u specified for addition\n",
728 bond->dev->name, NIPQUAD(newtarget));
729 ret = -EINVAL;
730 goto out;
731 }
732 /* look for an empty slot to put the target in, and check for dupes */
733 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
734 if (targets[i] == newtarget) { /* duplicate */
735 printk(KERN_ERR DRV_NAME
736 ": %s: ARP target %u.%u.%u.%u is already present\n",
737 bond->dev->name, NIPQUAD(newtarget));
738 if (done)
739 targets[i] = 0;
740 ret = -EINVAL;
741 goto out;
742 }
743 if (targets[i] == 0 && !done) {
744 printk(KERN_INFO DRV_NAME
745 ": %s: adding ARP target %d.%d.%d.%d.\n",
746 bond->dev->name, NIPQUAD(newtarget));
747 done = 1;
748 targets[i] = newtarget;
749 }
750 }
751 if (!done) {
752 printk(KERN_ERR DRV_NAME
753 ": %s: ARP target table is full!\n",
754 bond->dev->name);
755 ret = -EINVAL;
756 goto out;
757 }
758
759 }
760 else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400761 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800762 printk(KERN_ERR DRV_NAME
763 ": %s: invalid ARP target %d.%d.%d.%d specified for removal\n",
764 bond->dev->name, NIPQUAD(newtarget));
765 ret = -EINVAL;
766 goto out;
767 }
768
769 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
770 if (targets[i] == newtarget) {
771 printk(KERN_INFO DRV_NAME
772 ": %s: removing ARP target %d.%d.%d.%d.\n",
773 bond->dev->name, NIPQUAD(newtarget));
774 targets[i] = 0;
775 done = 1;
776 }
777 }
778 if (!done) {
779 printk(KERN_INFO DRV_NAME
780 ": %s: unable to remove nonexistent ARP target %d.%d.%d.%d.\n",
781 bond->dev->name, NIPQUAD(newtarget));
782 ret = -EINVAL;
783 goto out;
784 }
785 }
786 else {
787 printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
788 bond->dev->name);
789 ret = -EPERM;
790 goto out;
791 }
792
793out:
794 return ret;
795}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700796static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800797
798/*
799 * Show and set the up and down delays. These must be multiples of the
800 * MII monitoring value, and are stored internally as the multiplier.
801 * Thus, we must translate to MS for the real world.
802 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700803static ssize_t bonding_show_downdelay(struct device *d,
804 struct device_attribute *attr,
805 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800806{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700807 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800808
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800809 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800810}
811
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700812static ssize_t bonding_store_downdelay(struct device *d,
813 struct device_attribute *attr,
814 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800815{
816 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700817 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800818
819 if (!(bond->params.miimon)) {
820 printk(KERN_ERR DRV_NAME
821 ": %s: Unable to set down delay as MII monitoring is disabled\n",
822 bond->dev->name);
823 ret = -EPERM;
824 goto out;
825 }
826
827 if (sscanf(buf, "%d", &new_value) != 1) {
828 printk(KERN_ERR DRV_NAME
829 ": %s: no down delay value specified.\n",
830 bond->dev->name);
831 ret = -EINVAL;
832 goto out;
833 }
834 if (new_value < 0) {
835 printk(KERN_ERR DRV_NAME
836 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
837 bond->dev->name, new_value, 1, INT_MAX);
838 ret = -EINVAL;
839 goto out;
840 } else {
841 if ((new_value % bond->params.miimon) != 0) {
842 printk(KERN_WARNING DRV_NAME
843 ": %s: Warning: down delay (%d) is not a multiple "
844 "of miimon (%d), delay rounded to %d ms\n",
845 bond->dev->name, new_value, bond->params.miimon,
846 (new_value / bond->params.miimon) *
847 bond->params.miimon);
848 }
849 bond->params.downdelay = new_value / bond->params.miimon;
850 printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n",
851 bond->dev->name, bond->params.downdelay * bond->params.miimon);
852
853 }
854
855out:
856 return ret;
857}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700858static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800859
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700860static ssize_t bonding_show_updelay(struct device *d,
861 struct device_attribute *attr,
862 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800863{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700864 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800865
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800866 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800867
868}
869
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700870static ssize_t bonding_store_updelay(struct device *d,
871 struct device_attribute *attr,
872 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800873{
874 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700875 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800876
877 if (!(bond->params.miimon)) {
878 printk(KERN_ERR DRV_NAME
879 ": %s: Unable to set up delay as MII monitoring is disabled\n",
880 bond->dev->name);
881 ret = -EPERM;
882 goto out;
883 }
884
885 if (sscanf(buf, "%d", &new_value) != 1) {
886 printk(KERN_ERR DRV_NAME
887 ": %s: no up delay value specified.\n",
888 bond->dev->name);
889 ret = -EINVAL;
890 goto out;
891 }
892 if (new_value < 0) {
893 printk(KERN_ERR DRV_NAME
894 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
895 bond->dev->name, new_value, 1, INT_MAX);
896 ret = -EINVAL;
897 goto out;
898 } else {
899 if ((new_value % bond->params.miimon) != 0) {
900 printk(KERN_WARNING DRV_NAME
901 ": %s: Warning: up delay (%d) is not a multiple "
902 "of miimon (%d), updelay rounded to %d ms\n",
903 bond->dev->name, new_value, bond->params.miimon,
904 (new_value / bond->params.miimon) *
905 bond->params.miimon);
906 }
907 bond->params.updelay = new_value / bond->params.miimon;
908 printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n",
909 bond->dev->name, bond->params.updelay * bond->params.miimon);
910
911 }
912
913out:
914 return ret;
915}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700916static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800917
918/*
919 * Show and set the LACP interval. Interface must be down, and the mode
920 * must be set to 802.3ad mode.
921 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700922static ssize_t bonding_show_lacp(struct device *d,
923 struct device_attribute *attr,
924 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800925{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700926 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800927
928 return sprintf(buf, "%s %d\n",
929 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800930 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800931}
932
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700933static ssize_t bonding_store_lacp(struct device *d,
934 struct device_attribute *attr,
935 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800936{
937 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700938 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800939
940 if (bond->dev->flags & IFF_UP) {
941 printk(KERN_ERR DRV_NAME
942 ": %s: Unable to update LACP rate because interface is up.\n",
943 bond->dev->name);
944 ret = -EPERM;
945 goto out;
946 }
947
948 if (bond->params.mode != BOND_MODE_8023AD) {
949 printk(KERN_ERR DRV_NAME
950 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
951 bond->dev->name);
952 ret = -EPERM;
953 goto out;
954 }
955
956 new_value = bond_parse_parm((char *)buf, bond_lacp_tbl);
957
958 if ((new_value == 1) || (new_value == 0)) {
959 bond->params.lacp_fast = new_value;
960 printk(KERN_INFO DRV_NAME
961 ": %s: Setting LACP rate to %s (%d).\n",
962 bond->dev->name, bond_lacp_tbl[new_value].modename, new_value);
963 } else {
964 printk(KERN_ERR DRV_NAME
965 ": %s: Ignoring invalid LACP rate value %.*s.\n",
966 bond->dev->name, (int)strlen(buf) - 1, buf);
967 ret = -EINVAL;
968 }
969out:
970 return ret;
971}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700972static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800973
974/*
975 * Show and set the MII monitor interval. There are two tricky bits
976 * here. First, if MII monitoring is activated, then we must disable
977 * ARP monitoring. Second, if the timer isn't running, we must
978 * start it.
979 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700980static ssize_t bonding_show_miimon(struct device *d,
981 struct device_attribute *attr,
982 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800983{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700984 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800985
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800986 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800987}
988
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700989static ssize_t bonding_store_miimon(struct device *d,
990 struct device_attribute *attr,
991 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800992{
993 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700994 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800995
996 if (sscanf(buf, "%d", &new_value) != 1) {
997 printk(KERN_ERR DRV_NAME
998 ": %s: no miimon value specified.\n",
999 bond->dev->name);
1000 ret = -EINVAL;
1001 goto out;
1002 }
1003 if (new_value < 0) {
1004 printk(KERN_ERR DRV_NAME
1005 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1006 bond->dev->name, new_value, 1, INT_MAX);
1007 ret = -EINVAL;
1008 goto out;
1009 } else {
1010 printk(KERN_INFO DRV_NAME
1011 ": %s: Setting MII monitoring interval to %d.\n",
1012 bond->dev->name, new_value);
1013 bond->params.miimon = new_value;
1014 if(bond->params.updelay)
1015 printk(KERN_INFO DRV_NAME
1016 ": %s: Note: Updating updelay (to %d) "
1017 "since it is a multiple of the miimon value.\n",
1018 bond->dev->name,
1019 bond->params.updelay * bond->params.miimon);
1020 if(bond->params.downdelay)
1021 printk(KERN_INFO DRV_NAME
1022 ": %s: Note: Updating downdelay (to %d) "
1023 "since it is a multiple of the miimon value.\n",
1024 bond->dev->name,
1025 bond->params.downdelay * bond->params.miimon);
1026 if (bond->params.arp_interval) {
1027 printk(KERN_INFO DRV_NAME
1028 ": %s: MII monitoring cannot be used with "
1029 "ARP monitoring. Disabling ARP monitoring...\n",
1030 bond->dev->name);
1031 bond->params.arp_interval = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001032 if (bond->params.arp_validate) {
1033 bond_unregister_arp(bond);
1034 bond->params.arp_validate =
1035 BOND_ARP_VALIDATE_NONE;
1036 }
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001037 if (delayed_work_pending(&bond->arp_work)) {
1038 cancel_delayed_work(&bond->arp_work);
1039 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001040 }
1041 }
1042
1043 if (bond->dev->flags & IFF_UP) {
1044 /* If the interface is up, we may need to fire off
1045 * the MII timer. If the interface is down, the
1046 * timer will get fired off when the open function
1047 * is called.
1048 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001049 if (!delayed_work_pending(&bond->mii_work)) {
1050 INIT_DELAYED_WORK(&bond->mii_work,
1051 bond_mii_monitor);
1052 queue_delayed_work(bond->wq,
1053 &bond->mii_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001054 }
1055 }
1056 }
1057out:
1058 return ret;
1059}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001060static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001061
1062/*
1063 * Show and set the primary slave. The store function is much
1064 * simpler than bonding_store_slaves function because it only needs to
1065 * handle one interface name.
1066 * The bond must be a mode that supports a primary for this be
1067 * set.
1068 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001069static ssize_t bonding_show_primary(struct device *d,
1070 struct device_attribute *attr,
1071 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001072{
1073 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001074 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001075
1076 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001077 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001078
1079 return count;
1080}
1081
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001082static ssize_t bonding_store_primary(struct device *d,
1083 struct device_attribute *attr,
1084 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001085{
1086 int i;
1087 struct slave *slave;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001088 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001089
1090 write_lock_bh(&bond->lock);
1091 if (!USES_PRIMARY(bond->params.mode)) {
1092 printk(KERN_INFO DRV_NAME
1093 ": %s: Unable to set primary slave; %s is in mode %d\n",
1094 bond->dev->name, bond->dev->name, bond->params.mode);
1095 } else {
1096 bond_for_each_slave(bond, slave, i) {
1097 if (strnicmp
1098 (slave->dev->name, buf,
1099 strlen(slave->dev->name)) == 0) {
1100 printk(KERN_INFO DRV_NAME
1101 ": %s: Setting %s as primary slave.\n",
1102 bond->dev->name, slave->dev->name);
1103 bond->primary_slave = slave;
1104 bond_select_active_slave(bond);
1105 goto out;
1106 }
1107 }
1108
1109 /* if we got here, then we didn't match the name of any slave */
1110
1111 if (strlen(buf) == 0 || buf[0] == '\n') {
1112 printk(KERN_INFO DRV_NAME
1113 ": %s: Setting primary slave to None.\n",
1114 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001115 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001116 bond_select_active_slave(bond);
1117 } else {
1118 printk(KERN_INFO DRV_NAME
1119 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1120 bond->dev->name, (int)strlen(buf) - 1, buf);
1121 }
1122 }
1123out:
1124 write_unlock_bh(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001125
1126 rtnl_unlock();
1127
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001128 return count;
1129}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001130static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001131
1132/*
1133 * Show and set the use_carrier flag.
1134 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001135static ssize_t bonding_show_carrier(struct device *d,
1136 struct device_attribute *attr,
1137 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001138{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001139 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001140
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001141 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001142}
1143
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001144static ssize_t bonding_store_carrier(struct device *d,
1145 struct device_attribute *attr,
1146 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001147{
1148 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001149 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001150
1151
1152 if (sscanf(buf, "%d", &new_value) != 1) {
1153 printk(KERN_ERR DRV_NAME
1154 ": %s: no use_carrier value specified.\n",
1155 bond->dev->name);
1156 ret = -EINVAL;
1157 goto out;
1158 }
1159 if ((new_value == 0) || (new_value == 1)) {
1160 bond->params.use_carrier = new_value;
1161 printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
1162 bond->dev->name, new_value);
1163 } else {
1164 printk(KERN_INFO DRV_NAME
1165 ": %s: Ignoring invalid use_carrier value %d.\n",
1166 bond->dev->name, new_value);
1167 }
1168out:
1169 return count;
1170}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001171static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001172
1173
1174/*
1175 * Show and set currently active_slave.
1176 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001177static ssize_t bonding_show_active_slave(struct device *d,
1178 struct device_attribute *attr,
1179 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001180{
1181 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001182 struct bonding *bond = to_bond(d);
Wagner Ferenc16cd0162007-12-06 23:40:29 -08001183 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001184
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001185 read_lock(&bond->curr_slave_lock);
1186 curr = bond->curr_active_slave;
1187 read_unlock(&bond->curr_slave_lock);
1188
1189 if (USES_PRIMARY(bond->params.mode) && curr)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001190 count = sprintf(buf, "%s\n", curr->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001191 return count;
1192}
1193
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001194static ssize_t bonding_store_active_slave(struct device *d,
1195 struct device_attribute *attr,
1196 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001197{
1198 int i;
1199 struct slave *slave;
1200 struct slave *old_active = NULL;
1201 struct slave *new_active = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001202 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001203
Jay Vosburgh1466a212007-11-06 13:33:28 -08001204 rtnl_lock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001205 write_lock_bh(&bond->lock);
Jay Vosburgh1466a212007-11-06 13:33:28 -08001206
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001207 if (!USES_PRIMARY(bond->params.mode)) {
1208 printk(KERN_INFO DRV_NAME
1209 ": %s: Unable to change active slave; %s is in mode %d\n",
1210 bond->dev->name, bond->dev->name, bond->params.mode);
1211 } else {
1212 bond_for_each_slave(bond, slave, i) {
1213 if (strnicmp
1214 (slave->dev->name, buf,
1215 strlen(slave->dev->name)) == 0) {
1216 old_active = bond->curr_active_slave;
1217 new_active = slave;
Jay Vosburgha50d8de2006-09-22 21:53:25 -07001218 if (new_active == old_active) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001219 /* do nothing */
1220 printk(KERN_INFO DRV_NAME
1221 ": %s: %s is already the current active slave.\n",
1222 bond->dev->name, slave->dev->name);
1223 goto out;
1224 }
1225 else {
1226 if ((new_active) &&
1227 (old_active) &&
1228 (new_active->link == BOND_LINK_UP) &&
1229 IS_UP(new_active->dev)) {
1230 printk(KERN_INFO DRV_NAME
1231 ": %s: Setting %s as active slave.\n",
1232 bond->dev->name, slave->dev->name);
1233 bond_change_active_slave(bond, new_active);
1234 }
1235 else {
1236 printk(KERN_INFO DRV_NAME
1237 ": %s: Could not set %s as active slave; "
1238 "either %s is down or the link is down.\n",
1239 bond->dev->name, slave->dev->name,
1240 slave->dev->name);
1241 }
1242 goto out;
1243 }
1244 }
1245 }
1246
1247 /* if we got here, then we didn't match the name of any slave */
1248
1249 if (strlen(buf) == 0 || buf[0] == '\n') {
1250 printk(KERN_INFO DRV_NAME
1251 ": %s: Setting active slave to None.\n",
1252 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001253 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001254 bond_select_active_slave(bond);
1255 } else {
1256 printk(KERN_INFO DRV_NAME
1257 ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
1258 bond->dev->name, (int)strlen(buf) - 1, buf);
1259 }
1260 }
1261out:
1262 write_unlock_bh(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001263 rtnl_unlock();
1264
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001265 return count;
1266
1267}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001268static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001269
1270
1271/*
1272 * Show link status of the bond interface.
1273 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001274static ssize_t bonding_show_mii_status(struct device *d,
1275 struct device_attribute *attr,
1276 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001277{
1278 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001279 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001280
1281 read_lock(&bond->curr_slave_lock);
1282 curr = bond->curr_active_slave;
1283 read_unlock(&bond->curr_slave_lock);
1284
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001285 return sprintf(buf, "%s\n", (curr) ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001286}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001287static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001288
1289
1290/*
1291 * Show current 802.3ad aggregator ID.
1292 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001293static ssize_t bonding_show_ad_aggregator(struct device *d,
1294 struct device_attribute *attr,
1295 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001296{
1297 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001298 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001299
1300 if (bond->params.mode == BOND_MODE_8023AD) {
1301 struct ad_info ad_info;
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001302 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 -08001303 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001304
1305 return count;
1306}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001307static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001308
1309
1310/*
1311 * Show number of active 802.3ad ports.
1312 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001313static ssize_t bonding_show_ad_num_ports(struct device *d,
1314 struct device_attribute *attr,
1315 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001316{
1317 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001318 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001319
1320 if (bond->params.mode == BOND_MODE_8023AD) {
1321 struct ad_info ad_info;
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001322 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 -08001323 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001324
1325 return count;
1326}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001327static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001328
1329
1330/*
1331 * Show current 802.3ad actor key.
1332 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001333static ssize_t bonding_show_ad_actor_key(struct device *d,
1334 struct device_attribute *attr,
1335 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001336{
1337 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001338 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001339
1340 if (bond->params.mode == BOND_MODE_8023AD) {
1341 struct ad_info ad_info;
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001342 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 -08001343 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001344
1345 return count;
1346}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001347static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001348
1349
1350/*
1351 * Show current 802.3ad partner key.
1352 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001353static ssize_t bonding_show_ad_partner_key(struct device *d,
1354 struct device_attribute *attr,
1355 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001356{
1357 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001358 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001359
1360 if (bond->params.mode == BOND_MODE_8023AD) {
1361 struct ad_info ad_info;
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001362 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 -08001363 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001364
1365 return count;
1366}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001367static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001368
1369
1370/*
1371 * Show current 802.3ad partner mac.
1372 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001373static ssize_t bonding_show_ad_partner_mac(struct device *d,
1374 struct device_attribute *attr,
1375 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001376{
1377 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001378 struct bonding *bond = to_bond(d);
Joe Perches0795af52007-10-03 17:59:30 -07001379 DECLARE_MAC_BUF(mac);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001380
1381 if (bond->params.mode == BOND_MODE_8023AD) {
1382 struct ad_info ad_info;
1383 if (!bond_3ad_get_active_agg_info(bond, &ad_info)) {
Joe Perches0795af52007-10-03 17:59:30 -07001384 count = sprintf(buf,"%s\n",
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001385 print_mac(mac, ad_info.partner_system));
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001386 }
1387 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001388
1389 return count;
1390}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001391static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001392
1393
1394
1395static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001396 &dev_attr_slaves.attr,
1397 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001398 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001399 &dev_attr_arp_validate.attr,
1400 &dev_attr_arp_interval.attr,
1401 &dev_attr_arp_ip_target.attr,
1402 &dev_attr_downdelay.attr,
1403 &dev_attr_updelay.attr,
1404 &dev_attr_lacp_rate.attr,
1405 &dev_attr_xmit_hash_policy.attr,
1406 &dev_attr_miimon.attr,
1407 &dev_attr_primary.attr,
1408 &dev_attr_use_carrier.attr,
1409 &dev_attr_active_slave.attr,
1410 &dev_attr_mii_status.attr,
1411 &dev_attr_ad_aggregator.attr,
1412 &dev_attr_ad_num_ports.attr,
1413 &dev_attr_ad_actor_key.attr,
1414 &dev_attr_ad_partner_key.attr,
1415 &dev_attr_ad_partner_mac.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001416 NULL,
1417};
1418
1419static struct attribute_group bonding_group = {
1420 .name = "bonding",
1421 .attrs = per_bond_attrs,
1422};
1423
1424/*
1425 * Initialize sysfs. This sets up the bonding_masters file in
1426 * /sys/class/net.
1427 */
1428int bond_create_sysfs(void)
1429{
1430 int ret = 0;
1431 struct bonding *firstbond;
1432
1433 init_rwsem(&bonding_rwsem);
1434
1435 /* get the netdev class pointer */
1436 firstbond = container_of(bond_dev_list.next, struct bonding, bond_list);
1437 if (!firstbond)
1438 return -ENODEV;
1439
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001440 netdev_class = firstbond->dev->dev.class;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001441 if (!netdev_class)
1442 return -ENODEV;
1443
1444 ret = class_create_file(netdev_class, &class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001445 /*
1446 * Permit multiple loads of the module by ignoring failures to
1447 * create the bonding_masters sysfs file. Bonding devices
1448 * created by second or subsequent loads of the module will
1449 * not be listed in, or controllable by, bonding_masters, but
1450 * will have the usual "bonding" sysfs directory.
1451 *
1452 * This is done to preserve backwards compatibility for
1453 * initscripts/sysconfig, which load bonding multiple times to
1454 * configure multiple bonding devices.
1455 */
1456 if (ret == -EEXIST) {
1457 netdev_class = NULL;
1458 return 0;
1459 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001460
1461 return ret;
1462
1463}
1464
1465/*
1466 * Remove /sys/class/net/bonding_masters.
1467 */
1468void bond_destroy_sysfs(void)
1469{
1470 if (netdev_class)
1471 class_remove_file(netdev_class, &class_attr_bonding_masters);
1472}
1473
1474/*
1475 * Initialize sysfs for each bond. This sets up and registers
1476 * the 'bondctl' directory for each individual bond under /sys/class/net.
1477 */
1478int bond_create_sysfs_entry(struct bonding *bond)
1479{
1480 struct net_device *dev = bond->dev;
1481 int err;
1482
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001483 err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001484 if (err) {
1485 printk(KERN_EMERG "eek! didn't create group!\n");
1486 }
1487
1488 if (expected_refcount < 1)
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001489 expected_refcount = atomic_read(&bond->dev->dev.kobj.kref.refcount);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001490
1491 return err;
1492}
1493/*
1494 * Remove sysfs entries for each bond.
1495 */
1496void bond_destroy_sysfs_entry(struct bonding *bond)
1497{
1498 struct net_device *dev = bond->dev;
1499
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001500 sysfs_remove_group(&(dev->dev.kobj), &bonding_group);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001501}
1502