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/ctype.h> 20 #include <linux/inet.h> 21 #include "bonding.h" 22 23 static struct bond_opt_value bond_mode_tbl[] = { 24 { "balance-rr", BOND_MODE_ROUNDROBIN, BOND_VALFLAG_DEFAULT}, 25 { "active-backup", BOND_MODE_ACTIVEBACKUP, 0}, 26 { "balance-xor", BOND_MODE_XOR, 0}, 27 { "broadcast", BOND_MODE_BROADCAST, 0}, 28 { "802.3ad", BOND_MODE_8023AD, 0}, 29 { "balance-tlb", BOND_MODE_TLB, 0}, 30 { "balance-alb", BOND_MODE_ALB, 0}, 31 { NULL, -1, 0}, 32 }; 33 34 static struct bond_opt_value bond_pps_tbl[] = { 35 { "default", 1, BOND_VALFLAG_DEFAULT}, 36 { "maxval", USHRT_MAX, BOND_VALFLAG_MAX}, 37 { NULL, -1, 0}, 38 }; 39 40 static struct bond_opt_value bond_xmit_hashtype_tbl[] = { 41 { "layer2", BOND_XMIT_POLICY_LAYER2, BOND_VALFLAG_DEFAULT}, 42 { "layer3+4", BOND_XMIT_POLICY_LAYER34, 0}, 43 { "layer2+3", BOND_XMIT_POLICY_LAYER23, 0}, 44 { "encap2+3", BOND_XMIT_POLICY_ENCAP23, 0}, 45 { "encap3+4", BOND_XMIT_POLICY_ENCAP34, 0}, 46 { NULL, -1, 0}, 47 }; 48 49 static struct bond_opt_value bond_arp_validate_tbl[] = { 50 { "none", BOND_ARP_VALIDATE_NONE, BOND_VALFLAG_DEFAULT}, 51 { "active", BOND_ARP_VALIDATE_ACTIVE, 0}, 52 { "backup", BOND_ARP_VALIDATE_BACKUP, 0}, 53 { "all", BOND_ARP_VALIDATE_ALL, 0}, 54 { NULL, -1, 0}, 55 }; 56 57 static struct bond_opt_value bond_arp_all_targets_tbl[] = { 58 { "any", BOND_ARP_TARGETS_ANY, BOND_VALFLAG_DEFAULT}, 59 { "all", BOND_ARP_TARGETS_ALL, 0}, 60 { NULL, -1, 0}, 61 }; 62 63 static struct bond_opt_value bond_fail_over_mac_tbl[] = { 64 { "none", BOND_FOM_NONE, BOND_VALFLAG_DEFAULT}, 65 { "active", BOND_FOM_ACTIVE, 0}, 66 { "follow", BOND_FOM_FOLLOW, 0}, 67 { NULL, -1, 0}, 68 }; 69 70 static struct bond_opt_value bond_intmax_tbl[] = { 71 { "off", 0, BOND_VALFLAG_DEFAULT}, 72 { "maxval", INT_MAX, BOND_VALFLAG_MAX}, 73 }; 74 75 static struct bond_opt_value bond_lacp_rate_tbl[] = { 76 { "slow", AD_LACP_SLOW, 0}, 77 { "fast", AD_LACP_FAST, 0}, 78 { NULL, -1, 0}, 79 }; 80 81 static struct bond_opt_value bond_ad_select_tbl[] = { 82 { "stable", BOND_AD_STABLE, BOND_VALFLAG_DEFAULT}, 83 { "bandwidth", BOND_AD_BANDWIDTH, 0}, 84 { "count", BOND_AD_COUNT, 0}, 85 { NULL, -1, 0}, 86 }; 87 88 static struct bond_opt_value bond_num_peer_notif_tbl[] = { 89 { "off", 0, 0}, 90 { "maxval", 255, BOND_VALFLAG_MAX}, 91 { "default", 1, BOND_VALFLAG_DEFAULT}, 92 { NULL, -1, 0} 93 }; 94 95 static struct bond_opt_value bond_primary_reselect_tbl[] = { 96 { "always", BOND_PRI_RESELECT_ALWAYS, BOND_VALFLAG_DEFAULT}, 97 { "better", BOND_PRI_RESELECT_BETTER, 0}, 98 { "failure", BOND_PRI_RESELECT_FAILURE, 0}, 99 { NULL, -1}, 100 }; 101 102 static struct bond_opt_value bond_use_carrier_tbl[] = { 103 { "off", 0, 0}, 104 { "on", 1, BOND_VALFLAG_DEFAULT}, 105 { NULL, -1, 0} 106 }; 107 108 static struct bond_opt_value bond_all_slaves_active_tbl[] = { 109 { "off", 0, BOND_VALFLAG_DEFAULT}, 110 { "on", 1, 0}, 111 { NULL, -1, 0} 112 }; 113 114 static struct bond_opt_value bond_resend_igmp_tbl[] = { 115 { "off", 0, 0}, 116 { "maxval", 255, BOND_VALFLAG_MAX}, 117 { "default", 1, BOND_VALFLAG_DEFAULT}, 118 { NULL, -1, 0} 119 }; 120 121 static struct bond_opt_value bond_lp_interval_tbl[] = { 122 { "minval", 1, BOND_VALFLAG_MIN | BOND_VALFLAG_DEFAULT}, 123 { "maxval", INT_MAX, BOND_VALFLAG_MAX}, 124 }; 125 126 static struct bond_option bond_opts[] = { 127 [BOND_OPT_MODE] = { 128 .id = BOND_OPT_MODE, 129 .name = "mode", 130 .desc = "bond device mode", 131 .flags = BOND_OPTFLAG_NOSLAVES | BOND_OPTFLAG_IFDOWN, 132 .values = bond_mode_tbl, 133 .set = bond_option_mode_set 134 }, 135 [BOND_OPT_PACKETS_PER_SLAVE] = { 136 .id = BOND_OPT_PACKETS_PER_SLAVE, 137 .name = "packets_per_slave", 138 .desc = "Packets to send per slave in RR mode", 139 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ROUNDROBIN)), 140 .values = bond_pps_tbl, 141 .set = bond_option_pps_set 142 }, 143 [BOND_OPT_XMIT_HASH] = { 144 .id = BOND_OPT_XMIT_HASH, 145 .name = "xmit_hash_policy", 146 .desc = "balance-xor and 802.3ad hashing method", 147 .values = bond_xmit_hashtype_tbl, 148 .set = bond_option_xmit_hash_policy_set 149 }, 150 [BOND_OPT_ARP_VALIDATE] = { 151 .id = BOND_OPT_ARP_VALIDATE, 152 .name = "arp_validate", 153 .desc = "validate src/dst of ARP probes", 154 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP)), 155 .values = bond_arp_validate_tbl, 156 .set = bond_option_arp_validate_set 157 }, 158 [BOND_OPT_ARP_ALL_TARGETS] = { 159 .id = BOND_OPT_ARP_ALL_TARGETS, 160 .name = "arp_all_targets", 161 .desc = "fail on any/all arp targets timeout", 162 .values = bond_arp_all_targets_tbl, 163 .set = bond_option_arp_all_targets_set 164 }, 165 [BOND_OPT_FAIL_OVER_MAC] = { 166 .id = BOND_OPT_FAIL_OVER_MAC, 167 .name = "fail_over_mac", 168 .desc = "For active-backup, do not set all slaves to the same MAC", 169 .flags = BOND_OPTFLAG_NOSLAVES, 170 .values = bond_fail_over_mac_tbl, 171 .set = bond_option_fail_over_mac_set 172 }, 173 [BOND_OPT_ARP_INTERVAL] = { 174 .id = BOND_OPT_ARP_INTERVAL, 175 .name = "arp_interval", 176 .desc = "arp interval in milliseconds", 177 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) | 178 BIT(BOND_MODE_ALB), 179 .values = bond_intmax_tbl, 180 .set = bond_option_arp_interval_set 181 }, 182 [BOND_OPT_ARP_TARGETS] = { 183 .id = BOND_OPT_ARP_TARGETS, 184 .name = "arp_ip_target", 185 .desc = "arp targets in n.n.n.n form", 186 .flags = BOND_OPTFLAG_RAWVAL, 187 .set = bond_option_arp_ip_targets_set 188 }, 189 [BOND_OPT_DOWNDELAY] = { 190 .id = BOND_OPT_DOWNDELAY, 191 .name = "downdelay", 192 .desc = "Delay before considering link down, in milliseconds", 193 .values = bond_intmax_tbl, 194 .set = bond_option_downdelay_set 195 }, 196 [BOND_OPT_UPDELAY] = { 197 .id = BOND_OPT_UPDELAY, 198 .name = "updelay", 199 .desc = "Delay before considering link up, in milliseconds", 200 .values = bond_intmax_tbl, 201 .set = bond_option_updelay_set 202 }, 203 [BOND_OPT_LACP_RATE] = { 204 .id = BOND_OPT_LACP_RATE, 205 .name = "lacp_rate", 206 .desc = "LACPDU tx rate to request from 802.3ad partner", 207 .flags = BOND_OPTFLAG_IFDOWN, 208 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)), 209 .values = bond_lacp_rate_tbl, 210 .set = bond_option_lacp_rate_set 211 }, 212 [BOND_OPT_MINLINKS] = { 213 .id = BOND_OPT_MINLINKS, 214 .name = "min_links", 215 .desc = "Minimum number of available links before turning on carrier", 216 .values = bond_intmax_tbl, 217 .set = bond_option_min_links_set 218 }, 219 [BOND_OPT_AD_SELECT] = { 220 .id = BOND_OPT_AD_SELECT, 221 .name = "ad_select", 222 .desc = "803.ad aggregation selection logic", 223 .flags = BOND_OPTFLAG_IFDOWN, 224 .values = bond_ad_select_tbl, 225 .set = bond_option_ad_select_set 226 }, 227 [BOND_OPT_NUM_PEER_NOTIF] = { 228 .id = BOND_OPT_NUM_PEER_NOTIF, 229 .name = "num_unsol_na", 230 .desc = "Number of peer notifications to send on failover event", 231 .values = bond_num_peer_notif_tbl, 232 .set = bond_option_num_peer_notif_set 233 }, 234 [BOND_OPT_MIIMON] = { 235 .id = BOND_OPT_MIIMON, 236 .name = "miimon", 237 .desc = "Link check interval in milliseconds", 238 .values = bond_intmax_tbl, 239 .set = bond_option_miimon_set 240 }, 241 [BOND_OPT_PRIMARY] = { 242 .id = BOND_OPT_PRIMARY, 243 .name = "primary", 244 .desc = "Primary network device to use", 245 .flags = BOND_OPTFLAG_RAWVAL, 246 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) | 247 BIT(BOND_MODE_TLB) | 248 BIT(BOND_MODE_ALB)), 249 .set = bond_option_primary_set 250 }, 251 [BOND_OPT_PRIMARY_RESELECT] = { 252 .id = BOND_OPT_PRIMARY_RESELECT, 253 .name = "primary_reselect", 254 .desc = "Reselect primary slave once it comes up", 255 .values = bond_primary_reselect_tbl, 256 .set = bond_option_primary_reselect_set 257 }, 258 [BOND_OPT_USE_CARRIER] = { 259 .id = BOND_OPT_USE_CARRIER, 260 .name = "use_carrier", 261 .desc = "Use netif_carrier_ok (vs MII ioctls) in miimon", 262 .values = bond_use_carrier_tbl, 263 .set = bond_option_use_carrier_set 264 }, 265 [BOND_OPT_ACTIVE_SLAVE] = { 266 .id = BOND_OPT_ACTIVE_SLAVE, 267 .name = "active_slave", 268 .desc = "Currently active slave", 269 .flags = BOND_OPTFLAG_RAWVAL, 270 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) | 271 BIT(BOND_MODE_TLB) | 272 BIT(BOND_MODE_ALB)), 273 .set = bond_option_active_slave_set 274 }, 275 [BOND_OPT_QUEUE_ID] = { 276 .id = BOND_OPT_QUEUE_ID, 277 .name = "queue_id", 278 .desc = "Set queue id of a slave", 279 .flags = BOND_OPTFLAG_RAWVAL, 280 .set = bond_option_queue_id_set 281 }, 282 [BOND_OPT_ALL_SLAVES_ACTIVE] = { 283 .id = BOND_OPT_ALL_SLAVES_ACTIVE, 284 .name = "all_slaves_active", 285 .desc = "Keep all frames received on an interface by setting active flag for all slaves", 286 .values = bond_all_slaves_active_tbl, 287 .set = bond_option_all_slaves_active_set 288 }, 289 [BOND_OPT_RESEND_IGMP] = { 290 .id = BOND_OPT_RESEND_IGMP, 291 .name = "resend_igmp", 292 .desc = "Number of IGMP membership reports to send on link failure", 293 .values = bond_resend_igmp_tbl, 294 .set = bond_option_resend_igmp_set 295 }, 296 [BOND_OPT_LP_INTERVAL] = { 297 .id = BOND_OPT_LP_INTERVAL, 298 .name = "lp_interval", 299 .desc = "The number of seconds between instances where the bonding driver sends learning packets to each slave's peer switch", 300 .values = bond_lp_interval_tbl, 301 .set = bond_option_lp_interval_set 302 }, 303 [BOND_OPT_SLAVES] = { 304 .id = BOND_OPT_SLAVES, 305 .name = "slaves", 306 .desc = "Slave membership management", 307 .flags = BOND_OPTFLAG_RAWVAL, 308 .set = bond_option_slaves_set 309 }, 310 { } 311 }; 312 313 /* Searches for a value in opt's values[] table */ 314 struct bond_opt_value *bond_opt_get_val(unsigned int option, u64 val) 315 { 316 struct bond_option *opt; 317 int i; 318 319 opt = bond_opt_get(option); 320 if (WARN_ON(!opt)) 321 return NULL; 322 for (i = 0; opt->values && opt->values[i].string; i++) 323 if (opt->values[i].value == val) 324 return &opt->values[i]; 325 326 return NULL; 327 } 328 329 /* Searches for a value in opt's values[] table which matches the flagmask */ 330 static struct bond_opt_value *bond_opt_get_flags(const struct bond_option *opt, 331 u32 flagmask) 332 { 333 int i; 334 335 for (i = 0; opt->values && opt->values[i].string; i++) 336 if (opt->values[i].flags & flagmask) 337 return &opt->values[i]; 338 339 return NULL; 340 } 341 342 /* If maxval is missing then there's no range to check. In case minval is 343 * missing then it's considered to be 0. 344 */ 345 static bool bond_opt_check_range(const struct bond_option *opt, u64 val) 346 { 347 struct bond_opt_value *minval, *maxval; 348 349 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN); 350 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX); 351 if (!maxval || (minval && val < minval->value) || val > maxval->value) 352 return false; 353 354 return true; 355 } 356 357 /** 358 * bond_opt_parse - parse option value 359 * @opt: the option to parse against 360 * @val: value to parse 361 * 362 * This function tries to extract the value from @val and check if it's 363 * a possible match for the option and returns NULL if a match isn't found, 364 * or the struct_opt_value that matched. It also strips the new line from 365 * @val->string if it's present. 366 */ 367 struct bond_opt_value *bond_opt_parse(const struct bond_option *opt, 368 struct bond_opt_value *val) 369 { 370 char *p, valstr[BOND_OPT_MAX_NAMELEN + 1] = { 0, }; 371 struct bond_opt_value *tbl, *ret = NULL; 372 bool checkval; 373 int i, rv; 374 375 /* No parsing if the option wants a raw val */ 376 if (opt->flags & BOND_OPTFLAG_RAWVAL) 377 return val; 378 379 tbl = opt->values; 380 if (!tbl) 381 goto out; 382 383 /* ULLONG_MAX is used to bypass string processing */ 384 checkval = val->value != ULLONG_MAX; 385 if (!checkval) { 386 if (!val->string) 387 goto out; 388 p = strchr(val->string, '\n'); 389 if (p) 390 *p = '\0'; 391 for (p = val->string; *p; p++) 392 if (!(isdigit(*p) || isspace(*p))) 393 break; 394 /* The following code extracts the string to match or the value 395 * and sets checkval appropriately 396 */ 397 if (*p) { 398 rv = sscanf(val->string, "%32s", valstr); 399 } else { 400 rv = sscanf(val->string, "%llu", &val->value); 401 checkval = true; 402 } 403 if (!rv) 404 goto out; 405 } 406 407 for (i = 0; tbl[i].string; i++) { 408 /* Check for exact match */ 409 if (checkval) { 410 if (val->value == tbl[i].value) 411 ret = &tbl[i]; 412 } else { 413 if (!strcmp(valstr, "default") && 414 (tbl[i].flags & BOND_VALFLAG_DEFAULT)) 415 ret = &tbl[i]; 416 417 if (!strcmp(valstr, tbl[i].string)) 418 ret = &tbl[i]; 419 } 420 /* Found an exact match */ 421 if (ret) 422 goto out; 423 } 424 /* Possible range match */ 425 if (checkval && bond_opt_check_range(opt, val->value)) 426 ret = val; 427 out: 428 return ret; 429 } 430 431 /* Check opt's dependencies against bond mode and currently set options */ 432 static int bond_opt_check_deps(struct bonding *bond, 433 const struct bond_option *opt) 434 { 435 struct bond_params *params = &bond->params; 436 437 if (test_bit(params->mode, &opt->unsuppmodes)) 438 return -EACCES; 439 if ((opt->flags & BOND_OPTFLAG_NOSLAVES) && bond_has_slaves(bond)) 440 return -ENOTEMPTY; 441 if ((opt->flags & BOND_OPTFLAG_IFDOWN) && (bond->dev->flags & IFF_UP)) 442 return -EBUSY; 443 444 return 0; 445 } 446 447 static void bond_opt_dep_print(struct bonding *bond, 448 const struct bond_option *opt) 449 { 450 struct bond_opt_value *modeval; 451 struct bond_params *params; 452 453 params = &bond->params; 454 modeval = bond_opt_get_val(BOND_OPT_MODE, params->mode); 455 if (test_bit(params->mode, &opt->unsuppmodes)) 456 pr_err("%s: option %s: mode dependency failed, not supported in mode %s(%llu)\n", 457 bond->dev->name, opt->name, 458 modeval->string, modeval->value); 459 } 460 461 static void bond_opt_error_interpret(struct bonding *bond, 462 const struct bond_option *opt, 463 int error, struct bond_opt_value *val) 464 { 465 struct bond_opt_value *minval, *maxval; 466 char *p; 467 468 switch (error) { 469 case -EINVAL: 470 if (val) { 471 if (val->string) { 472 /* sometimes RAWVAL opts may have new lines */ 473 p = strchr(val->string, '\n'); 474 if (p) 475 *p = '\0'; 476 pr_err("%s: option %s: invalid value (%s).\n", 477 bond->dev->name, opt->name, val->string); 478 } else { 479 pr_err("%s: option %s: invalid value (%llu).\n", 480 bond->dev->name, opt->name, val->value); 481 } 482 } 483 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN); 484 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX); 485 if (!maxval) 486 break; 487 pr_err("%s: option %s: allowed values %llu - %llu.\n", 488 bond->dev->name, opt->name, minval ? minval->value : 0, 489 maxval->value); 490 break; 491 case -EACCES: 492 bond_opt_dep_print(bond, opt); 493 break; 494 case -ENOTEMPTY: 495 pr_err("%s: option %s: unable to set because the bond device has slaves.\n", 496 bond->dev->name, opt->name); 497 break; 498 case -EBUSY: 499 pr_err("%s: option %s: unable to set because the bond device is up.\n", 500 bond->dev->name, opt->name); 501 break; 502 default: 503 break; 504 } 505 } 506 507 /** 508 * __bond_opt_set - set a bonding option 509 * @bond: target bond device 510 * @option: option to set 511 * @val: value to set it to 512 * 513 * This function is used to change the bond's option value, it can be 514 * used for both enabling/changing an option and for disabling it. RTNL lock 515 * must be obtained before calling this function. 516 */ 517 int __bond_opt_set(struct bonding *bond, 518 unsigned int option, struct bond_opt_value *val) 519 { 520 struct bond_opt_value *retval = NULL; 521 const struct bond_option *opt; 522 int ret = -ENOENT; 523 524 ASSERT_RTNL(); 525 526 opt = bond_opt_get(option); 527 if (WARN_ON(!val) || WARN_ON(!opt)) 528 goto out; 529 ret = bond_opt_check_deps(bond, opt); 530 if (ret) 531 goto out; 532 retval = bond_opt_parse(opt, val); 533 if (!retval) { 534 ret = -EINVAL; 535 goto out; 536 } 537 ret = opt->set(bond, retval); 538 out: 539 if (ret) 540 bond_opt_error_interpret(bond, opt, ret, val); 541 542 return ret; 543 } 544 545 /** 546 * bond_opt_tryset_rtnl - try to acquire rtnl and call __bond_opt_set 547 * @bond: target bond device 548 * @option: option to set 549 * @buf: value to set it to 550 * 551 * This function tries to acquire RTNL without blocking and if successful 552 * calls __bond_opt_set. It is mainly used for sysfs option manipulation. 553 */ 554 int bond_opt_tryset_rtnl(struct bonding *bond, unsigned int option, char *buf) 555 { 556 struct bond_opt_value optval; 557 int ret; 558 559 if (!rtnl_trylock()) 560 return restart_syscall(); 561 bond_opt_initstr(&optval, buf); 562 ret = __bond_opt_set(bond, option, &optval); 563 rtnl_unlock(); 564 565 return ret; 566 } 567 568 /** 569 * bond_opt_get - get a pointer to an option 570 * @option: option for which to return a pointer 571 * 572 * This function checks if option is valid and if so returns a pointer 573 * to its entry in the bond_opts[] option array. 574 */ 575 struct bond_option *bond_opt_get(unsigned int option) 576 { 577 if (!BOND_OPT_VALID(option)) 578 return NULL; 579 580 return &bond_opts[option]; 581 } 582 583 int bond_option_mode_set(struct bonding *bond, struct bond_opt_value *newval) 584 { 585 if (BOND_NO_USES_ARP(newval->value) && bond->params.arp_interval) { 586 pr_info("%s: %s mode is incompatible with arp monitoring, start mii monitoring\n", 587 bond->dev->name, newval->string); 588 /* disable arp monitoring */ 589 bond->params.arp_interval = 0; 590 /* set miimon to default value */ 591 bond->params.miimon = BOND_DEFAULT_MIIMON; 592 pr_info("%s: Setting MII monitoring interval to %d.\n", 593 bond->dev->name, bond->params.miimon); 594 } 595 596 /* don't cache arp_validate between modes */ 597 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE; 598 bond->params.mode = newval->value; 599 600 return 0; 601 } 602 603 static struct net_device *__bond_option_active_slave_get(struct bonding *bond, 604 struct slave *slave) 605 { 606 return USES_PRIMARY(bond->params.mode) && slave ? slave->dev : NULL; 607 } 608 609 struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond) 610 { 611 struct slave *slave = rcu_dereference(bond->curr_active_slave); 612 613 return __bond_option_active_slave_get(bond, slave); 614 } 615 616 struct net_device *bond_option_active_slave_get(struct bonding *bond) 617 { 618 return __bond_option_active_slave_get(bond, bond->curr_active_slave); 619 } 620 621 int bond_option_active_slave_set(struct bonding *bond, 622 struct bond_opt_value *newval) 623 { 624 char ifname[IFNAMSIZ] = { 0, }; 625 struct net_device *slave_dev; 626 int ret = 0; 627 628 sscanf(newval->string, "%15s", ifname); /* IFNAMSIZ */ 629 if (!strlen(ifname) || newval->string[0] == '\n') { 630 slave_dev = NULL; 631 } else { 632 slave_dev = __dev_get_by_name(dev_net(bond->dev), ifname); 633 if (!slave_dev) 634 return -ENODEV; 635 } 636 637 if (slave_dev) { 638 if (!netif_is_bond_slave(slave_dev)) { 639 pr_err("Device %s is not bonding slave.\n", 640 slave_dev->name); 641 return -EINVAL; 642 } 643 644 if (bond->dev != netdev_master_upper_dev_get(slave_dev)) { 645 pr_err("%s: Device %s is not our slave.\n", 646 bond->dev->name, slave_dev->name); 647 return -EINVAL; 648 } 649 } 650 651 block_netpoll_tx(); 652 write_lock_bh(&bond->curr_slave_lock); 653 654 /* check to see if we are clearing active */ 655 if (!slave_dev) { 656 pr_info("%s: Clearing current active slave.\n", 657 bond->dev->name); 658 rcu_assign_pointer(bond->curr_active_slave, NULL); 659 bond_select_active_slave(bond); 660 } else { 661 struct slave *old_active = bond->curr_active_slave; 662 struct slave *new_active = bond_slave_get_rtnl(slave_dev); 663 664 BUG_ON(!new_active); 665 666 if (new_active == old_active) { 667 /* do nothing */ 668 pr_info("%s: %s is already the current active slave.\n", 669 bond->dev->name, new_active->dev->name); 670 } else { 671 if (old_active && (new_active->link == BOND_LINK_UP) && 672 IS_UP(new_active->dev)) { 673 pr_info("%s: Setting %s as active slave.\n", 674 bond->dev->name, new_active->dev->name); 675 bond_change_active_slave(bond, new_active); 676 } else { 677 pr_err("%s: Could not set %s as active slave; either %s is down or the link is down.\n", 678 bond->dev->name, new_active->dev->name, 679 new_active->dev->name); 680 ret = -EINVAL; 681 } 682 } 683 } 684 685 write_unlock_bh(&bond->curr_slave_lock); 686 unblock_netpoll_tx(); 687 688 return ret; 689 } 690 691 int bond_option_miimon_set(struct bonding *bond, struct bond_opt_value *newval) 692 { 693 pr_info("%s: Setting MII monitoring interval to %llu.\n", 694 bond->dev->name, newval->value); 695 bond->params.miimon = newval->value; 696 if (bond->params.updelay) 697 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n", 698 bond->dev->name, 699 bond->params.updelay * bond->params.miimon); 700 if (bond->params.downdelay) 701 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n", 702 bond->dev->name, 703 bond->params.downdelay * bond->params.miimon); 704 if (newval->value && bond->params.arp_interval) { 705 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n", 706 bond->dev->name); 707 bond->params.arp_interval = 0; 708 if (bond->params.arp_validate) 709 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE; 710 } 711 if (bond->dev->flags & IFF_UP) { 712 /* If the interface is up, we may need to fire off 713 * the MII timer. If the interface is down, the 714 * timer will get fired off when the open function 715 * is called. 716 */ 717 if (!newval->value) { 718 cancel_delayed_work_sync(&bond->mii_work); 719 } else { 720 cancel_delayed_work_sync(&bond->arp_work); 721 queue_delayed_work(bond->wq, &bond->mii_work, 0); 722 } 723 } 724 725 return 0; 726 } 727 728 int bond_option_updelay_set(struct bonding *bond, struct bond_opt_value *newval) 729 { 730 int value = newval->value; 731 732 if (!bond->params.miimon) { 733 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n", 734 bond->dev->name); 735 return -EPERM; 736 } 737 if ((value % bond->params.miimon) != 0) { 738 pr_warn("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n", 739 bond->dev->name, value, 740 bond->params.miimon, 741 (value / bond->params.miimon) * 742 bond->params.miimon); 743 } 744 bond->params.updelay = value / bond->params.miimon; 745 pr_info("%s: Setting up delay to %d.\n", 746 bond->dev->name, 747 bond->params.updelay * bond->params.miimon); 748 749 return 0; 750 } 751 752 int bond_option_downdelay_set(struct bonding *bond, 753 struct bond_opt_value *newval) 754 { 755 int value = newval->value; 756 757 if (!bond->params.miimon) { 758 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n", 759 bond->dev->name); 760 return -EPERM; 761 } 762 if ((value % bond->params.miimon) != 0) { 763 pr_warn("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n", 764 bond->dev->name, value, 765 bond->params.miimon, 766 (value / bond->params.miimon) * 767 bond->params.miimon); 768 } 769 bond->params.downdelay = value / bond->params.miimon; 770 pr_info("%s: Setting down delay to %d.\n", 771 bond->dev->name, 772 bond->params.downdelay * bond->params.miimon); 773 774 return 0; 775 } 776 777 int bond_option_use_carrier_set(struct bonding *bond, 778 struct bond_opt_value *newval) 779 { 780 pr_info("%s: Setting use_carrier to %llu.\n", 781 bond->dev->name, newval->value); 782 bond->params.use_carrier = newval->value; 783 784 return 0; 785 } 786 787 int bond_option_arp_interval_set(struct bonding *bond, 788 struct bond_opt_value *newval) 789 { 790 pr_info("%s: Setting ARP monitoring interval to %llu.\n", 791 bond->dev->name, newval->value); 792 bond->params.arp_interval = newval->value; 793 if (newval->value) { 794 if (bond->params.miimon) { 795 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n", 796 bond->dev->name, bond->dev->name); 797 bond->params.miimon = 0; 798 } 799 if (!bond->params.arp_targets[0]) 800 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n", 801 bond->dev->name); 802 } 803 if (bond->dev->flags & IFF_UP) { 804 /* If the interface is up, we may need to fire off 805 * the ARP timer. If the interface is down, the 806 * timer will get fired off when the open function 807 * is called. 808 */ 809 if (!newval->value) { 810 if (bond->params.arp_validate) 811 bond->recv_probe = NULL; 812 cancel_delayed_work_sync(&bond->arp_work); 813 } else { 814 /* arp_validate can be set only in active-backup mode */ 815 if (bond->params.arp_validate) 816 bond->recv_probe = bond_arp_rcv; 817 cancel_delayed_work_sync(&bond->mii_work); 818 queue_delayed_work(bond->wq, &bond->arp_work, 0); 819 } 820 } 821 822 return 0; 823 } 824 825 static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot, 826 __be32 target, 827 unsigned long last_rx) 828 { 829 __be32 *targets = bond->params.arp_targets; 830 struct list_head *iter; 831 struct slave *slave; 832 833 if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) { 834 bond_for_each_slave(bond, slave, iter) 835 slave->target_last_arp_rx[slot] = last_rx; 836 targets[slot] = target; 837 } 838 } 839 840 static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target) 841 { 842 __be32 *targets = bond->params.arp_targets; 843 int ind; 844 845 if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) { 846 pr_err("%s: invalid ARP target %pI4 specified for addition\n", 847 bond->dev->name, &target); 848 return -EINVAL; 849 } 850 851 if (bond_get_targets_ip(targets, target) != -1) { /* dup */ 852 pr_err("%s: ARP target %pI4 is already present\n", 853 bond->dev->name, &target); 854 return -EINVAL; 855 } 856 857 ind = bond_get_targets_ip(targets, 0); /* first free slot */ 858 if (ind == -1) { 859 pr_err("%s: ARP target table is full!\n", 860 bond->dev->name); 861 return -EINVAL; 862 } 863 864 pr_info("%s: adding ARP target %pI4.\n", bond->dev->name, &target); 865 866 _bond_options_arp_ip_target_set(bond, ind, target, jiffies); 867 868 return 0; 869 } 870 871 int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target) 872 { 873 int ret; 874 875 /* not to race with bond_arp_rcv */ 876 write_lock_bh(&bond->lock); 877 ret = _bond_option_arp_ip_target_add(bond, target); 878 write_unlock_bh(&bond->lock); 879 880 return ret; 881 } 882 883 int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target) 884 { 885 __be32 *targets = bond->params.arp_targets; 886 struct list_head *iter; 887 struct slave *slave; 888 unsigned long *targets_rx; 889 int ind, i; 890 891 if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) { 892 pr_err("%s: invalid ARP target %pI4 specified for removal\n", 893 bond->dev->name, &target); 894 return -EINVAL; 895 } 896 897 ind = bond_get_targets_ip(targets, target); 898 if (ind == -1) { 899 pr_err("%s: unable to remove nonexistent ARP target %pI4.\n", 900 bond->dev->name, &target); 901 return -EINVAL; 902 } 903 904 if (ind == 0 && !targets[1] && bond->params.arp_interval) 905 pr_warn("%s: removing last arp target with arp_interval on\n", 906 bond->dev->name); 907 908 pr_info("%s: removing ARP target %pI4.\n", bond->dev->name, 909 &target); 910 911 /* not to race with bond_arp_rcv */ 912 write_lock_bh(&bond->lock); 913 914 bond_for_each_slave(bond, slave, iter) { 915 targets_rx = slave->target_last_arp_rx; 916 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++) 917 targets_rx[i] = targets_rx[i+1]; 918 targets_rx[i] = 0; 919 } 920 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++) 921 targets[i] = targets[i+1]; 922 targets[i] = 0; 923 924 write_unlock_bh(&bond->lock); 925 926 return 0; 927 } 928 929 void bond_option_arp_ip_targets_clear(struct bonding *bond) 930 { 931 int i; 932 933 /* not to race with bond_arp_rcv */ 934 write_lock_bh(&bond->lock); 935 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) 936 _bond_options_arp_ip_target_set(bond, i, 0, 0); 937 write_unlock_bh(&bond->lock); 938 } 939 940 int bond_option_arp_ip_targets_set(struct bonding *bond, 941 struct bond_opt_value *newval) 942 { 943 int ret = -EPERM; 944 __be32 target; 945 946 if (newval->string) { 947 if (!in4_pton(newval->string+1, -1, (u8 *)&target, -1, NULL)) { 948 pr_err("%s: invalid ARP target %pI4 specified\n", 949 bond->dev->name, &target); 950 return ret; 951 } 952 if (newval->string[0] == '+') 953 ret = bond_option_arp_ip_target_add(bond, target); 954 else if (newval->string[0] == '-') 955 ret = bond_option_arp_ip_target_rem(bond, target); 956 else 957 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n", 958 bond->dev->name); 959 } else { 960 target = newval->value; 961 ret = bond_option_arp_ip_target_add(bond, target); 962 } 963 964 return ret; 965 } 966 967 int bond_option_arp_validate_set(struct bonding *bond, 968 struct bond_opt_value *newval) 969 { 970 pr_info("%s: setting arp_validate to %s (%llu).\n", 971 bond->dev->name, newval->string, newval->value); 972 973 if (bond->dev->flags & IFF_UP) { 974 if (!newval->value) 975 bond->recv_probe = NULL; 976 else if (bond->params.arp_interval) 977 bond->recv_probe = bond_arp_rcv; 978 } 979 bond->params.arp_validate = newval->value; 980 981 return 0; 982 } 983 984 int bond_option_arp_all_targets_set(struct bonding *bond, 985 struct bond_opt_value *newval) 986 { 987 pr_info("%s: setting arp_all_targets to %s (%llu).\n", 988 bond->dev->name, newval->string, newval->value); 989 bond->params.arp_all_targets = newval->value; 990 991 return 0; 992 } 993 994 int bond_option_primary_set(struct bonding *bond, struct bond_opt_value *newval) 995 { 996 char *p, *primary = newval->string; 997 struct list_head *iter; 998 struct slave *slave; 999 1000 block_netpoll_tx(); 1001 read_lock(&bond->lock); 1002 write_lock_bh(&bond->curr_slave_lock); 1003 1004 p = strchr(primary, '\n'); 1005 if (p) 1006 *p = '\0'; 1007 /* check to see if we are clearing primary */ 1008 if (!strlen(primary)) { 1009 pr_info("%s: Setting primary slave to None.\n", 1010 bond->dev->name); 1011 bond->primary_slave = NULL; 1012 memset(bond->params.primary, 0, sizeof(bond->params.primary)); 1013 bond_select_active_slave(bond); 1014 goto out; 1015 } 1016 1017 bond_for_each_slave(bond, slave, iter) { 1018 if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) { 1019 pr_info("%s: Setting %s as primary slave.\n", 1020 bond->dev->name, slave->dev->name); 1021 bond->primary_slave = slave; 1022 strcpy(bond->params.primary, slave->dev->name); 1023 bond_select_active_slave(bond); 1024 goto out; 1025 } 1026 } 1027 1028 if (bond->primary_slave) { 1029 pr_info("%s: Setting primary slave to None.\n", 1030 bond->dev->name); 1031 bond->primary_slave = NULL; 1032 bond_select_active_slave(bond); 1033 } 1034 strncpy(bond->params.primary, primary, IFNAMSIZ); 1035 bond->params.primary[IFNAMSIZ - 1] = 0; 1036 1037 pr_info("%s: Recording %s as primary, but it has not been enslaved to %s yet.\n", 1038 bond->dev->name, primary, bond->dev->name); 1039 1040 out: 1041 write_unlock_bh(&bond->curr_slave_lock); 1042 read_unlock(&bond->lock); 1043 unblock_netpoll_tx(); 1044 1045 return 0; 1046 } 1047 1048 int bond_option_primary_reselect_set(struct bonding *bond, 1049 struct bond_opt_value *newval) 1050 { 1051 pr_info("%s: setting primary_reselect to %s (%llu).\n", 1052 bond->dev->name, newval->string, newval->value); 1053 bond->params.primary_reselect = newval->value; 1054 1055 block_netpoll_tx(); 1056 write_lock_bh(&bond->curr_slave_lock); 1057 bond_select_active_slave(bond); 1058 write_unlock_bh(&bond->curr_slave_lock); 1059 unblock_netpoll_tx(); 1060 1061 return 0; 1062 } 1063 1064 int bond_option_fail_over_mac_set(struct bonding *bond, 1065 struct bond_opt_value *newval) 1066 { 1067 pr_info("%s: Setting fail_over_mac to %s (%llu).\n", 1068 bond->dev->name, newval->string, newval->value); 1069 bond->params.fail_over_mac = newval->value; 1070 1071 return 0; 1072 } 1073 1074 int bond_option_xmit_hash_policy_set(struct bonding *bond, 1075 struct bond_opt_value *newval) 1076 { 1077 pr_info("%s: setting xmit hash policy to %s (%llu).\n", 1078 bond->dev->name, newval->string, newval->value); 1079 bond->params.xmit_policy = newval->value; 1080 1081 return 0; 1082 } 1083 1084 int bond_option_resend_igmp_set(struct bonding *bond, 1085 struct bond_opt_value *newval) 1086 { 1087 pr_info("%s: Setting resend_igmp to %llu.\n", 1088 bond->dev->name, newval->value); 1089 bond->params.resend_igmp = newval->value; 1090 1091 return 0; 1092 } 1093 1094 int bond_option_num_peer_notif_set(struct bonding *bond, 1095 struct bond_opt_value *newval) 1096 { 1097 bond->params.num_peer_notif = newval->value; 1098 1099 return 0; 1100 } 1101 1102 int bond_option_all_slaves_active_set(struct bonding *bond, 1103 struct bond_opt_value *newval) 1104 { 1105 struct list_head *iter; 1106 struct slave *slave; 1107 1108 if (newval->value == bond->params.all_slaves_active) 1109 return 0; 1110 bond->params.all_slaves_active = newval->value; 1111 bond_for_each_slave(bond, slave, iter) { 1112 if (!bond_is_active_slave(slave)) { 1113 if (newval->value) 1114 slave->inactive = 0; 1115 else 1116 slave->inactive = 1; 1117 } 1118 } 1119 1120 return 0; 1121 } 1122 1123 int bond_option_min_links_set(struct bonding *bond, 1124 struct bond_opt_value *newval) 1125 { 1126 pr_info("%s: Setting min links value to %llu\n", 1127 bond->dev->name, newval->value); 1128 bond->params.min_links = newval->value; 1129 1130 return 0; 1131 } 1132 1133 int bond_option_lp_interval_set(struct bonding *bond, 1134 struct bond_opt_value *newval) 1135 { 1136 bond->params.lp_interval = newval->value; 1137 1138 return 0; 1139 } 1140 1141 int bond_option_pps_set(struct bonding *bond, struct bond_opt_value *newval) 1142 { 1143 bond->params.packets_per_slave = newval->value; 1144 if (newval->value > 0) { 1145 bond->params.reciprocal_packets_per_slave = 1146 reciprocal_value(newval->value); 1147 } else { 1148 /* reciprocal_packets_per_slave is unused if 1149 * packets_per_slave is 0 or 1, just initialize it 1150 */ 1151 bond->params.reciprocal_packets_per_slave = 1152 (struct reciprocal_value) { 0 }; 1153 } 1154 1155 return 0; 1156 } 1157 1158 int bond_option_lacp_rate_set(struct bonding *bond, 1159 struct bond_opt_value *newval) 1160 { 1161 pr_info("%s: Setting LACP rate to %s (%llu).\n", 1162 bond->dev->name, newval->string, newval->value); 1163 bond->params.lacp_fast = newval->value; 1164 bond_3ad_update_lacp_rate(bond); 1165 1166 return 0; 1167 } 1168 1169 int bond_option_ad_select_set(struct bonding *bond, 1170 struct bond_opt_value *newval) 1171 { 1172 pr_info("%s: Setting ad_select to %s (%llu).\n", 1173 bond->dev->name, newval->string, newval->value); 1174 bond->params.ad_select = newval->value; 1175 1176 return 0; 1177 } 1178 1179 int bond_option_queue_id_set(struct bonding *bond, 1180 struct bond_opt_value *newval) 1181 { 1182 struct slave *slave, *update_slave; 1183 struct net_device *sdev; 1184 struct list_head *iter; 1185 char *delim; 1186 int ret = 0; 1187 u16 qid; 1188 1189 /* delim will point to queue id if successful */ 1190 delim = strchr(newval->string, ':'); 1191 if (!delim) 1192 goto err_no_cmd; 1193 1194 /* Terminate string that points to device name and bump it 1195 * up one, so we can read the queue id there. 1196 */ 1197 *delim = '\0'; 1198 if (sscanf(++delim, "%hd\n", &qid) != 1) 1199 goto err_no_cmd; 1200 1201 /* Check buffer length, valid ifname and queue id */ 1202 if (strlen(newval->string) > IFNAMSIZ || 1203 !dev_valid_name(newval->string) || 1204 qid > bond->dev->real_num_tx_queues) 1205 goto err_no_cmd; 1206 1207 /* Get the pointer to that interface if it exists */ 1208 sdev = __dev_get_by_name(dev_net(bond->dev), newval->string); 1209 if (!sdev) 1210 goto err_no_cmd; 1211 1212 /* Search for thes slave and check for duplicate qids */ 1213 update_slave = NULL; 1214 bond_for_each_slave(bond, slave, iter) { 1215 if (sdev == slave->dev) 1216 /* We don't need to check the matching 1217 * slave for dups, since we're overwriting it 1218 */ 1219 update_slave = slave; 1220 else if (qid && qid == slave->queue_id) { 1221 goto err_no_cmd; 1222 } 1223 } 1224 1225 if (!update_slave) 1226 goto err_no_cmd; 1227 1228 /* Actually set the qids for the slave */ 1229 update_slave->queue_id = qid; 1230 1231 out: 1232 return ret; 1233 1234 err_no_cmd: 1235 pr_info("invalid input for queue_id set for %s.\n", 1236 bond->dev->name); 1237 ret = -EPERM; 1238 goto out; 1239 1240 } 1241 1242 int bond_option_slaves_set(struct bonding *bond, struct bond_opt_value *newval) 1243 { 1244 char command[IFNAMSIZ + 1] = { 0, }; 1245 struct net_device *dev; 1246 char *ifname; 1247 int ret; 1248 1249 sscanf(newval->string, "%16s", command); /* IFNAMSIZ*/ 1250 ifname = command + 1; 1251 if ((strlen(command) <= 1) || 1252 !dev_valid_name(ifname)) 1253 goto err_no_cmd; 1254 1255 dev = __dev_get_by_name(dev_net(bond->dev), ifname); 1256 if (!dev) { 1257 pr_info("%s: Interface %s does not exist!\n", 1258 bond->dev->name, ifname); 1259 ret = -ENODEV; 1260 goto out; 1261 } 1262 1263 switch (command[0]) { 1264 case '+': 1265 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name); 1266 ret = bond_enslave(bond->dev, dev); 1267 break; 1268 1269 case '-': 1270 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name); 1271 ret = bond_release(bond->dev, dev); 1272 break; 1273 1274 default: 1275 goto err_no_cmd; 1276 } 1277 1278 out: 1279 return ret; 1280 1281 err_no_cmd: 1282 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n", 1283 bond->dev->name); 1284 ret = -EPERM; 1285 goto out; 1286 } 1287