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