blob: 8788e3e338526d505b76729119ed4e4b4937626d [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[];
Jay Vosburgh3915c1e82008-05-17 21:10:14 -070053extern struct bond_parm_tbl fail_over_mac_tbl[];
Mitch Williamsb76cdba2005-11-09 10:36:41 -080054
55static int expected_refcount = -1;
Mitch Williamsb76cdba2005-11-09 10:36:41 -080056/*--------------------------- 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 Ferenc1dcdcd62007-12-06 23:40:31 -080094 if (res)
95 buf[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;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800112 int rv, res = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800113 struct bonding *bond;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800114
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800115 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
116 ifname = command + 1;
117 if ((strlen(command) <= 1) ||
118 !dev_valid_name(ifname))
119 goto err_no_cmd;
120
121 if (command[0] == '+') {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800122 printk(KERN_INFO DRV_NAME
123 ": %s is being created...\n", ifname);
Pavel Emelyanov0dd646f2008-05-17 21:10:09 -0700124 rv = bond_create(ifname, &bonding_defaults);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800125 if (rv) {
126 printk(KERN_INFO DRV_NAME ": Bond creation failed.\n");
127 res = rv;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800128 }
129 goto out;
130 }
131
132 if (command[0] == '-') {
Jay Vosburgh027ea042008-01-17 16:25:02 -0800133 rtnl_lock();
134 down_write(&bonding_rwsem);
135
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700136 list_for_each_entry(bond, &bond_dev_list, bond_list)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800137 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800138 /* check the ref count on the bond's kobject.
139 * If it's > expected, then there's a file open,
140 * and we have to fail.
141 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700142 if (atomic_read(&bond->dev->dev.kobj.kref.refcount)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800143 > expected_refcount){
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800144 printk(KERN_INFO DRV_NAME
145 ": Unable remove bond %s due to open references.\n",
146 ifname);
147 res = -EPERM;
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700148 goto out_unlock;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800149 }
150 printk(KERN_INFO DRV_NAME
151 ": %s is being deleted...\n",
152 bond->dev->name);
Moni Shouad90a1622007-10-09 19:43:43 -0700153 bond_destroy(bond);
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700154 goto out_unlock;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800155 }
156
157 printk(KERN_ERR DRV_NAME
158 ": unable to delete non-existent bond %s\n", ifname);
159 res = -ENODEV;
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700160 goto out_unlock;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800161 }
162
163err_no_cmd:
164 printk(KERN_ERR DRV_NAME
165 ": no command found in bonding_masters. Use +ifname or -ifname.\n");
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700166 return -EPERM;
167
168out_unlock:
169 up_write(&bonding_rwsem);
170 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800171
172 /* Always return either count or an error. If you return 0, you'll
173 * get called forever, which is bad.
174 */
175out:
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800176 return res;
177}
178/* class attribute for bond_masters file. This ends up in /sys/class/net */
179static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
180 bonding_show_bonds, bonding_store_bonds);
181
182int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave)
183{
184 char linkname[IFNAMSIZ+7];
185 int ret = 0;
186
187 /* first, create a link from the slave back to the master */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700188 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800189 "master");
190 if (ret)
191 return ret;
192 /* next, create a link from the master to the slave */
193 sprintf(linkname,"slave_%s",slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700194 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800195 linkname);
196 return ret;
197
198}
199
200void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave)
201{
202 char linkname[IFNAMSIZ+7];
203
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700204 sysfs_remove_link(&(slave->dev.kobj), "master");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800205 sprintf(linkname,"slave_%s",slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700206 sysfs_remove_link(&(master->dev.kobj), linkname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800207}
208
209
210/*
211 * Show the slaves in the current bond.
212 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700213static ssize_t bonding_show_slaves(struct device *d,
214 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800215{
216 struct slave *slave;
217 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700218 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800219
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700220 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800221 bond_for_each_slave(bond, slave, i) {
222 if (res > (PAGE_SIZE - IFNAMSIZ)) {
223 /* not enough space for another interface name */
224 if ((PAGE_SIZE - res) > 10)
225 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800226 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800227 break;
228 }
229 res += sprintf(buf + res, "%s ", slave->dev->name);
230 }
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700231 read_unlock(&bond->lock);
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800232 if (res)
233 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800234 return res;
235}
236
237/*
238 * Set the slaves in the current bond. The bond interface must be
239 * up for this to succeed.
240 * This function is largely the same flow as bonding_update_bonds().
241 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700242static ssize_t bonding_store_slaves(struct device *d,
243 struct device_attribute *attr,
244 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800245{
246 char command[IFNAMSIZ + 1] = { 0, };
247 char *ifname;
248 int i, res, found, ret = count;
Moni Shoua3158bf72007-10-09 19:43:41 -0700249 u32 original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800250 struct slave *slave;
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -0800251 struct net_device *dev = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700252 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800253
254 /* Quick sanity check -- is the bond interface up? */
255 if (!(bond->dev->flags & IFF_UP)) {
Moni Shoua6b1bf092007-10-09 19:43:40 -0700256 printk(KERN_WARNING DRV_NAME
257 ": %s: doing slave updates when interface is down.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800258 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800259 }
260
261 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
262
Jay Vosburgh027ea042008-01-17 16:25:02 -0800263 rtnl_lock();
264 down_write(&(bonding_rwsem));
265
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800266 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
267 ifname = command + 1;
268 if ((strlen(command) <= 1) ||
269 !dev_valid_name(ifname))
270 goto err_no_cmd;
271
272 if (command[0] == '+') {
273
274 /* Got a slave name in ifname. Is it already in the list? */
275 found = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700276 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800277 bond_for_each_slave(bond, slave, i)
278 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
279 printk(KERN_ERR DRV_NAME
280 ": %s: Interface %s is already enslaved!\n",
281 bond->dev->name, ifname);
282 ret = -EPERM;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700283 read_unlock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800284 goto out;
285 }
286
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700287 read_unlock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800288 printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n",
289 bond->dev->name, ifname);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700290 dev = dev_get_by_name(&init_net, ifname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800291 if (!dev) {
292 printk(KERN_INFO DRV_NAME
293 ": %s: Interface %s does not exist!\n",
294 bond->dev->name, ifname);
295 ret = -EPERM;
296 goto out;
297 }
298 else
299 dev_put(dev);
300
301 if (dev->flags & IFF_UP) {
302 printk(KERN_ERR DRV_NAME
303 ": %s: Error: Unable to enslave %s "
304 "because it is already up.\n",
305 bond->dev->name, dev->name);
306 ret = -EPERM;
307 goto out;
308 }
309 /* If this is the first slave, then we need to set
310 the master's hardware address to be the same as the
311 slave's. */
312 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
313 memcpy(bond->dev->dev_addr, dev->dev_addr,
314 dev->addr_len);
315 }
316
317 /* Set the slave's MTU to match the bond */
Moni Shoua3158bf72007-10-09 19:43:41 -0700318 original_mtu = dev->mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800319 if (dev->mtu != bond->dev->mtu) {
320 if (dev->change_mtu) {
321 res = dev->change_mtu(dev,
322 bond->dev->mtu);
323 if (res) {
324 ret = res;
325 goto out;
326 }
327 } else {
328 dev->mtu = bond->dev->mtu;
329 }
330 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800331 res = bond_enslave(bond->dev, dev);
Moni Shoua3158bf72007-10-09 19:43:41 -0700332 bond_for_each_slave(bond, slave, i)
333 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
334 slave->original_mtu = original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800335 if (res) {
336 ret = res;
337 }
338 goto out;
339 }
340
341 if (command[0] == '-') {
342 dev = NULL;
David S. Miller6952d8922008-03-28 16:15:38 -0700343 original_mtu = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800344 bond_for_each_slave(bond, slave, i)
345 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
346 dev = slave->dev;
Moni Shoua3158bf72007-10-09 19:43:41 -0700347 original_mtu = slave->original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800348 break;
349 }
350 if (dev) {
351 printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n",
352 bond->dev->name, dev->name);
Moni Shouad90a1622007-10-09 19:43:43 -0700353 res = bond_release(bond->dev, dev);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800354 if (res) {
355 ret = res;
356 goto out;
357 }
358 /* set the slave MTU to the default */
359 if (dev->change_mtu) {
Moni Shoua3158bf72007-10-09 19:43:41 -0700360 dev->change_mtu(dev, original_mtu);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800361 } else {
Moni Shoua3158bf72007-10-09 19:43:41 -0700362 dev->mtu = original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800363 }
364 }
365 else {
366 printk(KERN_ERR DRV_NAME ": unable to remove non-existent slave %s for bond %s.\n",
367 ifname, bond->dev->name);
368 ret = -ENODEV;
369 }
370 goto out;
371 }
372
373err_no_cmd:
374 printk(KERN_ERR DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
375 ret = -EPERM;
376
377out:
Jay Vosburgh027ea042008-01-17 16:25:02 -0800378 up_write(&(bonding_rwsem));
379 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800380 return ret;
381}
382
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700383static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves, bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800384
385/*
386 * Show and set the bonding mode. The bond interface must be down to
387 * change the mode.
388 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700389static ssize_t bonding_show_mode(struct device *d,
390 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800391{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700392 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800393
394 return sprintf(buf, "%s %d\n",
395 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800396 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800397}
398
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700399static ssize_t bonding_store_mode(struct device *d,
400 struct device_attribute *attr,
401 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800402{
403 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700404 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800405
406 if (bond->dev->flags & IFF_UP) {
407 printk(KERN_ERR DRV_NAME
408 ": unable to update mode of %s because interface is up.\n",
409 bond->dev->name);
410 ret = -EPERM;
411 goto out;
412 }
413
Jay Vosburghece95f72008-01-17 16:25:01 -0800414 new_value = bond_parse_parm(buf, bond_mode_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800415 if (new_value < 0) {
416 printk(KERN_ERR DRV_NAME
417 ": %s: Ignoring invalid mode value %.*s.\n",
418 bond->dev->name,
419 (int)strlen(buf) - 1, buf);
420 ret = -EINVAL;
421 goto out;
422 } else {
Jay Vosburgh8f903c72006-02-21 16:36:44 -0800423 if (bond->params.mode == BOND_MODE_8023AD)
424 bond_unset_master_3ad_flags(bond);
425
426 if (bond->params.mode == BOND_MODE_ALB)
427 bond_unset_master_alb_flags(bond);
428
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800429 bond->params.mode = new_value;
430 bond_set_mode_ops(bond, bond->params.mode);
431 printk(KERN_INFO DRV_NAME ": %s: setting mode to %s (%d).\n",
432 bond->dev->name, bond_mode_tbl[new_value].modename, new_value);
433 }
434out:
435 return ret;
436}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700437static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800438
439/*
440 * Show and set the bonding transmit hash method. The bond interface must be down to
441 * change the xmit hash policy.
442 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700443static ssize_t bonding_show_xmit_hash(struct device *d,
444 struct device_attribute *attr,
445 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800446{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700447 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800448
Wagner Ferenc8e4b9322007-12-06 23:40:32 -0800449 return sprintf(buf, "%s %d\n",
450 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
451 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800452}
453
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700454static ssize_t bonding_store_xmit_hash(struct device *d,
455 struct device_attribute *attr,
456 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800457{
458 int new_value, ret = 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->dev->flags & IFF_UP) {
462 printk(KERN_ERR DRV_NAME
463 "%s: Interface is up. Unable to update xmit policy.\n",
464 bond->dev->name);
465 ret = -EPERM;
466 goto out;
467 }
468
Jay Vosburghece95f72008-01-17 16:25:01 -0800469 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800470 if (new_value < 0) {
471 printk(KERN_ERR DRV_NAME
472 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
473 bond->dev->name,
474 (int)strlen(buf) - 1, buf);
475 ret = -EINVAL;
476 goto out;
477 } else {
478 bond->params.xmit_policy = new_value;
479 bond_set_mode_ops(bond, bond->params.mode);
480 printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
481 bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value);
482 }
483out:
484 return ret;
485}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700486static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800487
488/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700489 * Show and set arp_validate.
490 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700491static ssize_t bonding_show_arp_validate(struct device *d,
492 struct device_attribute *attr,
493 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700494{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700495 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700496
497 return sprintf(buf, "%s %d\n",
498 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800499 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700500}
501
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700502static ssize_t bonding_store_arp_validate(struct device *d,
503 struct device_attribute *attr,
504 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700505{
506 int new_value;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700507 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700508
Jay Vosburghece95f72008-01-17 16:25:01 -0800509 new_value = bond_parse_parm(buf, arp_validate_tbl);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700510 if (new_value < 0) {
511 printk(KERN_ERR DRV_NAME
512 ": %s: Ignoring invalid arp_validate value %s\n",
513 bond->dev->name, buf);
514 return -EINVAL;
515 }
516 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
517 printk(KERN_ERR DRV_NAME
518 ": %s: arp_validate only supported in active-backup mode.\n",
519 bond->dev->name);
520 return -EINVAL;
521 }
522 printk(KERN_INFO DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
523 bond->dev->name, arp_validate_tbl[new_value].modename,
524 new_value);
525
526 if (!bond->params.arp_validate && new_value) {
527 bond_register_arp(bond);
528 } else if (bond->params.arp_validate && !new_value) {
529 bond_unregister_arp(bond);
530 }
531
532 bond->params.arp_validate = new_value;
533
534 return count;
535}
536
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700537static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate, bonding_store_arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700538
539/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700540 * Show and store fail_over_mac. User only allowed to change the
541 * value when there are no slaves.
542 */
543static ssize_t bonding_show_fail_over_mac(struct device *d, struct device_attribute *attr, char *buf)
544{
545 struct bonding *bond = to_bond(d);
546
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700547 return sprintf(buf, "%s %d\n",
548 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
549 bond->params.fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700550}
551
552static ssize_t bonding_store_fail_over_mac(struct device *d, struct device_attribute *attr, const char *buf, size_t count)
553{
554 int new_value;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700555 struct bonding *bond = to_bond(d);
556
557 if (bond->slave_cnt != 0) {
558 printk(KERN_ERR DRV_NAME
559 ": %s: Can't alter fail_over_mac with slaves in bond.\n",
560 bond->dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700561 return -EPERM;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700562 }
563
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700564 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
565 if (new_value < 0) {
Jay Vosburghdd957c52007-10-09 19:57:24 -0700566 printk(KERN_ERR DRV_NAME
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700567 ": %s: Ignoring invalid fail_over_mac value %s.\n",
568 bond->dev->name, buf);
569 return -EINVAL;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700570 }
571
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700572 bond->params.fail_over_mac = new_value;
573 printk(KERN_INFO DRV_NAME ": %s: Setting fail_over_mac to %s (%d).\n",
574 bond->dev->name, fail_over_mac_tbl[new_value].modename,
575 new_value);
576
577 return count;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700578}
579
580static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR, bonding_show_fail_over_mac, bonding_store_fail_over_mac);
581
582/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800583 * Show and set the arp timer interval. There are two tricky bits
584 * here. First, if ARP monitoring is activated, then we must disable
585 * MII monitoring. Second, if the ARP timer isn't running, we must
586 * start it.
587 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700588static ssize_t bonding_show_arp_interval(struct device *d,
589 struct device_attribute *attr,
590 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800591{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700592 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800593
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800594 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800595}
596
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700597static ssize_t bonding_store_arp_interval(struct device *d,
598 struct device_attribute *attr,
599 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800600{
601 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700602 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800603
604 if (sscanf(buf, "%d", &new_value) != 1) {
605 printk(KERN_ERR DRV_NAME
606 ": %s: no arp_interval value specified.\n",
607 bond->dev->name);
608 ret = -EINVAL;
609 goto out;
610 }
611 if (new_value < 0) {
612 printk(KERN_ERR DRV_NAME
613 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
614 bond->dev->name, new_value, INT_MAX);
615 ret = -EINVAL;
616 goto out;
617 }
618
619 printk(KERN_INFO DRV_NAME
620 ": %s: Setting ARP monitoring interval to %d.\n",
621 bond->dev->name, new_value);
622 bond->params.arp_interval = new_value;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -0800623 if (bond->params.arp_interval)
624 bond->dev->priv_flags |= IFF_MASTER_ARPMON;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800625 if (bond->params.miimon) {
626 printk(KERN_INFO DRV_NAME
627 ": %s: ARP monitoring cannot be used with MII monitoring. "
628 "%s Disabling MII monitoring.\n",
629 bond->dev->name, bond->dev->name);
630 bond->params.miimon = 0;
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700631 if (delayed_work_pending(&bond->mii_work)) {
632 cancel_delayed_work(&bond->mii_work);
633 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800634 }
635 }
636 if (!bond->params.arp_targets[0]) {
637 printk(KERN_INFO DRV_NAME
638 ": %s: ARP monitoring has been set up, "
639 "but no ARP targets have been specified.\n",
640 bond->dev->name);
641 }
642 if (bond->dev->flags & IFF_UP) {
643 /* If the interface is up, we may need to fire off
644 * the ARP timer. If the interface is down, the
645 * timer will get fired off when the open function
646 * is called.
647 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700648 if (!delayed_work_pending(&bond->arp_work)) {
649 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
650 INIT_DELAYED_WORK(&bond->arp_work,
651 bond_activebackup_arp_mon);
652 else
653 INIT_DELAYED_WORK(&bond->arp_work,
654 bond_loadbalance_arp_mon);
655
656 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800657 }
658 }
659
660out:
661 return ret;
662}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700663static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800664
665/*
666 * Show and set the arp targets.
667 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700668static ssize_t bonding_show_arp_targets(struct device *d,
669 struct device_attribute *attr,
670 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800671{
672 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700673 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800674
675 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
676 if (bond->params.arp_targets[i])
Harvey Harrison63779432008-10-31 00:56:00 -0700677 res += sprintf(buf + res, "%pI4 ",
678 &bond->params.arp_targets[i]);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800679 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800680 if (res)
681 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800682 return res;
683}
684
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700685static ssize_t bonding_store_arp_targets(struct device *d,
686 struct device_attribute *attr,
687 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800688{
Al Virod3bb52b2007-08-22 20:06:58 -0400689 __be32 newtarget;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800690 int i = 0, done = 0, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700691 struct bonding *bond = to_bond(d);
Al Virod3bb52b2007-08-22 20:06:58 -0400692 __be32 *targets;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800693
694 targets = bond->params.arp_targets;
695 newtarget = in_aton(buf + 1);
696 /* look for adds */
697 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400698 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800699 printk(KERN_ERR DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700700 ": %s: invalid ARP target %pI4 specified for addition\n",
701 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800702 ret = -EINVAL;
703 goto out;
704 }
705 /* look for an empty slot to put the target in, and check for dupes */
706 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
707 if (targets[i] == newtarget) { /* duplicate */
708 printk(KERN_ERR DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700709 ": %s: ARP target %pI4 is already present\n",
710 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800711 if (done)
712 targets[i] = 0;
713 ret = -EINVAL;
714 goto out;
715 }
716 if (targets[i] == 0 && !done) {
717 printk(KERN_INFO DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700718 ": %s: adding ARP target %pI4.\n",
719 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800720 done = 1;
721 targets[i] = newtarget;
722 }
723 }
724 if (!done) {
725 printk(KERN_ERR DRV_NAME
726 ": %s: ARP target table is full!\n",
727 bond->dev->name);
728 ret = -EINVAL;
729 goto out;
730 }
731
732 }
733 else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400734 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800735 printk(KERN_ERR DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700736 ": %s: invalid ARP target %pI4 specified for removal\n",
737 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800738 ret = -EINVAL;
739 goto out;
740 }
741
742 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
743 if (targets[i] == newtarget) {
744 printk(KERN_INFO DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700745 ": %s: removing ARP target %pI4.\n",
746 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800747 targets[i] = 0;
748 done = 1;
749 }
750 }
751 if (!done) {
752 printk(KERN_INFO DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700753 ": %s: unable to remove nonexistent ARP target %pI4.\n",
754 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800755 ret = -EINVAL;
756 goto out;
757 }
758 }
759 else {
760 printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
761 bond->dev->name);
762 ret = -EPERM;
763 goto out;
764 }
765
766out:
767 return ret;
768}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700769static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800770
771/*
772 * Show and set the up and down delays. These must be multiples of the
773 * MII monitoring value, and are stored internally as the multiplier.
774 * Thus, we must translate to MS for the real world.
775 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700776static ssize_t bonding_show_downdelay(struct device *d,
777 struct device_attribute *attr,
778 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800779{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700780 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800781
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800782 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800783}
784
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700785static ssize_t bonding_store_downdelay(struct device *d,
786 struct device_attribute *attr,
787 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800788{
789 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700790 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800791
792 if (!(bond->params.miimon)) {
793 printk(KERN_ERR DRV_NAME
794 ": %s: Unable to set down delay as MII monitoring is disabled\n",
795 bond->dev->name);
796 ret = -EPERM;
797 goto out;
798 }
799
800 if (sscanf(buf, "%d", &new_value) != 1) {
801 printk(KERN_ERR DRV_NAME
802 ": %s: no down delay value specified.\n",
803 bond->dev->name);
804 ret = -EINVAL;
805 goto out;
806 }
807 if (new_value < 0) {
808 printk(KERN_ERR DRV_NAME
809 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
810 bond->dev->name, new_value, 1, INT_MAX);
811 ret = -EINVAL;
812 goto out;
813 } else {
814 if ((new_value % bond->params.miimon) != 0) {
815 printk(KERN_WARNING DRV_NAME
816 ": %s: Warning: down delay (%d) is not a multiple "
817 "of miimon (%d), delay rounded to %d ms\n",
818 bond->dev->name, new_value, bond->params.miimon,
819 (new_value / bond->params.miimon) *
820 bond->params.miimon);
821 }
822 bond->params.downdelay = new_value / bond->params.miimon;
823 printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n",
824 bond->dev->name, bond->params.downdelay * bond->params.miimon);
825
826 }
827
828out:
829 return ret;
830}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700831static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800832
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700833static ssize_t bonding_show_updelay(struct device *d,
834 struct device_attribute *attr,
835 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800836{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700837 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800838
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800839 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800840
841}
842
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700843static ssize_t bonding_store_updelay(struct device *d,
844 struct device_attribute *attr,
845 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800846{
847 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700848 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800849
850 if (!(bond->params.miimon)) {
851 printk(KERN_ERR DRV_NAME
852 ": %s: Unable to set up delay as MII monitoring is disabled\n",
853 bond->dev->name);
854 ret = -EPERM;
855 goto out;
856 }
857
858 if (sscanf(buf, "%d", &new_value) != 1) {
859 printk(KERN_ERR DRV_NAME
860 ": %s: no up delay value specified.\n",
861 bond->dev->name);
862 ret = -EINVAL;
863 goto out;
864 }
865 if (new_value < 0) {
866 printk(KERN_ERR DRV_NAME
867 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
868 bond->dev->name, new_value, 1, INT_MAX);
869 ret = -EINVAL;
870 goto out;
871 } else {
872 if ((new_value % bond->params.miimon) != 0) {
873 printk(KERN_WARNING DRV_NAME
874 ": %s: Warning: up delay (%d) is not a multiple "
875 "of miimon (%d), updelay rounded to %d ms\n",
876 bond->dev->name, new_value, bond->params.miimon,
877 (new_value / bond->params.miimon) *
878 bond->params.miimon);
879 }
880 bond->params.updelay = new_value / bond->params.miimon;
881 printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n",
882 bond->dev->name, bond->params.updelay * bond->params.miimon);
883
884 }
885
886out:
887 return ret;
888}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700889static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800890
891/*
892 * Show and set the LACP interval. Interface must be down, and the mode
893 * must be set to 802.3ad mode.
894 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700895static ssize_t bonding_show_lacp(struct device *d,
896 struct device_attribute *attr,
897 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800898{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700899 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800900
901 return sprintf(buf, "%s %d\n",
902 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800903 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800904}
905
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700906static ssize_t bonding_store_lacp(struct device *d,
907 struct device_attribute *attr,
908 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800909{
910 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700911 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800912
913 if (bond->dev->flags & IFF_UP) {
914 printk(KERN_ERR DRV_NAME
915 ": %s: Unable to update LACP rate because interface is up.\n",
916 bond->dev->name);
917 ret = -EPERM;
918 goto out;
919 }
920
921 if (bond->params.mode != BOND_MODE_8023AD) {
922 printk(KERN_ERR DRV_NAME
923 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
924 bond->dev->name);
925 ret = -EPERM;
926 goto out;
927 }
928
Jay Vosburghece95f72008-01-17 16:25:01 -0800929 new_value = bond_parse_parm(buf, bond_lacp_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800930
931 if ((new_value == 1) || (new_value == 0)) {
932 bond->params.lacp_fast = new_value;
933 printk(KERN_INFO DRV_NAME
934 ": %s: Setting LACP rate to %s (%d).\n",
935 bond->dev->name, bond_lacp_tbl[new_value].modename, new_value);
936 } else {
937 printk(KERN_ERR DRV_NAME
938 ": %s: Ignoring invalid LACP rate value %.*s.\n",
939 bond->dev->name, (int)strlen(buf) - 1, buf);
940 ret = -EINVAL;
941 }
942out:
943 return ret;
944}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700945static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800946
947/*
Moni Shoua7893b242008-05-17 21:10:12 -0700948 * Show and set the number of grat ARP to send after a failover event.
949 */
950static ssize_t bonding_show_n_grat_arp(struct device *d,
951 struct device_attribute *attr,
952 char *buf)
953{
954 struct bonding *bond = to_bond(d);
955
956 return sprintf(buf, "%d\n", bond->params.num_grat_arp);
957}
958
959static ssize_t bonding_store_n_grat_arp(struct device *d,
960 struct device_attribute *attr,
961 const char *buf, size_t count)
962{
963 int new_value, ret = count;
964 struct bonding *bond = to_bond(d);
965
966 if (sscanf(buf, "%d", &new_value) != 1) {
967 printk(KERN_ERR DRV_NAME
968 ": %s: no num_grat_arp value specified.\n",
969 bond->dev->name);
970 ret = -EINVAL;
971 goto out;
972 }
973 if (new_value < 0 || new_value > 255) {
974 printk(KERN_ERR DRV_NAME
975 ": %s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
976 bond->dev->name, new_value);
977 ret = -EINVAL;
978 goto out;
979 } else {
980 bond->params.num_grat_arp = new_value;
981 }
982out:
983 return ret;
984}
985static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR, bonding_show_n_grat_arp, bonding_store_n_grat_arp);
Brian Haley305d5522008-11-04 17:51:14 -0800986
987/*
988 * Show and set the number of unsolicted NA's to send after a failover event.
989 */
990static ssize_t bonding_show_n_unsol_na(struct device *d,
991 struct device_attribute *attr,
992 char *buf)
993{
994 struct bonding *bond = to_bond(d);
995
996 return sprintf(buf, "%d\n", bond->params.num_unsol_na);
997}
998
999static ssize_t bonding_store_n_unsol_na(struct device *d,
1000 struct device_attribute *attr,
1001 const char *buf, size_t count)
1002{
1003 int new_value, ret = count;
1004 struct bonding *bond = to_bond(d);
1005
1006 if (sscanf(buf, "%d", &new_value) != 1) {
1007 printk(KERN_ERR DRV_NAME
1008 ": %s: no num_unsol_na value specified.\n",
1009 bond->dev->name);
1010 ret = -EINVAL;
1011 goto out;
1012 }
1013 if (new_value < 0 || new_value > 255) {
1014 printk(KERN_ERR DRV_NAME
1015 ": %s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
1016 bond->dev->name, new_value);
1017 ret = -EINVAL;
1018 goto out;
1019 } else {
1020 bond->params.num_unsol_na = new_value;
1021 }
1022out:
1023 return ret;
1024}
1025static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR, bonding_show_n_unsol_na, bonding_store_n_unsol_na);
1026
Moni Shoua7893b242008-05-17 21:10:12 -07001027/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001028 * Show and set the MII monitor interval. There are two tricky bits
1029 * here. First, if MII monitoring is activated, then we must disable
1030 * ARP monitoring. Second, if the timer isn't running, we must
1031 * start it.
1032 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001033static ssize_t bonding_show_miimon(struct device *d,
1034 struct device_attribute *attr,
1035 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001036{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001037 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001038
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001039 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001040}
1041
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001042static ssize_t bonding_store_miimon(struct device *d,
1043 struct device_attribute *attr,
1044 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001045{
1046 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001047 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001048
1049 if (sscanf(buf, "%d", &new_value) != 1) {
1050 printk(KERN_ERR DRV_NAME
1051 ": %s: no miimon value specified.\n",
1052 bond->dev->name);
1053 ret = -EINVAL;
1054 goto out;
1055 }
1056 if (new_value < 0) {
1057 printk(KERN_ERR DRV_NAME
1058 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1059 bond->dev->name, new_value, 1, INT_MAX);
1060 ret = -EINVAL;
1061 goto out;
1062 } else {
1063 printk(KERN_INFO DRV_NAME
1064 ": %s: Setting MII monitoring interval to %d.\n",
1065 bond->dev->name, new_value);
1066 bond->params.miimon = new_value;
1067 if(bond->params.updelay)
1068 printk(KERN_INFO DRV_NAME
1069 ": %s: Note: Updating updelay (to %d) "
1070 "since it is a multiple of the miimon value.\n",
1071 bond->dev->name,
1072 bond->params.updelay * bond->params.miimon);
1073 if(bond->params.downdelay)
1074 printk(KERN_INFO DRV_NAME
1075 ": %s: Note: Updating downdelay (to %d) "
1076 "since it is a multiple of the miimon value.\n",
1077 bond->dev->name,
1078 bond->params.downdelay * bond->params.miimon);
1079 if (bond->params.arp_interval) {
1080 printk(KERN_INFO DRV_NAME
1081 ": %s: MII monitoring cannot be used with "
1082 "ARP monitoring. Disabling ARP monitoring...\n",
1083 bond->dev->name);
1084 bond->params.arp_interval = 0;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -08001085 bond->dev->priv_flags &= ~IFF_MASTER_ARPMON;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001086 if (bond->params.arp_validate) {
1087 bond_unregister_arp(bond);
1088 bond->params.arp_validate =
1089 BOND_ARP_VALIDATE_NONE;
1090 }
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001091 if (delayed_work_pending(&bond->arp_work)) {
1092 cancel_delayed_work(&bond->arp_work);
1093 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001094 }
1095 }
1096
1097 if (bond->dev->flags & IFF_UP) {
1098 /* If the interface is up, we may need to fire off
1099 * the MII timer. If the interface is down, the
1100 * timer will get fired off when the open function
1101 * is called.
1102 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001103 if (!delayed_work_pending(&bond->mii_work)) {
1104 INIT_DELAYED_WORK(&bond->mii_work,
1105 bond_mii_monitor);
1106 queue_delayed_work(bond->wq,
1107 &bond->mii_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001108 }
1109 }
1110 }
1111out:
1112 return ret;
1113}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001114static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001115
1116/*
1117 * Show and set the primary slave. The store function is much
1118 * simpler than bonding_store_slaves function because it only needs to
1119 * handle one interface name.
1120 * The bond must be a mode that supports a primary for this be
1121 * set.
1122 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001123static ssize_t bonding_show_primary(struct device *d,
1124 struct device_attribute *attr,
1125 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001126{
1127 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001128 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001129
1130 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001131 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001132
1133 return count;
1134}
1135
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001136static ssize_t bonding_store_primary(struct device *d,
1137 struct device_attribute *attr,
1138 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001139{
1140 int i;
1141 struct slave *slave;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001142 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001143
Jay Vosburghe934dd72008-01-17 16:24:57 -08001144 rtnl_lock();
1145 read_lock(&bond->lock);
1146 write_lock_bh(&bond->curr_slave_lock);
1147
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001148 if (!USES_PRIMARY(bond->params.mode)) {
1149 printk(KERN_INFO DRV_NAME
1150 ": %s: Unable to set primary slave; %s is in mode %d\n",
1151 bond->dev->name, bond->dev->name, bond->params.mode);
1152 } else {
1153 bond_for_each_slave(bond, slave, i) {
1154 if (strnicmp
1155 (slave->dev->name, buf,
1156 strlen(slave->dev->name)) == 0) {
1157 printk(KERN_INFO DRV_NAME
1158 ": %s: Setting %s as primary slave.\n",
1159 bond->dev->name, slave->dev->name);
1160 bond->primary_slave = slave;
1161 bond_select_active_slave(bond);
1162 goto out;
1163 }
1164 }
1165
1166 /* if we got here, then we didn't match the name of any slave */
1167
1168 if (strlen(buf) == 0 || buf[0] == '\n') {
1169 printk(KERN_INFO DRV_NAME
1170 ": %s: Setting primary slave to None.\n",
1171 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001172 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001173 bond_select_active_slave(bond);
1174 } else {
1175 printk(KERN_INFO DRV_NAME
1176 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1177 bond->dev->name, (int)strlen(buf) - 1, buf);
1178 }
1179 }
1180out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001181 write_unlock_bh(&bond->curr_slave_lock);
1182 read_unlock(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001183 rtnl_unlock();
1184
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001185 return count;
1186}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001187static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001188
1189/*
1190 * Show and set the use_carrier flag.
1191 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001192static ssize_t bonding_show_carrier(struct device *d,
1193 struct device_attribute *attr,
1194 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001195{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001196 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001197
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001198 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001199}
1200
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001201static ssize_t bonding_store_carrier(struct device *d,
1202 struct device_attribute *attr,
1203 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001204{
1205 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001206 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001207
1208
1209 if (sscanf(buf, "%d", &new_value) != 1) {
1210 printk(KERN_ERR DRV_NAME
1211 ": %s: no use_carrier value specified.\n",
1212 bond->dev->name);
1213 ret = -EINVAL;
1214 goto out;
1215 }
1216 if ((new_value == 0) || (new_value == 1)) {
1217 bond->params.use_carrier = new_value;
1218 printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
1219 bond->dev->name, new_value);
1220 } else {
1221 printk(KERN_INFO DRV_NAME
1222 ": %s: Ignoring invalid use_carrier value %d.\n",
1223 bond->dev->name, new_value);
1224 }
1225out:
1226 return count;
1227}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001228static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001229
1230
1231/*
1232 * Show and set currently active_slave.
1233 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001234static ssize_t bonding_show_active_slave(struct device *d,
1235 struct device_attribute *attr,
1236 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001237{
1238 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001239 struct bonding *bond = to_bond(d);
Wagner Ferenc16cd0162007-12-06 23:40:29 -08001240 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001241
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001242 read_lock(&bond->curr_slave_lock);
1243 curr = bond->curr_active_slave;
1244 read_unlock(&bond->curr_slave_lock);
1245
1246 if (USES_PRIMARY(bond->params.mode) && curr)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001247 count = sprintf(buf, "%s\n", curr->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001248 return count;
1249}
1250
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001251static ssize_t bonding_store_active_slave(struct device *d,
1252 struct device_attribute *attr,
1253 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001254{
1255 int i;
1256 struct slave *slave;
1257 struct slave *old_active = NULL;
1258 struct slave *new_active = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001259 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001260
Jay Vosburgh1466a212007-11-06 13:33:28 -08001261 rtnl_lock();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001262 read_lock(&bond->lock);
1263 write_lock_bh(&bond->curr_slave_lock);
Jay Vosburgh1466a212007-11-06 13:33:28 -08001264
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001265 if (!USES_PRIMARY(bond->params.mode)) {
1266 printk(KERN_INFO DRV_NAME
1267 ": %s: Unable to change active slave; %s is in mode %d\n",
1268 bond->dev->name, bond->dev->name, bond->params.mode);
1269 } else {
1270 bond_for_each_slave(bond, slave, i) {
1271 if (strnicmp
1272 (slave->dev->name, buf,
1273 strlen(slave->dev->name)) == 0) {
1274 old_active = bond->curr_active_slave;
1275 new_active = slave;
Jay Vosburgha50d8de2006-09-22 21:53:25 -07001276 if (new_active == old_active) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001277 /* do nothing */
1278 printk(KERN_INFO DRV_NAME
1279 ": %s: %s is already the current active slave.\n",
1280 bond->dev->name, slave->dev->name);
1281 goto out;
1282 }
1283 else {
1284 if ((new_active) &&
1285 (old_active) &&
1286 (new_active->link == BOND_LINK_UP) &&
1287 IS_UP(new_active->dev)) {
1288 printk(KERN_INFO DRV_NAME
1289 ": %s: Setting %s as active slave.\n",
1290 bond->dev->name, slave->dev->name);
1291 bond_change_active_slave(bond, new_active);
1292 }
1293 else {
1294 printk(KERN_INFO DRV_NAME
1295 ": %s: Could not set %s as active slave; "
1296 "either %s is down or the link is down.\n",
1297 bond->dev->name, slave->dev->name,
1298 slave->dev->name);
1299 }
1300 goto out;
1301 }
1302 }
1303 }
1304
1305 /* if we got here, then we didn't match the name of any slave */
1306
1307 if (strlen(buf) == 0 || buf[0] == '\n') {
1308 printk(KERN_INFO DRV_NAME
1309 ": %s: Setting active slave to None.\n",
1310 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001311 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001312 bond_select_active_slave(bond);
1313 } else {
1314 printk(KERN_INFO DRV_NAME
1315 ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
1316 bond->dev->name, (int)strlen(buf) - 1, buf);
1317 }
1318 }
1319out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001320 write_unlock_bh(&bond->curr_slave_lock);
1321 read_unlock(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001322 rtnl_unlock();
1323
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001324 return count;
1325
1326}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001327static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001328
1329
1330/*
1331 * Show link status of the bond interface.
1332 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001333static ssize_t bonding_show_mii_status(struct device *d,
1334 struct device_attribute *attr,
1335 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001336{
1337 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001338 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001339
1340 read_lock(&bond->curr_slave_lock);
1341 curr = bond->curr_active_slave;
1342 read_unlock(&bond->curr_slave_lock);
1343
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001344 return sprintf(buf, "%s\n", (curr) ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001345}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001346static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001347
1348
1349/*
1350 * Show current 802.3ad aggregator ID.
1351 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001352static ssize_t bonding_show_ad_aggregator(struct device *d,
1353 struct device_attribute *attr,
1354 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001355{
1356 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001357 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001358
1359 if (bond->params.mode == BOND_MODE_8023AD) {
1360 struct ad_info ad_info;
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001361 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 -08001362 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001363
1364 return count;
1365}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001366static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001367
1368
1369/*
1370 * Show number of active 802.3ad ports.
1371 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001372static ssize_t bonding_show_ad_num_ports(struct device *d,
1373 struct device_attribute *attr,
1374 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001375{
1376 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001377 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001378
1379 if (bond->params.mode == BOND_MODE_8023AD) {
1380 struct ad_info ad_info;
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001381 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 -08001382 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001383
1384 return count;
1385}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001386static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001387
1388
1389/*
1390 * Show current 802.3ad actor key.
1391 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001392static ssize_t bonding_show_ad_actor_key(struct device *d,
1393 struct device_attribute *attr,
1394 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001395{
1396 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001397 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001398
1399 if (bond->params.mode == BOND_MODE_8023AD) {
1400 struct ad_info ad_info;
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001401 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 -08001402 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001403
1404 return count;
1405}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001406static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001407
1408
1409/*
1410 * Show current 802.3ad partner key.
1411 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001412static ssize_t bonding_show_ad_partner_key(struct device *d,
1413 struct device_attribute *attr,
1414 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001415{
1416 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001417 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001418
1419 if (bond->params.mode == BOND_MODE_8023AD) {
1420 struct ad_info ad_info;
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001421 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 -08001422 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001423
1424 return count;
1425}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001426static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001427
1428
1429/*
1430 * Show current 802.3ad partner mac.
1431 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001432static ssize_t bonding_show_ad_partner_mac(struct device *d,
1433 struct device_attribute *attr,
1434 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001435{
1436 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001437 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001438
1439 if (bond->params.mode == BOND_MODE_8023AD) {
1440 struct ad_info ad_info;
1441 if (!bond_3ad_get_active_agg_info(bond, &ad_info)) {
Johannes Berge1749612008-10-27 15:59:26 -07001442 count = sprintf(buf, "%pM\n", ad_info.partner_system);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001443 }
1444 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001445
1446 return count;
1447}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001448static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001449
1450
1451
1452static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001453 &dev_attr_slaves.attr,
1454 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001455 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001456 &dev_attr_arp_validate.attr,
1457 &dev_attr_arp_interval.attr,
1458 &dev_attr_arp_ip_target.attr,
1459 &dev_attr_downdelay.attr,
1460 &dev_attr_updelay.attr,
1461 &dev_attr_lacp_rate.attr,
1462 &dev_attr_xmit_hash_policy.attr,
Moni Shoua7893b242008-05-17 21:10:12 -07001463 &dev_attr_num_grat_arp.attr,
Brian Haley305d5522008-11-04 17:51:14 -08001464 &dev_attr_num_unsol_na.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001465 &dev_attr_miimon.attr,
1466 &dev_attr_primary.attr,
1467 &dev_attr_use_carrier.attr,
1468 &dev_attr_active_slave.attr,
1469 &dev_attr_mii_status.attr,
1470 &dev_attr_ad_aggregator.attr,
1471 &dev_attr_ad_num_ports.attr,
1472 &dev_attr_ad_actor_key.attr,
1473 &dev_attr_ad_partner_key.attr,
1474 &dev_attr_ad_partner_mac.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001475 NULL,
1476};
1477
1478static struct attribute_group bonding_group = {
1479 .name = "bonding",
1480 .attrs = per_bond_attrs,
1481};
1482
1483/*
1484 * Initialize sysfs. This sets up the bonding_masters file in
1485 * /sys/class/net.
1486 */
1487int bond_create_sysfs(void)
1488{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001489 int ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001490
Jay Vosburghb8a97872008-06-13 18:12:04 -07001491 ret = netdev_class_create_file(&class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001492 /*
1493 * Permit multiple loads of the module by ignoring failures to
1494 * create the bonding_masters sysfs file. Bonding devices
1495 * created by second or subsequent loads of the module will
1496 * not be listed in, or controllable by, bonding_masters, but
1497 * will have the usual "bonding" sysfs directory.
1498 *
1499 * This is done to preserve backwards compatibility for
1500 * initscripts/sysconfig, which load bonding multiple times to
1501 * configure multiple bonding devices.
1502 */
1503 if (ret == -EEXIST) {
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001504 /* Is someone being kinky and naming a device bonding_master? */
1505 if (__dev_get_by_name(&init_net,
1506 class_attr_bonding_masters.attr.name))
1507 printk(KERN_ERR
1508 "network device named %s already exists in sysfs",
1509 class_attr_bonding_masters.attr.name);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001510 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001511
1512 return ret;
1513
1514}
1515
1516/*
1517 * Remove /sys/class/net/bonding_masters.
1518 */
1519void bond_destroy_sysfs(void)
1520{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001521 netdev_class_remove_file(&class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001522}
1523
1524/*
1525 * Initialize sysfs for each bond. This sets up and registers
1526 * the 'bondctl' directory for each individual bond under /sys/class/net.
1527 */
1528int bond_create_sysfs_entry(struct bonding *bond)
1529{
1530 struct net_device *dev = bond->dev;
1531 int err;
1532
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001533 err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001534 if (err) {
1535 printk(KERN_EMERG "eek! didn't create group!\n");
1536 }
1537
1538 if (expected_refcount < 1)
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001539 expected_refcount = atomic_read(&bond->dev->dev.kobj.kref.refcount);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001540
1541 return err;
1542}
1543/*
1544 * Remove sysfs entries for each bond.
1545 */
1546void bond_destroy_sysfs_entry(struct bonding *bond)
1547{
1548 struct net_device *dev = bond->dev;
1549
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001550 sysfs_remove_group(&(dev->dev.kobj), &bonding_group);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001551}
1552