bonding: add arp_ip_target netlink support
authorsfeldma@cumulusnetworks.com <sfeldma@cumulusnetworks.com>
Thu, 12 Dec 2013 22:10:31 +0000 (14:10 -0800)
committerDavid S. Miller <davem@davemloft.net>
Sat, 14 Dec 2013 06:07:32 +0000 (01:07 -0500)
Add IFLA_BOND_ARP_IP_TARGET to allow get/set of bonding parameter
arp_ip_target via netlink.

Signed-off-by: Scott Feldman <sfeldma@cumulusnetworks.com>
Signed-off-by: Jiri Pirko <jiri@resnulli.us>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/bonding/bond_netlink.c
drivers/net/bonding/bond_options.c
drivers/net/bonding/bond_sysfs.c
drivers/net/bonding/bonding.h
include/uapi/linux/if_link.h

index 58e71235b75f4b8ce26992e16bd1af864edfcca0..6d30e5db9e3cd66328c77b7dbd29bb13d374f086 100644 (file)
@@ -29,6 +29,7 @@ static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
        [IFLA_BOND_DOWNDELAY]           = { .type = NLA_U32 },
        [IFLA_BOND_USE_CARRIER]         = { .type = NLA_U8 },
        [IFLA_BOND_ARP_INTERVAL]        = { .type = NLA_U32 },
+       [IFLA_BOND_ARP_IP_TARGET]       = { .type = NLA_NESTED },
 };
 
 static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
@@ -116,6 +117,20 @@ static int bond_changelink(struct net_device *bond_dev,
                if (err)
                        return err;
        }
+       if (data[IFLA_BOND_ARP_IP_TARGET]) {
+               __be32 targets[BOND_MAX_ARP_TARGETS] = { 0, };
+               struct nlattr *attr;
+               int i = 0, rem;
+
+               nla_for_each_nested(attr, data[IFLA_BOND_ARP_IP_TARGET], rem) {
+                       __be32 target = nla_get_u32(attr);
+                       targets[i++] = target;
+               }
+
+               err = bond_option_arp_ip_targets_set(bond, targets, i);
+               if (err)
+                       return err;
+       }
        return 0;
 }
 
@@ -140,6 +155,8 @@ static size_t bond_get_size(const struct net_device *bond_dev)
                nla_total_size(sizeof(u32)) +   /* IFLA_BOND_DOWNDELAY */
                nla_total_size(sizeof(u8)) +    /* IFLA_BOND_USE_CARRIER */
                nla_total_size(sizeof(u32)) +   /* IFLA_BOND_ARP_INTERVAL */
+                                               /* IFLA_BOND_ARP_IP_TARGET */
+               nla_total_size(sizeof(u32)) * BOND_MAX_ARP_TARGETS +
                0;
 }
 
@@ -148,6 +165,8 @@ static int bond_fill_info(struct sk_buff *skb,
 {
        struct bonding *bond = netdev_priv(bond_dev);
        struct net_device *slave_dev = bond_option_active_slave_get(bond);
+       struct nlattr *targets;
+       int i, targets_added;
 
        if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode))
                goto nla_put_failure;
@@ -173,6 +192,23 @@ static int bond_fill_info(struct sk_buff *skb,
        if (nla_put_u32(skb, IFLA_BOND_ARP_INTERVAL, bond->params.arp_interval))
                goto nla_put_failure;
 
+       targets = nla_nest_start(skb, IFLA_BOND_ARP_IP_TARGET);
+       if (!targets)
+               goto nla_put_failure;
+
+       targets_added = 0;
+       for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
+               if (bond->params.arp_targets[i]) {
+                       nla_put_u32(skb, i, bond->params.arp_targets[i]);
+                       targets_added = 1;
+               }
+       }
+
+       if (targets_added)
+               nla_nest_end(skb, targets);
+       else
+               nla_nest_cancel(skb, targets);
+
        return 0;
 
 nla_put_failure:
index a84e729d02ef2e3336539f084184029f3c6f15d3..f8c2e4f170662d26990251fc7d4e127c05540937 100644 (file)
@@ -306,3 +306,133 @@ int bond_option_arp_interval_set(struct bonding *bond, int arp_interval)
 
        return 0;
 }
