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