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