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