1 /* 2 * drivers/net/bond/bond_options.c - bonding options 3 * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us> 4 * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com> 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> 16 #include <linux/netdevice.h> 17 #include <linux/rwlock.h> 18 #include <linux/rcupdate.h> 19 #include <linux/reciprocal_div.h> 20 #include "bonding.h" 21 22 static bool bond_mode_is_valid(int mode) 23 { 24 int i; 25 26 for (i = 0; bond_mode_tbl[i].modename; i++); 27 28 return mode >= 0 && mode < i; 29 } 30 31 int bond_option_mode_set(struct bonding *bond, int mode) 32 { 33 if (!bond_mode_is_valid(mode)) { 34 pr_err("invalid mode value %d.\n", mode); 35 return -EINVAL; 36 } 37 38 if (bond->dev->flags & IFF_UP) { 39 pr_err("%s: unable to update mode because interface is up.\n", 40 bond->dev->name); 41 return -EPERM; 42 } 43 44 if (bond_has_slaves(bond)) { 45 pr_err("%s: unable to update mode because bond has slaves.\n", 46 bond->dev->name); 47 return -EPERM; 48 } 49 50 if (BOND_NO_USES_ARP(mode) && bond->params.arp_interval) { 51 pr_info("%s: %s mode is incompatible with arp monitoring, start mii monitoring\n", 52 bond->dev->name, bond_mode_tbl[mode].modename); 53 /* disable arp monitoring */ 54 bond->params.arp_interval = 0; 55 /* set miimon to default value */ 56 bond->params.miimon = BOND_DEFAULT_MIIMON; 57 pr_info("%s: Setting MII monitoring interval to %d.\n", 58 bond->dev->name, bond->params.miimon); 59 } 60 61 /* don't cache arp_validate between modes */ 62 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE; 63 bond->params.mode = mode; 64 return 0; 65 } 66 67 static struct net_device *__bond_option_active_slave_get(struct bonding *bond, 68 struct slave *slave) 69 { 70 return USES_PRIMARY(bond->params.mode) && slave ? slave->dev : NULL; 71 } 72 73 struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond) 74 { 75 struct slave *slave = rcu_dereference(bond->curr_active_slave); 76 77 return __bond_option_active_slave_get(bond, slave); 78 } 79 80 struct net_device *bond_option_active_slave_get(struct bonding *bond) 81 { 82 return __bond_option_active_slave_get(bond, bond->curr_active_slave); 83 } 84 85 int bond_option_active_slave_set(struct bonding *bond, 86 struct net_device *slave_dev) 87 { 88 int ret = 0; 89 90 if (slave_dev) { 91 if (!netif_is_bond_slave(slave_dev)) { 92 pr_err("Device %s is not bonding slave.\n", 93 slave_dev->name); 94 return -EINVAL; 95 } 96 97 if (bond->dev != netdev_master_upper_dev_get(slave_dev)) { 98 pr_err("%s: Device %s is not our slave.\n", 99 bond->dev->name, slave_dev->name); 100 return -EINVAL; 101 } 102 } 103 104 if (!USES_PRIMARY(bond->params.mode)) { 105 pr_err("%s: Unable to change active slave; %s is in mode %d\n", 106 bond->dev->name, bond->dev->name, bond->params.mode); 107 return -EINVAL; 108 } 109 110 block_netpoll_tx(); 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 unblock_netpoll_tx(); 146 return ret; 147 } 148 149 int bond_option_miimon_set(struct bonding *bond, int miimon) 150 { 151 if (miimon < 0) { 152 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n", 153 bond->dev->name, miimon, 0, INT_MAX); 154 return -EINVAL; 155 } 156 pr_info("%s: Setting MII monitoring interval to %d.\n", 157 bond->dev->name, miimon); 158 bond->params.miimon = miimon; 159 if (bond->params.updelay) 160 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n", 161 bond->dev->name, 162 bond->params.updelay * bond->params.miimon); 163 if (bond->params.downdelay) 164 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n", 165 bond->dev->name, 166 bond->params.downdelay * bond->params.miimon); 167 if (miimon && bond->params.arp_interval) { 168 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n", 169 bond->dev->name); 170 bond->params.arp_interval = 0; 171 if (bond->params.arp_validate) 172 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE; 173 } 174 if (bond->dev->flags & IFF_UP) { 175 /* If the interface is up, we may need to fire off 176 * the MII timer. If the interface is down, the 177 * timer will get fired off when the open function 178 * is called. 179 */ 180 if (!miimon) { 181 cancel_delayed_work_sync(&bond->mii_work); 182 } else { 183 cancel_delayed_work_sync(&bond->arp_work); 184 queue_delayed_work(bond->wq, &bond->mii_work, 0); 185 } 186 } 187 return 0; 188 } 189 190 int bond_option_updelay_set(struct bonding *bond, int updelay) 191 { 192 if (!(bond->params.miimon)) { 193 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n", 194 bond->dev->name); 195 return -EPERM; 196 } 197 198 if (updelay < 0) { 199 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n", 200 bond->dev->name, updelay, 0, INT_MAX); 201 return -EINVAL; 202 } else { 203 if ((updelay % bond->params.miimon) != 0) { 204 pr_warn("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n", 205 bond->dev->name, updelay, 206 bond->params.miimon, 207 (updelay / bond->params.miimon) * 208 bond->params.miimon); 209 } 210 bond->params.updelay = updelay / bond->params.miimon; 211 pr_info("%s: Setting up delay to %d.\n", 212 bond->dev->name, 213 bond->params.updelay * bond->params.miimon); 214 } 215 216 return 0; 217 } 218 219 int bond_option_downdelay_set(struct bonding *bond, int downdelay) 220 { 221 if (!(bond->params.miimon)) { 222 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n", 223 bond->dev->name); 224 return -EPERM; 225 } 226 227 if (downdelay < 0) { 228 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n", 229 bond->dev->name, downdelay, 0, INT_MAX); 230 return -EINVAL; 231 } else { 232 if ((downdelay % bond->params.miimon) != 0) { 233 pr_warn("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n", 234 bond->dev->name, downdelay, 235 bond->params.miimon, 236 (downdelay / bond->params.miimon) * 237 bond->params.miimon); 238 } 239 bond->params.downdelay = downdelay / bond->params.miimon; 240 pr_info("%s: Setting down delay to %d.\n", 241 bond->dev->name, 242 bond->params.downdelay * bond->params.miimon); 243 } 244 245 return 0; 246 } 247 248 int bond_option_use_carrier_set(struct bonding *bond, int use_carrier) 249 { 250 if ((use_carrier == 0) || (use_carrier == 1)) { 251 bond->params.use_carrier = use_carrier; 252 pr_info("%s: Setting use_carrier to %d.\n", 253 bond->dev->name, use_carrier); 254 } else { 255 pr_info("%s: Ignoring invalid use_carrier value %d.\n", 256 bond->dev->name, use_carrier); 257 } 258 259 return 0; 260 } 261 262 int bond_option_arp_interval_set(struct bonding *bond, int arp_interval) 263 { 264 if (arp_interval < 0) { 265 pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n", 266 bond->dev->name, arp_interval, INT_MAX); 267 return -EINVAL; 268 } 269 if (BOND_NO_USES_ARP(bond->params.mode)) { 270 pr_info("%s: ARP monitoring cannot be used with ALB/TLB/802.3ad. Only MII monitoring is supported on %s.\n", 271 bond->dev->name, bond->dev->name); 272 return -EINVAL; 273 } 274 pr_info("%s: Setting ARP monitoring interval to %d.\n", 275 bond->dev->name, arp_interval); 276 bond->params.arp_interval = arp_interval; 277 if (arp_interval) { 278 if (bond->params.miimon) { 279 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n", 280 bond->dev->name, bond->dev->name); 281 bond->params.miimon = 0; 282 } 283 if (!bond->params.arp_targets[0]) 284 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n", 285 bond->dev->name); 286 } 287 if (bond->dev->flags & IFF_UP) { 288 /* If the interface is up, we may need to fire off 289 * the ARP timer. If the interface is down, the 290 * timer will get fired off when the open function 291 * is called. 292 */ 293 if (!arp_interval) { 294 if (bond->params.arp_validate) 295 bond->recv_probe = NULL; 296 cancel_delayed_work_sync(&bond->arp_work); 297 } else { 298 /* arp_validate can be set only in active-backup mode */ 299 if (bond->params.arp_validate) 300 bond->recv_probe = bond_arp_rcv; 301 cancel_delayed_work_sync(&bond->mii_work); 302 queue_delayed_work(bond->wq, &bond->arp_work, 0); 303 } 304 } 305 306 return 0; 307 } 308 309 static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot, 310 __be32 target, 311 unsigned long last_rx) 312 { 313 __be32 *targets = bond->params.arp_targets; 314 struct list_head *iter; 315 struct slave *slave; 316 317 if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) { 318 bond_for_each_slave(bond, slave, iter) 319 slave->target_last_arp_rx[slot] = last_rx; 320 targets[slot] = target; 321 } 322 } 323 324 static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target) 325 { 326 __be32 *targets = bond->params.arp_targets; 327 int ind; 328 329 if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) { 330 pr_err("%s: invalid ARP target %pI4 specified for addition\n", 331 bond->dev->name, &target); 332 return -EINVAL; 333 } 334 335 if (bond_get_targets_ip(targets, target) != -1) { /* dup */ 336 pr_err("%s: ARP target %pI4 is already present\n", 337 bond->dev->name, &target); 338 return -EINVAL; 339 } 340 341 ind = bond_get_targets_ip(targets, 0); /* first free slot */ 342 if (ind == -1) { 343 pr_err("%s: ARP target table is full!\n", 344 bond->dev->name); 345 return -EINVAL; 346 } 347 348 pr_info("%s: adding ARP target %pI4.\n", bond->dev->name, &target); 349 350 _bond_options_arp_ip_target_set(bond, ind, target, jiffies); 351 352 return 0; 353 } 354 355 int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target) 356 { 357 int ret; 358 359 /* not to race with bond_arp_rcv */ 360 write_lock_bh(&bond->lock); 361 ret = _bond_option_arp_ip_target_add(bond, target); 362 write_unlock_bh(&bond->lock); 363 364 return ret; 365 } 366 367 int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target) 368 { 369 __be32 *targets = bond->params.arp_targets; 370 struct list_head *iter; 371 struct slave *slave; 372 unsigned long *targets_rx; 373 int ind, i; 374 375 if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) { 376 pr_err("%s: invalid ARP target %pI4 specified for removal\n", 377 bond->dev->name, &target); 378 return -EINVAL; 379 } 380 381 ind = bond_get_targets_ip(targets, target); 382 if (ind == -1) { 383 pr_err("%s: unable to remove nonexistent ARP target %pI4.\n", 384 bond->dev->name, &target); 385 return -EINVAL; 386 } 387 388 if (ind == 0 && !targets[1] && bond->params.arp_interval) 389 pr_warn("%s: removing last arp target with arp_interval on\n", 390 bond->dev->name); 391 392 pr_info("%s: removing ARP target %pI4.\n", bond->dev->name, 393 &target); 394 395 /* not to race with bond_arp_rcv */ 396 write_lock_bh(&bond->lock); 397 398 bond_for_each_slave(bond, slave, iter) { 399 targets_rx = slave->target_last_arp_rx; 400 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++) 401 targets_rx[i] = targets_rx[i+1]; 402 targets_rx[i] = 0; 403 } 404 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++) 405 targets[i] = targets[i+1]; 406 targets[i] = 0; 407 408 write_unlock_bh(&bond->lock); 409 410 return 0; 411 } 412 413 int bond_option_arp_ip_targets_set(struct bonding *bond, __be32 *targets, 414 int count) 415 { 416 int i, ret = 0; 417 418 /* not to race with bond_arp_rcv */ 419 write_lock_bh(&bond->lock); 420 421 /* clear table */ 422 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) 423 _bond_options_arp_ip_target_set(bond, i, 0, 0); 424 425 if (count == 0 && bond->params.arp_interval) 426 pr_warn("%s: removing last arp target with arp_interval on\n", 427 bond->dev->name); 428 429 for (i = 0; i < count; i++) { 430 ret = _bond_option_arp_ip_target_add(bond, targets[i]); 431 if (ret) 432 break; 433 } 434 435 write_unlock_bh(&bond->lock); 436 return ret; 437 } 438 439 int bond_option_arp_validate_set(struct bonding *bond, int arp_validate) 440 { 441 if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) { 442 pr_err("%s: arp_validate only supported in active-backup mode.\n", 443 bond->dev->name); 444 return -EINVAL; 445 } 446 pr_info("%s: setting arp_validate to %s (%d).\n", 447 bond->dev->name, arp_validate_tbl[arp_validate].modename, 448 arp_validate); 449 450 if (bond->dev->flags & IFF_UP) { 451 if (!arp_validate) 452 bond->recv_probe = NULL; 453 else if (bond->params.arp_interval) 454 bond->recv_probe = bond_arp_rcv; 455 } 456 bond->params.arp_validate = arp_validate; 457 458 return 0; 459 } 460 461 int bond_option_arp_all_targets_set(struct bonding *bond, int arp_all_targets) 462 { 463 pr_info("%s: setting arp_all_targets to %s (%d).\n", 464 bond->dev->name, arp_all_targets_tbl[arp_all_targets].modename, 465 arp_all_targets); 466 467 bond->params.arp_all_targets = arp_all_targets; 468 469 return 0; 470 } 471 472 int bond_option_primary_set(struct bonding *bond, const char *primary) 473 { 474 struct list_head *iter; 475 struct slave *slave; 476 int err = 0; 477 478 block_netpoll_tx(); 479 read_lock(&bond->lock); 480 write_lock_bh(&bond->curr_slave_lock); 481 482 if (!USES_PRIMARY(bond->params.mode)) { 483 pr_err("%s: Unable to set primary slave; %s is in mode %d\n", 484 bond->dev->name, bond->dev->name, bond->params.mode); 485 err = -EINVAL; 486 goto out; 487 } 488 489 /* check to see if we are clearing primary */ 490 if (!strlen(primary)) { 491 pr_info("%s: Setting primary slave to None.\n", 492 bond->dev->name); 493 bond->primary_slave = NULL; 494 memset(bond->params.primary, 0, sizeof(bond->params.primary)); 495 bond_select_active_slave(bond); 496 goto out; 497 } 498 499 bond_for_each_slave(bond, slave, iter) { 500 if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) { 501 pr_info("%s: Setting %s as primary slave.\n", 502 bond->dev->name, slave->dev->name); 503 bond->primary_slave = slave; 504 strcpy(bond->params.primary, slave->dev->name); 505 bond_select_active_slave(bond); 506 goto out; 507 } 508 } 509 510 strncpy(bond->params.primary, primary, IFNAMSIZ); 511 bond->params.primary[IFNAMSIZ - 1] = 0; 512 513 pr_info("%s: Recording %s as primary, but it has not been enslaved to %s yet.\n", 514 bond->dev->name, primary, bond->dev->name); 515 516 out: 517 write_unlock_bh(&bond->curr_slave_lock); 518 read_unlock(&bond->lock); 519 unblock_netpoll_tx(); 520 521 return err; 522 } 523 524 int bond_option_primary_reselect_set(struct bonding *bond, int primary_reselect) 525 { 526 bond->params.primary_reselect = primary_reselect; 527 pr_info("%s: setting primary_reselect to %s (%d).\n", 528 bond->dev->name, pri_reselect_tbl[primary_reselect].modename, 529 primary_reselect); 530 531 block_netpoll_tx(); 532 write_lock_bh(&bond->curr_slave_lock); 533 bond_select_active_slave(bond); 534 write_unlock_bh(&bond->curr_slave_lock); 535 unblock_netpoll_tx(); 536 537 return 0; 538 } 539 540 int bond_option_fail_over_mac_set(struct bonding *bond, int fail_over_mac) 541 { 542 if (bond_has_slaves(bond)) { 543 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n", 544 bond->dev->name); 545 return -EPERM; 546 } 547 548 bond->params.fail_over_mac = fail_over_mac; 549 pr_info("%s: Setting fail_over_mac to %s (%d).\n", 550 bond->dev->name, fail_over_mac_tbl[fail_over_mac].modename, 551 fail_over_mac); 552 553 return 0; 554 } 555 556 int bond_option_xmit_hash_policy_set(struct bonding *bond, int xmit_hash_policy) 557 { 558 bond->params.xmit_policy = xmit_hash_policy; 559 pr_info("%s: setting xmit hash policy to %s (%d).\n", 560 bond->dev->name, 561 xmit_hashtype_tbl[xmit_hash_policy].modename, xmit_hash_policy); 562 563 return 0; 564 } 565 566 int bond_option_resend_igmp_set(struct bonding *bond, int resend_igmp) 567 { 568 if (resend_igmp < 0 || resend_igmp > 255) { 569 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n", 570 bond->dev->name, resend_igmp); 571 return -EINVAL; 572 } 573 574 bond->params.resend_igmp = resend_igmp; 575 pr_info("%s: Setting resend_igmp to %d.\n", 576 bond->dev->name, resend_igmp); 577 578 return 0; 579 } 580 581 int bond_option_num_peer_notif_set(struct bonding *bond, int num_peer_notif) 582 { 583 bond->params.num_peer_notif = num_peer_notif; 584 return 0; 585 } 586 587 int bond_option_all_slaves_active_set(struct bonding *bond, 588 int all_slaves_active) 589 { 590 struct list_head *iter; 591 struct slave *slave; 592 593 if (all_slaves_active == bond->params.all_slaves_active) 594 return 0; 595 596 if ((all_slaves_active == 0) || (all_slaves_active == 1)) { 597 bond->params.all_slaves_active = all_slaves_active; 598 } else { 599 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n", 600 bond->dev->name, all_slaves_active); 601 return -EINVAL; 602 } 603 604 bond_for_each_slave(bond, slave, iter) { 605 if (!bond_is_active_slave(slave)) { 606 if (all_slaves_active) 607 slave->inactive = 0; 608 else 609 slave->inactive = 1; 610 } 611 } 612 613 return 0; 614 } 615 616 int bond_option_min_links_set(struct bonding *bond, int min_links) 617 { 618 pr_info("%s: Setting min links value to %u\n", 619 bond->dev->name, min_links); 620 bond->params.min_links = min_links; 621 622 return 0; 623 } 624 625 int bond_option_lp_interval_set(struct bonding *bond, int lp_interval) 626 { 627 if (lp_interval <= 0) { 628 pr_err("%s: lp_interval must be between 1 and %d\n", 629 bond->dev->name, INT_MAX); 630 return -EINVAL; 631 } 632 633 bond->params.lp_interval = lp_interval; 634 635 return 0; 636 } 637 638 int bond_option_packets_per_slave_set(struct bonding *bond, 639 int packets_per_slave) 640 { 641 if (packets_per_slave < 0 || packets_per_slave > USHRT_MAX) { 642 pr_err("%s: packets_per_slave must be between 0 and %u\n", 643 bond->dev->name, USHRT_MAX); 644 return -EINVAL; 645 } 646 647 if (bond->params.mode != BOND_MODE_ROUNDROBIN) 648 pr_warn("%s: Warning: packets_per_slave has effect only in balance-rr mode\n", 649 bond->dev->name); 650 651 if (packets_per_slave > 1) 652 bond->params.packets_per_slave = 653 reciprocal_value(packets_per_slave); 654 else 655 bond->params.packets_per_slave = packets_per_slave; 656 657 return 0; 658 } 659