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