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