3  * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
 
   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.
 
  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
 
  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.
 
  19  * The full GNU General Public License is included in this distribution in the
 
  20  * file called LICENSE.
 
  23 #include <linux/config.h>
 
  24 #include <linux/kernel.h>
 
  25 #include <linux/module.h>
 
  26 #include <linux/sched.h>
 
  27 #include <linux/device.h>
 
  28 #include <linux/sysdev.h>
 
  30 #include <linux/types.h>
 
  31 #include <linux/string.h>
 
  32 #include <linux/netdevice.h>
 
  33 #include <linux/inetdevice.h>
 
  35 #include <linux/sysfs.h>
 
  36 #include <linux/string.h>
 
  37 #include <linux/ctype.h>
 
  38 #include <linux/inet.h>
 
  39 #include <linux/rtnetlink.h>
 
  41 /* #define BONDING_DEBUG 1 */
 
  43 #define to_class_dev(obj) container_of(obj,struct class_device,kobj)
 
  44 #define to_net_dev(class) container_of(class, struct net_device, class_dev)
 
  45 #define to_bond(cd)     ((struct bonding *)(to_net_dev(cd)->priv))
 
  47 /*---------------------------- Declarations -------------------------------*/
 
  50 extern struct list_head bond_dev_list;
 
  51 extern struct bond_params bonding_defaults;
 
  52 extern struct bond_parm_tbl bond_mode_tbl[];
 
  53 extern struct bond_parm_tbl bond_lacp_tbl[];
 
  54 extern struct bond_parm_tbl xmit_hashtype_tbl[];
 
  56 static int expected_refcount = -1;
 
  57 static struct class *netdev_class;
 
  58 /*--------------------------- Data Structures -----------------------------*/
 
  60 /* Bonding sysfs lock.  Why can't we just use the subsytem lock?
 
  61  * Because kobject_register tries to acquire the subsystem lock.  If
 
  62  * we already hold the lock (which we would if the user was creating
 
  63  * a new bond through the sysfs interface), we deadlock.
 
  64  * This lock is only needed when deleting a bond - we need to make sure
 
  65  * that we don't collide with an ongoing ioctl.
 
  68 struct rw_semaphore bonding_rwsem;
 
  73 /*------------------------------ Functions --------------------------------*/
 
  76  * "show" function for the bond_masters attribute.
 
  77  * The class parameter is ignored.
 
  79 static ssize_t bonding_show_bonds(struct class *cls, char *buffer)
 
  84         down_read(&(bonding_rwsem));
 
  86         list_for_each_entry(bond, &bond_dev_list, bond_list) {
 
  87                 if (res > (PAGE_SIZE - IFNAMSIZ)) {
 
  88                         /* not enough space for another interface name */
 
  89                         if ((PAGE_SIZE - res) > 10)
 
  91                         res += sprintf(buffer + res, "++more++");
 
  94                 res += sprintf(buffer + res, "%s ",
 
  97         res += sprintf(buffer + res, "\n");
 
  99         up_read(&(bonding_rwsem));
 
 104  * "store" function for the bond_masters attribute.  This is what
 
 105  * creates and deletes entire bonds.
 
 107  * The class parameter is ignored.
 
 111 static ssize_t bonding_store_bonds(struct class *cls, const char *buffer, size_t count)
 
 113         char command[IFNAMSIZ + 1] = {0, };
 
 116         struct bonding *bond;
 
 119         down_write(&(bonding_rwsem));
 
 120         sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
 
 121         ifname = command + 1;
 
 122         if ((strlen(command) <= 1) ||
 
 123             !dev_valid_name(ifname))
 
 126         if (command[0] == '+') {
 
 128                 /* Check to see if the bond already exists. */
 
 129                 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
 
 130                         if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
 
 131                                 printk(KERN_ERR DRV_NAME
 
 132                                         ": cannot add bond %s; it already exists\n",
 
 138                 printk(KERN_INFO DRV_NAME
 
 139                         ": %s is being created...\n", ifname);
 
 140                 if (bond_create(ifname, &bonding_defaults, &bond)) {
 
 141                         printk(KERN_INFO DRV_NAME
 
 142                         ": %s interface already exists. Bond creation failed.\n",
 
 149         if (command[0] == '-') {
 
 150                 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
 
 151                         if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
 
 153                                 /* check the ref count on the bond's kobject.
 
 154                                  * If it's > expected, then there's a file open,
 
 155                                  * and we have to fail.
 
 157                                 if (atomic_read(&bond->dev->class_dev.kobj.kref.refcount)
 
 158                                                         > expected_refcount){
 
 160                                         printk(KERN_INFO DRV_NAME
 
 161                                                 ": Unable remove bond %s due to open references.\n",
 
 166                                 printk(KERN_INFO DRV_NAME
 
 167                                         ": %s is being deleted...\n",
 
 169                                 unregister_netdevice(bond->dev);
 
 170                                 bond_deinit(bond->dev);
 
 171                                 bond_destroy_sysfs_entry(bond);
 
 176                 printk(KERN_ERR DRV_NAME
 
 177                         ": unable to delete non-existent bond %s\n", ifname);
 
 183         printk(KERN_ERR DRV_NAME
 
 184                 ": no command found in bonding_masters. Use +ifname or -ifname.\n");
 
 187         /* Always return either count or an error.  If you return 0, you'll
 
 188          * get called forever, which is bad.
 
 191         up_write(&(bonding_rwsem));
 
 194 /* class attribute for bond_masters file.  This ends up in /sys/class/net */
 
 195 static CLASS_ATTR(bonding_masters,  S_IWUSR | S_IRUGO,
 
 196                   bonding_show_bonds, bonding_store_bonds);
 
 198 int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave)
 
 200         char linkname[IFNAMSIZ+7];
 
 203         /* first, create a link from the slave back to the master */
 
 204         ret = sysfs_create_link(&(slave->class_dev.kobj), &(master->class_dev.kobj),
 
 208         /* next, create a link from the master to the slave */
 
 209         sprintf(linkname,"slave_%s",slave->name);
 
 210         ret = sysfs_create_link(&(master->class_dev.kobj), &(slave->class_dev.kobj),
 
 216 void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave)
 
 218         char linkname[IFNAMSIZ+7];
 
 220         sysfs_remove_link(&(slave->class_dev.kobj), "master");
 
 221         sprintf(linkname,"slave_%s",slave->name);
 
 222         sysfs_remove_link(&(master->class_dev.kobj), linkname);
 
 227  * Show the slaves in the current bond.
 
 229 static ssize_t bonding_show_slaves(struct class_device *cd, char *buf)
 
 233         struct bonding *bond = to_bond(cd);
 
 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++");
 
 244                 res += sprintf(buf + res, "%s ", slave->dev->name);
 
 246         read_unlock_bh(&bond->lock);
 
 247         res += sprintf(buf + res, "\n");
 
 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().
 
 257 static ssize_t bonding_store_slaves(struct class_device *cd, const char *buffer, size_t count)
 
 259         char command[IFNAMSIZ + 1] = { 0, };
 
 261         int i, res, found, ret = count;
 
 263         struct net_device *dev = NULL;
 
 264         struct bonding *bond = to_bond(cd);
 
 266         /* Quick sanity check -- is the bond interface up? */
 
 267         if (!(bond->dev->flags & IFF_UP)) {
 
 268                 printk(KERN_ERR DRV_NAME
 
 269                        ": %s: Unable to update slaves because interface is down.\n",
 
 275         /* Note:  We can't hold bond->lock here, as bond_create grabs it. */
 
 277         sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
 
 278         ifname = command + 1;
 
 279         if ((strlen(command) <= 1) ||
 
 280             !dev_valid_name(ifname))
 
 283         if (command[0] == '+') {
 
 285                 /* Got a slave name in ifname.  Is it already in the list? */
 
 287                 read_lock_bh(&bond->lock);
 
 288                 bond_for_each_slave(bond, slave, i)
 
 289                         if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
 
 290                                 printk(KERN_ERR DRV_NAME
 
 291                                        ": %s: Interface %s is already enslaved!\n",
 
 292                                        bond->dev->name, ifname);
 
 294                                 read_unlock_bh(&bond->lock);
 
 298                 read_unlock_bh(&bond->lock);
 
 299                 printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n",
 
 300                        bond->dev->name, ifname);
 
 301                 dev = dev_get_by_name(ifname);
 
 303                         printk(KERN_INFO DRV_NAME
 
 304                                ": %s: Interface %s does not exist!\n",
 
 305                                bond->dev->name, ifname);
 
 312                 if (dev->flags & IFF_UP) {
 
 313                         printk(KERN_ERR DRV_NAME
 
 314                                ": %s: Error: Unable to enslave %s "
 
 315                                "because it is already up.\n",
 
 316                                bond->dev->name, dev->name);
 
 320                 /* If this is the first slave, then we need to set
 
 321                    the master's hardware address to be the same as the
 
 323                 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
 
 324                         memcpy(bond->dev->dev_addr, dev->dev_addr,
 
 328                 /* Set the slave's MTU to match the bond */
 
 329                 if (dev->mtu != bond->dev->mtu) {
 
 330                         if (dev->change_mtu) {
 
 331                                 res = dev->change_mtu(dev,
 
 338                                 dev->mtu = bond->dev->mtu;
 
 342                 res = bond_enslave(bond->dev, dev);
 
 350         if (command[0] == '-') {
 
 352                 bond_for_each_slave(bond, slave, i)
 
 353                         if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
 
 358                         printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n",
 
 359                                 bond->dev->name, dev->name);
 
 361                         res = bond_release(bond->dev, dev);
 
 367                         /* set the slave MTU to the default */
 
 368                         if (dev->change_mtu) {
 
 369                                 dev->change_mtu(dev, 1500);
 
 375                         printk(KERN_ERR DRV_NAME ": unable to remove non-existent slave %s for bond %s.\n",
 
 376                                 ifname, bond->dev->name);
 
 383         printk(KERN_ERR DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
 
 390 static CLASS_DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves, bonding_store_slaves);
 
 393  * Show and set the bonding mode.  The bond interface must be down to
 
 396 static ssize_t bonding_show_mode(struct class_device *cd, char *buf)
 
 398         struct bonding *bond = to_bond(cd);
 
 400         return sprintf(buf, "%s %d\n",
 
 401                         bond_mode_tbl[bond->params.mode].modename,
 
 402                         bond->params.mode) + 1;
 
 405 static ssize_t bonding_store_mode(struct class_device *cd, const char *buf, size_t count)
 
 407         int new_value, ret = count;
 
 408         struct bonding *bond = to_bond(cd);
 
 410         if (bond->dev->flags & IFF_UP) {
 
 411                 printk(KERN_ERR DRV_NAME
 
 412                        ": unable to update mode of %s because interface is up.\n",
 
 418         new_value = bond_parse_parm((char *)buf, bond_mode_tbl);
 
 420                 printk(KERN_ERR DRV_NAME
 
 421                        ": %s: Ignoring invalid mode value %.*s.\n",
 
 423                        (int)strlen(buf) - 1, buf);
 
 427                 if (bond->params.mode == BOND_MODE_8023AD)
 
 428                         bond_unset_master_3ad_flags(bond);
 
 430                 if (bond->params.mode == BOND_MODE_ALB)
 
 431                         bond_unset_master_alb_flags(bond);
 
 433                 bond->params.mode = new_value;
 
 434                 bond_set_mode_ops(bond, bond->params.mode);
 
 435                 printk(KERN_INFO DRV_NAME ": %s: setting mode to %s (%d).\n",
 
 436                         bond->dev->name, bond_mode_tbl[new_value].modename, new_value);
 
 441 static CLASS_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, bonding_show_mode, bonding_store_mode);
 
 444  * Show and set the bonding transmit hash method.  The bond interface must be down to
 
 445  * change the xmit hash policy.
 
 447 static ssize_t bonding_show_xmit_hash(struct class_device *cd, char *buf)
 
 450         struct bonding *bond = to_bond(cd);
 
 452         if ((bond->params.mode != BOND_MODE_XOR) &&
 
 453             (bond->params.mode != BOND_MODE_8023AD)) {
 
 455                 count = sprintf(buf, "NA\n") + 1;
 
 457                 count = sprintf(buf, "%s %d\n",
 
 458                         xmit_hashtype_tbl[bond->params.xmit_policy].modename,
 
 459                         bond->params.xmit_policy) + 1;
 
 465 static ssize_t bonding_store_xmit_hash(struct class_device *cd, const char *buf, size_t count)
 
 467         int new_value, ret = count;
 
 468         struct bonding *bond = to_bond(cd);
 
 470         if (bond->dev->flags & IFF_UP) {
 
 471                 printk(KERN_ERR DRV_NAME
 
 472                        "%s: Interface is up. Unable to update xmit policy.\n",
 
 478         if ((bond->params.mode != BOND_MODE_XOR) &&
 
 479             (bond->params.mode != BOND_MODE_8023AD)) {
 
 480                 printk(KERN_ERR DRV_NAME
 
 481                        "%s: Transmit hash policy is irrelevant in this mode.\n",
 
 487         new_value = bond_parse_parm((char *)buf, xmit_hashtype_tbl);
 
 489                 printk(KERN_ERR DRV_NAME
 
 490                        ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
 
 492                        (int)strlen(buf) - 1, buf);
 
 496                 bond->params.xmit_policy = new_value;
 
 497                 bond_set_mode_ops(bond, bond->params.mode);
 
 498                 printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
 
 499                         bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value);
 
 504 static CLASS_DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash);
 
 507  * Show and set the arp timer interval.  There are two tricky bits
 
 508  * here.  First, if ARP monitoring is activated, then we must disable
 
 509  * MII monitoring.  Second, if the ARP timer isn't running, we must
 
 512 static ssize_t bonding_show_arp_interval(struct class_device *cd, char *buf)
 
 514         struct bonding *bond = to_bond(cd);
 
 516         return sprintf(buf, "%d\n", bond->params.arp_interval) + 1;
 
 519 static ssize_t bonding_store_arp_interval(struct class_device *cd, const char *buf, size_t count)
 
 521         int new_value, ret = count;
 
 522         struct bonding *bond = to_bond(cd);
 
 524         if (sscanf(buf, "%d", &new_value) != 1) {
 
 525                 printk(KERN_ERR DRV_NAME
 
 526                        ": %s: no arp_interval value specified.\n",
 
 532                 printk(KERN_ERR DRV_NAME
 
 533                        ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
 
 534                        bond->dev->name, new_value, INT_MAX);
 
 539         printk(KERN_INFO DRV_NAME
 
 540                ": %s: Setting ARP monitoring interval to %d.\n",
 
 541                bond->dev->name, new_value);
 
 542         bond->params.arp_interval = new_value;
 
 543         if (bond->params.miimon) {
 
 544                 printk(KERN_INFO DRV_NAME
 
 545                        ": %s: ARP monitoring cannot be used with MII monitoring. "
 
 546                        "%s Disabling MII monitoring.\n",
 
 547                        bond->dev->name, bond->dev->name);
 
 548                 bond->params.miimon = 0;
 
 549                 /* Kill MII timer, else it brings bond's link down */
 
 550                 if (bond->arp_timer.function) {
 
 551                         printk(KERN_INFO DRV_NAME
 
 552                         ": %s: Kill MII timer, else it brings bond's link down...\n",
 
 554                         del_timer_sync(&bond->mii_timer);
 
 557         if (!bond->params.arp_targets[0]) {
 
 558                 printk(KERN_INFO DRV_NAME
 
 559                        ": %s: ARP monitoring has been set up, "
 
 560                        "but no ARP targets have been specified.\n",
 
 563         if (bond->dev->flags & IFF_UP) {
 
 564                 /* If the interface is up, we may need to fire off
 
 565                  * the ARP timer.  If the interface is down, the
 
 566                  * timer will get fired off when the open function
 
 569                 if (bond->arp_timer.function) {
 
 570                         /* The timer's already set up, so fire it off */
 
 571                         mod_timer(&bond->arp_timer, jiffies + 1);
 
 573                         /* Set up the timer. */
 
 574                         init_timer(&bond->arp_timer);
 
 575                         bond->arp_timer.expires = jiffies + 1;
 
 576                         bond->arp_timer.data =
 
 577                                 (unsigned long) bond->dev;
 
 578                         if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
 
 579                                 bond->arp_timer.function =
 
 581                                         &bond_activebackup_arp_mon;
 
 583                                 bond->arp_timer.function =
 
 585                                         &bond_loadbalance_arp_mon;
 
 587                         add_timer(&bond->arp_timer);
 
 594 static CLASS_DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval);
 
 597  * Show and set the arp targets.
 
 599 static ssize_t bonding_show_arp_targets(struct class_device *cd, char *buf)
 
 602         struct bonding *bond = to_bond(cd);
 
 604         for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
 
 605                 if (bond->params.arp_targets[i])
 
 606                         res += sprintf(buf + res, "%u.%u.%u.%u ",
 
 607                                NIPQUAD(bond->params.arp_targets[i]));
 
 610                 res--;  /* eat the leftover space */
 
 611         res += sprintf(buf + res, "\n");
 
 616 static ssize_t bonding_store_arp_targets(struct class_device *cd, const char *buf, size_t count)
 
 619         int i = 0, done = 0, ret = count;
 
 620         struct bonding *bond = to_bond(cd);
 
 623         targets = bond->params.arp_targets;
 
 624         newtarget = in_aton(buf + 1);
 
 627                 if ((newtarget == 0) || (newtarget == INADDR_BROADCAST)) {
 
 628                         printk(KERN_ERR DRV_NAME
 
 629                                ": %s: invalid ARP target %u.%u.%u.%u specified for addition\n",
 
 630                                bond->dev->name, NIPQUAD(newtarget));
 
 634                 /* look for an empty slot to put the target in, and check for dupes */
 
 635                 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
 
 636                         if (targets[i] == newtarget) { /* duplicate */
 
 637                                 printk(KERN_ERR DRV_NAME
 
 638                                        ": %s: ARP target %u.%u.%u.%u is already present\n",
 
 639                                        bond->dev->name, NIPQUAD(newtarget));
 
 645                         if (targets[i] == 0 && !done) {
 
 646                                 printk(KERN_INFO DRV_NAME
 
 647                                        ": %s: adding ARP target %d.%d.%d.%d.\n",
 
 648                                        bond->dev->name, NIPQUAD(newtarget));
 
 650                                 targets[i] = newtarget;
 
 654                         printk(KERN_ERR DRV_NAME
 
 655                                ": %s: ARP target table is full!\n",
 
 662         else if (buf[0] == '-') {
 
 663                 if ((newtarget == 0) || (newtarget == INADDR_BROADCAST)) {
 
 664                         printk(KERN_ERR DRV_NAME
 
 665                                ": %s: invalid ARP target %d.%d.%d.%d specified for removal\n",
 
 666                                bond->dev->name, NIPQUAD(newtarget));
 
 671                 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
 
 672                         if (targets[i] == newtarget) {
 
 673                                 printk(KERN_INFO DRV_NAME
 
 674                                        ": %s: removing ARP target %d.%d.%d.%d.\n",
 
 675                                        bond->dev->name, NIPQUAD(newtarget));
 
 681                         printk(KERN_INFO DRV_NAME
 
 682                                ": %s: unable to remove nonexistent ARP target %d.%d.%d.%d.\n",
 
 683                                bond->dev->name, NIPQUAD(newtarget));
 
 689                 printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
 
 698 static CLASS_DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
 
 701  * Show and set the up and down delays.  These must be multiples of the
 
 702  * MII monitoring value, and are stored internally as the multiplier.
 
 703  * Thus, we must translate to MS for the real world.
 
 705 static ssize_t bonding_show_downdelay(struct class_device *cd, char *buf)
 
 707         struct bonding *bond = to_bond(cd);
 
 709         return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon) + 1;
 
 712 static ssize_t bonding_store_downdelay(struct class_device *cd, const char *buf, size_t count)
 
 714         int new_value, ret = count;
 
 715         struct bonding *bond = to_bond(cd);
 
 717         if (!(bond->params.miimon)) {
 
 718                 printk(KERN_ERR DRV_NAME
 
 719                        ": %s: Unable to set down delay as MII monitoring is disabled\n",
 
 725         if (sscanf(buf, "%d", &new_value) != 1) {
 
 726                 printk(KERN_ERR DRV_NAME
 
 727                        ": %s: no down delay value specified.\n",
 
 733                 printk(KERN_ERR DRV_NAME
 
 734                        ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
 
 735                        bond->dev->name, new_value, 1, INT_MAX);
 
 739                 if ((new_value % bond->params.miimon) != 0) {
 
 740                         printk(KERN_WARNING DRV_NAME
 
 741                                ": %s: Warning: down delay (%d) is not a multiple "
 
 742                                "of miimon (%d), delay rounded to %d ms\n",
 
 743                                bond->dev->name, new_value, bond->params.miimon,
 
 744                                (new_value / bond->params.miimon) *
 
 745                                bond->params.miimon);
 
 747                 bond->params.downdelay = new_value / bond->params.miimon;
 
 748                 printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n",
 
 749                        bond->dev->name, bond->params.downdelay * bond->params.miimon);
 
 756 static CLASS_DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay);
 
 758 static ssize_t bonding_show_updelay(struct class_device *cd, char *buf)
 
 760         struct bonding *bond = to_bond(cd);
 
 762         return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon) + 1;
 
 766 static ssize_t bonding_store_updelay(struct class_device *cd, const char *buf, size_t count)
 
 768         int new_value, ret = count;
 
 769         struct bonding *bond = to_bond(cd);
 
 771         if (!(bond->params.miimon)) {
 
 772                 printk(KERN_ERR DRV_NAME
 
 773                        ": %s: Unable to set up delay as MII monitoring is disabled\n",
 
 779         if (sscanf(buf, "%d", &new_value) != 1) {
 
 780                 printk(KERN_ERR DRV_NAME
 
 781                        ": %s: no up delay value specified.\n",
 
 787                 printk(KERN_ERR DRV_NAME
 
 788                        ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
 
 789                        bond->dev->name, new_value, 1, INT_MAX);
 
 793                 if ((new_value % bond->params.miimon) != 0) {
 
 794                         printk(KERN_WARNING DRV_NAME
 
 795                                ": %s: Warning: up delay (%d) is not a multiple "
 
 796                                "of miimon (%d), updelay rounded to %d ms\n",
 
 797                                bond->dev->name, new_value, bond->params.miimon,
 
 798                                (new_value / bond->params.miimon) *
 
 799                                bond->params.miimon);
 
 801                 bond->params.updelay = new_value / bond->params.miimon;
 
 802                 printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n",
 
 803                        bond->dev->name, bond->params.updelay * bond->params.miimon);
 
 810 static CLASS_DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay);
 
 813  * Show and set the LACP interval.  Interface must be down, and the mode
 
 814  * must be set to 802.3ad mode.
 
 816 static ssize_t bonding_show_lacp(struct class_device *cd, char *buf)
 
 818         struct bonding *bond = to_bond(cd);
 
 820         return sprintf(buf, "%s %d\n",
 
 821                 bond_lacp_tbl[bond->params.lacp_fast].modename,
 
 822                 bond->params.lacp_fast) + 1;
 
 825 static ssize_t bonding_store_lacp(struct class_device *cd, const char *buf, size_t count)
 
 827         int new_value, ret = count;
 
 828         struct bonding *bond = to_bond(cd);
 
 830         if (bond->dev->flags & IFF_UP) {
 
 831                 printk(KERN_ERR DRV_NAME
 
 832                        ": %s: Unable to update LACP rate because interface is up.\n",
 
 838         if (bond->params.mode != BOND_MODE_8023AD) {
 
 839                 printk(KERN_ERR DRV_NAME
 
 840                        ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
 
 846         new_value = bond_parse_parm((char *)buf, bond_lacp_tbl);
 
 848         if ((new_value == 1) || (new_value == 0)) {
 
 849                 bond->params.lacp_fast = new_value;
 
 850                 printk(KERN_INFO DRV_NAME
 
 851                        ": %s: Setting LACP rate to %s (%d).\n",
 
 852                        bond->dev->name, bond_lacp_tbl[new_value].modename, new_value);
 
 854                 printk(KERN_ERR DRV_NAME
 
 855                        ": %s: Ignoring invalid LACP rate value %.*s.\n",
 
 856                         bond->dev->name, (int)strlen(buf) - 1, buf);
 
 862 static CLASS_DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp);
 
 865  * Show and set the MII monitor interval.  There are two tricky bits
 
 866  * here.  First, if MII monitoring is activated, then we must disable
 
 867  * ARP monitoring.  Second, if the timer isn't running, we must
 
 870 static ssize_t bonding_show_miimon(struct class_device *cd, char *buf)
 
 872         struct bonding *bond = to_bond(cd);
 
 874         return sprintf(buf, "%d\n", bond->params.miimon) + 1;
 
 877 static ssize_t bonding_store_miimon(struct class_device *cd, const char *buf, size_t count)
 
 879         int new_value, ret = count;
 
 880         struct bonding *bond = to_bond(cd);
 
 882         if (sscanf(buf, "%d", &new_value) != 1) {
 
 883                 printk(KERN_ERR DRV_NAME
 
 884                        ": %s: no miimon value specified.\n",
 
 890                 printk(KERN_ERR DRV_NAME
 
 891                        ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
 
 892                        bond->dev->name, new_value, 1, INT_MAX);
 
 896                 printk(KERN_INFO DRV_NAME
 
 897                        ": %s: Setting MII monitoring interval to %d.\n",
 
 898                        bond->dev->name, new_value);
 
 899                 bond->params.miimon = new_value;
 
 900                 if(bond->params.updelay)
 
 901                         printk(KERN_INFO DRV_NAME
 
 902                               ": %s: Note: Updating updelay (to %d) "
 
 903                               "since it is a multiple of the miimon value.\n",
 
 905                               bond->params.updelay * bond->params.miimon);
 
 906                 if(bond->params.downdelay)
 
 907                         printk(KERN_INFO DRV_NAME
 
 908                               ": %s: Note: Updating downdelay (to %d) "
 
 909                               "since it is a multiple of the miimon value.\n",
 
 911                               bond->params.downdelay * bond->params.miimon);
 
 912                 if (bond->params.arp_interval) {
 
 913                         printk(KERN_INFO DRV_NAME
 
 914                                ": %s: MII monitoring cannot be used with "
 
 915                                "ARP monitoring. Disabling ARP monitoring...\n",
 
 917                         bond->params.arp_interval = 0;
 
 918                         /* Kill ARP timer, else it brings bond's link down */
 
 919                         if (bond->mii_timer.function) {
 
 920                                 printk(KERN_INFO DRV_NAME
 
 921                                 ": %s: Kill ARP timer, else it brings bond's link down...\n",
 
 923                                 del_timer_sync(&bond->arp_timer);
 
 927                 if (bond->dev->flags & IFF_UP) {
 
 928                         /* If the interface is up, we may need to fire off
 
 929                          * the MII timer. If the interface is down, the
 
 930                          * timer will get fired off when the open function
 
 933                         if (bond->mii_timer.function) {
 
 934                                 /* The timer's already set up, so fire it off */
 
 935                                 mod_timer(&bond->mii_timer, jiffies + 1);
 
 937                                 /* Set up the timer. */
 
 938                                 init_timer(&bond->mii_timer);
 
 939                                 bond->mii_timer.expires = jiffies + 1;
 
 940                                 bond->mii_timer.data =
 
 941                                         (unsigned long) bond->dev;
 
 942                                 bond->mii_timer.function =
 
 943                                         (void *) &bond_mii_monitor;
 
 944                                 add_timer(&bond->mii_timer);
 
 951 static CLASS_DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);
 
 954  * Show and set the primary slave.  The store function is much
 
 955  * simpler than bonding_store_slaves function because it only needs to
 
 956  * handle one interface name.
 
 957  * The bond must be a mode that supports a primary for this be
 
 960 static ssize_t bonding_show_primary(struct class_device *cd, char *buf)
 
 963         struct bonding *bond = to_bond(cd);
 
 965         if (bond->primary_slave)
 
 966                 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name) + 1;
 
 968                 count = sprintf(buf, "\n") + 1;
 
 973 static ssize_t bonding_store_primary(struct class_device *cd, const char *buf, size_t count)
 
 977         struct bonding *bond = to_bond(cd);
 
 979         write_lock_bh(&bond->lock);
 
 980         if (!USES_PRIMARY(bond->params.mode)) {
 
 981                 printk(KERN_INFO DRV_NAME
 
 982                        ": %s: Unable to set primary slave; %s is in mode %d\n",
 
 983                        bond->dev->name, bond->dev->name, bond->params.mode);
 
 985                 bond_for_each_slave(bond, slave, i) {
 
 987                             (slave->dev->name, buf,
 
 988                              strlen(slave->dev->name)) == 0) {
 
 989                                 printk(KERN_INFO DRV_NAME
 
 990                                        ": %s: Setting %s as primary slave.\n",
 
 991                                        bond->dev->name, slave->dev->name);
 
 992                                 bond->primary_slave = slave;
 
 993                                 bond_select_active_slave(bond);
 
 998                 /* if we got here, then we didn't match the name of any slave */
 
1000                 if (strlen(buf) == 0 || buf[0] == '\n') {
 
1001                         printk(KERN_INFO DRV_NAME
 
1002                                ": %s: Setting primary slave to None.\n",
 
1004                         bond->primary_slave = NULL;
 
1005                                 bond_select_active_slave(bond);
 
1007                         printk(KERN_INFO DRV_NAME
 
1008                                ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
 
1009                                bond->dev->name, (int)strlen(buf) - 1, buf);
 
1013         write_unlock_bh(&bond->lock);
 
1016 static CLASS_DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary);
 
1019  * Show and set the use_carrier flag.
 
1021 static ssize_t bonding_show_carrier(struct class_device *cd, char *buf)
 
1023         struct bonding *bond = to_bond(cd);
 
1025         return sprintf(buf, "%d\n", bond->params.use_carrier) + 1;
 
1028 static ssize_t bonding_store_carrier(struct class_device *cd, const char *buf, size_t count)
 
1030         int new_value, ret = count;
 
1031         struct bonding *bond = to_bond(cd);
 
1034         if (sscanf(buf, "%d", &new_value) != 1) {
 
1035                 printk(KERN_ERR DRV_NAME
 
1036                        ": %s: no use_carrier value specified.\n",
 
1041         if ((new_value == 0) || (new_value == 1)) {
 
1042                 bond->params.use_carrier = new_value;
 
1043                 printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
 
1044                        bond->dev->name, new_value);
 
1046                 printk(KERN_INFO DRV_NAME
 
1047                        ": %s: Ignoring invalid use_carrier value %d.\n",
 
1048                        bond->dev->name, new_value);
 
1053 static CLASS_DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier);
 
1057  * Show and set currently active_slave.
 
1059 static ssize_t bonding_show_active_slave(struct class_device *cd, char *buf)
 
1062         struct bonding *bond = to_bond(cd);
 
1066         read_lock(&bond->curr_slave_lock);
 
1067         curr = bond->curr_active_slave;
 
1068         read_unlock(&bond->curr_slave_lock);
 
1070         if (USES_PRIMARY(bond->params.mode) && curr)
 
1071                 count = sprintf(buf, "%s\n", curr->dev->name) + 1;
 
1073                 count = sprintf(buf, "\n") + 1;
 
1077 static ssize_t bonding_store_active_slave(struct class_device *cd, const char *buf, size_t count)
 
1080         struct slave *slave;
 
1081         struct slave *old_active = NULL;
 
1082         struct slave *new_active = NULL;
 
1083         struct bonding *bond = to_bond(cd);
 
1085         write_lock_bh(&bond->lock);
 
1086         if (!USES_PRIMARY(bond->params.mode)) {
 
1087                 printk(KERN_INFO DRV_NAME
 
1088                        ": %s: Unable to change active slave; %s is in mode %d\n",
 
1089                        bond->dev->name, bond->dev->name, bond->params.mode);
 
1091                 bond_for_each_slave(bond, slave, i) {
 
1093                             (slave->dev->name, buf,
 
1094                              strlen(slave->dev->name)) == 0) {
 
1095                                 old_active = bond->curr_active_slave;
 
1097                                 if (new_active && (new_active == old_active)) {
 
1099                                         printk(KERN_INFO DRV_NAME
 
1100                                                ": %s: %s is already the current active slave.\n",
 
1101                                                bond->dev->name, slave->dev->name);
 
1107                                             (new_active->link == BOND_LINK_UP) &&
 
1108                                             IS_UP(new_active->dev)) {
 
1109                                                 printk(KERN_INFO DRV_NAME
 
1110                                                       ": %s: Setting %s as active slave.\n",
 
1111                                                       bond->dev->name, slave->dev->name);
 
1112                                                 bond_change_active_slave(bond, new_active);
 
1115                                                 printk(KERN_INFO DRV_NAME
 
1116                                                       ": %s: Could not set %s as active slave; "
 
1117                                                       "either %s is down or the link is down.\n",
 
1118                                                       bond->dev->name, slave->dev->name,
 
1126                 /* if we got here, then we didn't match the name of any slave */
 
1128                 if (strlen(buf) == 0 || buf[0] == '\n') {
 
1129                         printk(KERN_INFO DRV_NAME
 
1130                                ": %s: Setting active slave to None.\n",
 
1132                         bond->primary_slave = NULL;
 
1133                                 bond_select_active_slave(bond);
 
1135                         printk(KERN_INFO DRV_NAME
 
1136                                ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
 
1137                                bond->dev->name, (int)strlen(buf) - 1, buf);
 
1141         write_unlock_bh(&bond->lock);
 
1145 static CLASS_DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);
 
1149  * Show link status of the bond interface.
 
1151 static ssize_t bonding_show_mii_status(struct class_device *cd, char *buf)
 
1154         struct bonding *bond = to_bond(cd);
 
1156         read_lock(&bond->curr_slave_lock);
 
1157         curr = bond->curr_active_slave;
 
1158         read_unlock(&bond->curr_slave_lock);
 
1160         return sprintf(buf, "%s\n", (curr) ? "up" : "down") + 1;
 
1162 static CLASS_DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
 
1166  * Show current 802.3ad aggregator ID.
 
1168 static ssize_t bonding_show_ad_aggregator(struct class_device *cd, char *buf)
 
1171         struct bonding *bond = to_bond(cd);
 
1173         if (bond->params.mode == BOND_MODE_8023AD) {
 
1174                 struct ad_info ad_info;
 
1175                 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ?  0 : ad_info.aggregator_id) + 1;
 
1178                 count = sprintf(buf, "\n") + 1;
 
1182 static CLASS_DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
 
1186  * Show number of active 802.3ad ports.
 
1188 static ssize_t bonding_show_ad_num_ports(struct class_device *cd, char *buf)
 
1191         struct bonding *bond = to_bond(cd);
 
1193         if (bond->params.mode == BOND_MODE_8023AD) {
 
1194                 struct ad_info ad_info;
 
1195                 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ?  0: ad_info.ports) + 1;
 
1198                 count = sprintf(buf, "\n") + 1;
 
1202 static CLASS_DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
 
1206  * Show current 802.3ad actor key.
 
1208 static ssize_t bonding_show_ad_actor_key(struct class_device *cd, char *buf)
 
1211         struct bonding *bond = to_bond(cd);
 
1213         if (bond->params.mode == BOND_MODE_8023AD) {
 
1214                 struct ad_info ad_info;
 
1215                 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ?  0 : ad_info.actor_key) + 1;
 
1218                 count = sprintf(buf, "\n") + 1;
 
1222 static CLASS_DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
 
1226  * Show current 802.3ad partner key.
 
1228 static ssize_t bonding_show_ad_partner_key(struct class_device *cd, char *buf)
 
1231         struct bonding *bond = to_bond(cd);
 
1233         if (bond->params.mode == BOND_MODE_8023AD) {
 
1234                 struct ad_info ad_info;
 
1235                 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ?  0 : ad_info.partner_key) + 1;
 
1238                 count = sprintf(buf, "\n") + 1;
 
1242 static CLASS_DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
 
1246  * Show current 802.3ad partner mac.
 
1248 static ssize_t bonding_show_ad_partner_mac(struct class_device *cd, char *buf)
 
1251         struct bonding *bond = to_bond(cd);
 
1253         if (bond->params.mode == BOND_MODE_8023AD) {
 
1254                 struct ad_info ad_info;
 
1255                 if (!bond_3ad_get_active_agg_info(bond, &ad_info)) {
 
1256                         count = sprintf(buf,"%02x:%02x:%02x:%02x:%02x:%02x\n",
 
1257                                        ad_info.partner_system[0],
 
1258                                        ad_info.partner_system[1],
 
1259                                        ad_info.partner_system[2],
 
1260                                        ad_info.partner_system[3],
 
1261                                        ad_info.partner_system[4],
 
1262                                        ad_info.partner_system[5]) + 1;
 
1266                 count = sprintf(buf, "\n") + 1;
 
1270 static CLASS_DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
 
1274 static struct attribute *per_bond_attrs[] = {
 
1275         &class_device_attr_slaves.attr,
 
1276         &class_device_attr_mode.attr,
 
1277         &class_device_attr_arp_interval.attr,
 
1278         &class_device_attr_arp_ip_target.attr,
 
1279         &class_device_attr_downdelay.attr,
 
1280         &class_device_attr_updelay.attr,
 
1281         &class_device_attr_lacp_rate.attr,
 
1282         &class_device_attr_xmit_hash_policy.attr,
 
1283         &class_device_attr_miimon.attr,
 
1284         &class_device_attr_primary.attr,
 
1285         &class_device_attr_use_carrier.attr,
 
1286         &class_device_attr_active_slave.attr,
 
1287         &class_device_attr_mii_status.attr,
 
1288         &class_device_attr_ad_aggregator.attr,
 
1289         &class_device_attr_ad_num_ports.attr,
 
1290         &class_device_attr_ad_actor_key.attr,
 
1291         &class_device_attr_ad_partner_key.attr,
 
1292         &class_device_attr_ad_partner_mac.attr,
 
1296 static struct attribute_group bonding_group = {
 
1298         .attrs = per_bond_attrs,
 
1302  * Initialize sysfs.  This sets up the bonding_masters file in
 
1305 int bond_create_sysfs(void)
 
1308         struct bonding *firstbond;
 
1310         init_rwsem(&bonding_rwsem);
 
1312         /* get the netdev class pointer */
 
1313         firstbond = container_of(bond_dev_list.next, struct bonding, bond_list);
 
1317         netdev_class = firstbond->dev->class_dev.class;
 
1321         ret = class_create_file(netdev_class, &class_attr_bonding_masters);
 
1328  * Remove /sys/class/net/bonding_masters.
 
1330 void bond_destroy_sysfs(void)
 
1333                 class_remove_file(netdev_class, &class_attr_bonding_masters);
 
1337  * Initialize sysfs for each bond.  This sets up and registers
 
1338  * the 'bondctl' directory for each individual bond under /sys/class/net.
 
1340 int bond_create_sysfs_entry(struct bonding *bond)
 
1342         struct net_device *dev = bond->dev;
 
1345         err = sysfs_create_group(&(dev->class_dev.kobj), &bonding_group);
 
1347                 printk(KERN_EMERG "eek! didn't create group!\n");
 
1350         if (expected_refcount < 1)
 
1351                 expected_refcount = atomic_read(&bond->dev->class_dev.kobj.kref.refcount);
 
1356  * Remove sysfs entries for each bond.
 
1358 void bond_destroy_sysfs_entry(struct bonding *bond)
 
1360         struct net_device *dev = bond->dev;
 
1362         sysfs_remove_group(&(dev->class_dev.kobj), &bonding_group);