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