+
+static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
+                                           __be32 target,
+                                           unsigned long last_rx)
+{
+       __be32 *targets = bond->params.arp_targets;
+       struct list_head *iter;
+       struct slave *slave;
+
+       if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) {
+               bond_for_each_slave(bond, slave, iter)
+                       slave->target_last_arp_rx[slot] = last_rx;
+               targets[slot] = target;
+       }
+}
+
+static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
+{
+       __be32 *targets = bond->params.arp_targets;
+       int ind;
+
+       if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) {
+               pr_err("%s: invalid ARP target %pI4 specified for addition\n",
+                      bond->dev->name, &target);
+               return -EINVAL;
+       }
+
+       if (bond_get_targets_ip(targets, target) != -1) { /* dup */
+               pr_err("%s: ARP target %pI4 is already present\n",
+                      bond->dev->name, &target);
+               return -EINVAL;
+       }
+
+       ind = bond_get_targets_ip(targets, 0); /* first free slot */
+       if (ind == -1) {
+               pr_err("%s: ARP target table is full!\n",
+                      bond->dev->name);
+               return -EINVAL;
+       }
+
+       pr_info("%s: adding ARP target %pI4.\n", bond->dev->name, &target);
+
+       _bond_options_arp_ip_target_set(bond, ind, target, jiffies);
+
+       return 0;
+}
+
+int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
+{
+       int ret;
+
+       /* not to race with bond_arp_rcv */
+       write_lock_bh(&bond->lock);
+       ret = _bond_option_arp_ip_target_add(bond, target);
+       write_unlock_bh(&bond->lock);
+
+       return ret;
+}
+
+int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
+{
+       __be32 *targets = bond->params.arp_targets;
+       struct list_head *iter;
+       struct slave *slave;
+       unsigned long *targets_rx;
+       int ind, i;
+
+       if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) {
+               pr_err("%s: invalid ARP target %pI4 specified for removal\n",
+                      bond->dev->name, &target);
+               return -EINVAL;
+       }
+
+       ind = bond_get_targets_ip(targets, target);
+       if (ind == -1) {
+               pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
+                      bond->dev->name, &target);
+               return -EINVAL;
+       }
+
+       if (ind == 0 && !targets[1] && bond->params.arp_interval)
+               pr_warn("%s: removing last arp target with arp_interval on\n",
+                       bond->dev->name);
+
+       pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
+               &target);
+
+       /* not to race with bond_arp_rcv */
+       write_lock_bh(&bond->lock);
+
+       bond_for_each_slave(bond, slave, iter) {
+               targets_rx = slave->target_last_arp_rx;
+               for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
+                       targets_rx[i] = targets_rx[i+1];
+               targets_rx[i] = 0;
+       }
+       for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
+               targets[i] = targets[i+1];
+       targets[i] = 0;
+
+       write_unlock_bh(&bond->lock);
+
+       return 0;
+}
+
+int bond_option_arp_ip_targets_set(struct bonding *bond, __be32 *targets,
+                                  int count)
+{
+       int i, ret = 0;
+
+       /* not to race with bond_arp_rcv */
+       write_lock_bh(&bond->lock);
+
+       /* clear table */
+       for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
+               _bond_options_arp_ip_target_set(bond, i, 0, 0);
+
+       if (count == 0 && bond->params.arp_interval)
+               pr_warn("%s: removing last arp target with arp_interval on\n",
+                       bond->dev->name);
+
+       for (i = 0; i < count; i++) {
+               ret = _bond_option_arp_ip_target_add(bond, targets[i]);
+               if (ret)
+                       break;
+       }
+
+       write_unlock_bh(&bond->lock);
+       return ret;
+}
index 12128bfa88cea526431a4b4a3848fb0dda6a0e1a..a443bbd0fe86b7b6f726e44e3481a5c94fa9aa80 100644 (file)
@@ -552,81 +552,29 @@ static ssize_t bonding_store_arp_targets(struct device *d,
                                         const char *buf, size_t count)
 {
        struct bonding *bond = to_bond(d);
-       struct list_head *iter;
-       struct slave *slave;
-       __be32 newtarget, *targets;
-       unsigned long *targets_rx;
-       int ind, i, j, ret = -EINVAL;
+       __be32 target;
+       int ret = -EPERM;
 
-       if (!rtnl_trylock())
-               return restart_syscall();
-
-       targets = bond->params.arp_targets;
-       if (!in4_pton(buf + 1, -1, (u8 *)&newtarget, -1, NULL) ||
-           IS_IP_TARGET_UNUSABLE_ADDRESS(newtarget)) {
-               pr_err("%s: invalid ARP target %pI4 specified for addition\n",
-                      bond->dev->name, &newtarget);
-               goto out;
+       if (!in4_pton(buf + 1, -1, (u8 *)&target, -1, NULL)) {
+               pr_err("%s: invalid ARP target %pI4 specified\n",
+                      bond->dev->name, &target);
+               return -EPERM;
        }
-       /* look for adds */
-       if (buf[0] == '+') {
-               if (bond_get_targets_ip(targets, newtarget) != -1) { /* dup */
-                       pr_err("%s: ARP target %pI4 is already present\n",
-                              bond->dev->name, &newtarget);
-                       goto out;
-               }
-
-               ind = bond_get_targets_ip(targets, 0); /* first free slot */
-               if (ind == -1) {
-                       pr_err("%s: ARP target table is full!\n",
-                              bond->dev->name);
-                       goto out;
-               }
-
-               pr_info("%s: adding ARP target %pI4.\n", bond->dev->name,
-                        &newtarget);
-               /* not to race with bond_arp_rcv */
-               write_lock_bh(&bond->lock);
-               bond_for_each_slave(bond, slave, iter)
-                       slave->target_last_arp_rx[ind] = jiffies;
-               targets[ind] = newtarget;
-               write_unlock_bh(&bond->lock);
-       } else if (buf[0] == '-')       {
-               ind = bond_get_targets_ip(targets, newtarget);
-               if (ind == -1) {
-                       pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
-                               bond->dev->name, &newtarget);
-                       goto out;
-               }
 
-               if (ind == 0 && !targets[1] && bond->params.arp_interval)
-                       pr_warn("%s: removing last arp target with arp_interval on\n",
-                               bond->dev->name);
-
-               pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
-                       &newtarget);
+       if (!rtnl_trylock())
+               return restart_syscall();
 
-               write_lock_bh(&bond->lock);
-               bond_for_each_slave(bond, slave, iter) {
-                       targets_rx = slave->target_last_arp_rx;
-                       j = ind;
-                       for (; (j < BOND_MAX_ARP_TARGETS-1) && targets[j+1]; j++)
-                               targets_rx[j] = targets_rx[j+1];
-                       targets_rx[j] = 0;
-               }
-               for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
-                       targets[i] = targets[i+1];
-               targets[i] = 0;
-               write_unlock_bh(&bond->lock);
-       } else {
+       if (buf[0] == '+')
+               ret = bond_option_arp_ip_target_add(bond, target);
+       else if (buf[0] == '-')
+               ret = bond_option_arp_ip_target_rem(bond, target);
+       else
                pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
                       bond->dev->name);
