Jiri Pirko | 72be35f | 2013-10-18 17:43:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * drivers/net/bond/bond_options.c - bonding options |
| 3 | * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us> |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 4 | * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com> |
Jiri Pirko | 72be35f | 2013-10-18 17:43:34 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/if.h> |
Jiri Pirko | d9e32b2 | 2013-10-18 17:43:35 +0200 | [diff] [blame] | 16 | #include <linux/netdevice.h> |
| 17 | #include <linux/rwlock.h> |
| 18 | #include <linux/rcupdate.h> |
Jiri Pirko | 72be35f | 2013-10-18 17:43:34 +0200 | [diff] [blame] | 19 | #include "bonding.h" |
| 20 | |
| 21 | static bool bond_mode_is_valid(int mode) |
| 22 | { |
| 23 | int i; |
| 24 | |
| 25 | for (i = 0; bond_mode_tbl[i].modename; i++); |
| 26 | |
| 27 | return mode >= 0 && mode < i; |
| 28 | } |
| 29 | |
| 30 | int bond_option_mode_set(struct bonding *bond, int mode) |
| 31 | { |
| 32 | if (!bond_mode_is_valid(mode)) { |
| 33 | pr_err("invalid mode value %d.\n", mode); |
| 34 | return -EINVAL; |
| 35 | } |
| 36 | |
| 37 | if (bond->dev->flags & IFF_UP) { |
| 38 | pr_err("%s: unable to update mode because interface is up.\n", |
| 39 | bond->dev->name); |
| 40 | return -EPERM; |
| 41 | } |
| 42 | |
| 43 | if (bond_has_slaves(bond)) { |
| 44 | pr_err("%s: unable to update mode because bond has slaves.\n", |
| 45 | bond->dev->name); |
| 46 | return -EPERM; |
| 47 | } |
| 48 | |
dingtianhong | fe9d04a | 2013-11-22 22:28:43 +0800 | [diff] [blame] | 49 | if (BOND_NO_USES_ARP(mode) && bond->params.arp_interval) { |
| 50 | pr_info("%s: %s mode is incompatible with arp monitoring, start mii monitoring\n", |
| 51 | bond->dev->name, bond_mode_tbl[mode].modename); |
| 52 | /* disable arp monitoring */ |
| 53 | bond->params.arp_interval = 0; |
| 54 | /* set miimon to default value */ |
| 55 | bond->params.miimon = BOND_DEFAULT_MIIMON; |
| 56 | pr_info("%s: Setting MII monitoring interval to %d.\n", |
| 57 | bond->dev->name, bond->params.miimon); |
Jiri Pirko | 72be35f | 2013-10-18 17:43:34 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /* don't cache arp_validate between modes */ |
| 61 | bond->params.arp_validate = BOND_ARP_VALIDATE_NONE; |
| 62 | bond->params.mode = mode; |
| 63 | return 0; |
| 64 | } |
Jiri Pirko | d9e32b2 | 2013-10-18 17:43:35 +0200 | [diff] [blame] | 65 | |
Jiri Pirko | 752d48b | 2013-10-18 17:43:37 +0200 | [diff] [blame] | 66 | static struct net_device *__bond_option_active_slave_get(struct bonding *bond, |
| 67 | struct slave *slave) |
| 68 | { |
| 69 | return USES_PRIMARY(bond->params.mode) && slave ? slave->dev : NULL; |
| 70 | } |
| 71 | |
| 72 | struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond) |
| 73 | { |
| 74 | struct slave *slave = rcu_dereference(bond->curr_active_slave); |
| 75 | |
| 76 | return __bond_option_active_slave_get(bond, slave); |
| 77 | } |
| 78 | |
| 79 | struct net_device *bond_option_active_slave_get(struct bonding *bond) |
| 80 | { |
| 81 | return __bond_option_active_slave_get(bond, bond->curr_active_slave); |
| 82 | } |
| 83 | |
Jiri Pirko | d9e32b2 | 2013-10-18 17:43:35 +0200 | [diff] [blame] | 84 | int bond_option_active_slave_set(struct bonding *bond, |
| 85 | struct net_device *slave_dev) |
| 86 | { |
| 87 | int ret = 0; |
| 88 | |
| 89 | if (slave_dev) { |
| 90 | if (!netif_is_bond_slave(slave_dev)) { |
| 91 | pr_err("Device %s is not bonding slave.\n", |
| 92 | slave_dev->name); |
| 93 | return -EINVAL; |
| 94 | } |
| 95 | |
| 96 | if (bond->dev != netdev_master_upper_dev_get(slave_dev)) { |
| 97 | pr_err("%s: Device %s is not our slave.\n", |
| 98 | bond->dev->name, slave_dev->name); |
| 99 | return -EINVAL; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if (!USES_PRIMARY(bond->params.mode)) { |
| 104 | pr_err("%s: Unable to change active slave; %s is in mode %d\n", |
| 105 | bond->dev->name, bond->dev->name, bond->params.mode); |
| 106 | return -EINVAL; |
| 107 | } |
| 108 | |
| 109 | block_netpoll_tx(); |
| 110 | read_lock(&bond->lock); |
| 111 | write_lock_bh(&bond->curr_slave_lock); |
| 112 | |
| 113 | /* check to see if we are clearing active */ |
| 114 | if (!slave_dev) { |
| 115 | pr_info("%s: Clearing current active slave.\n", |
| 116 | bond->dev->name); |
| 117 | rcu_assign_pointer(bond->curr_active_slave, NULL); |
| 118 | bond_select_active_slave(bond); |
| 119 | } else { |
| 120 | struct slave *old_active = bond->curr_active_slave; |
| 121 | struct slave *new_active = bond_slave_get_rtnl(slave_dev); |
| 122 | |
| 123 | BUG_ON(!new_active); |
| 124 | |
| 125 | if (new_active == old_active) { |
| 126 | /* do nothing */ |
| 127 | pr_info("%s: %s is already the current active slave.\n", |
| 128 | bond->dev->name, new_active->dev->name); |
| 129 | } else { |
| 130 | if (old_active && (new_active->link == BOND_LINK_UP) && |
| 131 | IS_UP(new_active->dev)) { |
| 132 | pr_info("%s: Setting %s as active slave.\n", |
| 133 | bond->dev->name, new_active->dev->name); |
| 134 | bond_change_active_slave(bond, new_active); |
| 135 | } else { |
| 136 | pr_err("%s: Could not set %s as active slave; either %s is down or the link is down.\n", |
| 137 | bond->dev->name, new_active->dev->name, |
| 138 | new_active->dev->name); |
| 139 | ret = -EINVAL; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | write_unlock_bh(&bond->curr_slave_lock); |
| 145 | read_unlock(&bond->lock); |
| 146 | unblock_netpoll_tx(); |
| 147 | return ret; |
| 148 | } |
sfeldma@cumulusnetworks.com | eecdaa6 | 2013-12-12 14:09:55 -0800 | [diff] [blame] | 149 | |
| 150 | int bond_option_miimon_set(struct bonding *bond, int miimon) |
| 151 | { |
| 152 | if (miimon < 0) { |
| 153 | pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n", |
| 154 | bond->dev->name, miimon, 0, INT_MAX); |
| 155 | return -EINVAL; |
| 156 | } |
| 157 | pr_info("%s: Setting MII monitoring interval to %d.\n", |
| 158 | bond->dev->name, miimon); |
| 159 | bond->params.miimon = miimon; |
| 160 | if (bond->params.updelay) |
| 161 | pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n", |
| 162 | bond->dev->name, |
| 163 | bond->params.updelay * bond->params.miimon); |
| 164 | if (bond->params.downdelay) |
| 165 | pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n", |
| 166 | bond->dev->name, |
| 167 | bond->params.downdelay * bond->params.miimon); |
| 168 | if (miimon && bond->params.arp_interval) { |
| 169 | pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n", |
| 170 | bond->dev->name); |
| 171 | bond->params.arp_interval = 0; |
| 172 | if (bond->params.arp_validate) |
| 173 | bond->params.arp_validate = BOND_ARP_VALIDATE_NONE; |
| 174 | } |
| 175 | if (bond->dev->flags & IFF_UP) { |
| 176 | /* If the interface is up, we may need to fire off |
| 177 | * the MII timer. If the interface is down, the |
| 178 | * timer will get fired off when the open function |
| 179 | * is called. |
| 180 | */ |
| 181 | if (!miimon) { |
| 182 | cancel_delayed_work_sync(&bond->mii_work); |
| 183 | } else { |
| 184 | cancel_delayed_work_sync(&bond->arp_work); |
| 185 | queue_delayed_work(bond->wq, &bond->mii_work, 0); |
| 186 | } |
| 187 | } |
| 188 | return 0; |
| 189 | } |
sfeldma@cumulusnetworks.com | 25852e2 | 2013-12-12 14:10:02 -0800 | [diff] [blame] | 190 | |
| 191 | int bond_option_updelay_set(struct bonding *bond, int updelay) |
| 192 | { |
| 193 | if (!(bond->params.miimon)) { |
| 194 | pr_err("%s: Unable to set up delay as MII monitoring is disabled\n", |
| 195 | bond->dev->name); |
| 196 | return -EPERM; |
| 197 | } |
| 198 | |
| 199 | if (updelay < 0) { |
| 200 | pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n", |
| 201 | bond->dev->name, updelay, 0, INT_MAX); |
| 202 | return -EINVAL; |
| 203 | } else { |
| 204 | if ((updelay % bond->params.miimon) != 0) { |
| 205 | pr_warn("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n", |
| 206 | bond->dev->name, updelay, |
| 207 | bond->params.miimon, |
| 208 | (updelay / bond->params.miimon) * |
| 209 | bond->params.miimon); |
| 210 | } |
| 211 | bond->params.updelay = updelay / bond->params.miimon; |
| 212 | pr_info("%s: Setting up delay to %d.\n", |
| 213 | bond->dev->name, |
| 214 | bond->params.updelay * bond->params.miimon); |
| 215 | } |
| 216 | |
| 217 | return 0; |
| 218 | } |
sfeldma@cumulusnetworks.com | c7461f9 | 2013-12-12 14:10:09 -0800 | [diff] [blame^] | 219 | |
| 220 | int bond_option_downdelay_set(struct bonding *bond, int downdelay) |
| 221 | { |
| 222 | if (!(bond->params.miimon)) { |
| 223 | pr_err("%s: Unable to set down delay as MII monitoring is disabled\n", |
| 224 | bond->dev->name); |
| 225 | return -EPERM; |
| 226 | } |
| 227 | |
| 228 | if (downdelay < 0) { |
| 229 | pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n", |
| 230 | bond->dev->name, downdelay, 0, INT_MAX); |
| 231 | return -EINVAL; |
| 232 | } else { |
| 233 | if ((downdelay % bond->params.miimon) != 0) { |
| 234 | pr_warn("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n", |
| 235 | bond->dev->name, downdelay, |
| 236 | bond->params.miimon, |
| 237 | (downdelay / bond->params.miimon) * |
| 238 | bond->params.miimon); |
| 239 | } |
| 240 | bond->params.downdelay = downdelay / bond->params.miimon; |
| 241 | pr_info("%s: Setting down delay to %d.\n", |
| 242 | bond->dev->name, |
| 243 | bond->params.downdelay * bond->params.miimon); |
| 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | } |