1 /* 2 * originally based on the dummy device. 3 * 4 * Copyright 1999, Thomas Davis, tadavis@lbl.gov. 5 * Licensed under the GPL. Based on dummy.c, and eql.c devices. 6 * 7 * bonding.c: an Ethernet Bonding driver 8 * 9 * This is useful to talk to a Cisco EtherChannel compatible equipment: 10 * Cisco 5500 11 * Sun Trunking (Solaris) 12 * Alteon AceDirector Trunks 13 * Linux Bonding 14 * and probably many L2 switches ... 15 * 16 * How it works: 17 * ifconfig bond0 ipaddress netmask up 18 * will setup a network device, with an ip address. No mac address 19 * will be assigned at this time. The hw mac address will come from 20 * the first slave bonded to the channel. All slaves will then use 21 * this hw mac address. 22 * 23 * ifconfig bond0 down 24 * will release all slaves, marking them as down. 25 * 26 * ifenslave bond0 eth0 27 * will attach eth0 to bond0 as a slave. eth0 hw mac address will either 28 * a: be used as initial mac address 29 * b: if a hw mac address already is there, eth0's hw mac address 30 * will then be set from bond0. 31 * 32 */ 33 34 #include <linux/kernel.h> 35 #include <linux/module.h> 36 #include <linux/types.h> 37 #include <linux/fcntl.h> 38 #include <linux/interrupt.h> 39 #include <linux/ptrace.h> 40 #include <linux/ioport.h> 41 #include <linux/in.h> 42 #include <net/ip.h> 43 #include <linux/ip.h> 44 #include <linux/tcp.h> 45 #include <linux/udp.h> 46 #include <linux/slab.h> 47 #include <linux/string.h> 48 #include <linux/init.h> 49 #include <linux/timer.h> 50 #include <linux/socket.h> 51 #include <linux/ctype.h> 52 #include <linux/inet.h> 53 #include <linux/bitops.h> 54 #include <linux/io.h> 55 #include <asm/dma.h> 56 #include <linux/uaccess.h> 57 #include <linux/errno.h> 58 #include <linux/netdevice.h> 59 #include <linux/inetdevice.h> 60 #include <linux/igmp.h> 61 #include <linux/etherdevice.h> 62 #include <linux/skbuff.h> 63 #include <net/sock.h> 64 #include <linux/rtnetlink.h> 65 #include <linux/smp.h> 66 #include <linux/if_ether.h> 67 #include <net/arp.h> 68 #include <linux/mii.h> 69 #include <linux/ethtool.h> 70 #include <linux/if_vlan.h> 71 #include <linux/if_bonding.h> 72 #include <linux/jiffies.h> 73 #include <linux/preempt.h> 74 #include <net/route.h> 75 #include <net/net_namespace.h> 76 #include <net/netns/generic.h> 77 #include <net/pkt_sched.h> 78 #include <linux/rculist.h> 79 #include <net/flow_dissector.h> 80 #include <net/switchdev.h> 81 #include <net/bonding.h> 82 #include <net/bond_3ad.h> 83 #include <net/bond_alb.h> 84 85 #include "bonding_priv.h" 86 87 /*---------------------------- Module parameters ----------------------------*/ 88 89 /* monitor all links that often (in milliseconds). <=0 disables monitoring */ 90 91 static int max_bonds = BOND_DEFAULT_MAX_BONDS; 92 static int tx_queues = BOND_DEFAULT_TX_QUEUES; 93 static int num_peer_notif = 1; 94 static int miimon; 95 static int updelay; 96 static int downdelay; 97 static int use_carrier = 1; 98 static char *mode; 99 static char *primary; 100 static char *primary_reselect; 101 static char *lacp_rate; 102 static int min_links; 103 static char *ad_select; 104 static char *xmit_hash_policy; 105 static int arp_interval; 106 static char *arp_ip_target[BOND_MAX_ARP_TARGETS]; 107 static char *arp_validate; 108 static char *arp_all_targets; 109 static char *fail_over_mac; 110 static int all_slaves_active; 111 static struct bond_params bonding_defaults; 112 static int resend_igmp = BOND_DEFAULT_RESEND_IGMP; 113 static int packets_per_slave = 1; 114 static int lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL; 115 116 module_param(max_bonds, int, 0); 117 MODULE_PARM_DESC(max_bonds, "Max number of bonded devices"); 118 module_param(tx_queues, int, 0); 119 MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)"); 120 module_param_named(num_grat_arp, num_peer_notif, int, 0644); 121 MODULE_PARM_DESC(num_grat_arp, "Number of peer notifications to send on " 122 "failover event (alias of num_unsol_na)"); 123 module_param_named(num_unsol_na, num_peer_notif, int, 0644); 124 MODULE_PARM_DESC(num_unsol_na, "Number of peer notifications to send on " 125 "failover event (alias of num_grat_arp)"); 126 module_param(miimon, int, 0); 127 MODULE_PARM_DESC(miimon, "Link check interval in milliseconds"); 128 module_param(updelay, int, 0); 129 MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds"); 130 module_param(downdelay, int, 0); 131 MODULE_PARM_DESC(downdelay, "Delay before considering link down, " 132 "in milliseconds"); 133 module_param(use_carrier, int, 0); 134 MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; " 135 "0 for off, 1 for on (default)"); 136 module_param(mode, charp, 0); 137 MODULE_PARM_DESC(mode, "Mode of operation; 0 for balance-rr, " 138 "1 for active-backup, 2 for balance-xor, " 139 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, " 140 "6 for balance-alb"); 141 module_param(primary, charp, 0); 142 MODULE_PARM_DESC(primary, "Primary network device to use"); 143 module_param(primary_reselect, charp, 0); 144 MODULE_PARM_DESC(primary_reselect, "Reselect primary slave " 145 "once it comes up; " 146 "0 for always (default), " 147 "1 for only if speed of primary is " 148 "better, " 149 "2 for only on active slave " 150 "failure"); 151 module_param(lacp_rate, charp, 0); 152 MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner; " 153 "0 for slow, 1 for fast"); 154 module_param(ad_select, charp, 0); 155 MODULE_PARM_DESC(ad_select, "802.3ad aggregation selection logic; " 156 "0 for stable (default), 1 for bandwidth, " 157 "2 for count"); 158 module_param(min_links, int, 0); 159 MODULE_PARM_DESC(min_links, "Minimum number of available links before turning on carrier"); 160 161 module_param(xmit_hash_policy, charp, 0); 162 MODULE_PARM_DESC(xmit_hash_policy, "balance-xor and 802.3ad hashing method; " 163 "0 for layer 2 (default), 1 for layer 3+4, " 164 "2 for layer 2+3, 3 for encap layer 2+3, " 165 "4 for encap layer 3+4"); 166 module_param(arp_interval, int, 0); 167 MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds"); 168 module_param_array(arp_ip_target, charp, NULL, 0); 169 MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form"); 170 module_param(arp_validate, charp, 0); 171 MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes; " 172 "0 for none (default), 1 for active, " 173 "2 for backup, 3 for all"); 174 module_param(arp_all_targets, charp, 0); 175 MODULE_PARM_DESC(arp_all_targets, "fail on any/all arp targets timeout; 0 for any (default), 1 for all"); 176 module_param(fail_over_mac, charp, 0); 177 MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to " 178 "the same MAC; 0 for none (default), " 179 "1 for active, 2 for follow"); 180 module_param(all_slaves_active, int, 0); 181 MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface " 182 "by setting active flag for all slaves; " 183 "0 for never (default), 1 for always."); 184 module_param(resend_igmp, int, 0); 185 MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on " 186 "link failure"); 187 module_param(packets_per_slave, int, 0); 188 MODULE_PARM_DESC(packets_per_slave, "Packets to send per slave in balance-rr " 189 "mode; 0 for a random slave, 1 packet per " 190 "slave (default), >1 packets per slave."); 191 module_param(lp_interval, uint, 0); 192 MODULE_PARM_DESC(lp_interval, "The number of seconds between instances where " 193 "the bonding driver sends learning packets to " 194 "each slaves peer switch. The default is 1."); 195 196 /*----------------------------- Global variables ----------------------------*/ 197 198 #ifdef CONFIG_NET_POLL_CONTROLLER 199 atomic_t netpoll_block_tx = ATOMIC_INIT(0); 200 #endif 201 202 unsigned int bond_net_id __read_mostly; 203 204 /*-------------------------- Forward declarations ---------------------------*/ 205 206 static int bond_init(struct net_device *bond_dev); 207 static void bond_uninit(struct net_device *bond_dev); 208 static void bond_get_stats(struct net_device *bond_dev, 209 struct rtnl_link_stats64 *stats); 210 static void bond_slave_arr_handler(struct work_struct *work); 211 static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act, 212 int mod); 213 214 /*---------------------------- General routines -----------------------------*/ 215 216 const char *bond_mode_name(int mode) 217 { 218 static const char *names[] = { 219 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)", 220 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)", 221 [BOND_MODE_XOR] = "load balancing (xor)", 222 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)", 223 [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation", 224 [BOND_MODE_TLB] = "transmit load balancing", 225 [BOND_MODE_ALB] = "adaptive load balancing", 226 }; 227 228 if (mode < BOND_MODE_ROUNDROBIN || mode > BOND_MODE_ALB) 229 return "unknown"; 230 231 return names[mode]; 232 } 233 234 /*---------------------------------- VLAN -----------------------------------*/ 235 236 /** 237 * bond_dev_queue_xmit - Prepare skb for xmit. 238 * 239 * @bond: bond device that got this skb for tx. 240 * @skb: hw accel VLAN tagged skb to transmit 241 * @slave_dev: slave that is supposed to xmit this skbuff 242 */ 243 void bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, 244 struct net_device *slave_dev) 245 { 246 skb->dev = slave_dev; 247 248 BUILD_BUG_ON(sizeof(skb->queue_mapping) != 249 sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping)); 250 skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping; 251 252 if (unlikely(netpoll_tx_running(bond->dev))) 253 bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb); 254 else 255 dev_queue_xmit(skb); 256 } 257 258 /* In the following 2 functions, bond_vlan_rx_add_vid and bond_vlan_rx_kill_vid, 259 * We don't protect the slave list iteration with a lock because: 260 * a. This operation is performed in IOCTL context, 261 * b. The operation is protected by the RTNL semaphore in the 8021q code, 262 * c. Holding a lock with BH disabled while directly calling a base driver 263 * entry point is generally a BAD idea. 264 * 265 * The design of synchronization/protection for this operation in the 8021q 266 * module is good for one or more VLAN devices over a single physical device 267 * and cannot be extended for a teaming solution like bonding, so there is a 268 * potential race condition here where a net device from the vlan group might 269 * be referenced (either by a base driver or the 8021q code) while it is being 270 * removed from the system. However, it turns out we're not making matters 271 * worse, and if it works for regular VLAN usage it will work here too. 272 */ 273 274 /** 275 * bond_vlan_rx_add_vid - Propagates adding an id to slaves 276 * @bond_dev: bonding net device that got called 277 * @vid: vlan id being added 278 */ 279 static int bond_vlan_rx_add_vid(struct net_device *bond_dev, 280 __be16 proto, u16 vid) 281 { 282 struct bonding *bond = netdev_priv(bond_dev); 283 struct slave *slave, *rollback_slave; 284 struct list_head *iter; 285 int res; 286 287 bond_for_each_slave(bond, slave, iter) { 288 res = vlan_vid_add(slave->dev, proto, vid); 289 if (res) 290 goto unwind; 291 } 292 293 return 0; 294 295 unwind: 296 /* unwind to the slave that failed */ 297 bond_for_each_slave(bond, rollback_slave, iter) { 298 if (rollback_slave == slave) 299 break; 300 301 vlan_vid_del(rollback_slave->dev, proto, vid); 302 } 303 304 return res; 305 } 306 307 /** 308 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves 309 * @bond_dev: bonding net device that got called 310 * @vid: vlan id being removed 311 */ 312 static int bond_vlan_rx_kill_vid(struct net_device *bond_dev, 313 __be16 proto, u16 vid) 314 { 315 struct bonding *bond = netdev_priv(bond_dev); 316 struct list_head *iter; 317 struct slave *slave; 318 319 bond_for_each_slave(bond, slave, iter) 320 vlan_vid_del(slave->dev, proto, vid); 321 322 if (bond_is_lb(bond)) 323 bond_alb_clear_vlan(bond, vid); 324 325 return 0; 326 } 327 328 /*------------------------------- Link status -------------------------------*/ 329 330 /* Set the carrier state for the master according to the state of its 331 * slaves. If any slaves are up, the master is up. In 802.3ad mode, 332 * do special 802.3ad magic. 333 * 334 * Returns zero if carrier state does not change, nonzero if it does. 335 */ 336 int bond_set_carrier(struct bonding *bond) 337 { 338 struct list_head *iter; 339 struct slave *slave; 340 341 if (!bond_has_slaves(bond)) 342 goto down; 343 344 if (BOND_MODE(bond) == BOND_MODE_8023AD) 345 return bond_3ad_set_carrier(bond); 346 347 bond_for_each_slave(bond, slave, iter) { 348 if (slave->link == BOND_LINK_UP) { 349 if (!netif_carrier_ok(bond->dev)) { 350 netif_carrier_on(bond->dev); 351 return 1; 352 } 353 return 0; 354 } 355 } 356 357 down: 358 if (netif_carrier_ok(bond->dev)) { 359 netif_carrier_off(bond->dev); 360 return 1; 361 } 362 return 0; 363 } 364 365 /* Get link speed and duplex from the slave's base driver 366 * using ethtool. If for some reason the call fails or the 367 * values are invalid, set speed and duplex to -1, 368 * and return. Return 1 if speed or duplex settings are 369 * UNKNOWN; 0 otherwise. 370 */ 371 static int bond_update_speed_duplex(struct slave *slave) 372 { 373 struct net_device *slave_dev = slave->dev; 374 struct ethtool_link_ksettings ecmd; 375 int res; 376 377 slave->speed = SPEED_UNKNOWN; 378 slave->duplex = DUPLEX_UNKNOWN; 379 380 res = __ethtool_get_link_ksettings(slave_dev, &ecmd); 381 if (res < 0) 382 return 1; 383 if (ecmd.base.speed == 0 || ecmd.base.speed == ((__u32)-1)) 384 return 1; 385 switch (ecmd.base.duplex) { 386 case DUPLEX_FULL: 387 case DUPLEX_HALF: 388 break; 389 default: 390 return 1; 391 } 392 393 slave->speed = ecmd.base.speed; 394 slave->duplex = ecmd.base.duplex; 395 396 return 0; 397 } 398 399 const char *bond_slave_link_status(s8 link) 400 { 401 switch (link) { 402 case BOND_LINK_UP: 403 return "up"; 404 case BOND_LINK_FAIL: 405 return "going down"; 406 case BOND_LINK_DOWN: 407 return "down"; 408 case BOND_LINK_BACK: 409 return "going back"; 410 default: 411 return "unknown"; 412 } 413 } 414 415 /* if <dev> supports MII link status reporting, check its link status. 416 * 417 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(), 418 * depending upon the setting of the use_carrier parameter. 419 * 420 * Return either BMSR_LSTATUS, meaning that the link is up (or we 421 * can't tell and just pretend it is), or 0, meaning that the link is 422 * down. 423 * 424 * If reporting is non-zero, instead of faking link up, return -1 if 425 * both ETHTOOL and MII ioctls fail (meaning the device does not 426 * support them). If use_carrier is set, return whatever it says. 427 * It'd be nice if there was a good way to tell if a driver supports 428 * netif_carrier, but there really isn't. 429 */ 430 static int bond_check_dev_link(struct bonding *bond, 431 struct net_device *slave_dev, int reporting) 432 { 433 const struct net_device_ops *slave_ops = slave_dev->netdev_ops; 434 int (*ioctl)(struct net_device *, struct ifreq *, int); 435 struct ifreq ifr; 436 struct mii_ioctl_data *mii; 437 438 if (!reporting && !netif_running(slave_dev)) 439 return 0; 440 441 if (bond->params.use_carrier) 442 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0; 443 444 /* Try to get link status using Ethtool first. */ 445 if (slave_dev->ethtool_ops->get_link) 446 return slave_dev->ethtool_ops->get_link(slave_dev) ? 447 BMSR_LSTATUS : 0; 448 449 /* Ethtool can't be used, fallback to MII ioctls. */ 450 ioctl = slave_ops->ndo_do_ioctl; 451 if (ioctl) { 452 /* TODO: set pointer to correct ioctl on a per team member 453 * bases to make this more efficient. that is, once 454 * we determine the correct ioctl, we will always 455 * call it and not the others for that team 456 * member. 457 */ 458 459 /* We cannot assume that SIOCGMIIPHY will also read a 460 * register; not all network drivers (e.g., e100) 461 * support that. 462 */ 463 464 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */ 465 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ); 466 mii = if_mii(&ifr); 467 if (ioctl(slave_dev, &ifr, SIOCGMIIPHY) == 0) { 468 mii->reg_num = MII_BMSR; 469 if (ioctl(slave_dev, &ifr, SIOCGMIIREG) == 0) 470 return mii->val_out & BMSR_LSTATUS; 471 } 472 } 473 474 /* If reporting, report that either there's no dev->do_ioctl, 475 * or both SIOCGMIIREG and get_link failed (meaning that we 476 * cannot report link status). If not reporting, pretend 477 * we're ok. 478 */ 479 return reporting ? -1 : BMSR_LSTATUS; 480 } 481 482 /*----------------------------- Multicast list ------------------------------*/ 483 484 /* Push the promiscuity flag down to appropriate slaves */ 485 static int bond_set_promiscuity(struct bonding *bond, int inc) 486 { 487 struct list_head *iter; 488 int err = 0; 489 490 if (bond_uses_primary(bond)) { 491 struct slave *curr_active = rtnl_dereference(bond->curr_active_slave); 492 493 if (curr_active) 494 err = dev_set_promiscuity(curr_active->dev, inc); 495 } else { 496 struct slave *slave; 497 498 bond_for_each_slave(bond, slave, iter) { 499 err = dev_set_promiscuity(slave->dev, inc); 500 if (err) 501 return err; 502 } 503 } 504 return err; 505 } 506 507 /* Push the allmulti flag down to all slaves */ 508 static int bond_set_allmulti(struct bonding *bond, int inc) 509 { 510 struct list_head *iter; 511 int err = 0; 512 513 if (bond_uses_primary(bond)) { 514 struct slave *curr_active = rtnl_dereference(bond->curr_active_slave); 515 516 if (curr_active) 517 err = dev_set_allmulti(curr_active->dev, inc); 518 } else { 519 struct slave *slave; 520 521 bond_for_each_slave(bond, slave, iter) { 522 err = dev_set_allmulti(slave->dev, inc); 523 if (err) 524 return err; 525 } 526 } 527 return err; 528 } 529 530 /* Retrieve the list of registered multicast addresses for the bonding 531 * device and retransmit an IGMP JOIN request to the current active 532 * slave. 533 */ 534 static void bond_resend_igmp_join_requests_delayed(struct work_struct *work) 535 { 536 struct bonding *bond = container_of(work, struct bonding, 537 mcast_work.work); 538 539 if (!rtnl_trylock()) { 540 queue_delayed_work(bond->wq, &bond->mcast_work, 1); 541 return; 542 } 543 call_netdevice_notifiers(NETDEV_RESEND_IGMP, bond->dev); 544 545 if (bond->igmp_retrans > 1) { 546 bond->igmp_retrans--; 547 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5); 548 } 549 rtnl_unlock(); 550 } 551 552 /* Flush bond's hardware addresses from slave */ 553 static void bond_hw_addr_flush(struct net_device *bond_dev, 554 struct net_device *slave_dev) 555 { 556 struct bonding *bond = netdev_priv(bond_dev); 557 558 dev_uc_unsync(slave_dev, bond_dev); 559 dev_mc_unsync(slave_dev, bond_dev); 560 561 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 562 /* del lacpdu mc addr from mc list */ 563 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR; 564 565 dev_mc_del(slave_dev, lacpdu_multicast); 566 } 567 } 568 569 /*--------------------------- Active slave change ---------------------------*/ 570 571 /* Update the hardware address list and promisc/allmulti for the new and 572 * old active slaves (if any). Modes that are not using primary keep all 573 * slaves up date at all times; only the modes that use primary need to call 574 * this function to swap these settings during a failover. 575 */ 576 static void bond_hw_addr_swap(struct bonding *bond, struct slave *new_active, 577 struct slave *old_active) 578 { 579 if (old_active) { 580 if (bond->dev->flags & IFF_PROMISC) 581 dev_set_promiscuity(old_active->dev, -1); 582 583 if (bond->dev->flags & IFF_ALLMULTI) 584 dev_set_allmulti(old_active->dev, -1); 585 586 bond_hw_addr_flush(bond->dev, old_active->dev); 587 } 588 589 if (new_active) { 590 /* FIXME: Signal errors upstream. */ 591 if (bond->dev->flags & IFF_PROMISC) 592 dev_set_promiscuity(new_active->dev, 1); 593 594 if (bond->dev->flags & IFF_ALLMULTI) 595 dev_set_allmulti(new_active->dev, 1); 596 597 netif_addr_lock_bh(bond->dev); 598 dev_uc_sync(new_active->dev, bond->dev); 599 dev_mc_sync(new_active->dev, bond->dev); 600 netif_addr_unlock_bh(bond->dev); 601 } 602 } 603 604 /** 605 * bond_set_dev_addr - clone slave's address to bond 606 * @bond_dev: bond net device 607 * @slave_dev: slave net device 608 * 609 * Should be called with RTNL held. 610 */ 611 static void bond_set_dev_addr(struct net_device *bond_dev, 612 struct net_device *slave_dev) 613 { 614 netdev_dbg(bond_dev, "bond_dev=%p slave_dev=%p slave_dev->name=%s slave_dev->addr_len=%d\n", 615 bond_dev, slave_dev, slave_dev->name, slave_dev->addr_len); 616 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len); 617 bond_dev->addr_assign_type = NET_ADDR_STOLEN; 618 call_netdevice_notifiers(NETDEV_CHANGEADDR, bond_dev); 619 } 620 621 static struct slave *bond_get_old_active(struct bonding *bond, 622 struct slave *new_active) 623 { 624 struct slave *slave; 625 struct list_head *iter; 626 627 bond_for_each_slave(bond, slave, iter) { 628 if (slave == new_active) 629 continue; 630 631 if (ether_addr_equal(bond->dev->dev_addr, slave->dev->dev_addr)) 632 return slave; 633 } 634 635 return NULL; 636 } 637 638 /* bond_do_fail_over_mac 639 * 640 * Perform special MAC address swapping for fail_over_mac settings 641 * 642 * Called with RTNL 643 */ 644 static void bond_do_fail_over_mac(struct bonding *bond, 645 struct slave *new_active, 646 struct slave *old_active) 647 { 648 u8 tmp_mac[MAX_ADDR_LEN]; 649 struct sockaddr_storage ss; 650 int rv; 651 652 switch (bond->params.fail_over_mac) { 653 case BOND_FOM_ACTIVE: 654 if (new_active) 655 bond_set_dev_addr(bond->dev, new_active->dev); 656 break; 657 case BOND_FOM_FOLLOW: 658 /* if new_active && old_active, swap them 659 * if just old_active, do nothing (going to no active slave) 660 * if just new_active, set new_active to bond's MAC 661 */ 662 if (!new_active) 663 return; 664 665 if (!old_active) 666 old_active = bond_get_old_active(bond, new_active); 667 668 if (old_active) { 669 bond_hw_addr_copy(tmp_mac, new_active->dev->dev_addr, 670 new_active->dev->addr_len); 671 bond_hw_addr_copy(ss.__data, 672 old_active->dev->dev_addr, 673 old_active->dev->addr_len); 674 ss.ss_family = new_active->dev->type; 675 } else { 676 bond_hw_addr_copy(ss.__data, bond->dev->dev_addr, 677 bond->dev->addr_len); 678 ss.ss_family = bond->dev->type; 679 } 680 681 rv = dev_set_mac_address(new_active->dev, 682 (struct sockaddr *)&ss); 683 if (rv) { 684 netdev_err(bond->dev, "Error %d setting MAC of slave %s\n", 685 -rv, new_active->dev->name); 686 goto out; 687 } 688 689 if (!old_active) 690 goto out; 691 692 bond_hw_addr_copy(ss.__data, tmp_mac, 693 new_active->dev->addr_len); 694 ss.ss_family = old_active->dev->type; 695 696 rv = dev_set_mac_address(old_active->dev, 697 (struct sockaddr *)&ss); 698 if (rv) 699 netdev_err(bond->dev, "Error %d setting MAC of slave %s\n", 700 -rv, new_active->dev->name); 701 out: 702 break; 703 default: 704 netdev_err(bond->dev, "bond_do_fail_over_mac impossible: bad policy %d\n", 705 bond->params.fail_over_mac); 706 break; 707 } 708 709 } 710 711 static struct slave *bond_choose_primary_or_current(struct bonding *bond) 712 { 713 struct slave *prim = rtnl_dereference(bond->primary_slave); 714 struct slave *curr = rtnl_dereference(bond->curr_active_slave); 715 716 if (!prim || prim->link != BOND_LINK_UP) { 717 if (!curr || curr->link != BOND_LINK_UP) 718 return NULL; 719 return curr; 720 } 721 722 if (bond->force_primary) { 723 bond->force_primary = false; 724 return prim; 725 } 726 727 if (!curr || curr->link != BOND_LINK_UP) 728 return prim; 729 730 /* At this point, prim and curr are both up */ 731 switch (bond->params.primary_reselect) { 732 case BOND_PRI_RESELECT_ALWAYS: 733 return prim; 734 case BOND_PRI_RESELECT_BETTER: 735 if (prim->speed < curr->speed) 736 return curr; 737 if (prim->speed == curr->speed && prim->duplex <= curr->duplex) 738 return curr; 739 return prim; 740 case BOND_PRI_RESELECT_FAILURE: 741 return curr; 742 default: 743 netdev_err(bond->dev, "impossible primary_reselect %d\n", 744 bond->params.primary_reselect); 745 return curr; 746 } 747 } 748 749 /** 750 * bond_find_best_slave - select the best available slave to be the active one 751 * @bond: our bonding struct 752 */ 753 static struct slave *bond_find_best_slave(struct bonding *bond) 754 { 755 struct slave *slave, *bestslave = NULL; 756 struct list_head *iter; 757 int mintime = bond->params.updelay; 758 759 slave = bond_choose_primary_or_current(bond); 760 if (slave) 761 return slave; 762 763 bond_for_each_slave(bond, slave, iter) { 764 if (slave->link == BOND_LINK_UP) 765 return slave; 766 if (slave->link == BOND_LINK_BACK && bond_slave_is_up(slave) && 767 slave->delay < mintime) { 768 mintime = slave->delay; 769 bestslave = slave; 770 } 771 } 772 773 return bestslave; 774 } 775 776 static bool bond_should_notify_peers(struct bonding *bond) 777 { 778 struct slave *slave; 779 780 rcu_read_lock(); 781 slave = rcu_dereference(bond->curr_active_slave); 782 rcu_read_unlock(); 783 784 netdev_dbg(bond->dev, "bond_should_notify_peers: slave %s\n", 785 slave ? slave->dev->name : "NULL"); 786 787 if (!slave || !bond->send_peer_notif || 788 !netif_carrier_ok(bond->dev) || 789 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state)) 790 return false; 791 792 return true; 793 } 794 795 /** 796 * change_active_interface - change the active slave into the specified one 797 * @bond: our bonding struct 798 * @new: the new slave to make the active one 799 * 800 * Set the new slave to the bond's settings and unset them on the old 801 * curr_active_slave. 802 * Setting include flags, mc-list, promiscuity, allmulti, etc. 803 * 804 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP, 805 * because it is apparently the best available slave we have, even though its 806 * updelay hasn't timed out yet. 807 * 808 * Caller must hold RTNL. 809 */ 810 void bond_change_active_slave(struct bonding *bond, struct slave *new_active) 811 { 812 struct slave *old_active; 813 814 ASSERT_RTNL(); 815 816 old_active = rtnl_dereference(bond->curr_active_slave); 817 818 if (old_active == new_active) 819 return; 820 821 if (new_active) { 822 new_active->last_link_up = jiffies; 823 824 if (new_active->link == BOND_LINK_BACK) { 825 if (bond_uses_primary(bond)) { 826 netdev_info(bond->dev, "making interface %s the new active one %d ms earlier\n", 827 new_active->dev->name, 828 (bond->params.updelay - new_active->delay) * bond->params.miimon); 829 } 830 831 new_active->delay = 0; 832 bond_set_slave_link_state(new_active, BOND_LINK_UP, 833 BOND_SLAVE_NOTIFY_NOW); 834 835 if (BOND_MODE(bond) == BOND_MODE_8023AD) 836 bond_3ad_handle_link_change(new_active, BOND_LINK_UP); 837 838 if (bond_is_lb(bond)) 839 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP); 840 } else { 841 if (bond_uses_primary(bond)) { 842 netdev_info(bond->dev, "making interface %s the new active one\n", 843 new_active->dev->name); 844 } 845 } 846 } 847 848 if (bond_uses_primary(bond)) 849 bond_hw_addr_swap(bond, new_active, old_active); 850 851 if (bond_is_lb(bond)) { 852 bond_alb_handle_active_change(bond, new_active); 853 if (old_active) 854 bond_set_slave_inactive_flags(old_active, 855 BOND_SLAVE_NOTIFY_NOW); 856 if (new_active) 857 bond_set_slave_active_flags(new_active, 858 BOND_SLAVE_NOTIFY_NOW); 859 } else { 860 rcu_assign_pointer(bond->curr_active_slave, new_active); 861 } 862 863 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) { 864 if (old_active) 865 bond_set_slave_inactive_flags(old_active, 866 BOND_SLAVE_NOTIFY_NOW); 867 868 if (new_active) { 869 bool should_notify_peers = false; 870 871 bond_set_slave_active_flags(new_active, 872 BOND_SLAVE_NOTIFY_NOW); 873 874 if (bond->params.fail_over_mac) 875 bond_do_fail_over_mac(bond, new_active, 876 old_active); 877 878 if (netif_running(bond->dev)) { 879 bond->send_peer_notif = 880 bond->params.num_peer_notif; 881 should_notify_peers = 882 bond_should_notify_peers(bond); 883 } 884 885 call_netdevice_notifiers(NETDEV_BONDING_FAILOVER, bond->dev); 886 if (should_notify_peers) 887 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, 888 bond->dev); 889 } 890 } 891 892 /* resend IGMP joins since active slave has changed or 893 * all were sent on curr_active_slave. 894 * resend only if bond is brought up with the affected 895 * bonding modes and the retransmission is enabled 896 */ 897 if (netif_running(bond->dev) && (bond->params.resend_igmp > 0) && 898 ((bond_uses_primary(bond) && new_active) || 899 BOND_MODE(bond) == BOND_MODE_ROUNDROBIN)) { 900 bond->igmp_retrans = bond->params.resend_igmp; 901 queue_delayed_work(bond->wq, &bond->mcast_work, 1); 902 } 903 } 904 905 /** 906 * bond_select_active_slave - select a new active slave, if needed 907 * @bond: our bonding struct 908 * 909 * This functions should be called when one of the following occurs: 910 * - The old curr_active_slave has been released or lost its link. 911 * - The primary_slave has got its link back. 912 * - A slave has got its link back and there's no old curr_active_slave. 913 * 914 * Caller must hold RTNL. 915 */ 916 void bond_select_active_slave(struct bonding *bond) 917 { 918 struct slave *best_slave; 919 int rv; 920 921 ASSERT_RTNL(); 922 923 best_slave = bond_find_best_slave(bond); 924 if (best_slave != rtnl_dereference(bond->curr_active_slave)) { 925 bond_change_active_slave(bond, best_slave); 926 rv = bond_set_carrier(bond); 927 if (!rv) 928 return; 929 930 if (netif_carrier_ok(bond->dev)) 931 netdev_info(bond->dev, "first active interface up!\n"); 932 else 933 netdev_info(bond->dev, "now running without any active interface!\n"); 934 } 935 } 936 937 #ifdef CONFIG_NET_POLL_CONTROLLER 938 static inline int slave_enable_netpoll(struct slave *slave) 939 { 940 struct netpoll *np; 941 int err = 0; 942 943 np = kzalloc(sizeof(*np), GFP_KERNEL); 944 err = -ENOMEM; 945 if (!np) 946 goto out; 947 948 err = __netpoll_setup(np, slave->dev); 949 if (err) { 950 kfree(np); 951 goto out; 952 } 953 slave->np = np; 954 out: 955 return err; 956 } 957 static inline void slave_disable_netpoll(struct slave *slave) 958 { 959 struct netpoll *np = slave->np; 960 961 if (!np) 962 return; 963 964 slave->np = NULL; 965 __netpoll_free_async(np); 966 } 967 968 static void bond_poll_controller(struct net_device *bond_dev) 969 { 970 struct bonding *bond = netdev_priv(bond_dev); 971 struct slave *slave = NULL; 972 struct list_head *iter; 973 struct ad_info ad_info; 974 struct netpoll_info *ni; 975 const struct net_device_ops *ops; 976 977 if (BOND_MODE(bond) == BOND_MODE_8023AD) 978 if (bond_3ad_get_active_agg_info(bond, &ad_info)) 979 return; 980 981 bond_for_each_slave_rcu(bond, slave, iter) { 982 ops = slave->dev->netdev_ops; 983 if (!bond_slave_is_up(slave) || !ops->ndo_poll_controller) 984 continue; 985 986 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 987 struct aggregator *agg = 988 SLAVE_AD_INFO(slave)->port.aggregator; 989 990 if (agg && 991 agg->aggregator_identifier != ad_info.aggregator_id) 992 continue; 993 } 994 995 ni = rcu_dereference_bh(slave->dev->npinfo); 996 if (down_trylock(&ni->dev_lock)) 997 continue; 998 ops->ndo_poll_controller(slave->dev); 999 up(&ni->dev_lock); 1000 } 1001 } 1002 1003 static void bond_netpoll_cleanup(struct net_device *bond_dev) 1004 { 1005 struct bonding *bond = netdev_priv(bond_dev); 1006 struct list_head *iter; 1007 struct slave *slave; 1008 1009 bond_for_each_slave(bond, slave, iter) 1010 if (bond_slave_is_up(slave)) 1011 slave_disable_netpoll(slave); 1012 } 1013 1014 static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni) 1015 { 1016 struct bonding *bond = netdev_priv(dev); 1017 struct list_head *iter; 1018 struct slave *slave; 1019 int err = 0; 1020 1021 bond_for_each_slave(bond, slave, iter) { 1022 err = slave_enable_netpoll(slave); 1023 if (err) { 1024 bond_netpoll_cleanup(dev); 1025 break; 1026 } 1027 } 1028 return err; 1029 } 1030 #else 1031 static inline int slave_enable_netpoll(struct slave *slave) 1032 { 1033 return 0; 1034 } 1035 static inline void slave_disable_netpoll(struct slave *slave) 1036 { 1037 } 1038 static void bond_netpoll_cleanup(struct net_device *bond_dev) 1039 { 1040 } 1041 #endif 1042 1043 /*---------------------------------- IOCTL ----------------------------------*/ 1044 1045 static netdev_features_t bond_fix_features(struct net_device *dev, 1046 netdev_features_t features) 1047 { 1048 struct bonding *bond = netdev_priv(dev); 1049 struct list_head *iter; 1050 netdev_features_t mask; 1051 struct slave *slave; 1052 1053 mask = features; 1054 1055 features &= ~NETIF_F_ONE_FOR_ALL; 1056 features |= NETIF_F_ALL_FOR_ALL; 1057 1058 bond_for_each_slave(bond, slave, iter) { 1059 features = netdev_increment_features(features, 1060 slave->dev->features, 1061 mask); 1062 } 1063 features = netdev_add_tso_features(features, mask); 1064 1065 return features; 1066 } 1067 1068 #define BOND_VLAN_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \ 1069 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \ 1070 NETIF_F_HIGHDMA | NETIF_F_LRO) 1071 1072 #define BOND_ENC_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \ 1073 NETIF_F_RXCSUM | NETIF_F_ALL_TSO) 1074 1075 static void bond_compute_features(struct bonding *bond) 1076 { 1077 unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE | 1078 IFF_XMIT_DST_RELEASE_PERM; 1079 netdev_features_t vlan_features = BOND_VLAN_FEATURES; 1080 netdev_features_t enc_features = BOND_ENC_FEATURES; 1081 struct net_device *bond_dev = bond->dev; 1082 struct list_head *iter; 1083 struct slave *slave; 1084 unsigned short max_hard_header_len = ETH_HLEN; 1085 unsigned int gso_max_size = GSO_MAX_SIZE; 1086 u16 gso_max_segs = GSO_MAX_SEGS; 1087 1088 if (!bond_has_slaves(bond)) 1089 goto done; 1090 vlan_features &= NETIF_F_ALL_FOR_ALL; 1091 1092 bond_for_each_slave(bond, slave, iter) { 1093 vlan_features = netdev_increment_features(vlan_features, 1094 slave->dev->vlan_features, BOND_VLAN_FEATURES); 1095 1096 enc_features = netdev_increment_features(enc_features, 1097 slave->dev->hw_enc_features, 1098 BOND_ENC_FEATURES); 1099 dst_release_flag &= slave->dev->priv_flags; 1100 if (slave->dev->hard_header_len > max_hard_header_len) 1101 max_hard_header_len = slave->dev->hard_header_len; 1102 1103 gso_max_size = min(gso_max_size, slave->dev->gso_max_size); 1104 gso_max_segs = min(gso_max_segs, slave->dev->gso_max_segs); 1105 } 1106 1107 done: 1108 bond_dev->vlan_features = vlan_features; 1109 bond_dev->hw_enc_features = enc_features | NETIF_F_GSO_ENCAP_ALL; 1110 bond_dev->hard_header_len = max_hard_header_len; 1111 bond_dev->gso_max_segs = gso_max_segs; 1112 netif_set_gso_max_size(bond_dev, gso_max_size); 1113 1114 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; 1115 if ((bond_dev->priv_flags & IFF_XMIT_DST_RELEASE_PERM) && 1116 dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM)) 1117 bond_dev->priv_flags |= IFF_XMIT_DST_RELEASE; 1118 1119 netdev_change_features(bond_dev); 1120 } 1121 1122 static void bond_setup_by_slave(struct net_device *bond_dev, 1123 struct net_device *slave_dev) 1124 { 1125 bond_dev->header_ops = slave_dev->header_ops; 1126 1127 bond_dev->type = slave_dev->type; 1128 bond_dev->hard_header_len = slave_dev->hard_header_len; 1129 bond_dev->addr_len = slave_dev->addr_len; 1130 1131 memcpy(bond_dev->broadcast, slave_dev->broadcast, 1132 slave_dev->addr_len); 1133 } 1134 1135 /* On bonding slaves other than the currently active slave, suppress 1136 * duplicates except for alb non-mcast/bcast. 1137 */ 1138 static bool bond_should_deliver_exact_match(struct sk_buff *skb, 1139 struct slave *slave, 1140 struct bonding *bond) 1141 { 1142 if (bond_is_slave_inactive(slave)) { 1143 if (BOND_MODE(bond) == BOND_MODE_ALB && 1144 skb->pkt_type != PACKET_BROADCAST && 1145 skb->pkt_type != PACKET_MULTICAST) 1146 return false; 1147 return true; 1148 } 1149 return false; 1150 } 1151 1152 static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb) 1153 { 1154 struct sk_buff *skb = *pskb; 1155 struct slave *slave; 1156 struct bonding *bond; 1157 int (*recv_probe)(const struct sk_buff *, struct bonding *, 1158 struct slave *); 1159 int ret = RX_HANDLER_ANOTHER; 1160 1161 skb = skb_share_check(skb, GFP_ATOMIC); 1162 if (unlikely(!skb)) 1163 return RX_HANDLER_CONSUMED; 1164 1165 *pskb = skb; 1166 1167 slave = bond_slave_get_rcu(skb->dev); 1168 bond = slave->bond; 1169 1170 recv_probe = ACCESS_ONCE(bond->recv_probe); 1171 if (recv_probe) { 1172 ret = recv_probe(skb, bond, slave); 1173 if (ret == RX_HANDLER_CONSUMED) { 1174 consume_skb(skb); 1175 return ret; 1176 } 1177 } 1178 1179 if (bond_should_deliver_exact_match(skb, slave, bond)) 1180 return RX_HANDLER_EXACT; 1181 1182 skb->dev = bond->dev; 1183 1184 if (BOND_MODE(bond) == BOND_MODE_ALB && 1185 bond->dev->priv_flags & IFF_BRIDGE_PORT && 1186 skb->pkt_type == PACKET_HOST) { 1187 1188 if (unlikely(skb_cow_head(skb, 1189 skb->data - skb_mac_header(skb)))) { 1190 kfree_skb(skb); 1191 return RX_HANDLER_CONSUMED; 1192 } 1193 bond_hw_addr_copy(eth_hdr(skb)->h_dest, bond->dev->dev_addr, 1194 bond->dev->addr_len); 1195 } 1196 1197 return ret; 1198 } 1199 1200 static enum netdev_lag_tx_type bond_lag_tx_type(struct bonding *bond) 1201 { 1202 switch (BOND_MODE(bond)) { 1203 case BOND_MODE_ROUNDROBIN: 1204 return NETDEV_LAG_TX_TYPE_ROUNDROBIN; 1205 case BOND_MODE_ACTIVEBACKUP: 1206 return NETDEV_LAG_TX_TYPE_ACTIVEBACKUP; 1207 case BOND_MODE_BROADCAST: 1208 return NETDEV_LAG_TX_TYPE_BROADCAST; 1209 case BOND_MODE_XOR: 1210 case BOND_MODE_8023AD: 1211 return NETDEV_LAG_TX_TYPE_HASH; 1212 default: 1213 return NETDEV_LAG_TX_TYPE_UNKNOWN; 1214 } 1215 } 1216 1217 static int bond_master_upper_dev_link(struct bonding *bond, struct slave *slave) 1218 { 1219 struct netdev_lag_upper_info lag_upper_info; 1220 int err; 1221 1222 lag_upper_info.tx_type = bond_lag_tx_type(bond); 1223 err = netdev_master_upper_dev_link(slave->dev, bond->dev, slave, 1224 &lag_upper_info); 1225 if (err) 1226 return err; 1227 rtmsg_ifinfo(RTM_NEWLINK, slave->dev, IFF_SLAVE, GFP_KERNEL); 1228 return 0; 1229 } 1230 1231 static void bond_upper_dev_unlink(struct bonding *bond, struct slave *slave) 1232 { 1233 netdev_upper_dev_unlink(slave->dev, bond->dev); 1234 slave->dev->flags &= ~IFF_SLAVE; 1235 rtmsg_ifinfo(RTM_NEWLINK, slave->dev, IFF_SLAVE, GFP_KERNEL); 1236 } 1237 1238 static struct slave *bond_alloc_slave(struct bonding *bond) 1239 { 1240 struct slave *slave = NULL; 1241 1242 slave = kzalloc(sizeof(*slave), GFP_KERNEL); 1243 if (!slave) 1244 return NULL; 1245 1246 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 1247 SLAVE_AD_INFO(slave) = kzalloc(sizeof(struct ad_slave_info), 1248 GFP_KERNEL); 1249 if (!SLAVE_AD_INFO(slave)) { 1250 kfree(slave); 1251 return NULL; 1252 } 1253 } 1254 return slave; 1255 } 1256 1257 static void bond_free_slave(struct slave *slave) 1258 { 1259 struct bonding *bond = bond_get_bond_by_slave(slave); 1260 1261 if (BOND_MODE(bond) == BOND_MODE_8023AD) 1262 kfree(SLAVE_AD_INFO(slave)); 1263 1264 kfree(slave); 1265 } 1266 1267 static void bond_fill_ifbond(struct bonding *bond, struct ifbond *info) 1268 { 1269 info->bond_mode = BOND_MODE(bond); 1270 info->miimon = bond->params.miimon; 1271 info->num_slaves = bond->slave_cnt; 1272 } 1273 1274 static void bond_fill_ifslave(struct slave *slave, struct ifslave *info) 1275 { 1276 strcpy(info->slave_name, slave->dev->name); 1277 info->link = slave->link; 1278 info->state = bond_slave_state(slave); 1279 info->link_failure_count = slave->link_failure_count; 1280 } 1281 1282 static void bond_netdev_notify(struct net_device *dev, 1283 struct netdev_bonding_info *info) 1284 { 1285 rtnl_lock(); 1286 netdev_bonding_info_change(dev, info); 1287 rtnl_unlock(); 1288 } 1289 1290 static void bond_netdev_notify_work(struct work_struct *_work) 1291 { 1292 struct netdev_notify_work *w = 1293 container_of(_work, struct netdev_notify_work, work.work); 1294 1295 bond_netdev_notify(w->dev, &w->bonding_info); 1296 dev_put(w->dev); 1297 kfree(w); 1298 } 1299 1300 void bond_queue_slave_event(struct slave *slave) 1301 { 1302 struct bonding *bond = slave->bond; 1303 struct netdev_notify_work *nnw = kzalloc(sizeof(*nnw), GFP_ATOMIC); 1304 1305 if (!nnw) 1306 return; 1307 1308 dev_hold(slave->dev); 1309 nnw->dev = slave->dev; 1310 bond_fill_ifslave(slave, &nnw->bonding_info.slave); 1311 bond_fill_ifbond(bond, &nnw->bonding_info.master); 1312 INIT_DELAYED_WORK(&nnw->work, bond_netdev_notify_work); 1313 1314 queue_delayed_work(slave->bond->wq, &nnw->work, 0); 1315 } 1316 1317 void bond_lower_state_changed(struct slave *slave) 1318 { 1319 struct netdev_lag_lower_state_info info; 1320 1321 info.link_up = slave->link == BOND_LINK_UP || 1322 slave->link == BOND_LINK_FAIL; 1323 info.tx_enabled = bond_is_active_slave(slave); 1324 netdev_lower_state_changed(slave->dev, &info); 1325 } 1326 1327 /* enslave device <slave> to bond device <master> */ 1328 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev) 1329 { 1330 struct bonding *bond = netdev_priv(bond_dev); 1331 const struct net_device_ops *slave_ops = slave_dev->netdev_ops; 1332 struct slave *new_slave = NULL, *prev_slave; 1333 struct sockaddr_storage ss; 1334 int link_reporting; 1335 int res = 0, i; 1336 1337 if (!bond->params.use_carrier && 1338 slave_dev->ethtool_ops->get_link == NULL && 1339 slave_ops->ndo_do_ioctl == NULL) { 1340 netdev_warn(bond_dev, "no link monitoring support for %s\n", 1341 slave_dev->name); 1342 } 1343 1344 /* already in-use? */ 1345 if (netdev_is_rx_handler_busy(slave_dev)) { 1346 netdev_err(bond_dev, 1347 "Error: Device is in use and cannot be enslaved\n"); 1348 return -EBUSY; 1349 } 1350 1351 if (bond_dev == slave_dev) { 1352 netdev_err(bond_dev, "cannot enslave bond to itself.\n"); 1353 return -EPERM; 1354 } 1355 1356 /* vlan challenged mutual exclusion */ 1357 /* no need to lock since we're protected by rtnl_lock */ 1358 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) { 1359 netdev_dbg(bond_dev, "%s is NETIF_F_VLAN_CHALLENGED\n", 1360 slave_dev->name); 1361 if (vlan_uses_dev(bond_dev)) { 1362 netdev_err(bond_dev, "Error: cannot enslave VLAN challenged slave %s on VLAN enabled bond %s\n", 1363 slave_dev->name, bond_dev->name); 1364 return -EPERM; 1365 } else { 1366 netdev_warn(bond_dev, "enslaved VLAN challenged slave %s. Adding VLANs will be blocked as long as %s is part of bond %s\n", 1367 slave_dev->name, slave_dev->name, 1368 bond_dev->name); 1369 } 1370 } else { 1371 netdev_dbg(bond_dev, "%s is !NETIF_F_VLAN_CHALLENGED\n", 1372 slave_dev->name); 1373 } 1374 1375 /* Old ifenslave binaries are no longer supported. These can 1376 * be identified with moderate accuracy by the state of the slave: 1377 * the current ifenslave will set the interface down prior to 1378 * enslaving it; the old ifenslave will not. 1379 */ 1380 if (slave_dev->flags & IFF_UP) { 1381 netdev_err(bond_dev, "%s is up - this may be due to an out of date ifenslave\n", 1382 slave_dev->name); 1383 return -EPERM; 1384 } 1385 1386 /* set bonding device ether type by slave - bonding netdevices are 1387 * created with ether_setup, so when the slave type is not ARPHRD_ETHER 1388 * there is a need to override some of the type dependent attribs/funcs. 1389 * 1390 * bond ether type mutual exclusion - don't allow slaves of dissimilar 1391 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond 1392 */ 1393 if (!bond_has_slaves(bond)) { 1394 if (bond_dev->type != slave_dev->type) { 1395 netdev_dbg(bond_dev, "change device type from %d to %d\n", 1396 bond_dev->type, slave_dev->type); 1397 1398 res = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE, 1399 bond_dev); 1400 res = notifier_to_errno(res); 1401 if (res) { 1402 netdev_err(bond_dev, "refused to change device type\n"); 1403 return -EBUSY; 1404 } 1405 1406 /* Flush unicast and multicast addresses */ 1407 dev_uc_flush(bond_dev); 1408 dev_mc_flush(bond_dev); 1409 1410 if (slave_dev->type != ARPHRD_ETHER) 1411 bond_setup_by_slave(bond_dev, slave_dev); 1412 else { 1413 ether_setup(bond_dev); 1414 bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1415 } 1416 1417 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, 1418 bond_dev); 1419 } 1420 } else if (bond_dev->type != slave_dev->type) { 1421 netdev_err(bond_dev, "%s ether type (%d) is different from other slaves (%d), can not enslave it\n", 1422 slave_dev->name, slave_dev->type, bond_dev->type); 1423 return -EINVAL; 1424 } 1425 1426 if (slave_dev->type == ARPHRD_INFINIBAND && 1427 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) { 1428 netdev_warn(bond_dev, "Type (%d) supports only active-backup mode\n", 1429 slave_dev->type); 1430 res = -EOPNOTSUPP; 1431 goto err_undo_flags; 1432 } 1433 1434 if (!slave_ops->ndo_set_mac_address || 1435 slave_dev->type == ARPHRD_INFINIBAND) { 1436 netdev_warn(bond_dev, "The slave device specified does not support setting the MAC address\n"); 1437 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP && 1438 bond->params.fail_over_mac != BOND_FOM_ACTIVE) { 1439 if (!bond_has_slaves(bond)) { 1440 bond->params.fail_over_mac = BOND_FOM_ACTIVE; 1441 netdev_warn(bond_dev, "Setting fail_over_mac to active for active-backup mode\n"); 1442 } else { 1443 netdev_err(bond_dev, "The slave device specified does not support setting the MAC address, but fail_over_mac is not set to active\n"); 1444 res = -EOPNOTSUPP; 1445 goto err_undo_flags; 1446 } 1447 } 1448 } 1449 1450 call_netdevice_notifiers(NETDEV_JOIN, slave_dev); 1451 1452 /* If this is the first slave, then we need to set the master's hardware 1453 * address to be the same as the slave's. 1454 */ 1455 if (!bond_has_slaves(bond) && 1456 bond->dev->addr_assign_type == NET_ADDR_RANDOM) 1457 bond_set_dev_addr(bond->dev, slave_dev); 1458 1459 new_slave = bond_alloc_slave(bond); 1460 if (!new_slave) { 1461 res = -ENOMEM; 1462 goto err_undo_flags; 1463 } 1464 1465 new_slave->bond = bond; 1466 new_slave->dev = slave_dev; 1467 /* Set the new_slave's queue_id to be zero. Queue ID mapping 1468 * is set via sysfs or module option if desired. 1469 */ 1470 new_slave->queue_id = 0; 1471 1472 /* Save slave's original mtu and then set it to match the bond */ 1473 new_slave->original_mtu = slave_dev->mtu; 1474 res = dev_set_mtu(slave_dev, bond->dev->mtu); 1475 if (res) { 1476 netdev_dbg(bond_dev, "Error %d calling dev_set_mtu\n", res); 1477 goto err_free; 1478 } 1479 1480 /* Save slave's original ("permanent") mac address for modes 1481 * that need it, and for restoring it upon release, and then 1482 * set it to the master's address 1483 */ 1484 bond_hw_addr_copy(new_slave->perm_hwaddr, slave_dev->dev_addr, 1485 slave_dev->addr_len); 1486 1487 if (!bond->params.fail_over_mac || 1488 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) { 1489 /* Set slave to master's mac address. The application already 1490 * set the master's mac address to that of the first slave 1491 */ 1492 memcpy(ss.__data, bond_dev->dev_addr, bond_dev->addr_len); 1493 ss.ss_family = slave_dev->type; 1494 res = dev_set_mac_address(slave_dev, (struct sockaddr *)&ss); 1495 if (res) { 1496 netdev_dbg(bond_dev, "Error %d calling set_mac_address\n", res); 1497 goto err_restore_mtu; 1498 } 1499 } 1500 1501 /* set slave flag before open to prevent IPv6 addrconf */ 1502 slave_dev->flags |= IFF_SLAVE; 1503 1504 /* open the slave since the application closed it */ 1505 res = dev_open(slave_dev); 1506 if (res) { 1507 netdev_dbg(bond_dev, "Opening slave %s failed\n", slave_dev->name); 1508 goto err_restore_mac; 1509 } 1510 1511 slave_dev->priv_flags |= IFF_BONDING; 1512 /* initialize slave stats */ 1513 dev_get_stats(new_slave->dev, &new_slave->slave_stats); 1514 1515 if (bond_is_lb(bond)) { 1516 /* bond_alb_init_slave() must be called before all other stages since 1517 * it might fail and we do not want to have to undo everything 1518 */ 1519 res = bond_alb_init_slave(bond, new_slave); 1520 if (res) 1521 goto err_close; 1522 } 1523 1524 /* If the mode uses primary, then the following is handled by 1525 * bond_change_active_slave(). 1526 */ 1527 if (!bond_uses_primary(bond)) { 1528 /* set promiscuity level to new slave */ 1529 if (bond_dev->flags & IFF_PROMISC) { 1530 res = dev_set_promiscuity(slave_dev, 1); 1531 if (res) 1532 goto err_close; 1533 } 1534 1535 /* set allmulti level to new slave */ 1536 if (bond_dev->flags & IFF_ALLMULTI) { 1537 res = dev_set_allmulti(slave_dev, 1); 1538 if (res) 1539 goto err_close; 1540 } 1541 1542 netif_addr_lock_bh(bond_dev); 1543 1544 dev_mc_sync_multiple(slave_dev, bond_dev); 1545 dev_uc_sync_multiple(slave_dev, bond_dev); 1546 1547 netif_addr_unlock_bh(bond_dev); 1548 } 1549 1550 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 1551 /* add lacpdu mc addr to mc list */ 1552 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR; 1553 1554 dev_mc_add(slave_dev, lacpdu_multicast); 1555 } 1556 1557 res = vlan_vids_add_by_dev(slave_dev, bond_dev); 1558 if (res) { 1559 netdev_err(bond_dev, "Couldn't add bond vlan ids to %s\n", 1560 slave_dev->name); 1561 goto err_close; 1562 } 1563 1564 prev_slave = bond_last_slave(bond); 1565 1566 new_slave->delay = 0; 1567 new_slave->link_failure_count = 0; 1568 1569 if (bond_update_speed_duplex(new_slave)) 1570 new_slave->link = BOND_LINK_DOWN; 1571 1572 new_slave->last_rx = jiffies - 1573 (msecs_to_jiffies(bond->params.arp_interval) + 1); 1574 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) 1575 new_slave->target_last_arp_rx[i] = new_slave->last_rx; 1576 1577 if (bond->params.miimon && !bond->params.use_carrier) { 1578 link_reporting = bond_check_dev_link(bond, slave_dev, 1); 1579 1580 if ((link_reporting == -1) && !bond->params.arp_interval) { 1581 /* miimon is set but a bonded network driver 1582 * does not support ETHTOOL/MII and 1583 * arp_interval is not set. Note: if 1584 * use_carrier is enabled, we will never go 1585 * here (because netif_carrier is always 1586 * supported); thus, we don't need to change 1587 * the messages for netif_carrier. 1588 */ 1589 netdev_warn(bond_dev, "MII and ETHTOOL support not available for interface %s, and arp_interval/arp_ip_target module parameters not specified, thus bonding will not detect link failures! see bonding.txt for details\n", 1590 slave_dev->name); 1591 } else if (link_reporting == -1) { 1592 /* unable get link status using mii/ethtool */ 1593 netdev_warn(bond_dev, "can't get link status from interface %s; the network driver associated with this interface does not support MII or ETHTOOL link status reporting, thus miimon has no effect on this interface\n", 1594 slave_dev->name); 1595 } 1596 } 1597 1598 /* check for initial state */ 1599 new_slave->link = BOND_LINK_NOCHANGE; 1600 if (bond->params.miimon) { 1601 if (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS) { 1602 if (bond->params.updelay) { 1603 bond_set_slave_link_state(new_slave, 1604 BOND_LINK_BACK, 1605 BOND_SLAVE_NOTIFY_NOW); 1606 new_slave->delay = bond->params.updelay; 1607 } else { 1608 bond_set_slave_link_state(new_slave, 1609 BOND_LINK_UP, 1610 BOND_SLAVE_NOTIFY_NOW); 1611 } 1612 } else { 1613 bond_set_slave_link_state(new_slave, BOND_LINK_DOWN, 1614 BOND_SLAVE_NOTIFY_NOW); 1615 } 1616 } else if (bond->params.arp_interval) { 1617 bond_set_slave_link_state(new_slave, 1618 (netif_carrier_ok(slave_dev) ? 1619 BOND_LINK_UP : BOND_LINK_DOWN), 1620 BOND_SLAVE_NOTIFY_NOW); 1621 } else { 1622 bond_set_slave_link_state(new_slave, BOND_LINK_UP, 1623 BOND_SLAVE_NOTIFY_NOW); 1624 } 1625 1626 if (new_slave->link != BOND_LINK_DOWN) 1627 new_slave->last_link_up = jiffies; 1628 netdev_dbg(bond_dev, "Initial state of slave_dev is BOND_LINK_%s\n", 1629 new_slave->link == BOND_LINK_DOWN ? "DOWN" : 1630 (new_slave->link == BOND_LINK_UP ? "UP" : "BACK")); 1631 1632 if (bond_uses_primary(bond) && bond->params.primary[0]) { 1633 /* if there is a primary slave, remember it */ 1634 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) { 1635 rcu_assign_pointer(bond->primary_slave, new_slave); 1636 bond->force_primary = true; 1637 } 1638 } 1639 1640 switch (BOND_MODE(bond)) { 1641 case BOND_MODE_ACTIVEBACKUP: 1642 bond_set_slave_inactive_flags(new_slave, 1643 BOND_SLAVE_NOTIFY_NOW); 1644 break; 1645 case BOND_MODE_8023AD: 1646 /* in 802.3ad mode, the internal mechanism 1647 * will activate the slaves in the selected 1648 * aggregator 1649 */ 1650 bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW); 1651 /* if this is the first slave */ 1652 if (!prev_slave) { 1653 SLAVE_AD_INFO(new_slave)->id = 1; 1654 /* Initialize AD with the number of times that the AD timer is called in 1 second 1655 * can be called only after the mac address of the bond is set 1656 */ 1657 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL); 1658 } else { 1659 SLAVE_AD_INFO(new_slave)->id = 1660 SLAVE_AD_INFO(prev_slave)->id + 1; 1661 } 1662 1663 bond_3ad_bind_slave(new_slave); 1664 break; 1665 case BOND_MODE_TLB: 1666 case BOND_MODE_ALB: 1667 bond_set_active_slave(new_slave); 1668 bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW); 1669 break; 1670 default: 1671 netdev_dbg(bond_dev, "This slave is always active in trunk mode\n"); 1672 1673 /* always active in trunk mode */ 1674 bond_set_active_slave(new_slave); 1675 1676 /* In trunking mode there is little meaning to curr_active_slave 1677 * anyway (it holds no special properties of the bond device), 1678 * so we can change it without calling change_active_interface() 1679 */ 1680 if (!rcu_access_pointer(bond->curr_active_slave) && 1681 new_slave->link == BOND_LINK_UP) 1682 rcu_assign_pointer(bond->curr_active_slave, new_slave); 1683 1684 break; 1685 } /* switch(bond_mode) */ 1686 1687 #ifdef CONFIG_NET_POLL_CONTROLLER 1688 slave_dev->npinfo = bond->dev->npinfo; 1689 if (slave_dev->npinfo) { 1690 if (slave_enable_netpoll(new_slave)) { 1691 netdev_info(bond_dev, "master_dev is using netpoll, but new slave device does not support netpoll\n"); 1692 res = -EBUSY; 1693 goto err_detach; 1694 } 1695 } 1696 #endif 1697 1698 if (!(bond_dev->features & NETIF_F_LRO)) 1699 dev_disable_lro(slave_dev); 1700 1701 res = netdev_rx_handler_register(slave_dev, bond_handle_frame, 1702 new_slave); 1703 if (res) { 1704 netdev_dbg(bond_dev, "Error %d calling netdev_rx_handler_register\n", res); 1705 goto err_detach; 1706 } 1707 1708 res = bond_master_upper_dev_link(bond, new_slave); 1709 if (res) { 1710 netdev_dbg(bond_dev, "Error %d calling bond_master_upper_dev_link\n", res); 1711 goto err_unregister; 1712 } 1713 1714 res = bond_sysfs_slave_add(new_slave); 1715 if (res) { 1716 netdev_dbg(bond_dev, "Error %d calling bond_sysfs_slave_add\n", res); 1717 goto err_upper_unlink; 1718 } 1719 1720 bond->slave_cnt++; 1721 bond_compute_features(bond); 1722 bond_set_carrier(bond); 1723 1724 if (bond_uses_primary(bond)) { 1725 block_netpoll_tx(); 1726 bond_select_active_slave(bond); 1727 unblock_netpoll_tx(); 1728 } 1729 1730 if (bond_mode_uses_xmit_hash(bond)) 1731 bond_update_slave_arr(bond, NULL); 1732 1733 netdev_info(bond_dev, "Enslaving %s as %s interface with %s link\n", 1734 slave_dev->name, 1735 bond_is_active_slave(new_slave) ? "an active" : "a backup", 1736 new_slave->link != BOND_LINK_DOWN ? "an up" : "a down"); 1737 1738 /* enslave is successful */ 1739 bond_queue_slave_event(new_slave); 1740 return 0; 1741 1742 /* Undo stages on error */ 1743 err_upper_unlink: 1744 bond_upper_dev_unlink(bond, new_slave); 1745 1746 err_unregister: 1747 netdev_rx_handler_unregister(slave_dev); 1748 1749 err_detach: 1750 if (!bond_uses_primary(bond)) 1751 bond_hw_addr_flush(bond_dev, slave_dev); 1752 1753 vlan_vids_del_by_dev(slave_dev, bond_dev); 1754 if (rcu_access_pointer(bond->primary_slave) == new_slave) 1755 RCU_INIT_POINTER(bond->primary_slave, NULL); 1756 if (rcu_access_pointer(bond->curr_active_slave) == new_slave) { 1757 block_netpoll_tx(); 1758 bond_change_active_slave(bond, NULL); 1759 bond_select_active_slave(bond); 1760 unblock_netpoll_tx(); 1761 } 1762 /* either primary_slave or curr_active_slave might've changed */ 1763 synchronize_rcu(); 1764 slave_disable_netpoll(new_slave); 1765 1766 err_close: 1767 slave_dev->priv_flags &= ~IFF_BONDING; 1768 dev_close(slave_dev); 1769 1770 err_restore_mac: 1771 slave_dev->flags &= ~IFF_SLAVE; 1772 if (!bond->params.fail_over_mac || 1773 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) { 1774 /* XXX TODO - fom follow mode needs to change master's 1775 * MAC if this slave's MAC is in use by the bond, or at 1776 * least print a warning. 1777 */ 1778 bond_hw_addr_copy(ss.__data, new_slave->perm_hwaddr, 1779 new_slave->dev->addr_len); 1780 ss.ss_family = slave_dev->type; 1781 dev_set_mac_address(slave_dev, (struct sockaddr *)&ss); 1782 } 1783 1784 err_restore_mtu: 1785 dev_set_mtu(slave_dev, new_slave->original_mtu); 1786 1787 err_free: 1788 bond_free_slave(new_slave); 1789 1790 err_undo_flags: 1791 /* Enslave of first slave has failed and we need to fix master's mac */ 1792 if (!bond_has_slaves(bond)) { 1793 if (ether_addr_equal_64bits(bond_dev->dev_addr, 1794 slave_dev->dev_addr)) 1795 eth_hw_addr_random(bond_dev); 1796 if (bond_dev->type != ARPHRD_ETHER) { 1797 dev_close(bond_dev); 1798 ether_setup(bond_dev); 1799 bond_dev->flags |= IFF_MASTER; 1800 bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING; 1801 } 1802 } 1803 1804 return res; 1805 } 1806 1807 /* Try to release the slave device <slave> from the bond device <master> 1808 * It is legal to access curr_active_slave without a lock because all the function 1809 * is RTNL-locked. If "all" is true it means that the function is being called 1810 * while destroying a bond interface and all slaves are being released. 1811 * 1812 * The rules for slave state should be: 1813 * for Active/Backup: 1814 * Active stays on all backups go down 1815 * for Bonded connections: 1816 * The first up interface should be left on and all others downed. 1817 */ 1818 static int __bond_release_one(struct net_device *bond_dev, 1819 struct net_device *slave_dev, 1820 bool all) 1821 { 1822 struct bonding *bond = netdev_priv(bond_dev); 1823 struct slave *slave, *oldcurrent; 1824 struct sockaddr_storage ss; 1825 int old_flags = bond_dev->flags; 1826 netdev_features_t old_features = bond_dev->features; 1827 1828 /* slave is not a slave or master is not master of this slave */ 1829 if (!(slave_dev->flags & IFF_SLAVE) || 1830 !netdev_has_upper_dev(slave_dev, bond_dev)) { 1831 netdev_dbg(bond_dev, "cannot release %s\n", 1832 slave_dev->name); 1833 return -EINVAL; 1834 } 1835 1836 block_netpoll_tx(); 1837 1838 slave = bond_get_slave_by_dev(bond, slave_dev); 1839 if (!slave) { 1840 /* not a slave of this bond */ 1841 netdev_info(bond_dev, "%s not enslaved\n", 1842 slave_dev->name); 1843 unblock_netpoll_tx(); 1844 return -EINVAL; 1845 } 1846 1847 bond_set_slave_inactive_flags(slave, BOND_SLAVE_NOTIFY_NOW); 1848 1849 bond_sysfs_slave_del(slave); 1850 1851 /* recompute stats just before removing the slave */ 1852 bond_get_stats(bond->dev, &bond->bond_stats); 1853 1854 bond_upper_dev_unlink(bond, slave); 1855 /* unregister rx_handler early so bond_handle_frame wouldn't be called 1856 * for this slave anymore. 1857 */ 1858 netdev_rx_handler_unregister(slave_dev); 1859 1860 if (BOND_MODE(bond) == BOND_MODE_8023AD) 1861 bond_3ad_unbind_slave(slave); 1862 1863 if (bond_mode_uses_xmit_hash(bond)) 1864 bond_update_slave_arr(bond, slave); 1865 1866 netdev_info(bond_dev, "Releasing %s interface %s\n", 1867 bond_is_active_slave(slave) ? "active" : "backup", 1868 slave_dev->name); 1869 1870 oldcurrent = rcu_access_pointer(bond->curr_active_slave); 1871 1872 RCU_INIT_POINTER(bond->current_arp_slave, NULL); 1873 1874 if (!all && (!bond->params.fail_over_mac || 1875 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)) { 1876 if (ether_addr_equal_64bits(bond_dev->dev_addr, slave->perm_hwaddr) && 1877 bond_has_slaves(bond)) 1878 netdev_warn(bond_dev, "the permanent HWaddr of %s - %pM - is still in use by %s - set the HWaddr of %s to a different address to avoid conflicts\n", 1879 slave_dev->name, slave->perm_hwaddr, 1880 bond_dev->name, slave_dev->name); 1881 } 1882 1883 if (rtnl_dereference(bond->primary_slave) == slave) 1884 RCU_INIT_POINTER(bond->primary_slave, NULL); 1885 1886 if (oldcurrent == slave) 1887 bond_change_active_slave(bond, NULL); 1888 1889 if (bond_is_lb(bond)) { 1890 /* Must be called only after the slave has been 1891 * detached from the list and the curr_active_slave 1892 * has been cleared (if our_slave == old_current), 1893 * but before a new active slave is selected. 1894 */ 1895 bond_alb_deinit_slave(bond, slave); 1896 } 1897 1898 if (all) { 1899 RCU_INIT_POINTER(bond->curr_active_slave, NULL); 1900 } else if (oldcurrent == slave) { 1901 /* Note that we hold RTNL over this sequence, so there 1902 * is no concern that another slave add/remove event 1903 * will interfere. 1904 */ 1905 bond_select_active_slave(bond); 1906 } 1907 1908 if (!bond_has_slaves(bond)) { 1909 bond_set_carrier(bond); 1910 eth_hw_addr_random(bond_dev); 1911 } 1912 1913 unblock_netpoll_tx(); 1914 synchronize_rcu(); 1915 bond->slave_cnt--; 1916 1917 if (!bond_has_slaves(bond)) { 1918 call_netdevice_notifiers(NETDEV_CHANGEADDR, bond->dev); 1919 call_netdevice_notifiers(NETDEV_RELEASE, bond->dev); 1920 } 1921 1922 bond_compute_features(bond); 1923 if (!(bond_dev->features & NETIF_F_VLAN_CHALLENGED) && 1924 (old_features & NETIF_F_VLAN_CHALLENGED)) 1925 netdev_info(bond_dev, "last VLAN challenged slave %s left bond %s - VLAN blocking is removed\n", 1926 slave_dev->name, bond_dev->name); 1927 1928 vlan_vids_del_by_dev(slave_dev, bond_dev); 1929 1930 /* If the mode uses primary, then this case was handled above by 1931 * bond_change_active_slave(..., NULL) 1932 */ 1933 if (!bond_uses_primary(bond)) { 1934 /* unset promiscuity level from slave 1935 * NOTE: The NETDEV_CHANGEADDR call above may change the value 1936 * of the IFF_PROMISC flag in the bond_dev, but we need the 1937 * value of that flag before that change, as that was the value 1938 * when this slave was attached, so we cache at the start of the 1939 * function and use it here. Same goes for ALLMULTI below 1940 */ 1941 if (old_flags & IFF_PROMISC) 1942 dev_set_promiscuity(slave_dev, -1); 1943 1944 /* unset allmulti level from slave */ 1945 if (old_flags & IFF_ALLMULTI) 1946 dev_set_allmulti(slave_dev, -1); 1947 1948 bond_hw_addr_flush(bond_dev, slave_dev); 1949 } 1950 1951 slave_disable_netpoll(slave); 1952 1953 /* close slave before restoring its mac address */ 1954 dev_close(slave_dev); 1955 1956 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE || 1957 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) { 1958 /* restore original ("permanent") mac address */ 1959 bond_hw_addr_copy(ss.__data, slave->perm_hwaddr, 1960 slave->dev->addr_len); 1961 ss.ss_family = slave_dev->type; 1962 dev_set_mac_address(slave_dev, (struct sockaddr *)&ss); 1963 } 1964 1965 dev_set_mtu(slave_dev, slave->original_mtu); 1966 1967 slave_dev->priv_flags &= ~IFF_BONDING; 1968 1969 bond_free_slave(slave); 1970 1971 return 0; 1972 } 1973 1974 /* A wrapper used because of ndo_del_link */ 1975 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev) 1976 { 1977 return __bond_release_one(bond_dev, slave_dev, false); 1978 } 1979 1980 /* First release a slave and then destroy the bond if no more slaves are left. 1981 * Must be under rtnl_lock when this function is called. 1982 */ 1983 static int bond_release_and_destroy(struct net_device *bond_dev, 1984 struct net_device *slave_dev) 1985 { 1986 struct bonding *bond = netdev_priv(bond_dev); 1987 int ret; 1988 1989 ret = bond_release(bond_dev, slave_dev); 1990 if (ret == 0 && !bond_has_slaves(bond)) { 1991 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL; 1992 netdev_info(bond_dev, "Destroying bond %s\n", 1993 bond_dev->name); 1994 bond_remove_proc_entry(bond); 1995 unregister_netdevice(bond_dev); 1996 } 1997 return ret; 1998 } 1999 2000 static void bond_info_query(struct net_device *bond_dev, struct ifbond *info) 2001 { 2002 struct bonding *bond = netdev_priv(bond_dev); 2003 bond_fill_ifbond(bond, info); 2004 } 2005 2006 static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info) 2007 { 2008 struct bonding *bond = netdev_priv(bond_dev); 2009 struct list_head *iter; 2010 int i = 0, res = -ENODEV; 2011 struct slave *slave; 2012 2013 bond_for_each_slave(bond, slave, iter) { 2014 if (i++ == (int)info->slave_id) { 2015 res = 0; 2016 bond_fill_ifslave(slave, info); 2017 break; 2018 } 2019 } 2020 2021 return res; 2022 } 2023 2024 /*-------------------------------- Monitoring -------------------------------*/ 2025 2026 /* called with rcu_read_lock() */ 2027 static int bond_miimon_inspect(struct bonding *bond) 2028 { 2029 int link_state, commit = 0; 2030 struct list_head *iter; 2031 struct slave *slave; 2032 bool ignore_updelay; 2033 2034 ignore_updelay = !rcu_dereference(bond->curr_active_slave); 2035 2036 bond_for_each_slave_rcu(bond, slave, iter) { 2037 slave->new_link = BOND_LINK_NOCHANGE; 2038 2039 link_state = bond_check_dev_link(bond, slave->dev, 0); 2040 2041 switch (slave->link) { 2042 case BOND_LINK_UP: 2043 if (link_state) 2044 continue; 2045 2046 bond_propose_link_state(slave, BOND_LINK_FAIL); 2047 slave->delay = bond->params.downdelay; 2048 if (slave->delay) { 2049 netdev_info(bond->dev, "link status down for %sinterface %s, disabling it in %d ms\n", 2050 (BOND_MODE(bond) == 2051 BOND_MODE_ACTIVEBACKUP) ? 2052 (bond_is_active_slave(slave) ? 2053 "active " : "backup ") : "", 2054 slave->dev->name, 2055 bond->params.downdelay * bond->params.miimon); 2056 } 2057 /*FALLTHRU*/ 2058 case BOND_LINK_FAIL: 2059 if (link_state) { 2060 /* recovered before downdelay expired */ 2061 bond_propose_link_state(slave, BOND_LINK_UP); 2062 slave->last_link_up = jiffies; 2063 netdev_info(bond->dev, "link status up again after %d ms for interface %s\n", 2064 (bond->params.downdelay - slave->delay) * 2065 bond->params.miimon, 2066 slave->dev->name); 2067 continue; 2068 } 2069 2070 if (slave->delay <= 0) { 2071 slave->new_link = BOND_LINK_DOWN; 2072 commit++; 2073 continue; 2074 } 2075 2076 slave->delay--; 2077 break; 2078 2079 case BOND_LINK_DOWN: 2080 if (!link_state) 2081 continue; 2082 2083 bond_propose_link_state(slave, BOND_LINK_BACK); 2084 slave->delay = bond->params.updelay; 2085 2086 if (slave->delay) { 2087 netdev_info(bond->dev, "link status up for interface %s, enabling it in %d ms\n", 2088 slave->dev->name, 2089 ignore_updelay ? 0 : 2090 bond->params.updelay * 2091 bond->params.miimon); 2092 } 2093 /*FALLTHRU*/ 2094 case BOND_LINK_BACK: 2095 if (!link_state) { 2096 bond_propose_link_state(slave, BOND_LINK_DOWN); 2097 netdev_info(bond->dev, "link status down again after %d ms for interface %s\n", 2098 (bond->params.updelay - slave->delay) * 2099 bond->params.miimon, 2100 slave->dev->name); 2101 2102 continue; 2103 } 2104 2105 if (ignore_updelay) 2106 slave->delay = 0; 2107 2108 if (slave->delay <= 0) { 2109 slave->new_link = BOND_LINK_UP; 2110 commit++; 2111 ignore_updelay = false; 2112 continue; 2113 } 2114 2115 slave->delay--; 2116 break; 2117 } 2118 } 2119 2120 return commit; 2121 } 2122 2123 static void bond_miimon_commit(struct bonding *bond) 2124 { 2125 struct list_head *iter; 2126 struct slave *slave, *primary; 2127 2128 bond_for_each_slave(bond, slave, iter) { 2129 switch (slave->new_link) { 2130 case BOND_LINK_NOCHANGE: 2131 continue; 2132 2133 case BOND_LINK_UP: 2134 if (bond_update_speed_duplex(slave)) { 2135 slave->link = BOND_LINK_DOWN; 2136 netdev_warn(bond->dev, 2137 "failed to get link speed/duplex for %s\n", 2138 slave->dev->name); 2139 continue; 2140 } 2141 bond_set_slave_link_state(slave, BOND_LINK_UP, 2142 BOND_SLAVE_NOTIFY_NOW); 2143 slave->last_link_up = jiffies; 2144 2145 primary = rtnl_dereference(bond->primary_slave); 2146 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 2147 /* prevent it from being the active one */ 2148 bond_set_backup_slave(slave); 2149 } else if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) { 2150 /* make it immediately active */ 2151 bond_set_active_slave(slave); 2152 } else if (slave != primary) { 2153 /* prevent it from being the active one */ 2154 bond_set_backup_slave(slave); 2155 } 2156 2157 netdev_info(bond->dev, "link status definitely up for interface %s, %u Mbps %s duplex\n", 2158 slave->dev->name, 2159 slave->speed == SPEED_UNKNOWN ? 0 : slave->speed, 2160 slave->duplex ? "full" : "half"); 2161 2162 /* notify ad that the link status has changed */ 2163 if (BOND_MODE(bond) == BOND_MODE_8023AD) 2164 bond_3ad_handle_link_change(slave, BOND_LINK_UP); 2165 2166 if (bond_is_lb(bond)) 2167 bond_alb_handle_link_change(bond, slave, 2168 BOND_LINK_UP); 2169 2170 if (BOND_MODE(bond) == BOND_MODE_XOR) 2171 bond_update_slave_arr(bond, NULL); 2172 2173 if (!bond->curr_active_slave || slave == primary) 2174 goto do_failover; 2175 2176 continue; 2177 2178 case BOND_LINK_DOWN: 2179 if (slave->link_failure_count < UINT_MAX) 2180 slave->link_failure_count++; 2181 2182 bond_set_slave_link_state(slave, BOND_LINK_DOWN, 2183 BOND_SLAVE_NOTIFY_NOW); 2184 2185 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP || 2186 BOND_MODE(bond) == BOND_MODE_8023AD) 2187 bond_set_slave_inactive_flags(slave, 2188 BOND_SLAVE_NOTIFY_NOW); 2189 2190 netdev_info(bond->dev, "link status definitely down for interface %s, disabling it\n", 2191 slave->dev->name); 2192 2193 if (BOND_MODE(bond) == BOND_MODE_8023AD) 2194 bond_3ad_handle_link_change(slave, 2195 BOND_LINK_DOWN); 2196 2197 if (bond_is_lb(bond)) 2198 bond_alb_handle_link_change(bond, slave, 2199 BOND_LINK_DOWN); 2200 2201 if (BOND_MODE(bond) == BOND_MODE_XOR) 2202 bond_update_slave_arr(bond, NULL); 2203 2204 if (slave == rcu_access_pointer(bond->curr_active_slave)) 2205 goto do_failover; 2206 2207 continue; 2208 2209 default: 2210 netdev_err(bond->dev, "invalid new link %d on slave %s\n", 2211 slave->new_link, slave->dev->name); 2212 slave->new_link = BOND_LINK_NOCHANGE; 2213 2214 continue; 2215 } 2216 2217 do_failover: 2218 block_netpoll_tx(); 2219 bond_select_active_slave(bond); 2220 unblock_netpoll_tx(); 2221 } 2222 2223 bond_set_carrier(bond); 2224 } 2225 2226 /* bond_mii_monitor 2227 * 2228 * Really a wrapper that splits the mii monitor into two phases: an 2229 * inspection, then (if inspection indicates something needs to be done) 2230 * an acquisition of appropriate locks followed by a commit phase to 2231 * implement whatever link state changes are indicated. 2232 */ 2233 static void bond_mii_monitor(struct work_struct *work) 2234 { 2235 struct bonding *bond = container_of(work, struct bonding, 2236 mii_work.work); 2237 bool should_notify_peers = false; 2238 unsigned long delay; 2239 struct slave *slave; 2240 struct list_head *iter; 2241 2242 delay = msecs_to_jiffies(bond->params.miimon); 2243 2244 if (!bond_has_slaves(bond)) 2245 goto re_arm; 2246 2247 rcu_read_lock(); 2248 2249 should_notify_peers = bond_should_notify_peers(bond); 2250 2251 if (bond_miimon_inspect(bond)) { 2252 rcu_read_unlock(); 2253 2254 /* Race avoidance with bond_close cancel of workqueue */ 2255 if (!rtnl_trylock()) { 2256 delay = 1; 2257 should_notify_peers = false; 2258 goto re_arm; 2259 } 2260 2261 bond_for_each_slave(bond, slave, iter) { 2262 bond_commit_link_state(slave, BOND_SLAVE_NOTIFY_LATER); 2263 } 2264 bond_miimon_commit(bond); 2265 2266 rtnl_unlock(); /* might sleep, hold no other locks */ 2267 } else 2268 rcu_read_unlock(); 2269 2270 re_arm: 2271 if (bond->params.miimon) 2272 queue_delayed_work(bond->wq, &bond->mii_work, delay); 2273 2274 if (should_notify_peers) { 2275 if (!rtnl_trylock()) 2276 return; 2277 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, bond->dev); 2278 rtnl_unlock(); 2279 } 2280 } 2281 2282 static int bond_upper_dev_walk(struct net_device *upper, void *data) 2283 { 2284 __be32 ip = *((__be32 *)data); 2285 2286 return ip == bond_confirm_addr(upper, 0, ip); 2287 } 2288 2289 static bool bond_has_this_ip(struct bonding *bond, __be32 ip) 2290 { 2291 bool ret = false; 2292 2293 if (ip == bond_confirm_addr(bond->dev, 0, ip)) 2294 return true; 2295 2296 rcu_read_lock(); 2297 if (netdev_walk_all_upper_dev_rcu(bond->dev, bond_upper_dev_walk, &ip)) 2298 ret = true; 2299 rcu_read_unlock(); 2300 2301 return ret; 2302 } 2303 2304 /* We go to the (large) trouble of VLAN tagging ARP frames because 2305 * switches in VLAN mode (especially if ports are configured as 2306 * "native" to a VLAN) might not pass non-tagged frames. 2307 */ 2308 static void bond_arp_send(struct net_device *slave_dev, int arp_op, 2309 __be32 dest_ip, __be32 src_ip, 2310 struct bond_vlan_tag *tags) 2311 { 2312 struct sk_buff *skb; 2313 struct bond_vlan_tag *outer_tag = tags; 2314 2315 netdev_dbg(slave_dev, "arp %d on slave %s: dst %pI4 src %pI4\n", 2316 arp_op, slave_dev->name, &dest_ip, &src_ip); 2317 2318 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip, 2319 NULL, slave_dev->dev_addr, NULL); 2320 2321 if (!skb) { 2322 net_err_ratelimited("ARP packet allocation failed\n"); 2323 return; 2324 } 2325 2326 if (!tags || tags->vlan_proto == VLAN_N_VID) 2327 goto xmit; 2328 2329 tags++; 2330 2331 /* Go through all the tags backwards and add them to the packet */ 2332 while (tags->vlan_proto != VLAN_N_VID) { 2333 if (!tags->vlan_id) { 2334 tags++; 2335 continue; 2336 } 2337 2338 netdev_dbg(slave_dev, "inner tag: proto %X vid %X\n", 2339 ntohs(outer_tag->vlan_proto), tags->vlan_id); 2340 skb = vlan_insert_tag_set_proto(skb, tags->vlan_proto, 2341 tags->vlan_id); 2342 if (!skb) { 2343 net_err_ratelimited("failed to insert inner VLAN tag\n"); 2344 return; 2345 } 2346 2347 tags++; 2348 } 2349 /* Set the outer tag */ 2350 if (outer_tag->vlan_id) { 2351 netdev_dbg(slave_dev, "outer tag: proto %X vid %X\n", 2352 ntohs(outer_tag->vlan_proto), outer_tag->vlan_id); 2353 __vlan_hwaccel_put_tag(skb, outer_tag->vlan_proto, 2354 outer_tag->vlan_id); 2355 } 2356 2357 xmit: 2358 arp_xmit(skb); 2359 } 2360 2361 /* Validate the device path between the @start_dev and the @end_dev. 2362 * The path is valid if the @end_dev is reachable through device 2363 * stacking. 2364 * When the path is validated, collect any vlan information in the 2365 * path. 2366 */ 2367 struct bond_vlan_tag *bond_verify_device_path(struct net_device *start_dev, 2368 struct net_device *end_dev, 2369 int level) 2370 { 2371 struct bond_vlan_tag *tags; 2372 struct net_device *upper; 2373 struct list_head *iter; 2374 2375 if (start_dev == end_dev) { 2376 tags = kzalloc(sizeof(*tags) * (level + 1), GFP_ATOMIC); 2377 if (!tags) 2378 return ERR_PTR(-ENOMEM); 2379 tags[level].vlan_proto = VLAN_N_VID; 2380 return tags; 2381 } 2382 2383 netdev_for_each_upper_dev_rcu(start_dev, upper, iter) { 2384 tags = bond_verify_device_path(upper, end_dev, level + 1); 2385 if (IS_ERR_OR_NULL(tags)) { 2386 if (IS_ERR(tags)) 2387 return tags; 2388 continue; 2389 } 2390 if (is_vlan_dev(upper)) { 2391 tags[level].vlan_proto = vlan_dev_vlan_proto(upper); 2392 tags[level].vlan_id = vlan_dev_vlan_id(upper); 2393 } 2394 2395 return tags; 2396 } 2397 2398 return NULL; 2399 } 2400 2401 static void bond_arp_send_all(struct bonding *bond, struct slave *slave) 2402 { 2403 struct rtable *rt; 2404 struct bond_vlan_tag *tags; 2405 __be32 *targets = bond->params.arp_targets, addr; 2406 int i; 2407 2408 for (i = 0; i < BOND_MAX_ARP_TARGETS && targets[i]; i++) { 2409 netdev_dbg(bond->dev, "basa: target %pI4\n", &targets[i]); 2410 tags = NULL; 2411 2412 /* Find out through which dev should the packet go */ 2413 rt = ip_route_output(dev_net(bond->dev), targets[i], 0, 2414 RTO_ONLINK, 0); 2415 if (IS_ERR(rt)) { 2416 /* there's no route to target - try to send arp 2417 * probe to generate any traffic (arp_validate=0) 2418 */ 2419 if (bond->params.arp_validate) 2420 net_warn_ratelimited("%s: no route to arp_ip_target %pI4 and arp_validate is set\n", 2421 bond->dev->name, 2422 &targets[i]); 2423 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i], 2424 0, tags); 2425 continue; 2426 } 2427 2428 /* bond device itself */ 2429 if (rt->dst.dev == bond->dev) 2430 goto found; 2431 2432 rcu_read_lock(); 2433 tags = bond_verify_device_path(bond->dev, rt->dst.dev, 0); 2434 rcu_read_unlock(); 2435 2436 if (!IS_ERR_OR_NULL(tags)) 2437 goto found; 2438 2439 /* Not our device - skip */ 2440 netdev_dbg(bond->dev, "no path to arp_ip_target %pI4 via rt.dev %s\n", 2441 &targets[i], rt->dst.dev ? rt->dst.dev->name : "NULL"); 2442 2443 ip_rt_put(rt); 2444 continue; 2445 2446 found: 2447 addr = bond_confirm_addr(rt->dst.dev, targets[i], 0); 2448 ip_rt_put(rt); 2449 bond_arp_send(slave->dev, ARPOP_REQUEST, targets[i], 2450 addr, tags); 2451 kfree(tags); 2452 } 2453 } 2454 2455 static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip) 2456 { 2457 int i; 2458 2459 if (!sip || !bond_has_this_ip(bond, tip)) { 2460 netdev_dbg(bond->dev, "bva: sip %pI4 tip %pI4 not found\n", 2461 &sip, &tip); 2462 return; 2463 } 2464 2465 i = bond_get_targets_ip(bond->params.arp_targets, sip); 2466 if (i == -1) { 2467 netdev_dbg(bond->dev, "bva: sip %pI4 not found in targets\n", 2468 &sip); 2469 return; 2470 } 2471 slave->last_rx = jiffies; 2472 slave->target_last_arp_rx[i] = jiffies; 2473 } 2474 2475 int bond_arp_rcv(const struct sk_buff *skb, struct bonding *bond, 2476 struct slave *slave) 2477 { 2478 struct arphdr *arp = (struct arphdr *)skb->data; 2479 struct slave *curr_active_slave, *curr_arp_slave; 2480 unsigned char *arp_ptr; 2481 __be32 sip, tip; 2482 int alen, is_arp = skb->protocol == __cpu_to_be16(ETH_P_ARP); 2483 2484 if (!slave_do_arp_validate(bond, slave)) { 2485 if ((slave_do_arp_validate_only(bond) && is_arp) || 2486 !slave_do_arp_validate_only(bond)) 2487 slave->last_rx = jiffies; 2488 return RX_HANDLER_ANOTHER; 2489 } else if (!is_arp) { 2490 return RX_HANDLER_ANOTHER; 2491 } 2492 2493 alen = arp_hdr_len(bond->dev); 2494 2495 netdev_dbg(bond->dev, "bond_arp_rcv: skb->dev %s\n", 2496 skb->dev->name); 2497 2498 if (alen > skb_headlen(skb)) { 2499 arp = kmalloc(alen, GFP_ATOMIC); 2500 if (!arp) 2501 goto out_unlock; 2502 if (skb_copy_bits(skb, 0, arp, alen) < 0) 2503 goto out_unlock; 2504 } 2505 2506 if (arp->ar_hln != bond->dev->addr_len || 2507 skb->pkt_type == PACKET_OTHERHOST || 2508 skb->pkt_type == PACKET_LOOPBACK || 2509 arp->ar_hrd != htons(ARPHRD_ETHER) || 2510 arp->ar_pro != htons(ETH_P_IP) || 2511 arp->ar_pln != 4) 2512 goto out_unlock; 2513 2514 arp_ptr = (unsigned char *)(arp + 1); 2515 arp_ptr += bond->dev->addr_len; 2516 memcpy(&sip, arp_ptr, 4); 2517 arp_ptr += 4 + bond->dev->addr_len; 2518 memcpy(&tip, arp_ptr, 4); 2519 2520 netdev_dbg(bond->dev, "bond_arp_rcv: %s/%d av %d sv %d sip %pI4 tip %pI4\n", 2521 slave->dev->name, bond_slave_state(slave), 2522 bond->params.arp_validate, slave_do_arp_validate(bond, slave), 2523 &sip, &tip); 2524 2525 curr_active_slave = rcu_dereference(bond->curr_active_slave); 2526 curr_arp_slave = rcu_dereference(bond->current_arp_slave); 2527 2528 /* We 'trust' the received ARP enough to validate it if: 2529 * 2530 * (a) the slave receiving the ARP is active (which includes the 2531 * current ARP slave, if any), or 2532 * 2533 * (b) the receiving slave isn't active, but there is a currently 2534 * active slave and it received valid arp reply(s) after it became 2535 * the currently active slave, or 2536 * 2537 * (c) there is an ARP slave that sent an ARP during the prior ARP 2538 * interval, and we receive an ARP reply on any slave. We accept 2539 * these because switch FDB update delays may deliver the ARP 2540 * reply to a slave other than the sender of the ARP request. 2541 * 2542 * Note: for (b), backup slaves are receiving the broadcast ARP 2543 * request, not a reply. This request passes from the sending 2544 * slave through the L2 switch(es) to the receiving slave. Since 2545 * this is checking the request, sip/tip are swapped for 2546 * validation. 2547 * 2548 * This is done to avoid endless looping when we can't reach the 2549 * arp_ip_target and fool ourselves with our own arp requests. 2550 */ 2551 if (bond_is_active_slave(slave)) 2552 bond_validate_arp(bond, slave, sip, tip); 2553 else if (curr_active_slave && 2554 time_after(slave_last_rx(bond, curr_active_slave), 2555 curr_active_slave->last_link_up)) 2556 bond_validate_arp(bond, slave, tip, sip); 2557 else if (curr_arp_slave && (arp->ar_op == htons(ARPOP_REPLY)) && 2558 bond_time_in_interval(bond, 2559 dev_trans_start(curr_arp_slave->dev), 1)) 2560 bond_validate_arp(bond, slave, sip, tip); 2561 2562 out_unlock: 2563 if (arp != (struct arphdr *)skb->data) 2564 kfree(arp); 2565 return RX_HANDLER_ANOTHER; 2566 } 2567 2568 /* function to verify if we're in the arp_interval timeslice, returns true if 2569 * (last_act - arp_interval) <= jiffies <= (last_act + mod * arp_interval + 2570 * arp_interval/2) . the arp_interval/2 is needed for really fast networks. 2571 */ 2572 static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act, 2573 int mod) 2574 { 2575 int delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval); 2576 2577 return time_in_range(jiffies, 2578 last_act - delta_in_ticks, 2579 last_act + mod * delta_in_ticks + delta_in_ticks/2); 2580 } 2581 2582 /* This function is called regularly to monitor each slave's link 2583 * ensuring that traffic is being sent and received when arp monitoring 2584 * is used in load-balancing mode. if the adapter has been dormant, then an 2585 * arp is transmitted to generate traffic. see activebackup_arp_monitor for 2586 * arp monitoring in active backup mode. 2587 */ 2588 static void bond_loadbalance_arp_mon(struct bonding *bond) 2589 { 2590 struct slave *slave, *oldcurrent; 2591 struct list_head *iter; 2592 int do_failover = 0, slave_state_changed = 0; 2593 2594 if (!bond_has_slaves(bond)) 2595 goto re_arm; 2596 2597 rcu_read_lock(); 2598 2599 oldcurrent = rcu_dereference(bond->curr_active_slave); 2600 /* see if any of the previous devices are up now (i.e. they have 2601 * xmt and rcv traffic). the curr_active_slave does not come into 2602 * the picture unless it is null. also, slave->last_link_up is not 2603 * needed here because we send an arp on each slave and give a slave 2604 * as long as it needs to get the tx/rx within the delta. 2605 * TODO: what about up/down delay in arp mode? it wasn't here before 2606 * so it can wait 2607 */ 2608 bond_for_each_slave_rcu(bond, slave, iter) { 2609 unsigned long trans_start = dev_trans_start(slave->dev); 2610 2611 if (slave->link != BOND_LINK_UP) { 2612 if (bond_time_in_interval(bond, trans_start, 1) && 2613 bond_time_in_interval(bond, slave->last_rx, 1)) { 2614 2615 slave->link = BOND_LINK_UP; 2616 slave_state_changed = 1; 2617 2618 /* primary_slave has no meaning in round-robin 2619 * mode. the window of a slave being up and 2620 * curr_active_slave being null after enslaving 2621 * is closed. 2622 */ 2623 if (!oldcurrent) { 2624 netdev_info(bond->dev, "link status definitely up for interface %s\n", 2625 slave->dev->name); 2626 do_failover = 1; 2627 } else { 2628 netdev_info(bond->dev, "interface %s is now up\n", 2629 slave->dev->name); 2630 } 2631 } 2632 } else { 2633 /* slave->link == BOND_LINK_UP */ 2634 2635 /* not all switches will respond to an arp request 2636 * when the source ip is 0, so don't take the link down 2637 * if we don't know our ip yet 2638 */ 2639 if (!bond_time_in_interval(bond, trans_start, 2) || 2640 !bond_time_in_interval(bond, slave->last_rx, 2)) { 2641 2642 slave->link = BOND_LINK_DOWN; 2643 slave_state_changed = 1; 2644 2645 if (slave->link_failure_count < UINT_MAX) 2646 slave->link_failure_count++; 2647 2648 netdev_info(bond->dev, "interface %s is now down\n", 2649 slave->dev->name); 2650 2651 if (slave == oldcurrent) 2652 do_failover = 1; 2653 } 2654 } 2655 2656 /* note: if switch is in round-robin mode, all links 2657 * must tx arp to ensure all links rx an arp - otherwise 2658 * links may oscillate or not come up at all; if switch is 2659 * in something like xor mode, there is nothing we can 2660 * do - all replies will be rx'ed on same link causing slaves 2661 * to be unstable during low/no traffic periods 2662 */ 2663 if (bond_slave_is_up(slave)) 2664 bond_arp_send_all(bond, slave); 2665 } 2666 2667 rcu_read_unlock(); 2668 2669 if (do_failover || slave_state_changed) { 2670 if (!rtnl_trylock()) 2671 goto re_arm; 2672 2673 if (slave_state_changed) { 2674 bond_slave_state_change(bond); 2675 if (BOND_MODE(bond) == BOND_MODE_XOR) 2676 bond_update_slave_arr(bond, NULL); 2677 } 2678 if (do_failover) { 2679 block_netpoll_tx(); 2680 bond_select_active_slave(bond); 2681 unblock_netpoll_tx(); 2682 } 2683 rtnl_unlock(); 2684 } 2685 2686 re_arm: 2687 if (bond->params.arp_interval) 2688 queue_delayed_work(bond->wq, &bond->arp_work, 2689 msecs_to_jiffies(bond->params.arp_interval)); 2690 } 2691 2692 /* Called to inspect slaves for active-backup mode ARP monitor link state 2693 * changes. Sets new_link in slaves to specify what action should take 2694 * place for the slave. Returns 0 if no changes are found, >0 if changes 2695 * to link states must be committed. 2696 * 2697 * Called with rcu_read_lock held. 2698 */ 2699 static int bond_ab_arp_inspect(struct bonding *bond) 2700 { 2701 unsigned long trans_start, last_rx; 2702 struct list_head *iter; 2703 struct slave *slave; 2704 int commit = 0; 2705 2706 bond_for_each_slave_rcu(bond, slave, iter) { 2707 slave->new_link = BOND_LINK_NOCHANGE; 2708 last_rx = slave_last_rx(bond, slave); 2709 2710 if (slave->link != BOND_LINK_UP) { 2711 if (bond_time_in_interval(bond, last_rx, 1)) { 2712 slave->new_link = BOND_LINK_UP; 2713 commit++; 2714 } 2715 continue; 2716 } 2717 2718 /* Give slaves 2*delta after being enslaved or made 2719 * active. This avoids bouncing, as the last receive 2720 * times need a full ARP monitor cycle to be updated. 2721 */ 2722 if (bond_time_in_interval(bond, slave->last_link_up, 2)) 2723 continue; 2724 2725 /* Backup slave is down if: 2726 * - No current_arp_slave AND 2727 * - more than 3*delta since last receive AND 2728 * - the bond has an IP address 2729 * 2730 * Note: a non-null current_arp_slave indicates 2731 * the curr_active_slave went down and we are 2732 * searching for a new one; under this condition 2733 * we only take the curr_active_slave down - this 2734 * gives each slave a chance to tx/rx traffic 2735 * before being taken out 2736 */ 2737 if (!bond_is_active_slave(slave) && 2738 !rcu_access_pointer(bond->current_arp_slave) && 2739 !bond_time_in_interval(bond, last_rx, 3)) { 2740 slave->new_link = BOND_LINK_DOWN; 2741 commit++; 2742 } 2743 2744 /* Active slave is down if: 2745 * - more than 2*delta since transmitting OR 2746 * - (more than 2*delta since receive AND 2747 * the bond has an IP address) 2748 */ 2749 trans_start = dev_trans_start(slave->dev); 2750 if (bond_is_active_slave(slave) && 2751 (!bond_time_in_interval(bond, trans_start, 2) || 2752 !bond_time_in_interval(bond, last_rx, 2))) { 2753 slave->new_link = BOND_LINK_DOWN; 2754 commit++; 2755 } 2756 } 2757 2758 return commit; 2759 } 2760 2761 /* Called to commit link state changes noted by inspection step of 2762 * active-backup mode ARP monitor. 2763 * 2764 * Called with RTNL hold. 2765 */ 2766 static void bond_ab_arp_commit(struct bonding *bond) 2767 { 2768 unsigned long trans_start; 2769 struct list_head *iter; 2770 struct slave *slave; 2771 2772 bond_for_each_slave(bond, slave, iter) { 2773 switch (slave->new_link) { 2774 case BOND_LINK_NOCHANGE: 2775 continue; 2776 2777 case BOND_LINK_UP: 2778 trans_start = dev_trans_start(slave->dev); 2779 if (rtnl_dereference(bond->curr_active_slave) != slave || 2780 (!rtnl_dereference(bond->curr_active_slave) && 2781 bond_time_in_interval(bond, trans_start, 1))) { 2782 struct slave *current_arp_slave; 2783 2784 current_arp_slave = rtnl_dereference(bond->current_arp_slave); 2785 bond_set_slave_link_state(slave, BOND_LINK_UP, 2786 BOND_SLAVE_NOTIFY_NOW); 2787 if (current_arp_slave) { 2788 bond_set_slave_inactive_flags( 2789 current_arp_slave, 2790 BOND_SLAVE_NOTIFY_NOW); 2791 RCU_INIT_POINTER(bond->current_arp_slave, NULL); 2792 } 2793 2794 netdev_info(bond->dev, "link status definitely up for interface %s\n", 2795 slave->dev->name); 2796 2797 if (!rtnl_dereference(bond->curr_active_slave) || 2798 slave == rtnl_dereference(bond->primary_slave)) 2799 goto do_failover; 2800 2801 } 2802 2803 continue; 2804 2805 case BOND_LINK_DOWN: 2806 if (slave->link_failure_count < UINT_MAX) 2807 slave->link_failure_count++; 2808 2809 bond_set_slave_link_state(slave, BOND_LINK_DOWN, 2810 BOND_SLAVE_NOTIFY_NOW); 2811 bond_set_slave_inactive_flags(slave, 2812 BOND_SLAVE_NOTIFY_NOW); 2813 2814 netdev_info(bond->dev, "link status definitely down for interface %s, disabling it\n", 2815 slave->dev->name); 2816 2817 if (slave == rtnl_dereference(bond->curr_active_slave)) { 2818 RCU_INIT_POINTER(bond->current_arp_slave, NULL); 2819 goto do_failover; 2820 } 2821 2822 continue; 2823 2824 default: 2825 netdev_err(bond->dev, "impossible: new_link %d on slave %s\n", 2826 slave->new_link, slave->dev->name); 2827 continue; 2828 } 2829 2830 do_failover: 2831 block_netpoll_tx(); 2832 bond_select_active_slave(bond); 2833 unblock_netpoll_tx(); 2834 } 2835 2836 bond_set_carrier(bond); 2837 } 2838 2839 /* Send ARP probes for active-backup mode ARP monitor. 2840 * 2841 * Called with rcu_read_lock held. 2842 */ 2843 static bool bond_ab_arp_probe(struct bonding *bond) 2844 { 2845 struct slave *slave, *before = NULL, *new_slave = NULL, 2846 *curr_arp_slave = rcu_dereference(bond->current_arp_slave), 2847 *curr_active_slave = rcu_dereference(bond->curr_active_slave); 2848 struct list_head *iter; 2849 bool found = false; 2850 bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER; 2851 2852 if (curr_arp_slave && curr_active_slave) 2853 netdev_info(bond->dev, "PROBE: c_arp %s && cas %s BAD\n", 2854 curr_arp_slave->dev->name, 2855 curr_active_slave->dev->name); 2856 2857 if (curr_active_slave) { 2858 bond_arp_send_all(bond, curr_active_slave); 2859 return should_notify_rtnl; 2860 } 2861 2862 /* if we don't have a curr_active_slave, search for the next available 2863 * backup slave from the current_arp_slave and make it the candidate 2864 * for becoming the curr_active_slave 2865 */ 2866 2867 if (!curr_arp_slave) { 2868 curr_arp_slave = bond_first_slave_rcu(bond); 2869 if (!curr_arp_slave) 2870 return should_notify_rtnl; 2871 } 2872 2873 bond_set_slave_inactive_flags(curr_arp_slave, BOND_SLAVE_NOTIFY_LATER); 2874 2875 bond_for_each_slave_rcu(bond, slave, iter) { 2876 if (!found && !before && bond_slave_is_up(slave)) 2877 before = slave; 2878 2879 if (found && !new_slave && bond_slave_is_up(slave)) 2880 new_slave = slave; 2881 /* if the link state is up at this point, we 2882 * mark it down - this can happen if we have 2883 * simultaneous link failures and 2884 * reselect_active_interface doesn't make this 2885 * one the current slave so it is still marked 2886 * up when it is actually down 2887 */ 2888 if (!bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) { 2889 bond_set_slave_link_state(slave, BOND_LINK_DOWN, 2890 BOND_SLAVE_NOTIFY_LATER); 2891 if (slave->link_failure_count < UINT_MAX) 2892 slave->link_failure_count++; 2893 2894 bond_set_slave_inactive_flags(slave, 2895 BOND_SLAVE_NOTIFY_LATER); 2896 2897 netdev_info(bond->dev, "backup interface %s is now down\n", 2898 slave->dev->name); 2899 } 2900 if (slave == curr_arp_slave) 2901 found = true; 2902 } 2903 2904 if (!new_slave && before) 2905 new_slave = before; 2906 2907 if (!new_slave) 2908 goto check_state; 2909 2910 bond_set_slave_link_state(new_slave, BOND_LINK_BACK, 2911 BOND_SLAVE_NOTIFY_LATER); 2912 bond_set_slave_active_flags(new_slave, BOND_SLAVE_NOTIFY_LATER); 2913 bond_arp_send_all(bond, new_slave); 2914 new_slave->last_link_up = jiffies; 2915 rcu_assign_pointer(bond->current_arp_slave, new_slave); 2916 2917 check_state: 2918 bond_for_each_slave_rcu(bond, slave, iter) { 2919 if (slave->should_notify || slave->should_notify_link) { 2920 should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW; 2921 break; 2922 } 2923 } 2924 return should_notify_rtnl; 2925 } 2926 2927 static void bond_activebackup_arp_mon(struct bonding *bond) 2928 { 2929 bool should_notify_peers = false; 2930 bool should_notify_rtnl = false; 2931 int delta_in_ticks; 2932 2933 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval); 2934 2935 if (!bond_has_slaves(bond)) 2936 goto re_arm; 2937 2938 rcu_read_lock(); 2939 2940 should_notify_peers = bond_should_notify_peers(bond); 2941 2942 if (bond_ab_arp_inspect(bond)) { 2943 rcu_read_unlock(); 2944 2945 /* Race avoidance with bond_close flush of workqueue */ 2946 if (!rtnl_trylock()) { 2947 delta_in_ticks = 1; 2948 should_notify_peers = false; 2949 goto re_arm; 2950 } 2951 2952 bond_ab_arp_commit(bond); 2953 2954 rtnl_unlock(); 2955 rcu_read_lock(); 2956 } 2957 2958 should_notify_rtnl = bond_ab_arp_probe(bond); 2959 rcu_read_unlock(); 2960 2961 re_arm: 2962 if (bond->params.arp_interval) 2963 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks); 2964 2965 if (should_notify_peers || should_notify_rtnl) { 2966 if (!rtnl_trylock()) 2967 return; 2968 2969 if (should_notify_peers) 2970 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, 2971 bond->dev); 2972 if (should_notify_rtnl) { 2973 bond_slave_state_notify(bond); 2974 bond_slave_link_notify(bond); 2975 } 2976 2977 rtnl_unlock(); 2978 } 2979 } 2980 2981 static void bond_arp_monitor(struct work_struct *work) 2982 { 2983 struct bonding *bond = container_of(work, struct bonding, 2984 arp_work.work); 2985 2986 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) 2987 bond_activebackup_arp_mon(bond); 2988 else 2989 bond_loadbalance_arp_mon(bond); 2990 } 2991 2992 /*-------------------------- netdev event handling --------------------------*/ 2993 2994 /* Change device name */ 2995 static int bond_event_changename(struct bonding *bond) 2996 { 2997 bond_remove_proc_entry(bond); 2998 bond_create_proc_entry(bond); 2999 3000 bond_debug_reregister(bond); 3001 3002 return NOTIFY_DONE; 3003 } 3004 3005 static int bond_master_netdev_event(unsigned long event, 3006 struct net_device *bond_dev) 3007 { 3008 struct bonding *event_bond = netdev_priv(bond_dev); 3009 3010 switch (event) { 3011 case NETDEV_CHANGENAME: 3012 return bond_event_changename(event_bond); 3013 case NETDEV_UNREGISTER: 3014 bond_remove_proc_entry(event_bond); 3015 break; 3016 case NETDEV_REGISTER: 3017 bond_create_proc_entry(event_bond); 3018 break; 3019 case NETDEV_NOTIFY_PEERS: 3020 if (event_bond->send_peer_notif) 3021 event_bond->send_peer_notif--; 3022 break; 3023 default: 3024 break; 3025 } 3026 3027 return NOTIFY_DONE; 3028 } 3029 3030 static int bond_slave_netdev_event(unsigned long event, 3031 struct net_device *slave_dev) 3032 { 3033 struct slave *slave = bond_slave_get_rtnl(slave_dev), *primary; 3034 struct bonding *bond; 3035 struct net_device *bond_dev; 3036 3037 /* A netdev event can be generated while enslaving a device 3038 * before netdev_rx_handler_register is called in which case 3039 * slave will be NULL 3040 */ 3041 if (!slave) 3042 return NOTIFY_DONE; 3043 bond_dev = slave->bond->dev; 3044 bond = slave->bond; 3045 primary = rtnl_dereference(bond->primary_slave); 3046 3047 switch (event) { 3048 case NETDEV_UNREGISTER: 3049 if (bond_dev->type != ARPHRD_ETHER) 3050 bond_release_and_destroy(bond_dev, slave_dev); 3051 else 3052 bond_release(bond_dev, slave_dev); 3053 break; 3054 case NETDEV_UP: 3055 case NETDEV_CHANGE: 3056 bond_update_speed_duplex(slave); 3057 if (BOND_MODE(bond) == BOND_MODE_8023AD) 3058 bond_3ad_adapter_speed_duplex_changed(slave); 3059 /* Fallthrough */ 3060 case NETDEV_DOWN: 3061 /* Refresh slave-array if applicable! 3062 * If the setup does not use miimon or arpmon (mode-specific!), 3063 * then these events will not cause the slave-array to be 3064 * refreshed. This will cause xmit to use a slave that is not 3065 * usable. Avoid such situation by refeshing the array at these 3066 * events. If these (miimon/arpmon) parameters are configured 3067 * then array gets refreshed twice and that should be fine! 3068 */ 3069 if (bond_mode_uses_xmit_hash(bond)) 3070 bond_update_slave_arr(bond, NULL); 3071 break; 3072 case NETDEV_CHANGEMTU: 3073 /* TODO: Should slaves be allowed to 3074 * independently alter their MTU? For 3075 * an active-backup bond, slaves need 3076 * not be the same type of device, so 3077 * MTUs may vary. For other modes, 3078 * slaves arguably should have the 3079 * same MTUs. To do this, we'd need to 3080 * take over the slave's change_mtu 3081 * function for the duration of their 3082 * servitude. 3083 */ 3084 break; 3085 case NETDEV_CHANGENAME: 3086 /* we don't care if we don't have primary set */ 3087 if (!bond_uses_primary(bond) || 3088 !bond->params.primary[0]) 3089 break; 3090 3091 if (slave == primary) { 3092 /* slave's name changed - he's no longer primary */ 3093 RCU_INIT_POINTER(bond->primary_slave, NULL); 3094 } else if (!strcmp(slave_dev->name, bond->params.primary)) { 3095 /* we have a new primary slave */ 3096 rcu_assign_pointer(bond->primary_slave, slave); 3097 } else { /* we didn't change primary - exit */ 3098 break; 3099 } 3100 3101 netdev_info(bond->dev, "Primary slave changed to %s, reselecting active slave\n", 3102 primary ? slave_dev->name : "none"); 3103 3104 block_netpoll_tx(); 3105 bond_select_active_slave(bond); 3106 unblock_netpoll_tx(); 3107 break; 3108 case NETDEV_FEAT_CHANGE: 3109 bond_compute_features(bond); 3110 break; 3111 case NETDEV_RESEND_IGMP: 3112 /* Propagate to master device */ 3113 call_netdevice_notifiers(event, slave->bond->dev); 3114 break; 3115 default: 3116 break; 3117 } 3118 3119 return NOTIFY_DONE; 3120 } 3121 3122 /* bond_netdev_event: handle netdev notifier chain events. 3123 * 3124 * This function receives events for the netdev chain. The caller (an 3125 * ioctl handler calling blocking_notifier_call_chain) holds the necessary 3126 * locks for us to safely manipulate the slave devices (RTNL lock, 3127 * dev_probe_lock). 3128 */ 3129 static int bond_netdev_event(struct notifier_block *this, 3130 unsigned long event, void *ptr) 3131 { 3132 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr); 3133 3134 netdev_dbg(event_dev, "event: %lx\n", event); 3135 3136 if (!(event_dev->priv_flags & IFF_BONDING)) 3137 return NOTIFY_DONE; 3138 3139 if (event_dev->flags & IFF_MASTER) { 3140 netdev_dbg(event_dev, "IFF_MASTER\n"); 3141 return bond_master_netdev_event(event, event_dev); 3142 } 3143 3144 if (event_dev->flags & IFF_SLAVE) { 3145 netdev_dbg(event_dev, "IFF_SLAVE\n"); 3146 return bond_slave_netdev_event(event, event_dev); 3147 } 3148 3149 return NOTIFY_DONE; 3150 } 3151 3152 static struct notifier_block bond_netdev_notifier = { 3153 .notifier_call = bond_netdev_event, 3154 }; 3155 3156 /*---------------------------- Hashing Policies -----------------------------*/ 3157 3158 /* L2 hash helper */ 3159 static inline u32 bond_eth_hash(struct sk_buff *skb) 3160 { 3161 struct ethhdr *ep, hdr_tmp; 3162 3163 ep = skb_header_pointer(skb, 0, sizeof(hdr_tmp), &hdr_tmp); 3164 if (ep) 3165 return ep->h_dest[5] ^ ep->h_source[5] ^ ep->h_proto; 3166 return 0; 3167 } 3168 3169 /* Extract the appropriate headers based on bond's xmit policy */ 3170 static bool bond_flow_dissect(struct bonding *bond, struct sk_buff *skb, 3171 struct flow_keys *fk) 3172 { 3173 const struct ipv6hdr *iph6; 3174 const struct iphdr *iph; 3175 int noff, proto = -1; 3176 3177 if (bond->params.xmit_policy > BOND_XMIT_POLICY_LAYER23) 3178 return skb_flow_dissect_flow_keys(skb, fk, 0); 3179 3180 fk->ports.ports = 0; 3181 noff = skb_network_offset(skb); 3182 if (skb->protocol == htons(ETH_P_IP)) { 3183 if (unlikely(!pskb_may_pull(skb, noff + sizeof(*iph)))) 3184 return false; 3185 iph = ip_hdr(skb); 3186 iph_to_flow_copy_v4addrs(fk, iph); 3187 noff += iph->ihl << 2; 3188 if (!ip_is_fragment(iph)) 3189 proto = iph->protocol; 3190 } else if (skb->protocol == htons(ETH_P_IPV6)) { 3191 if (unlikely(!pskb_may_pull(skb, noff + sizeof(*iph6)))) 3192 return false; 3193 iph6 = ipv6_hdr(skb); 3194 iph_to_flow_copy_v6addrs(fk, iph6); 3195 noff += sizeof(*iph6); 3196 proto = iph6->nexthdr; 3197 } else { 3198 return false; 3199 } 3200 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34 && proto >= 0) 3201 fk->ports.ports = skb_flow_get_ports(skb, noff, proto); 3202 3203 return true; 3204 } 3205 3206 /** 3207 * bond_xmit_hash - generate a hash value based on the xmit policy 3208 * @bond: bonding device 3209 * @skb: buffer to use for headers 3210 * 3211 * This function will extract the necessary headers from the skb buffer and use 3212 * them to generate a hash based on the xmit_policy set in the bonding device 3213 */ 3214 u32 bond_xmit_hash(struct bonding *bond, struct sk_buff *skb) 3215 { 3216 struct flow_keys flow; 3217 u32 hash; 3218 3219 if (bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP34 && 3220 skb->l4_hash) 3221 return skb->hash; 3222 3223 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER2 || 3224 !bond_flow_dissect(bond, skb, &flow)) 3225 return bond_eth_hash(skb); 3226 3227 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER23 || 3228 bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP23) 3229 hash = bond_eth_hash(skb); 3230 else 3231 hash = (__force u32)flow.ports.ports; 3232 hash ^= (__force u32)flow_get_u32_dst(&flow) ^ 3233 (__force u32)flow_get_u32_src(&flow); 3234 hash ^= (hash >> 16); 3235 hash ^= (hash >> 8); 3236 3237 return hash; 3238 } 3239 3240 /*-------------------------- Device entry points ----------------------------*/ 3241 3242 static void bond_work_init_all(struct bonding *bond) 3243 { 3244 INIT_DELAYED_WORK(&bond->mcast_work, 3245 bond_resend_igmp_join_requests_delayed); 3246 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor); 3247 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor); 3248 INIT_DELAYED_WORK(&bond->arp_work, bond_arp_monitor); 3249 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler); 3250 INIT_DELAYED_WORK(&bond->slave_arr_work, bond_slave_arr_handler); 3251 } 3252 3253 static void bond_work_cancel_all(struct bonding *bond) 3254 { 3255 cancel_delayed_work_sync(&bond->mii_work); 3256 cancel_delayed_work_sync(&bond->arp_work); 3257 cancel_delayed_work_sync(&bond->alb_work); 3258 cancel_delayed_work_sync(&bond->ad_work); 3259 cancel_delayed_work_sync(&bond->mcast_work); 3260 cancel_delayed_work_sync(&bond->slave_arr_work); 3261 } 3262 3263 static int bond_open(struct net_device *bond_dev) 3264 { 3265 struct bonding *bond = netdev_priv(bond_dev); 3266 struct list_head *iter; 3267 struct slave *slave; 3268 3269 /* reset slave->backup and slave->inactive */ 3270 if (bond_has_slaves(bond)) { 3271 bond_for_each_slave(bond, slave, iter) { 3272 if (bond_uses_primary(bond) && 3273 slave != rcu_access_pointer(bond->curr_active_slave)) { 3274 bond_set_slave_inactive_flags(slave, 3275 BOND_SLAVE_NOTIFY_NOW); 3276 } else if (BOND_MODE(bond) != BOND_MODE_8023AD) { 3277 bond_set_slave_active_flags(slave, 3278 BOND_SLAVE_NOTIFY_NOW); 3279 } 3280 } 3281 } 3282 3283 if (bond_is_lb(bond)) { 3284 /* bond_alb_initialize must be called before the timer 3285 * is started. 3286 */ 3287 if (bond_alb_initialize(bond, (BOND_MODE(bond) == BOND_MODE_ALB))) 3288 return -ENOMEM; 3289 if (bond->params.tlb_dynamic_lb) 3290 queue_delayed_work(bond->wq, &bond->alb_work, 0); 3291 } 3292 3293 if (bond->params.miimon) /* link check interval, in milliseconds. */ 3294 queue_delayed_work(bond->wq, &bond->mii_work, 0); 3295 3296 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */ 3297 queue_delayed_work(bond->wq, &bond->arp_work, 0); 3298 bond->recv_probe = bond_arp_rcv; 3299 } 3300 3301 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 3302 queue_delayed_work(bond->wq, &bond->ad_work, 0); 3303 /* register to receive LACPDUs */ 3304 bond->recv_probe = bond_3ad_lacpdu_recv; 3305 bond_3ad_initiate_agg_selection(bond, 1); 3306 } 3307 3308 if (bond_mode_uses_xmit_hash(bond)) 3309 bond_update_slave_arr(bond, NULL); 3310 3311 return 0; 3312 } 3313 3314 static int bond_close(struct net_device *bond_dev) 3315 { 3316 struct bonding *bond = netdev_priv(bond_dev); 3317 3318 bond_work_cancel_all(bond); 3319 bond->send_peer_notif = 0; 3320 if (bond_is_lb(bond)) 3321 bond_alb_deinitialize(bond); 3322 bond->recv_probe = NULL; 3323 3324 return 0; 3325 } 3326 3327 /* fold stats, assuming all rtnl_link_stats64 fields are u64, but 3328 * that some drivers can provide 32bit values only. 3329 */ 3330 static void bond_fold_stats(struct rtnl_link_stats64 *_res, 3331 const struct rtnl_link_stats64 *_new, 3332 const struct rtnl_link_stats64 *_old) 3333 { 3334 const u64 *new = (const u64 *)_new; 3335 const u64 *old = (const u64 *)_old; 3336 u64 *res = (u64 *)_res; 3337 int i; 3338 3339 for (i = 0; i < sizeof(*_res) / sizeof(u64); i++) { 3340 u64 nv = new[i]; 3341 u64 ov = old[i]; 3342 s64 delta = nv - ov; 3343 3344 /* detects if this particular field is 32bit only */ 3345 if (((nv | ov) >> 32) == 0) 3346 delta = (s64)(s32)((u32)nv - (u32)ov); 3347 3348 /* filter anomalies, some drivers reset their stats 3349 * at down/up events. 3350 */ 3351 if (delta > 0) 3352 res[i] += delta; 3353 } 3354 } 3355 3356 static void bond_get_stats(struct net_device *bond_dev, 3357 struct rtnl_link_stats64 *stats) 3358 { 3359 struct bonding *bond = netdev_priv(bond_dev); 3360 struct rtnl_link_stats64 temp; 3361 struct list_head *iter; 3362 struct slave *slave; 3363 3364 spin_lock(&bond->stats_lock); 3365 memcpy(stats, &bond->bond_stats, sizeof(*stats)); 3366 3367 rcu_read_lock(); 3368 bond_for_each_slave_rcu(bond, slave, iter) { 3369 const struct rtnl_link_stats64 *new = 3370 dev_get_stats(slave->dev, &temp); 3371 3372 bond_fold_stats(stats, new, &slave->slave_stats); 3373 3374 /* save off the slave stats for the next run */ 3375 memcpy(&slave->slave_stats, new, sizeof(*new)); 3376 } 3377 rcu_read_unlock(); 3378 3379 memcpy(&bond->bond_stats, stats, sizeof(*stats)); 3380 spin_unlock(&bond->stats_lock); 3381 } 3382 3383 static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd) 3384 { 3385 struct bonding *bond = netdev_priv(bond_dev); 3386 struct net_device *slave_dev = NULL; 3387 struct ifbond k_binfo; 3388 struct ifbond __user *u_binfo = NULL; 3389 struct ifslave k_sinfo; 3390 struct ifslave __user *u_sinfo = NULL; 3391 struct mii_ioctl_data *mii = NULL; 3392 struct bond_opt_value newval; 3393 struct net *net; 3394 int res = 0; 3395 3396 netdev_dbg(bond_dev, "bond_ioctl: cmd=%d\n", cmd); 3397 3398 switch (cmd) { 3399 case SIOCGMIIPHY: 3400 mii = if_mii(ifr); 3401 if (!mii) 3402 return -EINVAL; 3403 3404 mii->phy_id = 0; 3405 /* Fall Through */ 3406 case SIOCGMIIREG: 3407 /* We do this again just in case we were called by SIOCGMIIREG 3408 * instead of SIOCGMIIPHY. 3409 */ 3410 mii = if_mii(ifr); 3411 if (!mii) 3412 return -EINVAL; 3413 3414 if (mii->reg_num == 1) { 3415 mii->val_out = 0; 3416 if (netif_carrier_ok(bond->dev)) 3417 mii->val_out = BMSR_LSTATUS; 3418 } 3419 3420 return 0; 3421 case BOND_INFO_QUERY_OLD: 3422 case SIOCBONDINFOQUERY: 3423 u_binfo = (struct ifbond __user *)ifr->ifr_data; 3424 3425 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) 3426 return -EFAULT; 3427 3428 bond_info_query(bond_dev, &k_binfo); 3429 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) 3430 return -EFAULT; 3431 3432 return 0; 3433 case BOND_SLAVE_INFO_QUERY_OLD: 3434 case SIOCBONDSLAVEINFOQUERY: 3435 u_sinfo = (struct ifslave __user *)ifr->ifr_data; 3436 3437 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) 3438 return -EFAULT; 3439 3440 res = bond_slave_info_query(bond_dev, &k_sinfo); 3441 if (res == 0 && 3442 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) 3443 return -EFAULT; 3444 3445 return res; 3446 default: 3447 break; 3448 } 3449 3450 net = dev_net(bond_dev); 3451 3452 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 3453 return -EPERM; 3454 3455 slave_dev = __dev_get_by_name(net, ifr->ifr_slave); 3456 3457 netdev_dbg(bond_dev, "slave_dev=%p:\n", slave_dev); 3458 3459 if (!slave_dev) 3460 return -ENODEV; 3461 3462 netdev_dbg(bond_dev, "slave_dev->name=%s:\n", slave_dev->name); 3463 switch (cmd) { 3464 case BOND_ENSLAVE_OLD: 3465 case SIOCBONDENSLAVE: 3466 res = bond_enslave(bond_dev, slave_dev); 3467 break; 3468 case BOND_RELEASE_OLD: 3469 case SIOCBONDRELEASE: 3470 res = bond_release(bond_dev, slave_dev); 3471 break; 3472 case BOND_SETHWADDR_OLD: 3473 case SIOCBONDSETHWADDR: 3474 bond_set_dev_addr(bond_dev, slave_dev); 3475 res = 0; 3476 break; 3477 case BOND_CHANGE_ACTIVE_OLD: 3478 case SIOCBONDCHANGEACTIVE: 3479 bond_opt_initstr(&newval, slave_dev->name); 3480 res = __bond_opt_set(bond, BOND_OPT_ACTIVE_SLAVE, &newval); 3481 break; 3482 default: 3483 res = -EOPNOTSUPP; 3484 } 3485 3486 return res; 3487 } 3488 3489 static void bond_change_rx_flags(struct net_device *bond_dev, int change) 3490 { 3491 struct bonding *bond = netdev_priv(bond_dev); 3492 3493 if (change & IFF_PROMISC) 3494 bond_set_promiscuity(bond, 3495 bond_dev->flags & IFF_PROMISC ? 1 : -1); 3496 3497 if (change & IFF_ALLMULTI) 3498 bond_set_allmulti(bond, 3499 bond_dev->flags & IFF_ALLMULTI ? 1 : -1); 3500 } 3501 3502 static void bond_set_rx_mode(struct net_device *bond_dev) 3503 { 3504 struct bonding *bond = netdev_priv(bond_dev); 3505 struct list_head *iter; 3506 struct slave *slave; 3507 3508 rcu_read_lock(); 3509 if (bond_uses_primary(bond)) { 3510 slave = rcu_dereference(bond->curr_active_slave); 3511 if (slave) { 3512 dev_uc_sync(slave->dev, bond_dev); 3513 dev_mc_sync(slave->dev, bond_dev); 3514 } 3515 } else { 3516 bond_for_each_slave_rcu(bond, slave, iter) { 3517 dev_uc_sync_multiple(slave->dev, bond_dev); 3518 dev_mc_sync_multiple(slave->dev, bond_dev); 3519 } 3520 } 3521 rcu_read_unlock(); 3522 } 3523 3524 static int bond_neigh_init(struct neighbour *n) 3525 { 3526 struct bonding *bond = netdev_priv(n->dev); 3527 const struct net_device_ops *slave_ops; 3528 struct neigh_parms parms; 3529 struct slave *slave; 3530 int ret; 3531 3532 slave = bond_first_slave(bond); 3533 if (!slave) 3534 return 0; 3535 slave_ops = slave->dev->netdev_ops; 3536 if (!slave_ops->ndo_neigh_setup) 3537 return 0; 3538 3539 parms.neigh_setup = NULL; 3540 parms.neigh_cleanup = NULL; 3541 ret = slave_ops->ndo_neigh_setup(slave->dev, &parms); 3542 if (ret) 3543 return ret; 3544 3545 /* Assign slave's neigh_cleanup to neighbour in case cleanup is called 3546 * after the last slave has been detached. Assumes that all slaves 3547 * utilize the same neigh_cleanup (true at this writing as only user 3548 * is ipoib). 3549 */ 3550 n->parms->neigh_cleanup = parms.neigh_cleanup; 3551 3552 if (!parms.neigh_setup) 3553 return 0; 3554 3555 return parms.neigh_setup(n); 3556 } 3557 3558 /* The bonding ndo_neigh_setup is called at init time beofre any 3559 * slave exists. So we must declare proxy setup function which will 3560 * be used at run time to resolve the actual slave neigh param setup. 3561 * 3562 * It's also called by master devices (such as vlans) to setup their 3563 * underlying devices. In that case - do nothing, we're already set up from 3564 * our init. 3565 */ 3566 static int bond_neigh_setup(struct net_device *dev, 3567 struct neigh_parms *parms) 3568 { 3569 /* modify only our neigh_parms */ 3570 if (parms->dev == dev) 3571 parms->neigh_setup = bond_neigh_init; 3572 3573 return 0; 3574 } 3575 3576 /* Change the MTU of all of a master's slaves to match the master */ 3577 static int bond_change_mtu(struct net_device *bond_dev, int new_mtu) 3578 { 3579 struct bonding *bond = netdev_priv(bond_dev); 3580 struct slave *slave, *rollback_slave; 3581 struct list_head *iter; 3582 int res = 0; 3583 3584 netdev_dbg(bond_dev, "bond=%p, new_mtu=%d\n", bond, new_mtu); 3585 3586 bond_for_each_slave(bond, slave, iter) { 3587 netdev_dbg(bond_dev, "s %p c_m %p\n", 3588 slave, slave->dev->netdev_ops->ndo_change_mtu); 3589 3590 res = dev_set_mtu(slave->dev, new_mtu); 3591 3592 if (res) { 3593 /* If we failed to set the slave's mtu to the new value 3594 * we must abort the operation even in ACTIVE_BACKUP 3595 * mode, because if we allow the backup slaves to have 3596 * different mtu values than the active slave we'll 3597 * need to change their mtu when doing a failover. That 3598 * means changing their mtu from timer context, which 3599 * is probably not a good idea. 3600 */ 3601 netdev_dbg(bond_dev, "err %d %s\n", res, 3602 slave->dev->name); 3603 goto unwind; 3604 } 3605 } 3606 3607 bond_dev->mtu = new_mtu; 3608 3609 return 0; 3610 3611 unwind: 3612 /* unwind from head to the slave that failed */ 3613 bond_for_each_slave(bond, rollback_slave, iter) { 3614 int tmp_res; 3615 3616 if (rollback_slave == slave) 3617 break; 3618 3619 tmp_res = dev_set_mtu(rollback_slave->dev, bond_dev->mtu); 3620 if (tmp_res) { 3621 netdev_dbg(bond_dev, "unwind err %d dev %s\n", 3622 tmp_res, rollback_slave->dev->name); 3623 } 3624 } 3625 3626 return res; 3627 } 3628 3629 /* Change HW address 3630 * 3631 * Note that many devices must be down to change the HW address, and 3632 * downing the master releases all slaves. We can make bonds full of 3633 * bonding devices to test this, however. 3634 */ 3635 static int bond_set_mac_address(struct net_device *bond_dev, void *addr) 3636 { 3637 struct bonding *bond = netdev_priv(bond_dev); 3638 struct slave *slave, *rollback_slave; 3639 struct sockaddr_storage *ss = addr, tmp_ss; 3640 struct list_head *iter; 3641 int res = 0; 3642 3643 if (BOND_MODE(bond) == BOND_MODE_ALB) 3644 return bond_alb_set_mac_address(bond_dev, addr); 3645 3646 3647 netdev_dbg(bond_dev, "bond=%p\n", bond); 3648 3649 /* If fail_over_mac is enabled, do nothing and return success. 3650 * Returning an error causes ifenslave to fail. 3651 */ 3652 if (bond->params.fail_over_mac && 3653 BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) 3654 return 0; 3655 3656 if (!is_valid_ether_addr(ss->__data)) 3657 return -EADDRNOTAVAIL; 3658 3659 bond_for_each_slave(bond, slave, iter) { 3660 netdev_dbg(bond_dev, "slave %p %s\n", slave, slave->dev->name); 3661 res = dev_set_mac_address(slave->dev, addr); 3662 if (res) { 3663 /* TODO: consider downing the slave 3664 * and retry ? 3665 * User should expect communications 3666 * breakage anyway until ARP finish 3667 * updating, so... 3668 */ 3669 netdev_dbg(bond_dev, "err %d %s\n", res, slave->dev->name); 3670 goto unwind; 3671 } 3672 } 3673 3674 /* success */ 3675 memcpy(bond_dev->dev_addr, ss->__data, bond_dev->addr_len); 3676 return 0; 3677 3678 unwind: 3679 memcpy(tmp_ss.__data, bond_dev->dev_addr, bond_dev->addr_len); 3680 tmp_ss.ss_family = bond_dev->type; 3681 3682 /* unwind from head to the slave that failed */ 3683 bond_for_each_slave(bond, rollback_slave, iter) { 3684 int tmp_res; 3685 3686 if (rollback_slave == slave) 3687 break; 3688 3689 tmp_res = dev_set_mac_address(rollback_slave->dev, 3690 (struct sockaddr *)&tmp_ss); 3691 if (tmp_res) { 3692 netdev_dbg(bond_dev, "unwind err %d dev %s\n", 3693 tmp_res, rollback_slave->dev->name); 3694 } 3695 } 3696 3697 return res; 3698 } 3699 3700 /** 3701 * bond_xmit_slave_id - transmit skb through slave with slave_id 3702 * @bond: bonding device that is transmitting 3703 * @skb: buffer to transmit 3704 * @slave_id: slave id up to slave_cnt-1 through which to transmit 3705 * 3706 * This function tries to transmit through slave with slave_id but in case 3707 * it fails, it tries to find the first available slave for transmission. 3708 * The skb is consumed in all cases, thus the function is void. 3709 */ 3710 static void bond_xmit_slave_id(struct bonding *bond, struct sk_buff *skb, int slave_id) 3711 { 3712 struct list_head *iter; 3713 struct slave *slave; 3714 int i = slave_id; 3715 3716 /* Here we start from the slave with slave_id */ 3717 bond_for_each_slave_rcu(bond, slave, iter) { 3718 if (--i < 0) { 3719 if (bond_slave_can_tx(slave)) { 3720 bond_dev_queue_xmit(bond, skb, slave->dev); 3721 return; 3722 } 3723 } 3724 } 3725 3726 /* Here we start from the first slave up to slave_id */ 3727 i = slave_id; 3728 bond_for_each_slave_rcu(bond, slave, iter) { 3729 if (--i < 0) 3730 break; 3731 if (bond_slave_can_tx(slave)) { 3732 bond_dev_queue_xmit(bond, skb, slave->dev); 3733 return; 3734 } 3735 } 3736 /* no slave that can tx has been found */ 3737 bond_tx_drop(bond->dev, skb); 3738 } 3739 3740 /** 3741 * bond_rr_gen_slave_id - generate slave id based on packets_per_slave 3742 * @bond: bonding device to use 3743 * 3744 * Based on the value of the bonding device's packets_per_slave parameter 3745 * this function generates a slave id, which is usually used as the next 3746 * slave to transmit through. 3747 */ 3748 static u32 bond_rr_gen_slave_id(struct bonding *bond) 3749 { 3750 u32 slave_id; 3751 struct reciprocal_value reciprocal_packets_per_slave; 3752 int packets_per_slave = bond->params.packets_per_slave; 3753 3754 switch (packets_per_slave) { 3755 case 0: 3756 slave_id = prandom_u32(); 3757 break; 3758 case 1: 3759 slave_id = bond->rr_tx_counter; 3760 break; 3761 default: 3762 reciprocal_packets_per_slave = 3763 bond->params.reciprocal_packets_per_slave; 3764 slave_id = reciprocal_divide(bond->rr_tx_counter, 3765 reciprocal_packets_per_slave); 3766 break; 3767 } 3768 bond->rr_tx_counter++; 3769 3770 return slave_id; 3771 } 3772 3773 static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev) 3774 { 3775 struct bonding *bond = netdev_priv(bond_dev); 3776 struct iphdr *iph = ip_hdr(skb); 3777 struct slave *slave; 3778 u32 slave_id; 3779 3780 /* Start with the curr_active_slave that joined the bond as the 3781 * default for sending IGMP traffic. For failover purposes one 3782 * needs to maintain some consistency for the interface that will 3783 * send the join/membership reports. The curr_active_slave found 3784 * will send all of this type of traffic. 3785 */ 3786 if (iph->protocol == IPPROTO_IGMP && skb->protocol == htons(ETH_P_IP)) { 3787 slave = rcu_dereference(bond->curr_active_slave); 3788 if (slave) 3789 bond_dev_queue_xmit(bond, skb, slave->dev); 3790 else 3791 bond_xmit_slave_id(bond, skb, 0); 3792 } else { 3793 int slave_cnt = ACCESS_ONCE(bond->slave_cnt); 3794 3795 if (likely(slave_cnt)) { 3796 slave_id = bond_rr_gen_slave_id(bond); 3797 bond_xmit_slave_id(bond, skb, slave_id % slave_cnt); 3798 } else { 3799 bond_tx_drop(bond_dev, skb); 3800 } 3801 } 3802 3803 return NETDEV_TX_OK; 3804 } 3805 3806 /* In active-backup mode, we know that bond->curr_active_slave is always valid if 3807 * the bond has a usable interface. 3808 */ 3809 static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_dev) 3810 { 3811 struct bonding *bond = netdev_priv(bond_dev); 3812 struct slave *slave; 3813 3814 slave = rcu_dereference(bond->curr_active_slave); 3815 if (slave) 3816 bond_dev_queue_xmit(bond, skb, slave->dev); 3817 else 3818 bond_tx_drop(bond_dev, skb); 3819 3820 return NETDEV_TX_OK; 3821 } 3822 3823 /* Use this to update slave_array when (a) it's not appropriate to update 3824 * slave_array right away (note that update_slave_array() may sleep) 3825 * and / or (b) RTNL is not held. 3826 */ 3827 void bond_slave_arr_work_rearm(struct bonding *bond, unsigned long delay) 3828 { 3829 queue_delayed_work(bond->wq, &bond->slave_arr_work, delay); 3830 } 3831 3832 /* Slave array work handler. Holds only RTNL */ 3833 static void bond_slave_arr_handler(struct work_struct *work) 3834 { 3835 struct bonding *bond = container_of(work, struct bonding, 3836 slave_arr_work.work); 3837 int ret; 3838 3839 if (!rtnl_trylock()) 3840 goto err; 3841 3842 ret = bond_update_slave_arr(bond, NULL); 3843 rtnl_unlock(); 3844 if (ret) { 3845 pr_warn_ratelimited("Failed to update slave array from WT\n"); 3846 goto err; 3847 } 3848 return; 3849 3850 err: 3851 bond_slave_arr_work_rearm(bond, 1); 3852 } 3853 3854 /* Build the usable slaves array in control path for modes that use xmit-hash 3855 * to determine the slave interface - 3856 * (a) BOND_MODE_8023AD 3857 * (b) BOND_MODE_XOR 3858 * (c) BOND_MODE_TLB && tlb_dynamic_lb == 0 3859 * 3860 * The caller is expected to hold RTNL only and NO other lock! 3861 */ 3862 int bond_update_slave_arr(struct bonding *bond, struct slave *skipslave) 3863 { 3864 struct slave *slave; 3865 struct list_head *iter; 3866 struct bond_up_slave *new_arr, *old_arr; 3867 int agg_id = 0; 3868 int ret = 0; 3869 3870 #ifdef CONFIG_LOCKDEP 3871 WARN_ON(lockdep_is_held(&bond->mode_lock)); 3872 #endif 3873 3874 new_arr = kzalloc(offsetof(struct bond_up_slave, arr[bond->slave_cnt]), 3875 GFP_KERNEL); 3876 if (!new_arr) { 3877 ret = -ENOMEM; 3878 pr_err("Failed to build slave-array.\n"); 3879 goto out; 3880 } 3881 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 3882 struct ad_info ad_info; 3883 3884 if (bond_3ad_get_active_agg_info(bond, &ad_info)) { 3885 pr_debug("bond_3ad_get_active_agg_info failed\n"); 3886 kfree_rcu(new_arr, rcu); 3887 /* No active aggragator means it's not safe to use 3888 * the previous array. 3889 */ 3890 old_arr = rtnl_dereference(bond->slave_arr); 3891 if (old_arr) { 3892 RCU_INIT_POINTER(bond->slave_arr, NULL); 3893 kfree_rcu(old_arr, rcu); 3894 } 3895 goto out; 3896 } 3897 agg_id = ad_info.aggregator_id; 3898 } 3899 bond_for_each_slave(bond, slave, iter) { 3900 if (BOND_MODE(bond) == BOND_MODE_8023AD) { 3901 struct aggregator *agg; 3902 3903 agg = SLAVE_AD_INFO(slave)->port.aggregator; 3904 if (!agg || agg->aggregator_identifier != agg_id) 3905 continue; 3906 } 3907 if (!bond_slave_can_tx(slave)) 3908 continue; 3909 if (skipslave == slave) 3910 continue; 3911 new_arr->arr[new_arr->count++] = slave; 3912 } 3913 3914 old_arr = rtnl_dereference(bond->slave_arr); 3915 rcu_assign_pointer(bond->slave_arr, new_arr); 3916 if (old_arr) 3917 kfree_rcu(old_arr, rcu); 3918 out: 3919 if (ret != 0 && skipslave) { 3920 int idx; 3921 3922 /* Rare situation where caller has asked to skip a specific 3923 * slave but allocation failed (most likely!). BTW this is 3924 * only possible when the call is initiated from 3925 * __bond_release_one(). In this situation; overwrite the 3926 * skipslave entry in the array with the last entry from the 3927 * array to avoid a situation where the xmit path may choose 3928 * this to-be-skipped slave to send a packet out. 3929 */ 3930 old_arr = rtnl_dereference(bond->slave_arr); 3931 for (idx = 0; idx < old_arr->count; idx++) { 3932 if (skipslave == old_arr->arr[idx]) { 3933 old_arr->arr[idx] = 3934 old_arr->arr[old_arr->count-1]; 3935 old_arr->count--; 3936 break; 3937 } 3938 } 3939 } 3940 return ret; 3941 } 3942 3943 /* Use this Xmit function for 3AD as well as XOR modes. The current 3944 * usable slave array is formed in the control path. The xmit function 3945 * just calculates hash and sends the packet out. 3946 */ 3947 static int bond_3ad_xor_xmit(struct sk_buff *skb, struct net_device *dev) 3948 { 3949 struct bonding *bond = netdev_priv(dev); 3950 struct slave *slave; 3951 struct bond_up_slave *slaves; 3952 unsigned int count; 3953 3954 slaves = rcu_dereference(bond->slave_arr); 3955 count = slaves ? ACCESS_ONCE(slaves->count) : 0; 3956 if (likely(count)) { 3957 slave = slaves->arr[bond_xmit_hash(bond, skb) % count]; 3958 bond_dev_queue_xmit(bond, skb, slave->dev); 3959 } else { 3960 bond_tx_drop(dev, skb); 3961 } 3962 3963 return NETDEV_TX_OK; 3964 } 3965 3966 /* in broadcast mode, we send everything to all usable interfaces. */ 3967 static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev) 3968 { 3969 struct bonding *bond = netdev_priv(bond_dev); 3970 struct slave *slave = NULL; 3971 struct list_head *iter; 3972 3973 bond_for_each_slave_rcu(bond, slave, iter) { 3974 if (bond_is_last_slave(bond, slave)) 3975 break; 3976 if (bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) { 3977 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 3978 3979 if (!skb2) { 3980 net_err_ratelimited("%s: Error: %s: skb_clone() failed\n", 3981 bond_dev->name, __func__); 3982 continue; 3983 } 3984 bond_dev_queue_xmit(bond, skb2, slave->dev); 3985 } 3986 } 3987 if (slave && bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) 3988 bond_dev_queue_xmit(bond, skb, slave->dev); 3989 else 3990 bond_tx_drop(bond_dev, skb); 3991 3992 return NETDEV_TX_OK; 3993 } 3994 3995 /*------------------------- Device initialization ---------------------------*/ 3996 3997 /* Lookup the slave that corresponds to a qid */ 3998 static inline int bond_slave_override(struct bonding *bond, 3999 struct sk_buff *skb) 4000 { 4001 struct slave *slave = NULL; 4002 struct list_head *iter; 4003 4004 if (!skb->queue_mapping) 4005 return 1; 4006 4007 /* Find out if any slaves have the same mapping as this skb. */ 4008 bond_for_each_slave_rcu(bond, slave, iter) { 4009 if (slave->queue_id == skb->queue_mapping) { 4010 if (bond_slave_is_up(slave) && 4011 slave->link == BOND_LINK_UP) { 4012 bond_dev_queue_xmit(bond, skb, slave->dev); 4013 return 0; 4014 } 4015 /* If the slave isn't UP, use default transmit policy. */ 4016 break; 4017 } 4018 } 4019 4020 return 1; 4021 } 4022 4023 4024 static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb, 4025 void *accel_priv, select_queue_fallback_t fallback) 4026 { 4027 /* This helper function exists to help dev_pick_tx get the correct 4028 * destination queue. Using a helper function skips a call to 4029 * skb_tx_hash and will put the skbs in the queue we expect on their 4030 * way down to the bonding driver. 4031 */ 4032 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0; 4033 4034 /* Save the original txq to restore before passing to the driver */ 4035 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping; 4036 4037 if (unlikely(txq >= dev->real_num_tx_queues)) { 4038 do { 4039 txq -= dev->real_num_tx_queues; 4040 } while (txq >= dev->real_num_tx_queues); 4041 } 4042 return txq; 4043 } 4044 4045 static netdev_tx_t __bond_start_xmit(struct sk_buff *skb, struct net_device *dev) 4046 { 4047 struct bonding *bond = netdev_priv(dev); 4048 4049 if (bond_should_override_tx_queue(bond) && 4050 !bond_slave_override(bond, skb)) 4051 return NETDEV_TX_OK; 4052 4053 switch (BOND_MODE(bond)) { 4054 case BOND_MODE_ROUNDROBIN: 4055 return bond_xmit_roundrobin(skb, dev); 4056 case BOND_MODE_ACTIVEBACKUP: 4057 return bond_xmit_activebackup(skb, dev); 4058 case BOND_MODE_8023AD: 4059 case BOND_MODE_XOR: 4060 return bond_3ad_xor_xmit(skb, dev); 4061 case BOND_MODE_BROADCAST: 4062 return bond_xmit_broadcast(skb, dev); 4063 case BOND_MODE_ALB: 4064 return bond_alb_xmit(skb, dev); 4065 case BOND_MODE_TLB: 4066 return bond_tlb_xmit(skb, dev); 4067 default: 4068 /* Should never happen, mode already checked */ 4069 netdev_err(dev, "Unknown bonding mode %d\n", BOND_MODE(bond)); 4070 WARN_ON_ONCE(1); 4071 bond_tx_drop(dev, skb); 4072 return NETDEV_TX_OK; 4073 } 4074 } 4075 4076 static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev) 4077 { 4078 struct bonding *bond = netdev_priv(dev); 4079 netdev_tx_t ret = NETDEV_TX_OK; 4080 4081 /* If we risk deadlock from transmitting this in the 4082 * netpoll path, tell netpoll to queue the frame for later tx 4083 */ 4084 if (unlikely(is_netpoll_tx_blocked(dev))) 4085 return NETDEV_TX_BUSY; 4086 4087 rcu_read_lock(); 4088 if (bond_has_slaves(bond)) 4089 ret = __bond_start_xmit(skb, dev); 4090 else 4091 bond_tx_drop(dev, skb); 4092 rcu_read_unlock(); 4093 4094 return ret; 4095 } 4096 4097 static int bond_ethtool_get_link_ksettings(struct net_device *bond_dev, 4098 struct ethtool_link_ksettings *cmd) 4099 { 4100 struct bonding *bond = netdev_priv(bond_dev); 4101 unsigned long speed = 0; 4102 struct list_head *iter; 4103 struct slave *slave; 4104 4105 cmd->base.duplex = DUPLEX_UNKNOWN; 4106 cmd->base.port = PORT_OTHER; 4107 4108 /* Since bond_slave_can_tx returns false for all inactive or down slaves, we 4109 * do not need to check mode. Though link speed might not represent 4110 * the true receive or transmit bandwidth (not all modes are symmetric) 4111 * this is an accurate maximum. 4112 */ 4113 bond_for_each_slave(bond, slave, iter) { 4114 if (bond_slave_can_tx(slave)) { 4115 if (slave->speed != SPEED_UNKNOWN) 4116 speed += slave->speed; 4117 if (cmd->base.duplex == DUPLEX_UNKNOWN && 4118 slave->duplex != DUPLEX_UNKNOWN) 4119 cmd->base.duplex = slave->duplex; 4120 } 4121 } 4122 cmd->base.speed = speed ? : SPEED_UNKNOWN; 4123 4124 return 0; 4125 } 4126 4127 static void bond_ethtool_get_drvinfo(struct net_device *bond_dev, 4128 struct ethtool_drvinfo *drvinfo) 4129 { 4130 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver)); 4131 strlcpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version)); 4132 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "%d", 4133 BOND_ABI_VERSION); 4134 } 4135 4136 static const struct ethtool_ops bond_ethtool_ops = { 4137 .get_drvinfo = bond_ethtool_get_drvinfo, 4138 .get_link = ethtool_op_get_link, 4139 .get_link_ksettings = bond_ethtool_get_link_ksettings, 4140 }; 4141 4142 static const struct net_device_ops bond_netdev_ops = { 4143 .ndo_init = bond_init, 4144 .ndo_uninit = bond_uninit, 4145 .ndo_open = bond_open, 4146 .ndo_stop = bond_close, 4147 .ndo_start_xmit = bond_start_xmit, 4148 .ndo_select_queue = bond_select_queue, 4149 .ndo_get_stats64 = bond_get_stats, 4150 .ndo_do_ioctl = bond_do_ioctl, 4151 .ndo_change_rx_flags = bond_change_rx_flags, 4152 .ndo_set_rx_mode = bond_set_rx_mode, 4153 .ndo_change_mtu = bond_change_mtu, 4154 .ndo_set_mac_address = bond_set_mac_address, 4155 .ndo_neigh_setup = bond_neigh_setup, 4156 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid, 4157 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid, 4158 #ifdef CONFIG_NET_POLL_CONTROLLER 4159 .ndo_netpoll_setup = bond_netpoll_setup, 4160 .ndo_netpoll_cleanup = bond_netpoll_cleanup, 4161 .ndo_poll_controller = bond_poll_controller, 4162 #endif 4163 .ndo_add_slave = bond_enslave, 4164 .ndo_del_slave = bond_release, 4165 .ndo_fix_features = bond_fix_features, 4166 .ndo_bridge_setlink = switchdev_port_bridge_setlink, 4167 .ndo_bridge_getlink = switchdev_port_bridge_getlink, 4168 .ndo_bridge_dellink = switchdev_port_bridge_dellink, 4169 .ndo_fdb_add = switchdev_port_fdb_add, 4170 .ndo_fdb_del = switchdev_port_fdb_del, 4171 .ndo_fdb_dump = switchdev_port_fdb_dump, 4172 .ndo_features_check = passthru_features_check, 4173 }; 4174 4175 static const struct device_type bond_type = { 4176 .name = "bond", 4177 }; 4178 4179 static void bond_destructor(struct net_device *bond_dev) 4180 { 4181 struct bonding *bond = netdev_priv(bond_dev); 4182 if (bond->wq) 4183 destroy_workqueue(bond->wq); 4184 free_netdev(bond_dev); 4185 } 4186 4187 void bond_setup(struct net_device *bond_dev) 4188 { 4189 struct bonding *bond = netdev_priv(bond_dev); 4190 4191 spin_lock_init(&bond->mode_lock); 4192 spin_lock_init(&bond->stats_lock); 4193 bond->params = bonding_defaults; 4194 4195 /* Initialize pointers */ 4196 bond->dev = bond_dev; 4197 4198 /* Initialize the device entry points */ 4199 ether_setup(bond_dev); 4200 bond_dev->max_mtu = ETH_MAX_MTU; 4201 bond_dev->netdev_ops = &bond_netdev_ops; 4202 bond_dev->ethtool_ops = &bond_ethtool_ops; 4203 4204 bond_dev->destructor = bond_destructor; 4205 4206 SET_NETDEV_DEVTYPE(bond_dev, &bond_type); 4207 4208 /* Initialize the device options */ 4209 bond_dev->flags |= IFF_MASTER; 4210 bond_dev->priv_flags |= IFF_BONDING | IFF_UNICAST_FLT | IFF_NO_QUEUE; 4211 bond_dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING); 4212 4213 /* don't acquire bond device's netif_tx_lock when transmitting */ 4214 bond_dev->features |= NETIF_F_LLTX; 4215 4216 /* By default, we declare the bond to be fully 4217 * VLAN hardware accelerated capable. Special 4218 * care is taken in the various xmit functions 4219 * when there are slaves that are not hw accel 4220 * capable 4221 */ 4222 4223 /* Don't allow bond devices to change network namespaces. */ 4224 bond_dev->features |= NETIF_F_NETNS_LOCAL; 4225 4226 bond_dev->hw_features = BOND_VLAN_FEATURES | 4227 NETIF_F_HW_VLAN_CTAG_TX | 4228 NETIF_F_HW_VLAN_CTAG_RX | 4229 NETIF_F_HW_VLAN_CTAG_FILTER; 4230 4231 bond_dev->hw_features |= NETIF_F_GSO_ENCAP_ALL; 4232 bond_dev->features |= bond_dev->hw_features; 4233 } 4234 4235 /* Destroy a bonding device. 4236 * Must be under rtnl_lock when this function is called. 4237 */ 4238 static void bond_uninit(struct net_device *bond_dev) 4239 { 4240 struct bonding *bond = netdev_priv(bond_dev); 4241 struct list_head *iter; 4242 struct slave *slave; 4243 struct bond_up_slave *arr; 4244 4245 bond_netpoll_cleanup(bond_dev); 4246 4247 /* Release the bonded slaves */ 4248 bond_for_each_slave(bond, slave, iter) 4249 __bond_release_one(bond_dev, slave->dev, true); 4250 netdev_info(bond_dev, "Released all slaves\n"); 4251 4252 arr = rtnl_dereference(bond->slave_arr); 4253 if (arr) { 4254 RCU_INIT_POINTER(bond->slave_arr, NULL); 4255 kfree_rcu(arr, rcu); 4256 } 4257 4258 list_del(&bond->bond_list); 4259 4260 bond_debug_unregister(bond); 4261 } 4262 4263 /*------------------------- Module initialization ---------------------------*/ 4264 4265 static int bond_check_params(struct bond_params *params) 4266 { 4267 int arp_validate_value, fail_over_mac_value, primary_reselect_value, i; 4268 struct bond_opt_value newval; 4269 const struct bond_opt_value *valptr; 4270 int arp_all_targets_value; 4271 u16 ad_actor_sys_prio = 0; 4272 u16 ad_user_port_key = 0; 4273 __be32 arp_target[BOND_MAX_ARP_TARGETS]; 4274 int arp_ip_count; 4275 int bond_mode = BOND_MODE_ROUNDROBIN; 4276 int xmit_hashtype = BOND_XMIT_POLICY_LAYER2; 4277 int lacp_fast = 0; 4278 int tlb_dynamic_lb = 0; 4279 4280 /* Convert string parameters. */ 4281 if (mode) { 4282 bond_opt_initstr(&newval, mode); 4283 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_MODE), &newval); 4284 if (!valptr) { 4285 pr_err("Error: Invalid bonding mode \"%s\"\n", mode); 4286 return -EINVAL; 4287 } 4288 bond_mode = valptr->value; 4289 } 4290 4291 if (xmit_hash_policy) { 4292 if ((bond_mode != BOND_MODE_XOR) && 4293 (bond_mode != BOND_MODE_8023AD) && 4294 (bond_mode != BOND_MODE_TLB)) { 4295 pr_info("xmit_hash_policy param is irrelevant in mode %s\n", 4296 bond_mode_name(bond_mode)); 4297 } else { 4298 bond_opt_initstr(&newval, xmit_hash_policy); 4299 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_XMIT_HASH), 4300 &newval); 4301 if (!valptr) { 4302 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n", 4303 xmit_hash_policy); 4304 return -EINVAL; 4305 } 4306 xmit_hashtype = valptr->value; 4307 } 4308 } 4309 4310 if (lacp_rate) { 4311 if (bond_mode != BOND_MODE_8023AD) { 4312 pr_info("lacp_rate param is irrelevant in mode %s\n", 4313 bond_mode_name(bond_mode)); 4314 } else { 4315 bond_opt_initstr(&newval, lacp_rate); 4316 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_LACP_RATE), 4317 &newval); 4318 if (!valptr) { 4319 pr_err("Error: Invalid lacp rate \"%s\"\n", 4320 lacp_rate); 4321 return -EINVAL; 4322 } 4323 lacp_fast = valptr->value; 4324 } 4325 } 4326 4327 if (ad_select) { 4328 bond_opt_initstr(&newval, ad_select); 4329 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_SELECT), 4330 &newval); 4331 if (!valptr) { 4332 pr_err("Error: Invalid ad_select \"%s\"\n", ad_select); 4333 return -EINVAL; 4334 } 4335 params->ad_select = valptr->value; 4336 if (bond_mode != BOND_MODE_8023AD) 4337 pr_warn("ad_select param only affects 802.3ad mode\n"); 4338 } else { 4339 params->ad_select = BOND_AD_STABLE; 4340 } 4341 4342 if (max_bonds < 0) { 4343 pr_warn("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n", 4344 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS); 4345 max_bonds = BOND_DEFAULT_MAX_BONDS; 4346 } 4347 4348 if (miimon < 0) { 4349 pr_warn("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to 0\n", 4350 miimon, INT_MAX); 4351 miimon = 0; 4352 } 4353 4354 if (updelay < 0) { 4355 pr_warn("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n", 4356 updelay, INT_MAX); 4357 updelay = 0; 4358 } 4359 4360 if (downdelay < 0) { 4361 pr_warn("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n", 4362 downdelay, INT_MAX); 4363 downdelay = 0; 4364 } 4365 4366 if ((use_carrier != 0) && (use_carrier != 1)) { 4367 pr_warn("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n", 4368 use_carrier); 4369 use_carrier = 1; 4370 } 4371 4372 if (num_peer_notif < 0 || num_peer_notif > 255) { 4373 pr_warn("Warning: num_grat_arp/num_unsol_na (%d) not in range 0-255 so it was reset to 1\n", 4374 num_peer_notif); 4375 num_peer_notif = 1; 4376 } 4377 4378 /* reset values for 802.3ad/TLB/ALB */ 4379 if (!bond_mode_uses_arp(bond_mode)) { 4380 if (!miimon) { 4381 pr_warn("Warning: miimon must be specified, otherwise bonding will not detect link failure, speed and duplex which are essential for 802.3ad operation\n"); 4382 pr_warn("Forcing miimon to 100msec\n"); 4383 miimon = BOND_DEFAULT_MIIMON; 4384 } 4385 } 4386 4387 if (tx_queues < 1 || tx_queues > 255) { 4388 pr_warn("Warning: tx_queues (%d) should be between 1 and 255, resetting to %d\n", 4389 tx_queues, BOND_DEFAULT_TX_QUEUES); 4390 tx_queues = BOND_DEFAULT_TX_QUEUES; 4391 } 4392 4393 if ((all_slaves_active != 0) && (all_slaves_active != 1)) { 4394 pr_warn("Warning: all_slaves_active module parameter (%d), not of valid value (0/1), so it was set to 0\n", 4395 all_slaves_active); 4396 all_slaves_active = 0; 4397 } 4398 4399 if (resend_igmp < 0 || resend_igmp > 255) { 4400 pr_warn("Warning: resend_igmp (%d) should be between 0 and 255, resetting to %d\n", 4401 resend_igmp, BOND_DEFAULT_RESEND_IGMP); 4402 resend_igmp = BOND_DEFAULT_RESEND_IGMP; 4403 } 4404 4405 bond_opt_initval(&newval, packets_per_slave); 4406 if (!bond_opt_parse(bond_opt_get(BOND_OPT_PACKETS_PER_SLAVE), &newval)) { 4407 pr_warn("Warning: packets_per_slave (%d) should be between 0 and %u resetting to 1\n", 4408 packets_per_slave, USHRT_MAX); 4409 packets_per_slave = 1; 4410 } 4411 4412 if (bond_mode == BOND_MODE_ALB) { 4413 pr_notice("In ALB mode you might experience client disconnections upon reconnection of a link if the bonding module updelay parameter (%d msec) is incompatible with the forwarding delay time of the switch\n", 4414 updelay); 4415 } 4416 4417 if (!miimon) { 4418 if (updelay || downdelay) { 4419 /* just warn the user the up/down delay will have 4420 * no effect since miimon is zero... 4421 */ 4422 pr_warn("Warning: miimon module parameter not set and updelay (%d) or downdelay (%d) module parameter is set; updelay and downdelay have no effect unless miimon is set\n", 4423 updelay, downdelay); 4424 } 4425 } else { 4426 /* don't allow arp monitoring */ 4427 if (arp_interval) { 4428 pr_warn("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n", 4429 miimon, arp_interval); 4430 arp_interval = 0; 4431 } 4432 4433 if ((updelay % miimon) != 0) { 4434 pr_warn("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n", 4435 updelay, miimon, (updelay / miimon) * miimon); 4436 } 4437 4438 updelay /= miimon; 4439 4440 if ((downdelay % miimon) != 0) { 4441 pr_warn("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n", 4442 downdelay, miimon, 4443 (downdelay / miimon) * miimon); 4444 } 4445 4446 downdelay /= miimon; 4447 } 4448 4449 if (arp_interval < 0) { 4450 pr_warn("Warning: arp_interval module parameter (%d), not in range 0-%d, so it was reset to 0\n", 4451 arp_interval, INT_MAX); 4452 arp_interval = 0; 4453 } 4454 4455 for (arp_ip_count = 0, i = 0; 4456 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[i]; i++) { 4457 __be32 ip; 4458 4459 /* not a complete check, but good enough to catch mistakes */ 4460 if (!in4_pton(arp_ip_target[i], -1, (u8 *)&ip, -1, NULL) || 4461 !bond_is_ip_target_ok(ip)) { 4462 pr_warn("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n", 4463 arp_ip_target[i]); 4464 arp_interval = 0; 4465 } else { 4466 if (bond_get_targets_ip(arp_target, ip) == -1) 4467 arp_target[arp_ip_count++] = ip; 4468 else 4469 pr_warn("Warning: duplicate address %pI4 in arp_ip_target, skipping\n", 4470 &ip); 4471 } 4472 } 4473 4474 if (arp_interval && !arp_ip_count) { 4475 /* don't allow arping if no arp_ip_target given... */ 4476 pr_warn("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n", 4477 arp_interval); 4478 arp_interval = 0; 4479 } 4480 4481 if (arp_validate) { 4482 if (!arp_interval) { 4483 pr_err("arp_validate requires arp_interval\n"); 4484 return -EINVAL; 4485 } 4486 4487 bond_opt_initstr(&newval, arp_validate); 4488 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_VALIDATE), 4489 &newval); 4490 if (!valptr) { 4491 pr_err("Error: invalid arp_validate \"%s\"\n", 4492 arp_validate); 4493 return -EINVAL; 4494 } 4495 arp_validate_value = valptr->value; 4496 } else { 4497 arp_validate_value = 0; 4498 } 4499 4500 arp_all_targets_value = 0; 4501 if (arp_all_targets) { 4502 bond_opt_initstr(&newval, arp_all_targets); 4503 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_ALL_TARGETS), 4504 &newval); 4505 if (!valptr) { 4506 pr_err("Error: invalid arp_all_targets_value \"%s\"\n", 4507 arp_all_targets); 4508 arp_all_targets_value = 0; 4509 } else { 4510 arp_all_targets_value = valptr->value; 4511 } 4512 } 4513 4514 if (miimon) { 4515 pr_info("MII link monitoring set to %d ms\n", miimon); 4516 } else if (arp_interval) { 4517 valptr = bond_opt_get_val(BOND_OPT_ARP_VALIDATE, 4518 arp_validate_value); 4519 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):", 4520 arp_interval, valptr->string, arp_ip_count); 4521 4522 for (i = 0; i < arp_ip_count; i++) 4523 pr_cont(" %s", arp_ip_target[i]); 4524 4525 pr_cont("\n"); 4526 4527 } else if (max_bonds) { 4528 /* miimon and arp_interval not set, we need one so things 4529 * work as expected, see bonding.txt for details 4530 */ 4531 pr_debug("Warning: either miimon or arp_interval and arp_ip_target module parameters must be specified, otherwise bonding will not detect link failures! see bonding.txt for details\n"); 4532 } 4533 4534 if (primary && !bond_mode_uses_primary(bond_mode)) { 4535 /* currently, using a primary only makes sense 4536 * in active backup, TLB or ALB modes 4537 */ 4538 pr_warn("Warning: %s primary device specified but has no effect in %s mode\n", 4539 primary, bond_mode_name(bond_mode)); 4540 primary = NULL; 4541 } 4542 4543 if (primary && primary_reselect) { 4544 bond_opt_initstr(&newval, primary_reselect); 4545 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_PRIMARY_RESELECT), 4546 &newval); 4547 if (!valptr) { 4548 pr_err("Error: Invalid primary_reselect \"%s\"\n", 4549 primary_reselect); 4550 return -EINVAL; 4551 } 4552 primary_reselect_value = valptr->value; 4553 } else { 4554 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS; 4555 } 4556 4557 if (fail_over_mac) { 4558 bond_opt_initstr(&newval, fail_over_mac); 4559 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_FAIL_OVER_MAC), 4560 &newval); 4561 if (!valptr) { 4562 pr_err("Error: invalid fail_over_mac \"%s\"\n", 4563 fail_over_mac); 4564 return -EINVAL; 4565 } 4566 fail_over_mac_value = valptr->value; 4567 if (bond_mode != BOND_MODE_ACTIVEBACKUP) 4568 pr_warn("Warning: fail_over_mac only affects active-backup mode\n"); 4569 } else { 4570 fail_over_mac_value = BOND_FOM_NONE; 4571 } 4572 4573 bond_opt_initstr(&newval, "default"); 4574 valptr = bond_opt_parse( 4575 bond_opt_get(BOND_OPT_AD_ACTOR_SYS_PRIO), 4576 &newval); 4577 if (!valptr) { 4578 pr_err("Error: No ad_actor_sys_prio default value"); 4579 return -EINVAL; 4580 } 4581 ad_actor_sys_prio = valptr->value; 4582 4583 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_USER_PORT_KEY), 4584 &newval); 4585 if (!valptr) { 4586 pr_err("Error: No ad_user_port_key default value"); 4587 return -EINVAL; 4588 } 4589 ad_user_port_key = valptr->value; 4590 4591 if (bond_mode == BOND_MODE_TLB) { 4592 bond_opt_initstr(&newval, "default"); 4593 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_TLB_DYNAMIC_LB), 4594 &newval); 4595 if (!valptr) { 4596 pr_err("Error: No tlb_dynamic_lb default value"); 4597 return -EINVAL; 4598 } 4599 tlb_dynamic_lb = valptr->value; 4600 } 4601 4602 if (lp_interval == 0) { 4603 pr_warn("Warning: ip_interval must be between 1 and %d, so it was reset to %d\n", 4604 INT_MAX, BOND_ALB_DEFAULT_LP_INTERVAL); 4605 lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL; 4606 } 4607 4608 /* fill params struct with the proper values */ 4609 params->mode = bond_mode; 4610 params->xmit_policy = xmit_hashtype; 4611 params->miimon = miimon; 4612 params->num_peer_notif = num_peer_notif; 4613 params->arp_interval = arp_interval; 4614 params->arp_validate = arp_validate_value; 4615 params->arp_all_targets = arp_all_targets_value; 4616 params->updelay = updelay; 4617 params->downdelay = downdelay; 4618 params->use_carrier = use_carrier; 4619 params->lacp_fast = lacp_fast; 4620 params->primary[0] = 0; 4621 params->primary_reselect = primary_reselect_value; 4622 params->fail_over_mac = fail_over_mac_value; 4623 params->tx_queues = tx_queues; 4624 params->all_slaves_active = all_slaves_active; 4625 params->resend_igmp = resend_igmp; 4626 params->min_links = min_links; 4627 params->lp_interval = lp_interval; 4628 params->packets_per_slave = packets_per_slave; 4629 params->tlb_dynamic_lb = tlb_dynamic_lb; 4630 params->ad_actor_sys_prio = ad_actor_sys_prio; 4631 eth_zero_addr(params->ad_actor_system); 4632 params->ad_user_port_key = ad_user_port_key; 4633 if (packets_per_slave > 0) { 4634 params->reciprocal_packets_per_slave = 4635 reciprocal_value(packets_per_slave); 4636 } else { 4637 /* reciprocal_packets_per_slave is unused if 4638 * packets_per_slave is 0 or 1, just initialize it 4639 */ 4640 params->reciprocal_packets_per_slave = 4641 (struct reciprocal_value) { 0 }; 4642 } 4643 4644 if (primary) { 4645 strncpy(params->primary, primary, IFNAMSIZ); 4646 params->primary[IFNAMSIZ - 1] = 0; 4647 } 4648 4649 memcpy(params->arp_targets, arp_target, sizeof(arp_target)); 4650 4651 return 0; 4652 } 4653 4654 /* Called from registration process */ 4655 static int bond_init(struct net_device *bond_dev) 4656 { 4657 struct bonding *bond = netdev_priv(bond_dev); 4658 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id); 4659 4660 netdev_dbg(bond_dev, "Begin bond_init\n"); 4661 4662 bond->wq = alloc_ordered_workqueue(bond_dev->name, WQ_MEM_RECLAIM); 4663 if (!bond->wq) 4664 return -ENOMEM; 4665 4666 netdev_lockdep_set_classes(bond_dev); 4667 4668 list_add_tail(&bond->bond_list, &bn->dev_list); 4669 4670 bond_prepare_sysfs_group(bond); 4671 4672 bond_debug_register(bond); 4673 4674 /* Ensure valid dev_addr */ 4675 if (is_zero_ether_addr(bond_dev->dev_addr) && 4676 bond_dev->addr_assign_type == NET_ADDR_PERM) 4677 eth_hw_addr_random(bond_dev); 4678 4679 return 0; 4680 } 4681 4682 unsigned int bond_get_num_tx_queues(void) 4683 { 4684 return tx_queues; 4685 } 4686 4687 /* Create a new bond based on the specified name and bonding parameters. 4688 * If name is NULL, obtain a suitable "bond%d" name for us. 4689 * Caller must NOT hold rtnl_lock; we need to release it here before we 4690 * set up our sysfs entries. 4691 */ 4692 int bond_create(struct net *net, const char *name) 4693 { 4694 struct net_device *bond_dev; 4695 struct bonding *bond; 4696 struct alb_bond_info *bond_info; 4697 int res; 4698 4699 rtnl_lock(); 4700 4701 bond_dev = alloc_netdev_mq(sizeof(struct bonding), 4702 name ? name : "bond%d", NET_NAME_UNKNOWN, 4703 bond_setup, tx_queues); 4704 if (!bond_dev) { 4705 pr_err("%s: eek! can't alloc netdev!\n", name); 4706 rtnl_unlock(); 4707 return -ENOMEM; 4708 } 4709 4710 /* 4711 * Initialize rx_hashtbl_used_head to RLB_NULL_INDEX. 4712 * It is set to 0 by default which is wrong. 4713 */ 4714 bond = netdev_priv(bond_dev); 4715 bond_info = &(BOND_ALB_INFO(bond)); 4716 bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX; 4717 4718 dev_net_set(bond_dev, net); 4719 bond_dev->rtnl_link_ops = &bond_link_ops; 4720 4721 res = register_netdevice(bond_dev); 4722 4723 netif_carrier_off(bond_dev); 4724 4725 bond_work_init_all(bond); 4726 4727 rtnl_unlock(); 4728 if (res < 0) 4729 bond_destructor(bond_dev); 4730 return res; 4731 } 4732 4733 static int __net_init bond_net_init(struct net *net) 4734 { 4735 struct bond_net *bn = net_generic(net, bond_net_id); 4736 4737 bn->net = net; 4738 INIT_LIST_HEAD(&bn->dev_list); 4739 4740 bond_create_proc_dir(bn); 4741 bond_create_sysfs(bn); 4742 4743 return 0; 4744 } 4745 4746 static void __net_exit bond_net_exit(struct net *net) 4747 { 4748 struct bond_net *bn = net_generic(net, bond_net_id); 4749 struct bonding *bond, *tmp_bond; 4750 LIST_HEAD(list); 4751 4752 bond_destroy_sysfs(bn); 4753 4754 /* Kill off any bonds created after unregistering bond rtnl ops */ 4755 rtnl_lock(); 4756 list_for_each_entry_safe(bond, tmp_bond, &bn->dev_list, bond_list) 4757 unregister_netdevice_queue(bond->dev, &list); 4758 unregister_netdevice_many(&list); 4759 rtnl_unlock(); 4760 4761 bond_destroy_proc_dir(bn); 4762 } 4763 4764 static struct pernet_operations bond_net_ops = { 4765 .init = bond_net_init, 4766 .exit = bond_net_exit, 4767 .id = &bond_net_id, 4768 .size = sizeof(struct bond_net), 4769 }; 4770 4771 static int __init bonding_init(void) 4772 { 4773 int i; 4774 int res; 4775 4776 pr_info("%s", bond_version); 4777 4778 res = bond_check_params(&bonding_defaults); 4779 if (res) 4780 goto out; 4781 4782 res = register_pernet_subsys(&bond_net_ops); 4783 if (res) 4784 goto out; 4785 4786 res = bond_netlink_init(); 4787 if (res) 4788 goto err_link; 4789 4790 bond_create_debugfs(); 4791 4792 for (i = 0; i < max_bonds; i++) { 4793 res = bond_create(&init_net, NULL); 4794 if (res) 4795 goto err; 4796 } 4797 4798 register_netdevice_notifier(&bond_netdev_notifier); 4799 out: 4800 return res; 4801 err: 4802 bond_destroy_debugfs(); 4803 bond_netlink_fini(); 4804 err_link: 4805 unregister_pernet_subsys(&bond_net_ops); 4806 goto out; 4807 4808 } 4809 4810 static void __exit bonding_exit(void) 4811 { 4812 unregister_netdevice_notifier(&bond_netdev_notifier); 4813 4814 bond_destroy_debugfs(); 4815 4816 bond_netlink_fini(); 4817 unregister_pernet_subsys(&bond_net_ops); 4818 4819 #ifdef CONFIG_NET_POLL_CONTROLLER 4820 /* Make sure we don't have an imbalance on our netpoll blocking */ 4821 WARN_ON(atomic_read(&netpoll_block_tx)); 4822 #endif 4823 } 4824 4825 module_init(bonding_init); 4826 module_exit(bonding_exit); 4827 MODULE_LICENSE("GPL"); 4828 MODULE_VERSION(DRV_VERSION); 4829 MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION); 4830 MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others"); 4831