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