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