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