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