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 | } |
sfeldma@cumulusnetworks.com | 9f53e14 | 2013-12-12 14:10:16 -0800 | [diff] [blame] | 248 | |
| 249 | int bond_option_use_carrier_set(struct bonding *bond, int use_carrier) |
| 250 | { |
| 251 | if ((use_carrier == 0) || (use_carrier == 1)) { |
| 252 | bond->params.use_carrier = use_carrier; |
| 253 | pr_info("%s: Setting use_carrier to %d.\n", |
| 254 | bond->dev->name, use_carrier); |
| 255 | } else { |
| 256 | pr_info("%s: Ignoring invalid use_carrier value %d.\n", |
| 257 | bond->dev->name, use_carrier); |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | } |
sfeldma@cumulusnetworks.com | 06151db | 2013-12-12 14:10:24 -0800 | [diff] [blame] | 262 | |
| 263 | int bond_option_arp_interval_set(struct bonding *bond, int arp_interval) |
| 264 | { |
| 265 | if (arp_interval < 0) { |
| 266 | pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n", |
| 267 | bond->dev->name, arp_interval, INT_MAX); |
| 268 | return -EINVAL; |
| 269 | } |
| 270 | if (BOND_NO_USES_ARP(bond->params.mode)) { |
| 271 | pr_info("%s: ARP monitoring cannot be used with ALB/TLB/802.3ad. Only MII monitoring is supported on %s.\n", |
| 272 | bond->dev->name, bond->dev->name); |
| 273 | return -EINVAL; |
| 274 | } |
| 275 | pr_info("%s: Setting ARP monitoring interval to %d.\n", |
| 276 | bond->dev->name, arp_interval); |
| 277 | bond->params.arp_interval = arp_interval; |
| 278 | if (arp_interval) { |
| 279 | if (bond->params.miimon) { |
| 280 | pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n", |
| 281 | bond->dev->name, bond->dev->name); |
| 282 | bond->params.miimon = 0; |
| 283 | } |
| 284 | if (!bond->params.arp_targets[0]) |
| 285 | pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n", |
| 286 | bond->dev->name); |
| 287 | } |
| 288 | if (bond->dev->flags & IFF_UP) { |
| 289 | /* If the interface is up, we may need to fire off |
| 290 | * the ARP timer. If the interface is down, the |
| 291 | * timer will get fired off when the open function |
| 292 | * is called. |
| 293 | */ |
| 294 | if (!arp_interval) { |
| 295 | if (bond->params.arp_validate) |
| 296 | bond->recv_probe = NULL; |
| 297 | cancel_delayed_work_sync(&bond->arp_work); |
| 298 | } else { |
| 299 | /* arp_validate can be set only in active-backup mode */ |
| 300 | if (bond->params.arp_validate) |
| 301 | bond->recv_probe = bond_arp_rcv; |
| 302 | cancel_delayed_work_sync(&bond->mii_work); |
| 303 | queue_delayed_work(bond->wq, &bond->arp_work, 0); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | return 0; |
| 308 | } |
sfeldma@cumulusnetworks.com | 7f28fa1 | 2013-12-12 14:10:31 -0800 | [diff] [blame] | 309 | |
| 310 | static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot, |
| 311 | __be32 target, |
| 312 | unsigned long last_rx) |
| 313 | { |
| 314 | __be32 *targets = bond->params.arp_targets; |
| 315 | struct list_head *iter; |
| 316 | struct slave *slave; |
| 317 | |
| 318 | if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) { |
| 319 | bond_for_each_slave(bond, slave, iter) |
| 320 | slave->target_last_arp_rx[slot] = last_rx; |
| 321 | targets[slot] = target; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target) |
| 326 | { |
| 327 | __be32 *targets = bond->params.arp_targets; |
| 328 | int ind; |
| 329 | |
| 330 | if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) { |
| 331 | pr_err("%s: invalid ARP target %pI4 specified for addition\n", |
| 332 | bond->dev->name, &target); |
| 333 | return -EINVAL; |
| 334 | } |
| 335 | |
| 336 | if (bond_get_targets_ip(targets, target) != -1) { /* dup */ |
| 337 | pr_err("%s: ARP target %pI4 is already present\n", |
| 338 | bond->dev->name, &target); |
| 339 | return -EINVAL; |
| 340 | } |
| 341 | |
| 342 | ind = bond_get_targets_ip(targets, 0); /* first free slot */ |
| 343 | if (ind == -1) { |
| 344 | pr_err("%s: ARP target table is full!\n", |
| 345 | bond->dev->name); |
| 346 | return -EINVAL; |
| 347 | } |
| 348 | |
| 349 | pr_info("%s: adding ARP target %pI4.\n", bond->dev->name, &target); |
| 350 | |
| 351 | _bond_options_arp_ip_target_set(bond, ind, target, jiffies); |
| 352 | |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target) |
| 357 | { |
| 358 | int ret; |
| 359 | |
| 360 | /* not to race with bond_arp_rcv */ |
| 361 | write_lock_bh(&bond->lock); |
| 362 | ret = _bond_option_arp_ip_target_add(bond, target); |
| 363 | write_unlock_bh(&bond->lock); |
| 364 | |
| 365 | return ret; |
| 366 | } |
| 367 | |
| 368 | int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target) |
| 369 | { |
| 370 | __be32 *targets = bond->params.arp_targets; |
| 371 | struct list_head *iter; |
| 372 | struct slave *slave; |
| 373 | unsigned long *targets_rx; |
| 374 | int ind, i; |
| 375 | |
| 376 | if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) { |
| 377 | pr_err("%s: invalid ARP target %pI4 specified for removal\n", |
| 378 | bond->dev->name, &target); |
| 379 | return -EINVAL; |
| 380 | } |
| 381 | |
| 382 | ind = bond_get_targets_ip(targets, target); |
| 383 | if (ind == -1) { |
| 384 | pr_err("%s: unable to remove nonexistent ARP target %pI4.\n", |
| 385 | bond->dev->name, &target); |
| 386 | return -EINVAL; |
| 387 | } |
| 388 | |
| 389 | if (ind == 0 && !targets[1] && bond->params.arp_interval) |
| 390 | pr_warn("%s: removing last arp target with arp_interval on\n", |
| 391 | bond->dev->name); |
| 392 | |
| 393 | pr_info("%s: removing ARP target %pI4.\n", bond->dev->name, |
| 394 | &target); |
| 395 | |
| 396 | /* not to race with bond_arp_rcv */ |
| 397 | write_lock_bh(&bond->lock); |
| 398 | |
| 399 | bond_for_each_slave(bond, slave, iter) { |
| 400 | targets_rx = slave->target_last_arp_rx; |
| 401 | for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++) |
| 402 | targets_rx[i] = targets_rx[i+1]; |
| 403 | targets_rx[i] = 0; |
| 404 | } |
| 405 | for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++) |
| 406 | targets[i] = targets[i+1]; |
| 407 | targets[i] = 0; |
| 408 | |
| 409 | write_unlock_bh(&bond->lock); |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | int bond_option_arp_ip_targets_set(struct bonding *bond, __be32 *targets, |
| 415 | int count) |
| 416 | { |
| 417 | int i, ret = 0; |
| 418 | |
| 419 | /* not to race with bond_arp_rcv */ |
| 420 | write_lock_bh(&bond->lock); |
| 421 | |
| 422 | /* clear table */ |
| 423 | for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) |
| 424 | _bond_options_arp_ip_target_set(bond, i, 0, 0); |
| 425 | |
| 426 | if (count == 0 && bond->params.arp_interval) |
| 427 | pr_warn("%s: removing last arp target with arp_interval on\n", |
| 428 | bond->dev->name); |
| 429 | |
| 430 | for (i = 0; i < count; i++) { |
| 431 | ret = _bond_option_arp_ip_target_add(bond, targets[i]); |
| 432 | if (ret) |
| 433 | break; |
| 434 | } |
| 435 | |
| 436 | write_unlock_bh(&bond->lock); |
| 437 | return ret; |
| 438 | } |
sfeldma@cumulusnetworks.com | 29c4948 | 2013-12-12 14:10:38 -0800 | [diff] [blame] | 439 | |
| 440 | int bond_option_arp_validate_set(struct bonding *bond, int arp_validate) |
| 441 | { |
| 442 | if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) { |
| 443 | pr_err("%s: arp_validate only supported in active-backup mode.\n", |
| 444 | bond->dev->name); |
| 445 | return -EINVAL; |
| 446 | } |
| 447 | pr_info("%s: setting arp_validate to %s (%d).\n", |
| 448 | bond->dev->name, arp_validate_tbl[arp_validate].modename, |
| 449 | arp_validate); |
| 450 | |
| 451 | if (bond->dev->flags & IFF_UP) { |
| 452 | if (!arp_validate) |
| 453 | bond->recv_probe = NULL; |
| 454 | else if (bond->params.arp_interval) |
| 455 | bond->recv_probe = bond_arp_rcv; |
| 456 | } |
| 457 | bond->params.arp_validate = arp_validate; |
| 458 | |
| 459 | return 0; |
| 460 | } |
sfeldma@cumulusnetworks.com | d5c8425 | 2013-12-12 14:10:45 -0800 | [diff] [blame^] | 461 | |
| 462 | int bond_option_arp_all_targets_set(struct bonding *bond, int arp_all_targets) |
| 463 | { |
| 464 | pr_info("%s: setting arp_all_targets to %s (%d).\n", |
| 465 | bond->dev->name, arp_all_targets_tbl[arp_all_targets].modename, |
| 466 | arp_all_targets); |
| 467 | |
| 468 | bond->params.arp_all_targets = arp_all_targets; |
| 469 | |
| 470 | return 0; |
| 471 | } |