-               ret = -EPERM;
-               goto out;
-       }
 
-       ret = count;
-out:
+       if (!ret)
+               ret = count;
+
        rtnl_unlock();
        return ret;
 }
index 44df2738577d8896a24332d186cf77040b03d87f..dcdc8095c169aabb20412915dce3414b81374318 100644 (file)
@@ -444,6 +444,10 @@ int bond_option_updelay_set(struct bonding *bond, int updelay);
 int bond_option_downdelay_set(struct bonding *bond, int downdelay);
 int bond_option_use_carrier_set(struct bonding *bond, int use_carrier);
 int bond_option_arp_interval_set(struct bonding *bond, int arp_interval);
+int bond_option_arp_ip_targets_set(struct bonding *bond, __be32 *targets,
+                                  int count);
+int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target);
+int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target);
 struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond);
 struct net_device *bond_option_active_slave_get(struct bonding *bond);
 
index c73d878afd5871108ec9b926e09b4d1c42b455f3..5b0c444692790bea7d243ff192c7e0f4246cf304 100644 (file)
@@ -336,6 +336,7 @@ enum {
        IFLA_BOND_DOWNDELAY,
        IFLA_BOND_USE_CARRIER,
        IFLA_BOND_ARP_INTERVAL,
+       IFLA_BOND_ARP_IP_TARGET,
        __IFLA_BOND_MAX,
 };