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