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