Commit | Line | Data |
---|---|---|
b76cdba9 MW |
1 | |
2 | /* | |
3 | * Copyright(c) 2004-2005 Intel Corporation. All rights reserved. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the | |
7 | * Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
12 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
13 | * for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
18 | * | |
19 | * The full GNU General Public License is included in this distribution in the | |
20 | * file called LICENSE. | |
21 | * | |
b76cdba9 | 22 | */ |
b76cdba9 MW |
23 | #include <linux/kernel.h> |
24 | #include <linux/module.h> | |
b76cdba9 MW |
25 | #include <linux/device.h> |
26 | #include <linux/sysdev.h> | |
27 | #include <linux/fs.h> | |
28 | #include <linux/types.h> | |
29 | #include <linux/string.h> | |
30 | #include <linux/netdevice.h> | |
31 | #include <linux/inetdevice.h> | |
32 | #include <linux/in.h> | |
33 | #include <linux/sysfs.h> | |
b76cdba9 MW |
34 | #include <linux/ctype.h> |
35 | #include <linux/inet.h> | |
36 | #include <linux/rtnetlink.h> | |
881d966b | 37 | #include <net/net_namespace.h> |
b76cdba9 MW |
38 | |
39 | /* #define BONDING_DEBUG 1 */ | |
40 | #include "bonding.h" | |
43cb76d9 | 41 | #define to_dev(obj) container_of(obj,struct device,kobj) |
b76cdba9 MW |
42 | #define to_bond(cd) ((struct bonding *)(to_net_dev(cd)->priv)) |
43 | ||
44 | /*---------------------------- Declarations -------------------------------*/ | |
45 | ||
46 | ||
47 | extern struct list_head bond_dev_list; | |
48 | extern struct bond_params bonding_defaults; | |
49 | extern struct bond_parm_tbl bond_mode_tbl[]; | |
50 | extern struct bond_parm_tbl bond_lacp_tbl[]; | |
51 | extern struct bond_parm_tbl xmit_hashtype_tbl[]; | |
f5b2b966 | 52 | extern struct bond_parm_tbl arp_validate_tbl[]; |
b76cdba9 MW |
53 | |
54 | static int expected_refcount = -1; | |
55 | static struct class *netdev_class; | |
56 | /*--------------------------- Data Structures -----------------------------*/ | |
57 | ||
3a4fa0a2 | 58 | /* Bonding sysfs lock. Why can't we just use the subsystem lock? |
b76cdba9 MW |
59 | * Because kobject_register tries to acquire the subsystem lock. If |
60 | * we already hold the lock (which we would if the user was creating | |
61 | * a new bond through the sysfs interface), we deadlock. | |
62 | * This lock is only needed when deleting a bond - we need to make sure | |
63 | * that we don't collide with an ongoing ioctl. | |
64 | */ | |
65 | ||
66 | struct rw_semaphore bonding_rwsem; | |
67 | ||
68 | ||
69 | ||
70 | ||
71 | /*------------------------------ Functions --------------------------------*/ | |
72 | ||
73 | /* | |
74 | * "show" function for the bond_masters attribute. | |
75 | * The class parameter is ignored. | |
76 | */ | |
77 | static ssize_t bonding_show_bonds(struct class *cls, char *buffer) | |
78 | { | |
79 | int res = 0; | |
80 | struct bonding *bond; | |
81 | ||
82 | down_read(&(bonding_rwsem)); | |
83 | ||
84 | list_for_each_entry(bond, &bond_dev_list, bond_list) { | |
85 | if (res > (PAGE_SIZE - IFNAMSIZ)) { | |
86 | /* not enough space for another interface name */ | |
87 | if ((PAGE_SIZE - res) > 10) | |
88 | res = PAGE_SIZE - 10; | |
89 | res += sprintf(buffer + res, "++more++"); | |
90 | break; | |
91 | } | |
92 | res += sprintf(buffer + res, "%s ", | |
93 | bond->dev->name); | |
94 | } | |
95 | res += sprintf(buffer + res, "\n"); | |
96 | res++; | |
97 | up_read(&(bonding_rwsem)); | |
98 | return res; | |
99 | } | |
100 | ||
101 | /* | |
102 | * "store" function for the bond_masters attribute. This is what | |
103 | * creates and deletes entire bonds. | |
104 | * | |
105 | * The class parameter is ignored. | |
106 | * | |
107 | */ | |
108 | ||
109 | static ssize_t bonding_store_bonds(struct class *cls, const char *buffer, size_t count) | |
110 | { | |
111 | char command[IFNAMSIZ + 1] = {0, }; | |
112 | char *ifname; | |
113 | int res = count; | |
114 | struct bonding *bond; | |
115 | struct bonding *nxt; | |
116 | ||
117 | down_write(&(bonding_rwsem)); | |
118 | sscanf(buffer, "%16s", command); /* IFNAMSIZ*/ | |
119 | ifname = command + 1; | |
120 | if ((strlen(command) <= 1) || | |
121 | !dev_valid_name(ifname)) | |
122 | goto err_no_cmd; | |
123 | ||
124 | if (command[0] == '+') { | |
125 | ||
126 | /* Check to see if the bond already exists. */ | |
127 | list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) | |
128 | if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) { | |
129 | printk(KERN_ERR DRV_NAME | |
130 | ": cannot add bond %s; it already exists\n", | |
131 | ifname); | |
132 | res = -EPERM; | |
133 | goto out; | |
134 | } | |
135 | ||
136 | printk(KERN_INFO DRV_NAME | |
137 | ": %s is being created...\n", ifname); | |
138 | if (bond_create(ifname, &bonding_defaults, &bond)) { | |
139 | printk(KERN_INFO DRV_NAME | |
140 | ": %s interface already exists. Bond creation failed.\n", | |
141 | ifname); | |
142 | res = -EPERM; | |
143 | } | |
144 | goto out; | |
145 | } | |
146 | ||
147 | if (command[0] == '-') { | |
148 | list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) | |
149 | if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) { | |
150 | rtnl_lock(); | |
151 | /* check the ref count on the bond's kobject. | |
152 | * If it's > expected, then there's a file open, | |
153 | * and we have to fail. | |
154 | */ | |
43cb76d9 | 155 | if (atomic_read(&bond->dev->dev.kobj.kref.refcount) |
b76cdba9 MW |
156 | > expected_refcount){ |
157 | rtnl_unlock(); | |
158 | printk(KERN_INFO DRV_NAME | |
159 | ": Unable remove bond %s due to open references.\n", | |
160 | ifname); | |
161 | res = -EPERM; | |
162 | goto out; | |
163 | } | |
164 | printk(KERN_INFO DRV_NAME | |
165 | ": %s is being deleted...\n", | |
166 | bond->dev->name); | |
d90a162a | 167 | bond_destroy(bond); |
b76cdba9 MW |
168 | rtnl_unlock(); |
169 | goto out; | |
170 | } | |
171 | ||
172 | printk(KERN_ERR DRV_NAME | |
173 | ": unable to delete non-existent bond %s\n", ifname); | |
174 | res = -ENODEV; | |
175 | goto out; | |
176 | } | |
177 | ||
178 | err_no_cmd: | |
179 | printk(KERN_ERR DRV_NAME | |
180 | ": no command found in bonding_masters. Use +ifname or -ifname.\n"); | |
181 | res = -EPERM; | |
182 | ||
183 | /* Always return either count or an error. If you return 0, you'll | |
184 | * get called forever, which is bad. | |
185 | */ | |
186 | out: | |
187 | up_write(&(bonding_rwsem)); | |
188 | return res; | |
189 | } | |
190 | /* class attribute for bond_masters file. This ends up in /sys/class/net */ | |
191 | static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO, | |
192 | bonding_show_bonds, bonding_store_bonds); | |
193 | ||
194 | int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave) | |
195 | { | |
196 | char linkname[IFNAMSIZ+7]; | |
197 | int ret = 0; | |
198 | ||
199 | /* first, create a link from the slave back to the master */ | |
43cb76d9 | 200 | ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj), |
b76cdba9 MW |
201 | "master"); |
202 | if (ret) | |
203 | return ret; | |
204 | /* next, create a link from the master to the slave */ | |
205 | sprintf(linkname,"slave_%s",slave->name); | |
43cb76d9 | 206 | ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj), |
b76cdba9 MW |
207 | linkname); |
208 | return ret; | |
209 | ||
210 | } | |
211 | ||
212 | void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave) | |
213 | { | |
214 | char linkname[IFNAMSIZ+7]; | |
215 | ||
43cb76d9 | 216 | sysfs_remove_link(&(slave->dev.kobj), "master"); |
b76cdba9 | 217 | sprintf(linkname,"slave_%s",slave->name); |
43cb76d9 | 218 | sysfs_remove_link(&(master->dev.kobj), linkname); |
b76cdba9 MW |
219 | } |
220 | ||
221 | ||
222 | /* | |
223 | * Show the slaves in the current bond. | |
224 | */ | |
43cb76d9 GKH |
225 | static ssize_t bonding_show_slaves(struct device *d, |
226 | struct device_attribute *attr, char *buf) | |
b76cdba9 MW |
227 | { |
228 | struct slave *slave; | |
229 | int i, res = 0; | |
43cb76d9 | 230 | struct bonding *bond = to_bond(d); |
b76cdba9 | 231 | |
6603a6f2 | 232 | read_lock(&bond->lock); |
b76cdba9 MW |
233 | bond_for_each_slave(bond, slave, i) { |
234 | if (res > (PAGE_SIZE - IFNAMSIZ)) { | |
235 | /* not enough space for another interface name */ | |
236 | if ((PAGE_SIZE - res) > 10) | |
237 | res = PAGE_SIZE - 10; | |
238 | res += sprintf(buf + res, "++more++"); | |
239 | break; | |
240 | } | |
241 | res += sprintf(buf + res, "%s ", slave->dev->name); | |
242 | } | |
6603a6f2 | 243 | read_unlock(&bond->lock); |
b76cdba9 MW |
244 | res += sprintf(buf + res, "\n"); |
245 | res++; | |
246 | return res; | |
247 | } | |
248 | ||
249 | /* | |
250 | * Set the slaves in the current bond. The bond interface must be | |
251 | * up for this to succeed. | |
252 | * This function is largely the same flow as bonding_update_bonds(). | |
253 | */ | |
43cb76d9 GKH |
254 | static ssize_t bonding_store_slaves(struct device *d, |
255 | struct device_attribute *attr, | |
256 | const char *buffer, size_t count) | |
b76cdba9 MW |
257 | { |
258 | char command[IFNAMSIZ + 1] = { 0, }; | |
259 | char *ifname; | |
260 | int i, res, found, ret = count; | |
3158bf7d | 261 | u32 original_mtu; |
b76cdba9 | 262 | struct slave *slave; |
3418db7c | 263 | struct net_device *dev = NULL; |
43cb76d9 | 264 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
265 | |
266 | /* Quick sanity check -- is the bond interface up? */ | |
267 | if (!(bond->dev->flags & IFF_UP)) { | |
6b1bf096 MS |
268 | printk(KERN_WARNING DRV_NAME |
269 | ": %s: doing slave updates when interface is down.\n", | |
b76cdba9 | 270 | bond->dev->name); |
b76cdba9 MW |
271 | } |
272 | ||
273 | /* Note: We can't hold bond->lock here, as bond_create grabs it. */ | |
274 | ||
275 | sscanf(buffer, "%16s", command); /* IFNAMSIZ*/ | |
276 | ifname = command + 1; | |
277 | if ((strlen(command) <= 1) || | |
278 | !dev_valid_name(ifname)) | |
279 | goto err_no_cmd; | |
280 | ||
281 | if (command[0] == '+') { | |
282 | ||
283 | /* Got a slave name in ifname. Is it already in the list? */ | |
284 | found = 0; | |
6603a6f2 | 285 | read_lock(&bond->lock); |
b76cdba9 MW |
286 | bond_for_each_slave(bond, slave, i) |
287 | if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) { | |
288 | printk(KERN_ERR DRV_NAME | |
289 | ": %s: Interface %s is already enslaved!\n", | |
290 | bond->dev->name, ifname); | |
291 | ret = -EPERM; | |
6603a6f2 | 292 | read_unlock(&bond->lock); |
b76cdba9 MW |
293 | goto out; |
294 | } | |
295 | ||
6603a6f2 | 296 | read_unlock(&bond->lock); |
b76cdba9 MW |
297 | printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n", |
298 | bond->dev->name, ifname); | |
881d966b | 299 | dev = dev_get_by_name(&init_net, ifname); |
b76cdba9 MW |
300 | if (!dev) { |
301 | printk(KERN_INFO DRV_NAME | |
302 | ": %s: Interface %s does not exist!\n", | |
303 | bond->dev->name, ifname); | |
304 | ret = -EPERM; | |
305 | goto out; | |
306 | } | |
307 | else | |
308 | dev_put(dev); | |
309 | ||
310 | if (dev->flags & IFF_UP) { | |
311 | printk(KERN_ERR DRV_NAME | |
312 | ": %s: Error: Unable to enslave %s " | |
313 | "because it is already up.\n", | |
314 | bond->dev->name, dev->name); | |
315 | ret = -EPERM; | |
316 | goto out; | |
317 | } | |
318 | /* If this is the first slave, then we need to set | |
319 | the master's hardware address to be the same as the | |
320 | slave's. */ | |
321 | if (!(*((u32 *) & (bond->dev->dev_addr[0])))) { | |
322 | memcpy(bond->dev->dev_addr, dev->dev_addr, | |
323 | dev->addr_len); | |
324 | } | |
325 | ||
326 | /* Set the slave's MTU to match the bond */ | |
3158bf7d | 327 | original_mtu = dev->mtu; |
b76cdba9 MW |
328 | if (dev->mtu != bond->dev->mtu) { |
329 | if (dev->change_mtu) { | |
330 | res = dev->change_mtu(dev, | |
331 | bond->dev->mtu); | |
332 | if (res) { | |
333 | ret = res; | |
334 | goto out; | |
335 | } | |
336 | } else { | |
337 | dev->mtu = bond->dev->mtu; | |
338 | } | |
339 | } | |
340 | rtnl_lock(); | |
341 | res = bond_enslave(bond->dev, dev); | |
3158bf7d MS |
342 | bond_for_each_slave(bond, slave, i) |
343 | if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) | |
344 | slave->original_mtu = original_mtu; | |
b76cdba9 MW |
345 | rtnl_unlock(); |
346 | if (res) { | |
347 | ret = res; | |
348 | } | |
349 | goto out; | |
350 | } | |
351 | ||
352 | if (command[0] == '-') { | |
353 | dev = NULL; | |
354 | bond_for_each_slave(bond, slave, i) | |
355 | if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) { | |
356 | dev = slave->dev; | |
3158bf7d | 357 | original_mtu = slave->original_mtu; |
b76cdba9 MW |
358 | break; |
359 | } | |
360 | if (dev) { | |
361 | printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n", | |
362 | bond->dev->name, dev->name); | |
363 | rtnl_lock(); | |
d90a162a MS |
364 | if (bond->setup_by_slave) |
365 | res = bond_release_and_destroy(bond->dev, dev); | |
366 | else | |
367 | res = bond_release(bond->dev, dev); | |
b76cdba9 MW |
368 | rtnl_unlock(); |
369 | if (res) { | |
370 | ret = res; | |
371 | goto out; | |
372 | } | |
373 | /* set the slave MTU to the default */ | |
374 | if (dev->change_mtu) { | |
3158bf7d | 375 | dev->change_mtu(dev, original_mtu); |
b76cdba9 | 376 | } else { |
3158bf7d | 377 | dev->mtu = original_mtu; |
b76cdba9 MW |
378 | } |
379 | } | |
380 | else { | |
381 | printk(KERN_ERR DRV_NAME ": unable to remove non-existent slave %s for bond %s.\n", | |
382 | ifname, bond->dev->name); | |
383 | ret = -ENODEV; | |
384 | } | |
385 | goto out; | |
386 | } | |
387 | ||
388 | err_no_cmd: | |
389 | printk(KERN_ERR DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name); | |
390 | ret = -EPERM; | |
391 | ||
392 | out: | |
393 | return ret; | |
394 | } | |
395 | ||
43cb76d9 | 396 | static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves, bonding_store_slaves); |
b76cdba9 MW |
397 | |
398 | /* | |
399 | * Show and set the bonding mode. The bond interface must be down to | |
400 | * change the mode. | |
401 | */ | |
43cb76d9 GKH |
402 | static ssize_t bonding_show_mode(struct device *d, |
403 | struct device_attribute *attr, char *buf) | |
b76cdba9 | 404 | { |
43cb76d9 | 405 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
406 | |
407 | return sprintf(buf, "%s %d\n", | |
408 | bond_mode_tbl[bond->params.mode].modename, | |
409 | bond->params.mode) + 1; | |
410 | } | |
411 | ||
43cb76d9 GKH |
412 | static ssize_t bonding_store_mode(struct device *d, |
413 | struct device_attribute *attr, | |
414 | const char *buf, size_t count) | |
b76cdba9 MW |
415 | { |
416 | int new_value, ret = count; | |
43cb76d9 | 417 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
418 | |
419 | if (bond->dev->flags & IFF_UP) { | |
420 | printk(KERN_ERR DRV_NAME | |
421 | ": unable to update mode of %s because interface is up.\n", | |
422 | bond->dev->name); | |
423 | ret = -EPERM; | |
424 | goto out; | |
425 | } | |
426 | ||
427 | new_value = bond_parse_parm((char *)buf, bond_mode_tbl); | |
428 | if (new_value < 0) { | |
429 | printk(KERN_ERR DRV_NAME | |
430 | ": %s: Ignoring invalid mode value %.*s.\n", | |
431 | bond->dev->name, | |
432 | (int)strlen(buf) - 1, buf); | |
433 | ret = -EINVAL; | |
434 | goto out; | |
435 | } else { | |
8f903c70 JV |
436 | if (bond->params.mode == BOND_MODE_8023AD) |
437 | bond_unset_master_3ad_flags(bond); | |
438 | ||
439 | if (bond->params.mode == BOND_MODE_ALB) | |
440 | bond_unset_master_alb_flags(bond); | |
441 | ||
b76cdba9 MW |
442 | bond->params.mode = new_value; |
443 | bond_set_mode_ops(bond, bond->params.mode); | |
444 | printk(KERN_INFO DRV_NAME ": %s: setting mode to %s (%d).\n", | |
445 | bond->dev->name, bond_mode_tbl[new_value].modename, new_value); | |
446 | } | |
447 | out: | |
448 | return ret; | |
449 | } | |
43cb76d9 | 450 | static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, bonding_show_mode, bonding_store_mode); |
b76cdba9 MW |
451 | |
452 | /* | |
453 | * Show and set the bonding transmit hash method. The bond interface must be down to | |
454 | * change the xmit hash policy. | |
455 | */ | |
43cb76d9 GKH |
456 | static ssize_t bonding_show_xmit_hash(struct device *d, |
457 | struct device_attribute *attr, | |
458 | char *buf) | |
b76cdba9 MW |
459 | { |
460 | int count; | |
43cb76d9 | 461 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
462 | |
463 | if ((bond->params.mode != BOND_MODE_XOR) && | |
464 | (bond->params.mode != BOND_MODE_8023AD)) { | |
465 | // Not Applicable | |
466 | count = sprintf(buf, "NA\n") + 1; | |
467 | } else { | |
468 | count = sprintf(buf, "%s %d\n", | |
469 | xmit_hashtype_tbl[bond->params.xmit_policy].modename, | |
470 | bond->params.xmit_policy) + 1; | |
471 | } | |
472 | ||
473 | return count; | |
474 | } | |
475 | ||
43cb76d9 GKH |
476 | static ssize_t bonding_store_xmit_hash(struct device *d, |
477 | struct device_attribute *attr, | |
478 | const char *buf, size_t count) | |
b76cdba9 MW |
479 | { |
480 | int new_value, ret = count; | |
43cb76d9 | 481 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
482 | |
483 | if (bond->dev->flags & IFF_UP) { | |
484 | printk(KERN_ERR DRV_NAME | |
485 | "%s: Interface is up. Unable to update xmit policy.\n", | |
486 | bond->dev->name); | |
487 | ret = -EPERM; | |
488 | goto out; | |
489 | } | |
490 | ||
491 | if ((bond->params.mode != BOND_MODE_XOR) && | |
492 | (bond->params.mode != BOND_MODE_8023AD)) { | |
493 | printk(KERN_ERR DRV_NAME | |
494 | "%s: Transmit hash policy is irrelevant in this mode.\n", | |
495 | bond->dev->name); | |
496 | ret = -EPERM; | |
497 | goto out; | |
498 | } | |
499 | ||
500 | new_value = bond_parse_parm((char *)buf, xmit_hashtype_tbl); | |
501 | if (new_value < 0) { | |
502 | printk(KERN_ERR DRV_NAME | |
503 | ": %s: Ignoring invalid xmit hash policy value %.*s.\n", | |
504 | bond->dev->name, | |
505 | (int)strlen(buf) - 1, buf); | |
506 | ret = -EINVAL; | |
507 | goto out; | |
508 | } else { | |
509 | bond->params.xmit_policy = new_value; | |
510 | bond_set_mode_ops(bond, bond->params.mode); | |
511 | printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n", | |
512 | bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value); | |
513 | } | |
514 | out: | |
515 | return ret; | |
516 | } | |
43cb76d9 | 517 | static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash); |
b76cdba9 | 518 | |
f5b2b966 JV |
519 | /* |
520 | * Show and set arp_validate. | |
521 | */ | |
43cb76d9 GKH |
522 | static ssize_t bonding_show_arp_validate(struct device *d, |
523 | struct device_attribute *attr, | |
524 | char *buf) | |
f5b2b966 | 525 | { |
43cb76d9 | 526 | struct bonding *bond = to_bond(d); |
f5b2b966 JV |
527 | |
528 | return sprintf(buf, "%s %d\n", | |
529 | arp_validate_tbl[bond->params.arp_validate].modename, | |
530 | bond->params.arp_validate) + 1; | |
531 | } | |
532 | ||
43cb76d9 GKH |
533 | static ssize_t bonding_store_arp_validate(struct device *d, |
534 | struct device_attribute *attr, | |
535 | const char *buf, size_t count) | |
f5b2b966 JV |
536 | { |
537 | int new_value; | |
43cb76d9 | 538 | struct bonding *bond = to_bond(d); |
f5b2b966 JV |
539 | |
540 | new_value = bond_parse_parm((char *)buf, arp_validate_tbl); | |
541 | if (new_value < 0) { | |
542 | printk(KERN_ERR DRV_NAME | |
543 | ": %s: Ignoring invalid arp_validate value %s\n", | |
544 | bond->dev->name, buf); | |
545 | return -EINVAL; | |
546 | } | |
547 | if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) { | |
548 | printk(KERN_ERR DRV_NAME | |
549 | ": %s: arp_validate only supported in active-backup mode.\n", | |
550 | bond->dev->name); | |
551 | return -EINVAL; | |
552 | } | |
553 | printk(KERN_INFO DRV_NAME ": %s: setting arp_validate to %s (%d).\n", | |
554 | bond->dev->name, arp_validate_tbl[new_value].modename, | |
555 | new_value); | |
556 | ||
557 | if (!bond->params.arp_validate && new_value) { | |
558 | bond_register_arp(bond); | |
559 | } else if (bond->params.arp_validate && !new_value) { | |
560 | bond_unregister_arp(bond); | |
561 | } | |
562 | ||
563 | bond->params.arp_validate = new_value; | |
564 | ||
565 | return count; | |
566 | } | |
567 | ||
43cb76d9 | 568 | static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate, bonding_store_arp_validate); |
f5b2b966 | 569 | |
dd957c57 JV |
570 | /* |
571 | * Show and store fail_over_mac. User only allowed to change the | |
572 | * value when there are no slaves. | |
573 | */ | |
574 | static ssize_t bonding_show_fail_over_mac(struct device *d, struct device_attribute *attr, char *buf) | |
575 | { | |
576 | struct bonding *bond = to_bond(d); | |
577 | ||
578 | return sprintf(buf, "%d\n", bond->params.fail_over_mac) + 1; | |
579 | } | |
580 | ||
581 | static ssize_t bonding_store_fail_over_mac(struct device *d, struct device_attribute *attr, const char *buf, size_t count) | |
582 | { | |
583 | int new_value; | |
584 | int ret = count; | |
585 | struct bonding *bond = to_bond(d); | |
586 | ||
587 | if (bond->slave_cnt != 0) { | |
588 | printk(KERN_ERR DRV_NAME | |
589 | ": %s: Can't alter fail_over_mac with slaves in bond.\n", | |
590 | bond->dev->name); | |
591 | ret = -EPERM; | |
592 | goto out; | |
593 | } | |
594 | ||
595 | if (sscanf(buf, "%d", &new_value) != 1) { | |
596 | printk(KERN_ERR DRV_NAME | |
597 | ": %s: no fail_over_mac value specified.\n", | |
598 | bond->dev->name); | |
599 | ret = -EINVAL; | |
600 | goto out; | |
601 | } | |
602 | ||
603 | if ((new_value == 0) || (new_value == 1)) { | |
604 | bond->params.fail_over_mac = new_value; | |
605 | printk(KERN_INFO DRV_NAME ": %s: Setting fail_over_mac to %d.\n", | |
606 | bond->dev->name, new_value); | |
607 | } else { | |
608 | printk(KERN_INFO DRV_NAME | |
609 | ": %s: Ignoring invalid fail_over_mac value %d.\n", | |
610 | bond->dev->name, new_value); | |
611 | } | |
612 | out: | |
613 | return ret; | |
614 | } | |
615 | ||
616 | static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR, bonding_show_fail_over_mac, bonding_store_fail_over_mac); | |
617 | ||
b76cdba9 MW |
618 | /* |
619 | * Show and set the arp timer interval. There are two tricky bits | |
620 | * here. First, if ARP monitoring is activated, then we must disable | |
621 | * MII monitoring. Second, if the ARP timer isn't running, we must | |
622 | * start it. | |
623 | */ | |
43cb76d9 GKH |
624 | static ssize_t bonding_show_arp_interval(struct device *d, |
625 | struct device_attribute *attr, | |
626 | char *buf) | |
b76cdba9 | 627 | { |
43cb76d9 | 628 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
629 | |
630 | return sprintf(buf, "%d\n", bond->params.arp_interval) + 1; | |
631 | } | |
632 | ||
43cb76d9 GKH |
633 | static ssize_t bonding_store_arp_interval(struct device *d, |
634 | struct device_attribute *attr, | |
635 | const char *buf, size_t count) | |
b76cdba9 MW |
636 | { |
637 | int new_value, ret = count; | |
43cb76d9 | 638 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
639 | |
640 | if (sscanf(buf, "%d", &new_value) != 1) { | |
641 | printk(KERN_ERR DRV_NAME | |
642 | ": %s: no arp_interval value specified.\n", | |
643 | bond->dev->name); | |
644 | ret = -EINVAL; | |
645 | goto out; | |
646 | } | |
647 | if (new_value < 0) { | |
648 | printk(KERN_ERR DRV_NAME | |
649 | ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n", | |
650 | bond->dev->name, new_value, INT_MAX); | |
651 | ret = -EINVAL; | |
652 | goto out; | |
653 | } | |
654 | ||
655 | printk(KERN_INFO DRV_NAME | |
656 | ": %s: Setting ARP monitoring interval to %d.\n", | |
657 | bond->dev->name, new_value); | |
658 | bond->params.arp_interval = new_value; | |
659 | if (bond->params.miimon) { | |
660 | printk(KERN_INFO DRV_NAME | |
661 | ": %s: ARP monitoring cannot be used with MII monitoring. " | |
662 | "%s Disabling MII monitoring.\n", | |
663 | bond->dev->name, bond->dev->name); | |
664 | bond->params.miimon = 0; | |
1b76b316 JV |
665 | if (delayed_work_pending(&bond->mii_work)) { |
666 | cancel_delayed_work(&bond->mii_work); | |
667 | flush_workqueue(bond->wq); | |
b76cdba9 MW |
668 | } |
669 | } | |
670 | if (!bond->params.arp_targets[0]) { | |
671 | printk(KERN_INFO DRV_NAME | |
672 | ": %s: ARP monitoring has been set up, " | |
673 | "but no ARP targets have been specified.\n", | |
674 | bond->dev->name); | |
675 | } | |
676 | if (bond->dev->flags & IFF_UP) { | |
677 | /* If the interface is up, we may need to fire off | |
678 | * the ARP timer. If the interface is down, the | |
679 | * timer will get fired off when the open function | |
680 | * is called. | |
681 | */ | |
1b76b316 JV |
682 | if (!delayed_work_pending(&bond->arp_work)) { |
683 | if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) | |
684 | INIT_DELAYED_WORK(&bond->arp_work, | |
685 | bond_activebackup_arp_mon); | |
686 | else | |
687 | INIT_DELAYED_WORK(&bond->arp_work, | |
688 | bond_loadbalance_arp_mon); | |
689 | ||
690 | queue_delayed_work(bond->wq, &bond->arp_work, 0); | |
b76cdba9 MW |
691 | } |
692 | } | |
693 | ||
694 | out: | |
695 | return ret; | |
696 | } | |
43cb76d9 | 697 | static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval); |
b76cdba9 MW |
698 | |
699 | /* | |
700 | * Show and set the arp targets. | |
701 | */ | |
43cb76d9 GKH |
702 | static ssize_t bonding_show_arp_targets(struct device *d, |
703 | struct device_attribute *attr, | |
704 | char *buf) | |
b76cdba9 MW |
705 | { |
706 | int i, res = 0; | |
43cb76d9 | 707 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
708 | |
709 | for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) { | |
710 | if (bond->params.arp_targets[i]) | |
711 | res += sprintf(buf + res, "%u.%u.%u.%u ", | |
712 | NIPQUAD(bond->params.arp_targets[i])); | |
713 | } | |
714 | if (res) | |
715 | res--; /* eat the leftover space */ | |
716 | res += sprintf(buf + res, "\n"); | |
717 | res++; | |
718 | return res; | |
719 | } | |
720 | ||
43cb76d9 GKH |
721 | static ssize_t bonding_store_arp_targets(struct device *d, |
722 | struct device_attribute *attr, | |
723 | const char *buf, size_t count) | |
b76cdba9 | 724 | { |
d3bb52b0 | 725 | __be32 newtarget; |
b76cdba9 | 726 | int i = 0, done = 0, ret = count; |
43cb76d9 | 727 | struct bonding *bond = to_bond(d); |
d3bb52b0 | 728 | __be32 *targets; |
b76cdba9 MW |
729 | |
730 | targets = bond->params.arp_targets; | |
731 | newtarget = in_aton(buf + 1); | |
732 | /* look for adds */ | |
733 | if (buf[0] == '+') { | |
d3bb52b0 | 734 | if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) { |
b76cdba9 MW |
735 | printk(KERN_ERR DRV_NAME |
736 | ": %s: invalid ARP target %u.%u.%u.%u specified for addition\n", | |
737 | bond->dev->name, NIPQUAD(newtarget)); | |
738 | ret = -EINVAL; | |
739 | goto out; | |
740 | } | |
741 | /* look for an empty slot to put the target in, and check for dupes */ | |
742 | for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) { | |
743 | if (targets[i] == newtarget) { /* duplicate */ | |
744 | printk(KERN_ERR DRV_NAME | |
745 | ": %s: ARP target %u.%u.%u.%u is already present\n", | |
746 | bond->dev->name, NIPQUAD(newtarget)); | |
747 | if (done) | |
748 | targets[i] = 0; | |
749 | ret = -EINVAL; | |
750 | goto out; | |
751 | } | |
752 | if (targets[i] == 0 && !done) { | |
753 | printk(KERN_INFO DRV_NAME | |
754 | ": %s: adding ARP target %d.%d.%d.%d.\n", | |
755 | bond->dev->name, NIPQUAD(newtarget)); | |
756 | done = 1; | |
757 | targets[i] = newtarget; | |
758 | } | |
759 | } | |
760 | if (!done) { | |
761 | printk(KERN_ERR DRV_NAME | |
762 | ": %s: ARP target table is full!\n", | |
763 | bond->dev->name); | |
764 | ret = -EINVAL; | |
765 | goto out; | |
766 | } | |
767 | ||
768 | } | |
769 | else if (buf[0] == '-') { | |
d3bb52b0 | 770 | if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) { |
b76cdba9 MW |
771 | printk(KERN_ERR DRV_NAME |
772 | ": %s: invalid ARP target %d.%d.%d.%d specified for removal\n", | |
773 | bond->dev->name, NIPQUAD(newtarget)); | |
774 | ret = -EINVAL; | |
775 | goto out; | |
776 | } | |
777 | ||
778 | for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) { | |
779 | if (targets[i] == newtarget) { | |
780 | printk(KERN_INFO DRV_NAME | |
781 | ": %s: removing ARP target %d.%d.%d.%d.\n", | |
782 | bond->dev->name, NIPQUAD(newtarget)); | |
783 | targets[i] = 0; | |
784 | done = 1; | |
785 | } | |
786 | } | |
787 | if (!done) { | |
788 | printk(KERN_INFO DRV_NAME | |
789 | ": %s: unable to remove nonexistent ARP target %d.%d.%d.%d.\n", | |
790 | bond->dev->name, NIPQUAD(newtarget)); | |
791 | ret = -EINVAL; | |
792 | goto out; | |
793 | } | |
794 | } | |
795 | else { | |
796 | printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n", | |
797 | bond->dev->name); | |
798 | ret = -EPERM; | |
799 | goto out; | |
800 | } | |
801 | ||
802 | out: | |
803 | return ret; | |
804 | } | |
43cb76d9 | 805 | static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets); |
b76cdba9 MW |
806 | |
807 | /* | |
808 | * Show and set the up and down delays. These must be multiples of the | |
809 | * MII monitoring value, and are stored internally as the multiplier. | |
810 | * Thus, we must translate to MS for the real world. | |
811 | */ | |
43cb76d9 GKH |
812 | static ssize_t bonding_show_downdelay(struct device *d, |
813 | struct device_attribute *attr, | |
814 | char *buf) | |
b76cdba9 | 815 | { |
43cb76d9 | 816 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
817 | |
818 | return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon) + 1; | |
819 | } | |
820 | ||
43cb76d9 GKH |
821 | static ssize_t bonding_store_downdelay(struct device *d, |
822 | struct device_attribute *attr, | |
823 | const char *buf, size_t count) | |
b76cdba9 MW |
824 | { |
825 | int new_value, ret = count; | |
43cb76d9 | 826 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
827 | |
828 | if (!(bond->params.miimon)) { | |
829 | printk(KERN_ERR DRV_NAME | |
830 | ": %s: Unable to set down delay as MII monitoring is disabled\n", | |
831 | bond->dev->name); | |
832 | ret = -EPERM; | |
833 | goto out; | |
834 | } | |
835 | ||
836 | if (sscanf(buf, "%d", &new_value) != 1) { | |
837 | printk(KERN_ERR DRV_NAME | |
838 | ": %s: no down delay value specified.\n", | |
839 | bond->dev->name); | |
840 | ret = -EINVAL; | |
841 | goto out; | |
842 | } | |
843 | if (new_value < 0) { | |
844 | printk(KERN_ERR DRV_NAME | |
845 | ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n", | |
846 | bond->dev->name, new_value, 1, INT_MAX); | |
847 | ret = -EINVAL; | |
848 | goto out; | |
849 | } else { | |
850 | if ((new_value % bond->params.miimon) != 0) { | |
851 | printk(KERN_WARNING DRV_NAME | |
852 | ": %s: Warning: down delay (%d) is not a multiple " | |
853 | "of miimon (%d), delay rounded to %d ms\n", | |
854 | bond->dev->name, new_value, bond->params.miimon, | |
855 | (new_value / bond->params.miimon) * | |
856 | bond->params.miimon); | |
857 | } | |
858 | bond->params.downdelay = new_value / bond->params.miimon; | |
859 | printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n", | |
860 | bond->dev->name, bond->params.downdelay * bond->params.miimon); | |
861 | ||
862 | } | |
863 | ||
864 | out: | |
865 | return ret; | |
866 | } | |
43cb76d9 | 867 | static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay); |
b76cdba9 | 868 | |
43cb76d9 GKH |
869 | static ssize_t bonding_show_updelay(struct device *d, |
870 | struct device_attribute *attr, | |
871 | char *buf) | |
b76cdba9 | 872 | { |
43cb76d9 | 873 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
874 | |
875 | return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon) + 1; | |
876 | ||
877 | } | |
878 | ||
43cb76d9 GKH |
879 | static ssize_t bonding_store_updelay(struct device *d, |
880 | struct device_attribute *attr, | |
881 | const char *buf, size_t count) | |
b76cdba9 MW |
882 | { |
883 | int new_value, ret = count; | |
43cb76d9 | 884 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
885 | |
886 | if (!(bond->params.miimon)) { | |
887 | printk(KERN_ERR DRV_NAME | |
888 | ": %s: Unable to set up delay as MII monitoring is disabled\n", | |
889 | bond->dev->name); | |
890 | ret = -EPERM; | |
891 | goto out; | |
892 | } | |
893 | ||
894 | if (sscanf(buf, "%d", &new_value) != 1) { | |
895 | printk(KERN_ERR DRV_NAME | |
896 | ": %s: no up delay value specified.\n", | |
897 | bond->dev->name); | |
898 | ret = -EINVAL; | |
899 | goto out; | |
900 | } | |
901 | if (new_value < 0) { | |
902 | printk(KERN_ERR DRV_NAME | |
903 | ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n", | |
904 | bond->dev->name, new_value, 1, INT_MAX); | |
905 | ret = -EINVAL; | |
906 | goto out; | |
907 | } else { | |
908 | if ((new_value % bond->params.miimon) != 0) { | |
909 | printk(KERN_WARNING DRV_NAME | |
910 | ": %s: Warning: up delay (%d) is not a multiple " | |
911 | "of miimon (%d), updelay rounded to %d ms\n", | |
912 | bond->dev->name, new_value, bond->params.miimon, | |
913 | (new_value / bond->params.miimon) * | |
914 | bond->params.miimon); | |
915 | } | |
916 | bond->params.updelay = new_value / bond->params.miimon; | |
917 | printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n", | |
918 | bond->dev->name, bond->params.updelay * bond->params.miimon); | |
919 | ||
920 | } | |
921 | ||
922 | out: | |
923 | return ret; | |
924 | } | |
43cb76d9 | 925 | static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay); |
b76cdba9 MW |
926 | |
927 | /* | |
928 | * Show and set the LACP interval. Interface must be down, and the mode | |
929 | * must be set to 802.3ad mode. | |
930 | */ | |
43cb76d9 GKH |
931 | static ssize_t bonding_show_lacp(struct device *d, |
932 | struct device_attribute *attr, | |
933 | char *buf) | |
b76cdba9 | 934 | { |
43cb76d9 | 935 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
936 | |
937 | return sprintf(buf, "%s %d\n", | |
938 | bond_lacp_tbl[bond->params.lacp_fast].modename, | |
939 | bond->params.lacp_fast) + 1; | |
940 | } | |
941 | ||
43cb76d9 GKH |
942 | static ssize_t bonding_store_lacp(struct device *d, |
943 | struct device_attribute *attr, | |
944 | const char *buf, size_t count) | |
b76cdba9 MW |
945 | { |
946 | int new_value, ret = count; | |
43cb76d9 | 947 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
948 | |
949 | if (bond->dev->flags & IFF_UP) { | |
950 | printk(KERN_ERR DRV_NAME | |
951 | ": %s: Unable to update LACP rate because interface is up.\n", | |
952 | bond->dev->name); | |
953 | ret = -EPERM; | |
954 | goto out; | |
955 | } | |
956 | ||
957 | if (bond->params.mode != BOND_MODE_8023AD) { | |
958 | printk(KERN_ERR DRV_NAME | |
959 | ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n", | |
960 | bond->dev->name); | |
961 | ret = -EPERM; | |
962 | goto out; | |
963 | } | |
964 | ||
965 | new_value = bond_parse_parm((char *)buf, bond_lacp_tbl); | |
966 | ||
967 | if ((new_value == 1) || (new_value == 0)) { | |
968 | bond->params.lacp_fast = new_value; | |
969 | printk(KERN_INFO DRV_NAME | |
970 | ": %s: Setting LACP rate to %s (%d).\n", | |
971 | bond->dev->name, bond_lacp_tbl[new_value].modename, new_value); | |
972 | } else { | |
973 | printk(KERN_ERR DRV_NAME | |
974 | ": %s: Ignoring invalid LACP rate value %.*s.\n", | |
975 | bond->dev->name, (int)strlen(buf) - 1, buf); | |
976 | ret = -EINVAL; | |
977 | } | |
978 | out: | |
979 | return ret; | |
980 | } | |
43cb76d9 | 981 | static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp); |
b76cdba9 MW |
982 | |
983 | /* | |
984 | * Show and set the MII monitor interval. There are two tricky bits | |
985 | * here. First, if MII monitoring is activated, then we must disable | |
986 | * ARP monitoring. Second, if the timer isn't running, we must | |
987 | * start it. | |
988 | */ | |
43cb76d9 GKH |
989 | static ssize_t bonding_show_miimon(struct device *d, |
990 | struct device_attribute *attr, | |
991 | char *buf) | |
b76cdba9 | 992 | { |
43cb76d9 | 993 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
994 | |
995 | return sprintf(buf, "%d\n", bond->params.miimon) + 1; | |
996 | } | |
997 | ||
43cb76d9 GKH |
998 | static ssize_t bonding_store_miimon(struct device *d, |
999 | struct device_attribute *attr, | |
1000 | const char *buf, size_t count) | |
b76cdba9 MW |
1001 | { |
1002 | int new_value, ret = count; | |
43cb76d9 | 1003 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
1004 | |
1005 | if (sscanf(buf, "%d", &new_value) != 1) { | |
1006 | printk(KERN_ERR DRV_NAME | |
1007 | ": %s: no miimon value specified.\n", | |
1008 | bond->dev->name); | |
1009 | ret = -EINVAL; | |
1010 | goto out; | |
1011 | } | |
1012 | if (new_value < 0) { | |
1013 | printk(KERN_ERR DRV_NAME | |
1014 | ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n", | |
1015 | bond->dev->name, new_value, 1, INT_MAX); | |
1016 | ret = -EINVAL; | |
1017 | goto out; | |
1018 | } else { | |
1019 | printk(KERN_INFO DRV_NAME | |
1020 | ": %s: Setting MII monitoring interval to %d.\n", | |
1021 | bond->dev->name, new_value); | |
1022 | bond->params.miimon = new_value; | |
1023 | if(bond->params.updelay) | |
1024 | printk(KERN_INFO DRV_NAME | |
1025 | ": %s: Note: Updating updelay (to %d) " | |
1026 | "since it is a multiple of the miimon value.\n", | |
1027 | bond->dev->name, | |
1028 | bond->params.updelay * bond->params.miimon); | |
1029 | if(bond->params.downdelay) | |
1030 | printk(KERN_INFO DRV_NAME | |
1031 | ": %s: Note: Updating downdelay (to %d) " | |
1032 | "since it is a multiple of the miimon value.\n", | |
1033 | bond->dev->name, | |
1034 | bond->params.downdelay * bond->params.miimon); | |
1035 | if (bond->params.arp_interval) { | |
1036 | printk(KERN_INFO DRV_NAME | |
1037 | ": %s: MII monitoring cannot be used with " | |
1038 | "ARP monitoring. Disabling ARP monitoring...\n", | |
1039 | bond->dev->name); | |
1040 | bond->params.arp_interval = 0; | |
f5b2b966 JV |
1041 | if (bond->params.arp_validate) { |
1042 | bond_unregister_arp(bond); | |
1043 | bond->params.arp_validate = | |
1044 | BOND_ARP_VALIDATE_NONE; | |
1045 | } | |
1b76b316 JV |
1046 | if (delayed_work_pending(&bond->arp_work)) { |
1047 | cancel_delayed_work(&bond->arp_work); | |
1048 | flush_workqueue(bond->wq); | |
b76cdba9 MW |
1049 | } |
1050 | } | |
1051 | ||
1052 | if (bond->dev->flags & IFF_UP) { | |
1053 | /* If the interface is up, we may need to fire off | |
1054 | * the MII timer. If the interface is down, the | |
1055 | * timer will get fired off when the open function | |
1056 | * is called. | |
1057 | */ | |
1b76b316 JV |
1058 | if (!delayed_work_pending(&bond->mii_work)) { |
1059 | INIT_DELAYED_WORK(&bond->mii_work, | |
1060 | bond_mii_monitor); | |
1061 | queue_delayed_work(bond->wq, | |
1062 | &bond->mii_work, 0); | |
b76cdba9 MW |
1063 | } |
1064 | } | |
1065 | } | |
1066 | out: | |
1067 | return ret; | |
1068 | } | |
43cb76d9 | 1069 | static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon); |
b76cdba9 MW |
1070 | |
1071 | /* | |
1072 | * Show and set the primary slave. The store function is much | |
1073 | * simpler than bonding_store_slaves function because it only needs to | |
1074 | * handle one interface name. | |
1075 | * The bond must be a mode that supports a primary for this be | |
1076 | * set. | |
1077 | */ | |
43cb76d9 GKH |
1078 | static ssize_t bonding_show_primary(struct device *d, |
1079 | struct device_attribute *attr, | |
1080 | char *buf) | |
b76cdba9 MW |
1081 | { |
1082 | int count = 0; | |
43cb76d9 | 1083 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
1084 | |
1085 | if (bond->primary_slave) | |
1086 | count = sprintf(buf, "%s\n", bond->primary_slave->dev->name) + 1; | |
1087 | else | |
1088 | count = sprintf(buf, "\n") + 1; | |
1089 | ||
1090 | return count; | |
1091 | } | |
1092 | ||
43cb76d9 GKH |
1093 | static ssize_t bonding_store_primary(struct device *d, |
1094 | struct device_attribute *attr, | |
1095 | const char *buf, size_t count) | |
b76cdba9 MW |
1096 | { |
1097 | int i; | |
1098 | struct slave *slave; | |
43cb76d9 | 1099 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
1100 | |
1101 | write_lock_bh(&bond->lock); | |
1102 | if (!USES_PRIMARY(bond->params.mode)) { | |
1103 | printk(KERN_INFO DRV_NAME | |
1104 | ": %s: Unable to set primary slave; %s is in mode %d\n", | |
1105 | bond->dev->name, bond->dev->name, bond->params.mode); | |
1106 | } else { | |
1107 | bond_for_each_slave(bond, slave, i) { | |
1108 | if (strnicmp | |
1109 | (slave->dev->name, buf, | |
1110 | strlen(slave->dev->name)) == 0) { | |
1111 | printk(KERN_INFO DRV_NAME | |
1112 | ": %s: Setting %s as primary slave.\n", | |
1113 | bond->dev->name, slave->dev->name); | |
1114 | bond->primary_slave = slave; | |
1115 | bond_select_active_slave(bond); | |
1116 | goto out; | |
1117 | } | |
1118 | } | |
1119 | ||
1120 | /* if we got here, then we didn't match the name of any slave */ | |
1121 | ||
1122 | if (strlen(buf) == 0 || buf[0] == '\n') { | |
1123 | printk(KERN_INFO DRV_NAME | |
1124 | ": %s: Setting primary slave to None.\n", | |
1125 | bond->dev->name); | |
3418db7c | 1126 | bond->primary_slave = NULL; |
b76cdba9 MW |
1127 | bond_select_active_slave(bond); |
1128 | } else { | |
1129 | printk(KERN_INFO DRV_NAME | |
1130 | ": %s: Unable to set %.*s as primary slave as it is not a slave.\n", | |
1131 | bond->dev->name, (int)strlen(buf) - 1, buf); | |
1132 | } | |
1133 | } | |
1134 | out: | |
1135 | write_unlock_bh(&bond->lock); | |
6603a6f2 JV |
1136 | |
1137 | rtnl_unlock(); | |
1138 | ||
b76cdba9 MW |
1139 | return count; |
1140 | } | |
43cb76d9 | 1141 | static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary); |
b76cdba9 MW |
1142 | |
1143 | /* | |
1144 | * Show and set the use_carrier flag. | |
1145 | */ | |
43cb76d9 GKH |
1146 | static ssize_t bonding_show_carrier(struct device *d, |
1147 | struct device_attribute *attr, | |
1148 | char *buf) | |
b76cdba9 | 1149 | { |
43cb76d9 | 1150 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
1151 | |
1152 | return sprintf(buf, "%d\n", bond->params.use_carrier) + 1; | |
1153 | } | |
1154 | ||
43cb76d9 GKH |
1155 | static ssize_t bonding_store_carrier(struct device *d, |
1156 | struct device_attribute *attr, | |
1157 | const char *buf, size_t count) | |
b76cdba9 MW |
1158 | { |
1159 | int new_value, ret = count; | |
43cb76d9 | 1160 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
1161 | |
1162 | ||
1163 | if (sscanf(buf, "%d", &new_value) != 1) { | |
1164 | printk(KERN_ERR DRV_NAME | |
1165 | ": %s: no use_carrier value specified.\n", | |
1166 | bond->dev->name); | |
1167 | ret = -EINVAL; | |
1168 | goto out; | |
1169 | } | |
1170 | if ((new_value == 0) || (new_value == 1)) { | |
1171 | bond->params.use_carrier = new_value; | |
1172 | printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n", | |
1173 | bond->dev->name, new_value); | |
1174 | } else { | |
1175 | printk(KERN_INFO DRV_NAME | |
1176 | ": %s: Ignoring invalid use_carrier value %d.\n", | |
1177 | bond->dev->name, new_value); | |
1178 | } | |
1179 | out: | |
1180 | return count; | |
1181 | } | |
43cb76d9 | 1182 | static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier); |
b76cdba9 MW |
1183 | |
1184 | ||
1185 | /* | |
1186 | * Show and set currently active_slave. | |
1187 | */ | |
43cb76d9 GKH |
1188 | static ssize_t bonding_show_active_slave(struct device *d, |
1189 | struct device_attribute *attr, | |
1190 | char *buf) | |
b76cdba9 MW |
1191 | { |
1192 | struct slave *curr; | |
43cb76d9 | 1193 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
1194 | int count; |
1195 | ||
b76cdba9 MW |
1196 | read_lock(&bond->curr_slave_lock); |
1197 | curr = bond->curr_active_slave; | |
1198 | read_unlock(&bond->curr_slave_lock); | |
1199 | ||
1200 | if (USES_PRIMARY(bond->params.mode) && curr) | |
1201 | count = sprintf(buf, "%s\n", curr->dev->name) + 1; | |
1202 | else | |
1203 | count = sprintf(buf, "\n") + 1; | |
1204 | return count; | |
1205 | } | |
1206 | ||
43cb76d9 GKH |
1207 | static ssize_t bonding_store_active_slave(struct device *d, |
1208 | struct device_attribute *attr, | |
1209 | const char *buf, size_t count) | |
b76cdba9 MW |
1210 | { |
1211 | int i; | |
1212 | struct slave *slave; | |
1213 | struct slave *old_active = NULL; | |
1214 | struct slave *new_active = NULL; | |
43cb76d9 | 1215 | struct bonding *bond = to_bond(d); |
b76cdba9 | 1216 | |
1466a219 | 1217 | rtnl_lock(); |
b76cdba9 | 1218 | write_lock_bh(&bond->lock); |
1466a219 | 1219 | |
b76cdba9 MW |
1220 | if (!USES_PRIMARY(bond->params.mode)) { |
1221 | printk(KERN_INFO DRV_NAME | |
1222 | ": %s: Unable to change active slave; %s is in mode %d\n", | |
1223 | bond->dev->name, bond->dev->name, bond->params.mode); | |
1224 | } else { | |
1225 | bond_for_each_slave(bond, slave, i) { | |
1226 | if (strnicmp | |
1227 | (slave->dev->name, buf, | |
1228 | strlen(slave->dev->name)) == 0) { | |
1229 | old_active = bond->curr_active_slave; | |
1230 | new_active = slave; | |
a50d8de2 | 1231 | if (new_active == old_active) { |
b76cdba9 MW |
1232 | /* do nothing */ |
1233 | printk(KERN_INFO DRV_NAME | |
1234 | ": %s: %s is already the current active slave.\n", | |
1235 | bond->dev->name, slave->dev->name); | |
1236 | goto out; | |
1237 | } | |
1238 | else { | |
1239 | if ((new_active) && | |
1240 | (old_active) && | |
1241 | (new_active->link == BOND_LINK_UP) && | |
1242 | IS_UP(new_active->dev)) { | |
1243 | printk(KERN_INFO DRV_NAME | |
1244 | ": %s: Setting %s as active slave.\n", | |
1245 | bond->dev->name, slave->dev->name); | |
1246 | bond_change_active_slave(bond, new_active); | |
1247 | } | |
1248 | else { | |
1249 | printk(KERN_INFO DRV_NAME | |
1250 | ": %s: Could not set %s as active slave; " | |
1251 | "either %s is down or the link is down.\n", | |
1252 | bond->dev->name, slave->dev->name, | |
1253 | slave->dev->name); | |
1254 | } | |
1255 | goto out; | |
1256 | } | |
1257 | } | |
1258 | } | |
1259 | ||
1260 | /* if we got here, then we didn't match the name of any slave */ | |
1261 | ||
1262 | if (strlen(buf) == 0 || buf[0] == '\n') { | |
1263 | printk(KERN_INFO DRV_NAME | |
1264 | ": %s: Setting active slave to None.\n", | |
1265 | bond->dev->name); | |
3418db7c | 1266 | bond->primary_slave = NULL; |
b76cdba9 MW |
1267 | bond_select_active_slave(bond); |
1268 | } else { | |
1269 | printk(KERN_INFO DRV_NAME | |
1270 | ": %s: Unable to set %.*s as active slave as it is not a slave.\n", | |
1271 | bond->dev->name, (int)strlen(buf) - 1, buf); | |
1272 | } | |
1273 | } | |
1274 | out: | |
1275 | write_unlock_bh(&bond->lock); | |
6603a6f2 JV |
1276 | rtnl_unlock(); |
1277 | ||
b76cdba9 MW |
1278 | return count; |
1279 | ||
1280 | } | |
43cb76d9 | 1281 | static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave); |
b76cdba9 MW |
1282 | |
1283 | ||
1284 | /* | |
1285 | * Show link status of the bond interface. | |
1286 | */ | |
43cb76d9 GKH |
1287 | static ssize_t bonding_show_mii_status(struct device *d, |
1288 | struct device_attribute *attr, | |
1289 | char *buf) | |
b76cdba9 MW |
1290 | { |
1291 | struct slave *curr; | |
43cb76d9 | 1292 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
1293 | |
1294 | read_lock(&bond->curr_slave_lock); | |
1295 | curr = bond->curr_active_slave; | |
1296 | read_unlock(&bond->curr_slave_lock); | |
1297 | ||
1298 | return sprintf(buf, "%s\n", (curr) ? "up" : "down") + 1; | |
1299 | } | |
43cb76d9 | 1300 | static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL); |
b76cdba9 MW |
1301 | |
1302 | ||
1303 | /* | |
1304 | * Show current 802.3ad aggregator ID. | |
1305 | */ | |
43cb76d9 GKH |
1306 | static ssize_t bonding_show_ad_aggregator(struct device *d, |
1307 | struct device_attribute *attr, | |
1308 | char *buf) | |
b76cdba9 MW |
1309 | { |
1310 | int count = 0; | |
43cb76d9 | 1311 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
1312 | |
1313 | if (bond->params.mode == BOND_MODE_8023AD) { | |
1314 | struct ad_info ad_info; | |
1315 | count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.aggregator_id) + 1; | |
1316 | } | |
1317 | else | |
1318 | count = sprintf(buf, "\n") + 1; | |
1319 | ||
1320 | return count; | |
1321 | } | |
43cb76d9 | 1322 | static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL); |
b76cdba9 MW |
1323 | |
1324 | ||
1325 | /* | |
1326 | * Show number of active 802.3ad ports. | |
1327 | */ | |
43cb76d9 GKH |
1328 | static ssize_t bonding_show_ad_num_ports(struct device *d, |
1329 | struct device_attribute *attr, | |
1330 | char *buf) | |
b76cdba9 MW |
1331 | { |
1332 | int count = 0; | |
43cb76d9 | 1333 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
1334 | |
1335 | if (bond->params.mode == BOND_MODE_8023AD) { | |
1336 | struct ad_info ad_info; | |
1337 | count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0: ad_info.ports) + 1; | |
1338 | } | |
1339 | else | |
1340 | count = sprintf(buf, "\n") + 1; | |
1341 | ||
1342 | return count; | |
1343 | } | |
43cb76d9 | 1344 | static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL); |
b76cdba9 MW |
1345 | |
1346 | ||
1347 | /* | |
1348 | * Show current 802.3ad actor key. | |
1349 | */ | |
43cb76d9 GKH |
1350 | static ssize_t bonding_show_ad_actor_key(struct device *d, |
1351 | struct device_attribute *attr, | |
1352 | char *buf) | |
b76cdba9 MW |
1353 | { |
1354 | int count = 0; | |
43cb76d9 | 1355 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
1356 | |
1357 | if (bond->params.mode == BOND_MODE_8023AD) { | |
1358 | struct ad_info ad_info; | |
1359 | count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.actor_key) + 1; | |
1360 | } | |
1361 | else | |
1362 | count = sprintf(buf, "\n") + 1; | |
1363 | ||
1364 | return count; | |
1365 | } | |
43cb76d9 | 1366 | static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL); |
b76cdba9 MW |
1367 | |
1368 | ||
1369 | /* | |
1370 | * Show current 802.3ad partner key. | |
1371 | */ | |
43cb76d9 GKH |
1372 | static ssize_t bonding_show_ad_partner_key(struct device *d, |
1373 | struct device_attribute *attr, | |
1374 | char *buf) | |
b76cdba9 MW |
1375 | { |
1376 | int count = 0; | |
43cb76d9 | 1377 | struct bonding *bond = to_bond(d); |
b76cdba9 MW |
1378 | |
1379 | if (bond->params.mode == BOND_MODE_8023AD) { | |
1380 | struct ad_info ad_info; | |
1381 | count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.partner_key) + 1; | |
1382 | } | |
1383 | else | |
1384 | count = sprintf(buf, "\n") + 1; | |
1385 | ||
1386 | return count; | |
1387 | } | |
43cb76d9 | 1388 | static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL); |
b76cdba9 MW |
1389 | |
1390 | ||
1391 | /* | |
1392 | * Show current 802.3ad partner mac. | |
1393 | */ | |
43cb76d9 GKH |
1394 | static ssize_t bonding_show_ad_partner_mac(struct device *d, |
1395 | struct device_attribute *attr, | |
1396 | char *buf) | |
b76cdba9 MW |
1397 | { |
1398 | int count = 0; | |
43cb76d9 | 1399 | struct bonding *bond = to_bond(d); |
0795af57 | 1400 | DECLARE_MAC_BUF(mac); |
b76cdba9 MW |
1401 | |
1402 | if (bond->params.mode == BOND_MODE_8023AD) { | |
1403 | struct ad_info ad_info; | |
1404 | if (!bond_3ad_get_active_agg_info(bond, &ad_info)) { | |
0795af57 JP |
1405 | count = sprintf(buf,"%s\n", |
1406 | print_mac(mac, ad_info.partner_system)) | |
1407 | + 1; | |
b76cdba9 MW |
1408 | } |
1409 | } | |
1410 | else | |
1411 | count = sprintf(buf, "\n") + 1; | |
1412 | ||
1413 | return count; | |
1414 | } | |
43cb76d9 | 1415 | static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL); |
b76cdba9 MW |
1416 | |
1417 | ||
1418 | ||
1419 | static struct attribute *per_bond_attrs[] = { | |
43cb76d9 GKH |
1420 | &dev_attr_slaves.attr, |
1421 | &dev_attr_mode.attr, | |
dd957c57 | 1422 | &dev_attr_fail_over_mac.attr, |
43cb76d9 GKH |
1423 | &dev_attr_arp_validate.attr, |
1424 | &dev_attr_arp_interval.attr, | |
1425 | &dev_attr_arp_ip_target.attr, | |
1426 | &dev_attr_downdelay.attr, | |
1427 | &dev_attr_updelay.attr, | |
1428 | &dev_attr_lacp_rate.attr, | |
1429 | &dev_attr_xmit_hash_policy.attr, | |
1430 | &dev_attr_miimon.attr, | |
1431 | &dev_attr_primary.attr, | |
1432 | &dev_attr_use_carrier.attr, | |
1433 | &dev_attr_active_slave.attr, | |
1434 | &dev_attr_mii_status.attr, | |
1435 | &dev_attr_ad_aggregator.attr, | |
1436 | &dev_attr_ad_num_ports.attr, | |
1437 | &dev_attr_ad_actor_key.attr, | |
1438 | &dev_attr_ad_partner_key.attr, | |
1439 | &dev_attr_ad_partner_mac.attr, | |
b76cdba9 MW |
1440 | NULL, |
1441 | }; | |
1442 | ||
1443 | static struct attribute_group bonding_group = { | |
1444 | .name = "bonding", | |
1445 | .attrs = per_bond_attrs, | |
1446 | }; | |
1447 | ||
1448 | /* | |
1449 | * Initialize sysfs. This sets up the bonding_masters file in | |
1450 | * /sys/class/net. | |
1451 | */ | |
1452 | int bond_create_sysfs(void) | |
1453 | { | |
1454 | int ret = 0; | |
1455 | struct bonding *firstbond; | |
1456 | ||
1457 | init_rwsem(&bonding_rwsem); | |
1458 | ||
1459 | /* get the netdev class pointer */ | |
1460 | firstbond = container_of(bond_dev_list.next, struct bonding, bond_list); | |
1461 | if (!firstbond) | |
1462 | return -ENODEV; | |
1463 | ||
43cb76d9 | 1464 | netdev_class = firstbond->dev->dev.class; |
b76cdba9 MW |
1465 | if (!netdev_class) |
1466 | return -ENODEV; | |
1467 | ||
1468 | ret = class_create_file(netdev_class, &class_attr_bonding_masters); | |
877cbd36 JV |
1469 | /* |
1470 | * Permit multiple loads of the module by ignoring failures to | |
1471 | * create the bonding_masters sysfs file. Bonding devices | |
1472 | * created by second or subsequent loads of the module will | |
1473 | * not be listed in, or controllable by, bonding_masters, but | |
1474 | * will have the usual "bonding" sysfs directory. | |
1475 | * | |
1476 | * This is done to preserve backwards compatibility for | |
1477 | * initscripts/sysconfig, which load bonding multiple times to | |
1478 | * configure multiple bonding devices. | |
1479 | */ | |
1480 | if (ret == -EEXIST) { | |
1481 | netdev_class = NULL; | |
1482 | return 0; | |
1483 | } | |
b76cdba9 MW |
1484 | |
1485 | return ret; | |
1486 | ||
1487 | } | |
1488 | ||
1489 | /* | |
1490 | * Remove /sys/class/net/bonding_masters. | |
1491 | */ | |
1492 | void bond_destroy_sysfs(void) | |
1493 | { | |
1494 | if (netdev_class) | |
1495 | class_remove_file(netdev_class, &class_attr_bonding_masters); | |
1496 | } | |
1497 | ||
1498 | /* | |
1499 | * Initialize sysfs for each bond. This sets up and registers | |
1500 | * the 'bondctl' directory for each individual bond under /sys/class/net. | |
1501 | */ | |
1502 | int bond_create_sysfs_entry(struct bonding *bond) | |
1503 | { | |
1504 | struct net_device *dev = bond->dev; | |
1505 | int err; | |
1506 | ||
43cb76d9 | 1507 | err = sysfs_create_group(&(dev->dev.kobj), &bonding_group); |
b76cdba9 MW |
1508 | if (err) { |
1509 | printk(KERN_EMERG "eek! didn't create group!\n"); | |
1510 | } | |
1511 | ||
1512 | if (expected_refcount < 1) | |
43cb76d9 | 1513 | expected_refcount = atomic_read(&bond->dev->dev.kobj.kref.refcount); |
b76cdba9 MW |
1514 | |
1515 | return err; | |
1516 | } | |
1517 | /* | |
1518 | * Remove sysfs entries for each bond. | |
1519 | */ | |
1520 | void bond_destroy_sysfs_entry(struct bonding *bond) | |
1521 | { | |
1522 | struct net_device *dev = bond->dev; | |
1523 | ||
43cb76d9 | 1524 | sysfs_remove_group(&(dev->dev.kobj), &bonding_group); |
b76cdba9 MW |
1525 | } |
1526 |