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