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