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