blob: ecb20208f67cef9506ad3a4a06c28284d60d3e3b [file] [log] [blame]
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001/*
2 * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
Mitch Williamsb76cdba2005-11-09 10:36:41 -080021 */
Mitch Williamsb76cdba2005-11-09 10:36:41 -080022#include <linux/kernel.h>
23#include <linux/module.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080024#include <linux/device.h>
25#include <linux/sysdev.h>
26#include <linux/fs.h>
27#include <linux/types.h>
28#include <linux/string.h>
29#include <linux/netdevice.h>
30#include <linux/inetdevice.h>
31#include <linux/in.h>
32#include <linux/sysfs.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080033#include <linux/ctype.h>
34#include <linux/inet.h>
35#include <linux/rtnetlink.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070036#include <net/net_namespace.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080037
Mitch Williamsb76cdba2005-11-09 10:36:41 -080038#include "bonding.h"
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -080039
Stephen Hemminger3d632c32009-06-12 19:02:48 +000040#define to_dev(obj) container_of(obj, struct device, kobj)
Wang Chen454d7c92008-11-12 23:37:49 -080041#define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
Mitch Williamsb76cdba2005-11-09 10:36:41 -080042
43/*---------------------------- Declarations -------------------------------*/
44
Mitch Williamsb76cdba2005-11-09 10:36:41 -080045static int expected_refcount = -1;
Mitch Williamsb76cdba2005-11-09 10:36:41 -080046
47/*------------------------------ Functions --------------------------------*/
48
49/*
50 * "show" function for the bond_masters attribute.
51 * The class parameter is ignored.
52 */
Wagner Ferencb8843662007-12-06 23:40:30 -080053static ssize_t bonding_show_bonds(struct class *cls, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080054{
55 int res = 0;
56 struct bonding *bond;
57
Stephen Hemminger7e083842009-06-12 19:02:46 +000058 rtnl_lock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080059
60 list_for_each_entry(bond, &bond_dev_list, bond_list) {
61 if (res > (PAGE_SIZE - IFNAMSIZ)) {
62 /* not enough space for another interface name */
63 if ((PAGE_SIZE - res) > 10)
64 res = PAGE_SIZE - 10;
Wagner Ferencb8843662007-12-06 23:40:30 -080065 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -080066 break;
67 }
Wagner Ferencb8843662007-12-06 23:40:30 -080068 res += sprintf(buf + res, "%s ", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080069 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -080070 if (res)
71 buf[res-1] = '\n'; /* eat the leftover space */
Stephen Hemminger7e083842009-06-12 19:02:46 +000072
73 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -080074 return res;
75}
76
77/*
78 * "store" function for the bond_masters attribute. This is what
79 * creates and deletes entire bonds.
80 *
81 * The class parameter is ignored.
82 *
83 */
84
Stephen Hemminger3d632c32009-06-12 19:02:48 +000085static ssize_t bonding_store_bonds(struct class *cls,
86 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080087{
88 char command[IFNAMSIZ + 1] = {0, };
89 char *ifname;
Jay Vosburgh027ea042008-01-17 16:25:02 -080090 int rv, res = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -080091 struct bonding *bond;
Mitch Williamsb76cdba2005-11-09 10:36:41 -080092
Mitch Williamsb76cdba2005-11-09 10:36:41 -080093 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
94 ifname = command + 1;
95 if ((strlen(command) <= 1) ||
96 !dev_valid_name(ifname))
97 goto err_no_cmd;
98
99 if (command[0] == '+') {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000100 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800101 ": %s is being created...\n", ifname);
Stephen Hemmingerd2991f72009-06-12 19:02:44 +0000102 rv = bond_create(ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800103 if (rv) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000104 pr_info(DRV_NAME ": Bond creation failed.\n");
Jay Vosburgh027ea042008-01-17 16:25:02 -0800105 res = rv;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800106 }
107 goto out;
108 }
109
110 if (command[0] == '-') {
Jay Vosburgh027ea042008-01-17 16:25:02 -0800111 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -0800112
Pavel Emelyanov0883bec2008-05-17 21:10:10 -0700113 list_for_each_entry(bond, &bond_dev_list, bond_list)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800114 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800115 /* check the ref count on the bond's kobject.
116 * If it's > expected, then there's a file open,
117 * and we have to fail.
118 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700119 if (atomic_read(&bond->dev->dev.kobj.kref.refcount)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800120 > expected_refcount){
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000121 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800122 ": Unable remove bond %s due to open references.\n",
123 ifname);
124 res = -EPERM;
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700125 goto out_unlock;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800126 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000127 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800128 ": %s is being deleted...\n",
129 bond->dev->name);
Stephen Hemminger9e716262009-06-12 19:02:47 +0000130 unregister_netdevice(bond->dev);
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700131 goto out_unlock;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800132 }
133
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000134 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800135 ": unable to delete non-existent bond %s\n", ifname);
136 res = -ENODEV;
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700137 goto out_unlock;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800138 }
139
140err_no_cmd:
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000141 pr_err(DRV_NAME ": no command found in bonding_masters."
142 " Use +ifname or -ifname.\n");
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700143 return -EPERM;
144
145out_unlock:
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700146 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800147
148 /* Always return either count or an error. If you return 0, you'll
149 * get called forever, which is bad.
150 */
151out:
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800152 return res;
153}
154/* class attribute for bond_masters file. This ends up in /sys/class/net */
155static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
156 bonding_show_bonds, bonding_store_bonds);
157
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000158int bond_create_slave_symlinks(struct net_device *master,
159 struct net_device *slave)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800160{
161 char linkname[IFNAMSIZ+7];
162 int ret = 0;
163
164 /* first, create a link from the slave back to the master */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700165 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800166 "master");
167 if (ret)
168 return ret;
169 /* next, create a link from the master to the slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000170 sprintf(linkname, "slave_%s", slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700171 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800172 linkname);
173 return ret;
174
175}
176
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000177void bond_destroy_slave_symlinks(struct net_device *master,
178 struct net_device *slave)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800179{
180 char linkname[IFNAMSIZ+7];
181
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700182 sysfs_remove_link(&(slave->dev.kobj), "master");
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000183 sprintf(linkname, "slave_%s", slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700184 sysfs_remove_link(&(master->dev.kobj), linkname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800185}
186
187
188/*
189 * Show the slaves in the current bond.
190 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700191static ssize_t bonding_show_slaves(struct device *d,
192 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800193{
194 struct slave *slave;
195 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700196 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800197
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700198 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800199 bond_for_each_slave(bond, slave, i) {
200 if (res > (PAGE_SIZE - IFNAMSIZ)) {
201 /* not enough space for another interface name */
202 if ((PAGE_SIZE - res) > 10)
203 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800204 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800205 break;
206 }
207 res += sprintf(buf + res, "%s ", slave->dev->name);
208 }
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700209 read_unlock(&bond->lock);
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800210 if (res)
211 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800212 return res;
213}
214
215/*
216 * Set the slaves in the current bond. The bond interface must be
217 * up for this to succeed.
218 * This function is largely the same flow as bonding_update_bonds().
219 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700220static ssize_t bonding_store_slaves(struct device *d,
221 struct device_attribute *attr,
222 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800223{
224 char command[IFNAMSIZ + 1] = { 0, };
225 char *ifname;
226 int i, res, found, ret = count;
Moni Shoua3158bf72007-10-09 19:43:41 -0700227 u32 original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800228 struct slave *slave;
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -0800229 struct net_device *dev = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700230 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800231
232 /* Quick sanity check -- is the bond interface up? */
233 if (!(bond->dev->flags & IFF_UP)) {
Moni Shoua6b1bf092007-10-09 19:43:40 -0700234 printk(KERN_WARNING DRV_NAME
235 ": %s: doing slave updates when interface is down.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800236 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800237 }
238
239 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
240
Eric W. Biederman496a60c2009-05-13 17:02:50 +0000241 if (!rtnl_trylock())
242 return restart_syscall();
Jay Vosburgh027ea042008-01-17 16:25:02 -0800243
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800244 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
245 ifname = command + 1;
246 if ((strlen(command) <= 1) ||
247 !dev_valid_name(ifname))
248 goto err_no_cmd;
249
250 if (command[0] == '+') {
251
252 /* Got a slave name in ifname. Is it already in the list? */
253 found = 0;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700254 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800255 bond_for_each_slave(bond, slave, i)
256 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000257 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800258 ": %s: Interface %s is already enslaved!\n",
259 bond->dev->name, ifname);
260 ret = -EPERM;
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700261 read_unlock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800262 goto out;
263 }
264
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700265 read_unlock(&bond->lock);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000266 pr_info(DRV_NAME ": %s: Adding slave %s.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800267 bond->dev->name, ifname);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700268 dev = dev_get_by_name(&init_net, ifname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800269 if (!dev) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000270 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800271 ": %s: Interface %s does not exist!\n",
272 bond->dev->name, ifname);
273 ret = -EPERM;
274 goto out;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000275 } else
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800276 dev_put(dev);
277
278 if (dev->flags & IFF_UP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000279 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800280 ": %s: Error: Unable to enslave %s "
281 "because it is already up.\n",
282 bond->dev->name, dev->name);
283 ret = -EPERM;
284 goto out;
285 }
286 /* If this is the first slave, then we need to set
287 the master's hardware address to be the same as the
288 slave's. */
289 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
290 memcpy(bond->dev->dev_addr, dev->dev_addr,
291 dev->addr_len);
292 }
293
294 /* Set the slave's MTU to match the bond */
Moni Shoua3158bf72007-10-09 19:43:41 -0700295 original_mtu = dev->mtu;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800296 res = dev_set_mtu(dev, bond->dev->mtu);
297 if (res) {
298 ret = res;
299 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800300 }
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800301
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800302 res = bond_enslave(bond->dev, dev);
Moni Shoua3158bf72007-10-09 19:43:41 -0700303 bond_for_each_slave(bond, slave, i)
304 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
305 slave->original_mtu = original_mtu;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000306 if (res)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800307 ret = res;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000308
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800309 goto out;
310 }
311
312 if (command[0] == '-') {
313 dev = NULL;
David S. Miller6952d8922008-03-28 16:15:38 -0700314 original_mtu = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800315 bond_for_each_slave(bond, slave, i)
316 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
317 dev = slave->dev;
Moni Shoua3158bf72007-10-09 19:43:41 -0700318 original_mtu = slave->original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800319 break;
320 }
321 if (dev) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000322 pr_info(DRV_NAME ": %s: Removing slave %s\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800323 bond->dev->name, dev->name);
Moni Shouad90a1622007-10-09 19:43:43 -0700324 res = bond_release(bond->dev, dev);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800325 if (res) {
326 ret = res;
327 goto out;
328 }
329 /* set the slave MTU to the default */
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800330 dev_set_mtu(dev, original_mtu);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000331 } else {
332 pr_err(DRV_NAME ": unable to remove non-existent"
333 " slave %s for bond %s.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800334 ifname, bond->dev->name);
335 ret = -ENODEV;
336 }
337 goto out;
338 }
339
340err_no_cmd:
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000341 pr_err(DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800342 ret = -EPERM;
343
344out:
Jay Vosburgh027ea042008-01-17 16:25:02 -0800345 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800346 return ret;
347}
348
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000349static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
350 bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800351
352/*
353 * Show and set the bonding mode. The bond interface must be down to
354 * change the mode.
355 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700356static ssize_t bonding_show_mode(struct device *d,
357 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800358{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700359 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800360
361 return sprintf(buf, "%s %d\n",
362 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800363 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800364}
365
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700366static ssize_t bonding_store_mode(struct device *d,
367 struct device_attribute *attr,
368 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800369{
370 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700371 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800372
373 if (bond->dev->flags & IFF_UP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000374 pr_err(DRV_NAME ": unable to update mode of %s"
375 " because interface is up.\n", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800376 ret = -EPERM;
377 goto out;
378 }
379
Jay Vosburghece95f72008-01-17 16:25:01 -0800380 new_value = bond_parse_parm(buf, bond_mode_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800381 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000382 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800383 ": %s: Ignoring invalid mode value %.*s.\n",
384 bond->dev->name,
385 (int)strlen(buf) - 1, buf);
386 ret = -EINVAL;
387 goto out;
388 } else {
Jay Vosburgh8f903c72006-02-21 16:36:44 -0800389 if (bond->params.mode == BOND_MODE_8023AD)
390 bond_unset_master_3ad_flags(bond);
391
392 if (bond->params.mode == BOND_MODE_ALB)
393 bond_unset_master_alb_flags(bond);
394
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800395 bond->params.mode = new_value;
396 bond_set_mode_ops(bond, bond->params.mode);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000397 pr_info(DRV_NAME ": %s: setting mode to %s (%d).\n",
398 bond->dev->name, bond_mode_tbl[new_value].modename,
399 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800400 }
401out:
402 return ret;
403}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000404static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
405 bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800406
407/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000408 * Show and set the bonding transmit hash method.
409 * The bond interface must be down to change the xmit hash policy.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800410 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700411static ssize_t bonding_show_xmit_hash(struct device *d,
412 struct device_attribute *attr,
413 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800414{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700415 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800416
Wagner Ferenc8e4b9322007-12-06 23:40:32 -0800417 return sprintf(buf, "%s %d\n",
418 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
419 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800420}
421
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700422static ssize_t bonding_store_xmit_hash(struct device *d,
423 struct device_attribute *attr,
424 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800425{
426 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700427 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800428
429 if (bond->dev->flags & IFF_UP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000430 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800431 "%s: Interface is up. Unable to update xmit policy.\n",
432 bond->dev->name);
433 ret = -EPERM;
434 goto out;
435 }
436
Jay Vosburghece95f72008-01-17 16:25:01 -0800437 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800438 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000439 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800440 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
441 bond->dev->name,
442 (int)strlen(buf) - 1, buf);
443 ret = -EINVAL;
444 goto out;
445 } else {
446 bond->params.xmit_policy = new_value;
447 bond_set_mode_ops(bond, bond->params.mode);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000448 pr_info(DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
449 bond->dev->name,
450 xmit_hashtype_tbl[new_value].modename, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800451 }
452out:
453 return ret;
454}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000455static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
456 bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800457
458/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700459 * Show and set arp_validate.
460 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700461static ssize_t bonding_show_arp_validate(struct device *d,
462 struct device_attribute *attr,
463 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700464{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700465 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700466
467 return sprintf(buf, "%s %d\n",
468 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800469 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700470}
471
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700472static ssize_t bonding_store_arp_validate(struct device *d,
473 struct device_attribute *attr,
474 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700475{
476 int new_value;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700477 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700478
Jay Vosburghece95f72008-01-17 16:25:01 -0800479 new_value = bond_parse_parm(buf, arp_validate_tbl);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700480 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000481 pr_err(DRV_NAME
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700482 ": %s: Ignoring invalid arp_validate value %s\n",
483 bond->dev->name, buf);
484 return -EINVAL;
485 }
486 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000487 pr_err(DRV_NAME
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700488 ": %s: arp_validate only supported in active-backup mode.\n",
489 bond->dev->name);
490 return -EINVAL;
491 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000492 pr_info(DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700493 bond->dev->name, arp_validate_tbl[new_value].modename,
494 new_value);
495
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000496 if (!bond->params.arp_validate && new_value)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700497 bond_register_arp(bond);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000498 else if (bond->params.arp_validate && !new_value)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700499 bond_unregister_arp(bond);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700500
501 bond->params.arp_validate = new_value;
502
503 return count;
504}
505
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000506static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
507 bonding_store_arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700508
509/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700510 * Show and store fail_over_mac. User only allowed to change the
511 * value when there are no slaves.
512 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000513static ssize_t bonding_show_fail_over_mac(struct device *d,
514 struct device_attribute *attr,
515 char *buf)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700516{
517 struct bonding *bond = to_bond(d);
518
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700519 return sprintf(buf, "%s %d\n",
520 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
521 bond->params.fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700522}
523
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000524static ssize_t bonding_store_fail_over_mac(struct device *d,
525 struct device_attribute *attr,
526 const char *buf, size_t count)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700527{
528 int new_value;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700529 struct bonding *bond = to_bond(d);
530
531 if (bond->slave_cnt != 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000532 pr_err(DRV_NAME
Jay Vosburghdd957c52007-10-09 19:57:24 -0700533 ": %s: Can't alter fail_over_mac with slaves in bond.\n",
534 bond->dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700535 return -EPERM;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700536 }
537
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700538 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
539 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000540 pr_err(DRV_NAME
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700541 ": %s: Ignoring invalid fail_over_mac value %s.\n",
542 bond->dev->name, buf);
543 return -EINVAL;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700544 }
545
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700546 bond->params.fail_over_mac = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000547 pr_info(DRV_NAME ": %s: Setting fail_over_mac to %s (%d).\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700548 bond->dev->name, fail_over_mac_tbl[new_value].modename,
549 new_value);
550
551 return count;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700552}
553
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000554static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
555 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700556
557/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800558 * Show and set the arp timer interval. There are two tricky bits
559 * here. First, if ARP monitoring is activated, then we must disable
560 * MII monitoring. Second, if the ARP timer isn't running, we must
561 * start it.
562 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700563static ssize_t bonding_show_arp_interval(struct device *d,
564 struct device_attribute *attr,
565 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800566{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700567 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800568
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800569 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800570}
571
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700572static ssize_t bonding_store_arp_interval(struct device *d,
573 struct device_attribute *attr,
574 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800575{
576 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700577 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800578
579 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000580 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800581 ": %s: no arp_interval value specified.\n",
582 bond->dev->name);
583 ret = -EINVAL;
584 goto out;
585 }
586 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000587 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800588 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
589 bond->dev->name, new_value, INT_MAX);
590 ret = -EINVAL;
591 goto out;
592 }
593
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000594 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800595 ": %s: Setting ARP monitoring interval to %d.\n",
596 bond->dev->name, new_value);
597 bond->params.arp_interval = new_value;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -0800598 if (bond->params.arp_interval)
599 bond->dev->priv_flags |= IFF_MASTER_ARPMON;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800600 if (bond->params.miimon) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000601 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800602 ": %s: ARP monitoring cannot be used with MII monitoring. "
603 "%s Disabling MII monitoring.\n",
604 bond->dev->name, bond->dev->name);
605 bond->params.miimon = 0;
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700606 if (delayed_work_pending(&bond->mii_work)) {
607 cancel_delayed_work(&bond->mii_work);
608 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800609 }
610 }
611 if (!bond->params.arp_targets[0]) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000612 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800613 ": %s: ARP monitoring has been set up, "
614 "but no ARP targets have been specified.\n",
615 bond->dev->name);
616 }
617 if (bond->dev->flags & IFF_UP) {
618 /* If the interface is up, we may need to fire off
619 * the ARP timer. If the interface is down, the
620 * timer will get fired off when the open function
621 * is called.
622 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700623 if (!delayed_work_pending(&bond->arp_work)) {
624 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
625 INIT_DELAYED_WORK(&bond->arp_work,
626 bond_activebackup_arp_mon);
627 else
628 INIT_DELAYED_WORK(&bond->arp_work,
629 bond_loadbalance_arp_mon);
630
631 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800632 }
633 }
634
635out:
636 return ret;
637}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000638static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
639 bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800640
641/*
642 * Show and set the arp targets.
643 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700644static ssize_t bonding_show_arp_targets(struct device *d,
645 struct device_attribute *attr,
646 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800647{
648 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700649 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800650
651 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
652 if (bond->params.arp_targets[i])
Harvey Harrison63779432008-10-31 00:56:00 -0700653 res += sprintf(buf + res, "%pI4 ",
654 &bond->params.arp_targets[i]);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800655 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800656 if (res)
657 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800658 return res;
659}
660
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700661static ssize_t bonding_store_arp_targets(struct device *d,
662 struct device_attribute *attr,
663 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800664{
Al Virod3bb52b2007-08-22 20:06:58 -0400665 __be32 newtarget;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800666 int i = 0, done = 0, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700667 struct bonding *bond = to_bond(d);
Al Virod3bb52b2007-08-22 20:06:58 -0400668 __be32 *targets;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800669
670 targets = bond->params.arp_targets;
671 newtarget = in_aton(buf + 1);
672 /* look for adds */
673 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400674 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000675 pr_err(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700676 ": %s: invalid ARP target %pI4 specified for addition\n",
677 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800678 ret = -EINVAL;
679 goto out;
680 }
681 /* look for an empty slot to put the target in, and check for dupes */
Brian Haley5a31bec2009-04-13 00:11:30 -0700682 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800683 if (targets[i] == newtarget) { /* duplicate */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000684 pr_err(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700685 ": %s: ARP target %pI4 is already present\n",
686 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800687 ret = -EINVAL;
688 goto out;
689 }
Brian Haley5a31bec2009-04-13 00:11:30 -0700690 if (targets[i] == 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000691 pr_info(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700692 ": %s: adding ARP target %pI4.\n",
693 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800694 done = 1;
695 targets[i] = newtarget;
696 }
697 }
698 if (!done) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000699 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800700 ": %s: ARP target table is full!\n",
701 bond->dev->name);
702 ret = -EINVAL;
703 goto out;
704 }
705
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000706 } else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400707 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000708 pr_err(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700709 ": %s: invalid ARP target %pI4 specified for removal\n",
710 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800711 ret = -EINVAL;
712 goto out;
713 }
714
Brian Haley5a31bec2009-04-13 00:11:30 -0700715 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800716 if (targets[i] == newtarget) {
Brian Haley5a31bec2009-04-13 00:11:30 -0700717 int j;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000718 pr_info(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700719 ": %s: removing ARP target %pI4.\n",
720 bond->dev->name, &newtarget);
Brian Haley5a31bec2009-04-13 00:11:30 -0700721 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
722 targets[j] = targets[j+1];
723
724 targets[j] = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800725 done = 1;
726 }
727 }
728 if (!done) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000729 pr_info(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700730 ": %s: unable to remove nonexistent ARP target %pI4.\n",
731 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800732 ret = -EINVAL;
733 goto out;
734 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000735 } else {
736 pr_err(DRV_NAME ": no command found in arp_ip_targets file"
737 " for bond %s. Use +<addr> or -<addr>.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800738 bond->dev->name);
739 ret = -EPERM;
740 goto out;
741 }
742
743out:
744 return ret;
745}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700746static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800747
748/*
749 * Show and set the up and down delays. These must be multiples of the
750 * MII monitoring value, and are stored internally as the multiplier.
751 * Thus, we must translate to MS for the real world.
752 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700753static ssize_t bonding_show_downdelay(struct device *d,
754 struct device_attribute *attr,
755 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800756{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700757 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800758
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800759 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800760}
761
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700762static ssize_t bonding_store_downdelay(struct device *d,
763 struct device_attribute *attr,
764 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800765{
766 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700767 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800768
769 if (!(bond->params.miimon)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000770 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800771 ": %s: Unable to set down delay as MII monitoring is disabled\n",
772 bond->dev->name);
773 ret = -EPERM;
774 goto out;
775 }
776
777 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000778 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800779 ": %s: no down delay value specified.\n",
780 bond->dev->name);
781 ret = -EINVAL;
782 goto out;
783 }
784 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000785 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800786 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
787 bond->dev->name, new_value, 1, INT_MAX);
788 ret = -EINVAL;
789 goto out;
790 } else {
791 if ((new_value % bond->params.miimon) != 0) {
792 printk(KERN_WARNING DRV_NAME
793 ": %s: Warning: down delay (%d) is not a multiple "
794 "of miimon (%d), delay rounded to %d ms\n",
795 bond->dev->name, new_value, bond->params.miimon,
796 (new_value / bond->params.miimon) *
797 bond->params.miimon);
798 }
799 bond->params.downdelay = new_value / bond->params.miimon;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000800 pr_info(DRV_NAME ": %s: Setting down delay to %d.\n",
801 bond->dev->name,
802 bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800803
804 }
805
806out:
807 return ret;
808}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000809static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
810 bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800811
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700812static ssize_t bonding_show_updelay(struct device *d,
813 struct device_attribute *attr,
814 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800815{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700816 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800817
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800818 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800819
820}
821
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700822static ssize_t bonding_store_updelay(struct device *d,
823 struct device_attribute *attr,
824 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800825{
826 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700827 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800828
829 if (!(bond->params.miimon)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000830 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800831 ": %s: Unable to set up delay as MII monitoring is disabled\n",
832 bond->dev->name);
833 ret = -EPERM;
834 goto out;
835 }
836
837 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000838 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800839 ": %s: no up delay value specified.\n",
840 bond->dev->name);
841 ret = -EINVAL;
842 goto out;
843 }
844 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000845 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800846 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
847 bond->dev->name, new_value, 1, INT_MAX);
848 ret = -EINVAL;
849 goto out;
850 } else {
851 if ((new_value % bond->params.miimon) != 0) {
852 printk(KERN_WARNING DRV_NAME
853 ": %s: Warning: up delay (%d) is not a multiple "
854 "of miimon (%d), updelay rounded to %d ms\n",
855 bond->dev->name, new_value, bond->params.miimon,
856 (new_value / bond->params.miimon) *
857 bond->params.miimon);
858 }
859 bond->params.updelay = new_value / bond->params.miimon;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000860 pr_info(DRV_NAME ": %s: Setting up delay to %d.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800861 bond->dev->name, bond->params.updelay * bond->params.miimon);
862
863 }
864
865out:
866 return ret;
867}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000868static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
869 bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800870
871/*
872 * Show and set the LACP interval. Interface must be down, and the mode
873 * must be set to 802.3ad mode.
874 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700875static ssize_t bonding_show_lacp(struct device *d,
876 struct device_attribute *attr,
877 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800878{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700879 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800880
881 return sprintf(buf, "%s %d\n",
882 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800883 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800884}
885
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700886static ssize_t bonding_store_lacp(struct device *d,
887 struct device_attribute *attr,
888 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800889{
890 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700891 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800892
893 if (bond->dev->flags & IFF_UP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000894 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800895 ": %s: Unable to update LACP rate because interface is up.\n",
896 bond->dev->name);
897 ret = -EPERM;
898 goto out;
899 }
900
901 if (bond->params.mode != BOND_MODE_8023AD) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000902 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800903 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
904 bond->dev->name);
905 ret = -EPERM;
906 goto out;
907 }
908
Jay Vosburghece95f72008-01-17 16:25:01 -0800909 new_value = bond_parse_parm(buf, bond_lacp_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800910
911 if ((new_value == 1) || (new_value == 0)) {
912 bond->params.lacp_fast = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000913 pr_info(DRV_NAME ": %s: Setting LACP rate to %s (%d).\n",
914 bond->dev->name, bond_lacp_tbl[new_value].modename,
915 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800916 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000917 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800918 ": %s: Ignoring invalid LACP rate value %.*s.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000919 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800920 ret = -EINVAL;
921 }
922out:
923 return ret;
924}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000925static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
926 bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800927
Jay Vosburghfd989c82008-11-04 17:51:16 -0800928static ssize_t bonding_show_ad_select(struct device *d,
929 struct device_attribute *attr,
930 char *buf)
931{
932 struct bonding *bond = to_bond(d);
933
934 return sprintf(buf, "%s %d\n",
935 ad_select_tbl[bond->params.ad_select].modename,
936 bond->params.ad_select);
937}
938
939
940static ssize_t bonding_store_ad_select(struct device *d,
941 struct device_attribute *attr,
942 const char *buf, size_t count)
943{
944 int new_value, ret = count;
945 struct bonding *bond = to_bond(d);
946
947 if (bond->dev->flags & IFF_UP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000948 pr_err(DRV_NAME
Jay Vosburghfd989c82008-11-04 17:51:16 -0800949 ": %s: Unable to update ad_select because interface "
950 "is up.\n", bond->dev->name);
951 ret = -EPERM;
952 goto out;
953 }
954
955 new_value = bond_parse_parm(buf, ad_select_tbl);
956
957 if (new_value != -1) {
958 bond->params.ad_select = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000959 pr_info(DRV_NAME
Jay Vosburghfd989c82008-11-04 17:51:16 -0800960 ": %s: Setting ad_select to %s (%d).\n",
961 bond->dev->name, ad_select_tbl[new_value].modename,
962 new_value);
963 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000964 pr_err(DRV_NAME
Jay Vosburghfd989c82008-11-04 17:51:16 -0800965 ": %s: Ignoring invalid ad_select value %.*s.\n",
966 bond->dev->name, (int)strlen(buf) - 1, buf);
967 ret = -EINVAL;
968 }
969out:
970 return ret;
971}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000972static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
973 bonding_show_ad_select, bonding_store_ad_select);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800974
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800975/*
Moni Shoua7893b242008-05-17 21:10:12 -0700976 * Show and set the number of grat ARP to send after a failover event.
977 */
978static ssize_t bonding_show_n_grat_arp(struct device *d,
979 struct device_attribute *attr,
980 char *buf)
981{
982 struct bonding *bond = to_bond(d);
983
984 return sprintf(buf, "%d\n", bond->params.num_grat_arp);
985}
986
987static ssize_t bonding_store_n_grat_arp(struct device *d,
988 struct device_attribute *attr,
989 const char *buf, size_t count)
990{
991 int new_value, ret = count;
992 struct bonding *bond = to_bond(d);
993
994 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000995 pr_err(DRV_NAME
Moni Shoua7893b242008-05-17 21:10:12 -0700996 ": %s: no num_grat_arp value specified.\n",
997 bond->dev->name);
998 ret = -EINVAL;
999 goto out;
1000 }
1001 if (new_value < 0 || new_value > 255) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001002 pr_err(DRV_NAME
Moni Shoua7893b242008-05-17 21:10:12 -07001003 ": %s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
1004 bond->dev->name, new_value);
1005 ret = -EINVAL;
1006 goto out;
1007 } else {
1008 bond->params.num_grat_arp = new_value;
1009 }
1010out:
1011 return ret;
1012}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001013static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
1014 bonding_show_n_grat_arp, bonding_store_n_grat_arp);
Brian Haley305d5522008-11-04 17:51:14 -08001015
1016/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001017 * Show and set the number of unsolicited NA's to send after a failover event.
Brian Haley305d5522008-11-04 17:51:14 -08001018 */
1019static ssize_t bonding_show_n_unsol_na(struct device *d,
1020 struct device_attribute *attr,
1021 char *buf)
1022{
1023 struct bonding *bond = to_bond(d);
1024
1025 return sprintf(buf, "%d\n", bond->params.num_unsol_na);
1026}
1027
1028static ssize_t bonding_store_n_unsol_na(struct device *d,
1029 struct device_attribute *attr,
1030 const char *buf, size_t count)
1031{
1032 int new_value, ret = count;
1033 struct bonding *bond = to_bond(d);
1034
1035 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001036 pr_err(DRV_NAME
Brian Haley305d5522008-11-04 17:51:14 -08001037 ": %s: no num_unsol_na value specified.\n",
1038 bond->dev->name);
1039 ret = -EINVAL;
1040 goto out;
1041 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001042
Brian Haley305d5522008-11-04 17:51:14 -08001043 if (new_value < 0 || new_value > 255) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001044 pr_err(DRV_NAME
Brian Haley305d5522008-11-04 17:51:14 -08001045 ": %s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
1046 bond->dev->name, new_value);
1047 ret = -EINVAL;
1048 goto out;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001049 } else
Brian Haley305d5522008-11-04 17:51:14 -08001050 bond->params.num_unsol_na = new_value;
Brian Haley305d5522008-11-04 17:51:14 -08001051out:
1052 return ret;
1053}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001054static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
1055 bonding_show_n_unsol_na, bonding_store_n_unsol_na);
Brian Haley305d5522008-11-04 17:51:14 -08001056
Moni Shoua7893b242008-05-17 21:10:12 -07001057/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001058 * Show and set the MII monitor interval. There are two tricky bits
1059 * here. First, if MII monitoring is activated, then we must disable
1060 * ARP monitoring. Second, if the timer isn't running, we must
1061 * start it.
1062 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001063static ssize_t bonding_show_miimon(struct device *d,
1064 struct device_attribute *attr,
1065 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001066{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001067 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001068
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001069 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001070}
1071
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001072static ssize_t bonding_store_miimon(struct device *d,
1073 struct device_attribute *attr,
1074 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001075{
1076 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001077 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001078
1079 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001080 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001081 ": %s: no miimon value specified.\n",
1082 bond->dev->name);
1083 ret = -EINVAL;
1084 goto out;
1085 }
1086 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001087 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001088 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1089 bond->dev->name, new_value, 1, INT_MAX);
1090 ret = -EINVAL;
1091 goto out;
1092 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001093 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001094 ": %s: Setting MII monitoring interval to %d.\n",
1095 bond->dev->name, new_value);
1096 bond->params.miimon = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001097 if (bond->params.updelay)
1098 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001099 ": %s: Note: Updating updelay (to %d) "
1100 "since it is a multiple of the miimon value.\n",
1101 bond->dev->name,
1102 bond->params.updelay * bond->params.miimon);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001103 if (bond->params.downdelay)
1104 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001105 ": %s: Note: Updating downdelay (to %d) "
1106 "since it is a multiple of the miimon value.\n",
1107 bond->dev->name,
1108 bond->params.downdelay * bond->params.miimon);
1109 if (bond->params.arp_interval) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001110 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001111 ": %s: MII monitoring cannot be used with "
1112 "ARP monitoring. Disabling ARP monitoring...\n",
1113 bond->dev->name);
1114 bond->params.arp_interval = 0;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -08001115 bond->dev->priv_flags &= ~IFF_MASTER_ARPMON;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001116 if (bond->params.arp_validate) {
1117 bond_unregister_arp(bond);
1118 bond->params.arp_validate =
1119 BOND_ARP_VALIDATE_NONE;
1120 }
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001121 if (delayed_work_pending(&bond->arp_work)) {
1122 cancel_delayed_work(&bond->arp_work);
1123 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001124 }
1125 }
1126
1127 if (bond->dev->flags & IFF_UP) {
1128 /* If the interface is up, we may need to fire off
1129 * the MII timer. If the interface is down, the
1130 * timer will get fired off when the open function
1131 * is called.
1132 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001133 if (!delayed_work_pending(&bond->mii_work)) {
1134 INIT_DELAYED_WORK(&bond->mii_work,
1135 bond_mii_monitor);
1136 queue_delayed_work(bond->wq,
1137 &bond->mii_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001138 }
1139 }
1140 }
1141out:
1142 return ret;
1143}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001144static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1145 bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001146
1147/*
1148 * Show and set the primary slave. The store function is much
1149 * simpler than bonding_store_slaves function because it only needs to
1150 * handle one interface name.
1151 * The bond must be a mode that supports a primary for this be
1152 * set.
1153 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001154static ssize_t bonding_show_primary(struct device *d,
1155 struct device_attribute *attr,
1156 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001157{
1158 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001159 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001160
1161 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001162 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001163
1164 return count;
1165}
1166
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001167static ssize_t bonding_store_primary(struct device *d,
1168 struct device_attribute *attr,
1169 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001170{
1171 int i;
1172 struct slave *slave;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001173 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001174
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001175 if (!rtnl_trylock())
1176 return restart_syscall();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001177 read_lock(&bond->lock);
1178 write_lock_bh(&bond->curr_slave_lock);
1179
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001180 if (!USES_PRIMARY(bond->params.mode)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001181 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001182 ": %s: Unable to set primary slave; %s is in mode %d\n",
1183 bond->dev->name, bond->dev->name, bond->params.mode);
1184 } else {
1185 bond_for_each_slave(bond, slave, i) {
1186 if (strnicmp
1187 (slave->dev->name, buf,
1188 strlen(slave->dev->name)) == 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001189 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001190 ": %s: Setting %s as primary slave.\n",
1191 bond->dev->name, slave->dev->name);
1192 bond->primary_slave = slave;
1193 bond_select_active_slave(bond);
1194 goto out;
1195 }
1196 }
1197
1198 /* if we got here, then we didn't match the name of any slave */
1199
1200 if (strlen(buf) == 0 || buf[0] == '\n') {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001201 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001202 ": %s: Setting primary slave to None.\n",
1203 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001204 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001205 bond_select_active_slave(bond);
1206 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001207 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001208 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1209 bond->dev->name, (int)strlen(buf) - 1, buf);
1210 }
1211 }
1212out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001213 write_unlock_bh(&bond->curr_slave_lock);
1214 read_unlock(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001215 rtnl_unlock();
1216
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001217 return count;
1218}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001219static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1220 bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001221
1222/*
1223 * Show and set the use_carrier flag.
1224 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001225static ssize_t bonding_show_carrier(struct device *d,
1226 struct device_attribute *attr,
1227 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001228{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001229 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001230
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001231 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001232}
1233
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001234static ssize_t bonding_store_carrier(struct device *d,
1235 struct device_attribute *attr,
1236 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001237{
1238 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001239 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001240
1241
1242 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001243 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001244 ": %s: no use_carrier value specified.\n",
1245 bond->dev->name);
1246 ret = -EINVAL;
1247 goto out;
1248 }
1249 if ((new_value == 0) || (new_value == 1)) {
1250 bond->params.use_carrier = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001251 pr_info(DRV_NAME ": %s: Setting use_carrier to %d.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001252 bond->dev->name, new_value);
1253 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001254 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001255 ": %s: Ignoring invalid use_carrier value %d.\n",
1256 bond->dev->name, new_value);
1257 }
1258out:
1259 return count;
1260}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001261static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1262 bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001263
1264
1265/*
1266 * Show and set currently active_slave.
1267 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001268static ssize_t bonding_show_active_slave(struct device *d,
1269 struct device_attribute *attr,
1270 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001271{
1272 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001273 struct bonding *bond = to_bond(d);
Wagner Ferenc16cd0162007-12-06 23:40:29 -08001274 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001275
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001276 read_lock(&bond->curr_slave_lock);
1277 curr = bond->curr_active_slave;
1278 read_unlock(&bond->curr_slave_lock);
1279
1280 if (USES_PRIMARY(bond->params.mode) && curr)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001281 count = sprintf(buf, "%s\n", curr->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001282 return count;
1283}
1284
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001285static ssize_t bonding_store_active_slave(struct device *d,
1286 struct device_attribute *attr,
1287 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001288{
1289 int i;
1290 struct slave *slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001291 struct slave *old_active = NULL;
1292 struct slave *new_active = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001293 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001294
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001295 if (!rtnl_trylock())
1296 return restart_syscall();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001297 read_lock(&bond->lock);
1298 write_lock_bh(&bond->curr_slave_lock);
Jay Vosburgh1466a212007-11-06 13:33:28 -08001299
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001300 if (!USES_PRIMARY(bond->params.mode))
1301 pr_info(DRV_NAME ": %s: Unable to change active slave;"
1302 " %s is in mode %d\n",
1303 bond->dev->name, bond->dev->name, bond->params.mode);
1304 else {
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001305 bond_for_each_slave(bond, slave, i) {
1306 if (strnicmp
1307 (slave->dev->name, buf,
1308 strlen(slave->dev->name)) == 0) {
1309 old_active = bond->curr_active_slave;
1310 new_active = slave;
Jay Vosburgha50d8de2006-09-22 21:53:25 -07001311 if (new_active == old_active) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001312 /* do nothing */
1313 printk(KERN_INFO DRV_NAME
1314 ": %s: %s is already the current active slave.\n",
1315 bond->dev->name, slave->dev->name);
1316 goto out;
1317 }
1318 else {
1319 if ((new_active) &&
1320 (old_active) &&
1321 (new_active->link == BOND_LINK_UP) &&
1322 IS_UP(new_active->dev)) {
1323 printk(KERN_INFO DRV_NAME
1324 ": %s: Setting %s as active slave.\n",
1325 bond->dev->name, slave->dev->name);
1326 bond_change_active_slave(bond, new_active);
1327 }
1328 else {
1329 printk(KERN_INFO DRV_NAME
1330 ": %s: Could not set %s as active slave; "
1331 "either %s is down or the link is down.\n",
1332 bond->dev->name, slave->dev->name,
1333 slave->dev->name);
1334 }
1335 goto out;
1336 }
1337 }
1338 }
1339
1340 /* if we got here, then we didn't match the name of any slave */
1341
1342 if (strlen(buf) == 0 || buf[0] == '\n') {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001343 pr_info(DRV_NAME
1344 ": %s: Setting active slave to None.\n",
1345 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001346 bond->primary_slave = NULL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001347 bond_select_active_slave(bond);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001348 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001349 pr_info(DRV_NAME ": %s: Unable to set %.*s"
1350 " as active slave as it is not a slave.\n",
1351 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001352 }
1353 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001354 out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001355 write_unlock_bh(&bond->curr_slave_lock);
1356 read_unlock(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001357 rtnl_unlock();
1358
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001359 return count;
1360
1361}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001362static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1363 bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001364
1365
1366/*
1367 * Show link status of the bond interface.
1368 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001369static ssize_t bonding_show_mii_status(struct device *d,
1370 struct device_attribute *attr,
1371 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001372{
1373 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001374 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001375
1376 read_lock(&bond->curr_slave_lock);
1377 curr = bond->curr_active_slave;
1378 read_unlock(&bond->curr_slave_lock);
1379
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001380 return sprintf(buf, "%s\n", curr ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001381}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001382static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001383
1384
1385/*
1386 * Show current 802.3ad aggregator ID.
1387 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001388static ssize_t bonding_show_ad_aggregator(struct device *d,
1389 struct device_attribute *attr,
1390 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001391{
1392 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001393 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001394
1395 if (bond->params.mode == BOND_MODE_8023AD) {
1396 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001397 count = sprintf(buf, "%d\n",
1398 (bond_3ad_get_active_agg_info(bond, &ad_info))
1399 ? 0 : ad_info.aggregator_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001400 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001401
1402 return count;
1403}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001404static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001405
1406
1407/*
1408 * Show number of active 802.3ad ports.
1409 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001410static ssize_t bonding_show_ad_num_ports(struct device *d,
1411 struct device_attribute *attr,
1412 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001413{
1414 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001415 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001416
1417 if (bond->params.mode == BOND_MODE_8023AD) {
1418 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001419 count = sprintf(buf, "%d\n",
1420 (bond_3ad_get_active_agg_info(bond, &ad_info))
1421 ? 0 : ad_info.ports);
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_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001427
1428
1429/*
1430 * Show current 802.3ad actor key.
1431 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001432static ssize_t bonding_show_ad_actor_key(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;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001441 count = sprintf(buf, "%d\n",
1442 (bond_3ad_get_active_agg_info(bond, &ad_info))
1443 ? 0 : ad_info.actor_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001444 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001445
1446 return count;
1447}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001448static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001449
1450
1451/*
1452 * Show current 802.3ad partner key.
1453 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001454static ssize_t bonding_show_ad_partner_key(struct device *d,
1455 struct device_attribute *attr,
1456 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001457{
1458 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001459 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001460
1461 if (bond->params.mode == BOND_MODE_8023AD) {
1462 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001463 count = sprintf(buf, "%d\n",
1464 (bond_3ad_get_active_agg_info(bond, &ad_info))
1465 ? 0 : ad_info.partner_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001466 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001467
1468 return count;
1469}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001470static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001471
1472
1473/*
1474 * Show current 802.3ad partner mac.
1475 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001476static ssize_t bonding_show_ad_partner_mac(struct device *d,
1477 struct device_attribute *attr,
1478 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001479{
1480 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001481 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001482
1483 if (bond->params.mode == BOND_MODE_8023AD) {
1484 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001485 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
Johannes Berge1749612008-10-27 15:59:26 -07001486 count = sprintf(buf, "%pM\n", ad_info.partner_system);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001487 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001488
1489 return count;
1490}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001491static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001492
1493
1494
1495static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001496 &dev_attr_slaves.attr,
1497 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001498 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001499 &dev_attr_arp_validate.attr,
1500 &dev_attr_arp_interval.attr,
1501 &dev_attr_arp_ip_target.attr,
1502 &dev_attr_downdelay.attr,
1503 &dev_attr_updelay.attr,
1504 &dev_attr_lacp_rate.attr,
Jay Vosburghfd989c82008-11-04 17:51:16 -08001505 &dev_attr_ad_select.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001506 &dev_attr_xmit_hash_policy.attr,
Moni Shoua7893b242008-05-17 21:10:12 -07001507 &dev_attr_num_grat_arp.attr,
Brian Haley305d5522008-11-04 17:51:14 -08001508 &dev_attr_num_unsol_na.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001509 &dev_attr_miimon.attr,
1510 &dev_attr_primary.attr,
1511 &dev_attr_use_carrier.attr,
1512 &dev_attr_active_slave.attr,
1513 &dev_attr_mii_status.attr,
1514 &dev_attr_ad_aggregator.attr,
1515 &dev_attr_ad_num_ports.attr,
1516 &dev_attr_ad_actor_key.attr,
1517 &dev_attr_ad_partner_key.attr,
1518 &dev_attr_ad_partner_mac.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001519 NULL,
1520};
1521
1522static struct attribute_group bonding_group = {
1523 .name = "bonding",
1524 .attrs = per_bond_attrs,
1525};
1526
1527/*
1528 * Initialize sysfs. This sets up the bonding_masters file in
1529 * /sys/class/net.
1530 */
1531int bond_create_sysfs(void)
1532{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001533 int ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001534
Jay Vosburghb8a97872008-06-13 18:12:04 -07001535 ret = netdev_class_create_file(&class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001536 /*
1537 * Permit multiple loads of the module by ignoring failures to
1538 * create the bonding_masters sysfs file. Bonding devices
1539 * created by second or subsequent loads of the module will
1540 * not be listed in, or controllable by, bonding_masters, but
1541 * will have the usual "bonding" sysfs directory.
1542 *
1543 * This is done to preserve backwards compatibility for
1544 * initscripts/sysconfig, which load bonding multiple times to
1545 * configure multiple bonding devices.
1546 */
1547 if (ret == -EEXIST) {
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001548 /* Is someone being kinky and naming a device bonding_master? */
1549 if (__dev_get_by_name(&init_net,
1550 class_attr_bonding_masters.attr.name))
1551 printk(KERN_ERR
1552 "network device named %s already exists in sysfs",
1553 class_attr_bonding_masters.attr.name);
Stephen Hemminger130aa612009-06-11 05:46:04 -07001554 ret = 0;
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001555 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001556
1557 return ret;
1558
1559}
1560
1561/*
1562 * Remove /sys/class/net/bonding_masters.
1563 */
1564void bond_destroy_sysfs(void)
1565{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001566 netdev_class_remove_file(&class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001567}
1568
1569/*
1570 * Initialize sysfs for each bond. This sets up and registers
1571 * the 'bondctl' directory for each individual bond under /sys/class/net.
1572 */
1573int bond_create_sysfs_entry(struct bonding *bond)
1574{
1575 struct net_device *dev = bond->dev;
1576 int err;
1577
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001578 err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001579 if (err)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001580 printk(KERN_EMERG "eek! didn't create group!\n");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001581
1582 if (expected_refcount < 1)
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001583 expected_refcount = atomic_read(&bond->dev->dev.kobj.kref.refcount);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001584
1585 return err;
1586}
1587/*
1588 * Remove sysfs entries for each bond.
1589 */
1590void bond_destroy_sysfs_entry(struct bonding *bond)
1591{
1592 struct net_device *dev = bond->dev;
1593
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001594 sysfs_remove_group(&(dev->dev.kobj), &bonding_group);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001595}
1596