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