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