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