blob: 4e00b4f836411eabb4fcc41959c5415bc9bcb13d [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>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040025#include <linux/sched.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080026#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>
Stephen Hemminger5c5129b2009-06-12 19:02:51 +000037#include <linux/etherdevice.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070038#include <net/net_namespace.h>
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000039#include <net/netns/generic.h>
40#include <linux/nsproxy.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080041
Mitch Williamsb76cdba2005-11-09 10:36:41 -080042#include "bonding.h"
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -080043
Stephen Hemminger3d632c32009-06-12 19:02:48 +000044#define to_dev(obj) container_of(obj, struct device, kobj)
Wang Chen454d7c92008-11-12 23:37:49 -080045#define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
Mitch Williamsb76cdba2005-11-09 10:36:41 -080046
Mitch Williamsb76cdba2005-11-09 10:36:41 -080047/*
48 * "show" function for the bond_masters attribute.
49 * The class parameter is ignored.
50 */
Wagner Ferencb8843662007-12-06 23:40:30 -080051static ssize_t bonding_show_bonds(struct class *cls, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080052{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000053 struct net *net = current->nsproxy->net_ns;
54 struct bond_net *bn = net_generic(net, bond_net_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -080055 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
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000060 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -080061 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
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000077static struct net_device *bond_get_by_name(struct net *net, const char *ifname)
Stephen Hemminger373500d2009-06-12 19:02:50 +000078{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000079 struct bond_net *bn = net_generic(net, bond_net_id);
Stephen Hemminger373500d2009-06-12 19:02:50 +000080 struct bonding *bond;
81
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000082 list_for_each_entry(bond, &bn->dev_list, bond_list) {
Stephen Hemminger373500d2009-06-12 19:02:50 +000083 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
84 return bond->dev;
85 }
86 return NULL;
87}
88
Mitch Williamsb76cdba2005-11-09 10:36:41 -080089/*
90 * "store" function for the bond_masters attribute. This is what
91 * creates and deletes entire bonds.
92 *
93 * The class parameter is ignored.
94 *
95 */
96
Stephen Hemminger3d632c32009-06-12 19:02:48 +000097static ssize_t bonding_store_bonds(struct class *cls,
98 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080099{
Eric W. Biedermanec87fd32009-10-29 14:18:26 +0000100 struct net *net = current->nsproxy->net_ns;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800101 char command[IFNAMSIZ + 1] = {0, };
102 char *ifname;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800103 int rv, res = count;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800104
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800105 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
106 ifname = command + 1;
107 if ((strlen(command) <= 1) ||
108 !dev_valid_name(ifname))
109 goto err_no_cmd;
110
111 if (command[0] == '+') {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000112 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800113 ": %s is being created...\n", ifname);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +0000114 rv = bond_create(net, ifname);
Jay Vosburgh027ea042008-01-17 16:25:02 -0800115 if (rv) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000116 pr_info(DRV_NAME ": Bond creation failed.\n");
Jay Vosburgh027ea042008-01-17 16:25:02 -0800117 res = rv;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800118 }
Stephen Hemminger373500d2009-06-12 19:02:50 +0000119 } else if (command[0] == '-') {
120 struct net_device *bond_dev;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800121
Jay Vosburgh027ea042008-01-17 16:25:02 -0800122 rtnl_lock();
Eric W. Biedermanec87fd32009-10-29 14:18:26 +0000123 bond_dev = bond_get_by_name(net, ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000124 if (bond_dev) {
125 pr_info(DRV_NAME ": %s is being deleted...\n",
126 ifname);
127 unregister_netdevice(bond_dev);
128 } else {
129 pr_err(DRV_NAME ": unable to delete non-existent %s\n",
130 ifname);
131 res = -ENODEV;
132 }
133 rtnl_unlock();
134 } else
135 goto err_no_cmd;
Jay Vosburgh027ea042008-01-17 16:25:02 -0800136
Stephen Hemminger373500d2009-06-12 19:02:50 +0000137 /* Always return either count or an error. If you return 0, you'll
138 * get called forever, which is bad.
139 */
140 return res;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800141
142err_no_cmd:
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000143 pr_err(DRV_NAME ": no command found in bonding_masters."
144 " Use +ifname or -ifname.\n");
Jay Vosburghc4ebc662008-05-02 17:49:38 -0700145 return -EPERM;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800146}
Stephen Hemminger373500d2009-06-12 19:02:50 +0000147
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800148/* class attribute for bond_masters file. This ends up in /sys/class/net */
149static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
150 bonding_show_bonds, bonding_store_bonds);
151
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000152int bond_create_slave_symlinks(struct net_device *master,
153 struct net_device *slave)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800154{
155 char linkname[IFNAMSIZ+7];
156 int ret = 0;
157
158 /* first, create a link from the slave back to the master */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700159 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800160 "master");
161 if (ret)
162 return ret;
163 /* next, create a link from the master to the slave */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000164 sprintf(linkname, "slave_%s", slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700165 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800166 linkname);
167 return ret;
168
169}
170
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000171void bond_destroy_slave_symlinks(struct net_device *master,
172 struct net_device *slave)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800173{
174 char linkname[IFNAMSIZ+7];
175
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700176 sysfs_remove_link(&(slave->dev.kobj), "master");
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000177 sprintf(linkname, "slave_%s", slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700178 sysfs_remove_link(&(master->dev.kobj), linkname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800179}
180
181
182/*
183 * Show the slaves in the current bond.
184 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700185static ssize_t bonding_show_slaves(struct device *d,
186 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800187{
188 struct slave *slave;
189 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700190 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800191
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700192 read_lock(&bond->lock);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800193 bond_for_each_slave(bond, slave, i) {
194 if (res > (PAGE_SIZE - IFNAMSIZ)) {
195 /* not enough space for another interface name */
196 if ((PAGE_SIZE - res) > 10)
197 res = PAGE_SIZE - 10;
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800198 res += sprintf(buf + res, "++more++ ");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800199 break;
200 }
201 res += sprintf(buf + res, "%s ", slave->dev->name);
202 }
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700203 read_unlock(&bond->lock);
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800204 if (res)
205 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800206 return res;
207}
208
209/*
210 * Set the slaves in the current bond. The bond interface must be
211 * up for this to succeed.
212 * This function is largely the same flow as bonding_update_bonds().
213 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700214static ssize_t bonding_store_slaves(struct device *d,
215 struct device_attribute *attr,
216 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800217{
218 char command[IFNAMSIZ + 1] = { 0, };
219 char *ifname;
220 int i, res, found, ret = count;
Moni Shoua3158bf72007-10-09 19:43:41 -0700221 u32 original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800222 struct slave *slave;
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -0800223 struct net_device *dev = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700224 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800225
226 /* Quick sanity check -- is the bond interface up? */
227 if (!(bond->dev->flags & IFF_UP)) {
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +0000228 pr_warning(DRV_NAME ": %s: doing slave updates when "
229 "interface is down.\n", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800230 }
231
232 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
233
Eric W. Biederman496a60c2009-05-13 17:02:50 +0000234 if (!rtnl_trylock())
235 return restart_syscall();
Jay Vosburgh027ea042008-01-17 16:25:02 -0800236
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800237 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
238 ifname = command + 1;
239 if ((strlen(command) <= 1) ||
240 !dev_valid_name(ifname))
241 goto err_no_cmd;
242
243 if (command[0] == '+') {
244
245 /* Got a slave name in ifname. Is it already in the list? */
246 found = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800247
Eric W. Biedermanec87fd32009-10-29 14:18:26 +0000248 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800249 if (!dev) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000250 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800251 ": %s: Interface %s does not exist!\n",
252 bond->dev->name, ifname);
Stephen Hemminger373500d2009-06-12 19:02:50 +0000253 ret = -ENODEV;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800254 goto out;
Stephen Hemminger373500d2009-06-12 19:02:50 +0000255 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800256
257 if (dev->flags & IFF_UP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000258 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800259 ": %s: Error: Unable to enslave %s "
260 "because it is already up.\n",
261 bond->dev->name, dev->name);
262 ret = -EPERM;
263 goto out;
264 }
Stephen Hemminger373500d2009-06-12 19:02:50 +0000265
266 read_lock(&bond->lock);
267 bond_for_each_slave(bond, slave, i)
268 if (slave->dev == dev) {
269 pr_err(DRV_NAME
270 ": %s: Interface %s is already enslaved!\n",
271 bond->dev->name, ifname);
272 ret = -EPERM;
273 read_unlock(&bond->lock);
274 goto out;
275 }
276 read_unlock(&bond->lock);
277
278 pr_info(DRV_NAME ": %s: Adding slave %s.\n",
279 bond->dev->name, ifname);
280
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800281 /* If this is the first slave, then we need to set
282 the master's hardware address to be the same as the
283 slave's. */
Stephen Hemminger5c5129b2009-06-12 19:02:51 +0000284 if (is_zero_ether_addr(bond->dev->dev_addr))
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800285 memcpy(bond->dev->dev_addr, dev->dev_addr,
286 dev->addr_len);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800287
288 /* Set the slave's MTU to match the bond */
Moni Shoua3158bf72007-10-09 19:43:41 -0700289 original_mtu = dev->mtu;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800290 res = dev_set_mtu(dev, bond->dev->mtu);
291 if (res) {
292 ret = res;
293 goto out;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800294 }
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800295
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800296 res = bond_enslave(bond->dev, dev);
Moni Shoua3158bf72007-10-09 19:43:41 -0700297 bond_for_each_slave(bond, slave, i)
298 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
299 slave->original_mtu = original_mtu;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000300 if (res)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800301 ret = res;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000302
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800303 goto out;
304 }
305
306 if (command[0] == '-') {
307 dev = NULL;
David S. Miller6952d8922008-03-28 16:15:38 -0700308 original_mtu = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800309 bond_for_each_slave(bond, slave, i)
310 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
311 dev = slave->dev;
Moni Shoua3158bf72007-10-09 19:43:41 -0700312 original_mtu = slave->original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800313 break;
314 }
315 if (dev) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000316 pr_info(DRV_NAME ": %s: Removing slave %s\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800317 bond->dev->name, dev->name);
Moni Shouad90a1622007-10-09 19:43:43 -0700318 res = bond_release(bond->dev, dev);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800319 if (res) {
320 ret = res;
321 goto out;
322 }
323 /* set the slave MTU to the default */
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800324 dev_set_mtu(dev, original_mtu);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000325 } else {
326 pr_err(DRV_NAME ": unable to remove non-existent"
327 " slave %s for bond %s.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800328 ifname, bond->dev->name);
329 ret = -ENODEV;
330 }
331 goto out;
332 }
333
334err_no_cmd:
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000335 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 -0800336 ret = -EPERM;
337
338out:
Jay Vosburgh027ea042008-01-17 16:25:02 -0800339 rtnl_unlock();
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800340 return ret;
341}
342
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000343static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
344 bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800345
346/*
347 * Show and set the bonding mode. The bond interface must be down to
348 * change the mode.
349 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700350static ssize_t bonding_show_mode(struct device *d,
351 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800352{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700353 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800354
355 return sprintf(buf, "%s %d\n",
356 bond_mode_tbl[bond->params.mode].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800357 bond->params.mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800358}
359
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700360static ssize_t bonding_store_mode(struct device *d,
361 struct device_attribute *attr,
362 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800363{
364 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700365 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800366
367 if (bond->dev->flags & IFF_UP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000368 pr_err(DRV_NAME ": unable to update mode of %s"
369 " because interface is up.\n", bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800370 ret = -EPERM;
371 goto out;
372 }
373
Jay Vosburghece95f72008-01-17 16:25:01 -0800374 new_value = bond_parse_parm(buf, bond_mode_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800375 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000376 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800377 ": %s: Ignoring invalid mode value %.*s.\n",
378 bond->dev->name,
379 (int)strlen(buf) - 1, buf);
380 ret = -EINVAL;
381 goto out;
382 } else {
Jay Vosburgh8f903c72006-02-21 16:36:44 -0800383 if (bond->params.mode == BOND_MODE_8023AD)
384 bond_unset_master_3ad_flags(bond);
385
386 if (bond->params.mode == BOND_MODE_ALB)
387 bond_unset_master_alb_flags(bond);
388
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800389 bond->params.mode = new_value;
390 bond_set_mode_ops(bond, bond->params.mode);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000391 pr_info(DRV_NAME ": %s: setting mode to %s (%d).\n",
392 bond->dev->name, bond_mode_tbl[new_value].modename,
393 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800394 }
395out:
396 return ret;
397}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000398static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
399 bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800400
401/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000402 * Show and set the bonding transmit hash method.
403 * The bond interface must be down to change the xmit hash policy.
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800404 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700405static ssize_t bonding_show_xmit_hash(struct device *d,
406 struct device_attribute *attr,
407 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800408{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700409 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800410
Wagner Ferenc8e4b9322007-12-06 23:40:32 -0800411 return sprintf(buf, "%s %d\n",
412 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
413 bond->params.xmit_policy);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800414}
415
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700416static ssize_t bonding_store_xmit_hash(struct device *d,
417 struct device_attribute *attr,
418 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800419{
420 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700421 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800422
423 if (bond->dev->flags & IFF_UP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000424 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800425 "%s: Interface is up. Unable to update xmit policy.\n",
426 bond->dev->name);
427 ret = -EPERM;
428 goto out;
429 }
430
Jay Vosburghece95f72008-01-17 16:25:01 -0800431 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800432 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000433 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800434 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
435 bond->dev->name,
436 (int)strlen(buf) - 1, buf);
437 ret = -EINVAL;
438 goto out;
439 } else {
440 bond->params.xmit_policy = new_value;
441 bond_set_mode_ops(bond, bond->params.mode);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000442 pr_info(DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
443 bond->dev->name,
444 xmit_hashtype_tbl[new_value].modename, new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800445 }
446out:
447 return ret;
448}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000449static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
450 bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800451
452/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700453 * Show and set arp_validate.
454 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700455static ssize_t bonding_show_arp_validate(struct device *d,
456 struct device_attribute *attr,
457 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700458{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700459 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700460
461 return sprintf(buf, "%s %d\n",
462 arp_validate_tbl[bond->params.arp_validate].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800463 bond->params.arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700464}
465
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700466static ssize_t bonding_store_arp_validate(struct device *d,
467 struct device_attribute *attr,
468 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700469{
470 int new_value;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700471 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700472
Jay Vosburghece95f72008-01-17 16:25:01 -0800473 new_value = bond_parse_parm(buf, arp_validate_tbl);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700474 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000475 pr_err(DRV_NAME
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700476 ": %s: Ignoring invalid arp_validate value %s\n",
477 bond->dev->name, buf);
478 return -EINVAL;
479 }
480 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000481 pr_err(DRV_NAME
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700482 ": %s: arp_validate only supported in active-backup mode.\n",
483 bond->dev->name);
484 return -EINVAL;
485 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000486 pr_info(DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700487 bond->dev->name, arp_validate_tbl[new_value].modename,
488 new_value);
489
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000490 if (!bond->params.arp_validate && new_value)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700491 bond_register_arp(bond);
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000492 else if (bond->params.arp_validate && !new_value)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700493 bond_unregister_arp(bond);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700494
495 bond->params.arp_validate = new_value;
496
497 return count;
498}
499
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000500static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
501 bonding_store_arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700502
503/*
Jay Vosburghdd957c52007-10-09 19:57:24 -0700504 * Show and store fail_over_mac. User only allowed to change the
505 * value when there are no slaves.
506 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000507static ssize_t bonding_show_fail_over_mac(struct device *d,
508 struct device_attribute *attr,
509 char *buf)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700510{
511 struct bonding *bond = to_bond(d);
512
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700513 return sprintf(buf, "%s %d\n",
514 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
515 bond->params.fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700516}
517
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000518static ssize_t bonding_store_fail_over_mac(struct device *d,
519 struct device_attribute *attr,
520 const char *buf, size_t count)
Jay Vosburghdd957c52007-10-09 19:57:24 -0700521{
522 int new_value;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700523 struct bonding *bond = to_bond(d);
524
525 if (bond->slave_cnt != 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000526 pr_err(DRV_NAME
Jay Vosburghdd957c52007-10-09 19:57:24 -0700527 ": %s: Can't alter fail_over_mac with slaves in bond.\n",
528 bond->dev->name);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700529 return -EPERM;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700530 }
531
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700532 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
533 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000534 pr_err(DRV_NAME
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700535 ": %s: Ignoring invalid fail_over_mac value %s.\n",
536 bond->dev->name, buf);
537 return -EINVAL;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700538 }
539
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700540 bond->params.fail_over_mac = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000541 pr_info(DRV_NAME ": %s: Setting fail_over_mac to %s (%d).\n",
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700542 bond->dev->name, fail_over_mac_tbl[new_value].modename,
543 new_value);
544
545 return count;
Jay Vosburghdd957c52007-10-09 19:57:24 -0700546}
547
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000548static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
549 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
Jay Vosburghdd957c52007-10-09 19:57:24 -0700550
551/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800552 * Show and set the arp timer interval. There are two tricky bits
553 * here. First, if ARP monitoring is activated, then we must disable
554 * MII monitoring. Second, if the ARP timer isn't running, we must
555 * start it.
556 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700557static ssize_t bonding_show_arp_interval(struct device *d,
558 struct device_attribute *attr,
559 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800560{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700561 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800562
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800563 return sprintf(buf, "%d\n", bond->params.arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800564}
565
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700566static ssize_t bonding_store_arp_interval(struct device *d,
567 struct device_attribute *attr,
568 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800569{
570 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700571 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800572
573 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000574 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800575 ": %s: no arp_interval value specified.\n",
576 bond->dev->name);
577 ret = -EINVAL;
578 goto out;
579 }
580 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000581 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800582 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
583 bond->dev->name, new_value, INT_MAX);
584 ret = -EINVAL;
585 goto out;
586 }
587
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000588 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800589 ": %s: Setting ARP monitoring interval to %d.\n",
590 bond->dev->name, new_value);
591 bond->params.arp_interval = new_value;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -0800592 if (bond->params.arp_interval)
593 bond->dev->priv_flags |= IFF_MASTER_ARPMON;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800594 if (bond->params.miimon) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000595 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800596 ": %s: ARP monitoring cannot be used with MII monitoring. "
597 "%s Disabling MII monitoring.\n",
598 bond->dev->name, bond->dev->name);
599 bond->params.miimon = 0;
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700600 if (delayed_work_pending(&bond->mii_work)) {
601 cancel_delayed_work(&bond->mii_work);
602 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800603 }
604 }
605 if (!bond->params.arp_targets[0]) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000606 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800607 ": %s: ARP monitoring has been set up, "
608 "but no ARP targets have been specified.\n",
609 bond->dev->name);
610 }
611 if (bond->dev->flags & IFF_UP) {
612 /* If the interface is up, we may need to fire off
613 * the ARP timer. If the interface is down, the
614 * timer will get fired off when the open function
615 * is called.
616 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -0700617 if (!delayed_work_pending(&bond->arp_work)) {
618 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
619 INIT_DELAYED_WORK(&bond->arp_work,
620 bond_activebackup_arp_mon);
621 else
622 INIT_DELAYED_WORK(&bond->arp_work,
623 bond_loadbalance_arp_mon);
624
625 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800626 }
627 }
628
629out:
630 return ret;
631}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000632static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
633 bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800634
635/*
636 * Show and set the arp targets.
637 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700638static ssize_t bonding_show_arp_targets(struct device *d,
639 struct device_attribute *attr,
640 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800641{
642 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700643 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800644
645 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
646 if (bond->params.arp_targets[i])
Harvey Harrison63779432008-10-31 00:56:00 -0700647 res += sprintf(buf + res, "%pI4 ",
648 &bond->params.arp_targets[i]);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800649 }
Wagner Ferenc1dcdcd62007-12-06 23:40:31 -0800650 if (res)
651 buf[res-1] = '\n'; /* eat the leftover space */
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800652 return res;
653}
654
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700655static ssize_t bonding_store_arp_targets(struct device *d,
656 struct device_attribute *attr,
657 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800658{
Al Virod3bb52b2007-08-22 20:06:58 -0400659 __be32 newtarget;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800660 int i = 0, done = 0, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700661 struct bonding *bond = to_bond(d);
Al Virod3bb52b2007-08-22 20:06:58 -0400662 __be32 *targets;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800663
664 targets = bond->params.arp_targets;
665 newtarget = in_aton(buf + 1);
666 /* look for adds */
667 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400668 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000669 pr_err(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700670 ": %s: invalid ARP target %pI4 specified for addition\n",
671 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800672 ret = -EINVAL;
673 goto out;
674 }
675 /* look for an empty slot to put the target in, and check for dupes */
Brian Haley5a31bec2009-04-13 00:11:30 -0700676 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800677 if (targets[i] == newtarget) { /* duplicate */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000678 pr_err(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700679 ": %s: ARP target %pI4 is already present\n",
680 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800681 ret = -EINVAL;
682 goto out;
683 }
Brian Haley5a31bec2009-04-13 00:11:30 -0700684 if (targets[i] == 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000685 pr_info(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700686 ": %s: adding ARP target %pI4.\n",
687 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800688 done = 1;
689 targets[i] = newtarget;
690 }
691 }
692 if (!done) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000693 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800694 ": %s: ARP target table is full!\n",
695 bond->dev->name);
696 ret = -EINVAL;
697 goto out;
698 }
699
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000700 } else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400701 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000702 pr_err(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700703 ": %s: invalid ARP target %pI4 specified for removal\n",
704 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800705 ret = -EINVAL;
706 goto out;
707 }
708
Brian Haley5a31bec2009-04-13 00:11:30 -0700709 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800710 if (targets[i] == newtarget) {
Brian Haley5a31bec2009-04-13 00:11:30 -0700711 int j;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000712 pr_info(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700713 ": %s: removing ARP target %pI4.\n",
714 bond->dev->name, &newtarget);
Brian Haley5a31bec2009-04-13 00:11:30 -0700715 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
716 targets[j] = targets[j+1];
717
718 targets[j] = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800719 done = 1;
720 }
721 }
722 if (!done) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000723 pr_info(DRV_NAME
Harvey Harrison63779432008-10-31 00:56:00 -0700724 ": %s: unable to remove nonexistent ARP target %pI4.\n",
725 bond->dev->name, &newtarget);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800726 ret = -EINVAL;
727 goto out;
728 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000729 } else {
730 pr_err(DRV_NAME ": no command found in arp_ip_targets file"
731 " for bond %s. Use +<addr> or -<addr>.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800732 bond->dev->name);
733 ret = -EPERM;
734 goto out;
735 }
736
737out:
738 return ret;
739}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700740static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800741
742/*
743 * Show and set the up and down delays. These must be multiples of the
744 * MII monitoring value, and are stored internally as the multiplier.
745 * Thus, we must translate to MS for the real world.
746 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700747static ssize_t bonding_show_downdelay(struct device *d,
748 struct device_attribute *attr,
749 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800750{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700751 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800752
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800753 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800754}
755
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700756static ssize_t bonding_store_downdelay(struct device *d,
757 struct device_attribute *attr,
758 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800759{
760 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700761 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800762
763 if (!(bond->params.miimon)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000764 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800765 ": %s: Unable to set down delay as MII monitoring is disabled\n",
766 bond->dev->name);
767 ret = -EPERM;
768 goto out;
769 }
770
771 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000772 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800773 ": %s: no down delay value specified.\n",
774 bond->dev->name);
775 ret = -EINVAL;
776 goto out;
777 }
778 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000779 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800780 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
781 bond->dev->name, new_value, 1, INT_MAX);
782 ret = -EINVAL;
783 goto out;
784 } else {
785 if ((new_value % bond->params.miimon) != 0) {
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +0000786 pr_warning(DRV_NAME
787 ": %s: Warning: down delay (%d) is not a "
788 "multiple of miimon (%d), delay rounded "
789 "to %d ms\n",
790 bond->dev->name, new_value,
791 bond->params.miimon,
792 (new_value / bond->params.miimon) *
793 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800794 }
795 bond->params.downdelay = new_value / bond->params.miimon;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000796 pr_info(DRV_NAME ": %s: Setting down delay to %d.\n",
797 bond->dev->name,
798 bond->params.downdelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800799
800 }
801
802out:
803 return ret;
804}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000805static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
806 bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800807
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700808static ssize_t bonding_show_updelay(struct device *d,
809 struct device_attribute *attr,
810 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800811{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700812 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800813
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800814 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800815
816}
817
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700818static ssize_t bonding_store_updelay(struct device *d,
819 struct device_attribute *attr,
820 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800821{
822 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700823 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800824
825 if (!(bond->params.miimon)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000826 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800827 ": %s: Unable to set up delay as MII monitoring is disabled\n",
828 bond->dev->name);
829 ret = -EPERM;
830 goto out;
831 }
832
833 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000834 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800835 ": %s: no up delay value specified.\n",
836 bond->dev->name);
837 ret = -EINVAL;
838 goto out;
839 }
840 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000841 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800842 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
843 bond->dev->name, new_value, 1, INT_MAX);
844 ret = -EINVAL;
845 goto out;
846 } else {
847 if ((new_value % bond->params.miimon) != 0) {
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +0000848 pr_warning(DRV_NAME
849 ": %s: Warning: up delay (%d) is not a "
850 "multiple of miimon (%d), updelay rounded "
851 "to %d ms\n",
852 bond->dev->name, new_value,
853 bond->params.miimon,
854 (new_value / bond->params.miimon) *
855 bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800856 }
857 bond->params.updelay = new_value / bond->params.miimon;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000858 pr_info(DRV_NAME ": %s: Setting up delay to %d.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800859 bond->dev->name, bond->params.updelay * bond->params.miimon);
860
861 }
862
863out:
864 return ret;
865}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000866static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
867 bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800868
869/*
870 * Show and set the LACP interval. Interface must be down, and the mode
871 * must be set to 802.3ad mode.
872 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700873static ssize_t bonding_show_lacp(struct device *d,
874 struct device_attribute *attr,
875 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800876{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700877 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800878
879 return sprintf(buf, "%s %d\n",
880 bond_lacp_tbl[bond->params.lacp_fast].modename,
Wagner Ferenc7bd46502007-12-06 23:40:28 -0800881 bond->params.lacp_fast);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800882}
883
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700884static ssize_t bonding_store_lacp(struct device *d,
885 struct device_attribute *attr,
886 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800887{
888 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700889 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800890
891 if (bond->dev->flags & IFF_UP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000892 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800893 ": %s: Unable to update LACP rate because interface is up.\n",
894 bond->dev->name);
895 ret = -EPERM;
896 goto out;
897 }
898
899 if (bond->params.mode != BOND_MODE_8023AD) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000900 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800901 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
902 bond->dev->name);
903 ret = -EPERM;
904 goto out;
905 }
906
Jay Vosburghece95f72008-01-17 16:25:01 -0800907 new_value = bond_parse_parm(buf, bond_lacp_tbl);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800908
909 if ((new_value == 1) || (new_value == 0)) {
910 bond->params.lacp_fast = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000911 pr_info(DRV_NAME ": %s: Setting LACP rate to %s (%d).\n",
912 bond->dev->name, bond_lacp_tbl[new_value].modename,
913 new_value);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800914 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000915 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800916 ": %s: Ignoring invalid LACP rate value %.*s.\n",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000917 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800918 ret = -EINVAL;
919 }
920out:
921 return ret;
922}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000923static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
924 bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800925
Jay Vosburghfd989c82008-11-04 17:51:16 -0800926static ssize_t bonding_show_ad_select(struct device *d,
927 struct device_attribute *attr,
928 char *buf)
929{
930 struct bonding *bond = to_bond(d);
931
932 return sprintf(buf, "%s %d\n",
933 ad_select_tbl[bond->params.ad_select].modename,
934 bond->params.ad_select);
935}
936
937
938static ssize_t bonding_store_ad_select(struct device *d,
939 struct device_attribute *attr,
940 const char *buf, size_t count)
941{
942 int new_value, ret = count;
943 struct bonding *bond = to_bond(d);
944
945 if (bond->dev->flags & IFF_UP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000946 pr_err(DRV_NAME
Jay Vosburghfd989c82008-11-04 17:51:16 -0800947 ": %s: Unable to update ad_select because interface "
948 "is up.\n", bond->dev->name);
949 ret = -EPERM;
950 goto out;
951 }
952
953 new_value = bond_parse_parm(buf, ad_select_tbl);
954
955 if (new_value != -1) {
956 bond->params.ad_select = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000957 pr_info(DRV_NAME
Jay Vosburghfd989c82008-11-04 17:51:16 -0800958 ": %s: Setting ad_select to %s (%d).\n",
959 bond->dev->name, ad_select_tbl[new_value].modename,
960 new_value);
961 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000962 pr_err(DRV_NAME
Jay Vosburghfd989c82008-11-04 17:51:16 -0800963 ": %s: Ignoring invalid ad_select value %.*s.\n",
964 bond->dev->name, (int)strlen(buf) - 1, buf);
965 ret = -EINVAL;
966 }
967out:
968 return ret;
969}
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000970static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
971 bonding_show_ad_select, bonding_store_ad_select);
Jay Vosburghfd989c82008-11-04 17:51:16 -0800972
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800973/*
Moni Shoua7893b242008-05-17 21:10:12 -0700974 * Show and set the number of grat ARP to send after a failover event.
975 */
976static ssize_t bonding_show_n_grat_arp(struct device *d,
977 struct device_attribute *attr,
978 char *buf)
979{
980 struct bonding *bond = to_bond(d);
981
982 return sprintf(buf, "%d\n", bond->params.num_grat_arp);
983}
984
985static ssize_t bonding_store_n_grat_arp(struct device *d,
986 struct device_attribute *attr,
987 const char *buf, size_t count)
988{
989 int new_value, ret = count;
990 struct bonding *bond = to_bond(d);
991
992 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000993 pr_err(DRV_NAME
Moni Shoua7893b242008-05-17 21:10:12 -0700994 ": %s: no num_grat_arp value specified.\n",
995 bond->dev->name);
996 ret = -EINVAL;
997 goto out;
998 }
999 if (new_value < 0 || new_value > 255) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001000 pr_err(DRV_NAME
Moni Shoua7893b242008-05-17 21:10:12 -07001001 ": %s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
1002 bond->dev->name, new_value);
1003 ret = -EINVAL;
1004 goto out;
1005 } else {
1006 bond->params.num_grat_arp = new_value;
1007 }
1008out:
1009 return ret;
1010}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001011static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
1012 bonding_show_n_grat_arp, bonding_store_n_grat_arp);
Brian Haley305d5522008-11-04 17:51:14 -08001013
1014/*
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001015 * Show and set the number of unsolicited NA's to send after a failover event.
Brian Haley305d5522008-11-04 17:51:14 -08001016 */
1017static ssize_t bonding_show_n_unsol_na(struct device *d,
1018 struct device_attribute *attr,
1019 char *buf)
1020{
1021 struct bonding *bond = to_bond(d);
1022
1023 return sprintf(buf, "%d\n", bond->params.num_unsol_na);
1024}
1025
1026static ssize_t bonding_store_n_unsol_na(struct device *d,
1027 struct device_attribute *attr,
1028 const char *buf, size_t count)
1029{
1030 int new_value, ret = count;
1031 struct bonding *bond = to_bond(d);
1032
1033 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001034 pr_err(DRV_NAME
Brian Haley305d5522008-11-04 17:51:14 -08001035 ": %s: no num_unsol_na value specified.\n",
1036 bond->dev->name);
1037 ret = -EINVAL;
1038 goto out;
1039 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001040
Brian Haley305d5522008-11-04 17:51:14 -08001041 if (new_value < 0 || new_value > 255) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001042 pr_err(DRV_NAME
Brian Haley305d5522008-11-04 17:51:14 -08001043 ": %s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
1044 bond->dev->name, new_value);
1045 ret = -EINVAL;
1046 goto out;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001047 } else
Brian Haley305d5522008-11-04 17:51:14 -08001048 bond->params.num_unsol_na = new_value;
Brian Haley305d5522008-11-04 17:51:14 -08001049out:
1050 return ret;
1051}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001052static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
1053 bonding_show_n_unsol_na, bonding_store_n_unsol_na);
Brian Haley305d5522008-11-04 17:51:14 -08001054
Moni Shoua7893b242008-05-17 21:10:12 -07001055/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001056 * Show and set the MII monitor interval. There are two tricky bits
1057 * here. First, if MII monitoring is activated, then we must disable
1058 * ARP monitoring. Second, if the timer isn't running, we must
1059 * start it.
1060 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001061static ssize_t bonding_show_miimon(struct device *d,
1062 struct device_attribute *attr,
1063 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001064{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001065 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001066
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001067 return sprintf(buf, "%d\n", bond->params.miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001068}
1069
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001070static ssize_t bonding_store_miimon(struct device *d,
1071 struct device_attribute *attr,
1072 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001073{
1074 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001075 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001076
1077 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001078 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001079 ": %s: no miimon value specified.\n",
1080 bond->dev->name);
1081 ret = -EINVAL;
1082 goto out;
1083 }
1084 if (new_value < 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001085 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001086 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1087 bond->dev->name, new_value, 1, INT_MAX);
1088 ret = -EINVAL;
1089 goto out;
1090 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001091 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001092 ": %s: Setting MII monitoring interval to %d.\n",
1093 bond->dev->name, new_value);
1094 bond->params.miimon = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001095 if (bond->params.updelay)
1096 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001097 ": %s: Note: Updating updelay (to %d) "
1098 "since it is a multiple of the miimon value.\n",
1099 bond->dev->name,
1100 bond->params.updelay * bond->params.miimon);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001101 if (bond->params.downdelay)
1102 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001103 ": %s: Note: Updating downdelay (to %d) "
1104 "since it is a multiple of the miimon value.\n",
1105 bond->dev->name,
1106 bond->params.downdelay * bond->params.miimon);
1107 if (bond->params.arp_interval) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001108 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001109 ": %s: MII monitoring cannot be used with "
1110 "ARP monitoring. Disabling ARP monitoring...\n",
1111 bond->dev->name);
1112 bond->params.arp_interval = 0;
Jay Vosburgh6cf3f412008-11-03 18:16:50 -08001113 bond->dev->priv_flags &= ~IFF_MASTER_ARPMON;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001114 if (bond->params.arp_validate) {
1115 bond_unregister_arp(bond);
1116 bond->params.arp_validate =
1117 BOND_ARP_VALIDATE_NONE;
1118 }
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001119 if (delayed_work_pending(&bond->arp_work)) {
1120 cancel_delayed_work(&bond->arp_work);
1121 flush_workqueue(bond->wq);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001122 }
1123 }
1124
1125 if (bond->dev->flags & IFF_UP) {
1126 /* If the interface is up, we may need to fire off
1127 * the MII timer. If the interface is down, the
1128 * timer will get fired off when the open function
1129 * is called.
1130 */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001131 if (!delayed_work_pending(&bond->mii_work)) {
1132 INIT_DELAYED_WORK(&bond->mii_work,
1133 bond_mii_monitor);
1134 queue_delayed_work(bond->wq,
1135 &bond->mii_work, 0);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001136 }
1137 }
1138 }
1139out:
1140 return ret;
1141}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001142static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1143 bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001144
1145/*
1146 * Show and set the primary slave. The store function is much
1147 * simpler than bonding_store_slaves function because it only needs to
1148 * handle one interface name.
1149 * The bond must be a mode that supports a primary for this be
1150 * set.
1151 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001152static ssize_t bonding_show_primary(struct device *d,
1153 struct device_attribute *attr,
1154 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001155{
1156 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001157 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001158
1159 if (bond->primary_slave)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001160 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001161
1162 return count;
1163}
1164
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001165static ssize_t bonding_store_primary(struct device *d,
1166 struct device_attribute *attr,
1167 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001168{
1169 int i;
1170 struct slave *slave;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001171 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001172
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001173 if (!rtnl_trylock())
1174 return restart_syscall();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001175 read_lock(&bond->lock);
1176 write_lock_bh(&bond->curr_slave_lock);
1177
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001178 if (!USES_PRIMARY(bond->params.mode)) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001179 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001180 ": %s: Unable to set primary slave; %s is in mode %d\n",
1181 bond->dev->name, bond->dev->name, bond->params.mode);
1182 } else {
1183 bond_for_each_slave(bond, slave, i) {
1184 if (strnicmp
1185 (slave->dev->name, buf,
1186 strlen(slave->dev->name)) == 0) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001187 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001188 ": %s: Setting %s as primary slave.\n",
1189 bond->dev->name, slave->dev->name);
1190 bond->primary_slave = slave;
Jiri Pirkoce501ca2009-09-18 02:13:22 +00001191 strcpy(bond->params.primary, slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001192 bond_select_active_slave(bond);
1193 goto out;
1194 }
1195 }
1196
1197 /* if we got here, then we didn't match the name of any slave */
1198
1199 if (strlen(buf) == 0 || buf[0] == '\n') {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001200 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001201 ": %s: Setting primary slave to None.\n",
1202 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001203 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001204 bond_select_active_slave(bond);
1205 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001206 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001207 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1208 bond->dev->name, (int)strlen(buf) - 1, buf);
1209 }
1210 }
1211out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001212 write_unlock_bh(&bond->curr_slave_lock);
1213 read_unlock(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001214 rtnl_unlock();
1215
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001216 return count;
1217}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001218static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1219 bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001220
1221/*
Jiri Pirkoa5499522009-09-25 03:28:09 +00001222 * Show and set the primary_reselect flag.
1223 */
1224static ssize_t bonding_show_primary_reselect(struct device *d,
1225 struct device_attribute *attr,
1226 char *buf)
1227{
1228 struct bonding *bond = to_bond(d);
1229
1230 return sprintf(buf, "%s %d\n",
1231 pri_reselect_tbl[bond->params.primary_reselect].modename,
1232 bond->params.primary_reselect);
1233}
1234
1235static ssize_t bonding_store_primary_reselect(struct device *d,
1236 struct device_attribute *attr,
1237 const char *buf, size_t count)
1238{
1239 int new_value, ret = count;
1240 struct bonding *bond = to_bond(d);
1241
1242 if (!rtnl_trylock())
1243 return restart_syscall();
1244
1245 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1246 if (new_value < 0) {
1247 pr_err(DRV_NAME
1248 ": %s: Ignoring invalid primary_reselect value %.*s.\n",
1249 bond->dev->name,
1250 (int) strlen(buf) - 1, buf);
1251 ret = -EINVAL;
1252 goto out;
1253 }
1254
1255 bond->params.primary_reselect = new_value;
1256 pr_info(DRV_NAME ": %s: setting primary_reselect to %s (%d).\n",
1257 bond->dev->name, pri_reselect_tbl[new_value].modename,
1258 new_value);
1259
1260 read_lock(&bond->lock);
1261 write_lock_bh(&bond->curr_slave_lock);
1262 bond_select_active_slave(bond);
1263 write_unlock_bh(&bond->curr_slave_lock);
1264 read_unlock(&bond->lock);
1265out:
1266 rtnl_unlock();
1267 return ret;
1268}
1269static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1270 bonding_show_primary_reselect,
1271 bonding_store_primary_reselect);
1272
1273/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001274 * Show and set the use_carrier flag.
1275 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001276static ssize_t bonding_show_carrier(struct device *d,
1277 struct device_attribute *attr,
1278 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001279{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001280 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001281
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001282 return sprintf(buf, "%d\n", bond->params.use_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001283}
1284
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001285static ssize_t bonding_store_carrier(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 new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001290 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001291
1292
1293 if (sscanf(buf, "%d", &new_value) != 1) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001294 pr_err(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001295 ": %s: no use_carrier value specified.\n",
1296 bond->dev->name);
1297 ret = -EINVAL;
1298 goto out;
1299 }
1300 if ((new_value == 0) || (new_value == 1)) {
1301 bond->params.use_carrier = new_value;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001302 pr_info(DRV_NAME ": %s: Setting use_carrier to %d.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001303 bond->dev->name, new_value);
1304 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001305 pr_info(DRV_NAME
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001306 ": %s: Ignoring invalid use_carrier value %d.\n",
1307 bond->dev->name, new_value);
1308 }
1309out:
1310 return count;
1311}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001312static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1313 bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001314
1315
1316/*
1317 * Show and set currently active_slave.
1318 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001319static ssize_t bonding_show_active_slave(struct device *d,
1320 struct device_attribute *attr,
1321 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001322{
1323 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001324 struct bonding *bond = to_bond(d);
Wagner Ferenc16cd0162007-12-06 23:40:29 -08001325 int count = 0;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001326
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001327 read_lock(&bond->curr_slave_lock);
1328 curr = bond->curr_active_slave;
1329 read_unlock(&bond->curr_slave_lock);
1330
1331 if (USES_PRIMARY(bond->params.mode) && curr)
Wagner Ferenc7bd46502007-12-06 23:40:28 -08001332 count = sprintf(buf, "%s\n", curr->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001333 return count;
1334}
1335
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001336static ssize_t bonding_store_active_slave(struct device *d,
1337 struct device_attribute *attr,
1338 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001339{
1340 int i;
1341 struct slave *slave;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001342 struct slave *old_active = NULL;
1343 struct slave *new_active = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001344 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001345
Eric W. Biederman496a60c2009-05-13 17:02:50 +00001346 if (!rtnl_trylock())
1347 return restart_syscall();
Jay Vosburghe934dd72008-01-17 16:24:57 -08001348 read_lock(&bond->lock);
1349 write_lock_bh(&bond->curr_slave_lock);
Jay Vosburgh1466a212007-11-06 13:33:28 -08001350
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001351 if (!USES_PRIMARY(bond->params.mode))
1352 pr_info(DRV_NAME ": %s: Unable to change active slave;"
1353 " %s is in mode %d\n",
1354 bond->dev->name, bond->dev->name, bond->params.mode);
1355 else {
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001356 bond_for_each_slave(bond, slave, i) {
1357 if (strnicmp
1358 (slave->dev->name, buf,
1359 strlen(slave->dev->name)) == 0) {
1360 old_active = bond->curr_active_slave;
1361 new_active = slave;
Jay Vosburgha50d8de2006-09-22 21:53:25 -07001362 if (new_active == old_active) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001363 /* do nothing */
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00001364 pr_info(DRV_NAME
1365 ": %s: %s is already the current active slave.\n",
1366 bond->dev->name, slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001367 goto out;
1368 }
1369 else {
1370 if ((new_active) &&
1371 (old_active) &&
1372 (new_active->link == BOND_LINK_UP) &&
1373 IS_UP(new_active->dev)) {
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00001374 pr_info(DRV_NAME
1375 ": %s: Setting %s as active slave.\n",
1376 bond->dev->name, slave->dev->name);
1377 bond_change_active_slave(bond, new_active);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001378 }
1379 else {
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00001380 pr_info(DRV_NAME
1381 ": %s: Could not set %s as active slave; "
1382 "either %s is down or the link is down.\n",
1383 bond->dev->name, slave->dev->name,
1384 slave->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001385 }
1386 goto out;
1387 }
1388 }
1389 }
1390
1391 /* if we got here, then we didn't match the name of any slave */
1392
1393 if (strlen(buf) == 0 || buf[0] == '\n') {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001394 pr_info(DRV_NAME
1395 ": %s: Setting active slave to None.\n",
1396 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001397 bond->primary_slave = NULL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001398 bond_select_active_slave(bond);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001399 } else {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001400 pr_info(DRV_NAME ": %s: Unable to set %.*s"
1401 " as active slave as it is not a slave.\n",
1402 bond->dev->name, (int)strlen(buf) - 1, buf);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001403 }
1404 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001405 out:
Jay Vosburghe934dd72008-01-17 16:24:57 -08001406 write_unlock_bh(&bond->curr_slave_lock);
1407 read_unlock(&bond->lock);
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001408 rtnl_unlock();
1409
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001410 return count;
1411
1412}
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001413static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1414 bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001415
1416
1417/*
1418 * Show link status of the bond interface.
1419 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001420static ssize_t bonding_show_mii_status(struct device *d,
1421 struct device_attribute *attr,
1422 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001423{
1424 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001425 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001426
1427 read_lock(&bond->curr_slave_lock);
1428 curr = bond->curr_active_slave;
1429 read_unlock(&bond->curr_slave_lock);
1430
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001431 return sprintf(buf, "%s\n", curr ? "up" : "down");
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001432}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001433static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001434
1435
1436/*
1437 * Show current 802.3ad aggregator ID.
1438 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001439static ssize_t bonding_show_ad_aggregator(struct device *d,
1440 struct device_attribute *attr,
1441 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001442{
1443 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001444 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001445
1446 if (bond->params.mode == BOND_MODE_8023AD) {
1447 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001448 count = sprintf(buf, "%d\n",
1449 (bond_3ad_get_active_agg_info(bond, &ad_info))
1450 ? 0 : ad_info.aggregator_id);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001451 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001452
1453 return count;
1454}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001455static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001456
1457
1458/*
1459 * Show number of active 802.3ad ports.
1460 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001461static ssize_t bonding_show_ad_num_ports(struct device *d,
1462 struct device_attribute *attr,
1463 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001464{
1465 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001466 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001467
1468 if (bond->params.mode == BOND_MODE_8023AD) {
1469 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001470 count = sprintf(buf, "%d\n",
1471 (bond_3ad_get_active_agg_info(bond, &ad_info))
1472 ? 0 : ad_info.ports);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001473 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001474
1475 return count;
1476}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001477static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001478
1479
1480/*
1481 * Show current 802.3ad actor key.
1482 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001483static ssize_t bonding_show_ad_actor_key(struct device *d,
1484 struct device_attribute *attr,
1485 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001486{
1487 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001488 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001489
1490 if (bond->params.mode == BOND_MODE_8023AD) {
1491 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001492 count = sprintf(buf, "%d\n",
1493 (bond_3ad_get_active_agg_info(bond, &ad_info))
1494 ? 0 : ad_info.actor_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001495 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001496
1497 return count;
1498}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001499static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001500
1501
1502/*
1503 * Show current 802.3ad partner key.
1504 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001505static ssize_t bonding_show_ad_partner_key(struct device *d,
1506 struct device_attribute *attr,
1507 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001508{
1509 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001510 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001511
1512 if (bond->params.mode == BOND_MODE_8023AD) {
1513 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001514 count = sprintf(buf, "%d\n",
1515 (bond_3ad_get_active_agg_info(bond, &ad_info))
1516 ? 0 : ad_info.partner_key);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001517 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001518
1519 return count;
1520}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001521static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001522
1523
1524/*
1525 * Show current 802.3ad partner mac.
1526 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001527static ssize_t bonding_show_ad_partner_mac(struct device *d,
1528 struct device_attribute *attr,
1529 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001530{
1531 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001532 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001533
1534 if (bond->params.mode == BOND_MODE_8023AD) {
1535 struct ad_info ad_info;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001536 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
Johannes Berge1749612008-10-27 15:59:26 -07001537 count = sprintf(buf, "%pM\n", ad_info.partner_system);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001538 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001539
1540 return count;
1541}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001542static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001543
1544
1545
1546static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001547 &dev_attr_slaves.attr,
1548 &dev_attr_mode.attr,
Jay Vosburghdd957c52007-10-09 19:57:24 -07001549 &dev_attr_fail_over_mac.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001550 &dev_attr_arp_validate.attr,
1551 &dev_attr_arp_interval.attr,
1552 &dev_attr_arp_ip_target.attr,
1553 &dev_attr_downdelay.attr,
1554 &dev_attr_updelay.attr,
1555 &dev_attr_lacp_rate.attr,
Jay Vosburghfd989c82008-11-04 17:51:16 -08001556 &dev_attr_ad_select.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001557 &dev_attr_xmit_hash_policy.attr,
Moni Shoua7893b242008-05-17 21:10:12 -07001558 &dev_attr_num_grat_arp.attr,
Brian Haley305d5522008-11-04 17:51:14 -08001559 &dev_attr_num_unsol_na.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001560 &dev_attr_miimon.attr,
1561 &dev_attr_primary.attr,
Jiri Pirkoa5499522009-09-25 03:28:09 +00001562 &dev_attr_primary_reselect.attr,
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001563 &dev_attr_use_carrier.attr,
1564 &dev_attr_active_slave.attr,
1565 &dev_attr_mii_status.attr,
1566 &dev_attr_ad_aggregator.attr,
1567 &dev_attr_ad_num_ports.attr,
1568 &dev_attr_ad_actor_key.attr,
1569 &dev_attr_ad_partner_key.attr,
1570 &dev_attr_ad_partner_mac.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001571 NULL,
1572};
1573
1574static struct attribute_group bonding_group = {
1575 .name = "bonding",
1576 .attrs = per_bond_attrs,
1577};
1578
1579/*
1580 * Initialize sysfs. This sets up the bonding_masters file in
1581 * /sys/class/net.
1582 */
1583int bond_create_sysfs(void)
1584{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001585 int ret;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001586
Jay Vosburghb8a97872008-06-13 18:12:04 -07001587 ret = netdev_class_create_file(&class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001588 /*
1589 * Permit multiple loads of the module by ignoring failures to
1590 * create the bonding_masters sysfs file. Bonding devices
1591 * created by second or subsequent loads of the module will
1592 * not be listed in, or controllable by, bonding_masters, but
1593 * will have the usual "bonding" sysfs directory.
1594 *
1595 * This is done to preserve backwards compatibility for
1596 * initscripts/sysconfig, which load bonding multiple times to
1597 * configure multiple bonding devices.
1598 */
1599 if (ret == -EEXIST) {
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001600 /* Is someone being kinky and naming a device bonding_master? */
1601 if (__dev_get_by_name(&init_net,
1602 class_attr_bonding_masters.attr.name))
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00001603 pr_err("network device named %s already "
1604 "exists in sysfs",
Stephen Hemminger38d2f382008-05-14 22:35:04 -07001605 class_attr_bonding_masters.attr.name);
Stephen Hemminger130aa612009-06-11 05:46:04 -07001606 ret = 0;
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001607 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001608
1609 return ret;
1610
1611}
1612
1613/*
1614 * Remove /sys/class/net/bonding_masters.
1615 */
1616void bond_destroy_sysfs(void)
1617{
Jay Vosburghb8a97872008-06-13 18:12:04 -07001618 netdev_class_remove_file(&class_attr_bonding_masters);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001619}
1620
1621/*
1622 * Initialize sysfs for each bond. This sets up and registers
1623 * the 'bondctl' directory for each individual bond under /sys/class/net.
1624 */
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001625void bond_prepare_sysfs_group(struct bonding *bond)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001626{
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00001627 bond->dev->sysfs_groups[0] = &bonding_group;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001628}
1629