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